@@ -11,66 +11,66 @@ |
||
11 | 11 | */ |
12 | 12 | class A256CBCHS512Algorithm extends AESCBCAlgorithm |
13 | 13 | { |
14 | - /** |
|
15 | - * |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - public function keySize() |
|
19 | - { |
|
20 | - return 64; |
|
21 | - } |
|
14 | + /** |
|
15 | + * |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + public function keySize() |
|
19 | + { |
|
20 | + return 64; |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - public function encryptionAlgorithmParamValue() |
|
28 | - { |
|
29 | - return JWA::ALGO_A256CBC_HS512; |
|
30 | - } |
|
23 | + /** |
|
24 | + * |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + public function encryptionAlgorithmParamValue() |
|
28 | + { |
|
29 | + return JWA::ALGO_A256CBC_HS512; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * |
|
34 | - * {@inheritdoc} |
|
35 | - */ |
|
36 | - protected function _cipherMethod() |
|
37 | - { |
|
38 | - return "AES-256-CBC"; |
|
39 | - } |
|
32 | + /** |
|
33 | + * |
|
34 | + * {@inheritdoc} |
|
35 | + */ |
|
36 | + protected function _cipherMethod() |
|
37 | + { |
|
38 | + return "AES-256-CBC"; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * |
|
43 | - * {@inheritdoc} |
|
44 | - */ |
|
45 | - protected function _hashAlgo() |
|
46 | - { |
|
47 | - return "sha512"; |
|
48 | - } |
|
41 | + /** |
|
42 | + * |
|
43 | + * {@inheritdoc} |
|
44 | + */ |
|
45 | + protected function _hashAlgo() |
|
46 | + { |
|
47 | + return "sha512"; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected function _encKeyLen() |
|
55 | - { |
|
56 | - return 32; |
|
57 | - } |
|
50 | + /** |
|
51 | + * |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected function _encKeyLen() |
|
55 | + { |
|
56 | + return 32; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - protected function _macKeyLen() |
|
64 | - { |
|
65 | - return 32; |
|
66 | - } |
|
59 | + /** |
|
60 | + * |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + protected function _macKeyLen() |
|
64 | + { |
|
65 | + return 32; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * |
|
70 | - * {@inheritdoc} |
|
71 | - */ |
|
72 | - protected function _tagLen() |
|
73 | - { |
|
74 | - return 32; |
|
75 | - } |
|
68 | + /** |
|
69 | + * |
|
70 | + * {@inheritdoc} |
|
71 | + */ |
|
72 | + protected function _tagLen() |
|
73 | + { |
|
74 | + return 32; |
|
75 | + } |
|
76 | 76 | } |
@@ -12,30 +12,30 @@ |
||
12 | 12 | */ |
13 | 13 | class A192GCMAlgorithm extends AESGCMAlgorithm |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - public function encryptionAlgorithmParamValue() |
|
20 | - { |
|
21 | - return JWA::ALGO_A192GCM; |
|
22 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + public function encryptionAlgorithmParamValue() |
|
20 | + { |
|
21 | + return JWA::ALGO_A192GCM; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - public function keySize() |
|
29 | - { |
|
30 | - return 24; |
|
31 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + public function keySize() |
|
29 | + { |
|
30 | + return 24; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - protected function _getGCMCipher() |
|
38 | - { |
|
39 | - return new AES192Cipher(); |
|
40 | - } |
|
33 | + /** |
|
34 | + * |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + protected function _getGCMCipher() |
|
38 | + { |
|
39 | + return new AES192Cipher(); |
|
40 | + } |
|
41 | 41 | } |
@@ -18,353 +18,353 @@ |
||
18 | 18 | */ |
19 | 19 | class JWE |
20 | 20 | { |
21 | - /** |
|
22 | - * Protected header. |
|
23 | - * |
|
24 | - * @var Header $_protectedHeader |
|
25 | - */ |
|
26 | - protected $_protectedHeader; |
|
21 | + /** |
|
22 | + * Protected header. |
|
23 | + * |
|
24 | + * @var Header $_protectedHeader |
|
25 | + */ |
|
26 | + protected $_protectedHeader; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Encrypted key. |
|
30 | - * |
|
31 | - * @var string $_encryptedKey |
|
32 | - */ |
|
33 | - protected $_encryptedKey; |
|
28 | + /** |
|
29 | + * Encrypted key. |
|
30 | + * |
|
31 | + * @var string $_encryptedKey |
|
32 | + */ |
|
33 | + protected $_encryptedKey; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Initialization vector. |
|
37 | - * |
|
38 | - * @var string |
|
39 | - */ |
|
40 | - protected $_iv; |
|
35 | + /** |
|
36 | + * Initialization vector. |
|
37 | + * |
|
38 | + * @var string |
|
39 | + */ |
|
40 | + protected $_iv; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Additional authenticated data. |
|
44 | - * |
|
45 | - * @var string $_aad |
|
46 | - */ |
|
47 | - protected $_aad; |
|
42 | + /** |
|
43 | + * Additional authenticated data. |
|
44 | + * |
|
45 | + * @var string $_aad |
|
46 | + */ |
|
47 | + protected $_aad; |
|
48 | 48 | |
49 | - /** |
|
50 | - * Ciphertext. |
|
51 | - * |
|
52 | - * @var string $_ciphertext |
|
53 | - */ |
|
54 | - protected $_ciphertext; |
|
49 | + /** |
|
50 | + * Ciphertext. |
|
51 | + * |
|
52 | + * @var string $_ciphertext |
|
53 | + */ |
|
54 | + protected $_ciphertext; |
|
55 | 55 | |
56 | - /** |
|
57 | - * Authentication tag. |
|
58 | - * |
|
59 | - * @var string $_authenticationTag |
|
60 | - */ |
|
61 | - protected $_authenticationTag; |
|
56 | + /** |
|
57 | + * Authentication tag. |
|
58 | + * |
|
59 | + * @var string $_authenticationTag |
|
60 | + */ |
|
61 | + protected $_authenticationTag; |
|
62 | 62 | |
63 | - /** |
|
64 | - * Constructor. |
|
65 | - * |
|
66 | - * @param Header $protected_header JWE Protected Header |
|
67 | - * @param string $encrypted_key Encrypted key |
|
68 | - * @param string $iv Initialization vector |
|
69 | - * @param string $ciphertext Ciphertext |
|
70 | - * @param string $auth_tag Authentication tag |
|
71 | - * @param string|null $aad Additional authenticated data |
|
72 | - */ |
|
73 | - public function __construct(Header $protected_header, $encrypted_key, $iv, |
|
74 | - $ciphertext, $auth_tag, $aad = null) |
|
75 | - { |
|
76 | - $this->_protectedHeader = $protected_header; |
|
77 | - $this->_encryptedKey = $encrypted_key; |
|
78 | - $this->_iv = $iv; |
|
79 | - $this->_aad = $aad; |
|
80 | - $this->_ciphertext = $ciphertext; |
|
81 | - $this->_authenticationTag = $auth_tag; |
|
82 | - } |
|
63 | + /** |
|
64 | + * Constructor. |
|
65 | + * |
|
66 | + * @param Header $protected_header JWE Protected Header |
|
67 | + * @param string $encrypted_key Encrypted key |
|
68 | + * @param string $iv Initialization vector |
|
69 | + * @param string $ciphertext Ciphertext |
|
70 | + * @param string $auth_tag Authentication tag |
|
71 | + * @param string|null $aad Additional authenticated data |
|
72 | + */ |
|
73 | + public function __construct(Header $protected_header, $encrypted_key, $iv, |
|
74 | + $ciphertext, $auth_tag, $aad = null) |
|
75 | + { |
|
76 | + $this->_protectedHeader = $protected_header; |
|
77 | + $this->_encryptedKey = $encrypted_key; |
|
78 | + $this->_iv = $iv; |
|
79 | + $this->_aad = $aad; |
|
80 | + $this->_ciphertext = $ciphertext; |
|
81 | + $this->_authenticationTag = $auth_tag; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Initialize from compact serialization. |
|
86 | - * |
|
87 | - * @param string $data |
|
88 | - * @return self |
|
89 | - */ |
|
90 | - public static function fromCompact($data) |
|
91 | - { |
|
92 | - return self::fromParts(explode(".", $data)); |
|
93 | - } |
|
84 | + /** |
|
85 | + * Initialize from compact serialization. |
|
86 | + * |
|
87 | + * @param string $data |
|
88 | + * @return self |
|
89 | + */ |
|
90 | + public static function fromCompact($data) |
|
91 | + { |
|
92 | + return self::fromParts(explode(".", $data)); |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Initialize from parts of compact serialization. |
|
97 | - * |
|
98 | - * @param array $parts |
|
99 | - * @throws \UnexpectedValueException |
|
100 | - * @return self |
|
101 | - */ |
|
102 | - public static function fromParts(array $parts) |
|
103 | - { |
|
104 | - if (count($parts) != 5) { |
|
105 | - throw new \UnexpectedValueException( |
|
106 | - "Invalid JWE compact serialization."); |
|
107 | - } |
|
108 | - $header = Header::fromJSON(Base64::urlDecode($parts[0])); |
|
109 | - $encrypted_key = Base64::urlDecode($parts[1]); |
|
110 | - $iv = Base64::urlDecode($parts[2]); |
|
111 | - $ciphertext = Base64::urlDecode($parts[3]); |
|
112 | - $auth_tag = Base64::urlDecode($parts[4]); |
|
113 | - return new self($header, $encrypted_key, $iv, $ciphertext, $auth_tag); |
|
114 | - } |
|
95 | + /** |
|
96 | + * Initialize from parts of compact serialization. |
|
97 | + * |
|
98 | + * @param array $parts |
|
99 | + * @throws \UnexpectedValueException |
|
100 | + * @return self |
|
101 | + */ |
|
102 | + public static function fromParts(array $parts) |
|
103 | + { |
|
104 | + if (count($parts) != 5) { |
|
105 | + throw new \UnexpectedValueException( |
|
106 | + "Invalid JWE compact serialization."); |
|
107 | + } |
|
108 | + $header = Header::fromJSON(Base64::urlDecode($parts[0])); |
|
109 | + $encrypted_key = Base64::urlDecode($parts[1]); |
|
110 | + $iv = Base64::urlDecode($parts[2]); |
|
111 | + $ciphertext = Base64::urlDecode($parts[3]); |
|
112 | + $auth_tag = Base64::urlDecode($parts[4]); |
|
113 | + return new self($header, $encrypted_key, $iv, $ciphertext, $auth_tag); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Initialize by encrypting the given payload. |
|
118 | - * |
|
119 | - * @param string $payload Payload |
|
120 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
121 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
122 | - * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
123 | - * @param Header|null $header Optional desired header. Algorithm specific |
|
124 | - * parameters are automatically added. |
|
125 | - * @param string|null $cek Optional content encryption key. Randomly |
|
126 | - * generated if not set. |
|
127 | - * @param string|null $iv Optional initialization vector. Randomly generated |
|
128 | - * if not set. |
|
129 | - * @throws \RuntimeException If encrypt fails |
|
130 | - * @return self |
|
131 | - */ |
|
132 | - public static function encrypt($payload, KeyManagementAlgorithm $key_algo, |
|
133 | - ContentEncryptionAlgorithm $enc_algo, |
|
134 | - CompressionAlgorithm $zip_algo = null, Header $header = null, $cek = null, $iv = null) |
|
135 | - { |
|
136 | - // if header was not given, initialize empty |
|
137 | - if (!isset($header)) { |
|
138 | - $header = new Header(); |
|
139 | - } |
|
140 | - // generate random CEK |
|
141 | - if (!isset($cek)) { |
|
142 | - $cek = $key_algo->cekForEncryption($enc_algo->keySize()); |
|
143 | - } |
|
144 | - // generate random IV |
|
145 | - if (!isset($iv)) { |
|
146 | - $iv = openssl_random_pseudo_bytes($enc_algo->ivSize()); |
|
147 | - } |
|
148 | - // compress |
|
149 | - if (isset($zip_algo)) { |
|
150 | - $payload = $zip_algo->compress($payload); |
|
151 | - $header = $header->withParameters(...$zip_algo->headerParameters()); |
|
152 | - } |
|
153 | - return self::_encryptContent($payload, $cek, $iv, $key_algo, $enc_algo, |
|
154 | - $header); |
|
155 | - } |
|
116 | + /** |
|
117 | + * Initialize by encrypting the given payload. |
|
118 | + * |
|
119 | + * @param string $payload Payload |
|
120 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
121 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
122 | + * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
|
123 | + * @param Header|null $header Optional desired header. Algorithm specific |
|
124 | + * parameters are automatically added. |
|
125 | + * @param string|null $cek Optional content encryption key. Randomly |
|
126 | + * generated if not set. |
|
127 | + * @param string|null $iv Optional initialization vector. Randomly generated |
|
128 | + * if not set. |
|
129 | + * @throws \RuntimeException If encrypt fails |
|
130 | + * @return self |
|
131 | + */ |
|
132 | + public static function encrypt($payload, KeyManagementAlgorithm $key_algo, |
|
133 | + ContentEncryptionAlgorithm $enc_algo, |
|
134 | + CompressionAlgorithm $zip_algo = null, Header $header = null, $cek = null, $iv = null) |
|
135 | + { |
|
136 | + // if header was not given, initialize empty |
|
137 | + if (!isset($header)) { |
|
138 | + $header = new Header(); |
|
139 | + } |
|
140 | + // generate random CEK |
|
141 | + if (!isset($cek)) { |
|
142 | + $cek = $key_algo->cekForEncryption($enc_algo->keySize()); |
|
143 | + } |
|
144 | + // generate random IV |
|
145 | + if (!isset($iv)) { |
|
146 | + $iv = openssl_random_pseudo_bytes($enc_algo->ivSize()); |
|
147 | + } |
|
148 | + // compress |
|
149 | + if (isset($zip_algo)) { |
|
150 | + $payload = $zip_algo->compress($payload); |
|
151 | + $header = $header->withParameters(...$zip_algo->headerParameters()); |
|
152 | + } |
|
153 | + return self::_encryptContent($payload, $cek, $iv, $key_algo, $enc_algo, |
|
154 | + $header); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * Encrypt content with explicit parameters. |
|
159 | - * |
|
160 | - * @param string $plaintext Plaintext content to encrypt |
|
161 | - * @param string $cek Content encryption key |
|
162 | - * @param string $iv Initialization vector |
|
163 | - * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
164 | - * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
165 | - * @param Header $header Header |
|
166 | - * @throws \UnexpectedValueException |
|
167 | - * @return self |
|
168 | - */ |
|
169 | - private static function _encryptContent($plaintext, $cek, $iv, |
|
170 | - KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
171 | - Header $header) |
|
172 | - { |
|
173 | - // check that content encryption key has correct size |
|
174 | - if (strlen($cek) != $enc_algo->keySize()) { |
|
175 | - throw new \UnexpectedValueException("Invalid key size."); |
|
176 | - } |
|
177 | - // check that initialization vector has correct size |
|
178 | - if (strlen($iv) != $enc_algo->ivSize()) { |
|
179 | - throw new \UnexpectedValueException("Invalid IV size."); |
|
180 | - } |
|
181 | - // add key and encryption algorithm parameters to the header |
|
182 | - $header = $header->withParameters(...$key_algo->headerParameters()) |
|
183 | - ->withParameters(...$enc_algo->headerParameters()); |
|
184 | - // encrypt the content encryption key |
|
185 | - $encrypted_key = $key_algo->encrypt($cek, $header); |
|
186 | - // sanity check that header wasn't unset via reference |
|
187 | - if (!$header instanceof Header) { |
|
188 | - throw new \RuntimeException("Broken key algorithm."); |
|
189 | - } |
|
190 | - // additional authenticated data |
|
191 | - $aad = Base64::urlEncode($header->toJSON()); |
|
192 | - // encrypt |
|
193 | - list($ciphertext, $auth_tag) = $enc_algo->encrypt($plaintext, $cek, $iv, |
|
194 | - $aad); |
|
195 | - return new self($header, $encrypted_key, $iv, $ciphertext, $auth_tag); |
|
196 | - } |
|
157 | + /** |
|
158 | + * Encrypt content with explicit parameters. |
|
159 | + * |
|
160 | + * @param string $plaintext Plaintext content to encrypt |
|
161 | + * @param string $cek Content encryption key |
|
162 | + * @param string $iv Initialization vector |
|
163 | + * @param KeyManagementAlgorithm $key_algo Key management algorithm |
|
164 | + * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
|
165 | + * @param Header $header Header |
|
166 | + * @throws \UnexpectedValueException |
|
167 | + * @return self |
|
168 | + */ |
|
169 | + private static function _encryptContent($plaintext, $cek, $iv, |
|
170 | + KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, |
|
171 | + Header $header) |
|
172 | + { |
|
173 | + // check that content encryption key has correct size |
|
174 | + if (strlen($cek) != $enc_algo->keySize()) { |
|
175 | + throw new \UnexpectedValueException("Invalid key size."); |
|
176 | + } |
|
177 | + // check that initialization vector has correct size |
|
178 | + if (strlen($iv) != $enc_algo->ivSize()) { |
|
179 | + throw new \UnexpectedValueException("Invalid IV size."); |
|
180 | + } |
|
181 | + // add key and encryption algorithm parameters to the header |
|
182 | + $header = $header->withParameters(...$key_algo->headerParameters()) |
|
183 | + ->withParameters(...$enc_algo->headerParameters()); |
|
184 | + // encrypt the content encryption key |
|
185 | + $encrypted_key = $key_algo->encrypt($cek, $header); |
|
186 | + // sanity check that header wasn't unset via reference |
|
187 | + if (!$header instanceof Header) { |
|
188 | + throw new \RuntimeException("Broken key algorithm."); |
|
189 | + } |
|
190 | + // additional authenticated data |
|
191 | + $aad = Base64::urlEncode($header->toJSON()); |
|
192 | + // encrypt |
|
193 | + list($ciphertext, $auth_tag) = $enc_algo->encrypt($plaintext, $cek, $iv, |
|
194 | + $aad); |
|
195 | + return new self($header, $encrypted_key, $iv, $ciphertext, $auth_tag); |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * Decrypt the content using explicit algorithms. |
|
200 | - * |
|
201 | - * @param KeyManagementAlgorithm $key_algo |
|
202 | - * @param ContentEncryptionAlgorithm $enc_algo |
|
203 | - * @throws \RuntimeException If decrypt fails |
|
204 | - * @return string Plaintext payload |
|
205 | - */ |
|
206 | - public function decrypt(KeyManagementAlgorithm $key_algo, |
|
207 | - ContentEncryptionAlgorithm $enc_algo) |
|
208 | - { |
|
209 | - // check that key management algorithm matches |
|
210 | - if ($key_algo->algorithmParamValue() != $this->algorithmName()) { |
|
211 | - throw new \UnexpectedValueException( |
|
212 | - "Invalid key management algorithm."); |
|
213 | - } |
|
214 | - // check that encryption algorithm matches |
|
215 | - if ($enc_algo->encryptionAlgorithmParamValue() != |
|
216 | - $this->encryptionAlgorithmName()) { |
|
217 | - throw new \UnexpectedValueException("Invalid encryption algorithm."); |
|
218 | - } |
|
219 | - $header = $this->header(); |
|
220 | - // decrypt content encryption key |
|
221 | - $cek = $key_algo->decrypt($this->_encryptedKey, $header); |
|
222 | - // decrypt payload |
|
223 | - $aad = Base64::urlEncode($this->_protectedHeader->toJSON()); |
|
224 | - $payload = $enc_algo->decrypt($this->_ciphertext, $cek, $this->_iv, $aad, |
|
225 | - $this->_authenticationTag); |
|
226 | - // decompress |
|
227 | - if ($header->hasCompressionAlgorithm()) { |
|
228 | - $payload = CompressionFactory::algoByHeader($header)->decompress( |
|
229 | - $payload); |
|
230 | - } |
|
231 | - return $payload; |
|
232 | - } |
|
198 | + /** |
|
199 | + * Decrypt the content using explicit algorithms. |
|
200 | + * |
|
201 | + * @param KeyManagementAlgorithm $key_algo |
|
202 | + * @param ContentEncryptionAlgorithm $enc_algo |
|
203 | + * @throws \RuntimeException If decrypt fails |
|
204 | + * @return string Plaintext payload |
|
205 | + */ |
|
206 | + public function decrypt(KeyManagementAlgorithm $key_algo, |
|
207 | + ContentEncryptionAlgorithm $enc_algo) |
|
208 | + { |
|
209 | + // check that key management algorithm matches |
|
210 | + if ($key_algo->algorithmParamValue() != $this->algorithmName()) { |
|
211 | + throw new \UnexpectedValueException( |
|
212 | + "Invalid key management algorithm."); |
|
213 | + } |
|
214 | + // check that encryption algorithm matches |
|
215 | + if ($enc_algo->encryptionAlgorithmParamValue() != |
|
216 | + $this->encryptionAlgorithmName()) { |
|
217 | + throw new \UnexpectedValueException("Invalid encryption algorithm."); |
|
218 | + } |
|
219 | + $header = $this->header(); |
|
220 | + // decrypt content encryption key |
|
221 | + $cek = $key_algo->decrypt($this->_encryptedKey, $header); |
|
222 | + // decrypt payload |
|
223 | + $aad = Base64::urlEncode($this->_protectedHeader->toJSON()); |
|
224 | + $payload = $enc_algo->decrypt($this->_ciphertext, $cek, $this->_iv, $aad, |
|
225 | + $this->_authenticationTag); |
|
226 | + // decompress |
|
227 | + if ($header->hasCompressionAlgorithm()) { |
|
228 | + $payload = CompressionFactory::algoByHeader($header)->decompress( |
|
229 | + $payload); |
|
230 | + } |
|
231 | + return $payload; |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Decrypt content using given JWK. |
|
236 | - * |
|
237 | - * Key management and content encryption algorithms are determined from the |
|
238 | - * header. |
|
239 | - * |
|
240 | - * @param JWK $jwk JSON Web Key |
|
241 | - * @throws \RuntimeException If algorithm initialization fails |
|
242 | - * @return string Plaintext payload |
|
243 | - */ |
|
244 | - public function decryptWithJWK(JWK $jwk) |
|
245 | - { |
|
246 | - $header = $this->header(); |
|
247 | - $key_algo = KeyManagementAlgorithm::fromJWK($jwk, $header); |
|
248 | - $enc_algo = EncryptionAlgorithmFactory::algoByHeader($header); |
|
249 | - return $this->decrypt($key_algo, $enc_algo); |
|
250 | - } |
|
234 | + /** |
|
235 | + * Decrypt content using given JWK. |
|
236 | + * |
|
237 | + * Key management and content encryption algorithms are determined from the |
|
238 | + * header. |
|
239 | + * |
|
240 | + * @param JWK $jwk JSON Web Key |
|
241 | + * @throws \RuntimeException If algorithm initialization fails |
|
242 | + * @return string Plaintext payload |
|
243 | + */ |
|
244 | + public function decryptWithJWK(JWK $jwk) |
|
245 | + { |
|
246 | + $header = $this->header(); |
|
247 | + $key_algo = KeyManagementAlgorithm::fromJWK($jwk, $header); |
|
248 | + $enc_algo = EncryptionAlgorithmFactory::algoByHeader($header); |
|
249 | + return $this->decrypt($key_algo, $enc_algo); |
|
250 | + } |
|
251 | 251 | |
252 | - /** |
|
253 | - * Decrypt content using a key from the given JWK set. |
|
254 | - * |
|
255 | - * Correct key shall be sought by the key ID indicated by the header. |
|
256 | - * |
|
257 | - * @param JWKSet $set Set of JSON Web Keys |
|
258 | - * @throws \RuntimeException If algorithm initialization fails |
|
259 | - * @return string Plaintext payload |
|
260 | - */ |
|
261 | - public function decryptWithJWKSet(JWKSet $set) |
|
262 | - { |
|
263 | - if (!count($set)) { |
|
264 | - throw new \RuntimeException("No keys."); |
|
265 | - } |
|
266 | - $header = $this->header(); |
|
267 | - $factory = new KeyAlgorithmFactory($header); |
|
268 | - $key_algo = $factory->algoByKeys($set); |
|
269 | - $enc_algo = EncryptionAlgorithmFactory::algoByHeader($header); |
|
270 | - return $this->decrypt($key_algo, $enc_algo); |
|
271 | - } |
|
252 | + /** |
|
253 | + * Decrypt content using a key from the given JWK set. |
|
254 | + * |
|
255 | + * Correct key shall be sought by the key ID indicated by the header. |
|
256 | + * |
|
257 | + * @param JWKSet $set Set of JSON Web Keys |
|
258 | + * @throws \RuntimeException If algorithm initialization fails |
|
259 | + * @return string Plaintext payload |
|
260 | + */ |
|
261 | + public function decryptWithJWKSet(JWKSet $set) |
|
262 | + { |
|
263 | + if (!count($set)) { |
|
264 | + throw new \RuntimeException("No keys."); |
|
265 | + } |
|
266 | + $header = $this->header(); |
|
267 | + $factory = new KeyAlgorithmFactory($header); |
|
268 | + $key_algo = $factory->algoByKeys($set); |
|
269 | + $enc_algo = EncryptionAlgorithmFactory::algoByHeader($header); |
|
270 | + return $this->decrypt($key_algo, $enc_algo); |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * Get JOSE header. |
|
275 | - * |
|
276 | - * @return JOSE |
|
277 | - */ |
|
278 | - public function header() |
|
279 | - { |
|
280 | - return new JOSE($this->_protectedHeader); |
|
281 | - } |
|
273 | + /** |
|
274 | + * Get JOSE header. |
|
275 | + * |
|
276 | + * @return JOSE |
|
277 | + */ |
|
278 | + public function header() |
|
279 | + { |
|
280 | + return new JOSE($this->_protectedHeader); |
|
281 | + } |
|
282 | 282 | |
283 | - /** |
|
284 | - * Get the name of the key management algorithm. |
|
285 | - * |
|
286 | - * @return string |
|
287 | - */ |
|
288 | - public function algorithmName() |
|
289 | - { |
|
290 | - return $this->header() |
|
291 | - ->algorithm() |
|
292 | - ->value(); |
|
293 | - } |
|
283 | + /** |
|
284 | + * Get the name of the key management algorithm. |
|
285 | + * |
|
286 | + * @return string |
|
287 | + */ |
|
288 | + public function algorithmName() |
|
289 | + { |
|
290 | + return $this->header() |
|
291 | + ->algorithm() |
|
292 | + ->value(); |
|
293 | + } |
|
294 | 294 | |
295 | - /** |
|
296 | - * Get the name of the encryption algorithm. |
|
297 | - * |
|
298 | - * @return string |
|
299 | - */ |
|
300 | - public function encryptionAlgorithmName() |
|
301 | - { |
|
302 | - return $this->header() |
|
303 | - ->encryptionAlgorithm() |
|
304 | - ->value(); |
|
305 | - } |
|
295 | + /** |
|
296 | + * Get the name of the encryption algorithm. |
|
297 | + * |
|
298 | + * @return string |
|
299 | + */ |
|
300 | + public function encryptionAlgorithmName() |
|
301 | + { |
|
302 | + return $this->header() |
|
303 | + ->encryptionAlgorithm() |
|
304 | + ->value(); |
|
305 | + } |
|
306 | 306 | |
307 | - /** |
|
308 | - * Get encrypted CEK. |
|
309 | - * |
|
310 | - * @return string |
|
311 | - */ |
|
312 | - public function encryptedKey() |
|
313 | - { |
|
314 | - return $this->_encryptedKey; |
|
315 | - } |
|
307 | + /** |
|
308 | + * Get encrypted CEK. |
|
309 | + * |
|
310 | + * @return string |
|
311 | + */ |
|
312 | + public function encryptedKey() |
|
313 | + { |
|
314 | + return $this->_encryptedKey; |
|
315 | + } |
|
316 | 316 | |
317 | - /** |
|
318 | - * Get initialization vector. |
|
319 | - * |
|
320 | - * @return string |
|
321 | - */ |
|
322 | - public function initializationVector() |
|
323 | - { |
|
324 | - return $this->_iv; |
|
325 | - } |
|
317 | + /** |
|
318 | + * Get initialization vector. |
|
319 | + * |
|
320 | + * @return string |
|
321 | + */ |
|
322 | + public function initializationVector() |
|
323 | + { |
|
324 | + return $this->_iv; |
|
325 | + } |
|
326 | 326 | |
327 | - /** |
|
328 | - * Get ciphertext. |
|
329 | - * |
|
330 | - * @return string |
|
331 | - */ |
|
332 | - public function ciphertext() |
|
333 | - { |
|
334 | - return $this->_ciphertext; |
|
335 | - } |
|
327 | + /** |
|
328 | + * Get ciphertext. |
|
329 | + * |
|
330 | + * @return string |
|
331 | + */ |
|
332 | + public function ciphertext() |
|
333 | + { |
|
334 | + return $this->_ciphertext; |
|
335 | + } |
|
336 | 336 | |
337 | - /** |
|
338 | - * Get authentication tag. |
|
339 | - * |
|
340 | - * @return string |
|
341 | - */ |
|
342 | - public function authenticationTag() |
|
343 | - { |
|
344 | - return $this->_authenticationTag; |
|
345 | - } |
|
337 | + /** |
|
338 | + * Get authentication tag. |
|
339 | + * |
|
340 | + * @return string |
|
341 | + */ |
|
342 | + public function authenticationTag() |
|
343 | + { |
|
344 | + return $this->_authenticationTag; |
|
345 | + } |
|
346 | 346 | |
347 | - /** |
|
348 | - * Convert to compact serialization. |
|
349 | - * |
|
350 | - * @return string |
|
351 | - */ |
|
352 | - public function toCompact() |
|
353 | - { |
|
354 | - return Base64::urlEncode($this->_protectedHeader->toJSON()) . "." . |
|
355 | - Base64::urlEncode($this->_encryptedKey) . "." . |
|
356 | - Base64::urlEncode($this->_iv) . "." . |
|
357 | - Base64::urlEncode($this->_ciphertext) . "." . |
|
358 | - Base64::urlEncode($this->_authenticationTag); |
|
359 | - } |
|
347 | + /** |
|
348 | + * Convert to compact serialization. |
|
349 | + * |
|
350 | + * @return string |
|
351 | + */ |
|
352 | + public function toCompact() |
|
353 | + { |
|
354 | + return Base64::urlEncode($this->_protectedHeader->toJSON()) . "." . |
|
355 | + Base64::urlEncode($this->_encryptedKey) . "." . |
|
356 | + Base64::urlEncode($this->_iv) . "." . |
|
357 | + Base64::urlEncode($this->_ciphertext) . "." . |
|
358 | + Base64::urlEncode($this->_authenticationTag); |
|
359 | + } |
|
360 | 360 | |
361 | - /** |
|
362 | - * Convert JWE to string. |
|
363 | - * |
|
364 | - * @return string |
|
365 | - */ |
|
366 | - public function __toString() |
|
367 | - { |
|
368 | - return $this->toCompact(); |
|
369 | - } |
|
361 | + /** |
|
362 | + * Convert JWE to string. |
|
363 | + * |
|
364 | + * @return string |
|
365 | + */ |
|
366 | + public function __toString() |
|
367 | + { |
|
368 | + return $this->toCompact(); |
|
369 | + } |
|
370 | 370 | } |
@@ -12,92 +12,92 @@ |
||
12 | 12 | */ |
13 | 13 | class KeyAlgorithmFactory |
14 | 14 | { |
15 | - /** |
|
16 | - * Header. |
|
17 | - * |
|
18 | - * @var Header $_header |
|
19 | - */ |
|
20 | - protected $_header; |
|
15 | + /** |
|
16 | + * Header. |
|
17 | + * |
|
18 | + * @var Header $_header |
|
19 | + */ |
|
20 | + protected $_header; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Mapping from algorithm name to class name. |
|
24 | - * |
|
25 | - * @internal |
|
26 | - * |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - const MAP_ALGO_TO_CLASS = array( |
|
30 | - /* @formatter:off */ |
|
31 | - JWA::ALGO_A128KW => A128KWAlgorithm::class, |
|
32 | - JWA::ALGO_A192KW => A192KWAlgorithm::class, |
|
33 | - JWA::ALGO_A128KW => A256KWAlgorithm::class, |
|
34 | - JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
|
35 | - JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
|
36 | - JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class, |
|
37 | - JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class, |
|
38 | - JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class, |
|
39 | - JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class, |
|
40 | - JWA::ALGO_DIR => DirectCEKAlgorithm::class, |
|
41 | - JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
|
42 | - JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class |
|
43 | - /* @formatter:on */ |
|
44 | - ); |
|
22 | + /** |
|
23 | + * Mapping from algorithm name to class name. |
|
24 | + * |
|
25 | + * @internal |
|
26 | + * |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + const MAP_ALGO_TO_CLASS = array( |
|
30 | + /* @formatter:off */ |
|
31 | + JWA::ALGO_A128KW => A128KWAlgorithm::class, |
|
32 | + JWA::ALGO_A192KW => A192KWAlgorithm::class, |
|
33 | + JWA::ALGO_A128KW => A256KWAlgorithm::class, |
|
34 | + JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class, |
|
35 | + JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class, |
|
36 | + JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class, |
|
37 | + JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class, |
|
38 | + JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class, |
|
39 | + JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class, |
|
40 | + JWA::ALGO_DIR => DirectCEKAlgorithm::class, |
|
41 | + JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
|
42 | + JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class |
|
43 | + /* @formatter:on */ |
|
44 | + ); |
|
45 | 45 | |
46 | - /** |
|
47 | - * Constructor. |
|
48 | - * |
|
49 | - * @param Header $header |
|
50 | - */ |
|
51 | - public function __construct(Header $header) |
|
52 | - { |
|
53 | - $this->_header = $header; |
|
54 | - } |
|
46 | + /** |
|
47 | + * Constructor. |
|
48 | + * |
|
49 | + * @param Header $header |
|
50 | + */ |
|
51 | + public function __construct(Header $header) |
|
52 | + { |
|
53 | + $this->_header = $header; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get key management algorithm by given JWK. |
|
58 | - * |
|
59 | - * @param JWK $jwk |
|
60 | - * @return \JWX\JWE\KeyManagementAlgorithm |
|
61 | - */ |
|
62 | - public function algoByKey(JWK $jwk) |
|
63 | - { |
|
64 | - $alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
65 | - $cls = self::_algoClassByName($alg); |
|
66 | - return $cls::fromJWK($jwk, $this->_header); |
|
67 | - } |
|
56 | + /** |
|
57 | + * Get key management algorithm by given JWK. |
|
58 | + * |
|
59 | + * @param JWK $jwk |
|
60 | + * @return \JWX\JWE\KeyManagementAlgorithm |
|
61 | + */ |
|
62 | + public function algoByKey(JWK $jwk) |
|
63 | + { |
|
64 | + $alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
|
65 | + $cls = self::_algoClassByName($alg); |
|
66 | + return $cls::fromJWK($jwk, $this->_header); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Get key management algorithm using a matching key from given JWK set. |
|
71 | - * |
|
72 | - * @param JWKSet $set |
|
73 | - * @throws \UnexpectedValueException If a key cannot be found |
|
74 | - * @return \JWX\JWE\KeyManagementAlgorithm |
|
75 | - */ |
|
76 | - public function algoByKeys(JWKSet $set) |
|
77 | - { |
|
78 | - if (!$this->_header->hasKeyID()) { |
|
79 | - throw new \UnexpectedValueException("No key ID paremeter."); |
|
80 | - } |
|
81 | - $id = $this->_header->keyID()->value(); |
|
82 | - if (!$set->hasKeyID($id)) { |
|
83 | - throw new \UnexpectedValueException("No key for ID '$id'."); |
|
84 | - } |
|
85 | - return $this->algoByKey($set->keyByID($id)); |
|
86 | - } |
|
69 | + /** |
|
70 | + * Get key management algorithm using a matching key from given JWK set. |
|
71 | + * |
|
72 | + * @param JWKSet $set |
|
73 | + * @throws \UnexpectedValueException If a key cannot be found |
|
74 | + * @return \JWX\JWE\KeyManagementAlgorithm |
|
75 | + */ |
|
76 | + public function algoByKeys(JWKSet $set) |
|
77 | + { |
|
78 | + if (!$this->_header->hasKeyID()) { |
|
79 | + throw new \UnexpectedValueException("No key ID paremeter."); |
|
80 | + } |
|
81 | + $id = $this->_header->keyID()->value(); |
|
82 | + if (!$set->hasKeyID($id)) { |
|
83 | + throw new \UnexpectedValueException("No key for ID '$id'."); |
|
84 | + } |
|
85 | + return $this->algoByKey($set->keyByID($id)); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get the algorithm implementation class name by an algorithm name. |
|
90 | - * |
|
91 | - * @param string $alg Algorithm name |
|
92 | - * @throws \UnexpectedValueException |
|
93 | - * @return string Class name |
|
94 | - */ |
|
95 | - private static function _algoClassByName($alg) |
|
96 | - { |
|
97 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
98 | - throw new \UnexpectedValueException( |
|
99 | - "Algorithm '$alg' not supported."); |
|
100 | - } |
|
101 | - return self::MAP_ALGO_TO_CLASS[$alg]; |
|
102 | - } |
|
88 | + /** |
|
89 | + * Get the algorithm implementation class name by an algorithm name. |
|
90 | + * |
|
91 | + * @param string $alg Algorithm name |
|
92 | + * @throws \UnexpectedValueException |
|
93 | + * @return string Class name |
|
94 | + */ |
|
95 | + private static function _algoClassByName($alg) |
|
96 | + { |
|
97 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
98 | + throw new \UnexpectedValueException( |
|
99 | + "Algorithm '$alg' not supported."); |
|
100 | + } |
|
101 | + return self::MAP_ALGO_TO_CLASS[$alg]; |
|
102 | + } |
|
103 | 103 | } |
@@ -16,104 +16,104 @@ |
||
16 | 16 | */ |
17 | 17 | class DirectCEKAlgorithm extends KeyManagementAlgorithm |
18 | 18 | { |
19 | - /** |
|
20 | - * Content encryption key. |
|
21 | - * |
|
22 | - * @var string $_cek |
|
23 | - */ |
|
24 | - protected $_cek; |
|
19 | + /** |
|
20 | + * Content encryption key. |
|
21 | + * |
|
22 | + * @var string $_cek |
|
23 | + */ |
|
24 | + protected $_cek; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param string $cek Content encryption key |
|
30 | - */ |
|
31 | - public function __construct($cek) |
|
32 | - { |
|
33 | - $this->_cek = $cek; |
|
34 | - } |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param string $cek Content encryption key |
|
30 | + */ |
|
31 | + public function __construct($cek) |
|
32 | + { |
|
33 | + $this->_cek = $cek; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * @param JWK $jwk |
|
39 | - * @param Header $header |
|
40 | - * @throws \UnexpectedValueException |
|
41 | - * @return self |
|
42 | - */ |
|
43 | - public static function fromJWK(JWK $jwk, Header $header) |
|
44 | - { |
|
45 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
46 | - $alg = JWA::deriveAlgorithmName($header); |
|
47 | - if ($alg != JWA::ALGO_DIR) { |
|
48 | - throw new \UnexpectedValueException("Invalid algorithm '$alg'."); |
|
49 | - } |
|
50 | - return new self($jwk->key()); |
|
51 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * @param JWK $jwk |
|
39 | + * @param Header $header |
|
40 | + * @throws \UnexpectedValueException |
|
41 | + * @return self |
|
42 | + */ |
|
43 | + public static function fromJWK(JWK $jwk, Header $header) |
|
44 | + { |
|
45 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
46 | + $alg = JWA::deriveAlgorithmName($header); |
|
47 | + if ($alg != JWA::ALGO_DIR) { |
|
48 | + throw new \UnexpectedValueException("Invalid algorithm '$alg'."); |
|
49 | + } |
|
50 | + return new self($jwk->key()); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get content encryption key. |
|
55 | - * |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - public function cek() |
|
59 | - { |
|
60 | - return $this->_cek; |
|
61 | - } |
|
53 | + /** |
|
54 | + * Get content encryption key. |
|
55 | + * |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + public function cek() |
|
59 | + { |
|
60 | + return $this->_cek; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * |
|
65 | - * {@inheritdoc} |
|
66 | - */ |
|
67 | - protected function _encryptKey($key, Header &$header) |
|
68 | - { |
|
69 | - if ($key != $this->_cek) { |
|
70 | - throw new \LogicException("Content encryption key doesn't match."); |
|
71 | - } |
|
72 | - return ""; |
|
73 | - } |
|
63 | + /** |
|
64 | + * |
|
65 | + * {@inheritdoc} |
|
66 | + */ |
|
67 | + protected function _encryptKey($key, Header &$header) |
|
68 | + { |
|
69 | + if ($key != $this->_cek) { |
|
70 | + throw new \LogicException("Content encryption key doesn't match."); |
|
71 | + } |
|
72 | + return ""; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * |
|
77 | - * {@inheritdoc} |
|
78 | - */ |
|
79 | - protected function _decryptKey($ciphertext, Header $header) |
|
80 | - { |
|
81 | - if ($ciphertext !== "") { |
|
82 | - throw new \UnexpectedValueException( |
|
83 | - "Encrypted key must be an empty octet sequence."); |
|
84 | - } |
|
85 | - return $this->_cek; |
|
86 | - } |
|
75 | + /** |
|
76 | + * |
|
77 | + * {@inheritdoc} |
|
78 | + */ |
|
79 | + protected function _decryptKey($ciphertext, Header $header) |
|
80 | + { |
|
81 | + if ($ciphertext !== "") { |
|
82 | + throw new \UnexpectedValueException( |
|
83 | + "Encrypted key must be an empty octet sequence."); |
|
84 | + } |
|
85 | + return $this->_cek; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * |
|
90 | - * {@inheritdoc} |
|
91 | - */ |
|
92 | - public function cekForEncryption($length) |
|
93 | - { |
|
94 | - if (strlen($this->_cek) != $length) { |
|
95 | - throw new \UnexpectedValueException("Invalid key length."); |
|
96 | - } |
|
97 | - return $this->_cek; |
|
98 | - } |
|
88 | + /** |
|
89 | + * |
|
90 | + * {@inheritdoc} |
|
91 | + */ |
|
92 | + public function cekForEncryption($length) |
|
93 | + { |
|
94 | + if (strlen($this->_cek) != $length) { |
|
95 | + throw new \UnexpectedValueException("Invalid key length."); |
|
96 | + } |
|
97 | + return $this->_cek; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * |
|
102 | - * {@inheritdoc} |
|
103 | - */ |
|
104 | - public function algorithmParamValue() |
|
105 | - { |
|
106 | - return JWA::ALGO_DIR; |
|
107 | - } |
|
100 | + /** |
|
101 | + * |
|
102 | + * {@inheritdoc} |
|
103 | + */ |
|
104 | + public function algorithmParamValue() |
|
105 | + { |
|
106 | + return JWA::ALGO_DIR; |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * |
|
111 | - * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
112 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
113 | - */ |
|
114 | - public function headerParameters() |
|
115 | - { |
|
116 | - return array_merge(parent::headerParameters(), |
|
117 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
118 | - } |
|
109 | + /** |
|
110 | + * |
|
111 | + * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
112 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
113 | + */ |
|
114 | + public function headerParameters() |
|
115 | + { |
|
116 | + return array_merge(parent::headerParameters(), |
|
117 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
118 | + } |
|
119 | 119 | } |
@@ -64,7 +64,7 @@ |
||
64 | 64 | * |
65 | 65 | * {@inheritdoc} |
66 | 66 | */ |
67 | - protected function _encryptKey($key, Header &$header) |
|
67 | + protected function _encryptKey($key, Header&$header) |
|
68 | 68 | { |
69 | 69 | if ($key != $this->_cek) { |
70 | 70 | throw new \LogicException("Content encryption key doesn't match."); |
@@ -12,39 +12,39 @@ |
||
12 | 12 | */ |
13 | 13 | class PBES2HS384A192KWAlgorithm extends PBES2Algorithm |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - protected function _hashAlgo() |
|
20 | - { |
|
21 | - return "sha384"; |
|
22 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + protected function _hashAlgo() |
|
20 | + { |
|
21 | + return "sha384"; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected function _keyLength() |
|
29 | - { |
|
30 | - return 24; |
|
31 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected function _keyLength() |
|
29 | + { |
|
30 | + return 24; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - protected function _kwAlgo() |
|
38 | - { |
|
39 | - return new AESKW192(); |
|
40 | - } |
|
33 | + /** |
|
34 | + * |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + protected function _kwAlgo() |
|
38 | + { |
|
39 | + return new AESKW192(); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * |
|
44 | - * {@inheritdoc} |
|
45 | - */ |
|
46 | - public function algorithmParamValue() |
|
47 | - { |
|
48 | - return JWA::ALGO_PBES2_HS384_A192KW; |
|
49 | - } |
|
42 | + /** |
|
43 | + * |
|
44 | + * {@inheritdoc} |
|
45 | + */ |
|
46 | + public function algorithmParamValue() |
|
47 | + { |
|
48 | + return JWA::ALGO_PBES2_HS384_A192KW; |
|
49 | + } |
|
50 | 50 | } |
@@ -12,30 +12,30 @@ |
||
12 | 12 | */ |
13 | 13 | class A192GCMKWAlgorithm extends AESGCMKWAlgorithm |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - protected function _getGCMCipher() |
|
20 | - { |
|
21 | - return new AES192Cipher(); |
|
22 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + protected function _getGCMCipher() |
|
20 | + { |
|
21 | + return new AES192Cipher(); |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected function _keySize() |
|
29 | - { |
|
30 | - return 24; |
|
31 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected function _keySize() |
|
29 | + { |
|
30 | + return 24; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - public function algorithmParamValue() |
|
38 | - { |
|
39 | - return JWA::ALGO_A192GCMKW; |
|
40 | - } |
|
33 | + /** |
|
34 | + * |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + public function algorithmParamValue() |
|
38 | + { |
|
39 | + return JWA::ALGO_A192GCMKW; |
|
40 | + } |
|
41 | 41 | } |
@@ -17,124 +17,124 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class AESKWAlgorithm extends KeyManagementAlgorithm |
19 | 19 | { |
20 | - use RandomCEK; |
|
20 | + use RandomCEK; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Key encryption key. |
|
24 | - * |
|
25 | - * @var string $_kek |
|
26 | - */ |
|
27 | - protected $_kek; |
|
22 | + /** |
|
23 | + * Key encryption key. |
|
24 | + * |
|
25 | + * @var string $_kek |
|
26 | + */ |
|
27 | + protected $_kek; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Key wrapping algorithm. |
|
31 | - * |
|
32 | - * Lazily initialized. |
|
33 | - * |
|
34 | - * @var \AESKW\AESKeyWrapAlgorithm|null $_kw |
|
35 | - */ |
|
36 | - protected $_kw; |
|
29 | + /** |
|
30 | + * Key wrapping algorithm. |
|
31 | + * |
|
32 | + * Lazily initialized. |
|
33 | + * |
|
34 | + * @var \AESKW\AESKeyWrapAlgorithm|null $_kw |
|
35 | + */ |
|
36 | + protected $_kw; |
|
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_A128KW => A128KWAlgorithm::class, |
|
48 | - JWA::ALGO_A192KW => A192KWAlgorithm::class, |
|
49 | - JWA::ALGO_A256KW => A256KWAlgorithm::class |
|
50 | - /* @formatter:on */ |
|
51 | - ); |
|
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_A128KW => A128KWAlgorithm::class, |
|
48 | + JWA::ALGO_A192KW => A192KWAlgorithm::class, |
|
49 | + JWA::ALGO_A256KW => A256KWAlgorithm::class |
|
50 | + /* @formatter:on */ |
|
51 | + ); |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the size of the key encryption key in bytes. |
|
55 | - * |
|
56 | - * @return int |
|
57 | - */ |
|
58 | - abstract protected function _kekSize(); |
|
53 | + /** |
|
54 | + * Get the size of the key encryption key in bytes. |
|
55 | + * |
|
56 | + * @return int |
|
57 | + */ |
|
58 | + abstract protected function _kekSize(); |
|
59 | 59 | |
60 | - /** |
|
61 | - * Get key wrapping algorithm instance. |
|
62 | - * |
|
63 | - * @return \AESKW\AESKeyWrapAlgorithm |
|
64 | - */ |
|
65 | - abstract protected function _AESKWAlgo(); |
|
60 | + /** |
|
61 | + * Get key wrapping algorithm instance. |
|
62 | + * |
|
63 | + * @return \AESKW\AESKeyWrapAlgorithm |
|
64 | + */ |
|
65 | + abstract protected function _AESKWAlgo(); |
|
66 | 66 | |
67 | - /** |
|
68 | - * Constructor. |
|
69 | - * |
|
70 | - * @param string $kek Key encryption key |
|
71 | - */ |
|
72 | - public function __construct($kek) |
|
73 | - { |
|
74 | - if (strlen($kek) != $this->_kekSize()) { |
|
75 | - throw new \LengthException( |
|
76 | - "Key encryption key must be " . $this->_kekSize() . " bytes."); |
|
77 | - } |
|
78 | - $this->_kek = $kek; |
|
79 | - } |
|
67 | + /** |
|
68 | + * Constructor. |
|
69 | + * |
|
70 | + * @param string $kek Key encryption key |
|
71 | + */ |
|
72 | + public function __construct($kek) |
|
73 | + { |
|
74 | + if (strlen($kek) != $this->_kekSize()) { |
|
75 | + throw new \LengthException( |
|
76 | + "Key encryption key must be " . $this->_kekSize() . " bytes."); |
|
77 | + } |
|
78 | + $this->_kek = $kek; |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * |
|
83 | - * @param JWK $jwk |
|
84 | - * @param Header $header |
|
85 | - * @throws \UnexpectedValueException |
|
86 | - * @return AESKWAlgorithm |
|
87 | - */ |
|
88 | - public static function fromJWK(JWK $jwk, Header $header) |
|
89 | - { |
|
90 | - $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
91 | - $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
92 | - if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
93 | - throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
94 | - } |
|
95 | - $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
96 | - return new $cls($jwk->key()); |
|
97 | - } |
|
81 | + /** |
|
82 | + * |
|
83 | + * @param JWK $jwk |
|
84 | + * @param Header $header |
|
85 | + * @throws \UnexpectedValueException |
|
86 | + * @return AESKWAlgorithm |
|
87 | + */ |
|
88 | + public static function fromJWK(JWK $jwk, Header $header) |
|
89 | + { |
|
90 | + $jwk = SymmetricKeyJWK::fromJWK($jwk); |
|
91 | + $alg = JWA::deriveAlgorithmName($header, $jwk); |
|
92 | + if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
|
93 | + throw new \UnexpectedValueException("Unsupported algorithm '$alg'."); |
|
94 | + } |
|
95 | + $cls = self::MAP_ALGO_TO_CLASS[$alg]; |
|
96 | + return new $cls($jwk->key()); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Get key wrapping algorithm. |
|
101 | - * |
|
102 | - * @return \AESKW\AESKeyWrapAlgorithm |
|
103 | - */ |
|
104 | - protected function _kw() |
|
105 | - { |
|
106 | - if (!isset($this->_kw)) { |
|
107 | - $this->_kw = $this->_AESKWAlgo(); |
|
108 | - } |
|
109 | - return $this->_kw; |
|
110 | - } |
|
99 | + /** |
|
100 | + * Get key wrapping algorithm. |
|
101 | + * |
|
102 | + * @return \AESKW\AESKeyWrapAlgorithm |
|
103 | + */ |
|
104 | + protected function _kw() |
|
105 | + { |
|
106 | + if (!isset($this->_kw)) { |
|
107 | + $this->_kw = $this->_AESKWAlgo(); |
|
108 | + } |
|
109 | + return $this->_kw; |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * |
|
114 | - * {@inheritdoc} |
|
115 | - */ |
|
116 | - protected function _encryptKey($key, Header &$header) |
|
117 | - { |
|
118 | - return $this->_kw()->wrap($key, $this->_kek); |
|
119 | - } |
|
112 | + /** |
|
113 | + * |
|
114 | + * {@inheritdoc} |
|
115 | + */ |
|
116 | + protected function _encryptKey($key, Header &$header) |
|
117 | + { |
|
118 | + return $this->_kw()->wrap($key, $this->_kek); |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * |
|
123 | - * {@inheritdoc} |
|
124 | - */ |
|
125 | - protected function _decryptKey($ciphertext, Header $header) |
|
126 | - { |
|
127 | - return $this->_kw()->unwrap($ciphertext, $this->_kek); |
|
128 | - } |
|
121 | + /** |
|
122 | + * |
|
123 | + * {@inheritdoc} |
|
124 | + */ |
|
125 | + protected function _decryptKey($ciphertext, Header $header) |
|
126 | + { |
|
127 | + return $this->_kw()->unwrap($ciphertext, $this->_kek); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * |
|
132 | - * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
133 | - * @return \JWX\JWT\Parameter\JWTParameter[] |
|
134 | - */ |
|
135 | - public function headerParameters() |
|
136 | - { |
|
137 | - return array_merge(parent::headerParameters(), |
|
138 | - array(AlgorithmParameter::fromAlgorithm($this))); |
|
139 | - } |
|
130 | + /** |
|
131 | + * |
|
132 | + * @see \JWX\JWE\KeyManagementAlgorithm::headerParameters() |
|
133 | + * @return \JWX\JWT\Parameter\JWTParameter[] |
|
134 | + */ |
|
135 | + public function headerParameters() |
|
136 | + { |
|
137 | + return array_merge(parent::headerParameters(), |
|
138 | + array(AlgorithmParameter::fromAlgorithm($this))); |
|
139 | + } |
|
140 | 140 | } |
@@ -113,7 +113,7 @@ |
||
113 | 113 | * |
114 | 114 | * {@inheritdoc} |
115 | 115 | */ |
116 | - protected function _encryptKey($key, Header &$header) |
|
116 | + protected function _encryptKey($key, Header&$header) |
|
117 | 117 | { |
118 | 118 | return $this->_kw()->wrap($key, $this->_kek); |
119 | 119 | } |
@@ -12,30 +12,30 @@ |
||
12 | 12 | */ |
13 | 13 | class A256KWAlgorithm extends AESKWAlgorithm |
14 | 14 | { |
15 | - /** |
|
16 | - * |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - protected function _kekSize() |
|
20 | - { |
|
21 | - return 32; |
|
22 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + protected function _kekSize() |
|
20 | + { |
|
21 | + return 32; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected function _AESKWAlgo() |
|
29 | - { |
|
30 | - return new AESKW256(); |
|
31 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected function _AESKWAlgo() |
|
29 | + { |
|
30 | + return new AESKW256(); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - public function algorithmParamValue() |
|
38 | - { |
|
39 | - return JWA::ALGO_A256KW; |
|
40 | - } |
|
33 | + /** |
|
34 | + * |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + public function algorithmParamValue() |
|
38 | + { |
|
39 | + return JWA::ALGO_A256KW; |
|
40 | + } |
|
41 | 41 | } |