@@ -15,30 +15,30 @@ |
||
15 | 15 | */ |
16 | 16 | class A128GCMKWAlgorithm extends AESGCMKWAlgorithm |
17 | 17 | { |
18 | - /** |
|
19 | - * |
|
20 | - * {@inheritdoc} |
|
21 | - */ |
|
22 | - protected function _getGCMCipher(): Cipher |
|
23 | - { |
|
24 | - return new AES128Cipher(); |
|
25 | - } |
|
18 | + /** |
|
19 | + * |
|
20 | + * {@inheritdoc} |
|
21 | + */ |
|
22 | + protected function _getGCMCipher(): Cipher |
|
23 | + { |
|
24 | + return new AES128Cipher(); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * {@inheritdoc} |
|
30 | - */ |
|
31 | - protected function _keySize(): int |
|
32 | - { |
|
33 | - return 16; |
|
34 | - } |
|
27 | + /** |
|
28 | + * |
|
29 | + * {@inheritdoc} |
|
30 | + */ |
|
31 | + protected function _keySize(): int |
|
32 | + { |
|
33 | + return 16; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * {@inheritdoc} |
|
39 | - */ |
|
40 | - public function algorithmParamValue(): string |
|
41 | - { |
|
42 | - return JWA::ALGO_A128GCMKW; |
|
43 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * {@inheritdoc} |
|
39 | + */ |
|
40 | + public function algorithmParamValue(): string |
|
41 | + { |
|
42 | + return JWA::ALGO_A128GCMKW; |
|
43 | + } |
|
44 | 44 | } |
@@ -15,30 +15,30 @@ |
||
15 | 15 | */ |
16 | 16 | class A192GCMKWAlgorithm extends AESGCMKWAlgorithm |
17 | 17 | { |
18 | - /** |
|
19 | - * |
|
20 | - * {@inheritdoc} |
|
21 | - */ |
|
22 | - protected function _getGCMCipher(): Cipher |
|
23 | - { |
|
24 | - return new AES192Cipher(); |
|
25 | - } |
|
18 | + /** |
|
19 | + * |
|
20 | + * {@inheritdoc} |
|
21 | + */ |
|
22 | + protected function _getGCMCipher(): Cipher |
|
23 | + { |
|
24 | + return new AES192Cipher(); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * {@inheritdoc} |
|
30 | - */ |
|
31 | - protected function _keySize(): int |
|
32 | - { |
|
33 | - return 24; |
|
34 | - } |
|
27 | + /** |
|
28 | + * |
|
29 | + * {@inheritdoc} |
|
30 | + */ |
|
31 | + protected function _keySize(): int |
|
32 | + { |
|
33 | + return 24; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * {@inheritdoc} |
|
39 | - */ |
|
40 | - public function algorithmParamValue(): string |
|
41 | - { |
|
42 | - return JWA::ALGO_A192GCMKW; |
|
43 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * {@inheritdoc} |
|
39 | + */ |
|
40 | + public function algorithmParamValue(): string |
|
41 | + { |
|
42 | + return JWA::ALGO_A192GCMKW; |
|
43 | + } |
|
44 | 44 | } |
@@ -15,39 +15,39 @@ |
||
15 | 15 | */ |
16 | 16 | class PBES2HS384A192KWAlgorithm extends PBES2Algorithm |
17 | 17 | { |
18 | - /** |
|
19 | - * |
|
20 | - * {@inheritdoc} |
|
21 | - */ |
|
22 | - protected function _hashAlgo(): string |
|
23 | - { |
|
24 | - return "sha384"; |
|
25 | - } |
|
18 | + /** |
|
19 | + * |
|
20 | + * {@inheritdoc} |
|
21 | + */ |
|
22 | + protected function _hashAlgo(): string |
|
23 | + { |
|
24 | + return "sha384"; |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * {@inheritdoc} |
|
30 | - */ |
|
31 | - protected function _keyLength(): int |
|
32 | - { |
|
33 | - return 24; |
|
34 | - } |
|
27 | + /** |
|
28 | + * |
|
29 | + * {@inheritdoc} |
|
30 | + */ |
|
31 | + protected function _keyLength(): int |
|
32 | + { |
|
33 | + return 24; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * {@inheritdoc} |
|
39 | - */ |
|
40 | - protected function _kwAlgo(): AESKeyWrapAlgorithm |
|
41 | - { |
|
42 | - return new AESKW192(); |
|
43 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * {@inheritdoc} |
|
39 | + */ |
|
40 | + protected function _kwAlgo(): AESKeyWrapAlgorithm |
|
41 | + { |
|
42 | + return new AESKW192(); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * |
|
47 | - * {@inheritdoc} |
|
48 | - */ |
|
49 | - public function algorithmParamValue(): string |
|
50 | - { |
|
51 | - return JWA::ALGO_PBES2_HS384_A192KW; |
|
52 | - } |
|
45 | + /** |
|
46 | + * |
|
47 | + * {@inheritdoc} |
|
48 | + */ |
|
49 | + public function algorithmParamValue(): string |
|
50 | + { |
|
51 | + return JWA::ALGO_PBES2_HS384_A192KW; |
|
52 | + } |
|
53 | 53 | } |
@@ -18,104 +18,104 @@ |
||
18 | 18 | */ |
19 | 19 | class DirectCEKAlgorithm extends KeyManagementAlgorithm |
20 | 20 | { |
21 | - /** |
|
22 | - * Content encryption key. |
|
23 | - * |
|
24 | - * @var string $_cek |
|
25 | - */ |
|
26 | - protected $_cek; |
|
21 | + /** |
|
22 | + * Content encryption key. |
|
23 | + * |
|
24 | + * @var string $_cek |
|
25 | + */ |
|
26 | + protected $_cek; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param string $cek Content encryption key |
|
32 | - */ |
|
33 | - public function __construct(string $cek) |
|
34 | - { |
|
35 | - $this->_cek = $cek; |
|
36 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param string $cek Content encryption key |
|
32 | + */ |
|
33 | + public function __construct(string $cek) |
|
34 | + { |
|
35 | + $this->_cek = $cek; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * |
|
40 | - * @param JWK $jwk |
|
41 | - * @param Header $header |
|
42 | - * @throws \UnexpectedValueException |
|
43 | - * @return self |
|
44 | - */ |
|
45 | - public static function fromJWK(JWK $jwk, Header $header) |
|
46 | - { |
|
47 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
48 | - $alg = JWA::deriveAlgorithmName($header); |
|
49 | - if ($alg != JWA::ALGO_DIR) { |
|
50 | - throw new \UnexpectedValueException("Invalid algorithm '$alg'."); |
|
51 | - } |
|
52 | - return new self($jwk->key()); |
|
53 | - } |
|
38 | + /** |
|
39 | + * |
|
40 | + * @param JWK $jwk |
|
41 | + * @param Header $header |
|
42 | + * @throws \UnexpectedValueException |
|
43 | + * @return self |
|
44 | + */ |
|
45 | + public static function fromJWK(JWK $jwk, Header $header) |
|
46 | + { |
|
47 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
48 | + $alg = JWA::deriveAlgorithmName($header); |
|
49 | + if ($alg != JWA::ALGO_DIR) { |
|
50 | + throw new \UnexpectedValueException("Invalid algorithm '$alg'."); |
|
51 | + } |
|
52 | + return new self($jwk->key()); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Get content encryption key. |
|
57 | - * |
|
58 | - * @return string |
|
59 | - */ |
|
60 | - public function cek(): string |
|
61 | - { |
|
62 | - return $this->_cek; |
|
63 | - } |
|
55 | + /** |
|
56 | + * Get content encryption key. |
|
57 | + * |
|
58 | + * @return string |
|
59 | + */ |
|
60 | + public function cek(): string |
|
61 | + { |
|
62 | + return $this->_cek; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - protected function _encryptKey(string $key, Header &$header): string |
|
70 | - { |
|
71 | - if ($key != $this->_cek) { |
|
72 | - throw new \LogicException("Content encryption key doesn't match."); |
|
73 | - } |
|
74 | - return ""; |
|
75 | - } |
|
65 | + /** |
|
66 | + * |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + protected function _encryptKey(string $key, Header &$header): string |
|
70 | + { |
|
71 | + if ($key != $this->_cek) { |
|
72 | + throw new \LogicException("Content encryption key doesn't match."); |
|
73 | + } |
|
74 | + return ""; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * |
|
79 | - * {@inheritdoc} |
|
80 | - */ |
|
81 | - protected function _decryptKey(string $ciphertext, Header $header): string |
|
82 | - { |
|
83 | - if ($ciphertext !== "") { |
|
84 | - throw new \UnexpectedValueException( |
|
85 | - "Encrypted key must be an empty octet sequence."); |
|
86 | - } |
|
87 | - return $this->_cek; |
|
88 | - } |
|
77 | + /** |
|
78 | + * |
|
79 | + * {@inheritdoc} |
|
80 | + */ |
|
81 | + protected function _decryptKey(string $ciphertext, Header $header): string |
|
82 | + { |
|
83 | + if ($ciphertext !== "") { |
|
84 | + throw new \UnexpectedValueException( |
|
85 | + "Encrypted key must be an empty octet sequence."); |
|
86 | + } |
|
87 | + return $this->_cek; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * |
|
92 | - * {@inheritdoc} |
|
93 | - */ |
|
94 | - public function cekForEncryption(int $length): string |
|
95 | - { |
|
96 | - if (strlen($this->_cek) != $length) { |
|
97 | - throw new \UnexpectedValueException("Invalid key length."); |
|
98 | - } |
|
99 | - return $this->_cek; |
|
100 | - } |
|
90 | + /** |
|
91 | + * |
|
92 | + * {@inheritdoc} |
|
93 | + */ |
|
94 | + public function cekForEncryption(int $length): string |
|
95 | + { |
|
96 | + if (strlen($this->_cek) != $length) { |
|
97 | + throw new \UnexpectedValueException("Invalid key length."); |
|
98 | + } |
|
99 | + return $this->_cek; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * |
|
104 | - * {@inheritdoc} |
|
105 | - */ |
|
106 | - public function algorithmParamValue(): string |
|
107 | - { |
|
108 | - return JWA::ALGO_DIR; |
|
109 | - } |
|
102 | + /** |
|
103 | + * |
|
104 | + * {@inheritdoc} |
|
105 | + */ |
|
106 | + public function algorithmParamValue(): string |
|
107 | + { |
|
108 | + return JWA::ALGO_DIR; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * |
|
113 | - * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
114 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
115 | - */ |
|
116 | - public function headerParameters(): array |
|
117 | - { |
|
118 | - return array_merge(parent::headerParameters(), |
|
119 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
120 | - } |
|
111 | + /** |
|
112 | + * |
|
113 | + * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
114 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
115 | + */ |
|
116 | + public function headerParameters(): array |
|
117 | + { |
|
118 | + return array_merge(parent::headerParameters(), |
|
119 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
120 | + } |
|
121 | 121 | } |
@@ -66,7 +66,7 @@ |
||
66 | 66 | * |
67 | 67 | * {@inheritdoc} |
68 | 68 | */ |
69 | - protected function _encryptKey(string $key, Header &$header): string |
|
69 | + protected function _encryptKey(string $key, Header&$header): string |
|
70 | 70 | { |
71 | 71 | if ($key != $this->_cek) { |
72 | 72 | throw new \LogicException("Content encryption key doesn't match."); |
@@ -15,30 +15,30 @@ |
||
15 | 15 | */ |
16 | 16 | class A256KWAlgorithm extends AESKWAlgorithm |
17 | 17 | { |
18 | - /** |
|
19 | - * |
|
20 | - * {@inheritdoc} |
|
21 | - */ |
|
22 | - protected function _kekSize(): int |
|
23 | - { |
|
24 | - return 32; |
|
25 | - } |
|
18 | + /** |
|
19 | + * |
|
20 | + * {@inheritdoc} |
|
21 | + */ |
|
22 | + protected function _kekSize(): int |
|
23 | + { |
|
24 | + return 32; |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * {@inheritdoc} |
|
30 | - */ |
|
31 | - protected function _AESKWAlgo(): AESKeyWrapAlgorithm |
|
32 | - { |
|
33 | - return new AESKW256(); |
|
34 | - } |
|
27 | + /** |
|
28 | + * |
|
29 | + * {@inheritdoc} |
|
30 | + */ |
|
31 | + protected function _AESKWAlgo(): AESKeyWrapAlgorithm |
|
32 | + { |
|
33 | + return new AESKW256(); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * {@inheritdoc} |
|
39 | - */ |
|
40 | - public function algorithmParamValue(): string |
|
41 | - { |
|
42 | - return JWA::ALGO_A256KW; |
|
43 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * {@inheritdoc} |
|
39 | + */ |
|
40 | + public function algorithmParamValue(): string |
|
41 | + { |
|
42 | + return JWA::ALGO_A256KW; |
|
43 | + } |
|
44 | 44 | } |
@@ -22,201 +22,201 @@ |
||
22 | 22 | */ |
23 | 23 | abstract class PBES2Algorithm extends KeyManagementAlgorithm |
24 | 24 | { |
25 | - use RandomCEK; |
|
26 | - |
|
27 | - /** |
|
28 | - * Password. |
|
29 | - * |
|
30 | - * @var string $_password |
|
31 | - */ |
|
32 | - protected $_password; |
|
33 | - |
|
34 | - /** |
|
35 | - * Salt input. |
|
36 | - * |
|
37 | - * @var string $_salt |
|
38 | - */ |
|
39 | - protected $_saltInput; |
|
40 | - |
|
41 | - /** |
|
42 | - * Iteration count. |
|
43 | - * |
|
44 | - * @var int $_count |
|
45 | - */ |
|
46 | - protected $_count; |
|
47 | - |
|
48 | - /** |
|
49 | - * Derived key. |
|
50 | - * |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - private $_derivedKey; |
|
54 | - |
|
55 | - /** |
|
56 | - * Mapping from algorithm name to class name. |
|
57 | - * |
|
58 | - * @internal |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - const MAP_ALGO_TO_CLASS = array( |
|
63 | - /* @formatter:off */ |
|
64 | - JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class, |
|
65 | - JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class, |
|
66 | - JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class |
|
67 | - /* @formatter:on */ |
|
68 | - ); |
|
69 | - |
|
70 | - /** |
|
71 | - * Get hash algorithm for hash_pbkdf2. |
|
72 | - * |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - abstract protected function _hashAlgo(): string; |
|
76 | - |
|
77 | - /** |
|
78 | - * Get derived key length. |
|
79 | - * |
|
80 | - * @return int |
|
81 | - */ |
|
82 | - abstract protected function _keyLength(): int; |
|
83 | - |
|
84 | - /** |
|
85 | - * Get key wrapping algoritym. |
|
86 | - * |
|
87 | - * @return AESKeyWrapAlgorithm |
|
88 | - */ |
|
89 | - abstract protected function _kwAlgo(): AESKeyWrapAlgorithm; |
|
90 | - |
|
91 | - /** |
|
92 | - * Constructor. |
|
93 | - * |
|
94 | - * @param string $password Password |
|
95 | - * @param string $salt_input Salt input |
|
96 | - * @param int $count Iteration count |
|
97 | - */ |
|
98 | - public function __construct(string $password, string $salt_input, int $count) |
|
99 | - { |
|
100 | - $this->_password = $password; |
|
101 | - $this->_saltInput = $salt_input; |
|
102 | - $this->_count = $count; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * |
|
107 | - * @param JWK $jwk |
|
108 | - * @param Header $header |
|
109 | - * @throws \UnexpectedValueException |
|
110 | - * @return self |
|
111 | - */ |
|
112 | - public static function fromJWK(JWK $jwk, Header $header) |
|
113 | - { |
|
114 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
115 | - if (!$header->hasPBES2SaltInput()) { |
|
116 | - throw new \UnexpectedValueException("No salt input."); |
|
117 | - } |
|
118 | - $salt_input = $header->PBES2SaltInput()->saltInput(); |
|
119 | - if (!$header->hasPBES2Count()) { |
|
120 | - throw new \UnexpectedValueException("No iteration count."); |
|
121 | - } |
|
122 | - $count = $header->PBES2Count()->value(); |
|
123 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
124 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
125 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
126 | - } |
|
127 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
128 | - return new $cls($jwk->key(), $salt_input, $count); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Initialize from a password with random salt and default iteration count. |
|
133 | - * |
|
134 | - * @param string $password Password |
|
135 | - * @param int $count Optional user defined iteration count |
|
136 | - * @param int $salt_bytes Optional user defined salt length |
|
137 | - * @return self |
|
138 | - */ |
|
139 | - public static function fromPassword(string $password, int $count = 64000, |
|
140 | - int $salt_bytes = 8): self |
|
141 | - { |
|
142 | - $salt_input = openssl_random_pseudo_bytes($salt_bytes); |
|
143 | - return new static($password, $salt_input, $count); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Get salt input. |
|
148 | - * |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function saltInput(): string |
|
152 | - { |
|
153 | - return $this->_saltInput; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Get computed salt. |
|
158 | - * |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - public function salt(): string |
|
162 | - { |
|
163 | - return PBES2SaltInputParameter::fromString($this->_saltInput)->salt( |
|
164 | - AlgorithmParameter::fromAlgorithm($this)); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Get iteration count. |
|
169 | - * |
|
170 | - * @return int |
|
171 | - */ |
|
172 | - public function iterationCount(): int |
|
173 | - { |
|
174 | - return $this->_count; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Get derived key. |
|
179 | - * |
|
180 | - * @return string |
|
181 | - */ |
|
182 | - protected function _derivedKey(): string |
|
183 | - { |
|
184 | - if (!isset($this->_derivedKey)) { |
|
185 | - $this->_derivedKey = hash_pbkdf2($this->_hashAlgo(), |
|
186 | - $this->_password, $this->salt(), $this->_count, |
|
187 | - $this->_keyLength(), true); |
|
188 | - } |
|
189 | - return $this->_derivedKey; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * |
|
194 | - * {@inheritdoc} |
|
195 | - */ |
|
196 | - protected function _encryptKey(string $key, Header &$header): string |
|
197 | - { |
|
198 | - return $this->_kwAlgo()->wrap($key, $this->_derivedKey()); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * |
|
203 | - * {@inheritdoc} |
|
204 | - */ |
|
205 | - protected function _decryptKey(string $ciphertext, Header $header): string |
|
206 | - { |
|
207 | - return $this->_kwAlgo()->unwrap($ciphertext, $this->_derivedKey()); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * |
|
212 | - * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
213 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
214 | - */ |
|
215 | - public function headerParameters(): array |
|
216 | - { |
|
217 | - return array_merge(parent::headerParameters(), |
|
218 | - array(AlgorithmParameter::fromAlgorithm($this), |
|
219 | - PBES2SaltInputParameter::fromString($this->_saltInput), |
|
220 | - new PBES2CountParameter($this->_count))); |
|
221 | - } |
|
25 | + use RandomCEK; |
|
26 | + |
|
27 | + /** |
|
28 | + * Password. |
|
29 | + * |
|
30 | + * @var string $_password |
|
31 | + */ |
|
32 | + protected $_password; |
|
33 | + |
|
34 | + /** |
|
35 | + * Salt input. |
|
36 | + * |
|
37 | + * @var string $_salt |
|
38 | + */ |
|
39 | + protected $_saltInput; |
|
40 | + |
|
41 | + /** |
|
42 | + * Iteration count. |
|
43 | + * |
|
44 | + * @var int $_count |
|
45 | + */ |
|
46 | + protected $_count; |
|
47 | + |
|
48 | + /** |
|
49 | + * Derived key. |
|
50 | + * |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + private $_derivedKey; |
|
54 | + |
|
55 | + /** |
|
56 | + * Mapping from algorithm name to class name. |
|
57 | + * |
|
58 | + * @internal |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + const MAP_ALGO_TO_CLASS = array( |
|
63 | + /* @formatter:off */ |
|
64 | + JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class, |
|
65 | + JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class, |
|
66 | + JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class |
|
67 | + /* @formatter:on */ |
|
68 | + ); |
|
69 | + |
|
70 | + /** |
|
71 | + * Get hash algorithm for hash_pbkdf2. |
|
72 | + * |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + abstract protected function _hashAlgo(): string; |
|
76 | + |
|
77 | + /** |
|
78 | + * Get derived key length. |
|
79 | + * |
|
80 | + * @return int |
|
81 | + */ |
|
82 | + abstract protected function _keyLength(): int; |
|
83 | + |
|
84 | + /** |
|
85 | + * Get key wrapping algoritym. |
|
86 | + * |
|
87 | + * @return AESKeyWrapAlgorithm |
|
88 | + */ |
|
89 | + abstract protected function _kwAlgo(): AESKeyWrapAlgorithm; |
|
90 | + |
|
91 | + /** |
|
92 | + * Constructor. |
|
93 | + * |
|
94 | + * @param string $password Password |
|
95 | + * @param string $salt_input Salt input |
|
96 | + * @param int $count Iteration count |
|
97 | + */ |
|
98 | + public function __construct(string $password, string $salt_input, int $count) |
|
99 | + { |
|
100 | + $this->_password = $password; |
|
101 | + $this->_saltInput = $salt_input; |
|
102 | + $this->_count = $count; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * |
|
107 | + * @param JWK $jwk |
|
108 | + * @param Header $header |
|
109 | + * @throws \UnexpectedValueException |
|
110 | + * @return self |
|
111 | + */ |
|
112 | + public static function fromJWK(JWK $jwk, Header $header) |
|
113 | + { |
|
114 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
115 | + if (!$header->hasPBES2SaltInput()) { |
|
116 | + throw new \UnexpectedValueException("No salt input."); |
|
117 | + } |
|
118 | + $salt_input = $header->PBES2SaltInput()->saltInput(); |
|
119 | + if (!$header->hasPBES2Count()) { |
|
120 | + throw new \UnexpectedValueException("No iteration count."); |
|
121 | + } |
|
122 | + $count = $header->PBES2Count()->value(); |
|
123 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
124 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
125 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
126 | + } |
|
127 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
128 | + return new $cls($jwk->key(), $salt_input, $count); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Initialize from a password with random salt and default iteration count. |
|
133 | + * |
|
134 | + * @param string $password Password |
|
135 | + * @param int $count Optional user defined iteration count |
|
136 | + * @param int $salt_bytes Optional user defined salt length |
|
137 | + * @return self |
|
138 | + */ |
|
139 | + public static function fromPassword(string $password, int $count = 64000, |
|
140 | + int $salt_bytes = 8): self |
|
141 | + { |
|
142 | + $salt_input = openssl_random_pseudo_bytes($salt_bytes); |
|
143 | + return new static($password, $salt_input, $count); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Get salt input. |
|
148 | + * |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function saltInput(): string |
|
152 | + { |
|
153 | + return $this->_saltInput; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Get computed salt. |
|
158 | + * |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + public function salt(): string |
|
162 | + { |
|
163 | + return PBES2SaltInputParameter::fromString($this->_saltInput)->salt( |
|
164 | + AlgorithmParameter::fromAlgorithm($this)); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Get iteration count. |
|
169 | + * |
|
170 | + * @return int |
|
171 | + */ |
|
172 | + public function iterationCount(): int |
|
173 | + { |
|
174 | + return $this->_count; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Get derived key. |
|
179 | + * |
|
180 | + * @return string |
|
181 | + */ |
|
182 | + protected function _derivedKey(): string |
|
183 | + { |
|
184 | + if (!isset($this->_derivedKey)) { |
|
185 | + $this->_derivedKey = hash_pbkdf2($this->_hashAlgo(), |
|
186 | + $this->_password, $this->salt(), $this->_count, |
|
187 | + $this->_keyLength(), true); |
|
188 | + } |
|
189 | + return $this->_derivedKey; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * |
|
194 | + * {@inheritdoc} |
|
195 | + */ |
|
196 | + protected function _encryptKey(string $key, Header &$header): string |
|
197 | + { |
|
198 | + return $this->_kwAlgo()->wrap($key, $this->_derivedKey()); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * |
|
203 | + * {@inheritdoc} |
|
204 | + */ |
|
205 | + protected function _decryptKey(string $ciphertext, Header $header): string |
|
206 | + { |
|
207 | + return $this->_kwAlgo()->unwrap($ciphertext, $this->_derivedKey()); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * |
|
212 | + * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
213 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
214 | + */ |
|
215 | + public function headerParameters(): array |
|
216 | + { |
|
217 | + return array_merge(parent::headerParameters(), |
|
218 | + array(AlgorithmParameter::fromAlgorithm($this), |
|
219 | + PBES2SaltInputParameter::fromString($this->_saltInput), |
|
220 | + new PBES2CountParameter($this->_count))); |
|
221 | + } |
|
222 | 222 | } |
@@ -193,7 +193,7 @@ |
||
193 | 193 | * |
194 | 194 | * {@inheritdoc} |
195 | 195 | */ |
196 | - protected function _encryptKey(string $key, Header &$header): string |
|
196 | + protected function _encryptKey(string $key, Header&$header): string |
|
197 | 197 | { |
198 | 198 | return $this->_kwAlgo()->wrap($key, $this->_derivedKey()); |
199 | 199 | } |
@@ -20,124 +20,124 @@ |
||
20 | 20 | */ |
21 | 21 | abstract class AESKWAlgorithm 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 | - * Key wrapping algorithm. |
|
34 | - * |
|
35 | - * Lazily initialized. |
|
36 | - * |
|
37 | - * @var AESKeyWrapAlgorithm|null $_kw |
|
38 | - */ |
|
39 | - protected $_kw; |
|
32 | + /** |
|
33 | + * Key wrapping algorithm. |
|
34 | + * |
|
35 | + * Lazily initialized. |
|
36 | + * |
|
37 | + * @var AESKeyWrapAlgorithm|null $_kw |
|
38 | + */ |
|
39 | + protected $_kw; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Mapping from algorithm name to class name. |
|
43 | - * |
|
44 | - * @internal |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - const MAP_ALGO_TO_CLASS = array( |
|
49 | - /* @formatter:off */ |
|
50 | - JWA::ALGO_A128KW => A128KWAlgorithm::class, |
|
51 | - JWA::ALGO_A192KW => A192KWAlgorithm::class, |
|
52 | - JWA::ALGO_A256KW => A256KWAlgorithm::class |
|
53 | - /* @formatter:on */ |
|
54 | - ); |
|
41 | + /** |
|
42 | + * Mapping from algorithm name to class name. |
|
43 | + * |
|
44 | + * @internal |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + const MAP_ALGO_TO_CLASS = array( |
|
49 | + /* @formatter:off */ |
|
50 | + JWA::ALGO_A128KW => A128KWAlgorithm::class, |
|
51 | + JWA::ALGO_A192KW => A192KWAlgorithm::class, |
|
52 | + JWA::ALGO_A256KW => A256KWAlgorithm::class |
|
53 | + /* @formatter:on */ |
|
54 | + ); |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get the size of the key encryption key in bytes. |
|
58 | - * |
|
59 | - * @return int |
|
60 | - */ |
|
61 | - abstract protected function _kekSize(): int; |
|
56 | + /** |
|
57 | + * Get the size of the key encryption key in bytes. |
|
58 | + * |
|
59 | + * @return int |
|
60 | + */ |
|
61 | + abstract protected function _kekSize(): int; |
|
62 | 62 | |
63 | - /** |
|
64 | - * Get key wrapping algorithm instance. |
|
65 | - * |
|
66 | - * @return AESKeyWrapAlgorithm |
|
67 | - */ |
|
68 | - abstract protected function _AESKWAlgo(): AESKeyWrapAlgorithm; |
|
63 | + /** |
|
64 | + * Get key wrapping algorithm instance. |
|
65 | + * |
|
66 | + * @return AESKeyWrapAlgorithm |
|
67 | + */ |
|
68 | + abstract protected function _AESKWAlgo(): AESKeyWrapAlgorithm; |
|
69 | 69 | |
70 | - /** |
|
71 | - * Constructor. |
|
72 | - * |
|
73 | - * @param string $kek Key encryption key |
|
74 | - */ |
|
75 | - public function __construct(string $kek) |
|
76 | - { |
|
77 | - if (strlen($kek) != $this->_kekSize()) { |
|
78 | - throw new \LengthException( |
|
79 | - "Key encryption key must be " . $this->_kekSize() . " bytes."); |
|
80 | - } |
|
81 | - $this->_kek = $kek; |
|
82 | - } |
|
70 | + /** |
|
71 | + * Constructor. |
|
72 | + * |
|
73 | + * @param string $kek Key encryption key |
|
74 | + */ |
|
75 | + public function __construct(string $kek) |
|
76 | + { |
|
77 | + if (strlen($kek) != $this->_kekSize()) { |
|
78 | + throw new \LengthException( |
|
79 | + "Key encryption key must be " . $this->_kekSize() . " bytes."); |
|
80 | + } |
|
81 | + $this->_kek = $kek; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * |
|
86 | - * @param JWK $jwk |
|
87 | - * @param Header $header |
|
88 | - * @throws \UnexpectedValueException |
|
89 | - * @return self |
|
90 | - */ |
|
91 | - public static function fromJWK(JWK $jwk, Header $header) |
|
92 | - { |
|
93 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
94 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
95 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
96 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
97 | - } |
|
98 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
99 | - return new $cls($jwk->key()); |
|
100 | - } |
|
84 | + /** |
|
85 | + * |
|
86 | + * @param JWK $jwk |
|
87 | + * @param Header $header |
|
88 | + * @throws \UnexpectedValueException |
|
89 | + * @return self |
|
90 | + */ |
|
91 | + public static function fromJWK(JWK $jwk, Header $header) |
|
92 | + { |
|
93 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
94 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
95 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
96 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
97 | + } |
|
98 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
99 | + return new $cls($jwk->key()); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Get key wrapping algorithm. |
|
104 | - * |
|
105 | - * @return AESKeyWrapAlgorithm |
|
106 | - */ |
|
107 | - protected function _kw(): AESKeyWrapAlgorithm |
|
108 | - { |
|
109 | - if (!isset($this->_kw)) { |
|
110 | - $this->_kw = $this->_AESKWAlgo(); |
|
111 | - } |
|
112 | - return $this->_kw; |
|
113 | - } |
|
102 | + /** |
|
103 | + * Get key wrapping algorithm. |
|
104 | + * |
|
105 | + * @return AESKeyWrapAlgorithm |
|
106 | + */ |
|
107 | + protected function _kw(): AESKeyWrapAlgorithm |
|
108 | + { |
|
109 | + if (!isset($this->_kw)) { |
|
110 | + $this->_kw = $this->_AESKWAlgo(); |
|
111 | + } |
|
112 | + return $this->_kw; |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * |
|
117 | - * {@inheritdoc} |
|
118 | - */ |
|
119 | - protected function _encryptKey(string $key, Header &$header): string |
|
120 | - { |
|
121 | - return $this->_kw()->wrap($key, $this->_kek); |
|
122 | - } |
|
115 | + /** |
|
116 | + * |
|
117 | + * {@inheritdoc} |
|
118 | + */ |
|
119 | + protected function _encryptKey(string $key, Header &$header): string |
|
120 | + { |
|
121 | + return $this->_kw()->wrap($key, $this->_kek); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * |
|
126 | - * {@inheritdoc} |
|
127 | - */ |
|
128 | - protected function _decryptKey(string $ciphertext, Header $header): string |
|
129 | - { |
|
130 | - return $this->_kw()->unwrap($ciphertext, $this->_kek); |
|
131 | - } |
|
124 | + /** |
|
125 | + * |
|
126 | + * {@inheritdoc} |
|
127 | + */ |
|
128 | + protected function _decryptKey(string $ciphertext, Header $header): string |
|
129 | + { |
|
130 | + return $this->_kw()->unwrap($ciphertext, $this->_kek); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * |
|
135 | - * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
136 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
137 | - */ |
|
138 | - public function headerParameters(): array |
|
139 | - { |
|
140 | - return array_merge(parent::headerParameters(), |
|
141 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
142 | - } |
|
133 | + /** |
|
134 | + * |
|
135 | + * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
136 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
137 | + */ |
|
138 | + public function headerParameters(): array |
|
139 | + { |
|
140 | + return array_merge(parent::headerParameters(), |
|
141 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
142 | + } |
|
143 | 143 | } |
@@ -116,7 +116,7 @@ |
||
116 | 116 | * |
117 | 117 | * {@inheritdoc} |
118 | 118 | */ |
119 | - protected function _encryptKey(string $key, Header &$header): string |
|
119 | + protected function _encryptKey(string $key, Header&$header): string |
|
120 | 120 | { |
121 | 121 | return $this->_kw()->wrap($key, $this->_kek); |
122 | 122 | } |
@@ -13,21 +13,21 @@ |
||
13 | 13 | */ |
14 | 14 | class RSAESOAEPAlgorithm extends RSAESKeyAlgorithm |
15 | 15 | { |
16 | - /** |
|
17 | - * |
|
18 | - * {@inheritdoc} |
|
19 | - */ |
|
20 | - protected function _paddingScheme(): int |
|
21 | - { |
|
22 | - return OPENSSL_PKCS1_OAEP_PADDING; |
|
23 | - } |
|
16 | + /** |
|
17 | + * |
|
18 | + * {@inheritdoc} |
|
19 | + */ |
|
20 | + protected function _paddingScheme(): int |
|
21 | + { |
|
22 | + return OPENSSL_PKCS1_OAEP_PADDING; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * |
|
27 | - * {@inheritdoc} |
|
28 | - */ |
|
29 | - public function algorithmParamValue(): string |
|
30 | - { |
|
31 | - return JWA::ALGO_RSA_OAEP; |
|
32 | - } |
|
25 | + /** |
|
26 | + * |
|
27 | + * {@inheritdoc} |
|
28 | + */ |
|
29 | + public function algorithmParamValue(): string |
|
30 | + { |
|
31 | + return JWA::ALGO_RSA_OAEP; |
|
32 | + } |
|
33 | 33 | } |
@@ -9,19 +9,19 @@ |
||
9 | 9 | */ |
10 | 10 | trait RandomCEK |
11 | 11 | { |
12 | - /** |
|
13 | - * Generate a random content encryption key. |
|
14 | - * |
|
15 | - * @param int $length Key length in bytes |
|
16 | - * @throws \RuntimeException |
|
17 | - * @return string |
|
18 | - */ |
|
19 | - public function cekForEncryption(int $length): string |
|
20 | - { |
|
21 | - $ret = openssl_random_pseudo_bytes($length); |
|
22 | - if (false === $ret) { |
|
23 | - throw new \RuntimeException("openssl_random_pseudo_bytes() failed."); |
|
24 | - } |
|
25 | - return $ret; |
|
26 | - } |
|
12 | + /** |
|
13 | + * Generate a random content encryption key. |
|
14 | + * |
|
15 | + * @param int $length Key length in bytes |
|
16 | + * @throws \RuntimeException |
|
17 | + * @return string |
|
18 | + */ |
|
19 | + public function cekForEncryption(int $length): string |
|
20 | + { |
|
21 | + $ret = openssl_random_pseudo_bytes($length); |
|
22 | + if (false === $ret) { |
|
23 | + throw new \RuntimeException("openssl_random_pseudo_bytes() failed."); |
|
24 | + } |
|
25 | + return $ret; |
|
26 | + } |
|
27 | 27 | } |