| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\CryptoBridge\Crypto; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use function mb_strlen; |
| 9 |
use const OPENSSL_ALGO_MD4; |
| 10 |
use const OPENSSL_ALGO_MD5; |
| 11 |
use const OPENSSL_ALGO_SHA1; |
| 12 |
use const OPENSSL_ALGO_SHA224; |
| 13 |
use const OPENSSL_ALGO_SHA256; |
| 14 |
use const OPENSSL_ALGO_SHA384; |
| 15 |
use const OPENSSL_ALGO_SHA512; |
| 16 |
use const OPENSSL_RAW_DATA; |
| 17 |
use const OPENSSL_ZERO_PADDING; |
| 18 |
use RuntimeException; |
| 19 |
use SpomkyLabs\Pki\CryptoBridge\Crypto; |
| 20 |
use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier; |
| 21 |
use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Cipher\BlockCipherAlgorithmIdentifier; |
| 22 |
use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Cipher\CipherAlgorithmIdentifier; |
| 23 |
use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Cipher\RC2CBCAlgorithmIdentifier; |
| 24 |
use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier; |
| 25 |
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PrivateKeyInfo; |
| 26 |
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PublicKeyInfo; |
| 27 |
use SpomkyLabs\Pki\CryptoTypes\Signature\Signature; |
| 28 |
use function sprintf; |
| 29 |
use UnexpectedValueException; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
final class OpenSSLCrypto extends Crypto |
| 35 |
{ |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private const MAP_DIGEST_OID = [ |
| 44 |
AlgorithmIdentifier::OID_MD4_WITH_RSA_ENCRYPTION => OPENSSL_ALGO_MD4, |
| 45 |
AlgorithmIdentifier::OID_MD5_WITH_RSA_ENCRYPTION => OPENSSL_ALGO_MD5, |
| 46 |
AlgorithmIdentifier::OID_SHA1_WITH_RSA_ENCRYPTION => OPENSSL_ALGO_SHA1, |
| 47 |
AlgorithmIdentifier::OID_SHA224_WITH_RSA_ENCRYPTION => OPENSSL_ALGO_SHA224, |
| 48 |
AlgorithmIdentifier::OID_SHA256_WITH_RSA_ENCRYPTION => OPENSSL_ALGO_SHA256, |
| 49 |
AlgorithmIdentifier::OID_SHA384_WITH_RSA_ENCRYPTION => OPENSSL_ALGO_SHA384, |
| 50 |
AlgorithmIdentifier::OID_SHA512_WITH_RSA_ENCRYPTION => OPENSSL_ALGO_SHA512, |
| 51 |
AlgorithmIdentifier::OID_ECDSA_WITH_SHA1 => OPENSSL_ALGO_SHA1, |
| 52 |
AlgorithmIdentifier::OID_ECDSA_WITH_SHA224 => OPENSSL_ALGO_SHA224, |
| 53 |
AlgorithmIdentifier::OID_ECDSA_WITH_SHA256 => OPENSSL_ALGO_SHA256, |
| 54 |
AlgorithmIdentifier::OID_ECDSA_WITH_SHA384 => OPENSSL_ALGO_SHA384, |
| 55 |
AlgorithmIdentifier::OID_ECDSA_WITH_SHA512 => OPENSSL_ALGO_SHA512, |
| 56 |
]; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private const MAP_CIPHER_OID = [ |
| 66 |
AlgorithmIdentifier::OID_DES_CBC => 'des-cbc', |
| 67 |
AlgorithmIdentifier::OID_DES_EDE3_CBC => 'des-ede3-cbc', |
| 68 |
AlgorithmIdentifier::OID_AES_128_CBC => 'aes-128-cbc', |
| 69 |
AlgorithmIdentifier::OID_AES_192_CBC => 'aes-192-cbc', |
| 70 |
AlgorithmIdentifier::OID_AES_256_CBC => 'aes-256-cbc', |
| 71 |
]; |
| 72 |
|
| 73 |
public function sign( |
| 74 |
string $data, |
| 75 |
PrivateKeyInfo $privkey_info, |
| 76 |
SignatureAlgorithmIdentifier $algo |
| 77 |
): Signature { |
| 78 |
$this->_checkSignatureAlgoAndKey($algo, $privkey_info->algorithmIdentifier()); |
| 79 |
$result = openssl_sign($data, $signature, (string) $privkey_info->toPEM(), $this->_algoToDigest($algo)); |
| 80 |
if ($result === false) { |
| 81 |
throw new RuntimeException('openssl_sign() failed: ' . $this->_getLastError()); |
| 82 |
} |
| 83 |
return Signature::fromSignatureData($signature, $algo); |
| 84 |
} |
| 85 |
|
| 86 |
public function verify( |
| 87 |
string $data, |
| 88 |
Signature $signature, |
| 89 |
PublicKeyInfo $pubkey_info, |
| 90 |
SignatureAlgorithmIdentifier $algo |
| 91 |
): bool { |
| 92 |
$this->_checkSignatureAlgoAndKey($algo, $pubkey_info->algorithmIdentifier()); |
| 93 |
$result = openssl_verify( |
| 94 |
$data, |
| 95 |
$signature->bitString() |
| 96 |
->string(), |
| 97 |
(string) $pubkey_info->toPEM(), |
| 98 |
$this->_algoToDigest($algo) |
| 99 |
); |
| 100 |
if ($result === -1) { |
| 101 |
throw new RuntimeException('openssl_verify() failed: ' . $this->_getLastError()); |
| 102 |
} |
| 103 |
return $result === 1; |
| 104 |
} |
| 105 |
|
| 106 |
public function encrypt(string $data, string $key, CipherAlgorithmIdentifier $algo): string |
| 107 |
{ |
| 108 |
$this->_checkCipherKeySize($algo, $key); |
| 109 |
$iv = $algo->initializationVector(); |
| 110 |
$result = openssl_encrypt( |
| 111 |
$data, |
| 112 |
$this->_algoToCipher($algo), |
| 113 |
$key, |
| 114 |
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, |
| 115 |
$iv |
| 116 |
); |
| 117 |
if ($result === false) { |
| 118 |
throw new RuntimeException('openssl_encrypt() failed: ' . $this->_getLastError()); |
| 119 |
} |
| 120 |
return $result; |
| 121 |
} |
| 122 |
|
| 123 |
public function decrypt(string $data, string $key, CipherAlgorithmIdentifier $algo): string |
| 124 |
{ |
| 125 |
$this->_checkCipherKeySize($algo, $key); |
| 126 |
$iv = $algo->initializationVector(); |
| 127 |
$result = openssl_decrypt( |
| 128 |
$data, |
| 129 |
$this->_algoToCipher($algo), |
| 130 |
$key, |
| 131 |
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, |
| 132 |
$iv |
| 133 |
); |
| 134 |
if ($result === false) { |
| 135 |
throw new RuntimeException('openssl_decrypt() failed: ' . $this->_getLastError()); |
| 136 |
} |
| 137 |
return $result; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
protected function _checkCipherKeySize(CipherAlgorithmIdentifier $algo, string $key): void |
| 144 |
{ |
| 145 |
if ($algo instanceof BlockCipherAlgorithmIdentifier) { |
| 146 |
if (mb_strlen($key, '8bit') !== $algo->keySize()) { |
| 147 |
throw new UnexpectedValueException( |
| 148 |
sprintf( |
| 149 |
'Key length for %s must be %d, %d given.', |
| 150 |
$algo->name(), |
| 151 |
$algo->keySize(), |
| 152 |
mb_strlen($key, '8bit') |
| 153 |
) |
| 154 |
); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
protected function _getLastError(): ?string |
| 163 |
{ |
| 164 |
|
| 165 |
$msg = null; |
| 166 |
while (false !== ($err = openssl_error_string())) { |
| 167 |
$msg = $err; |
| 168 |
} |
| 169 |
return $msg; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
protected function _checkSignatureAlgoAndKey( |
| 179 |
SignatureAlgorithmIdentifier $sig_algo, |
| 180 |
AlgorithmIdentifier $key_algo |
| 181 |
): void { |
| 182 |
if (! $sig_algo->supportsKeyAlgorithm($key_algo)) { |
| 183 |
throw new UnexpectedValueException( |
| 184 |
sprintf( |
| 185 |
'Signature algorithm %s does not support key algorithm %s.', |
| 186 |
$sig_algo->name(), |
| 187 |
$key_algo->name() |
| 188 |
) |
| 189 |
); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
protected function _algoToDigest(SignatureAlgorithmIdentifier $algo): int |
| 197 |
{ |
| 198 |
$oid = $algo->oid(); |
| 199 |
if (! array_key_exists($oid, self::MAP_DIGEST_OID)) { |
| 200 |
throw new UnexpectedValueException(sprintf('Digest method %s not supported.', $algo->name())); |
| 201 |
} |
| 202 |
return self::MAP_DIGEST_OID[$oid]; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
protected function _algoToCipher(CipherAlgorithmIdentifier $algo): string |
| 209 |
{ |
| 210 |
$oid = $algo->oid(); |
| 211 |
if (array_key_exists($oid, self::MAP_CIPHER_OID)) { |
| 212 |
return self::MAP_CIPHER_OID[$oid]; |
| 213 |
} |
| 214 |
if ($oid === AlgorithmIdentifier::OID_RC2_CBC) { |
| 215 |
if (! $algo instanceof RC2CBCAlgorithmIdentifier) { |
| 216 |
throw new UnexpectedValueException('Not an RC2-CBC algorithm.'); |
| 217 |
} |
| 218 |
return $this->_rc2AlgoToCipher($algo); |
| 219 |
} |
| 220 |
throw new UnexpectedValueException(sprintf('Cipher method %s not supported.', $algo->name())); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
protected function _rc2AlgoToCipher(RC2CBCAlgorithmIdentifier $algo): string |
| 227 |
{ |
| 228 |
return match ($algo->effectiveKeyBits()) { |
| 229 |
128 => 'rc2-cbc', |
| 230 |
64 => 'rc2-64-cbc', |
| 231 |
40 => 'rc2-40-cbc', |
| 232 |
default => throw new UnexpectedValueException($algo->effectiveKeyBits() . ' bit RC2 not supported.'), |
| 233 |
}; |
| 234 |
} |
| 235 |
} |