Path: blob/master/test/jdk/java/security/KeyStore/KeyStoreBuilder.java
41149 views
/*1* Copyright (c) 2003, 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* @test25* @bug 4938922 4961104 5071293 6236533 813018126* @summary verify that the KeyStore.Builder API works27* @author Andreas Sterbenz28*/2930import java.io.*;31import java.util.*;3233import java.security.*;34import java.security.KeyStore.*;35import java.security.cert.*;36import java.security.cert.Certificate;3738import javax.security.auth.callback.*;3940public class KeyStoreBuilder {4142private final static String DIR = System.getProperty("test.src", ".");4344private static final char[] password = "passphrase".toCharArray();4546private static final char[] wrongPassword = "wrong".toCharArray();4748public static void main(String[] args) throws Exception {49File KSFILE = new File(DIR, "keystore.jks");50KeyStore ks;51String alias = "vajra";52Entry entry = null;5354Builder builder;55builder = Builder.newInstance("JKS", null, KSFILE, new PasswordProtection(password));56ks = builder.getKeyStore();57System.out.println("-KeyStore: " + ks.size());58entry = ks.getEntry(alias, builder.getProtectionParameter(alias));59showEntry(entry);6061builder = Builder.newInstance("JKS", Security.getProvider("Sun"), KSFILE,62new CallbackHandlerProtection(new DummyHandler()));63ks = builder.getKeyStore();64System.out.println("-KeyStore: " + ks.size());65entry = ks.getEntry(alias, builder.getProtectionParameter(alias));66showEntry(entry);6768builder = Builder.newInstance("JKS", null, new PasswordProtection(password));69ks = builder.getKeyStore();70int k = ks.size();71System.out.println("-KeyStore: " + k);72if (k != 0) {73throw new Exception("Size not zero: " + k);74}7576DummyHandler handler = new DummyHandler();7778handler.useWrongPassword = 2;79builder = Builder.newInstance("JKS", null, KSFILE, new CallbackHandlerProtection(handler));80ks = builder.getKeyStore();81System.out.println("-KeyStore: " + ks.size());82entry = ks.getEntry(alias, builder.getProtectionParameter(alias));83showEntry(entry);8485handler.useWrongPassword = 3;86builder = Builder.newInstance("JKS", null, KSFILE, new CallbackHandlerProtection(handler));87try {88ks = builder.getKeyStore();89throw new Exception("should not succeed");90} catch (KeyStoreException e) {91System.out.println(e);92}93try {94ks = builder.getKeyStore();95throw new Exception("should not succeed");96} catch (KeyStoreException e) {97System.out.println(e);98}99100Provider p = new MyProvider();101102handler.useWrongPassword = 2;103builder = Builder.newInstance("My", p, new CallbackHandlerProtection(handler));104ks = builder.getKeyStore();105k = ks.size();106System.out.println("-KeyStore: " + k);107if (k != 0) {108throw new Exception("Size not zero: " + k);109}110111handler.useWrongPassword = 3;112builder = Builder.newInstance("My", p, new CallbackHandlerProtection(handler));113try {114ks = builder.getKeyStore();115throw new Exception("should not succeed");116} catch (KeyStoreException e) {117System.out.println(e);118}119try {120ks = builder.getKeyStore();121throw new Exception("should not succeed");122} catch (KeyStoreException e) {123System.out.println(e);124}125126System.out.println("-OK");127}128129private static void showEntry(Entry entry) {130PrivateKeyEntry pke = (PrivateKeyEntry)entry;131X509Certificate cert = (X509Certificate)pke.getCertificate();132System.out.println("subject: " + cert.getSubjectX500Principal());133}134135private static class DummyHandler implements CallbackHandler {136137int useWrongPassword;138139public void handle(Callback[] callbacks)140throws IOException, UnsupportedCallbackException {141System.out.println("** Callbackhandler invoked");142for (int i = 0; i < callbacks.length; i++) {143Callback cb = callbacks[i];144if (cb instanceof PasswordCallback) {145System.out.println("Found PasswordCallback");146PasswordCallback pcb = (PasswordCallback)cb;147if (useWrongPassword == 0) {148pcb.setPassword(password);149} else {150pcb.setPassword(wrongPassword);151useWrongPassword--;152}153break;154}155}156}157}158159private static class BaseKeyStoreSpi extends KeyStoreSpi {160public Key engineGetKey(String alias, char[] password) {161return null;162}163public Certificate[] engineGetCertificateChain(String alias) {164return null;165}166public Certificate engineGetCertificate(String alias) {167return null;168}169public Date engineGetCreationDate(String alias) {170return null;171}172public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] certs) {173//174}175public void engineSetKeyEntry(String alias, byte[] key, Certificate[] certs) {176//177}178public void engineSetCertificateEntry(String alias, Certificate cert) {179//180}181public void engineDeleteEntry(String alias) {182//183}184public Enumeration<String> engineAliases() {185return new Vector<String>().elements();186}187public boolean engineContainsAlias(String alias) {188return false;189}190public int engineSize() {191return 0;192}193public boolean engineIsKeyEntry(String alias) {194return false;195}196public boolean engineIsCertificateEntry(String alias) {197return false;198}199public String engineGetCertificateAlias(Certificate cert) {200return null;201}202public void engineStore(OutputStream stream, char[] password) {203//204}205public void engineLoad(InputStream stream, char[] password) throws IOException {206//207}208}209210public static class MyKeyStoreSpi extends BaseKeyStoreSpi {211public void engineLoad(InputStream stream, char[] pw) throws IOException {212if (Arrays.equals(password, pw) == false) {213Throwable t = new UnrecoverableKeyException("Wrong password: " + new String(pw));214throw (IOException)new IOException("load() failed").initCause(t);215}216}217}218219private static class MyProvider extends Provider {220MyProvider() {221super("My", "1.0", null);222put("KeyStore.My", "KeyStoreBuilder$MyKeyStoreSpi");223}224}225226}227228229