jdk/src/share/classes/sun/security/tools/keytool/Main.java
author sflores
Sun, 14 Oct 2012 22:58:59 +0100
changeset 14182 3041082abb40
parent 11912 jdk/src/share/classes/sun/security/tools/KeyTool.java@49b066b32693
child 14421 a64b2cc9d429
permissions -rw-r--r--
7194449: String resources for Key Tool and Policy Tool should be in their respective packages Reviewed-by: alanb, weijun, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
     2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
    26
package sun.security.tools.keytool;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
    29
import java.security.CodeSigner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Signature;
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
    38
import java.security.Timestamp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.UnrecoverableEntryException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.UnrecoverableKeyException;
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
    41
import java.security.NoSuchAlgorithmException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.cert.CertificateFactory;
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
    46
import java.security.cert.CertStoreException;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    47
import java.security.cert.CRL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.text.Collator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.*;
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
    53
import java.util.jar.JarEntry;
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
    54
import java.util.jar.JarFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.lang.reflect.Constructor;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    56
import java.math.BigInteger;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    57
import java.net.URI;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.net.URLClassLoader;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    60
import java.security.cert.CertStore;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    61
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    62
import java.security.cert.X509CRL;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    63
import java.security.cert.X509CRLEntry;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    64
import java.security.cert.X509CRLSelector;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    65
import javax.security.auth.x500.X500Principal;
2
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;
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
    68
import sun.security.pkcs10.PKCS10;
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
    69
import sun.security.pkcs10.PKCS10Attribute;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import sun.security.provider.X509Factory;
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
    71
import sun.security.provider.certpath.CertStoreHelper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import sun.security.util.Password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import javax.crypto.KeyGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
    76
import sun.misc.BASE64Decoder;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
    77
import sun.security.pkcs.PKCS9Attribute;
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
    78
import sun.security.tools.KeyStoreUtil;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
    79
import sun.security.tools.PathList;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
    80
import sun.security.util.DerValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
import static java.security.KeyStore.*;
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
    84
import static sun.security.tools.keytool.Main.Command.*;
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
    85
import static sun.security.tools.keytool.Main.Option.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * This tool manages keystores.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @see java.security.KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @see sun.security.provider.KeyProtector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @see sun.security.provider.JavaKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 */
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
    99
