jdk/test/sun/security/tools/keytool/KeyToolTest.java
author sflores
Sun, 14 Oct 2012 22:58:59 +0100
changeset 14182 3041082abb40
parent 12871 b583b4c82a82
child 27344 890be6336eab
permissions -rw-r--r--
7194449: String resources for Key Tool and Policy Tool should be in their respective packages Reviewed-by: alanb, weijun, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12871
b583b4c82a82 7174351: test/sun/security/tools/keytool/standard.sh failed after new Hashtable
weijun
parents: 10138
diff changeset
     2
 * Copyright (c) 2005, 2012, 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.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.io.*;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    61
import java.security.KeyPairGenerator;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    62
import java.security.NoSuchAlgorithmException;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    63
import java.util.*;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    64
import java.security.cert.X509Certificate;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    65
import sun.security.util.ObjectIdentifier;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public class KeyToolTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // The stdout and stderr outputs after a keytool run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    String out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    String err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // the output of println() in KeyTool.run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    String ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    String lastInput = "", lastCommand = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final boolean debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.getProperty("debug") != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static final String NSS_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            "-keystore NONE -storetype PKCS11 -providerName SunPKCS11-nss -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nss.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final String NSS_SRC_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            "-srckeystore NONE -srcstoretype PKCS11 -srcproviderName SunPKCS11-nss -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nss.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static final String NZZ_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            "-keystore NONE -storetype PKCS11 -providerName SunPKCS11-nzz -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nzz.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static final String NZZ_SRC_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            "-srckeystore NONE -srcstoretype PKCS11 -srcproviderName SunPKCS11-nzz -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nzz.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static final String SUN_P11_ARG = "-keystore NONE -storetype PKCS11 ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static final String SUN_SRC_P11_ARG = "-srckeystore NONE -srcstoretype PKCS11 ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    String p11Arg, srcP11Arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /** Creates a new instance of KeyToolTest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    KeyToolTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // so that there is "Warning" and not translated into other language
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Locale.setDefault(Locale.US);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Helper, removes a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    void remove(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.err.println("Removing " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        new File(filename).delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (new File(filename).exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new RuntimeException("Error deleting " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
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
     * Run a set of keytool command with given terminal input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param input the terminal inputs, the characters typed by human
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *        if <code>cmd</code> is running on a terminal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param cmd the argument of a keytool command line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @throws if keytool goes wrong in some place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void test(String input, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        lastInput = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        lastCommand = cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   123
        // "X" is appended so that we can precisely test how input is consumed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        HumanInputStream in = new HumanInputStream(input+"X");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        test(in, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // make sure the input string is no more no less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if(in.read() != 'X' || in.read() != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new Exception("Input not consumed exactly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    void test(InputStream in, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // save the original 3 streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            System.err.println(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.err.print(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        PrintStream p1 = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        PrintStream p2 = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        InputStream i1 = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        ByteArrayOutputStream b1 = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ByteArrayOutputStream b2 = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.setIn(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            System.setOut(new PrintStream(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            System.setErr(new PrintStream(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12871
diff changeset
   151
            // since System.in is overrided, the
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12871
diff changeset
   152
            // sun.security.tools.keytool.Main.main() method will
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // never block at user input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12871
diff changeset
   155
            // use -debug so that main() will throw an Exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            // instead of calling System.exit()
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12871
diff changeset
   157
            sun.security.tools.keytool.Main.main(("-debug "+cmd).split("\\s+"));
2
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
12871
b583b4c82a82 7174351: test/sun/security/tools/keytool/standard.sh failed after new Hashtable
weijun
parents: 10138
diff changeset
   456
        // Importing a JCEKS keystore to a JKS one. Will warn for the 2 SecretKey entries
b583b4c82a82 7174351: test/sun/security/tools/keytool/standard.sh failed after new Hashtable
weijun
parents: 10138
diff changeset
   457
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        remove("x.jks");
12871
b583b4c82a82 7174351: test/sun/security/tools/keytool/standard.sh failed after new Hashtable
weijun
parents: 10138
diff changeset
   459
        // Two "no" answers to bypass warnings
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        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
   461
        assertTrue(err.indexOf("s1 not") != -1, "s1 not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        assertTrue(err.indexOf("s2 not") != -1, "s2 not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        assertTrue(err.indexOf("c1 success") != -1, "c1 success");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        assertTrue(err.indexOf("p1 success") != -1, "p1 success");
12871
b583b4c82a82 7174351: test/sun/security/tools/keytool/standard.sh failed after new Hashtable
weijun
parents: 10138
diff changeset
   465
        remove("x.jks");
b583b4c82a82 7174351: test/sun/security/tools/keytool/standard.sh failed after new Hashtable
weijun
parents: 10138
diff changeset
   466
        // One "yes" to stop
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        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
   468
        // 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
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        testFail("changeit\nchangeit\n", "-keystore x.jks -genkeypair -alias p1 -dname CN=olala"); // JKS prompt for keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        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
   475
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        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
   477
        testOK("changeit\n", "-keystore x.p12 -storetype PKCS12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        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
   479
        assertTrue(err.indexOf("Warning") == -1, "PKCS12 silent when keypass == storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        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
   481
        assertTrue(err.indexOf("Warning") != -1, "PKCS12 warning when keypass != storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        testFail("", "-keystore x.p12 -storepass changeit -storetype PKCS12 -keypasswd -new changeit -alias p3"); // no -keypasswd for PKCS12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        testOK("", "-keystore x.p12 -storepass changeit -storetype PKCS12 -changealias -alias p3 -destalias p33");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        testOK("", "-keystore x.p12 -storepass changeit -storetype PKCS12 -keyclone -alias p33 -destalias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        // pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        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
   489
        testOK("", "-storepass changeit -keystore x.p12 -storetype PKCS12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        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
   491
        assertTrue(err.indexOf("Warning") == -1, "PKCS12 silent when keypass == storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        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
   493
        assertTrue(err.indexOf("Warning") != -1, "PKCS12 warning when keypass != storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        remove("x2.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        remove("x2.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    void testPKCS11() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        // pkcs11, the password maybe different and maybe PKCS11 is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        // in case last test is not executed successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        testAnyway("", p11Arg + "-storepass test12 -delete -alias nss");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        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
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        testOK("", p11Arg + "-storepass test12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        testOK("test12\n", p11Arg + "-genkeypair -alias p2 -dname CN=olala2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        testFail("test12\n", p11Arg + "-keypass test12 -genkeypair -alias p3 -dname CN=olala3"); // cannot provide keypass for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        testFail("test12\n", p11Arg + "-keypass nonsense -genkeypair -alias p3 -dname CN=olala3"); // cannot provide keypass for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1, "2 entries in p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        testOK("test12\n", p11Arg + "-alias p1 -changealias -destalias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        testOK("", p11Arg + "-storepass test12 -list -alias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        testFail("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        testOK("test12\n", p11Arg + "-alias p3 -keyclone -destalias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        testFail("", p11Arg + "-storepass test12 -list -alias p3");   // in PKCS11, keyclone will delete old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        testOK("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        testFail("test12\n", p11Arg + "-alias p1 -keypasswd -new another"); // cannot change password for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1, "2 entries in p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        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
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    void testPKCS11ImportKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        testOK("", p11Arg + "-storepass test12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        testOK("test12\n", p11Arg + "-genkeypair -alias p2 -dname CN=olala2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        // test importkeystore for pkcs11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        // pkcs11 -> jks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        testOK("changeit\nchangeit\ntest12\n", srcP11Arg + "-importkeystore -destkeystore x.jks -deststoretype JKS -srcalias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        assertTrue(err.indexOf("not imported") != -1, "cannot import key without destkeypass");
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 not imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        testOK("changeit\ntest12\n", srcP11Arg + "-importkeystore -destkeystore x.jks -deststoretype JKS -srcalias p1 -destkeypass changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        testOK("changeit\ntest12\n", srcP11Arg + "-importkeystore -destkeystore x.jks -deststoretype JKS -srcalias p2 -destkeypass changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        assertTrue(ks.containsAlias("p1"), "p1 is imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        assertTrue(ks.containsAlias("p2"), "p2 is imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // jks -> pkcs11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        testOK("test12\nchangeit\n", p11Arg + "-importkeystore -srckeystore x.jks -srcstoretype JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        testOK("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        testOK("", p11Arg + "-storepass test12 -list -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 2 entries") != -1, "2 entries in p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        // clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1, "empty p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    // The sqeTest reflects the test suggested by judy.gao and bill.situ at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    // /net/sqesvr-nfs/global/nfs/sec/ws_6.0_int/security/src/SecurityTools/Keytool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    void sqeTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        FileOutputStream fos = new FileOutputStream("badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            fos.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        sqeCsrTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        sqePrintcertTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        sqeDeleteTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        sqeExportTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        sqeGenkeyTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        sqeImportTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        sqeKeyclonetest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        sqeKeypasswdTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        sqeListTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        sqeSelfCertTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        sqeStorepassTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        remove("badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    // Import: cacert, prompt, trusted, non-trusted, bad chain, not match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    void sqeImportTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        /* deleted */ testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        /* deleted */ testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        testOK("yes\n", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        assertTrue(ks.containsAlias("mykey"), "imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        /* deleted */ testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        testOK("\n", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        assertTrue(!ks.containsAlias("mykey"), "imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        testOK("no\n", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        assertTrue(!ks.containsAlias("mykey"), "imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        testFail("no\n", "-keystore x.jks -storepass changeit -importcert -file nonexist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        testFail("no\n", "-keystore x.jks -storepass changeit -importcert -file x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    // keyclone: exist. nonexist err, cert err, dest exist, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    void sqeKeyclonetest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -new newpass -keyclone -dest p0"); // new pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        testOK("\n", "-keystore x.jks -storepass changeit -keypass changeit -keyclone -dest p1"); // new pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        testOK("\n", "-keystore x.jks -storepass changeit -keyclone -dest p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        testFail("\n", "-keystore x.jks -storepass changeit -keyclone -dest p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        testFail("\n", "-keystore x.jks -storepass changeit -keyclone -dest p3 -alias noexist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        // no cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -new newpass -keyclone -dest p0"); // new pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    // keypasswd: exist, short, nonexist err, cert err, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    void sqeKeypasswdTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        testOK("newpass\nnewpass\n", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        testOK("new\nnew\nnewpass\nnewpass\n", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        testOK("", "-keystore x.jks -storepass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        testOK("changeit\n", "-keystore x.jks -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        testFail("", "-keystore x.jks -storepass badpass -keypass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        testFail("", "-keystore x.jks -storepass changeit -keypass bad -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        // no cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        // diff pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        testOK("", "-keystore x.jks -storepass changeit -keypass keypass -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        testFail("", "-keystore x.jks -storepass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        testOK("keypass\n", "-keystore x.jks -storepass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        // i hate those misc test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    // list: -f -alias, exist, nonexist err; otherwise, check all shows, -rfc shows more, and misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    void sqeListTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        testOK("", "-keystore x.jks -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        testOK("", "-keystore x.jks -storepass changeit -list -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        testFail("", "-keystore x.jks -storepass changeit -list -alias notexist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        testFail("", "-keystore x.jks -storepass badpass -list -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        testOK("", "-keystore x.jks -storepass changeit -keypass badpass -list -alias mykey");  // keypass ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        testOK("\n", "-keystore x.jks -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        assertTrue(err.indexOf("WARNING") != -1, "no storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        testOK("changeit\n", "-keystore x.jks -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        assertTrue(err.indexOf("WARNING") == -1, "has storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        testFail("badpass\n", "-keystore x.jks -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        // misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        testFail("", "-keystore aa\\bb//cc -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        testFail("", "-keystore nonexisting -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        testFail("", "-keystore badkeystore -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    // selfcert: exist, non-exist err, cert err, sig..., dname, wrong keypass, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    void sqeSelfCertTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        testOK("", "-keystore x.jks -storepass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert -alias nonexisting"); // not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert -dname CN=NewName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert -sigalg MD5withRSA"); // sig not compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        testFail("", "-keystore x.jks -storepass wrong -keypass changeit -selfcert"); // bad pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        testFail("", "-keystore x.jks -storepass changeit -keypass wrong -selfcert"); // bad pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        //misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        testFail("", "-keystore nonexist -storepass changeit -keypass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        testFail("", "-keystore aa//dd\\gg -storepass changeit -keypass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // diff pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        testOK("", "-keystore x.jks -storepass changeit -keypass keypass -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        testFail("", "-keystore x.jks -storepass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        testOK("keypass\n", "-keystore x.jks -storepass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        testFail("", "-keystore x.jks -storepass changeit -selfcert");  // certentry cannot do selfcert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    // storepass: bad old, short new, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    void sqeStorepassTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        testOK("", "-storepasswd -keystore x.jks -storepass changeit -new newstore"); // all in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        testOK("changeit\nnewstore\nnewstore\n", "-storepasswd -keystore x.jks"); // all not in arg, new twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        testOK("changeit\n", "-storepasswd -keystore x.jks -new newstore"); // new in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        testOK("newstore\nnewstore\n", "-storepasswd -keystore x.jks -storepass changeit"); // old in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        testOK("new\nnew\nnewstore\nnewstore\n", "-storepasswd -keystore x.jks -storepass changeit"); // old in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        testFail("", "-storepasswd -keystore x.jks -storepass badold -new newstore"); // bad old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        testFail("", "-storepasswd -keystore x.jks -storepass changeit -new new"); // short new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        // misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        testFail("", "-storepasswd -keystore nonexist -storepass changeit -new newstore"); // non exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        testFail("", "-storepasswd -keystore badkeystore -storepass changeit -new newstore"); // bad file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        testFail("", "-storepasswd -keystore aa\\bb//cc//dd -storepass changeit -new newstore"); // bad file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    void sqeGenkeyTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -alias newentry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -alias newentry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg DSA -alias n1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -alias n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg NoSuchAlg -alias n3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 56 -alias n4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 999 -alias n5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 512 -alias n6");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 1024 -alias n7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -sigalg NoSuchAlg -alias n8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        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
   758
        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
   759
        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
   760
        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
   761
        testFail("", "-keystore badkeystore -storepass changeit -keypass changeit -genkeypair -dname CN=olala -alias n14");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        testFail("", "-keystore x.jks -storepass badpass -keypass changeit -genkeypair -dname CN=olala -alias n16");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CNN=olala -alias n17");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    void sqeExportTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        testFail("", "-keystore x.jks -storepass changeit -export -file mykey.cert -alias mykey"); // nonexist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        testOK("", "-keystore x.jks -storepass changeit -export -file mykey.cert -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        testOK("", "-keystore x.jks -storepass changeit -import -file mykey.cert -noprompt -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        testOK("", "-keystore x.jks -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        testFail("", "-keystore nonexistkeystore -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        testFail("", "-keystore badkeystore -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        testFail("", "-keystore x.jks -storepass badpass -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        remove("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        remove("mykey.cert2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    void sqeDeleteTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        testFail("", "-keystore x.jks -storepass changeit -delete -alias mykey"); // nonexist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit -delete -alias mykey"); // keystore name illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        testFail("", "-keystore nonexistkeystore -storepass changeit -delete -alias mykey"); // keystore not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        testFail("", "-keystore badkeystore -storepass changeit -delete -alias mykey"); // keystore invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        testFail("", "-keystore x.jks -storepass xxxxxxxx -delete -alias mykey"); // wrong pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    void sqeCsrTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        remove("csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        // PrivateKeyEntry can do certreq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg SHA1withDSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg MD5withRSA"); // unmatched sigalg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        // misc test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        testFail("", "-keystore x.jks -storepass badstorepass -certreq -file csr1"); // bad storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        testOK("changeit\n", "-keystore x.jks -certreq -file csr1"); // storepass from terminal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        testFail("\n", "-keystore x.jks -certreq -file csr1"); // must provide storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        testFail("", "-keystore x.jks -storepass changeit -keypass badkeypass -certreq -file csr1"); // bad keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        testFail("", "-keystore x.jks -storepass changeit -certreq -file aa\\bb//cc\\dd");  // bad filepath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        testFail("", "-keystore noexistks -storepass changeit -certreq -file csr1"); // non-existing keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // Try the RSA private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg SHA1withDSA"); // unmatched sigalg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg MD5withRSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        // TrustedCertificateEntry cannot do certreq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1 -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        remove("csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    void sqePrintcertTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        remove("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        testOK("", "-keystore x.jks -storepass changeit -export -file mykey.cert -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        testFail("", "-printcert -file badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        testFail("", "-printcert -file a/b/c/d");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        testOK("", "-printcert -file mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        FileInputStream fin = new FileInputStream("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        testOK(fin, "-printcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        fin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        remove("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   847
    void v3extTest(String keyAlg) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   848
        KeyStore ks;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   849
        remove("x.jks");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   850
        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
   851
        String pre = simple + "-genkeypair -dname CN=Olala -alias ";
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   852
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   853
        // Version and SKID
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   854
        testOK("", pre + "o1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   855
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   856
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   857
        assertTrue(((X509Certificate)ks.getCertificate("o1")).getVersion() == 3);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   858
        assertTrue(((X509CertImpl)ks.getCertificate("o1")).getSubjectKeyIdentifierExtension() != null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   859
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   860
        // BC
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   861
        testOK("", pre + "b1 -ext BC:critical");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   862
        testOK("", pre + "b2 -ext BC");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   863
        testOK("", pre + "b3 -ext bc");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   864
        testOK("", pre + "b4 -ext BasicConstraints");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   865
        testOK("", pre + "b5 -ext basicconstraints");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   866
        testOK("", pre + "b6 -ext BC=ca:true,pathlen:12");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   867
        testOK("", pre + "b7 -ext BC=ca:false");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   868
        testOK("", pre + "b8 -ext BC:critical=ca:false");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   869
        testOK("", pre + "b9 -ext BC=12");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   870
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   871
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   872
        assertTrue(((X509CertImpl)ks.getCertificate("b1")).getBasicConstraintsExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   873
        assertTrue(!((X509CertImpl)ks.getCertificate("b2")).getBasicConstraintsExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   874
        assertTrue(((X509CertImpl)ks.getCertificate("b8")).getBasicConstraintsExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   875
        assertTrue(((X509Certificate)ks.getCertificate("b1")).getBasicConstraints() == Integer.MAX_VALUE);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   876
        assertTrue(((X509Certificate)ks.getCertificate("b2")).getBasicConstraints() == Integer.MAX_VALUE);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   877
        assertTrue(((X509Certificate)ks.getCertificate("b3")).getBasicConstraints() == Integer.MAX_VALUE);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   878
        assertTrue(((X509Certificate)ks.getCertificate("b4")).getBasicConstraints() == Integer.MAX_VALUE);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   879
        assertTrue(((X509Certificate)ks.getCertificate("b5")).getBasicConstraints() == Integer.MAX_VALUE);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   880
        assertTrue(((X509Certificate)ks.getCertificate("b6")).getBasicConstraints() == 12);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   881
        assertTrue(((X509Certificate)ks.getCertificate("b7")).getBasicConstraints() == -1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   882
        assertTrue(((X509Certificate)ks.getCertificate("b9")).getBasicConstraints() == 12);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   883
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   884
        // KU
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   885
        testOK("", pre + "ku1 -ext KeyUsage:critical=digitalsignature");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   886
        testOK("", pre + "ku2 -ext KU=digitalSignature");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   887
        testOK("", pre + "ku3 -ext KU=ds");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   888
        testOK("", pre + "ku4 -ext KU=dig");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   889
        testFail("", pre + "ku5 -ext KU=d");    // ambigous value
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   890
        testFail("", pre + "ku6 -ext KU=cs");   // cRLSign cannot be cs
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   891
        testOK("", pre + "ku11 -ext KU=nr");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   892
        testFail("", pre + "ku12 -ext KU=ke");  // ke also means keyAgreement
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   893
        testOK("", pre + "ku12 -ext KU=keyE");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   894
        testFail("", pre + "ku13 -ext KU=de");  // de also means decipherOnly
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   895
        testOK("", pre + "ku13 -ext KU=dataE");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   896
        testOK("", pre + "ku14 -ext KU=ka");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   897
        testOK("", pre + "ku15 -ext KU=kcs");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   898
        testOK("", pre + "ku16 -ext KU=crls");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   899
        testOK("", pre + "ku17 -ext KU=eo");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   900
        testOK("", pre + "ku18 -ext KU=do");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   901
        testOK("", pre + "ku19 -ext KU=cc");
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
        testOK("", pre + "ku017 -ext KU=ds,cc,eo");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   904
        testOK("", pre + "ku135 -ext KU=nr,dataEncipherment,keyCertSign");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   905
        testOK("", pre + "ku246 -ext KU=keyEnc,cRL,keyA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   906
        testOK("", pre + "ku1234 -ext KU=ka,da,keyE,nonR");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   907
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   908
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   909
        class CheckKU {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   910
            void check(KeyStore ks, String alias, int... pos) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   911
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   912
                boolean[] bs = ((X509Certificate)ks.getCertificate(alias)).getKeyUsage();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   913
                bs = Arrays.copyOf(bs, 9);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   914
                for (int i=0; i<bs.length; i++) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   915
                    boolean found = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   916
                    for (int p: pos) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   917
                        if (p == i) found = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   918
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   919
                    if (!found ^ bs[i]) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   920
                        // OK
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   921
                    } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   922
                        throw new RuntimeException("KU not match at " + i +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   923
                                ": " + found + " vs " + bs[i]);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   924
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   925
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   926
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   927
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   928
        CheckKU c = new CheckKU();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   929
        assertTrue(((X509CertImpl)ks.getCertificate("ku1")).getExtension(PKIXExtensions.KeyUsage_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   930
        assertTrue(!((X509CertImpl)ks.getCertificate("ku2")).getExtension(PKIXExtensions.KeyUsage_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   931
        c.check(ks, "ku1", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   932
        c.check(ks, "ku2", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   933
        c.check(ks, "ku3", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   934
        c.check(ks, "ku4", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   935
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   936
        c.check(ks, "ku12", 2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   937
        c.check(ks, "ku13", 3);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   938
        c.check(ks, "ku14", 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   939
        c.check(ks, "ku15", 5);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   940
        c.check(ks, "ku16", 6);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   941
        c.check(ks, "ku17", 7);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   942
        c.check(ks, "ku18", 8);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   943
        c.check(ks, "ku19", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   944
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   945
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   946
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   947
        c.check(ks, "ku017", 0, 1, 7);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   948
        c.check(ks, "ku135", 1, 3, 5);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   949
        c.check(ks, "ku246", 6, 2, 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   950
        c.check(ks, "ku1234", 1, 2, 3, 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   951
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   952
        // EKU
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   953
        testOK("", pre + "eku1 -ext EKU:critical=sa");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   954
        testOK("", pre + "eku2 -ext ExtendedKeyUsage=ca");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   955
        testOK("", pre + "eku3 -ext EKU=cs");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   956
        testOK("", pre + "eku4 -ext EKU=ep");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   957
        testOK("", pre + "eku8 -ext EKU=ts");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   958
        testFail("", pre + "eku9 -ext EKU=os");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   959
        testOK("", pre + "eku9 -ext EKU=ocsps");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   960
        testOK("", pre + "eku10 -ext EKU=any");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   961
        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
   962
        testFail("", pre + "eku12 -ext EKU=c");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   963
        testFail("", pre + "eku12 -ext EKU=nothing");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   964
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   965
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   966
        class CheckEKU {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   967
            void check(KeyStore ks, String alias, String... pos) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   968
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   969
                List<String> bs = ((X509Certificate)ks.getCertificate(alias)).getExtendedKeyUsage();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   970
                int found = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   971
                for (String p: pos) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   972
                    if (bs.contains(p)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   973
                        found++;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   974
                    } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   975
                        throw new RuntimeException("EKU: not included " + p);
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
                if (found != bs.size()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   979
                    throw new RuntimeException("EKU: more items than expected");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   980
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   981
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   982
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   983
        CheckEKU cx = new CheckEKU();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   984
        assertTrue(((X509CertImpl)ks.getCertificate("eku1")).getExtension(PKIXExtensions.ExtendedKeyUsage_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   985
        assertTrue(!((X509CertImpl)ks.getCertificate("eku2")).getExtension(PKIXExtensions.ExtendedKeyUsage_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   986
        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
   987
        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
   988
        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
   989
        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
   990
        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
   991
        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
   992
        cx.check(ks, "eku10", "2.5.29.37.0");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   993
        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
   994
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   995
        // SAN
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   996
        testOK("", pre+"san1 -ext san:critical=email:me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   997
        testOK("", pre+"san2 -ext san=uri:http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   998
        testOK("", pre+"san3 -ext san=dns:me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   999
        testOK("", pre+"san4 -ext san=ip:192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1000
        testOK("", pre+"san5 -ext san=oid:1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1001
        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
  1002
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1003
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1004
        class CheckSAN {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1005
            // Please sort items with name type
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1006
            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
  1007
                int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1008
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1009
                Object[] names = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1010
                if (type == 0) names = ((X509Certificate)ks.getCertificate(alias)).getSubjectAlternativeNames().toArray();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1011
                else names = ((X509Certificate)ks.getCertificate(alias)).getIssuerAlternativeNames().toArray();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1012
                Arrays.sort(names, new Comparator() {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1013
                    public int compare(Object o1, Object o2) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1014
                        int i1 = (Integer)((List)o1).get(0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1015
                        int i2 = (Integer)((List)o2).get(0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1016
                        return i1 - i2;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1017
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1018
                });
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1019
                for (Object o: names) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1020
                    List l = (List)o;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1021
                    for (Object o2: l) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1022
                        if (!items[pos++].equals(o2)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1023
                            throw new RuntimeException("Not equals at " + pos
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1024
                                    + ": " + items[pos-1] + " vs " + o2);
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
                if (pos != items.length) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1029
                    throw new RuntimeException("Extra items, pos is " + pos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1030
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1031
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1032
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1033
        CheckSAN csan = new CheckSAN();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1034
        assertTrue(((X509CertImpl)ks.getCertificate("san1")).getSubjectAlternativeNameExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1035
        assertTrue(!((X509CertImpl)ks.getCertificate("san2")).getSubjectAlternativeNameExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1036
        csan.check(ks, "san1", 0, 1, "me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1037
        csan.check(ks, "san2", 0, 6, "http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1038
        csan.check(ks, "san3", 0, 2, "me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1039
        csan.check(ks, "san4", 0, 7, "192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1040
        csan.check(ks, "san5", 0, 8, "1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1041
        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
  1042
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1043
        // IAN
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1044
        testOK("", pre+"ian1 -ext ian:critical=email:me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1045
        testOK("", pre+"ian2 -ext ian=uri:http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1046
        testOK("", pre+"ian3 -ext ian=dns:me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1047
        testOK("", pre+"ian4 -ext ian=ip:192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1048
        testOK("", pre+"ian5 -ext ian=oid:1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1049
        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
  1050
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1051
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1052
        assertTrue(((X509CertImpl)ks.getCertificate("ian1")).getIssuerAlternativeNameExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1053
        assertTrue(!((X509CertImpl)ks.getCertificate("ian2")).getIssuerAlternativeNameExtension().isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1054
        csan.check(ks, "ian1", 1, 1, "me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1055
        csan.check(ks, "ian2", 1, 6, "http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1056
        csan.check(ks, "ian3", 1, 2, "me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1057
        csan.check(ks, "ian4", 1, 7, "192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1058
        csan.check(ks, "ian5", 1, 8, "1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1059
        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
  1060
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1061
        // SIA
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1062
        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
  1063
        testOK("", pre+"sia2 -ext sia=ts:email:ts@ca.com");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1064
        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
  1065
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1066
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1067
        class CheckSia {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1068
            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
  1069
                int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1070
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1071
                AccessDescription[] ads = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1072
                if (type == 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1073
                    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
  1074
                    ads = siae.getAccessDescriptions().toArray(new AccessDescription[0]);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1075
                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1076
                    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
  1077
                    ads = aiae.getAccessDescriptions().toArray(new AccessDescription[0]);
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
                Arrays.sort(ads, new Comparator<AccessDescription>() {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1080
                    @Override
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1081
                    public int compare(AccessDescription o1, AccessDescription o2) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1082
                        return o1.getAccessMethod().toString().compareTo(o2.getAccessMethod().toString());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1083
                    }
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
                for (AccessDescription ad: ads) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1086
                    if (!ad.getAccessMethod().equals(items[pos++]) ||
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1087
                            !new Integer(ad.getAccessLocation().getType()).equals(items[pos++])) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1088
                        throw new RuntimeException("Not same type at " + pos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1089
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1090
                    String name = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1091
                    switch (ad.getAccessLocation().getType()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1092
                        case 1:
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1093
                            name = ((RFC822Name)ad.getAccessLocation().getName()).getName();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1094
                            break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1095
                        case 6:
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1096
                            name = ((URIName)ad.getAccessLocation().getName()).getURI().toString();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1097
                            break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1098
                        default:
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1099
                            throw new RuntimeException("Not implemented: " + ad);
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
                    if (!name.equals(items[pos++])) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1102
                        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
  1103
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1104
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1105
            }
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
        CheckSia csia = new CheckSia();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1108
        assertTrue(!((X509CertImpl)ks.getCertificate("sia1")).getExtension(PKIXExtensions.SubjectInfoAccess_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1109
        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
  1110
        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
  1111
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1112
        // AIA
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1113
        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
  1114
        testOK("", pre+"aia2 -ext aia=ocsp:email:ocsp@ca.com");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1115
        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
  1116
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1117
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1118
        assertTrue(!((X509CertImpl)ks.getCertificate("aia1")).getExtension(PKIXExtensions.AuthInfoAccess_Id).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1119
        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
  1120
        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
  1121
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1122
        // OID
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1123
        testOK("", pre+"oid1 -ext 1.2.3:critical=0102");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1124
        testOK("", pre+"oid2 -ext 1.2.3");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1125
        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
  1126
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1127
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1128
        class CheckOid {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1129
            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
  1130
                int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1131
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1132
                Extension ex = ((X509CertImpl)ks.getCertificate(alias)).getExtension(new ObjectIdentifier(oid));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1133
                if (!Arrays.equals(value, ex.getValue())) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1134
                    throw new RuntimeException("Not same content in " + alias + " for " + oid);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1135
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1136
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1137
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1138
        CheckOid coid = new CheckOid();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1139
        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
  1140
        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
  1141
        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
  1142
        coid.check(ks, "oid2", "1.2.3", new byte[]{});
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1143
        coid.check(ks, "oid12", "1.2.3", new byte[]{});
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1144
        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
  1145
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1146
        // honored
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1147
        testOK("", pre+"ca");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1148
        testOK("", pre+"a");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1149
        // 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
  1150
        testOK("", simple+"-alias a -certreq " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1151
                "-ext BC=1 -ext KU=crl " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1152
                "-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
  1153
                "-rfc -file test.req");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1154
        // printcertreq
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1155
        testOK("", "-printcertreq -file test.req");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1156
        // 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
  1157
        testOK("", simple+"-gencert -alias ca -infile test.req -ext " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1158
                "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
  1159
                "-ext BC=2 -ext 2.3.4=01020304 " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1160
                "-debug -rfc -outfile test.cert");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1161
        testOK("", simple+"-importcert -file test.cert -alias a");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1162
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1163
        X509CertImpl a = (X509CertImpl)ks.getCertificate("a");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1164
        assertTrue(a.getAuthorityKeyIdentifierExtension() != null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1165
        assertTrue(a.getSubjectKeyIdentifierExtension() != null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1166
        assertTrue(a.getKeyUsage() == null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1167
        assertTrue(a.getExtension(new ObjectIdentifier("1.2.3")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1168
        assertTrue(!a.getExtension(new ObjectIdentifier("1.2.4")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1169
        assertTrue(!a.getExtension(new ObjectIdentifier("1.2.5")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1170
        assertTrue(a.getExtensionValue("1.2.3").length == 3);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1171
        assertTrue(a.getExtensionValue("1.2.4").length == 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1172
        assertTrue(a.getExtensionValue("1.2.5").length == 5);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1173
        assertTrue(a.getBasicConstraints() == 2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1174
        assertTrue(!a.getExtension(new ObjectIdentifier("2.3.4")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1175
        assertTrue(a.getExtensionValue("2.3.4").length == 6);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1176
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1177
        remove("x.jks");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1178
        remove("test.req");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1179
        remove("test.cert");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1180
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1181
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    void i18nTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        //   1.  keytool -help
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        remove("x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1185
        testOK("", "-help");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1186
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        //   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
  1188
        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
  1189
        //   3. keytool -list -v -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        testOK("", "-list -v -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        //   4. keytool -list -v Type "a" for the keystore password. Check error (wrong keystore password).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        testFail("a\n", "-list -v -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1193
        assertTrue(ex.indexOf("password was incorrect") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        //   5. keytool -genkey -v -keysize 512 Enter "password" as the password. Check error (alias 'mykey' already exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        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
  1196
        assertTrue(ex.indexOf("alias <mykey> already exists") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        //   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
  1198
        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
  1199
        //   7. keytool -list -v Type 'password' for the store password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        testOK("password\n", "-list -v -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        //   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
  1202
        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
  1203
        assertTrue(ex.indexOf("Too many failures - try later") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        //   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
  1205
        testOK("aaaaaa\naaaaaa\n", "-keypasswd -v -alias mykey2 -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        //  10. keytool -selfcert -v -alias mykey -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        testOK("", "-selfcert -v -alias mykey -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        //  11. keytool -list -v -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        testOK("", "-list -v -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        //  12. keytool -export -v -alias mykey -file cert -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        remove("cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        testOK("", "-export -v -alias mykey -file cert -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        //  13. keytool -import -v -file cert -storepass password Check error (Certificate reply and cert are the same)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        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
  1215
        assertTrue(ex.indexOf("Certificate reply and certificate in keystore are identical") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        //  14. keytool -printcert -file cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        testOK("", "-printcert -file cert -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        remove("cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        //  15. keytool -list -storepass password -provider sun.security.provider.Sun
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        testOK("", "-list -storepass password -provider sun.security.provider.Sun -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        //Error tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        //   1. keytool -storepasswd -storepass password -new abc Check error (password too short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        testFail("", "-storepasswd -storepass password -new abc");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1226
        assertTrue(ex.indexOf("New password must be at least 6 characters") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        // Changed, no NONE needed now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        //   2. keytool -list -storetype PKCS11 Check error (-keystore must be NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        //testFail("", "-list -storetype PKCS11");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1230
        //assertTrue(err.indexOf("keystore must be NONE") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        //   3. keytool -storepasswd -storetype PKCS11 -keystore NONE Check error (unsupported operation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        testFail("", "-storepasswd -storetype PKCS11 -keystore NONE");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1233
        assertTrue(ex.indexOf("UnsupportedOperationException") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        //   4. keytool -keypasswd -storetype PKCS11 -keystore NONE Check error (unsupported operation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        testFail("", "-keypasswd -storetype PKCS11 -keystore NONE");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1236
        assertTrue(ex.indexOf("UnsupportedOperationException") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        //   5. keytool -list -protected -storepass password Check error (password can not be specified with -protected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        testFail("", "-list -protected -storepass password -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1239
        assertTrue(ex.indexOf("if -protected is specified, then") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        //   6. keytool -keypasswd -protected -keypass password Check error (password can not be specified with -protected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        testFail("", "-keypasswd -protected -keypass password -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1242
        assertTrue(ex.indexOf("if -protected is specified, then") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        //   7. keytool -keypasswd -protected -new password Check error (password can not be specified with -protected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        testFail("", "-keypasswd -protected -new password -keystore x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1245
        assertTrue(ex.indexOf("if -protected is specified, then") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    void i18nPKCS11Test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        //PKCS#11 tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        //   1. sccs edit cert8.db key3.db
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        //Runtime.getRuntime().exec("/usr/ccs/bin/sccs edit cert8.db key3.db");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        testOK("", p11Arg + "-storepass test12 -genkey -alias genkey -dname cn=genkey -keysize 512 -keyalg rsa");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        testOK("", p11Arg + "-storepass test12 -list -alias genkey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        testOK("", p11Arg + "-storepass test12 -certreq -alias genkey -file genkey.certreq");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        testOK("", p11Arg + "-storepass test12 -export -alias genkey -file genkey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        testOK("", "-printcert -file genkey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        testOK("", p11Arg + "-storepass test12 -selfcert -alias genkey -dname cn=selfCert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        testOK("", p11Arg + "-storepass test12 -list -alias genkey -v");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1262
        assertTrue(out.indexOf("Owner: CN=selfCert") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        //(check that cert subject DN is [cn=selfCert])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        testOK("", p11Arg + "-storepass test12 -delete -alias genkey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        testOK("", p11Arg + "-storepass test12 -list");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1266
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        //(check for empty database listing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        //Runtime.getRuntime().exec("/usr/ccs/bin/sccs unedit cert8.db key3.db");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        remove("genkey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        remove("genkey.certreq");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        //  12. sccs unedit cert8.db key3.db
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    // tesing new option -srcProviderName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    void sszzTest() throws Exception {
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
        testOK("", NSS_P11_ARG+"-genkeypair -dname CN=NSS -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        testOK("", NSS_SRC_P11_ARG + NZZ_P11_ARG +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                "-importkeystore -srcstorepass test12 -deststorepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        testAnyway("", NSS_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        testAnyway("", NZZ_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    public static void main(String[] args) throws Exception {
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1286
        Locale reservedLocale = Locale.getDefault();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1287
        try {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1288
            // first test if HumanInputStream really acts like a human being
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1289
            HumanInputStream.test();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1290
            KeyToolTest t = new KeyToolTest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1292
            if (System.getProperty("file") != null) {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1293
                t.sqeTest();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1294
                t.testAll();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1295
                t.i18nTest();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1296
                t.v3extTest("RSA");
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1297
                t.v3extTest("DSA");
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1298
                boolean testEC = true;
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1299
                try {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1300
                    KeyPairGenerator.getInstance("EC");
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1301
                } catch (NoSuchAlgorithmException nae) {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1302
                    testEC = false;
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1303
                }
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1304
                if (testEC) t.v3extTest("EC");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1305
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1307
            if (System.getProperty("nss") != null) {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1308
                t.srcP11Arg = NSS_SRC_P11_ARG;
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1309
                t.p11Arg = NSS_P11_ARG;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1311
                t.testPKCS11();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1312
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1313
                // FAIL:
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1314
                // 1. we still don't have srcprovidername yet
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1315
                // 2. cannot store privatekey into NSS keystore
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1316
                //    java.security.KeyStoreException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_TEMPLATE_INCOMPLETE.
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1317
                //t.testPKCS11ImportKeyStore();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1318
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1319
                t.i18nPKCS11Test();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1320
                //FAIL: currently PKCS11-NSS does not support 2 NSS KeyStores to be loaded at the same time
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1321
                //t.sszzTest();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1322
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1324
            if (System.getProperty("solaris") != null) {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1325
                // For Solaris Cryptography Framework
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1326
                t.srcP11Arg = SUN_SRC_P11_ARG;
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1327
                t.p11Arg = SUN_P11_ARG;
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1328
                t.testPKCS11();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1329
                t.testPKCS11ImportKeyStore();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1330
                t.i18nPKCS11Test();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1331
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1333
            System.out.println("Test pass!!!");
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1334
        } finally {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1335
            // restore the reserved locale
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1336
            Locale.setDefault(reservedLocale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
class TestException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    public TestException(String e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        super(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
 * HumanInputStream tries to act like a human sitting in front of a computer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
 * terminal typing on the keyboard while the keytool program is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
 * keytool has called InputStream.read() and BufferedReader.readLine() in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
 * various places. a call to B.readLine() will try to buffer as much input as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
 * possible. Thus, a trivial InputStream will find it impossible to feed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
 * anything to I.read() after a B.readLine() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
 * This is why i create HumanInputStream, which will only send a single line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
 * to B.readLine(), no more, no less, and the next I.read() can have a chance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
 * to read the exact character right after "\n".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
 * I don't know why HumanInputStream works.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
class HumanInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    byte[] src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    boolean inLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    int stopIt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    public HumanInputStream(String input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        src = input.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        length = src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        stopIt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        inLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    // the trick: when called through read(byte[], int, int),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    // return -1 twice after "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    @Override public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        int re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        if(pos < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            re = src[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            if(inLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                if(stopIt > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    stopIt--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    re = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    if(re == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                        stopIt = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                    pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            re = -1;//throw new IOException("NO MORE TO READ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        //if (re < 32) System.err.printf("[%02d]", re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        //else System.err.printf("[%c]", (char)re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        return re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    @Override public int read(byte[] buffer, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        inLine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            int re = super.read(buffer, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            return re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            throw new RuntimeException("HumanInputStream error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            inLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    @Override public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        if(pos < length) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    // test part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    static void assertTrue(boolean bool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        if(!bool)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    public static void test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        class Tester {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            HumanInputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            BufferedReader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            Tester(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                is = new HumanInputStream(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                reader = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            // three kinds of test method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            // 1. read byte by byte from InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            void testStreamReadOnce(int expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                assertTrue(is.read() == expection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            void testStreamReadMany(String expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                char[] keys = expection.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                for(int i=0; i<keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                    assertTrue(is.read() == keys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            // 2. read a line with a newly created Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            void testReaderReadline(String expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                String s = new BufferedReader(new InputStreamReader(is)).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                if(s == null) assertTrue(expection == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                else assertTrue(s.equals(expection));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            // 3. read a line with the old Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            void testReaderReadline2(String expection) throws Exception  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                String s = reader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                if(s == null) assertTrue(expection == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                else assertTrue(s.equals(expection));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        Tester test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        test.testReaderReadline("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        test.testReaderReadline("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        test.testReaderReadline("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        test.testReaderReadline2("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        test.testReaderReadline2("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        test.testReaderReadline2("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        test.testReaderReadline2("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        test.testReaderReadline("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        test.testReaderReadline2("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        test.testReaderReadline("");
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("1\n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        test.testStreamReadMany("1\n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        test.testStreamReadOnce(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        test = new Tester("12\n234");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        test.testStreamReadOnce('1');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        test.testReaderReadline("2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        test.testStreamReadOnce('2');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        test.testReaderReadline2("34");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        test = new Tester("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        test.testStreamReadMany("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        test = new Tester("changeit\nName\nCountry\nYes\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        test.testStreamReadMany("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        test.testReaderReadline("Name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        test.testReaderReadline("Country");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        test.testReaderReadline("Yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        test = new Tester("Me\nHere\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        test.testReaderReadline2("Me");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        test.testReaderReadline2("Here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
}