@@ -9,99 +9,99 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | trait NumericDateClaim |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Constructor. |
|
| 14 | - * |
|
| 15 | - * @param int $timestamp Unix timestamp |
|
| 16 | - */ |
|
| 17 | - abstract public function __construct(int $timestamp); |
|
| 12 | + /** |
|
| 13 | + * Constructor. |
|
| 14 | + * |
|
| 15 | + * @param int $timestamp Unix timestamp |
|
| 16 | + */ |
|
| 17 | + abstract public function __construct(int $timestamp); |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Get the parameter value. |
|
| 21 | - * |
|
| 22 | - * @return string |
|
| 23 | - */ |
|
| 24 | - abstract public function value(); |
|
| 19 | + /** |
|
| 20 | + * Get the parameter value. |
|
| 21 | + * |
|
| 22 | + * @return string |
|
| 23 | + */ |
|
| 24 | + abstract public function value(); |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Initialize instance from date/time string. |
|
| 28 | - * |
|
| 29 | - * @param string $time `strtotime` compatible time string |
|
| 30 | - * @param string $tz Default timezone |
|
| 31 | - * |
|
| 32 | - * @throws \RuntimeException |
|
| 33 | - * |
|
| 34 | - * @return static |
|
| 35 | - */ |
|
| 36 | - public static function fromString(string $time, string $tz = 'UTC') |
|
| 37 | - { |
|
| 38 | - try { |
|
| 39 | - $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 40 | - return new static($dt->getTimestamp()); |
|
| 41 | - } catch (\Exception $e) { |
|
| 42 | - throw new \RuntimeException( |
|
| 43 | - 'Failed to create DateTime: ' . |
|
| 44 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 45 | - } |
|
| 46 | - } |
|
| 26 | + /** |
|
| 27 | + * Initialize instance from date/time string. |
|
| 28 | + * |
|
| 29 | + * @param string $time `strtotime` compatible time string |
|
| 30 | + * @param string $tz Default timezone |
|
| 31 | + * |
|
| 32 | + * @throws \RuntimeException |
|
| 33 | + * |
|
| 34 | + * @return static |
|
| 35 | + */ |
|
| 36 | + public static function fromString(string $time, string $tz = 'UTC') |
|
| 37 | + { |
|
| 38 | + try { |
|
| 39 | + $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 40 | + return new static($dt->getTimestamp()); |
|
| 41 | + } catch (\Exception $e) { |
|
| 42 | + throw new \RuntimeException( |
|
| 43 | + 'Failed to create DateTime: ' . |
|
| 44 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Get date as a unix timestamp. |
|
| 50 | - * |
|
| 51 | - * @return int |
|
| 52 | - */ |
|
| 53 | - public function timestamp(): int |
|
| 54 | - { |
|
| 55 | - return (int) $this->value(); |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Get date as a unix timestamp. |
|
| 50 | + * |
|
| 51 | + * @return int |
|
| 52 | + */ |
|
| 53 | + public function timestamp(): int |
|
| 54 | + { |
|
| 55 | + return (int) $this->value(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Get date as a datetime object. |
|
| 60 | - * |
|
| 61 | - * @param string $tz Timezone |
|
| 62 | - * |
|
| 63 | - * @throws \RuntimeException |
|
| 64 | - * |
|
| 65 | - * @return \DateTimeImmutable |
|
| 66 | - */ |
|
| 67 | - public function dateTime(string $tz = 'UTC'): \DateTimeImmutable |
|
| 68 | - { |
|
| 69 | - $dt = \DateTimeImmutable::createFromFormat('!U', strval($this->value()), |
|
| 70 | - self::_createTimeZone($tz)); |
|
| 71 | - if (false === $dt) { |
|
| 72 | - throw new \RuntimeException( |
|
| 73 | - 'Failed to create DateTime: ' . |
|
| 74 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 75 | - } |
|
| 76 | - return $dt; |
|
| 77 | - } |
|
| 58 | + /** |
|
| 59 | + * Get date as a datetime object. |
|
| 60 | + * |
|
| 61 | + * @param string $tz Timezone |
|
| 62 | + * |
|
| 63 | + * @throws \RuntimeException |
|
| 64 | + * |
|
| 65 | + * @return \DateTimeImmutable |
|
| 66 | + */ |
|
| 67 | + public function dateTime(string $tz = 'UTC'): \DateTimeImmutable |
|
| 68 | + { |
|
| 69 | + $dt = \DateTimeImmutable::createFromFormat('!U', strval($this->value()), |
|
| 70 | + self::_createTimeZone($tz)); |
|
| 71 | + if (false === $dt) { |
|
| 72 | + throw new \RuntimeException( |
|
| 73 | + 'Failed to create DateTime: ' . |
|
| 74 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 75 | + } |
|
| 76 | + return $dt; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Create DateTimeZone object from string. |
|
| 81 | - * |
|
| 82 | - * @param string $tz |
|
| 83 | - * |
|
| 84 | - * @throws \UnexpectedValueException |
|
| 85 | - * |
|
| 86 | - * @return \DateTimeZone |
|
| 87 | - */ |
|
| 88 | - private static function _createTimeZone(string $tz): \DateTimeZone |
|
| 89 | - { |
|
| 90 | - try { |
|
| 91 | - return new \DateTimeZone($tz); |
|
| 92 | - } catch (\Exception $e) { |
|
| 93 | - throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
| 94 | - } |
|
| 95 | - } |
|
| 79 | + /** |
|
| 80 | + * Create DateTimeZone object from string. |
|
| 81 | + * |
|
| 82 | + * @param string $tz |
|
| 83 | + * |
|
| 84 | + * @throws \UnexpectedValueException |
|
| 85 | + * |
|
| 86 | + * @return \DateTimeZone |
|
| 87 | + */ |
|
| 88 | + private static function _createTimeZone(string $tz): \DateTimeZone |
|
| 89 | + { |
|
| 90 | + try { |
|
| 91 | + return new \DateTimeZone($tz); |
|
| 92 | + } catch (\Exception $e) { |
|
| 93 | + throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Get last error caused by DateTimeImmutable. |
|
| 99 | - * |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - private static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 103 | - { |
|
| 104 | - $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
| 105 | - return implode(', ', $errors); |
|
| 106 | - } |
|
| 97 | + /** |
|
| 98 | + * Get last error caused by DateTimeImmutable. |
|
| 99 | + * |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + private static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 103 | + { |
|
| 104 | + $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
| 105 | + return implode(', ', $errors); |
|
| 106 | + } |
|
| 107 | 107 | } |
@@ -21,202 +21,202 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | abstract class RSAESKeyAlgorithm extends KeyManagementAlgorithm |
| 23 | 23 | { |
| 24 | - use RandomCEK; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Mapping from algorithm name to class name. |
|
| 28 | - * |
|
| 29 | - * @internal |
|
| 30 | - * |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - const MAP_ALGO_TO_CLASS = [ |
|
| 34 | - JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
|
| 35 | - JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class, |
|
| 36 | - ]; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Public key. |
|
| 40 | - * |
|
| 41 | - * @var RSAPublicKeyJWK |
|
| 42 | - */ |
|
| 43 | - protected $_publicKey; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Private key. |
|
| 47 | - * |
|
| 48 | - * @var null|RSAPrivateKeyJWK |
|
| 49 | - */ |
|
| 50 | - protected $_privateKey; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Constructor. |
|
| 54 | - * |
|
| 55 | - * Use `fromPublicKey` or `fromPrivateKey` instead! |
|
| 56 | - * |
|
| 57 | - * @param RSAPublicKeyJWK $pub_key RSA public key |
|
| 58 | - * @param RSAPrivateKeyJWK $priv_key Optional RSA private key |
|
| 59 | - */ |
|
| 60 | - protected function __construct(RSAPublicKeyJWK $pub_key, |
|
| 61 | - ?RSAPrivateKeyJWK $priv_key = null) |
|
| 62 | - { |
|
| 63 | - $this->_publicKey = $pub_key; |
|
| 64 | - $this->_privateKey = $priv_key; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Initialize from JWK. |
|
| 69 | - * |
|
| 70 | - * @param JWK $jwk |
|
| 71 | - * @param Header $header |
|
| 72 | - * |
|
| 73 | - * @throws \UnexpectedValueException |
|
| 74 | - * |
|
| 75 | - * @return self |
|
| 76 | - */ |
|
| 77 | - public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
| 78 | - { |
|
| 79 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 80 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 81 | - throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 82 | - } |
|
| 83 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 84 | - if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 85 | - return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
| 86 | - } |
|
| 87 | - return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Initialize from a public key. |
|
| 92 | - * |
|
| 93 | - * @param RSAPublicKeyJWK $jwk |
|
| 94 | - * |
|
| 95 | - * @return self |
|
| 96 | - */ |
|
| 97 | - public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
| 98 | - { |
|
| 99 | - return new static($jwk); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Initialize from a private key. |
|
| 104 | - * |
|
| 105 | - * @param RSAPrivateKeyJWK $jwk |
|
| 106 | - * |
|
| 107 | - * @return self |
|
| 108 | - */ |
|
| 109 | - public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
| 110 | - { |
|
| 111 | - return new static($jwk->publicKey(), $jwk); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the public key. |
|
| 116 | - * |
|
| 117 | - * @return RSAPublicKeyJWK |
|
| 118 | - */ |
|
| 119 | - public function publicKey(): RSAPublicKeyJWK |
|
| 120 | - { |
|
| 121 | - return $this->_publicKey; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Check whether the private key is present. |
|
| 126 | - * |
|
| 127 | - * @return bool |
|
| 128 | - */ |
|
| 129 | - public function hasPrivateKey(): bool |
|
| 130 | - { |
|
| 131 | - return isset($this->_privateKey); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Get the private key. |
|
| 136 | - * |
|
| 137 | - * @throws \LogicException |
|
| 138 | - * |
|
| 139 | - * @return RSAPrivateKeyJWK |
|
| 140 | - */ |
|
| 141 | - public function privateKey(): RSAPrivateKeyJWK |
|
| 142 | - { |
|
| 143 | - if (!$this->hasPrivateKey()) { |
|
| 144 | - throw new \LogicException('Private key not set.'); |
|
| 145 | - } |
|
| 146 | - return $this->_privateKey; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * {@inheritdoc} |
|
| 151 | - */ |
|
| 152 | - public function headerParameters(): array |
|
| 153 | - { |
|
| 154 | - return array_merge(parent::headerParameters(), |
|
| 155 | - [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Get the padding scheme. |
|
| 160 | - * |
|
| 161 | - * @return int |
|
| 162 | - */ |
|
| 163 | - abstract protected function _paddingScheme(): int; |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * {@inheritdoc} |
|
| 167 | - */ |
|
| 168 | - protected function _encryptKey(string $key, Header &$header): string |
|
| 169 | - { |
|
| 170 | - $pubkey = openssl_pkey_get_public( |
|
| 171 | - $this->publicKey()->toPEM()->string()); |
|
| 172 | - if (false === $pubkey) { |
|
| 173 | - throw new \RuntimeException( |
|
| 174 | - 'openssl_pkey_get_public() failed: ' . |
|
| 175 | - $this->_getLastOpenSSLError()); |
|
| 176 | - } |
|
| 177 | - $result = openssl_public_encrypt($key, $crypted, $pubkey, |
|
| 178 | - $this->_paddingScheme()); |
|
| 179 | - if (!$result) { |
|
| 180 | - throw new \RuntimeException( |
|
| 181 | - 'openssl_public_encrypt() failed: ' . |
|
| 182 | - $this->_getLastOpenSSLError()); |
|
| 183 | - } |
|
| 184 | - return $crypted; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * {@inheritdoc} |
|
| 189 | - */ |
|
| 190 | - protected function _decryptKey(string $ciphertext, Header $header): string |
|
| 191 | - { |
|
| 192 | - $privkey = openssl_pkey_get_private( |
|
| 193 | - $this->privateKey()->toPEM()->string()); |
|
| 194 | - if (false === $privkey) { |
|
| 195 | - throw new \RuntimeException( |
|
| 196 | - 'openssl_pkey_get_private() failed: ' . |
|
| 197 | - $this->_getLastOpenSSLError()); |
|
| 198 | - } |
|
| 199 | - $result = openssl_private_decrypt($ciphertext, $cek, $privkey, |
|
| 200 | - $this->_paddingScheme()); |
|
| 201 | - if (!$result) { |
|
| 202 | - throw new \RuntimeException( |
|
| 203 | - 'openssl_private_decrypt() failed: ' . |
|
| 204 | - $this->_getLastOpenSSLError()); |
|
| 205 | - } |
|
| 206 | - return $cek; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Get last OpenSSL error message. |
|
| 211 | - * |
|
| 212 | - * @return null|string |
|
| 213 | - */ |
|
| 214 | - protected function _getLastOpenSSLError(): ?string |
|
| 215 | - { |
|
| 216 | - $msg = null; |
|
| 217 | - while (false !== ($err = openssl_error_string())) { |
|
| 218 | - $msg = $err; |
|
| 219 | - } |
|
| 220 | - return $msg; |
|
| 221 | - } |
|
| 24 | + use RandomCEK; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Mapping from algorithm name to class name. |
|
| 28 | + * |
|
| 29 | + * @internal |
|
| 30 | + * |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + const MAP_ALGO_TO_CLASS = [ |
|
| 34 | + JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
|
| 35 | + JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class, |
|
| 36 | + ]; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Public key. |
|
| 40 | + * |
|
| 41 | + * @var RSAPublicKeyJWK |
|
| 42 | + */ |
|
| 43 | + protected $_publicKey; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Private key. |
|
| 47 | + * |
|
| 48 | + * @var null|RSAPrivateKeyJWK |
|
| 49 | + */ |
|
| 50 | + protected $_privateKey; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Constructor. |
|
| 54 | + * |
|
| 55 | + * Use `fromPublicKey` or `fromPrivateKey` instead! |
|
| 56 | + * |
|
| 57 | + * @param RSAPublicKeyJWK $pub_key RSA public key |
|
| 58 | + * @param RSAPrivateKeyJWK $priv_key Optional RSA private key |
|
| 59 | + */ |
|
| 60 | + protected function __construct(RSAPublicKeyJWK $pub_key, |
|
| 61 | + ?RSAPrivateKeyJWK $priv_key = null) |
|
| 62 | + { |
|
| 63 | + $this->_publicKey = $pub_key; |
|
| 64 | + $this->_privateKey = $priv_key; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Initialize from JWK. |
|
| 69 | + * |
|
| 70 | + * @param JWK $jwk |
|
| 71 | + * @param Header $header |
|
| 72 | + * |
|
| 73 | + * @throws \UnexpectedValueException |
|
| 74 | + * |
|
| 75 | + * @return self |
|
| 76 | + */ |
|
| 77 | + public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm |
|
| 78 | + { |
|
| 79 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
| 80 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
| 81 | + throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'."); |
|
| 82 | + } |
|
| 83 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
| 84 | + if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
| 85 | + return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
| 86 | + } |
|
| 87 | + return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Initialize from a public key. |
|
| 92 | + * |
|
| 93 | + * @param RSAPublicKeyJWK $jwk |
|
| 94 | + * |
|
| 95 | + * @return self |
|
| 96 | + */ |
|
| 97 | + public static function fromPublicKey(RSAPublicKeyJWK $jwk): self |
|
| 98 | + { |
|
| 99 | + return new static($jwk); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Initialize from a private key. |
|
| 104 | + * |
|
| 105 | + * @param RSAPrivateKeyJWK $jwk |
|
| 106 | + * |
|
| 107 | + * @return self |
|
| 108 | + */ |
|
| 109 | + public static function fromPrivateKey(RSAPrivateKeyJWK $jwk): self |
|
| 110 | + { |
|
| 111 | + return new static($jwk->publicKey(), $jwk); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the public key. |
|
| 116 | + * |
|
| 117 | + * @return RSAPublicKeyJWK |
|
| 118 | + */ |
|
| 119 | + public function publicKey(): RSAPublicKeyJWK |
|
| 120 | + { |
|
| 121 | + return $this->_publicKey; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Check whether the private key is present. |
|
| 126 | + * |
|
| 127 | + * @return bool |
|
| 128 | + */ |
|
| 129 | + public function hasPrivateKey(): bool |
|
| 130 | + { |
|
| 131 | + return isset($this->_privateKey); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Get the private key. |
|
| 136 | + * |
|
| 137 | + * @throws \LogicException |
|
| 138 | + * |
|
| 139 | + * @return RSAPrivateKeyJWK |
|
| 140 | + */ |
|
| 141 | + public function privateKey(): RSAPrivateKeyJWK |
|
| 142 | + { |
|
| 143 | + if (!$this->hasPrivateKey()) { |
|
| 144 | + throw new \LogicException('Private key not set.'); |
|
| 145 | + } |
|
| 146 | + return $this->_privateKey; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * {@inheritdoc} |
|
| 151 | + */ |
|
| 152 | + public function headerParameters(): array |
|
| 153 | + { |
|
| 154 | + return array_merge(parent::headerParameters(), |
|
| 155 | + [AlgorithmParameter::fromAlgorithm($this)]); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Get the padding scheme. |
|
| 160 | + * |
|
| 161 | + * @return int |
|
| 162 | + */ |
|
| 163 | + abstract protected function _paddingScheme(): int; |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * {@inheritdoc} |
|
| 167 | + */ |
|
| 168 | + protected function _encryptKey(string $key, Header &$header): string |
|
| 169 | + { |
|
| 170 | + $pubkey = openssl_pkey_get_public( |
|
| 171 | + $this->publicKey()->toPEM()->string()); |
|
| 172 | + if (false === $pubkey) { |
|
| 173 | + throw new \RuntimeException( |
|
| 174 | + 'openssl_pkey_get_public() failed: ' . |
|
| 175 | + $this->_getLastOpenSSLError()); |
|
| 176 | + } |
|
| 177 | + $result = openssl_public_encrypt($key, $crypted, $pubkey, |
|
| 178 | + $this->_paddingScheme()); |
|
| 179 | + if (!$result) { |
|
| 180 | + throw new \RuntimeException( |
|
| 181 | + 'openssl_public_encrypt() failed: ' . |
|
| 182 | + $this->_getLastOpenSSLError()); |
|
| 183 | + } |
|
| 184 | + return $crypted; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * {@inheritdoc} |
|
| 189 | + */ |
|
| 190 | + protected function _decryptKey(string $ciphertext, Header $header): string |
|
| 191 | + { |
|
| 192 | + $privkey = openssl_pkey_get_private( |
|
| 193 | + $this->privateKey()->toPEM()->string()); |
|
| 194 | + if (false === $privkey) { |
|
| 195 | + throw new \RuntimeException( |
|
| 196 | + 'openssl_pkey_get_private() failed: ' . |
|
| 197 | + $this->_getLastOpenSSLError()); |
|
| 198 | + } |
|
| 199 | + $result = openssl_private_decrypt($ciphertext, $cek, $privkey, |
|
| 200 | + $this->_paddingScheme()); |
|
| 201 | + if (!$result) { |
|
| 202 | + throw new \RuntimeException( |
|
| 203 | + 'openssl_private_decrypt() failed: ' . |
|
| 204 | + $this->_getLastOpenSSLError()); |
|
| 205 | + } |
|
| 206 | + return $cek; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Get last OpenSSL error message. |
|
| 211 | + * |
|
| 212 | + * @return null|string |
|
| 213 | + */ |
|
| 214 | + protected function _getLastOpenSSLError(): ?string |
|
| 215 | + { |
|
| 216 | + $msg = null; |
|
| 217 | + while (false !== ($err = openssl_error_string())) { |
|
| 218 | + $msg = $err; |
|
| 219 | + } |
|
| 220 | + return $msg; |
|
| 221 | + } |
|
| 222 | 222 | } |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class A128GCMAlgorithm extends AESGCMAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function encryptionAlgorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_A128GCM; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function encryptionAlgorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_A128GCM; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - public function keySize(): int |
|
| 28 | - { |
|
| 29 | - return 16; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + public function keySize(): int |
|
| 28 | + { |
|
| 29 | + return 16; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class A192GCMAlgorithm extends AESGCMAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function encryptionAlgorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_A192GCM; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function encryptionAlgorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_A192GCM; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - public function keySize(): int |
|
| 28 | - { |
|
| 29 | - return 24; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + public function keySize(): int |
|
| 28 | + { |
|
| 29 | + return 24; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -13,19 +13,19 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class A256GCMAlgorithm extends AESGCMAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public function encryptionAlgorithmParamValue(): string |
|
| 20 | - { |
|
| 21 | - return JWA::ALGO_A256GCM; |
|
| 22 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public function encryptionAlgorithmParamValue(): string |
|
| 20 | + { |
|
| 21 | + return JWA::ALGO_A256GCM; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - public function keySize(): int |
|
| 28 | - { |
|
| 29 | - return 32; |
|
| 30 | - } |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + public function keySize(): int |
|
| 28 | + { |
|
| 29 | + return 32; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -17,74 +17,74 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | abstract class AESGCMAlgorithm implements ContentEncryptionAlgorithm |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function encrypt(string $plaintext, string $key, string $iv, |
|
| 24 | - string $aad): array |
|
| 25 | - { |
|
| 26 | - $this->_validateKey($key); |
|
| 27 | - $this->_validateIV($iv); |
|
| 28 | - return AESGCM::encrypt($plaintext, $aad, $key, $iv, 16); |
|
| 29 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function encrypt(string $plaintext, string $key, string $iv, |
|
| 24 | + string $aad): array |
|
| 25 | + { |
|
| 26 | + $this->_validateKey($key); |
|
| 27 | + $this->_validateIV($iv); |
|
| 28 | + return AESGCM::encrypt($plaintext, $aad, $key, $iv, 16); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * {@inheritdoc} |
|
| 33 | - */ |
|
| 34 | - public function decrypt(string $ciphertext, string $key, string $iv, |
|
| 35 | - string $aad, string $auth_tag): string |
|
| 36 | - { |
|
| 37 | - $this->_validateKey($key); |
|
| 38 | - $this->_validateIV($iv); |
|
| 39 | - try { |
|
| 40 | - $plaintext = AESGCM::decrypt($ciphertext, $auth_tag, $aad, $key, $iv); |
|
| 41 | - } catch (GCMAuthException $e) { |
|
| 42 | - throw new AuthenticationException('Message authentication failed.'); |
|
| 43 | - } |
|
| 44 | - return $plaintext; |
|
| 45 | - } |
|
| 31 | + /** |
|
| 32 | + * {@inheritdoc} |
|
| 33 | + */ |
|
| 34 | + public function decrypt(string $ciphertext, string $key, string $iv, |
|
| 35 | + string $aad, string $auth_tag): string |
|
| 36 | + { |
|
| 37 | + $this->_validateKey($key); |
|
| 38 | + $this->_validateIV($iv); |
|
| 39 | + try { |
|
| 40 | + $plaintext = AESGCM::decrypt($ciphertext, $auth_tag, $aad, $key, $iv); |
|
| 41 | + } catch (GCMAuthException $e) { |
|
| 42 | + throw new AuthenticationException('Message authentication failed.'); |
|
| 43 | + } |
|
| 44 | + return $plaintext; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * {@inheritdoc} |
|
| 49 | - */ |
|
| 50 | - public function ivSize(): int |
|
| 51 | - { |
|
| 52 | - return 12; |
|
| 53 | - } |
|
| 47 | + /** |
|
| 48 | + * {@inheritdoc} |
|
| 49 | + */ |
|
| 50 | + public function ivSize(): int |
|
| 51 | + { |
|
| 52 | + return 12; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * {@inheritdoc} |
|
| 57 | - */ |
|
| 58 | - public function headerParameters(): array |
|
| 59 | - { |
|
| 60 | - return [EncryptionAlgorithmParameter::fromAlgorithm($this)]; |
|
| 61 | - } |
|
| 55 | + /** |
|
| 56 | + * {@inheritdoc} |
|
| 57 | + */ |
|
| 58 | + public function headerParameters(): array |
|
| 59 | + { |
|
| 60 | + return [EncryptionAlgorithmParameter::fromAlgorithm($this)]; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Check that key is valid. |
|
| 65 | - * |
|
| 66 | - * @param string $key |
|
| 67 | - * |
|
| 68 | - * @throws \RuntimeException |
|
| 69 | - */ |
|
| 70 | - final protected function _validateKey(string $key): void |
|
| 71 | - { |
|
| 72 | - if (strlen($key) !== $this->keySize()) { |
|
| 73 | - throw new \RuntimeException('Invalid key size.'); |
|
| 74 | - } |
|
| 75 | - } |
|
| 63 | + /** |
|
| 64 | + * Check that key is valid. |
|
| 65 | + * |
|
| 66 | + * @param string $key |
|
| 67 | + * |
|
| 68 | + * @throws \RuntimeException |
|
| 69 | + */ |
|
| 70 | + final protected function _validateKey(string $key): void |
|
| 71 | + { |
|
| 72 | + if (strlen($key) !== $this->keySize()) { |
|
| 73 | + throw new \RuntimeException('Invalid key size.'); |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Check that IV is valid. |
|
| 79 | - * |
|
| 80 | - * @param string $iv |
|
| 81 | - * |
|
| 82 | - * @throws \RuntimeException |
|
| 83 | - */ |
|
| 84 | - final protected function _validateIV(string $iv): void |
|
| 85 | - { |
|
| 86 | - if (strlen($iv) !== $this->ivSize()) { |
|
| 87 | - throw new \RuntimeException('Invalid IV length.'); |
|
| 88 | - } |
|
| 89 | - } |
|
| 77 | + /** |
|
| 78 | + * Check that IV is valid. |
|
| 79 | + * |
|
| 80 | + * @param string $iv |
|
| 81 | + * |
|
| 82 | + * @throws \RuntimeException |
|
| 83 | + */ |
|
| 84 | + final protected function _validateIV(string $iv): void |
|
| 85 | + { |
|
| 86 | + if (strlen($iv) !== $this->ivSize()) { |
|
| 87 | + throw new \RuntimeException('Invalid IV length.'); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | 90 | } |