jdk/test/java/security/KeyStore/KeyStoreBuilder.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4938922 4961104 5071293 6236533
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary verify that the KeyStore.Builder API works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.KeyStore.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class KeyStoreBuilder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private final static String DIR = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final char[] password = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final char[] wrongPassword = "wrong".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        File KSFILE = new File(DIR, "keystore.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        String alias = "vajra";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Entry entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Builder builder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        builder = Builder.newInstance("JKS", null, KSFILE, new PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("-KeyStore: " + ks.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        entry = ks.getEntry(alias, builder.getProtectionParameter(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        showEntry(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        builder = Builder.newInstance("JKS", Security.getProvider("Sun"), KSFILE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                new CallbackHandlerProtection(new DummyHandler()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.out.println("-KeyStore: " + ks.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        entry = ks.getEntry(alias, builder.getProtectionParameter(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        showEntry(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        builder = Builder.newInstance("JKS", null, new PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int k = ks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println("-KeyStore: " + k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (k != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new Exception("Size not zero: " + k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        DummyHandler handler = new DummyHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        handler.useWrongPassword = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        builder = Builder.newInstance("JKS", null, KSFILE, new CallbackHandlerProtection(handler));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        System.out.println("-KeyStore: " + ks.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        entry = ks.getEntry(alias, builder.getProtectionParameter(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        showEntry(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        handler.useWrongPassword = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        builder = Builder.newInstance("JKS", null, KSFILE, new CallbackHandlerProtection(handler));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new Exception("should not succeed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } catch (KeyStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new Exception("should not succeed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } catch (KeyStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        Provider p = new MyProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        handler.useWrongPassword = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        builder = Builder.newInstance("My", p, new CallbackHandlerProtection(handler));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        k = ks.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        System.out.println("-KeyStore: " + k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (k != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new Exception("Size not zero: " + k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        handler.useWrongPassword = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        builder = Builder.newInstance("My", p, new CallbackHandlerProtection(handler));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new Exception("should not succeed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (KeyStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new Exception("should not succeed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } catch (KeyStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        System.out.println("-OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static void showEntry(Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        PrivateKeyEntry pke = (PrivateKeyEntry)entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        X509Certificate cert = (X509Certificate)pke.getCertificate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.out.println("subject: " + cert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static class DummyHandler implements CallbackHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int useWrongPassword;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        public void handle(Callback[] callbacks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                throws IOException, UnsupportedCallbackException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            System.out.println("** Callbackhandler invoked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            for (int i = 0; i < callbacks.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                Callback cb = callbacks[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                if (cb instanceof PasswordCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    System.out.println("Found PasswordCallback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    PasswordCallback pcb = (PasswordCallback)cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    if (useWrongPassword == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        pcb.setPassword(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        pcb.setPassword(wrongPassword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        useWrongPassword--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static class BaseKeyStoreSpi extends KeyStoreSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        public Key engineGetKey(String alias, char[] password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public Certificate[] engineGetCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        public Certificate engineGetCertificate(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public Date engineGetCreationDate(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        public void engineSetKeyEntry(String alias, byte[] key, Certificate[] certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        public void engineSetCertificateEntry(String alias, Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        public void engineDeleteEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        public Enumeration<String> engineAliases() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return new Vector<String>().elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        public boolean engineContainsAlias(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        public int engineSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        public boolean engineIsKeyEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public boolean engineIsCertificateEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        public String engineGetCertificateAlias(Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        public void engineStore(OutputStream stream, char[] password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        public void engineLoad(InputStream stream, char[] password) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public static class MyKeyStoreSpi extends BaseKeyStoreSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        public void engineLoad(InputStream stream, char[] pw) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (Arrays.equals(password, pw) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                Throwable t = new UnrecoverableKeyException("Wrong password: " + new String(pw));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                throw (IOException)new IOException("load() failed").initCause(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static class MyProvider extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        MyProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            super("My", 1.0d, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            put("KeyStore.My", "KeyStoreBuilder$MyKeyStoreSpi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}