KeyStoreTest.java revision 5ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0
1e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom/* 2e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * Copyright (C) 2010 The Android Open Source Project 3e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * 4e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License"); 5e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * you may not use this file except in compliance with the License. 6e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * You may obtain a copy of the License at 7e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * 8e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * http://www.apache.org/licenses/LICENSE-2.0 9e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * 10e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * Unless required by applicable law or agreed to in writing, software 11e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS, 12e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * See the License for the specific language governing permissions and 14e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * limitations under the License. 15e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom */ 16e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.security; 18e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 19e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.io.ByteArrayInputStream; 20e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.io.ByteArrayOutputStream; 21e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.io.File; 22e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.io.FileInputStream; 23e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.io.FileOutputStream; 24a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Rootimport java.io.IOException; 2528192ac5dbb128c63d914fab324d15757fe98fdaJesse Wilsonimport java.io.InputStream; 2657f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstromimport java.io.OutputStream; 274557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Key; 284557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.KeyStore; 29e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.Builder; 30e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.Entry; 31e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.LoadStoreParameter; 32e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.PasswordProtection; 33e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.PrivateKeyEntry; 34e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.ProtectionParameter; 35e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.SecretKeyEntry; 36e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.TrustedCertificateEntry; 374557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.KeyStoreException; 384557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.NoSuchAlgorithmException; 394557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Provider; 404557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Security; 414557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.UnrecoverableKeyException; 42e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.cert.Certificate; 43e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.cert.X509Certificate; 44e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.ArrayList; 45e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Arrays; 46e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Collections; 47e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Date; 48e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.HashSet; 49e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.List; 50e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Set; 51e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.KeyGenerator; 52e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.SecretKey; 53e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport junit.framework.TestCase; 54e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 55e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrompublic class KeyStoreTest extends TestCase { 56e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 57003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static PrivateKeyEntry PRIVATE_KEY; 58003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static PrivateKeyEntry PRIVATE_KEY_2; 596a75005c0547634e5179829c61eb03209197cedaJesse Wilson 60003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static SecretKey SECRET_KEY; 61003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static SecretKey SECRET_KEY_2; 62e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 63e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_PRIVATE = "private"; 64e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_CERTIFICATE = "certificate"; 65e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_SECRET = "secret"; 66e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 67e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_ALT_CASE_PRIVATE = "pRiVaTe"; 68e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_ALT_CASE_CERTIFICATE = "cErTiFiCaTe"; 69e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_ALT_CASE_SECRET = "sEcRet"; 70e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 713d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root private static final String ALIAS_UNICODE_PRIVATE = "\u6400\u7902\u3101\u8c02\u5002\u8702\udd01"; 723d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root private static final String ALIAS_UNICODE_CERTIFICATE = "\u5402\udd01\u7902\u8702\u3101\u5f02\u3101\u5402\u5002\u8702\udd01"; 733d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root private static final String ALIAS_UNICODE_SECRET = "\ue224\ud424\ud224\ue124\ud424\ue324"; 743d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root 75e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_NO_PASSWORD_PRIVATE = "private-no-password"; 76e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_NO_PASSWORD_SECRET = "secret-no-password"; 77e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 78e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final char[] PASSWORD_STORE = "store password".toCharArray(); 79e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final char[] PASSWORD_KEY = "key password".toCharArray(); 80e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final char[] PASSWORD_BAD = "dummy".toCharArray(); 81e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 82e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final ProtectionParameter PARAM_STORE = new PasswordProtection(PASSWORD_STORE); 83e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final ProtectionParameter PARAM_KEY = new PasswordProtection(PASSWORD_KEY); 84e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final ProtectionParameter PARAM_BAD = new PasswordProtection(PASSWORD_BAD); 85e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 86003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static PrivateKeyEntry getPrivateKey() { 87003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom if (PRIVATE_KEY == null) { 88003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom PRIVATE_KEY = TestKeyStore.getServer().getPrivateKey("RSA", "RSA"); 89003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 90003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom return PRIVATE_KEY; 91003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 92003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom 93003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static PrivateKeyEntry getPrivateKey2() { 94003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom if (PRIVATE_KEY_2 == null) { 95003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom PRIVATE_KEY_2 = TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA"); 96003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 97003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom return PRIVATE_KEY_2; 98003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 99003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom 100003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static SecretKey getSecretKey() { 101003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom if (SECRET_KEY == null) { 102003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom SECRET_KEY = generateSecretKey(); 103003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 104003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom return SECRET_KEY; 105003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 106003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom 107003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static SecretKey getSecretKey2() { 108003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom if (SECRET_KEY_2 == null) { 109003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom SECRET_KEY_2 = generateSecretKey(); 110003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 111003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom return SECRET_KEY_2; 112003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 113003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom 114003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static SecretKey generateSecretKey() { 115003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom try { 116003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom KeyGenerator kg = KeyGenerator.getInstance("DES"); 117003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom return kg.generateKey(); 118003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } catch (NoSuchAlgorithmException e) { 119003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom throw new RuntimeException(e); 120003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 121003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 122003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom 123a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom public static List<KeyStore> keyStores() throws Exception { 124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom List<KeyStore> keyStores = new ArrayList<KeyStore>(); 125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Provider[] providers = Security.getProviders(); 126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (Provider provider : providers) { 127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Set<Provider.Service> services = provider.getServices(); 128e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (Provider.Service service : services) { 129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String type = service.getType(); 130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!type.equals("KeyStore")) { 131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String algorithm = service.getAlgorithm(); 134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(algorithm, provider); 135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(provider, ks.getProvider()); 136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(algorithm, ks.getType()); 137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!isUnsupported(ks)) { 138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStores.add(ks); 139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 140e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 141e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 142e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return keyStores; 143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isSecretKeyEnabled(KeyStore ks) { 146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // JKS key stores cannot store secret keys, neither can the RI's PKCS12 147e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (!(ks.getType().equals("JKS") 148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("CaseExactJKS") 149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || (ks.getType().equals("PKCS12")))); 150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 152e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isCertificateEnabled(KeyStore ks) { 153e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // RI can't handle certificate in PKCS12, but BC can 154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (!(ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("SunJSSE"))); 155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isCaseSensitive(KeyStore ks) { 158e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (ks.getType().equals("CaseExactJKS") 159e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("BKS") 160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("BouncyCastle")); 161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isUnsupported(KeyStore ks) { 165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // Don't bother testing BC on RI 166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (StandardNames.IS_RI && ks.getProvider().getName().equals("BC")); 167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isNullPasswordAllowed(KeyStore ks) { 170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (!(ks.getType().equals("JKS") 171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("CaseExactJKS") 172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("JCEKS") 173e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("PKCS12"))); 174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isKeyPasswordIgnored(KeyStore ks) { 177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // BouncyCastle's PKCS12 ignores the key password unlike the RI which requires it 178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC")); 179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 18146c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom private static boolean isLoadStoreParameterSupported(KeyStore ks) { 18246c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom // BouncyCastle's PKCS12 allows a JDKPKCS12StoreParameter 18346c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC")); 18446c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom } 18546c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom 186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isSetKeyByteArrayUnimplemented(KeyStore ks) { 187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // All of BouncyCastle's 188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // KeyStore.setKeyEntry(String,byte[],char[]) implementations 189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // throw RuntimeException 190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (ks.getProvider().getName().equals("BC")); 191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 193347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom private static boolean hasDefaultContents(KeyStore ks) { 194347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom // AndroidCAStore exposes CA cert files via the KeyStore 195347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom // interface, so it does start out empty like other KeyStores 196347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom return (ks.getType().equals("AndroidCAStore")); 197347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 198347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 199347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom private static boolean isReadOnly(KeyStore ks) { 200347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom // AndroidCAStore is read only, throwing 201347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom // UnsupportedOperationException on write operations 202347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom return (ks.getType().equals("AndroidCAStore")); 203347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 204347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void populate(KeyStore ks) throws Exception { 206e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.load(null, null); 207347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(ks)) { 208347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 209347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom setPrivateKey(ks); 2105ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(ks.toString()); 211347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException e) { 212347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 213347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom return; 214347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 215e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setPrivateKey(ks); 216e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(ks)) { 217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(ALIAS_NO_PASSWORD_PRIVATE, 218003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getPrivateKey(), 219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 220003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificateChain()); 221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 222e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(ks)) { 223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setCertificateEntry(ALIAS_CERTIFICATE, 224003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificate()); 225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 226e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(ks)) { 227e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setSecretKey(ks); 228e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(ks)) { 229e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(ALIAS_NO_PASSWORD_SECRET, 230003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getSecretKey(), 231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKey(KeyStore ks) throws Exception { 238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setPrivateKey(ks, ALIAS_PRIVATE); 239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 240e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKey(KeyStore ks, String alias) throws Exception { 241003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setPrivateKey(ks, alias, getPrivateKey()); 242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKey(KeyStore ks, 244e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String alias, 245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PrivateKeyEntry privateKey) 246e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 247e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(alias, 248e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom privateKey.getPrivateKey(), 249e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PASSWORD_KEY, 250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom privateKey.getCertificateChain()); 251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 252e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKeyBytes(KeyStore ks) throws Exception { 254e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setPrivateKeyBytes(ks, ALIAS_PRIVATE); 255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKeyBytes(KeyStore ks, String alias) throws Exception { 257003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setPrivateKeyBytes(ks, alias, getPrivateKey()); 258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 259e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKeyBytes(KeyStore ks, 260e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String alias, 261e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PrivateKeyEntry privateKey) 262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(alias, 264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom privateKey.getPrivateKey().getEncoded(), 265e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom privateKey.getCertificateChain()); 266e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 267e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 268e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKey(KeyStore ks) throws Exception { 269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setSecretKey(ks, ALIAS_SECRET); 270e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 271e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKey(KeyStore ks, String alias) throws Exception { 272003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKey(ks, alias, getSecretKey()); 273e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKey(KeyStore ks, String alias, SecretKey key) throws Exception { 275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(alias, 276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom key, 277e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PASSWORD_KEY, 278e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 279e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 280e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKeyBytes(KeyStore ks) throws Exception { 282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setSecretKeyBytes(ks, ALIAS_SECRET); 283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 284e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKeyBytes(KeyStore ks, String alias) throws Exception { 285003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKeyBytes(ks, alias, getSecretKey()); 286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKeyBytes(KeyStore ks, String alias, SecretKey key) 288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(alias, 290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom key.getEncoded(), 291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setCertificate(KeyStore ks) throws Exception { 295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setCertificate(ks, ALIAS_CERTIFICATE); 296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setCertificate(KeyStore ks, String alias) throws Exception { 298003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setCertificate(ks, alias, getPrivateKey().getCertificate()); 299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 300e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setCertificate(KeyStore ks, String alias, Certificate certificate) 301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setCertificateEntry(alias, certificate); 303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertPrivateKey(Key actual) 306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 307003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey().getPrivateKey(), actual); 308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertPrivateKey2(Key actual) 310e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 311003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey2().getPrivateKey(), actual); 312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertPrivateKey(Entry actual) 314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 315347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNotNull(actual); 316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSame(PrivateKeyEntry.class, actual.getClass()); 317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PrivateKeyEntry privateKey = (PrivateKeyEntry) actual; 318003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey().getPrivateKey(), privateKey.getPrivateKey()); 319003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey().getCertificate(), privateKey.getCertificate()); 320003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(Arrays.asList(getPrivateKey().getCertificateChain()), 321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Arrays.asList(privateKey.getCertificateChain())); 322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertSecretKey(Key actual) 325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 326003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getSecretKey(), actual); 327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertSecretKey2(Key actual) 329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 330003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getSecretKey2(), actual); 331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertSecretKey(Entry actual) 333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSame(SecretKeyEntry.class, actual.getClass()); 335003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getSecretKey(), ((SecretKeyEntry) actual).getSecretKey()); 336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertCertificate(Certificate actual) 339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 340003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey().getCertificate(), actual); 341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertCertificate2(Certificate actual) 343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 344003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey2().getCertificate(), actual); 345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertCertificate(Entry actual) 347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSame(TrustedCertificateEntry.class, actual.getClass()); 349003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey().getCertificate(), 350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ((TrustedCertificateEntry) actual).getTrustedCertificate()); 351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 352e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 353e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertCertificateChain(Certificate[] actual) 354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 355003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(Arrays.asList(getPrivateKey().getCertificateChain()), 356e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Arrays.asList(actual)); 357e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_create() throws Exception { 360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Provider[] providers = Security.getProviders(); 361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (Provider provider : providers) { 362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Set<Provider.Service> services = provider.getServices(); 363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (Provider.Service service : services) { 364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String type = service.getType(); 365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!type.equals("KeyStore")) { 366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String algorithm = service.getAlgorithm(); 369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(algorithm, provider); 370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(provider, ks.getProvider()); 371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(algorithm, ks.getType()); 372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getInstance() throws Exception { 377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String type = KeyStore.getDefaultType(); 378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(null); 3805ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 381e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 382e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(KeyStore.getInstance(type)); 385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String providerName = StandardNames.SECURITY_PROVIDER_NAME; 387e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(null, (String)null); 3895ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (IllegalArgumentException expected) { 391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(null, providerName); 3945ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 397e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(type, (String)null); 4035ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (IllegalArgumentException expected) { 405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(KeyStore.getInstance(type, providerName)); 407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Provider provider = Security.getProvider(providerName); 409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(null, (Provider)null); 4115ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (IllegalArgumentException expected) { 413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(null, provider); 4165ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 420e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(type, (Provider)null); 4215ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (IllegalArgumentException expected) { 423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(KeyStore.getInstance(type, provider)); 425e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 427e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getDefaultType() throws Exception { 428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String type = KeyStore.getDefaultType(); 429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(type); 430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(type); 431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(ks); 432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(type, ks.getType()); 433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getProvider() throws Exception { 436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(ks.getProvider()); 438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(StandardNames.SECURITY_PROVIDER_NAME, ks.getProvider().getName()); 439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(keyStore.getProvider()); 442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getType() throws Exception { 446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String type = KeyStore.getDefaultType(); 447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(type); 448e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(ks.getType()); 449e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(type, ks.getType()); 450e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(keyStore.getType()); 453e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 454e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getKey() throws Exception { 457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 458e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 459e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(null, null); 4605ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 461e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 462e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 463e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 464e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 465e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(null, null); 4715ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(null, PASSWORD_KEY); 4805ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class 484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey("", null)); 489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey("", PASSWORD_KEY)); 490e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 491e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 492347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 493347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 494e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 495347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 496347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isSecretKeyEnabled(keyStore)) { 497347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 498347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 499347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 500347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 502e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 503e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 504347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) { 505e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 506e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 507e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 508e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 509e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 510e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 511e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 512e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 514e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with null passwords 515e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isKeyPasswordIgnored(keyStore)) { 516e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null)); 517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 518347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 519347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, null)); 520347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 521347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 522347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.getKey(ALIAS_PRIVATE, null); 5235ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 524347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (Exception e) { 525347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 526347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom && e.getClass() != IllegalArgumentException.class) { 527347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom throw e; 528347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 530e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 531e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 532347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 533347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, null)); 534347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isSecretKeyEnabled(keyStore)) { 535e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 536e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(ALIAS_SECRET, null); 5375ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 540e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 541e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 542e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 543e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 545e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 546e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with bad passwords 547347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 548347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, null)); 549347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isKeyPasswordIgnored(keyStore)) { 550e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null)); 551e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 552e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 553e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(ALIAS_PRIVATE, PASSWORD_BAD); 5545ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 555e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnrecoverableKeyException expected) { 556e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 557e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 558347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 559347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_BAD)); 560347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isSecretKeyEnabled(keyStore)) { 561e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 562e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(ALIAS_SECRET, PASSWORD_BAD); 5635ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 564e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnrecoverableKeyException expected) { 565e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 566e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 567e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 568e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 569e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 570e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getCertificateChain() throws Exception { 571e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 572e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 573e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCertificateChain(null); 5745ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 575e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 576e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 577e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 578e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 579e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 580e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 581e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 583e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCertificateChain(null); 5845ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 586e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 589e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 590e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 591e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificateChain("")); 592e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 594347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 595347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getCertificateChain(ALIAS_PRIVATE)); 596347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 597347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); 598347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 599e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 600e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 601347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) { 602e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE)); 603e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 604e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificateChain(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE)); 605e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 606e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 607e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 608e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 609e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getCertificate() throws Exception { 610e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 611e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 612e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCertificate(null); 6135ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 614e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 615e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 616e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 617e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 619e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 621e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 622e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCertificate(null); 6235ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 624e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 627e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate("")); 631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 633347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (!isReadOnly(keyStore) && isCertificateEnabled(keyStore)) { 634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 635e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 638e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 639e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 640347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) { 641e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 642e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 643e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 645e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 646e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 647e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 649e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 650e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getCreationDate() throws Exception { 651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 652e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 653a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom keyStore.getCreationDate(null); 6545ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 658e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom long before = System.currentTimeMillis(); 659e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 660e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // add 1000 since some key stores round of time to nearest second 661e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom long after = System.currentTimeMillis() + 1000; 662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 663e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 664e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 665e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCreationDate(null); 6675ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 668e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 669e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 670e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCreationDate("")); 671e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 672e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 673347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (!isReadOnly(keyStore) && isCertificateEnabled(keyStore)) { 674e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Date date = keyStore.getCreationDate(ALIAS_CERTIFICATE); 675e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(date); 676e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(before <= date.getTime()); 677e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(date.getTime() <= after); 678e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 679e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCreationDate(ALIAS_CERTIFICATE)); 680e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 681e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 682e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 683347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) { 684e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE)); 685e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 687e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Date date = keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE); 688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(before <= date.getTime()); 689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(date.getTime() <= after); 690e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 691e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 693e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 694e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_setKeyEntry_Key() throws Exception { 696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 698e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(null, null, null, null); 6995ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 702e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 703e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 704e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 705e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 706347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 707347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 708347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.setKeyEntry(null, null, null, null); 7095ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 710347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 711347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 712347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 713347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 714e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 715e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 716e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 717e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(null, null, null, null); 7185ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 719e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 720e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 721e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 722e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 723e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 724e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 725e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(null, null, PASSWORD_KEY, null); 7275ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 728e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 729e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 730e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 731e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 732e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 733e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 734e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 735e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(ALIAS_PRIVATE, 736003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getPrivateKey(), 737e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PASSWORD_KEY, 738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 7395ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (IllegalArgumentException expected) { 741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 742e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 743e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 747e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 749347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 750347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 751003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null); 7525ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 753347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 754347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 755347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 756347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 757e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setPrivateKey(keyStore); 758e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 759e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); 760e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 761e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 762e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setSecretKey(keyStore); 763e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 764e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 766003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null); 7675ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 768e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 769e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != KeyStoreException.class 770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != NullPointerException.class) { 771e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 772e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 773e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 774e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 775e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 776e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 777e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 778e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 779e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 780347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 781347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 782347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 783347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 784347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 785347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isCaseSensitive(keyStore)) { 786e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 787e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 788003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); 789e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 790e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 791e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 792e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 793e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 794e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 795003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKey(keyStore, ALIAS_ALT_CASE_SECRET, getSecretKey2()); 796e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 797e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 798e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 799e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 800e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 801e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 802003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); 803e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 804e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 805e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 806e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 807e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 808003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2()); 809e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 810e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 811e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 812e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 813e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 814e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 815e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 816e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 817347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 818347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 819347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.setKeyEntry(ALIAS_PRIVATE, 820003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getPrivateKey(), 821347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom null, 822003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificateChain()); 8235ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 824347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 825347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 826347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 827347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 828e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 829e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with null passwords 830e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { 831e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(ALIAS_PRIVATE, 832003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getPrivateKey(), 833e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 834003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificateChain()); 835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null)); 836e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(ALIAS_PRIVATE, 839003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getPrivateKey(), 840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 841003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificateChain()); 8425ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 843e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 844e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 845e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class 846e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 847e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 848e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 849e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 850e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 851e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 852e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { 853003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), null, null); 854e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, null)); 855e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 856e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 857003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), null, null); 8585ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 859e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 860e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 861e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class 862e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 863e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 864e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 865e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 866e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 867e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 868e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 869e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 870e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 871e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_setKeyEntry_array() throws Exception { 872e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 873e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 874e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(null, null, null); 8755ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 876e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 877e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 878e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 879e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 880e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 881e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 882e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 883347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 884347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 885347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.setKeyEntry(null, null, null); 8865ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 887347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 888347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 889347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 890347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 891347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 892e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 893e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 894e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(null, null, null); 8955ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 896e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 897e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 898e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class 899e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class 900e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != RuntimeException.class) { 901e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 902e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 903e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 904e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 905e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 906e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 907e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!isNullPasswordAllowed(keyStore)) { 908e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // TODO Use EncryptedPrivateKeyInfo to protect keys if 909e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // password is required. 910e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 911e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 912e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSetKeyByteArrayUnimplemented(keyStore)) { 913e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 914e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 915e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 916e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 917e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 918e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 919e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 920347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 921347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 922347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom setPrivateKeyBytes(keyStore); 9235ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 924347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 925347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 926347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 927347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 928e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setPrivateKeyBytes(keyStore); 929e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 930e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); 931e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 932e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 933e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setSecretKeyBytes(keyStore); 934e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 935e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 936e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 937003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey().getEncoded(), null); 9385ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 939e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 940e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 941e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 942e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 943e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 944e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 945e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!isNullPasswordAllowed(keyStore)) { 946e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // TODO Use EncryptedPrivateKeyInfo to protect keys if 947e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // password is required. 948e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 949e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 950e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSetKeyByteArrayUnimplemented(keyStore)) { 951e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 952e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 953e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 954e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 955e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 956347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 957347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 958347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 959347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 960347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 961347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isCaseSensitive(keyStore)) { 962e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 963e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 964003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); 965e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 966e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 967e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 968e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 969e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 970e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 971003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2()); 972e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 973e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 974e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 975e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 976e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 977e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 978003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); 979e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 980e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 981e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 982e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 983e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 984e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 985003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2()); 986e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 987e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 988e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 989e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 990e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 991e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 992e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 993e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_setCertificateEntry() throws Exception { 994e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 995e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 996e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setCertificateEntry(null, null); 9975ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 998e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 999e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1000e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1001e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1002e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1003e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1004347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1005e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 1006e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1007e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setCertificateEntry(null, null); 10085ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1009e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1010e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 1011e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 1012e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1013e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1014e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1015e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1016347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1017347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1018347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1019347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null); 10205ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1021347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 1022347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1023347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1024347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1025347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1026e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // Sort of delete by setting null. Note that even though 1027347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom // certificate is null, size doesn't change, 1028e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // isCertificateEntry returns true, and it is still listed in aliases. 1029e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1030e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1031e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1032e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom int size = keyStore.size(); 1033e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null); 1034e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1035e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(size, keyStore.size()); 1036e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(keyStore.isCertificateEntry(ALIAS_CERTIFICATE)); 1037e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(Collections.list(keyStore.aliases()).contains(ALIAS_CERTIFICATE)); 1038e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expectedSometimes) { 1039e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals("PKCS12", keyStore.getType()); 1040e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals("BC", keyStore.getProvider().getName()); 1041e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1042e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1043e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1044e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null); 10455ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1046e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1047e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1048e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1049e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1050e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1051e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1052e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!isCertificateEnabled(keyStore)) { 1053e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 1054e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1055e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1056e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1057e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1058e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 1059e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1060347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1061347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1062347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom setCertificate(keyStore); 10635ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1064347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 1065347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1066347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1067347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1068e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setCertificate(keyStore); 1069e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1070e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1071e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1072e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1073e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!isCertificateEnabled(keyStore)) { 1074e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 1075e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1076e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1077e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1078e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1079347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1080347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1081347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 1082347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1083347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1084347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isCaseSensitive(keyStore)) { 1085e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1086e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1087e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setCertificate(keyStore, 1088e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ALIAS_ALT_CASE_CERTIFICATE, 1089003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey2().getCertificate()); 1090e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1091e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1092e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1093e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1094e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1095e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setCertificate(keyStore, 1096e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ALIAS_ALT_CASE_CERTIFICATE, 1097003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey2().getCertificate()); 1098e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1099e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1100e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1101e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1102e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1103e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_deleteEntry() throws Exception { 1104e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1105e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1106e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(null); 11075ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1108e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1109e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1110e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1113e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1114e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1115347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1116347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1117347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.deleteEntry(null); 11185ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1119347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 1120347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1121347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1122347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1123347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 1125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(null); 11275ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1128e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 1130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 1131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(""); 1135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1140347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1141347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1142347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.deleteEntry(ALIAS_PRIVATE); 1143347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException e) { 1144347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1145347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1146347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1147347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 1149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); 1151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_PRIVATE); 1152e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1153e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_SECRET); 1157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1158e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1159e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_SECRET); 1160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_CERTIFICATE); 1165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_CERTIFICATE); 1168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1173347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 1175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCaseSensitive(keyStore)) { 1177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_ALT_CASE_PRIVATE); 1179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1181e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1182e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1183e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_ALT_CASE_SECRET); 1184e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1185e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_SECRET); 1187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_ALT_CASE_CERTIFICATE); 1192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1193e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_CERTIFICATE); 1195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1196e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1197e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1198e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1199e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1200e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_aliases() throws Exception { 1201e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1202e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1203e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.aliases(); 12045ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1206e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1208e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1209e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1211347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (hasDefaultContents(keyStore)) { 1212347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(keyStore.aliases().hasMoreElements()); 1213347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1214347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(Collections.EMPTY_SET, 12150647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson new HashSet(Collections.list(keyStore.aliases()))); 1216347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1221347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1222e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Set<String> expected = new HashSet<String>(); 1223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_PRIVATE); 1224e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_NO_PASSWORD_PRIVATE); 1226e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1227e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1228e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_SECRET); 1229e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_NO_PASSWORD_SECRET); 1231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_CERTIFICATE); 1235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1236347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (hasDefaultContents(keyStore)) { 1237347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(keyStore.aliases().hasMoreElements()); 1238347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1239347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(expected, new HashSet<String>(Collections.list(keyStore.aliases()))); 1240347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1244e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_containsAlias() throws Exception { 1245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1246e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1247e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.containsAlias(null); 12485ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1249e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1252e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1254e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.containsAlias(null); 12585ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1259e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1260e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1261e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.containsAlias("")); 1263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1265e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1266e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1267347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1268e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.containsAlias("")); 1269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1270347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1271347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.containsAlias(ALIAS_PRIVATE)); 1272347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1273347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(keyStore.containsAlias(ALIAS_PRIVATE)); 1275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(isSecretKeyEnabled(keyStore), keyStore.containsAlias(ALIAS_SECRET)); 1276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(isCertificateEnabled(keyStore), keyStore.containsAlias(ALIAS_CERTIFICATE)); 1277e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1278e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore), 1279e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.containsAlias(ALIAS_ALT_CASE_PRIVATE)); 1280e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore), 1281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.containsAlias(ALIAS_ALT_CASE_SECRET)); 1282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore), 1283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.containsAlias(ALIAS_ALT_CASE_CERTIFICATE)); 1284e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1285e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_size() throws Exception { 1288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.aliases(); 12915ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1298347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (hasDefaultContents(keyStore)) { 1299347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(keyStore.size() > 0); 1300347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1301347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(0, keyStore.size()); 1302347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1307347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (hasDefaultContents(keyStore)) { 1308347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(keyStore.size() > 0); 1309347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1310347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1311347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom int expected = 1; 1313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected++; 1315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected++; 1318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected++; 1320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected++; 1324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(expected, keyStore.size()); 1326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_isKeyEntry() throws Exception { 1330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isKeyEntry(null); 13335ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isKeyEntry(null); 13435ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isKeyEntry("")); 1348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1352e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1353347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.isKeyEntry("")); 1354347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1355347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.isKeyEntry(ALIAS_PRIVATE)); 1356347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1357347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(keyStore.isKeyEntry(ALIAS_PRIVATE)); 1359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(isSecretKeyEnabled(keyStore), keyStore.isKeyEntry(ALIAS_SECRET)); 1360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isKeyEntry(ALIAS_CERTIFICATE)); 1361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore), 1363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isKeyEntry(ALIAS_ALT_CASE_PRIVATE)); 1364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore), 1365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isKeyEntry(ALIAS_ALT_CASE_SECRET)); 1366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isKeyEntry(ALIAS_ALT_CASE_CERTIFICATE)); 1367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_isCertificateEntry() throws Exception { 1371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isCertificateEntry(null); 13745ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1381e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1382e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isCertificateEntry(null); 13855ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1387e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry(null)); 1390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry("")); 1393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1397347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry("")); 1399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry(ALIAS_PRIVATE)); 1401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry(ALIAS_SECRET)); 1402347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(isCertificateEnabled(keyStore) && !isReadOnly(keyStore), 14030647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson keyStore.isCertificateEntry(ALIAS_CERTIFICATE)); 1404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_PRIVATE)); 1406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_SECRET)); 1407347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(!isCaseSensitive(keyStore) 14080647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson && isCertificateEnabled(keyStore) 14090647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson && !isReadOnly(keyStore), 14100647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson keyStore.isCertificateEntry(ALIAS_ALT_CASE_CERTIFICATE)); 1411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getCertificateAlias() throws Exception { 1415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1416e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCertificateAlias(null); 14185ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1420e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1421e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1425e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificateAlias(null)); 1426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1427e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1430347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Set<String> expected = new HashSet<String>(); 1432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_PRIVATE); 1433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_NO_PASSWORD_PRIVATE); 1435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_CERTIFICATE); 1438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1439003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom String actual = keyStore.getCertificateAlias(getPrivateKey().getCertificate()); 1440347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(!isReadOnly(keyStore), expected.contains(actual)); 1441003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertNull(keyStore.getCertificateAlias(getPrivateKey2().getCertificate())); 1442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void assertEqualsKeyStores(File expected, char[] storePassword, KeyStore actual) 1446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception{ 1447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(actual.getType(), actual.getProvider()); 144857f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom InputStream is = new FileInputStream(expected); 144957f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom ks.load(is, storePassword); 145057f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom is.close(); 1451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(ks, actual); 1452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1453e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1454e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void assertEqualsKeyStores(KeyStore expected, 1455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ByteArrayOutputStream actual, char[] storePassword) 1456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception{ 1457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(expected.getType(), expected.getProvider()); 1458e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.load(new ByteArrayInputStream(actual.toByteArray()), storePassword); 1459e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(expected, ks); 1460e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1461e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1462e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void assertEqualsKeyStores(KeyStore expected, KeyStore actual) 1463e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception{ 1464e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(expected.size(), actual.size()); 1465e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (String alias : Collections.list(actual.aliases())) { 1466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (alias.equals(ALIAS_NO_PASSWORD_PRIVATE) 1467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || alias.equals(ALIAS_NO_PASSWORD_SECRET)) { 1468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(expected.getKey(alias, null), 1469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom actual.getKey(alias, null)); 1470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(expected.getKey(alias, PASSWORD_KEY), 1472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom actual.getKey(alias, PASSWORD_KEY)); 1473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(expected.getCertificate(alias), actual.getCertificate(alias)); 1475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_store_OutputStream() throws Exception { 1479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(null, null); 14825ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ByteArrayOutputStream out = new ByteArrayOutputStream(); 1490347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1491347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1492347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(out, null); 14935ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1494347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 1495347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1496347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1497347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1498347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(out, null); 1501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(keyStore, out, null); 1502347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1503347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1504347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1505347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1506347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(out, null); 15075ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1508347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (Exception e) { 1509347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (e.getClass() != IllegalArgumentException.class 1510347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom && e.getClass() != NullPointerException.class) { 1511347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom throw e; 1512e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1514e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1515e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1516e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1518347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1519e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ByteArrayOutputStream out = new ByteArrayOutputStream(); 1520347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1521347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1522347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(out, null); 15235ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1524347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException e) { 1525347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1526347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isNullPasswordAllowed(keyStore)) { 1527e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(out, null); 1528e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(keyStore, out, null); 1529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1530e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1531e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(out, null); 15325ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1533e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1534e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != IllegalArgumentException.class 1535e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != NullPointerException.class) { 1536e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1537e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1540e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1541e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1542e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1543e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ByteArrayOutputStream out = new ByteArrayOutputStream(); 1545347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1546347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1547347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(out, PASSWORD_STORE); 15485ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1549347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException e) { 1550347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1551347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1552347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1553e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(out, PASSWORD_STORE); 1554e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(keyStore, out, PASSWORD_STORE); 1555e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1556e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1557e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1558e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1559e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ByteArrayOutputStream out = new ByteArrayOutputStream(); 1560347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1561347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1562347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(out, PASSWORD_STORE); 15635ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1564347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException e) { 1565347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1566347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1567347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1568e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(out, PASSWORD_STORE); 1569e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(keyStore, out, PASSWORD_STORE); 1570e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1571e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1572e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1573e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_store_LoadStoreParameter() throws Exception { 1574e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1575e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1576e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(null); 15775ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1578e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1579e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1580e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1581e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1583e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(null); 15865ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnsupportedOperationException expected) { 158846c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom assertFalse(isLoadStoreParameterSupported(keyStore)); 158946c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom } catch (IllegalArgumentException expected) { 159046c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom // its supported, but null causes an exception 159146c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom assertTrue(isLoadStoreParameterSupported(keyStore)); 1592e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1594e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1595e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1596e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_load_InputStream() throws Exception { 1597e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1598e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1599347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (hasDefaultContents(keyStore)) { 1600347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(keyStore.size() > 0); 1601347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1602347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(0, keyStore.size()); 1603347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1604e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1605e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1606e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1607e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, PASSWORD_STORE); 1608347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (hasDefaultContents(keyStore)) { 1609347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(keyStore.size() > 0); 1610347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1611347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(0, keyStore.size()); 1612347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1613e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1614e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1615e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test_KeyStore_store_OutputStream effectively tests load as well as store 1616e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1617e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_load_LoadStoreParameter() throws Exception { 1619e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null); 1621347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (hasDefaultContents(keyStore)) { 1622347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(keyStore.size() > 0); 1623347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1624347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(0, keyStore.size()); 1625347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1627e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(new LoadStoreParameter() { 1631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public ProtectionParameter getProtectionParameter() { 1632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return null; 1633e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom }); 16355ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnsupportedOperationException expected) { 1637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1638e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1639e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1640e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1641e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getEntry() throws Exception { 1642e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1643e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(null, null); 16455ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1646e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1647e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1649e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1650e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1652e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 1654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(null, null); 16565ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1658e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1659e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1660e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(null, PARAM_KEY); 16615ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1663e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1664e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getEntry("", null)); 1665e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getEntry("", PARAM_KEY)); 1666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1667e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 1668347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1669347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY)); 1670e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1671347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY)); 1672347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1673347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertSecretKey(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY)); 1674347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1675347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY)); 1676347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1677347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isCertificateEnabled(keyStore)) { 1678347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertCertificate(keyStore.getEntry(ALIAS_CERTIFICATE, null)); 1679347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1680347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_CERTIFICATE, null)); 1681347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1682e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1683e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1684e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 1685347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) { 1686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY)); 1687e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY)); 1688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY)); 1690e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1691e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY)); 1692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1693e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1694347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) { 1695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null)); 1696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1698e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null)); 1699e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1702e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with null passwords 1703347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1704347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null)); 1705347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isNullPasswordAllowed(keyStore)) { 1706e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null)); 1707e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else if (isKeyPasswordIgnored(keyStore)) { 1708e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, null)); 1709e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1711e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(ALIAS_PRIVATE, null); 17125ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1713e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1714e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 1715e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 1716e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1717e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1718e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1719e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1720347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1721347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_SECRET, null)); 1722347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isSecretKeyEnabled(keyStore)) { 1723e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1724e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(ALIAS_SECRET, null); 17255ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1727e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 1728e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 1729e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1730e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1731e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1732e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1733e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1734e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with bad passwords 1735347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1736347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD)); 1737347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isKeyPasswordIgnored(keyStore)) { 1738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD)); 1739e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD); 17425ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1743e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnrecoverableKeyException expected) { 1744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1746347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1747347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_BAD)); 1748347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isSecretKeyEnabled(keyStore)) { 1749e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1750e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(ALIAS_SECRET, PARAM_BAD); 17515ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnrecoverableKeyException expected) { 1753e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1754e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1755e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1756e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1757e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1758a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root public static class FakeProtectionParameter implements ProtectionParameter { 1759a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } 1760a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root 1761e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_setEntry() throws Exception { 1762e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1763e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1764e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(null, null, null); 17665ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1767e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1768e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1769e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1771e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1772e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1773e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1774a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root try { 1775a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), new FakeProtectionParameter()); 1776a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root fail("Should not accept unknown ProtectionParameter"); 1777a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } catch (KeyStoreException expected) { 1778a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } 1779a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } 1780a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root 1781a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root for (KeyStore keyStore : keyStores()) { 1782a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root keyStore.load(null, null); 1783a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root 1784e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 1785e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1786e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(null, null, null); 17875ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1788e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1789e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 1790e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 1791e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1792e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1793e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1794e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1795e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(null, null, PARAM_KEY); 17965ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1797e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1798e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 1799e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 1800e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1801e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1802e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1803e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1804e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry("", null, PARAM_KEY); 18055ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1806e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1807e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1808e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1809e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1810e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1811e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1812e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1813e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 1814e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1815347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1816347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1817003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY); 18185ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1819347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 1820347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1821347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1822347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1823003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY); 1824e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1825e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); 1826e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1827e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1828003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY); 1829e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1830e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1831e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1832003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null); 18335ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1834e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1836e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_CERTIFICATE, 1840003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry(getPrivateKey().getCertificate()), 1841e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 1842e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1843e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1844e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1845e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_CERTIFICATE, 1846003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry(getPrivateKey().getCertificate()), 1847e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 18485ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1849e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1850e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1851e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 18523d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root keyStore.setEntry(ALIAS_UNICODE_PRIVATE, getPrivateKey(), PARAM_KEY); 18533d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root assertPrivateKey(keyStore.getKey(ALIAS_UNICODE_PRIVATE, PASSWORD_KEY)); 18543d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_UNICODE_PRIVATE)); 18553d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root if (isSecretKeyEnabled(keyStore)) { 18563d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root assertNull(keyStore.getKey(ALIAS_UNICODE_SECRET, PASSWORD_KEY)); 18573d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root keyStore.setEntry(ALIAS_UNICODE_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY); 18583d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root assertSecretKey(keyStore.getKey(ALIAS_UNICODE_SECRET, PASSWORD_KEY)); 18593d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root } else { 18603d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root try { 18613d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root keyStore.setKeyEntry(ALIAS_UNICODE_SECRET, getSecretKey(), PASSWORD_KEY, null); 18625ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 18633d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root } catch (KeyStoreException expected) { 18643d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root } 18653d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root } 1866e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1867e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1868e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1869e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1870e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1871347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1872347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1873347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 1874347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1875347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1876347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isCaseSensitive(keyStore)) { 1877e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1878e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 1879003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY); 1880e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1881e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 1882e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1883e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1884e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1885e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1886e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_SECRET, 1887003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new SecretKeyEntry(getSecretKey2()), 1888e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_KEY); 1889e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1890e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1891e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1892e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1893e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1894e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1895e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1896e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE, 1897003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry( 1898003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey2().getCertificate()), 1899e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 1900e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1901e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 19023d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root keyStore.setEntry(ALIAS_UNICODE_CERTIFICATE, 19033d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root new TrustedCertificateEntry( 19043d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root getPrivateKey().getCertificate()), 19053d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root null); 19063d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root assertCertificate(keyStore.getCertificate(ALIAS_UNICODE_CERTIFICATE)); 1907e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1908e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1909e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1910e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 1911003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY); 1912e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1913e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 1914e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1915e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1916e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1917e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1918e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_SECRET, 1919003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new SecretKeyEntry(getSecretKey2()), 1920e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_KEY); 1921e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1922e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1923e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1924e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1925e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1926e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1927e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1928e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE, 1929003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry( 1930003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey2().getCertificate()), 1931e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 1932e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1933e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 19343d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root keyStore.setEntry(ALIAS_UNICODE_CERTIFICATE, 19353d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root new TrustedCertificateEntry( 19363d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root getPrivateKey().getCertificate()), 19373d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root null); 19383d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root assertCertificate(keyStore.getCertificate(ALIAS_UNICODE_CERTIFICATE)); 1939e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1940e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1941e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1942e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1943e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1944e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1945e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1946e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with null/non-null passwords 19475ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom if (isReadOnly(keyStore)) { 19485ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom try { 19495ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null); 19505ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 19515ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } catch (UnsupportedOperationException expected) { 1952e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1953e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1954003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null); 19555ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 19565ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } catch (UnsupportedOperationException expected) { 19575ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } 19585ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom try { 19595ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_CERTIFICATE, 19605ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom new TrustedCertificateEntry(getPrivateKey().getCertificate()), 19615ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom null); 19625ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 19635ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } catch (UnsupportedOperationException expected) { 19645ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } 19655ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom continue; 19665ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } 19675ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { 19685ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null); 19695ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null)); 19705ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } else { 19715ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom try { 19725ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null); 19735ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1974e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1975e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 1976e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class 1977e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 1978e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1979e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1980e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1981e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 19825ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom if (isSecretKeyEnabled(keyStore)) { 19835ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { 19845ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null); 19855ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, null)); 19865ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } else { 19875ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom try { 19885ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null); 19895ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 19905ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } catch (Exception e) { 19915ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 19925ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom && e.getClass() != IllegalArgumentException.class 19935ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom && e.getClass() != KeyStoreException.class) { 19945ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom throw e; 19955ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } 19965ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } 1997347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1998347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1999e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 2000e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { 2001e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_CERTIFICATE, 2002003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry(getPrivateKey().getCertificate()), 2003e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_KEY); 2004e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 2005e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 2006e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2007e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_CERTIFICATE, 2008003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry( 2009003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificate()), 2010e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_KEY); 20115ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2012e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 2013e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2014e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2015e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2016e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2017e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2018e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2019e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_entryInstanceOf() throws Exception { 2020e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2021e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2022e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(null, null); 20235ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2024e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2025e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2026e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2027e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2028e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2029e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 2030e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2031e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2032e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(null, null); 20335ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2034e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2035e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2036e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2037e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(null, Entry.class); 20385ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2039e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2040e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2041e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2042e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf("", null); 20435ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2044e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2045e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2046e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2047e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf("", Entry.class)); 2048e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2049e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2050e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2051e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 2052e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2053e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 2054e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf("", Entry.class)); 2055e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf("", PrivateKeyEntry.class)); 2056e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf("", SecretKeyEntry.class)); 2057e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf("", TrustedCertificateEntry.class)); 2058e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2059347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 2060347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, PrivateKeyEntry.class)); 2061347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, SecretKeyEntry.class)); 2062347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, TrustedCertificateEntry.class)); 2063347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2064347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, SecretKeyEntry.class)); 2065347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, PrivateKeyEntry.class)); 2066347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, TrustedCertificateEntry.class)); 2067347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2068347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, 2069347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom TrustedCertificateEntry.class)); 2070347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, PrivateKeyEntry.class)); 2071347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, SecretKeyEntry.class)); 2072347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 2073347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 2074347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2075e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 2076e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(keyStore.entryInstanceOf(ALIAS_PRIVATE, PrivateKeyEntry.class)); 2077e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, SecretKeyEntry.class)); 2078e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, TrustedCertificateEntry.class)); 2079e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2080e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(isSecretKeyEnabled(keyStore), 2081e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(ALIAS_SECRET, SecretKeyEntry.class)); 2082e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, PrivateKeyEntry.class)); 2083e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, TrustedCertificateEntry.class)); 2084e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2085e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(isCertificateEnabled(keyStore), 2086e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(ALIAS_CERTIFICATE, 2087e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom TrustedCertificateEntry.class)); 2088e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, PrivateKeyEntry.class)); 2089e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, SecretKeyEntry.class)); 2090e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2091e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 2092e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore), 2093e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, PrivateKeyEntry.class)); 2094e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, SecretKeyEntry.class)); 2095e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, 2096e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom TrustedCertificateEntry.class)); 2097e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2098e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore), 2099e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, SecretKeyEntry.class)); 2100e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, PrivateKeyEntry.class)); 2101e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, 2102e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom TrustedCertificateEntry.class)); 2103e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2104e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore), 2105e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE, 2106e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom TrustedCertificateEntry.class)); 2107e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE, 2108e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PrivateKeyEntry.class)); 2109e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE, SecretKeyEntry.class)); 2110e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2113e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_Builder() throws Exception { 2114e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2115e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 2116e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2117e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder.newInstance(keyStore, null); 21185ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2119e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2120e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2121e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2122e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2123e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder.newInstance(keyStore.getType(), 2126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getProvider(), 2127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 21285ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder.newInstance(null, 2136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 2137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 2138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 21395ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2140e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2141e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2142e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder.newInstance(keyStore.getType(), 2144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getProvider(), 2145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 2146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 21475ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2152e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2153e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 2154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder builder = Builder.newInstance(keyStore, PARAM_STORE); 2155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom builder.getProtectionParameter(null); 21575ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2158e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2159e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(keyStore, builder.getKeyStore()); 2161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom builder.getProtectionParameter(null); 21635ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(PARAM_STORE, builder.getProtectionParameter("")); 2167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 2171347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom File file = File.createTempFile("keystore", keyStore.getProvider().getName()); 2173347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom OutputStream os = null; 2174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2175347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom os = new FileOutputStream(file); 2176347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 2177347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 2178347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(os, PASSWORD_STORE); 21795ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2180347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 2181347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 2182347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 2183347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 2184347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 218557f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom keyStore.store(os, PASSWORD_STORE); 218657f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom os.close(); 2187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder builder = Builder.newInstance(keyStore.getType(), 2188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getProvider(), 2189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom file, 2190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_STORE); 2191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(keyStore.getType(), builder.getKeyStore().getType()); 2192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider()); 2193e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(PARAM_STORE, builder.getProtectionParameter("")); 2194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(file, PASSWORD_STORE, keyStore); 2195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } finally { 2196a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root try { 2197a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root if (os != null) { 2198a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root os.close(); 2199a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } 2200a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } catch (IOException ignored) { 2201a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } 2202e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom file.delete(); 2203e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2204e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2206e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder builder = Builder.newInstance(keyStore.getType(), 2208e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getProvider(), 2209e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_STORE); 2210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(keyStore.getType(), builder.getKeyStore().getType()); 2211e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider()); 2212e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(PARAM_STORE, builder.getProtectionParameter("")); 2213e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2214e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2215e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2216347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom public void test_KeyStore_cacerts() throws Exception { 2217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (StandardNames.IS_RI) { 2218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return; 2219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2220347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom KeyStore ks = KeyStore.getInstance("AndroidCAStore"); 2221347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals("AndroidCAStore", ks.getType()); 2222347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals("HarmonyJSSE", ks.getProvider().getName()); 2223347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2224347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom ks.load(null, null); 2225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (String alias : Collections.list(ks.aliases())) { 2226347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom Certificate c = null; 2227cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom try { 2228347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom c = ks.getCertificate(alias); 2229347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNotNull(c); 2230347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(ks.isCertificateEntry(alias)); 2231347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(ks.entryInstanceOf(alias, TrustedCertificateEntry.class)); 2232347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(alias, ks.getCertificateAlias(c)); 2233347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2234cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom assertTrue(c instanceof X509Certificate); 2235cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom X509Certificate cert = (X509Certificate) c; 2236cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom assertEquals(cert.getSubjectUniqueID(), cert.getIssuerUniqueID()); 2237cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom assertNotNull(cert.getPublicKey()); 2238347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2239347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(ks.containsAlias(alias)); 2240347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNotNull(ks.getCreationDate(alias)); 2241347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNotNull(ks.getEntry(alias, null)); 2242347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2243347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(ks.isKeyEntry(alias)); 2244347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(ks.getKey(alias, null)); 2245347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(ks.getCertificateChain(alias)); 2246347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2247347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (Throwable t) { 2248347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom throw new Exception("alias=" + alias + " cert=" + c, t); 2249cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom } 2250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 22520647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson 22530647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson // http://b/857840: want JKS key store 22540647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson public void testDefaultKeystore() { 22550647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson String type = KeyStore.getDefaultType(); 2256a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root assertEquals(StandardNames.KEY_STORE_ALGORITHM, type); 22570647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson 22580647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson try { 22590647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType()); 22600647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson assertNotNull("Keystore must not be null", store); 22610647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson } catch (Exception ex) { 22620647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson throw new RuntimeException(ex); 22630647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson } 22640647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson 22650647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson try { 2266a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root KeyStore store = KeyStore.getInstance(StandardNames.KEY_STORE_ALGORITHM); 22670647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson assertNotNull("Keystore must not be null", store); 22680647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson } catch (Exception ex) { 22690647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson throw new RuntimeException(ex); 22700647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson } 22710647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson } 2272e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom} 2273