< prev index next >

test/java/security/KeyStore/TestKeyStoreBasic.java

Print this page




  99         + "gWWBd+4S4PCKlpSur0gj2rMW4fP5HQfNlHci8JV8/bG4KuKRAXW56dg1818Hl3pc\n"
 100         + "iIrUSRn8uUjH3p9qb+Rb/u3mmVQRyJjN2t/zceNsO8/+Dd808OB9aEwGs8lMT0nn\n"
 101         + "ZYaaAqYz1GIY/Ecyx1vfEZEQ1ljo6i/r70C3igbypBUShxSiGsleiVTLOGNA+MN1\n"
 102         + "/a/Qh0bkaQyTGqK3bwvzzMeQVqWu2EWTBD/PmND5ExkpRICdv8LBVXfLnpoBr4lL\n"
 103         + "hnxn9+e0Ah+t8dS5EKfn44w5bI5PCu2bqxs6RCTxNjcY\n"
 104         + "-----END CERTIFICATE-----\n";
 105 
 106     private static final char[] PASSWD2 = new char[] {
 107             'b', 'o', 'r', 'e', 'd'
 108     };
 109     private static final char[] PASSWDK = "cannot be null"
 110             .toCharArray();
 111     private static final String[] KS_Type = {
 112             "jks", "jceks", "pkcs12", "PKCS11KeyStore"
 113     };
 114     private static final String[] PROVIDERS = {
 115             "SUN", "SunJCE", "SunJSSE", "SunPKCS11-Solaris"
 116     };
 117     private static final String ALIAS_HEAD = "test";
 118 


 119     public static void main(String args[]) throws Exception {
 120         TestKeyStoreBasic jstest = new TestKeyStoreBasic();
 121         jstest.run();
 122     }
 123 
 124     public void run() throws Exception {
 125         for (String provider : PROVIDERS) {
 126             try {
 127                 runTest(provider);
 128                 System.out.println("Test with provider " + provider + "passed");
 129             } catch (java.security.KeyStoreException e) {
 130                 if (provider.equals("SunPKCS11-Solaris")) {
 131                     System.out.println("KeyStoreException is expected: "
 132                             + "PKCS11KeyStore is invalid keystore type: " + e);
 133                 } else {
 134                     throw e;
 135                 }
 136             } catch (NoSuchProviderException e) {
 137                 String osName = System.getProperty("os.name");
 138                 if (provider.equals("SunPKCS11-Solaris")
 139                         && !osName.equals("SunOS")) {
 140                     System.out.println("Skip SunPKCS11-Solaris provider on "
 141                             + osName);
 142                 } else {
 143                     throw e;
 144                 }
 145             }
 146         }
 147     }
 148 


 219                 throw new RuntimeException("Unexpected cause");
 220             }
 221             System.out.println("Expected cause: "
 222                     + UnrecoverableKeyException.class.getName());
 223 
 224             bais.reset();
 225             ks.load(bais, PASSWD2);
 226             bais.reset();
 227             ks.load(bais, null);
 228         }
 229 
 230         // check key store type
 231         checkType(ks, type);
 232 
 233         // check the existing aliases
 234         checkAlias(ks, numEntries);
 235 
 236         // compare the creation date of the 2 key stores for all aliases
 237         compareCreationDate(ks, ks2, numEntries);
 238 





 239     }









 240 
























 241     // check key store type
 242     private void checkType(KeyStore obj, String type) {
 243         if (!obj.getType().equals(type)) {
 244             throw new RuntimeException("ERROR: wrong key store type");
 245         }
 246     }
 247 
 248     // check the existing aliases
 249     private void checkAlias(KeyStore obj, int range) throws KeyStoreException {
 250         for (int k = 0; k < range; k++) {
 251             if (!obj.containsAlias(ALIAS_HEAD + k)) {
 252                 throw new RuntimeException("ERROR: alias (" + k
 253                         + ") should exist");
 254             }
 255         }
 256     }
 257 
 258     // compare the creation dates - true if all the same
 259     private void compareCreationDate(KeyStore o1, KeyStore o2, int range)
 260             throws KeyStoreException {




  99         + "gWWBd+4S4PCKlpSur0gj2rMW4fP5HQfNlHci8JV8/bG4KuKRAXW56dg1818Hl3pc\n"
 100         + "iIrUSRn8uUjH3p9qb+Rb/u3mmVQRyJjN2t/zceNsO8/+Dd808OB9aEwGs8lMT0nn\n"
 101         + "ZYaaAqYz1GIY/Ecyx1vfEZEQ1ljo6i/r70C3igbypBUShxSiGsleiVTLOGNA+MN1\n"
 102         + "/a/Qh0bkaQyTGqK3bwvzzMeQVqWu2EWTBD/PmND5ExkpRICdv8LBVXfLnpoBr4lL\n"
 103         + "hnxn9+e0Ah+t8dS5EKfn44w5bI5PCu2bqxs6RCTxNjcY\n"
 104         + "-----END CERTIFICATE-----\n";
 105 
 106     private static final char[] PASSWD2 = new char[] {
 107             'b', 'o', 'r', 'e', 'd'
 108     };
 109     private static final char[] PASSWDK = "cannot be null"
 110             .toCharArray();
 111     private static final String[] KS_Type = {
 112             "jks", "jceks", "pkcs12", "PKCS11KeyStore"
 113     };
 114     private static final String[] PROVIDERS = {
 115             "SUN", "SunJCE", "SunJSSE", "SunPKCS11-Solaris"
 116     };
 117     private static final String ALIAS_HEAD = "test";
 118 
 119     private static final String CRYPTO_ALG = "PBEWithHmacSHA256AndAES_128";
 120 
 121     public static void main(String args[]) throws Exception {
 122         TestKeyStoreBasic jstest = new TestKeyStoreBasic();
 123         jstest.run();
 124     }
 125 
 126     public void run() throws Exception {
 127         for (String provider : PROVIDERS) {
 128             try {
 129                 runTest(provider);
 130                 System.out.println("Test with provider " + provider + " passed");
 131             } catch (java.security.KeyStoreException e) {
 132                 if (provider.equals("SunPKCS11-Solaris")) {
 133                     System.out.println("KeyStoreException is expected: "
 134                             + "PKCS11KeyStore is invalid keystore type: " + e);
 135                 } else {
 136                     throw e;
 137                 }
 138             } catch (NoSuchProviderException e) {
 139                 String osName = System.getProperty("os.name");
 140                 if (provider.equals("SunPKCS11-Solaris")
 141                         && !osName.equals("SunOS")) {
 142                     System.out.println("Skip SunPKCS11-Solaris provider on "
 143                             + osName);
 144                 } else {
 145                     throw e;
 146                 }
 147             }
 148         }
 149     }
 150 


 221                 throw new RuntimeException("Unexpected cause");
 222             }
 223             System.out.println("Expected cause: "
 224                     + UnrecoverableKeyException.class.getName());
 225 
 226             bais.reset();
 227             ks.load(bais, PASSWD2);
 228             bais.reset();
 229             ks.load(bais, null);
 230         }
 231 
 232         // check key store type
 233         checkType(ks, type);
 234 
 235         // check the existing aliases
 236         checkAlias(ks, numEntries);
 237 
 238         // compare the creation date of the 2 key stores for all aliases
 239         compareCreationDate(ks, ks2, numEntries);
 240 
 241         // check setEntry/getEntry with a password protection algorithm
 242         if ("PKCS12".equalsIgnoreCase(ks.getType())) {
 243             System.out.println(
 244                 "Skipping the setEntry/getEntry check for PKCS12 keystore...");
 245             return;
 246         }
 247         String alias = ALIAS_HEAD + ALIAS_HEAD;
 248         KeyStore.PasswordProtection pw =
 249             new KeyStore.PasswordProtection(PASSWD2, CRYPTO_ALG, null);
 250         KeyStore.PrivateKeyEntry entry =
 251             new KeyStore.PrivateKeyEntry(privateKey, new Certificate[]{ cert });
 252         checkSetEntry(ks, alias, pw, entry);
 253         ks.setEntry(alias, entry, new KeyStore.PasswordProtection(PASSWD2));
 254         checkGetEntry(ks, alias, pw);
 255     }
 256 
 257     // check setEntry with a password protection algorithm
 258     private void checkSetEntry(KeyStore ks, String alias,
 259         KeyStore.PasswordProtection pw, KeyStore.Entry entry) throws Exception {
 260         try {
 261             ks.setEntry(alias, entry, pw);
 262             throw new Exception(
 263                 "ERROR: expected KeyStore.setEntry to throw an exception");
 264         } catch (KeyStoreException e) {
 265             // ignore the expected exception
 266         }
 267     }
 268 
 269     // check getEntry with a password protection algorithm
 270     private void checkGetEntry(KeyStore ks, String alias,
 271         KeyStore.PasswordProtection pw) throws Exception {
 272         try {
 273             ks.getEntry(alias, pw);
 274             throw new Exception(
 275                 "ERROR: expected KeyStore.getEntry to throw an exception");
 276         } catch (KeyStoreException e) {
 277             // ignore the expected exception
 278         }
 279     }
 280 
 281     // check key store type
 282     private void checkType(KeyStore obj, String type) {
 283         if (!obj.getType().equals(type)) {
 284             throw new RuntimeException("ERROR: wrong key store type");
 285         }
 286     }
 287 
 288     // check the existing aliases
 289     private void checkAlias(KeyStore obj, int range) throws KeyStoreException {
 290         for (int k = 0; k < range; k++) {
 291             if (!obj.containsAlias(ALIAS_HEAD + k)) {
 292                 throw new RuntimeException("ERROR: alias (" + k
 293                         + ") should exist");
 294             }
 295         }
 296     }
 297 
 298     // compare the creation dates - true if all the same
 299     private void compareCreationDate(KeyStore o1, KeyStore o2, int range)
 300             throws KeyStoreException {


< prev index next >