@@ -12,30 +12,30 @@ |
||
12 | 12 | */ |
13 | 13 | class A128KWAlgorithm extends AESKWAlgorithm |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - protected function _kekSize() |
|
20 | - { |
|
21 | - return 16; |
|
22 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + protected function _kekSize() |
|
20 | + { |
|
21 | + return 16; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected function _AESKWAlgo() |
|
29 | - { |
|
30 | - return new AESKW128(); |
|
31 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected function _AESKWAlgo() |
|
29 | + { |
|
30 | + return new AESKW128(); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - public function algorithmParamValue() |
|
38 | - { |
|
39 | - return JWA::ALGO_A128KW; |
|
40 | - } |
|
33 | + /** |
|
34 | + * |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + public function algorithmParamValue() |
|
38 | + { |
|
39 | + return JWA::ALGO_A128KW; |
|
40 | + } |
|
41 | 41 | } |
@@ -19,200 +19,200 @@ |
||
19 | 19 | */ |
20 | 20 | abstract class PBES2Algorithm extends KeyManagementAlgorithm |
21 | 21 | { |
22 | - use RandomCEK; |
|
23 | - |
|
24 | - /** |
|
25 | - * Password. |
|
26 | - * |
|
27 | - * @var string $_password |
|
28 | - */ |
|
29 | - protected $_password; |
|
30 | - |
|
31 | - /** |
|
32 | - * Salt input. |
|
33 | - * |
|
34 | - * @var string $_salt |
|
35 | - */ |
|
36 | - protected $_saltInput; |
|
37 | - |
|
38 | - /** |
|
39 | - * Iteration count. |
|
40 | - * |
|
41 | - * @var int $_count |
|
42 | - */ |
|
43 | - protected $_count; |
|
44 | - |
|
45 | - /** |
|
46 | - * Derived key. |
|
47 | - * |
|
48 | - * @var string |
|
49 | - */ |
|
50 | - private $_derivedKey; |
|
51 | - |
|
52 | - /** |
|
53 | - * Mapping from algorithm name to class name. |
|
54 | - * |
|
55 | - * @internal |
|
56 | - * |
|
57 | - * @var array |
|
58 | - */ |
|
59 | - const MAP_ALGO_TO_CLASS = array( |
|
60 | - /* @formatter:off */ |
|
61 | - JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class, |
|
62 | - JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class, |
|
63 | - JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class |
|
64 | - /* @formatter:on */ |
|
65 | - ); |
|
66 | - |
|
67 | - /** |
|
68 | - * Get hash algorithm for hash_pbkdf2. |
|
69 | - * |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - abstract protected function _hashAlgo(); |
|
73 | - |
|
74 | - /** |
|
75 | - * Get derived key length. |
|
76 | - * |
|
77 | - * @return int |
|
78 | - */ |
|
79 | - abstract protected function _keyLength(); |
|
80 | - |
|
81 | - /** |
|
82 | - * Get key wrapping algoritym. |
|
83 | - * |
|
84 | - * @return \AESKW\AESKeyWrapAlgorithm |
|
85 | - */ |
|
86 | - abstract protected function _kwAlgo(); |
|
87 | - |
|
88 | - /** |
|
89 | - * Constructor. |
|
90 | - * |
|
91 | - * @param string $password Password |
|
92 | - * @param string $salt_input Salt input |
|
93 | - * @param int $count Iteration count |
|
94 | - */ |
|
95 | - public function __construct($password, $salt_input, $count) |
|
96 | - { |
|
97 | - $this->_password = $password; |
|
98 | - $this->_saltInput = $salt_input; |
|
99 | - $this->_count = $count; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * |
|
104 | - * @param JWK $jwk |
|
105 | - * @param Header $header |
|
106 | - * @throws \UnexpectedValueException |
|
107 | - * @return PBES2Algorithm |
|
108 | - */ |
|
109 | - public static function fromJWK(JWK $jwk, Header $header) |
|
110 | - { |
|
111 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
112 | - if (!$header->hasPBES2SaltInput()) { |
|
113 | - throw new \UnexpectedValueException("No salt input."); |
|
114 | - } |
|
115 | - $salt_input = $header->PBES2SaltInput()->saltInput(); |
|
116 | - if (!$header->hasPBES2Count()) { |
|
117 | - throw new \UnexpectedValueException("No iteration count."); |
|
118 | - } |
|
119 | - $count = $header->PBES2Count()->value(); |
|
120 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
121 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
122 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
123 | - } |
|
124 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
125 | - return new $cls($jwk->key(), $salt_input, $count); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Initialize from a password with random salt and default iteration count. |
|
130 | - * |
|
131 | - * @param string $password Password |
|
132 | - * @param int $count Optional user defined iteration count |
|
133 | - * @param int $salt_bytes Optional user defined salt length |
|
134 | - * @return self |
|
135 | - */ |
|
136 | - public static function fromPassword($password, $count = 64000, $salt_bytes = 8) |
|
137 | - { |
|
138 | - $salt_input = openssl_random_pseudo_bytes($salt_bytes); |
|
139 | - return new static($password, $salt_input, $count); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Get salt input. |
|
144 | - * |
|
145 | - * @return string |
|
146 | - */ |
|
147 | - public function saltInput() |
|
148 | - { |
|
149 | - return $this->_saltInput; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Get computed salt. |
|
154 | - * |
|
155 | - * @return string |
|
156 | - */ |
|
157 | - public function salt() |
|
158 | - { |
|
159 | - return PBES2SaltInputParameter::fromString($this->_saltInput)->salt( |
|
160 | - AlgorithmParameter::fromAlgorithm($this)); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Get iteration count. |
|
165 | - * |
|
166 | - * @return int |
|
167 | - */ |
|
168 | - public function iterationCount() |
|
169 | - { |
|
170 | - return $this->_count; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Get derived key. |
|
175 | - * |
|
176 | - * @return string |
|
177 | - */ |
|
178 | - protected function _derivedKey() |
|
179 | - { |
|
180 | - if (!isset($this->_derivedKey)) { |
|
181 | - $this->_derivedKey = hash_pbkdf2($this->_hashAlgo(), |
|
182 | - $this->_password, $this->salt(), $this->_count, |
|
183 | - $this->_keyLength(), true); |
|
184 | - } |
|
185 | - return $this->_derivedKey; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * |
|
190 | - * {@inheritdoc} |
|
191 | - */ |
|
192 | - protected function _encryptKey($key, Header &$header) |
|
193 | - { |
|
194 | - return $this->_kwAlgo()->wrap($key, $this->_derivedKey()); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * |
|
199 | - * {@inheritdoc} |
|
200 | - */ |
|
201 | - protected function _decryptKey($ciphertext, Header $header) |
|
202 | - { |
|
203 | - return $this->_kwAlgo()->unwrap($ciphertext, $this->_derivedKey()); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * |
|
208 | - * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
209 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
210 | - */ |
|
211 | - public function headerParameters() |
|
212 | - { |
|
213 | - return array_merge(parent::headerParameters(), |
|
214 | - array(AlgorithmParameter::fromAlgorithm($this), |
|
215 | - PBES2SaltInputParameter::fromString($this->_saltInput), |
|
216 | - new PBES2CountParameter($this->_count))); |
|
217 | - } |
|
22 | + use RandomCEK; |
|
23 | + |
|
24 | + /** |
|
25 | + * Password. |
|
26 | + * |
|
27 | + * @var string $_password |
|
28 | + */ |
|
29 | + protected $_password; |
|
30 | + |
|
31 | + /** |
|
32 | + * Salt input. |
|
33 | + * |
|
34 | + * @var string $_salt |
|
35 | + */ |
|
36 | + protected $_saltInput; |
|
37 | + |
|
38 | + /** |
|
39 | + * Iteration count. |
|
40 | + * |
|
41 | + * @var int $_count |
|
42 | + */ |
|
43 | + protected $_count; |
|
44 | + |
|
45 | + /** |
|
46 | + * Derived key. |
|
47 | + * |
|
48 | + * @var string |
|
49 | + */ |
|
50 | + private $_derivedKey; |
|
51 | + |
|
52 | + /** |
|
53 | + * Mapping from algorithm name to class name. |
|
54 | + * |
|
55 | + * @internal |
|
56 | + * |
|
57 | + * @var array |
|
58 | + */ |
|
59 | + const MAP_ALGO_TO_CLASS = array( |
|
60 | + /* @formatter:off */ |
|
61 | + JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class, |
|
62 | + JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class, |
|
63 | + JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class |
|
64 | + /* @formatter:on */ |
|
65 | + ); |
|
66 | + |
|
67 | + /** |
|
68 | + * Get hash algorithm for hash_pbkdf2. |
|
69 | + * |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + abstract protected function _hashAlgo(); |
|
73 | + |
|
74 | + /** |
|
75 | + * Get derived key length. |
|
76 | + * |
|
77 | + * @return int |
|
78 | + */ |
|
79 | + abstract protected function _keyLength(); |
|
80 | + |
|
81 | + /** |
|
82 | + * Get key wrapping algoritym. |
|
83 | + * |
|
84 | + * @return \AESKW\AESKeyWrapAlgorithm |
|
85 | + */ |
|
86 | + abstract protected function _kwAlgo(); |
|
87 | + |
|
88 | + /** |
|
89 | + * Constructor. |
|
90 | + * |
|
91 | + * @param string $password Password |
|
92 | + * @param string $salt_input Salt input |
|
93 | + * @param int $count Iteration count |
|
94 | + */ |
|
95 | + public function __construct($password, $salt_input, $count) |
|
96 | + { |
|
97 | + $this->_password = $password; |
|
98 | + $this->_saltInput = $salt_input; |
|
99 | + $this->_count = $count; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * |
|
104 | + * @param JWK $jwk |
|
105 | + * @param Header $header |
|
106 | + * @throws \UnexpectedValueException |
|
107 | + * @return PBES2Algorithm |
|
108 | + */ |
|
109 | + public static function fromJWK(JWK $jwk, Header $header) |
|
110 | + { |
|
111 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
112 | + if (!$header->hasPBES2SaltInput()) { |
|
113 | + throw new \UnexpectedValueException("No salt input."); |
|
114 | + } |
|
115 | + $salt_input = $header->PBES2SaltInput()->saltInput(); |
|
116 | + if (!$header->hasPBES2Count()) { |
|
117 | + throw new \UnexpectedValueException("No iteration count."); |
|
118 | + } |
|
119 | + $count = $header->PBES2Count()->value(); |
|
120 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
121 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
122 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
123 | + } |
|
124 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
125 | + return new $cls($jwk->key(), $salt_input, $count); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Initialize from a password with random salt and default iteration count. |
|
130 | + * |
|
131 | + * @param string $password Password |
|
132 | + * @param int $count Optional user defined iteration count |
|
133 | + * @param int $salt_bytes Optional user defined salt length |
|
134 | + * @return self |
|
135 | + */ |
|
136 | + public static function fromPassword($password, $count = 64000, $salt_bytes = 8) |
|
137 | + { |
|
138 | + $salt_input = openssl_random_pseudo_bytes($salt_bytes); |
|
139 | + return new static($password, $salt_input, $count); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Get salt input. |
|
144 | + * |
|
145 | + * @return string |
|
146 | + */ |
|
147 | + public function saltInput() |
|
148 | + { |
|
149 | + return $this->_saltInput; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Get computed salt. |
|
154 | + * |
|
155 | + * @return string |
|
156 | + */ |
|
157 | + public function salt() |
|
158 | + { |
|
159 | + return PBES2SaltInputParameter::fromString($this->_saltInput)->salt( |
|
160 | + AlgorithmParameter::fromAlgorithm($this)); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Get iteration count. |
|
165 | + * |
|
166 | + * @return int |
|
167 | + */ |
|
168 | + public function iterationCount() |
|
169 | + { |
|
170 | + return $this->_count; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Get derived key. |
|
175 | + * |
|
176 | + * @return string |
|
177 | + */ |
|
178 | + protected function _derivedKey() |
|
179 | + { |
|
180 | + if (!isset($this->_derivedKey)) { |
|
181 | + $this->_derivedKey = hash_pbkdf2($this->_hashAlgo(), |
|
182 | + $this->_password, $this->salt(), $this->_count, |
|
183 | + $this->_keyLength(), true); |
|
184 | + } |
|
185 | + return $this->_derivedKey; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * |
|
190 | + * {@inheritdoc} |
|
191 | + */ |
|
192 | + protected function _encryptKey($key, Header &$header) |
|
193 | + { |
|
194 | + return $this->_kwAlgo()->wrap($key, $this->_derivedKey()); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * |
|
199 | + * {@inheritdoc} |
|
200 | + */ |
|
201 | + protected function _decryptKey($ciphertext, Header $header) |
|
202 | + { |
|
203 | + return $this->_kwAlgo()->unwrap($ciphertext, $this->_derivedKey()); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * |
|
208 | + * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
209 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
210 | + */ |
|
211 | + public function headerParameters() |
|
212 | + { |
|
213 | + return array_merge(parent::headerParameters(), |
|
214 | + array(AlgorithmParameter::fromAlgorithm($this), |
|
215 | + PBES2SaltInputParameter::fromString($this->_saltInput), |
|
216 | + new PBES2CountParameter($this->_count))); |
|
217 | + } |
|
218 | 218 | } |
@@ -189,7 +189,7 @@ |
||
189 | 189 | * |
190 | 190 | * {@inheritdoc} |
191 | 191 | */ |
192 | - protected function _encryptKey($key, Header &$header) |
|
192 | + protected function _encryptKey($key, Header&$header) |
|
193 | 193 | { |
194 | 194 | return $this->_kwAlgo()->wrap($key, $this->_derivedKey()); |
195 | 195 | } |
@@ -11,21 +11,21 @@ |
||
11 | 11 | */ |
12 | 12 | class RSAESPKCS1Algorithm extends RSAESKeyAlgorithm |
13 | 13 | { |
14 | - /** |
|
15 | - * |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - protected function _paddingScheme() |
|
19 | - { |
|
20 | - return OPENSSL_PKCS1_PADDING; |
|
21 | - } |
|
14 | + /** |
|
15 | + * |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + protected function _paddingScheme() |
|
19 | + { |
|
20 | + return OPENSSL_PKCS1_PADDING; |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - public function algorithmParamValue() |
|
28 | - { |
|
29 | - return JWA::ALGO_RSA1_5; |
|
30 | - } |
|
23 | + /** |
|
24 | + * |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + public function algorithmParamValue() |
|
28 | + { |
|
29 | + return JWA::ALGO_RSA1_5; |
|
30 | + } |
|
31 | 31 | } |
@@ -19,206 +19,206 @@ |
||
19 | 19 | */ |
20 | 20 | abstract class RSAESKeyAlgorithm extends KeyManagementAlgorithm |
21 | 21 | { |
22 | - use RandomCEK; |
|
22 | + use RandomCEK; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Public key. |
|
26 | - * |
|
27 | - * @var RSAPublicKeyJWK $_publicKey |
|
28 | - */ |
|
29 | - protected $_publicKey; |
|
24 | + /** |
|
25 | + * Public key. |
|
26 | + * |
|
27 | + * @var RSAPublicKeyJWK $_publicKey |
|
28 | + */ |
|
29 | + protected $_publicKey; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Private key. |
|
33 | - * |
|
34 | - * @var RSAPrivateKeyJWK|null $_privateKey |
|
35 | - */ |
|
36 | - protected $_privateKey; |
|
31 | + /** |
|
32 | + * Private key. |
|
33 | + * |
|
34 | + * @var RSAPrivateKeyJWK|null $_privateKey |
|
35 | + */ |
|
36 | + protected $_privateKey; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Mapping from algorithm name to class name. |
|
40 | - * |
|
41 | - * @internal |
|
42 | - * |
|
43 | - * @var array |
|
44 | - */ |
|
45 | - const MAP_ALGO_TO_CLASS = array( |
|
46 | - /* @formatter:off */ |
|
47 | - JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
|
48 | - JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class |
|
49 | - /* @formatter:on */ |
|
50 | - ); |
|
38 | + /** |
|
39 | + * Mapping from algorithm name to class name. |
|
40 | + * |
|
41 | + * @internal |
|
42 | + * |
|
43 | + * @var array |
|
44 | + */ |
|
45 | + const MAP_ALGO_TO_CLASS = array( |
|
46 | + /* @formatter:off */ |
|
47 | + JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
|
48 | + JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class |
|
49 | + /* @formatter:on */ |
|
50 | + ); |
|
51 | 51 | |
52 | - /** |
|
53 | - * Get the padding scheme. |
|
54 | - * |
|
55 | - * @return int |
|
56 | - */ |
|
57 | - abstract protected function _paddingScheme(); |
|
52 | + /** |
|
53 | + * Get the padding scheme. |
|
54 | + * |
|
55 | + * @return int |
|
56 | + */ |
|
57 | + abstract protected function _paddingScheme(); |
|
58 | 58 | |
59 | - /** |
|
60 | - * Constructor. |
|
61 | - * |
|
62 | - * Use <code>fromPublicKey</code> or <code>fromPrivateKey</code> instead! |
|
63 | - * |
|
64 | - * @param RSAPublicKeyJWK $pub_key RSA public key |
|
65 | - * @param RSAPrivateKeyJWK $priv_key Optional RSA private key |
|
66 | - */ |
|
67 | - protected function __construct(RSAPublicKeyJWK $pub_key, |
|
68 | - RSAPrivateKeyJWK $priv_key = null) |
|
69 | - { |
|
70 | - $this->_publicKey = $pub_key; |
|
71 | - $this->_privateKey = $priv_key; |
|
72 | - } |
|
59 | + /** |
|
60 | + * Constructor. |
|
61 | + * |
|
62 | + * Use <code>fromPublicKey</code> or <code>fromPrivateKey</code> instead! |
|
63 | + * |
|
64 | + * @param RSAPublicKeyJWK $pub_key RSA public key |
|
65 | + * @param RSAPrivateKeyJWK $priv_key Optional RSA private key |
|
66 | + */ |
|
67 | + protected function __construct(RSAPublicKeyJWK $pub_key, |
|
68 | + RSAPrivateKeyJWK $priv_key = null) |
|
69 | + { |
|
70 | + $this->_publicKey = $pub_key; |
|
71 | + $this->_privateKey = $priv_key; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * |
|
76 | - * @param JWK $jwk |
|
77 | - * @param Header $header |
|
78 | - * @throws \UnexpectedValueException |
|
79 | - * @return RSAESKeyAlgorithm |
|
80 | - */ |
|
81 | - public static function fromJWK(JWK $jwk, Header $header) |
|
82 | - { |
|
83 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
84 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
85 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
86 | - } |
|
87 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
88 | - if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
89 | - return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
90 | - } |
|
91 | - return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
92 | - } |
|
74 | + /** |
|
75 | + * |
|
76 | + * @param JWK $jwk |
|
77 | + * @param Header $header |
|
78 | + * @throws \UnexpectedValueException |
|
79 | + * @return RSAESKeyAlgorithm |
|
80 | + */ |
|
81 | + public static function fromJWK(JWK $jwk, Header $header) |
|
82 | + { |
|
83 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
84 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
85 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
86 | + } |
|
87 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
88 | + if ($jwk->has(...RSAPrivateKeyJWK::MANAGED_PARAMS)) { |
|
89 | + return $cls::fromPrivateKey(RSAPrivateKeyJWK::fromJWK($jwk)); |
|
90 | + } |
|
91 | + return $cls::fromPublicKey(RSAPublicKeyJWK::fromJWK($jwk)); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Initialize from a public key. |
|
96 | - * |
|
97 | - * @param RSAPublicKeyJWK $jwk |
|
98 | - * @return self |
|
99 | - */ |
|
100 | - public static function fromPublicKey(RSAPublicKeyJWK $jwk) |
|
101 | - { |
|
102 | - return new static($jwk); |
|
103 | - } |
|
94 | + /** |
|
95 | + * Initialize from a public key. |
|
96 | + * |
|
97 | + * @param RSAPublicKeyJWK $jwk |
|
98 | + * @return self |
|
99 | + */ |
|
100 | + public static function fromPublicKey(RSAPublicKeyJWK $jwk) |
|
101 | + { |
|
102 | + return new static($jwk); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Initialize from a private key. |
|
107 | - * |
|
108 | - * @param RSAPrivateKeyJWK $jwk |
|
109 | - * @return self |
|
110 | - */ |
|
111 | - public static function fromPrivateKey(RSAPrivateKeyJWK $jwk) |
|
112 | - { |
|
113 | - return new static($jwk->publicKey(), $jwk); |
|
114 | - } |
|
105 | + /** |
|
106 | + * Initialize from a private key. |
|
107 | + * |
|
108 | + * @param RSAPrivateKeyJWK $jwk |
|
109 | + * @return self |
|
110 | + */ |
|
111 | + public static function fromPrivateKey(RSAPrivateKeyJWK $jwk) |
|
112 | + { |
|
113 | + return new static($jwk->publicKey(), $jwk); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Get the public key. |
|
118 | - * |
|
119 | - * @return RSAPublicKeyJWK |
|
120 | - */ |
|
121 | - public function publicKey() |
|
122 | - { |
|
123 | - return $this->_publicKey; |
|
124 | - } |
|
116 | + /** |
|
117 | + * Get the public key. |
|
118 | + * |
|
119 | + * @return RSAPublicKeyJWK |
|
120 | + */ |
|
121 | + public function publicKey() |
|
122 | + { |
|
123 | + return $this->_publicKey; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * Check whether the private key is present. |
|
128 | - * |
|
129 | - * @return bool |
|
130 | - */ |
|
131 | - public function hasPrivateKey() |
|
132 | - { |
|
133 | - return isset($this->_privateKey); |
|
134 | - } |
|
126 | + /** |
|
127 | + * Check whether the private key is present. |
|
128 | + * |
|
129 | + * @return bool |
|
130 | + */ |
|
131 | + public function hasPrivateKey() |
|
132 | + { |
|
133 | + return isset($this->_privateKey); |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * Get the private key. |
|
138 | - * |
|
139 | - * @throws \LogicException |
|
140 | - * @return RSAPrivateKeyJWK |
|
141 | - */ |
|
142 | - public function privateKey() |
|
143 | - { |
|
144 | - if (!$this->hasPrivateKey()) { |
|
145 | - throw new \LogicException("Private key not set."); |
|
146 | - } |
|
147 | - return $this->_privateKey; |
|
148 | - } |
|
136 | + /** |
|
137 | + * Get the private key. |
|
138 | + * |
|
139 | + * @throws \LogicException |
|
140 | + * @return RSAPrivateKeyJWK |
|
141 | + */ |
|
142 | + public function privateKey() |
|
143 | + { |
|
144 | + if (!$this->hasPrivateKey()) { |
|
145 | + throw new \LogicException("Private key not set."); |
|
146 | + } |
|
147 | + return $this->_privateKey; |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * |
|
152 | - * {@inheritdoc} |
|
153 | - */ |
|
154 | - protected function _encryptKey($key, Header &$header) |
|
155 | - { |
|
156 | - $pubkey = openssl_pkey_get_public( |
|
157 | - $this->publicKey() |
|
158 | - ->toPEM() |
|
159 | - ->string()); |
|
160 | - if (false === $pubkey) { |
|
161 | - throw new \RuntimeException( |
|
162 | - "openssl_pkey_get_public() failed: " . |
|
163 | - $this->_getLastOpenSSLError()); |
|
164 | - } |
|
165 | - $result = openssl_public_encrypt($key, $crypted, $pubkey, |
|
166 | - $this->_paddingScheme()); |
|
167 | - if (!$result) { |
|
168 | - throw new \RuntimeException( |
|
169 | - "openssl_public_encrypt() failed: " . |
|
170 | - $this->_getLastOpenSSLError()); |
|
171 | - } |
|
172 | - return $crypted; |
|
173 | - } |
|
150 | + /** |
|
151 | + * |
|
152 | + * {@inheritdoc} |
|
153 | + */ |
|
154 | + protected function _encryptKey($key, Header &$header) |
|
155 | + { |
|
156 | + $pubkey = openssl_pkey_get_public( |
|
157 | + $this->publicKey() |
|
158 | + ->toPEM() |
|
159 | + ->string()); |
|
160 | + if (false === $pubkey) { |
|
161 | + throw new \RuntimeException( |
|
162 | + "openssl_pkey_get_public() failed: " . |
|
163 | + $this->_getLastOpenSSLError()); |
|
164 | + } |
|
165 | + $result = openssl_public_encrypt($key, $crypted, $pubkey, |
|
166 | + $this->_paddingScheme()); |
|
167 | + if (!$result) { |
|
168 | + throw new \RuntimeException( |
|
169 | + "openssl_public_encrypt() failed: " . |
|
170 | + $this->_getLastOpenSSLError()); |
|
171 | + } |
|
172 | + return $crypted; |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * |
|
177 | - * {@inheritdoc} |
|
178 | - */ |
|
179 | - protected function _decryptKey($ciphertext, Header $header) |
|
180 | - { |
|
181 | - $privkey = openssl_pkey_get_private( |
|
182 | - $this->privateKey() |
|
183 | - ->toPEM() |
|
184 | - ->string()); |
|
185 | - if (!$privkey) { |
|
186 | - throw new \RuntimeException( |
|
187 | - "openssl_pkey_get_private() failed: " . |
|
188 | - $this->_getLastOpenSSLError()); |
|
189 | - } |
|
190 | - $result = openssl_private_decrypt($ciphertext, $cek, $privkey, |
|
191 | - $this->_paddingScheme()); |
|
192 | - if (!$result) { |
|
193 | - throw new \RuntimeException( |
|
194 | - "openssl_private_decrypt() failed: " . |
|
195 | - $this->_getLastOpenSSLError()); |
|
196 | - } |
|
197 | - return $cek; |
|
198 | - } |
|
175 | + /** |
|
176 | + * |
|
177 | + * {@inheritdoc} |
|
178 | + */ |
|
179 | + protected function _decryptKey($ciphertext, Header $header) |
|
180 | + { |
|
181 | + $privkey = openssl_pkey_get_private( |
|
182 | + $this->privateKey() |
|
183 | + ->toPEM() |
|
184 | + ->string()); |
|
185 | + if (!$privkey) { |
|
186 | + throw new \RuntimeException( |
|
187 | + "openssl_pkey_get_private() failed: " . |
|
188 | + $this->_getLastOpenSSLError()); |
|
189 | + } |
|
190 | + $result = openssl_private_decrypt($ciphertext, $cek, $privkey, |
|
191 | + $this->_paddingScheme()); |
|
192 | + if (!$result) { |
|
193 | + throw new \RuntimeException( |
|
194 | + "openssl_private_decrypt() failed: " . |
|
195 | + $this->_getLastOpenSSLError()); |
|
196 | + } |
|
197 | + return $cek; |
|
198 | + } |
|
199 | 199 | |
200 | - /** |
|
201 | - * Get last OpenSSL error message. |
|
202 | - * |
|
203 | - * @return string|null |
|
204 | - */ |
|
205 | - protected function _getLastOpenSSLError() |
|
206 | - { |
|
207 | - $msg = null; |
|
208 | - while (false !== ($err = openssl_error_string())) { |
|
209 | - $msg = $err; |
|
210 | - } |
|
211 | - return $msg; |
|
212 | - } |
|
200 | + /** |
|
201 | + * Get last OpenSSL error message. |
|
202 | + * |
|
203 | + * @return string|null |
|
204 | + */ |
|
205 | + protected function _getLastOpenSSLError() |
|
206 | + { |
|
207 | + $msg = null; |
|
208 | + while (false !== ($err = openssl_error_string())) { |
|
209 | + $msg = $err; |
|
210 | + } |
|
211 | + return $msg; |
|
212 | + } |
|
213 | 213 | |
214 | - /** |
|
215 | - * |
|
216 | - * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
217 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
218 | - */ |
|
219 | - public function headerParameters() |
|
220 | - { |
|
221 | - return array_merge(parent::headerParameters(), |
|
222 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
223 | - } |
|
214 | + /** |
|
215 | + * |
|
216 | + * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
217 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
218 | + */ |
|
219 | + public function headerParameters() |
|
220 | + { |
|
221 | + return array_merge(parent::headerParameters(), |
|
222 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
223 | + } |
|
224 | 224 | } |
@@ -151,7 +151,7 @@ |
||
151 | 151 | * |
152 | 152 | * {@inheritdoc} |
153 | 153 | */ |
154 | - protected function _encryptKey($key, Header &$header) |
|
154 | + protected function _encryptKey($key, Header&$header) |
|
155 | 155 | { |
156 | 156 | $pubkey = openssl_pkey_get_public( |
157 | 157 | $this->publicKey() |
@@ -12,30 +12,30 @@ |
||
12 | 12 | */ |
13 | 13 | class A192KWAlgorithm extends AESKWAlgorithm |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - protected function _kekSize() |
|
20 | - { |
|
21 | - return 24; |
|
22 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + protected function _kekSize() |
|
20 | + { |
|
21 | + return 24; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected function _AESKWAlgo() |
|
29 | - { |
|
30 | - return new AESKW192(); |
|
31 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected function _AESKWAlgo() |
|
29 | + { |
|
30 | + return new AESKW192(); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - public function algorithmParamValue() |
|
38 | - { |
|
39 | - return JWA::ALGO_A192KW; |
|
40 | - } |
|
33 | + /** |
|
34 | + * |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + public function algorithmParamValue() |
|
38 | + { |
|
39 | + return JWA::ALGO_A192KW; |
|
40 | + } |
|
41 | 41 | } |
@@ -20,171 +20,171 @@ |
||
20 | 20 | */ |
21 | 21 | abstract class AESGCMKWAlgorithm extends KeyManagementAlgorithm |
22 | 22 | { |
23 | - use RandomCEK; |
|
23 | + use RandomCEK; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Key encryption key. |
|
27 | - * |
|
28 | - * @var string $_kek |
|
29 | - */ |
|
30 | - protected $_kek; |
|
25 | + /** |
|
26 | + * Key encryption key. |
|
27 | + * |
|
28 | + * @var string $_kek |
|
29 | + */ |
|
30 | + protected $_kek; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Initialization vector. |
|
34 | - * |
|
35 | - * @var string $_iv |
|
36 | - */ |
|
37 | - protected $_iv; |
|
32 | + /** |
|
33 | + * Initialization vector. |
|
34 | + * |
|
35 | + * @var string $_iv |
|
36 | + */ |
|
37 | + protected $_iv; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Mapping from algorithm name to class name. |
|
41 | - * |
|
42 | - * @internal |
|
43 | - * |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - const MAP_ALGO_TO_CLASS = array( |
|
47 | - /* @formatter:off */ |
|
48 | - JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
|
49 | - JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
|
50 | - JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class |
|
51 | - /* @formatter:on */ |
|
52 | - ); |
|
39 | + /** |
|
40 | + * Mapping from algorithm name to class name. |
|
41 | + * |
|
42 | + * @internal |
|
43 | + * |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + const MAP_ALGO_TO_CLASS = array( |
|
47 | + /* @formatter:off */ |
|
48 | + JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
|
49 | + JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
|
50 | + JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class |
|
51 | + /* @formatter:on */ |
|
52 | + ); |
|
53 | 53 | |
54 | - /** |
|
55 | - * Required IV size in bytes. |
|
56 | - * |
|
57 | - * @var int |
|
58 | - */ |
|
59 | - const IV_SIZE = 12; |
|
54 | + /** |
|
55 | + * Required IV size in bytes. |
|
56 | + * |
|
57 | + * @var int |
|
58 | + */ |
|
59 | + const IV_SIZE = 12; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Authentication tag size in bytes. |
|
63 | - * |
|
64 | - * @var int |
|
65 | - */ |
|
66 | - const AUTH_TAG_SIZE = 16; |
|
61 | + /** |
|
62 | + * Authentication tag size in bytes. |
|
63 | + * |
|
64 | + * @var int |
|
65 | + */ |
|
66 | + const AUTH_TAG_SIZE = 16; |
|
67 | 67 | |
68 | - /** |
|
69 | - * Get GCM Cipher instance. |
|
70 | - * |
|
71 | - * @return \GCM\Cipher\Cipher |
|
72 | - */ |
|
73 | - abstract protected function _getGCMCipher(); |
|
68 | + /** |
|
69 | + * Get GCM Cipher instance. |
|
70 | + * |
|
71 | + * @return \GCM\Cipher\Cipher |
|
72 | + */ |
|
73 | + abstract protected function _getGCMCipher(); |
|
74 | 74 | |
75 | - /** |
|
76 | - * Get the required key size. |
|
77 | - * |
|
78 | - * @return int |
|
79 | - */ |
|
80 | - abstract protected function _keySize(); |
|
75 | + /** |
|
76 | + * Get the required key size. |
|
77 | + * |
|
78 | + * @return int |
|
79 | + */ |
|
80 | + abstract protected function _keySize(); |
|
81 | 81 | |
82 | - /** |
|
83 | - * Get GCM instance. |
|
84 | - * |
|
85 | - * @return GCM |
|
86 | - */ |
|
87 | - final protected function _getGCM() |
|
88 | - { |
|
89 | - return new GCM($this->_getGCMCipher(), self::AUTH_TAG_SIZE); |
|
90 | - } |
|
82 | + /** |
|
83 | + * Get GCM instance. |
|
84 | + * |
|
85 | + * @return GCM |
|
86 | + */ |
|
87 | + final protected function _getGCM() |
|
88 | + { |
|
89 | + return new GCM($this->_getGCMCipher(), self::AUTH_TAG_SIZE); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Constructor. |
|
94 | - * |
|
95 | - * @param string $kek Key encryption key |
|
96 | - * @param string $iv Initialization vector |
|
97 | - */ |
|
98 | - public function __construct($kek, $iv) |
|
99 | - { |
|
100 | - if (strlen($kek) != $this->_keySize()) { |
|
101 | - throw new \LengthException("Invalid key size."); |
|
102 | - } |
|
103 | - if (strlen($iv) != self::IV_SIZE) { |
|
104 | - throw new \LengthException("Initialization vector must be 96 bits."); |
|
105 | - } |
|
106 | - $this->_kek = $kek; |
|
107 | - $this->_iv = $iv; |
|
108 | - } |
|
92 | + /** |
|
93 | + * Constructor. |
|
94 | + * |
|
95 | + * @param string $kek Key encryption key |
|
96 | + * @param string $iv Initialization vector |
|
97 | + */ |
|
98 | + public function __construct($kek, $iv) |
|
99 | + { |
|
100 | + if (strlen($kek) != $this->_keySize()) { |
|
101 | + throw new \LengthException("Invalid key size."); |
|
102 | + } |
|
103 | + if (strlen($iv) != self::IV_SIZE) { |
|
104 | + throw new \LengthException("Initialization vector must be 96 bits."); |
|
105 | + } |
|
106 | + $this->_kek = $kek; |
|
107 | + $this->_iv = $iv; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * |
|
112 | - * @param JWK $jwk |
|
113 | - * @param Header $header |
|
114 | - * @throws \UnexpectedValueException |
|
115 | - * @return AESGCMKWAlgorithm |
|
116 | - */ |
|
117 | - public static function fromJWK(JWK $jwk, Header $header) |
|
118 | - { |
|
119 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
120 | - if (!$header->hasInitializationVector()) { |
|
121 | - throw new \UnexpectedValueException("No initialization vector."); |
|
122 | - } |
|
123 | - $iv = $header->initializationVector()->initializationVector(); |
|
124 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
125 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
126 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
127 | - } |
|
128 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
129 | - return new $cls($jwk->key(), $iv); |
|
130 | - } |
|
110 | + /** |
|
111 | + * |
|
112 | + * @param JWK $jwk |
|
113 | + * @param Header $header |
|
114 | + * @throws \UnexpectedValueException |
|
115 | + * @return AESGCMKWAlgorithm |
|
116 | + */ |
|
117 | + public static function fromJWK(JWK $jwk, Header $header) |
|
118 | + { |
|
119 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
120 | + if (!$header->hasInitializationVector()) { |
|
121 | + throw new \UnexpectedValueException("No initialization vector."); |
|
122 | + } |
|
123 | + $iv = $header->initializationVector()->initializationVector(); |
|
124 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
125 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
126 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
127 | + } |
|
128 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
129 | + return new $cls($jwk->key(), $iv); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Initialize from key encryption key with random IV. |
|
134 | - * |
|
135 | - * Key size must match the underlying cipher. |
|
136 | - * |
|
137 | - * @param string $key Key encryption key |
|
138 | - * @return self |
|
139 | - */ |
|
140 | - public static function fromKey($key) |
|
141 | - { |
|
142 | - $iv = openssl_random_pseudo_bytes(self::IV_SIZE); |
|
143 | - return new static($key, $iv); |
|
144 | - } |
|
132 | + /** |
|
133 | + * Initialize from key encryption key with random IV. |
|
134 | + * |
|
135 | + * Key size must match the underlying cipher. |
|
136 | + * |
|
137 | + * @param string $key Key encryption key |
|
138 | + * @return self |
|
139 | + */ |
|
140 | + public static function fromKey($key) |
|
141 | + { |
|
142 | + $iv = openssl_random_pseudo_bytes(self::IV_SIZE); |
|
143 | + return new static($key, $iv); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * |
|
148 | - * @see \JWX\JWE\KeyManagementAlgorithm::_encryptKey() |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - protected function _encryptKey($key, Header &$header) |
|
152 | - { |
|
153 | - list($ciphertext, $auth_tag) = $this->_getGCM()->encrypt($key, "", |
|
154 | - $this->_kek, $this->_iv); |
|
155 | - // insert authentication tag to the header |
|
156 | - $header = $header->withParameters( |
|
157 | - AuthenticationTagParameter::fromString($auth_tag)); |
|
158 | - return $ciphertext; |
|
159 | - } |
|
146 | + /** |
|
147 | + * |
|
148 | + * @see \JWX\JWE\KeyManagementAlgorithm::_encryptKey() |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + protected function _encryptKey($key, Header &$header) |
|
152 | + { |
|
153 | + list($ciphertext, $auth_tag) = $this->_getGCM()->encrypt($key, "", |
|
154 | + $this->_kek, $this->_iv); |
|
155 | + // insert authentication tag to the header |
|
156 | + $header = $header->withParameters( |
|
157 | + AuthenticationTagParameter::fromString($auth_tag)); |
|
158 | + return $ciphertext; |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * |
|
163 | - * @see \JWX\JWE\KeyManagementAlgorithm::_decryptKey() |
|
164 | - * @throws \RuntimeException For generic errors |
|
165 | - * @return string |
|
166 | - */ |
|
167 | - protected function _decryptKey($ciphertext, Header $header) |
|
168 | - { |
|
169 | - if (!$header->hasAuthenticationTag()) { |
|
170 | - throw new \RuntimeException( |
|
171 | - "Header doesn't contain authentication tag."); |
|
172 | - } |
|
173 | - $auth_tag = $header->authenticationTag()->authenticationTag(); |
|
174 | - $cek = $this->_getGCM()->decrypt($ciphertext, $auth_tag, "", $this->_kek, |
|
175 | - $this->_iv); |
|
176 | - return $cek; |
|
177 | - } |
|
161 | + /** |
|
162 | + * |
|
163 | + * @see \JWX\JWE\KeyManagementAlgorithm::_decryptKey() |
|
164 | + * @throws \RuntimeException For generic errors |
|
165 | + * @return string |
|
166 | + */ |
|
167 | + protected function _decryptKey($ciphertext, Header $header) |
|
168 | + { |
|
169 | + if (!$header->hasAuthenticationTag()) { |
|
170 | + throw new \RuntimeException( |
|
171 | + "Header doesn't contain authentication tag."); |
|
172 | + } |
|
173 | + $auth_tag = $header->authenticationTag()->authenticationTag(); |
|
174 | + $cek = $this->_getGCM()->decrypt($ciphertext, $auth_tag, "", $this->_kek, |
|
175 | + $this->_iv); |
|
176 | + return $cek; |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * |
|
181 | - * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
182 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
183 | - */ |
|
184 | - public function headerParameters() |
|
185 | - { |
|
186 | - return array_merge(parent::headerParameters(), |
|
187 | - array(AlgorithmParameter::fromAlgorithm($this), |
|
188 | - InitializationVectorParameter::fromString($this->_iv))); |
|
189 | - } |
|
179 | + /** |
|
180 | + * |
|
181 | + * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
182 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
183 | + */ |
|
184 | + public function headerParameters() |
|
185 | + { |
|
186 | + return array_merge(parent::headerParameters(), |
|
187 | + array(AlgorithmParameter::fromAlgorithm($this), |
|
188 | + InitializationVectorParameter::fromString($this->_iv))); |
|
189 | + } |
|
190 | 190 | } |
@@ -148,7 +148,7 @@ |
||
148 | 148 | * @see \JWX\JWE\KeyManagementAlgorithm::_encryptKey() |
149 | 149 | * @return string |
150 | 150 | */ |
151 | - protected function _encryptKey($key, Header &$header) |
|
151 | + protected function _encryptKey($key, Header&$header) |
|
152 | 152 | { |
153 | 153 | list($ciphertext, $auth_tag) = $this->_getGCM()->encrypt($key, "", |
154 | 154 | $this->_kek, $this->_iv); |
@@ -12,30 +12,30 @@ |
||
12 | 12 | */ |
13 | 13 | class A256GCMKWAlgorithm extends AESGCMKWAlgorithm |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - protected function _getGCMCipher() |
|
20 | - { |
|
21 | - return new AES256Cipher(); |
|
22 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + protected function _getGCMCipher() |
|
20 | + { |
|
21 | + return new AES256Cipher(); |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected function _keySize() |
|
29 | - { |
|
30 | - return 32; |
|
31 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected function _keySize() |
|
29 | + { |
|
30 | + return 32; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - public function algorithmParamValue() |
|
38 | - { |
|
39 | - return JWA::ALGO_A256GCMKW; |
|
40 | - } |
|
33 | + /** |
|
34 | + * |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + public function algorithmParamValue() |
|
38 | + { |
|
39 | + return JWA::ALGO_A256GCMKW; |
|
40 | + } |
|
41 | 41 | } |
@@ -12,39 +12,39 @@ |
||
12 | 12 | */ |
13 | 13 | class PBES2HS256A128KWAlgorithm extends PBES2Algorithm |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - protected function _hashAlgo() |
|
20 | - { |
|
21 | - return "sha256"; |
|
22 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + protected function _hashAlgo() |
|
20 | + { |
|
21 | + return "sha256"; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected function _keyLength() |
|
29 | - { |
|
30 | - return 16; |
|
31 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected function _keyLength() |
|
29 | + { |
|
30 | + return 16; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - protected function _kwAlgo() |
|
38 | - { |
|
39 | - return new AESKW128(); |
|
40 | - } |
|
33 | + /** |
|
34 | + * |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + protected function _kwAlgo() |
|
38 | + { |
|
39 | + return new AESKW128(); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * |
|
44 | - * {@inheritdoc} |
|
45 | - */ |
|
46 | - public function algorithmParamValue() |
|
47 | - { |
|
48 | - return JWA::ALGO_PBES2_HS256_A128KW; |
|
49 | - } |
|
42 | + /** |
|
43 | + * |
|
44 | + * {@inheritdoc} |
|
45 | + */ |
|
46 | + public function algorithmParamValue() |
|
47 | + { |
|
48 | + return JWA::ALGO_PBES2_HS256_A128KW; |
|
49 | + } |
|
50 | 50 | } |
@@ -7,19 +7,19 @@ |
||
7 | 7 | */ |
8 | 8 | trait RandomCEK |
9 | 9 | { |
10 | - /** |
|
11 | - * Generate a random content encryption key. |
|
12 | - * |
|
13 | - * @param string $length Key length in bytes |
|
14 | - * @throws \RuntimeException |
|
15 | - * @return string |
|
16 | - */ |
|
17 | - public function cekForEncryption($length) |
|
18 | - { |
|
19 | - $ret = openssl_random_pseudo_bytes($length); |
|
20 | - if (false === $ret) { |
|
21 | - throw new \RuntimeException("openssl_random_pseudo_bytes() failed."); |
|
22 | - } |
|
23 | - return $ret; |
|
24 | - } |
|
10 | + /** |
|
11 | + * Generate a random content encryption key. |
|
12 | + * |
|
13 | + * @param string $length Key length in bytes |
|
14 | + * @throws \RuntimeException |
|
15 | + * @return string |
|
16 | + */ |
|
17 | + public function cekForEncryption($length) |
|
18 | + { |
|
19 | + $ret = openssl_random_pseudo_bytes($length); |
|
20 | + if (false === $ret) { |
|
21 | + throw new \RuntimeException("openssl_random_pseudo_bytes() failed."); |
|
22 | + } |
|
23 | + return $ret; |
|
24 | + } |
|
25 | 25 | } |