jdk/src/share/classes/sun/security/tools/KeyTool.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 904 eadc9fa4b700
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.tools;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.InvalidParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.Signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.UnrecoverableEntryException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.UnrecoverableKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.Identity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.Signer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.security.interfaces.DSAParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.security.interfaces.DSAPrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.security.interfaces.DSAPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.security.interfaces.RSAPrivateCrtKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.security.interfaces.RSAPrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.security.interfaces.RSAPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.text.Collator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import sun.misc.BASE64Decoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import sun.misc.BASE64Encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import sun.security.pkcs.PKCS10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import sun.security.provider.IdentityDatabase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import sun.security.provider.SystemSigner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import sun.security.provider.SystemIdentity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import sun.security.provider.X509Factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import sun.security.util.Password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import sun.security.util.Resources;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import sun.security.util.PathList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import javax.crypto.KeyGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
import static java.security.KeyStore.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * This tool manages keystores.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @see java.security.KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @see sun.security.provider.KeyProtector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @see sun.security.provider.JavaKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
public final class KeyTool {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private int command = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private String sigAlgName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private String keyAlgName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private boolean verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private int keysize = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private boolean rfc = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private long validity = (long)90;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private String alias = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private String dname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private String dest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private String filename = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private String srcksfname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // User-specified providers are added before any command is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // However, they are not removed before the end of the main() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // If you're calling KeyTool.main() directly in your own Java program,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // please programtically add any providers you need and do not specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // them through the command line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private Set<Pair <String, String>> providers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private String storetype = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private String srcProviderName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private String providerName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private String pathlist = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private char[] storePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private char[] storePassNew = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private char[] keyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private char[] keyPassNew = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private char[] oldPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private char[] newPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private char[] destKeyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private char[] srckeyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private String ksfname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private File ksfile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private InputStream ksStream = null; // keystore stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private KeyStore keyStore = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private boolean token = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private boolean nullStream = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private boolean kssave = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private boolean noprompt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private boolean trustcacerts = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private boolean protectedPath = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private boolean srcprotectedPath = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private CertificateFactory cf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private KeyStore caks = null; // "cacerts" keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private char[] srcstorePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private String srcstoretype = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private Set<char[]> passwords = new HashSet<char[]> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private String startDate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static final int CERTREQ = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static final int CHANGEALIAS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final int DELETE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static final int EXPORTCERT = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static final int GENKEYPAIR = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final int GENSECKEY = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // there is no HELP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final int IDENTITYDB = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static final int IMPORTCERT = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final int IMPORTKEYSTORE = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static final int KEYCLONE = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static final int KEYPASSWD = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private static final int LIST = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static final int PRINTCERT = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static final int SELFCERT = 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static final int STOREPASSWD = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private static final Class[] PARAM_STRING = { String.class };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private static final String JKS = "jks";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static final String NONE = "NONE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private static final String P11KEYSTORE = "PKCS11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static final String P12KEYSTORE = "PKCS12";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private final String keyAlias = "mykey";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // for i18n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static final java.util.ResourceBundle rb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        java.util.ResourceBundle.getBundle("sun.security.util.Resources");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static final Collator collator = Collator.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // this is for case insensitive string comparisons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        collator.setStrength(Collator.PRIMARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private KeyTool() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        KeyTool kt = new KeyTool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        kt.run(args, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private void run(String[] args, PrintStream out) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            parseArgs(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            doCommands(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            System.out.println(rb.getString("keytool error: ") + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (!debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            for (char[] pass : passwords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (pass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    Arrays.fill(pass, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    pass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (ksStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                ksStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Parse command line arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    void parseArgs(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (args.length == 0) usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        for (i=0; (i < args.length) && args[i].startsWith("-"); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            String flags = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
             * command modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (collator.compare(flags, "-certreq") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                command = CERTREQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            } else if (collator.compare(flags, "-delete") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                command = DELETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            } else if (collator.compare(flags, "-export") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    collator.compare(flags, "-exportcert") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                command = EXPORTCERT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            } else if (collator.compare(flags, "-genkey") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    collator.compare(flags, "-genkeypair") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                command = GENKEYPAIR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            } else if (collator.compare(flags, "-help") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            } else if (collator.compare(flags, "-identitydb") == 0) { // obsolete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                command = IDENTITYDB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            } else if (collator.compare(flags, "-import") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    collator.compare(flags, "-importcert") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                command = IMPORTCERT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            } else if (collator.compare(flags, "-keyclone") == 0) { // obsolete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                command = KEYCLONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            } else if (collator.compare(flags, "-changealias") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                command = CHANGEALIAS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            } else if (collator.compare(flags, "-keypasswd") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                command = KEYPASSWD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            } else if (collator.compare(flags, "-list") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                command = LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            } else if (collator.compare(flags, "-printcert") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                command = PRINTCERT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            } else if (collator.compare(flags, "-selfcert") == 0) {     // obsolete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                command = SELFCERT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            } else if (collator.compare(flags, "-storepasswd") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                command = STOREPASSWD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            } else if (collator.compare(flags, "-importkeystore") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                command = IMPORTKEYSTORE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            } else if (collator.compare(flags, "-genseckey") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                command = GENSECKEY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
             * specifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            else if (collator.compare(flags, "-keystore") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    collator.compare(flags, "-destkeystore") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                ksfname = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } else if (collator.compare(flags, "-storepass") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    collator.compare(flags, "-deststorepass") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                storePass = args[i].toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                passwords.add(storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            } else if (collator.compare(flags, "-storetype") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    collator.compare(flags, "-deststoretype") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                storetype = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            } else if (collator.compare(flags, "-srcstorepass") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                srcstorePass = args[i].toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                passwords.add(srcstorePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            } else if (collator.compare(flags, "-srcstoretype") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                srcstoretype = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            } else if (collator.compare(flags, "-srckeypass") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                srckeyPass = args[i].toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                passwords.add(srckeyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            } else if (collator.compare(flags, "-srcprovidername") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                srcProviderName = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            } else if (collator.compare(flags, "-providername") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    collator.compare(flags, "-destprovidername") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                providerName = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            } else if (collator.compare(flags, "-providerpath") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                pathlist = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            } else if (collator.compare(flags, "-keypass") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                keyPass = args[i].toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                passwords.add(keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            } else if (collator.compare(flags, "-new") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                newPass = args[i].toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                passwords.add(newPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } else if (collator.compare(flags, "-destkeypass") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                destKeyPass = args[i].toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                passwords.add(destKeyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            } else if (collator.compare(flags, "-alias") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    collator.compare(flags, "-srcalias") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                alias = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } else if (collator.compare(flags, "-dest") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    collator.compare(flags, "-destalias") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                dest = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            } else if (collator.compare(flags, "-dname") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                dname = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            } else if (collator.compare(flags, "-keysize") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                keysize = Integer.parseInt(args[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            } else if (collator.compare(flags, "-keyalg") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                keyAlgName = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            } else if (collator.compare(flags, "-sigalg") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                sigAlgName = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            } else if (collator.compare(flags, "-startdate") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                startDate = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            } else if (collator.compare(flags, "-validity") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                validity = Long.parseLong(args[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            } else if (collator.compare(flags, "-file") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                filename = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            } else if (collator.compare(flags, "-srckeystore") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                srcksfname = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            } else if ((collator.compare(flags, "-provider") == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        (collator.compare(flags, "-providerclass") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                if (++i == args.length) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                if (providers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    providers = new HashSet<Pair <String, String>> (3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                String providerClass = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                String providerArg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                if (args.length > (i+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    flags = args[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    if (collator.compare(flags, "-providerarg") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        if (args.length == (i+2)) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        providerArg = args[i+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                providers.add(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        new Pair<String, String>(providerClass, providerArg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
             * options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            else if (collator.compare(flags, "-v") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            } else if (collator.compare(flags, "-debug") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            } else if (collator.compare(flags, "-rfc") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                rfc = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            } else if (collator.compare(flags, "-noprompt") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                noprompt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            } else if (collator.compare(flags, "-trustcacerts") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                trustcacerts = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            } else if (collator.compare(flags, "-protected") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    collator.compare(flags, "-destprotected") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                protectedPath = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            } else if (collator.compare(flags, "-srcprotected") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                srcprotectedPath = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                System.err.println(rb.getString("Illegal option:  ") + flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (i<args.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                (rb.getString("Usage error, <arg> is not a legal command"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            Object[] source = {args[i]};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            throw new RuntimeException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (command == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            System.err.println(rb.getString("Usage error: no command provided"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Execute the commands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    void doCommands(PrintStream out) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (storetype == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            storetype = KeyStore.getDefaultType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        storetype = KeyStoreUtil.niceStoreTypeName(storetype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (srcstoretype == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            srcstoretype = KeyStore.getDefaultType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        srcstoretype = KeyStoreUtil.niceStoreTypeName(srcstoretype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (P11KEYSTORE.equalsIgnoreCase(storetype) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            token = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            if (ksfname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                ksfname = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (NONE.equals(ksfname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            nullStream = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (token && !nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            System.err.println(MessageFormat.format(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                ("-keystore must be NONE if -storetype is {0}"), storetype));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (token &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            (command == KEYPASSWD || command == STOREPASSWD)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            throw new UnsupportedOperationException(MessageFormat.format(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        ("-storepasswd and -keypasswd commands not supported " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        "if -storetype is {0}"), storetype));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (P12KEYSTORE.equalsIgnoreCase(storetype) && command == KEYPASSWD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            throw new UnsupportedOperationException(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        ("-keypasswd commands not supported " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        "if -storetype is PKCS12"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (token && (keyPass != null || newPass != null || destKeyPass != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            throw new IllegalArgumentException(MessageFormat.format(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                ("-keypass and -new " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                "can not be specified if -storetype is {0}"), storetype));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (protectedPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (storePass != null || keyPass != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    newPass != null || destKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                throw new IllegalArgumentException(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        ("if -protected is specified, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        "then -storepass, -keypass, and -new " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        "must not be specified"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (srcprotectedPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (srcstorePass != null || srckeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                throw new IllegalArgumentException(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        ("if -srcprotected is specified, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        "then -srcstorepass and -srckeypass " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        "must not be specified"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            if (storePass != null || keyPass != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    newPass != null || destKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                throw new IllegalArgumentException(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        ("if keystore is not password protected, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        "then -storepass, -keypass, and -new " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        "must not be specified"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (srcstorePass != null || srckeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                throw new IllegalArgumentException(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        ("if source keystore is not password protected, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        "then -srcstorepass and -srckeypass " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                        "must not be specified"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (validity <= (long)0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                (rb.getString("Validity must be greater than zero"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        // Try to load and install specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (providers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            ClassLoader cl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if (pathlist != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                String path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                path = PathList.appendPath(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        path, System.getProperty("java.class.path"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                path = PathList.appendPath(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        path, System.getProperty("env.class.path"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                path = PathList.appendPath(path, pathlist);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                URL[] urls = PathList.pathToURLs(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                cl = new URLClassLoader(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            for (Pair <String, String> provider: providers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                String provName = provider.fst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                Class<?> provClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    provClass = cl.loadClass(provName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    provClass = Class.forName(provName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                String provArg = provider.snd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                if (provArg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    obj = provClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    Constructor<?> c = provClass.getConstructor(PARAM_STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    obj = c.newInstance(provArg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                if (!(obj instanceof Provider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                        (rb.getString("provName not a provider"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    Object[] source = {provName};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                Security.addProvider((Provider)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (command == LIST && verbose && rfc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                ("Must not specify both -v and -rfc with 'list' command"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        // Make sure provided passwords are at least 6 characters long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (command == GENKEYPAIR && keyPass!=null && keyPass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                ("Key password must be at least 6 characters"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (newPass != null && newPass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                ("New password must be at least 6 characters"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (destKeyPass != null && destKeyPass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                ("New password must be at least 6 characters"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        // Check if keystore exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        // If no keystore has been specified at the command line, try to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        // the default, which is located in $HOME/.keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        // If the command is "genkey", "identitydb", "import", or "printcert",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        // it is OK not to have a keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if (command != PRINTCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            if (ksfname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                ksfname = System.getProperty("user.home") + File.separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    + ".keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (!nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    ksfile = new File(ksfname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    // Check if keystore file is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    if (ksfile.exists() && ksfile.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        ("Keystore file exists, but is empty: ") + ksfname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    ksStream = new FileInputStream(ksfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    if (command != GENKEYPAIR &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        command != GENSECKEY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        command != IDENTITYDB &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        command != IMPORTCERT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                        command != IMPORTKEYSTORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                        throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                ("Keystore file does not exist: ") + ksfname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if ((command == KEYCLONE || command == CHANGEALIAS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                && dest == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            dest = getAlias("destination");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if ("".equals(dest)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        ("Must specify destination alias"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (command == DELETE && alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            alias = getAlias(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            if ("".equals(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                throw new Exception(rb.getString("Must specify alias"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        // Create new keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (providerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            keyStore = KeyStore.getInstance(storetype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            keyStore = KeyStore.getInstance(storetype, providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * Load the keystore data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * At this point, it's OK if no keystore password has been provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * We want to make sure that we can load the keystore data, i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * the keystore data has the right format. If we cannot load the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * keystore, why bother asking the user for his or her password?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         * Only if we were able to load the keystore, and no keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         * password has been provided, will we prompt the user for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         * keystore password to verify the keystore integrity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
         * This means that the keystore is loaded twice: first load operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
         * checks the keystore format, second load operation verifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
         * keystore integrity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         * If the keystore password has already been provided (at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         * command line), however, the keystore is loaded only once, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         * keystore format and integrity are checked "at the same time".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         * Null stream keystores are loaded later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (!nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            keyStore.load(ksStream, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            if (ksStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                ksStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        // All commands that create or modify the keystore require a keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        // password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (nullStream && storePass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            keyStore.load(null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        } else if (!nullStream && storePass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            // If we are creating a new non nullStream-based keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            // insist that the password be at least 6 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            if (ksStream == null && storePass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                        ("Keystore password must be at least 6 characters"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        } else if (storePass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            // only prompt if (protectedPath == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            if (!protectedPath && !KeyStoreUtil.isWindowsKeyStore(storetype) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                (command == CERTREQ ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        command == DELETE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        command == GENKEYPAIR ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        command == GENSECKEY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                        command == IMPORTCERT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        command == IMPORTKEYSTORE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                        command == KEYCLONE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        command == CHANGEALIAS ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                        command == SELFCERT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        command == STOREPASSWD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        command == KEYPASSWD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        command == IDENTITYDB)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    if (command == IMPORTKEYSTORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        System.err.print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                                (rb.getString("Enter destination keystore password:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        System.err.print
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                                (rb.getString("Enter keystore password:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    storePass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    passwords.add(storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    // If we are creating a new non nullStream-based keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    // insist that the password be at least 6 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    if (!nullStream && (storePass == null || storePass.length < 6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                                ("Keystore password is too short - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                                "must be at least 6 characters"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                        storePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    // If the keystore file does not exist and needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    // created, the storepass should be prompted twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    if (storePass != null && !nullStream && ksStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        System.err.print(rb.getString("Re-enter new password: "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        char[] storePassAgain = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        passwords.add(storePassAgain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                        if (!Arrays.equals(storePass, storePassAgain)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                            System.err.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                                (rb.getString("They don't match. Try again"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                            storePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                } while ((storePass == null) && count < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                if (storePass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    System.err.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        (rb.getString("Too many failures - try later"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            } else if (!protectedPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    && !KeyStoreUtil.isWindowsKeyStore(storetype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    && !(command == PRINTCERT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                // here we have EXPORTCERT and LIST (info valid until STOREPASSWD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                System.err.print(rb.getString("Enter keystore password:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                storePass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                passwords.add(storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            // Now load a nullStream-based keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            // or verify the integrity of an input stream-based keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                keyStore.load(null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            } else if (ksStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                ksStream = new FileInputStream(ksfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                keyStore.load(ksStream, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                ksStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        if (storePass != null && P12KEYSTORE.equalsIgnoreCase(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            MessageFormat form = new MessageFormat(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                "Warning:  Different store and key passwords not supported " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                "for PKCS12 KeyStores. Ignoring user-specified <command> value."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            if (keyPass != null && !Arrays.equals(storePass, keyPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                Object[] source = {"-keypass"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                keyPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            if (newPass != null && !Arrays.equals(storePass, newPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                Object[] source = {"-new"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                newPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            if (destKeyPass != null && !Arrays.equals(storePass, destKeyPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                Object[] source = {"-destkeypass"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                destKeyPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        // Create a certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (command == PRINTCERT || command == IMPORTCERT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
               || command == IDENTITYDB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (trustcacerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            caks = getCacertsKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        // Perform the specified command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (command == CERTREQ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            PrintStream ps = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                ps = new PrintStream(new FileOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                                                 (filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                out = ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                doCertReq(alias, sigAlgName, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                if (ps != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    ps.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            if (verbose && filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                        ("Certification request stored in file <filename>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                Object[] source = {filename};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                System.err.println(rb.getString("Submit this to your CA"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        } else if (command == DELETE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            doDeleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        } else if (command == EXPORTCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            PrintStream ps = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                ps = new PrintStream(new FileOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                                 (filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                out = ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                doExportCert(alias, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                if (ps != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    ps.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                        ("Certificate stored in file <filename>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                Object[] source = {filename};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        } else if (command == GENKEYPAIR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            if (keyAlgName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                keyAlgName = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            doGenKeyPair(alias, dname, keyAlgName, keysize, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        } else if (command == GENSECKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            if (keyAlgName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                keyAlgName = "DES";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            doGenSecretKey(alias, keyAlgName, keysize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        } else if (command == IDENTITYDB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            InputStream inStream = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                inStream = new FileInputStream(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                doImportIdentityDatabase(inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                if (inStream != System.in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    inStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        } else if (command == IMPORTCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            InputStream inStream = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                inStream = new FileInputStream(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                String importAlias = (alias!=null)?alias:keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                if (keyStore.entryInstanceOf(importAlias, KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    kssave = installReply(importAlias, inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    if (kssave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                            ("Certificate reply was installed in keystore"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                            ("Certificate reply was not installed in keystore"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                } else if (!keyStore.containsAlias(importAlias) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                        keyStore.entryInstanceOf(importAlias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                            KeyStore.TrustedCertificateEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    kssave = addTrustedCert(importAlias, inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    if (kssave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                            ("Certificate was added to keystore"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                            ("Certificate was not added to keystore"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                if (inStream != System.in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    inStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        } else if (command == IMPORTKEYSTORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            doImportKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        } else if (command == KEYCLONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            keyPassNew = newPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            // added to make sure only key can go thru
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    (rb.getString("Alias <alias> does not exist"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            if (!keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                MessageFormat form = new MessageFormat(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                        "Alias <alias> references an entry type that is not a private key entry.  " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                        "The -keyclone command only supports cloning of private key entries"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            doCloneEntry(alias, dest, true);  // Now everything can be cloned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        } else if (command == CHANGEALIAS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            doCloneEntry(alias, dest, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            // in PKCS11, clone a PrivateKeyEntry will delete the old one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                doDeleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        } else if (command == KEYPASSWD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            keyPassNew = newPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            doChangeKeyPasswd(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        } else if (command == LIST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            if (alias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                doPrintEntry(alias, out, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                doPrintEntries(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        } else if (command == PRINTCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            InputStream inStream = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                inStream = new FileInputStream(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                doPrintCert(inStream, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                if (inStream != System.in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    inStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        } else if (command == SELFCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            doSelfCert(alias, dname, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        } else if (command == STOREPASSWD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            storePassNew = newPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (storePassNew == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                storePassNew = getNewPasswd("keystore password", storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        // If we need to save the keystore, do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        if (kssave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                        (rb.getString("[Storing ksfname]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                Object[] source = {nullStream ? "keystore" : ksfname};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            if (token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                keyStore.store(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                FileOutputStream fout = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                    fout = (nullStream ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                                        (FileOutputStream)null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                                        new FileOutputStream(ksfname));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                    keyStore.store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                        (fout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                        (storePassNew!=null) ? storePassNew : storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                    if (fout != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                        fout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * Creates a PKCS#10 cert signing request, corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * keys (and name) associated with a given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    private void doCertReq(String alias, String sigAlgName, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        Object[] objs = recoverKey(alias, storePass, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        PrivateKey privKey = (PrivateKey)objs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            keyPass = (char[])objs[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        Certificate cert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        if (cert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                (rb.getString("alias has no public key (certificate)"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        PKCS10 request = new PKCS10(cert.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        // Construct an X500Signer object, so that we can sign the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        if (sigAlgName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            // If no signature algorithm was specified at the command line,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            // we choose one that is compatible with the selected private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            String keyAlgName = privKey.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            if ("DSA".equalsIgnoreCase(keyAlgName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                   || "DSS".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                sigAlgName = "SHA1WithDSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            } else if ("RSA".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                sigAlgName = "SHA1WithRSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                        ("Cannot derive signature algorithm"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        Signature signature = Signature.getInstance(sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        signature.initSign(privKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        X500Name subject =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            new X500Name(((X509Certificate)cert).getSubjectDN().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        X500Signer signer = new X500Signer(signature, subject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        // Sign the request and base-64 encode it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        request.encodeAndSign(signer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        request.print(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * Deletes an entry from the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    private void doDeleteEntry(String alias) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                (rb.getString("Alias <alias> does not exist"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        keyStore.deleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * Exports a certificate from the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    private void doExportCert(String alias, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        if (storePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                && !KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            printWarning();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                (rb.getString("Alias <alias> does not exist"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        X509Certificate cert = (X509Certificate)keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        if (cert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                (rb.getString("Alias <alias> has no certificate"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        dumpCert(cert, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * Prompt the user for a keypass when generating a key entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @param alias the entry we will set password for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @param orig the original entry of doing a dup, null if generate new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @param origPass the password to copy from if user press ENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    private char[] promptForKeyPass(String alias, String orig, char[] origPass) throws Exception{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        if (P12KEYSTORE.equalsIgnoreCase(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            return origPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        } else if (!token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            // Prompt for key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            for (count = 0; count < 3; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                        ("Enter key password for <alias>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                if (orig == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    System.err.print(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                            ("\t(RETURN if same as keystore password):  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                    form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                            ("\t(RETURN if same as for <otherAlias>)"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                    Object[] src = {orig};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    System.err.print(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                char[] entered = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                passwords.add(entered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                if (entered == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                    return origPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                } else if (entered.length >= 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    System.err.print(rb.getString("Re-enter new password: "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                    char[] passAgain = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    passwords.add(passAgain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    if (!Arrays.equals(entered, passAgain)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                        System.err.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                            (rb.getString("They don't match. Try again"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                    return entered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                    System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                        ("Key password is too short - must be at least 6 characters"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            if (count == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                if (command == KEYCLONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                        ("Too many failures. Key entry not cloned"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                            ("Too many failures - key not added to keystore"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return null;    // PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * Creates a new secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    private void doGenSecretKey(String alias, String keyAlgName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                              int keysize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                ("Secret key not generated, alias <alias> already exists"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        SecretKey secKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        KeyGenerator keygen = KeyGenerator.getInstance(keyAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        if (keysize != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            keygen.init(keysize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        } else if ("DES".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            keygen.init(56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        } else if ("DESede".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            keygen.init(168);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                ("Please provide -keysize for secret key generation"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        secKey = keygen.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            keyPass = promptForKeyPass(alias, null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        keyStore.setKeyEntry(alias, secKey, keyPass, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * Creates a new key pair and self-signed certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    private void doGenKeyPair(String alias, String dname, String keyAlgName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                              int keysize, String sigAlgName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        if (keysize == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            if ("EC".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                keysize = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                keysize = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                ("Key pair not generated, alias <alias> already exists"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        if (sigAlgName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            if ("DSA".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                sigAlgName = "SHA1WithDSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            } else if ("RSA".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                sigAlgName = "SHA1WithRSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            } else if ("EC".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                sigAlgName = "SHA1withECDSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                        ("Cannot derive signature algorithm"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        CertAndKeyGen keypair =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                new CertAndKeyGen(keyAlgName, sigAlgName, providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        // If DN is provided, parse it. Otherwise, prompt the user for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        X500Name x500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        if (dname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            x500Name = getX500Name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            x500Name = new X500Name(dname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        keypair.generate(keysize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        PrivateKey privKey = keypair.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        X509Certificate[] chain = new X509Certificate[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        chain[0] = keypair.getSelfCertificate(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                x500Name, getStartDate(startDate), validity*24L*60L*60L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                ("Generating keysize bit keyAlgName key pair and self-signed certificate " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    "(sigAlgName) with a validity of validality days\n\tfor: x500Name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            Object[] source = {new Integer(keysize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                                privKey.getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                                chain[0].getSigAlgName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                                new Long(validity),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                                x500Name};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            keyPass = promptForKeyPass(alias, null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        keyStore.setKeyEntry(alias, privKey, keyPass, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * Clones an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * @param orig original alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * @param dest destination alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * @changePassword if the password can be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    private void doCloneEntry(String orig, String dest, boolean changePassword)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        if (orig == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            orig = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        if (keyStore.containsAlias(dest)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                (rb.getString("Destination alias <dest> already exists"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            Object[] source = {dest};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        Object[] objs = recoverEntry(keyStore, orig, storePass, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        Entry entry = (Entry)objs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        keyPass = (char[])objs[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        PasswordProtection pp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        if (keyPass != null) {  // protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            if (!changePassword || P12KEYSTORE.equalsIgnoreCase(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                keyPassNew = keyPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                if (keyPassNew == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    keyPassNew = promptForKeyPass(dest, orig, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            pp = new PasswordProtection(keyPassNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        keyStore.setEntry(dest, entry, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * Changes a key password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    private void doChangeKeyPasswd(String alias) throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        Object[] objs = recoverKey(alias, storePass, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        Key privKey = (Key)objs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            keyPass = (char[])objs[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        if (keyPassNew == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                (rb.getString("key password for <alias>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            keyPassNew = getNewPasswd(form.format(source), keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        keyStore.setKeyEntry(alias, privKey, keyPassNew,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                             keyStore.getCertificateChain(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * Imports a JDK 1.1-style identity database. We can only store one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * certificate per identity, because we use the identity's name as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * alias (which references a keystore entry), and aliases must be unique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    private void doImportIdentityDatabase(InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        ByteArrayInputStream bais;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        java.security.cert.X509Certificate newCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        java.security.cert.Certificate[] chain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        PrivateKey privKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        boolean modified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        IdentityDatabase idb = IdentityDatabase.fromStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        for (Enumeration<Identity> enum_ = idb.identities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                                        enum_.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            Identity id = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            newCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            // only store trusted identities in keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            if ((id instanceof SystemSigner && ((SystemSigner)id).isTrusted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                || (id instanceof SystemIdentity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                    && ((SystemIdentity)id).isTrusted())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                // ignore if keystore entry with same alias name already exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                if (keyStore.containsAlias(id.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                    MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                        (rb.getString("Keystore entry for <id.getName()> already exists"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                    Object[] source = {id.getName()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                    System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                java.security.Certificate[] certs = id.certificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                if (certs!=null && certs.length>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                    // we can only store one user cert per identity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                    // convert old-style to new-style cert via the encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                    DerOutputStream dos = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    certs[0].encode(dos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    encoded = dos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                    bais = new ByteArrayInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    newCert = (X509Certificate)cf.generateCertificate(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                    bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                    // if certificate is self-signed, make sure it verifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                    if (isSelfSigned(newCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                        PublicKey pubKey = newCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                            newCert.verify(pubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                            // ignore this cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                    if (id instanceof SystemSigner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                        MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                            ("Creating keystore entry for <id.getName()> ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                        Object[] source = {id.getName()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                        System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                        if (chain==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                            chain = new java.security.cert.Certificate[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                        chain[0] = newCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                        privKey = ((SystemSigner)id).getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                        keyStore.setKeyEntry(id.getName(), privKey, storePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                                             chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                        keyStore.setCertificateEntry(id.getName(), newCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                    kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        if (!kssave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                ("No entries from identity database added"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * Prints a single keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    private void doPrintEntry(String alias, PrintStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                              boolean printWarning)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        if (storePass == null && printWarning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                && !KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            printWarning();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                (rb.getString("Alias <alias> does not exist"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                (rb.getString("Alias name: alias"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            if (!token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                    ("Creation date: keyStore.getCreationDate(alias)"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                Object[] src = {keyStore.getCreationDate(alias)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                out.println(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            if (!token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                    (rb.getString("alias, keyStore.getCreationDate(alias), "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                Object[] source = {alias, keyStore.getCreationDate(alias)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                out.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                    (rb.getString("alias, "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                out.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        if (keyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                Object[] source = {"SecretKeyEntry"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                out.println(new MessageFormat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                        rb.getString("Entry type: <type>")).format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                out.println("SecretKeyEntry, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        } else if (keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                Object[] source = {"PrivateKeyEntry"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                out.println(new MessageFormat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                        rb.getString("Entry type: <type>")).format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                out.println("PrivateKeyEntry, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            // Get the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            Certificate[] chain = keyStore.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            if (chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                    out.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                        ("Certificate chain length: ") + chain.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                    for (int i = 0; i < chain.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                        MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                                (rb.getString("Certificate[(i + 1)]:"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                        Object[] source = {new Integer((i + 1))};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                        out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                        if (verbose && (chain[i] instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                            printX509Cert((X509Certificate)(chain[i]), out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                        } else if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                            out.println(chain[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                            dumpCert(chain[i], out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                    // Print the digest of the user cert only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                    out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                        (rb.getString("Certificate fingerprint (MD5): ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                        getCertFingerPrint("MD5", chain[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        } else if (keyStore.entryInstanceOf(alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                KeyStore.TrustedCertificateEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            // We have a trusted certificate entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            Certificate cert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            if (verbose && (cert instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                out.println(rb.getString("Entry type: trustedCertEntry\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                printX509Cert((X509Certificate)cert, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            } else if (rfc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                out.println(rb.getString("Entry type: trustedCertEntry\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                dumpCert(cert, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            } else if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                out.println(cert.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                out.println(rb.getString("trustedCertEntry,"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                out.println(rb.getString("Certificate fingerprint (MD5): ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                            + getCertFingerPrint("MD5", cert));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            out.println(rb.getString("Unknown Entry Type"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * Load the srckeystore from a stream, used in -importkeystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * @returns the src KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    KeyStore loadSourceKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        boolean isPkcs11 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        if (P11KEYSTORE.equalsIgnoreCase(srcstoretype) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            if (!NONE.equals(srcksfname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                System.err.println(MessageFormat.format(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                    ("-keystore must be NONE if -storetype is {0}"), srcstoretype));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            isPkcs11 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            if (srcksfname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                File srcksfile = new File(srcksfname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                    if (srcksfile.exists() && srcksfile.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                        throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                                ("Source keystore file exists, but is empty: ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                                srcksfname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                is = new FileInputStream(srcksfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                        ("Please specify -srckeystore"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        KeyStore store;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            if (srcProviderName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                store = KeyStore.getInstance(srcstoretype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                store = KeyStore.getInstance(srcstoretype, srcProviderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            if (srcstorePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                    && !srcprotectedPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    && !KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                System.err.print(rb.getString("Enter source keystore password:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                srcstorePass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                passwords.add(srcstorePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            // always let keypass be storepass when using pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            if (P12KEYSTORE.equalsIgnoreCase(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                if (srckeyPass != null && srcstorePass != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                        !Arrays.equals(srcstorePass, srckeyPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                    MessageFormat form = new MessageFormat(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                        "Warning:  Different store and key passwords not supported " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                        "for PKCS12 KeyStores. Ignoring user-specified <command> value."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                    Object[] source = {"-srckeypass"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                    System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                    srckeyPass = srcstorePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            store.load(is, srcstorePass);   // "is" already null in PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        if (srcstorePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                && !KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            // anti refactoring, copied from printWarning(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            // but change 2 lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                ("*****************  WARNING WARNING WARNING  *****************"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                ("* The integrity of the information stored in the srckeystore*"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                ("* has NOT been verified!  In order to verify its integrity, *"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                ("* you must provide the srckeystore password.                *"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                ("*****************  WARNING WARNING WARNING  *****************"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        return store;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * import all keys and certs from importkeystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * keep alias unchanged if no name conflict, otherwise, prompt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * keep keypass unchanged for keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    private void doImportKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        if (alias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            doImportKeyStoreSingle(loadSourceKeyStore(), alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            if (dest != null || srckeyPass != null || destKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                throw new Exception(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                        "if alias not specified, destalias, srckeypass, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                        "and destkeypass must not be specified"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            doImportKeyStoreAll(loadSourceKeyStore());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
         * Information display rule of -importkeystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
         * 1. inside single, shows failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
         * 2. inside all, shows sucess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
         * 3. inside all where there is a failure, prompt for continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
         * 4. at the final of all, shows summary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * Import a single entry named alias from srckeystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * @returns 1 if the import action succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     *          0 if user choose to ignore an alias-dumplicated entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     *          2 if setEntry throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    private int doImportKeyStoreSingle(KeyStore srckeystore, String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        String newAlias = (dest==null) ? alias : dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        if (keyStore.containsAlias(newAlias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            if (noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                System.err.println(new MessageFormat(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                        "Warning: Overwriting existing alias <alias> in destination keystore")).format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                String reply = getYesNoReply(new MessageFormat(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                        "Existing entry alias <alias> exists, overwrite? [no]:  ")).format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                if ("NO".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                    newAlias = inputStringFromStdin(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                            ("Enter new alias name\t(RETURN to cancel import for this entry):  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                    if ("".equals(newAlias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                        System.err.println(new MessageFormat(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                                "Entry for alias <alias> not imported.")).format(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                                source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        Object[] objs = recoverEntry(srckeystore, alias, srcstorePass, srckeyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        Entry entry = (Entry)objs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        PasswordProtection pp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        // According to keytool.html, "The destination entry will be protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        // using destkeypass. If destkeypass is not provided, the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        // entry will be protected with the source entry password."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        // so always try to protect with destKeyPass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        if (destKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            pp = new PasswordProtection(destKeyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        } else if (objs[1] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            pp = new PasswordProtection((char[])objs[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            keyStore.setEntry(newAlias, entry, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            Object[] source2 = {alias, kse.toString()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            MessageFormat form = new MessageFormat(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                    "Problem importing entry for alias <alias>: <exception>.\nEntry for alias <alias> not imported."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            System.err.println(form.format(source2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    private void doImportKeyStoreAll(KeyStore srckeystore) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        int ok = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        int count = srckeystore.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        for (Enumeration<String> e = srckeystore.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                                        e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            int result = doImportKeyStoreSingle(srckeystore, alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            if (result == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                ok++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                MessageFormat form = new MessageFormat(rb.getString("Entry for alias <alias> successfully imported."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            } else if (result == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                if (!noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                    String reply = getYesNoReply("Do you want to quit the import process? [no]:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                    if ("YES".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        Object[] source = {ok, count-ok};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        MessageFormat form = new MessageFormat(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                "Import command completed:  <ok> entries successfully imported, <fail> entries failed or cancelled"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * Prints all keystore entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
    private void doPrintEntries(PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        if (storePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                && !KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            printWarning();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        out.println(rb.getString("Keystore type: ") + keyStore.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        out.println(rb.getString("Keystore provider: ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                keyStore.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        MessageFormat form;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        form = (keyStore.size() == 1) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                        ("Your keystore contains keyStore.size() entry")) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                        ("Your keystore contains keyStore.size() entries"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        Object[] source = {new Integer(keyStore.size())};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        for (Enumeration<String> e = keyStore.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                                        e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            doPrintEntry(alias, out, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            if (verbose || rfc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                out.println(rb.getString("\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                out.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                        ("*******************************************"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                out.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                        ("*******************************************\n\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * Reads a certificate (or certificate chain) and prints its contents in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * a human readbable format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
    private void doPrintCert(InputStream in, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        Collection<? extends Certificate> c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            c = cf.generateCertificates(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            throw new Exception(rb.getString("Failed to parse input"), ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        if (c.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            throw new Exception(rb.getString("Empty input"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        Certificate[] certs = c.toArray(new Certificate[c.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        for (int i=0; i<certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            X509Certificate x509Cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                x509Cert = (X509Certificate)certs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                throw new Exception(rb.getString("Not X.509 certificate"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            if (certs.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                        (rb.getString("Certificate[(i + 1)]:"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                Object[] source = {new Integer(i + 1)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            printX509Cert(x509Cert, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            if (i < (certs.length-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * Creates a self-signed certificate, and stores it as a single-element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    private void doSelfCert(String alias, String dname, String sigAlgName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        Object[] objs = recoverKey(alias, storePass, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        PrivateKey privKey = (PrivateKey)objs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        if (keyPass == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            keyPass = (char[])objs[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        // Determine the signature algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        if (sigAlgName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            // If no signature algorithm was specified at the command line,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            // we choose one that is compatible with the selected private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            String keyAlgName = privKey.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            if ("DSA".equalsIgnoreCase(keyAlgName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                   || "DSS".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                sigAlgName = "SHA1WithDSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            } else if ("RSA".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                sigAlgName = "SHA1WithRSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            } else if ("EC".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                sigAlgName = "SHA1withECDSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                        (rb.getString("Cannot derive signature algorithm"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        // Get the old certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        Certificate oldCert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        if (oldCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                (rb.getString("alias has no public key"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        if (!(oldCert instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                (rb.getString("alias has no X.509 certificate"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        // convert to X509CertImpl, so that we can modify selected fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        // (no public APIs available yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        byte[] encoded = oldCert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        X509CertImpl certImpl = new X509CertImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        X509CertInfo certInfo = (X509CertInfo)certImpl.get(X509CertImpl.NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                                                           + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                                                           X509CertImpl.INFO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        // Extend its validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        Date firstDate = getStartDate(startDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        Date lastDate = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        lastDate.setTime(firstDate.getTime() + validity*1000L*24L*60L*60L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        CertificateValidity interval = new CertificateValidity(firstDate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                                                               lastDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        certInfo.set(X509CertInfo.VALIDITY, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        // Make new serial number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                     ((int)(firstDate.getTime()/1000)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        // Set owner and issuer fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        X500Name owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        if (dname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            // Get the owner name from the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            owner = (X500Name)certInfo.get(X509CertInfo.SUBJECT + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                                           CertificateSubjectName.DN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            // Use the owner name specified at the command line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            owner = new X500Name(dname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            certInfo.set(X509CertInfo.SUBJECT + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                         CertificateSubjectName.DN_NAME, owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        // Make issuer same as owner (self-signed!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        certInfo.set(X509CertInfo.ISSUER + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                     CertificateIssuerName.DN_NAME, owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        // The inner and outer signature algorithms have to match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        // The way we achieve that is really ugly, but there seems to be no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        // other solution: We first sign the cert, then retrieve the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        // outer sigalg and use it to set the inner sigalg
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        X509CertImpl newCert = new X509CertImpl(certInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        newCert.sign(privKey, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        AlgorithmId sigAlgid = (AlgorithmId)newCert.get(X509CertImpl.SIG_ALG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        certInfo.set(CertificateAlgorithmId.NAME + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                     CertificateAlgorithmId.ALGORITHM, sigAlgid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        // first upgrade to version 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        certInfo.set(X509CertInfo.VERSION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                        new CertificateVersion(CertificateVersion.V3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        // Sign the new certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        newCert = new X509CertImpl(certInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        newCert.sign(privKey, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        // Store the new certificate as a single-element certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        keyStore.setKeyEntry(alias, privKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                             (keyPass != null) ? keyPass : storePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                             new Certificate[] { newCert } );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            System.err.println(rb.getString("New certificate (self-signed):"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            System.err.print(newCert.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * Processes a certificate reply from a certificate authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * <p>Builds a certificate chain on top of the certificate reply,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     * using trusted certificates from the keystore. The chain is complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * after a self-signed certificate has been encountered. The self-signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * certificate is considered a root certificate authority, and is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * at the end of the chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * <p>The newly generated chain replaces the old chain associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * key entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * @return true if the certificate reply was installed, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
    private boolean installReply(String alias, InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        Object[] objs = recoverKey(alias, storePass, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        PrivateKey privKey = (PrivateKey)objs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            keyPass = (char[])objs[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        Certificate userCert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        if (userCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                (rb.getString("alias has no public key (certificate)"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        // Read the certificates in the reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        Collection<? extends Certificate> c = cf.generateCertificates(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        if (c.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            throw new Exception(rb.getString("Reply has no certificates"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        Certificate[] replyCerts = c.toArray(new Certificate[c.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        Certificate[] newChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        if (replyCerts.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            // single-cert reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            newChain = establishCertChain(userCert, replyCerts[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            // cert-chain reply (e.g., PKCS#7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            newChain = validateReply(alias, userCert, replyCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        // Now store the newly established chain in the keystore. The new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        // chain replaces the old one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        if (newChain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            keyStore.setKeyEntry(alias, privKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                                 (keyPass != null) ? keyPass : storePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                                 newChain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * Imports a certificate and adds it to the list of trusted certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * @return true if the certificate was added, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    private boolean addTrustedCert(String alias, InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            throw new Exception(rb.getString("Must specify alias"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                ("Certificate not imported, alias <alias> already exists"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        // Read the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        X509Certificate cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            cert = (X509Certificate)cf.generateCertificate(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            throw new Exception(rb.getString("Input not an X.509 certificate"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            throw new Exception(rb.getString("Input not an X.509 certificate"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        // if certificate is self-signed, make sure it verifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        boolean selfSigned = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        if (isSelfSigned(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
            cert.verify(cert.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            selfSigned = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        if (noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        // check if cert already exists in keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        String reply = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        String trustalias = keyStore.getCertificateAlias(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        if (trustalias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                ("Certificate already exists in keystore under alias <trustalias>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
            Object[] source = {trustalias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            reply = getYesNoReply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                (rb.getString("Do you still want to add it? [no]:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        } else if (selfSigned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            if (trustcacerts && (caks != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                    ((trustalias=caks.getCertificateAlias(cert)) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                        ("Certificate already exists in system-wide CA keystore under alias <trustalias>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                Object[] source = {trustalias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                reply = getYesNoReply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                        (rb.getString("Do you still want to add it to your own keystore? [no]:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            if (trustalias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                // Print the cert and ask user if they really want to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                // it to their keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                printX509Cert(cert, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                reply = getYesNoReply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                        (rb.getString("Trust this certificate? [no]:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        if (reply != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            if ("YES".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        // Try to establish trust chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            Certificate[] chain = establishCertChain(null, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            if (chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            // Print the cert and ask user if they really want to add it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            // their keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            printX509Cert(cert, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            reply = getYesNoReply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                (rb.getString("Trust this certificate? [no]:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            if ("YES".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * Prompts user for new password. New password must be different from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * old one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * @param prompt the message that gets prompted on the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * @param oldPasswd the current (i.e., old) password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
    private char[] getNewPasswd(String prompt, char[] oldPasswd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        char[] entered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        char[] reentered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        for (int count = 0; count < 3; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                (rb.getString("New prompt: "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            Object[] source = {prompt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            entered = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            passwords.add(entered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            if (entered == null || entered.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                    ("Password is too short - must be at least 6 characters"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            } else if (Arrays.equals(entered, oldPasswd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                System.err.println(rb.getString("Passwords must differ"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                        (rb.getString("Re-enter new prompt: "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                Object[] src = {prompt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                System.err.print(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                reentered = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                passwords.add(reentered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                if (!Arrays.equals(entered, reentered)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                    System.err.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                        (rb.getString("They don't match. Try again"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                    Arrays.fill(reentered, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                    return entered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            if (entered != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                Arrays.fill(entered, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                entered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
            if (reentered != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                Arrays.fill(reentered, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                reentered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        throw new Exception(rb.getString("Too many failures - try later"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * Prompts user for alias name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * @param prompt the {0} of "Enter {0} alias name:  " in prompt line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * @returns the string entered by the user, without the \n at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
    private String getAlias(String prompt) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        if (prompt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                (rb.getString("Enter prompt alias name:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            Object[] source = {prompt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
            System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            System.err.print(rb.getString("Enter alias name:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        return (new BufferedReader(new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                                        System.in))).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * Prompts user for an input string from the command line (System.in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * @prompt the prompt string printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * @returns the string entered by the user, without the \n at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
    private String inputStringFromStdin(String prompt) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
        System.err.print(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        return (new BufferedReader(new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                                        System.in))).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * Prompts user for key password. User may select to choose the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * password (<code>otherKeyPass</code>) as for <code>otherAlias</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    private char[] getKeyPasswd(String alias, String otherAlias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                                char[] otherKeyPass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        char[] keyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
            if (otherKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                        ("Enter key password for <alias>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                        ("\t(RETURN if same as for <otherAlias>)"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                Object[] src = {otherAlias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                System.err.print(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                        ("Enter key password for <alias>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            keyPass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            passwords.add(keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
            if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                keyPass = otherKeyPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        } while ((keyPass == null) && count < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            throw new Exception(rb.getString("Too many failures - try later"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        return keyPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * Prints a certificate in a human readable format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    private void printX509Cert(X509Certificate cert, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        out.println("Owner: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                    + cert.getSubjectDN().toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                    + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                    + "Issuer: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                    + cert.getIssuerDN().toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                    + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                    + "Serial number: " + cert.getSerialNumber().toString(16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                    + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                    + "Valid from: " + cert.getNotBefore().toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                    + " until: " + cert.getNotAfter().toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                    + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                    + "Certificate fingerprints:\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                    + "\t MD5:  " + getCertFingerPrint("MD5", cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                    + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                    + "\t SHA1: " + getCertFingerPrint("SHA1", cert));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
        MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                (rb.getString("*PATTERN* printX509Cert"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
        Object[] source = {cert.getSubjectDN().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                        cert.getIssuerDN().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                        cert.getSerialNumber().toString(16),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                        cert.getNotBefore().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                        cert.getNotAfter().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                        getCertFingerPrint("MD5", cert),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                        getCertFingerPrint("SHA1", cert),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                        cert.getSigAlgName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                        cert.getVersion()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
        out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
        int extnum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        if (cert instanceof X509CertImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            X509CertImpl impl = (X509CertImpl)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
            if (cert.getCriticalExtensionOIDs() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                for (String extOID : cert.getCriticalExtensionOIDs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                    if (extnum == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                        out.println(rb.getString("Extensions: "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                    out.println("#"+(++extnum)+": "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                            impl.getExtension(new ObjectIdentifier(extOID)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            if (cert.getNonCriticalExtensionOIDs() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                for (String extOID : cert.getNonCriticalExtensionOIDs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                    if (extnum == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                        out.println(rb.getString("Extensions: "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                    Extension ext = impl.getExtension(new ObjectIdentifier(extOID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                    if (ext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                        out.println("#"+(++extnum)+": "+ ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                        out.println("#"+(++extnum)+": "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                                impl.getUnparseableExtension(new ObjectIdentifier(extOID)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     * Returns true if the certificate is self-signed, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
    private boolean isSelfSigned(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
        return cert.getSubjectDN().equals(cert.getIssuerDN());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     * Returns true if the given certificate is trusted, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
    private boolean isTrusted(Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        if (keyStore.getCertificateAlias(cert) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            return true; // found in own keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        if (trustcacerts && (caks != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                (caks.getCertificateAlias(cert) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            return true; // found in CA keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     * Gets an X.500 name suitable for inclusion in a certification request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
    private X500Name getX500Name() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        BufferedReader in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        in = new BufferedReader(new InputStreamReader(System.in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        String commonName = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        String organizationalUnit = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        String organization = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        String city = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        String state = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        String country = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        X500Name name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
        String userInput = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        int maxRetry = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            if (maxRetry-- < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                throw new RuntimeException(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                        "Too may retries, program terminated"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            commonName = inputString(in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                    rb.getString("What is your first and last name?"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                    commonName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            organizationalUnit = inputString(in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                    rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                        ("What is the name of your organizational unit?"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                    organizationalUnit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            organization = inputString(in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                    rb.getString("What is the name of your organization?"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                    organization);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            city = inputString(in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                    rb.getString("What is the name of your City or Locality?"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                    city);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
            state = inputString(in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                    rb.getString("What is the name of your State or Province?"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                    state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            country = inputString(in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                    rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                        ("What is the two-letter country code for this unit?"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                    country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            name = new X500Name(commonName, organizationalUnit, organization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                                city, state, country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                (rb.getString("Is <name> correct?"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            Object[] source = {name};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            userInput = inputString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                (in, form.format(source), rb.getString("no"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        } while (collator.compare(userInput, rb.getString("yes")) != 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                 collator.compare(userInput, rb.getString("y")) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
    private String inputString(BufferedReader in, String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                               String defaultValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        System.err.println(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                (rb.getString("  [defaultValue]:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        Object[] source = {defaultValue};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        String value = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
        if (value == null || collator.compare(value, "") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            value = defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     * Writes an X.509 certificate in base64 or binary encoding to an output
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
    private void dumpCert(Certificate cert, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        throws IOException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        if (rfc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            BASE64Encoder encoder = new BASE64Encoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
            out.println(X509Factory.BEGIN_CERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            encoder.encodeBuffer(cert.getEncoded(), out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            out.println(X509Factory.END_CERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
            out.write(cert.getEncoded()); // binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     * Converts a byte to hex digit and writes to the supplied buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    private void byte2hex(byte b, StringBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                            '9', 'A', 'B', 'C', 'D', 'E', 'F' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        int high = ((b & 0xf0) >> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        int low = (b & 0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        buf.append(hexChars[high]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        buf.append(hexChars[low]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     * Converts a byte array to hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
    private String toHexString(byte[] block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        int len = block.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
             byte2hex(block[i], buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
             if (i < len-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                 buf.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * Recovers (private) key associated with given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * @return an array of objects, where the 1st element in the array is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * recovered private key, and the 2nd element is the password used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * recover it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
    private Object[] recoverKey(String alias, char[] storePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
                                       char[] keyPass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        Key key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                (rb.getString("Alias <alias> does not exist"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        if (!keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                !keyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                (rb.getString("Alias <alias> has no key"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
            // Try to recover the key using the keystore password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                key = keyStore.getKey(alias, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                keyPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                passwords.add(keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
            } catch (UnrecoverableKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                // Did not work out, so prompt user for key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
                if (!token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                    keyPass = getKeyPasswd(alias, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                    key = keyStore.getKey(alias, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
            key = keyStore.getKey(alias, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        return new Object[] {key, keyPass};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
     * Recovers entry associated with given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
     * @return an array of objects, where the 1st element in the array is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     * recovered entry, and the 2nd element is the password used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     * recover it (null if no password).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
    private Object[] recoverEntry(KeyStore ks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
                            String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
                            char[] pstore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                            char[] pkey) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        if (ks.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
            MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
                (rb.getString("Alias <alias> does not exist"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
        PasswordProtection pp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
        Entry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
            // First attempt to access entry without key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
            // (PKCS11 entry or trusted certificate entry, for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
            entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
            pkey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
        } catch (UnrecoverableEntryException une) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
            if(P11KEYSTORE.equalsIgnoreCase(ks.getType()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                KeyStoreUtil.isWindowsKeyStore(ks.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                // should not happen, but a possibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                throw une;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
            // entry is protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
            if (pkey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                // try provided key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                pp = new PasswordProtection(pkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                // try store pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                    pp = new PasswordProtection(pstore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                    entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                    pkey = pstore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                } catch (UnrecoverableEntryException une2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                    if (P12KEYSTORE.equalsIgnoreCase(ks.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                        // P12 keystore currently does not support separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                        // store and entry passwords
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                        throw une2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
                        // prompt for entry password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                        pkey = getKeyPasswd(alias, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                        pp = new PasswordProtection(pkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                        entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
        return new Object[] {entry, pkey};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     * Gets the requested finger print of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
    private String getCertFingerPrint(String mdAlg, Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
        byte[] encCertInfo = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
        MessageDigest md = MessageDigest.getInstance(mdAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        byte[] digest = md.digest(encCertInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        return toHexString(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
     * Prints warning about missing integrity check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
    private void printWarning() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
            ("*****************  WARNING WARNING WARNING  *****************"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
            ("* The integrity of the information stored in your keystore  *"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
            ("* has NOT been verified!  In order to verify its integrity, *"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
            ("* you must provide your keystore password.                  *"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
            ("*****************  WARNING WARNING WARNING  *****************"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
     * Validates chain in certification reply, and returns the ordered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
     * elements of the chain (with user certificate first, and root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
     * certificate last in the array).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
     * @param userCert the user certificate of the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
     * @param replyCerts the chain provided in the reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
    private Certificate[] validateReply(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                                        Certificate userCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                                        Certificate[] replyCerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        // order the certs in the reply (bottom-up).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
        // we know that all certs in the reply are of type X.509, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
        // we parsed them using an X.509 certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
        PublicKey userPubKey = userCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
        for (i=0; i<replyCerts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
            if (userPubKey.equals(replyCerts[i].getPublicKey())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
        if (i == replyCerts.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
            MessageFormat form = new MessageFormat(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                ("Certificate reply does not contain public key for <alias>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        Certificate tmpCert = replyCerts[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        replyCerts[0] = replyCerts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        replyCerts[i] = tmpCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        Principal issuer = ((X509Certificate)replyCerts[0]).getIssuerDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
        for (i=1; i < replyCerts.length-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
            // find a cert in the reply whose "subject" is the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
            // given "issuer"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
            int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
            for (j=i; j<replyCerts.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                Principal subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                subject = ((X509Certificate)replyCerts[j]).getSubjectDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                if (subject.equals(issuer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                    tmpCert = replyCerts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                    replyCerts[i] = replyCerts[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                    replyCerts[j] = tmpCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                    issuer = ((X509Certificate)replyCerts[i]).getIssuerDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
            if (j == replyCerts.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                    (rb.getString("Incomplete certificate chain in reply"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        // now verify each cert in the ordered chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
        for (i=0; i<replyCerts.length-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            PublicKey pubKey = replyCerts[i+1].getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                replyCerts[i].verify(pubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                        ("Certificate chain in reply does not verify: ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                        e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
        if (noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
            return replyCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
        // do we trust the (root) cert at the top?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        Certificate topCert = replyCerts[replyCerts.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
        if (!isTrusted(topCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
            boolean verified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
            Certificate rootCert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
            if (trustcacerts && (caks!= null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                for (Enumeration<String> aliases = caks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                     aliases.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
                    String name = aliases.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
                    rootCert = caks.getCertificate(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
                    if (rootCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
                            topCert.verify(rootCert.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
                            verified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
            if (!verified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                System.err.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                        (rb.getString("Top-level certificate in reply:\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                printX509Cert((X509Certificate)topCert, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
                System.err.print(rb.getString("... is not trusted. "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                String reply = getYesNoReply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                        (rb.getString("Install reply anyway? [no]:  "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                if ("NO".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                if (!isSelfSigned((X509Certificate)topCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                    // append the (self-signed) root CA cert to the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                    Certificate[] tmpCerts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                        new Certificate[replyCerts.length+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                    System.arraycopy(replyCerts, 0, tmpCerts, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
                                     replyCerts.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
                    tmpCerts[tmpCerts.length-1] = rootCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                    replyCerts = tmpCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
        return replyCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
     * Establishes a certificate chain (using trusted certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
     * keystore), starting with the user certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
     * and ending at a self-signed certificate found in the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
     * @param userCert the user certificate of the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
     * @param certToVerify the single certificate provided in the reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
    private Certificate[] establishCertChain(Certificate userCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
                                             Certificate certToVerify)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
        if (userCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
            // Make sure that the public key of the certificate reply matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
            // the original public key in the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
            PublicKey origPubKey = userCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
            PublicKey replyPubKey = certToVerify.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
            if (!origPubKey.equals(replyPubKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
                throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                        ("Public keys in reply and keystore don't match"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            // If the two certs are identical, we're done: no need to import
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
            // anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
            if (certToVerify.equals(userCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
                throw new Exception(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                        ("Certificate reply and certificate in keystore are identical"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
        // Build a hash table of all certificates in the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
        // Use the subject distinguished name as the key into the hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
        // All certificates associated with the same subject distinguished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
        // name are stored in the same hash table entry as a vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
        Hashtable<Principal, Vector<Certificate>> certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
        if (keyStore.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
            certs = new Hashtable<Principal, Vector<Certificate>>(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
            keystorecerts2Hashtable(keyStore, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        if (trustcacerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
            if (caks!=null && caks.size()>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
                if (certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
                    certs = new Hashtable<Principal, Vector<Certificate>>(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
                keystorecerts2Hashtable(caks, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
        // start building chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
        Vector<Certificate> chain = new Vector<Certificate>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
        if (buildChain((X509Certificate)certToVerify, chain, certs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
            Certificate[] newChain = new Certificate[chain.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
            // buildChain() returns chain with self-signed root-cert first and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
            // user-cert last, so we need to invert the chain before we store
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            // it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
            int j=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
            for (int i=chain.size()-1; i>=0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
                newChain[j] = chain.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
            return newChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                (rb.getString("Failed to establish chain from reply"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
     * Recursively tries to establish chain from pool of trusted certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
     * @param certToVerify the cert that needs to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
     * @param chain the chain that's being built.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
     * @param certs the pool of trusted certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
     * @return true if successful, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
    private boolean buildChain(X509Certificate certToVerify,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
                        Vector<Certificate> chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
                        Hashtable<Principal, Vector<Certificate>> certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
        Principal subject = certToVerify.getSubjectDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
        Principal issuer = certToVerify.getIssuerDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
        if (subject.equals(issuer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            // reached self-signed root cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
            // no verification needed because it's trusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            chain.addElement(certToVerify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
        // Get the issuer's certificate(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
        Vector<Certificate> vec = certs.get(issuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        if (vec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
        // Try out each certificate in the vector, until we find one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
        // whose public key verifies the signature of the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
        // in question.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
        for (Enumeration<Certificate> issuerCerts = vec.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
             issuerCerts.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
            X509Certificate issuerCert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                = (X509Certificate)issuerCerts.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
            PublicKey issuerPubKey = issuerCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
                certToVerify.verify(issuerPubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
            if (buildChain(issuerCert, chain, certs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
                chain.addElement(certToVerify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
     * Prompts user for yes/no decision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     * @return the user's decision, can only be "YES" or "NO"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
    private String getYesNoReply(String prompt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        String reply = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        int maxRetry = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
            if (maxRetry-- < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                throw new RuntimeException(rb.getString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                        "Too may retries, program terminated"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
            System.err.print(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
            System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
            reply = (new BufferedReader(new InputStreamReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
                                        (System.in))).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
            if (collator.compare(reply, "") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                collator.compare(reply, rb.getString("n")) == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                collator.compare(reply, rb.getString("no")) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                reply = "NO";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            } else if (collator.compare(reply, rb.getString("y")) == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                       collator.compare(reply, rb.getString("yes")) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                reply = "YES";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                System.err.println(rb.getString("Wrong answer, try again"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                reply = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
        } while (reply == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
        return reply;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
     * Returns the keystore with the configured CA certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
    private KeyStore getCacertsKeyStore()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
        String sep = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        File file = new File(System.getProperty("java.home") + sep
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                             + "lib" + sep + "security" + sep
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
                             + "cacerts");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
        if (!file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
        FileInputStream fis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
        KeyStore caks = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
            fis = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
            caks = KeyStore.getInstance(JKS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
            caks.load(fis, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
            if (fis != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
                fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
        return caks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
     * Stores the (leaf) certificates of a keystore in a hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
     * All certs belonging to the same CA are stored in a vector that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
     * in turn is stored in the hashtable, keyed by the CA's subject DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
    private void keystorecerts2Hashtable(KeyStore ks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
                Hashtable<Principal, Vector<Certificate>> hash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        for (Enumeration<String> aliases = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
                                        aliases.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
            String alias = aliases.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
            Certificate cert = ks.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
            if (cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                Principal subjectDN = ((X509Certificate)cert).getSubjectDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                Vector<Certificate> vec = hash.get(subjectDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
                if (vec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
                    vec = new Vector<Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                    vec.addElement(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
                    if (!vec.contains(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                        vec.addElement(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
                hash.put(subjectDN, vec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
     * Returns the issue time that's specified the -startdate option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
     * @param s the value of -startdate option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
    private static Date getStartDate(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
        Calendar c = new GregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
            IOException ioe = new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                    rb.getString("Illegal startdate value"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
            if (s.charAt(0) == '-' || s.charAt(0) == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
                // Form 1: ([+-]nnn[ymdHMS])+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
                int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                while (start < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                    int sign = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
                    switch (s.charAt(start)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                        case '+': sign = 1; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                        case '-': sign = -1; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                        default: throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                    int i = start+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                    for (; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                        char ch = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                        if (ch < '0' || ch > '9') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
                    if (i == start+1) throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                    int number = Integer.parseInt(s.substring(start+1, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                    if (i >= len) throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                    int unit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
                    switch (s.charAt(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
                        case 'y': unit = Calendar.YEAR; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
                        case 'm': unit = Calendar.MONTH; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
                        case 'd': unit = Calendar.DATE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
                        case 'H': unit = Calendar.HOUR; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
                        case 'M': unit = Calendar.MINUTE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                        case 'S': unit = Calendar.SECOND; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                        default: throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                    c.add(unit, sign * number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                    start = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
            } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                // Form 2: [yyyy/mm/dd] [HH:MM:SS]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
                String date = null, time = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                if (len == 19) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                    date = s.substring(0, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
                    time = s.substring(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                    if (s.charAt(10) != ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                } else if (len == 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                    date = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                } else if (len == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                    time = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                    throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                if (date != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                    if (date.matches("\\d\\d\\d\\d\\/\\d\\d\\/\\d\\d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                        c.set(Integer.valueOf(date.substring(0, 4)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                                Integer.valueOf(date.substring(5, 7))-1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                                Integer.valueOf(date.substring(8, 10)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                if (time != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
                    if (time.matches("\\d\\d:\\d\\d:\\d\\d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
                        c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(time.substring(0, 2)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                        c.set(Calendar.MINUTE, Integer.valueOf(time.substring(0, 2)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                        c.set(Calendar.SECOND, Integer.valueOf(time.substring(0, 2)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
                        c.set(Calendar.MILLISECOND, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
        return c.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
     * Prints the usage of this tool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
    private void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
        System.err.println(rb.getString("keytool usage:\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
                ("-certreq     [-v] [-protected]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
                ("\t     [-alias <alias>] [-sigalg <sigalg>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
                ("\t     [-file <csr_file>] [-keypass <keypass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                ("\t     [-keystore <keystore>] [-storepass <storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                ("\t     [-storetype <storetype>] [-providername <name>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                ("-changealias [-v] [-protected] -alias <alias> -destalias <destalias>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                ("\t     [-keypass <keypass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                ("\t     [-keystore <keystore>] [-storepass <storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                ("\t     [-storetype <storetype>] [-providername <name>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
                ("-delete      [-v] [-protected] -alias <alias>"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                ("\t     [-keystore <keystore>] [-storepass <storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                ("\t     [-storetype <storetype>] [-providername <name>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                ("-exportcert  [-v] [-rfc] [-protected]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
                ("\t     [-alias <alias>] [-file <cert_file>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
                ("\t     [-keystore <keystore>] [-storepass <storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                ("\t     [-storetype <storetype>] [-providername <name>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                ("-genkeypair  [-v] [-protected]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
                ("\t     [-alias <alias>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                ("\t     [-keyalg <keyalg>] [-keysize <keysize>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
                ("\t     [-sigalg <sigalg>] [-dname <dname>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
                ("\t     [-startdate <startdate>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
                ("\t     [-validity <valDays>] [-keypass <keypass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
                ("\t     [-keystore <keystore>] [-storepass <storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
                ("\t     [-storetype <storetype>] [-providername <name>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
                ("-genseckey   [-v] [-protected]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
                ("\t     [-alias <alias>] [-keypass <keypass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
                ("\t     [-keyalg <keyalg>] [-keysize <keysize>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
                ("\t     [-keystore <keystore>] [-storepass <storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
                ("\t     [-storetype <storetype>] [-providername <name>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
        System.err.println(rb.getString("-help"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
                ("-importcert  [-v] [-noprompt] [-trustcacerts] [-protected]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
                ("\t     [-alias <alias>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
                ("\t     [-file <cert_file>] [-keypass <keypass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
                ("\t     [-keystore <keystore>] [-storepass <storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
                ("\t     [-storetype <storetype>] [-providername <name>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
                ("-importkeystore [-v] "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
                ("\t     [-srckeystore <srckeystore>] [-destkeystore <destkeystore>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
                ("\t     [-srcstoretype <srcstoretype>] [-deststoretype <deststoretype>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
                ("\t     [-srcstorepass <srcstorepass>] [-deststorepass <deststorepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
                ("\t     [-srcprotected] [-destprotected]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
                ("\t     [-srcprovidername <srcprovidername>]\n\t     [-destprovidername <destprovidername>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
                ("\t     [-srcalias <srcalias> [-destalias <destalias>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
                ("\t       [-srckeypass <srckeypass>] [-destkeypass <destkeypass>]]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                ("\t     [-noprompt]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
                ("-keypasswd   [-v] [-alias <alias>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
                ("\t     [-keypass <old_keypass>] [-new <new_keypass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
                ("\t     [-keystore <keystore>] [-storepass <storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
                ("\t     [-storetype <storetype>] [-providername <name>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
                ("-list        [-v | -rfc] [-protected]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
                ("\t     [-alias <alias>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
                ("\t     [-keystore <keystore>] [-storepass <storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
                ("\t     [-storetype <storetype>] [-providername <name>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
                ("-printcert   [-v] [-file <cert_file>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
                ("-storepasswd [-v] [-new <new_storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
                ("\t     [-keystore <keystore>] [-storepass <storepass>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
                ("\t     [-storetype <storetype>] [-providername <name>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
                ("\t     [-providerclass <provider_class_name> [-providerarg <arg>]] ..."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
        System.err.println(rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
                ("\t     [-providerpath <pathlist>]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
            throw new RuntimeException("NO ERROR, SORRY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
    private void tinyHelp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
        System.err.println(rb.getString("Try keytool -help"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
        // do not drown user with the help lines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
            throw new RuntimeException("NO BIG ERROR, SORRY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
    private void errorNeedArgument(String flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
        Object[] source = {flag};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
        System.err.println(new MessageFormat(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
                rb.getString("Command option <flag> needs an argument.")).format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
        tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
// This class is exactly the same as com.sun.tools.javac.util.Pair,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
// it's copied here since the original one is not included in JRE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
class Pair<A, B> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
    public final A fst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
    public final B snd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
    public Pair(A fst, B snd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
        this.fst = fst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
        this.snd = snd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
        return "Pair[" + fst + "," + snd + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
    private static boolean equals(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
        return (x == null && y == null) || (x != null && x.equals(y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
            other instanceof Pair &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
            equals(fst, ((Pair)other).fst) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
            equals(snd, ((Pair)other).snd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
        if (fst == null) return (snd == null) ? 0 : snd.hashCode() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
        else if (snd == null) return fst.hashCode() + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
        else return fst.hashCode() * 17 + snd.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
}