Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/TestKeystoreCompat.java
38811 views
/*1* Copyright (c) 2015, 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* @test25* @bug 806255226* @run main/othervm TestKeystoreCompat27* @summary test compatibility mode for JKS and PKCS12 keystores28*/2930import java.io.*;31import java.security.*;32import java.security.KeyStore.*;33import java.security.cert.*;34import javax.crypto.*;35import javax.security.auth.callback.*;3637public class TestKeystoreCompat {38private static final char[] PASSWORD = "changeit".toCharArray();39private static final String DIR = System.getProperty("test.src", ".");40// This is an arbitrary X.509 certificate41private static final String CERT_FILE = "trusted.pem";4243public static final void main(String[] args) throws Exception {4445// Testing empty keystores4647init("empty.jks", "JKS");48init("empty.jceks", "JCEKS");49init("empty.p12", "PKCS12");5051load("empty.jks", "JKS");52load("empty.jceks", "JCEKS");53load("empty.p12", "PKCS12");54load("empty.p12", "JKS"); // test compatibility mode55load("empty.jks", "PKCS12", true); // test without compatibility mode56load("empty.jks", "JKS", false); // test without compatibility mode57load("empty.p12", "JKS", true); // test without compatibility mode58load("empty.p12", "PKCS12", false); // test without compatibility mode5960build("empty.jks", "JKS", true);61build("empty.jks", "JKS", false);62build("empty.jceks", "JCEKS", true);63build("empty.jceks", "JCEKS", false);64build("empty.p12", "PKCS12", true);65build("empty.p12", "PKCS12", false);6667// Testing keystores containing an X.509 certificate6869X509Certificate cert = loadCertificate(CERT_FILE);70init("onecert.jks", "JKS", cert);71init("onecert.jceks", "JCEKS", cert);72init("onecert.p12", "PKCS12", cert);7374load("onecert.jks", "JKS");75load("onecert.jceks", "JCEKS");76load("onecert.p12", "PKCS12");77load("onecert.p12", "JKS"); // test compatibility mode78load("onecert.jks", "PKCS12", true); // test without compatibility mode79load("onecert.jks", "JKS", false); // test without compatibility mode80load("onecert.p12", "JKS", true); // test without compatibility mode81load("onecert.p12", "PKCS12", false); // test without compatibility mode8283build("onecert.jks", "JKS", true);84build("onecert.jks", "JKS", false);85build("onecert.jceks", "JCEKS", true);86build("onecert.jceks", "JCEKS", false);87build("onecert.p12", "PKCS12", true);88build("onecert.p12", "PKCS12", false);8990// Testing keystores containing a secret key9192SecretKey key = generateSecretKey("AES", 128);93init("onekey.jceks", "JCEKS", key);94init("onekey.p12", "PKCS12", key);9596load("onekey.jceks", "JCEKS");97load("onekey.p12", "PKCS12");98load("onekey.p12", "JKS"); // test compatibility mode99load("onekey.p12", "JKS", true); // test without compatibility mode100load("onekey.p12", "PKCS12", false); // test without compatibility mode101102build("onekey.jceks", "JCEKS", true);103build("onekey.jceks", "JCEKS", false);104build("onekey.p12", "PKCS12", true);105build("onekey.p12", "PKCS12", false);106107System.out.println("OK.");108}109110// Instantiate an empty keystore using the supplied keystore type111private static void init(String file, String type) throws Exception {112KeyStore ks = KeyStore.getInstance(type);113ks.load(null, null);114try (OutputStream stream = new FileOutputStream(file)) {115ks.store(stream, PASSWORD);116}117System.out.println("Created a " + type + " keystore named '" + file + "'");118}119120// Instantiate a keystore using the supplied keystore type & create an entry121private static void init(String file, String type, X509Certificate cert)122throws Exception {123KeyStore ks = KeyStore.getInstance(type);124ks.load(null, null);125ks.setEntry("mycert", new KeyStore.TrustedCertificateEntry(cert), null);126try (OutputStream stream = new FileOutputStream(file)) {127ks.store(stream, PASSWORD);128}129System.out.println("Created a " + type + " keystore named '" + file + "'");130}131132// Instantiate a keystore using the supplied keystore type & create an entry133private static void init(String file, String type, SecretKey key)134throws Exception {135KeyStore ks = KeyStore.getInstance(type);136ks.load(null, null);137ks.setEntry("mykey", new KeyStore.SecretKeyEntry(key),138new PasswordProtection(PASSWORD));139try (OutputStream stream = new FileOutputStream(file)) {140ks.store(stream, PASSWORD);141}142System.out.println("Created a " + type + " keystore named '" + file + "'");143}144145// Instantiate a keystore by probing the supplied file for the keystore type146private static void build(String file, String type, boolean usePassword)147throws Exception {148149Builder builder;150if (usePassword) {151builder = Builder.newInstance(type, null, new File(file),152new PasswordProtection(PASSWORD));153} else {154builder = Builder.newInstance(type, null, new File(file),155new CallbackHandlerProtection(new DummyHandler()));156}157KeyStore ks = builder.getKeyStore();158if (!type.equalsIgnoreCase(ks.getType())) {159throw new Exception("ERROR: expected a " + type + " keystore, " +160"got a " + ks.getType() + " keystore instead");161} else {162System.out.println("Built a " + type + " keystore named '" + file + "'");163}164}165166// Load the keystore entries167private static void load(String file, String type) throws Exception {168KeyStore ks = KeyStore.getInstance(type);169try (InputStream stream = new FileInputStream(file)) {170ks.load(stream, PASSWORD);171}172if (!type.equalsIgnoreCase(ks.getType())) {173throw new Exception("ERROR: expected a " + type + " keystore, " +174"got a " + ks.getType() + " keystore instead");175} else {176System.out.println("Loaded a " + type + " keystore named '" + file + "'");177}178}179180// Load the keystore entries (with compatibility mode disabled)181private static void load(String file, String type, boolean expectFailure)182throws Exception {183Security.setProperty("keystore.type.compat", "false");184try {185load(file, type);186if (expectFailure) {187throw new Exception("ERROR: expected load to fail but it didn't");188}189} catch (IOException e) {190if (expectFailure) {191System.out.println("Failed to load a " + type + " keystore named '" + file + "' (as expected)");192} else {193throw e;194}195} finally {196Security.setProperty("keystore.type.compat", "true");197}198}199200// Read an X.509 certificate from the supplied file201private static X509Certificate loadCertificate(String certFile)202throws Exception {203X509Certificate cert = null;204try (FileInputStream certStream =205new FileInputStream(DIR + "/" + certFile)) {206CertificateFactory factory =207CertificateFactory.getInstance("X.509");208return (X509Certificate) factory.generateCertificate(certStream);209}210}211212// Generate a secret key using the supplied algorithm name and key size213private static SecretKey generateSecretKey(String algorithm, int size)214throws NoSuchAlgorithmException {215KeyGenerator generator = KeyGenerator.getInstance(algorithm);216generator.init(size);217return generator.generateKey();218}219220private static class DummyHandler implements CallbackHandler {221public void handle(Callback[] callbacks)222throws IOException, UnsupportedCallbackException {223System.out.println("** Callbackhandler invoked");224for (int i = 0; i < callbacks.length; i++) {225Callback cb = callbacks[i];226if (cb instanceof PasswordCallback) {227PasswordCallback pcb = (PasswordCallback)cb;228pcb.setPassword(PASSWORD);229break;230}231}232}233}234}235236237