@@ -7,21 +7,21 @@ |
||
7 | 7 | */ |
8 | 8 | trait StringParameterValue |
9 | 9 | { |
10 | - /** |
|
11 | - * Constructor. |
|
12 | - * |
|
13 | - * @param string $value Parameter value |
|
14 | - */ |
|
15 | - abstract public function __construct($value); |
|
10 | + /** |
|
11 | + * Constructor. |
|
12 | + * |
|
13 | + * @param string $value Parameter value |
|
14 | + */ |
|
15 | + abstract public function __construct($value); |
|
16 | 16 | |
17 | - /** |
|
18 | - * Initialize from a JSON value. |
|
19 | - * |
|
20 | - * @param string $value |
|
21 | - * @return static |
|
22 | - */ |
|
23 | - public static function fromJSONValue($value) |
|
24 | - { |
|
25 | - return new static(strval($value)); |
|
26 | - } |
|
17 | + /** |
|
18 | + * Initialize from a JSON value. |
|
19 | + * |
|
20 | + * @param string $value |
|
21 | + * @return static |
|
22 | + */ |
|
23 | + public static function fromJSONValue($value) |
|
24 | + { |
|
25 | + return new static(strval($value)); |
|
26 | + } |
|
27 | 27 | } |
@@ -12,27 +12,27 @@ |
||
12 | 12 | */ |
13 | 13 | trait Base64UIntValue |
14 | 14 | { |
15 | - use Base64URLValue; |
|
15 | + use Base64URLValue; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Initialize parameter from base10 number. |
|
19 | - * |
|
20 | - * @param int|string $number |
|
21 | - * @return self |
|
22 | - */ |
|
23 | - public static function fromNumber($number) |
|
24 | - { |
|
25 | - $data = BigInt::fromBase10($number)->base256(); |
|
26 | - return self::fromString($data); |
|
27 | - } |
|
17 | + /** |
|
18 | + * Initialize parameter from base10 number. |
|
19 | + * |
|
20 | + * @param int|string $number |
|
21 | + * @return self |
|
22 | + */ |
|
23 | + public static function fromNumber($number) |
|
24 | + { |
|
25 | + $data = BigInt::fromBase10($number)->base256(); |
|
26 | + return self::fromString($data); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Get value as a number. |
|
31 | - * |
|
32 | - * @return BigInt |
|
33 | - */ |
|
34 | - public function number() |
|
35 | - { |
|
36 | - return BigInt::fromBase256(Base64::urlDecode($this->value())); |
|
37 | - } |
|
29 | + /** |
|
30 | + * Get value as a number. |
|
31 | + * |
|
32 | + * @return BigInt |
|
33 | + */ |
|
34 | + public function number() |
|
35 | + { |
|
36 | + return BigInt::fromBase256(Base64::urlDecode($this->value())); |
|
37 | + } |
|
38 | 38 | } |
@@ -15,219 +15,219 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class JWA |
17 | 17 | { |
18 | - /** |
|
19 | - * HMAC using SHA-256. |
|
20 | - */ |
|
21 | - const ALGO_HS256 = "HS256"; |
|
22 | - |
|
23 | - /** |
|
24 | - * HMAC using SHA-384. |
|
25 | - */ |
|
26 | - const ALGO_HS384 = "HS384"; |
|
27 | - |
|
28 | - /** |
|
29 | - * HMAC using SHA-512. |
|
30 | - */ |
|
31 | - const ALGO_HS512 = "HS512"; |
|
32 | - |
|
33 | - /** |
|
34 | - * RSASSA-PKCS1-v1_5 using SHA-256. |
|
35 | - */ |
|
36 | - const ALGO_RS256 = "RS256"; |
|
37 | - |
|
38 | - /** |
|
39 | - * RSASSA-PKCS1-v1_5 using SHA-384. |
|
40 | - */ |
|
41 | - const ALGO_RS384 = "RS384"; |
|
42 | - |
|
43 | - /** |
|
44 | - * RSASSA-PKCS1-v1_5 using SHA-512. |
|
45 | - */ |
|
46 | - const ALGO_RS512 = "RS512"; |
|
47 | - |
|
48 | - /** |
|
49 | - * ECDSA using P-256 and SHA-256. |
|
50 | - */ |
|
51 | - const ALGO_ES256 = "ES256"; |
|
52 | - |
|
53 | - /** |
|
54 | - * ECDSA using P-384 and SHA-384. |
|
55 | - */ |
|
56 | - const ALGO_ES384 = "ES384"; |
|
57 | - |
|
58 | - /** |
|
59 | - * ECDSA using P-521 and SHA-512. |
|
60 | - */ |
|
61 | - const ALGO_ES512 = "ES512"; |
|
62 | - |
|
63 | - /** |
|
64 | - * RSASSA-PSS using SHA-256 and MGF1 with SHA-256. |
|
65 | - */ |
|
66 | - const ALGO_PS256 = "PS256"; |
|
67 | - |
|
68 | - /** |
|
69 | - * RSASSA-PSS using SHA-384 and MGF1 with SHA-384. |
|
70 | - */ |
|
71 | - const ALGO_PS384 = "PS384"; |
|
72 | - |
|
73 | - /** |
|
74 | - * RSASSA-PSS using SHA-512 and MGF1 with SHA-512. |
|
75 | - */ |
|
76 | - const ALGO_PS512 = "PS512"; |
|
77 | - |
|
78 | - /** |
|
79 | - * No digital signature or MAC performed. |
|
80 | - */ |
|
81 | - const ALGO_NONE = "none"; |
|
82 | - |
|
83 | - /** |
|
84 | - * RSAES-PKCS1-v1_5. |
|
85 | - */ |
|
86 | - const ALGO_RSA1_5 = "RSA1_5"; |
|
87 | - |
|
88 | - /** |
|
89 | - * RSAES OAEP using default parameters. |
|
90 | - */ |
|
91 | - const ALGO_RSA_OAEP = "RSA-OAEP"; |
|
92 | - |
|
93 | - /** |
|
94 | - * RSAES OAEP using SHA-256 and MGF1 with SHA-256. |
|
95 | - */ |
|
96 | - const ALGO_RSA_OAEP256 = "RSA-OAEP-256"; |
|
97 | - |
|
98 | - /** |
|
99 | - * AES Key Wrap using 128-bit key. |
|
100 | - */ |
|
101 | - const ALGO_A128KW = "A128KW"; |
|
102 | - |
|
103 | - /** |
|
104 | - * AES Key Wrap using 192-bit key. |
|
105 | - */ |
|
106 | - const ALGO_A192KW = "A192KW"; |
|
107 | - |
|
108 | - /** |
|
109 | - * AES Key Wrap using 256-bit key. |
|
110 | - */ |
|
111 | - const ALGO_A256KW = "A256KW"; |
|
112 | - |
|
113 | - /** |
|
114 | - * Direct use of a shared symmetric key. |
|
115 | - */ |
|
116 | - const ALGO_DIR = "dir"; |
|
117 | - |
|
118 | - /** |
|
119 | - * ECDH-ES using Concat KDF. |
|
120 | - */ |
|
121 | - const ALGO_ECDH_ES = "ECDH-ES"; |
|
122 | - |
|
123 | - /** |
|
124 | - * ECDH-ES using Concat KDF and "A128KW" wrapping. |
|
125 | - */ |
|
126 | - const ALGO_ECDH_ES_A128KW = "ECDH-ES+A128KW"; |
|
127 | - |
|
128 | - /** |
|
129 | - * ECDH-ES using Concat KDF and "A192KW" wrapping. |
|
130 | - */ |
|
131 | - const ALGO_ECDH_ES_A192KW = "ECDH-ES+A192KW"; |
|
132 | - |
|
133 | - /** |
|
134 | - * ECDH-ES using Concat KDF and "A256KW" wrapping. |
|
135 | - */ |
|
136 | - const ALGO_ECDH_ES_A256KW = "ECDH-ES+A256KW"; |
|
137 | - |
|
138 | - /** |
|
139 | - * Key wrapping with AES GCM using 128-bit key. |
|
140 | - */ |
|
141 | - const ALGO_A128GCMKW = "A128GCMKW"; |
|
142 | - |
|
143 | - /** |
|
144 | - * Key wrapping with AES GCM using 192-bit key. |
|
145 | - */ |
|
146 | - const ALGO_A192GCMKW = "A192GCMKW"; |
|
147 | - |
|
148 | - /** |
|
149 | - * Key wrapping with AES GCM using 256-bit key. |
|
150 | - */ |
|
151 | - const ALGO_A256GCMKW = "A256GCMKW"; |
|
152 | - |
|
153 | - /** |
|
154 | - * PBES2 with HMAC SHA-256 and "A128KW" wrapping. |
|
155 | - */ |
|
156 | - const ALGO_PBES2_HS256_A128KW = "PBES2-HS256+A128KW"; |
|
157 | - |
|
158 | - /** |
|
159 | - * PBES2 with HMAC SHA-384 and "A192KW" wrapping. |
|
160 | - */ |
|
161 | - const ALGO_PBES2_HS384_A192KW = "PBES2-HS384+A192KW"; |
|
162 | - |
|
163 | - /** |
|
164 | - * PBES2 with HMAC SHA-512 and "A256KW" wrapping. |
|
165 | - */ |
|
166 | - const ALGO_PBES2_HS512_A256KW = "PBES2-HS512+A256KW"; |
|
167 | - |
|
168 | - /** |
|
169 | - * AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm. |
|
170 | - */ |
|
171 | - const ALGO_A128CBC_HS256 = "A128CBC-HS256"; |
|
172 | - |
|
173 | - /** |
|
174 | - * AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm. |
|
175 | - */ |
|
176 | - const ALGO_A192CBC_HS384 = "A192CBC-HS384"; |
|
177 | - |
|
178 | - /** |
|
179 | - * AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm. |
|
180 | - */ |
|
181 | - const ALGO_A256CBC_HS512 = "A256CBC-HS512"; |
|
182 | - |
|
183 | - /** |
|
184 | - * AES GCM using 128-bit key. |
|
185 | - */ |
|
186 | - const ALGO_A128GCM = "A128GCM"; |
|
187 | - |
|
188 | - /** |
|
189 | - * AES GCM using 192-bit key. |
|
190 | - */ |
|
191 | - const ALGO_A192GCM = "A192GCM"; |
|
192 | - |
|
193 | - /** |
|
194 | - * AES GCM using 256-bit key. |
|
195 | - */ |
|
196 | - const ALGO_A256GCM = "A256GCM"; |
|
197 | - |
|
198 | - /** |
|
199 | - * DEFLATE compression. |
|
200 | - */ |
|
201 | - const ALGO_DEFLATE = "DEF"; |
|
202 | - |
|
203 | - /** |
|
204 | - * Derive algorithm name from the header and optionally from the given JWK. |
|
205 | - * |
|
206 | - * @param Header $header Header |
|
207 | - * @param JWK $jwk Optional JWK |
|
208 | - * @throws \UnexpectedValueException If algorithm parameter is not present |
|
209 | - * or header and JWK algorithms differ. |
|
210 | - * @return string Algorithm name |
|
211 | - */ |
|
212 | - public static function deriveAlgorithmName(Header $header, JWK $jwk = null) |
|
213 | - { |
|
214 | - if ($header->hasAlgorithm()) { |
|
215 | - $alg = $header->algorithm()->value(); |
|
216 | - } |
|
217 | - // if JWK is set, and has an algorithm parameter |
|
218 | - if (isset($jwk) && $jwk->hasAlgorithmParameter()) { |
|
219 | - $jwk_alg = $jwk->algorithmParameter()->value(); |
|
220 | - // check that algorithms match |
|
221 | - if (isset($alg) && $alg != $jwk_alg) { |
|
222 | - throw new \UnexpectedValueException( |
|
223 | - "JWK algorithm '$jwk_alg' doesn't match" . |
|
224 | - " the header's algorithm '$alg'."); |
|
225 | - } |
|
226 | - $alg = $jwk_alg; |
|
227 | - } |
|
228 | - if (!isset($alg)) { |
|
229 | - throw new \UnexpectedValueException("No algorithm parameter."); |
|
230 | - } |
|
231 | - return $alg; |
|
232 | - } |
|
18 | + /** |
|
19 | + * HMAC using SHA-256. |
|
20 | + */ |
|
21 | + const ALGO_HS256 = "HS256"; |
|
22 | + |
|
23 | + /** |
|
24 | + * HMAC using SHA-384. |
|
25 | + */ |
|
26 | + const ALGO_HS384 = "HS384"; |
|
27 | + |
|
28 | + /** |
|
29 | + * HMAC using SHA-512. |
|
30 | + */ |
|
31 | + const ALGO_HS512 = "HS512"; |
|
32 | + |
|
33 | + /** |
|
34 | + * RSASSA-PKCS1-v1_5 using SHA-256. |
|
35 | + */ |
|
36 | + const ALGO_RS256 = "RS256"; |
|
37 | + |
|
38 | + /** |
|
39 | + * RSASSA-PKCS1-v1_5 using SHA-384. |
|
40 | + */ |
|
41 | + const ALGO_RS384 = "RS384"; |
|
42 | + |
|
43 | + /** |
|
44 | + * RSASSA-PKCS1-v1_5 using SHA-512. |
|
45 | + */ |
|
46 | + const ALGO_RS512 = "RS512"; |
|
47 | + |
|
48 | + /** |
|
49 | + * ECDSA using P-256 and SHA-256. |
|
50 | + */ |
|
51 | + const ALGO_ES256 = "ES256"; |
|
52 | + |
|
53 | + /** |
|
54 | + * ECDSA using P-384 and SHA-384. |
|
55 | + */ |
|
56 | + const ALGO_ES384 = "ES384"; |
|
57 | + |
|
58 | + /** |
|
59 | + * ECDSA using P-521 and SHA-512. |
|
60 | + */ |
|
61 | + const ALGO_ES512 = "ES512"; |
|
62 | + |
|
63 | + /** |
|
64 | + * RSASSA-PSS using SHA-256 and MGF1 with SHA-256. |
|
65 | + */ |
|
66 | + const ALGO_PS256 = "PS256"; |
|
67 | + |
|
68 | + /** |
|
69 | + * RSASSA-PSS using SHA-384 and MGF1 with SHA-384. |
|
70 | + */ |
|
71 | + const ALGO_PS384 = "PS384"; |
|
72 | + |
|
73 | + /** |
|
74 | + * RSASSA-PSS using SHA-512 and MGF1 with SHA-512. |
|
75 | + */ |
|
76 | + const ALGO_PS512 = "PS512"; |
|
77 | + |
|
78 | + /** |
|
79 | + * No digital signature or MAC performed. |
|
80 | + */ |
|
81 | + const ALGO_NONE = "none"; |
|
82 | + |
|
83 | + /** |
|
84 | + * RSAES-PKCS1-v1_5. |
|
85 | + */ |
|
86 | + const ALGO_RSA1_5 = "RSA1_5"; |
|
87 | + |
|
88 | + /** |
|
89 | + * RSAES OAEP using default parameters. |
|
90 | + */ |
|
91 | + const ALGO_RSA_OAEP = "RSA-OAEP"; |
|
92 | + |
|
93 | + /** |
|
94 | + * RSAES OAEP using SHA-256 and MGF1 with SHA-256. |
|
95 | + */ |
|
96 | + const ALGO_RSA_OAEP256 = "RSA-OAEP-256"; |
|
97 | + |
|
98 | + /** |
|
99 | + * AES Key Wrap using 128-bit key. |
|
100 | + */ |
|
101 | + const ALGO_A128KW = "A128KW"; |
|
102 | + |
|
103 | + /** |
|
104 | + * AES Key Wrap using 192-bit key. |
|
105 | + */ |
|
106 | + const ALGO_A192KW = "A192KW"; |
|
107 | + |
|
108 | + /** |
|
109 | + * AES Key Wrap using 256-bit key. |
|
110 | + */ |
|
111 | + const ALGO_A256KW = "A256KW"; |
|
112 | + |
|
113 | + /** |
|
114 | + * Direct use of a shared symmetric key. |
|
115 | + */ |
|
116 | + const ALGO_DIR = "dir"; |
|
117 | + |
|
118 | + /** |
|
119 | + * ECDH-ES using Concat KDF. |
|
120 | + */ |
|
121 | + const ALGO_ECDH_ES = "ECDH-ES"; |
|
122 | + |
|
123 | + /** |
|
124 | + * ECDH-ES using Concat KDF and "A128KW" wrapping. |
|
125 | + */ |
|
126 | + const ALGO_ECDH_ES_A128KW = "ECDH-ES+A128KW"; |
|
127 | + |
|
128 | + /** |
|
129 | + * ECDH-ES using Concat KDF and "A192KW" wrapping. |
|
130 | + */ |
|
131 | + const ALGO_ECDH_ES_A192KW = "ECDH-ES+A192KW"; |
|
132 | + |
|
133 | + /** |
|
134 | + * ECDH-ES using Concat KDF and "A256KW" wrapping. |
|
135 | + */ |
|
136 | + const ALGO_ECDH_ES_A256KW = "ECDH-ES+A256KW"; |
|
137 | + |
|
138 | + /** |
|
139 | + * Key wrapping with AES GCM using 128-bit key. |
|
140 | + */ |
|
141 | + const ALGO_A128GCMKW = "A128GCMKW"; |
|
142 | + |
|
143 | + /** |
|
144 | + * Key wrapping with AES GCM using 192-bit key. |
|
145 | + */ |
|
146 | + const ALGO_A192GCMKW = "A192GCMKW"; |
|
147 | + |
|
148 | + /** |
|
149 | + * Key wrapping with AES GCM using 256-bit key. |
|
150 | + */ |
|
151 | + const ALGO_A256GCMKW = "A256GCMKW"; |
|
152 | + |
|
153 | + /** |
|
154 | + * PBES2 with HMAC SHA-256 and "A128KW" wrapping. |
|
155 | + */ |
|
156 | + const ALGO_PBES2_HS256_A128KW = "PBES2-HS256+A128KW"; |
|
157 | + |
|
158 | + /** |
|
159 | + * PBES2 with HMAC SHA-384 and "A192KW" wrapping. |
|
160 | + */ |
|
161 | + const ALGO_PBES2_HS384_A192KW = "PBES2-HS384+A192KW"; |
|
162 | + |
|
163 | + /** |
|
164 | + * PBES2 with HMAC SHA-512 and "A256KW" wrapping. |
|
165 | + */ |
|
166 | + const ALGO_PBES2_HS512_A256KW = "PBES2-HS512+A256KW"; |
|
167 | + |
|
168 | + /** |
|
169 | + * AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm. |
|
170 | + */ |
|
171 | + const ALGO_A128CBC_HS256 = "A128CBC-HS256"; |
|
172 | + |
|
173 | + /** |
|
174 | + * AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm. |
|
175 | + */ |
|
176 | + const ALGO_A192CBC_HS384 = "A192CBC-HS384"; |
|
177 | + |
|
178 | + /** |
|
179 | + * AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm. |
|
180 | + */ |
|
181 | + const ALGO_A256CBC_HS512 = "A256CBC-HS512"; |
|
182 | + |
|
183 | + /** |
|
184 | + * AES GCM using 128-bit key. |
|
185 | + */ |
|
186 | + const ALGO_A128GCM = "A128GCM"; |
|
187 | + |
|
188 | + /** |
|
189 | + * AES GCM using 192-bit key. |
|
190 | + */ |
|
191 | + const ALGO_A192GCM = "A192GCM"; |
|
192 | + |
|
193 | + /** |
|
194 | + * AES GCM using 256-bit key. |
|
195 | + */ |
|
196 | + const ALGO_A256GCM = "A256GCM"; |
|
197 | + |
|
198 | + /** |
|
199 | + * DEFLATE compression. |
|
200 | + */ |
|
201 | + const ALGO_DEFLATE = "DEF"; |
|
202 | + |
|
203 | + /** |
|
204 | + * Derive algorithm name from the header and optionally from the given JWK. |
|
205 | + * |
|
206 | + * @param Header $header Header |
|
207 | + * @param JWK $jwk Optional JWK |
|
208 | + * @throws \UnexpectedValueException If algorithm parameter is not present |
|
209 | + * or header and JWK algorithms differ. |
|
210 | + * @return string Algorithm name |
|
211 | + */ |
|
212 | + public static function deriveAlgorithmName(Header $header, JWK $jwk = null) |
|
213 | + { |
|
214 | + if ($header->hasAlgorithm()) { |
|
215 | + $alg = $header->algorithm()->value(); |
|
216 | + } |
|
217 | + // if JWK is set, and has an algorithm parameter |
|
218 | + if (isset($jwk) && $jwk->hasAlgorithmParameter()) { |
|
219 | + $jwk_alg = $jwk->algorithmParameter()->value(); |
|
220 | + // check that algorithms match |
|
221 | + if (isset($alg) && $alg != $jwk_alg) { |
|
222 | + throw new \UnexpectedValueException( |
|
223 | + "JWK algorithm '$jwk_alg' doesn't match" . |
|
224 | + " the header's algorithm '$alg'."); |
|
225 | + } |
|
226 | + $alg = $jwk_alg; |
|
227 | + } |
|
228 | + if (!isset($alg)) { |
|
229 | + throw new \UnexpectedValueException("No algorithm parameter."); |
|
230 | + } |
|
231 | + return $alg; |
|
232 | + } |
|
233 | 233 | } |