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;
491b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Rootimport java.util.HashMap;
50e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.HashSet;
51e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.List;
52e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Set;
53e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.KeyGenerator;
54e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.SecretKey;
55e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport junit.framework.TestCase;
56e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
57e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrompublic class KeyStoreTest extends TestCase {
58e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
59434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller    private static final HashMap<String, PrivateKeyEntry> sPrivateKeys
601b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root            = new HashMap<String, PrivateKeyEntry>();
611b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root
62434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller    private static TestKeyStore sTestKeyStore;
631b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root
641b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root    private static final String[] KEY_TYPES = new String[] { "DH", "DSA", "RSA", "EC" };
651b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root
66434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller    private static PrivateKeyEntry sPrivateKey2;
676a75005c0547634e5179829c61eb03209197cedaJesse Wilson
68434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller    private static SecretKey sSecretKey;
69434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller    private static SecretKey sSecretKey2;
70e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
71e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_PRIVATE = "private";
72e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_CERTIFICATE = "certificate";
73e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_SECRET = "secret";
74e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
75e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_PRIVATE = "pRiVaTe";
766256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static final String ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE = "PrIvAtE-no-password";
77e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_CERTIFICATE = "cErTiFiCaTe";
78e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_SECRET = "sEcRet";
79e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
803d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root    private static final String ALIAS_UNICODE_PRIVATE = "\u6400\u7902\u3101\u8c02\u5002\u8702\udd01";
816256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static final String ALIAS_UNICODE_NO_PASSWORD_PRIVATE = "\u926c\u0967\uc65b\ubc78";
823d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root    private static final String ALIAS_UNICODE_CERTIFICATE = "\u5402\udd01\u7902\u8702\u3101\u5f02\u3101\u5402\u5002\u8702\udd01";
833d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root    private static final String ALIAS_UNICODE_SECRET = "\ue224\ud424\ud224\ue124\ud424\ue324";
843d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root
85e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_NO_PASSWORD_PRIVATE = "private-no-password";
86e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_NO_PASSWORD_SECRET = "secret-no-password";
87e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
88e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_STORE = "store password".toCharArray();
89e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_KEY = "key password".toCharArray();
90e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_BAD = "dummy".toCharArray();
91e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
92e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_STORE = new PasswordProtection(PASSWORD_STORE);
93e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_KEY = new PasswordProtection(PASSWORD_KEY);
94e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_BAD = new PasswordProtection(PASSWORD_BAD);
95e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
96003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static PrivateKeyEntry getPrivateKey() {
971b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root        return getPrivateKey("RSA");
981b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root    }
991b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root
1001b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root    private static PrivateKeyEntry getPrivateKey(String keyType) {
101434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller        // Avoiding initialization of TestKeyStore in the static initializer: it breaks CTS tests
102434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller        // by causing a NetworkOnMainThreadException.
103434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller        if (sTestKeyStore == null) {
104434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller            sTestKeyStore = new TestKeyStore.Builder()
105434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller                .keyAlgorithms("RSA", "DH_RSA", "DSA", "EC")
106434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller                .aliasPrefix("rsa-dsa-ec-dh")
107434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller                .build();
108434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller        }
109434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller
1101b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root        PrivateKeyEntry entry = sPrivateKeys.get(keyType);
1111b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root        if (entry == null) {
1121b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root            if ("RSA".equals(keyType)) {
113434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller                entry = sTestKeyStore.getPrivateKey("RSA", "RSA");
1141b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root            } else if ("DH".equals(keyType)) {
115434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller                entry = sTestKeyStore.getPrivateKey("DH", "RSA");
1161b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root            } else if ("DSA".equals(keyType)) {
117434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller                entry = sTestKeyStore.getPrivateKey("DSA", "DSA");
1181b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root            } else if ("EC".equals(keyType)) {
119434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller                entry = sTestKeyStore.getPrivateKey("EC", "EC");
1201b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root            } else {
1211b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root                throw new IllegalArgumentException("Unexpected key type " + keyType);
1221b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root            }
1231b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root            sPrivateKeys.put(keyType, entry);
124003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        }
1251b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root        return entry;
126003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    }
127003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom
128003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static PrivateKeyEntry getPrivateKey2() {
129434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller        if (sPrivateKey2 == null) {
130434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller            sPrivateKey2 = TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA");
131003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        }
132434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller        return sPrivateKey2;
133003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    }
134003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom
135003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static SecretKey getSecretKey() {
136434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller        if (sSecretKey == null) {
137434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller            sSecretKey = generateSecretKey();
138003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        }
139434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller        return sSecretKey;
140003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    }
141003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom
142003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static SecretKey getSecretKey2() {
143434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller        if (sSecretKey2 == null) {
144434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller            sSecretKey2 = generateSecretKey();
145003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        }
146434f6706b7b47372e83f1ecf2b07a17aa3d3f7d6Neil Fuller        return sSecretKey2;
147003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    }
148003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom
149003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static SecretKey generateSecretKey() {
150003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        try {
151003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            KeyGenerator kg = KeyGenerator.getInstance("DES");
152003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            return kg.generateKey();
153003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        } catch (NoSuchAlgorithmException e) {
154003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            throw new RuntimeException(e);
155003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        }
156003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    }
157003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom
158a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom    public static List<KeyStore> keyStores() throws Exception {
159e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        List<KeyStore> keyStores = new ArrayList<KeyStore>();
160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider[] providers = Security.getProviders();
161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (Provider provider : providers) {
162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<Provider.Service> services = provider.getServices();
163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            for (Provider.Service service : services) {
164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String type = service.getType();
165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!type.equals("KeyStore")) {
166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    continue;
167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String algorithm = service.getAlgorithm();
169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                KeyStore ks = KeyStore.getInstance(algorithm, provider);
170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(provider, ks.getProvider());
171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(algorithm, ks.getType());
172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!isUnsupported(ks)) {
173e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStores.add(ks);
174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return keyStores;
178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isSecretKeyEnabled(KeyStore ks) {
181e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // JKS key stores cannot store secret keys, neither can the RI's PKCS12
182e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("JKS")
183e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("CaseExactJKS")
1846256280922cc8a6622a156afeb7f43a31576d43fKenny Root                  || (ks.getType().equals("PKCS12"))
1856256280922cc8a6622a156afeb7f43a31576d43fKenny Root                  || (ks.getType().equals("AndroidKeyStore"))));
186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isCertificateEnabled(KeyStore ks) {
189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // RI can't handle certificate in PKCS12, but BC can
190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("SunJSSE")));
191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
193e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isCaseSensitive(KeyStore ks) {
194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getType().equals("CaseExactJKS")
195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                || ks.getType().equals("BKS")
1966256280922cc8a6622a156afeb7f43a31576d43fKenny Root                || ks.getType().equals("BouncyCastle")
1976256280922cc8a6622a156afeb7f43a31576d43fKenny Root                || ks.getType().equals("AndroidKeyStore"));
198e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
199e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
200e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
201e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isUnsupported(KeyStore ks) {
202e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // Don't bother testing BC on RI
203a138def5cf0f28168d353184050c1e526a3c9db9Kenny Root        // TODO enable AndroidKeyStore when CTS can set up the keystore
204a138def5cf0f28168d353184050c1e526a3c9db9Kenny Root        return (StandardNames.IS_RI && ks.getProvider().getName().equals("BC"))
205f0cd15d29ea97346b7c2e870969907cd586b1b30An Liu                || "AndroidKeyStore".equalsIgnoreCase(ks.getType())
206f0cd15d29ea97346b7c2e870969907cd586b1b30An Liu                || "TimaKeyStore".equalsIgnoreCase(ks.getType());
207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
208e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
209e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isNullPasswordAllowed(KeyStore ks) {
210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("JKS")
211e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("CaseExactJKS")
212e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("JCEKS")
213e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("PKCS12")));
214e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
2156256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static boolean isKeyPasswordSupported(KeyStore ks) {
2166256280922cc8a6622a156afeb7f43a31576d43fKenny Root        return !ks.getType().equals("AndroidKeyStore");
2176256280922cc8a6622a156afeb7f43a31576d43fKenny Root    }
218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isKeyPasswordIgnored(KeyStore ks) {
219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // BouncyCastle's PKCS12 ignores the key password unlike the RI which requires it
220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC"));
221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
222e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
22346c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom    private static boolean isLoadStoreParameterSupported(KeyStore ks) {
22446c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom        // BouncyCastle's PKCS12 allows a JDKPKCS12StoreParameter
22546c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom        return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC"));
22646c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom    }
22746c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom
2286256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static boolean isPersistentStorage(KeyStore ks) {
2296256280922cc8a6622a156afeb7f43a31576d43fKenny Root        return ks.getType().equalsIgnoreCase("AndroidKeyStore");
2306256280922cc8a6622a156afeb7f43a31576d43fKenny Root    }
2316256280922cc8a6622a156afeb7f43a31576d43fKenny Root
2326256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static boolean isLoadStoreUnsupported(KeyStore ks) {
2336256280922cc8a6622a156afeb7f43a31576d43fKenny Root        return ks.getType().equalsIgnoreCase("AndroidKeyStore");
2346256280922cc8a6622a156afeb7f43a31576d43fKenny Root    }
2356256280922cc8a6622a156afeb7f43a31576d43fKenny Root
236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isSetKeyByteArrayUnimplemented(KeyStore ks) {
237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // All of BouncyCastle's
238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // KeyStore.setKeyEntry(String,byte[],char[]) implementations
239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // throw RuntimeException
240e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getProvider().getName().equals("BC"));
241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
243347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom    private static boolean hasDefaultContents(KeyStore ks) {
244347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        // AndroidCAStore exposes CA cert files via the KeyStore
245347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        // interface, so it does start out empty like other KeyStores
246347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        return (ks.getType().equals("AndroidCAStore"));
247347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom    }
248347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
249347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom    private static boolean isReadOnly(KeyStore ks) {
250347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        // AndroidCAStore is read only, throwing
251347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        // UnsupportedOperationException on write operations
252347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        return (ks.getType().equals("AndroidCAStore"));
253347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom    }
254347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void populate(KeyStore ks) throws Exception {
2565ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom        boolean readOnly = clearKeyStore(ks);
2575ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom        if (readOnly) {
2585ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom            return;
2595ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom        }
2606256280922cc8a6622a156afeb7f43a31576d43fKenny Root        if (isKeyPasswordSupported(ks)) {
2616256280922cc8a6622a156afeb7f43a31576d43fKenny Root            setPrivateKey(ks);
262347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        }
263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isNullPasswordAllowed(ks)) {
264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ks.setKeyEntry(ALIAS_NO_PASSWORD_PRIVATE,
265003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                           getPrivateKey().getPrivateKey(),
266e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                           null,
267003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                           getPrivateKey().getCertificateChain());
268e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isCertificateEnabled(ks)) {
270e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ks.setCertificateEntry(ALIAS_CERTIFICATE,
271003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                   getPrivateKey().getCertificate());
272e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
273e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isSecretKeyEnabled(ks)) {
274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setSecretKey(ks);
275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(ks)) {
276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                ks.setKeyEntry(ALIAS_NO_PASSWORD_SECRET,
277003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                               getSecretKey(),
278e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               null,
279e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               null);
280e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2845ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom    private static boolean clearKeyStore(KeyStore ks) throws Exception {
2856256280922cc8a6622a156afeb7f43a31576d43fKenny Root        ks.load(null, null);
2866256280922cc8a6622a156afeb7f43a31576d43fKenny Root        if (isReadOnly(ks)) {
2876256280922cc8a6622a156afeb7f43a31576d43fKenny Root            try {
2886256280922cc8a6622a156afeb7f43a31576d43fKenny Root                setPrivateKey(ks);
2896256280922cc8a6622a156afeb7f43a31576d43fKenny Root                fail(ks.toString());
2906256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } catch (UnsupportedOperationException e) {
2916256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
2925ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom            return true;
2936256280922cc8a6622a156afeb7f43a31576d43fKenny Root        }
2946256280922cc8a6622a156afeb7f43a31576d43fKenny Root        if (isPersistentStorage(ks)) {
2956256280922cc8a6622a156afeb7f43a31576d43fKenny Root            Enumeration<String> aliases = ks.aliases();
2966256280922cc8a6622a156afeb7f43a31576d43fKenny Root            while (aliases.hasMoreElements()) {
2976256280922cc8a6622a156afeb7f43a31576d43fKenny Root                String alias = aliases.nextElement();
2986256280922cc8a6622a156afeb7f43a31576d43fKenny Root                ks.deleteEntry(alias);
2996256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
3006256280922cc8a6622a156afeb7f43a31576d43fKenny Root        }
3015ab0d1d4c268a760176a12b85abcbf1bdee0f455Brian Carlstrom        return false;
3026256280922cc8a6622a156afeb7f43a31576d43fKenny Root    }
3036256280922cc8a6622a156afeb7f43a31576d43fKenny Root
3046256280922cc8a6622a156afeb7f43a31576d43fKenny Root    public static void setPrivateKeyNoPassword(KeyStore ks, String alias, PrivateKeyEntry privateKey)
3056256280922cc8a6622a156afeb7f43a31576d43fKenny Root            throws Exception {
3066256280922cc8a6622a156afeb7f43a31576d43fKenny Root        ks.setKeyEntry(alias, privateKey.getPrivateKey(), null, privateKey.getCertificateChain());
3076256280922cc8a6622a156afeb7f43a31576d43fKenny Root    }
308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks) throws Exception {
309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKey(ks, ALIAS_PRIVATE);
310e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks, String alias) throws Exception {
312003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        setPrivateKey(ks, alias, getPrivateKey());
313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks,
315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     String alias,
316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PrivateKeyEntry privateKey)
317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getPrivateKey(),
320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       PASSWORD_KEY,
321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getCertificateChain());
322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks) throws Exception {
325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKeyBytes(ks, ALIAS_PRIVATE);
326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks, String alias) throws Exception {
328003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        setPrivateKeyBytes(ks, alias, getPrivateKey());
329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks,
331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     String alias,
332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PrivateKeyEntry privateKey)
333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getPrivateKey().getEncoded(),
336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getCertificateChain());
337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks) throws Exception {
340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKey(ks, ALIAS_SECRET);
341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks, String alias) throws Exception {
343003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        setSecretKey(ks, alias, getSecretKey());
344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks, String alias, SecretKey key) throws Exception {
346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       key,
348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       PASSWORD_KEY,
349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       null);
350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
352e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks) throws Exception {
353e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKeyBytes(ks, ALIAS_SECRET);
354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks, String alias) throws Exception {
356003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        setSecretKeyBytes(ks, alias, getSecretKey());
357e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks, String alias, SecretKey key)
359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       key.getEncoded(),
362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       null);
363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks) throws Exception {
366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setCertificate(ks, ALIAS_CERTIFICATE);
367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks, String alias) throws Exception {
369003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        setCertificate(ks, alias, getPrivateKey().getCertificate());
370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks, String alias, Certificate certificate)
372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setCertificateEntry(alias, certificate);
374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
3761b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root
377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey(Key actual)
378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
379003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey().getPrivateKey(), actual);
380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
3811b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root    public static void assertPrivateKey(String keyType, Key actual)
3821b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root            throws Exception {
3831b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root        assertEquals(getPrivateKey(keyType).getPrivateKey(), actual);
3841b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root    }
385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey2(Key actual)
386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
387003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey2().getPrivateKey(), actual);
388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey(Entry actual)
390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
391347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        assertNotNull(actual);
392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(PrivateKeyEntry.class, actual.getClass());
393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        PrivateKeyEntry privateKey = (PrivateKeyEntry) actual;
394003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey().getPrivateKey(), privateKey.getPrivateKey());
395003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey().getCertificate(), privateKey.getCertificate());
396003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(Arrays.asList(getPrivateKey().getCertificateChain()),
397e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     Arrays.asList(privateKey.getCertificateChain()));
398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey(Key actual)
401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
402003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getSecretKey(), actual);
403e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey2(Key actual)
405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
406003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getSecretKey2(), actual);
407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey(Entry actual)
409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(SecretKeyEntry.class, actual.getClass());
411003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getSecretKey(), ((SecretKeyEntry) actual).getSecretKey());
412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate(Certificate actual)
415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
416003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey().getCertificate(), actual);
417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate2(Certificate actual)
419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
420003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey2().getCertificate(), actual);
421e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate(Entry actual)
423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(TrustedCertificateEntry.class, actual.getClass());
425003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey().getCertificate(),
426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     ((TrustedCertificateEntry) actual).getTrustedCertificate());
427e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificateChain(Certificate[] actual)
430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
431003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(Arrays.asList(getPrivateKey().getCertificateChain()),
432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     Arrays.asList(actual));
433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_create() throws Exception {
436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider[] providers = Security.getProviders();
437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (Provider provider : providers) {
438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<Provider.Service> services = provider.getServices();
439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            for (Provider.Service service : services) {
440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String type = service.getType();
441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!type.equals("KeyStore")) {
442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    continue;
443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String algorithm = service.getAlgorithm();
445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                KeyStore ks = KeyStore.getInstance(algorithm, provider);
446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(provider, ks.getProvider());
447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(algorithm, ks.getType());
448e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
449e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
450e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getInstance() throws Exception {
453e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
454e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null);
4565ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (NullPointerException expected) {
458e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
459e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
460e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type));
461e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
462e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String providerName = StandardNames.SECURITY_PROVIDER_NAME;
463e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
464e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, (String)null);
4655ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, providerName);
4705ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (Exception e) {
472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (e.getClass() != NullPointerException.class
473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                && e.getClass() != KeyStoreException.class) {
474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                throw e;
475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(type, (String)null);
4795ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type, providerName));
483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider provider = Security.getProvider(providerName);
485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, (Provider)null);
4875ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
490e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
491e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, provider);
4925ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
493e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (NullPointerException expected) {
494e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
495e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
496e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(type, (Provider)null);
4975ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
498e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type, provider));
501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
502e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
503e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getDefaultType() throws Exception {
504e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
505e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(type);
506e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(type);
507e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks);
508e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(type, ks.getType());
509e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
510e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
511e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getProvider() throws Exception {
512e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks.getProvider());
514e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(StandardNames.SECURITY_PROVIDER_NAME, ks.getProvider().getName());
515e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
516e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNotNull(keyStore.getProvider());
518e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
519e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
520e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
521e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getType() throws Exception {
522e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
523e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(type);
524e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks.getType());
525e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(type, ks.getType());
526e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
527e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
528e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNotNull(keyStore.getType());
529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
530e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
531e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
532e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getKey() throws Exception {
533e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
534e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
535e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, null);
5365ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
537e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
540e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
541e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
542e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
543e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
545e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
546e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, null);
5475ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
548e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
549e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
550e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
551e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
552e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
553e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
554e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
555e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, PASSWORD_KEY);
5565ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
557e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
558e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
559e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class
560e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
561e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
562e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
563e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
564e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey("", null));
565e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey("", PASSWORD_KEY));
566e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
567e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
568347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
569347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
570e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
5716256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
5726256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
5736256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
5746256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
5756256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
5766256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
577347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
578347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
579347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } else {
580347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
581347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
583e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
585347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) {
586e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
5876256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, PASSWORD_KEY));
588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
589e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
5906256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
5916256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
5926256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
5936256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
5946256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
5956256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
596e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
597e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
598e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
599e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
600e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
601e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
6026256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
603e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
604e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
605347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                if (isReadOnly(keyStore)) {
606347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertNull(keyStore.getKey(ALIAS_PRIVATE, null));
6076256280922cc8a6622a156afeb7f43a31576d43fKenny Root                } else if (isKeyPasswordSupported(keyStore)) {
608347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    try {
609347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                        keyStore.getKey(ALIAS_PRIVATE, null);
6105ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        fail(keyStore.getType());
611347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    } catch (Exception e) {
612347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                        if (e.getClass() != UnrecoverableKeyException.class
613347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                            && e.getClass() != IllegalArgumentException.class) {
614347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                            throw e;
615347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                        }
616e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
617e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
619347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
620347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, null));
621347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isSecretKeyEnabled(keyStore)) {
622e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
623e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_SECRET, null);
6245ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
627e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
633e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with bad passwords
634347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
635347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_PRIVATE, null));
6366256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
6386256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore)) {
639e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
640e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_PRIVATE, PASSWORD_BAD);
6415ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
642e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
643e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
645347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
646347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_BAD));
647347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isSecretKeyEnabled(keyStore)) {
648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
649e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_SECRET, PASSWORD_BAD);
6505ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
652e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificateChain() throws Exception {
658e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
659e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
660e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateChain(null);
6615ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
663e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
664e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
665e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
667e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
668e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
669e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
670e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateChain(null);
6715ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
672e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
673e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
674e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
675e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
676e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
677e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
678e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificateChain(""));
679e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
680e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
681347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
682347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getCertificateChain(ALIAS_PRIVATE));
6836256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore)) {
684347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
6856256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isNullPasswordAllowed(keyStore)) {
6866256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
687347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
690347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) {
691e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE));
692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
693e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificateChain(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE));
694e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
698e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificate() throws Exception {
699e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificate(null);
7025ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
703e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
704e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
705e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
706e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
707e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
708e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
709e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
711e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificate(null);
7125ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
713e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
714e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
715e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
716e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
717e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
718e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
719e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificate(""));
720e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
721e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
722347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (!isReadOnly(keyStore) && isCertificateEnabled(keyStore)) {
723e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
724e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
725e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
727e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
728e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
729347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) {
730e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
731e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
732e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
733e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
734e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
735e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
736e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
737e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
739e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCreationDate() throws Exception {
740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
742a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                keyStore.getCreationDate(null);
7435ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
747e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        long before = System.currentTimeMillis();
748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
7496256280922cc8a6622a156afeb7f43a31576d43fKenny Root            populate(keyStore);
7506256280922cc8a6622a156afeb7f43a31576d43fKenny Root
751e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // add 1000 since some key stores round of time to nearest second
752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            long after = System.currentTimeMillis() + 1000;
753e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
754e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
755e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
756e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCreationDate(null);
7575ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
758e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
759e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
760e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCreationDate(""));
761e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
762e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
763347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (!isReadOnly(keyStore) && isCertificateEnabled(keyStore)) {
764e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Date date = keyStore.getCreationDate(ALIAS_CERTIFICATE);
765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNotNull(date);
7666256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("date should be after start time: " + date.getTime() + " >= " + before,
7676256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        before <= date.getTime());
7686256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("date should be before expiry time: " + date.getTime() + " <= " + after,
7696256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        date.getTime() <= after);
770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
771e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCreationDate(ALIAS_CERTIFICATE));
772e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
773e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
774e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
775347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) {
776e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE));
777e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
778e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
779e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    Date date = keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE);
780e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(before <= date.getTime());
781e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(date.getTime() <= after);
782e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
783e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
784e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
785e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
786e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
787e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setKeyEntry_Key() throws Exception {
788e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
789e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
790e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null, null);
7915ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
792e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
793e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
794e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
795e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
796e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
797e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
798347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
799347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
800347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.setKeyEntry(null, null, null, null);
8015ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
802347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
803347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
804347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
805347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
806e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
807e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
808e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
809e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null, null);
8105ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
811e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
812e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
813e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
814e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
815e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
816e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
817e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
818e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, PASSWORD_KEY, null);
8195ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
820e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
821e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
822e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
823e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
824e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
825e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
826e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
827e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(ALIAS_PRIVATE,
828003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                     getPrivateKey().getPrivateKey(),
829e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PASSWORD_KEY,
830e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     null);
8315ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
8326256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } catch (Exception e) {
8336256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (e.getClass() != IllegalArgumentException.class
8346256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        && e.getClass() != KeyStoreException.class) {
8356256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    throw e;
8366256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
8416256280922cc8a6622a156afeb7f43a31576d43fKenny Root            clearKeyStore(keyStore);
842e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
843e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
8446256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
8456256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
8466256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
8476256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
8486256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
8496256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
850347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
851347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
852003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null);
8535ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
854347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
855347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
856347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
857347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
8586256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
8596256280922cc8a6622a156afeb7f43a31576d43fKenny Root                setPrivateKey(keyStore);
8606256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
8616256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
8626256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
8636256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
8646256280922cc8a6622a156afeb7f43a31576d43fKenny Root                setPrivateKeyNoPassword(keyStore, ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey());
8656256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
8666256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
8676256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
868e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
869e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
870e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setSecretKey(keyStore);
871e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
872e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
873e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
874003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null);
8755ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
876e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
877e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != KeyStoreException.class
878e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != NullPointerException.class) {
879e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
880e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
881e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
882e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
883e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
884e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
885e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
886e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
887e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
888347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
889347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
890347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
891347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
892347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
893347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isCaseSensitive(keyStore)) {
8946256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
8956256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
8966256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
8976256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2());
8986256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
8996256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
9006256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
9016256280922cc8a6622a156afeb7f43a31576d43fKenny Root
9026256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
9036256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
9046256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
9056256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE,
9066256280922cc8a6622a156afeb7f43a31576d43fKenny Root                            getPrivateKey2());
9076256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
9086256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
9096256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
910e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
911e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
912e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
913e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
914003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    setSecretKey(keyStore, ALIAS_ALT_CASE_SECRET, getSecretKey2());
915e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
916e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
917e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
918e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
9196256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
9206256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
9216256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
9226256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2());
9236256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
9246256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
9256256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
9266256280922cc8a6622a156afeb7f43a31576d43fKenny Root
9276256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
9286256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
9296256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
9306256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKey(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, getPrivateKey2());
9316256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, null));
9326256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
9336256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
9346256280922cc8a6622a156afeb7f43a31576d43fKenny Root
935e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
936e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
937e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
938003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    setSecretKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2());
939e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
940e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
941e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
942e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
943e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
944e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
945e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
946e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
947347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
948347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
949347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.setKeyEntry(ALIAS_PRIVATE,
950003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                         getPrivateKey().getPrivateKey(),
951347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                                         null,
952003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                         getPrivateKey().getCertificateChain());
9535ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
954347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
955347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
956347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
957347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
958e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
959e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
960e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
961e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(ALIAS_PRIVATE,
962003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                     getPrivateKey().getPrivateKey(),
963e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     null,
964003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                     getPrivateKey().getCertificateChain());
965e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
966e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
967e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
968e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_PRIVATE,
969003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                         getPrivateKey().getPrivateKey(),
970e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                         null,
971003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                         getPrivateKey().getCertificateChain());
9725ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
973e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
974e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
975e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class
976e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != KeyStoreException.class) {
977e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
978e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
979e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
980e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
981e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
982e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
983003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), null, null);
984e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, null));
985e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
986e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    try {
987003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                        keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), null, null);
9885ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        fail(keyStore.getType());
989e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    } catch (Exception e) {
990e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        if (e.getClass() != UnrecoverableKeyException.class
991e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            && e.getClass() != IllegalArgumentException.class
992e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            && e.getClass() != KeyStoreException.class) {
993e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            throw e;
994e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        }
995e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
996e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
997e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
998e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
999e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1000e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1001e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setKeyEntry_array() throws Exception {
1002e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1003e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1004e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null);
10055ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1006e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1007e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1008e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1009e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1010e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1011e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1012e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1013347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1014347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1015347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.setKeyEntry(null, null, null);
10165ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1017347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1018347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1019347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1020347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1021347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1022e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1023e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1024e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null);
10255ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1026e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1027e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1028e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class
1029e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class
1030e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != RuntimeException.class) {
1031e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1032e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1033e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1034e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1035e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1036e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1037e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isNullPasswordAllowed(keyStore)) {
1038e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // TODO Use EncryptedPrivateKeyInfo to protect keys if
1039e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // password is required.
1040e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1041e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1042e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSetKeyByteArrayUnimplemented(keyStore)) {
1043e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1044e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1045e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
10466256280922cc8a6622a156afeb7f43a31576d43fKenny Root            clearKeyStore(keyStore);
1047e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1048e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
10496256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
10506256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
10516256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
10526256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
10536256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
10546256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1055347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1056347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1057347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    setPrivateKeyBytes(keyStore);
10585ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1059347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1060347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1061347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1062347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
10636256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
10646256280922cc8a6622a156afeb7f43a31576d43fKenny Root                setPrivateKeyBytes(keyStore);
10656256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
10666256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
10676256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
10686256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
10696256280922cc8a6622a156afeb7f43a31576d43fKenny Root                setPrivateKeyNoPassword(keyStore, ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey());
10706256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
10716256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
10726256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1073e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1074e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1075e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setSecretKeyBytes(keyStore);
1076e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1077e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1078e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1079003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey().getEncoded(), null);
10805ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1081e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
1082e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1083e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1084e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1085e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1086e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1087e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isNullPasswordAllowed(keyStore)) {
1088e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // TODO Use EncryptedPrivateKeyInfo to protect keys if
1089e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // password is required.
1090e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1091e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1092e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSetKeyByteArrayUnimplemented(keyStore)) {
1093e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1094e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1095e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1096e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1097e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1098347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1099347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1100347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
1101347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1102347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1103347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isCaseSensitive(keyStore)) {
11046256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
11056256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
11066256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
11076256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2());
11086256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
11096256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
11106256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
11116256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
11126256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
11136256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
11146256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE,
11156256280922cc8a6622a156afeb7f43a31576d43fKenny Root                            getPrivateKey2());
11166256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
11176256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
11186256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
1119e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1120e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1121e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1122e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1123003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2());
1124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
11286256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
11296256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
11306256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
11316256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2());
11326256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
11336256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
11346256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
11356256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
11366256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
11376256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
11386256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE,
11396256280922cc8a6622a156afeb7f43a31576d43fKenny Root                            getPrivateKey2());
11406256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
11416256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
11426256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
1143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1147003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2());
1148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1152e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1153e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setCertificateEntry() throws Exception {
1156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1158e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setCertificateEntry(null, null);
11595ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1166347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setCertificateEntry(null, null);
11705ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1173e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1178347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1179347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1180347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
1181347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null);
11825ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1183347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1184347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1185347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1186347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1187347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // Sort of delete by setting null.  Note that even though
1189347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            // certificate is null, size doesn't change,
1190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // isCertificateEntry returns true, and it is still listed in aliases.
1191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1193e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    int size = keyStore.size();
1195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null);
11966256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getType(), keyStore.getCertificate(ALIAS_CERTIFICATE));
11976256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertEquals(keyStore.getType(), size, keyStore.size());
11986256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertTrue(keyStore.getType(), keyStore.isCertificateEntry(ALIAS_CERTIFICATE));
11996256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertTrue(keyStore.getType(),
12006256280922cc8a6622a156afeb7f43a31576d43fKenny Root                            Collections.list(keyStore.aliases()).contains(ALIAS_CERTIFICATE));
1201e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (NullPointerException expectedSometimes) {
12026256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    if (!("PKCS12".equalsIgnoreCase(keyStore.getType()) &&
12036256280922cc8a6622a156afeb7f43a31576d43fKenny Root                                "BC".equalsIgnoreCase(keyStore.getProvider().getName()))
12046256280922cc8a6622a156afeb7f43a31576d43fKenny Root                            && !"AndroidKeyStore".equalsIgnoreCase(keyStore.getType())) {
12056256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        throw expectedSometimes;
12066256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    }
1207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1208e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1209e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null);
12115ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1212e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
1213e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1214e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1215e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1216e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isCertificateEnabled(keyStore)) {
1219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
12226256280922cc8a6622a156afeb7f43a31576d43fKenny Root            clearKeyStore(keyStore);
1223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1224e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
1225347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1226347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1227347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    setCertificate(keyStore);
12285ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1229347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1230347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1231347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1232347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setCertificate(keyStore);
1234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isCertificateEnabled(keyStore)) {
1239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1240e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1244347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
12456256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
12466256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1247347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isCaseSensitive(keyStore)) {
1248e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1249e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setCertificate(keyStore,
1251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               ALIAS_ALT_CASE_CERTIFICATE,
1252003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                               getPrivateKey2().getCertificate());
1253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1254e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setCertificate(keyStore,
1259e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               ALIAS_ALT_CASE_CERTIFICATE,
1260003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                               getPrivateKey2().getCertificate());
1261e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE));
1262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1265e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1266e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_deleteEntry() throws Exception {
1267e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1268e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(null);
12705ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1271e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1272e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1273e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1277e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1278347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1279347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1280347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.deleteEntry(null);
12815ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1282347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1283347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1284347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1285347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1286347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(null);
12905ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.deleteEntry("");
1298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1300e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1303347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1304347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1305347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.deleteEntry(ALIAS_PRIVATE);
1306347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException e) {
1307347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1308347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1309347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1310347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
13126256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
13136256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
13146256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
13156256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.deleteEntry(ALIAS_PRIVATE);
13166256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
13176256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
13186256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
13196256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
13206256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
13216256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.deleteEntry(ALIAS_NO_PASSWORD_PRIVATE);
13226256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
13236256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_SECRET);
1328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_SECRET);
1331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_CERTIFICATE);
1336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
1337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_CERTIFICATE);
1339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1344347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
13486256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
13496256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
13506256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.deleteEntry(ALIAS_ALT_CASE_PRIVATE);
13516256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
13526256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
13536256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
13546256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
13556256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.deleteEntry(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE);
13566256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
13576256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
1358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_ALT_CASE_SECRET);
1362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
1364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_SECRET);
1365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_ALT_CASE_CERTIFICATE);
1370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
1372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_CERTIFICATE);
1373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_aliases() throws Exception {
1379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1381e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.aliases();
13825ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1387e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
13896256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
13906256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNotNull("Should be able to query size: " + keyStore.getType(),
13916256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.aliases());
13926256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
13936256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have more than one alias already: " + keyStore.getType(),
13946256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.aliases().hasMoreElements());
1395347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
13966256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertEquals("Should have no aliases:" + keyStore.getType(), Collections.EMPTY_SET,
13970647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson                        new HashSet(Collections.list(keyStore.aliases())));
1398347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1403347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<String> expected = new HashSet<String>();
14056256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
14066256280922cc8a6622a156afeb7f43a31576d43fKenny Root                expected.add(ALIAS_PRIVATE);
14076256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_NO_PASSWORD_PRIVATE);
1410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_SECRET);
1413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore)) {
1414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    expected.add(ALIAS_NO_PASSWORD_SECRET);
1415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1416e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_CERTIFICATE);
1419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
14206256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
14216256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNotNull("Should be able to query size: " + keyStore.getType(),
14226256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.aliases());
14236256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
1424347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertTrue(keyStore.aliases().hasMoreElements());
1425347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
1426347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertEquals(expected, new HashSet<String>(Collections.list(keyStore.aliases())));
1427347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_containsAlias() throws Exception {
1432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.containsAlias(null);
14355ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.containsAlias(null);
14455ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1448e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1449e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.containsAlias(""));
1450e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1453e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1454347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.containsAlias(""));
1456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1457347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1458347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.containsAlias(ALIAS_PRIVATE));
1459347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1460347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
14616256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
14626256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue(keyStore.containsAlias(ALIAS_PRIVATE));
14636256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isNullPasswordAllowed(keyStore)) {
14646256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue(keyStore.containsAlias(ALIAS_NO_PASSWORD_PRIVATE));
14656256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore), keyStore.containsAlias(ALIAS_SECRET));
1467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isCertificateEnabled(keyStore), keyStore.containsAlias(ALIAS_CERTIFICATE));
1468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
1470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_PRIVATE));
1471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
1472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_SECRET));
1473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore),
1474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_CERTIFICATE));
1475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_size() throws Exception {
1479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.aliases();
14825ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
14896256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
14906256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should successfully query size: " + keyStore.getType(),
14916256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() >= 0);
14926256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
14936256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have non-empty store: " + keyStore.getType(),
14946256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() > 0);
1495347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
14966256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size());
1497347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1498e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1502347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (hasDefaultContents(keyStore)) {
15036256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have non-empty store: " + keyStore.getType(),
15046256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() > 0);
1505347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1506347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1507347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
15086256280922cc8a6622a156afeb7f43a31576d43fKenny Root            int expected = 0;
15096256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
15106256280922cc8a6622a156afeb7f43a31576d43fKenny Root                expected++;
15116256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1512e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1514e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1515e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1516e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore)) {
1518e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    expected++;
1519e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1520e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1521e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1522e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1523e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1524e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(expected, keyStore.size());
1525e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1526e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1527e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1528e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_isKeyEntry() throws Exception {
1529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1530e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1531e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isKeyEntry(null);
15325ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1533e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1534e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1535e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1536e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1537e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1540e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1541e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isKeyEntry(null);
15425ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1543e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1545e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1546e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(""));
1547e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1548e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1549e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1550e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1551e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1552347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            assertFalse(keyStore.isKeyEntry(""));
1553347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1554347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.isKeyEntry(ALIAS_PRIVATE));
1555347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1556347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
15576256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
15586256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue(keyStore.isKeyEntry(ALIAS_PRIVATE));
15596256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
15606256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
15616256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue(keyStore.isKeyEntry(ALIAS_NO_PASSWORD_PRIVATE));
15626256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1563e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore), keyStore.isKeyEntry(ALIAS_SECRET));
1564e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(ALIAS_CERTIFICATE));
1565e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1566e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
1567e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isKeyEntry(ALIAS_ALT_CASE_PRIVATE));
1568e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
1569e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isKeyEntry(ALIAS_ALT_CASE_SECRET));
1570e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(ALIAS_ALT_CASE_CERTIFICATE));
1571e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1572e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1573e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1574e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_isCertificateEntry() throws Exception {
1575e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1576e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1577e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isCertificateEntry(null);
15785ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1579e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1580e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1581e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1583e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1586e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.isCertificateEntry(null);
15895ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1590e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (NullPointerException expected) {
1591e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1592e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertFalse(keyStore.isCertificateEntry(null));
1594e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1595e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1596e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(""));
1597e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1598e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1599e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1600e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1601347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1602e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(""));
1603e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
16046256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
16056256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertFalse(keyStore.isCertificateEntry(ALIAS_PRIVATE));
16066256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
16076256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
16086256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertFalse(keyStore.isCertificateEntry(ALIAS_NO_PASSWORD_PRIVATE));
16096256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1610e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_SECRET));
1611347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            assertEquals(isCertificateEnabled(keyStore) && !isReadOnly(keyStore),
16120647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson                    keyStore.isCertificateEntry(ALIAS_CERTIFICATE));
1613e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1614e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_PRIVATE));
1615e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_SECRET));
1616347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            assertEquals(!isCaseSensitive(keyStore)
16170647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson                    && isCertificateEnabled(keyStore)
16180647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson                    && !isReadOnly(keyStore),
16190647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson                    keyStore.isCertificateEntry(ALIAS_ALT_CASE_CERTIFICATE));
1620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1621e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1622e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1623e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificateAlias() throws Exception {
1624e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateAlias(null);
16275ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1633e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificateAlias(null));
1635e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1638e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1639347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1640e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<String> expected = new HashSet<String>();
16416256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
16426256280922cc8a6622a156afeb7f43a31576d43fKenny Root                expected.add(ALIAS_PRIVATE);
16436256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1645e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_NO_PASSWORD_PRIVATE);
1646e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1647e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_CERTIFICATE);
1649e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1650003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            String actual = keyStore.getCertificateAlias(getPrivateKey().getCertificate());
1651347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            assertEquals(!isReadOnly(keyStore), expected.contains(actual));
1652003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            assertNull(keyStore.getCertificateAlias(getPrivateKey2().getCertificate()));
1653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(File expected, char[] storePassword, KeyStore actual)
1657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1658e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(actual.getType(), actual.getProvider());
165957f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        InputStream is = new FileInputStream(expected);
166057f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        ks.load(is, storePassword);
166157f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        is.close();
1662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEqualsKeyStores(ks, actual);
1663e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1664e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1665e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(KeyStore expected,
1666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      ByteArrayOutputStream actual, char[] storePassword)
1667e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1668e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(expected.getType(), expected.getProvider());
1669e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.load(new ByteArrayInputStream(actual.toByteArray()), storePassword);
1670e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEqualsKeyStores(expected, ks);
1671e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1672e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1673e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(KeyStore expected, KeyStore actual)
1674e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1675e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(expected.size(), actual.size());
1676e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (String alias : Collections.list(actual.aliases())) {
1677e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (alias.equals(ALIAS_NO_PASSWORD_PRIVATE)
1678e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    || alias.equals(ALIAS_NO_PASSWORD_SECRET)) {
1679e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(expected.getKey(alias, null),
1680e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                             actual.getKey(alias, null));
1681e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1682e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(expected.getKey(alias, PASSWORD_KEY),
1683e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                             actual.getKey(alias, PASSWORD_KEY));
1684e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1685e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(expected.getCertificate(alias), actual.getCertificate(alias));
1686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1687e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_store_OutputStream() throws Exception {
1690e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1691e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null, null);
16935ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1694e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1698e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1699e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
17016256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
1702347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1703347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.store(out, null);
17045ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1705347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1706347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1707347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1708347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1709347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1711e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(out, null);
1712e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(keyStore, out, null);
1713347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1714347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1715347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1716347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            try {
1717347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                keyStore.store(out, null);
17185ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1719347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } catch (Exception e) {
1720347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                if (e.getClass() != IllegalArgumentException.class
1721347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    && e.getClass() != NullPointerException.class) {
1722347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    throw e;
1723e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1724e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1725e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1727e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1728e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1729347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1730e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
17316256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
1732347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1733347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.store(out, null);
17345ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
17356256280922cc8a6622a156afeb7f43a31576d43fKenny Root                } catch (UnsupportedOperationException expected) {
1736347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1737347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isNullPasswordAllowed(keyStore)) {
1738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(out, null);
1739e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(keyStore, out, null);
1740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1742e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.store(out, null);
17435ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != IllegalArgumentException.class
1746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != NullPointerException.class) {
1747e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1749e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1750e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1751e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1753e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1754e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1755e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
17566256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
1757347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1758347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.store(out, PASSWORD_STORE);
17595ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1760347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException e) {
1761347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1762347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1763347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1764e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.store(out, PASSWORD_STORE);
1765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEqualsKeyStores(keyStore, out, PASSWORD_STORE);
1766e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1767e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1768e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1769e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
17716256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
1772347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1773347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.store(out, PASSWORD_STORE);
17745ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1775347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException e) {
1776347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1777347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1778347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1779e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.store(out, PASSWORD_STORE);
1780e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEqualsKeyStores(keyStore, out, PASSWORD_STORE);
1781e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1782e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1783e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1784e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_store_LoadStoreParameter() throws Exception {
1785e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1786e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1787e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null);
17885ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1789e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1790e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1791e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1792e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1793e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1794e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1795e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1796e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null);
17975ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1798e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (UnsupportedOperationException expected) {
179946c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom                assertFalse(isLoadStoreParameterSupported(keyStore));
180046c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom            } catch (IllegalArgumentException expected) {
180146c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom                // its supported, but null causes an exception
180246c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom                assertTrue(isLoadStoreParameterSupported(keyStore));
1803e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1804e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1805e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1806e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1807e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_load_InputStream() throws Exception {
1808e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1809e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
18106256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
18116256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should be able to query size: " + keyStore.getType(),
18126256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() >= 0);
18136256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
18146256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have non-empty store: " + keyStore.getType(),
18156256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() > 0);
1816347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
18176256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size());
1818347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1819e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1820e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1821e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
18226256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore)) {
18236256280922cc8a6622a156afeb7f43a31576d43fKenny Root                continue;
18246256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1825e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, PASSWORD_STORE);
18266256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
18276256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should be able to query size: " + keyStore.getType(),
18286256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() >= 0);
18296256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
18306256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have non-empty store: " + keyStore.getType(),
18316256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() > 0);
1832347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
18336256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size());
1834347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1836e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // test_KeyStore_store_OutputStream effectively tests load as well as store
1838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_load_LoadStoreParameter() throws Exception {
1841e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1842e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null);
18436256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
18446256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should be able to query size: " + keyStore.getType(),
18456256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() >= 0);
18466256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
18476256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have non-empty store: " + keyStore.getType(),
18486256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() > 0);
1849347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
18506256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size());
1851347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1852e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1853e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1854e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1855e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1856e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.load(new LoadStoreParameter() {
1857e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        public ProtectionParameter getProtectionParameter() {
1858e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            return null;
1859e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        }
1860e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    });
18615ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1862e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (UnsupportedOperationException expected) {
1863e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1864e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1865e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1866e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1867e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getEntry() throws Exception {
1868e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1869e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1870e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, null);
18715ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1872e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1873e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1874e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1875e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1876e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1877e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1878e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1879e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1880e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1881e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, null);
18825ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1883e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1884e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1885e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1886e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, PARAM_KEY);
18875ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1888e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1889e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1890e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getEntry("", null));
1891e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getEntry("", PARAM_KEY));
1892e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1893e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
1894347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1895347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY));
1896e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
18976256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
18986256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY));
18996256280922cc8a6622a156afeb7f43a31576d43fKenny Root                } else if (isNullPasswordAllowed(keyStore)) {
19006256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
19016256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
1902347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1903347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertSecretKey(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
1904347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } else {
1905347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
1906347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1907347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                if (isCertificateEnabled(keyStore)) {
1908347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertCertificate(keyStore.getEntry(ALIAS_CERTIFICATE, null));
1909347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } else {
1910347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertNull(keyStore.getEntry(ALIAS_CERTIFICATE, null));
1911347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1912e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1913e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1914e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1915347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) {
1916e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
1917e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
1918e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1919e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
1920e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1921e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
1922e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1923e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1924347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) {
1925e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
1926e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1927e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1928e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
1929e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1930e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1931e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1932e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
1933347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1934347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
1935347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isNullPasswordAllowed(keyStore)) {
1936e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
19376256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
1938e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, null));
19396256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordIgnored(keyStore)) {
1940e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1941e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_PRIVATE, null);
19425ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1943e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1944e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
1945e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
1946e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1947e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1948e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1949e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1950347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1951347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getEntry(ALIAS_SECRET, null));
1952347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isSecretKeyEnabled(keyStore)) {
1953e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1954e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_SECRET, null);
19555ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1956e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1957e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
1958e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
1959e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1960e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1961e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1962e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1963e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1964e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with bad passwords
1965347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1966347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD));
19676256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
1968e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD));
19696256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore)) {
1970e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1971e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD);
19725ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1973e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
1974e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1975e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1976347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1977347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_BAD));
1978347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isSecretKeyEnabled(keyStore)) {
1979e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1980e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_SECRET, PARAM_BAD);
19815ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1982e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
1983e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1984e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1985e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1986e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1987e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1988a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root    public static class FakeProtectionParameter implements ProtectionParameter {
1989a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root    }
1990a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root
1991e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setEntry() throws Exception {
1992e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1993e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1994e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1995e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, null);
19965ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1997e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1998e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1999e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2000e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2001e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2002e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
2003e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2004a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root            try {
2005a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), new FakeProtectionParameter());
20066256280922cc8a6622a156afeb7f43a31576d43fKenny Root                fail("Should not accept unknown ProtectionParameter: " + keyStore.getProvider());
2007a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root            } catch (KeyStoreException expected) {
2008a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root            }
2009a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root        }
2010a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root
2011a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root        for (KeyStore keyStore : keyStores()) {
2012a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root            keyStore.load(null, null);
2013a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root
2014e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
2015e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2016e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, null);
20175ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2018e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
2019e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
2020e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
2021e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
2022e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2023e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2024e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2025e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, PARAM_KEY);
20265ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2027e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
2028e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
2029e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
2030e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
2031e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2032e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2033e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2034e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry("", null, PARAM_KEY);
20355ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2036e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2037e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2038e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2039e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2040e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
20416256280922cc8a6622a156afeb7f43a31576d43fKenny Root            clearKeyStore(keyStore);
2042e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2043e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
2044e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
2045347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
2046347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
2047003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY);
20485ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
2049347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
2050347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
2051347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
2052347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
20536256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
20546256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY);
20556256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
20566256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
20576256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
20586256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
20596256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.setEntry(ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey(), null);
20606256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
20616256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
20626256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
2063e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
2064e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2065003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY);
2066e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2067e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
2068e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
2069003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null);
20705ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
2071e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
2072e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2073e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2074e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
2075e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
2076e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_CERTIFICATE,
2077003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                  new TrustedCertificateEntry(getPrivateKey().getCertificate()),
2078e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                  null);
2079e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
2080e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
2081e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
2082e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_CERTIFICATE,
2083003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new TrustedCertificateEntry(getPrivateKey().getCertificate()),
2084e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
20855ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
2086e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
2087e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2088e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
20896256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
20906256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.setEntry(ALIAS_UNICODE_PRIVATE, getPrivateKey(), PARAM_KEY);
20916256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_UNICODE_PRIVATE, PASSWORD_KEY));
20926256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_UNICODE_PRIVATE));
20936256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
20946256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
20956256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.setEntry(ALIAS_UNICODE_NO_PASSWORD_PRIVATE, getPrivateKey(), null);
20966256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_UNICODE_NO_PASSWORD_PRIVATE, null));
20976256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore
20986256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        .getCertificateChain(ALIAS_UNICODE_NO_PASSWORD_PRIVATE));
20996256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
21003d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root            if (isSecretKeyEnabled(keyStore)) {
21013d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                assertNull(keyStore.getKey(ALIAS_UNICODE_SECRET, PASSWORD_KEY));
21023d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                keyStore.setEntry(ALIAS_UNICODE_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY);
21033d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                assertSecretKey(keyStore.getKey(ALIAS_UNICODE_SECRET, PASSWORD_KEY));
21043d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root            } else {
21053d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                try {
21063d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                    keyStore.setKeyEntry(ALIAS_UNICODE_SECRET, getSecretKey(), PASSWORD_KEY, null);
21075ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
21083d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                } catch (KeyStoreException expected) {
21093d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                }
21103d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root            }
2111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2113e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2114e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
2115e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2116347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
2117347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
2118347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
2119347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2120347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
2121347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isCaseSensitive(keyStore)) {
21226256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
21236256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
21246256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
21256256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY);
21266256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
21276256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
21286256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
21296256280922cc8a6622a156afeb7f43a31576d43fKenny Root
21306256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
21316256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
21326256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
21336256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.setEntry(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, getPrivateKey2(), null);
21346256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
21356256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
21366256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
2137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
2139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2140e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
2141e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_SECRET,
2142003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new SecretKeyEntry(getSecretKey2()),
2143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
2144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
2146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2147e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
2149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
2150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
2151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE,
2152003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new TrustedCertificateEntry(
2153003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                              getPrivateKey2().getCertificate()),
2154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
2155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
2156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
21573d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                    keyStore.setEntry(ALIAS_UNICODE_CERTIFICATE,
21583d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                      new TrustedCertificateEntry(
21593d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                              getPrivateKey().getCertificate()),
21603d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                      null);
21613d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                    assertCertificate(keyStore.getCertificate(ALIAS_UNICODE_CERTIFICATE));
2162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
2164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
2165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
2166003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY);
2167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
2168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
2169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
2171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
2173e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_SECRET,
2174003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new SecretKeyEntry(getSecretKey2()),
2175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
2176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
2178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
2181e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
2182e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
2183e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE,
2184003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new TrustedCertificateEntry(
2185003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                              getPrivateKey2().getCertificate()),
2186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
2187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE));
2188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
21893d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                    keyStore.setEntry(ALIAS_UNICODE_CERTIFICATE,
21903d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                      new TrustedCertificateEntry(
21913d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                              getPrivateKey().getCertificate()),
21923d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                      null);
21933d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                    assertCertificate(keyStore.getCertificate(ALIAS_UNICODE_CERTIFICATE));
2194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2196e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2197e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2198e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2199e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
2200e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2201e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null/non-null passwords
22025ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            if (isReadOnly(keyStore)) {
22035ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                try {
22045ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null);
22055ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
22065ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                } catch (UnsupportedOperationException expected) {
2207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2208e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
2209003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null);
22105ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
22115ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                } catch (UnsupportedOperationException expected) {
22125ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                }
22135ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                try {
22145ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    keyStore.setEntry(ALIAS_CERTIFICATE,
22155ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                                      new TrustedCertificateEntry(getPrivateKey().getCertificate()),
22165ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                                      null);
22175ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
22185ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                } catch (UnsupportedOperationException expected) {
22195ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                }
22205ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                continue;
22215ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            }
22225ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
22231b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root                for (String keyType : KEY_TYPES) {
22241b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root                    keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(keyType), null);
22251b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root                    assertPrivateKey(keyType, keyStore.getKey(ALIAS_PRIVATE, null));
22261b6a2f56cf25fcbd0e2311be58d8577565622cf3Kenny Root                }
22275ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            } else {
22285ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                try {
22295ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null);
22305ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
2231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
2232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
2233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class
2234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != KeyStoreException.class) {
2235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
2236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
2237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
22395ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
22405ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
22415ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null);
22425ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, null));
22435ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                } else {
22445ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    try {
22455ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null);
22465ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        fail(keyStore.getType());
22475ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    } catch (Exception e) {
22485ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        if (e.getClass() != UnrecoverableKeyException.class
22495ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                            && e.getClass() != IllegalArgumentException.class
22505ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                            && e.getClass() != KeyStoreException.class) {
22515ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                            throw e;
22525ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        }
22535ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    }
2254347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
2255347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
2256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
2257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
2258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_CERTIFICATE,
2259003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new TrustedCertificateEntry(getPrivateKey().getCertificate()),
2260e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
2261e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
2262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
2263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    try {
2264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        keyStore.setEntry(ALIAS_CERTIFICATE,
2265003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                          new TrustedCertificateEntry(
2266003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                                  getPrivateKey().getCertificate()),
2267e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                          PARAM_KEY);
22685ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        fail(keyStore.getType());
2269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    } catch (KeyStoreException expected) {
2270e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
2271e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2272e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2273e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
2275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_entryInstanceOf() throws Exception {
2277e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2278e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2279e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, null);
22805ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2284e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2285e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
2287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, null);
22905ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, Entry.class);
22955ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf("", null);
23005ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", Entry.class));
2305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2307e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
2309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2310e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
2311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", Entry.class));
2312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", PrivateKeyEntry.class));
2313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", SecretKeyEntry.class));
2314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", TrustedCertificateEntry.class));
2315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2316347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
2317347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, PrivateKeyEntry.class));
2318347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, SecretKeyEntry.class));
2319347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, TrustedCertificateEntry.class));
2320347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2321347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, SecretKeyEntry.class));
2322347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, PrivateKeyEntry.class));
2323347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, TrustedCertificateEntry.class));
2324347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2325347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE,
2326347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                                                     TrustedCertificateEntry.class));
2327347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, PrivateKeyEntry.class));
2328347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, SecretKeyEntry.class));
2329347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
2330347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
2331347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
23336256280922cc8a6622a156afeb7f43a31576d43fKenny Root            assertEquals(isKeyPasswordSupported(keyStore),
23346256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.entryInstanceOf(ALIAS_PRIVATE, PrivateKeyEntry.class));
2335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, SecretKeyEntry.class));
2336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, TrustedCertificateEntry.class));
2337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
23386256280922cc8a6622a156afeb7f43a31576d43fKenny Root            assertEquals(isNullPasswordAllowed(keyStore),
23396256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.entryInstanceOf(ALIAS_NO_PASSWORD_PRIVATE, PrivateKeyEntry.class));
23406256280922cc8a6622a156afeb7f43a31576d43fKenny Root            assertFalse(keyStore.entryInstanceOf(ALIAS_NO_PASSWORD_PRIVATE, SecretKeyEntry.class));
23416256280922cc8a6622a156afeb7f43a31576d43fKenny Root            assertFalse(keyStore.entryInstanceOf(ALIAS_NO_PASSWORD_PRIVATE,
23426256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    TrustedCertificateEntry.class));
23436256280922cc8a6622a156afeb7f43a31576d43fKenny Root
2344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore),
2345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_SECRET, SecretKeyEntry.class));
2346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, PrivateKeyEntry.class));
2347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, TrustedCertificateEntry.class));
2348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isCertificateEnabled(keyStore),
2350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_CERTIFICATE,
2351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  TrustedCertificateEntry.class));
2352e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, PrivateKeyEntry.class));
2353e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, SecretKeyEntry.class));
2354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
2356e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
2357e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, PrivateKeyEntry.class));
2358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, SecretKeyEntry.class));
2359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE,
2360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 TrustedCertificateEntry.class));
2361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
2363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, SecretKeyEntry.class));
2364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, PrivateKeyEntry.class));
2365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET,
2366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 TrustedCertificateEntry.class));
2367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore),
2369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE,
2370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  TrustedCertificateEntry.class));
2371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE,
2372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 PrivateKeyEntry.class));
2373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE, SecretKeyEntry.class));
2374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
2376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_Builder() throws Exception {
2378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
2380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2381e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore, null);
23825ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2387e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore.getType(),
2390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    keyStore.getProvider(),
2391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
23925ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2397e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(null,
2400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
2401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
2402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
24035ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore.getType(),
2408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    keyStore.getProvider(),
2409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
2410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
24115ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2416e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
2418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Builder builder = Builder.newInstance(keyStore, PARAM_STORE);
2419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2420e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                builder.getProtectionParameter(null);
24215ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore, builder.getKeyStore());
2425e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                builder.getProtectionParameter(null);
24275ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
2431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
2435347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            File file = File.createTempFile("keystore", keyStore.getProvider().getName());
2437347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            OutputStream os = null;
2438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2439347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                os = new FileOutputStream(file);
24406256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
2441347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    try {
2442347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                        keyStore.store(os, PASSWORD_STORE);
24435ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        fail(keyStore.getType());
2444347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    } catch (UnsupportedOperationException expected) {
2445347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    }
2446347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    continue;
2447347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
2448347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
244957f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom                keyStore.store(os, PASSWORD_STORE);
245057f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom                os.close();
2451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder builder = Builder.newInstance(keyStore.getType(),
2452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      keyStore.getProvider(),
2453e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      file,
2454e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      PARAM_STORE);
2455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(keyStore.getType(), builder.getKeyStore().getType());
2456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider());
2457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
2458e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(file, PASSWORD_STORE, keyStore);
2459e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } finally {
2460a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                try {
2461a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                    if (os != null) {
2462a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                        os.close();
2463a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                    }
2464a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                } catch (IOException ignored) {
2465a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                }
2466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                file.delete();
2467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
24716256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore)) {
24726256280922cc8a6622a156afeb7f43a31576d43fKenny Root                continue;
24736256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
2474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Builder builder = Builder.newInstance(keyStore.getType(),
2475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  keyStore.getProvider(),
2476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  PARAM_STORE);
2477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore.getType(), builder.getKeyStore().getType());
2478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider());
2479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
2480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
2482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2483347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom    public void test_KeyStore_cacerts() throws Exception {
2484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (StandardNames.IS_RI) {
2485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            return;
2486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2487347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        KeyStore ks = KeyStore.getInstance("AndroidCAStore");
2488347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        assertEquals("AndroidCAStore", ks.getType());
2489347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        assertEquals("HarmonyJSSE", ks.getProvider().getName());
2490347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2491347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        ks.load(null, null);
2492e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (String alias : Collections.list(ks.aliases())) {
2493347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            Certificate c = null;
2494cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom            try {
2495347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                c = ks.getCertificate(alias);
2496347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNotNull(c);
2497347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertTrue(ks.isCertificateEntry(alias));
2498347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertTrue(ks.entryInstanceOf(alias, TrustedCertificateEntry.class));
2499347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertEquals(alias, ks.getCertificateAlias(c));
2500347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2501cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                assertTrue(c instanceof X509Certificate);
2502cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                X509Certificate cert = (X509Certificate) c;
2503cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                assertEquals(cert.getSubjectUniqueID(), cert.getIssuerUniqueID());
2504cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                assertNotNull(cert.getPublicKey());
2505347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2506347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertTrue(ks.containsAlias(alias));
2507347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNotNull(ks.getCreationDate(alias));
2508347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNotNull(ks.getEntry(alias, null));
2509347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2510347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(ks.isKeyEntry(alias));
2511347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(ks.getKey(alias, null));
2512347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(ks.getCertificateChain(alias));
2513347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2514347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } catch (Throwable t) {
2515347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                throw new Exception("alias=" + alias + " cert=" + c, t);
2516cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom            }
2517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2518e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
25190647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson
25200647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson    // http://b/857840: want JKS key store
25210647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson    public void testDefaultKeystore() {
25220647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        String type = KeyStore.getDefaultType();
2523a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root        assertEquals(StandardNames.KEY_STORE_ALGORITHM, type);
25240647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson
25250647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        try {
25260647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson            KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
25270647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson            assertNotNull("Keystore must not be null", store);
25280647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        } catch (Exception ex) {
25290647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson            throw new RuntimeException(ex);
25300647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        }
25310647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson
25320647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        try {
2533a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root            KeyStore store = KeyStore.getInstance(StandardNames.KEY_STORE_ALGORITHM);
25340647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson            assertNotNull("Keystore must not be null", store);
25350647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        } catch (Exception ex) {
25360647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson            throw new RuntimeException(ex);
25370647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        }
25380647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson    }
2539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom}
2540