jdk/test/sun/security/tools/keytool/KeyToolTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2067 6f9db5f305cd
child 10138 b7572da25d15
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
     2
 * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
    21
 * questions.
2
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Testing keytool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author weijun.wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Run through autotest.sh and manualtest.sh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Testing non-PKCS11 keystores:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *       echo | java -Dfile KeyToolTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Testing NSS PKCS11 keystores:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *       # testing NSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *       # make sure the NSS db files are in current directory and writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *       echo | java -Dnss -Dnss.lib=/path/to/libsoftokn3.so KeyToolTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Testing Solaris Cryptography Framework PKCS11 keystores:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *       # make sure you've already run pktool and set test12 as pin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *       echo | java -Dsolaris KeyToolTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * ATTENTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Exception in thread "main" java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_KEY_SIZE_RANGE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *       at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:420)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *       ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_KEY_SIZE_RANGE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *       at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *       at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:391)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *       ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * been observed. Possibly a Solaris bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * ATTENTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * NSS PKCS11 config file are changed, DSA not supported now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.security.tools.KeyTool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.io.*;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    62
import java.security.KeyPairGenerator;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    63
import java.security.NoSuchAlgorithmException;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    64
import java.util.*;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    65
import java.security.cert.X509Certificate;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    66
import sun.security.util.ObjectIdentifier;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class KeyToolTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // The stdout and stderr outputs after a keytool run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    String out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    String err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // the output of println() in KeyTool.run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    String ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    String lastInput = "", lastCommand = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final boolean debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.getProperty("debug") != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static final String NSS_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            "-keystore NONE -storetype PKCS11 -providerName SunPKCS11-nss -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nss.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static final String NSS_SRC_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            "-srckeystore NONE -srcstoretype PKCS11 -srcproviderName SunPKCS11-nss -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nss.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static final String NZZ_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            "-keystore NONE -storetype PKCS11 -providerName SunPKCS11-nzz -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nzz.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static final String NZZ_SRC_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            "-srckeystore NONE -srcstoretype PKCS11 -srcproviderName SunPKCS11-nzz -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nzz.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static final String SUN_P11_ARG = "-keystore NONE -storetype PKCS11 ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static final String SUN_SRC_P11_ARG = "-srckeystore NONE -srcstoretype PKCS11 ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    String p11Arg, srcP11Arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /** Creates a new instance of KeyToolTest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    KeyToolTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // so that there is "Warning" and not translated into other language
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Locale.setDefault(Locale.US);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Helper, removes a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    void remove(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            System.err.println("Removing " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        new File(filename).delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (new File(filename).exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new RuntimeException("Error deleting " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Run a set of keytool command with given terminal input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param input the terminal inputs, the characters typed by human
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *        if <code>cmd</code> is running on a terminal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param cmd the argument of a keytool command line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @throws if keytool goes wrong in some place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    void test(String input, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        lastInput = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        lastCommand = cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   124
        // "X" is appended so that we can precisely test how input is consumed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        HumanInputStream in = new HumanInputStream(input+"X");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        test(in, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // make sure the input string is no more no less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if(in.read() != 'X' || in.read() != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new Exception("Input not consumed exactly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    void test(InputStream in, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // save the original 3 streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            System.err.println(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            System.err.print(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        PrintStream p1 = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        PrintStream p2 = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        InputStream i1 = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ByteArrayOutputStream b1 = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        ByteArrayOutputStream b2 = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            System.setIn(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            System.setOut(new PrintStream(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            System.setErr(new PrintStream(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // since System.in is overrided, the KeyTool.main() method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // never block at user input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            // use -debug so that KeyTool.main() will throw an Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            // instead of calling System.exit()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            KeyTool.main(("-debug "+cmd).split("\\s+"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            out = b1.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            err = b2.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            ex = out;   // now it goes to System.out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            System.setIn(i1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            System.setOut(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            System.setErr(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Call this method if you expect test(input, cmd) should go OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    void testOK(String input, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            test(input, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            afterFail(input, cmd, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Call this method if you expect test(input, cmd) should fail and throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    void testFail(String input, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            test(input, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (e instanceof MissingResourceException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if(ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            afterFail(input, cmd, "FAIL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Call this method if you expect test(input, cmd) should go OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    void testOK(InputStream is, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            test(is, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            afterFail("", cmd, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Call this method if you expect test(input, cmd) should fail and throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    void testFail(InputStream is, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            test(is, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if(ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            afterFail("", cmd, "FAIL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Call this method if you just want to run the command and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * not care if it succeeds or fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    void testAnyway(String input, String cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            test(input, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Helper method, print some output after a test does not do as expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    void afterFail(String input, String cmd, String should) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        System.err.println("\nTest fails for the command ---\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                "keytool " + cmd + "\nOr its debug version ---\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                "keytool -debug " + cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        System.err.println("The command result should be " + should +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                ", but it's not. Try run the command manually and type" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                " these input into it: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        char[] inputChars = input.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        for (int i=0; i<inputChars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            char ch = inputChars[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (ch == '\n') System.err.print("ENTER ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            else if (ch == ' ') System.err.print("SPACE ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            else System.err.print(ch + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        System.err.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        System.err.println("ERR is:\n"+err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        System.err.println("OUT is:\n"+out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    void assertTrue(boolean bool, String msg) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   270
        if (debug) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   271
            System.err.println("If not " + bool + ", " + msg);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   272
        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   273
            System.err.print("v");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   274
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if(!bool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            afterFail(lastInput, lastCommand, "TRUE");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   277
                System.err.println(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new RuntimeException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   282
    void assertTrue(boolean bool) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   283
        assertTrue(bool, "well...");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   284
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Helper method, load a keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param file file for keystore, null or "NONE" for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @pass password for the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @type keystore type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @returns the KeyStore object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @exception Exception if anything goes wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    KeyStore loadStore(String file, String pass, String type) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        KeyStore ks = KeyStore.getInstance(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        FileInputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (file != null && !file.equals("NONE")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            is = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        ks.load(is, pass.toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * The test suite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Maybe it's better to put this outside the KeyToolTest class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    void testAll() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        remove("x2.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        remove("x2.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // name changes: genkeypair, importcert, exportcert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        testOK("", "-keystore x.jks -storepass changeit -exportcert -alias p1 -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        assertTrue(ks.getKey("p1", "changeit".toCharArray()) != null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            "key not DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        assertTrue(new File("x.jks.p1.cert").exists(), "p1 export err");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        testOK("", "-keystore x.jks -storepass changeit -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        testOK("y\n", "-keystore x.jks -storepass changeit -importcert -alias c1 -file x.jks.p1.cert");  // importcert, prompt for Yes/No
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        testOK("", "-keystore x.jks -storepass changeit -importcert -alias c2 -file x.jks.p1.cert -noprompt"); // importcert, -noprompt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        assertTrue(ks.getCertificate("c1") != null, "import c1 err");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // v3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        byte[] encoded = ks.getCertificate("c1").getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        X509CertImpl certImpl = new X509CertImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        assertTrue(certImpl.getVersion() == 3, "Version is not 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // changealias and keyclone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        testOK("changeit\n", "-keystore x.jks -changealias -alias p1 -destalias p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        testOK("changeit\n", "-keystore x.jks -changealias -alias c1 -destalias c11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        testOK("changeit\n\n", "-keystore x.jks -keyclone -alias p11 -destalias p111"); // press ENTER when prompt for p111's keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        assertTrue(!ks.containsAlias("p1"), "there is no p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        assertTrue(!ks.containsAlias("c1"), "there is no c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        assertTrue(ks.containsAlias("p11"), "there is p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        assertTrue(ks.containsAlias("c11"), "there is c11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        assertTrue(ks.containsAlias("p111"), "there is p111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // genSecKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        testOK("changeit\nchangeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -alias s1"); // DES, no need keysize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        testFail("changeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -alias s11 -keysize 128"); // DES, keysize cannot be 128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -keyalg DESede -alias s2"); // DESede. no need keysize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        testFail("changeit\n\n", "-keystore x.jceks -storetype AES -genseckey -keyalg Rijndael -alias s3"); // AES, need keysize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -keyalg AES -alias s3 -keysize 128");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                // about keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        testOK("\n", "-keystore x.jceks -storetype JCEKS -storepass changeit -genseckey -alias s4"); // can accept storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        testOK("keypass\nkeypass\n", "-keystore x.jceks -storetype JCEKS -storepass changeit -genseckey -alias s5"); // or a new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        testOK("bad\n\bad\nkeypass\nkeypass\n", "-keystore x.jceks -storetype JCEKS -storepass changeit -genseckey -alias s6"); // keypass must be valid (prompt 3 times)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        testFail("bad\n\bad\nbad\n", "-keystore x.jceks -storetype JCEKS -storepass changeit -genseckey -alias s7"); // keypass must be valid (prompt 3 times)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        testFail("bad\n\bad\nbad\nkeypass\n", "-keystore x.jceks -storetype JCEKS -storepass changeit -genseckey -alias s7"); // keypass must be valid (prompt 3 times)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        ks = loadStore("x.jceks", "changeit", "JCEKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        assertTrue(ks.getKey("s1", "changeit".toCharArray()).getAlgorithm().equalsIgnoreCase("DES"), "s1 is DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        assertTrue(ks.getKey("s1", "changeit".toCharArray()).getEncoded().length == 8,  "DES is 56");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        assertTrue(ks.getKey("s2", "changeit".toCharArray()).getEncoded().length == 24,  "DESede is 168");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        assertTrue(ks.getKey("s2", "changeit".toCharArray()).getAlgorithm().equalsIgnoreCase("DESede"), "s2 is DESede");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        assertTrue(ks.getKey("s3", "changeit".toCharArray()).getAlgorithm().equalsIgnoreCase("AES"), "s3 is AES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        assertTrue(ks.getKey("s4", "changeit".toCharArray()).getAlgorithm().equalsIgnoreCase("DES"), "s4 is DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        assertTrue(ks.getKey("s5", "keypass".toCharArray()).getAlgorithm().equalsIgnoreCase("DES"), "s5 is DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        assertTrue(ks.getKey("s6", "keypass".toCharArray()).getAlgorithm().equalsIgnoreCase("DES"), "s6 is DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        assertTrue(!ks.containsAlias("s7"), "s7 not created");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // maybe we needn't test this, one day JKS will support SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        //testFail("changeit\nchangeit\n", "-keystore x.jks -genseckey -keyalg AES -alias s3 -keysize 128");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        // importKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        testOK("changeit\nchangeit\n\n", "-keystore x.jceks -storetype JCEKS -genkeypair -alias p1 -dname CN=Olala"); // create 2 entries...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        testOK("", "-keystore x.jceks -storetype JCEKS -storepass changeit -importcert -alias c1 -file x.jks.p1.cert -noprompt"); // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        ks = loadStore("x.jceks", "changeit", "JCEKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        assertTrue(ks.size() == 2, "2 entries in JCEKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // import, shouldn't mention destalias/srckeypass/destkeypass if srcalias is no given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        testFail("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -destalias pp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        testFail("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srckeypass changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        testFail("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -destkeypass changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // normal import
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        assertTrue(ks.size() == 2, "2 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // import again, type yes to overwrite old entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        testOK("changeit\nchangeit\ny\ny\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // import again, specify -nopromt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        testOK("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        assertTrue(err.indexOf("Warning") != -1, "noprompt will warn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        assertTrue(ks.size() == 2, "2 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        // import again, type into new aliases when prompted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        testOK("changeit\nchangeit\n\ns1\n\ns2\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        assertTrue(ks.size() == 4, "4 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // importkeystore single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p1"); // normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        assertTrue(ks.size() == 1, "1 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        testOK("changeit\nchangeit\ny\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p1"); // overwrite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        assertTrue(ks.size() == 1, "1 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        testOK("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p1 -noprompt"); // noprompt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        assertTrue(ks.size() == 1, "1 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        testOK("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p1 -destalias p2"); // rename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        assertTrue(ks.size() == 2, "2 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        testOK("changeit\nchangeit\n\nnewalias\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p1"); // another rename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        assertTrue(ks.size() == 3, "3 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // importkeystore single, different keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        testOK("changeit\nkeypass\nkeypass\n", "-keystore x.jceks -storetype JCEKS -genkeypair -alias p2 -dname CN=Olala"); // generate entry with different keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        testOK("changeit\nchangeit\nchangeit\nkeypass\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p2"); // prompt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        assertTrue(ks.size() == 1, "1 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        testOK("changeit\nchangeit\nkeypass\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p2 -destalias p3 -destkeypass keypass2"); // diff destkeypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        assertTrue(ks.size() == 2, "2 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        assertTrue(ks.getKey("p2", "keypass".toCharArray()) != null, "p2 has old password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        assertTrue(ks.getKey("p3", "keypass2".toCharArray()) != null, "p3 has new password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        // importkeystore single, cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias c1"); // normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        testOK("changeit\n\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias c1 -destalias c2");   // in fact srcstorepass can be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        assertTrue(err.indexOf("WARNING") != -1, "But will warn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        testOK("changeit\n\ny\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias c1 -destalias c2");   // 2nd import, press y to overwrite ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        testOK("changeit\n\n\nc3\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias c1 -destalias c2");   // ... or rename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        assertTrue(ks.size() == 3, "3 entries in JKS"); // c1, c2, c3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // importkeystore, secretkey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -alias s1"); // create SecretKeyEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -alias s2"); // create SecretKeyEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        testOK("changeit\n", "-keystore x.jceks -storetype JCEKS -delete -alias p2"); // remove the keypass!=storepass one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        ks = loadStore("x.jceks", "changeit", "JCEKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        assertTrue(ks.size() == 4, "4 entries in JCEKS");       // p1, c1, s1, s2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias s1"); // normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        assertTrue(err.indexOf("not imported") != -1, "Not imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        assertTrue(err.indexOf("Cannot store non-PrivateKeys") != -1, "Not imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        testOK("\n\n", "-srcstorepass changeit -deststorepass changeit -importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS"); // normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        assertTrue(err.indexOf("s1 not") != -1, "s1 not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        assertTrue(err.indexOf("s2 not") != -1, "s2 not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        assertTrue(err.indexOf("c1 success") != -1, "c1 success");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        assertTrue(err.indexOf("p1 success") != -1, "p1 success");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        testOK("yes\n", "-srcstorepass changeit -deststorepass changeit -importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS"); // normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        // maybe c1 or p1 has been imported before s1 or s2 is touched, anyway we know yesNo is only asked once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        testFail("changeit\nchangeit\n", "-keystore x.jks -genkeypair -alias p1 -dname CN=olala"); // JKS prompt for keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        testOK("changeit\nchangeit\n\n", "-keystore x.jks -genkeypair -alias p1 -dname CN=olala"); // just type ENTER means keypass=storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        testOK("", "-keystore x.p12 -storetype PKCS12 -storepass changeit -genkeypair -alias p0 -dname CN=olala"); // PKCS12 only need storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        testOK("changeit\n", "-keystore x.p12 -storetype PKCS12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        testOK("changeit\n", "-keystore x.p12 -keypass changeit -storetype PKCS12 -genkeypair -alias p3 -dname CN=olala"); // when specify keypass, make sure keypass==storepass...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        assertTrue(err.indexOf("Warning") == -1, "PKCS12 silent when keypass == storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        testOK("changeit\n", "-keystore x.p12 -keypass another -storetype PKCS12 -genkeypair -alias p2 -dname CN=olala"); // otherwise, print a warning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        assertTrue(err.indexOf("Warning") != -1, "PKCS12 warning when keypass != storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        testFail("", "-keystore x.p12 -storepass changeit -storetype PKCS12 -keypasswd -new changeit -alias p3"); // no -keypasswd for PKCS12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        testOK("", "-keystore x.p12 -storepass changeit -storetype PKCS12 -changealias -alias p3 -destalias p33");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        testOK("", "-keystore x.p12 -storepass changeit -storetype PKCS12 -keyclone -alias p33 -destalias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        // pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        testOK("", "-keystore x.p12 -storetype PKCS12 -storepass changeit -genkeypair -alias p0 -dname CN=olala"); // PKCS12 only need storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        testOK("", "-storepass changeit -keystore x.p12 -storetype PKCS12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        testOK("", "-storepass changeit -keystore x.p12 -keypass changeit -storetype PKCS12 -genkeypair -alias p3 -dname CN=olala"); // when specify keypass, make sure keypass==storepass...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        assertTrue(err.indexOf("Warning") == -1, "PKCS12 silent when keypass == storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        testOK("", "-storepass changeit -keystore x.p12 -keypass another -storetype PKCS12 -genkeypair -alias p2 -dname CN=olala"); // otherwise, print a warning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        assertTrue(err.indexOf("Warning") != -1, "PKCS12 warning when keypass != storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        remove("x2.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        remove("x2.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    void testPKCS11() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // pkcs11, the password maybe different and maybe PKCS11 is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // in case last test is not executed successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        testAnyway("", p11Arg + "-storepass test12 -delete -alias nss");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1, "*** MAKE SURE YOU HAVE NO ENTRIES IN YOUR PKCS11 KEYSTORE BEFORE THIS TEST ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        testOK("", p11Arg + "-storepass test12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        testOK("test12\n", p11Arg + "-genkeypair -alias p2 -dname CN=olala2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        testFail("test12\n", p11Arg + "-keypass test12 -genkeypair -alias p3 -dname CN=olala3"); // cannot provide keypass for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        testFail("test12\n", p11Arg + "-keypass nonsense -genkeypair -alias p3 -dname CN=olala3"); // cannot provide keypass for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1, "2 entries in p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        testOK("test12\n", p11Arg + "-alias p1 -changealias -destalias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        testOK("", p11Arg + "-storepass test12 -list -alias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        testFail("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        testOK("test12\n", p11Arg + "-alias p3 -keyclone -destalias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        testFail("", p11Arg + "-storepass test12 -list -alias p3");   // in PKCS11, keyclone will delete old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        testOK("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        testFail("test12\n", p11Arg + "-alias p1 -keypasswd -new another"); // cannot change password for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1, "2 entries in p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1, "*** MAKE SURE YOU HAVE NO ENTRIES IN YOUR PKCS11 KEYSTORE BEFORE THIS TEST ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    void testPKCS11ImportKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        testOK("", p11Arg + "-storepass test12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        testOK("test12\n", p11Arg + "-genkeypair -alias p2 -dname CN=olala2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        // test importkeystore for pkcs11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        // pkcs11 -> jks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        testOK("changeit\nchangeit\ntest12\n", srcP11Arg + "-importkeystore -destkeystore x.jks -deststoretype JKS -srcalias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        assertTrue(err.indexOf("not imported") != -1, "cannot import key without destkeypass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        assertTrue(!ks.containsAlias("p1"), "p1 is not imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        testOK("changeit\ntest12\n", srcP11Arg + "-importkeystore -destkeystore x.jks -deststoretype JKS -srcalias p1 -destkeypass changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        testOK("changeit\ntest12\n", srcP11Arg + "-importkeystore -destkeystore x.jks -deststoretype JKS -srcalias p2 -destkeypass changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        assertTrue(ks.containsAlias("p1"), "p1 is imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        assertTrue(ks.containsAlias("p2"), "p2 is imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        // jks -> pkcs11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        testOK("test12\nchangeit\n", p11Arg + "-importkeystore -srckeystore x.jks -srcstoretype JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        testOK("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        testOK("", p11Arg + "-storepass test12 -list -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1, "2 entries in p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        // clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1, "empty p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    // The sqeTest reflects the test suggested by judy.gao and bill.situ at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    // /net/sqesvr-nfs/global/nfs/sec/ws_6.0_int/security/src/SecurityTools/Keytool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    void sqeTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        FileOutputStream fos = new FileOutputStream("badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            fos.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        sqeCsrTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        sqePrintcertTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        sqeDeleteTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        sqeExportTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        sqeGenkeyTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        sqeImportTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        sqeKeyclonetest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        sqeKeypasswdTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        sqeListTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        sqeSelfCertTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        sqeStorepassTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        remove("badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    // Import: cacert, prompt, trusted, non-trusted, bad chain, not match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    void sqeImportTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        /* deleted */ testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        /* deleted */ testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        testOK("yes\n", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        assertTrue(ks.containsAlias("mykey"), "imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        /* deleted */ testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        testOK("\n", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        assertTrue(!ks.containsAlias("mykey"), "imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        testOK("no\n", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        assertTrue(!ks.containsAlias("mykey"), "imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        testFail("no\n", "-keystore x.jks -storepass changeit -importcert -file nonexist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        testFail("no\n", "-keystore x.jks -storepass changeit -importcert -file x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    // keyclone: exist. nonexist err, cert err, dest exist, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    void sqeKeyclonetest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -new newpass -keyclone -dest p0"); // new pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        testOK("\n", "-keystore x.jks -storepass changeit -keypass changeit -keyclone -dest p1"); // new pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        testOK("\n", "-keystore x.jks -storepass changeit -keyclone -dest p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        testFail("\n", "-keystore x.jks -storepass changeit -keyclone -dest p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        testFail("\n", "-keystore x.jks -storepass changeit -keyclone -dest p3 -alias noexist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        // no cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -new newpass -keyclone -dest p0"); // new pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    // keypasswd: exist, short, nonexist err, cert err, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    void sqeKeypasswdTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        testOK("newpass\nnewpass\n", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        testOK("new\nnew\nnewpass\nnewpass\n", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        testOK("", "-keystore x.jks -storepass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        testOK("changeit\n", "-keystore x.jks -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        testFail("", "-keystore x.jks -storepass badpass -keypass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        testFail("", "-keystore x.jks -storepass changeit -keypass bad -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        // no cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        // diff pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        testOK("", "-keystore x.jks -storepass changeit -keypass keypass -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        testFail("", "-keystore x.jks -storepass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        testOK("keypass\n", "-keystore x.jks -storepass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        // i hate those misc test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    // list: -f -alias, exist, nonexist err; otherwise, check all shows, -rfc shows more, and misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    void sqeListTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        testOK("", "-keystore x.jks -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        testOK("", "-keystore x.jks -storepass changeit -list -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        testFail("", "-keystore x.jks -storepass changeit -list -alias notexist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        testFail("", "-keystore x.jks -storepass badpass -list -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        testOK("", "-keystore x.jks -storepass changeit -keypass badpass -list -alias mykey");  // keypass ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        testOK("\n", "-keystore x.jks -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        assertTrue(err.indexOf("WARNING") != -1, "no storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        testOK("changeit\n", "-keystore x.jks -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        assertTrue(err.indexOf("WARNING") == -1, "has storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        testFail("badpass\n", "-keystore x.jks -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        // misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        testFail("", "-keystore aa\\bb//cc -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        testFail("", "-keystore nonexisting -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        testFail("", "-keystore badkeystore -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    // selfcert: exist, non-exist err, cert err, sig..., dname, wrong keypass, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    void sqeSelfCertTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        testOK("", "-keystore x.jks -storepass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert -alias nonexisting"); // not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert -dname CN=NewName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert -sigalg MD5withRSA"); // sig not compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        testFail("", "-keystore x.jks -storepass wrong -keypass changeit -selfcert"); // bad pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        testFail("", "-keystore x.jks -storepass changeit -keypass wrong -selfcert"); // bad pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        //misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        testFail("", "-keystore nonexist -storepass changeit -keypass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        testFail("", "-keystore aa//dd\\gg -storepass changeit -keypass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        // diff pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        testOK("", "-keystore x.jks -storepass changeit -keypass keypass -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        testFail("", "-keystore x.jks -storepass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        testOK("keypass\n", "-keystore x.jks -storepass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        testFail("", "-keystore x.jks -storepass changeit -selfcert");  // certentry cannot do selfcert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    // storepass: bad old, short new, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    void sqeStorepassTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        testOK("", "-storepasswd -keystore x.jks -storepass changeit -new newstore"); // all in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        testOK("changeit\nnewstore\nnewstore\n", "-storepasswd -keystore x.jks"); // all not in arg, new twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        testOK("changeit\n", "-storepasswd -keystore x.jks -new newstore"); // new in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        testOK("newstore\nnewstore\n", "-storepasswd -keystore x.jks -storepass changeit"); // old in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        testOK("new\nnew\nnewstore\nnewstore\n", "-storepasswd -keystore x.jks -storepass changeit"); // old in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        testFail("", "-storepasswd -keystore x.jks -storepass badold -new newstore"); // bad old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        testFail("", "-storepasswd -keystore x.jks -storepass changeit -new new"); // short new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        // misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        testFail("", "-storepasswd -keystore nonexist -storepass changeit -new newstore"); // non exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        testFail("", "-storepasswd -keystore badkeystore -storepass changeit -new newstore"); // bad file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        testFail("", "-storepasswd -keystore aa\\bb//cc//dd -storepass changeit -new newstore"); // bad file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    void sqeGenkeyTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -alias newentry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -alias newentry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg DSA -alias n1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -alias n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg NoSuchAlg -alias n3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 56 -alias n4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 999 -alias n5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 512 -alias n6");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 1024 -alias n7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -sigalg NoSuchAlg -alias n8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -sigalg MD2withRSA -alias n9");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -sigalg MD5withRSA -alias n10");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -sigalg SHA1withRSA -alias n11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -sigalg NoSuchAlg -alias n12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        testFail("", "-keystore badkeystore -storepass changeit -keypass changeit -genkeypair -dname CN=olala -alias n14");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        testFail("", "-keystore x.jks -storepass badpass -keypass changeit -genkeypair -dname CN=olala -alias n16");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CNN=olala -alias n17");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    void sqeExportTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        testFail("", "-keystore x.jks -storepass changeit -export -file mykey.cert -alias mykey"); // nonexist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        testOK("", "-keystore x.jks -storepass changeit -export -file mykey.cert -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        testOK("", "-keystore x.jks -storepass changeit -import -file mykey.cert -noprompt -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        testOK("", "-keystore x.jks -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        testFail("", "-keystore nonexistkeystore -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        testFail("", "-keystore badkeystore -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        testFail("", "-keystore x.jks -storepass badpass -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        remove("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        remove("mykey.cert2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    void sqeDeleteTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        testFail("", "-keystore x.jks -storepass changeit -delete -alias mykey"); // nonexist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit -delete -alias mykey"); // keystore name illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        testFail("", "-keystore nonexistkeystore -storepass changeit -delete -alias mykey"); // keystore not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        testFail("", "-keystore badkeystore -storepass changeit -delete -alias mykey"); // keystore invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        testFail("", "-keystore x.jks -storepass xxxxxxxx -delete -alias mykey"); // wrong pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    void sqeCsrTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        remove("csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        // PrivateKeyEntry can do certreq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg SHA1withDSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg MD5withRSA"); // unmatched sigalg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        // misc test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        testFail("", "-keystore x.jks -storepass badstorepass -certreq -file csr1"); // bad storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        testOK("changeit\n", "-keystore x.jks -certreq -file csr1"); // storepass from terminal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        testFail("\n", "-keystore x.jks -certreq -file csr1"); // must provide storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        testFail("", "-keystore x.jks -storepass changeit -keypass badkeypass -certreq -file csr1"); // bad keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        testFail("", "-keystore x.jks -storepass changeit -certreq -file aa\\bb//cc\\dd");  // bad filepath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        testFail("", "-keystore noexistks -storepass changeit -certreq -file csr1"); // non-existing keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        // Try the RSA private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg SHA1withDSA"); // unmatched sigalg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg MD5withRSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        // TrustedCertificateEntry cannot do certreq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1 -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        remove("csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    void sqePrintcertTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        remove("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        testOK("", "-keystore x.jks -storepass changeit -export -file mykey.cert -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        testFail("", "-printcert -file badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        testFail("", "-printcert -file a/b/c/d");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        testOK("", "-printcert -file mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        FileInputStream fin = new FileInputStream("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        testOK(fin, "-printcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        fin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        remove("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   842
    void v3extTest(String keyAlg) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   843
        KeyStore ks;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   844
        remove("x.jks");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   845
        String simple = "-keystore x.jks -storepass changeit -keypass changeit -noprompt -keyalg " + keyAlg + " ";
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   846
        String pre = simple + "-genkeypair -dname CN=Olala -alias ";
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   847
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   848
        // Version and SKID
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   849
        testOK("", pre + "o1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   850
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   851
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   852
        assertTrue(((X509Certificate)ks.getCertificate("o1")).getVersion() == 3);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   853
        assertTrue(((X509CertImpl)ks.getCertificate("o1")).getSubjectKeyIdentifierExtension() != null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   854
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   855
        // BC
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   856
        testOK("", pre + "b1 -ext BC:critical");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   857
        testOK("", pre + "b2 -ext BC");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   858
        testOK("", pre + "b3 -ext bc");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   859
        testOK("", pre + "b4 -ext BasicConstraints");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   860
        testOK("", pre + "b5 -ext basicconstraints");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   861
        testOK("", pre + "b6 -ext BC=ca:true,pathlen:12");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   862
        testOK("", pre + "b7 -ext BC=ca:false");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   863
        testOK("", pre + "b8 -ext BC:critical=ca:false");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   864
        testOK("", pre + "b9 -ext BC=12");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   865
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   866
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   867
        assertTrue(((X509CertImpl)ks.getCertificate("b1")).getBasicConstraintsExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   868
        assertTrue(!((X509CertImpl)ks.getCertificate("b2")).getBasicConstraintsExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   869
        assertTrue(((X509CertImpl)ks.getCertificate("b8")).getBasicConstraintsExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   870
        assertTrue(((X509Certificate)ks.getCertificate("b1")).getBasicConstraints() == Integer.MAX_VALUE);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   871
        assertTrue(((X509Certificate)ks.getCertificate("b2")).getBasicConstraints() == Integer.MAX_VALUE);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   872
        assertTrue(((X509Certificate)ks.getCertificate("b3")).getBasicConstraints() == Integer.MAX_VALUE);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   873
        assertTrue(((X509Certificate)ks.getCertificate("b4")).getBasicConstraints() == Integer.MAX_VALUE);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   874
        assertTrue(((X509Certificate)ks.getCertificate("b5")).getBasicConstraints() == Integer.MAX_VALUE);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   875
        assertTrue(((X509Certificate)ks.getCertificate("b6")).getBasicConstraints() == 12);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   876
        assertTrue(((X509Certificate)ks.getCertificate("b7")).getBasicConstraints() == -1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   877
        assertTrue(((X509Certificate)ks.getCertificate("b9")).getBasicConstraints() == 12);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   878
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   879
        // KU
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   880
        testOK("", pre + "ku1 -ext KeyUsage:critical=digitalsignature");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   881
        testOK("", pre + "ku2 -ext KU=digitalSignature");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   882
        testOK("", pre + "ku3 -ext KU=ds");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   883
        testOK("", pre + "ku4 -ext KU=dig");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   884
        testFail("", pre + "ku5 -ext KU=d");    // ambigous value
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   885
        testFail("", pre + "ku6 -ext KU=cs");   // cRLSign cannot be cs
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   886
        testOK("", pre + "ku11 -ext KU=nr");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   887
        testFail("", pre + "ku12 -ext KU=ke");  // ke also means keyAgreement
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   888
        testOK("", pre + "ku12 -ext KU=keyE");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   889
        testFail("", pre + "ku13 -ext KU=de");  // de also means decipherOnly
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   890
        testOK("", pre + "ku13 -ext KU=dataE");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   891
        testOK("", pre + "ku14 -ext KU=ka");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   892
        testOK("", pre + "ku15 -ext KU=kcs");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   893
        testOK("", pre + "ku16 -ext KU=crls");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   894
        testOK("", pre + "ku17 -ext KU=eo");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   895
        testOK("", pre + "ku18 -ext KU=do");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   896
        testOK("", pre + "ku19 -ext KU=cc");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   897
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   898
        testOK("", pre + "ku017 -ext KU=ds,cc,eo");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   899
        testOK("", pre + "ku135 -ext KU=nr,dataEncipherment,keyCertSign");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   900
        testOK("", pre + "ku246 -ext KU=keyEnc,cRL,keyA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   901
        testOK("", pre + "ku1234 -ext KU=ka,da,keyE,nonR");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   902
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   903
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   904
        class CheckKU {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   905
            void check(KeyStore ks, String alias, int... pos) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   906
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   907
                boolean[] bs = ((X509Certificate)ks.getCertificate(alias)).getKeyUsage();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   908
                bs = Arrays.copyOf(bs, 9);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   909
                for (int i=0; i<bs.length; i++) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   910
                    boolean found = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   911
                    for (int p: pos) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   912
                        if (p == i) found = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   913
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   914
                    if (!found ^ bs[i]) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   915
                        // OK
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   916
                    } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   917
                        throw new RuntimeException("KU not match at " + i +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   918
                                ": " + found + " vs " + bs[i]);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   919
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   920
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   921
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   922
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   923
        CheckKU c = new CheckKU();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   924
        assertTrue(((X509CertImpl)ks.getCertificate("ku1")).getExtension(PKIXExtensions.KeyUsage_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   925
        assertTrue(!((X509CertImpl)ks.getCertificate("ku2")).getExtension(PKIXExtensions.KeyUsage_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   926
        c.check(ks, "ku1", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   927
        c.check(ks, "ku2", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   928
        c.check(ks, "ku3", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   929
        c.check(ks, "ku4", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   930
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   931
        c.check(ks, "ku12", 2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   932
        c.check(ks, "ku13", 3);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   933
        c.check(ks, "ku14", 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   934
        c.check(ks, "ku15", 5);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   935
        c.check(ks, "ku16", 6);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   936
        c.check(ks, "ku17", 7);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   937
        c.check(ks, "ku18", 8);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   938
        c.check(ks, "ku19", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   939
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   940
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   941
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   942
        c.check(ks, "ku017", 0, 1, 7);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   943
        c.check(ks, "ku135", 1, 3, 5);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   944
        c.check(ks, "ku246", 6, 2, 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   945
        c.check(ks, "ku1234", 1, 2, 3, 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   946
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   947
        // EKU
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   948
        testOK("", pre + "eku1 -ext EKU:critical=sa");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   949
        testOK("", pre + "eku2 -ext ExtendedKeyUsage=ca");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   950
        testOK("", pre + "eku3 -ext EKU=cs");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   951
        testOK("", pre + "eku4 -ext EKU=ep");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   952
        testOK("", pre + "eku8 -ext EKU=ts");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   953
        testFail("", pre + "eku9 -ext EKU=os");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   954
        testOK("", pre + "eku9 -ext EKU=ocsps");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   955
        testOK("", pre + "eku10 -ext EKU=any");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   956
        testOK("", pre + "eku11 -ext EKU=1.2.3.4,1.3.5.7,ep");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   957
        testFail("", pre + "eku12 -ext EKU=c");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   958
        testFail("", pre + "eku12 -ext EKU=nothing");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   959
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   960
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   961
        class CheckEKU {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   962
            void check(KeyStore ks, String alias, String... pos) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   963
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   964
                List<String> bs = ((X509Certificate)ks.getCertificate(alias)).getExtendedKeyUsage();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   965
                int found = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   966
                for (String p: pos) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   967
                    if (bs.contains(p)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   968
                        found++;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   969
                    } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   970
                        throw new RuntimeException("EKU: not included " + p);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   971
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   972
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   973
                if (found != bs.size()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   974
                    throw new RuntimeException("EKU: more items than expected");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   975
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   976
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   977
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   978
        CheckEKU cx = new CheckEKU();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   979
        assertTrue(((X509CertImpl)ks.getCertificate("eku1")).getExtension(PKIXExtensions.ExtendedKeyUsage_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   980
        assertTrue(!((X509CertImpl)ks.getCertificate("eku2")).getExtension(PKIXExtensions.ExtendedKeyUsage_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   981
        cx.check(ks, "eku1", "1.3.6.1.5.5.7.3.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   982
        cx.check(ks, "eku2", "1.3.6.1.5.5.7.3.2");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   983
        cx.check(ks, "eku3", "1.3.6.1.5.5.7.3.3");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   984
        cx.check(ks, "eku4", "1.3.6.1.5.5.7.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   985
        cx.check(ks, "eku8", "1.3.6.1.5.5.7.3.8");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   986
        cx.check(ks, "eku9", "1.3.6.1.5.5.7.3.9");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   987
        cx.check(ks, "eku10", "2.5.29.37.0");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   988
        cx.check(ks, "eku11", "1.3.6.1.5.5.7.3.4", "1.2.3.4", "1.3.5.7");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   989
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   990
        // SAN
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   991
        testOK("", pre+"san1 -ext san:critical=email:me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   992
        testOK("", pre+"san2 -ext san=uri:http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   993
        testOK("", pre+"san3 -ext san=dns:me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   994
        testOK("", pre+"san4 -ext san=ip:192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   995
        testOK("", pre+"san5 -ext san=oid:1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   996
        testOK("", pre+"san235 -ext san=uri:http://me.org,dns:me.org,oid:1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   997
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   998
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   999
        class CheckSAN {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1000
            // Please sort items with name type
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1001
            void check(KeyStore ks, String alias, int type, Object... items) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1002
                int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1003
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1004
                Object[] names = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1005
                if (type == 0) names = ((X509Certificate)ks.getCertificate(alias)).getSubjectAlternativeNames().toArray();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1006
                else names = ((X509Certificate)ks.getCertificate(alias)).getIssuerAlternativeNames().toArray();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1007
                Arrays.sort(names, new Comparator() {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1008
                    public int compare(Object o1, Object o2) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1009
                        int i1 = (Integer)((List)o1).get(0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1010
                        int i2 = (Integer)((List)o2).get(0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1011
                        return i1 - i2;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1012
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1013
                });
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1014
                for (Object o: names) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1015
                    List l = (List)o;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1016
                    for (Object o2: l) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1017
                        if (!items[pos++].equals(o2)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1018
                            throw new RuntimeException("Not equals at " + pos
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1019
                                    + ": " + items[pos-1] + " vs " + o2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1020
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1021
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1022
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1023
                if (pos != items.length) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1024
                    throw new RuntimeException("Extra items, pos is " + pos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1025
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1026
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1027
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1028
        CheckSAN csan = new CheckSAN();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1029
        assertTrue(((X509CertImpl)ks.getCertificate("san1")).getSubjectAlternativeNameExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1030
        assertTrue(!((X509CertImpl)ks.getCertificate("san2")).getSubjectAlternativeNameExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1031
        csan.check(ks, "san1", 0, 1, "me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1032
        csan.check(ks, "san2", 0, 6, "http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1033
        csan.check(ks, "san3", 0, 2, "me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1034
        csan.check(ks, "san4", 0, 7, "192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1035
        csan.check(ks, "san5", 0, 8, "1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1036
        csan.check(ks, "san235", 0, 2, "me.org", 6, "http://me.org", 8, "1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1037
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1038
        // IAN
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1039
        testOK("", pre+"ian1 -ext ian:critical=email:me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1040
        testOK("", pre+"ian2 -ext ian=uri:http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1041
        testOK("", pre+"ian3 -ext ian=dns:me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1042
        testOK("", pre+"ian4 -ext ian=ip:192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1043
        testOK("", pre+"ian5 -ext ian=oid:1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1044
        testOK("", pre+"ian235 -ext ian=uri:http://me.org,dns:me.org,oid:1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1045
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1046
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1047
        assertTrue(((X509CertImpl)ks.getCertificate("ian1")).getIssuerAlternativeNameExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1048
        assertTrue(!((X509CertImpl)ks.getCertificate("ian2")).getIssuerAlternativeNameExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1049
        csan.check(ks, "ian1", 1, 1, "me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1050
        csan.check(ks, "ian2", 1, 6, "http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1051
        csan.check(ks, "ian3", 1, 2, "me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1052
        csan.check(ks, "ian4", 1, 7, "192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1053
        csan.check(ks, "ian5", 1, 8, "1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1054
        csan.check(ks, "ian235", 1, 2, "me.org", 6, "http://me.org", 8, "1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1055
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1056
        // SIA
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1057
        testOK("", pre+"sia1 -ext sia=care:uri:ldap://ca.com/cn=CA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1058
        testOK("", pre+"sia2 -ext sia=ts:email:ts@ca.com");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1059
        testFail("SIA never critical", pre+"sia3 -ext sia:critical=ts:email:ts@ca.com");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1060
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1061
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1062
        class CheckSia {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1063
            void check(KeyStore ks, String alias, int type, Object... items) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1064
                int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1065
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1066
                AccessDescription[] ads = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1067
                if (type == 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1068
                    SubjectInfoAccessExtension siae = (SubjectInfoAccessExtension)((X509CertImpl)ks.getCertificate(alias)).getExtension(PKIXExtensions.SubjectInfoAccess_Id);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1069
                    ads = siae.getAccessDescriptions().toArray(new AccessDescription[0]);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1070
                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1071
                    AuthorityInfoAccessExtension aiae = (AuthorityInfoAccessExtension)((X509CertImpl)ks.getCertificate(alias)).getExtension(PKIXExtensions.AuthInfoAccess_Id);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1072
                    ads = aiae.getAccessDescriptions().toArray(new AccessDescription[0]);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1073
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1074
                Arrays.sort(ads, new Comparator<AccessDescription>() {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1075
                    @Override
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1076
                    public int compare(AccessDescription o1, AccessDescription o2) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1077
                        return o1.getAccessMethod().toString().compareTo(o2.getAccessMethod().toString());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1078
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1079
                });
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1080
                for (AccessDescription ad: ads) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1081
                    if (!ad.getAccessMethod().equals(items[pos++]) ||
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1082
                            !new Integer(ad.getAccessLocation().getType()).equals(items[pos++])) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1083
                        throw new RuntimeException("Not same type at " + pos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1084
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1085
                    String name = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1086
                    switch (ad.getAccessLocation().getType()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1087
                        case 1:
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1088
                            name = ((RFC822Name)ad.getAccessLocation().getName()).getName();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1089
                            break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1090
                        case 6:
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1091
                            name = ((URIName)ad.getAccessLocation().getName()).getURI().toString();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1092
                            break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1093
                        default:
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1094
                            throw new RuntimeException("Not implemented: " + ad);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1095
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1096
                    if (!name.equals(items[pos++])) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1097
                        throw new Exception("Name not same for " + ad + " at pos " + pos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1098
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1099
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1100
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1101
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1102
        CheckSia csia = new CheckSia();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1103
        assertTrue(!((X509CertImpl)ks.getCertificate("sia1")).getExtension(PKIXExtensions.SubjectInfoAccess_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1104
        csia.check(ks, "sia1", 0, AccessDescription.Ad_CAREPOSITORY_Id, 6, "ldap://ca.com/cn=CA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1105
        csia.check(ks, "sia2", 0, AccessDescription.Ad_TIMESTAMPING_Id, 1, "ts@ca.com");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1106
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1107
        // AIA
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1108
        testOK("", pre+"aia1 -ext aia=cai:uri:ldap://ca.com/cn=CA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1109
        testOK("", pre+"aia2 -ext aia=ocsp:email:ocsp@ca.com");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1110
        testFail("AIA never critical", pre+"aia3 -ext aia:critical=ts:email:ts@ca.com");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1111
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1112
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1113
        assertTrue(!((X509CertImpl)ks.getCertificate("aia1")).getExtension(PKIXExtensions.AuthInfoAccess_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1114
        csia.check(ks, "aia1", 1, AccessDescription.Ad_CAISSUERS_Id, 6, "ldap://ca.com/cn=CA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1115
        csia.check(ks, "aia2", 1, AccessDescription.Ad_OCSP_Id, 1, "ocsp@ca.com");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1116
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1117
        // OID
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1118
        testOK("", pre+"oid1 -ext 1.2.3:critical=0102");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1119
        testOK("", pre+"oid2 -ext 1.2.3");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1120
        testOK("", pre+"oid12 -ext 1.2.3 -ext 1.2.4=01:02:03");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1121
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1122
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1123
        class CheckOid {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1124
            void check(KeyStore ks, String alias, String oid, byte[] value) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1125
                int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1126
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1127
                Extension ex = ((X509CertImpl)ks.getCertificate(alias)).getExtension(new ObjectIdentifier(oid));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1128
                if (!Arrays.equals(value, ex.getValue())) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1129
                    throw new RuntimeException("Not same content in " + alias + " for " + oid);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1130
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1131
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1132
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1133
        CheckOid coid = new CheckOid();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1134
        assertTrue(((X509CertImpl)ks.getCertificate("oid1")).getExtension(new ObjectIdentifier("1.2.3")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1135
        assertTrue(!((X509CertImpl)ks.getCertificate("oid2")).getExtension(new ObjectIdentifier("1.2.3")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1136
        coid.check(ks, "oid1", "1.2.3", new byte[]{1,2});
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1137
        coid.check(ks, "oid2", "1.2.3", new byte[]{});
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1138
        coid.check(ks, "oid12", "1.2.3", new byte[]{});
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1139
        coid.check(ks, "oid12", "1.2.4", new byte[]{1,2,3});
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1140
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1141
        // honored
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1142
        testOK("", pre+"ca");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1143
        testOK("", pre+"a");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1144
        // request: BC,KU,1.2.3,1.2.4,1.2.5
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1145
        testOK("", simple+"-alias a -certreq " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1146
                "-ext BC=1 -ext KU=crl " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1147
                "-ext 1.2.3=01 -ext 1.2.4:critical=0102 -ext 1.2.5=010203 " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1148
                "-rfc -file test.req");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1149
        // printcertreq
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1150
        testOK("", "-printcertreq -file test.req");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1151
        // issue: deny KU, change criticality of 1.2.3 and 1.2.4, change content of BC, add 2.3.4
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1152
        testOK("", simple+"-gencert -alias ca -infile test.req -ext " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1153
                "honored=all,-KU,1.2.3:critical,1.2.4:non-critical " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1154
                "-ext BC=2 -ext 2.3.4=01020304 " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1155
                "-debug -rfc -outfile test.cert");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1156
        testOK("", simple+"-importcert -file test.cert -alias a");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1157
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1158
        X509CertImpl a = (X509CertImpl)ks.getCertificate("a");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1159
        assertTrue(a.getAuthorityKeyIdentifierExtension() != null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1160
        assertTrue(a.getSubjectKeyIdentifierExtension() != null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1161
        assertTrue(a.getKeyUsage() == null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1162
        assertTrue(a.getExtension(new ObjectIdentifier("1.2.3")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1163
        assertTrue(!a.getExtension(new ObjectIdentifier("1.2.4")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1164
        assertTrue(!a.getExtension(new ObjectIdentifier("1.2.5")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1165
        assertTrue(a.getExtensionValue("1.2.3").length == 3);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1166
        assertTrue(a.getExtensionValue("1.2.4").length == 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1167
        assertTrue(a.getExtensionValue("1.2.5").length == 5);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1168
        assertTrue(a.getBasicConstraints() == 2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1169
        assertTrue(!a.getExtension(new ObjectIdentifier("2.3.4")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1170
        assertTrue(a.getExtensionValue("2.3.4").length == 6);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1171
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1172
        remove("x.jks");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1173
        remove("test.req");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1174
        remove("test.cert");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1175
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1176
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    void i18nTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        //   1.  keytool -help
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        remove("x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1180
        testOK("", "-help");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1181
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        //   2. keytool -genkey -v -keysize 512 Enter "a" for the keystore password. Check error (password too short). Enter "password" for the keystore password. Hit 'return' for "first and last name", "organizational unit", "City", "State", and "Country Code". Type "yes" when they ask you if everything is correct. Type 'return' for new key password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        testOK("a\npassword\npassword\nMe\nHere\nNow\nPlace\nPlace\nUS\nyes\n\n", "-genkey -v -keysize 512 -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        //   3. keytool -list -v -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        testOK("", "-list -v -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        //   4. keytool -list -v Type "a" for the keystore password. Check error (wrong keystore password).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        testFail("a\n", "-list -v -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1188
        assertTrue(ex.indexOf("password was incorrect") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        //   5. keytool -genkey -v -keysize 512 Enter "password" as the password. Check error (alias 'mykey' already exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        testFail("password\n", "-genkey -v -keysize 512 -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1191
        assertTrue(ex.indexOf("alias <mykey> already exists") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        //   6. keytool -genkey -v -keysize 512 -alias mykey2 -storepass password Hit 'return' for "first and last name", "organizational unit", "City", "State", and "Country Code". Type "yes" when they ask you if everything is correct. Type 'return' for new key password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        testOK("\n\n\n\n\n\nyes\n\n", "-genkey -v -keysize 512 -alias mykey2 -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        //   7. keytool -list -v Type 'password' for the store password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        testOK("password\n", "-list -v -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        //   8. keytool -keypasswd -v -alias mykey2 -storepass password Type "a" for the new key password. Type "aaaaaa" for the new key password. Type "bbbbbb" when re-entering the new key password. Type "a" for the new key password. Check Error (too many failures).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        testFail("a\naaaaaa\nbbbbbb\na\n", "-keypasswd -v -alias mykey2 -storepass password -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1198
        assertTrue(ex.indexOf("Too many failures - try later") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        //   9. keytool -keypasswd -v -alias mykey2 -storepass password Type "aaaaaa" for the new key password. Type "aaaaaa" when re-entering the new key password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        testOK("aaaaaa\naaaaaa\n", "-keypasswd -v -alias mykey2 -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        //  10. keytool -selfcert -v -alias mykey -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        testOK("", "-selfcert -v -alias mykey -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        //  11. keytool -list -v -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        testOK("", "-list -v -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        //  12. keytool -export -v -alias mykey -file cert -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        remove("cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        testOK("", "-export -v -alias mykey -file cert -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        //  13. keytool -import -v -file cert -storepass password Check error (Certificate reply and cert are the same)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        testFail("", "-import -v -file cert -storepass password -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1210
        assertTrue(ex.indexOf("Certificate reply and certificate in keystore are identical") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        //  14. keytool -printcert -file cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        testOK("", "-printcert -file cert -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        remove("cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        //  15. keytool -list -storepass password -provider sun.security.provider.Sun
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        testOK("", "-list -storepass password -provider sun.security.provider.Sun -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        //Error tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        //   1. keytool -storepasswd -storepass password -new abc Check error (password too short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        testFail("", "-storepasswd -storepass password -new abc");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1221
        assertTrue(ex.indexOf("New password must be at least 6 characters") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        // Changed, no NONE needed now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        //   2. keytool -list -storetype PKCS11 Check error (-keystore must be NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        //testFail("", "-list -storetype PKCS11");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1225
        //assertTrue(err.indexOf("keystore must be NONE") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        //   3. keytool -storepasswd -storetype PKCS11 -keystore NONE Check error (unsupported operation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        testFail("", "-storepasswd -storetype PKCS11 -keystore NONE");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1228
        assertTrue(ex.indexOf("UnsupportedOperationException") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        //   4. keytool -keypasswd -storetype PKCS11 -keystore NONE Check error (unsupported operation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        testFail("", "-keypasswd -storetype PKCS11 -keystore NONE");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1231
        assertTrue(ex.indexOf("UnsupportedOperationException") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        //   5. keytool -list -protected -storepass password Check error (password can not be specified with -protected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        testFail("", "-list -protected -storepass password -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1234
        assertTrue(ex.indexOf("if -protected is specified, then") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        //   6. keytool -keypasswd -protected -keypass password Check error (password can not be specified with -protected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        testFail("", "-keypasswd -protected -keypass password -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1237
        assertTrue(ex.indexOf("if -protected is specified, then") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        //   7. keytool -keypasswd -protected -new password Check error (password can not be specified with -protected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        testFail("", "-keypasswd -protected -new password -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1240
        assertTrue(ex.indexOf("if -protected is specified, then") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    void i18nPKCS11Test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        //PKCS#11 tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        //   1. sccs edit cert8.db key3.db
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        //Runtime.getRuntime().exec("/usr/ccs/bin/sccs edit cert8.db key3.db");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        testOK("", p11Arg + "-storepass test12 -genkey -alias genkey -dname cn=genkey -keysize 512 -keyalg rsa");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        testOK("", p11Arg + "-storepass test12 -list -alias genkey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        testOK("", p11Arg + "-storepass test12 -certreq -alias genkey -file genkey.certreq");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        testOK("", p11Arg + "-storepass test12 -export -alias genkey -file genkey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        testOK("", "-printcert -file genkey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        testOK("", p11Arg + "-storepass test12 -selfcert -alias genkey -dname cn=selfCert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        testOK("", p11Arg + "-storepass test12 -list -alias genkey -v");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1257
        assertTrue(out.indexOf("Owner: CN=selfCert") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        //(check that cert subject DN is [cn=selfCert])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        testOK("", p11Arg + "-storepass test12 -delete -alias genkey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        testOK("", p11Arg + "-storepass test12 -list");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1261
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        //(check for empty database listing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        //Runtime.getRuntime().exec("/usr/ccs/bin/sccs unedit cert8.db key3.db");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        remove("genkey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        remove("genkey.certreq");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        //  12. sccs unedit cert8.db key3.db
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    // tesing new option -srcProviderName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    void sszzTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        testAnyway("", NSS_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        testAnyway("", NZZ_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        testOK("", NSS_P11_ARG+"-genkeypair -dname CN=NSS -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        testOK("", NSS_SRC_P11_ARG + NZZ_P11_ARG +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                "-importkeystore -srcstorepass test12 -deststorepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        testAnyway("", NSS_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        testAnyway("", NZZ_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        // first test if HumanInputStream really acts like a human being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        HumanInputStream.test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        KeyToolTest t = new KeyToolTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        if (System.getProperty("file") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            t.sqeTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            t.testAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            t.i18nTest();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1289
            t.v3extTest("RSA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1290
            t.v3extTest("DSA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1291
            boolean testEC = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1292
            try {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1293
                KeyPairGenerator.getInstance("EC");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1294
            } catch (NoSuchAlgorithmException nae) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1295
                testEC = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1296
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1297
            if (testEC) t.v3extTest("EC");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        if (System.getProperty("nss") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            t.srcP11Arg = NSS_SRC_P11_ARG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            t.p11Arg = NSS_P11_ARG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            t.testPKCS11();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            // FAIL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            // 1. we still don't have srcprovidername yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            // 2. cannot store privatekey into NSS keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            //    java.security.KeyStoreException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_TEMPLATE_INCOMPLETE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            //t.testPKCS11ImportKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            t.i18nPKCS11Test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            //FAIL: currently PKCS11-NSS does not support 2 NSS KeyStores to be loaded at the same time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            //t.sszzTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        if (System.getProperty("solaris") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            // For Solaris Cryptography Framework
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            t.srcP11Arg = SUN_SRC_P11_ARG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            t.p11Arg = SUN_P11_ARG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            t.testPKCS11();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            t.testPKCS11ImportKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            t.i18nPKCS11Test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        System.out.println("Test pass!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
class TestException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    public TestException(String e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        super(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
 * HumanInputStream tries to act like a human sitting in front of a computer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
 * terminal typing on the keyboard while the keytool program is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
 * keytool has called InputStream.read() and BufferedReader.readLine() in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
 * various places. a call to B.readLine() will try to buffer as much input as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
 * possible. Thus, a trivial InputStream will find it impossible to feed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
 * anything to I.read() after a B.readLine() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
 * This is why i create HumanInputStream, which will only send a single line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
 * to B.readLine(), no more, no less, and the next I.read() can have a chance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
 * to read the exact character right after "\n".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
 * I don't know why HumanInputStream works.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
class HumanInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    byte[] src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    boolean inLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    int stopIt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    public HumanInputStream(String input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        src = input.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        length = src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        stopIt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        inLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    // the trick: when called through read(byte[], int, int),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    // return -1 twice after "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    @Override public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        int re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        if(pos < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            re = src[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            if(inLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                if(stopIt > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                    stopIt--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                    re = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                    if(re == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                        stopIt = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            re = -1;//throw new IOException("NO MORE TO READ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        //if (re < 32) System.err.printf("[%02d]", re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        //else System.err.printf("[%c]", (char)re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        return re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    @Override public int read(byte[] buffer, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        inLine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            int re = super.read(buffer, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            return re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            throw new RuntimeException("HumanInputStream error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            inLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    @Override public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        if(pos < length) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    // test part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    static void assertTrue(boolean bool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        if(!bool)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    public static void test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        class Tester {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            HumanInputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            BufferedReader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            Tester(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                is = new HumanInputStream(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                reader = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            // three kinds of test method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            // 1. read byte by byte from InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            void testStreamReadOnce(int expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                assertTrue(is.read() == expection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            void testStreamReadMany(String expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                char[] keys = expection.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                for(int i=0; i<keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                    assertTrue(is.read() == keys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            // 2. read a line with a newly created Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            void testReaderReadline(String expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                String s = new BufferedReader(new InputStreamReader(is)).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                if(s == null) assertTrue(expection == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                else assertTrue(s.equals(expection));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            // 3. read a line with the old Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            void testReaderReadline2(String expection) throws Exception  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                String s = reader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                if(s == null) assertTrue(expection == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                else assertTrue(s.equals(expection));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        Tester test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        test.testReaderReadline("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        test.testReaderReadline("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        test.testReaderReadline("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        test.testReaderReadline2("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        test.testReaderReadline2("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        test.testReaderReadline2("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        test.testReaderReadline2("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        test.testReaderReadline("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        test.testReaderReadline2("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        test = new Tester("1\n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        test.testStreamReadMany("1\n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        test.testStreamReadOnce(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        test = new Tester("12\n234");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        test.testStreamReadOnce('1');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        test.testReaderReadline("2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        test.testStreamReadOnce('2');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        test.testReaderReadline2("34");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        test = new Tester("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        test.testStreamReadMany("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        test = new Tester("changeit\nName\nCountry\nYes\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        test.testStreamReadMany("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        test.testReaderReadline("Name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        test.testReaderReadline("Country");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        test.testReaderReadline("Yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        test = new Tester("Me\nHere\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        test.testReaderReadline2("Me");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        test.testReaderReadline2("Here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
}