Path: blob/master/test/jdk/java/security/KeyStore/ProbeKeystores.java
41149 views
/*1* Copyright (c) 2014, 2018, 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 8044445 8194307 820731826* @summary test new methods from JEP-229: Create PKCS12 Keystores by Default27*/2829import java.io.*;30import java.security.*;31import java.security.KeyStore.*;32import java.security.cert.*;33import javax.crypto.*;34import javax.security.auth.callback.*;3536public class ProbeKeystores {37private static final char[] PASSWORD = "changeit".toCharArray();38private static final char[] BAD_PASSWORD = "badpasword".toCharArray();39private static final LoadStoreParameter LOAD_STORE_PARAM =40new MyLoadStoreParameter(new PasswordProtection(PASSWORD));41private static final LoadStoreParameter BAD_LOAD_STORE_PARAM =42new MyLoadStoreParameter(new PasswordProtection(BAD_PASSWORD));43private static final String DIR = System.getProperty("test.src", ".");44private static final String CERT_FILE = "trusted.pem";4546private static class MyLoadStoreParameter implements LoadStoreParameter {4748private ProtectionParameter protection;4950MyLoadStoreParameter(ProtectionParameter protection) {51this.protection = protection;52}5354public ProtectionParameter getProtectionParameter() {55return protection;56}57}5859public static final void main(String[] args) throws Exception {6061// Testing empty keystores6263init("empty.jks", "JKS");64init("empty.jceks", "JCEKS");65init("empty.p12", "PKCS12");6667load("empty.jks", "JKS");68load("empty.jceks", "JCEKS");69load("empty.p12", "PKCS12");70load("empty.jks", "PKCS12"); // test compatibility mode71load("empty.p12", "JKS"); // test compatibility mode72load("empty.jks", "PKCS12", true); // test without compatibility mode73load("empty.jks", "JKS", false); // test without compatibility mode74load("empty.p12", "JKS", true); // test without compatibility mode75load("empty.p12", "PKCS12", false); // test without compatibility mode7677probe("empty.jks", "JKS");78probe("empty.jceks", "JCEKS");79probe("empty.p12", "PKCS12");8081build("empty.jks", "JKS", true);82build("empty.jks", "JKS", false);83build("empty.jceks", "JCEKS", true);84build("empty.jceks", "JCEKS", false);85build("empty.p12", "PKCS12", true);86build("empty.p12", "PKCS12", false);8788// Testing keystores containing an X.509 certificate8990X509Certificate cert = loadCertificate(CERT_FILE);91init("onecert.jks", "JKS", cert);92init("onecert.jceks", "JCEKS", cert);93init("onecert.p12", "PKCS12", cert);9495load("onecert.jks", "JKS");96load("onecert.jceks", "JCEKS");97load("onecert.p12", "PKCS12");98load("onecert.jks", "PKCS12"); // test compatibility mode99load("onecert.p12", "JKS"); // test compatibility mode100load("onecert.jks", "PKCS12", true); // test without compatibility mode101load("onecert.jks", "JKS", false); // test without compatibility mode102load("onecert.p12", "JKS", true); // test without compatibility mode103load("onecert.p12", "PKCS12", false); // test without compatibility mode104105probe("onecert.jks", "JKS");106probe("onecert.jceks", "JCEKS");107probe("onecert.p12", "PKCS12");108109build("onecert.jks", "JKS", true);110build("onecert.jks", "JKS", false);111build("onecert.jceks", "JCEKS", true);112build("onecert.jceks", "JCEKS", false);113build("onecert.p12", "PKCS12", true);114build("onecert.p12", "PKCS12", false);115116// Testing keystores containing a secret key117118SecretKey key = generateSecretKey("AES", 128);119init("onekey.jceks", "JCEKS", key);120init("onekey.p12", "PKCS12", key);121122load("onekey.jceks", "JCEKS");123load("onekey.p12", "PKCS12");124load("onekey.p12", "JKS"); // test compatibility mode125load("onekey.p12", "JKS", true); // test without compatibility mode126load("onekey.p12", "PKCS12", false); // test without compatibility mode127128probe("onekey.jceks", "JCEKS");129probe("onekey.p12", "PKCS12");130131build("onekey.jceks", "JCEKS", true);132build("onekey.jceks", "JCEKS", false);133build("onekey.p12", "PKCS12", true);134build("onekey.p12", "PKCS12", false);135136System.out.println("OK.");137}138139// Instantiate an empty keystore using the supplied keystore type140private static void init(String file, String type) throws Exception {141KeyStore ks = KeyStore.getInstance(type);142ks.load(null, null);143try (OutputStream stream = new FileOutputStream(file)) {144ks.store(stream, PASSWORD);145}146System.out.println("Created a " + type + " keystore named '" + file + "'");147}148149// Instantiate a keystore using the supplied keystore type & create an entry150private static void init(String file, String type, X509Certificate cert)151throws Exception {152KeyStore ks = KeyStore.getInstance(type);153ks.load(null, null);154ks.setEntry("mycert", new KeyStore.TrustedCertificateEntry(cert), null);155try (OutputStream stream = new FileOutputStream(file)) {156ks.store(stream, PASSWORD);157}158System.out.println("Created a " + type + " keystore named '" + file + "'");159}160161// Instantiate a keystore using the supplied keystore type & create an entry162private static void init(String file, String type, SecretKey key)163throws Exception {164KeyStore ks = KeyStore.getInstance(type);165ks.load(null, null);166ks.setEntry("mykey", new KeyStore.SecretKeyEntry(key),167new PasswordProtection(PASSWORD));168try (OutputStream stream = new FileOutputStream(file)) {169ks.store(stream, PASSWORD);170}171System.out.println("Created a " + type + " keystore named '" + file + "'");172}173174// Instantiate a keystore by probing the supplied file for the keystore type175private static void probe(String file, String type) throws Exception {176// First try with the correct password177KeyStore ks = KeyStore.getInstance(new File(file), PASSWORD);178if (!type.equalsIgnoreCase(ks.getType())) {179throw new Exception("ERROR: expected a " + type + " keystore, " +180"got a " + ks.getType() + " keystore instead");181} else {182System.out.println("Probed a " + type + " keystore named '" + file183+ "' with " + ks.size() + " entries");184}185186// Next try with an incorrect password187try {188ks = KeyStore.getInstance(new File(file), BAD_PASSWORD);189throw new Exception("ERROR: expected an exception but got success");190} catch (IOException e) {191System.out.println("Failed to load a " + type + " keystore named '" + file + "' (as expected)");192}193194// Now try with the correct password within a LoadStoreParameter195ks = KeyStore.getInstance(new File(file), LOAD_STORE_PARAM);196if (!type.equalsIgnoreCase(ks.getType())) {197throw new Exception("ERROR: expected a " + type + " keystore, " +198"got a " + ks.getType() + " keystore instead");199} else {200System.out.println("Probed a " + type + " keystore named '" + file201+ "' with " + ks.size() + " entries");202}203204// Next try with an incorrect password within a LoadStoreParameter205try {206ks = KeyStore.getInstance(new File(file), BAD_LOAD_STORE_PARAM);207throw new Exception("ERROR: expected an exception but got success");208} catch (IOException e) {209System.out.println("Failed to load a " + type + " keystore named '" + file + "' (as expected)");210}211}212213// Instantiate a keystore by probing the supplied file for the keystore type214private static void build(String file, String type, boolean usePassword)215throws Exception {216217Builder builder;218if (usePassword) {219builder = Builder.newInstance(new File(file),220new PasswordProtection(PASSWORD));221} else {222builder = Builder.newInstance(new File(file),223new CallbackHandlerProtection(new DummyHandler()));224}225KeyStore ks = builder.getKeyStore();226if (!type.equalsIgnoreCase(ks.getType())) {227throw new Exception("ERROR: expected a " + type + " keystore, " +228"got a " + ks.getType() + " keystore instead");229} else {230System.out.println("Built a " + type + " keystore named '" + file + "'");231}232}233234// Load the keystore entries235private static void load(String file, String type) throws Exception {236KeyStore ks = KeyStore.getInstance(type);237try (InputStream stream = new FileInputStream(file)) {238ks.load(stream, PASSWORD);239}240if (!type.equalsIgnoreCase(ks.getType())) {241throw new Exception("ERROR: expected a " + type + " keystore, " +242"got a " + ks.getType() + " keystore instead");243} else {244System.out.println("Loaded a " + type + " keystore named '" + file + "'");245}246}247248// Load the keystore entries (with compatibility mode disabled)249private static void load(String file, String type, boolean expectFailure)250throws Exception {251Security.setProperty("keystore.type.compat", "false");252try {253load(file, type);254if (expectFailure) {255throw new Exception("ERROR: expected load to fail but it didn't");256}257} catch (IOException e) {258if (expectFailure) {259System.out.println("Failed to load a " + type + " keystore named '" + file + "' (as expected)");260} else {261throw e;262}263} finally {264Security.setProperty("keystore.type.compat", "true");265}266}267268// Read an X.509 certificate from the supplied file269private static X509Certificate loadCertificate(String certFile)270throws Exception {271X509Certificate cert = null;272try (FileInputStream certStream =273new FileInputStream(DIR + "/" + certFile)) {274CertificateFactory factory =275CertificateFactory.getInstance("X.509");276return (X509Certificate) factory.generateCertificate(certStream);277}278}279280// Generate a secret key using the supplied algorithm name and key size281private static SecretKey generateSecretKey(String algorithm, int size)282throws NoSuchAlgorithmException {283KeyGenerator generator = KeyGenerator.getInstance(algorithm);284generator.init(size);285return generator.generateKey();286}287288private static class DummyHandler implements CallbackHandler {289public void handle(Callback[] callbacks)290throws IOException, UnsupportedCallbackException {291System.out.println("** Callbackhandler invoked");292for (int i = 0; i < callbacks.length; i++) {293Callback cb = callbacks[i];294if (cb instanceof PasswordCallback) {295PasswordCallback pcb = (PasswordCallback)cb;296pcb.setPassword(PASSWORD);297break;298}299}300}301}302}303304305