0N/A/*
2362N/A * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4938922 4961104 5071293 6236533
0N/A * @summary verify that the KeyStore.Builder API works
0N/A * @author Andreas Sterbenz
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.KeyStore.*;
0N/Aimport java.security.cert.*;
0N/Aimport java.security.cert.Certificate;
0N/A
0N/Aimport javax.security.auth.callback.*;
0N/A
0N/Apublic class KeyStoreBuilder {
0N/A
0N/A private final static String DIR = System.getProperty("test.src", ".");
0N/A
0N/A private static final char[] password = "passphrase".toCharArray();
0N/A
0N/A private static final char[] wrongPassword = "wrong".toCharArray();
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A File KSFILE = new File(DIR, "keystore.jks");
0N/A KeyStore ks;
0N/A String alias = "vajra";
0N/A Entry entry = null;
0N/A
0N/A Builder builder;
0N/A builder = Builder.newInstance("JKS", null, KSFILE, new PasswordProtection(password));
0N/A ks = builder.getKeyStore();
0N/A System.out.println("-KeyStore: " + ks.size());
0N/A entry = ks.getEntry(alias, builder.getProtectionParameter(alias));
0N/A showEntry(entry);
0N/A
0N/A builder = Builder.newInstance("JKS", Security.getProvider("Sun"), KSFILE,
0N/A new CallbackHandlerProtection(new DummyHandler()));
0N/A ks = builder.getKeyStore();
0N/A System.out.println("-KeyStore: " + ks.size());
0N/A entry = ks.getEntry(alias, builder.getProtectionParameter(alias));
0N/A showEntry(entry);
0N/A
0N/A builder = Builder.newInstance("JKS", null, new PasswordProtection(password));
0N/A ks = builder.getKeyStore();
0N/A int k = ks.size();
0N/A System.out.println("-KeyStore: " + k);
0N/A if (k != 0) {
0N/A throw new Exception("Size not zero: " + k);
0N/A }
0N/A
0N/A DummyHandler handler = new DummyHandler();
0N/A
0N/A handler.useWrongPassword = 2;
0N/A builder = Builder.newInstance("JKS", null, KSFILE, new CallbackHandlerProtection(handler));
0N/A ks = builder.getKeyStore();
0N/A System.out.println("-KeyStore: " + ks.size());
0N/A entry = ks.getEntry(alias, builder.getProtectionParameter(alias));
0N/A showEntry(entry);
0N/A
0N/A handler.useWrongPassword = 3;
0N/A builder = Builder.newInstance("JKS", null, KSFILE, new CallbackHandlerProtection(handler));
0N/A try {
0N/A ks = builder.getKeyStore();
0N/A throw new Exception("should not succeed");
0N/A } catch (KeyStoreException e) {
0N/A System.out.println(e);
0N/A }
0N/A try {
0N/A ks = builder.getKeyStore();
0N/A throw new Exception("should not succeed");
0N/A } catch (KeyStoreException e) {
0N/A System.out.println(e);
0N/A }
0N/A
0N/A Provider p = new MyProvider();
0N/A
0N/A handler.useWrongPassword = 2;
0N/A builder = Builder.newInstance("My", p, new CallbackHandlerProtection(handler));
0N/A ks = builder.getKeyStore();
0N/A k = ks.size();
0N/A System.out.println("-KeyStore: " + k);
0N/A if (k != 0) {
0N/A throw new Exception("Size not zero: " + k);
0N/A }
0N/A
0N/A handler.useWrongPassword = 3;
0N/A builder = Builder.newInstance("My", p, new CallbackHandlerProtection(handler));
0N/A try {
0N/A ks = builder.getKeyStore();
0N/A throw new Exception("should not succeed");
0N/A } catch (KeyStoreException e) {
0N/A System.out.println(e);
0N/A }
0N/A try {
0N/A ks = builder.getKeyStore();
0N/A throw new Exception("should not succeed");
0N/A } catch (KeyStoreException e) {
0N/A System.out.println(e);
0N/A }
0N/A
0N/A System.out.println("-OK");
0N/A }
0N/A
0N/A private static void showEntry(Entry entry) {
0N/A PrivateKeyEntry pke = (PrivateKeyEntry)entry;
0N/A X509Certificate cert = (X509Certificate)pke.getCertificate();
0N/A System.out.println("subject: " + cert.getSubjectX500Principal());
0N/A }
0N/A
0N/A private static class DummyHandler implements CallbackHandler {
0N/A
0N/A int useWrongPassword;
0N/A
0N/A public void handle(Callback[] callbacks)
0N/A throws IOException, UnsupportedCallbackException {
0N/A System.out.println("** Callbackhandler invoked");
0N/A for (int i = 0; i < callbacks.length; i++) {
0N/A Callback cb = callbacks[i];
0N/A if (cb instanceof PasswordCallback) {
0N/A System.out.println("Found PasswordCallback");
0N/A PasswordCallback pcb = (PasswordCallback)cb;
0N/A if (useWrongPassword == 0) {
0N/A pcb.setPassword(password);
0N/A } else {
0N/A pcb.setPassword(wrongPassword);
0N/A useWrongPassword--;
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static class BaseKeyStoreSpi extends KeyStoreSpi {
0N/A public Key engineGetKey(String alias, char[] password) {
0N/A return null;
0N/A }
0N/A public Certificate[] engineGetCertificateChain(String alias) {
0N/A return null;
0N/A }
0N/A public Certificate engineGetCertificate(String alias) {
0N/A return null;
0N/A }
0N/A public Date engineGetCreationDate(String alias) {
0N/A return null;
0N/A }
0N/A public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] certs) {
0N/A //
0N/A }
0N/A public void engineSetKeyEntry(String alias, byte[] key, Certificate[] certs) {
0N/A //
0N/A }
0N/A public void engineSetCertificateEntry(String alias, Certificate cert) {
0N/A //
0N/A }
0N/A public void engineDeleteEntry(String alias) {
0N/A //
0N/A }
0N/A public Enumeration<String> engineAliases() {
0N/A return new Vector<String>().elements();
0N/A }
0N/A public boolean engineContainsAlias(String alias) {
0N/A return false;
0N/A }
0N/A public int engineSize() {
0N/A return 0;
0N/A }
0N/A public boolean engineIsKeyEntry(String alias) {
0N/A return false;
0N/A }
0N/A public boolean engineIsCertificateEntry(String alias) {
0N/A return false;
0N/A }
0N/A public String engineGetCertificateAlias(Certificate cert) {
0N/A return null;
0N/A }
0N/A public void engineStore(OutputStream stream, char[] password) {
0N/A //
0N/A }
0N/A public void engineLoad(InputStream stream, char[] password) throws IOException {
0N/A //
0N/A }
0N/A }
0N/A
0N/A public static class MyKeyStoreSpi extends BaseKeyStoreSpi {
0N/A public void engineLoad(InputStream stream, char[] pw) throws IOException {
0N/A if (Arrays.equals(password, pw) == false) {
0N/A Throwable t = new UnrecoverableKeyException("Wrong password: " + new String(pw));
0N/A throw (IOException)new IOException("load() failed").initCause(t);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static class MyProvider extends Provider {
0N/A MyProvider() {
0N/A super("My", 1.0d, null);
0N/A put("KeyStore.My", "KeyStoreBuilder$MyKeyStoreSpi");
0N/A }
0N/A }
0N/A
0N/A}