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