Skip to content

Commit e1b4df7

Browse files
tsitkovjricher
authored andcommitted
Added tests for DefaultJwtEncryptionAndDecryptionService
1 parent 5223d88 commit e1b4df7

File tree

1 file changed

+222
-10
lines changed

1 file changed

+222
-10
lines changed

openid-connect-common/src/test/java/org/mitre/jwt/encryption/service/impl/TestDefaultJwtEncryptionAndDecryptionService.java

Lines changed: 222 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
import static org.hamcrest.CoreMatchers.nullValue;
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertThat;
22+
import static org.junit.Assert.assertTrue;
2223

2324
import java.security.NoSuchAlgorithmException;
2425
import java.security.spec.InvalidKeySpecException;
2526
import java.text.ParseException;
27+
import java.util.LinkedList;
28+
import java.util.List;
2629
import java.util.Map;
2730

2831
import org.junit.Before;
2932
import org.junit.Test;
33+
import org.mitre.jose.keystore.JWKSetKeyStore;
3034

3135
import com.google.common.collect.ImmutableMap;
3236
import com.nimbusds.jose.EncryptionMethod;
@@ -35,17 +39,23 @@
3539
import com.nimbusds.jose.JWEHeader;
3640
import com.nimbusds.jose.JWEObject;
3741
import com.nimbusds.jose.jwk.JWK;
38-
import com.nimbusds.jose.jwk.RSAKey;
42+
import com.nimbusds.jose.jwk.JWKSet;
3943
import com.nimbusds.jose.jwk.KeyUse;
44+
import com.nimbusds.jose.jwk.OctetSequenceKey;
45+
import com.nimbusds.jose.jwk.RSAKey;
4046
import com.nimbusds.jose.util.Base64URL;
47+
import com.nimbusds.jose.util.JSONObjectUtils;
4148
import com.nimbusds.jwt.EncryptedJWT;
4249
import com.nimbusds.jwt.JWTClaimsSet;
4350
import com.nimbusds.jwt.ReadOnlyJWTClaimsSet;
4451

52+
4553
/**
4654
* @author wkim
55+
* @author tsitkov
4756
*
4857
*/
58+
4959
public class TestDefaultJwtEncryptionAndDecryptionService {
5060

5161
private String plainText = "The true sign of intelligence is not knowledge but imagination.";
@@ -83,30 +93,80 @@ public class TestDefaultJwtEncryptionAndDecryptionService {
8393
"VTIznSxfyrj8ILL6MG_Uv8YAu7VILSB3lOW085-4qE3DzgrTjgyQ"), // d
8494
KeyUse.ENCRYPTION, null, JWEAlgorithm.RSA_OAEP, RSAkid, null, null, null);
8595

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+
92135

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>();
95137

96138
private DefaultJwtEncryptionAndDecryptionService service;
139+
private DefaultJwtEncryptionAndDecryptionService service_2;
140+
private DefaultJwtEncryptionAndDecryptionService service_3;
141+
private DefaultJwtEncryptionAndDecryptionService service_4;
142+
private DefaultJwtEncryptionAndDecryptionService service_ks;
143+
97144

98145
@Before
99146
public void prepare() throws NoSuchAlgorithmException, InvalidKeySpecException, JOSEException {
100147

101148
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);
102152

103153
claimsSet.setIssuer(issuer);
104154
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);
105164
}
106165

166+
107167
@Test
108168
public void decrypt_RSA() throws ParseException {
109-
169+
110170
service.setDefaultDecryptionKeyId(RSAkid);
111171
service.setDefaultEncryptionKeyId(RSAkid);
112172

@@ -120,12 +180,44 @@ public void decrypt_RSA() throws ParseException {
120180
assertEquals(plainText, result);
121181
}
122182

183+
123184
@Test
124185
public void encryptThenDecrypt_RSA() throws ParseException {
125186

126187
service.setDefaultDecryptionKeyId(RSAkid);
127188
service.setDefaultEncryptionKeyId(RSAkid);
128189

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+
129221
JWEHeader header = new JWEHeader(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A256GCM);
130222

131223
EncryptedJWT jwt = new EncryptedJWT(header, claimsSet);
@@ -143,4 +235,124 @@ public void encryptThenDecrypt_RSA() throws ParseException {
143235
assertEquals(claimsSet.getSubject(), resultClaims.getSubject());
144236
}
145237

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+
}
146358
}

0 commit comments

Comments
 (0)