Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/KeyStore/DKSTest.java
38853 views
/*1* Copyright (c) 2013, 2016, 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("eckeystore2",55new KeyStore.PasswordProtection("password".toCharArray()));56put("truststore",57new KeyStore.PasswordProtection("changeit".toCharArray()));58put("empty",59new KeyStore.PasswordProtection("passphrase".toCharArray()));60}};6162private static final Map<String, KeyStore.ProtectionParameter>63WRONG_PASSWORDS = new HashMap<String, KeyStore.ProtectionParameter>() {{64put("policy_keystore",65new KeyStore.PasswordProtection(66"wrong".toCharArray()));67put("pw_keystore",68new KeyStore.PasswordProtection("wrong".toCharArray()));69put("eckeystore1",70new KeyStore.PasswordProtection("wrong".toCharArray()));71put("eckeystore2",72new KeyStore.PasswordProtection("wrong".toCharArray()));73}};7475public static void main(String[] args) throws Exception {76/*77* domain keystore: keystores with wrong passwords78*/79try {80URI config = new URI(CONFIG + "#keystores");81KeyStore ks = KeyStore.getInstance("DKS");82ks.load(new DomainLoadStoreParameter(config, WRONG_PASSWORDS));83throw new RuntimeException("Expected exception not thrown");84} catch (IOException e) {85System.out.println("Expected exception: " + e);86if (!causedBy(e, UnrecoverableKeyException.class)) {87e.printStackTrace(System.out);88throw new RuntimeException("Unexpected cause");89}90System.out.println("Expected cause: " + e);91}9293/*94* domain keystore: system95*/96URI config = new URI(CONFIG + "#system");97int cacertsCount;98int expected;99KeyStore keystore = KeyStore.getInstance("DKS");100// load entries101keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));102cacertsCount = expected = keystore.size();103System.out.println("\nLoading domain keystore: " + config + "\t[" +104expected + " entries]");105checkEntries(keystore, expected);106107/*108* domain keystore: system_plus109*/110config = new URI(CONFIG + "#system_plus");111expected = cacertsCount + 1;112keystore = KeyStore.getInstance("DKS");113// load entries114keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));115System.out.println("\nLoading domain keystore: " + config + "\t[" +116expected + " entries]");117checkEntries(keystore, expected);118119/*120* domain keystore: system_env121*/122config = new URI(CONFIG + "#system_env");123expected = 1 + cacertsCount;124keystore = KeyStore.getInstance("DKS");125// load entries126keystore.load(127new DomainLoadStoreParameter(config,128Collections.<String, KeyStore.ProtectionParameter>emptyMap()));129System.out.println("\nLoading domain keystore: " + config + "\t[" +130expected + " entries]");131checkEntries(keystore, expected);132133/*134* domain keystore: empty135*/136KeyStore empty = KeyStore.getInstance("JKS");137empty.load(null, null);138139try (OutputStream outStream =140new FileOutputStream(new File(USER_DIR, "empty.jks"))) {141empty.store(outStream, "passphrase".toCharArray());142}143config = new URI(CONFIG + "#empty");144expected = 0;145keystore = KeyStore.getInstance("DKS");146// load entries147keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));148System.out.println("\nLoading domain keystore: " + config + "\t[" +149expected + " entries]");150checkEntries(keystore, expected);151152/*153* domain keystore: keystores154*/155config = new URI(CONFIG + "#keystores");156expected = 2 + 1 + 1 + 1;157keystore = KeyStore.getInstance("DKS");158// load entries159keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));160System.out.println("\nLoading domain keystore: " + config + "\t[" +161expected + " entries]");162checkEntries(keystore, expected);163// set a new trusted certificate entry164Certificate cert = loadCertificate(CERT);165String alias = "pw_keystore tmp-cert";166System.out.println("Setting new trusted certificate entry: " + alias);167keystore.setEntry(alias,168new KeyStore.TrustedCertificateEntry(cert), null);169expected++;170// store entries171config = new URI(CONFIG + "#keystores_tmp");172System.out.println("Storing domain keystore: " + config + "\t[" +173expected + " entries]");174keystore.store(new DomainLoadStoreParameter(config, PASSWORDS));175keystore = KeyStore.getInstance("DKS");176// reload entries177keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));178System.out.println("Reloading domain keystore: " + config + "\t[" +179expected + " entries]");180checkEntries(keystore, expected);181// get the new trusted certificate entry182System.out.println("Getting new trusted certificate entry: " + alias);183if (!keystore.isCertificateEntry(alias)) {184throw new Exception("Error: cannot retrieve certificate entry: " +185alias);186}187keystore.setEntry(alias,188new KeyStore.TrustedCertificateEntry(cert), null);189}190191private static void checkEntries(KeyStore keystore, int expected)192throws Exception {193int i = 0;194for (String alias : Collections.list(keystore.aliases())) {195System.out.print(".");196i++;197}198System.out.println();199if (expected != i) {200throw new Exception("Error: unexpected entry count in keystore: " +201"loaded=" + i + ", expected=" + expected);202}203}204205private static Certificate loadCertificate(String certFile)206throws Exception {207X509Certificate cert = null;208try (FileInputStream certStream = new FileInputStream(certFile)) {209CertificateFactory factory =210CertificateFactory.getInstance("X.509");211return factory.generateCertificate(certStream);212}213}214215// checks if an exception was caused by specified exception class216private static boolean causedBy(Exception e, Class klass) {217Throwable cause = e;218while ((cause = cause.getCause()) != null) {219if (cause.getClass().equals(klass)) {220return true;221}222}223return false;224}225}226227228