public final class Main {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private boolean debug = false;
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   102
    private Command command = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private String sigAlgName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private String keyAlgName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private boolean verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private int keysize = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private boolean rfc = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private long validity = (long)90;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private String alias = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private String dname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private String dest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private String filename = null;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   113
    private String infilename = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   114
    private String outfilename = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private String srcksfname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // User-specified providers are added before any command is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // However, they are not removed before the end of the main() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // If you're calling KeyTool.main() directly in your own Java program,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // please programtically add any providers you need and do not specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // them through the command line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private Set<Pair <String, String>> providers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private String storetype = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private String srcProviderName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private String providerName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private String pathlist = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private char[] storePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private char[] storePassNew = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private char[] keyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private char[] keyPassNew = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private char[] newPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private char[] destKeyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private char[] srckeyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private String ksfname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private File ksfile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private InputStream ksStream = null; // keystore stream
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
   138
    private String sslserver = null;
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
   139
    private String jarfile = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private KeyStore keyStore = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private boolean token = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private boolean nullStream = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private boolean kssave = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private boolean noprompt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private boolean trustcacerts = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private boolean protectedPath = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private boolean srcprotectedPath = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private CertificateFactory cf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private KeyStore caks = null; // "cacerts" keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private char[] srcstorePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private String srcstoretype = null;
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
   152
    private Set<char[]> passwords = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private String startDate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
   155
    private List<String> ids = new ArrayList<>();   // used in GENCRL
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
   156
    private List<String> v3ext = new ArrayList<>();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   157
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   158
    enum Command {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   159
        CERTREQ("Generates.a.certificate.request",
4918
13ed75166139 6925639: keytool -genkeypair -help missing dname option
weijun
parents: 4819
diff changeset
   160
            ALIAS, SIGALG, FILEOUT, KEYPASS, KEYSTORE, DNAME,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   161
            STOREPASS, STORETYPE, PROVIDERNAME, PROVIDERCLASS,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   162
            PROVIDERARG, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   163
        CHANGEALIAS("Changes.an.entry.s.alias",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   164
            ALIAS, DESTALIAS, KEYPASS, KEYSTORE, STOREPASS,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   165
            STORETYPE, PROVIDERNAME, PROVIDERCLASS, PROVIDERARG,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   166
            PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   167
        DELETE("Deletes.an.entry",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   168
            ALIAS, KEYSTORE, STOREPASS, STORETYPE,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   169
            PROVIDERNAME, PROVIDERCLASS, PROVIDERARG,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   170
            PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   171
        EXPORTCERT("Exports.certificate",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   172
            RFC, ALIAS, FILEOUT, KEYSTORE, STOREPASS,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   173
            STORETYPE, PROVIDERNAME, PROVIDERCLASS, PROVIDERARG,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   174
            PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   175
        GENKEYPAIR("Generates.a.key.pair",
4918
13ed75166139 6925639: keytool -genkeypair -help missing dname option
weijun
parents: 4819
diff changeset
   176
            ALIAS, KEYALG, KEYSIZE, SIGALG, DESTALIAS, DNAME,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   177
            STARTDATE, EXT, VALIDITY, KEYPASS, KEYSTORE,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   178
            STOREPASS, STORETYPE, PROVIDERNAME, PROVIDERCLASS,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   179
            PROVIDERARG, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   180
        GENSECKEY("Generates.a.secret.key",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   181
            ALIAS, KEYPASS, KEYALG, KEYSIZE, KEYSTORE,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   182
            STOREPASS, STORETYPE, PROVIDERNAME, PROVIDERCLASS,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   183
            PROVIDERARG, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   184
        GENCERT("Generates.certificate.from.a.certificate.request",
4918
13ed75166139 6925639: keytool -genkeypair -help missing dname option
weijun
parents: 4819
diff changeset
   185
            RFC, INFILE, OUTFILE, ALIAS, SIGALG, DNAME,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   186
            STARTDATE, EXT, VALIDITY, KEYPASS, KEYSTORE,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   187
            STOREPASS, STORETYPE, PROVIDERNAME, PROVIDERCLASS,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   188
            PROVIDERARG, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   189
        IMPORTCERT("Imports.a.certificate.or.a.certificate.chain",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   190
            NOPROMPT, TRUSTCACERTS, PROTECTED, ALIAS, FILEIN,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   191
            KEYPASS, KEYSTORE, STOREPASS, STORETYPE,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   192
            PROVIDERNAME, PROVIDERCLASS, PROVIDERARG,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   193
            PROVIDERPATH, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   194
        IMPORTKEYSTORE("Imports.one.or.all.entries.from.another.keystore",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   195
            SRCKEYSTORE, DESTKEYSTORE, SRCSTORETYPE,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   196
            DESTSTORETYPE, SRCSTOREPASS, DESTSTOREPASS,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   197
            SRCPROTECTED, SRCPROVIDERNAME, DESTPROVIDERNAME,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   198
            SRCALIAS, DESTALIAS, SRCKEYPASS, DESTKEYPASS,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   199
            NOPROMPT, PROVIDERCLASS, PROVIDERARG, PROVIDERPATH,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   200
            V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   201
        KEYPASSWD("Changes.the.key.password.of.an.entry",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   202
            ALIAS, KEYPASS, NEW, KEYSTORE, STOREPASS,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   203
            STORETYPE, PROVIDERNAME, PROVIDERCLASS, PROVIDERARG,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   204
            PROVIDERPATH, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   205
        LIST("Lists.entries.in.a.keystore",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   206
            RFC, ALIAS, KEYSTORE, STOREPASS, STORETYPE,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   207
            PROVIDERNAME, PROVIDERCLASS, PROVIDERARG,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   208
            PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   209
        PRINTCERT("Prints.the.content.of.a.certificate",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   210
            RFC, FILEIN, SSLSERVER, JARFILE, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   211
        PRINTCERTREQ("Prints.the.content.of.a.certificate.request",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   212
            FILEIN, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   213
        PRINTCRL("Prints.the.content.of.a.CRL.file",
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   214
            FILEIN, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   215
        STOREPASSWD("Changes.the.store.password.of.a.keystore",
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   216
            NEW, KEYSTORE, STOREPASS, STORETYPE, PROVIDERNAME,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   217
            PROVIDERCLASS, PROVIDERARG, PROVIDERPATH, V),
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   218
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   219
        // Undocumented start here, KEYCLONE is used a marker in -help;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   220
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   221
        KEYCLONE("Clones.a.key.entry",
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   222
            ALIAS, DESTALIAS, KEYPASS, NEW, STORETYPE,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   223
            KEYSTORE, STOREPASS, PROVIDERNAME, PROVIDERCLASS,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   224
            PROVIDERARG, PROVIDERPATH, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   225
        SELFCERT("Generates.a.self.signed.certificate",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   226
            ALIAS, SIGALG, DNAME, STARTDATE, VALIDITY, KEYPASS,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   227
            STORETYPE, KEYSTORE, STOREPASS, PROVIDERNAME,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   228
            PROVIDERCLASS, PROVIDERARG, PROVIDERPATH, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   229
        GENCRL("Generates.CRL",
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   230
            RFC, FILEOUT, ID,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   231
            ALIAS, SIGALG, EXT, KEYPASS, KEYSTORE,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   232
            STOREPASS, STORETYPE, PROVIDERNAME, PROVIDERCLASS,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   233
            PROVIDERARG, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   234
        IDENTITYDB("Imports.entries.from.a.JDK.1.1.x.style.identity.database",
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   235
            FILEIN, STORETYPE, KEYSTORE, STOREPASS, PROVIDERNAME,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   236
            PROVIDERCLASS, PROVIDERARG, PROVIDERPATH, V);
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   237
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   238
        final String description;
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   239
        final Option[] options;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   240
        Command(String d, Option... o) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   241
            description = d;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   242
            options = o;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   243
        }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   244
        @Override
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   245
        public String toString() {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   246
            return "-" + name().toLowerCase(Locale.ENGLISH);
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   247
        }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   248
    };
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   249
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   250
    enum Option {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   251
        ALIAS("alias", "<alias>", "alias.name.of.the.entry.to.process"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   252
        DESTALIAS("destalias", "<destalias>", "destination.alias"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   253
        DESTKEYPASS("destkeypass", "<arg>", "destination.key.password"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   254
        DESTKEYSTORE("destkeystore", "<destkeystore>", "destination.keystore.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   255
        DESTPROTECTED("destprotected", null, "destination.keystore.password.protected"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   256
        DESTPROVIDERNAME("destprovidername", "<destprovidername>", "destination.keystore.provider.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   257
        DESTSTOREPASS("deststorepass", "<arg>", "destination.keystore.password"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   258
        DESTSTORETYPE("deststoretype", "<deststoretype>", "destination.keystore.type"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   259
        DNAME("dname", "<dname>", "distinguished.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   260
        EXT("ext", "<value>", "X.509.extension"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   261
        FILEOUT("file", "<filename>", "output.file.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   262
        FILEIN("file", "<filename>", "input.file.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   263
        ID("id", "<id:reason>", "Serial.ID.of.cert.to.revoke"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   264
        INFILE("infile", "<filename>", "input.file.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   265
        KEYALG("keyalg", "<keyalg>", "key.algorithm.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   266
        KEYPASS("keypass", "<arg>", "key.password"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   267
        KEYSIZE("keysize", "<keysize>", "key.bit.size"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   268
        KEYSTORE("keystore", "<keystore>", "keystore.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   269
        NEW("new", "<arg>", "new.password"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   270
        NOPROMPT("noprompt", null, "do.not.prompt"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   271
        OUTFILE("outfile", "<filename>", "output.file.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   272
        PROTECTED("protected", null, "password.through.protected.mechanism"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   273
        PROVIDERARG("providerarg", "<arg>", "provider.argument"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   274
        PROVIDERCLASS("providerclass", "<providerclass>", "provider.class.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   275
        PROVIDERNAME("providername", "<providername>", "provider.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   276
        PROVIDERPATH("providerpath", "<pathlist>", "provider.classpath"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   277
        RFC("rfc", null, "output.in.RFC.style"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   278
        SIGALG("sigalg", "<sigalg>", "signature.algorithm.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   279
        SRCALIAS("srcalias", "<srcalias>", "source.alias"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   280
        SRCKEYPASS("srckeypass", "<arg>", "source.key.password"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   281
        SRCKEYSTORE("srckeystore", "<srckeystore>", "source.keystore.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   282
        SRCPROTECTED("srcprotected", null, "source.keystore.password.protected"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   283
        SRCPROVIDERNAME("srcprovidername", "<srcprovidername>", "source.keystore.provider.name"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   284
        SRCSTOREPASS("srcstorepass", "<arg>", "source.keystore.password"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   285
        SRCSTORETYPE("srcstoretype", "<srcstoretype>", "source.keystore.type"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   286
        SSLSERVER("sslserver", "<server[:port]>", "SSL.server.host.and.port"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   287
        JARFILE("jarfile", "<filename>", "signed.jar.file"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   288
        STARTDATE("startdate", "<startdate>", "certificate.validity.start.date.time"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   289
        STOREPASS("storepass", "<arg>", "keystore.password"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   290
        STORETYPE("storetype", "<storetype>", "keystore.type"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   291
        TRUSTCACERTS("trustcacerts", null, "trust.certificates.from.cacerts"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   292
        V("v", null, "verbose.output"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   293
        VALIDITY("validity", "<valDays>", "validity.number.of.days");
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   294
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   295
        final String name, arg, description;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   296
        Option(String name, String arg, String description) {
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   297
            this.name = name;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   298
            this.arg = arg;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   299
            this.description = description;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   300
        }
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   301
        @Override
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   302
        public String toString() {
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   303
            return "-" + name;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   304
        }
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   305
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private static final Class[] PARAM_STRING = { String.class };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    private static final String NONE = "NONE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private static final String P11KEYSTORE = "PKCS11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private static final String P12KEYSTORE = "PKCS12";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private final String keyAlias = "mykey";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    // for i18n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private static final java.util.ResourceBundle rb =
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
   316
        java.util.ResourceBundle.getBundle(
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
   317
            "sun.security.tools.keytool.Resources");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private static final Collator collator = Collator.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // this is for case insensitive string comparisons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        collator.setStrength(Collator.PRIMARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
   324
    private Main() { }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public static void main(String[] args) throws Exception {
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
   327
        Main kt = new Main();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        kt.run(args, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private void run(String[] args, PrintStream out) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            parseArgs(args);
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   334
            if (command != null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   335
                doCommands(out);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   336
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        } catch (Exception e) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   338
            System.out.println(rb.getString("keytool.error.") + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (!debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            for (char[] pass : passwords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (pass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    Arrays.fill(pass, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    pass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            if (ksStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                ksStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Parse command line arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    void parseArgs(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int i=0;
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   367
        boolean help = args.length == 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        for (i=0; (i < args.length) && args[i].startsWith("-"); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            String flags = args[i];
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   372
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   373
            // Check if the last option needs an arg
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   374
            if (i == args.length - 1) {
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   375
                for (Option option: Option.values()) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   376
                    // Only options with an arg need to be checked
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   377
                    if (collator.compare(flags, option.toString()) == 0) {
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   378
                        if (option.arg != null) errorNeedArgument(flags);
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   379
                        break;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   380
                    }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   381
                }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   382
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   383
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            /*
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   385
             * Check modifiers
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   386
             */
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   387
            String modifier = null;
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   388
            int pos = flags.indexOf(':');
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   389
            if (pos > 0) {
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   390
                modifier = flags.substring(pos+1);
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   391
                flags = flags.substring(0, pos);
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   392
            }
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   393
            /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
             * command modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
             */
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   396
            boolean isCommand = false;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   397
            for (Command c: Command.values()) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   398
                if (collator.compare(flags, c.toString()) == 0) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   399
                    command = c;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   400
                    isCommand = true;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   401
                    break;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   402
                }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   403
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   404
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   405
            if (isCommand) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   406
                // already recognized as a command
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   407
            } else if (collator.compare(flags, "-export") == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                command = EXPORTCERT;
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   409
            } else if (collator.compare(flags, "-genkey") == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                command = GENKEYPAIR;
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   411
            } else if (collator.compare(flags, "-import") == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                command = IMPORTCERT;
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   413
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   414
            /*
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   415
             * Help
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   416
             */
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   417
            else if (collator.compare(flags, "-help") == 0) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   418
                help = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
             * specifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            else if (collator.compare(flags, "-keystore") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    collator.compare(flags, "-destkeystore") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   426
                ksfname = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            } else if (collator.compare(flags, "-storepass") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    collator.compare(flags, "-deststorepass") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   429
                storePass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                passwords.add(storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            } else if (collator.compare(flags, "-storetype") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    collator.compare(flags, "-deststoretype") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   433
                storetype = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            } else if (collator.compare(flags, "-srcstorepass") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   435
                srcstorePass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                passwords.add(srcstorePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            } else if (collator.compare(flags, "-srcstoretype") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   438
                srcstoretype = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            } else if (collator.compare(flags, "-srckeypass") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   440
                srckeyPass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                passwords.add(srckeyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            } else if (collator.compare(flags, "-srcprovidername") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   443
                srcProviderName = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } else if (collator.compare(flags, "-providername") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    collator.compare(flags, "-destprovidername") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   446
                providerName = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            } else if (collator.compare(flags, "-providerpath") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   448
                pathlist = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            } else if (collator.compare(flags, "-keypass") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   450
                keyPass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                passwords.add(keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            } else if (collator.compare(flags, "-new") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   453
                newPass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                passwords.add(newPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            } else if (collator.compare(flags, "-destkeypass") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   456
                destKeyPass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                passwords.add(destKeyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            } else if (collator.compare(flags, "-alias") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    collator.compare(flags, "-srcalias") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   460
                alias = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            } else if (collator.compare(flags, "-dest") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    collator.compare(flags, "-destalias") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   463
                dest = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            } else if (collator.compare(flags, "-dname") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   465
                dname = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            } else if (collator.compare(flags, "-keysize") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   467
                keysize = Integer.parseInt(args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            } else if (collator.compare(flags, "-keyalg") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   469
                keyAlgName = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            } else if (collator.compare(flags, "-sigalg") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   471
                sigAlgName = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            } else if (collator.compare(flags, "-startdate") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   473
                startDate = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            } else if (collator.compare(flags, "-validity") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   475
                validity = Long.parseLong(args[++i]);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   476
            } else if (collator.compare(flags, "-ext") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   477
                v3ext.add(args[++i]);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   478
            } else if (collator.compare(flags, "-id") == 0) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   479
                ids.add(args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            } else if (collator.compare(flags, "-file") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   481
                filename = args[++i];
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   482
            } else if (collator.compare(flags, "-infile") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   483
                infilename = args[++i];
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   484
            } else if (collator.compare(flags, "-outfile") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   485
                outfilename = args[++i];
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
   486
            } else if (collator.compare(flags, "-sslserver") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   487
                sslserver = args[++i];
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
   488
            } else if (collator.compare(flags, "-jarfile") == 0) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
   489
                jarfile = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            } else if (collator.compare(flags, "-srckeystore") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   491
                srcksfname = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            } else if ((collator.compare(flags, "-provider") == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                        (collator.compare(flags, "-providerclass") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                if (providers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    providers = new HashSet<Pair <String, String>> (3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                }
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   497
                String providerClass = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                String providerArg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                if (args.length > (i+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    flags = args[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    if (collator.compare(flags, "-providerarg") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        if (args.length == (i+2)) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        providerArg = args[i+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                providers.add(
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   509
                        Pair.of(providerClass, providerArg));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
             * options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            else if (collator.compare(flags, "-v") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            } else if (collator.compare(flags, "-debug") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            } else if (collator.compare(flags, "-rfc") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                rfc = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            } else if (collator.compare(flags, "-noprompt") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                noprompt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            } else if (collator.compare(flags, "-trustcacerts") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                trustcacerts = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            } else if (collator.compare(flags, "-protected") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    collator.compare(flags, "-destprotected") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                protectedPath = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            } else if (collator.compare(flags, "-srcprotected") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                srcprotectedPath = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            } else  {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   531
                System.err.println(rb.getString("Illegal.option.") + flags);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (i<args.length) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   537
            System.err.println(rb.getString("Illegal.option.") + args[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   540
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   541
        if (command == null) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   542
            if (help) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   543
                usage();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   544
            } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   545
                System.err.println(rb.getString("Usage.error.no.command.provided"));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   546
                tinyHelp();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   547
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   548
        } else if (help) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   549
            usage();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   550
            command = null;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   551
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   554
    boolean isKeyStoreRelated(Command cmd) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   555
        return cmd != PRINTCERT && cmd != PRINTCERTREQ;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   556
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   557
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Execute the commands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    void doCommands(PrintStream out) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (storetype == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            storetype = KeyStore.getDefaultType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        storetype = KeyStoreUtil.niceStoreTypeName(storetype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (srcstoretype == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            srcstoretype = KeyStore.getDefaultType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        srcstoretype = KeyStoreUtil.niceStoreTypeName(srcstoretype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (P11KEYSTORE.equalsIgnoreCase(storetype) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            token = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            if (ksfname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                ksfname = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (NONE.equals(ksfname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            nullStream = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (token && !nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            System.err.println(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   586
                (".keystore.must.be.NONE.if.storetype.is.{0}"), storetype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (token &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            (command == KEYPASSWD || command == STOREPASSWD)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            throw new UnsupportedOperationException(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   594
                        (".storepasswd.and.keypasswd.commands.not.supported.if.storetype.is.{0}"), storetype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (P12KEYSTORE.equalsIgnoreCase(storetype) && command == KEYPASSWD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            throw new UnsupportedOperationException(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   599
                        (".keypasswd.commands.not.supported.if.storetype.is.PKCS12"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        if (token && (keyPass != null || newPass != null || destKeyPass != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            throw new IllegalArgumentException(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   604
                (".keypass.and.new.can.not.be.specified.if.storetype.is.{0}"), storetype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (protectedPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            if (storePass != null || keyPass != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    newPass != null || destKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                throw new IllegalArgumentException(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   611
                        ("if.protected.is.specified.then.storepass.keypass.and.new.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (srcprotectedPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (srcstorePass != null || srckeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                throw new IllegalArgumentException(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   618
                        ("if.srcprotected.is.specified.then.srcstorepass.and.srckeypass.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            if (storePass != null || keyPass != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    newPass != null || destKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                throw new IllegalArgumentException(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   626
                        ("if.keystore.is.not.password.protected.then.storepass.keypass.and.new.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            if (srcstorePass != null || srckeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                throw new IllegalArgumentException(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   633
                        ("if.source.keystore.is.not.password.protected.then.srcstorepass.and.srckeypass.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (validity <= (long)0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            throw new Exception
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   639
                (rb.getString("Validity.must.be.greater.than.zero"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        // Try to load and install specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (providers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            ClassLoader cl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            if (pathlist != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                String path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                path = PathList.appendPath(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        path, System.getProperty("java.class.path"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                path = PathList.appendPath(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        path, System.getProperty("env.class.path"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                path = PathList.appendPath(path, pathlist);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                URL[] urls = PathList.pathToURLs(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                cl = new URLClassLoader(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            for (Pair <String, String> provider: providers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                String provName = provider.fst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                Class<?> provClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    provClass = cl.loadClass(provName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    provClass = Class.forName(provName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                String provArg = provider.snd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                if (provArg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    obj = provClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    Constructor<?> c = provClass.getConstructor(PARAM_STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    obj = c.newInstance(provArg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                if (!(obj instanceof Provider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                    MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   678
                        (rb.getString("provName.not.a.provider"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    Object[] source = {provName};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                Security.addProvider((Provider)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (command == LIST && verbose && rfc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   688
                ("Must.not.specify.both.v.and.rfc.with.list.command"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        // Make sure provided passwords are at least 6 characters long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (command == GENKEYPAIR && keyPass!=null && keyPass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   695
                ("Key.password.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (newPass != null && newPass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   699
                ("New.password.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (destKeyPass != null && destKeyPass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   703
                ("New.password.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        // Check if keystore exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // If no keystore has been specified at the command line, try to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        // the default, which is located in $HOME/.keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // If the command is "genkey", "identitydb", "import", or "printcert",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        // it is OK not to have a keystore.
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   711
        if (isKeyStoreRelated(command)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (ksfname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                ksfname = System.getProperty("user.home") + File.separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    + ".keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            if (!nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    ksfile = new File(ksfname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    // Check if keystore file is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    if (ksfile.exists() && ksfile.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   723
                        ("Keystore.file.exists.but.is.empty.") + ksfname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    ksStream = new FileInputStream(ksfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    if (command != GENKEYPAIR &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                        command != GENSECKEY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        command != IDENTITYDB &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        command != IMPORTCERT &&
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   731
                        command != IMPORTKEYSTORE &&
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   732
                        command != PRINTCRL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   734
                                ("Keystore.file.does.not.exist.") + ksfname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if ((command == KEYCLONE || command == CHANGEALIAS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                && dest == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            dest = getAlias("destination");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            if ("".equals(dest)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   745
                        ("Must.specify.destination.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if (command == DELETE && alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            alias = getAlias(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            if ("".equals(alias)) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   752
                throw new Exception(rb.getString("Must.specify.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        // Create new keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if (providerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            keyStore = KeyStore.getInstance(storetype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            keyStore = KeyStore.getInstance(storetype, providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
         * Load the keystore data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
         * At this point, it's OK if no keystore password has been provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         * We want to make sure that we can load the keystore data, i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         * the keystore data has the right format. If we cannot load the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
         * keystore, why bother asking the user for his or her password?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
         * Only if we were able to load the keystore, and no keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
         * password has been provided, will we prompt the user for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
         * keystore password to verify the keystore integrity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
         * This means that the keystore is loaded twice: first load operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
         * checks the keystore format, second load operation verifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         * keystore integrity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
         * If the keystore password has already been provided (at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
         * command line), however, the keystore is loaded only once, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         * keystore format and integrity are checked "at the same time".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         * Null stream keystores are loaded later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (!nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            keyStore.load(ksStream, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            if (ksStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                ksStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        // All commands that create or modify the keystore require a keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        // password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        if (nullStream && storePass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            keyStore.load(null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        } else if (!nullStream && storePass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            // If we are creating a new non nullStream-based keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            // insist that the password be at least 6 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            if (ksStream == null && storePass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   800
                        ("Keystore.password.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        } else if (storePass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            // only prompt if (protectedPath == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            if (!protectedPath && !KeyStoreUtil.isWindowsKeyStore(storetype) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                (command == CERTREQ ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                        command == DELETE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                        command == GENKEYPAIR ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                        command == GENSECKEY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                        command == IMPORTCERT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                        command == IMPORTKEYSTORE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                        command == KEYCLONE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                        command == CHANGEALIAS ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                        command == SELFCERT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                        command == STOREPASSWD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        command == KEYPASSWD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        command == IDENTITYDB)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    if (command == IMPORTKEYSTORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                        System.err.print
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   823
                                (rb.getString("Enter.destination.keystore.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                        System.err.print
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   826
                                (rb.getString("Enter.keystore.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    storePass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                    passwords.add(storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                    // If we are creating a new non nullStream-based keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    // insist that the password be at least 6 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    if (!nullStream && (storePass == null || storePass.length < 6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   836
                                ("Keystore.password.is.too.short.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                        storePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    // If the keystore file does not exist and needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    // created, the storepass should be prompted twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    if (storePass != null && !nullStream && ksStream == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   843
                        System.err.print(rb.getString("Re.enter.new.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        char[] storePassAgain = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                        passwords.add(storePassAgain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        if (!Arrays.equals(storePass, storePassAgain)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                            System.err.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   848
                                (rb.getString("They.don.t.match.Try.again"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                            storePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                } while ((storePass == null) && count < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                if (storePass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                    System.err.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   859
                        (rb.getString("Too.many.failures.try.later"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            } else if (!protectedPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                    && !KeyStoreUtil.isWindowsKeyStore(storetype)
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   864
                    && isKeyStoreRelated(command)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                // here we have EXPORTCERT and LIST (info valid until STOREPASSWD)
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   866
                if (command != PRINTCRL) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   867
                    System.err.print(rb.getString("Enter.keystore.password."));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   868
                    System.err.flush();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   869
                    storePass = Password.readPassword(System.in);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   870
                    passwords.add(storePass);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   871
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            // Now load a nullStream-based keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            // or verify the integrity of an input stream-based keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            if (nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                keyStore.load(null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            } else if (ksStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                ksStream = new FileInputStream(ksfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                keyStore.load(ksStream, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                ksStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        if (storePass != null && P12KEYSTORE.equalsIgnoreCase(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            MessageFormat form = new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   887
                "Warning.Different.store.and.key.passwords.not.supported.for.PKCS12.KeyStores.Ignoring.user.specified.command.value."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            if (keyPass != null && !Arrays.equals(storePass, keyPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                Object[] source = {"-keypass"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                keyPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            if (newPass != null && !Arrays.equals(storePass, newPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                Object[] source = {"-new"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                newPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            if (destKeyPass != null && !Arrays.equals(storePass, destKeyPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                Object[] source = {"-destkeypass"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                destKeyPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        // Create a certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        if (command == PRINTCERT || command == IMPORTCERT
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   907
                || command == IDENTITYDB || command == PRINTCRL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        if (trustcacerts) {
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
   912
            caks = KeyStoreUtil.getCacertsKeyStore();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        // Perform the specified command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (command == CERTREQ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            if (filename != null) {
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   918
                try (PrintStream ps = new PrintStream(new FileOutputStream
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   919
                                                      (filename))) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   920
                    doCertReq(alias, sigAlgName, ps);
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   921
                }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   922
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                doCertReq(alias, sigAlgName, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            if (verbose && filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   927
                        ("Certification.request.stored.in.file.filename."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                Object[] source = {filename};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                System.err.println(form.format(source));
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   930
                System.err.println(rb.getString("Submit.this.to.your.CA"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        } else if (command == DELETE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            doDeleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        } else if (command == EXPORTCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            if (filename != null) {
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   937
                try (PrintStream ps = new PrintStream(new FileOutputStream
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   938
                                                   (filename))) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   939
                    doExportCert(alias, ps);
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   940
                }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   941
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                doExportCert(alias, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   946
                        ("Certificate.stored.in.file.filename."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                Object[] source = {filename};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        } else if (command == GENKEYPAIR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            if (keyAlgName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                keyAlgName = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            doGenKeyPair(alias, dname, keyAlgName, keysize, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        } else if (command == GENSECKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if (keyAlgName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                keyAlgName = "DES";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            doGenSecretKey(alias, keyAlgName, keysize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        } else if (command == IDENTITYDB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            if (filename != null) {
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   964
                try (InputStream inStream = new FileInputStream(filename)) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   965
                    doImportIdentityDatabase(inStream);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                }
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   967
            } else {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
   968
                doImportIdentityDatabase(System.in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        } else if (command == IMPORTCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            InputStream inStream = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                inStream = new FileInputStream(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            }
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   975
            String importAlias = (alias!=null)?alias:keyAlias;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            try {
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   977
                if (keyStore.entryInstanceOf(
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   978
                        importAlias, KeyStore.PrivateKeyEntry.class)) {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   979
                    kssave = installReply(importAlias, inStream);
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   980
                    if (kssave) {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   981
                        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   982
                            ("Certificate.reply.was.installed.in.keystore"));
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   983
                    } else {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   984
                        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   985
                            ("Certificate.reply.was.not.installed.in.keystore"));
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   986
                    }
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   987
                } else if (!keyStore.containsAlias(importAlias) ||
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   988
                        keyStore.entryInstanceOf(importAlias,
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   989
                            KeyStore.TrustedCertificateEntry.class)) {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   990
                    kssave = addTrustedCert(importAlias, inStream);
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   991
                    if (kssave) {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   992
                        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   993
                            ("Certificate.was.added.to.keystore"));
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   994
                    } else {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   995
                        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   996
                            ("Certificate.was.not.added.to.keystore"));
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
   997
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                if (inStream != System.in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    inStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        } else if (command == IMPORTKEYSTORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            doImportKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        } else if (command == KEYCLONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            keyPassNew = newPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            // added to make sure only key can go thru
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1016
                    (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            if (!keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                MessageFormat form = new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1022
                        "Alias.alias.references.an.entry.type.that.is.not.a.private.key.entry.The.keyclone.command.only.supports.cloning.of.private.key"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            doCloneEntry(alias, dest, true);  // Now everything can be cloned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        } else if (command == CHANGEALIAS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            doCloneEntry(alias, dest, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            // in PKCS11, clone a PrivateKeyEntry will delete the old one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                doDeleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        } else if (command == KEYPASSWD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            keyPassNew = newPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            doChangeKeyPasswd(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        } else if (command == LIST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            if (alias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                doPrintEntry(alias, out, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                doPrintEntries(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        } else if (command == PRINTCERT) {
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  1050
            doPrintCert(out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        } else if (command == SELFCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            doSelfCert(alias, dname, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        } else if (command == STOREPASSWD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            storePassNew = newPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            if (storePassNew == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                storePassNew = getNewPasswd("keystore password", storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            kssave = true;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1060
        } else if (command == GENCERT) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1061
            if (alias == null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1062
                alias = keyAlias;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1063
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1064
            InputStream inStream = System.in;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1065
            if (infilename != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1066
                inStream = new FileInputStream(infilename);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1067
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1068
            PrintStream ps = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1069
            if (outfilename != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1070
                ps = new PrintStream(new FileOutputStream(outfilename));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1071
                out = ps;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1072
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1073
            try {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1074
                doGenCert(alias, sigAlgName, inStream, out);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1075
            } finally {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1076
                if (inStream != System.in) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1077
                    inStream.close();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1078
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1079
                if (ps != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1080
                    ps.close();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1081
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1082
            }
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1083
        } else if (command == GENCRL) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1084
            if (alias == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1085
                alias = keyAlias;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1086
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1087
            if (filename != null) {
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1088
                try (PrintStream ps =
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1089
                         new PrintStream(new FileOutputStream(filename))) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1090
                    doGenCRL(ps);
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1091
                }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1092
            } else {
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1093
                doGenCRL(out);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1094
            }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1095
        } else if (command == PRINTCERTREQ) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1096
            if (filename != null) {
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1097
                try (InputStream inStream = new FileInputStream(filename)) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1098
                    doPrintCertReq(inStream, out);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1099
                }
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1100
            } else {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1101
                doPrintCertReq(System.in, out);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1102
            }
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1103
        } else if (command == PRINTCRL) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1104
            doPrintCRL(filename, out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        // If we need to save the keystore, do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        if (kssave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1111
                        (rb.getString(".Storing.ksfname."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                Object[] source = {nullStream ? "keystore" : ksfname};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            if (token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                keyStore.store(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            } else {
10436
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1119
                char[] pass = (storePassNew!=null) ? storePassNew : storePass;
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1120
                if (nullStream) {
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1121
                    keyStore.store(null, pass);
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1122
                } else {
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1123
                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1124
                    keyStore.store(bout, pass);
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1125
                    try (FileOutputStream fout = new FileOutputStream(ksfname)) {
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1126
                        fout.write(bout.toByteArray());
2
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    /**
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1134
     * Generate a certificate: Read PKCS10 request from in, and print
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1135
     * certificate to out. Use alias as CA, sigAlgName as the signature
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1136
     * type.
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1137
     */
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1138
    private void doGenCert(String alias, String sigAlgName, InputStream in, PrintStream out)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1139
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1140
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1141
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1142
        Certificate signerCert = keyStore.getCertificate(alias);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1143
        byte[] encoded = signerCert.getEncoded();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1144
        X509CertImpl signerCertImpl = new X509CertImpl(encoded);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1145
        X509CertInfo signerCertInfo = (X509CertInfo)signerCertImpl.get(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1146
                X509CertImpl.NAME + "." + X509CertImpl.INFO);
3316
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  1147
        X500Name issuer = (X500Name)signerCertInfo.get(X509CertInfo.SUBJECT + "." +
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1148
                                           CertificateSubjectName.DN_NAME);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1149
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1150
        Date firstDate = getStartDate(startDate);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1151
        Date lastDate = new Date();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1152
        lastDate.setTime(firstDate.getTime() + validity*1000L*24L*60L*60L);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1153
        CertificateValidity interval = new CertificateValidity(firstDate,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1154
                                                               lastDate);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1155
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1156
        PrivateKey privateKey =
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1157
                (PrivateKey)recoverKey(alias, storePass, keyPass).fst;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1158
        if (sigAlgName == null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1159
            sigAlgName = getCompatibleSigAlgName(privateKey.getAlgorithm());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1160
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1161
        Signature signature = Signature.getInstance(sigAlgName);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1162
        signature.initSign(privateKey);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1163
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1164
        X509CertInfo info = new X509CertInfo();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1165
        info.set(X509CertInfo.VALIDITY, interval);
2293
cb6d01cb3c3d 6820606: keytool can generate serialno more randomly
weijun
parents: 2289
diff changeset
  1166
        info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(
cb6d01cb3c3d 6820606: keytool can generate serialno more randomly
weijun
parents: 2289
diff changeset
  1167
                    new java.util.Random().nextInt() & 0x7fffffff));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1168
        info.set(X509CertInfo.VERSION,
4350
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 4169
diff changeset
  1169
                    new CertificateVersion(CertificateVersion.V3));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1170
        info.set(X509CertInfo.ALGORITHM_ID,
4350
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 4169
diff changeset
  1171
                    new CertificateAlgorithmId(
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  1172
                        AlgorithmId.get(sigAlgName)));
4350
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 4169
diff changeset
  1173
        info.set(X509CertInfo.ISSUER, new CertificateIssuerName(issuer));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1174
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1175
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1176
        boolean canRead = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1177
        StringBuffer sb = new StringBuffer();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1178
        while (true) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1179
            String s = reader.readLine();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1180
            if (s == null) break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1181
            // OpenSSL does not use NEW
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1182
            //if (s.startsWith("-----BEGIN NEW CERTIFICATE REQUEST-----")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1183
            if (s.startsWith("-----BEGIN") && s.indexOf("REQUEST") >= 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1184
                canRead = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1185
            //} else if (s.startsWith("-----END NEW CERTIFICATE REQUEST-----")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1186
            } else if (s.startsWith("-----END") && s.indexOf("REQUEST") >= 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1187
                break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1188
            } else if (canRead) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1189
                sb.append(s);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1190
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1191
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1192
        byte[] rawReq = new BASE64Decoder().decodeBuffer(new String(sb));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1193
        PKCS10 req = new PKCS10(rawReq);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1194
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1195
        info.set(X509CertInfo.KEY, new CertificateX509Key(req.getSubjectPublicKeyInfo()));
3316
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  1196
        info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  1197
                dname==null?req.getSubjectName():new X500Name(dname)));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1198
        CertificateExtensions reqex = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1199
        Iterator<PKCS10Attribute> attrs = req.getAttributes().getAttributes().iterator();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1200
        while (attrs.hasNext()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1201
            PKCS10Attribute attr = attrs.next();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  1202
            if (attr.getAttributeId().equals((Object)PKCS9Attribute.EXTENSION_REQUEST_OID)) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1203
                reqex = (CertificateExtensions)attr.getAttributeValue();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1204
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1205
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1206
        CertificateExtensions ext = createV3Extensions(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1207
                reqex,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1208
                null,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1209
                v3ext,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1210
                req.getSubjectPublicKeyInfo(),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1211
                signerCert.getPublicKey());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1212
        info.set(X509CertInfo.EXTENSIONS, ext);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1213
        X509CertImpl cert = new X509CertImpl(info);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1214
        cert.sign(privateKey, sigAlgName);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1215
        dumpCert(cert, out);
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1216
        for (Certificate ca: keyStore.getCertificateChain(alias)) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1217
            if (ca instanceof X509Certificate) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1218
                X509Certificate xca = (X509Certificate)ca;
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1219
                if (!isSelfSigned(xca)) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1220
                    dumpCert(xca, out);
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1221
                }
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1222
            }
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1223
        }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1224
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1225
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1226
    private void doGenCRL(PrintStream out)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1227
            throws Exception {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1228
        if (ids == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1229
            throw new Exception("Must provide -id when -gencrl");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1230
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1231
        Certificate signerCert = keyStore.getCertificate(alias);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1232
        byte[] encoded = signerCert.getEncoded();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1233
        X509CertImpl signerCertImpl = new X509CertImpl(encoded);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1234
        X509CertInfo signerCertInfo = (X509CertInfo)signerCertImpl.get(
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1235
                X509CertImpl.NAME + "." + X509CertImpl.INFO);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1236
        X500Name owner = (X500Name)signerCertInfo.get(X509CertInfo.SUBJECT + "." +
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1237
                                           CertificateSubjectName.DN_NAME);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1238
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1239
        Date firstDate = getStartDate(startDate);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1240
        Date lastDate = (Date) firstDate.clone();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  1241
        lastDate.setTime(lastDate.getTime() + validity*1000*24*60*60);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1242
        CertificateValidity interval = new CertificateValidity(firstDate,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1243
                                                               lastDate);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1244
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1245
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1246
        PrivateKey privateKey =
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1247
                (PrivateKey)recoverKey(alias, storePass, keyPass).fst;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1248
        if (sigAlgName == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1249
            sigAlgName = getCompatibleSigAlgName(privateKey.getAlgorithm());
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1250
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1251
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1252
        X509CRLEntry[] badCerts = new X509CRLEntry[ids.size()];
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1253
        for (int i=0; i<ids.size(); i++) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1254
            String id = ids.get(i);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1255
            int d = id.indexOf(':');
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1256
            if (d >= 0) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1257
                CRLExtensions ext = new CRLExtensions();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1258
                ext.set("Reason", new CRLReasonCodeExtension(Integer.parseInt(id.substring(d+1))));
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1259
                badCerts[i] = new X509CRLEntryImpl(new BigInteger(id.substring(0, d)),
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1260
                        firstDate, ext);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1261
            } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1262
                badCerts[i] = new X509CRLEntryImpl(new BigInteger(ids.get(i)), firstDate);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1263
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1264
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1265
        X509CRLImpl crl = new X509CRLImpl(owner, firstDate, lastDate, badCerts);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1266
        crl.sign(privateKey, sigAlgName);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1267
        if (rfc) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1268
            out.println("-----BEGIN X509 CRL-----");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1269
            new BASE64Encoder().encodeBuffer(crl.getEncodedInternal(), out);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1270
            out.println("-----END X509 CRL-----");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1271
        } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1272
            out.write(crl.getEncodedInternal());
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1273
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1274
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1275
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1276
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * Creates a PKCS#10 cert signing request, corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * keys (and name) associated with a given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    private void doCertReq(String alias, String sigAlgName, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1287
        Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1288
        PrivateKey privKey = (PrivateKey)objs.fst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        if (keyPass == null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1290
            keyPass = objs.snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        Certificate cert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        if (cert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1296
                (rb.getString("alias.has.no.public.key.certificate."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        PKCS10 request = new PKCS10(cert.getPublicKey());
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1301
        CertificateExtensions ext = createV3Extensions(null, null, v3ext, cert.getPublicKey(), null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1302
        // Attribute name is not significant
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1303
        request.getAttributes().setAttribute(X509CertInfo.EXTENSIONS,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1304
                new PKCS10Attribute(PKCS9Attribute.EXTENSION_REQUEST_OID, ext));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
4350
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 4169
diff changeset
  1306
        // Construct a Signature object, so that we can sign the request
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        if (sigAlgName == null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1308
            sigAlgName = getCompatibleSigAlgName(privKey.getAlgorithm());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        Signature signature = Signature.getInstance(sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        signature.initSign(privKey);
3316
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  1313
        X500Name subject = dname == null?
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  1314
                new X500Name(((X509Certificate)cert).getSubjectDN().toString()):
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  1315
                new X500Name(dname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        // Sign the request and base-64 encode it
4350
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 4169
diff changeset
  1318
        request.encodeAndSign(subject, signature);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        request.print(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * Deletes an entry from the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    private void doDeleteEntry(String alias) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1328
                (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        keyStore.deleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * Exports a certificate from the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    private void doExportCert(String alias, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        if (storePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                && !KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            printWarning();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1350
                (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        X509Certificate cert = (X509Certificate)keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        if (cert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1358
                (rb.getString("Alias.alias.has.no.certificate"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        dumpCert(cert, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * Prompt the user for a keypass when generating a key entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * @param alias the entry we will set password for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * @param orig the original entry of doing a dup, null if generate new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * @param origPass the password to copy from if user press ENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    private char[] promptForKeyPass(String alias, String orig, char[] origPass) throws Exception{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        if (P12KEYSTORE.equalsIgnoreCase(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            return origPass;
10436
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1374
        } else if (!token && !protectedPath) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            // Prompt for key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            for (count = 0; count < 3; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1379
                        ("Enter.key.password.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                if (orig == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                    System.err.print(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1384
                            (".RETURN.if.same.as.keystore.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1387
                            (".RETURN.if.same.as.for.otherAlias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                    Object[] src = {orig};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    System.err.print(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                char[] entered = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                passwords.add(entered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                if (entered == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                    return origPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                } else if (entered.length >= 6) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1397
                    System.err.print(rb.getString("Re.enter.new.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                    char[] passAgain = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                    passwords.add(passAgain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                    if (!Arrays.equals(entered, passAgain)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                        System.err.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1402
                            (rb.getString("They.don.t.match.Try.again"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    return entered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                    System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1408
                        ("Key.password.is.too.short.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            if (count == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                if (command == KEYCLONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                    throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1414
                        ("Too.many.failures.Key.entry.not.cloned"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                    throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1417
                            ("Too.many.failures.key.not.added.to.keystore"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        }
10436
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1421
        return null;    // PKCS11, MSCAPI, or -protected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * Creates a new secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    private void doGenSecretKey(String alias, String keyAlgName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                              int keysize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1435
                ("Secret.key.not.generated.alias.alias.already.exists"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        SecretKey secKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        KeyGenerator keygen = KeyGenerator.getInstance(keyAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        if (keysize != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            keygen.init(keysize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        } else if ("DES".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            keygen.init(56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        } else if ("DESede".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            keygen.init(168);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1450
                ("Please.provide.keysize.for.secret.key.generation"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        secKey = keygen.generateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            keyPass = promptForKeyPass(alias, null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        keyStore.setKeyEntry(alias, secKey, keyPass, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    /**
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1461
     * If no signature algorithm was specified at the command line,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1462
     * we choose one that is compatible with the selected private key
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1463
     */
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1464
    private static String getCompatibleSigAlgName(String keyAlgName)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1465
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1466
        if ("DSA".equalsIgnoreCase(keyAlgName)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1467
            return "SHA1WithDSA";
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1468
        } else if ("RSA".equalsIgnoreCase(keyAlgName)) {
3318
dade78e63c92 6561126: keytool should use larger default keysize for keypairs
weijun
parents: 3316
diff changeset
  1469
            return "SHA256WithRSA";
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1470
        } else if ("EC".equalsIgnoreCase(keyAlgName)) {
4152
bc36a9f01ac6 6870812: enhance security tools to use ECC algorithms
weijun
parents: 3951
diff changeset
  1471
            return "SHA256withECDSA";
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1472
        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1473
            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1474
                    ("Cannot.derive.signature.algorithm"));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1475
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1476
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1477
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * Creates a new key pair and self-signed certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    private void doGenKeyPair(String alias, String dname, String keyAlgName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                              int keysize, String sigAlgName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        if (keysize == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            if ("EC".equalsIgnoreCase(keyAlgName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                keysize = 256;
3318
dade78e63c92 6561126: keytool should use larger default keysize for keypairs
weijun
parents: 3316
diff changeset
  1487
            } else if ("RSA".equalsIgnoreCase(keyAlgName)) {
dade78e63c92 6561126: keytool should use larger default keysize for keypairs
weijun
parents: 3316
diff changeset
  1488
                keysize = 2048;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                keysize = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1500
                ("Key.pair.not.generated.alias.alias.already.exists"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        if (sigAlgName == null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1506
            sigAlgName = getCompatibleSigAlgName(keyAlgName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        CertAndKeyGen keypair =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                new CertAndKeyGen(keyAlgName, sigAlgName, providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        // If DN is provided, parse it. Otherwise, prompt the user for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        X500Name x500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        if (dname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            x500Name = getX500Name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            x500Name = new X500Name(dname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        keypair.generate(keysize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        PrivateKey privKey = keypair.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
11012
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1523
        CertificateExtensions ext = createV3Extensions(
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1524
                null,
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1525
                null,
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1526
                v3ext,
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1527
                keypair.getPublicKeyAnyway(),
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1528
                null);
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1529
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        X509Certificate[] chain = new X509Certificate[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        chain[0] = keypair.getSelfCertificate(
11012
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1532
                x500Name, getStartDate(startDate), validity*24L*60L*60L, ext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1536
                ("Generating.keysize.bit.keyAlgName.key.pair.and.self.signed.certificate.sigAlgName.with.a.validity.of.validality.days.for"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            Object[] source = {new Integer(keysize),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                                privKey.getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                                chain[0].getSigAlgName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                                new Long(validity),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                                x500Name};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            keyPass = promptForKeyPass(alias, null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        keyStore.setKeyEntry(alias, privKey, keyPass, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * Clones an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * @param orig original alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * @param dest destination alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * @changePassword if the password can be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    private void doCloneEntry(String orig, String dest, boolean changePassword)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        if (orig == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            orig = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        if (keyStore.containsAlias(dest)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1566
                (rb.getString("Destination.alias.dest.already.exists"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            Object[] source = {dest};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1571
        Pair<Entry,char[]> objs = recoverEntry(keyStore, orig, storePass, keyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1572
        Entry entry = objs.fst;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1573
        keyPass = objs.snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        PasswordProtection pp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        if (keyPass != null) {  // protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            if (!changePassword || P12KEYSTORE.equalsIgnoreCase(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                keyPassNew = keyPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                if (keyPassNew == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                    keyPassNew = promptForKeyPass(dest, orig, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            pp = new PasswordProtection(keyPassNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        keyStore.setEntry(dest, entry, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * Changes a key password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    private void doChangeKeyPasswd(String alias) throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1599
        Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1600
        Key privKey = objs.fst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        if (keyPass == null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1602
            keyPass = objs.snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        if (keyPassNew == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1607
                (rb.getString("key.password.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            keyPassNew = getNewPasswd(form.format(source), keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        keyStore.setKeyEntry(alias, privKey, keyPassNew,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                             keyStore.getCertificateChain(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * Imports a JDK 1.1-style identity database. We can only store one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * certificate per identity, because we use the identity's name as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * alias (which references a keystore entry), and aliases must be unique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    private void doImportIdentityDatabase(InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    {
4350
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 4169
diff changeset
  1623
        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1624
            ("No.entries.from.identity.database.added"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * Prints a single keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
    private void doPrintEntry(String alias, PrintStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                              boolean printWarning)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        if (storePass == null && printWarning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                && !KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            printWarning();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1641
                (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1648
                (rb.getString("Alias.name.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            if (!token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1654
                    ("Creation.date.keyStore.getCreationDate.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                Object[] src = {keyStore.getCreationDate(alias)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                out.println(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            if (!token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1661
                    (rb.getString("alias.keyStore.getCreationDate.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                Object[] source = {alias, keyStore.getCreationDate(alias)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                out.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1666
                    (rb.getString("alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                out.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        if (keyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                Object[] source = {"SecretKeyEntry"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                out.println(new MessageFormat(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1676
                        rb.getString("Entry.type.type.")).format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                out.println("SecretKeyEntry, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        } else if (keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                Object[] source = {"PrivateKeyEntry"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                out.println(new MessageFormat(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1684
                        rb.getString("Entry.type.type.")).format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                out.println("PrivateKeyEntry, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
            // Get the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            Certificate[] chain = keyStore.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            if (chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                    out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1694
                        ("Certificate.chain.length.") + chain.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                    for (int i = 0; i < chain.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                        MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1697
                                (rb.getString("Certificate.i.1."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                        Object[] source = {new Integer((i + 1))};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                        out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                        if (verbose && (chain[i] instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                            printX509Cert((X509Certificate)(chain[i]), out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                        } else if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                            out.println(chain[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                            dumpCert(chain[i], out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                    // Print the digest of the user cert only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                    out.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1711
                        (rb.getString("Certificate.fingerprint.SHA1.") +
909
c7bb1699d1b0 6709758: keytool default cert fingerprint algorithm should be SHA1, not MD5
weijun
parents: 904
diff changeset
  1712
                        getCertFingerPrint("SHA1", chain[0]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        } else if (keyStore.entryInstanceOf(alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                KeyStore.TrustedCertificateEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            // We have a trusted certificate entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            Certificate cert = keyStore.getCertificate(alias);
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  1719
            Object[] source = {"trustedCertEntry"};
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  1720
            String mf = new MessageFormat(
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  1721
                    rb.getString("Entry.type.type.")).format(source) + "\n";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            if (verbose && (cert instanceof X509Certificate)) {
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  1723
                out.println(mf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                printX509Cert((X509Certificate)cert, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            } else if (rfc) {
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  1726
                out.println(mf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                dumpCert(cert, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            } else if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                out.println(cert.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
            } else {
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  1731
                out.println("trustedCertEntry, ");
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1732
                out.println(rb.getString("Certificate.fingerprint.SHA1.")
909
c7bb1699d1b0 6709758: keytool default cert fingerprint algorithm should be SHA1, not MD5
weijun
parents: 904
diff changeset
  1733
                            + getCertFingerPrint("SHA1", cert));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1736
            out.println(rb.getString("Unknown.Entry.Type"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * Load the srckeystore from a stream, used in -importkeystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * @returns the src KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    KeyStore loadSourceKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        boolean isPkcs11 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        if (P11KEYSTORE.equalsIgnoreCase(srcstoretype) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            if (!NONE.equals(srcksfname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                System.err.println(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1753
                    (".keystore.must.be.NONE.if.storetype.is.{0}"), srcstoretype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            isPkcs11 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            if (srcksfname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                File srcksfile = new File(srcksfname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                    if (srcksfile.exists() && srcksfile.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                        throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1763
                                ("Source.keystore.file.exists.but.is.empty.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                                srcksfname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                is = new FileInputStream(srcksfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1769
                        ("Please.specify.srckeystore"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        KeyStore store;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            if (srcProviderName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                store = KeyStore.getInstance(srcstoretype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                store = KeyStore.getInstance(srcstoretype, srcProviderName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
            if (srcstorePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                    && !srcprotectedPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                    && !KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1784
                System.err.print(rb.getString("Enter.source.keystore.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                srcstorePass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                passwords.add(srcstorePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            // always let keypass be storepass when using pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            if (P12KEYSTORE.equalsIgnoreCase(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                if (srckeyPass != null && srcstorePass != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                        !Arrays.equals(srcstorePass, srckeyPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                    MessageFormat form = new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1795
                        "Warning.Different.store.and.key.passwords.not.supported.for.PKCS12.KeyStores.Ignoring.user.specified.command.value."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                    Object[] source = {"-srckeypass"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                    System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                    srckeyPass = srcstorePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            store.load(is, srcstorePass);   // "is" already null in PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        if (srcstorePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                && !KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            // anti refactoring, copied from printWarning(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
            // but change 2 lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1815
                (".WARNING.WARNING.WARNING."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1817
                (".The.integrity.of.the.information.stored.in.the.srckeystore."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1819
                (".WARNING.WARNING.WARNING."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        return store;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     * import all keys and certs from importkeystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * keep alias unchanged if no name conflict, otherwise, prompt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * keep keypass unchanged for keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
    private void doImportKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        if (alias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            doImportKeyStoreSingle(loadSourceKeyStore(), alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            if (dest != null || srckeyPass != null || destKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1838
                        "if.alias.not.specified.destalias.srckeypass.and.destkeypass.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            doImportKeyStoreAll(loadSourceKeyStore());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
         * Information display rule of -importkeystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
         * 1. inside single, shows failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
         * 2. inside all, shows sucess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
         * 3. inside all where there is a failure, prompt for continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
         * 4. at the final of all, shows summary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * Import a single entry named alias from srckeystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * @returns 1 if the import action succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     *          0 if user choose to ignore an alias-dumplicated entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     *          2 if setEntry throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
    private int doImportKeyStoreSingle(KeyStore srckeystore, String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        String newAlias = (dest==null) ? alias : dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        if (keyStore.containsAlias(newAlias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            if (noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                System.err.println(new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1866
                        "Warning.Overwriting.existing.alias.alias.in.destination.keystore")).format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                String reply = getYesNoReply(new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1869
                        "Existing.entry.alias.alias.exists.overwrite.no.")).format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                if ("NO".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                    newAlias = inputStringFromStdin(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1872
                            ("Enter.new.alias.name.RETURN.to.cancel.import.for.this.entry."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                    if ("".equals(newAlias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                        System.err.println(new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1875
                                "Entry.for.alias.alias.not.imported.")).format(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                                source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1883
        Pair<Entry,char[]> objs = recoverEntry(srckeystore, alias, srcstorePass, srckeyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1884
        Entry entry = objs.fst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        PasswordProtection pp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        // According to keytool.html, "The destination entry will be protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        // using destkeypass. If destkeypass is not provided, the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        // entry will be protected with the source entry password."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        // so always try to protect with destKeyPass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        if (destKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            pp = new PasswordProtection(destKeyPass);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1894
        } else if (objs.snd != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1895
            pp = new PasswordProtection(objs.snd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            keyStore.setEntry(newAlias, entry, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            Object[] source2 = {alias, kse.toString()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            MessageFormat form = new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1904
                    "Problem.importing.entry.for.alias.alias.exception.Entry.for.alias.alias.not.imported."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            System.err.println(form.format(source2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
    private void doImportKeyStoreAll(KeyStore srckeystore) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        int ok = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        int count = srckeystore.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        for (Enumeration<String> e = srckeystore.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                                        e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            int result = doImportKeyStoreSingle(srckeystore, alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            if (result == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                ok++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                Object[] source = {alias};
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1921
                MessageFormat form = new MessageFormat(rb.getString("Entry.for.alias.alias.successfully.imported."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            } else if (result == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                if (!noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                    String reply = getYesNoReply("Do you want to quit the import process? [no]:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                    if ("YES".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        Object[] source = {ok, count-ok};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        MessageFormat form = new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1934
                "Import.command.completed.ok.entries.successfully.imported.fail.entries.failed.or.cancelled"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * Prints all keystore entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    private void doPrintEntries(PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        if (storePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                && !KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            printWarning();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1951
        out.println(rb.getString("Keystore.type.") + keyStore.getType());
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1952
        out.println(rb.getString("Keystore.provider.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                keyStore.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        MessageFormat form;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        form = (keyStore.size() == 1) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1959
                        ("Your.keystore.contains.keyStore.size.entry")) :
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1961
                        ("Your.keystore.contains.keyStore.size.entries"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        Object[] source = {new Integer(keyStore.size())};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        for (Enumeration<String> e = keyStore.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                                        e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            doPrintEntry(alias, out, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            if (verbose || rfc) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1971
                out.println(rb.getString("NEWLINE"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1973
                        ("STAR"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1975
                        ("STARNN"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1980
    private static <T> Iterable<T> e2i(final Enumeration<T> e) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1981
        return new Iterable<T>() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1982
            @Override
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1983
            public Iterator<T> iterator() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1984
                return new Iterator<T>() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1985
                    @Override
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1986
                    public boolean hasNext() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1987
                        return e.hasMoreElements();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1988
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1989
                    @Override
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1990
                    public T next() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1991
                        return e.nextElement();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1992
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1993
                    public void remove() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1994
                        throw new UnsupportedOperationException("Not supported yet.");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1995
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1996
                };
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1997
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1998
        };
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1999
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2000
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2001
    /**
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2002
     * Loads CRLs from a source. This method is also called in JarSigner.
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2003
     * @param src the source, which means System.in if null, or a URI,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2004
     *        or a bare file path name
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2005
     */
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2006
    public static Collection<? extends CRL> loadCRLs(String src) throws Exception {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2007
        InputStream in = null;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2008
        URI uri = null;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2009
        if (src == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2010
            in = System.in;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2011
        } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2012
            try {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2013
                uri = new URI(src);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2014
                if (uri.getScheme().equals("ldap")) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2015
                    // No input stream for LDAP
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2016
                } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2017
                    in = uri.toURL().openStream();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2018
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2019
            } catch (Exception e) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2020
                try {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2021
                    in = new FileInputStream(src);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2022
                } catch (Exception e2) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2023
                    if (uri == null || uri.getScheme() == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2024
                        throw e2;   // More likely a bare file path
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2025
                    } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2026
                        throw e;    // More likely a protocol or network problem
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2027
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2028
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2029
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2030
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2031
        if (in != null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2032
            try {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2033
                // Read the full stream before feeding to X509Factory,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2034
                // otherwise, keytool -gencrl | keytool -printcrl
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2035
                // might not work properly, since -gencrl is slow
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2036
                // and there's no data in the pipe at the beginning.
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2037
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2038
                byte[] b = new byte[4096];
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2039
                while (true) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2040
                    int len = in.read(b);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2041
                    if (len < 0) break;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2042
                    bout.write(b, 0, len);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2043
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2044
                return CertificateFactory.getInstance("X509").generateCRLs(
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2045
                        new ByteArrayInputStream(bout.toByteArray()));
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2046
            } finally {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2047
                if (in != System.in) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2048
                    in.close();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2049
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2050
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2051
        } else {    // must be LDAP, and uri is not null
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2052
            // Lazily load LDAPCertStoreHelper if present
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2053
            CertStoreHelper helper = CertStoreHelper.getInstance("LDAP");
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2054
            String path = uri.getPath();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2055
            if (path.charAt(0) == '/') path = path.substring(1);
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2056
            CertStore s = helper.getCertStore(uri);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2057
            X509CRLSelector sel =
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2058
                    helper.wrap(new X509CRLSelector(), null, path);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2059
            return s.getCRLs(sel);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2060
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2061
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2062
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2063
    /**
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2064
     * Returns CRLs described in a X509Certificate's CRLDistributionPoints
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2065
     * Extension. Only those containing a general name of type URI are read.
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2066
     */
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2067
    public static List<CRL> readCRLsFromCert(X509Certificate cert)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2068
            throws Exception {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
  2069
        List<CRL> crls = new ArrayList<>();
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2070
        CRLDistributionPointsExtension ext =
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2071
                X509CertImpl.toImpl(cert).getCRLDistributionPointsExtension();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2072
        if (ext == null) return crls;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  2073
        List<DistributionPoint> distPoints =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  2074
                ext.get(CRLDistributionPointsExtension.POINTS);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  2075
        for (DistributionPoint o: distPoints) {
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2076
            GeneralNames names = o.getFullName();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2077
            if (names != null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2078
                for (GeneralName name: names.names()) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2079
                    if (name.getType() == GeneralNameInterface.NAME_URI) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2080
                        URIName uriName = (URIName)name.getName();
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
  2081
                        for (CRL crl: loadCRLs(uriName.getName())) {
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2082
                            if (crl instanceof X509CRL) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2083
                                crls.add((X509CRL)crl);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2084
                            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2085
                        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2086
                        break;  // Different name should point to same CRL
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2087
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2088
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2089
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2090
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2091
        return crls;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2092
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2093
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2094
    private static String verifyCRL(KeyStore ks, CRL crl)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2095
            throws Exception {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2096
        X509CRLImpl xcrl = (X509CRLImpl)crl;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2097
        X500Principal issuer = xcrl.getIssuerX500Principal();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2098
        for (String s: e2i(ks.aliases())) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2099
            Certificate cert = ks.getCertificate(s);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2100
            if (cert instanceof X509Certificate) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2101
                X509Certificate xcert = (X509Certificate)cert;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2102
                if (xcert.getSubjectX500Principal().equals(issuer)) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2103
                    try {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2104
                        ((X509CRLImpl)crl).verify(cert.getPublicKey());
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2105
                        return s;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2106
                    } catch (Exception e) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2107
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2108
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2109
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2110
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2111
        return null;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2112
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2113
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2114
    private void doPrintCRL(String src, PrintStream out)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2115
            throws Exception {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2116
        for (CRL crl: loadCRLs(src)) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2117
            printCRL(crl, out);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2118
            String issuer = null;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2119
            if (caks != null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2120
                issuer = verifyCRL(caks, crl);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2121
                if (issuer != null) {
11912
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2122
                    out.printf(rb.getString(
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2123
                            "verified.by.s.in.s"), issuer, "cacerts");
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2124
                    out.println();
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2125
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2126
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2127
            if (issuer == null && keyStore != null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2128
                issuer = verifyCRL(keyStore, crl);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2129
                if (issuer != null) {
11912
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2130
                    out.printf(rb.getString(
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2131
                            "verified.by.s.in.s"), issuer, "keystore");
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2132
                    out.println();
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2133
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2134
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2135
            if (issuer == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2136
                out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2137
                        ("STAR"));
11912
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2138
                out.println(rb.getString
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2139
                        ("warning.not.verified.make.sure.keystore.is.correct"));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2140
                out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2141
                        ("STARNN"));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2142
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2143
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2144
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2145
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2146
    private void printCRL(CRL crl, PrintStream out)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2147
            throws Exception {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2148
        if (rfc) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2149
            X509CRL xcrl = (X509CRL)crl;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2150
            out.println("-----BEGIN X509 CRL-----");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2151
            new BASE64Encoder().encodeBuffer(xcrl.getEncoded(), out);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2152
            out.println("-----END X509 CRL-----");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2153
        } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2154
            out.println(crl.toString());
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2155
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2156
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2157
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2158
    private void doPrintCertReq(InputStream in, PrintStream out)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2159
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2160
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2161
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2162
        StringBuffer sb = new StringBuffer();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2163
        boolean started = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2164
        while (true) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2165
            String s = reader.readLine();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2166
            if (s == null) break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2167
            if (!started) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2168
                if (s.startsWith("-----")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2169
                    started = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2170
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2171
            } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2172
                if (s.startsWith("-----")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2173
                    break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2174
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2175
                sb.append(s);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2176
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2177
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2178
        PKCS10 req = new PKCS10(new BASE64Decoder().decodeBuffer(new String(sb)));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2179
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2180
        PublicKey pkey = req.getSubjectPublicKeyInfo();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2181
        out.printf(rb.getString("PKCS.10.Certificate.Request.Version.1.0.Subject.s.Public.Key.s.format.s.key."),
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2182
                req.getSubjectName(), pkey.getFormat(), pkey.getAlgorithm());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2183
        for (PKCS10Attribute attr: req.getAttributes().getAttributes()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2184
            ObjectIdentifier oid = attr.getAttributeId();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  2185
            if (oid.equals((Object)PKCS9Attribute.EXTENSION_REQUEST_OID)) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2186
                CertificateExtensions exts = (CertificateExtensions)attr.getAttributeValue();
2179
e172c13ca87a 6813402: keytool cannot -printcert entries without extensions
weijun
parents: 2067
diff changeset
  2187
                if (exts != null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2188
                    printExtensions(rb.getString("Extension.Request."), exts, out);
2179
e172c13ca87a 6813402: keytool cannot -printcert entries without extensions
weijun
parents: 2067
diff changeset
  2189
                }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2190
            } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2191
                out.println(attr.getAttributeId());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2192
                out.println(attr.getAttributeValue());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2193
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2194
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2195
        if (debug) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2196
            out.println(req);   // Just to see more, say, public key length...
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2197
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2198
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2199
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     * Reads a certificate (or certificate chain) and prints its contents in
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2202
     * a human readable format.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     */
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2204
    private void printCertFromStream(InputStream in, PrintStream out)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        Collection<? extends Certificate> c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            c = cf.generateCertificates(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
        } catch (CertificateException ce) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2211
            throw new Exception(rb.getString("Failed.to.parse.input"), ce);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        if (c.isEmpty()) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2214
            throw new Exception(rb.getString("Empty.input"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        Certificate[] certs = c.toArray(new Certificate[c.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        for (int i=0; i<certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            X509Certificate x509Cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                x509Cert = (X509Certificate)certs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
            } catch (ClassCastException cce) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2222
                throw new Exception(rb.getString("Not.X.509.certificate"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            if (certs.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2226
                        (rb.getString("Certificate.i.1."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                Object[] source = {new Integer(i + 1)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            }
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2230
            if (rfc) dumpCert(x509Cert, out);
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2231
            else printX509Cert(x509Cert, out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            if (i < (certs.length-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2238
    private void doPrintCert(final PrintStream out) throws Exception {
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2239
        if (jarfile != null) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2240
            JarFile jf = new JarFile(jarfile, true);
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2241
            Enumeration<JarEntry> entries = jf.entries();
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
  2242
            Set<CodeSigner> ss = new HashSet<>();
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2243
            byte[] buffer = new byte[8192];
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2244
            int pos = 0;
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2245
            while (entries.hasMoreElements()) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2246
                JarEntry je = entries.nextElement();
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2247
                try (InputStream is = jf.getInputStream(je)) {
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2248
                    while (is.read(buffer) != -1) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2249
                        // we just read. this will throw a SecurityException
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2250
                        // if a signature/digest check fails. This also
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2251
                        // populate the signers
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2252
                    }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2253
                }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2254
                CodeSigner[] signers = je.getCodeSigners();
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2255
                if (signers != null) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2256
                    for (CodeSigner signer: signers) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2257
                        if (!ss.contains(signer)) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2258
                            ss.add(signer);
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2259
                            out.printf(rb.getString("Signer.d."), ++pos);
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2260
                            out.println();
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2261
                            out.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2262
                            out.println(rb.getString("Signature."));
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2263
                            out.println();
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2264
                            for (Certificate cert: signer.getSignerCertPath().getCertificates()) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2265
                                X509Certificate x = (X509Certificate)cert;
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2266
                                if (rfc) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2267
                                    out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2268
                                    dumpCert(x, out);
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2269
                                } else {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2270
                                    printX509Cert(x, out);
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2271
                                }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2272
                                out.println();
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2273
                            }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2274
                            Timestamp ts = signer.getTimestamp();
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2275
                            if (ts != null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2276
                                out.println(rb.getString("Timestamp."));
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2277
                                out.println();
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2278
                                for (Certificate cert: ts.getSignerCertPath().getCertificates()) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2279
                                    X509Certificate x = (X509Certificate)cert;
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2280
                                    if (rfc) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2281
                                        out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2282
                                        dumpCert(x, out);
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2283
                                    } else {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2284
                                        printX509Cert(x, out);
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2285
                                    }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2286
                                    out.println();
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2287
                                }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2288
                            }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2289
                        }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2290
                    }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2291
                }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2292
            }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2293
            jf.close();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  2294
            if (ss.isEmpty()) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2295
                out.println(rb.getString("Not.a.signed.jar.file"));
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2296
            }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2297
        } else if (sslserver != null) {
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2298
            // Lazily load SSLCertStoreHelper if present
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2299
            CertStoreHelper helper = CertStoreHelper.getInstance("SSLServer");
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2300
            CertStore cs = helper.getCertStore(new URI("https://" + sslserver));
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2301
            Collection<? extends Certificate> chain;
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2302
            try {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2303
                chain = cs.getCertificates(null);
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2304
                if (chain.isEmpty()) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2305
                    // If the certs are not retrieved, we consider it an error
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2306
                    // even if the URL connection is successful.
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2307
                    throw new Exception(rb.getString(
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2308
                                        "No.certificate.from.the.SSL.server"));
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2309
                }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2310
            } catch (CertStoreException cse) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2311
                if (cse.getCause() instanceof IOException) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2312
                    throw new Exception(rb.getString(
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2313
                                        "No.certificate.from.the.SSL.server"),
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2314
                                        cse.getCause());
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2315
                } else {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2316
                    throw cse;
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2317
                }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2318
            }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2319
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2320
            int i = 0;
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2321
            for (Certificate cert : chain) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2322
                try {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2323
                    if (rfc) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2324
                        dumpCert(cert, out);
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2325
                    } else {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2326
                        out.println("Certificate #" + i++);
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2327
                        out.println("====================================");
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2328
                        printX509Cert((X509Certificate)cert, out);
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2329
                        out.println();
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2330
                    }
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2331
                } catch (Exception e) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2332
                    if (debug) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2333
                        e.printStackTrace();
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2334
                    }
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2335
                }
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2336
            }
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2337
        } else {
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2338
            if (filename != null) {
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2339
                try (FileInputStream inStream = new FileInputStream(filename)) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2340
                    printCertFromStream(inStream, out);
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2341
                }
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2342
            } else {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2343
                printCertFromStream(System.in, out);
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2344
            }
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2345
        }
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2346
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
     * Creates a self-signed certificate, and stores it as a single-element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
     * certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
    private void doSelfCert(String alias, String dname, String sigAlgName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2358
        Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2359
        PrivateKey privKey = (PrivateKey)objs.fst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
        if (keyPass == null)
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2361
            keyPass = objs.snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
        // Determine the signature algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        if (sigAlgName == null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2365
            sigAlgName = getCompatibleSigAlgName(privKey.getAlgorithm());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
        // Get the old certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        Certificate oldCert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        if (oldCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2372
                (rb.getString("alias.has.no.public.key"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        if (!(oldCert instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2378
                (rb.getString("alias.has.no.X.509.certificate"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        // convert to X509CertImpl, so that we can modify selected fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        // (no public APIs available yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        byte[] encoded = oldCert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        X509CertImpl certImpl = new X509CertImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
        X509CertInfo certInfo = (X509CertInfo)certImpl.get(X509CertImpl.NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                                                           + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                                                           X509CertImpl.INFO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        // Extend its validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
        Date firstDate = getStartDate(startDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        Date lastDate = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
        lastDate.setTime(firstDate.getTime() + validity*1000L*24L*60L*60L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        CertificateValidity interval = new CertificateValidity(firstDate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                                                               lastDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        certInfo.set(X509CertInfo.VALIDITY, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        // Make new serial number
2293
cb6d01cb3c3d 6820606: keytool can generate serialno more randomly
weijun
parents: 2289
diff changeset
  2400
        certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(
cb6d01cb3c3d 6820606: keytool can generate serialno more randomly
weijun
parents: 2289
diff changeset
  2401
                    new java.util.Random().nextInt() & 0x7fffffff));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        // Set owner and issuer fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        X500Name owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        if (dname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            // Get the owner name from the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            owner = (X500Name)certInfo.get(X509CertInfo.SUBJECT + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
                                           CertificateSubjectName.DN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
            // Use the owner name specified at the command line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            owner = new X500Name(dname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            certInfo.set(X509CertInfo.SUBJECT + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                         CertificateSubjectName.DN_NAME, owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        // Make issuer same as owner (self-signed!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
        certInfo.set(X509CertInfo.ISSUER + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                     CertificateIssuerName.DN_NAME, owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        // The inner and outer signature algorithms have to match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
        // The way we achieve that is really ugly, but there seems to be no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        // other solution: We first sign the cert, then retrieve the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        // outer sigalg and use it to set the inner sigalg
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        X509CertImpl newCert = new X509CertImpl(certInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        newCert.sign(privKey, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
        AlgorithmId sigAlgid = (AlgorithmId)newCert.get(X509CertImpl.SIG_ALG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        certInfo.set(CertificateAlgorithmId.NAME + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                     CertificateAlgorithmId.ALGORITHM, sigAlgid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        certInfo.set(X509CertInfo.VERSION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                        new CertificateVersion(CertificateVersion.V3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2432
        CertificateExtensions ext = createV3Extensions(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2433
                null,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2434
                (CertificateExtensions)certInfo.get(X509CertInfo.EXTENSIONS),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2435
                v3ext,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2436
                oldCert.getPublicKey(),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2437
                null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2438
        certInfo.set(X509CertInfo.EXTENSIONS, ext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        // Sign the new certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        newCert = new X509CertImpl(certInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        newCert.sign(privKey, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        // Store the new certificate as a single-element certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        keyStore.setKeyEntry(alias, privKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                             (keyPass != null) ? keyPass : storePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                             new Certificate[] { newCert } );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
        if (verbose) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2449
            System.err.println(rb.getString("New.certificate.self.signed."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
            System.err.print(newCert.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * Processes a certificate reply from a certificate authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * <p>Builds a certificate chain on top of the certificate reply,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     * using trusted certificates from the keystore. The chain is complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
     * after a self-signed certificate has been encountered. The self-signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
     * certificate is considered a root certificate authority, and is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * at the end of the chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
     * <p>The newly generated chain replaces the old chain associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
     * key entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * @return true if the certificate reply was installed, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
    private boolean installReply(String alias, InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2476
        Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2477
        PrivateKey privKey = (PrivateKey)objs.fst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
        if (keyPass == null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2479
            keyPass = objs.snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
        Certificate userCert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
        if (userCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2485
                (rb.getString("alias.has.no.public.key.certificate."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
        // Read the certificates in the reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        Collection<? extends Certificate> c = cf.generateCertificates(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        if (c.isEmpty()) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2493
            throw new Exception(rb.getString("Reply.has.no.certificates"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
        Certificate[] replyCerts = c.toArray(new Certificate[c.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
        Certificate[] newChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        if (replyCerts.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            // single-cert reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
            newChain = establishCertChain(userCert, replyCerts[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
            // cert-chain reply (e.g., PKCS#7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
            newChain = validateReply(alias, userCert, replyCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
        // Now store the newly established chain in the keystore. The new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
        // chain replaces the old one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
        if (newChain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
            keyStore.setKeyEntry(alias, privKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                                 (keyPass != null) ? keyPass : storePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                                 newChain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
            return false;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     * Imports a certificate and adds it to the list of trusted certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     * @return true if the certificate was added, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
    private boolean addTrustedCert(String alias, InputStream in)
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
        if (alias == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2526
            throw new Exception(rb.getString("Must.specify.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2530
                ("Certificate.not.imported.alias.alias.already.exists"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
        // Read the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
        X509Certificate cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
            cert = (X509Certificate)cf.generateCertificate(in);
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2539
        } catch (ClassCastException | CertificateException ce) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2540
            throw new Exception(rb.getString("Input.not.an.X.509.certificate"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
        // if certificate is self-signed, make sure it verifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        boolean selfSigned = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        if (isSelfSigned(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
            cert.verify(cert.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            selfSigned = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
        if (noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
            keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        // check if cert already exists in keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        String reply = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
        String trustalias = keyStore.getCertificateAlias(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        if (trustalias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2560
                ("Certificate.already.exists.in.keystore.under.alias.trustalias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
            Object[] source = {trustalias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
            reply = getYesNoReply
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2564
                (rb.getString("Do.you.still.want.to.add.it.no."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
        } else if (selfSigned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
            if (trustcacerts && (caks != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                    ((trustalias=caks.getCertificateAlias(cert)) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2569
                        ("Certificate.already.exists.in.system.wide.CA.keystore.under.alias.trustalias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                Object[] source = {trustalias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                reply = getYesNoReply
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2573
                        (rb.getString("Do.you.still.want.to.add.it.to.your.own.keystore.no."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
            if (trustalias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                // Print the cert and ask user if they really want to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                // it to their keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                printX509Cert(cert, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                reply = getYesNoReply
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2580
                        (rb.getString("Trust.this.certificate.no."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        if (reply != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
            if ("YES".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
        // Try to establish trust chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
            Certificate[] chain = establishCertChain(null, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
            if (chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
            // Print the cert and ask user if they really want to add it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            // their keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            printX509Cert(cert, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
            reply = getYesNoReply
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2604
                (rb.getString("Trust.this.certificate.no."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            if ("YES".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
     * Prompts user for new password. New password must be different from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
     * old one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
     * @param prompt the message that gets prompted on the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
     * @param oldPasswd the current (i.e., old) password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
    private char[] getNewPasswd(String prompt, char[] oldPasswd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        char[] entered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
        char[] reentered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        for (int count = 0; count < 3; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2631
                (rb.getString("New.prompt."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
            Object[] source = {prompt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
            System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
            entered = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            passwords.add(entered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
            if (entered == null || entered.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2638
                    ("Password.is.too.short.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            } else if (Arrays.equals(entered, oldPasswd)) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2640
                System.err.println(rb.getString("Passwords.must.differ"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2643
                        (rb.getString("Re.enter.new.prompt."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                Object[] src = {prompt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                System.err.print(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                reentered = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                passwords.add(reentered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
                if (!Arrays.equals(entered, reentered)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                    System.err.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2650
                        (rb.getString("They.don.t.match.Try.again"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
                    Arrays.fill(reentered, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                    return entered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
            if (entered != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                Arrays.fill(entered, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                entered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            if (reentered != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
                Arrays.fill(reentered, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                reentered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
        }
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2665
        throw new Exception(rb.getString("Too.many.failures.try.later"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
     * Prompts user for alias name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
     * @param prompt the {0} of "Enter {0} alias name:  " in prompt line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
     * @returns the string entered by the user, without the \n at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
    private String getAlias(String prompt) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
        if (prompt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2676
                (rb.getString("Enter.prompt.alias.name."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
            Object[] source = {prompt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
            System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
        } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2680
            System.err.print(rb.getString("Enter.alias.name."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
        return (new BufferedReader(new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                                        System.in))).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
     * Prompts user for an input string from the command line (System.in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
     * @prompt the prompt string printed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
     * @returns the string entered by the user, without the \n at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
    private String inputStringFromStdin(String prompt) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
        System.err.print(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
        return (new BufferedReader(new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                                        System.in))).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
     * Prompts user for key password. User may select to choose the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
     * password (<code>otherKeyPass</code>) as for <code>otherAlias</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
    private char[] getKeyPasswd(String alias, String otherAlias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
                                char[] otherKeyPass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
        char[] keyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
            if (otherKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2711
                        ("Enter.key.password.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
                form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2716
                        (".RETURN.if.same.as.for.otherAlias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                Object[] src = {otherAlias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                System.err.print(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2721
                        ("Enter.key.password.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
                System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
            System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
            keyPass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
            passwords.add(keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
            if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
                keyPass = otherKeyPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        } while ((keyPass == null) && count < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
        if (keyPass == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2735
            throw new Exception(rb.getString("Too.many.failures.try.later"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
        return keyPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
     * Prints a certificate in a human readable format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
    private void printX509Cert(X509Certificate cert, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        out.println("Owner: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                    + cert.getSubjectDN().toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                    + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
                    + "Issuer: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
                    + cert.getIssuerDN().toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                    + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
                    + "Serial number: " + cert.getSerialNumber().toString(16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
                    + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
                    + "Valid from: " + cert.getNotBefore().toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                    + " until: " + cert.getNotAfter().toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                    + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                    + "Certificate fingerprints:\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
                    + "\t MD5:  " + getCertFingerPrint("MD5", cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
                    + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
                    + "\t SHA1: " + getCertFingerPrint("SHA1", cert));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
        MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2766
                (rb.getString(".PATTERN.printX509Cert"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
        Object[] source = {cert.getSubjectDN().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                        cert.getIssuerDN().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                        cert.getSerialNumber().toString(16),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                        cert.getNotBefore().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                        cert.getNotAfter().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                        getCertFingerPrint("MD5", cert),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
                        getCertFingerPrint("SHA1", cert),
3318
dade78e63c92 6561126: keytool should use larger default keysize for keypairs
weijun
parents: 3316
diff changeset
  2774
                        getCertFingerPrint("SHA-256", cert),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                        cert.getSigAlgName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                        cert.getVersion()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
                        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
        out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
        if (cert instanceof X509CertImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
            X509CertImpl impl = (X509CertImpl)cert;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2782
            X509CertInfo certInfo = (X509CertInfo)impl.get(X509CertImpl.NAME
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2783
                                                           + "." +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2784
                                                           X509CertImpl.INFO);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2785
            CertificateExtensions exts = (CertificateExtensions)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2786
                    certInfo.get(X509CertInfo.EXTENSIONS);
2179
e172c13ca87a 6813402: keytool cannot -printcert entries without extensions
weijun
parents: 2067
diff changeset
  2787
            if (exts != null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2788
                printExtensions(rb.getString("Extensions."), exts, out);
2179
e172c13ca87a 6813402: keytool cannot -printcert entries without extensions
weijun
parents: 2067
diff changeset
  2789
            }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2790
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2791
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2792
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2793
    private static void printExtensions(String title, CertificateExtensions exts, PrintStream out)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2794
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2795
        int extnum = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2796
        Iterator<Extension> i1 = exts.getAllExtensions().iterator();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2797
        Iterator<Extension> i2 = exts.getUnparseableExtensions().values().iterator();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2798
        while (i1.hasNext() || i2.hasNext()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2799
            Extension ext = i1.hasNext()?i1.next():i2.next();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2800
            if (extnum == 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2801
                out.println();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2802
                out.println(title);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2803
                out.println();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2804
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2805
            out.print("#"+(++extnum)+": "+ ext);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2806
            if (ext.getClass() == Extension.class) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2807
                byte[] v = ext.getExtensionValue();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2808
                if (v.length == 0) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2809
                    out.println(rb.getString(".Empty.value."));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2810
                } else {
5457
d2782f1ecc9f 6947487: use HexDumpEncoder.encodeBuffer()
weijun
parents: 5296
diff changeset
  2811
                    new sun.misc.HexDumpEncoder().encodeBuffer(ext.getExtensionValue(), out);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2812
                    out.println();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
            }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2815
            out.println();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
        }
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 true if the certificate is self-signed, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
    private boolean isSelfSigned(X509Certificate cert) {
2437
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2823
        return signedBy(cert, cert);
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2824
    }
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2825
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2826
    private boolean signedBy(X509Certificate end, X509Certificate ca) {
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2827
        if (!ca.getSubjectDN().equals(end.getIssuerDN())) {
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2828
            return false;
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2829
        }
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2830
        try {
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2831
            end.verify(ca.getPublicKey());
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2832
            return true;
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2833
        } catch (Exception e) {
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2834
            return false;
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  2835
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
    /**
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2839
     * Locates a signer for a given certificate from a given keystore and
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2840
     * returns the signer's certificate.
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2841
     * @param cert the certificate whose signer is searched, not null
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2842
     * @param ks the keystore to search with, not null
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2843
     * @return <code>cert</code> itself if it's already inside <code>ks</code>,
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2844
     * or a certificate inside <code>ks</code> who signs <code>cert</code>,
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2845
     * or null otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
     */
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2847
    private static Certificate getTrustedSigner(Certificate cert, KeyStore ks)
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2848
            throws Exception {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2849
        if (ks.getCertificateAlias(cert) != null) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2850
            return cert;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
        }
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2852
        for (Enumeration<String> aliases = ks.aliases();
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2853
                aliases.hasMoreElements(); ) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2854
            String name = aliases.nextElement();
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2855
            Certificate trustedCert = ks.getCertificate(name);
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2856
            if (trustedCert != null) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2857
                try {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2858
                    cert.verify(trustedCert.getPublicKey());
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2859
                    return trustedCert;
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2860
                } catch (Exception e) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2861
                    // Not verified, skip to the next one
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2862
                }
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2863
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
        }
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  2865
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
     * Gets an X.500 name suitable for inclusion in a certification request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
    private X500Name getX500Name() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        BufferedReader in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
        in = new BufferedReader(new InputStreamReader(System.in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        String commonName = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
        String organizationalUnit = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
        String organization = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
        String city = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
        String state = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
        String country = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
        X500Name name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
        String userInput = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
        int maxRetry = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
            if (maxRetry-- < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                throw new RuntimeException(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2887
                        "Too.many.retries.program.terminated"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            commonName = inputString(in,
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2890
                    rb.getString("What.is.your.first.and.last.name."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                    commonName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
            organizationalUnit = inputString(in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
                    rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2894
                        ("What.is.the.name.of.your.organizational.unit."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                    organizationalUnit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
            organization = inputString(in,
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2897
                    rb.getString("What.is.the.name.of.your.organization."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                    organization);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
            city = inputString(in,
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2900
                    rb.getString("What.is.the.name.of.your.City.or.Locality."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                    city);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
            state = inputString(in,
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2903
                    rb.getString("What.is.the.name.of.your.State.or.Province."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                    state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
            country = inputString(in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                    rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2907
                        ("What.is.the.two.letter.country.code.for.this.unit."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
                    country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
            name = new X500Name(commonName, organizationalUnit, organization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
                                city, state, country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2912
                (rb.getString("Is.name.correct."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
            Object[] source = {name};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
            userInput = inputString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                (in, form.format(source), rb.getString("no"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
        } while (collator.compare(userInput, rb.getString("yes")) != 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                 collator.compare(userInput, rb.getString("y")) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
    private String inputString(BufferedReader in, String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
                               String defaultValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
        System.err.println(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
        MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2929
                (rb.getString(".defaultValue."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
        Object[] source = {defaultValue};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
        System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
        System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
        String value = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
        if (value == null || collator.compare(value, "") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
            value = defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
     * Writes an X.509 certificate in base64 or binary encoding to an output
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
    private void dumpCert(Certificate cert, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        throws IOException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
        if (rfc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
            BASE64Encoder encoder = new BASE64Encoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
            out.println(X509Factory.BEGIN_CERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
            encoder.encodeBuffer(cert.getEncoded(), out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
            out.println(X509Factory.END_CERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
            out.write(cert.getEncoded()); // binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
        }
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
     * Converts a byte to hex digit and writes to the supplied buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
    private void byte2hex(byte b, StringBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
                            '9', 'A', 'B', 'C', 'D', 'E', 'F' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
        int high = ((b & 0xf0) >> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
        int low = (b & 0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        buf.append(hexChars[high]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
        buf.append(hexChars[low]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
     * Converts a byte array to hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
    private String toHexString(byte[] block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
        int len = block.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
             byte2hex(block[i], buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
             if (i < len-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
                 buf.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
     * Recovers (private) key associated with given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
     * @return an array of objects, where the 1st element in the array is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
     * recovered private key, and the 2nd element is the password used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
     * recover it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
     */
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2992
    private Pair<Key,char[]> recoverKey(String alias, char[] storePass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
                                       char[] keyPass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
        Key key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3000
                (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
        if (!keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
                !keyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3007
                (rb.getString("Alias.alias.has.no.key"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
            // Try to recover the key using the keystore password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                key = keyStore.getKey(alias, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                keyPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
                passwords.add(keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
            } catch (UnrecoverableKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                // Did not work out, so prompt user for key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                if (!token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
                    keyPass = getKeyPasswd(alias, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
                    key = keyStore.getKey(alias, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
            key = keyStore.getKey(alias, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3032
        return Pair.of(key, keyPass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
     * Recovers entry associated with given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
     * @return an array of objects, where the 1st element in the array is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
     * recovered entry, and the 2nd element is the password used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
     * recover it (null if no password).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
     */
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3042
    private Pair<Entry,char[]> recoverEntry(KeyStore ks,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
                            String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
                            char[] pstore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
                            char[] pkey) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
        if (ks.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3049
                (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        PasswordProtection pp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
        Entry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
            // First attempt to access entry without key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
            // (PKCS11 entry or trusted certificate entry, for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
            entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
            pkey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
        } catch (UnrecoverableEntryException une) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
            if(P11KEYSTORE.equalsIgnoreCase(ks.getType()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
                KeyStoreUtil.isWindowsKeyStore(ks.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
                // should not happen, but a possibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
                throw une;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
            // entry is protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
            if (pkey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
                // try provided key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
                pp = new PasswordProtection(pkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
                entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
                // try store pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
                    pp = new PasswordProtection(pstore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
                    entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
                    pkey = pstore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
                } catch (UnrecoverableEntryException une2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
                    if (P12KEYSTORE.equalsIgnoreCase(ks.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
                        // P12 keystore currently does not support separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
                        // store and entry passwords
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                        throw une2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
                        // prompt for entry password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
                        pkey = getKeyPasswd(alias, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
                        pp = new PasswordProtection(pkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                        entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3107
        return Pair.of(entry, pkey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
     * Gets the requested finger print of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
    private String getCertFingerPrint(String mdAlg, Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
        byte[] encCertInfo = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
        MessageDigest md = MessageDigest.getInstance(mdAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
        byte[] digest = md.digest(encCertInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
        return toHexString(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
     * Prints warning about missing integrity check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
    private void printWarning() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3127
            (".WARNING.WARNING.WARNING."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3129
            (".The.integrity.of.the.information.stored.in.your.keystore."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3131
            (".WARNING.WARNING.WARNING."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
     * Validates chain in certification reply, and returns the ordered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
     * elements of the chain (with user certificate first, and root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
     * certificate last in the array).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
     * @param userCert the user certificate of the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
     * @param replyCerts the chain provided in the reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
    private Certificate[] validateReply(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                                        Certificate userCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
                                        Certificate[] replyCerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
        // order the certs in the reply (bottom-up).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
        // we know that all certs in the reply are of type X.509, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
        // we parsed them using an X.509 certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
        PublicKey userPubKey = userCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
        for (i=0; i<replyCerts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
            if (userPubKey.equals(replyCerts[i].getPublicKey())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
        if (i == replyCerts.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3161
                ("Certificate.reply.does.not.contain.public.key.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
        Certificate tmpCert = replyCerts[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
        replyCerts[0] = replyCerts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
        replyCerts[i] = tmpCert;
2437
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  3169
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  3170
        X509Certificate thisCert = (X509Certificate)replyCerts[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
        for (i=1; i < replyCerts.length-1; i++) {
2437
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  3173
            // find a cert in the reply who signs thisCert
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
            int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
            for (j=i; j<replyCerts.length; j++) {
2437
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  3176
                if (signedBy(thisCert, (X509Certificate)replyCerts[j])) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
                    tmpCert = replyCerts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
                    replyCerts[i] = replyCerts[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
                    replyCerts[j] = tmpCert;
2437
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  3180
                    thisCert = (X509Certificate)replyCerts[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
            if (j == replyCerts.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
                throw new Exception
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3186
                    (rb.getString("Incomplete.certificate.chain.in.reply"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
        if (noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
            return replyCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3194
        // do we trust the cert at the top?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
        Certificate topCert = replyCerts[replyCerts.length-1];
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3196
        Certificate root = getTrustedSigner(topCert, keyStore);
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3197
        if (root == null && trustcacerts && caks != null) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3198
            root = getTrustedSigner(topCert, caks);
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3199
        }
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3200
        if (root == null) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3201
            System.err.println();
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3202
            System.err.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3203
                    (rb.getString("Top.level.certificate.in.reply."));
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3204
            printX509Cert((X509Certificate)topCert, System.out);
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3205
            System.err.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3206
            System.err.print(rb.getString(".is.not.trusted."));
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3207
            String reply = getYesNoReply
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3208
                    (rb.getString("Install.reply.anyway.no."));
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3209
            if ("NO".equals(reply)) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3210
                return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
            }
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3212
        } else {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3213
            if (root != topCert) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3214
                // append the root CA cert to the chain
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3215
                Certificate[] tmpCerts =
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3216
                    new Certificate[replyCerts.length+1];
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3217
                System.arraycopy(replyCerts, 0, tmpCerts, 0,
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3218
                                 replyCerts.length);
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3219
                tmpCerts[tmpCerts.length-1] = root;
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3220
                replyCerts = tmpCerts;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
        return replyCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
     * Establishes a certificate chain (using trusted certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
     * keystore), starting with the user certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
     * and ending at a self-signed certificate found in the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
     * @param userCert the user certificate of the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
     * @param certToVerify the single certificate provided in the reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
    private Certificate[] establishCertChain(Certificate userCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
                                             Certificate certToVerify)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
        if (userCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
            // Make sure that the public key of the certificate reply matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
            // the original public key in the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
            PublicKey origPubKey = userCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
            PublicKey replyPubKey = certToVerify.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
            if (!origPubKey.equals(replyPubKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
                throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3246
                        ("Public.keys.in.reply.and.keystore.don.t.match"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
            // If the two certs are identical, we're done: no need to import
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
            // anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
            if (certToVerify.equals(userCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
                throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3253
                        ("Certificate.reply.and.certificate.in.keystore.are.identical"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
        // Build a hash table of all certificates in the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
        // Use the subject distinguished name as the key into the hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
        // All certificates associated with the same subject distinguished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
        // name are stored in the same hash table entry as a vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
        Hashtable<Principal, Vector<Certificate>> certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
        if (keyStore.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
            certs = new Hashtable<Principal, Vector<Certificate>>(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
            keystorecerts2Hashtable(keyStore, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
        if (trustcacerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
            if (caks!=null && caks.size()>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
                if (certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
                    certs = new Hashtable<Principal, Vector<Certificate>>(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
                keystorecerts2Hashtable(caks, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
        // start building chain
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
  3276
        Vector<Certificate> chain = new Vector<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
        if (buildChain((X509Certificate)certToVerify, chain, certs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
            Certificate[] newChain = new Certificate[chain.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
            // buildChain() returns chain with self-signed root-cert first and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
            // user-cert last, so we need to invert the chain before we store
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
            // it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
            int j=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
            for (int i=chain.size()-1; i>=0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
                newChain[j] = chain.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
            return newChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
            throw new Exception
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3290
                (rb.getString("Failed.to.establish.chain.from.reply"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
     * Recursively tries to establish chain from pool of trusted certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
     * @param certToVerify the cert that needs to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
     * @param chain the chain that's being built.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
     * @param certs the pool of trusted certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
     * @return true if successful, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
    private boolean buildChain(X509Certificate certToVerify,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
                        Vector<Certificate> chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
                        Hashtable<Principal, Vector<Certificate>> certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
        Principal issuer = certToVerify.getIssuerDN();
2437
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  3307
        if (isSelfSigned(certToVerify)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
            // reached self-signed root cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
            // no verification needed because it's trusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
            chain.addElement(certToVerify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
        // Get the issuer's certificate(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
        Vector<Certificate> vec = certs.get(issuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
        if (vec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
        // Try out each certificate in the vector, until we find one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
        // whose public key verifies the signature of the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
        // in question.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
        for (Enumeration<Certificate> issuerCerts = vec.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
             issuerCerts.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
            X509Certificate issuerCert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
                = (X509Certificate)issuerCerts.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
            PublicKey issuerPubKey = issuerCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
                certToVerify.verify(issuerPubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
            if (buildChain(issuerCert, chain, certs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                chain.addElement(certToVerify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
     * Prompts user for yes/no decision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
     * @return the user's decision, can only be "YES" or "NO"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
    private String getYesNoReply(String prompt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
        String reply = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
        int maxRetry = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
            if (maxRetry-- < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
                throw new RuntimeException(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3354
                        "Too.many.retries.program.terminated"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
            System.err.print(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
            System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
            reply = (new BufferedReader(new InputStreamReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
                                        (System.in))).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
            if (collator.compare(reply, "") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
                collator.compare(reply, rb.getString("n")) == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
                collator.compare(reply, rb.getString("no")) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
                reply = "NO";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
            } else if (collator.compare(reply, rb.getString("y")) == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
                       collator.compare(reply, rb.getString("yes")) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
                reply = "YES";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
            } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3368
                System.err.println(rb.getString("Wrong.answer.try.again"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
                reply = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
        } while (reply == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
        return reply;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
     * Stores the (leaf) certificates of a keystore in a hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
     * All certs belonging to the same CA are stored in a vector that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
     * in turn is stored in the hashtable, keyed by the CA's subject DN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
    private void keystorecerts2Hashtable(KeyStore ks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
                Hashtable<Principal, Vector<Certificate>> hash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
        for (Enumeration<String> aliases = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
                                        aliases.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
            String alias = aliases.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
            Certificate cert = ks.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
            if (cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
                Principal subjectDN = ((X509Certificate)cert).getSubjectDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
                Vector<Certificate> vec = hash.get(subjectDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
                if (vec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
                    vec = new Vector<Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
                    vec.addElement(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
                    if (!vec.contains(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
                        vec.addElement(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
                hash.put(subjectDN, vec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
     * Returns the issue time that's specified the -startdate option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
     * @param s the value of -startdate option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
    private static Date getStartDate(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
        Calendar c = new GregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
            IOException ioe = new IOException(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3412
                    rb.getString("Illegal.startdate.value"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
            int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
            if (s.charAt(0) == '-' || s.charAt(0) == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
                // Form 1: ([+-]nnn[ymdHMS])+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
                int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
                while (start < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
                    int sign = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
                    switch (s.charAt(start)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
                        case '+': sign = 1; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
                        case '-': sign = -1; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
                        default: throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
                    int i = start+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
                    for (; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
                        char ch = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
                        if (ch < '0' || ch > '9') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
                    if (i == start+1) throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
                    int number = Integer.parseInt(s.substring(start+1, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
                    if (i >= len) throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
                    int unit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
                    switch (s.charAt(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
                        case 'y': unit = Calendar.YEAR; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
                        case 'm': unit = Calendar.MONTH; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
                        case 'd': unit = Calendar.DATE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
                        case 'H': unit = Calendar.HOUR; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
                        case 'M': unit = Calendar.MINUTE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
                        case 'S': unit = Calendar.SECOND; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
                        default: throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
                    c.add(unit, sign * number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
                    start = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
            } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
                // Form 2: [yyyy/mm/dd] [HH:MM:SS]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
                String date = null, time = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
                if (len == 19) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                    date = s.substring(0, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
                    time = s.substring(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
                    if (s.charAt(10) != ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
                        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                } else if (len == 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                    date = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                } else if (len == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
                    time = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
                    throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
                if (date != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
                    if (date.matches("\\d\\d\\d\\d\\/\\d\\d\\/\\d\\d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                        c.set(Integer.valueOf(date.substring(0, 4)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                                Integer.valueOf(date.substring(5, 7))-1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
                                Integer.valueOf(date.substring(8, 10)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
                        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
                if (time != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
                    if (time.matches("\\d\\d:\\d\\d:\\d\\d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
                        c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(time.substring(0, 2)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
                        c.set(Calendar.MINUTE, Integer.valueOf(time.substring(0, 2)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
                        c.set(Calendar.SECOND, Integer.valueOf(time.substring(0, 2)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
                        c.set(Calendar.MILLISECOND, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
                        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
        return c.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
    /**
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3488
     * Match a command (may be abbreviated) with a command set.
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3489
     * @param s the command provided
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3490
     * @param list the legal command set. If there is a null, commands after it
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3491
     * are regarded experimental, which means they are supported but their
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3492
     * existence should not be revealed to user.
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3493
     * @return the position of a single match, or -1 if none matched
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3494
     * @throws Exception if s is ambiguous
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3495
     */
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3496
    private static int oneOf(String s, String... list) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3497
        int[] match = new int[list.length];
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3498
        int nmatch = 0;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3499
        int experiment = Integer.MAX_VALUE;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3500
        for (int i = 0; i<list.length; i++) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3501
            String one = list[i];
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3502
            if (one == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3503
                experiment = i;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3504
                continue;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3505
            }
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3506
            if (one.toLowerCase(Locale.ENGLISH)
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3507
                    .startsWith(s.toLowerCase(Locale.ENGLISH))) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3508
                match[nmatch++] = i;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3509
            } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3510
                StringBuffer sb = new StringBuffer();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3511
                boolean first = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3512
                for (char c: one.toCharArray()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3513
                    if (first) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3514
                        sb.append(c);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3515
                        first = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3516
                    } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3517
                        if (!Character.isLowerCase(c)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3518
                            sb.append(c);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3519
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3520
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3521
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3522
                if (sb.toString().equalsIgnoreCase(s)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3523
                    match[nmatch++] = i;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3524
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3525
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3526
        }
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3527
        if (nmatch == 0) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3528
            return -1;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3529
        } else if (nmatch == 1) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3530
            return match[0];
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3531
        } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3532
            // If multiple matches is in experimental commands, ignore them
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3533
            if (match[1] > experiment) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3534
                return match[0];
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3535
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3536
            StringBuffer sb = new StringBuffer();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3537
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3538
                ("command.{0}.is.ambiguous."));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3539
            Object[] source = {s};
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3540
            sb.append(form.format(source));
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3541
            sb.append("\n    ");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3542
            for (int i=0; i<nmatch && match[i]<experiment; i++) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3543
                sb.append(' ');
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3544
                sb.append(list[match[i]]);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3545
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3546
            throw new Exception(sb.toString());
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3547
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3548
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3549
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3550
    /**
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3551
     * Create a GeneralName object from known types
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3552
     * @param t one of 5 known types
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3553
     * @param v value
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3554
     * @return which one
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3555
     */
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3556
    private GeneralName createGeneralName(String t, String v)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3557
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3558
        GeneralNameInterface gn;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3559
        int p = oneOf(t, "EMAIL", "URI", "DNS", "IP", "OID");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3560
        if (p < 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3561
            throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3562
                    "Unrecognized.GeneralName.type.") + t);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3563
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3564
        switch (p) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3565
            case 0: gn = new RFC822Name(v); break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3566
            case 1: gn = new URIName(v); break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3567
            case 2: gn = new DNSName(v); break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3568
            case 3: gn = new IPAddressName(v); break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3569
            default: gn = new OIDName(v); break; //4
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3570
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3571
        return new GeneralName(gn);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3572
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3573
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3574
    private static final String[] extSupported = {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3575
                        "BasicConstraints",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3576
                        "KeyUsage",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3577
                        "ExtendedKeyUsage",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3578
                        "SubjectAlternativeName",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3579
                        "IssuerAlternativeName",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3580
                        "SubjectInfoAccess",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3581
                        "AuthorityInfoAccess",
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3582
                        null,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3583
                        "CRLDistributionPoints",
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3584
    };
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3585
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3586
    private ObjectIdentifier findOidForExtName(String type)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3587
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3588
        switch (oneOf(type, extSupported)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3589
            case 0: return PKIXExtensions.BasicConstraints_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3590
            case 1: return PKIXExtensions.KeyUsage_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3591
            case 2: return PKIXExtensions.ExtendedKeyUsage_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3592
            case 3: return PKIXExtensions.SubjectAlternativeName_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3593
            case 4: return PKIXExtensions.IssuerAlternativeName_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3594
            case 5: return PKIXExtensions.SubjectInfoAccess_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3595
            case 6: return PKIXExtensions.AuthInfoAccess_Id;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3596
            case 8: return PKIXExtensions.CRLDistributionPoints_Id;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3597
            default: return new ObjectIdentifier(type);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3598
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3599
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3600
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3601
    /**
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3602
     * Create X509v3 extensions from a string representation. Note that the
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3603
     * SubjectKeyIdentifierExtension will always be created non-critical besides
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3604
     * the extension requested in the <code>extstr</code> argument.
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3605
     *
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3606
     * @param reqex the requested extensions, can be null, used for -gencert
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3607
     * @param ext the original extensions, can be null, used for -selfcert
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3608
     * @param extstrs -ext values, Read keytool doc
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3609
     * @param pkey the public key for the certificate
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3610
     * @param akey the public key for the authority (issuer)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3611
     * @return the created CertificateExtensions
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3612
     */
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3613
    private CertificateExtensions createV3Extensions(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3614
            CertificateExtensions reqex,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3615
            CertificateExtensions ext,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3616
            List <String> extstrs,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3617
            PublicKey pkey,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3618
            PublicKey akey) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3619
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3620
        if (ext != null && reqex != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3621
            // This should not happen
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3622
            throw new Exception("One of request and original should be null.");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3623
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3624
        if (ext == null) ext = new CertificateExtensions();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3625
        try {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3626
            // name{:critical}{=value}
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3627
            // Honoring requested extensions
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3628
            if (reqex != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3629
                for(String extstr: extstrs) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3630
                    if (extstr.toLowerCase(Locale.ENGLISH).startsWith("honored=")) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3631
                        List<String> list = Arrays.asList(
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3632
                                extstr.toLowerCase(Locale.ENGLISH).substring(8).split(","));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3633
                        // First check existence of "all"
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3634
                        if (list.contains("all")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3635
                            ext = reqex;    // we know ext was null
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3636
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3637
                        // one by one for others
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3638
                        for (String item: list) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3639
                            if (item.equals("all")) continue;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3640
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3641
                            // add or remove
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3642
                            boolean add = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3643
                            // -1, unchanged, 0 crtical, 1 non-critical
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3644
                            int action = -1;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3645
                            String type = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3646
                            if (item.startsWith("-")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3647
                                add = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3648
                                type = item.substring(1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3649
                            } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3650
                                int colonpos = item.indexOf(':');
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3651
                                if (colonpos >= 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3652
                                    type = item.substring(0, colonpos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3653
                                    action = oneOf(item.substring(colonpos+1),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3654
                                            "critical", "non-critical");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3655
                                    if (action == -1) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3656
                                        throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3657
                                            ("Illegal.value.") + item);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3658
                                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3659
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3660
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3661
                            String n = reqex.getNameByOid(findOidForExtName(type));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3662
                            if (add) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  3663
                                Extension e = reqex.get(n);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3664
                                if (!e.isCritical() && action == 0
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3665
                                        || e.isCritical() && action == 1) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3666
                                    e = Extension.newExtension(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3667
                                            e.getExtensionId(),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3668
                                            !e.isCritical(),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3669
                                            e.getExtensionValue());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3670
                                    ext.set(n, e);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3671
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3672
                            } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3673
                                ext.delete(n);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3674
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3675
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3676
                        break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3677
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3678
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3679
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3680
            for(String extstr: extstrs) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3681
                String name, value;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3682
                boolean isCritical = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3683
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3684
                int eqpos = extstr.indexOf('=');
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3685
                if (eqpos >= 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3686
                    name = extstr.substring(0, eqpos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3687
                    value = extstr.substring(eqpos+1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3688
                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3689
                    name = extstr;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3690
                    value = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3691
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3692
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3693
                int colonpos = name.indexOf(':');
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3694
                if (colonpos >= 0) {
3316
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  3695
                    if (oneOf(name.substring(colonpos+1), "critical") == 0) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3696
                        isCritical = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3697
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3698
                    name = name.substring(0, colonpos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3699
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3700
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3701
                if (name.equalsIgnoreCase("honored")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3702
                    continue;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3703
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3704
                int exttype = oneOf(name, extSupported);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3705
                switch (exttype) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3706
                    case 0:     // BC
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3707
                        int pathLen = -1;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3708
                        boolean isCA = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3709
                        if (value == null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3710
                            isCA = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3711
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3712
                            try {   // the abbr format
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3713
                                pathLen = Integer.parseInt(value);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3714
                                isCA = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3715
                            } catch (NumberFormatException ufe) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3716
                                // ca:true,pathlen:1
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3717
                                for (String part: value.split(",")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3718
                                    String[] nv = part.split(":");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3719
                                    if (nv.length != 2) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3720
                                        throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3721
                                                ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3722
                                    } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3723
                                        if (nv[0].equalsIgnoreCase("ca")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3724
                                            isCA = Boolean.parseBoolean(nv[1]);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3725
                                        } else if (nv[0].equalsIgnoreCase("pathlen")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3726
                                            pathLen = Integer.parseInt(nv[1]);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3727
                                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3728
                                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3729
                                                ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3730
                                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3731
                                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3732
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3733
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3734
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3735
                        ext.set(BasicConstraintsExtension.NAME,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3736
                                new BasicConstraintsExtension(isCritical, isCA,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3737
                                pathLen));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3738
                        break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3739
                    case 1:     // KU
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3740
                        if(value != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3741
                            boolean[] ok = new boolean[9];
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3742
                            for (String s: value.split(",")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3743
                                int p = oneOf(s,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3744
                                       "digitalSignature",  // (0),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3745
                                       "nonRepudiation",    // (1)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3746
                                       "keyEncipherment",   // (2),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3747
                                       "dataEncipherment",  // (3),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3748
                                       "keyAgreement",      // (4),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3749
                                       "keyCertSign",       // (5),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3750
                                       "cRLSign",           // (6),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3751
                                       "encipherOnly",      // (7),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3752
                                       "decipherOnly",      // (8)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3753
                                       "contentCommitment"  // also (1)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3754
                                       );
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3755
                                if (p < 0) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3756
                                    throw new Exception(rb.getString("Unknown.keyUsage.type.") + s);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3757
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3758
                                if (p == 9) p = 1;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3759
                                ok[p] = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3760
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3761
                            KeyUsageExtension kue = new KeyUsageExtension(ok);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3762
                            // The above KeyUsageExtension constructor does not
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3763
                            // allow isCritical value, so...
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3764
                            ext.set(KeyUsageExtension.NAME, Extension.newExtension(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3765
                                    kue.getExtensionId(),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3766
                                    isCritical,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3767
                                    kue.getExtensionValue()));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3768
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3769
                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3770
                                    ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3771
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3772
                        break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3773
                    case 2:     // EKU
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3774
                        if(value != null) {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
  3775
                            Vector<ObjectIdentifier> v = new Vector<>();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3776
                            for (String s: value.split(",")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3777
                                int p = oneOf(s,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3778
                                        "anyExtendedKeyUsage",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3779
                                        "serverAuth",       //1
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3780
                                        "clientAuth",       //2
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3781
                                        "codeSigning",      //3
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3782
                                        "emailProtection",  //4
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3783
                                        "",                 //5
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3784
                                        "",                 //6
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3785
                                        "",                 //7
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3786
                                        "timeStamping",     //8
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3787
                                        "OCSPSigning"       //9
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3788
                                       );
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3789
                                if (p < 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3790
                                    try {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3791
                                        v.add(new ObjectIdentifier(s));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3792
                                    } catch (Exception e) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3793
                                        throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3794
                                                "Unknown.extendedkeyUsage.type.") + s);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3795
                                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3796
                                } else if (p == 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3797
                                    v.add(new ObjectIdentifier("2.5.29.37.0"));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3798
                                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3799
                                    v.add(new ObjectIdentifier("1.3.6.1.5.5.7.3." + p));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3800
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3801
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3802
                            ext.set(ExtendedKeyUsageExtension.NAME,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3803
                                    new ExtendedKeyUsageExtension(isCritical, v));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3804
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3805
                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3806
                                    ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3807
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3808
                        break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3809
                    case 3:     // SAN
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3810
                    case 4:     // IAN
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3811
                        if(value != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3812
                            String[] ps = value.split(",");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3813
                            GeneralNames gnames = new GeneralNames();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3814
                            for(String item: ps) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3815
                                colonpos = item.indexOf(':');
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3816
                                if (colonpos < 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3817
                                    throw new Exception("Illegal item " + item + " in " + extstr);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3818
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3819
                                String t = item.substring(0, colonpos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3820
                                String v = item.substring(colonpos+1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3821
                                gnames.add(createGeneralName(t, v));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3822
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3823
                            if (exttype == 3) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3824
                                ext.set(SubjectAlternativeNameExtension.NAME,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3825
                                        new SubjectAlternativeNameExtension(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3826
                                            isCritical, gnames));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3827
                            } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3828
                                ext.set(IssuerAlternativeNameExtension.NAME,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3829
                                        new IssuerAlternativeNameExtension(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3830
                                            isCritical, gnames));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3831
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3832
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3833
                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3834
                                    ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3835
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3836
                        break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3837
                    case 5:     // SIA, always non-critical
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3838
                    case 6:     // AIA, always non-critical
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3839
                        if (isCritical) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3840
                            throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3841
                                    "This.extension.cannot.be.marked.as.critical.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3842
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3843
                        if(value != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3844
                            List<AccessDescription> accessDescriptions =
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
  3845
                                    new ArrayList<>();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3846
                            String[] ps = value.split(",");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3847
                            for(String item: ps) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3848
                                colonpos = item.indexOf(':');
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3849
                                int colonpos2 = item.indexOf(':', colonpos+1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3850
                                if (colonpos < 0 || colonpos2 < 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3851
                                    throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3852
                                            ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3853
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3854
                                String m = item.substring(0, colonpos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3855
                                String t = item.substring(colonpos+1, colonpos2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3856
                                String v = item.substring(colonpos2+1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3857
                                int p = oneOf(m,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3858
                                        "",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3859
                                        "ocsp",         //1
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3860
                                        "caIssuers",    //2
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3861
                                        "timeStamping", //3
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3862
                                        "",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3863
                                        "caRepository"  //5
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3864
                                        );
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3865
                                ObjectIdentifier oid;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3866
                                if (p < 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3867
                                    try {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3868
                                        oid = new ObjectIdentifier(m);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3869
                                    } catch (Exception e) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3870
                                        throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3871
                                                "Unknown.AccessDescription.type.") + m);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3872
                                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3873
                                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3874
                                    oid = new ObjectIdentifier("1.3.6.1.5.5.7.48." + p);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3875
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3876
                                accessDescriptions.add(new AccessDescription(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3877
                                        oid, createGeneralName(t, v)));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3878
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3879
                            if (exttype == 5) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3880
                                ext.set(SubjectInfoAccessExtension.NAME,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3881
                                        new SubjectInfoAccessExtension(accessDescriptions));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3882
                            } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3883
                                ext.set(AuthorityInfoAccessExtension.NAME,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3884
                                        new AuthorityInfoAccessExtension(accessDescriptions));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3885
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3886
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3887
                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3888
                                    ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3889
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3890
                        break;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3891
                    case 8: // CRL, experimental, only support 1 distributionpoint
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3892
                        if(value != null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3893
                            String[] ps = value.split(",");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3894
                            GeneralNames gnames = new GeneralNames();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3895
                            for(String item: ps) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3896
                                colonpos = item.indexOf(':');
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3897
                                if (colonpos < 0) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3898
                                    throw new Exception("Illegal item " + item + " in " + extstr);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3899
                                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3900
                                String t = item.substring(0, colonpos);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3901
                                String v = item.substring(colonpos+1);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3902
                                gnames.add(createGeneralName(t, v));
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3903
                            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3904
                            ext.set(CRLDistributionPointsExtension.NAME,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3905
                                    new CRLDistributionPointsExtension(
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3906
                                        isCritical, Collections.singletonList(
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3907
                                        new DistributionPoint(gnames, null, null))));
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3908
                        } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3909
                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3910
                                    ("Illegal.value.") + extstr);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3911
                        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3912
                        break;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3913
                    case -1:
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3914
                        ObjectIdentifier oid = new ObjectIdentifier(name);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3915
                        byte[] data = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3916
                        if (value != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3917
                            data = new byte[value.length() / 2 + 1];
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3918
                            int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3919
                            for (char c: value.toCharArray()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3920
                                int hex;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3921
                                if (c >= '0' && c <= '9') {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3922
                                    hex = c - '0' ;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3923
                                } else if (c >= 'A' && c <= 'F') {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3924
                                    hex = c - 'A' + 10;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3925
                                } else if (c >= 'a' && c <= 'f') {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3926
                                    hex = c - 'a' + 10;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3927
                                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3928
                                    continue;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3929
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3930
                                if (pos % 2 == 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3931
                                    data[pos/2] = (byte)(hex << 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3932
                                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3933
                                    data[pos/2] += hex;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3934
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3935
                                pos++;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3936
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3937
                            if (pos % 2 != 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3938
                                throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3939
                                        "Odd.number.of.hex.digits.found.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3940
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3941
                            data = Arrays.copyOf(data, pos/2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3942
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3943
                            data = new byte[0];
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3944
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3945
                        ext.set(oid.toString(), new Extension(oid, isCritical,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3946
                                new DerValue(DerValue.tag_OctetString, data)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3947
                                        .toByteArray()));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3948
                        break;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3949
                    default:
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3950
                        throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3951
                                "Unknown.extension.type.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3952
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3953
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3954
            // always non-critical
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3955
            ext.set(SubjectKeyIdentifierExtension.NAME,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3956
                    new SubjectKeyIdentifierExtension(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3957
                        new KeyIdentifier(pkey).getIdentifier()));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3958
            if (akey != null && !pkey.equals(akey)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3959
                ext.set(AuthorityKeyIdentifierExtension.NAME,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3960
                        new AuthorityKeyIdentifierExtension(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3961
                        new KeyIdentifier(akey), null, null));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3962
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3963
        } catch(IOException e) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3964
            throw new RuntimeException(e);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3965
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3966
        return ext;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3967
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3968
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3969
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
     * Prints the usage of this tool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
    private void usage() {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3973
        if (command != null) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3974
            System.err.println("keytool " + command +
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3975
                    rb.getString(".OPTION."));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3976
            System.err.println();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3977
            System.err.println(rb.getString(command.description));
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3978
            System.err.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3979
            System.err.println(rb.getString("Options."));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3980
            System.err.println();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3981
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3982
            // Left and right sides of the options list
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3983
            String[] left = new String[command.options.length];
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3984
            String[] right = new String[command.options.length];
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3985
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3986
            // Check if there's an unknown option
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3987
            boolean found = false;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3988
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3989
            // Length of left side of options list
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3990
            int lenLeft = 0;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3991
            for (int j=0; j<left.length; j++) {
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
  3992
                Option opt = command.options[j];
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
  3993
                left[j] = opt.toString();
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
  3994
                if (opt.arg != null) left[j] += " " + opt.arg;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
  3995
                if (left[j].length() > lenLeft) {
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
  3996
                    lenLeft = left[j].length();
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3997
                }
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
  3998
                right[j] = rb.getString(opt.description);
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3999
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4000
            for (int j=0; j<left.length; j++) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4001
                System.err.printf(" %-" + lenLeft + "s  %s\n",
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4002
                        left[j], right[j]);
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4003
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4004
            System.err.println();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4005
            System.err.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4006
                    "Use.keytool.help.for.all.available.commands"));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4007
        } else {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4008
            System.err.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4009
                    "Key.and.Certificate.Management.Tool"));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4010
            System.err.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4011
            System.err.println(rb.getString("Commands."));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4012
            System.err.println();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4013
            for (Command c: Command.values()) {
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4014
                if (c == KEYCLONE) break;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4015
                System.err.printf(" %-20s%s\n", c, rb.getString(c.description));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4016
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4017
            System.err.println();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4018
            System.err.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4019
                    "Use.keytool.command.name.help.for.usage.of.command.name"));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4020
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
    private void tinyHelp() {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4024
        usage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
            throw new RuntimeException("NO BIG ERROR, SORRY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
    private void errorNeedArgument(String flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
        Object[] source = {flag};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
        System.err.println(new MessageFormat(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4035
                rb.getString("Command.option.flag.needs.an.argument.")).format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
        tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
    }
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4038
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4039
    private char[] getPass(String modifier, String arg) {
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
  4040
        char[] output = KeyStoreUtil.getPassWithModifier(modifier, arg, rb);
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4041
        if (output != null) return output;
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4042
        tinyHelp();
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4043
        return null;    // Useless, tinyHelp() already exits.
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4044
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
// This class is exactly the same as com.sun.tools.javac.util.Pair,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
// it's copied here since the original one is not included in JRE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
class Pair<A, B> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
    public final A fst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
    public final B snd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
    public Pair(A fst, B snd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
        this.fst = fst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
        this.snd = snd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
        return "Pair[" + fst + "," + snd + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
            other instanceof Pair &&
10051
b96a8c7fefef 7041252: Use j.u.Objects.equals in security classes
darcy
parents: 9011
diff changeset
  4066
            Objects.equals(fst, ((Pair)other).fst) &&
b96a8c7fefef 7041252: Use j.u.Objects.equals in security classes
darcy
parents: 9011
diff changeset
  4067
            Objects.equals(snd, ((Pair)other).snd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
        if (fst == null) return (snd == null) ? 0 : snd.hashCode() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
        else if (snd == null) return fst.hashCode() + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
        else return fst.hashCode() * 17 + snd.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
    }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4075
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4076
    public static <A,B> Pair<A,B> of(A a, B b) {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
  4077
        return new Pair<>(a,b);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4078
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
}
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4080