KeyStoreTest.java revision 57f2cc03ff2cf5d2f6413c5410680b4908d7301d
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;
2428192ac5dbb128c63d914fab324d15757fe98fdaJesse Wilsonimport java.io.InputStream;
2557f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstromimport java.io.OutputStream;
264557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Key;
274557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.KeyStore;
28e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.Builder;
29e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.Entry;
30e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.LoadStoreParameter;
31e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.PasswordProtection;
32e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.PrivateKeyEntry;
33e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.ProtectionParameter;
34e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.SecretKeyEntry;
35e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.TrustedCertificateEntry;
364557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.KeyStoreException;
374557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.NoSuchAlgorithmException;
384557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Provider;
394557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Security;
404557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.UnrecoverableKeyException;
41e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.cert.Certificate;
42e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.cert.X509Certificate;
43e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.ArrayList;
44e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Arrays;
45e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Collections;
46e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Date;
47e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.HashSet;
48e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.List;
49e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Set;
50e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.KeyGenerator;
51e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.SecretKey;
52e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport junit.framework.TestCase;
53e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
54e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrompublic class KeyStoreTest extends TestCase {
55e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
56e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final PrivateKeyEntry PRIVATE_KEY
57a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom            = TestKeyStore.getServer().getPrivateKey("RSA");
58e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final PrivateKeyEntry PRIVATE_KEY_2
59a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom            = TestKeyStore.getClientCertificate().getPrivateKey("RSA");
60e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final SecretKey SECRET_KEY = generateSecretKey();
61e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final SecretKey SECRET_KEY_2 = generateSecretKey();
62e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
63e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static SecretKey generateSecretKey() {
64e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
65e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyGenerator kg = KeyGenerator.getInstance("DES");
66e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            return kg.generateKey();
67e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (NoSuchAlgorithmException e) {
68e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throw new RuntimeException(e);
69e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
70e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
71e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
72e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_PRIVATE = "private";
73e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_CERTIFICATE = "certificate";
74e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_SECRET = "secret";
75e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
76e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_PRIVATE = "pRiVaTe";
77e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_CERTIFICATE = "cErTiFiCaTe";
78e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_SECRET = "sEcRet";
79e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
80e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_NO_PASSWORD_PRIVATE = "private-no-password";
81e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_NO_PASSWORD_SECRET = "secret-no-password";
82e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
83e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_STORE = "store password".toCharArray();
84e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_KEY = "key password".toCharArray();
85e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_BAD = "dummy".toCharArray();
86e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
87e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_STORE = new PasswordProtection(PASSWORD_STORE);
88e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_KEY = new PasswordProtection(PASSWORD_KEY);
89e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_BAD = new PasswordProtection(PASSWORD_BAD);
90e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
91a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom    public static List<KeyStore> keyStores() throws Exception {
92e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        List<KeyStore> keyStores = new ArrayList<KeyStore>();
93e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider[] providers = Security.getProviders();
94e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (Provider provider : providers) {
95e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<Provider.Service> services = provider.getServices();
96e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            for (Provider.Service service : services) {
97e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String type = service.getType();
98e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!type.equals("KeyStore")) {
99e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    continue;
100e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
101e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String algorithm = service.getAlgorithm();
102e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                KeyStore ks = KeyStore.getInstance(algorithm, provider);
103e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(provider, ks.getProvider());
104e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(algorithm, ks.getType());
105e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!isUnsupported(ks)) {
106e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStores.add(ks);
107e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
108e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
109e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
110e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return keyStores;
111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
113e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isSecretKeyEnabled(KeyStore ks) {
114e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // JKS key stores cannot store secret keys, neither can the RI's PKCS12
115e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("JKS")
116e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("CaseExactJKS")
117e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || (ks.getType().equals("PKCS12"))));
118e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
119e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
120e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isCertificateEnabled(KeyStore ks) {
121e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // RI can't handle certificate in PKCS12, but BC can
122e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("SunJSSE")));
123e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isCaseSensitive(KeyStore ks) {
126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getType().equals("CaseExactJKS")
127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                || ks.getType().equals("BKS")
128e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                || ks.getType().equals("BouncyCastle"));
129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isUnsupported(KeyStore ks) {
133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // Don't bother testing BC on RI
134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (StandardNames.IS_RI && ks.getProvider().getName().equals("BC"));
135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isNullPasswordAllowed(KeyStore ks) {
138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("JKS")
139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("CaseExactJKS")
140e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("JCEKS")
141e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("PKCS12")));
142e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isKeyPasswordIgnored(KeyStore ks) {
145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // BouncyCastle's PKCS12 ignores the key password unlike the RI which requires it
146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC"));
147e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isSetKeyByteArrayUnimplemented(KeyStore ks) {
150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // All of BouncyCastle's
151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // KeyStore.setKeyEntry(String,byte[],char[]) implementations
152e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // throw RuntimeException
153e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getProvider().getName().equals("BC"));
154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void populate(KeyStore ks) throws Exception {
157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.load(null, null);
158e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKey(ks);
159e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isNullPasswordAllowed(ks)) {
160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ks.setKeyEntry(ALIAS_NO_PASSWORD_PRIVATE,
161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                           PRIVATE_KEY.getPrivateKey(),
162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                           null,
163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                           PRIVATE_KEY.getCertificateChain());
164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isCertificateEnabled(ks)) {
166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ks.setCertificateEntry(ALIAS_CERTIFICATE,
167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                   PRIVATE_KEY.getCertificate());
168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isSecretKeyEnabled(ks)) {
170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setSecretKey(ks);
171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(ks)) {
172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                ks.setKeyEntry(ALIAS_NO_PASSWORD_SECRET,
173e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               SECRET_KEY,
174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               null,
175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               null);
176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks) throws Exception {
181e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKey(ks, ALIAS_PRIVATE);
182e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
183e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks, String alias) throws Exception {
184e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKey(ks, alias, PRIVATE_KEY);
185e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks,
187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     String alias,
188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PrivateKeyEntry privateKey)
189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getPrivateKey(),
192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       PASSWORD_KEY,
193e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getCertificateChain());
194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
196e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks) throws Exception {
197e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKeyBytes(ks, ALIAS_PRIVATE);
198e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
199e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks, String alias) throws Exception {
200e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKeyBytes(ks, alias, PRIVATE_KEY);
201e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
202e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks,
203e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     String alias,
204e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PrivateKeyEntry privateKey)
205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
206e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getPrivateKey().getEncoded(),
208e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getCertificateChain());
209e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
211e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks) throws Exception {
212e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKey(ks, ALIAS_SECRET);
213e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
214e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks, String alias) throws Exception {
215e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKey(ks, alias, SECRET_KEY);
216e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks, String alias, SecretKey key) throws Exception {
218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       key,
220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       PASSWORD_KEY,
221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       null);
222e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
224e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks) throws Exception {
225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKeyBytes(ks, ALIAS_SECRET);
226e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
227e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks, String alias) throws Exception {
228e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKeyBytes(ks, alias, SECRET_KEY);
229e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks, String alias, SecretKey key)
231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       key.getEncoded(),
234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       null);
235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks) throws Exception {
238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setCertificate(ks, ALIAS_CERTIFICATE);
239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
240e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks, String alias) throws Exception {
241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setCertificate(ks, alias, PRIVATE_KEY.getCertificate());
242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks, String alias, Certificate certificate)
244e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setCertificateEntry(alias, certificate);
246e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
247e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
248e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey(Key actual)
249e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY.getPrivateKey(), actual);
251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
252e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey2(Key actual)
253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
254e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY_2.getPrivateKey(), actual);
255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey(Entry actual)
257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(PrivateKeyEntry.class, actual.getClass());
259e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        PrivateKeyEntry privateKey = (PrivateKeyEntry) actual;
260e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY.getPrivateKey(), privateKey.getPrivateKey());
261e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY.getCertificate(), privateKey.getCertificate());
262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(Arrays.asList(PRIVATE_KEY.getCertificateChain()),
263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     Arrays.asList(privateKey.getCertificateChain()));
264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
265e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
266e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey(Key actual)
267e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
268e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(SECRET_KEY, actual);
269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
270e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey2(Key actual)
271e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
272e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(SECRET_KEY_2, actual);
273e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey(Entry actual)
275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(SecretKeyEntry.class, actual.getClass());
277e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(SECRET_KEY, ((SecretKeyEntry) actual).getSecretKey());
278e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
279e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
280e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate(Certificate actual)
281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY.getCertificate(), actual);
283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
284e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate2(Certificate actual)
285e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY_2.getCertificate(), actual);
287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate(Entry actual)
289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(TrustedCertificateEntry.class, actual.getClass());
291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY.getCertificate(),
292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     ((TrustedCertificateEntry) actual).getTrustedCertificate());
293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificateChain(Certificate[] actual)
296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(Arrays.asList(PRIVATE_KEY.getCertificateChain()),
298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     Arrays.asList(actual));
299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
300e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_create() throws Exception {
302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider[] providers = Security.getProviders();
303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (Provider provider : providers) {
304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<Provider.Service> services = provider.getServices();
305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            for (Provider.Service service : services) {
306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String type = service.getType();
307e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!type.equals("KeyStore")) {
308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    continue;
309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
310e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String algorithm = service.getAlgorithm();
311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                KeyStore ks = KeyStore.getInstance(algorithm, provider);
312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(provider, ks.getProvider());
313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(algorithm, ks.getType());
314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getInstance() throws Exception {
319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null);
322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (NullPointerException expected) {
324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type));
327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String providerName = StandardNames.SECURITY_PROVIDER_NAME;
329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, (String)null);
331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, providerName);
336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (Exception e) {
338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (e.getClass() != NullPointerException.class
339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                && e.getClass() != KeyStoreException.class) {
340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                throw e;
341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(type, (String)null);
345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type, providerName));
349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider provider = Security.getProvider(providerName);
351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
352e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, (Provider)null);
353e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
356e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
357e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, provider);
358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (NullPointerException expected) {
360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(type, (Provider)null);
363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type, provider));
367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getDefaultType() throws Exception {
370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(type);
372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(type);
373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks);
374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(type, ks.getType());
375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getProvider() throws Exception {
378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks.getProvider());
380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(StandardNames.SECURITY_PROVIDER_NAME, ks.getProvider().getName());
381e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
382e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNotNull(keyStore.getProvider());
384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
387e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getType() throws Exception {
388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(type);
390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks.getType());
391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(type, ks.getType());
392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNotNull(keyStore.getType());
395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
397e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getKey() throws Exception {
399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, null);
402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
403e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, null);
413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
416e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
420e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
421e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, PASSWORD_KEY);
422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
425e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class
426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
427e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey("", null));
431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey("", PASSWORD_KEY));
432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
448e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
449e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
450e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
453e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isKeyPasswordIgnored(keyStore)) {
454e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_PRIVATE, null);
458e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
459e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
460e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
461e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
462e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
463e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
464e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
465e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_SECRET, null);
469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with bad passwords
479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isKeyPasswordIgnored(keyStore)) {
480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_PRIVATE, PASSWORD_BAD);
484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
490e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_SECRET, PASSWORD_BAD);
491e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
492e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
493e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
494e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
495e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
496e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
497e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
498e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificateChain() throws Exception {
499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateChain(null);
502e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
503e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
504e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
505e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
506e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
507e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
508e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
509e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
510e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
511e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateChain(null);
512e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
514e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
515e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
516e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
518e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
519e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificateChain(""));
520e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
521e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
522e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
523e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
524e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
525e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
526e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE));
527e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
528e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificateChain(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE));
529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
530e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
531e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
532e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
533e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificate() throws Exception {
534e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
535e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
536e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificate(null);
537e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
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.getCertificate(null);
547e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
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            assertNull(keyStore.getCertificate(""));
555e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
556e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
557e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
558e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
559e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
560e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
561e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
562e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
563e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
564e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
565e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
566e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
567e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
568e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
569e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
570e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
571e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
572e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
573e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
574e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCreationDate() throws Exception {
575e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
576e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
577a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                keyStore.getCreationDate(null);
578a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                fail();
579e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
580e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
581e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        long before = System.currentTimeMillis();
583e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // add 1000 since some key stores round of time to nearest second
585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            long after = System.currentTimeMillis() + 1000;
586e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
589e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
590e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCreationDate(null);
591e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
592e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
594e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCreationDate(""));
595e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
596e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
597e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
598e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Date date = keyStore.getCreationDate(ALIAS_CERTIFICATE);
599e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNotNull(date);
600e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertTrue(before <= date.getTime());
601e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertTrue(date.getTime() <= after);
602e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
603e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCreationDate(ALIAS_CERTIFICATE));
604e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
605e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
606e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
607e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
608e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE));
609e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
610e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
611e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    Date date = keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE);
612e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(before <= date.getTime());
613e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(date.getTime() <= after);
614e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
615e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
616e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
617e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
619e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setKeyEntry_Key() throws Exception {
620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
621e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
622e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null, null);
623e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
624e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
627e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
633e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null, null);
634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
635e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
638e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
639e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
640e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
641e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
642e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, PASSWORD_KEY, null);
643e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
645e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
646e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
647e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
649e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
650e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(ALIAS_PRIVATE,
652e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PRIVATE_KEY.getPrivateKey(),
653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PASSWORD_KEY,
654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     null);
655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (IllegalArgumentException expected) {
657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
658e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
659e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
660e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
661e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
663e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
664e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
665e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setPrivateKey(keyStore);
666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
667e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
668e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
669e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
670e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setSecretKey(keyStore);
671e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
672e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
673e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
674e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, PASSWORD_KEY, null);
675e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
676e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
677e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != KeyStoreException.class
678e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != NullPointerException.class) {
679e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
680e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
681e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
682e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
683e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
684e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
685e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
687e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
690e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
691e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2);
693e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
694e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
698e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
699e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    setSecretKey(keyStore, ALIAS_ALT_CASE_SECRET, SECRET_KEY_2);
700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
702e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
703e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
704e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
705e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
706e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2);
707e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
708e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
709e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
711e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
712e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    setSecretKey(keyStore, ALIAS_ALT_CASE_PRIVATE, SECRET_KEY_2);
713e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
714e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
715e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
716e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
717e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
718e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
719e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
720e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
721e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
722e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
723e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
724e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(ALIAS_PRIVATE,
725e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PRIVATE_KEY.getPrivateKey(),
726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     null,
727e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PRIVATE_KEY.getCertificateChain());
728e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
729e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
730e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
731e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_PRIVATE,
732e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                         PRIVATE_KEY.getPrivateKey(),
733e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                         null,
734e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                         PRIVATE_KEY.getCertificateChain());
735e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
736e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
737e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class
739e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != KeyStoreException.class) {
740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
742e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
743e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, null, null);
747e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, null));
748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
749e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    try {
750e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, null, null);
751e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        fail();
752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    } catch (Exception e) {
753e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        if (e.getClass() != UnrecoverableKeyException.class
754e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            && e.getClass() != IllegalArgumentException.class
755e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            && e.getClass() != KeyStoreException.class) {
756e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            throw e;
757e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        }
758e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
759e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
760e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
761e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
762e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
763e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
764e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setKeyEntry_array() throws Exception {
765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
766e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
767e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null);
768e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
769e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
771e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
772e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
773e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
774e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
775e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
776e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
777e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
778e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null);
779e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
780e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
781e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
782e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class
783e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class
784e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != RuntimeException.class) {
785e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
786e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
787e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
788e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
789e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
790e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
791e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isNullPasswordAllowed(keyStore)) {
792e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // TODO Use EncryptedPrivateKeyInfo to protect keys if
793e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // password is required.
794e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
795e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
796e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSetKeyByteArrayUnimplemented(keyStore)) {
797e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
798e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
799e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
800e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
801e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
802e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
803e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
804e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setPrivateKeyBytes(keyStore);
805e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
806e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
807e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
808e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
809e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setSecretKeyBytes(keyStore);
810e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
811e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
812e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
813e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY.getEncoded(), null);
814e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
815e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
816e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
817e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
818e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
819e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
820e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
821e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isNullPasswordAllowed(keyStore)) {
822e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // TODO Use EncryptedPrivateKeyInfo to protect keys if
823e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // password is required.
824e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
825e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
826e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSetKeyByteArrayUnimplemented(keyStore)) {
827e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
828e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
829e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
830e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
831e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
832e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
833e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
834e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
836e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2);
837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
841e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
842e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
843e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, SECRET_KEY_2);
844e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
845e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
846e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
847e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
848e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
849e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
850e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2);
851e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
852e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
853e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
854e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
855e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
856e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
857e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, SECRET_KEY_2);
858e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
859e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
860e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
861e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
862e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
863e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
864e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
865e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setCertificateEntry() throws Exception {
866e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
867e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
868e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setCertificateEntry(null, null);
869e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
870e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
871e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
872e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
873e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
874e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
875e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
876e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
877e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
878e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setCertificateEntry(null, null);
879e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
880e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
881e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
882e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
883e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
884e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
885e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
886e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
887e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // Sort of delete by setting null.  Note that even though
888e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // certificiate is null, size doesn't change,
889e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // isCertificateEntry returns true, and it is still listed in aliases.
890e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
891e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
892e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
893e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    int size = keyStore.size();
894e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null);
895e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
896e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertEquals(size, keyStore.size());
897e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(keyStore.isCertificateEntry(ALIAS_CERTIFICATE));
898e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(Collections.list(keyStore.aliases()).contains(ALIAS_CERTIFICATE));
899e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (NullPointerException expectedSometimes) {
900e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertEquals("PKCS12", keyStore.getType());
901e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertEquals("BC", keyStore.getProvider().getName());
902e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
903e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
904e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
905e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null);
906a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                    fail();
907e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
908e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
909e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
910e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
911e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
912e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
913e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isCertificateEnabled(keyStore)) {
914e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
915e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
916e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
917e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
918e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
919e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
920e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
921e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setCertificate(keyStore);
922e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
923e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
924e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
925e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
926e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isCertificateEnabled(keyStore)) {
927e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
928e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
929e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
930e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
931e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
932e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
933e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
934e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
935e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
936e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setCertificate(keyStore,
937e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               ALIAS_ALT_CASE_CERTIFICATE,
938e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               PRIVATE_KEY_2.getCertificate());
939e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
940e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
941e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
942e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
943e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
944e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setCertificate(keyStore,
945e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               ALIAS_ALT_CASE_CERTIFICATE,
946e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               PRIVATE_KEY_2.getCertificate());
947e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE));
948e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
949e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
950e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
951e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
952e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_deleteEntry() throws Exception {
953e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
954e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
955e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(null);
956e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
957e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
958e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
959e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
960e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
961e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
962e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
963e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
964e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
965e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
966e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(null);
967e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
968e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
969e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
970e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
971e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
972e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
973e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
974e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.deleteEntry("");
975e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
976e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
977e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
978e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
979e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
980e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
981e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
982e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
983e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.deleteEntry(ALIAS_PRIVATE);
984e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
985e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
986e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
987e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
988e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_SECRET);
989e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
990e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
991e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_SECRET);
992e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
993e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
994e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
995e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
996e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_CERTIFICATE);
997e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
998e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
999e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_CERTIFICATE);
1000e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1001e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1002e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1003e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1004e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1005e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1006e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1007e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
1008e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1009e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_ALT_CASE_PRIVATE);
1010e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1011e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1012e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1013e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1014e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_ALT_CASE_SECRET);
1015e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1016e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
1017e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_SECRET);
1018e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1019e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1020e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1021e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1022e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_ALT_CASE_CERTIFICATE);
1023e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1024e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
1025e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_CERTIFICATE);
1026e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1027e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1028e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1029e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1030e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1031e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_aliases() throws Exception {
1032e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1033e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1034e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.aliases();
1035e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1036e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1037e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1038e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1039e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1040e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1041e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1042e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(Collections.EMPTY_SET, new HashSet(Collections.list(keyStore.aliases())));
1043e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1044e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1045e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1046e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1047e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<String> expected = new HashSet<String>();
1048e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            expected.add(ALIAS_PRIVATE);
1049e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1050e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_NO_PASSWORD_PRIVATE);
1051e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1052e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1053e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_SECRET);
1054e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore)) {
1055e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    expected.add(ALIAS_NO_PASSWORD_SECRET);
1056e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1057e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1058e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1059e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_CERTIFICATE);
1060e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1061e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(expected, new HashSet<String>(Collections.list(keyStore.aliases())));
1062e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1063e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1064e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1065e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_containsAlias() throws Exception {
1066e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1067e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1068e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.containsAlias(null);
1069e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1070e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1071e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1072e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1073e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1074e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1075e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1076e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1077e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1078e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.containsAlias(null);
1079e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1080e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1081e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1082e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1083e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.containsAlias(""));
1084e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1085e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1086e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1087e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1088e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.containsAlias(""));
1089e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1090e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertTrue(keyStore.containsAlias(ALIAS_PRIVATE));
1091e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore), keyStore.containsAlias(ALIAS_SECRET));
1092e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isCertificateEnabled(keyStore), keyStore.containsAlias(ALIAS_CERTIFICATE));
1093e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1094e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
1095e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_PRIVATE));
1096e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
1097e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_SECRET));
1098e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore),
1099e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_CERTIFICATE));
1100e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1101e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1102e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1103e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_size() throws Exception {
1104e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1105e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1106e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.aliases();
1107e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1108e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1109e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1110e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1113e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1114e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(0, keyStore.size());
1115e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1116e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1117e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1118e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1119e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            int expected = 1;
1120e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1121e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1122e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1123e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore)) {
1126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    expected++;
1127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1128e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(expected, keyStore.size());
1133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_isKeyEntry() throws Exception {
1137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isKeyEntry(null);
1140e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1141e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1142e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1147e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isKeyEntry(null);
1150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1152e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1153e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(""));
1155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1158e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1159e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(""));
1160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertTrue(keyStore.isKeyEntry(ALIAS_PRIVATE));
1162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore), keyStore.isKeyEntry(ALIAS_SECRET));
1163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(ALIAS_CERTIFICATE));
1164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
1166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isKeyEntry(ALIAS_ALT_CASE_PRIVATE));
1167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
1168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isKeyEntry(ALIAS_ALT_CASE_SECRET));
1169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(ALIAS_ALT_CASE_CERTIFICATE));
1170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1173e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_isCertificateEntry() throws Exception {
1174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isCertificateEntry(null);
1177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1181e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1182e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1183e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1184e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1185e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.isCertificateEntry(null);
1188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (NullPointerException expected) {
1190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertFalse(keyStore.isCertificateEntry(null));
1193e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(""));
1196e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1197e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1198e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1199e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1200e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(""));
1201e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1202e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_PRIVATE));
1203e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_SECRET));
1204e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isCertificateEnabled(keyStore),
1205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isCertificateEntry(ALIAS_CERTIFICATE));
1206e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_PRIVATE));
1208e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_SECRET));
1209e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore),
1210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isCertificateEntry(ALIAS_ALT_CASE_CERTIFICATE));
1211e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1212e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1213e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1214e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificateAlias() throws Exception {
1215e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1216e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateAlias(null);
1218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1222e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1224e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificateAlias(null));
1226e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1227e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1228e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1229e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<String> expected = new HashSet<String>();
1231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            expected.add(ALIAS_PRIVATE);
1232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_NO_PASSWORD_PRIVATE);
1234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_CERTIFICATE);
1237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            String actual = keyStore.getCertificateAlias(PRIVATE_KEY.getCertificate());
1239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertTrue(expected.contains(actual));
1240e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificateAlias(PRIVATE_KEY_2.getCertificate()));
1241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1244e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(File expected, char[] storePassword, KeyStore actual)
1245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1246e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(actual.getType(), actual.getProvider());
124757f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        InputStream is = new FileInputStream(expected);
124857f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        ks.load(is, storePassword);
124957f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        is.close();
1250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEqualsKeyStores(ks, actual);
1251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1252e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(KeyStore expected,
1254e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      ByteArrayOutputStream actual, char[] storePassword)
1255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(expected.getType(), expected.getProvider());
1257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.load(new ByteArrayInputStream(actual.toByteArray()), storePassword);
1258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEqualsKeyStores(expected, ks);
1259e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1260e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1261e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(KeyStore expected, KeyStore actual)
1262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(expected.size(), actual.size());
1264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (String alias : Collections.list(actual.aliases())) {
1265e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (alias.equals(ALIAS_NO_PASSWORD_PRIVATE)
1266e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    || alias.equals(ALIAS_NO_PASSWORD_SECRET)) {
1267e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(expected.getKey(alias, null),
1268e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                             actual.getKey(alias, null));
1269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1270e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(expected.getKey(alias, PASSWORD_KEY),
1271e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                             actual.getKey(alias, PASSWORD_KEY));
1272e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1273e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(expected.getCertificate(alias), actual.getCertificate(alias));
1274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1277e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_store_OutputStream() throws Exception {
1278e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1279e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1280e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null, null);
1281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1284e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1285e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
1289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(out, null);
1291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(keyStore, out, null);
1292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.store(out, null);
1295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != IllegalArgumentException.class
1297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != NullPointerException.class) {
1298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1300e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
1307e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(out, null);
1309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(keyStore, out, null);
1310e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.store(out, null);
1313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != IllegalArgumentException.class
1315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != NullPointerException.class) {
1316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
1325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.store(out, PASSWORD_STORE);
1326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEqualsKeyStores(keyStore, out, PASSWORD_STORE);
1327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
1332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.store(out, PASSWORD_STORE);
1333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEqualsKeyStores(keyStore, out, PASSWORD_STORE);
1334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_store_LoadStoreParameter() throws Exception {
1338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null);
1341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null);
1350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (UnsupportedOperationException expected) {
1352e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1353e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1356e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_load_InputStream() throws Exception {
1357e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(0, keyStore.size());
1360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, PASSWORD_STORE);
1364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(0, keyStore.size());
1365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // test_KeyStore_store_OutputStream effectively tests load as well as store
1368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_load_LoadStoreParameter() throws Exception {
1371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null);
1373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(0, keyStore.size());
1374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.load(new LoadStoreParameter() {
1379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        public ProtectionParameter getProtectionParameter() {
1380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            return null;
1381e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        }
1382e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    });
1383a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                fail();
1384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (UnsupportedOperationException expected) {
1385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1387e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getEntry() throws Exception {
1390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, null);
1393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1397e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1403e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, null);
1404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, PARAM_KEY);
1409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getEntry("", null));
1413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getEntry("", PARAM_KEY));
1414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
1416e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY));
1417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
1419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1420e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
1421e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getEntry(ALIAS_CERTIFICATE, null));
1424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1425e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_CERTIFICATE, null));
1426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1427e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
1430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
1431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
1432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
1434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
1436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
1439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
1440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
1443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
1447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1448e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
1449e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else if (isKeyPasswordIgnored(keyStore)) {
1450e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, null));
1451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1453e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_PRIVATE, null);
1454e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
1457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
1458e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1459e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1460e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1461e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1462e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1463e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1464e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_SECRET, null);
1465e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
1468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
1469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with bad passwords
1475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isKeyPasswordIgnored(keyStore)) {
1476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD));
1477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD);
1480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
1482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_SECRET, PARAM_BAD);
1487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
1489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1490e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1491e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1492e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1493e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1494e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setEntry() throws Exception {
1495e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1496e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1497e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1498e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, null);
1499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1502e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1503e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1504e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1505e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1506e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1507e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1508e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1509e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, null);
1510e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1511e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1512e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1514e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1515e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1516e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1518e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, PARAM_KEY);
1519e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1520e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1521e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1522e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1523e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1524e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1525e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1526e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1527e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry("", null, PARAM_KEY);
1528e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1530e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1531e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1532e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1533e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1534e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1535e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1536e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
1537e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.setEntry(ALIAS_PRIVATE, PRIVATE_KEY, PARAM_KEY);
1539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1540e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
1541e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1542e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1543e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(SECRET_KEY), PARAM_KEY);
1544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1545e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1546e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1547e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, PASSWORD_KEY, null);
1548e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1549e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
1550e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1551e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1552e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1553e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
1554e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_CERTIFICATE,
1555e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                  new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()),
1556e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                  null);
1557e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1558e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1559e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1560e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_CERTIFICATE,
1561e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()),
1562e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
1563e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1564e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
1565e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1566e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1567e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1568e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1569e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1570e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1571e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1572e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1573e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
1574e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1575e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
1576e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2, PARAM_KEY);
1577e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1578e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
1579e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1580e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1581e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1583e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_SECRET,
1584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new SecretKeyEntry(SECRET_KEY_2),
1585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
1586e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1589e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1590e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1591e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1592e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE,
1594e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new TrustedCertificateEntry(PRIVATE_KEY_2.getCertificate()),
1595e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
1596e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1597e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1598e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1599e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1600e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1601e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
1602e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2, PARAM_KEY);
1603e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1604e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
1605e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1606e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1607e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1608e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1609e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_SECRET,
1610e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new SecretKeyEntry(SECRET_KEY_2),
1611e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
1612e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1613e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1614e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1615e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1616e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1617e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1619e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE,
1620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new TrustedCertificateEntry(PRIVATE_KEY_2.getCertificate()),
1621e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
1622e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE));
1623e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1624e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1627e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null/non-null passwords
1632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1633e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_PRIVATE, PRIVATE_KEY, null);
1634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1635e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != UnrecoverableKeyException.class
1637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class
1638e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1639e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1640e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1641e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1642e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1643e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(SECRET_KEY), null);
1645e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1646e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1647e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
1648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class
1649e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != KeyStoreException.class) {
1650e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1652e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
1656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_CERTIFICATE,
1657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()),
1658e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
1659e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1660e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
1661e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    try {
1662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        keyStore.setEntry(ALIAS_CERTIFICATE,
1663e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                          new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()),
1664e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                          PARAM_KEY);
1665e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        fail();
1666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    } catch (KeyStoreException expected) {
1667e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1668e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1669e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1670e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1671e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1672e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1673e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_entryInstanceOf() throws Exception {
1674e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1675e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1676e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, null);
1677e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1678e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1679e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1680e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1681e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1682e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1683e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1684e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1685e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, null);
1687e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1690e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1691e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, Entry.class);
1692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1693e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1694e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf("", null);
1697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1698e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1699e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", Entry.class));
1702e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1703e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1704e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1705e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1706e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1707e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1708e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", Entry.class));
1709e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", PrivateKeyEntry.class));
1710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", SecretKeyEntry.class));
1711e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", TrustedCertificateEntry.class));
1712e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1713e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
1714e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertTrue(keyStore.entryInstanceOf(ALIAS_PRIVATE, PrivateKeyEntry.class));
1715e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, SecretKeyEntry.class));
1716e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, TrustedCertificateEntry.class));
1717e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1718e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore),
1719e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_SECRET, SecretKeyEntry.class));
1720e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, PrivateKeyEntry.class));
1721e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, TrustedCertificateEntry.class));
1722e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1723e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isCertificateEnabled(keyStore),
1724e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_CERTIFICATE,
1725e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  TrustedCertificateEntry.class));
1726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, PrivateKeyEntry.class));
1727e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, SecretKeyEntry.class));
1728e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1729e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1730e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
1731e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, PrivateKeyEntry.class));
1732e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, SecretKeyEntry.class));
1733e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE,
1734e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 TrustedCertificateEntry.class));
1735e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1736e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
1737e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, SecretKeyEntry.class));
1738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, PrivateKeyEntry.class));
1739e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET,
1740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 TrustedCertificateEntry.class));
1741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1742e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore),
1743e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE,
1744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  TrustedCertificateEntry.class));
1745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE,
1746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 PrivateKeyEntry.class));
1747e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE, SecretKeyEntry.class));
1748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1749e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1750e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1751e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_Builder() throws Exception {
1752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1753e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1754e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1755e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore, null);
1756e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1757e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1758e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1759e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1760e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1761e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1762e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1763e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore.getType(),
1764e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    keyStore.getProvider(),
1765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
1766a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                fail();
1767e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1768e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1769e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1771e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1772e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1773e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(null,
1774e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
1775e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
1776e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
1777e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1778e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1779e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1780e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1781e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore.getType(),
1782e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    keyStore.getProvider(),
1783e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
1784e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
1785e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1786e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1787e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1788e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1789e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1790e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1791e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1792e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Builder builder = Builder.newInstance(keyStore, PARAM_STORE);
1793e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1794e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                builder.getProtectionParameter(null);
1795e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1796e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1797e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1798e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore, builder.getKeyStore());
1799e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1800e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                builder.getProtectionParameter(null);
1801e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1802e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1803e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1804e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
1805e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1806e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1807e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1808e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1809e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            File file = File.createTempFile("keystore", keyStore.getProvider().getName());
1810e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
181157f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom                OutputStream os = new FileOutputStream(file);
181257f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom                keyStore.store(os, PASSWORD_STORE);
181357f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom                os.close();
1814e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder builder = Builder.newInstance(keyStore.getType(),
1815e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      keyStore.getProvider(),
1816e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      file,
1817e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      PARAM_STORE);
1818e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(keyStore.getType(), builder.getKeyStore().getType());
1819e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider());
1820e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
1821e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(file, PASSWORD_STORE, keyStore);
1822e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } finally {
1823e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                file.delete();
1824e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1825e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1826e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1827e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1828e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Builder builder = Builder.newInstance(keyStore.getType(),
1829e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  keyStore.getProvider(),
1830e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  PARAM_STORE);
1831e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore.getType(), builder.getKeyStore().getType());
1832e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider());
1833e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
1834e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1836e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_cacerts_bks() throws Exception {
1838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (StandardNames.IS_RI) {
1839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            return;
1840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1841e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance("BKS");
184228192ac5dbb128c63d914fab324d15757fe98fdaJesse Wilson        InputStream in = new FileInputStream(System.getProperty(
184328192ac5dbb128c63d914fab324d15757fe98fdaJesse Wilson                "javax.net.ssl.trustStore", "/etc/security/cacerts.bks"));
184428192ac5dbb128c63d914fab324d15757fe98fdaJesse Wilson        ks.load(in, null);
184557f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        in.close();
1846e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (String alias : Collections.list(ks.aliases())) {
1847e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assert(ks.isCertificateEntry(alias));
1848e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Certificate c = ks.getCertificate(alias);
1849e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertTrue(c instanceof X509Certificate);
1850e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            X509Certificate cert = (X509Certificate) c;
1851e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(cert.getSubjectUniqueID(), cert.getIssuerUniqueID());
185257f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom            assertNotNull(cert.getPublicKey());
1853e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1854e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1855e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom}
1856