|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2014 The MITRE Corporation |
| 3 | + * and the MIT Kerberos and Internet Trust Consortium |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + ******************************************************************************/ |
| 17 | +package org.mitre.jose; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | + |
| 22 | +import org.springframework.core.io.Resource; |
| 23 | +import org.springframework.core.io.FileSystemResource; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.File; |
| 27 | +import java.io.FileOutputStream; |
| 28 | +import java.util.LinkedList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.After; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import com.nimbusds.jose.JWEAlgorithm; |
| 36 | +import com.nimbusds.jose.jwk.JWK; |
| 37 | +import com.nimbusds.jose.jwk.JWKSet; |
| 38 | +import com.nimbusds.jose.jwk.RSAKey; |
| 39 | +import com.nimbusds.jose.jwk.Use; |
| 40 | +import com.nimbusds.jose.util.Base64URL; |
| 41 | + |
| 42 | +import org.mitre.jose.keystore.JWKSetKeyStore; |
| 43 | + |
| 44 | + |
| 45 | +/** |
| 46 | + * @author tsitkov |
| 47 | + * |
| 48 | + */ |
| 49 | + |
| 50 | +public class TestJWKSetKeyStore { |
| 51 | + |
| 52 | +private String RSAkid = "rsa_1"; |
| 53 | +private JWK RSAjwk = new RSAKey( |
| 54 | +new Base64URL("oahUIoWw0K0usKNuOR6H4wkf4oBUXHTxRvgb48E-BVvxkeDNjbC4he8rUW" + |
| 55 | +"cJoZmds2h7M70imEVhRU5djINXtqllXI4DFqcI1DgjT9LewND8MW2Krf3S" + |
| 56 | +"psk_ZkoFnilakGygTwpZ3uesH-PFABNIUYpOiN15dsQRkgr0vEhxN92i2a" + |
| 57 | +"sbOenSZeyaxziK72UwxrrKoExv6kc5twXTq4h-QChLOln0_mtUZwfsRaMS" + |
| 58 | +"tPs6mS6XrgxnxbWhojf663tuEQueGC-FCMfra36C9knDFGzKsNa7LZK2dj" + |
| 59 | +"YgyD3JR_MB_4NUJW_TqOQtwHYbxevoJArm-L5StowjzGy-_bq6Gw"), // n |
| 60 | +new Base64URL("AQAB"), // e |
| 61 | +new Base64URL("kLdtIj6GbDks_ApCSTYQtelcNttlKiOyPzMrXHeI-yk1F7-kpDxY4-WY5N" + |
| 62 | +"WV5KntaEeXS1j82E375xxhWMHXyvjYecPT9fpwR_M9gV8n9Hrh2anTpTD9" + |
| 63 | +"3Dt62ypW3yDsJzBnTnrYu1iwWRgBKrEYY46qAZIrA2xAwnm2X7uGR1hghk" + |
| 64 | +"qDp0Vqj3kbSCz1XyfCs6_LehBwtxHIyh8Ripy40p24moOAbgxVw3rxT_vl" + |
| 65 | +"t3UVe4WO3JkJOzlpUf-KTVI2Ptgm-dARxTEtE-id-4OJr0h-K-VFs3VSnd" + |
| 66 | +"VTIznSxfyrj8ILL6MG_Uv8YAu7VILSB3lOW085-4qE3DzgrTjgyQ"), // d |
| 67 | +Use.ENCRYPTION, JWEAlgorithm.RSA_OAEP, RSAkid, null, null, null); |
| 68 | + |
| 69 | +private String RSAkid_rsa2 = "rsa_2"; |
| 70 | +private JWK RSAjwk_rsa2 = new RSAKey( |
| 71 | +new Base64URL("oahUIoWw0K0usKNuOR6H4wkf4oBUXHTxRvgb48E-BVvxkeDNjbC4he8rUW" + |
| 72 | +"cJoZmds2h7M70imEVhRU5djINXtqllXI4DFqcI1DgjT9LewND8MW2Krf3S" + |
| 73 | +"psk_ZkoFnilakGygTwpZ3uesH-PFABNIUYpOiN15dsQRkgr0vEhxN92i2a" + |
| 74 | +"sbOenSZeyaxziK72UwxrrKoExv6kc5twXTq4h-QChLOln0_mtUZwfsRaMS" + |
| 75 | +"tPs6mS6XrgxnxbWhojf663tuEQueGC-FCMfra36C9knDFGzKsNa7LZK2dj" + |
| 76 | +"YgyD3JR_MB_4NUJW_TqOQtwHYbxevoJArm-L5StowjzGy-_bq6Gw"), // n |
| 77 | +new Base64URL("AQAB"), // e |
| 78 | +new Base64URL("kLdtIj6GbDks_ApCSTYQtelcNttlKiOyPzMrXHeI-yk1F7-kpDxY4-WY5N" + |
| 79 | +"WV5KntaEeXS1j82E375xxhWMHXyvjYecPT9fpwR_M9gV8n9Hrh2anTpTD9" + |
| 80 | +"3Dt62ypW3yDsJzBnTnrYu1iwWRgBKrEYY46qAZIrA2xAwnm2X7uGR1hghk" + |
| 81 | +"qDp0Vqj3kbSCz1XyfCs6_LehBwtxHIyh8Ripy40p24moOAbgxVw3rxT_vl" + |
| 82 | +"t3UVe4WO3JkJOzlpUf-KTVI2Ptgm-dARxTEtE-id-4OJr0h-K-VFs3VSnd" + |
| 83 | +"VTIznSxfyrj8ILL6MG_Uv8YAu7VILSB3lOW085-4qE3DzgrTjgyQ"), // d |
| 84 | +Use.ENCRYPTION, JWEAlgorithm.RSA1_5, RSAkid_rsa2, null, null, null); |
| 85 | + |
| 86 | + |
| 87 | + List<JWK> keys_list = new LinkedList<JWK>(); |
| 88 | + private JWKSet jwkSet; |
| 89 | + private String ks_file = "ks.txt"; |
| 90 | + private String ks_file_badJWK = "ks_badJWK.txt"; |
| 91 | + |
| 92 | +@Before |
| 93 | +public void prepare() throws IOException { |
| 94 | + |
| 95 | +keys_list.add(RSAjwk); |
| 96 | +keys_list.add(RSAjwk_rsa2); |
| 97 | +jwkSet = new JWKSet(keys_list); |
| 98 | +jwkSet.getKeys(); |
| 99 | + |
| 100 | +byte jwtbyte[] = jwkSet.toString().getBytes(); |
| 101 | +FileOutputStream out = new FileOutputStream(ks_file); |
| 102 | +out.write(jwtbyte); |
| 103 | +out.close(); |
| 104 | +} |
| 105 | + |
| 106 | + @After |
| 107 | + public void cleanup() throws IOException { |
| 108 | + |
| 109 | +File f1 = new File(ks_file); |
| 110 | +if (f1.exists()) { |
| 111 | +f1.delete(); |
| 112 | +} |
| 113 | +File f2 = new File(ks_file_badJWK); |
| 114 | +if (f2.exists()) { |
| 115 | +f2.delete(); |
| 116 | +} |
| 117 | + } |
| 118 | + |
| 119 | +/* Constructors with no valid Resource setup */ |
| 120 | +@Test |
| 121 | +public void ksConstructorTest() { |
| 122 | + |
| 123 | +JWKSetKeyStore ks = new JWKSetKeyStore(jwkSet); |
| 124 | +assertEquals(ks.getJwkSet(), jwkSet); |
| 125 | + |
| 126 | +JWKSetKeyStore ks_empty= new JWKSetKeyStore(); |
| 127 | +assertEquals(ks_empty.getJwkSet(), null); |
| 128 | + |
| 129 | +boolean thrown = false; |
| 130 | +try { |
| 131 | +JWKSetKeyStore ks_null = new JWKSetKeyStore(null); |
| 132 | +} catch (IllegalArgumentException e) { |
| 133 | +thrown = true; |
| 134 | +} |
| 135 | +assertTrue(thrown); |
| 136 | +} |
| 137 | + |
| 138 | +/* Misformatted JWK */ |
| 139 | +@Test(expected=IllegalArgumentException.class) |
| 140 | +public void ksBadJWKinput() throws IOException { |
| 141 | + |
| 142 | +byte jwtbyte[] = RSAjwk.toString().getBytes(); |
| 143 | +FileOutputStream out = new FileOutputStream(ks_file_badJWK); |
| 144 | +out.write(jwtbyte); |
| 145 | +out.close(); |
| 146 | + |
| 147 | +JWKSetKeyStore ks_badJWK = new JWKSetKeyStore(); |
| 148 | +Resource loc = new FileSystemResource(ks_file_badJWK); |
| 149 | +assertTrue(loc.exists()); |
| 150 | + |
| 151 | +ks_badJWK.setLocation(loc); |
| 152 | +assertEquals(loc.getFilename(), ks_file_badJWK); |
| 153 | + |
| 154 | +ks_badJWK = new JWKSetKeyStore(null); |
| 155 | +} |
| 156 | + |
| 157 | +/* Empty constructor with valid Resource */ |
| 158 | +@Test |
| 159 | +public void ksEmptyConstructorkLoc() { |
| 160 | + |
| 161 | +JWKSetKeyStore ks = new JWKSetKeyStore(); |
| 162 | + |
| 163 | +File file = new File(ks_file); |
| 164 | + |
| 165 | +/* First, test with file without "read" permission */ |
| 166 | + |
| 167 | +if (file.exists()) { |
| 168 | +file.setReadable(false); |
| 169 | +} |
| 170 | +Resource loc_noread = new FileSystemResource(file); |
| 171 | +assertTrue(loc_noread.exists()); |
| 172 | +assertTrue(!loc_noread.isReadable()); |
| 173 | + |
| 174 | +boolean thrown = false; |
| 175 | +try { |
| 176 | +ks.setLocation(loc_noread); |
| 177 | +} catch (IllegalArgumentException e) { |
| 178 | +thrown = true; |
| 179 | +} |
| 180 | +assertTrue(thrown); |
| 181 | + |
| 182 | +/* Now, make cache file readable */ |
| 183 | + |
| 184 | +if (file.exists()) { |
| 185 | +file.setReadable(true); |
| 186 | +} |
| 187 | +Resource loc = new FileSystemResource(file); |
| 188 | +assertTrue(loc.exists()); |
| 189 | +assertTrue(loc.isReadable()); |
| 190 | + |
| 191 | +ks.setLocation(loc); |
| 192 | + |
| 193 | +assertEquals(loc.getFilename(),ks.getLocation().getFilename()); |
| 194 | +} |
| 195 | + |
| 196 | + |
| 197 | +@Test |
| 198 | +public void ksSetJwkSet() throws IllegalArgumentException { |
| 199 | + |
| 200 | +JWKSetKeyStore ks = new JWKSetKeyStore(); |
| 201 | +boolean thrown = false; |
| 202 | +try { |
| 203 | +ks.setJwkSet(null); |
| 204 | +} catch (IllegalArgumentException e) { |
| 205 | +thrown = true; |
| 206 | +} |
| 207 | +assertTrue(thrown); |
| 208 | + |
| 209 | +ks.setJwkSet(jwkSet);; |
| 210 | +assertEquals(ks.getJwkSet(), jwkSet); |
| 211 | +} |
| 212 | +} |
0 commit comments