Path: blob/master/test/jdk/sun/security/provider/KeyStore/DKSTest.java
41152 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* see ./DKSTest.sh25*/2627import java.io.*;28import java.net.*;29import java.security.*;30import java.security.KeyStore;31import java.security.cert.*;32import java.security.cert.Certificate;33import java.util.*;3435// Load and store entries in domain keystores3637public class DKSTest {3839private static final String TEST_SRC = System.getProperty("test.src");40private static final String USER_DIR = System.getProperty("user.dir");41private static final String CERT = TEST_SRC + "/../../pkcs12/trusted.pem";42private static final String CONFIG = "file://" + TEST_SRC + "/domains.cfg";43private static final Map<String, KeyStore.ProtectionParameter> PASSWORDS =44new HashMap<String, KeyStore.ProtectionParameter>() {{45put("keystore",46new KeyStore.PasswordProtection("test123".toCharArray()));47put("policy_keystore",48new KeyStore.PasswordProtection(49"Alias.password".toCharArray()));50put("pw_keystore",51new KeyStore.PasswordProtection("test12".toCharArray()));52put("eckeystore1",53new KeyStore.PasswordProtection("password".toCharArray()));54put("truststore",55new KeyStore.PasswordProtection("changeit".toCharArray()));56put("empty",57new KeyStore.PasswordProtection("passphrase".toCharArray()));58}};5960private static final Map<String, KeyStore.ProtectionParameter>61WRONG_PASSWORDS = new HashMap<String, KeyStore.ProtectionParameter>() {{62put("policy_keystore",63new KeyStore.PasswordProtection(64"wrong".toCharArray()));65put("pw_keystore",66new KeyStore.PasswordProtection("wrong".toCharArray()));67put("eckeystore1",68new KeyStore.PasswordProtection("wrong".toCharArray()));69}};7071public static void main(String[] args) throws Exception {72/*73* domain keystore: keystores with wrong passwords74*/75try {76URI config = new URI(CONFIG + "#keystores");77KeyStore ks = KeyStore.getInstance("DKS");78ks.load(new DomainLoadStoreParameter(config, WRONG_PASSWORDS));79throw new RuntimeException("Expected exception not thrown");80} catch (IOException e) {81System.out.println("Expected exception: " + e);82if (!causedBy(e, UnrecoverableKeyException.class)) {83e.printStackTrace(System.out);84throw new RuntimeException("Unexpected cause");85}86System.out.println("Expected cause: " + e);87}8889/*90* domain keystore: system91*/92URI config = new URI(CONFIG + "#system");93int cacertsCount;94int expected;95KeyStore keystore = KeyStore.getInstance("DKS");96// load entries97keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));98cacertsCount = expected = keystore.size();99System.out.println("\nLoading domain keystore: " + config + "\t[" +100expected + " entries]");101checkEntries(keystore, expected);102103/*104* domain keystore: system_plus105*/106config = new URI(CONFIG + "#system_plus");107expected = cacertsCount + 1;108keystore = KeyStore.getInstance("DKS");109// load entries110keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));111System.out.println("\nLoading domain keystore: " + config + "\t[" +112expected + " entries]");113checkEntries(keystore, expected);114115/*116* domain keystore: system_env117*/118config = new URI(CONFIG + "#system_env");119expected = 1 + cacertsCount;120keystore = KeyStore.getInstance("DKS");121// load entries122keystore.load(123new DomainLoadStoreParameter(config,124Collections.<String, KeyStore.ProtectionParameter>emptyMap()));125System.out.println("\nLoading domain keystore: " + config + "\t[" +126expected + " entries]");127checkEntries(keystore, expected);128129/*130* domain keystore: empty131*/132KeyStore empty = KeyStore.getInstance("JKS");133empty.load(null, null);134135try (OutputStream outStream =136new FileOutputStream(new File(USER_DIR, "empty.jks"))) {137empty.store(outStream, "passphrase".toCharArray());138}139config = new URI(CONFIG + "#empty");140expected = 0;141keystore = KeyStore.getInstance("DKS");142// load entries143keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));144System.out.println("\nLoading domain keystore: " + config + "\t[" +145expected + " entries]");146checkEntries(keystore, expected);147148/*149* domain keystore: keystores150*/151config = new URI(CONFIG + "#keystores");152expected = 2 + 1 + 1;153keystore = KeyStore.getInstance("DKS");154// load entries155keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));156System.out.println("\nLoading domain keystore: " + config + "\t[" +157expected + " entries]");158checkEntries(keystore, expected);159// set a new trusted certificate entry160Certificate cert = loadCertificate(CERT);161String alias = "pw_keystore tmp-cert";162System.out.println("Setting new trusted certificate entry: " + alias);163keystore.setEntry(alias,164new KeyStore.TrustedCertificateEntry(cert), null);165expected++;166// store entries167config = new URI(CONFIG + "#keystores_tmp");168System.out.println("Storing domain keystore: " + config + "\t[" +169expected + " entries]");170keystore.store(new DomainLoadStoreParameter(config, PASSWORDS));171keystore = KeyStore.getInstance("DKS");172// reload entries173keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));174System.out.println("Reloading domain keystore: " + config + "\t[" +175expected + " entries]");176checkEntries(keystore, expected);177// get the new trusted certificate entry178System.out.println("Getting new trusted certificate entry: " + alias);179if (!keystore.isCertificateEntry(alias)) {180throw new Exception("Error: cannot retrieve certificate entry: " +181alias);182}183keystore.setEntry(alias,184new KeyStore.TrustedCertificateEntry(cert), null);185}186187private static void checkEntries(KeyStore keystore, int expected)188throws Exception {189int i = 0;190for (String alias : Collections.list(keystore.aliases())) {191System.out.print(".");192i++;193}194System.out.println();195if (expected != i) {196throw new Exception("Error: unexpected entry count in keystore: " +197"loaded=" + i + ", expected=" + expected);198}199}200201private static Certificate loadCertificate(String certFile)202throws Exception {203X509Certificate cert = null;204try (FileInputStream certStream = new FileInputStream(certFile)) {205CertificateFactory factory =206CertificateFactory.getInstance("X.509");207return factory.generateCertificate(certStream);208}209}210211// checks if an exception was caused by specified exception class212private static boolean causedBy(Exception e, Class klass) {213Throwable cause = e;214while ((cause = cause.getCause()) != null) {215if (cause.getClass().equals(klass)) {216return true;217}218}219return false;220}221}222223224