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; 486256280922cc8a6622a156afeb7f43a31576d43fKenny Rootimport java.util.Enumeration; 49e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.HashSet; 50e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.List; 51e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Set; 52e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.KeyGenerator; 53e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.SecretKey; 54e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport junit.framework.TestCase; 55e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 56e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrompublic class KeyStoreTest extends TestCase { 57e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 58003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static PrivateKeyEntry PRIVATE_KEY; 59003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static PrivateKeyEntry PRIVATE_KEY_2; 606a75005c0547634e5179829c61eb03209197cedaJesse Wilson 61003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static SecretKey SECRET_KEY; 62003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static SecretKey SECRET_KEY_2; 63e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 64e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_PRIVATE = "private"; 65e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_CERTIFICATE = "certificate"; 66e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_SECRET = "secret"; 67e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 68e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_ALT_CASE_PRIVATE = "pRiVaTe"; 696256280922cc8a6622a156afeb7f43a31576d43fKenny Root private static final String ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE = "PrIvAtE-no-password"; 70e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_ALT_CASE_CERTIFICATE = "cErTiFiCaTe"; 71e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_ALT_CASE_SECRET = "sEcRet"; 72e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 733d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root private static final String ALIAS_UNICODE_PRIVATE = "\u6400\u7902\u3101\u8c02\u5002\u8702\udd01"; 746256280922cc8a6622a156afeb7f43a31576d43fKenny Root private static final String ALIAS_UNICODE_NO_PASSWORD_PRIVATE = "\u926c\u0967\uc65b\ubc78"; 753d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root private static final String ALIAS_UNICODE_CERTIFICATE = "\u5402\udd01\u7902\u8702\u3101\u5f02\u3101\u5402\u5002\u8702\udd01"; 763d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root private static final String ALIAS_UNICODE_SECRET = "\ue224\ud424\ud224\ue124\ud424\ue324"; 773d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root 78e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_NO_PASSWORD_PRIVATE = "private-no-password"; 79e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final String ALIAS_NO_PASSWORD_SECRET = "secret-no-password"; 80e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 81e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final char[] PASSWORD_STORE = "store password".toCharArray(); 82e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final char[] PASSWORD_KEY = "key password".toCharArray(); 83e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final char[] PASSWORD_BAD = "dummy".toCharArray(); 84e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 85e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final ProtectionParameter PARAM_STORE = new PasswordProtection(PASSWORD_STORE); 86e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final ProtectionParameter PARAM_KEY = new PasswordProtection(PASSWORD_KEY); 87e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static final ProtectionParameter PARAM_BAD = new PasswordProtection(PASSWORD_BAD); 88e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 89003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static PrivateKeyEntry getPrivateKey() { 90003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom if (PRIVATE_KEY == null) { 91003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom PRIVATE_KEY = TestKeyStore.getServer().getPrivateKey("RSA", "RSA"); 92003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 93003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom return PRIVATE_KEY; 94003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 95003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom 96003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static PrivateKeyEntry getPrivateKey2() { 97003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom if (PRIVATE_KEY_2 == null) { 98003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom PRIVATE_KEY_2 = TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA"); 99003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 100003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom return PRIVATE_KEY_2; 101003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 102003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom 103003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static SecretKey getSecretKey() { 104003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom if (SECRET_KEY == null) { 105003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom SECRET_KEY = generateSecretKey(); 106003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 107003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom return SECRET_KEY; 108003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 109003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom 110003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static SecretKey getSecretKey2() { 111003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom if (SECRET_KEY_2 == null) { 112003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom SECRET_KEY_2 = generateSecretKey(); 113003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 114003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom return SECRET_KEY_2; 115003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 116003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom 117003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom private static SecretKey generateSecretKey() { 118003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom try { 119003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom KeyGenerator kg = KeyGenerator.getInstance("DES"); 120003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom return kg.generateKey(); 121003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } catch (NoSuchAlgorithmException e) { 122003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom throw new RuntimeException(e); 123003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 124003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom } 125003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom 126a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom public static List<KeyStore> keyStores() throws Exception { 127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom List<KeyStore> keyStores = new ArrayList<KeyStore>(); 128e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Provider[] providers = Security.getProviders(); 129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (Provider provider : providers) { 130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Set<Provider.Service> services = provider.getServices(); 131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (Provider.Service service : services) { 132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String type = service.getType(); 133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!type.equals("KeyStore")) { 134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String algorithm = service.getAlgorithm(); 137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(algorithm, provider); 138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(provider, ks.getProvider()); 139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(algorithm, ks.getType()); 140e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!isUnsupported(ks)) { 141e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStores.add(ks); 142e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return keyStores; 146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 147e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isSecretKeyEnabled(KeyStore ks) { 149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // JKS key stores cannot store secret keys, neither can the RI's PKCS12 150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (!(ks.getType().equals("JKS") 151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("CaseExactJKS") 1526256280922cc8a6622a156afeb7f43a31576d43fKenny Root || (ks.getType().equals("PKCS12")) 1536256280922cc8a6622a156afeb7f43a31576d43fKenny Root || (ks.getType().equals("AndroidKeyStore")))); 154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isCertificateEnabled(KeyStore ks) { 157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // RI can't handle certificate in PKCS12, but BC can 158e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (!(ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("SunJSSE"))); 159e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isCaseSensitive(KeyStore ks) { 162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (ks.getType().equals("CaseExactJKS") 163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("BKS") 1646256280922cc8a6622a156afeb7f43a31576d43fKenny Root || ks.getType().equals("BouncyCastle") 1656256280922cc8a6622a156afeb7f43a31576d43fKenny Root || ks.getType().equals("AndroidKeyStore")); 166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isUnsupported(KeyStore ks) { 170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // Don't bother testing BC on RI 171a138def5cf0f28168d353184050c1e526a3c9db9Kenny Root // TODO enable AndroidKeyStore when CTS can set up the keystore 172a138def5cf0f28168d353184050c1e526a3c9db9Kenny Root return (StandardNames.IS_RI && ks.getProvider().getName().equals("BC")) 173a138def5cf0f28168d353184050c1e526a3c9db9Kenny Root || "AndroidKeyStore".equalsIgnoreCase(ks.getType()); 174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isNullPasswordAllowed(KeyStore ks) { 177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (!(ks.getType().equals("JKS") 178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("CaseExactJKS") 179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("JCEKS") 180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || ks.getType().equals("PKCS12"))); 181e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1826256280922cc8a6622a156afeb7f43a31576d43fKenny Root private static boolean isKeyPasswordSupported(KeyStore ks) { 1836256280922cc8a6622a156afeb7f43a31576d43fKenny Root return !ks.getType().equals("AndroidKeyStore"); 1846256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 185e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isKeyPasswordIgnored(KeyStore ks) { 186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // BouncyCastle's PKCS12 ignores the key password unlike the RI which requires it 187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC")); 188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 19046c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom private static boolean isLoadStoreParameterSupported(KeyStore ks) { 19146c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom // BouncyCastle's PKCS12 allows a JDKPKCS12StoreParameter 19246c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC")); 19346c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom } 19446c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom 1956256280922cc8a6622a156afeb7f43a31576d43fKenny Root private static boolean isPersistentStorage(KeyStore ks) { 1966256280922cc8a6622a156afeb7f43a31576d43fKenny Root return ks.getType().equalsIgnoreCase("AndroidKeyStore"); 1976256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1986256280922cc8a6622a156afeb7f43a31576d43fKenny Root 1996256280922cc8a6622a156afeb7f43a31576d43fKenny Root private static boolean isLoadStoreUnsupported(KeyStore ks) { 2006256280922cc8a6622a156afeb7f43a31576d43fKenny Root return ks.getType().equalsIgnoreCase("AndroidKeyStore"); 2016256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 2026256280922cc8a6622a156afeb7f43a31576d43fKenny Root 203e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom private static boolean isSetKeyByteArrayUnimplemented(KeyStore ks) { 204e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // All of BouncyCastle's 205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // KeyStore.setKeyEntry(String,byte[],char[]) implementations 206e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // throw RuntimeException 207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return (ks.getProvider().getName().equals("BC")); 208e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 209e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 210347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom private static boolean hasDefaultContents(KeyStore ks) { 211347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom // AndroidCAStore exposes CA cert files via the KeyStore 212347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom // interface, so it does start out empty like other KeyStores 213347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom return (ks.getType().equals("AndroidCAStore")); 214347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 215347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 216347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom private static boolean isReadOnly(KeyStore ks) { 217347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom // AndroidCAStore is read only, throwing 218347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom // UnsupportedOperationException on write operations 219347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom return (ks.getType().equals("AndroidCAStore")); 220347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 221347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 222e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void populate(KeyStore ks) throws Exception { 2235ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom boolean readOnly = clearKeyStore(ks); 2245ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom if (readOnly) { 2255ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom return; 2265ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom } 2276256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(ks)) { 2286256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKey(ks); 229347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(ks)) { 231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(ALIAS_NO_PASSWORD_PRIVATE, 232003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getPrivateKey(), 233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 234003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificateChain()); 235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(ks)) { 237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setCertificateEntry(ALIAS_CERTIFICATE, 238003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificate()); 239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 240e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(ks)) { 241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setSecretKey(ks); 242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(ks)) { 243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(ALIAS_NO_PASSWORD_SECRET, 244003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getSecretKey(), 245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 246e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 247e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 248e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 249e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2515ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom private static boolean clearKeyStore(KeyStore ks) throws Exception { 2526256280922cc8a6622a156afeb7f43a31576d43fKenny Root ks.load(null, null); 2536256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isReadOnly(ks)) { 2546256280922cc8a6622a156afeb7f43a31576d43fKenny Root try { 2556256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKey(ks); 2566256280922cc8a6622a156afeb7f43a31576d43fKenny Root fail(ks.toString()); 2576256280922cc8a6622a156afeb7f43a31576d43fKenny Root } catch (UnsupportedOperationException e) { 2586256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 2595ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom return true; 2606256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 2616256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isPersistentStorage(ks)) { 2626256280922cc8a6622a156afeb7f43a31576d43fKenny Root Enumeration<String> aliases = ks.aliases(); 2636256280922cc8a6622a156afeb7f43a31576d43fKenny Root while (aliases.hasMoreElements()) { 2646256280922cc8a6622a156afeb7f43a31576d43fKenny Root String alias = aliases.nextElement(); 2656256280922cc8a6622a156afeb7f43a31576d43fKenny Root ks.deleteEntry(alias); 2666256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 2676256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 2685ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom return false; 2696256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 2706256280922cc8a6622a156afeb7f43a31576d43fKenny Root 2716256280922cc8a6622a156afeb7f43a31576d43fKenny Root public static void setPrivateKeyNoPassword(KeyStore ks, String alias, PrivateKeyEntry privateKey) 2726256280922cc8a6622a156afeb7f43a31576d43fKenny Root throws Exception { 2736256280922cc8a6622a156afeb7f43a31576d43fKenny Root ks.setKeyEntry(alias, privateKey.getPrivateKey(), null, privateKey.getCertificateChain()); 2746256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKey(KeyStore ks) throws Exception { 276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setPrivateKey(ks, ALIAS_PRIVATE); 277e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 278e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKey(KeyStore ks, String alias) throws Exception { 279003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setPrivateKey(ks, alias, getPrivateKey()); 280e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKey(KeyStore ks, 282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String alias, 283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PrivateKeyEntry privateKey) 284e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 285e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(alias, 286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom privateKey.getPrivateKey(), 287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PASSWORD_KEY, 288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom privateKey.getCertificateChain()); 289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKeyBytes(KeyStore ks) throws Exception { 292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setPrivateKeyBytes(ks, ALIAS_PRIVATE); 293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKeyBytes(KeyStore ks, String alias) throws Exception { 295003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setPrivateKeyBytes(ks, alias, getPrivateKey()); 296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setPrivateKeyBytes(KeyStore ks, 298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String alias, 299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PrivateKeyEntry privateKey) 300e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(alias, 302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom privateKey.getPrivateKey().getEncoded(), 303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom privateKey.getCertificateChain()); 304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKey(KeyStore ks) throws Exception { 307e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setSecretKey(ks, ALIAS_SECRET); 308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKey(KeyStore ks, String alias) throws Exception { 310003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKey(ks, alias, getSecretKey()); 311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKey(KeyStore ks, String alias, SecretKey key) throws Exception { 313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(alias, 314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom key, 315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PASSWORD_KEY, 316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKeyBytes(KeyStore ks) throws Exception { 320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setSecretKeyBytes(ks, ALIAS_SECRET); 321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKeyBytes(KeyStore ks, String alias) throws Exception { 323003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKeyBytes(ks, alias, getSecretKey()); 324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setSecretKeyBytes(KeyStore ks, String alias, SecretKey key) 326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setKeyEntry(alias, 328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom key.getEncoded(), 329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setCertificate(KeyStore ks) throws Exception { 333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setCertificate(ks, ALIAS_CERTIFICATE); 334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setCertificate(KeyStore ks, String alias) throws Exception { 336003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setCertificate(ks, alias, getPrivateKey().getCertificate()); 337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void setCertificate(KeyStore ks, String alias, Certificate certificate) 339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.setCertificateEntry(alias, certificate); 341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertPrivateKey(Key actual) 344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 345003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey().getPrivateKey(), actual); 346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertPrivateKey2(Key actual) 348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 349003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey2().getPrivateKey(), actual); 350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertPrivateKey(Entry actual) 352e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 353347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNotNull(actual); 354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSame(PrivateKeyEntry.class, actual.getClass()); 355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PrivateKeyEntry privateKey = (PrivateKeyEntry) actual; 356003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey().getPrivateKey(), privateKey.getPrivateKey()); 357003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey().getCertificate(), privateKey.getCertificate()); 358003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(Arrays.asList(getPrivateKey().getCertificateChain()), 359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Arrays.asList(privateKey.getCertificateChain())); 360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertSecretKey(Key actual) 363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 364003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getSecretKey(), actual); 365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertSecretKey2(Key actual) 367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 368003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getSecretKey2(), actual); 369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertSecretKey(Entry actual) 371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSame(SecretKeyEntry.class, actual.getClass()); 373003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getSecretKey(), ((SecretKeyEntry) actual).getSecretKey()); 374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertCertificate(Certificate actual) 377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 378003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey().getCertificate(), actual); 379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertCertificate2(Certificate actual) 381e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 382003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey2().getCertificate(), actual); 383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertCertificate(Entry actual) 385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSame(TrustedCertificateEntry.class, actual.getClass()); 387003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(getPrivateKey().getCertificate(), 388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ((TrustedCertificateEntry) actual).getTrustedCertificate()); 389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public static void assertCertificateChain(Certificate[] actual) 392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception { 393003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertEquals(Arrays.asList(getPrivateKey().getCertificateChain()), 394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Arrays.asList(actual)); 395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 397e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_create() throws Exception { 398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Provider[] providers = Security.getProviders(); 399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (Provider provider : providers) { 400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Set<Provider.Service> services = provider.getServices(); 401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (Provider.Service service : services) { 402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String type = service.getType(); 403e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!type.equals("KeyStore")) { 404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String algorithm = service.getAlgorithm(); 407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(algorithm, provider); 408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(provider, ks.getProvider()); 409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(algorithm, ks.getType()); 410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getInstance() throws Exception { 415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String type = KeyStore.getDefaultType(); 416e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(null); 4185ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 420e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 421e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(KeyStore.getInstance(type)); 423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String providerName = StandardNames.SECURITY_PROVIDER_NAME; 425e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(null, (String)null); 4275ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (IllegalArgumentException expected) { 429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(null, providerName); 4325ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(type, (String)null); 4415ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (IllegalArgumentException expected) { 443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(KeyStore.getInstance(type, providerName)); 445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Provider provider = Security.getProvider(providerName); 447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 448e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(null, (Provider)null); 4495ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 450e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (IllegalArgumentException expected) { 451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 453e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(null, provider); 4545ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 458e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore.getInstance(type, (Provider)null); 4595ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(type); 460e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (IllegalArgumentException expected) { 461e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 462e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(KeyStore.getInstance(type, provider)); 463e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 464e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 465e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getDefaultType() throws Exception { 466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String type = KeyStore.getDefaultType(); 467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(type); 468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(type); 469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(ks); 470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(type, ks.getType()); 471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getProvider() throws Exception { 474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(ks.getProvider()); 476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(StandardNames.SECURITY_PROVIDER_NAME, ks.getProvider().getName()); 477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(keyStore.getProvider()); 480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getType() throws Exception { 484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom String type = KeyStore.getDefaultType(); 485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(type); 486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(ks.getType()); 487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(type, ks.getType()); 488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 490e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(keyStore.getType()); 491e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 492e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 493e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 494e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getKey() throws Exception { 495e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 496e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 497e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(null, null); 4985ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 502e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 503e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 504e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 505e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 506e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 507e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 508e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(null, null); 5095ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 510e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 511e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 512e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 514e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 515e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 516e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(null, PASSWORD_KEY); 5185ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 519e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 520e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 521e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class 522e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 523e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 524e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 525e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 526e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey("", null)); 527e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey("", PASSWORD_KEY)); 528e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 530347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 531347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 532e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 5336256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 5346256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 5356256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 5366256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 5376256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 5386256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 539347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isSecretKeyEnabled(keyStore)) { 540347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 541347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 542347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 543347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 545e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 546e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 547347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) { 548e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 5496256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, PASSWORD_KEY)); 550e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 551e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 5526256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 5536256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 5546256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 5556256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 5566256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 5576256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 558e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 559e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 560e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 561e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 562e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 563e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with null passwords 5646256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) { 565e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null)); 566e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 567347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 568347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, null)); 5696256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isKeyPasswordSupported(keyStore)) { 570347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 571347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.getKey(ALIAS_PRIVATE, null); 5725ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 573347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (Exception e) { 574347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 575347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom && e.getClass() != IllegalArgumentException.class) { 576347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom throw e; 577347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 578e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 579e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 580e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 581347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 582347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, null)); 583347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isSecretKeyEnabled(keyStore)) { 584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(ALIAS_SECRET, null); 5865ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 589e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 590e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 591e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 592e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 594e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 595e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with bad passwords 596347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 597347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, null)); 5986256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) { 599e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null)); 6006256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isKeyPasswordSupported(keyStore)) { 601e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 602e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(ALIAS_PRIVATE, PASSWORD_BAD); 6035ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 604e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnrecoverableKeyException expected) { 605e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 606e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 607347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 608347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_BAD)); 609347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isSecretKeyEnabled(keyStore)) { 610e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 611e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getKey(ALIAS_SECRET, PASSWORD_BAD); 6125ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 613e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnrecoverableKeyException expected) { 614e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 615e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 616e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 617e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 619e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getCertificateChain() throws Exception { 620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 621e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 622e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCertificateChain(null); 6235ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 624e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 627e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCertificateChain(null); 6335ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 635e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 638e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 639e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 640e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificateChain("")); 641e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 642e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 643347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 644347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getCertificateChain(ALIAS_PRIVATE)); 6456256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isKeyPasswordSupported(keyStore)) { 646347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); 6476256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isNullPasswordAllowed(keyStore)) { 6486256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE)); 649347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 650e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 652347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) { 653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE)); 654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificateChain(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE)); 656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 658e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 659e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 660e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getCertificate() throws Exception { 661e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 663e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCertificate(null); 6645ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 665e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 667e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 668e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 669e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 670e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 671e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 672e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 673e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCertificate(null); 6745ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 675e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 676e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 677e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 678e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 679e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 680e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 681e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate("")); 682e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 683e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 684347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (!isReadOnly(keyStore) && isCertificateEnabled(keyStore)) { 685e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 687e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 690e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 691347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) { 692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 693e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 694e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 698e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 699e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getCreationDate() throws Exception { 702e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 703e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 704a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom keyStore.getCreationDate(null); 7055ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 706e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 707e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 708e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 709e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom long before = System.currentTimeMillis(); 710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 7116256280922cc8a6622a156afeb7f43a31576d43fKenny Root populate(keyStore); 7126256280922cc8a6622a156afeb7f43a31576d43fKenny Root 713e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // add 1000 since some key stores round of time to nearest second 714e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom long after = System.currentTimeMillis() + 1000; 715e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 716e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 717e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 718e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCreationDate(null); 7195ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 720e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 721e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 722e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCreationDate("")); 723e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 724e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 725347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (!isReadOnly(keyStore) && isCertificateEnabled(keyStore)) { 726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Date date = keyStore.getCreationDate(ALIAS_CERTIFICATE); 727e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNotNull(date); 7286256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("date should be after start time: " + date.getTime() + " >= " + before, 7296256280922cc8a6622a156afeb7f43a31576d43fKenny Root before <= date.getTime()); 7306256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("date should be before expiry time: " + date.getTime() + " <= " + after, 7316256280922cc8a6622a156afeb7f43a31576d43fKenny Root date.getTime() <= after); 732e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 733e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCreationDate(ALIAS_CERTIFICATE)); 734e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 735e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 736e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 737347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) { 738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE)); 739e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Date date = keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE); 742e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(before <= date.getTime()); 743e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertTrue(date.getTime() <= after); 744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 747e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 749e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_setKeyEntry_Key() throws Exception { 750e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 751e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(null, null, null, null); 7535ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 754e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 755e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 756e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 757e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 758e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 759e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 760347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 761347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 762347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.setKeyEntry(null, null, null, null); 7635ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 764347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 765347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 766347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 767347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 768e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 769e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 771e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(null, null, null, null); 7725ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 773e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 774e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 775e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 776e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 777e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 778e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 779e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 780e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(null, null, PASSWORD_KEY, null); 7815ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 782e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 783e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 784e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 785e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 786e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 787e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 788e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 789e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(ALIAS_PRIVATE, 790003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getPrivateKey(), 791e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PASSWORD_KEY, 792e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 7935ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 7946256280922cc8a6622a156afeb7f43a31576d43fKenny Root } catch (Exception e) { 7956256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (e.getClass() != IllegalArgumentException.class 7966256280922cc8a6622a156afeb7f43a31576d43fKenny Root && e.getClass() != KeyStoreException.class) { 7976256280922cc8a6622a156afeb7f43a31576d43fKenny Root throw e; 7986256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 799e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 800e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 801e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 802e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 8036256280922cc8a6622a156afeb7f43a31576d43fKenny Root clearKeyStore(keyStore); 804e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 805e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 8066256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 8076256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 8086256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 8096256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 8106256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 8116256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 812347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 813347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 814003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null); 8155ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 816347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 817347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 818347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 819347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 8206256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 8216256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKey(keyStore); 8226256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 8236256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); 8246256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 8256256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 8266256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKeyNoPassword(keyStore, ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey()); 8276256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 8286256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE)); 8296256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 830e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 831e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 832e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setSecretKey(keyStore); 833e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 834e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 836003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null); 8375ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != KeyStoreException.class 840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != NullPointerException.class) { 841e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 842e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 843e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 844e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 845e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 846e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 847e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 848e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 849e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 850347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 851347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 852347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 853347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 854347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 855347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isCaseSensitive(keyStore)) { 8566256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 8576256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 8586256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 8596256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); 8606256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 8616256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 8626256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 8636256280922cc8a6622a156afeb7f43a31576d43fKenny Root 8646256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 8656256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 8666256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 8676256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, 8686256280922cc8a6622a156afeb7f43a31576d43fKenny Root getPrivateKey2()); 8696256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 8706256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 8716256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 872e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 873e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 874e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 875e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 876003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKey(keyStore, ALIAS_ALT_CASE_SECRET, getSecretKey2()); 877e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 878e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 879e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 880e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 8816256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 8826256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 8836256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 8846256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); 8856256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 8866256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 8876256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 8886256280922cc8a6622a156afeb7f43a31576d43fKenny Root 8896256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 8906256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null)); 8916256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 8926256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKey(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, getPrivateKey2()); 8936256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, null)); 8946256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 8956256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 8966256280922cc8a6622a156afeb7f43a31576d43fKenny Root 897e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 898e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 899e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 900003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2()); 901e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 902e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 903e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 904e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 905e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 906e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 907e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 908e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 909347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 910347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 911347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.setKeyEntry(ALIAS_PRIVATE, 912003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getPrivateKey(), 913347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom null, 914003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificateChain()); 9155ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 916347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 917347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 918347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 919347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 920e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 921e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with null passwords 922e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { 923e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(ALIAS_PRIVATE, 924003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getPrivateKey(), 925e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 926003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificateChain()); 927e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null)); 928e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 929e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 930e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(ALIAS_PRIVATE, 931003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getPrivateKey(), 932e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 933003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificateChain()); 9345ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 935e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 936e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 937e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class 938e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 939e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 940e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 941e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 942e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 943e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 944e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { 945003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), null, null); 946e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, null)); 947e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 948e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 949003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), null, null); 9505ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 951e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 952e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 953e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class 954e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 955e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 956e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 957e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 958e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 959e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 960e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 961e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 962e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 963e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_setKeyEntry_array() throws Exception { 964e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 965e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 966e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(null, null, null); 9675ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 968e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 969e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 970e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 971e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 972e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 973e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 974e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 975347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 976347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 977347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.setKeyEntry(null, null, null); 9785ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 979347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 980347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 981347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 982347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 983347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 984e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 985e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 986e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setKeyEntry(null, null, null); 9875ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 988e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 989e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 990e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class 991e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class 992e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != RuntimeException.class) { 993e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 994e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 995e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 996e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 997e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 998e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 999e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!isNullPasswordAllowed(keyStore)) { 1000e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // TODO Use EncryptedPrivateKeyInfo to protect keys if 1001e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // password is required. 1002e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 1003e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1004e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSetKeyByteArrayUnimplemented(keyStore)) { 1005e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 1006e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1007e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 10086256280922cc8a6622a156afeb7f43a31576d43fKenny Root clearKeyStore(keyStore); 1009e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1010e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 10116256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 10126256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 10136256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 10146256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 10156256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 10166256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1017347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1018347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1019347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom setPrivateKeyBytes(keyStore); 10205ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1021347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 1022347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1023347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1024347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 10256256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 10266256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKeyBytes(keyStore); 10276256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 10286256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); 10296256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 10306256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 10316256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKeyNoPassword(keyStore, ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey()); 10326256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 10336256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE)); 10346256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1035e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1036e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1037e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setSecretKeyBytes(keyStore); 1038e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1039e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1040e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1041003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey().getEncoded(), null); 10425ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1043e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1044e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1045e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1046e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1047e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1048e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1049e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!isNullPasswordAllowed(keyStore)) { 1050e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // TODO Use EncryptedPrivateKeyInfo to protect keys if 1051e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // password is required. 1052e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 1053e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1054e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSetKeyByteArrayUnimplemented(keyStore)) { 1055e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 1056e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1057e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1058e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1059e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1060347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1061347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 1062347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 1063347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1064347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1065347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isCaseSensitive(keyStore)) { 10666256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 10676256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 10686256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 10696256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); 10706256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 10716256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 10726256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 10736256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 10746256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 10756256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 10766256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, 10776256280922cc8a6622a156afeb7f43a31576d43fKenny Root getPrivateKey2()); 10786256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 10796256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 10806256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1081e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1082e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1083e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1084e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1085003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2()); 1086e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1087e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1088e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1089e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 10906256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 10916256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 10926256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 10936256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2()); 10946256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 10956256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 10966256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 10976256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 10986256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 10996256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 11006256280922cc8a6622a156afeb7f43a31576d43fKenny Root setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, 11016256280922cc8a6622a156afeb7f43a31576d43fKenny Root getPrivateKey2()); 11026256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 11036256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 11046256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1105e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1106e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1107e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1108e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1109003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2()); 1110e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 1112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1113e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1114e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1115e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1116e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1117e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_setCertificateEntry() throws Exception { 1118e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1119e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1120e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setCertificateEntry(null, null); 11215ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1122e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1123e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1128347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 1130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setCertificateEntry(null, null); 11325ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 1135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 1136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1140347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1141347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1142347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1143347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null); 11445ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1145347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 1146347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1147347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1148347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1149347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // Sort of delete by setting null. Note that even though 1151347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom // certificate is null, size doesn't change, 1152e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // isCertificateEntry returns true, and it is still listed in aliases. 1153e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom int size = keyStore.size(); 1157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null); 11586256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getType(), keyStore.getCertificate(ALIAS_CERTIFICATE)); 11596256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertEquals(keyStore.getType(), size, keyStore.size()); 11606256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue(keyStore.getType(), keyStore.isCertificateEntry(ALIAS_CERTIFICATE)); 11616256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue(keyStore.getType(), 11626256280922cc8a6622a156afeb7f43a31576d43fKenny Root Collections.list(keyStore.aliases()).contains(ALIAS_CERTIFICATE)); 1163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expectedSometimes) { 11646256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (!("PKCS12".equalsIgnoreCase(keyStore.getType()) && 11656256280922cc8a6622a156afeb7f43a31576d43fKenny Root "BC".equalsIgnoreCase(keyStore.getProvider().getName())) 11666256280922cc8a6622a156afeb7f43a31576d43fKenny Root && !"AndroidKeyStore".equalsIgnoreCase(keyStore.getType())) { 11676256280922cc8a6622a156afeb7f43a31576d43fKenny Root throw expectedSometimes; 11686256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null); 11735ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!isCertificateEnabled(keyStore)) { 1181e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 1182e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1183e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 11846256280922cc8a6622a156afeb7f43a31576d43fKenny Root clearKeyStore(keyStore); 1185e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1187347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1188347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1189347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom setCertificate(keyStore); 11905ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1191347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 1192347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1193347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1194347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setCertificate(keyStore); 1196e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1197e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1198e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1199e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1200e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (!isCertificateEnabled(keyStore)) { 1201e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom continue; 1202e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1203e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1204e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1206347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 12076256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 12086256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1209347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isCaseSensitive(keyStore)) { 1210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1211e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1212e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setCertificate(keyStore, 1213e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ALIAS_ALT_CASE_CERTIFICATE, 1214003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey2().getCertificate()); 1215e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1216e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom setCertificate(keyStore, 1221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ALIAS_ALT_CASE_CERTIFICATE, 1222003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey2().getCertificate()); 1223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1224e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 1225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1226e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1227e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1228e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_deleteEntry() throws Exception { 1229e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(null); 12325ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1240347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1241347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1242347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.deleteEntry(null); 12435ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1244347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 1245347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1246347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1247347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1248347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1249e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 1250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(null); 12525ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1254e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 1255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 1256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1259e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(""); 1260e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1261e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1265347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1266347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1267347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.deleteEntry(ALIAS_PRIVATE); 1268347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException e) { 1269347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1270347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1271347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1272347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1273e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 12746256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 12756256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 12766256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); 12776256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.deleteEntry(ALIAS_PRIVATE); 12786256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 12796256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 12806256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 12816256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 12826256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE)); 12836256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.deleteEntry(ALIAS_NO_PASSWORD_PRIVATE); 12846256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 12856256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_SECRET); 1290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_SECRET); 1293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_CERTIFICATE); 1298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1300e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_CERTIFICATE); 1301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1306347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1307e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 1308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCaseSensitive(keyStore)) { 13106256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 13116256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 13126256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.deleteEntry(ALIAS_ALT_CASE_PRIVATE); 13136256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 13146256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 13156256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 13166256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 13176256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.deleteEntry(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE); 13186256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 13196256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_ALT_CASE_SECRET); 1324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 1325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_SECRET); 1327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_ALT_CASE_CERTIFICATE); 1332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 1333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.deleteEntry(ALIAS_CERTIFICATE); 1335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_aliases() throws Exception { 1341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.aliases(); 13445ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 13516256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isPersistentStorage(keyStore)) { 13526256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNotNull("Should be able to query size: " + keyStore.getType(), 13536256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.aliases()); 13546256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (hasDefaultContents(keyStore)) { 13556256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("Should have more than one alias already: " + keyStore.getType(), 13566256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.aliases().hasMoreElements()); 1357347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 13586256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertEquals("Should have no aliases:" + keyStore.getType(), Collections.EMPTY_SET, 13590647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson new HashSet(Collections.list(keyStore.aliases()))); 1360347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1365347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Set<String> expected = new HashSet<String>(); 13676256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 13686256280922cc8a6622a156afeb7f43a31576d43fKenny Root expected.add(ALIAS_PRIVATE); 13696256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_NO_PASSWORD_PRIVATE); 1372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_SECRET); 1375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_NO_PASSWORD_SECRET); 1377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_CERTIFICATE); 1381e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 13826256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isPersistentStorage(keyStore)) { 13836256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNotNull("Should be able to query size: " + keyStore.getType(), 13846256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.aliases()); 13856256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (hasDefaultContents(keyStore)) { 1386347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(keyStore.aliases().hasMoreElements()); 1387347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1388347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(expected, new HashSet<String>(Collections.list(keyStore.aliases()))); 1389347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_containsAlias() throws Exception { 1394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.containsAlias(null); 13975ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1403e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.containsAlias(null); 14075ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.containsAlias("")); 1412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1416347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.containsAlias("")); 1418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1419347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1420347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.containsAlias(ALIAS_PRIVATE)); 1421347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1422347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 14236256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 14246256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue(keyStore.containsAlias(ALIAS_PRIVATE)); 14256256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isNullPasswordAllowed(keyStore)) { 14266256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue(keyStore.containsAlias(ALIAS_NO_PASSWORD_PRIVATE)); 14276256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(isSecretKeyEnabled(keyStore), keyStore.containsAlias(ALIAS_SECRET)); 1429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(isCertificateEnabled(keyStore), keyStore.containsAlias(ALIAS_CERTIFICATE)); 1430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore), 1432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.containsAlias(ALIAS_ALT_CASE_PRIVATE)); 1433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore), 1434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.containsAlias(ALIAS_ALT_CASE_SECRET)); 1435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore), 1436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.containsAlias(ALIAS_ALT_CASE_CERTIFICATE)); 1437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_size() throws Exception { 1441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.aliases(); 14445ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1448e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1449e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1450e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 14516256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isPersistentStorage(keyStore)) { 14526256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("Should successfully query size: " + keyStore.getType(), 14536256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.size() >= 0); 14546256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (hasDefaultContents(keyStore)) { 14556256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("Should have non-empty store: " + keyStore.getType(), 14566256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.size() > 0); 1457347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 14586256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size()); 1459347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1460e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1461e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1462e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1463e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1464347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (hasDefaultContents(keyStore)) { 14656256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("Should have non-empty store: " + keyStore.getType(), 14666256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.size() > 0); 1467347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1468347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1469347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 14706256280922cc8a6622a156afeb7f43a31576d43fKenny Root int expected = 0; 14716256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 14726256280922cc8a6622a156afeb7f43a31576d43fKenny Root expected++; 14736256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected++; 1476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected++; 1479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected++; 1481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected++; 1485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(expected, keyStore.size()); 1487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1490e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_isKeyEntry() throws Exception { 1491e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1492e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1493e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isKeyEntry(null); 14945ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1495e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1496e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1497e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1498e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1502e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1503e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isKeyEntry(null); 15045ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1505e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1506e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1507e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1508e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isKeyEntry("")); 1509e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1510e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1511e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1512e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1514347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.isKeyEntry("")); 1515347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1516347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.isKeyEntry(ALIAS_PRIVATE)); 1517347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1518347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 15196256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 15206256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue(keyStore.isKeyEntry(ALIAS_PRIVATE)); 15216256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 15226256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 15236256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue(keyStore.isKeyEntry(ALIAS_NO_PASSWORD_PRIVATE)); 15246256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1525e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(isSecretKeyEnabled(keyStore), keyStore.isKeyEntry(ALIAS_SECRET)); 1526e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isKeyEntry(ALIAS_CERTIFICATE)); 1527e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1528e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore), 1529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isKeyEntry(ALIAS_ALT_CASE_PRIVATE)); 1530e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore), 1531e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isKeyEntry(ALIAS_ALT_CASE_SECRET)); 1532e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isKeyEntry(ALIAS_ALT_CASE_CERTIFICATE)); 1533e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1534e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1535e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1536e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_isCertificateEntry() throws Exception { 1537e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isCertificateEntry(null); 15405ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1541e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1542e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1543e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1545e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1546e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1547e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1548e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1549e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1550e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.isCertificateEntry(null); 15515ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1552e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1553e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1554e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1555e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry(null)); 1556e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1557e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1558e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry("")); 1559e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1560e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1561e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1562e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1563347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1564e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry("")); 1565e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 15666256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 15676256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertFalse(keyStore.isCertificateEntry(ALIAS_PRIVATE)); 15686256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 15696256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 15706256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertFalse(keyStore.isCertificateEntry(ALIAS_NO_PASSWORD_PRIVATE)); 15716256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1572e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry(ALIAS_SECRET)); 1573347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(isCertificateEnabled(keyStore) && !isReadOnly(keyStore), 15740647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson keyStore.isCertificateEntry(ALIAS_CERTIFICATE)); 1575e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1576e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_PRIVATE)); 1577e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_SECRET)); 1578347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(!isCaseSensitive(keyStore) 15790647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson && isCertificateEnabled(keyStore) 15800647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson && !isReadOnly(keyStore), 15810647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson keyStore.isCertificateEntry(ALIAS_ALT_CASE_CERTIFICATE)); 1582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1583e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getCertificateAlias() throws Exception { 1586e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getCertificateAlias(null); 15895ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1590e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1591e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1592e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1594e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1595e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1596e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificateAlias(null)); 1597e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1598e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1599e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1600e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1601347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1602e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Set<String> expected = new HashSet<String>(); 16036256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 16046256280922cc8a6622a156afeb7f43a31576d43fKenny Root expected.add(ALIAS_PRIVATE); 16056256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1606e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1607e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_NO_PASSWORD_PRIVATE); 1608e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1609e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1610e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom expected.add(ALIAS_CERTIFICATE); 1611e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1612003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom String actual = keyStore.getCertificateAlias(getPrivateKey().getCertificate()); 1613347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(!isReadOnly(keyStore), expected.contains(actual)); 1614003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom assertNull(keyStore.getCertificateAlias(getPrivateKey2().getCertificate())); 1615e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1616e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1617e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void assertEqualsKeyStores(File expected, char[] storePassword, KeyStore actual) 1619e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception{ 1620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(actual.getType(), actual.getProvider()); 162157f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom InputStream is = new FileInputStream(expected); 162257f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom ks.load(is, storePassword); 162357f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom is.close(); 1624e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(ks, actual); 1625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1627e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void assertEqualsKeyStores(KeyStore expected, 1628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ByteArrayOutputStream actual, char[] storePassword) 1629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception{ 1630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom KeyStore ks = KeyStore.getInstance(expected.getType(), expected.getProvider()); 1631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ks.load(new ByteArrayInputStream(actual.toByteArray()), storePassword); 1632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(expected, ks); 1633e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1635e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void assertEqualsKeyStores(KeyStore expected, KeyStore actual) 1636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throws Exception{ 1637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(expected.size(), actual.size()); 1638e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (String alias : Collections.list(actual.aliases())) { 1639e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (alias.equals(ALIAS_NO_PASSWORD_PRIVATE) 1640e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom || alias.equals(ALIAS_NO_PASSWORD_SECRET)) { 1641e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(expected.getKey(alias, null), 1642e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom actual.getKey(alias, null)); 1643e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(expected.getKey(alias, PASSWORD_KEY), 1645e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom actual.getKey(alias, PASSWORD_KEY)); 1646e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1647e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(expected.getCertificate(alias), actual.getCertificate(alias)); 1648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1649e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1650e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_store_OutputStream() throws Exception { 1652e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(null, null); 16555ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1658e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1659e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1660e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1661e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ByteArrayOutputStream out = new ByteArrayOutputStream(); 16636256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) { 1664347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1665347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(out, null); 16665ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1667347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 1668347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1669347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1670347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1671347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1672e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore)) { 1673e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(out, null); 1674e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(keyStore, out, null); 1675347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1676347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1677347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1678347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1679347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(out, null); 16805ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1681347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (Exception e) { 1682347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (e.getClass() != IllegalArgumentException.class 1683347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom && e.getClass() != NullPointerException.class) { 1684347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom throw e; 1685e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1687e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1690e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1691347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 1692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ByteArrayOutputStream out = new ByteArrayOutputStream(); 16936256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) { 1694347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1695347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(out, null); 16965ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 16976256280922cc8a6622a156afeb7f43a31576d43fKenny Root } catch (UnsupportedOperationException expected) { 1698347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1699347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isNullPasswordAllowed(keyStore)) { 1700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(out, null); 1701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(keyStore, out, null); 1702e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1703e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1704e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(out, null); 17055ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1706e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1707e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != IllegalArgumentException.class 1708e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != NullPointerException.class) { 1709e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1711e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1712e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1713e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1714e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1715e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1716e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1717e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ByteArrayOutputStream out = new ByteArrayOutputStream(); 17186256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) { 1719347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1720347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(out, PASSWORD_STORE); 17215ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1722347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException e) { 1723347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1724347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1725347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(out, PASSWORD_STORE); 1727e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(keyStore, out, PASSWORD_STORE); 1728e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1729e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1730e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1731e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1732e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom ByteArrayOutputStream out = new ByteArrayOutputStream(); 17336256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) { 1734347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 1735347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(out, PASSWORD_STORE); 17365ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1737347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException e) { 1738347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1739347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 1740347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(out, PASSWORD_STORE); 1742e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(keyStore, out, PASSWORD_STORE); 1743e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_store_LoadStoreParameter() throws Exception { 1747e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1749e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(null); 17505ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1751e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 1752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1753e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1754e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1755e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1756e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1757e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1758e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.store(null); 17595ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1760e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnsupportedOperationException expected) { 176146c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom assertFalse(isLoadStoreParameterSupported(keyStore)); 176246c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom } catch (IllegalArgumentException expected) { 176346c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom // its supported, but null causes an exception 176446c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom assertTrue(isLoadStoreParameterSupported(keyStore)); 1765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1766e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1767e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1768e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1769e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_load_InputStream() throws Exception { 1770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1771e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 17726256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isPersistentStorage(keyStore)) { 17736256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("Should be able to query size: " + keyStore.getType(), 17746256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.size() >= 0); 17756256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (hasDefaultContents(keyStore)) { 17766256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("Should have non-empty store: " + keyStore.getType(), 17776256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.size() > 0); 1778347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 17796256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size()); 1780347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1781e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1782e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1783e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 17846256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isLoadStoreUnsupported(keyStore)) { 17856256280922cc8a6622a156afeb7f43a31576d43fKenny Root continue; 17866256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1787e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, PASSWORD_STORE); 17886256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isPersistentStorage(keyStore)) { 17896256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("Should be able to query size: " + keyStore.getType(), 17906256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.size() >= 0); 17916256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (hasDefaultContents(keyStore)) { 17926256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("Should have non-empty store: " + keyStore.getType(), 17936256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.size() > 0); 1794347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 17956256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size()); 1796347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1797e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1798e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1799e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test_KeyStore_store_OutputStream effectively tests load as well as store 1800e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1801e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1802e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_load_LoadStoreParameter() throws Exception { 1803e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1804e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null); 18056256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isPersistentStorage(keyStore)) { 18066256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("Should be able to query size: " + keyStore.getType(), 18076256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.size() >= 0); 18086256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (hasDefaultContents(keyStore)) { 18096256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertTrue("Should have non-empty store: " + keyStore.getType(), 18106256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.size() > 0); 1811347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 18126256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size()); 1813347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1814e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1815e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1816e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1817e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1818e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(new LoadStoreParameter() { 1819e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public ProtectionParameter getProtectionParameter() { 1820e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return null; 1821e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1822e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom }); 18235ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1824e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnsupportedOperationException expected) { 1825e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1826e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1827e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1828e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1829e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_getEntry() throws Exception { 1830e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1831e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1832e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(null, null); 18335ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1834e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1836e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 1840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1841e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 1842e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1843e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(null, null); 18445ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1845e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1846e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1847e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1848e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(null, PARAM_KEY); 18495ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1850e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1851e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1852e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getEntry("", null)); 1853e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getEntry("", PARAM_KEY)); 1854e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1855e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 1856347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1857347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY)); 1858e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 18596256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 18606256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY)); 18616256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isNullPasswordAllowed(keyStore)) { 18626256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null)); 18636256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 1864347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1865347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertSecretKey(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY)); 1866347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1867347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY)); 1868347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1869347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isCertificateEnabled(keyStore)) { 1870347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertCertificate(keyStore.getEntry(ALIAS_CERTIFICATE, null)); 1871347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else { 1872347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_CERTIFICATE, null)); 1873347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 1874e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1875e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1876e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 1877347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) { 1878e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY)); 1879e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY)); 1880e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1881e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY)); 1882e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 1883e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY)); 1884e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1885e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1886347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) { 1887e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null)); 1888e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 1889e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 1890e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null)); 1891e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1892e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1893e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1894e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with null passwords 1895347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1896347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null)); 1897347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isNullPasswordAllowed(keyStore)) { 1898e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null)); 18996256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) { 1900e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, null)); 19016256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isKeyPasswordIgnored(keyStore)) { 1902e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1903e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(ALIAS_PRIVATE, null); 19045ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1905e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1906e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 1907e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 1908e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1909e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1910e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1911e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1912347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1913347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_SECRET, null)); 1914347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isSecretKeyEnabled(keyStore)) { 1915e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1916e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(ALIAS_SECRET, null); 19175ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1918e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1919e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 1920e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class) { 1921e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1922e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1923e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1924e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1925e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1926e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with bad passwords 1927347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1928347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD)); 19296256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) { 1930e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD)); 19316256280922cc8a6622a156afeb7f43a31576d43fKenny Root } else if (isKeyPasswordSupported(keyStore)) { 1932e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1933e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD); 19345ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1935e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnrecoverableKeyException expected) { 1936e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1937e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1938347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 1939347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_BAD)); 1940347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isSecretKeyEnabled(keyStore)) { 1941e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1942e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getEntry(ALIAS_SECRET, PARAM_BAD); 19435ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1944e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (UnrecoverableKeyException expected) { 1945e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1946e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1947e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1948e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1949e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1950a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root public static class FakeProtectionParameter implements ProtectionParameter { 1951a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } 1952a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root 1953e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_setEntry() throws Exception { 1954e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1955e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1956e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1957e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(null, null, null); 19585ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1959e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1960e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1961e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1962e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1963e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 1964e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 1965e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 1966a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root try { 1967a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), new FakeProtectionParameter()); 19686256280922cc8a6622a156afeb7f43a31576d43fKenny Root fail("Should not accept unknown ProtectionParameter: " + keyStore.getProvider()); 1969a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } catch (KeyStoreException expected) { 1970a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } 1971a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } 1972a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root 1973a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root for (KeyStore keyStore : keyStores()) { 1974a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root keyStore.load(null, null); 1975a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root 1976e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 1977e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1978e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(null, null, null); 19795ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1980e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1981e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 1982e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 1983e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1984e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1985e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1986e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1987e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(null, null, PARAM_KEY); 19885ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1989e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 1990e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != NullPointerException.class 1991e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 1992e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 1993e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1994e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 1995e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 1996e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry("", null, PARAM_KEY); 19975ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 1998e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 1999e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2000e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2001e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2002e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 20036256280922cc8a6622a156afeb7f43a31576d43fKenny Root clearKeyStore(keyStore); 2004e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2005e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 2006e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 2007347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 2008347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 2009003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY); 20105ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2011347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 2012347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 2013347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 2014347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 20156256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 20166256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY); 20176256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 20186256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE)); 20196256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 20206256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 20216256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.setEntry(ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey(), null); 20226256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 20236256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE)); 20246256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 2025e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 2026e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 2027003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY); 2028e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 2029e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 2030e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2031003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null); 20325ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2033e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 2034e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2035e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2036e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 2037e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE)); 2038e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_CERTIFICATE, 2039003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry(getPrivateKey().getCertificate()), 2040e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 2041e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 2042e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 2043e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2044e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_CERTIFICATE, 2045003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry(getPrivateKey().getCertificate()), 2046e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 20475ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2048e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 2049e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2050e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 20516256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 20526256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.setEntry(ALIAS_UNICODE_PRIVATE, getPrivateKey(), PARAM_KEY); 20536256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_UNICODE_PRIVATE, PASSWORD_KEY)); 20546256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore.getCertificateChain(ALIAS_UNICODE_PRIVATE)); 20556256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 20566256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 20576256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.setEntry(ALIAS_UNICODE_NO_PASSWORD_PRIVATE, getPrivateKey(), null); 20586256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_UNICODE_NO_PASSWORD_PRIVATE, null)); 20596256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertCertificateChain(keyStore 20606256280922cc8a6622a156afeb7f43a31576d43fKenny Root .getCertificateChain(ALIAS_UNICODE_NO_PASSWORD_PRIVATE)); 20616256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 20623d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root if (isSecretKeyEnabled(keyStore)) { 20633d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root assertNull(keyStore.getKey(ALIAS_UNICODE_SECRET, PASSWORD_KEY)); 20643d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root keyStore.setEntry(ALIAS_UNICODE_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY); 20653d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root assertSecretKey(keyStore.getKey(ALIAS_UNICODE_SECRET, PASSWORD_KEY)); 20663d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root } else { 20673d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root try { 20683d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root keyStore.setKeyEntry(ALIAS_UNICODE_SECRET, getSecretKey(), PASSWORD_KEY, null); 20695ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 20703d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root } catch (KeyStoreException expected) { 20713d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root } 20723d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root } 2073e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2074e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2075e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2076e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 2077e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2078347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 2079347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 2080347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 2081347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 2082347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 2083347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } else if (isCaseSensitive(keyStore)) { 20846256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isKeyPasswordSupported(keyStore)) { 20856256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 20866256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 20876256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY); 20886256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 20896256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 20906256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 20916256280922cc8a6622a156afeb7f43a31576d43fKenny Root 20926256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isNullPasswordAllowed(keyStore)) { 20936256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 20946256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 20956256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.setEntry(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, getPrivateKey2(), null); 20966256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null)); 20976256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null)); 20986256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 2099e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2100e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 2101e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 2102e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 2103e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_SECRET, 2104003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new SecretKeyEntry(getSecretKey2()), 2105e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_KEY); 2106e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 2107e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 2108e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2109e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2110e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 2111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 2112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 2113e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE, 2114003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry( 2115003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey2().getCertificate()), 2116e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 2117e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 2118e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 21193d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root keyStore.setEntry(ALIAS_UNICODE_CERTIFICATE, 21203d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root new TrustedCertificateEntry( 21213d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root getPrivateKey().getCertificate()), 21223d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root null); 21233d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root assertCertificate(keyStore.getCertificate(ALIAS_UNICODE_CERTIFICATE)); 2124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 2126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 2127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 2128003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY); 2129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY)); 2130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY)); 2131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isSecretKeyEnabled(keyStore)) { 2133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 2134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 2135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_SECRET, 2136003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new SecretKeyEntry(getSecretKey2()), 2137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_KEY); 2138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY)); 2139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY)); 2140e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2141e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2142e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 2143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 2144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 2145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE, 2146003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry( 2147003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey2().getCertificate()), 2148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 2149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE)); 2150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE)); 21513d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root keyStore.setEntry(ALIAS_UNICODE_CERTIFICATE, 21523d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root new TrustedCertificateEntry( 21533d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root getPrivateKey().getCertificate()), 21543d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root null); 21553d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root assertCertificate(keyStore.getCertificate(ALIAS_UNICODE_CERTIFICATE)); 2156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2158e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2159e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 2162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test with null/non-null passwords 21645ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom if (isReadOnly(keyStore)) { 21655ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom try { 21665ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null); 21675ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 21685ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } catch (UnsupportedOperationException expected) { 2169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2171003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null); 21725ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 21735ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } catch (UnsupportedOperationException expected) { 21745ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } 21755ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom try { 21765ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_CERTIFICATE, 21775ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom new TrustedCertificateEntry(getPrivateKey().getCertificate()), 21785ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom null); 21795ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 21805ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } catch (UnsupportedOperationException expected) { 21815ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } 21825ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom continue; 21835ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } 21845ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { 21855ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null); 21865ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null)); 21875ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } else { 21885ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom try { 21895ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null); 21905ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (Exception e) { 2192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 2193e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != IllegalArgumentException.class 2194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom && e.getClass() != KeyStoreException.class) { 2195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom throw e; 2196e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2197e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2198e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 21995ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom if (isSecretKeyEnabled(keyStore)) { 22005ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { 22015ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null); 22025ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom assertSecretKey(keyStore.getKey(ALIAS_SECRET, null)); 22035ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } else { 22045ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom try { 22055ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null); 22065ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 22075ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } catch (Exception e) { 22085ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom if (e.getClass() != UnrecoverableKeyException.class 22095ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom && e.getClass() != IllegalArgumentException.class 22105ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom && e.getClass() != KeyStoreException.class) { 22115ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom throw e; 22125ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } 22135ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom } 2214347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 2215347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 2216e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isCertificateEnabled(keyStore)) { 2217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) { 2218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_CERTIFICATE, 2219003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry(getPrivateKey().getCertificate()), 2220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_KEY); 2221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE)); 2222e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } else { 2223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2224e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.setEntry(ALIAS_CERTIFICATE, 2225003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom new TrustedCertificateEntry( 2226003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom getPrivateKey().getCertificate()), 2227e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_KEY); 22285ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2229e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (KeyStoreException expected) { 2230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_entryInstanceOf() throws Exception { 2237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(null, null); 22405ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2244e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2246e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 2247e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2248e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2249e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(null, null); 22505ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2252e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2254e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(null, Entry.class); 22555ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2259e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf("", null); 22605ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2261e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf("", Entry.class)); 2265e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2266e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2267e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2268e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 2269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2270e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test odd inputs 2271e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf("", Entry.class)); 2272e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf("", PrivateKeyEntry.class)); 2273e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf("", SecretKeyEntry.class)); 2274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf("", TrustedCertificateEntry.class)); 2275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2276347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom if (isReadOnly(keyStore)) { 2277347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, PrivateKeyEntry.class)); 2278347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, SecretKeyEntry.class)); 2279347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, TrustedCertificateEntry.class)); 2280347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2281347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, SecretKeyEntry.class)); 2282347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, PrivateKeyEntry.class)); 2283347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, TrustedCertificateEntry.class)); 2284347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2285347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, 2286347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom TrustedCertificateEntry.class)); 2287347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, PrivateKeyEntry.class)); 2288347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, SecretKeyEntry.class)); 2289347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 2290347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 2291347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case sensitive 22936256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertEquals(isKeyPasswordSupported(keyStore), 22946256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.entryInstanceOf(ALIAS_PRIVATE, PrivateKeyEntry.class)); 2295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, SecretKeyEntry.class)); 2296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, TrustedCertificateEntry.class)); 2297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 22986256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertEquals(isNullPasswordAllowed(keyStore), 22996256280922cc8a6622a156afeb7f43a31576d43fKenny Root keyStore.entryInstanceOf(ALIAS_NO_PASSWORD_PRIVATE, PrivateKeyEntry.class)); 23006256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertFalse(keyStore.entryInstanceOf(ALIAS_NO_PASSWORD_PRIVATE, SecretKeyEntry.class)); 23016256280922cc8a6622a156afeb7f43a31576d43fKenny Root assertFalse(keyStore.entryInstanceOf(ALIAS_NO_PASSWORD_PRIVATE, 23026256280922cc8a6622a156afeb7f43a31576d43fKenny Root TrustedCertificateEntry.class)); 23036256280922cc8a6622a156afeb7f43a31576d43fKenny Root 2304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(isSecretKeyEnabled(keyStore), 2305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(ALIAS_SECRET, SecretKeyEntry.class)); 2306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, PrivateKeyEntry.class)); 2307e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, TrustedCertificateEntry.class)); 2308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(isCertificateEnabled(keyStore), 2310e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(ALIAS_CERTIFICATE, 2311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom TrustedCertificateEntry.class)); 2312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, PrivateKeyEntry.class)); 2313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, SecretKeyEntry.class)); 2314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom // test case insensitive 2316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore), 2317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, PrivateKeyEntry.class)); 2318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, SecretKeyEntry.class)); 2319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, 2320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom TrustedCertificateEntry.class)); 2321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore), 2323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, SecretKeyEntry.class)); 2324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, PrivateKeyEntry.class)); 2325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, 2326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom TrustedCertificateEntry.class)); 2327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore), 2329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE, 2330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom TrustedCertificateEntry.class)); 2331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE, 2332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PrivateKeyEntry.class)); 2333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE, SecretKeyEntry.class)); 2334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom public void test_KeyStore_Builder() throws Exception { 2338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 2340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder.newInstance(keyStore, null); 23425ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder.newInstance(keyStore.getType(), 2350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getProvider(), 2351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 23525ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2353e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2356e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2357e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder.newInstance(null, 2360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 2361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 2362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 23635ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder.newInstance(keyStore.getType(), 2368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getProvider(), 2369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null, 2370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom null); 23715ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.load(null, null); 2378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder builder = Builder.newInstance(keyStore, PARAM_STORE); 2379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom builder.getProtectionParameter(null); 23815ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2382e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(keyStore, builder.getKeyStore()); 2385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom builder.getProtectionParameter(null); 23875ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } catch (NullPointerException expected) { 2389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(PARAM_STORE, builder.getProtectionParameter("")); 2391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 2394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom populate(keyStore); 2395347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom File file = File.createTempFile("keystore", keyStore.getProvider().getName()); 2397347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom OutputStream os = null; 2398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom try { 2399347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom os = new FileOutputStream(file); 24006256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) { 2401347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom try { 2402347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom keyStore.store(os, PASSWORD_STORE); 24035ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom fail(keyStore.getType()); 2404347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (UnsupportedOperationException expected) { 2405347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 2406347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom continue; 2407347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } 2408347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 240957f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom keyStore.store(os, PASSWORD_STORE); 241057f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom os.close(); 2411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder builder = Builder.newInstance(keyStore.getType(), 2412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getProvider(), 2413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom file, 2414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_STORE); 2415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(keyStore.getType(), builder.getKeyStore().getType()); 2416e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider()); 2417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(PARAM_STORE, builder.getProtectionParameter("")); 2418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEqualsKeyStores(file, PASSWORD_STORE, keyStore); 2419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } finally { 2420a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root try { 2421a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root if (os != null) { 2422a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root os.close(); 2423a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } 2424a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } catch (IOException ignored) { 2425a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root } 2426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom file.delete(); 2427e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (KeyStore keyStore : keyStores()) { 24316256280922cc8a6622a156afeb7f43a31576d43fKenny Root if (isLoadStoreUnsupported(keyStore)) { 24326256280922cc8a6622a156afeb7f43a31576d43fKenny Root continue; 24336256280922cc8a6622a156afeb7f43a31576d43fKenny Root } 2434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom Builder builder = Builder.newInstance(keyStore.getType(), 2435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom keyStore.getProvider(), 2436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom PARAM_STORE); 2437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(keyStore.getType(), builder.getKeyStore().getType()); 2438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider()); 2439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom assertEquals(PARAM_STORE, builder.getProtectionParameter("")); 2440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom 2443347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom public void test_KeyStore_cacerts() throws Exception { 2444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom if (StandardNames.IS_RI) { 2445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom return; 2446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2447347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom KeyStore ks = KeyStore.getInstance("AndroidCAStore"); 2448347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals("AndroidCAStore", ks.getType()); 2449347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals("HarmonyJSSE", ks.getProvider().getName()); 2450347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2451347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom ks.load(null, null); 2452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom for (String alias : Collections.list(ks.aliases())) { 2453347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom Certificate c = null; 2454cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom try { 2455347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom c = ks.getCertificate(alias); 2456347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNotNull(c); 2457347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(ks.isCertificateEntry(alias)); 2458347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(ks.entryInstanceOf(alias, TrustedCertificateEntry.class)); 2459347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertEquals(alias, ks.getCertificateAlias(c)); 2460347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2461cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom assertTrue(c instanceof X509Certificate); 2462cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom X509Certificate cert = (X509Certificate) c; 2463cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom assertEquals(cert.getSubjectUniqueID(), cert.getIssuerUniqueID()); 2464cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom assertNotNull(cert.getPublicKey()); 2465347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2466347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertTrue(ks.containsAlias(alias)); 2467347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNotNull(ks.getCreationDate(alias)); 2468347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNotNull(ks.getEntry(alias, null)); 2469347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2470347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertFalse(ks.isKeyEntry(alias)); 2471347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(ks.getKey(alias, null)); 2472347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom assertNull(ks.getCertificateChain(alias)); 2473347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom 2474347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom } catch (Throwable t) { 2475347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom throw new Exception("alias=" + alias + " cert=" + c, t); 2476cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom } 2477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 2478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom } 24790647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson 24800647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson // http://b/857840: want JKS key store 24810647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson public void testDefaultKeystore() { 24820647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson String type = KeyStore.getDefaultType(); 2483a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root assertEquals(StandardNames.KEY_STORE_ALGORITHM, type); 24840647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson 24850647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson try { 24860647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType()); 24870647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson assertNotNull("Keystore must not be null", store); 24880647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson } catch (Exception ex) { 24890647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson throw new RuntimeException(ex); 24900647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson } 24910647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson 24920647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson try { 2493a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root KeyStore store = KeyStore.getInstance(StandardNames.KEY_STORE_ALGORITHM); 24940647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson assertNotNull("Keystore must not be null", store); 24950647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson } catch (Exception ex) { 24960647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson throw new RuntimeException(ex); 24970647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson } 24980647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson } 2499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom} 2500