--- old/test/java/security/KeyStore/TestKeyStoreBasic.java Thu Oct 13 11:13:01 2016 +++ new/test/java/security/KeyStore/TestKeyStoreBasic.java Thu Oct 13 11:13:01 2016 @@ -116,6 +116,8 @@ }; private static final String ALIAS_HEAD = "test"; + private static final String CRYPTO_ALG = "PBEWithHmacSHA256AndAES_128"; + public static void main(String args[]) throws Exception { TestKeyStoreBasic jstest = new TestKeyStoreBasic(); jstest.run(); @@ -125,7 +127,7 @@ for (String provider : PROVIDERS) { try { runTest(provider); - System.out.println("Test with provider " + provider + "passed"); + System.out.println("Test with provider " + provider + " passed"); } catch (java.security.KeyStoreException e) { if (provider.equals("SunPKCS11-Solaris")) { System.out.println("KeyStoreException is expected: " @@ -236,8 +238,46 @@ // compare the creation date of the 2 key stores for all aliases compareCreationDate(ks, ks2, numEntries); + // check setEntry/getEntry with a password protection algorithm + if ("PKCS12".equalsIgnoreCase(ks.getType())) { + System.out.println( + "Skipping the setEntry/getEntry check for PKCS12 keystore..."); + return; + } + String alias = ALIAS_HEAD + ALIAS_HEAD; + KeyStore.PasswordProtection pw = + new KeyStore.PasswordProtection(PASSWD2, CRYPTO_ALG, null); + KeyStore.PrivateKeyEntry entry = + new KeyStore.PrivateKeyEntry(privateKey, new Certificate[]{ cert }); + checkSetEntry(ks, alias, pw, entry); + ks.setEntry(alias, entry, new KeyStore.PasswordProtection(PASSWD2)); + checkGetEntry(ks, alias, pw); } + // check setEntry with a password protection algorithm + private void checkSetEntry(KeyStore ks, String alias, + KeyStore.PasswordProtection pw, KeyStore.Entry entry) throws Exception { + try { + ks.setEntry(alias, entry, pw); + throw new Exception( + "ERROR: expected KeyStore.setEntry to throw an exception"); + } catch (KeyStoreException e) { + // ignore the expected exception + } + } + + // check getEntry with a password protection algorithm + private void checkGetEntry(KeyStore ks, String alias, + KeyStore.PasswordProtection pw) throws Exception { + try { + ks.getEntry(alias, pw); + throw new Exception( + "ERROR: expected KeyStore.getEntry to throw an exception"); + } catch (KeyStoreException e) { + // ignore the expected exception + } + } + // check key store type private void checkType(KeyStore obj, String type) { if (!obj.getType().equals(type)) {