23
23
import java .util .List ;
24
24
import java .util .Map ;
25
25
26
+ import javax .crypto .Cipher ;
27
+
28
+ import org .junit .Assume ;
26
29
import org .junit .Before ;
30
+ import org .junit .Rule ;
27
31
import org .junit .Test ;
32
+ import org .junit .rules .ExpectedException ;
28
33
import org .mitre .jose .keystore .JWKSetKeyStore ;
34
+ import org .slf4j .Logger ;
35
+ import org .slf4j .LoggerFactory ;
29
36
30
37
import com .google .common .collect .ImmutableMap ;
31
38
import com .nimbusds .jose .EncryptionMethod ;
32
39
import com .nimbusds .jose .JOSEException ;
33
40
import com .nimbusds .jose .JWEAlgorithm ;
34
41
import com .nimbusds .jose .JWEHeader ;
35
42
import com .nimbusds .jose .JWEObject ;
43
+ import com .nimbusds .jose .jca .JCASupport ;
36
44
import com .nimbusds .jose .jwk .JWK ;
37
45
import com .nimbusds .jose .jwk .JWKSet ;
38
46
import com .nimbusds .jose .jwk .KeyUse ;
57
65
*/
58
66
59
67
public class TestDefaultJWTEncryptionAndDecryptionService {
68
+
69
+ private static Logger logger = LoggerFactory .getLogger (TestDefaultJWTEncryptionAndDecryptionService .class );
60
70
61
71
private String plainText = "The true sign of intelligence is not knowledge but imagination." ;
62
72
63
73
private String issuer = "www.example.net" ;
64
74
private String subject = "example_user" ;
65
75
private JWTClaimsSet claimsSet = null ;
76
+
77
+ @ Rule
78
+ public ExpectedException exception = ExpectedException .none ();
66
79
67
- // Example data taken from Mike Jones's draft-ietf-jose-json-web-encryption-14 appendix examples
80
+ // Example data taken from rfc7516 appendix A
68
81
private String compactSerializedJwe = "eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkEyNTZHQ00ifQ." +
69
82
"OKOawDo13gRp2ojaHV7LFpZcgV7T6DVZKTyKOMTYUmKoTCVJRgckCL9kiMT03JGe" +
70
83
"ipsEdY3mx_etLbbWSrFr05kLzcSr4qKAq7YN7e9jwQRb23nfa6c9d-StnImGyFDb" +
@@ -167,9 +180,13 @@ public void prepare() throws NoSuchAlgorithmException, InvalidKeySpecException,
167
180
}
168
181
169
182
170
- // @Test
171
- public void decrypt_RSA () throws ParseException {
183
+ @ Test
184
+ public void decrypt_RSA () throws ParseException , NoSuchAlgorithmException {
172
185
186
+ Assume .assumeTrue (JCASupport .isSupported (JWEAlgorithm .RSA_OAEP ) // check for algorithm support
187
+ && JCASupport .isSupported (EncryptionMethod .A256GCM )
188
+ && Cipher .getMaxAllowedKeyLength ("RC5" ) >= 256 ); // check for unlimited crypto strength
189
+
173
190
service .setDefaultDecryptionKeyId (RSAkid );
174
191
service .setDefaultEncryptionKeyId (RSAkid );
175
192
@@ -184,9 +201,13 @@ public void decrypt_RSA() throws ParseException {
184
201
}
185
202
186
203
187
- // @Test
188
- public void encryptThenDecrypt_RSA () throws ParseException {
204
+ @ Test
205
+ public void encryptThenDecrypt_RSA () throws ParseException , NoSuchAlgorithmException {
189
206
207
+ Assume .assumeTrue (JCASupport .isSupported (JWEAlgorithm .RSA_OAEP ) // check for algorithm support
208
+ && JCASupport .isSupported (EncryptionMethod .A256GCM )
209
+ && Cipher .getMaxAllowedKeyLength ("RC5" ) >= 256 ); // check for unlimited crypto strength
210
+
190
211
service .setDefaultDecryptionKeyId (RSAkid );
191
212
service .setDefaultEncryptionKeyId (RSAkid );
192
213
@@ -212,9 +233,13 @@ public void encryptThenDecrypt_RSA() throws ParseException {
212
233
213
234
214
235
// The same as encryptThenDecrypt_RSA() but relies on the key from the map
215
- //@Test
216
- public void encryptThenDecrypt_nullID () throws ParseException {
217
-
236
+ @ Test
237
+ public void encryptThenDecrypt_nullID () throws ParseException , NoSuchAlgorithmException {
238
+
239
+ Assume .assumeTrue (JCASupport .isSupported (JWEAlgorithm .RSA_OAEP ) // check for algorithm support
240
+ && JCASupport .isSupported (EncryptionMethod .A256GCM )
241
+ && Cipher .getMaxAllowedKeyLength ("RC5" ) >= 256 ); // check for unlimited crypto strength
242
+
218
243
service .setDefaultDecryptionKeyId (null );
219
244
service .setDefaultEncryptionKeyId (null );
220
245
@@ -239,9 +264,15 @@ public void encryptThenDecrypt_nullID() throws ParseException {
239
264
}
240
265
241
266
242
- @ Test (expected =IllegalStateException .class )
243
- public void encrypt_nullID_oneKey () {
267
+ @ Test
268
+ public void encrypt_nullID_oneKey () throws NoSuchAlgorithmException {
269
+
270
+ Assume .assumeTrue (JCASupport .isSupported (JWEAlgorithm .RSA_OAEP ) // check for algorithm support
271
+ && JCASupport .isSupported (EncryptionMethod .A256GCM )
272
+ && Cipher .getMaxAllowedKeyLength ("RC5" ) >= 256 ); // check for unlimited crypto strength
244
273
274
+ exception .expect (IllegalStateException .class );
275
+
245
276
service_2 .setDefaultEncryptionKeyId (null );
246
277
assertEquals (null , service_2 .getDefaultEncryptionKeyId ());
247
278
@@ -254,9 +285,16 @@ public void encrypt_nullID_oneKey() {
254
285
}
255
286
256
287
257
- @ Test (expected =IllegalStateException .class )
258
- public void decrypt_nullID () throws ParseException {
288
+ @ Test
289
+ public void decrypt_nullID () throws ParseException , NoSuchAlgorithmException {
290
+
291
+ Assume .assumeTrue (JCASupport .isSupported (JWEAlgorithm .RSA_OAEP ) // check for algorithm support
292
+ && JCASupport .isSupported (EncryptionMethod .A256GCM )
293
+ && Cipher .getMaxAllowedKeyLength ("RC5" ) >= 256 ); // check for unlimited crypto strength
294
+
259
295
296
+ exception .expect (IllegalStateException .class );
297
+
260
298
service_2 .setDefaultEncryptionKeyId (RSAkid );
261
299
service_2 .setDefaultDecryptionKeyId (null );
262
300
0 commit comments