19
19
import static org .hamcrest .CoreMatchers .nullValue ;
20
20
import static org .junit .Assert .assertEquals ;
21
21
import static org .junit .Assert .assertThat ;
22
+ import static org .junit .Assert .assertTrue ;
22
23
23
24
import java .security .NoSuchAlgorithmException ;
24
25
import java .security .spec .InvalidKeySpecException ;
25
26
import java .text .ParseException ;
27
+ import java .util .LinkedList ;
28
+ import java .util .List ;
26
29
import java .util .Map ;
27
30
28
31
import org .junit .Before ;
29
32
import org .junit .Test ;
33
+ import org .mitre .jose .keystore .JWKSetKeyStore ;
30
34
31
35
import com .google .common .collect .ImmutableMap ;
32
36
import com .nimbusds .jose .EncryptionMethod ;
35
39
import com .nimbusds .jose .JWEHeader ;
36
40
import com .nimbusds .jose .JWEObject ;
37
41
import com .nimbusds .jose .jwk .JWK ;
38
- import com .nimbusds .jose .jwk .RSAKey ;
42
+ import com .nimbusds .jose .jwk .JWKSet ;
39
43
import com .nimbusds .jose .jwk .KeyUse ;
44
+ import com .nimbusds .jose .jwk .OctetSequenceKey ;
45
+ import com .nimbusds .jose .jwk .RSAKey ;
40
46
import com .nimbusds .jose .util .Base64URL ;
47
+ import com .nimbusds .jose .util .JSONObjectUtils ;
41
48
import com .nimbusds .jwt .EncryptedJWT ;
42
49
import com .nimbusds .jwt .JWTClaimsSet ;
43
50
import com .nimbusds .jwt .ReadOnlyJWTClaimsSet ;
44
51
52
+
45
53
/**
46
54
* @author wkim
55
+ * @author tsitkov
47
56
*
48
57
*/
58
+
49
59
public class TestDefaultJwtEncryptionAndDecryptionService {
50
60
51
61
private String plainText = "The true sign of intelligence is not knowledge but imagination." ;
@@ -83,30 +93,80 @@ public class TestDefaultJwtEncryptionAndDecryptionService {
83
93
"VTIznSxfyrj8ILL6MG_Uv8YAu7VILSB3lOW085-4qE3DzgrTjgyQ" ), // d
84
94
KeyUse .ENCRYPTION , null , JWEAlgorithm .RSA_OAEP , RSAkid , null , null , null );
85
95
86
- // AES key wrap not yet tested
87
- // private String AESkid = "aes123";
88
- // private JWK AESjwk = new OctetSequenceKey(new Base64URL("GawgguFyGrWKav7AX4VKUg"), Use.ENCRYPTION, JWEAlgorithm.A128KW, AESkid);
89
- //
90
- // private Map<String, JWK> keys = new ImmutableMap.Builder<String, JWK>().
91
- // put(RSAkid, RSAjwk).put(AESkid, AESjwk).build();
96
+ private String RSAkid_2 = "rsa3210" ;
97
+ private JWK RSAjwk_2 = new RSAKey (
98
+ new Base64URL ("oahUIoWw0K0usKNuOR6H4wkf4oBUXHTxRvgb48E-BVvxkeDNjbC4he8rUW" +
99
+ "cJoZmds2h7M70imEVhRU5djINXtqllXI4DFqcI1DgjT9LewND8MW2Krf3S" +
100
+ "psk_ZkoFnilakGygTwpZ3uesH-PFABNIUYpOiN15dsQRkgr0vEhxN92i2a" +
101
+ "sbOenSZeyaxziK72UwxrrKoExv6kc5twXTq4h-QChLOln0_mtUZwfsRaMS" +
102
+ "tPs6mS6XrgxnxbWhojf663tuEQueGC-FCMfra36C9knDFGzKsNa7LZK2dj" +
103
+ "YgyD3JR_MB_4NUJW_TqOQtwHYbxevoJArm-L5StowjzGy-_bq6Gw" ), // n
104
+ new Base64URL ("AQAB" ), // e
105
+ new Base64URL ("kLdtIj6GbDks_ApCSTYQtelcNttlKiOyPzMrXHeI-yk1F7-kpDxY4-WY5N" +
106
+ "WV5KntaEeXS1j82E375xxhWMHXyvjYecPT9fpwR_M9gV8n9Hrh2anTpTD9" +
107
+ "3Dt62ypW3yDsJzBnTnrYu1iwWRgBKrEYY46qAZIrA2xAwnm2X7uGR1hghk" +
108
+ "qDp0Vqj3kbSCz1XyfCs6_LehBwtxHIyh8Ripy40p24moOAbgxVw3rxT_vl" +
109
+ "t3UVe4WO3JkJOzlpUf-KTVI2Ptgm-dARxTEtE-id-4OJr0h-K-VFs3VSnd" +
110
+ "VTIznSxfyrj8ILL6MG_Uv8YAu7VILSB3lOW085-4qE3DzgrTjgyQ" ), // d
111
+ KeyUse .ENCRYPTION , null , JWEAlgorithm .RSA1_5 , RSAkid_2 , null , null , null );
112
+
113
+ private String AESkid = "aes123" ;
114
+ private JWK AESjwk = new OctetSequenceKey ( new Base64URL ("GawgguFyGrWKav7AX4VKUg" ),
115
+ KeyUse .ENCRYPTION , null , JWEAlgorithm .A128KW ,
116
+ AESkid , null , null , null );
117
+
118
+
119
+ private Map <String , JWK > keys = new ImmutableMap .Builder <String , JWK >()
120
+ .put (RSAkid , RSAjwk )
121
+ .build ();
122
+ private Map <String , JWK > keys_2 = new ImmutableMap .Builder <String , JWK >()
123
+ .put (RSAkid , RSAjwk )
124
+ .put (RSAkid_2 , RSAjwk_2 )
125
+ .build ();
126
+ private Map <String , JWK > keys_3 = new ImmutableMap .Builder <String , JWK >()
127
+ .put (AESkid , AESjwk )
128
+ .build ();
129
+ private Map <String , JWK > keys_4 = new ImmutableMap .Builder <String , JWK >()
130
+ .put (RSAkid , RSAjwk )
131
+ .put (RSAkid_2 , RSAjwk_2 )
132
+ .put (AESkid , AESjwk )
133
+ .build ();
134
+
92
135
93
- private Map <String , JWK > keys = new ImmutableMap .Builder <String , JWK >().
94
- put (RSAkid , RSAjwk ).build ();
136
+ private List <JWK > keys_list = new LinkedList <JWK >();
95
137
96
138
private DefaultJwtEncryptionAndDecryptionService service ;
139
+ private DefaultJwtEncryptionAndDecryptionService service_2 ;
140
+ private DefaultJwtEncryptionAndDecryptionService service_3 ;
141
+ private DefaultJwtEncryptionAndDecryptionService service_4 ;
142
+ private DefaultJwtEncryptionAndDecryptionService service_ks ;
143
+
97
144
98
145
@ Before
99
146
public void prepare () throws NoSuchAlgorithmException , InvalidKeySpecException , JOSEException {
100
147
101
148
service = new DefaultJwtEncryptionAndDecryptionService (keys );
149
+ service_2 = new DefaultJwtEncryptionAndDecryptionService (keys_2 );
150
+ service_3 = new DefaultJwtEncryptionAndDecryptionService (keys_3 );
151
+ service_4 = new DefaultJwtEncryptionAndDecryptionService (keys_4 );
102
152
103
153
claimsSet .setIssuer (issuer );
104
154
claimsSet .setSubject (subject );
155
+
156
+ // Key Store
157
+
158
+ keys_list .add (RSAjwk );
159
+ keys_list .add (AESjwk );
160
+ JWKSet jwkSet = new JWKSet (keys_list );
161
+ JWKSetKeyStore keyStore = new JWKSetKeyStore (jwkSet );
162
+
163
+ service_ks = new DefaultJwtEncryptionAndDecryptionService (keyStore );
105
164
}
106
165
166
+
107
167
@ Test
108
168
public void decrypt_RSA () throws ParseException {
109
-
169
+
110
170
service .setDefaultDecryptionKeyId (RSAkid );
111
171
service .setDefaultEncryptionKeyId (RSAkid );
112
172
@@ -120,12 +180,44 @@ public void decrypt_RSA() throws ParseException {
120
180
assertEquals (plainText , result );
121
181
}
122
182
183
+
123
184
@ Test
124
185
public void encryptThenDecrypt_RSA () throws ParseException {
125
186
126
187
service .setDefaultDecryptionKeyId (RSAkid );
127
188
service .setDefaultEncryptionKeyId (RSAkid );
128
189
190
+ assertEquals (RSAkid ,service .getDefaultEncryptionKeyId ());
191
+ assertEquals (RSAkid ,service .getDefaultDecryptionKeyId ());
192
+
193
+ JWEHeader header = new JWEHeader (JWEAlgorithm .RSA_OAEP , EncryptionMethod .A256GCM );
194
+
195
+ EncryptedJWT jwt = new EncryptedJWT (header , claimsSet );
196
+
197
+ service .encryptJwt (jwt );
198
+ String serialized = jwt .serialize ();
199
+
200
+ EncryptedJWT encryptedJwt = EncryptedJWT .parse (serialized );
201
+ assertThat (encryptedJwt .getJWTClaimsSet (), nullValue ());
202
+ service .decryptJwt (encryptedJwt );
203
+
204
+ ReadOnlyJWTClaimsSet resultClaims = encryptedJwt .getJWTClaimsSet ();
205
+
206
+ assertEquals (claimsSet .getIssuer (), resultClaims .getIssuer ());
207
+ assertEquals (claimsSet .getSubject (), resultClaims .getSubject ());
208
+ }
209
+
210
+
211
+ // The same as encryptThenDecrypt_RSA() but relies on the key from the map
212
+ @ Test
213
+ public void encryptThenDecrypt_nullID () throws ParseException {
214
+
215
+ service .setDefaultDecryptionKeyId (null );
216
+ service .setDefaultEncryptionKeyId (null );
217
+
218
+ assertEquals (RSAkid ,service .getDefaultEncryptionKeyId ());
219
+ assertEquals (RSAkid ,service .getDefaultDecryptionKeyId ());
220
+
129
221
JWEHeader header = new JWEHeader (JWEAlgorithm .RSA_OAEP , EncryptionMethod .A256GCM );
130
222
131
223
EncryptedJWT jwt = new EncryptedJWT (header , claimsSet );
@@ -143,4 +235,124 @@ public void encryptThenDecrypt_RSA() throws ParseException {
143
235
assertEquals (claimsSet .getSubject (), resultClaims .getSubject ());
144
236
}
145
237
238
+
239
+ @ Test (expected =IllegalStateException .class )
240
+ public void encrypt_nullID_oneKey () {
241
+
242
+ service_2 .setDefaultEncryptionKeyId (null );
243
+ assertEquals (null , service_2 .getDefaultEncryptionKeyId ());
244
+
245
+ JWEHeader header = new JWEHeader (JWEAlgorithm .RSA_OAEP , EncryptionMethod .A256GCM );
246
+
247
+ EncryptedJWT jwt = new EncryptedJWT (header , claimsSet );
248
+
249
+ service_2 .encryptJwt (jwt );
250
+ assertEquals (null , service_2 .getDefaultEncryptionKeyId ());
251
+ }
252
+
253
+
254
+ @ Test (expected =IllegalStateException .class )
255
+ public void decrypt_nullID () throws ParseException {
256
+
257
+ service_2 .setDefaultEncryptionKeyId (RSAkid );
258
+ service_2 .setDefaultDecryptionKeyId (null );
259
+
260
+ assertEquals (RSAkid , service_2 .getDefaultEncryptionKeyId ());
261
+ assertEquals (null , service_2 .getDefaultDecryptionKeyId ());
262
+
263
+ JWEHeader header = new JWEHeader (JWEAlgorithm .RSA_OAEP , EncryptionMethod .A256GCM );
264
+
265
+ EncryptedJWT jwt = new EncryptedJWT (header , claimsSet );
266
+ service_2 .encryptJwt (jwt );
267
+ String serialized = jwt .serialize ();
268
+
269
+ EncryptedJWT encryptedJwt = EncryptedJWT .parse (serialized );
270
+ assertThat (encryptedJwt .getJWTClaimsSet (), nullValue ());
271
+
272
+ assertEquals (null , service_2 .getDefaultDecryptionKeyId ());
273
+ service_2 .decryptJwt (encryptedJwt );
274
+ }
275
+
276
+
277
+ @ Test
278
+ public void setThenGetDefAlg () throws ParseException {
279
+
280
+ service .setDefaultAlgorithm (JWEAlgorithm .A128KW );
281
+ assertEquals (JWEAlgorithm .A128KW , service .getDefaultAlgorithm ());
282
+
283
+ service .setDefaultAlgorithm (JWEAlgorithm .RSA_OAEP );
284
+ assertEquals (JWEAlgorithm .RSA_OAEP , service .getDefaultAlgorithm ());
285
+ }
286
+
287
+
288
+ @ Test
289
+ public void runAfterPropertiesSet () throws NoSuchAlgorithmException ,
290
+ InvalidKeySpecException , JOSEException {
291
+
292
+ // TODO : Need more meaningful test here
293
+ service .afterPropertiesSet ();
294
+ }
295
+
296
+
297
+ @ Test
298
+ public void getAllPubKeys () throws ParseException {
299
+
300
+ Map <String ,JWK > keys2check = service_2 .getAllPublicKeys ();
301
+ assertEquals (
302
+ JSONObjectUtils .getString (RSAjwk .toPublicJWK ().toJSONObject (), "e" ),
303
+ JSONObjectUtils .getString (keys2check .get (RSAkid ).toJSONObject (), "e" )
304
+ );
305
+ assertEquals (
306
+ JSONObjectUtils .getString (RSAjwk_2 .toPublicJWK ().toJSONObject (), "e" ),
307
+ JSONObjectUtils .getString (keys2check .get (RSAkid_2 ).toJSONObject (), "e" )
308
+ );
309
+
310
+ assertTrue (service_3 .getAllPublicKeys ().isEmpty ());
311
+ }
312
+
313
+
314
+ @ Test
315
+ public void getAllCryptoAlgsSupported () throws ParseException {
316
+
317
+ assertTrue (service_4 .getAllEncryptionAlgsSupported ().contains (JWEAlgorithm .RSA_OAEP ));
318
+ assertTrue (service_4 .getAllEncryptionAlgsSupported ().contains (JWEAlgorithm .RSA1_5 ));
319
+ assertTrue (service_4 .getAllEncryptionAlgsSupported ().contains (JWEAlgorithm .DIR ));
320
+ assertTrue (service_4 .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A128CBC_HS256 ));
321
+ assertTrue (service_4 .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A128GCM ));
322
+ assertTrue (service_4 .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A192CBC_HS384 ));
323
+ assertTrue (service_4 .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A192GCM ));
324
+ assertTrue (service_4 .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A256GCM ));
325
+ assertTrue (service_4 .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A256CBC_HS512 ));
326
+
327
+ assertTrue (service_ks .getAllEncryptionAlgsSupported ().contains (JWEAlgorithm .RSA_OAEP ));
328
+ assertTrue (service_ks .getAllEncryptionAlgsSupported ().contains (JWEAlgorithm .RSA1_5 ));
329
+ assertTrue (service_ks .getAllEncryptionAlgsSupported ().contains (JWEAlgorithm .DIR ));
330
+ assertTrue (service_ks .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A128CBC_HS256 ));
331
+ assertTrue (service_ks .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A128GCM ));
332
+ assertTrue (service_ks .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A192CBC_HS384 ));
333
+ assertTrue (service_ks .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A192GCM ));
334
+ assertTrue (service_ks .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A256GCM ));
335
+ assertTrue (service_ks .getAllEncryptionEncsSupported ().contains (EncryptionMethod .A256CBC_HS512 ));
336
+ }
337
+
338
+
339
+ @ Test
340
+ public void getDefaultCryptoKeyId () throws ParseException {
341
+
342
+ // Test set/getDefaultEn/DecryptionKeyId
343
+
344
+ assertEquals (null , service_4 .getDefaultEncryptionKeyId ());
345
+ assertEquals (null , service_4 .getDefaultDecryptionKeyId ());
346
+ service_4 .setDefaultEncryptionKeyId (RSAkid );
347
+ service_4 .setDefaultDecryptionKeyId (AESkid );
348
+ assertEquals (RSAkid , service_4 .getDefaultEncryptionKeyId ());
349
+ assertEquals (AESkid , service_4 .getDefaultDecryptionKeyId ());
350
+
351
+ assertEquals (null , service_ks .getDefaultEncryptionKeyId ());
352
+ assertEquals (null , service_ks .getDefaultDecryptionKeyId ());
353
+ service_ks .setDefaultEncryptionKeyId (RSAkid );
354
+ service_ks .setDefaultDecryptionKeyId (AESkid );
355
+ assertEquals ( RSAkid , service_ks .getDefaultEncryptionKeyId ()) ;
356
+ assertEquals (AESkid , service_ks .getDefaultDecryptionKeyId ());
357
+ }
146
358
}
0 commit comments