@@ -11,22 +11,22 @@ |
||
| 11 | 11 | * Interface for algorithms that may be used to compress and decompress data. |
| 12 | 12 | */ |
| 13 | 13 | interface CompressionAlgorithm extends |
| 14 | - CompressionAlgorithmParameterValue, |
|
| 15 | - HeaderParameters |
|
| 14 | + CompressionAlgorithmParameterValue, |
|
| 15 | + HeaderParameters |
|
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Compress data. |
|
| 19 | - * |
|
| 20 | - * @param string $data Uncompressed data |
|
| 21 | - * @return string Compressed data |
|
| 22 | - */ |
|
| 23 | - public function compress(string $data): string; |
|
| 17 | + /** |
|
| 18 | + * Compress data. |
|
| 19 | + * |
|
| 20 | + * @param string $data Uncompressed data |
|
| 21 | + * @return string Compressed data |
|
| 22 | + */ |
|
| 23 | + public function compress(string $data): string; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Decompress data. |
|
| 27 | - * |
|
| 28 | - * @param string $data Compressed data |
|
| 29 | - * @return string Uncompressed data |
|
| 30 | - */ |
|
| 31 | - public function decompress(string $data): string; |
|
| 25 | + /** |
|
| 26 | + * Decompress data. |
|
| 27 | + * |
|
| 28 | + * @param string $data Compressed data |
|
| 29 | + * @return string Uncompressed data |
|
| 30 | + */ |
|
| 31 | + public function decompress(string $data): string; |
|
| 32 | 32 | } |
@@ -16,122 +16,122 @@ |
||
| 16 | 16 | * algorithms. |
| 17 | 17 | */ |
| 18 | 18 | abstract class KeyManagementAlgorithm implements |
| 19 | - AlgorithmParameterValue, |
|
| 20 | - HeaderParameters |
|
| 19 | + AlgorithmParameterValue, |
|
| 20 | + HeaderParameters |
|
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * ID of the key used by the algorithm. |
|
| 24 | - * |
|
| 25 | - * If set, KeyID parameter shall be automatically inserted into JWE's |
|
| 26 | - * header. |
|
| 27 | - * |
|
| 28 | - * @var string|null $_keyID |
|
| 29 | - */ |
|
| 30 | - protected $_keyID; |
|
| 22 | + /** |
|
| 23 | + * ID of the key used by the algorithm. |
|
| 24 | + * |
|
| 25 | + * If set, KeyID parameter shall be automatically inserted into JWE's |
|
| 26 | + * header. |
|
| 27 | + * |
|
| 28 | + * @var string|null $_keyID |
|
| 29 | + */ |
|
| 30 | + protected $_keyID; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Encrypt a key. |
|
| 34 | - * |
|
| 35 | - * @param string $key Key to be encrypted |
|
| 36 | - * @param Header $header Reference to the Header variable, that shall |
|
| 37 | - * be updated to contain parameters specific to the encryption |
|
| 38 | - * @return string Ciphertext |
|
| 39 | - */ |
|
| 40 | - abstract protected function _encryptKey(string $key, Header &$header): string; |
|
| 32 | + /** |
|
| 33 | + * Encrypt a key. |
|
| 34 | + * |
|
| 35 | + * @param string $key Key to be encrypted |
|
| 36 | + * @param Header $header Reference to the Header variable, that shall |
|
| 37 | + * be updated to contain parameters specific to the encryption |
|
| 38 | + * @return string Ciphertext |
|
| 39 | + */ |
|
| 40 | + abstract protected function _encryptKey(string $key, Header &$header): string; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Decrypt a key. |
|
| 44 | - * |
|
| 45 | - * @param string $ciphertext Ciphertext of the encrypted key |
|
| 46 | - * @param Header $header Header possibly containing encoding specific |
|
| 47 | - * parameters |
|
| 48 | - * @return string Plaintext key |
|
| 49 | - */ |
|
| 50 | - abstract protected function _decryptKey(string $ciphertext, Header $header): string; |
|
| 42 | + /** |
|
| 43 | + * Decrypt a key. |
|
| 44 | + * |
|
| 45 | + * @param string $ciphertext Ciphertext of the encrypted key |
|
| 46 | + * @param Header $header Header possibly containing encoding specific |
|
| 47 | + * parameters |
|
| 48 | + * @return string Plaintext key |
|
| 49 | + */ |
|
| 50 | + abstract protected function _decryptKey(string $ciphertext, Header $header): string; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Encrypt a key to be inserted into JWE header. |
|
| 54 | - * |
|
| 55 | - * @param string $cek Content encryption key |
|
| 56 | - * @param Header|null $header Optional reference to the Header variable, |
|
| 57 | - * which may be updated to contain parameters specific to this |
|
| 58 | - * encrypt invocation. If the variable is referenced, but is a null, |
|
| 59 | - * it shall be initialized to an empty Header. |
|
| 60 | - * @throws \RuntimeException For generic errors |
|
| 61 | - * @return string Encrypted key |
|
| 62 | - */ |
|
| 63 | - final public function encrypt(string $cek, Header &$header = null): string |
|
| 64 | - { |
|
| 65 | - if (!isset($header)) { |
|
| 66 | - $header = new Header(); |
|
| 67 | - } |
|
| 68 | - return $this->_encryptKey($cek, $header); |
|
| 69 | - } |
|
| 52 | + /** |
|
| 53 | + * Encrypt a key to be inserted into JWE header. |
|
| 54 | + * |
|
| 55 | + * @param string $cek Content encryption key |
|
| 56 | + * @param Header|null $header Optional reference to the Header variable, |
|
| 57 | + * which may be updated to contain parameters specific to this |
|
| 58 | + * encrypt invocation. If the variable is referenced, but is a null, |
|
| 59 | + * it shall be initialized to an empty Header. |
|
| 60 | + * @throws \RuntimeException For generic errors |
|
| 61 | + * @return string Encrypted key |
|
| 62 | + */ |
|
| 63 | + final public function encrypt(string $cek, Header &$header = null): string |
|
| 64 | + { |
|
| 65 | + if (!isset($header)) { |
|
| 66 | + $header = new Header(); |
|
| 67 | + } |
|
| 68 | + return $this->_encryptKey($cek, $header); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Decrypt a CEK from the encrypted data. |
|
| 73 | - * |
|
| 74 | - * @param string $data Encrypted key |
|
| 75 | - * @param Header|null Optional header containing parameters required to |
|
| 76 | - * decrypt the key. |
|
| 77 | - * @throws \RuntimeException For generic errors |
|
| 78 | - * @return string Content encryption key |
|
| 79 | - */ |
|
| 80 | - final public function decrypt(string $data, Header $header = null): string |
|
| 81 | - { |
|
| 82 | - if (!isset($header)) { |
|
| 83 | - $header = new Header(); |
|
| 84 | - } |
|
| 85 | - return $this->_decryptKey($data, $header); |
|
| 86 | - } |
|
| 71 | + /** |
|
| 72 | + * Decrypt a CEK from the encrypted data. |
|
| 73 | + * |
|
| 74 | + * @param string $data Encrypted key |
|
| 75 | + * @param Header|null Optional header containing parameters required to |
|
| 76 | + * decrypt the key. |
|
| 77 | + * @throws \RuntimeException For generic errors |
|
| 78 | + * @return string Content encryption key |
|
| 79 | + */ |
|
| 80 | + final public function decrypt(string $data, Header $header = null): string |
|
| 81 | + { |
|
| 82 | + if (!isset($header)) { |
|
| 83 | + $header = new Header(); |
|
| 84 | + } |
|
| 85 | + return $this->_decryptKey($data, $header); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Get content encryption key for the encryption. |
|
| 90 | - * |
|
| 91 | - * Returned key may be random depending on the key management algorithm. |
|
| 92 | - * |
|
| 93 | - * @param int $length Required key size in bytes |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - abstract public function cekForEncryption(int $length): string; |
|
| 88 | + /** |
|
| 89 | + * Get content encryption key for the encryption. |
|
| 90 | + * |
|
| 91 | + * Returned key may be random depending on the key management algorithm. |
|
| 92 | + * |
|
| 93 | + * @param int $length Required key size in bytes |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + abstract public function cekForEncryption(int $length): string; |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Initialize key management algorithm from a JWK and a header. |
|
| 100 | - * |
|
| 101 | - * @param JWK $jwk |
|
| 102 | - * @param Header $header |
|
| 103 | - * @return KeyManagementAlgorithm |
|
| 104 | - */ |
|
| 105 | - public static function fromJWK(JWK $jwk, Header $header) |
|
| 106 | - { |
|
| 107 | - $factory = new KeyAlgorithmFactory($header); |
|
| 108 | - return $factory->algoByKey($jwk); |
|
| 109 | - } |
|
| 98 | + /** |
|
| 99 | + * Initialize key management algorithm from a JWK and a header. |
|
| 100 | + * |
|
| 101 | + * @param JWK $jwk |
|
| 102 | + * @param Header $header |
|
| 103 | + * @return KeyManagementAlgorithm |
|
| 104 | + */ |
|
| 105 | + public static function fromJWK(JWK $jwk, Header $header) |
|
| 106 | + { |
|
| 107 | + $factory = new KeyAlgorithmFactory($header); |
|
| 108 | + return $factory->algoByKey($jwk); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Get self with key ID. |
|
| 113 | - * |
|
| 114 | - * @param string|null $id Key ID or null to remove |
|
| 115 | - * @return self |
|
| 116 | - */ |
|
| 117 | - public function withKeyID($id): self |
|
| 118 | - { |
|
| 119 | - $obj = clone $this; |
|
| 120 | - $obj->_keyID = $id; |
|
| 121 | - return $obj; |
|
| 122 | - } |
|
| 111 | + /** |
|
| 112 | + * Get self with key ID. |
|
| 113 | + * |
|
| 114 | + * @param string|null $id Key ID or null to remove |
|
| 115 | + * @return self |
|
| 116 | + */ |
|
| 117 | + public function withKeyID($id): self |
|
| 118 | + { |
|
| 119 | + $obj = clone $this; |
|
| 120 | + $obj->_keyID = $id; |
|
| 121 | + return $obj; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * |
|
| 126 | - * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
|
| 127 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 128 | - */ |
|
| 129 | - public function headerParameters(): array |
|
| 130 | - { |
|
| 131 | - $params = array(); |
|
| 132 | - if (isset($this->_keyID)) { |
|
| 133 | - $params[] = new KeyIDParameter($this->_keyID); |
|
| 134 | - } |
|
| 135 | - return $params; |
|
| 136 | - } |
|
| 124 | + /** |
|
| 125 | + * |
|
| 126 | + * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
|
| 127 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
| 128 | + */ |
|
| 129 | + public function headerParameters(): array |
|
| 130 | + { |
|
| 131 | + $params = array(); |
|
| 132 | + if (isset($this->_keyID)) { |
|
| 133 | + $params[] = new KeyIDParameter($this->_keyID); |
|
| 134 | + } |
|
| 135 | + return $params; |
|
| 136 | + } |
|
| 137 | 137 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * be updated to contain parameters specific to the encryption |
| 38 | 38 | * @return string Ciphertext |
| 39 | 39 | */ |
| 40 | - abstract protected function _encryptKey(string $key, Header &$header): string; |
|
| 40 | + abstract protected function _encryptKey(string $key, Header&$header): string; |
|
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * Decrypt a key. |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | * @throws \RuntimeException For generic errors |
| 61 | 61 | * @return string Encrypted key |
| 62 | 62 | */ |
| 63 | - final public function encrypt(string $cek, Header &$header = null): string |
|
| 63 | + final public function encrypt(string $cek, Header&$header = null): string |
|
| 64 | 64 | { |
| 65 | 65 | if (!isset($header)) { |
| 66 | 66 | $header = new Header(); |
@@ -13,66 +13,66 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class A128CBCHS256Algorithm extends AESCBCAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - public function keySize(): int |
|
| 21 | - { |
|
| 22 | - return 32; |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + public function keySize(): int |
|
| 21 | + { |
|
| 22 | + return 32; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - public function encryptionAlgorithmParamValue(): string |
|
| 30 | - { |
|
| 31 | - return JWA::ALGO_A128CBC_HS256; |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + public function encryptionAlgorithmParamValue(): string |
|
| 30 | + { |
|
| 31 | + return JWA::ALGO_A128CBC_HS256; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * |
|
| 36 | - * {@inheritdoc} |
|
| 37 | - */ |
|
| 38 | - protected function _cipherMethod(): string |
|
| 39 | - { |
|
| 40 | - return "AES-128-CBC"; |
|
| 41 | - } |
|
| 34 | + /** |
|
| 35 | + * |
|
| 36 | + * {@inheritdoc} |
|
| 37 | + */ |
|
| 38 | + protected function _cipherMethod(): string |
|
| 39 | + { |
|
| 40 | + return "AES-128-CBC"; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * |
|
| 45 | - * {@inheritdoc} |
|
| 46 | - */ |
|
| 47 | - protected function _hashAlgo(): string |
|
| 48 | - { |
|
| 49 | - return "sha256"; |
|
| 50 | - } |
|
| 43 | + /** |
|
| 44 | + * |
|
| 45 | + * {@inheritdoc} |
|
| 46 | + */ |
|
| 47 | + protected function _hashAlgo(): string |
|
| 48 | + { |
|
| 49 | + return "sha256"; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * |
|
| 54 | - * {@inheritdoc} |
|
| 55 | - */ |
|
| 56 | - protected function _encKeyLen(): int |
|
| 57 | - { |
|
| 58 | - return 16; |
|
| 59 | - } |
|
| 52 | + /** |
|
| 53 | + * |
|
| 54 | + * {@inheritdoc} |
|
| 55 | + */ |
|
| 56 | + protected function _encKeyLen(): int |
|
| 57 | + { |
|
| 58 | + return 16; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * |
|
| 63 | - * {@inheritdoc} |
|
| 64 | - */ |
|
| 65 | - protected function _macKeyLen(): int |
|
| 66 | - { |
|
| 67 | - return 16; |
|
| 68 | - } |
|
| 61 | + /** |
|
| 62 | + * |
|
| 63 | + * {@inheritdoc} |
|
| 64 | + */ |
|
| 65 | + protected function _macKeyLen(): int |
|
| 66 | + { |
|
| 67 | + return 16; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * |
|
| 72 | - * {@inheritdoc} |
|
| 73 | - */ |
|
| 74 | - protected function _tagLen(): int |
|
| 75 | - { |
|
| 76 | - return 16; |
|
| 77 | - } |
|
| 70 | + /** |
|
| 71 | + * |
|
| 72 | + * {@inheritdoc} |
|
| 73 | + */ |
|
| 74 | + protected function _tagLen(): int |
|
| 75 | + { |
|
| 76 | + return 16; |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -15,210 +15,210 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class AESCBCAlgorithm implements ContentEncryptionAlgorithm |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Get cipher method name that is recognized by OpenSSL. |
|
| 20 | - * |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - abstract protected function _cipherMethod(): string; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Get algorithm name that is recognized by the Hash extension. |
|
| 27 | - * |
|
| 28 | - * @return string |
|
| 29 | - */ |
|
| 30 | - abstract protected function _hashAlgo(): string; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Get length of the encryption key. |
|
| 34 | - * |
|
| 35 | - * @return int |
|
| 36 | - */ |
|
| 37 | - abstract protected function _encKeyLen(): int; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Get length of the MAC key. |
|
| 41 | - * |
|
| 42 | - * @return int |
|
| 43 | - */ |
|
| 44 | - abstract protected function _macKeyLen(): int; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Get length of the authentication tag. |
|
| 48 | - * |
|
| 49 | - * @return int |
|
| 50 | - */ |
|
| 51 | - abstract protected function _tagLen(): int; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Get cipher method and verify that it's supported. |
|
| 55 | - * |
|
| 56 | - * @throws \RuntimeException |
|
| 57 | - * @return string |
|
| 58 | - */ |
|
| 59 | - final protected function _getCipherMethod(): string |
|
| 60 | - { |
|
| 61 | - static $supported_ciphers; |
|
| 62 | - if (!isset($supported_ciphers)) { |
|
| 63 | - $supported_ciphers = array_flip(openssl_get_cipher_methods()); |
|
| 64 | - } |
|
| 65 | - $method = $this->_cipherMethod(); |
|
| 66 | - if (!isset($supported_ciphers[$method])) { |
|
| 67 | - throw new \RuntimeException( |
|
| 68 | - "Cipher method $method is not" . |
|
| 69 | - " supported by this version of OpenSSL."); |
|
| 70 | - } |
|
| 71 | - return $method; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Check that key is valid. |
|
| 76 | - * |
|
| 77 | - * @param string $key |
|
| 78 | - * @throws \RuntimeException |
|
| 79 | - */ |
|
| 80 | - final protected function _validateKey(string $key) |
|
| 81 | - { |
|
| 82 | - if (strlen($key) != $this->keySize()) { |
|
| 83 | - throw new \RuntimeException("Invalid key size."); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Check that IV is valid. |
|
| 89 | - * |
|
| 90 | - * @param string $iv |
|
| 91 | - * @throws \RuntimeException |
|
| 92 | - */ |
|
| 93 | - final protected function _validateIV(string $iv) |
|
| 94 | - { |
|
| 95 | - $len = openssl_cipher_iv_length($this->_getCipherMethod()); |
|
| 96 | - if ($len != strlen($iv)) { |
|
| 97 | - throw new \RuntimeException("Invalid IV length."); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get MAC key from CEK. |
|
| 103 | - * |
|
| 104 | - * @param string $key |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - final protected function _macKey(string $key): string |
|
| 108 | - { |
|
| 109 | - return substr($key, 0, $this->_macKeyLen()); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Get encryption key from CEK. |
|
| 114 | - * |
|
| 115 | - * @param string $key |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - final protected function _encKey(string $key): string |
|
| 119 | - { |
|
| 120 | - return substr($key, -$this->_encKeyLen()); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Compute AL value. |
|
| 125 | - * |
|
| 126 | - * @param string $aad |
|
| 127 | - * @return string 64 bits |
|
| 128 | - */ |
|
| 129 | - final protected function _aadLen(string $aad): string |
|
| 130 | - { |
|
| 131 | - // truncate on 32 bit hosts |
|
| 132 | - if (PHP_INT_SIZE < 8) { |
|
| 133 | - return "\0\0\0\0" . pack("N", strlen($aad) * 8); |
|
| 134 | - } |
|
| 135 | - return pack("J", strlen($aad) * 8); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Compute authentication tag. |
|
| 140 | - * |
|
| 141 | - * @param string $data |
|
| 142 | - * @param string $key CEK |
|
| 143 | - * @return string |
|
| 144 | - */ |
|
| 145 | - final protected function _computeAuthTag(string $data, string $key): string |
|
| 146 | - { |
|
| 147 | - $tag = hash_hmac($this->_hashAlgo(), $data, $this->_macKey($key), true); |
|
| 148 | - return substr($tag, 0, $this->_tagLen()); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * |
|
| 153 | - * {@inheritdoc} |
|
| 154 | - */ |
|
| 155 | - public function encrypt(string $plaintext, string $key, string $iv, |
|
| 156 | - string $aad): array |
|
| 157 | - { |
|
| 158 | - $this->_validateKey($key); |
|
| 159 | - $this->_validateIV($iv); |
|
| 160 | - $ciphertext = openssl_encrypt($plaintext, $this->_getCipherMethod(), |
|
| 161 | - $this->_encKey($key), OPENSSL_RAW_DATA, $iv); |
|
| 162 | - if (false === $ciphertext) { |
|
| 163 | - throw new \RuntimeException( |
|
| 164 | - "openssl_encrypt() failed: " . $this->_getLastOpenSSLError()); |
|
| 165 | - } |
|
| 166 | - $auth_data = $aad . $iv . $ciphertext . $this->_aadLen($aad); |
|
| 167 | - $auth_tag = $this->_computeAuthTag($auth_data, $key); |
|
| 168 | - return [$ciphertext, $auth_tag]; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * |
|
| 173 | - * {@inheritdoc} |
|
| 174 | - */ |
|
| 175 | - public function decrypt(string $ciphertext, string $key, string $iv, |
|
| 176 | - string $aad, string $auth_tag): string |
|
| 177 | - { |
|
| 178 | - $this->_validateKey($key); |
|
| 179 | - $this->_validateIV($iv); |
|
| 180 | - $auth_data = $aad . $iv . $ciphertext . $this->_aadLen($aad); |
|
| 181 | - if ($this->_computeAuthTag($auth_data, $key) != $auth_tag) { |
|
| 182 | - throw new AuthenticationException("Message authentication failed."); |
|
| 183 | - } |
|
| 184 | - $plaintext = openssl_decrypt($ciphertext, $this->_getCipherMethod(), |
|
| 185 | - $this->_encKey($key), OPENSSL_RAW_DATA, $iv); |
|
| 186 | - if (false === $plaintext) { |
|
| 187 | - throw new \RuntimeException( |
|
| 188 | - "openssl_decrypt() failed: " . $this->_getLastOpenSSLError()); |
|
| 189 | - } |
|
| 190 | - return $plaintext; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Get last OpenSSL error message. |
|
| 195 | - * |
|
| 196 | - * @return string|null |
|
| 197 | - */ |
|
| 198 | - protected function _getLastOpenSSLError() |
|
| 199 | - { |
|
| 200 | - $msg = null; |
|
| 201 | - while (false !== ($err = openssl_error_string())) { |
|
| 202 | - $msg = $err; |
|
| 203 | - } |
|
| 204 | - return $msg; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * |
|
| 209 | - * {@inheritdoc} |
|
| 210 | - */ |
|
| 211 | - public function ivSize(): int |
|
| 212 | - { |
|
| 213 | - return 16; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * |
|
| 218 | - * {@inheritdoc} |
|
| 219 | - */ |
|
| 220 | - public function headerParameters(): array |
|
| 221 | - { |
|
| 222 | - return array(EncryptionAlgorithmParameter::fromAlgorithm($this)); |
|
| 223 | - } |
|
| 18 | + /** |
|
| 19 | + * Get cipher method name that is recognized by OpenSSL. |
|
| 20 | + * |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + abstract protected function _cipherMethod(): string; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Get algorithm name that is recognized by the Hash extension. |
|
| 27 | + * |
|
| 28 | + * @return string |
|
| 29 | + */ |
|
| 30 | + abstract protected function _hashAlgo(): string; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Get length of the encryption key. |
|
| 34 | + * |
|
| 35 | + * @return int |
|
| 36 | + */ |
|
| 37 | + abstract protected function _encKeyLen(): int; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Get length of the MAC key. |
|
| 41 | + * |
|
| 42 | + * @return int |
|
| 43 | + */ |
|
| 44 | + abstract protected function _macKeyLen(): int; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Get length of the authentication tag. |
|
| 48 | + * |
|
| 49 | + * @return int |
|
| 50 | + */ |
|
| 51 | + abstract protected function _tagLen(): int; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Get cipher method and verify that it's supported. |
|
| 55 | + * |
|
| 56 | + * @throws \RuntimeException |
|
| 57 | + * @return string |
|
| 58 | + */ |
|
| 59 | + final protected function _getCipherMethod(): string |
|
| 60 | + { |
|
| 61 | + static $supported_ciphers; |
|
| 62 | + if (!isset($supported_ciphers)) { |
|
| 63 | + $supported_ciphers = array_flip(openssl_get_cipher_methods()); |
|
| 64 | + } |
|
| 65 | + $method = $this->_cipherMethod(); |
|
| 66 | + if (!isset($supported_ciphers[$method])) { |
|
| 67 | + throw new \RuntimeException( |
|
| 68 | + "Cipher method $method is not" . |
|
| 69 | + " supported by this version of OpenSSL."); |
|
| 70 | + } |
|
| 71 | + return $method; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Check that key is valid. |
|
| 76 | + * |
|
| 77 | + * @param string $key |
|
| 78 | + * @throws \RuntimeException |
|
| 79 | + */ |
|
| 80 | + final protected function _validateKey(string $key) |
|
| 81 | + { |
|
| 82 | + if (strlen($key) != $this->keySize()) { |
|
| 83 | + throw new \RuntimeException("Invalid key size."); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Check that IV is valid. |
|
| 89 | + * |
|
| 90 | + * @param string $iv |
|
| 91 | + * @throws \RuntimeException |
|
| 92 | + */ |
|
| 93 | + final protected function _validateIV(string $iv) |
|
| 94 | + { |
|
| 95 | + $len = openssl_cipher_iv_length($this->_getCipherMethod()); |
|
| 96 | + if ($len != strlen($iv)) { |
|
| 97 | + throw new \RuntimeException("Invalid IV length."); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get MAC key from CEK. |
|
| 103 | + * |
|
| 104 | + * @param string $key |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + final protected function _macKey(string $key): string |
|
| 108 | + { |
|
| 109 | + return substr($key, 0, $this->_macKeyLen()); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Get encryption key from CEK. |
|
| 114 | + * |
|
| 115 | + * @param string $key |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + final protected function _encKey(string $key): string |
|
| 119 | + { |
|
| 120 | + return substr($key, -$this->_encKeyLen()); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Compute AL value. |
|
| 125 | + * |
|
| 126 | + * @param string $aad |
|
| 127 | + * @return string 64 bits |
|
| 128 | + */ |
|
| 129 | + final protected function _aadLen(string $aad): string |
|
| 130 | + { |
|
| 131 | + // truncate on 32 bit hosts |
|
| 132 | + if (PHP_INT_SIZE < 8) { |
|
| 133 | + return "\0\0\0\0" . pack("N", strlen($aad) * 8); |
|
| 134 | + } |
|
| 135 | + return pack("J", strlen($aad) * 8); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Compute authentication tag. |
|
| 140 | + * |
|
| 141 | + * @param string $data |
|
| 142 | + * @param string $key CEK |
|
| 143 | + * @return string |
|
| 144 | + */ |
|
| 145 | + final protected function _computeAuthTag(string $data, string $key): string |
|
| 146 | + { |
|
| 147 | + $tag = hash_hmac($this->_hashAlgo(), $data, $this->_macKey($key), true); |
|
| 148 | + return substr($tag, 0, $this->_tagLen()); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * |
|
| 153 | + * {@inheritdoc} |
|
| 154 | + */ |
|
| 155 | + public function encrypt(string $plaintext, string $key, string $iv, |
|
| 156 | + string $aad): array |
|
| 157 | + { |
|
| 158 | + $this->_validateKey($key); |
|
| 159 | + $this->_validateIV($iv); |
|
| 160 | + $ciphertext = openssl_encrypt($plaintext, $this->_getCipherMethod(), |
|
| 161 | + $this->_encKey($key), OPENSSL_RAW_DATA, $iv); |
|
| 162 | + if (false === $ciphertext) { |
|
| 163 | + throw new \RuntimeException( |
|
| 164 | + "openssl_encrypt() failed: " . $this->_getLastOpenSSLError()); |
|
| 165 | + } |
|
| 166 | + $auth_data = $aad . $iv . $ciphertext . $this->_aadLen($aad); |
|
| 167 | + $auth_tag = $this->_computeAuthTag($auth_data, $key); |
|
| 168 | + return [$ciphertext, $auth_tag]; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * |
|
| 173 | + * {@inheritdoc} |
|
| 174 | + */ |
|
| 175 | + public function decrypt(string $ciphertext, string $key, string $iv, |
|
| 176 | + string $aad, string $auth_tag): string |
|
| 177 | + { |
|
| 178 | + $this->_validateKey($key); |
|
| 179 | + $this->_validateIV($iv); |
|
| 180 | + $auth_data = $aad . $iv . $ciphertext . $this->_aadLen($aad); |
|
| 181 | + if ($this->_computeAuthTag($auth_data, $key) != $auth_tag) { |
|
| 182 | + throw new AuthenticationException("Message authentication failed."); |
|
| 183 | + } |
|
| 184 | + $plaintext = openssl_decrypt($ciphertext, $this->_getCipherMethod(), |
|
| 185 | + $this->_encKey($key), OPENSSL_RAW_DATA, $iv); |
|
| 186 | + if (false === $plaintext) { |
|
| 187 | + throw new \RuntimeException( |
|
| 188 | + "openssl_decrypt() failed: " . $this->_getLastOpenSSLError()); |
|
| 189 | + } |
|
| 190 | + return $plaintext; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Get last OpenSSL error message. |
|
| 195 | + * |
|
| 196 | + * @return string|null |
|
| 197 | + */ |
|
| 198 | + protected function _getLastOpenSSLError() |
|
| 199 | + { |
|
| 200 | + $msg = null; |
|
| 201 | + while (false !== ($err = openssl_error_string())) { |
|
| 202 | + $msg = $err; |
|
| 203 | + } |
|
| 204 | + return $msg; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * |
|
| 209 | + * {@inheritdoc} |
|
| 210 | + */ |
|
| 211 | + public function ivSize(): int |
|
| 212 | + { |
|
| 213 | + return 16; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * |
|
| 218 | + * {@inheritdoc} |
|
| 219 | + */ |
|
| 220 | + public function headerParameters(): array |
|
| 221 | + { |
|
| 222 | + return array(EncryptionAlgorithmParameter::fromAlgorithm($this)); |
|
| 223 | + } |
|
| 224 | 224 | } |
@@ -13,66 +13,66 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class A256CBCHS512Algorithm extends AESCBCAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - public function keySize(): int |
|
| 21 | - { |
|
| 22 | - return 64; |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + public function keySize(): int |
|
| 21 | + { |
|
| 22 | + return 64; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - public function encryptionAlgorithmParamValue(): string |
|
| 30 | - { |
|
| 31 | - return JWA::ALGO_A256CBC_HS512; |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + public function encryptionAlgorithmParamValue(): string |
|
| 30 | + { |
|
| 31 | + return JWA::ALGO_A256CBC_HS512; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * |
|
| 36 | - * {@inheritdoc} |
|
| 37 | - */ |
|
| 38 | - protected function _cipherMethod(): string |
|
| 39 | - { |
|
| 40 | - return "AES-256-CBC"; |
|
| 41 | - } |
|
| 34 | + /** |
|
| 35 | + * |
|
| 36 | + * {@inheritdoc} |
|
| 37 | + */ |
|
| 38 | + protected function _cipherMethod(): string |
|
| 39 | + { |
|
| 40 | + return "AES-256-CBC"; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * |
|
| 45 | - * {@inheritdoc} |
|
| 46 | - */ |
|
| 47 | - protected function _hashAlgo(): string |
|
| 48 | - { |
|
| 49 | - return "sha512"; |
|
| 50 | - } |
|
| 43 | + /** |
|
| 44 | + * |
|
| 45 | + * {@inheritdoc} |
|
| 46 | + */ |
|
| 47 | + protected function _hashAlgo(): string |
|
| 48 | + { |
|
| 49 | + return "sha512"; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * |
|
| 54 | - * {@inheritdoc} |
|
| 55 | - */ |
|
| 56 | - protected function _encKeyLen(): int |
|
| 57 | - { |
|
| 58 | - return 32; |
|
| 59 | - } |
|
| 52 | + /** |
|
| 53 | + * |
|
| 54 | + * {@inheritdoc} |
|
| 55 | + */ |
|
| 56 | + protected function _encKeyLen(): int |
|
| 57 | + { |
|
| 58 | + return 32; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * |
|
| 63 | - * {@inheritdoc} |
|
| 64 | - */ |
|
| 65 | - protected function _macKeyLen(): int |
|
| 66 | - { |
|
| 67 | - return 32; |
|
| 68 | - } |
|
| 61 | + /** |
|
| 62 | + * |
|
| 63 | + * {@inheritdoc} |
|
| 64 | + */ |
|
| 65 | + protected function _macKeyLen(): int |
|
| 66 | + { |
|
| 67 | + return 32; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * |
|
| 72 | - * {@inheritdoc} |
|
| 73 | - */ |
|
| 74 | - protected function _tagLen(): int |
|
| 75 | - { |
|
| 76 | - return 32; |
|
| 77 | - } |
|
| 70 | + /** |
|
| 71 | + * |
|
| 72 | + * {@inheritdoc} |
|
| 73 | + */ |
|
| 74 | + protected function _tagLen(): int |
|
| 75 | + { |
|
| 76 | + return 32; |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -13,66 +13,66 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class A192CBCHS384Algorithm extends AESCBCAlgorithm |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * {@inheritdoc} |
|
| 19 | - */ |
|
| 20 | - public function keySize(): int |
|
| 21 | - { |
|
| 22 | - return 48; |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * {@inheritdoc} |
|
| 19 | + */ |
|
| 20 | + public function keySize(): int |
|
| 21 | + { |
|
| 22 | + return 48; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - public function encryptionAlgorithmParamValue(): string |
|
| 30 | - { |
|
| 31 | - return JWA::ALGO_A192CBC_HS384; |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + public function encryptionAlgorithmParamValue(): string |
|
| 30 | + { |
|
| 31 | + return JWA::ALGO_A192CBC_HS384; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * |
|
| 36 | - * {@inheritdoc} |
|
| 37 | - */ |
|
| 38 | - protected function _cipherMethod(): string |
|
| 39 | - { |
|
| 40 | - return "AES-192-CBC"; |
|
| 41 | - } |
|
| 34 | + /** |
|
| 35 | + * |
|
| 36 | + * {@inheritdoc} |
|
| 37 | + */ |
|
| 38 | + protected function _cipherMethod(): string |
|
| 39 | + { |
|
| 40 | + return "AES-192-CBC"; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * |
|
| 45 | - * {@inheritdoc} |
|
| 46 | - */ |
|
| 47 | - protected function _hashAlgo(): string |
|
| 48 | - { |
|
| 49 | - return "sha384"; |
|
| 50 | - } |
|
| 43 | + /** |
|
| 44 | + * |
|
| 45 | + * {@inheritdoc} |
|
| 46 | + */ |
|
| 47 | + protected function _hashAlgo(): string |
|
| 48 | + { |
|
| 49 | + return "sha384"; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * |
|
| 54 | - * {@inheritdoc} |
|
| 55 | - */ |
|
| 56 | - protected function _encKeyLen(): int |
|
| 57 | - { |
|
| 58 | - return 24; |
|
| 59 | - } |
|
| 52 | + /** |
|
| 53 | + * |
|
| 54 | + * {@inheritdoc} |
|
| 55 | + */ |
|
| 56 | + protected function _encKeyLen(): int |
|
| 57 | + { |
|
| 58 | + return 24; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * |
|
| 63 | - * {@inheritdoc} |
|
| 64 | - */ |
|
| 65 | - protected function _macKeyLen(): int |
|
| 66 | - { |
|
| 67 | - return 24; |
|
| 68 | - } |
|
| 61 | + /** |
|
| 62 | + * |
|
| 63 | + * {@inheritdoc} |
|
| 64 | + */ |
|
| 65 | + protected function _macKeyLen(): int |
|
| 66 | + { |
|
| 67 | + return 24; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * |
|
| 72 | - * {@inheritdoc} |
|
| 73 | - */ |
|
| 74 | - protected function _tagLen(): int |
|
| 75 | - { |
|
| 76 | - return 24; |
|
| 77 | - } |
|
| 70 | + /** |
|
| 71 | + * |
|
| 72 | + * {@inheritdoc} |
|
| 73 | + */ |
|
| 74 | + protected function _tagLen(): int |
|
| 75 | + { |
|
| 76 | + return 24; |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -13,55 +13,55 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | abstract class EncryptionAlgorithmFactory |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Mapping from algorithm name to class name. |
|
| 18 | - * |
|
| 19 | - * @internal |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - const MAP_ALGO_TO_CLASS = array( |
|
| 24 | - /* @formatter:off */ |
|
| 25 | - JWA::ALGO_A128CBC_HS256 => A128CBCHS256Algorithm::class, |
|
| 26 | - JWA::ALGO_A192CBC_HS384 => A192CBCHS384Algorithm::class, |
|
| 27 | - JWA::ALGO_A256CBC_HS512 => A256CBCHS512Algorithm::class, |
|
| 28 | - JWA::ALGO_A128GCM => A128GCMAlgorithm::class, |
|
| 29 | - JWA::ALGO_A192GCM => A192GCMAlgorithm::class, |
|
| 30 | - JWA::ALGO_A256GCM => A256GCMAlgorithm::class |
|
| 31 | - /* @formatter:on */ |
|
| 32 | - ); |
|
| 16 | + /** |
|
| 17 | + * Mapping from algorithm name to class name. |
|
| 18 | + * |
|
| 19 | + * @internal |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + const MAP_ALGO_TO_CLASS = array( |
|
| 24 | + /* @formatter:off */ |
|
| 25 | + JWA::ALGO_A128CBC_HS256 => A128CBCHS256Algorithm::class, |
|
| 26 | + JWA::ALGO_A192CBC_HS384 => A192CBCHS384Algorithm::class, |
|
| 27 | + JWA::ALGO_A256CBC_HS512 => A256CBCHS512Algorithm::class, |
|
| 28 | + JWA::ALGO_A128GCM => A128GCMAlgorithm::class, |
|
| 29 | + JWA::ALGO_A192GCM => A192GCMAlgorithm::class, |
|
| 30 | + JWA::ALGO_A256GCM => A256GCMAlgorithm::class |
|
| 31 | + /* @formatter:on */ |
|
| 32 | + ); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Get the content encryption algorithm by algorithm name. |
|
| 36 | - * |
|
| 37 | - * @param string $name Algorithm name |
|
| 38 | - * @throws \UnexpectedValueException If algorithm is not supported. |
|
| 39 | - * @return ContentEncryptionAlgorithm |
|
| 40 | - */ |
|
| 41 | - public static function algoByName(string $name): ContentEncryptionAlgorithm |
|
| 42 | - { |
|
| 43 | - if (!array_key_exists($name, self::MAP_ALGO_TO_CLASS)) { |
|
| 44 | - throw new \UnexpectedValueException( |
|
| 45 | - "No content encryption algorithm '$name'."); |
|
| 46 | - } |
|
| 47 | - $cls = self::MAP_ALGO_TO_CLASS[$name]; |
|
| 48 | - return new $cls(); |
|
| 49 | - } |
|
| 34 | + /** |
|
| 35 | + * Get the content encryption algorithm by algorithm name. |
|
| 36 | + * |
|
| 37 | + * @param string $name Algorithm name |
|
| 38 | + * @throws \UnexpectedValueException If algorithm is not supported. |
|
| 39 | + * @return ContentEncryptionAlgorithm |
|
| 40 | + */ |
|
| 41 | + public static function algoByName(string $name): ContentEncryptionAlgorithm |
|
| 42 | + { |
|
| 43 | + if (!array_key_exists($name, self::MAP_ALGO_TO_CLASS)) { |
|
| 44 | + throw new \UnexpectedValueException( |
|
| 45 | + "No content encryption algorithm '$name'."); |
|
| 46 | + } |
|
| 47 | + $cls = self::MAP_ALGO_TO_CLASS[$name]; |
|
| 48 | + return new $cls(); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Get the content encryption algorithm as specified in the given header. |
|
| 53 | - * |
|
| 54 | - * @param Header $header Header |
|
| 55 | - * @throws \UnexpectedValueException If content encryption algorithm |
|
| 56 | - * parameter is not present or algorithm is not supported. |
|
| 57 | - * @return ContentEncryptionAlgorithm |
|
| 58 | - */ |
|
| 59 | - public static function algoByHeader(Header $header): ContentEncryptionAlgorithm |
|
| 60 | - { |
|
| 61 | - if (!$header->hasEncryptionAlgorithm()) { |
|
| 62 | - throw new \UnexpectedValueException( |
|
| 63 | - "No encryption algorithm parameter."); |
|
| 64 | - } |
|
| 65 | - return self::algoByName($header->encryptionAlgorithm()->value()); |
|
| 66 | - } |
|
| 51 | + /** |
|
| 52 | + * Get the content encryption algorithm as specified in the given header. |
|
| 53 | + * |
|
| 54 | + * @param Header $header Header |
|
| 55 | + * @throws \UnexpectedValueException If content encryption algorithm |
|
| 56 | + * parameter is not present or algorithm is not supported. |
|
| 57 | + * @return ContentEncryptionAlgorithm |
|
| 58 | + */ |
|
| 59 | + public static function algoByHeader(Header $header): ContentEncryptionAlgorithm |
|
| 60 | + { |
|
| 61 | + if (!$header->hasEncryptionAlgorithm()) { |
|
| 62 | + throw new \UnexpectedValueException( |
|
| 63 | + "No encryption algorithm parameter."); |
|
| 64 | + } |
|
| 65 | + return self::algoByName($header->encryptionAlgorithm()->value()); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -15,30 +15,30 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class A192GCMAlgorithm extends AESGCMAlgorithm |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * |
|
| 20 | - * {@inheritdoc} |
|
| 21 | - */ |
|
| 22 | - public function encryptionAlgorithmParamValue(): string |
|
| 23 | - { |
|
| 24 | - return JWA::ALGO_A192GCM; |
|
| 25 | - } |
|
| 18 | + /** |
|
| 19 | + * |
|
| 20 | + * {@inheritdoc} |
|
| 21 | + */ |
|
| 22 | + public function encryptionAlgorithmParamValue(): string |
|
| 23 | + { |
|
| 24 | + return JWA::ALGO_A192GCM; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - public function keySize(): int |
|
| 32 | - { |
|
| 33 | - return 24; |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + public function keySize(): int |
|
| 32 | + { |
|
| 33 | + return 24; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * |
|
| 38 | - * {@inheritdoc} |
|
| 39 | - */ |
|
| 40 | - protected function _getGCMCipher(): Cipher |
|
| 41 | - { |
|
| 42 | - return new AES192Cipher(); |
|
| 43 | - } |
|
| 36 | + /** |
|
| 37 | + * |
|
| 38 | + * {@inheritdoc} |
|
| 39 | + */ |
|
| 40 | + protected function _getGCMCipher(): Cipher |
|
| 41 | + { |
|
| 42 | + return new AES192Cipher(); |
|
| 43 | + } |
|
| 44 | 44 | } |
@@ -15,30 +15,30 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class A128GCMAlgorithm extends AESGCMAlgorithm |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * |
|
| 20 | - * {@inheritdoc} |
|
| 21 | - */ |
|
| 22 | - public function encryptionAlgorithmParamValue(): string |
|
| 23 | - { |
|
| 24 | - return JWA::ALGO_A128GCM; |
|
| 25 | - } |
|
| 18 | + /** |
|
| 19 | + * |
|
| 20 | + * {@inheritdoc} |
|
| 21 | + */ |
|
| 22 | + public function encryptionAlgorithmParamValue(): string |
|
| 23 | + { |
|
| 24 | + return JWA::ALGO_A128GCM; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - public function keySize(): int |
|
| 32 | - { |
|
| 33 | - return 16; |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + public function keySize(): int |
|
| 32 | + { |
|
| 33 | + return 16; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * |
|
| 38 | - * {@inheritdoc} |
|
| 39 | - */ |
|
| 40 | - protected function _getGCMCipher(): Cipher |
|
| 41 | - { |
|
| 42 | - return new AES128Cipher(); |
|
| 43 | - } |
|
| 36 | + /** |
|
| 37 | + * |
|
| 38 | + * {@inheritdoc} |
|
| 39 | + */ |
|
| 40 | + protected function _getGCMCipher(): Cipher |
|
| 41 | + { |
|
| 42 | + return new AES128Cipher(); |
|
| 43 | + } |
|
| 44 | 44 | } |