src/java.base/share/classes/sun/security/tools/keytool/Main.java
author weijun
Thu, 14 Dec 2017 20:19:34 +0800
changeset 48333 f47c18852172
parent 48216 e3b6cb90d7ce
child 48543 7067fe4e054e
child 48572 1820a65c4e59
permissions -rw-r--r--
8192988: keytool should support -storepasswd for pkcs12 keystores Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43238
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents: 41960
diff changeset
     2
 * Copyright (c) 1997, 2017, 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.*;
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
    29
import java.nio.file.Files;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
    30
import java.nio.file.Paths;
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
    31
import java.security.CodeSigner;
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
    32
import java.security.CryptoPrimitive;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.Signature;
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
    40
import java.security.Timestamp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.UnrecoverableEntryException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.UnrecoverableKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.Principal;
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;
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 30374
diff changeset
    50
import java.security.cert.URICertStoreParameters;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 30374
diff changeset
    51
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 30374
diff changeset
    52
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.text.Collator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.*;
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
    56
import java.util.jar.JarEntry;
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
    57
import java.util.jar.JarFile;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    58
import java.math.BigInteger;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    59
import java.net.URI;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.net.URLClassLoader;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    62
import java.security.cert.CertStore;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    63
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    64
import java.security.cert.X509CRL;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    65
import java.security.cert.X509CRLEntry;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    66
import java.security.cert.X509CRLSelector;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
    67
import javax.security.auth.x500.X500Principal;
16020
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 14421
diff changeset
    68
import java.util.Base64;
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
    69
27038
64cb6608f8a6 8029659: Keytool, print key algorithm of certificate or key entry
weijun
parents: 25859
diff changeset
    70
import sun.security.util.KeyUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
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
    72
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
    73
import sun.security.pkcs10.PKCS10Attribute;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import sun.security.provider.X509Factory;
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 30374
diff changeset
    75
import sun.security.provider.certpath.ssl.SSLServerCertStore;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import sun.security.util.Password;
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47420
diff changeset
    77
import sun.security.util.SecurityProviderConstants;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import javax.crypto.KeyGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import javax.crypto.SecretKey;
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
    80
import javax.crypto.SecretKeyFactory;
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
    81
import javax.crypto.spec.PBEKeySpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
    83
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
    84
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
    85
import sun.security.tools.PathList;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
    86
import sun.security.util.DerValue;
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
    87
import sun.security.util.Pem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
import static java.security.KeyStore.*;
41960
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 40393
diff changeset
    91
import java.security.Security;
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
    92
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
    93
import static sun.security.tools.keytool.Main.Option.*;
41960
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 40393
diff changeset
    94
import sun.security.util.DisabledAlgorithmConstraints;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * This tool manages keystores.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @see java.security.KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @see sun.security.provider.KeyProtector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @see sun.security.provider.JavaKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 */
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
   108
public final class Main {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
   110
    private static final byte[] CRLF = new byte[] {'\r', '\n'};
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
   111
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private boolean debug = false;
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   113
    private Command command = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private String sigAlgName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private String keyAlgName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private boolean verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private int keysize = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private boolean rfc = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private long validity = (long)90;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private String alias = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private String dname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private String dest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private String filename = null;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   124
    private String infilename = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   125
    private String outfilename = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private String srcksfname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    // User-specified providers are added before any command is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // However, they are not removed before the end of the main() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // If you're calling KeyTool.main() directly in your own Java program,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // please programtically add any providers you need and do not specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // them through the command line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private Set<Pair <String, String>> providers = null;
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   135
    private Set<Pair <String, String>> providerClasses = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private String storetype = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private String srcProviderName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private String providerName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private String pathlist = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private char[] storePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private char[] storePassNew = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private char[] keyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private char[] keyPassNew = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private char[] newPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private char[] destKeyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private char[] srckeyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private String ksfname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private File ksfile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    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
   150
    private String sslserver = null;
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
   151
    private String jarfile = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private KeyStore keyStore = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private boolean token = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private boolean nullStream = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private boolean kssave = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private boolean noprompt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private boolean trustcacerts = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private boolean protectedPath = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private boolean srcprotectedPath = false;
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   160
    private boolean cacerts = false;
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   161
    private boolean nowarn = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private CertificateFactory cf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private KeyStore caks = null; // "cacerts" keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private char[] srcstorePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private String srcstoretype = null;
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
   166
    private Set<char[]> passwords = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private String startDate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
   169
    private List<String> ids = new ArrayList<>();   // used in GENCRL
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
   170
    private List<String> v3ext = new ArrayList<>();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   171
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   172
    // In-place importkeystore is special.
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   173
    // A backup is needed, and no need to prompt for deststorepass.
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   174
    private boolean inplaceImport = false;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   175
    private String inplaceBackupName = null;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   176
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   177
    // Warnings on weak algorithms etc
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   178
    private List<String> weakWarnings = new ArrayList<>();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   179
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   180
    private static final DisabledAlgorithmConstraints DISABLED_CHECK =
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   181
            new DisabledAlgorithmConstraints(
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   182
                    DisabledAlgorithmConstraints.PROPERTY_CERTPATH_DISABLED_ALGS);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   183
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   184
    private static final Set<CryptoPrimitive> SIG_PRIMITIVE_SET = Collections
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   185
            .unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE));
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   186
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   187
    enum Command {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   188
        CERTREQ("Generates.a.certificate.request",
4918
13ed75166139 6925639: keytool -genkeypair -help missing dname option
weijun
parents: 4819
diff changeset
   189
            ALIAS, SIGALG, FILEOUT, KEYPASS, KEYSTORE, DNAME,
45475
19aefb1b6fc6 8181501: KeyTool help does not show -ext for -certreq
weijun
parents: 44597
diff changeset
   190
            EXT, STOREPASS, STORETYPE, PROVIDERNAME, ADDPROVIDER,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   191
            PROVIDERCLASS, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   192
        CHANGEALIAS("Changes.an.entry.s.alias",
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   193
            ALIAS, DESTALIAS, KEYPASS, KEYSTORE, CACERTS, STOREPASS,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   194
            STORETYPE, PROVIDERNAME, ADDPROVIDER, PROVIDERCLASS,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   195
            PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   196
        DELETE("Deletes.an.entry",
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   197
            ALIAS, KEYSTORE, CACERTS, STOREPASS, STORETYPE,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   198
            PROVIDERNAME, ADDPROVIDER, PROVIDERCLASS,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   199
            PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   200
        EXPORTCERT("Exports.certificate",
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   201
            RFC, ALIAS, FILEOUT, KEYSTORE, CACERTS, STOREPASS,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   202
            STORETYPE, PROVIDERNAME, ADDPROVIDER, PROVIDERCLASS,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   203
            PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   204
        GENKEYPAIR("Generates.a.key.pair",
4918
13ed75166139 6925639: keytool -genkeypair -help missing dname option
weijun
parents: 4819
diff changeset
   205
            ALIAS, KEYALG, KEYSIZE, SIGALG, DESTALIAS, DNAME,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   206
            STARTDATE, EXT, VALIDITY, KEYPASS, KEYSTORE,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   207
            STOREPASS, STORETYPE, PROVIDERNAME, ADDPROVIDER,
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   208
            PROVIDERCLASS, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   209
        GENSECKEY("Generates.a.secret.key",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   210
            ALIAS, KEYPASS, KEYALG, KEYSIZE, KEYSTORE,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   211
            STOREPASS, STORETYPE, PROVIDERNAME, ADDPROVIDER,
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   212
            PROVIDERCLASS, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   213
        GENCERT("Generates.certificate.from.a.certificate.request",
4918
13ed75166139 6925639: keytool -genkeypair -help missing dname option
weijun
parents: 4819
diff changeset
   214
            RFC, INFILE, OUTFILE, ALIAS, SIGALG, DNAME,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   215
            STARTDATE, EXT, VALIDITY, KEYPASS, KEYSTORE,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   216
            STOREPASS, STORETYPE, PROVIDERNAME, ADDPROVIDER,
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   217
            PROVIDERCLASS, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   218
        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
   219
            NOPROMPT, TRUSTCACERTS, PROTECTED, ALIAS, FILEIN,
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   220
            KEYPASS, KEYSTORE, CACERTS, STOREPASS, STORETYPE,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   221
            PROVIDERNAME, ADDPROVIDER, PROVIDERCLASS,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   222
            PROVIDERPATH, V),
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
   223
        IMPORTPASS("Imports.a.password",
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
   224
            ALIAS, KEYPASS, KEYALG, KEYSIZE, KEYSTORE,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   225
            STOREPASS, STORETYPE, PROVIDERNAME, ADDPROVIDER,
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   226
            PROVIDERCLASS, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   227
        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
   228
            SRCKEYSTORE, DESTKEYSTORE, SRCSTORETYPE,
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   229
            DESTSTORETYPE, SRCSTOREPASS, DESTSTOREPASS,
38855
4e587173e37e 8151836: keytool -importkeystore -help does not list option -destprotected
weijun
parents: 38439
diff changeset
   230
            SRCPROTECTED, DESTPROTECTED, SRCPROVIDERNAME, DESTPROVIDERNAME,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   231
            SRCALIAS, DESTALIAS, SRCKEYPASS, DESTKEYPASS,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   232
            NOPROMPT, ADDPROVIDER, PROVIDERCLASS, PROVIDERPATH,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   233
            V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   234
        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
   235
            ALIAS, KEYPASS, NEW, KEYSTORE, STOREPASS,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   236
            STORETYPE, PROVIDERNAME, ADDPROVIDER, PROVIDERCLASS,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   237
            PROVIDERPATH, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   238
        LIST("Lists.entries.in.a.keystore",
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   239
            RFC, ALIAS, KEYSTORE, CACERTS, STOREPASS, STORETYPE,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   240
            PROVIDERNAME, ADDPROVIDER, PROVIDERCLASS,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   241
            PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   242
        PRINTCERT("Prints.the.content.of.a.certificate",
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   243
            RFC, FILEIN, SSLSERVER, JARFILE, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   244
        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
   245
            FILEIN, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   246
        PRINTCRL("Prints.the.content.of.a.CRL.file",
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   247
            FILEIN, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   248
        STOREPASSWD("Changes.the.store.password.of.a.keystore",
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   249
            NEW, KEYSTORE, CACERTS, STOREPASS, STORETYPE, PROVIDERNAME,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   250
            ADDPROVIDER, PROVIDERCLASS, PROVIDERPATH, V),
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   251
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   252
        // 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
   253
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   254
        KEYCLONE("Clones.a.key.entry",
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   255
            ALIAS, DESTALIAS, KEYPASS, NEW, STORETYPE,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   256
            KEYSTORE, STOREPASS, PROVIDERNAME, ADDPROVIDER,
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   257
            PROVIDERCLASS, PROVIDERPATH, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   258
        SELFCERT("Generates.a.self.signed.certificate",
45475
19aefb1b6fc6 8181501: KeyTool help does not show -ext for -certreq
weijun
parents: 44597
diff changeset
   259
            ALIAS, SIGALG, DNAME, STARTDATE, EXT, VALIDITY, KEYPASS,
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   260
            STORETYPE, KEYSTORE, STOREPASS, PROVIDERNAME,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   261
            ADDPROVIDER, PROVIDERCLASS, PROVIDERPATH, V),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   262
        GENCRL("Generates.CRL",
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   263
            RFC, FILEOUT, ID,
45475
19aefb1b6fc6 8181501: KeyTool help does not show -ext for -certreq
weijun
parents: 44597
diff changeset
   264
            ALIAS, SIGALG, KEYPASS, KEYSTORE,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   265
            STOREPASS, STORETYPE, PROVIDERNAME, ADDPROVIDER,
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   266
            PROVIDERCLASS, PROVIDERPATH, V, PROTECTED),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   267
        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
   268
            FILEIN, STORETYPE, KEYSTORE, STOREPASS, PROVIDERNAME,
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   269
            ADDPROVIDER, PROVIDERCLASS, PROVIDERPATH, V);
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   270
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   271
        final String description;
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   272
        final Option[] options;
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   273
        final String name;
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   274
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   275
        String altName;     // "genkey" is altName for "genkeypair"
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   276
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   277
        Command(String d, Option... o) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   278
            description = d;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   279
            options = o;
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   280
            name = "-" + name().toLowerCase(Locale.ENGLISH);
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   281
        }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   282
        @Override
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   283
        public String toString() {
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   284
            return name;
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   285
        }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   286
        public String getAltName() {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   287
            return altName;
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   288
        }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   289
        public void setAltName(String altName) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   290
            this.altName = altName;
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   291
        }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   292
        public static Command getCommand(String cmd) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   293
            for (Command c: Command.values()) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   294
                if (collator.compare(cmd, c.name) == 0
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   295
                        || (c.altName != null
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   296
                            && collator.compare(cmd, c.altName) == 0)) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   297
                    return c;
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   298
                }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   299
            }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   300
            return null;
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   301
        }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   302
    };
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   303
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   304
    static {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   305
        Command.GENKEYPAIR.setAltName("-genkey");
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   306
        Command.IMPORTCERT.setAltName("-import");
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   307
        Command.EXPORTCERT.setAltName("-export");
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   308
        Command.IMPORTPASS.setAltName("-importpassword");
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   309
    }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   310
45839
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   311
    // If an option is allowed multiple times, remember to record it
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   312
    // in the optionsSet.contains() block in parseArgs().
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   313
    enum Option {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   314
        ALIAS("alias", "<alias>", "alias.name.of.the.entry.to.process"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   315
        DESTALIAS("destalias", "<alias>", "destination.alias"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   316
        DESTKEYPASS("destkeypass", "<arg>", "destination.key.password"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   317
        DESTKEYSTORE("destkeystore", "<keystore>", "destination.keystore.name"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   318
        DESTPROTECTED("destprotected", null, "destination.keystore.password.protected"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   319
        DESTPROVIDERNAME("destprovidername", "<name>", "destination.keystore.provider.name"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   320
        DESTSTOREPASS("deststorepass", "<arg>", "destination.keystore.password"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   321
        DESTSTORETYPE("deststoretype", "<type>", "destination.keystore.type"),
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   322
        DNAME("dname", "<name>", "distinguished.name"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   323
        EXT("ext", "<value>", "X.509.extension"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   324
        FILEOUT("file", "<file>", "output.file.name"),
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   325
        FILEIN("file", "<file>", "input.file.name"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   326
        ID("id", "<id:reason>", "Serial.ID.of.cert.to.revoke"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   327
        INFILE("infile", "<file>", "input.file.name"),
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   328
        KEYALG("keyalg", "<alg>", "key.algorithm.name"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   329
        KEYPASS("keypass", "<arg>", "key.password"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   330
        KEYSIZE("keysize", "<size>", "key.bit.size"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   331
        KEYSTORE("keystore", "<keystore>", "keystore.name"),
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   332
        CACERTS("cacerts", null, "access.the.cacerts.keystore"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   333
        NEW("new", "<arg>", "new.password"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   334
        NOPROMPT("noprompt", null, "do.not.prompt"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   335
        OUTFILE("outfile", "<file>", "output.file.name"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   336
        PROTECTED("protected", null, "password.through.protected.mechanism"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   337
        PROVIDERCLASS("providerclass", "<class>\n[-providerarg <arg>]", "provider.class.option"),
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   338
        ADDPROVIDER("addprovider", "<name>\n[-providerarg <arg>]", "addprovider.option"),
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   339
        PROVIDERNAME("providername", "<name>", "provider.name"),
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   340
        PROVIDERPATH("providerpath", "<list>", "provider.classpath"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   341
        RFC("rfc", null, "output.in.RFC.style"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   342
        SIGALG("sigalg", "<alg>", "signature.algorithm.name"),
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   343
        SRCALIAS("srcalias", "<alias>", "source.alias"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   344
        SRCKEYPASS("srckeypass", "<arg>", "source.key.password"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   345
        SRCKEYSTORE("srckeystore", "<keystore>", "source.keystore.name"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   346
        SRCPROTECTED("srcprotected", null, "source.keystore.password.protected"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   347
        SRCPROVIDERNAME("srcprovidername", "<name>", "source.keystore.provider.name"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   348
        SRCSTOREPASS("srcstorepass", "<arg>", "source.keystore.password"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   349
        SRCSTORETYPE("srcstoretype", "<type>", "source.keystore.type"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   350
        SSLSERVER("sslserver", "<server[:port]>", "SSL.server.host.and.port"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   351
        JARFILE("jarfile", "<file>", "signed.jar.file"),
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   352
        STARTDATE("startdate", "<date>", "certificate.validity.start.date.time"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   353
        STOREPASS("storepass", "<arg>", "keystore.password"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   354
        STORETYPE("storetype", "<type>", "keystore.type"),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   355
        TRUSTCACERTS("trustcacerts", null, "trust.certificates.from.cacerts"),
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   356
        V("v", null, "verbose.output"),
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   357
        VALIDITY("validity", "<days>", "validity.number.of.days");
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   358
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   359
        final String name, arg, description;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   360
        Option(String name, String arg, String description) {
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   361
            this.name = name;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   362
            this.arg = arg;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   363
            this.description = description;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   364
        }
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   365
        @Override
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   366
        public String toString() {
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   367
            return "-" + name;
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   368
        }
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   369
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private static final String NONE = "NONE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private static final String P11KEYSTORE = "PKCS11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private static final String P12KEYSTORE = "PKCS12";
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   374
    private static final String keyAlias = "mykey";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    // for i18n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    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
   378
        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
   379
            "sun.security.tools.keytool.Resources");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private static final Collator collator = Collator.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // this is for case insensitive string comparisons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        collator.setStrength(Collator.PRIMARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
   386
    private Main() { }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    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
   389
        Main kt = new Main();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        kt.run(args, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    private void run(String[] args, PrintStream out) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        try {
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   395
            args = parseArgs(args);
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   396
            if (command != null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   397
                doCommands(out);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   398
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        } catch (Exception e) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   400
            System.out.println(rb.getString("keytool.error.") + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (!debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        } finally {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   410
            printWeakWarnings(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            for (char[] pass : passwords) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                if (pass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    Arrays.fill(pass, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    pass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if (ksStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                ksStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Parse command line arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   427
    String[] parseArgs(String[] args) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        int i=0;
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   430
        boolean help = args.length == 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   432
        String confFile = null;
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   433
45839
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   434
        // Records all commands and options set. Used to check dups.
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   435
        Set<String> optionsSet = new HashSet<>();
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   436
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   437
        for (i=0; i < args.length; i++) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   438
            String flags = args[i];
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   439
            if (flags.startsWith("-")) {
45839
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   440
                String lowerFlags = flags.toLowerCase(Locale.ROOT);
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   441
                if (optionsSet.contains(lowerFlags)) {
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   442
                    switch (lowerFlags) {
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   443
                        case "-ext":
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   444
                        case "-id":
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   445
                        case "-provider":
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   446
                        case "-addprovider":
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   447
                        case "-providerclass":
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   448
                        case "-providerarg":
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   449
                            // These options are allowed multiple times
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   450
                            break;
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   451
                        default:
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   452
                            weakWarnings.add(String.format(
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   453
                                    rb.getString("option.1.set.twice"),
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   454
                                    lowerFlags));
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   455
                    }
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   456
                } else {
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   457
                    optionsSet.add(lowerFlags);
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   458
                }
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   459
                if (collator.compare(flags, "-conf") == 0) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   460
                    if (i == args.length - 1) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   461
                        errorNeedArgument(flags);
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   462
                    }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   463
                    confFile = args[++i];
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   464
                } else {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   465
                    Command c = Command.getCommand(flags);
45839
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   466
                    if (c != null) {
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   467
                        if (command == null) {
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   468
                            command = c;
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   469
                        } else {
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   470
                            throw new Exception(String.format(
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   471
                                    rb.getString("multiple.commands.1.2"),
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   472
                                    command.name, c.name));
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   473
                        }
6df5e24443fc 8183509: keytool should not allow multiple commands
weijun
parents: 45475
diff changeset
   474
                    }
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   475
                }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   476
            }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   477
        }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   478
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   479
        if (confFile != null && command != null) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   480
            args = KeyStoreUtil.expandArgs("keytool", confFile,
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   481
                    command.toString(),
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   482
                    command.getAltName(), args);
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   483
        }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   484
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   485
        debug = Arrays.stream(args).anyMatch(
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   486
                x -> collator.compare(x, "-debug") == 0);
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   487
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   488
        if (debug) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   489
            // No need to localize debug output
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   490
            System.out.println("Command line args: " +
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   491
                    Arrays.toString(args));
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   492
        }
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   493
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        for (i=0; (i < args.length) && args[i].startsWith("-"); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            String flags = args[i];
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   497
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   498
            // Check if the last option needs an arg
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   499
            if (i == args.length - 1) {
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   500
                for (Option option: Option.values()) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   501
                    // 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
   502
                    if (collator.compare(flags, option.toString()) == 0) {
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
   503
                        if (option.arg != null) errorNeedArgument(flags);
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   504
                        break;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   505
                    }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   506
                }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   507
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   508
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            /*
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   510
             * Check modifiers
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   511
             */
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   512
            String modifier = null;
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   513
            int pos = flags.indexOf(':');
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   514
            if (pos > 0) {
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   515
                modifier = flags.substring(pos+1);
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   516
                flags = flags.substring(0, pos);
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   517
            }
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   518
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   519
            /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
             * command modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
             */
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   522
            Command c = Command.getCommand(flags);
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   523
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   524
            if (c != null) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   525
                command = c;
44597
98f0c39ce3a8 8172422: jarsigner needs to understand -?
weijun
parents: 44419
diff changeset
   526
            } else if (collator.compare(flags, "-help") == 0 ||
98f0c39ce3a8 8172422: jarsigner needs to understand -?
weijun
parents: 44419
diff changeset
   527
                    collator.compare(flags, "-h") == 0 ||
98f0c39ce3a8 8172422: jarsigner needs to understand -?
weijun
parents: 44419
diff changeset
   528
                    collator.compare(flags, "-?") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   529
                help = true;
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   530
            } else if (collator.compare(flags, "-conf") == 0) {
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   531
                i++;
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   532
            } else if (collator.compare(flags, "-nowarn") == 0) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
   533
                nowarn = true;
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   534
            } else if (collator.compare(flags, "-keystore") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   535
                ksfname = args[++i];
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   536
                if (new File(ksfname).getCanonicalPath().equals(
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   537
                        new File(KeyStoreUtil.getCacerts()).getCanonicalPath())) {
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   538
                    System.err.println(rb.getString("warning.cacerts.option"));
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   539
                }
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   540
            } else if (collator.compare(flags, "-destkeystore") == 0) {
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   541
                ksfname = args[++i];
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   542
            } else if (collator.compare(flags, "-cacerts") == 0) {
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   543
                cacerts = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            } else if (collator.compare(flags, "-storepass") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    collator.compare(flags, "-deststorepass") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   546
                storePass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                passwords.add(storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            } else if (collator.compare(flags, "-storetype") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    collator.compare(flags, "-deststoretype") == 0) {
48216
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   550
                storetype = KeyStoreUtil.niceStoreTypeName(args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            } else if (collator.compare(flags, "-srcstorepass") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   552
                srcstorePass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                passwords.add(srcstorePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            } else if (collator.compare(flags, "-srcstoretype") == 0) {
48216
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   555
                srcstoretype = KeyStoreUtil.niceStoreTypeName(args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            } else if (collator.compare(flags, "-srckeypass") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   557
                srckeyPass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                passwords.add(srckeyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            } else if (collator.compare(flags, "-srcprovidername") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   560
                srcProviderName = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            } else if (collator.compare(flags, "-providername") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    collator.compare(flags, "-destprovidername") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   563
                providerName = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            } else if (collator.compare(flags, "-providerpath") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   565
                pathlist = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            } else if (collator.compare(flags, "-keypass") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   567
                keyPass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                passwords.add(keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            } else if (collator.compare(flags, "-new") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   570
                newPass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                passwords.add(newPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            } else if (collator.compare(flags, "-destkeypass") == 0) {
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
   573
                destKeyPass = getPass(modifier, args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                passwords.add(destKeyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            } else if (collator.compare(flags, "-alias") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    collator.compare(flags, "-srcalias") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   577
                alias = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            } else if (collator.compare(flags, "-dest") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    collator.compare(flags, "-destalias") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   580
                dest = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            } else if (collator.compare(flags, "-dname") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   582
                dname = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            } else if (collator.compare(flags, "-keysize") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   584
                keysize = Integer.parseInt(args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            } else if (collator.compare(flags, "-keyalg") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   586
                keyAlgName = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            } else if (collator.compare(flags, "-sigalg") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   588
                sigAlgName = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            } else if (collator.compare(flags, "-startdate") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   590
                startDate = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            } else if (collator.compare(flags, "-validity") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   592
                validity = Long.parseLong(args[++i]);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   593
            } else if (collator.compare(flags, "-ext") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   594
                v3ext.add(args[++i]);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   595
            } else if (collator.compare(flags, "-id") == 0) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   596
                ids.add(args[++i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            } else if (collator.compare(flags, "-file") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   598
                filename = args[++i];
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   599
            } else if (collator.compare(flags, "-infile") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   600
                infilename = args[++i];
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   601
            } else if (collator.compare(flags, "-outfile") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   602
                outfilename = args[++i];
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
   603
            } else if (collator.compare(flags, "-sslserver") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   604
                sslserver = args[++i];
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
   605
            } else if (collator.compare(flags, "-jarfile") == 0) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
   606
                jarfile = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            } else if (collator.compare(flags, "-srckeystore") == 0) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   608
                srcksfname = args[++i];
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   609
            } else if (collator.compare(flags, "-provider") == 0 ||
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   610
                        collator.compare(flags, "-providerclass") == 0) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   611
                if (providerClasses == null) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   612
                    providerClasses = new HashSet<Pair <String, String>> (3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                }
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   614
                String providerClass = args[++i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                String providerArg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                if (args.length > (i+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    flags = args[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    if (collator.compare(flags, "-providerarg") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                        if (args.length == (i+2)) errorNeedArgument(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                        providerArg = args[i+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                        i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                }
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   625
                providerClasses.add(
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   626
                        Pair.of(providerClass, providerArg));
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   627
            } else if (collator.compare(flags, "-addprovider") == 0) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   628
                if (providers == null) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   629
                    providers = new HashSet<Pair <String, String>> (3);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   630
                }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   631
                String provider = args[++i];
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   632
                String providerArg = null;
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   634
                if (args.length > (i+1)) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   635
                    flags = args[i+1];
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   636
                    if (collator.compare(flags, "-providerarg") == 0) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   637
                        if (args.length == (i+2)) errorNeedArgument(flags);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   638
                        providerArg = args[i+2];
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   639
                        i += 2;
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   640
                    }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   641
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                providers.add(
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   643
                        Pair.of(provider, providerArg));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
             * options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            else if (collator.compare(flags, "-v") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            } else if (collator.compare(flags, "-debug") == 0) {
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   652
                // Already processed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            } else if (collator.compare(flags, "-rfc") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                rfc = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            } else if (collator.compare(flags, "-noprompt") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                noprompt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            } else if (collator.compare(flags, "-trustcacerts") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                trustcacerts = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            } else if (collator.compare(flags, "-protected") == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    collator.compare(flags, "-destprotected") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                protectedPath = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            } else if (collator.compare(flags, "-srcprotected") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                srcprotectedPath = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            } else  {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   665
                System.err.println(rb.getString("Illegal.option.") + flags);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        if (i<args.length) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   671
            System.err.println(rb.getString("Illegal.option.") + args[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   674
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   675
        if (command == null) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   676
            if (help) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   677
                usage();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   678
            } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   679
                System.err.println(rb.getString("Usage.error.no.command.provided"));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   680
                tinyHelp();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   681
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   682
        } else if (help) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   683
            usage();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   684
            command = null;
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   685
        }
24868
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   686
89d9bd9eba96 8023197: Pre-configured command line options for keytool and jarsigner
weijun
parents: 20754
diff changeset
   687
        return args;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
   690
    boolean isKeyStoreRelated(Command cmd) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   691
        return cmd != PRINTCERT && cmd != PRINTCERTREQ;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   692
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
   693
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * Execute the commands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    void doCommands(PrintStream out) throws Exception {
40253
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   698
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   699
        if (cacerts) {
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   700
            if (ksfname != null || storetype != null) {
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   701
                throw new IllegalArgumentException(rb.getString
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   702
                        ("the.keystore.or.storetype.option.cannot.be.used.with.the.cacerts.option"));
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   703
            }
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   704
            ksfname = KeyStoreUtil.getCacerts();
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   705
        }
08809866c0bc 8162739: Create new keytool option to access cacerts file
weijun
parents: 39886
diff changeset
   706
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (P11KEYSTORE.equalsIgnoreCase(storetype) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            token = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            if (ksfname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                ksfname = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (NONE.equals(ksfname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            nullStream = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (token && !nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            System.err.println(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   720
                (".keystore.must.be.NONE.if.storetype.is.{0}"), storetype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (token &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            (command == KEYPASSWD || command == STOREPASSWD)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            throw new UnsupportedOperationException(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   728
                        (".storepasswd.and.keypasswd.commands.not.supported.if.storetype.is.{0}"), storetype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (token && (keyPass != null || newPass != null || destKeyPass != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            throw new IllegalArgumentException(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   733
                (".keypass.and.new.can.not.be.specified.if.storetype.is.{0}"), storetype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (protectedPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (storePass != null || keyPass != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    newPass != null || destKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                throw new IllegalArgumentException(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   740
                        ("if.protected.is.specified.then.storepass.keypass.and.new.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (srcprotectedPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            if (srcstorePass != null || srckeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                throw new IllegalArgumentException(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   747
                        ("if.srcprotected.is.specified.then.srcstorepass.and.srckeypass.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        if (KeyStoreUtil.isWindowsKeyStore(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            if (storePass != null || keyPass != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    newPass != null || destKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                throw new IllegalArgumentException(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   755
                        ("if.keystore.is.not.password.protected.then.storepass.keypass.and.new.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            if (srcstorePass != null || srckeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                throw new IllegalArgumentException(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   762
                        ("if.source.keystore.is.not.password.protected.then.srcstorepass.and.srckeypass.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (validity <= (long)0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            throw new Exception
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   768
                (rb.getString("Validity.must.be.greater.than.zero"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        // Try to load and install specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (providers != null) {
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   773
            for (Pair<String, String> provider : providers) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   774
                try {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   775
                    KeyStoreUtil.loadProviderByName(
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   776
                            provider.fst, provider.snd);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   777
                    if (debug) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   778
                        System.out.println("loadProviderByName: " + provider.fst);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   779
                    }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   780
                } catch (IllegalArgumentException e) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   781
                    throw new Exception(String.format(rb.getString(
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   782
                            "provider.name.not.found"), provider.fst));
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   783
                }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   784
            }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   785
        }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   786
        if (providerClasses != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            ClassLoader cl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            if (pathlist != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                String path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                path = PathList.appendPath(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                        path, System.getProperty("java.class.path"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                path = PathList.appendPath(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                        path, System.getProperty("env.class.path"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                path = PathList.appendPath(path, pathlist);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                URL[] urls = PathList.pathToURLs(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                cl = new URLClassLoader(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            }
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   801
            for (Pair<String, String> provider : providerClasses) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   802
                try {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   803
                    KeyStoreUtil.loadProviderByClass(
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   804
                            provider.fst, provider.snd, cl);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   805
                    if (debug) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   806
                        System.out.println("loadProviderByClass: " + provider.fst);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   807
                    }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   808
                } catch (ClassCastException cce) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   809
                    throw new Exception(String.format(rb.getString(
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   810
                            "provclass.not.a.provider"), provider.fst));
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   811
                } catch (IllegalArgumentException e) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   812
                    throw new Exception(String.format(rb.getString(
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
   813
                            "provider.class.not.found"), provider.fst), e.getCause());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (command == LIST && verbose && rfc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   820
                ("Must.not.specify.both.v.and.rfc.with.list.command"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        // Make sure provided passwords are at least 6 characters long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        if (command == GENKEYPAIR && keyPass!=null && keyPass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   827
                ("Key.password.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        if (newPass != null && newPass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   831
                ("New.password.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        if (destKeyPass != null && destKeyPass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   835
                ("New.password.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   838
        // Set this before inplaceImport check so we can compare name.
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   839
        if (ksfname == null) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   840
            ksfname = System.getProperty("user.home") + File.separator
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   841
                    + ".keystore";
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   842
        }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   843
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   844
        KeyStore srcKeyStore = null;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   845
        if (command == IMPORTKEYSTORE) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   846
            inplaceImport = inplaceImportCheck();
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   847
            if (inplaceImport) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   848
                // We load srckeystore first so we have srcstorePass that
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   849
                // can be assigned to storePass
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   850
                srcKeyStore = loadSourceKeyStore();
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   851
                if (storePass == null) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   852
                    storePass = srcstorePass;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   853
                }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   854
            }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   855
        }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   856
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        // Check if keystore exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        // If no keystore has been specified at the command line, try to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        // the default, which is located in $HOME/.keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        // If the command is "genkey", "identitydb", "import", or "printcert",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        // it is OK not to have a keystore.
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   862
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   863
        // DO NOT open the existing keystore if this is an in-place import.
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   864
        // The keystore should be created as brand new.
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   865
        if (isKeyStoreRelated(command) && !nullStream && !inplaceImport) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   866
            try {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   867
                ksfile = new File(ksfname);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   868
                // Check if keystore file is empty
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   869
                if (ksfile.exists() && ksfile.length() == 0) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   870
                    throw new Exception(rb.getString
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   871
                            ("Keystore.file.exists.but.is.empty.") + ksfname);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   872
                }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   873
                ksStream = new FileInputStream(ksfile);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   874
            } catch (FileNotFoundException e) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   875
                if (command != GENKEYPAIR &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        command != GENSECKEY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                        command != IDENTITYDB &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                        command != IMPORTCERT &&
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
   879
                        command != IMPORTPASS &&
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   880
                        command != IMPORTKEYSTORE &&
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
   881
                        command != PRINTCRL) {
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   882
                    throw new Exception(rb.getString
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   883
                            ("Keystore.file.does.not.exist.") + ksfname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        if ((command == KEYCLONE || command == CHANGEALIAS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                && dest == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            dest = getAlias("destination");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            if ("".equals(dest)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   893
                        ("Must.specify.destination.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        if (command == DELETE && alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            alias = getAlias(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            if ("".equals(alias)) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   900
                throw new Exception(rb.getString("Must.specify.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        // Create new keystore
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   905
        // Probe for keystore type when filename is available
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   906
        if (ksfile != null && ksStream != null && providerName == null &&
48216
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   907
                storetype == null && !inplaceImport) {
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   908
            keyStore = KeyStore.getInstance(ksfile, storePass);
48216
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   909
            storetype = keyStore.getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        } else {
48216
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   911
            if (storetype == null) {
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   912
                storetype = KeyStore.getDefaultType();
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   913
            }
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   914
            if (providerName == null) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   915
                keyStore = KeyStore.getInstance(storetype);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   916
            } else {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   917
                keyStore = KeyStore.getInstance(storetype, providerName);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   918
            }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   919
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   920
            /*
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   921
             * Load the keystore data.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   922
             *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   923
             * At this point, it's OK if no keystore password has been provided.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   924
             * We want to make sure that we can load the keystore data, i.e.,
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   925
             * the keystore data has the right format. If we cannot load the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   926
             * keystore, why bother asking the user for his or her password?
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   927
             * Only if we were able to load the keystore, and no keystore
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   928
             * password has been provided, will we prompt the user for the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   929
             * keystore password to verify the keystore integrity.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   930
             * This means that the keystore is loaded twice: first load operation
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   931
             * checks the keystore format, second load operation verifies the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   932
             * keystore integrity.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   933
             *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   934
             * If the keystore password has already been provided (at the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   935
             * command line), however, the keystore is loaded only once, and the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   936
             * keystore format and integrity are checked "at the same time".
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   937
             *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   938
             * Null stream keystores are loaded later.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   939
             */
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   940
            if (!nullStream) {
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   941
                if (inplaceImport) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   942
                    keyStore.load(null, storePass);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   943
                } else {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   944
                    keyStore.load(ksStream, storePass);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
   945
                }
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   946
                if (ksStream != null) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   947
                    ksStream.close();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   948
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
48216
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   952
        if (P12KEYSTORE.equalsIgnoreCase(storetype) && command == KEYPASSWD) {
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   953
            throw new UnsupportedOperationException(rb.getString
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   954
                    (".keypasswd.commands.not.supported.if.storetype.is.PKCS12"));
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   955
        }
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
   956
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        // All commands that create or modify the keystore require a keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        // password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        if (nullStream && storePass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            keyStore.load(null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        } else if (!nullStream && storePass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            // If we are creating a new non nullStream-based keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            // insist that the password be at least 6 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            if (ksStream == null && storePass.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   967
                        ("Keystore.password.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        } else if (storePass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            // only prompt if (protectedPath == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            if (!protectedPath && !KeyStoreUtil.isWindowsKeyStore(storetype) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                (command == CERTREQ ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                        command == DELETE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                        command == GENKEYPAIR ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                        command == GENSECKEY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                        command == IMPORTCERT ||
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
   979
                        command == IMPORTPASS ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                        command == IMPORTKEYSTORE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                        command == KEYCLONE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                        command == CHANGEALIAS ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                        command == SELFCERT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                        command == STOREPASSWD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                        command == KEYPASSWD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                        command == IDENTITYDB)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                    if (command == IMPORTKEYSTORE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                        System.err.print
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   991
                                (rb.getString("Enter.destination.keystore.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                        System.err.print
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
   994
                                (rb.getString("Enter.keystore.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                    storePass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                    passwords.add(storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                    // If we are creating a new non nullStream-based keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    // insist that the password be at least 6 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                    if (!nullStream && (storePass == null || storePass.length < 6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1004
                                ("Keystore.password.is.too.short.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                        storePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                    // If the keystore file does not exist and needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                    // created, the storepass should be prompted twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    if (storePass != null && !nullStream && ksStream == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1011
                        System.err.print(rb.getString("Re.enter.new.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                        char[] storePassAgain = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                        passwords.add(storePassAgain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                        if (!Arrays.equals(storePass, storePassAgain)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                            System.err.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1016
                                (rb.getString("They.don.t.match.Try.again"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                            storePass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                } while ((storePass == null) && count < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                if (storePass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                    System.err.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1027
                        (rb.getString("Too.many.failures.try.later"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            } else if (!protectedPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    && !KeyStoreUtil.isWindowsKeyStore(storetype)
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1032
                    && isKeyStoreRelated(command)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                // 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
  1034
                if (command != PRINTCRL) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1035
                    System.err.print(rb.getString("Enter.keystore.password."));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1036
                    System.err.flush();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1037
                    storePass = Password.readPassword(System.in);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1038
                    passwords.add(storePass);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1039
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            // Now load a nullStream-based keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            // or verify the integrity of an input stream-based keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            if (nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                keyStore.load(null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            } else if (ksStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                ksStream = new FileInputStream(ksfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                keyStore.load(ksStream, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                ksStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        if (storePass != null && P12KEYSTORE.equalsIgnoreCase(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            MessageFormat form = new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1055
                "Warning.Different.store.and.key.passwords.not.supported.for.PKCS12.KeyStores.Ignoring.user.specified.command.value."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            if (keyPass != null && !Arrays.equals(storePass, keyPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                Object[] source = {"-keypass"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                keyPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            if (destKeyPass != null && !Arrays.equals(storePass, destKeyPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                Object[] source = {"-destkeypass"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                destKeyPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        // Create a certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (command == PRINTCERT || command == IMPORTCERT
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1070
                || command == IDENTITYDB || command == PRINTCRL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  1074
        // -trustcacerts can only be specified on -importcert.
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  1075
        // Reset it so that warnings on CA cert will remain for
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  1076
        // -printcert, etc.
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  1077
        if (command != IMPORTCERT) {
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  1078
            trustcacerts = false;
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  1079
        }
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  1080
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        if (trustcacerts) {
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 11912
diff changeset
  1082
            caks = KeyStoreUtil.getCacertsKeyStore();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        // Perform the specified command
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        if (command == CERTREQ) {
90ce3da70b43 Initial load
duke
parents:
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 = 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
  1089
                                                      (filename))) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1090
                    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
  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 {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                doCertReq(alias, sigAlgName, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            if (verbose && filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1097
                        ("Certification.request.stored.in.file.filename."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                Object[] source = {filename};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                System.err.println(form.format(source));
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1100
                System.err.println(rb.getString("Submit.this.to.your.CA"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        } else if (command == DELETE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            doDeleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        } else if (command == EXPORTCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            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
  1107
                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
  1108
                                                   (filename))) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1109
                    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
  1110
                }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1111
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                doExportCert(alias, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1116
                        ("Certificate.stored.in.file.filename."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                Object[] source = {filename};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        } else if (command == GENKEYPAIR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            if (keyAlgName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                keyAlgName = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            doGenKeyPair(alias, dname, keyAlgName, keysize, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        } else if (command == GENSECKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            if (keyAlgName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                keyAlgName = "DES";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            doGenSecretKey(alias, keyAlgName, keysize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            kssave = true;
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1132
        } else if (command == IMPORTPASS) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1133
            if (keyAlgName == null) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1134
                keyAlgName = "PBE";
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1135
            }
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1136
            // password is stored as a secret key
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1137
            doGenSecretKey(alias, keyAlgName, keysize);
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1138
            kssave = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        } else if (command == IDENTITYDB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            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
  1141
                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
  1142
                    doImportIdentityDatabase(inStream);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                }
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1144
            } else {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1145
                doImportIdentityDatabase(System.in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        } else if (command == IMPORTCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            InputStream inStream = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            if (filename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                inStream = new FileInputStream(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            }
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1152
            String importAlias = (alias!=null)?alias:keyAlias;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            try {
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1154
                if (keyStore.entryInstanceOf(
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1155
                        importAlias, KeyStore.PrivateKeyEntry.class)) {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1156
                    kssave = installReply(importAlias, inStream);
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1157
                    if (kssave) {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1158
                        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1159
                            ("Certificate.reply.was.installed.in.keystore"));
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1160
                    } else {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1161
                        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1162
                            ("Certificate.reply.was.not.installed.in.keystore"));
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1163
                    }
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1164
                } else if (!keyStore.containsAlias(importAlias) ||
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1165
                        keyStore.entryInstanceOf(importAlias,
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1166
                            KeyStore.TrustedCertificateEntry.class)) {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1167
                    kssave = addTrustedCert(importAlias, inStream);
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1168
                    if (kssave) {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1169
                        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1170
                            ("Certificate.was.added.to.keystore"));
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1171
                    } else {
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1172
                        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1173
                            ("Certificate.was.not.added.to.keystore"));
5164
337ae296b6d6 6813340: X509Factory should not depend on is.available()==0
weijun
parents: 4918
diff changeset
  1174
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                if (inStream != System.in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                    inStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        } else if (command == IMPORTKEYSTORE) {
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1182
            // When not in-place import, srcKeyStore is not loaded yet.
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1183
            if (srcKeyStore == null) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1184
                srcKeyStore = loadSourceKeyStore();
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1185
            }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1186
            doImportKeyStore(srcKeyStore);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        } else if (command == KEYCLONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            keyPassNew = newPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            // added to make sure only key can go thru
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1197
                    (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            if (!keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                MessageFormat form = new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1203
                        "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
  1204
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            doCloneEntry(alias, dest, true);  // Now everything can be cloned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        } else if (command == CHANGEALIAS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            doCloneEntry(alias, dest, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            // in PKCS11, clone a PrivateKeyEntry will delete the old one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                doDeleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        } else if (command == KEYPASSWD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            keyPassNew = newPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            doChangeKeyPasswd(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        } else if (command == LIST) {
27038
64cb6608f8a6 8029659: Keytool, print key algorithm of certificate or key entry
weijun
parents: 25859
diff changeset
  1225
            if (storePass == null
64cb6608f8a6 8029659: Keytool, print key algorithm of certificate or key entry
weijun
parents: 25859
diff changeset
  1226
                    && !KeyStoreUtil.isWindowsKeyStore(storetype)) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1227
                printNoIntegrityWarning();
27038
64cb6608f8a6 8029659: Keytool, print key algorithm of certificate or key entry
weijun
parents: 25859
diff changeset
  1228
            }
64cb6608f8a6 8029659: Keytool, print key algorithm of certificate or key entry
weijun
parents: 25859
diff changeset
  1229
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            if (alias != null) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1231
                doPrintEntry(rb.getString("the.certificate"), alias, out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                doPrintEntries(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        } else if (command == PRINTCERT) {
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  1236
            doPrintCert(out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        } else if (command == SELFCERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            doSelfCert(alias, dname, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            kssave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        } else if (command == STOREPASSWD) {
48333
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  1241
            doChangeStorePasswd();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            kssave = true;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1243
        } else if (command == GENCERT) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1244
            if (alias == null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1245
                alias = keyAlias;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1246
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1247
            InputStream inStream = System.in;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1248
            if (infilename != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1249
                inStream = new FileInputStream(infilename);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1250
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1251
            PrintStream ps = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1252
            if (outfilename != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1253
                ps = new PrintStream(new FileOutputStream(outfilename));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1254
                out = ps;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1255
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1256
            try {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1257
                doGenCert(alias, sigAlgName, inStream, out);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1258
            } finally {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1259
                if (inStream != System.in) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1260
                    inStream.close();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1261
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1262
                if (ps != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1263
                    ps.close();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1264
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1265
            }
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1266
        } else if (command == GENCRL) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1267
            if (alias == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1268
                alias = keyAlias;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1269
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1270
            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
  1271
                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
  1272
                         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
  1273
                    doGenCRL(ps);
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1274
                }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1275
            } else {
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1276
                doGenCRL(out);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1277
            }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1278
        } else if (command == PRINTCERTREQ) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1279
            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
  1280
                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
  1281
                    doPrintCertReq(inStream, out);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1282
                }
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1283
            } else {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  1284
                doPrintCertReq(System.in, out);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1285
            }
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1286
        } else if (command == PRINTCRL) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1287
            doPrintCRL(filename, out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        // If we need to save the keystore, do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        if (kssave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1294
                        (rb.getString(".Storing.ksfname."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                Object[] source = {nullStream ? "keystore" : ksfname};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            if (token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                keyStore.store(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            } else {
10436
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1302
                char[] pass = (storePassNew!=null) ? storePassNew : storePass;
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1303
                if (nullStream) {
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1304
                    keyStore.store(null, pass);
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1305
                } else {
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1306
                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1307
                    keyStore.store(bout, pass);
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1308
                    try (FileOutputStream fout = new FileOutputStream(ksfname)) {
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1309
                        fout.write(bout.toByteArray());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        }
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1314
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1315
        if (isKeyStoreRelated(command)
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1316
                && !token && !nullStream && ksfname != null) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1317
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1318
            // JKS storetype warning on the final result keystore
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1319
            File f = new File(ksfname);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1320
            char[] pass = (storePassNew!=null) ? storePassNew : storePass;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1321
            if (f.exists()) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1322
                // Probe for real type. A JKS can be loaded as PKCS12 because
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1323
                // DualFormat support, vice versa.
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1324
                keyStore = KeyStore.getInstance(f, pass);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1325
                String realType = keyStore.getType();
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1326
                if (realType.equalsIgnoreCase("JKS")
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1327
                        || realType.equalsIgnoreCase("JCEKS")) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1328
                    boolean allCerts = true;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1329
                    for (String a : Collections.list(keyStore.aliases())) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1330
                        if (!keyStore.entryInstanceOf(
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1331
                                a, TrustedCertificateEntry.class)) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1332
                            allCerts = false;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1333
                            break;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1334
                        }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1335
                    }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1336
                    // Don't warn for "cacerts" style keystore.
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1337
                    if (!allCerts) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1338
                        weakWarnings.add(String.format(
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1339
                                rb.getString("jks.storetype.warning"),
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1340
                                realType, ksfname));
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1341
                    }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1342
                }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1343
                if (inplaceImport) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1344
                    String realSourceStoreType = KeyStore.getInstance(
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1345
                            new File(inplaceBackupName), srcstorePass).getType();
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1346
                    String format =
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1347
                            realType.equalsIgnoreCase(realSourceStoreType) ?
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1348
                            rb.getString("backup.keystore.warning") :
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1349
                            rb.getString("migrate.keystore.warning");
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1350
                    weakWarnings.add(
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1351
                            String.format(format,
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1352
                                    srcksfname,
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1353
                                    realSourceStoreType,
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1354
                                    inplaceBackupName,
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1355
                                    realType));
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1356
                }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1357
            }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  1358
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    /**
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1362
     * Generate a certificate: Read PKCS10 request from in, and print
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1363
     * 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
  1364
     * type.
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1365
     */
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1366
    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
  1367
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1368
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1369
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1370
        if (keyStore.containsAlias(alias) == false) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1371
            MessageFormat form = new MessageFormat
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1372
                    (rb.getString("Alias.alias.does.not.exist"));
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1373
            Object[] source = {alias};
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1374
            throw new Exception(form.format(source));
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1375
        }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1376
        Certificate signerCert = keyStore.getCertificate(alias);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1377
        byte[] encoded = signerCert.getEncoded();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1378
        X509CertImpl signerCertImpl = new X509CertImpl(encoded);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1379
        X509CertInfo signerCertInfo = (X509CertInfo)signerCertImpl.get(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1380
                X509CertImpl.NAME + "." + X509CertImpl.INFO);
3316
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  1381
        X500Name issuer = (X500Name)signerCertInfo.get(X509CertInfo.SUBJECT + "." +
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14182
diff changeset
  1382
                                           X509CertInfo.DN_NAME);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1383
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1384
        Date firstDate = getStartDate(startDate);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1385
        Date lastDate = new Date();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1386
        lastDate.setTime(firstDate.getTime() + validity*1000L*24L*60L*60L);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1387
        CertificateValidity interval = new CertificateValidity(firstDate,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1388
                                                               lastDate);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1389
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1390
        PrivateKey privateKey =
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1391
                (PrivateKey)recoverKey(alias, storePass, keyPass).fst;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1392
        if (sigAlgName == null) {
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 37782
diff changeset
  1393
            sigAlgName = getCompatibleSigAlgName(privateKey);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1394
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1395
        Signature signature = Signature.getInstance(sigAlgName);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1396
        signature.initSign(privateKey);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1397
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1398
        X509CertInfo info = new X509CertInfo();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1399
        info.set(X509CertInfo.VALIDITY, interval);
2293
cb6d01cb3c3d 6820606: keytool can generate serialno more randomly
weijun
parents: 2289
diff changeset
  1400
        info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(
cb6d01cb3c3d 6820606: keytool can generate serialno more randomly
weijun
parents: 2289
diff changeset
  1401
                    new java.util.Random().nextInt() & 0x7fffffff));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1402
        info.set(X509CertInfo.VERSION,
4350
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 4169
diff changeset
  1403
                    new CertificateVersion(CertificateVersion.V3));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1404
        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
  1405
                    new CertificateAlgorithmId(
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  1406
                        AlgorithmId.get(sigAlgName)));
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14182
diff changeset
  1407
        info.set(X509CertInfo.ISSUER, issuer);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1408
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1409
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1410
        boolean canRead = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1411
        StringBuffer sb = new StringBuffer();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1412
        while (true) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1413
            String s = reader.readLine();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1414
            if (s == null) break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1415
            // OpenSSL does not use NEW
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1416
            //if (s.startsWith("-----BEGIN NEW CERTIFICATE REQUEST-----")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1417
            if (s.startsWith("-----BEGIN") && s.indexOf("REQUEST") >= 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1418
                canRead = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1419
            //} else if (s.startsWith("-----END NEW CERTIFICATE REQUEST-----")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1420
            } else if (s.startsWith("-----END") && s.indexOf("REQUEST") >= 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1421
                break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1422
            } else if (canRead) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1423
                sb.append(s);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1424
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1425
        }
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1426
        byte[] rawReq = Pem.decode(new String(sb));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1427
        PKCS10 req = new PKCS10(rawReq);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1428
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1429
        checkWeak(rb.getString("the.certificate.request"), req);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1430
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1431
        info.set(X509CertInfo.KEY, new CertificateX509Key(req.getSubjectPublicKeyInfo()));
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14182
diff changeset
  1432
        info.set(X509CertInfo.SUBJECT,
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14182
diff changeset
  1433
                    dname==null?req.getSubjectName():new X500Name(dname));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1434
        CertificateExtensions reqex = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1435
        Iterator<PKCS10Attribute> attrs = req.getAttributes().getAttributes().iterator();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1436
        while (attrs.hasNext()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1437
            PKCS10Attribute attr = attrs.next();
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 31270
diff changeset
  1438
            if (attr.getAttributeId().equals(PKCS9Attribute.EXTENSION_REQUEST_OID)) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1439
                reqex = (CertificateExtensions)attr.getAttributeValue();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1440
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1441
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1442
        CertificateExtensions ext = createV3Extensions(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1443
                reqex,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1444
                null,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1445
                v3ext,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1446
                req.getSubjectPublicKeyInfo(),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1447
                signerCert.getPublicKey());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1448
        info.set(X509CertInfo.EXTENSIONS, ext);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1449
        X509CertImpl cert = new X509CertImpl(info);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1450
        cert.sign(privateKey, sigAlgName);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1451
        dumpCert(cert, out);
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1452
        for (Certificate ca: keyStore.getCertificateChain(alias)) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1453
            if (ca instanceof X509Certificate) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1454
                X509Certificate xca = (X509Certificate)ca;
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 34381
diff changeset
  1455
                if (!KeyStoreUtil.isSelfSigned(xca)) {
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1456
                    dumpCert(xca, out);
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1457
                }
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1458
            }
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  1459
        }
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1460
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1461
        checkWeak(rb.getString("the.issuer"), keyStore.getCertificateChain(alias));
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1462
        checkWeak(rb.getString("the.generated.certificate"), cert);
2067
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
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1465
    private void doGenCRL(PrintStream out)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1466
            throws Exception {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1467
        if (ids == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1468
            throw new Exception("Must provide -id when -gencrl");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1469
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1470
        Certificate signerCert = keyStore.getCertificate(alias);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1471
        byte[] encoded = signerCert.getEncoded();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1472
        X509CertImpl signerCertImpl = new X509CertImpl(encoded);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1473
        X509CertInfo signerCertInfo = (X509CertInfo)signerCertImpl.get(
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1474
                X509CertImpl.NAME + "." + X509CertImpl.INFO);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1475
        X500Name owner = (X500Name)signerCertInfo.get(X509CertInfo.SUBJECT + "." +
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14182
diff changeset
  1476
                                                      X509CertInfo.DN_NAME);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1477
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1478
        Date firstDate = getStartDate(startDate);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1479
        Date lastDate = (Date) firstDate.clone();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  1480
        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
  1481
        CertificateValidity interval = new CertificateValidity(firstDate,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1482
                                                               lastDate);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1483
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1484
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1485
        PrivateKey privateKey =
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1486
                (PrivateKey)recoverKey(alias, storePass, keyPass).fst;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1487
        if (sigAlgName == null) {
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 37782
diff changeset
  1488
            sigAlgName = getCompatibleSigAlgName(privateKey);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1489
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1490
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1491
        X509CRLEntry[] badCerts = new X509CRLEntry[ids.size()];
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1492
        for (int i=0; i<ids.size(); i++) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1493
            String id = ids.get(i);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1494
            int d = id.indexOf(':');
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1495
            if (d >= 0) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1496
                CRLExtensions ext = new CRLExtensions();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1497
                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
  1498
                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
  1499
                        firstDate, ext);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1500
            } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1501
                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
  1502
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1503
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1504
        X509CRLImpl crl = new X509CRLImpl(owner, firstDate, lastDate, badCerts);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1505
        crl.sign(privateKey, sigAlgName);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1506
        if (rfc) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1507
            out.println("-----BEGIN X509 CRL-----");
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1508
            out.println(Base64.getMimeEncoder(64, CRLF).encodeToString(crl.getEncodedInternal()));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1509
            out.println("-----END X509 CRL-----");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1510
        } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1511
            out.write(crl.getEncodedInternal());
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1512
        }
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1513
        checkWeak(rb.getString("the.generated.crl"), crl, privateKey);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1514
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  1515
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1516
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * Creates a PKCS#10 cert signing request, corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * keys (and name) associated with a given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
    private void doCertReq(String alias, String sigAlgName, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1527
        Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1528
        PrivateKey privKey = (PrivateKey)objs.fst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        if (keyPass == null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1530
            keyPass = objs.snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        Certificate cert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        if (cert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1536
                (rb.getString("alias.has.no.public.key.certificate."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        PKCS10 request = new PKCS10(cert.getPublicKey());
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1541
        CertificateExtensions ext = createV3Extensions(null, null, v3ext, cert.getPublicKey(), null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1542
        // Attribute name is not significant
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1543
        request.getAttributes().setAttribute(X509CertInfo.EXTENSIONS,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1544
                new PKCS10Attribute(PKCS9Attribute.EXTENSION_REQUEST_OID, ext));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
4350
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 4169
diff changeset
  1546
        // Construct a Signature object, so that we can sign the request
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        if (sigAlgName == null) {
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 37782
diff changeset
  1548
            sigAlgName = getCompatibleSigAlgName(privKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        Signature signature = Signature.getInstance(sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        signature.initSign(privKey);
3316
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  1553
        X500Name subject = dname == null?
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  1554
                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
  1555
                new X500Name(dname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        // 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
  1558
        request.encodeAndSign(subject, signature);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        request.print(out);
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1560
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1561
        checkWeak(rb.getString("the.generated.certificate.request"), request);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * Deletes an entry from the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    private void doDeleteEntry(String alias) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1570
                (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        keyStore.deleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * Exports a certificate from the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    private void doExportCert(String alias, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        if (storePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                && !KeyStoreUtil.isWindowsKeyStore(storetype)) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1585
            printNoIntegrityWarning();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1592
                (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        X509Certificate cert = (X509Certificate)keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        if (cert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1600
                (rb.getString("Alias.alias.has.no.certificate"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        dumpCert(cert, out);
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1605
        checkWeak(rb.getString("the.certificate"), cert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * Prompt the user for a keypass when generating a key entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * @param alias the entry we will set password for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * @param orig the original entry of doing a dup, null if generate new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * @param origPass the password to copy from if user press ENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    private char[] promptForKeyPass(String alias, String orig, char[] origPass) throws Exception{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        if (P12KEYSTORE.equalsIgnoreCase(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            return origPass;
10436
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1617
        } else if (!token && !protectedPath) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            // Prompt for key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            for (count = 0; count < 3; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1622
                        ("Enter.key.password.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                if (orig == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                    System.err.print(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1627
                            (".RETURN.if.same.as.keystore.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                    form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1630
                            (".RETURN.if.same.as.for.otherAlias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                    Object[] src = {orig};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                    System.err.print(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                char[] entered = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                passwords.add(entered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                if (entered == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                    return origPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                } else if (entered.length >= 6) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1640
                    System.err.print(rb.getString("Re.enter.new.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                    char[] passAgain = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                    passwords.add(passAgain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                    if (!Arrays.equals(entered, passAgain)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                        System.err.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1645
                            (rb.getString("They.don.t.match.Try.again"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                    return entered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                    System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1651
                        ("Key.password.is.too.short.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            if (count == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                if (command == KEYCLONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                    throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1657
                        ("Too.many.failures.Key.entry.not.cloned"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                    throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1660
                            ("Too.many.failures.key.not.added.to.keystore"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        }
10436
4288852bdda6 7047200: keytool safe store
weijun
parents: 10336
diff changeset
  1664
        return null;    // PKCS11, MSCAPI, or -protected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    }
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1666
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1667
    /*
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1668
     * Prompt the user for the password credential to be stored.
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1669
     */
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1670
    private char[] promptForCredential() throws Exception {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1671
        // Handle password supplied via stdin
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1672
        if (System.console() == null) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1673
            char[] importPass = Password.readPassword(System.in);
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1674
            passwords.add(importPass);
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1675
            return importPass;
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1676
        }
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1677
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1678
        int count;
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1679
        for (count = 0; count < 3; count++) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1680
            System.err.print(
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1681
                rb.getString("Enter.the.password.to.be.stored."));
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1682
            System.err.flush();
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1683
            char[] entered = Password.readPassword(System.in);
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1684
            passwords.add(entered);
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1685
            System.err.print(rb.getString("Re.enter.password."));
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1686
            char[] passAgain = Password.readPassword(System.in);
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1687
            passwords.add(passAgain);
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1688
            if (!Arrays.equals(entered, passAgain)) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1689
                System.err.println(rb.getString("They.don.t.match.Try.again"));
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1690
                continue;
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1691
            }
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1692
            return entered;
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1693
        }
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1694
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1695
        if (count == 3) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1696
            throw new Exception(rb.getString
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1697
                ("Too.many.failures.key.not.added.to.keystore"));
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1698
        }
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1699
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1700
        return null;
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1701
    }
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1702
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * Creates a new secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    private void doGenSecretKey(String alias, String keyAlgName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                              int keysize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1715
                ("Secret.key.not.generated.alias.alias.already.exists"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1720
        // Use the keystore's default PBE algorithm for entry protection
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1721
        boolean useDefaultPBEAlgorithm = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        SecretKey secKey = null;
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1723
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 25186
diff changeset
  1724
        if (keyAlgName.toUpperCase(Locale.ENGLISH).startsWith("PBE")) {
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1725
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1726
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1727
            // User is prompted for PBE credential
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1728
            secKey =
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1729
                factory.generateSecret(new PBEKeySpec(promptForCredential()));
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1730
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1731
            // Check whether a specific PBE algorithm was specified
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1732
            if (!"PBE".equalsIgnoreCase(keyAlgName)) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1733
                useDefaultPBEAlgorithm = false;
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1734
            }
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1735
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1736
            if (verbose) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1737
                MessageFormat form = new MessageFormat(rb.getString(
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1738
                    "Generated.keyAlgName.secret.key"));
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1739
                Object[] source =
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1740
                    {useDefaultPBEAlgorithm ? "PBE" : secKey.getAlgorithm()};
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1741
                System.err.println(form.format(source));
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1742
            }
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1743
        } else {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1744
            KeyGenerator keygen = KeyGenerator.getInstance(keyAlgName);
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1745
            if (keysize == -1) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1746
                if ("DES".equalsIgnoreCase(keyAlgName)) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1747
                    keysize = 56;
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1748
                } else if ("DESede".equalsIgnoreCase(keyAlgName)) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1749
                    keysize = 168;
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1750
                } else {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1751
                    throw new Exception(rb.getString
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1752
                        ("Please.provide.keysize.for.secret.key.generation"));
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1753
                }
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1754
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            keygen.init(keysize);
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1756
            secKey = keygen.generateKey();
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1757
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1758
            if (verbose) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1759
                MessageFormat form = new MessageFormat(rb.getString
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1760
                    ("Generated.keysize.bit.keyAlgName.secret.key"));
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25402
diff changeset
  1761
                Object[] source = {keysize,
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1762
                                    secKey.getAlgorithm()};
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1763
                System.err.println(form.format(source));
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1764
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            keyPass = promptForKeyPass(alias, null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        }
20516
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1770
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1771
        if (useDefaultPBEAlgorithm) {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1772
            keyStore.setKeyEntry(alias, secKey, keyPass, null);
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1773
        } else {
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1774
            keyStore.setEntry(alias, new KeyStore.SecretKeyEntry(secKey),
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1775
                new KeyStore.PasswordProtection(keyPass, keyAlgName, null));
fa2edce67c48 8008296: keytool utility doesn't support '-importpassword' command
vinnie
parents: 19043
diff changeset
  1776
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    /**
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1780
     * If no signature algorithm was specified at the command line,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1781
     * 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
  1782
     */
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 37782
diff changeset
  1783
    private static String getCompatibleSigAlgName(PrivateKey key)
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1784
            throws Exception {
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 37782
diff changeset
  1785
        String result = AlgorithmId.getDefaultSigAlgForKey(key);
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 37782
diff changeset
  1786
        if (result != null) {
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 37782
diff changeset
  1787
            return result;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1788
        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1789
            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1790
                    ("Cannot.derive.signature.algorithm"));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1791
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1792
    }
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 37782
diff changeset
  1793
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1794
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * Creates a new key pair and self-signed certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    private void doGenKeyPair(String alias, String dname, String keyAlgName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                              int keysize, String sigAlgName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        if (keysize == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            if ("EC".equalsIgnoreCase(keyAlgName)) {
47421
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47420
diff changeset
  1803
                keysize = SecurityProviderConstants.DEF_EC_KEY_SIZE;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47420
diff changeset
  1804
            } else if ("RSA".equalsIgnoreCase(keyAlgName)) {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47420
diff changeset
  1805
                keysize = SecurityProviderConstants.DEF_RSA_KEY_SIZE;
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47420
diff changeset
  1806
            } else if ("DSA".equalsIgnoreCase(keyAlgName)) {
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47420
diff changeset
  1807
                // hardcode for now as DEF_DSA_KEY_SIZE is still 1024
f9e03aef3a49 8181048: Refactor existing providers to refer to the same constants for default values for key length
valeriep
parents: 47420
diff changeset
  1808
                keysize = 2048; // SecurityProviderConstants.DEF_DSA_KEY_SIZE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1818
                ("Key.pair.not.generated.alias.alias.already.exists"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        CertAndKeyGen keypair =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                new CertAndKeyGen(keyAlgName, sigAlgName, providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        // If DN is provided, parse it. Otherwise, prompt the user for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        X500Name x500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        if (dname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            x500Name = getX500Name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            x500Name = new X500Name(dname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        keypair.generate(keysize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        PrivateKey privKey = keypair.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
11012
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1838
        CertificateExtensions ext = createV3Extensions(
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1839
                null,
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1840
                null,
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1841
                v3ext,
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1842
                keypair.getPublicKeyAnyway(),
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1843
                null);
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1844
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        X509Certificate[] chain = new X509Certificate[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        chain[0] = keypair.getSelfCertificate(
11012
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
  1847
                x500Name, getStartDate(startDate), validity*24L*60L*60L, ext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1851
                ("Generating.keysize.bit.keyAlgName.key.pair.and.self.signed.certificate.sigAlgName.with.a.validity.of.validality.days.for"));
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25402
diff changeset
  1852
            Object[] source = {keysize,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                                privKey.getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                                chain[0].getSigAlgName(),
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 24969
diff changeset
  1855
                                validity,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                                x500Name};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            keyPass = promptForKeyPass(alias, null, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        }
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  1863
        checkWeak(rb.getString("the.generated.certificate"), chain[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        keyStore.setKeyEntry(alias, privKey, keyPass, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * Clones an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     * @param orig original alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * @param dest destination alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * @changePassword if the password can be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    private void doCloneEntry(String orig, String dest, boolean changePassword)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        if (orig == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            orig = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        if (keyStore.containsAlias(dest)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1882
                (rb.getString("Destination.alias.dest.already.exists"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            Object[] source = {dest};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1887
        Pair<Entry,char[]> objs = recoverEntry(keyStore, orig, storePass, keyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1888
        Entry entry = objs.fst;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1889
        keyPass = objs.snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        PasswordProtection pp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        if (keyPass != null) {  // protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            if (!changePassword || P12KEYSTORE.equalsIgnoreCase(storetype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                keyPassNew = keyPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                if (keyPassNew == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                    keyPassNew = promptForKeyPass(dest, orig, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            pp = new PasswordProtection(keyPassNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        keyStore.setEntry(dest, entry, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * Changes a key password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    private void doChangeKeyPasswd(String alias) throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1915
        Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1916
        Key privKey = objs.fst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        if (keyPass == null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  1918
            keyPass = objs.snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        if (keyPassNew == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1923
                (rb.getString("key.password.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            keyPassNew = getNewPasswd(form.format(source), keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        keyStore.setKeyEntry(alias, privKey, keyPassNew,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                             keyStore.getCertificateChain(alias));
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
     * Imports a JDK 1.1-style identity database. We can only store one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * certificate per identity, because we use the identity's name as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * alias (which references a keystore entry), and aliases must be unique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    private void doImportIdentityDatabase(InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
    {
4350
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 4169
diff changeset
  1939
        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1940
            ("No.entries.from.identity.database.added"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * Prints a single keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     */
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  1946
    private void doPrintEntry(String label, String alias, PrintStream out)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1951
                (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1958
                (rb.getString("Alias.name.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            if (!token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1964
                    ("Creation.date.keyStore.getCreationDate.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                Object[] src = {keyStore.getCreationDate(alias)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
                out.println(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            if (!token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1971
                    (rb.getString("alias.keyStore.getCreationDate.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                Object[] source = {alias, keyStore.getCreationDate(alias)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                out.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1976
                    (rb.getString("alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                out.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        if (keyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                Object[] source = {"SecretKeyEntry"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                out.println(new MessageFormat(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1986
                        rb.getString("Entry.type.type.")).format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                out.println("SecretKeyEntry, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        } else if (keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                Object[] source = {"PrivateKeyEntry"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                out.println(new MessageFormat(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  1994
                        rb.getString("Entry.type.type.")).format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                out.println("PrivateKeyEntry, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
            // Get the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
            Certificate[] chain = keyStore.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            if (chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                if (verbose || rfc || debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                    out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2004
                        ("Certificate.chain.length.") + chain.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                    for (int i = 0; i < chain.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                        MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2007
                                (rb.getString("Certificate.i.1."));
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25402
diff changeset
  2008
                        Object[] source = {(i + 1)};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                        out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                        if (verbose && (chain[i] instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                            printX509Cert((X509Certificate)(chain[i]), out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                        } else if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                            out.println(chain[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                            dumpCert(chain[i], out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                        }
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2017
                        checkWeak(label, chain[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                    // Print the digest of the user cert only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    out.println
34381
be5600f6a4da 8141457: keytool default cert fingerprint algorithm should be SHA-256
weijun
parents: 31426
diff changeset
  2022
                        (rb.getString("Certificate.fingerprint.SHA.256.") +
be5600f6a4da 8141457: keytool default cert fingerprint algorithm should be SHA-256
weijun
parents: 31426
diff changeset
  2023
                        getCertFingerPrint("SHA-256", chain[0]));
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2024
                    checkWeak(label, chain);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        } else if (keyStore.entryInstanceOf(alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                KeyStore.TrustedCertificateEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            // We have a trusted certificate entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            Certificate cert = keyStore.getCertificate(alias);
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  2031
            Object[] source = {"trustedCertEntry"};
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  2032
            String mf = new MessageFormat(
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  2033
                    rb.getString("Entry.type.type.")).format(source) + "\n";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            if (verbose && (cert instanceof X509Certificate)) {
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  2035
                out.println(mf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                printX509Cert((X509Certificate)cert, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            } else if (rfc) {
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  2038
                out.println(mf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                dumpCert(cert, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            } else if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                out.println(cert.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            } else {
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 8556
diff changeset
  2043
                out.println("trustedCertEntry, ");
34381
be5600f6a4da 8141457: keytool default cert fingerprint algorithm should be SHA-256
weijun
parents: 31426
diff changeset
  2044
                out.println(rb.getString("Certificate.fingerprint.SHA.256.")
be5600f6a4da 8141457: keytool default cert fingerprint algorithm should be SHA-256
weijun
parents: 31426
diff changeset
  2045
                            + getCertFingerPrint("SHA-256", cert));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            }
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2047
            checkWeak(label, cert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
        } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2049
            out.println(rb.getString("Unknown.Entry.Type"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2053
    boolean inplaceImportCheck() throws Exception {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2054
        if (P11KEYSTORE.equalsIgnoreCase(srcstoretype) ||
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2055
                KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2056
            return false;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2057
        }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2058
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2059
        if (srcksfname != null) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2060
            File srcksfile = new File(srcksfname);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2061
            if (srcksfile.exists() && srcksfile.length() == 0) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2062
                throw new Exception(rb.getString
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2063
                        ("Source.keystore.file.exists.but.is.empty.") +
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2064
                        srcksfname);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2065
            }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2066
            if (srcksfile.getCanonicalFile()
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2067
                    .equals(new File(ksfname).getCanonicalFile())) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2068
                return true;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2069
            } else {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2070
                // Informational, especially if destkeystore is not
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2071
                // provided, which default to ~/.keystore.
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2072
                System.err.println(String.format(rb.getString(
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2073
                        "importing.keystore.status"), srcksfname, ksfname));
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2074
                return false;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2075
            }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2076
        } else {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2077
            throw new Exception(rb.getString
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2078
                    ("Please.specify.srckeystore"));
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2079
        }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2080
    }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2081
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * Load the srckeystore from a stream, used in -importkeystore
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 29596
diff changeset
  2084
     * @return the src KeyStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
    KeyStore loadSourceKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        InputStream is = null;
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  2089
        File srcksfile = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        if (P11KEYSTORE.equalsIgnoreCase(srcstoretype) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            if (!NONE.equals(srcksfname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                System.err.println(MessageFormat.format(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2095
                    (".keystore.must.be.NONE.if.storetype.is.{0}"), srcstoretype));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        } else {
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2100
            srcksfile = new File(srcksfname);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2101
            is = new FileInputStream(srcksfile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        KeyStore store;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        try {
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  2106
            // Probe for keystore type when filename is available
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  2107
            if (srcksfile != null && is != null && srcProviderName == null &&
48216
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
  2108
                    srcstoretype == null) {
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  2109
                store = KeyStore.getInstance(srcksfile, srcstorePass);
48216
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
  2110
                srcstoretype = store.getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            } else {
48216
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
  2112
                if (srcstoretype == null) {
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
  2113
                    srcstoretype = KeyStore.getDefaultType();
e3b6cb90d7ce 8192987: keytool should remember real storetype if it is not provided
weijun
parents: 48050
diff changeset
  2114
                }
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  2115
                if (srcProviderName == null) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  2116
                    store = KeyStore.getInstance(srcstoretype);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  2117
                } else {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  2118
                    store = KeyStore.getInstance(srcstoretype, srcProviderName);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  2119
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
            if (srcstorePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                    && !srcprotectedPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                    && !KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2125
                System.err.print(rb.getString("Enter.source.keystore.password."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                srcstorePass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                passwords.add(srcstorePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            // always let keypass be storepass when using pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            if (P12KEYSTORE.equalsIgnoreCase(srcstoretype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                if (srckeyPass != null && srcstorePass != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                        !Arrays.equals(srcstorePass, srckeyPass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                    MessageFormat form = new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2136
                        "Warning.Different.store.and.key.passwords.not.supported.for.PKCS12.KeyStores.Ignoring.user.specified.command.value."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                    Object[] source = {"-srckeypass"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                    System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                    srckeyPass = srcstorePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            store.load(is, srcstorePass);   // "is" already null in PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
            if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        if (srcstorePass == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                && !KeyStoreUtil.isWindowsKeyStore(srcstoretype)) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2152
            // anti refactoring, copied from printNoIntegrityWarning(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
            // but change 2 lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2156
                (".WARNING.WARNING.WARNING."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
            System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2158
                (".The.integrity.of.the.information.stored.in.the.srckeystore."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2160
                (".WARNING.WARNING.WARNING."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        return store;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * import all keys and certs from importkeystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * keep alias unchanged if no name conflict, otherwise, prompt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     * keep keypass unchanged for keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     */
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2172
    private void doImportKeyStore(KeyStore srcKS) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        if (alias != null) {
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2175
            doImportKeyStoreSingle(srcKS, alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        } else {
16720
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2177
            if (dest != null || srckeyPass != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                throw new Exception(rb.getString(
16720
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2179
                        "if.alias.not.specified.destalias.and.srckeypass.must.not.be.specified"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
            }
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2181
            doImportKeyStoreAll(srcKS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        }
47420
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2183
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2184
        if (inplaceImport) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2185
            // Backup to file.old or file.old2...
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2186
            // The keystore is not rewritten yet now.
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2187
            for (int n = 1; /* forever */; n++) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2188
                inplaceBackupName = srcksfname + ".old" + (n == 1 ? "" : n);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2189
                File bkFile = new File(inplaceBackupName);
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2190
                if (!bkFile.exists()) {
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2191
                    Files.copy(Paths.get(srcksfname), bkFile.toPath());
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2192
                    break;
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2193
                }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2194
            }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2195
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2196
        }
a2bf68a0365f 8182879: Add warnings to keytool when using JKS and JCEKS
weijun
parents: 47216
diff changeset
  2197
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
         * Information display rule of -importkeystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
         * 1. inside single, shows failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
         * 2. inside all, shows sucess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
         * 3. inside all where there is a failure, prompt for continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
         * 4. at the final of all, shows summary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * Import a single entry named alias from srckeystore
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 29596
diff changeset
  2209
     * @return  1 if the import action succeed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     *          0 if user choose to ignore an alias-dumplicated entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     *          2 if setEntry throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    private int doImportKeyStoreSingle(KeyStore srckeystore, String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        String newAlias = (dest==null) ? alias : dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
        if (keyStore.containsAlias(newAlias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            if (noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                System.err.println(new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2222
                        "Warning.Overwriting.existing.alias.alias.in.destination.keystore")).format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                String reply = getYesNoReply(new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2225
                        "Existing.entry.alias.alias.exists.overwrite.no.")).format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                if ("NO".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                    newAlias = inputStringFromStdin(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2228
                            ("Enter.new.alias.name.RETURN.to.cancel.import.for.this.entry."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                    if ("".equals(newAlias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                        System.err.println(new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2231
                                "Entry.for.alias.alias.not.imported.")).format(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                                source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                        return 0;
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2239
        Pair<Entry,char[]> objs = recoverEntry(srckeystore, alias, srcstorePass, srckeyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2240
        Entry entry = objs.fst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
        PasswordProtection pp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
        // According to keytool.html, "The destination entry will be protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        // using destkeypass. If destkeypass is not provided, the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
        // entry will be protected with the source entry password."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        // so always try to protect with destKeyPass.
16720
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2248
        char[] newPass = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        if (destKeyPass != null) {
16720
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2250
            newPass = destKeyPass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            pp = new PasswordProtection(destKeyPass);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2252
        } else if (objs.snd != null) {
48333
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2253
            newPass = P12KEYSTORE.equalsIgnoreCase(storetype) ?
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2254
                    storePass : objs.snd;
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2255
            pp = new PasswordProtection(newPass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        try {
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  2259
            Certificate c = srckeystore.getCertificate(alias);
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  2260
            if (c != null) {
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  2261
                checkWeak("<" + newAlias + ">", c);
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  2262
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
            keyStore.setEntry(newAlias, entry, pp);
16720
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2264
            // Place the check so that only successful imports are blocked.
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2265
            // For example, we don't block a failed SecretEntry import.
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2266
            if (P12KEYSTORE.equalsIgnoreCase(storetype)) {
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2267
                if (newPass != null && !Arrays.equals(newPass, storePass)) {
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2268
                    throw new Exception(rb.getString(
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2269
                            "The.destination.pkcs12.keystore.has.different.storepass.and.keypass.Please.retry.with.destkeypass.specified."));
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2270
                }
0e7e9e6ed98a 8010125: keytool -importkeystore could create a pkcs12 keystore with different storepass and keypass
weijun
parents: 16020
diff changeset
  2271
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            Object[] source2 = {alias, kse.toString()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
            MessageFormat form = new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2276
                    "Problem.importing.entry.for.alias.alias.exception.Entry.for.alias.alias.not.imported."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
            System.err.println(form.format(source2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
            return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
    private void doImportKeyStoreAll(KeyStore srckeystore) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        int ok = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        int count = srckeystore.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        for (Enumeration<String> e = srckeystore.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                                        e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            int result = doImportKeyStoreSingle(srckeystore, alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            if (result == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                ok++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                Object[] source = {alias};
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2293
                MessageFormat form = new MessageFormat(rb.getString("Entry.for.alias.alias.successfully.imported."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            } else if (result == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                if (!noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                    String reply = getYesNoReply("Do you want to quit the import process? [no]:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                    if ("YES".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
        Object[] source = {ok, count-ok};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        MessageFormat form = new MessageFormat(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2306
                "Import.command.completed.ok.entries.successfully.imported.fail.entries.failed.or.cancelled"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     * Prints all keystore entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
    private void doPrintEntries(PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
    {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2316
        out.println(rb.getString("Keystore.type.") + keyStore.getType());
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2317
        out.println(rb.getString("Keystore.provider.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                keyStore.getProvider().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        MessageFormat form;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        form = (keyStore.size() == 1) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2324
                        ("Your.keystore.contains.keyStore.size.entry")) :
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2326
                        ("Your.keystore.contains.keyStore.size.entries"));
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25402
diff changeset
  2327
        Object[] source = {keyStore.size()};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
        for (Enumeration<String> e = keyStore.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                                        e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            String alias = e.nextElement();
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2334
            doPrintEntry("<" + alias + ">", alias, out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            if (verbose || rfc) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2336
                out.println(rb.getString("NEWLINE"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2338
                        ("STAR"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2340
                        ("STARNN"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2345
    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
  2346
        return new Iterable<T>() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2347
            @Override
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2348
            public Iterator<T> iterator() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2349
                return new Iterator<T>() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2350
                    @Override
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2351
                    public boolean hasNext() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2352
                        return e.hasMoreElements();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2353
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2354
                    @Override
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2355
                    public T next() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2356
                        return e.nextElement();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2357
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2358
                    public void remove() {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2359
                        throw new UnsupportedOperationException("Not supported yet.");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2360
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2361
                };
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2362
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2363
        };
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2364
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2365
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2366
    /**
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2367
     * 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
  2368
     * @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
  2369
     *        or a bare file path name
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2370
     */
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2371
    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
  2372
        InputStream in = null;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2373
        URI uri = null;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2374
        if (src == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2375
            in = System.in;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2376
        } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2377
            try {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2378
                uri = new URI(src);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2379
                if (uri.getScheme().equals("ldap")) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2380
                    // No input stream for LDAP
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2381
                } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2382
                    in = uri.toURL().openStream();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2383
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2384
            } catch (Exception e) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2385
                try {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2386
                    in = new FileInputStream(src);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2387
                } catch (Exception e2) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2388
                    if (uri == null || uri.getScheme() == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2389
                        throw e2;   // More likely a bare file path
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2390
                    } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2391
                        throw e;    // More likely a protocol or network problem
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2392
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2393
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2394
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2395
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2396
        if (in != null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2397
            try {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2398
                // Read the full stream before feeding to X509Factory,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2399
                // otherwise, keytool -gencrl | keytool -printcrl
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2400
                // might not work properly, since -gencrl is slow
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2401
                // 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
  2402
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2403
                byte[] b = new byte[4096];
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2404
                while (true) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2405
                    int len = in.read(b);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2406
                    if (len < 0) break;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2407
                    bout.write(b, 0, len);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2408
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2409
                return CertificateFactory.getInstance("X509").generateCRLs(
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2410
                        new ByteArrayInputStream(bout.toByteArray()));
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2411
            } finally {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2412
                if (in != System.in) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2413
                    in.close();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2414
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2415
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2416
        } else {    // must be LDAP, and uri is not null
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 30374
diff changeset
  2417
            URICertStoreParameters params =
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 30374
diff changeset
  2418
                new URICertStoreParameters(uri);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 30374
diff changeset
  2419
            CertStore s = CertStore.getInstance("LDAP", params);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 30374
diff changeset
  2420
            return s.getCRLs(new X509CRLSelector());
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2421
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2422
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2423
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2424
    /**
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2425
     * Returns CRLs described in a X509Certificate's CRLDistributionPoints
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2426
     * 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
  2427
     */
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2428
    public static List<CRL> readCRLsFromCert(X509Certificate cert)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2429
            throws Exception {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
  2430
        List<CRL> crls = new ArrayList<>();
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2431
        CRLDistributionPointsExtension ext =
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2432
                X509CertImpl.toImpl(cert).getCRLDistributionPointsExtension();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2433
        if (ext == null) return crls;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  2434
        List<DistributionPoint> distPoints =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  2435
                ext.get(CRLDistributionPointsExtension.POINTS);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  2436
        for (DistributionPoint o: distPoints) {
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2437
            GeneralNames names = o.getFullName();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2438
            if (names != null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2439
                for (GeneralName name: names.names()) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2440
                    if (name.getType() == GeneralNameInterface.NAME_URI) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2441
                        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
  2442
                        for (CRL crl: loadCRLs(uriName.getName())) {
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2443
                            if (crl instanceof X509CRL) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2444
                                crls.add((X509CRL)crl);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2445
                            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2446
                        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2447
                        break;  // Different name should point to same CRL
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2448
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2449
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2450
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2451
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2452
        return crls;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2453
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2454
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2455
    private static String verifyCRL(KeyStore ks, CRL crl)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2456
            throws Exception {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2457
        X509CRLImpl xcrl = (X509CRLImpl)crl;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2458
        X500Principal issuer = xcrl.getIssuerX500Principal();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2459
        for (String s: e2i(ks.aliases())) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2460
            Certificate cert = ks.getCertificate(s);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2461
            if (cert instanceof X509Certificate) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2462
                X509Certificate xcert = (X509Certificate)cert;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2463
                if (xcert.getSubjectX500Principal().equals(issuer)) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2464
                    try {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2465
                        ((X509CRLImpl)crl).verify(cert.getPublicKey());
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2466
                        return s;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2467
                    } catch (Exception e) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2468
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2469
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2470
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2471
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2472
        return null;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2473
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2474
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2475
    private void doPrintCRL(String src, PrintStream out)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2476
            throws Exception {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2477
        for (CRL crl: loadCRLs(src)) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2478
            printCRL(crl, out);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2479
            String issuer = null;
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2480
            Certificate signer = null;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2481
            if (caks != null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2482
                issuer = verifyCRL(caks, crl);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2483
                if (issuer != null) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2484
                    signer = caks.getCertificate(issuer);
11912
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2485
                    out.printf(rb.getString(
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2486
                            "verified.by.s.in.s.weak"),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2487
                            issuer,
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2488
                            "cacerts",
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2489
                            withWeak(signer.getPublicKey()));
11912
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2490
                    out.println();
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2491
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2492
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2493
            if (issuer == null && keyStore != null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2494
                issuer = verifyCRL(keyStore, crl);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2495
                if (issuer != null) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2496
                    signer = keyStore.getCertificate(issuer);
11912
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2497
                    out.printf(rb.getString(
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2498
                            "verified.by.s.in.s.weak"),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2499
                            issuer,
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2500
                            "keystore",
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2501
                            withWeak(signer.getPublicKey()));
11912
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2502
                    out.println();
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2503
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2504
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2505
            if (issuer == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2506
                out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2507
                        ("STAR"));
11912
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2508
                out.println(rb.getString
49b066b32693 7147336: clarification on warning of keytool -printcrl
weijun
parents: 11012
diff changeset
  2509
                        ("warning.not.verified.make.sure.keystore.is.correct"));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2510
                out.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2511
                        ("STARNN"));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2512
            }
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2513
            checkWeak(rb.getString("the.crl"), crl, signer == null ? null : signer.getPublicKey());
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2514
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2515
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2516
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2517
    private void printCRL(CRL crl, PrintStream out)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2518
            throws Exception {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2519
        X509CRL xcrl = (X509CRL)crl;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2520
        if (rfc) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2521
            out.println("-----BEGIN X509 CRL-----");
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  2522
            out.println(Base64.getMimeEncoder(64, CRLF).encodeToString(xcrl.getEncoded()));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2523
            out.println("-----END X509 CRL-----");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2524
        } else {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2525
            String s;
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2526
            if (crl instanceof X509CRLImpl) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2527
                X509CRLImpl x509crl = (X509CRLImpl) crl;
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2528
                s = x509crl.toStringWithAlgName(withWeak("" + x509crl.getSigAlgId()));
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2529
            } else {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2530
                s = crl.toString();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2531
            }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2532
            out.println(s);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2533
        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2534
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  2535
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2536
    private void doPrintCertReq(InputStream in, PrintStream out)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2537
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2538
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2539
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2540
        StringBuffer sb = new StringBuffer();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2541
        boolean started = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2542
        while (true) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2543
            String s = reader.readLine();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2544
            if (s == null) break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2545
            if (!started) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2546
                if (s.startsWith("-----")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2547
                    started = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2548
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2549
            } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2550
                if (s.startsWith("-----")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2551
                    break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2552
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2553
                sb.append(s);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2554
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2555
        }
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  2556
        PKCS10 req = new PKCS10(Pem.decode(new String(sb)));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2557
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2558
        PublicKey pkey = req.getSubjectPublicKeyInfo();
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2559
        out.printf(rb.getString("PKCS.10.with.weak"),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2560
                req.getSubjectName(),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2561
                pkey.getFormat(),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2562
                withWeak(pkey),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2563
                withWeak(req.getSigAlg()));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2564
        for (PKCS10Attribute attr: req.getAttributes().getAttributes()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2565
            ObjectIdentifier oid = attr.getAttributeId();
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 31270
diff changeset
  2566
            if (oid.equals(PKCS9Attribute.EXTENSION_REQUEST_OID)) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2567
                CertificateExtensions exts = (CertificateExtensions)attr.getAttributeValue();
2179
e172c13ca87a 6813402: keytool cannot -printcert entries without extensions
weijun
parents: 2067
diff changeset
  2568
                if (exts != null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2569
                    printExtensions(rb.getString("Extension.Request."), exts, out);
2179
e172c13ca87a 6813402: keytool cannot -printcert entries without extensions
weijun
parents: 2067
diff changeset
  2570
                }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2571
            } else {
19043
42407be709a3 8016916: UnstructuredName should support DirectoryString
juh
parents: 16720
diff changeset
  2572
                out.println("Attribute: " + attr.getAttributeId());
42407be709a3 8016916: UnstructuredName should support DirectoryString
juh
parents: 16720
diff changeset
  2573
                PKCS9Attribute pkcs9Attr =
42407be709a3 8016916: UnstructuredName should support DirectoryString
juh
parents: 16720
diff changeset
  2574
                        new PKCS9Attribute(attr.getAttributeId(),
42407be709a3 8016916: UnstructuredName should support DirectoryString
juh
parents: 16720
diff changeset
  2575
                                           attr.getAttributeValue());
42407be709a3 8016916: UnstructuredName should support DirectoryString
juh
parents: 16720
diff changeset
  2576
                out.print(pkcs9Attr.getName() + ": ");
42407be709a3 8016916: UnstructuredName should support DirectoryString
juh
parents: 16720
diff changeset
  2577
                Object attrVal = attr.getAttributeValue();
42407be709a3 8016916: UnstructuredName should support DirectoryString
juh
parents: 16720
diff changeset
  2578
                out.println(attrVal instanceof String[] ?
42407be709a3 8016916: UnstructuredName should support DirectoryString
juh
parents: 16720
diff changeset
  2579
                            Arrays.toString((String[]) attrVal) :
42407be709a3 8016916: UnstructuredName should support DirectoryString
juh
parents: 16720
diff changeset
  2580
                            attrVal);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2581
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2582
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2583
        if (debug) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2584
            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
  2585
        }
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2586
        checkWeak(rb.getString("the.certificate.request"), req);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2587
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2588
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
     * 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
  2591
     * a human readable format.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
     */
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2593
    private void printCertFromStream(InputStream in, PrintStream out)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
        Collection<? extends Certificate> c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
            c = cf.generateCertificates(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
        } catch (CertificateException ce) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2600
            throw new Exception(rb.getString("Failed.to.parse.input"), ce);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        if (c.isEmpty()) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2603
            throw new Exception(rb.getString("Empty.input"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
        Certificate[] certs = c.toArray(new Certificate[c.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        for (int i=0; i<certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
            X509Certificate x509Cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                x509Cert = (X509Certificate)certs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            } catch (ClassCastException cce) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2611
                throw new Exception(rb.getString("Not.X.509.certificate"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            if (certs.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2615
                        (rb.getString("Certificate.i.1."));
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 25402
diff changeset
  2616
                Object[] source = {i + 1};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
                out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            }
16020
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 14421
diff changeset
  2619
            if (rfc)
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 14421
diff changeset
  2620
                dumpCert(x509Cert, out);
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 14421
diff changeset
  2621
            else
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 14421
diff changeset
  2622
                printX509Cert(x509Cert, out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
            if (i < (certs.length-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
                out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
            }
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2626
            checkWeak(oneInMany(rb.getString("the.certificate"), i, certs.length), x509Cert);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2627
        }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2628
    }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2629
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2630
    private static String oneInMany(String label, int i, int num) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2631
        if (num == 1) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2632
            return label;
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2633
        } else {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2634
            return String.format(rb.getString("one.in.many"), label, i+1, num);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2638
    private void doPrintCert(final PrintStream out) throws Exception {
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2639
        if (jarfile != null) {
41960
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 40393
diff changeset
  2640
            // reset "jdk.certpath.disabledAlgorithms" security property
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 40393
diff changeset
  2641
            // to be able to read jars which were signed with weak algorithms
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 40393
diff changeset
  2642
            Security.setProperty(DisabledAlgorithmConstraints.PROPERTY_JAR_DISABLED_ALGS, "");
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 40393
diff changeset
  2643
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2644
            JarFile jf = new JarFile(jarfile, true);
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2645
            Enumeration<JarEntry> entries = jf.entries();
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
  2646
            Set<CodeSigner> ss = new HashSet<>();
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2647
            byte[] buffer = new byte[8192];
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2648
            int pos = 0;
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2649
            while (entries.hasMoreElements()) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2650
                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
  2651
                try (InputStream is = jf.getInputStream(je)) {
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2652
                    while (is.read(buffer) != -1) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2653
                        // we just read. this will throw a SecurityException
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2654
                        // if a signature/digest check fails. This also
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2655
                        // populate the signers
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2656
                    }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2657
                }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2658
                CodeSigner[] signers = je.getCodeSigners();
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2659
                if (signers != null) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2660
                    for (CodeSigner signer: signers) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2661
                        if (!ss.contains(signer)) {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2662
                            ss.add(signer);
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2663
                            out.printf(rb.getString("Signer.d."), ++pos);
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2664
                            out.println();
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2665
                            out.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2666
                            out.println(rb.getString("Signature."));
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2667
                            out.println();
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2668
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2669
                            List<? extends Certificate> certs
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2670
                                    = signer.getSignerCertPath().getCertificates();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2671
                            int cc = 0;
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2672
                            for (Certificate cert: certs) {
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2673
                                X509Certificate x = (X509Certificate)cert;
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2674
                                if (rfc) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2675
                                    out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2676
                                    dumpCert(x, out);
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2677
                                } else {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2678
                                    printX509Cert(x, out);
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2679
                                }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2680
                                out.println();
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2681
                                checkWeak(oneInMany(rb.getString("the.certificate"), cc++, certs.size()), x);
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2682
                            }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2683
                            Timestamp ts = signer.getTimestamp();
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2684
                            if (ts != null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2685
                                out.println(rb.getString("Timestamp."));
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2686
                                out.println();
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2687
                                certs = ts.getSignerCertPath().getCertificates();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2688
                                cc = 0;
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2689
                                for (Certificate cert: certs) {
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2690
                                    X509Certificate x = (X509Certificate)cert;
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2691
                                    if (rfc) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2692
                                        out.println(rb.getString("Certificate.owner.") + x.getSubjectDN() + "\n");
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2693
                                        dumpCert(x, out);
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2694
                                    } else {
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2695
                                        printX509Cert(x, out);
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2696
                                    }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2697
                                    out.println();
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2698
                                    checkWeak(oneInMany(rb.getString("the.tsa.certificate"), cc++, certs.size()), x);
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2699
                                }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2700
                            }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2701
                        }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2702
                    }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2703
                }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2704
            }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2705
            jf.close();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10051
diff changeset
  2706
            if (ss.isEmpty()) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2707
                out.println(rb.getString("Not.a.signed.jar.file"));
4169
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2708
            }
0ca7e3e74ba4 6890872: keytool -printcert to recognize signed jar files
weijun
parents: 4152
diff changeset
  2709
        } else if (sslserver != null) {
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 30374
diff changeset
  2710
            CertStore cs = SSLServerCertStore.getInstance(new URI("https://" + sslserver));
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2711
            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
  2712
            try {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2713
                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
  2714
                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
  2715
                    // 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
  2716
                    // 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
  2717
                    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
  2718
                                        "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
  2719
                }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2720
            } 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
  2721
                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
  2722
                    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
  2723
                                        "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
  2724
                                        cse.getCause());
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2725
                } else {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2726
                    throw cse;
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2727
                }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2728
            }
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2729
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2730
            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
  2731
            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
  2732
                try {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2733
                    if (rfc) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2734
                        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
  2735
                    } else {
48050
b1993e334da1 8187985: Broken certificate number in debug output
igerasim
parents: 47421
diff changeset
  2736
                        out.println("Certificate #" + i);
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2737
                        out.println("====================================");
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2738
                        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
  2739
                        out.println();
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2740
                    }
48050
b1993e334da1 8187985: Broken certificate number in debug output
igerasim
parents: 47421
diff changeset
  2741
                    checkWeak(oneInMany(rb.getString("the.certificate"), i++, chain.size()), cert);
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2742
                } 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
  2743
                    if (debug) {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2744
                        e.printStackTrace();
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2745
                    }
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2746
                }
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2747
            }
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2748
        } else {
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2749
            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
  2750
                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
  2751
                    printCertFromStream(inStream, out);
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2752
                }
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2753
            } else {
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10436
diff changeset
  2754
                printCertFromStream(System.in, out);
904
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2755
            }
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2756
        }
eadc9fa4b700 6480981: keytool should be able to import certificates from remote SSL servers
weijun
parents: 2
diff changeset
  2757
    }
48333
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2758
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2759
    private void doChangeStorePasswd() throws Exception {
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2760
        storePassNew = newPass;
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2761
        if (storePassNew == null) {
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2762
            storePassNew = getNewPasswd("keystore password", storePass);
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2763
        }
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2764
        if (P12KEYSTORE.equalsIgnoreCase(storetype)) {
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2765
            // When storetype is PKCS12, we need to change all keypass as well
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2766
            for (String alias : Collections.list(keyStore.aliases())) {
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2767
                if (!keyStore.isCertificateEntry(alias)) {
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2768
                    // keyPass should be either null or same with storePass,
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2769
                    // but keep it in case one day we want to "normalize"
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2770
                    // a PKCS12 keystore having different passwords.
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2771
                    Pair<Entry, char[]> objs
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2772
                            = recoverEntry(keyStore, alias, storePass, keyPass);
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2773
                    keyStore.setEntry(alias, objs.fst,
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2774
                            new PasswordProtection(storePassNew));
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2775
                }
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2776
            }
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2777
        }
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2778
    }
f47c18852172 8192988: keytool should support -storepasswd for pkcs12 keystores
weijun
parents: 48216
diff changeset
  2779
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * Creates a self-signed certificate, and stores it as a single-element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     * certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
    private void doSelfCert(String alias, String dname, String sigAlgName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2791
        Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2792
        PrivateKey privKey = (PrivateKey)objs.fst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        if (keyPass == null)
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2794
            keyPass = objs.snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
        // Determine the signature algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
        if (sigAlgName == null) {
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 37782
diff changeset
  2798
            sigAlgName = getCompatibleSigAlgName(privKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
        // Get the old certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
        Certificate oldCert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
        if (oldCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2805
                (rb.getString("alias.has.no.public.key"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
        if (!(oldCert instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2811
                (rb.getString("alias.has.no.X.509.certificate"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
        // convert to X509CertImpl, so that we can modify selected fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
        // (no public APIs available yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
        byte[] encoded = oldCert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
        X509CertImpl certImpl = new X509CertImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
        X509CertInfo certInfo = (X509CertInfo)certImpl.get(X509CertImpl.NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                                                           + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                                                           X509CertImpl.INFO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
        // Extend its validity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
        Date firstDate = getStartDate(startDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        Date lastDate = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
        lastDate.setTime(firstDate.getTime() + validity*1000L*24L*60L*60L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
        CertificateValidity interval = new CertificateValidity(firstDate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                                                               lastDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
        certInfo.set(X509CertInfo.VALIDITY, interval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
        // Make new serial number
2293
cb6d01cb3c3d 6820606: keytool can generate serialno more randomly
weijun
parents: 2289
diff changeset
  2833
        certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(
cb6d01cb3c3d 6820606: keytool can generate serialno more randomly
weijun
parents: 2289
diff changeset
  2834
                    new java.util.Random().nextInt() & 0x7fffffff));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
        // Set owner and issuer fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
        X500Name owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
        if (dname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
            // Get the owner name from the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
            owner = (X500Name)certInfo.get(X509CertInfo.SUBJECT + "." +
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14182
diff changeset
  2841
                                           X509CertInfo.DN_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
            // Use the owner name specified at the command line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
            owner = new X500Name(dname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
            certInfo.set(X509CertInfo.SUBJECT + "." +
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14182
diff changeset
  2846
                         X509CertInfo.DN_NAME, owner);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
        // Make issuer same as owner (self-signed!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
        certInfo.set(X509CertInfo.ISSUER + "." +
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14182
diff changeset
  2850
                     X509CertInfo.DN_NAME, owner);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
        // The inner and outer signature algorithms have to match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
        // The way we achieve that is really ugly, but there seems to be no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        // other solution: We first sign the cert, then retrieve the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        // outer sigalg and use it to set the inner sigalg
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
        X509CertImpl newCert = new X509CertImpl(certInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
        newCert.sign(privKey, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
        AlgorithmId sigAlgid = (AlgorithmId)newCert.get(X509CertImpl.SIG_ALG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
        certInfo.set(CertificateAlgorithmId.NAME + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                     CertificateAlgorithmId.ALGORITHM, sigAlgid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        certInfo.set(X509CertInfo.VERSION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
                        new CertificateVersion(CertificateVersion.V3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2865
        CertificateExtensions ext = createV3Extensions(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2866
                null,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2867
                (CertificateExtensions)certInfo.get(X509CertInfo.EXTENSIONS),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2868
                v3ext,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2869
                oldCert.getPublicKey(),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2870
                null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2871
        certInfo.set(X509CertInfo.EXTENSIONS, ext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        // Sign the new certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
        newCert = new X509CertImpl(certInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        newCert.sign(privKey, sigAlgName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
        // Store the new certificate as a single-element certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
        keyStore.setKeyEntry(alias, privKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
                             (keyPass != null) ? keyPass : storePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
                             new Certificate[] { newCert } );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
        if (verbose) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2882
            System.err.println(rb.getString("New.certificate.self.signed."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
            System.err.print(newCert.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
     * Processes a certificate reply from a certificate authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
     * <p>Builds a certificate chain on top of the certificate reply,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
     * using trusted certificates from the keystore. The chain is complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
     * after a self-signed certificate has been encountered. The self-signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
     * certificate is considered a root certificate authority, and is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
     * at the end of the chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
     * <p>The newly generated chain replaces the old chain associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
     * key entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
     * @return true if the certificate reply was installed, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
    private boolean installReply(String alias, InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
            alias = keyAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2909
        Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2910
        PrivateKey privKey = (PrivateKey)objs.fst;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
        if (keyPass == null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  2912
            keyPass = objs.snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
        Certificate userCert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
        if (userCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2918
                (rb.getString("alias.has.no.public.key.certificate."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
        // Read the certificates in the reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
        Collection<? extends Certificate> c = cf.generateCertificates(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
        if (c.isEmpty()) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2926
            throw new Exception(rb.getString("Reply.has.no.certificates"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
        Certificate[] replyCerts = c.toArray(new Certificate[c.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
        Certificate[] newChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
        if (replyCerts.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
            // single-cert reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
            newChain = establishCertChain(userCert, replyCerts[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
            // cert-chain reply (e.g., PKCS#7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
            newChain = validateReply(alias, userCert, replyCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
        // Now store the newly established chain in the keystore. The new
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2939
        // chain replaces the old one. The chain can be null if user chooses no.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
        if (newChain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
            keyStore.setKeyEntry(alias, privKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
                                 (keyPass != null) ? keyPass : storePass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                                 newChain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
     * Imports a certificate and adds it to the list of trusted certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
     * @return true if the certificate was added, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
    private boolean addTrustedCert(String alias, InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
        if (alias == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2959
            throw new Exception(rb.getString("Must.specify.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
        if (keyStore.containsAlias(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2963
                ("Certificate.not.imported.alias.alias.already.exists"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
        // Read the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
        X509Certificate cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
            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
  2972
        } catch (ClassCastException | CertificateException ce) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2973
            throw new Exception(rb.getString("Input.not.an.X.509.certificate"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2976
        if (noprompt) {
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  2977
            checkWeak(rb.getString("the.input"), cert);
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2978
            keyStore.setCertificateEntry(alias, cert);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2979
            return true;
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2980
        }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2981
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
        // if certificate is self-signed, make sure it verifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
        boolean selfSigned = false;
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 34381
diff changeset
  2984
        if (KeyStoreUtil.isSelfSigned(cert)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
            cert.verify(cert.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
            selfSigned = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        // check if cert already exists in keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
        String reply = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
        String trustalias = keyStore.getCertificateAlias(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
        if (trustalias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  2994
                ("Certificate.already.exists.in.keystore.under.alias.trustalias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
            Object[] source = {trustalias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
            System.err.println(form.format(source));
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2997
            checkWeak(rb.getString("the.input"), cert);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  2998
            printWeakWarnings(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
            reply = getYesNoReply
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3000
                (rb.getString("Do.you.still.want.to.add.it.no."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
        } else if (selfSigned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
            if (trustcacerts && (caks != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
                    ((trustalias=caks.getCertificateAlias(cert)) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3005
                        ("Certificate.already.exists.in.system.wide.CA.keystore.under.alias.trustalias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
                Object[] source = {trustalias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                System.err.println(form.format(source));
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3008
                checkWeak(rb.getString("the.input"), cert);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3009
                printWeakWarnings(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                reply = getYesNoReply
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3011
                        (rb.getString("Do.you.still.want.to.add.it.to.your.own.keystore.no."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
            if (trustalias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
                // Print the cert and ask user if they really want to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                // it to their keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                printX509Cert(cert, System.out);
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3017
                checkWeak(rb.getString("the.input"), cert);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3018
                printWeakWarnings(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                reply = getYesNoReply
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3020
                        (rb.getString("Trust.this.certificate.no."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
        if (reply != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
            if ("YES".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3032
        // Not found in this keystore and not self-signed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
        // Try to establish trust chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
            Certificate[] chain = establishCertChain(null, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
            if (chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
                keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
            // Print the cert and ask user if they really want to add it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
            // their keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
            printX509Cert(cert, System.out);
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3044
            checkWeak(rb.getString("the.input"), cert);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3045
            printWeakWarnings(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
            reply = getYesNoReply
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3047
                (rb.getString("Trust.this.certificate.no."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
            if ("YES".equals(reply)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
                keyStore.setCertificateEntry(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
     * Prompts user for new password. New password must be different from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
     * old one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
     * @param prompt the message that gets prompted on the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
     * @param oldPasswd the current (i.e., old) password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
    private char[] getNewPasswd(String prompt, char[] oldPasswd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
        char[] entered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
        char[] reentered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
        for (int count = 0; count < 3; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3074
                (rb.getString("New.prompt."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
            Object[] source = {prompt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
            System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
            entered = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
            passwords.add(entered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
            if (entered == null || entered.length < 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
                System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3081
                    ("Password.is.too.short.must.be.at.least.6.characters"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
            } else if (Arrays.equals(entered, oldPasswd)) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3083
                System.err.println(rb.getString("Passwords.must.differ"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
                form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3086
                        (rb.getString("Re.enter.new.prompt."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
                Object[] src = {prompt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
                System.err.print(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
                reentered = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
                passwords.add(reentered);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
                if (!Arrays.equals(entered, reentered)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
                    System.err.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3093
                        (rb.getString("They.don.t.match.Try.again"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
                    Arrays.fill(reentered, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
                    return entered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
            if (entered != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
                Arrays.fill(entered, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                entered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
            if (reentered != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
                Arrays.fill(reentered, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
                reentered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        }
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3108
        throw new Exception(rb.getString("Too.many.failures.try.later"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
     * Prompts user for alias name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
     * @param prompt the {0} of "Enter {0} alias name:  " in prompt line
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 29596
diff changeset
  3114
     * @return the string entered by the user, without the \n at the end
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
    private String getAlias(String prompt) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
        if (prompt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3119
                (rb.getString("Enter.prompt.alias.name."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
            Object[] source = {prompt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
            System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
        } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3123
            System.err.print(rb.getString("Enter.alias.name."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
        return (new BufferedReader(new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
                                        System.in))).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
     * Prompts user for an input string from the command line (System.in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
     * @prompt the prompt string printed
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 29596
diff changeset
  3132
     * @return the string entered by the user, without the \n at the end
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
    private String inputStringFromStdin(String prompt) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
        System.err.print(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
        return (new BufferedReader(new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
                                        System.in))).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
     * Prompts user for key password. User may select to choose the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
     * password (<code>otherKeyPass</code>) as for <code>otherAlias</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
    private char[] getKeyPasswd(String alias, String otherAlias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                                char[] otherKeyPass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
        char[] keyPass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
            if (otherKeyPass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3154
                        ("Enter.key.password.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
                System.err.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
                form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3159
                        (".RETURN.if.same.as.for.otherAlias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
                Object[] src = {otherAlias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
                System.err.print(form.format(src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
                MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3164
                        ("Enter.key.password.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
                Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
                System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
            System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
            keyPass = Password.readPassword(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
            passwords.add(keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
            if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
                keyPass = otherKeyPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
        } while ((keyPass == null) && count < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
        if (keyPass == null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3178
            throw new Exception(rb.getString("Too.many.failures.try.later"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
        return keyPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3184
    private String withWeak(String alg) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3185
        if (DISABLED_CHECK.permits(SIG_PRIMITIVE_SET, alg, null)) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3186
            return alg;
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3187
        } else {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3188
            return String.format(rb.getString("with.weak"), alg);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3189
        }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3190
    }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3191
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3192
    private String withWeak(PublicKey key) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3193
        if (DISABLED_CHECK.permits(SIG_PRIMITIVE_SET, key)) {
46139
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 45839
diff changeset
  3194
            int kLen = KeyUtil.getKeySize(key);
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 45839
diff changeset
  3195
            if (kLen >= 0) {
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 45839
diff changeset
  3196
                return String.format(rb.getString("key.bit"),
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 45839
diff changeset
  3197
                        kLen, key.getAlgorithm());
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 45839
diff changeset
  3198
            } else {
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 45839
diff changeset
  3199
                return String.format(
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 45839
diff changeset
  3200
                        rb.getString("unknown.size.1"), key.getAlgorithm());
5196af754957 8185934: keytool shows "Signature algorithm: SHA1withECDSA, -1-bit key"
weijun
parents: 45839
diff changeset
  3201
            }
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3202
        } else {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3203
            return String.format(rb.getString("key.bit.weak"),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3204
                    KeyUtil.getKeySize(key), key.getAlgorithm());
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3205
        }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3206
    }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3207
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
     * Prints a certificate in a human readable format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
    private void printX509Cert(X509Certificate cert, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
        MessageFormat form = new MessageFormat
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3216
                (rb.getString(".PATTERN.printX509Cert.with.weak"));
27038
64cb6608f8a6 8029659: Keytool, print key algorithm of certificate or key entry
weijun
parents: 25859
diff changeset
  3217
        PublicKey pkey = cert.getPublicKey();
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  3218
        String sigName = cert.getSigAlgName();
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  3219
        // No need to warn about sigalg of a trust anchor
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  3220
        if (!isTrustedCert(cert)) {
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  3221
            sigName = withWeak(sigName);
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  3222
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
        Object[] source = {cert.getSubjectDN().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
                        cert.getIssuerDN().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
                        cert.getSerialNumber().toString(16),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
                        cert.getNotBefore().toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
                        cert.getNotAfter().toString(),
34381
be5600f6a4da 8141457: keytool default cert fingerprint algorithm should be SHA-256
weijun
parents: 31426
diff changeset
  3228
                        getCertFingerPrint("SHA-1", cert),
3318
dade78e63c92 6561126: keytool should use larger default keysize for keypairs
weijun
parents: 3316
diff changeset
  3229
                        getCertFingerPrint("SHA-256", cert),
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  3230
                        sigName,
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3231
                        withWeak(pkey),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3232
                        cert.getVersion()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
                        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
        out.println(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
        if (cert instanceof X509CertImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
            X509CertImpl impl = (X509CertImpl)cert;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3238
            X509CertInfo certInfo = (X509CertInfo)impl.get(X509CertImpl.NAME
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3239
                                                           + "." +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3240
                                                           X509CertImpl.INFO);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3241
            CertificateExtensions exts = (CertificateExtensions)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3242
                    certInfo.get(X509CertInfo.EXTENSIONS);
2179
e172c13ca87a 6813402: keytool cannot -printcert entries without extensions
weijun
parents: 2067
diff changeset
  3243
            if (exts != null) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3244
                printExtensions(rb.getString("Extensions."), exts, out);
2179
e172c13ca87a 6813402: keytool cannot -printcert entries without extensions
weijun
parents: 2067
diff changeset
  3245
            }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3246
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3247
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3248
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3249
    private static void printExtensions(String title, CertificateExtensions exts, PrintStream out)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3250
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3251
        int extnum = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3252
        Iterator<Extension> i1 = exts.getAllExtensions().iterator();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3253
        Iterator<Extension> i2 = exts.getUnparseableExtensions().values().iterator();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3254
        while (i1.hasNext() || i2.hasNext()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3255
            Extension ext = i1.hasNext()?i1.next():i2.next();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3256
            if (extnum == 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3257
                out.println();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3258
                out.println(title);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3259
                out.println();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3260
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3261
            out.print("#"+(++extnum)+": "+ ext);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3262
            if (ext.getClass() == Extension.class) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3263
                byte[] v = ext.getExtensionValue();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3264
                if (v.length == 0) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3265
                    out.println(rb.getString(".Empty.value."));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3266
                } else {
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 34382
diff changeset
  3267
                    new sun.security.util.HexDumpEncoder().encodeBuffer(ext.getExtensionValue(), out);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3268
                    out.println();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
            }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3271
            out.println();
2
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
    /**
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3276
     * 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
  3277
     * returns the signer's certificate.
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3278
     * @param cert the certificate whose signer is searched, not null
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3279
     * @param ks the keystore to search with, not null
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3280
     * @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
  3281
     * or a certificate inside <code>ks</code> who signs <code>cert</code>,
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3282
     * or null otherwise. A label is added.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
     */
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3284
    private static Pair<String,Certificate>
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  3285
            getSigner(Certificate cert, KeyStore ks) throws Exception {
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3286
        if (ks.getCertificateAlias(cert) != null) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3287
            return new Pair<>("", cert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
        }
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3289
        for (Enumeration<String> aliases = ks.aliases();
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3290
                aliases.hasMoreElements(); ) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3291
            String name = aliases.nextElement();
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3292
            Certificate trustedCert = ks.getCertificate(name);
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3293
            if (trustedCert != null) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3294
                try {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3295
                    cert.verify(trustedCert.getPublicKey());
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3296
                    return new Pair<>(name, trustedCert);
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3297
                } catch (Exception e) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3298
                    // Not verified, skip to the next one
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3299
                }
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3300
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
        }
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3302
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
     * Gets an X.500 name suitable for inclusion in a certification request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
    private X500Name getX500Name() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
        BufferedReader in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
        in = new BufferedReader(new InputStreamReader(System.in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
        String commonName = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
        String organizationalUnit = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
        String organization = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
        String city = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
        String state = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
        String country = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
        X500Name name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
        String userInput = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
        int maxRetry = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
            if (maxRetry-- < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
                throw new RuntimeException(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3324
                        "Too.many.retries.program.terminated"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
            commonName = inputString(in,
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3327
                    rb.getString("What.is.your.first.and.last.name."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
                    commonName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
            organizationalUnit = inputString(in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
                    rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3331
                        ("What.is.the.name.of.your.organizational.unit."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                    organizationalUnit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
            organization = inputString(in,
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3334
                    rb.getString("What.is.the.name.of.your.organization."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
                    organization);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
            city = inputString(in,
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3337
                    rb.getString("What.is.the.name.of.your.City.or.Locality."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
                    city);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
            state = inputString(in,
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3340
                    rb.getString("What.is.the.name.of.your.State.or.Province."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
                    state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
            country = inputString(in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
                    rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3344
                        ("What.is.the.two.letter.country.code.for.this.unit."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
                    country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
            name = new X500Name(commonName, organizationalUnit, organization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
                                city, state, country);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3349
                (rb.getString("Is.name.correct."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
            Object[] source = {name};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
            userInput = inputString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
                (in, form.format(source), rb.getString("no"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
        } while (collator.compare(userInput, rb.getString("yes")) != 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
                 collator.compare(userInput, rb.getString("y")) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
    private String inputString(BufferedReader in, String prompt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
                               String defaultValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
        System.err.println(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
        MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3366
                (rb.getString(".defaultValue."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
        Object[] source = {defaultValue};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
        System.err.print(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
        System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
        String value = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
        if (value == null || collator.compare(value, "") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
            value = defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
     * Writes an X.509 certificate in base64 or binary encoding to an output
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
    private void dumpCert(Certificate cert, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
        throws IOException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
        if (rfc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
            out.println(X509Factory.BEGIN_CERT);
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  3387
            out.println(Base64.getMimeEncoder(64, CRLF).encodeToString(cert.getEncoded()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
            out.println(X509Factory.END_CERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
            out.write(cert.getEncoded()); // binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
     * Converts a byte to hex digit and writes to the supplied buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
    private void byte2hex(byte b, StringBuffer buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
                            '9', 'A', 'B', 'C', 'D', 'E', 'F' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
        int high = ((b & 0xf0) >> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
        int low = (b & 0x0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
        buf.append(hexChars[high]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
        buf.append(hexChars[low]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
     * Converts a byte array to hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
    private String toHexString(byte[] block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
        int len = block.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
             byte2hex(block[i], buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
             if (i < len-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
                 buf.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
     * Recovers (private) key associated with given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
     * @return an array of objects, where the 1st element in the array is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
     * recovered private key, and the 2nd element is the password used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
     * recover it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
     */
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3428
    private Pair<Key,char[]> recoverKey(String alias, char[] storePass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
                                       char[] keyPass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
        Key key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
        if (keyStore.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3436
                (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
        if (!keyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
                !keyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3443
                (rb.getString("Alias.alias.has.no.key"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
        if (keyPass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
            // Try to recover the key using the keystore password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
                key = keyStore.getKey(alias, storePass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
                keyPass = storePass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
                passwords.add(keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
            } catch (UnrecoverableKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                // Did not work out, so prompt user for key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                if (!token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                    keyPass = getKeyPasswd(alias, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
                    key = keyStore.getKey(alias, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
            key = keyStore.getKey(alias, keyPass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3468
        return Pair.of(key, keyPass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
     * Recovers entry associated with given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
     * @return an array of objects, where the 1st element in the array is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
     * recovered entry, and the 2nd element is the password used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
     * recover it (null if no password).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
     */
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3478
    private Pair<Entry,char[]> recoverEntry(KeyStore ks,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
                            String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
                            char[] pstore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
                            char[] pkey) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
        if (ks.containsAlias(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
            MessageFormat form = new MessageFormat
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3485
                (rb.getString("Alias.alias.does.not.exist"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
        PasswordProtection pp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
        Entry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
            // First attempt to access entry without key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
            // (PKCS11 entry or trusted certificate entry, for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
            entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
            pkey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
        } catch (UnrecoverableEntryException une) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
            if(P11KEYSTORE.equalsIgnoreCase(ks.getType()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
                KeyStoreUtil.isWindowsKeyStore(ks.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
                // should not happen, but a possibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
                throw une;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
            // entry is protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
            if (pkey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
                // try provided key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
                pp = new PasswordProtection(pkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
                entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
                // try store pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
                    pp = new PasswordProtection(pstore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
                    entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
                    pkey = pstore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
                } catch (UnrecoverableEntryException une2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
                    if (P12KEYSTORE.equalsIgnoreCase(ks.getType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
                        // P12 keystore currently does not support separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
                        // store and entry passwords
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
                        throw une2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
                        // prompt for entry password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
                        pkey = getKeyPasswd(alias, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
                        pp = new PasswordProtection(pkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
                        entry = ks.getEntry(alias, pp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3543
        return Pair.of(entry, pkey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
     * Gets the requested finger print of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
    private String getCertFingerPrint(String mdAlg, Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
        byte[] encCertInfo = cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
        MessageDigest md = MessageDigest.getInstance(mdAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
        byte[] digest = md.digest(encCertInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
        return toHexString(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
     * Prints warning about missing integrity check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
     */
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3560
    private void printNoIntegrityWarning() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3563
            (".WARNING.WARNING.WARNING."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3565
            (".The.integrity.of.the.information.stored.in.your.keystore."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
        System.err.println(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3567
            (".WARNING.WARNING.WARNING."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
     * Validates chain in certification reply, and returns the ordered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
     * elements of the chain (with user certificate first, and root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
     * certificate last in the array).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
     * @param userCert the user certificate of the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
     * @param replyCerts the chain provided in the reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
    private Certificate[] validateReply(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
                                        Certificate userCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
                                        Certificate[] replyCerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
    {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3585
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3586
        checkWeak(rb.getString("reply"), replyCerts);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3587
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
        // order the certs in the reply (bottom-up).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
        // we know that all certs in the reply are of type X.509, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
        // we parsed them using an X.509 certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
        PublicKey userPubKey = userCert.getPublicKey();
39046
db1dddd32a8f 6968542: keytool -importcert cannot deal with duplicate certs
weijun
parents: 38855
diff changeset
  3593
db1dddd32a8f 6968542: keytool -importcert cannot deal with duplicate certs
weijun
parents: 38855
diff changeset
  3594
        // Remove duplicated certificates.
db1dddd32a8f 6968542: keytool -importcert cannot deal with duplicate certs
weijun
parents: 38855
diff changeset
  3595
        HashSet<Certificate> nodup = new HashSet<>(Arrays.asList(replyCerts));
db1dddd32a8f 6968542: keytool -importcert cannot deal with duplicate certs
weijun
parents: 38855
diff changeset
  3596
        replyCerts = nodup.toArray(new Certificate[nodup.size()]);
db1dddd32a8f 6968542: keytool -importcert cannot deal with duplicate certs
weijun
parents: 38855
diff changeset
  3597
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
        for (i=0; i<replyCerts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
            if (userPubKey.equals(replyCerts[i].getPublicKey())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
        if (i == replyCerts.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3605
                ("Certificate.reply.does.not.contain.public.key.for.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
            Object[] source = {alias};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
            throw new Exception(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
        Certificate tmpCert = replyCerts[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
        replyCerts[0] = replyCerts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
        replyCerts[i] = tmpCert;
2437
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  3613
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  3614
        X509Certificate thisCert = (X509Certificate)replyCerts[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
        for (i=1; i < replyCerts.length-1; i++) {
2437
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  3617
            // find a cert in the reply who signs thisCert
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
            int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
            for (j=i; j<replyCerts.length; j++) {
34382
5d11306d6969 8130132: jarsigner should emit warning if weak algorithms or keysizes are used
weijun
parents: 34381
diff changeset
  3620
                if (KeyStoreUtil.signedBy(thisCert, (X509Certificate)replyCerts[j])) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
                    tmpCert = replyCerts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
                    replyCerts[i] = replyCerts[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
                    replyCerts[j] = tmpCert;
2437
098db6faaf66 6825352: support self-issued certificate in keytool
weijun
parents: 2432
diff changeset
  3624
                    thisCert = (X509Certificate)replyCerts[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
            if (j == replyCerts.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
                throw new Exception
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3630
                    (rb.getString("Incomplete.certificate.chain.in.reply"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
        if (noprompt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
            return replyCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3638
        // do we trust the cert at the top?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
        Certificate topCert = replyCerts[replyCerts.length-1];
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3640
        boolean fromKeyStore = true;
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  3641
        Pair<String,Certificate> root = getSigner(topCert, keyStore);
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3642
        if (root == null && trustcacerts && caks != null) {
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  3643
            root = getSigner(topCert, caks);
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3644
            fromKeyStore = false;
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3645
        }
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3646
        if (root == null) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3647
            System.err.println();
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3648
            System.err.println
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3649
                    (rb.getString("Top.level.certificate.in.reply."));
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3650
            printX509Cert((X509Certificate)topCert, System.out);
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3651
            System.err.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3652
            System.err.print(rb.getString(".is.not.trusted."));
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3653
            printWeakWarnings(true);
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3654
            String reply = getYesNoReply
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3655
                    (rb.getString("Install.reply.anyway.no."));
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3656
            if ("NO".equals(reply)) {
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3657
                return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
            }
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3659
        } else {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3660
            if (root.snd != topCert) {
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3661
                // append the root CA cert to the chain
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3662
                Certificate[] tmpCerts =
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3663
                    new Certificate[replyCerts.length+1];
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3664
                System.arraycopy(replyCerts, 0, tmpCerts, 0,
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3665
                                 replyCerts.length);
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3666
                tmpCerts[tmpCerts.length-1] = root.snd;
5296
8e6ab18c0fde 6937978: let keytool -gencert generate the chain
weijun
parents: 5164
diff changeset
  3667
                replyCerts = tmpCerts;
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3668
                checkWeak(String.format(rb.getString(fromKeyStore ?
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3669
                                            "alias.in.keystore" :
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3670
                                            "alias.in.cacerts"),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3671
                                        root.fst),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3672
                          root.snd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
        return replyCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
     * Establishes a certificate chain (using trusted certificates in the
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3680
     * keystore and cacerts), starting with the reply (certToVerify)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
     * and ending at a self-signed certificate found in the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
     *
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3683
     * @param userCert optional existing certificate, mostly likely be the
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3684
     *                 original self-signed cert created by -genkeypair.
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3685
     *                 It must have the same public key as certToVerify
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3686
     *                 but cannot be the same cert.
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3687
     * @param certToVerify the starting certificate to build the chain
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3688
     * @returns the established chain, might be null if user decides not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
    private Certificate[] establishCertChain(Certificate userCert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
                                             Certificate certToVerify)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
        if (userCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
            // Make sure that the public key of the certificate reply matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
            // the original public key in the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
            PublicKey origPubKey = userCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
            PublicKey replyPubKey = certToVerify.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
            if (!origPubKey.equals(replyPubKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
                throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3701
                        ("Public.keys.in.reply.and.keystore.don.t.match"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
            // If the two certs are identical, we're done: no need to import
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
            // anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
            if (certToVerify.equals(userCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
                throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3708
                        ("Certificate.reply.and.certificate.in.keystore.are.identical"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
        // Build a hash table of all certificates in the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
        // Use the subject distinguished name as the key into the hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
        // All certificates associated with the same subject distinguished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
        // name are stored in the same hash table entry as a vector.
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3716
        Hashtable<Principal, Vector<Pair<String,X509Certificate>>> certs = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
        if (keyStore.size() > 0) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3718
            certs = new Hashtable<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
            keystorecerts2Hashtable(keyStore, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
        if (trustcacerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
            if (caks!=null && caks.size()>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
                if (certs == null) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3724
                    certs = new Hashtable<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
                keystorecerts2Hashtable(caks, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
        // start building chain
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3731
        Vector<Pair<String,X509Certificate>> chain = new Vector<>(2);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3732
        if (buildChain(
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3733
                new Pair<>(rb.getString("the.input"),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3734
                           (X509Certificate) certToVerify),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3735
                chain, certs)) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3736
            for (Pair<String,X509Certificate> p : chain) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3737
                checkWeak(p.fst, p.snd);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3738
            }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3739
            Certificate[] newChain =
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3740
                    new Certificate[chain.size()];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
            // buildChain() returns chain with self-signed root-cert first and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
            // user-cert last, so we need to invert the chain before we store
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
            // it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
            int j=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
            for (int i=chain.size()-1; i>=0; i--) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3746
                newChain[j] = chain.elementAt(i).snd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
            return newChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
            throw new Exception
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3752
                (rb.getString("Failed.to.establish.chain.from.reply"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
    /**
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3757
     * Recursively tries to establish chain from pool of certs starting from
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3758
     * certToVerify until a self-signed cert is found, and fill the certs found
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3759
     * into chain. Each cert in the chain signs the next one.
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3760
     *
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3761
     * This method is able to recover from an error, say, if certToVerify
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3762
     * is signed by certA but certA has no issuer in certs and itself is not
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3763
     * self-signed, the method can try another certB that also signs
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3764
     * certToVerify and look for signer of certB, etc, etc.
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3765
     *
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3766
     * Each cert in chain comes with a label showing its origin. The label is
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3767
     * used in the warning message when the cert is considered a risk.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
     * @param certToVerify the cert that needs to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
     * @param chain the chain that's being built.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
     * @param certs the pool of trusted certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
     * @return true if successful, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
     */
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3775
    private boolean buildChain(Pair<String,X509Certificate> certToVerify,
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3776
            Vector<Pair<String,X509Certificate>> chain,
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3777
            Hashtable<Principal, Vector<Pair<String,X509Certificate>>> certs) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3778
        if (KeyStoreUtil.isSelfSigned(certToVerify.snd)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
            // reached self-signed root cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
            // no verification needed because it's trusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
            chain.addElement(certToVerify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3785
        Principal issuer = certToVerify.snd.getIssuerDN();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3786
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
        // Get the issuer's certificate(s)
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3788
        Vector<Pair<String,X509Certificate>> vec = certs.get(issuer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
        if (vec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
        // Try out each certificate in the vector, until we find one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
        // whose public key verifies the signature of the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
        // in question.
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3796
        for (Enumeration<Pair<String,X509Certificate>> issuerCerts = vec.elements();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3797
                issuerCerts.hasMoreElements(); ) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3798
            Pair<String,X509Certificate> issuerCert = issuerCerts.nextElement();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3799
            PublicKey issuerPubKey = issuerCert.snd.getPublicKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
            try {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3801
                certToVerify.snd.verify(issuerPubKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
            if (buildChain(issuerCert, chain, certs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
                chain.addElement(certToVerify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
     * Prompts user for yes/no decision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
     * @return the user's decision, can only be "YES" or "NO"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
    private String getYesNoReply(String prompt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
        String reply = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
        int maxRetry = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
            if (maxRetry-- < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
                throw new RuntimeException(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3826
                        "Too.many.retries.program.terminated"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
            System.err.print(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
            System.err.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
            reply = (new BufferedReader(new InputStreamReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
                                        (System.in))).readLine();
43238
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents: 41960
diff changeset
  3832
            if (reply == null ||
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents: 41960
diff changeset
  3833
                collator.compare(reply, "") == 0 ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
                collator.compare(reply, rb.getString("n")) == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
                collator.compare(reply, rb.getString("no")) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
                reply = "NO";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
            } else if (collator.compare(reply, rb.getString("y")) == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
                       collator.compare(reply, rb.getString("yes")) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
                reply = "YES";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
            } else {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3841
                System.err.println(rb.getString("Wrong.answer.try.again"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
                reply = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
        } while (reply == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
        return reply;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
     * Stores the (leaf) certificates of a keystore in a hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
     * All certs belonging to the same CA are stored in a vector that
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3851
     * in turn is stored in the hashtable, keyed by the CA's subject DN.
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3852
     * Each cert comes with a string label that shows its origin and alias.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
    private void keystorecerts2Hashtable(KeyStore ks,
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3855
                Hashtable<Principal, Vector<Pair<String,X509Certificate>>> hash)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
        for (Enumeration<String> aliases = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
                                        aliases.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
            String alias = aliases.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
            Certificate cert = ks.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
            if (cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
                Principal subjectDN = ((X509Certificate)cert).getSubjectDN();
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3864
                Pair<String,X509Certificate> pair = new Pair<>(
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3865
                        String.format(
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3866
                                rb.getString(ks == caks ?
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3867
                                        "alias.in.cacerts" :
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3868
                                        "alias.in.keystore"),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3869
                                alias),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3870
                        (X509Certificate)cert);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3871
                Vector<Pair<String,X509Certificate>> vec = hash.get(subjectDN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
                if (vec == null) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3873
                    vec = new Vector<>();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3874
                    vec.addElement(pair);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
                } else {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3876
                    if (!vec.contains(pair)) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  3877
                        vec.addElement(pair);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
                hash.put(subjectDN, vec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
     * Returns the issue time that's specified the -startdate option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
     * @param s the value of -startdate option
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
    private static Date getStartDate(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
        Calendar c = new GregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
            IOException ioe = new IOException(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  3893
                    rb.getString("Illegal.startdate.value"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
            int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
            if (s.charAt(0) == '-' || s.charAt(0) == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
                // Form 1: ([+-]nnn[ymdHMS])+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
                int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
                while (start < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
                    int sign = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
                    switch (s.charAt(start)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
                        case '+': sign = 1; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
                        case '-': sign = -1; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
                        default: throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
                    int i = start+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
                    for (; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
                        char ch = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
                        if (ch < '0' || ch > '9') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
                    if (i == start+1) throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
                    int number = Integer.parseInt(s.substring(start+1, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
                    if (i >= len) throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
                    int unit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
                    switch (s.charAt(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
                        case 'y': unit = Calendar.YEAR; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
                        case 'm': unit = Calendar.MONTH; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
                        case 'd': unit = Calendar.DATE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
                        case 'H': unit = Calendar.HOUR; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
                        case 'M': unit = Calendar.MINUTE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
                        case 'S': unit = Calendar.SECOND; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
                        default: throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
                    c.add(unit, sign * number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
                    start = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
            } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
                // Form 2: [yyyy/mm/dd] [HH:MM:SS]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
                String date = null, time = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
                if (len == 19) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
                    date = s.substring(0, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
                    time = s.substring(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
                    if (s.charAt(10) != ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
                        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
                } else if (len == 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
                    date = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
                } else if (len == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
                    time = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
                    throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
                if (date != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
                    if (date.matches("\\d\\d\\d\\d\\/\\d\\d\\/\\d\\d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
                        c.set(Integer.valueOf(date.substring(0, 4)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
                                Integer.valueOf(date.substring(5, 7))-1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
                                Integer.valueOf(date.substring(8, 10)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
                        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
                if (time != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
                    if (time.matches("\\d\\d:\\d\\d:\\d\\d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
                        c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(time.substring(0, 2)));
40393
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 40253
diff changeset
  3956
                        c.set(Calendar.MINUTE, Integer.valueOf(time.substring(3, 5)));
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 40253
diff changeset
  3957
                        c.set(Calendar.SECOND, Integer.valueOf(time.substring(6, 8)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
                        c.set(Calendar.MILLISECOND, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
                        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
        return c.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
    /**
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3969
     * Match a command (may be abbreviated) with a command set.
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3970
     * @param s the command provided
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3971
     * @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
  3972
     * 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
  3973
     * existence should not be revealed to user.
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3974
     * @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
  3975
     * @throws Exception if s is ambiguous
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3976
     */
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3977
    private static int oneOf(String s, String... list) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3978
        int[] match = new int[list.length];
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3979
        int nmatch = 0;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3980
        int experiment = Integer.MAX_VALUE;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3981
        for (int i = 0; i<list.length; i++) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3982
            String one = list[i];
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3983
            if (one == null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3984
                experiment = i;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3985
                continue;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  3986
            }
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3987
            if (one.toLowerCase(Locale.ENGLISH)
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  3988
                    .startsWith(s.toLowerCase(Locale.ENGLISH))) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3989
                match[nmatch++] = i;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3990
            } else {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24868
diff changeset
  3991
                StringBuilder sb = new StringBuilder();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3992
                boolean first = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3993
                for (char c: one.toCharArray()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3994
                    if (first) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3995
                        sb.append(c);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3996
                        first = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3997
                    } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3998
                        if (!Character.isLowerCase(c)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  3999
                            sb.append(c);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4000
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4001
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4002
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4003
                if (sb.toString().equalsIgnoreCase(s)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4004
                    match[nmatch++] = i;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4005
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4006
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4007
        }
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4008
        if (nmatch == 0) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4009
            return -1;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4010
        } else if (nmatch == 1) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4011
            return match[0];
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4012
        } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4013
            // If multiple matches is in experimental commands, ignore them
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4014
            if (match[1] > experiment) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4015
                return match[0];
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4016
            }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24868
diff changeset
  4017
            StringBuilder sb = new StringBuilder();
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4018
            MessageFormat form = new MessageFormat(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4019
                ("command.{0}.is.ambiguous."));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4020
            Object[] source = {s};
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4021
            sb.append(form.format(source));
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4022
            sb.append("\n    ");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4023
            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
  4024
                sb.append(' ');
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4025
                sb.append(list[match[i]]);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4026
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4027
            throw new Exception(sb.toString());
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4028
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4029
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4030
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4031
    /**
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4032
     * Create a GeneralName object from known types
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4033
     * @param t one of 5 known types
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4034
     * @param v value
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4035
     * @return which one
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4036
     */
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4037
    private GeneralName createGeneralName(String t, String v)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4038
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4039
        GeneralNameInterface gn;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4040
        int p = oneOf(t, "EMAIL", "URI", "DNS", "IP", "OID");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4041
        if (p < 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4042
            throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4043
                    "Unrecognized.GeneralName.type.") + t);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4044
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4045
        switch (p) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4046
            case 0: gn = new RFC822Name(v); break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4047
            case 1: gn = new URIName(v); break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4048
            case 2: gn = new DNSName(v); break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4049
            case 3: gn = new IPAddressName(v); break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4050
            default: gn = new OIDName(v); break; //4
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4051
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4052
        return new GeneralName(gn);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4053
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4054
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4055
    private static final String[] extSupported = {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4056
                        "BasicConstraints",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4057
                        "KeyUsage",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4058
                        "ExtendedKeyUsage",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4059
                        "SubjectAlternativeName",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4060
                        "IssuerAlternativeName",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4061
                        "SubjectInfoAccess",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4062
                        "AuthorityInfoAccess",
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4063
                        null,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4064
                        "CRLDistributionPoints",
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4065
    };
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4066
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4067
    private ObjectIdentifier findOidForExtName(String type)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4068
            throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4069
        switch (oneOf(type, extSupported)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4070
            case 0: return PKIXExtensions.BasicConstraints_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4071
            case 1: return PKIXExtensions.KeyUsage_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4072
            case 2: return PKIXExtensions.ExtendedKeyUsage_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4073
            case 3: return PKIXExtensions.SubjectAlternativeName_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4074
            case 4: return PKIXExtensions.IssuerAlternativeName_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4075
            case 5: return PKIXExtensions.SubjectInfoAccess_Id;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4076
            case 6: return PKIXExtensions.AuthInfoAccess_Id;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4077
            case 8: return PKIXExtensions.CRLDistributionPoints_Id;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4078
            default: return new ObjectIdentifier(type);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4079
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4080
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4081
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4082
    // Add an extension into a CertificateExtensions, always using OID as key
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4083
    private static void setExt(CertificateExtensions result, Extension ex)
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4084
            throws IOException {
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4085
        result.set(ex.getId(), ex);
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4086
    }
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4087
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4088
    /**
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4089
     * Create X509v3 extensions from a string representation. Note that the
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4090
     * SubjectKeyIdentifierExtension will always be created non-critical besides
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4091
     * the extension requested in the <code>extstr</code> argument.
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4092
     *
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4093
     * @param requestedEx the requested extensions, can be null, used for -gencert
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4094
     * @param existingEx the original extensions, can be null, used for -selfcert
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4095
     * @param extstrs -ext values, Read keytool doc
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4096
     * @param pkey the public key for the certificate
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4097
     * @param akey the public key for the authority (issuer)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4098
     * @return the created CertificateExtensions
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4099
     */
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4100
    private CertificateExtensions createV3Extensions(
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4101
            CertificateExtensions requestedEx,
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4102
            CertificateExtensions existingEx,
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4103
            List <String> extstrs,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4104
            PublicKey pkey,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4105
            PublicKey akey) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4106
29225
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4107
        // By design, inside a CertificateExtensions object, all known
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4108
        // extensions uses name (say, "BasicConstraints") as key and
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4109
        // a child Extension type (say, "BasicConstraintsExtension")
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4110
        // as value, unknown extensions uses OID as key and bare
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4111
        // Extension object as value. This works fine inside JDK.
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4112
        //
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4113
        // However, in keytool, there is no way to prevent people
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4114
        // using OID in -ext, either as a new extension, or in a
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4115
        // honored value. Thus here we (ab)use CertificateExtensions
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4116
        // by always using OID as key and value can be of any type.
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4117
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4118
        if (existingEx != null && requestedEx != null) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4119
            // This should not happen
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4120
            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
  4121
        }
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4122
        // A new extensions always using OID as key
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4123
        CertificateExtensions result = new CertificateExtensions();
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4124
        if (existingEx != null) {
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4125
            for (Extension ex: existingEx.getAllExtensions()) {
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4126
                setExt(result, ex);
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4127
            }
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4128
        }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4129
        try {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4130
            // name{:critical}{=value}
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4131
            // Honoring requested extensions
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4132
            if (requestedEx != null) {
29225
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4133
                // The existing requestedEx might use names as keys,
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4134
                // translate to all-OID first.
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4135
                CertificateExtensions request2 = new CertificateExtensions();
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4136
                for (sun.security.x509.Extension ex: requestedEx.getAllExtensions()) {
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4137
                    request2.set(ex.getId(), ex);
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4138
                }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4139
                for(String extstr: extstrs) {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4140
                    if (extstr.toLowerCase(Locale.ENGLISH).startsWith("honored=")) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4141
                        List<String> list = Arrays.asList(
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4142
                                extstr.toLowerCase(Locale.ENGLISH).substring(8).split(","));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4143
                        // First check existence of "all"
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4144
                        if (list.contains("all")) {
29225
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4145
                            for (Extension ex: request2.getAllExtensions()) {
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4146
                                setExt(result, ex);
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4147
                            }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4148
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4149
                        // one by one for others
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4150
                        for (String item: list) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4151
                            if (item.equals("all")) continue;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4152
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4153
                            // add or remove
29110
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  4154
                            boolean add;
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  4155
                            // -1, unchanged, 0 critical, 1 non-critical
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4156
                            int action = -1;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4157
                            String type = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4158
                            if (item.startsWith("-")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4159
                                add = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4160
                                type = item.substring(1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4161
                            } else {
29110
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  4162
                                add = true;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4163
                                int colonpos = item.indexOf(':');
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4164
                                if (colonpos >= 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4165
                                    type = item.substring(0, colonpos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4166
                                    action = oneOf(item.substring(colonpos+1),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4167
                                            "critical", "non-critical");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4168
                                    if (action == -1) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4169
                                        throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4170
                                            ("Illegal.value.") + item);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4171
                                    }
29110
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  4172
                                } else {
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  4173
                                    type = item;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4174
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4175
                            }
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4176
                            String n = findOidForExtName(type).toString();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4177
                            if (add) {
29225
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29111
diff changeset
  4178
                                Extension e = request2.get(n);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4179
                                if (!e.isCritical() && action == 0
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4180
                                        || e.isCritical() && action == 1) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4181
                                    e = Extension.newExtension(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4182
                                            e.getExtensionId(),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4183
                                            !e.isCritical(),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4184
                                            e.getExtensionValue());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4185
                                }
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4186
                                setExt(result, e);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4187
                            } else {
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4188
                                result.delete(n);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4189
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4190
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4191
                        break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4192
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4193
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4194
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4195
            for(String extstr: extstrs) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4196
                String name, value;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4197
                boolean isCritical = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4198
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4199
                int eqpos = extstr.indexOf('=');
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4200
                if (eqpos >= 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4201
                    name = extstr.substring(0, eqpos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4202
                    value = extstr.substring(eqpos+1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4203
                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4204
                    name = extstr;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4205
                    value = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4206
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4207
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4208
                int colonpos = name.indexOf(':');
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4209
                if (colonpos >= 0) {
3316
32d30c561c5a 6847026: keytool should be able to generate certreq and cert without subject name
weijun
parents: 2437
diff changeset
  4210
                    if (oneOf(name.substring(colonpos+1), "critical") == 0) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4211
                        isCritical = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4212
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4213
                    name = name.substring(0, colonpos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4214
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4215
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4216
                if (name.equalsIgnoreCase("honored")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4217
                    continue;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4218
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4219
                int exttype = oneOf(name, extSupported);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4220
                switch (exttype) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4221
                    case 0:     // BC
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4222
                        int pathLen = -1;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4223
                        boolean isCA = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4224
                        if (value == null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4225
                            isCA = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4226
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4227
                            try {   // the abbr format
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4228
                                pathLen = Integer.parseInt(value);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4229
                                isCA = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4230
                            } catch (NumberFormatException ufe) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4231
                                // ca:true,pathlen:1
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4232
                                for (String part: value.split(",")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4233
                                    String[] nv = part.split(":");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4234
                                    if (nv.length != 2) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4235
                                        throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4236
                                                ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4237
                                    } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4238
                                        if (nv[0].equalsIgnoreCase("ca")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4239
                                            isCA = Boolean.parseBoolean(nv[1]);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4240
                                        } else if (nv[0].equalsIgnoreCase("pathlen")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4241
                                            pathLen = Integer.parseInt(nv[1]);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4242
                                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4243
                                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4244
                                                ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4245
                                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4246
                                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4247
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4248
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4249
                        }
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4250
                        setExt(result, new BasicConstraintsExtension(isCritical, isCA,
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4251
                                pathLen));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4252
                        break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4253
                    case 1:     // KU
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4254
                        if(value != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4255
                            boolean[] ok = new boolean[9];
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4256
                            for (String s: value.split(",")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4257
                                int p = oneOf(s,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4258
                                       "digitalSignature",  // (0),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4259
                                       "nonRepudiation",    // (1)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4260
                                       "keyEncipherment",   // (2),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4261
                                       "dataEncipherment",  // (3),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4262
                                       "keyAgreement",      // (4),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4263
                                       "keyCertSign",       // (5),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4264
                                       "cRLSign",           // (6),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4265
                                       "encipherOnly",      // (7),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4266
                                       "decipherOnly",      // (8)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4267
                                       "contentCommitment"  // also (1)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4268
                                       );
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4269
                                if (p < 0) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4270
                                    throw new Exception(rb.getString("Unknown.keyUsage.type.") + s);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4271
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4272
                                if (p == 9) p = 1;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4273
                                ok[p] = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4274
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4275
                            KeyUsageExtension kue = new KeyUsageExtension(ok);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4276
                            // The above KeyUsageExtension constructor does not
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4277
                            // allow isCritical value, so...
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4278
                            setExt(result, Extension.newExtension(
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4279
                                    kue.getExtensionId(),
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4280
                                    isCritical,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4281
                                    kue.getExtensionValue()));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4282
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4283
                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4284
                                    ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4285
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4286
                        break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4287
                    case 2:     // EKU
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4288
                        if(value != null) {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
  4289
                            Vector<ObjectIdentifier> v = new Vector<>();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4290
                            for (String s: value.split(",")) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4291
                                int p = oneOf(s,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4292
                                        "anyExtendedKeyUsage",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4293
                                        "serverAuth",       //1
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4294
                                        "clientAuth",       //2
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4295
                                        "codeSigning",      //3
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4296
                                        "emailProtection",  //4
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4297
                                        "",                 //5
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4298
                                        "",                 //6
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4299
                                        "",                 //7
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4300
                                        "timeStamping",     //8
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4301
                                        "OCSPSigning"       //9
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4302
                                       );
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4303
                                if (p < 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4304
                                    try {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4305
                                        v.add(new ObjectIdentifier(s));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4306
                                    } catch (Exception e) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4307
                                        throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4308
                                                "Unknown.extendedkeyUsage.type.") + s);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4309
                                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4310
                                } else if (p == 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4311
                                    v.add(new ObjectIdentifier("2.5.29.37.0"));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4312
                                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4313
                                    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
  4314
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4315
                            }
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4316
                            setExt(result, new ExtendedKeyUsageExtension(isCritical, v));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4317
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4318
                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4319
                                    ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4320
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4321
                        break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4322
                    case 3:     // SAN
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4323
                    case 4:     // IAN
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4324
                        if(value != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4325
                            String[] ps = value.split(",");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4326
                            GeneralNames gnames = new GeneralNames();
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4327
                            for(String item: ps) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4328
                                colonpos = item.indexOf(':');
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4329
                                if (colonpos < 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4330
                                    throw new Exception("Illegal item " + item + " in " + extstr);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4331
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4332
                                String t = item.substring(0, colonpos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4333
                                String v = item.substring(colonpos+1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4334
                                gnames.add(createGeneralName(t, v));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4335
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4336
                            if (exttype == 3) {
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4337
                                setExt(result, new SubjectAlternativeNameExtension(
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4338
                                        isCritical, gnames));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4339
                            } else {
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4340
                                setExt(result, new IssuerAlternativeNameExtension(
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4341
                                        isCritical, gnames));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4342
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4343
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4344
                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4345
                                    ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4346
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4347
                        break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4348
                    case 5:     // SIA, always non-critical
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4349
                    case 6:     // AIA, always non-critical
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4350
                        if (isCritical) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4351
                            throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4352
                                    "This.extension.cannot.be.marked.as.critical.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4353
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4354
                        if(value != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4355
                            List<AccessDescription> accessDescriptions =
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7179
diff changeset
  4356
                                    new ArrayList<>();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4357
                            String[] ps = value.split(",");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4358
                            for(String item: ps) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4359
                                colonpos = item.indexOf(':');
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4360
                                int colonpos2 = item.indexOf(':', colonpos+1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4361
                                if (colonpos < 0 || colonpos2 < 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4362
                                    throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4363
                                            ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4364
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4365
                                String m = item.substring(0, colonpos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4366
                                String t = item.substring(colonpos+1, colonpos2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4367
                                String v = item.substring(colonpos2+1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4368
                                int p = oneOf(m,
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4369
                                        "",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4370
                                        "ocsp",         //1
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4371
                                        "caIssuers",    //2
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4372
                                        "timeStamping", //3
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4373
                                        "",
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4374
                                        "caRepository"  //5
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4375
                                        );
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4376
                                ObjectIdentifier oid;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4377
                                if (p < 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4378
                                    try {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4379
                                        oid = new ObjectIdentifier(m);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4380
                                    } catch (Exception e) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4381
                                        throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4382
                                                "Unknown.AccessDescription.type.") + m);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4383
                                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4384
                                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4385
                                    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
  4386
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4387
                                accessDescriptions.add(new AccessDescription(
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4388
                                        oid, createGeneralName(t, v)));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4389
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4390
                            if (exttype == 5) {
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4391
                                setExt(result, new SubjectInfoAccessExtension(accessDescriptions));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4392
                            } else {
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4393
                                setExt(result, new AuthorityInfoAccessExtension(accessDescriptions));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4394
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4395
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4396
                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4397
                                    ("Illegal.value.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4398
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4399
                        break;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4400
                    case 8: // CRL, experimental, only support 1 distributionpoint
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4401
                        if(value != null) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4402
                            String[] ps = value.split(",");
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4403
                            GeneralNames gnames = new GeneralNames();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4404
                            for(String item: ps) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4405
                                colonpos = item.indexOf(':');
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4406
                                if (colonpos < 0) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4407
                                    throw new Exception("Illegal item " + item + " in " + extstr);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4408
                                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4409
                                String t = item.substring(0, colonpos);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4410
                                String v = item.substring(colonpos+1);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4411
                                gnames.add(createGeneralName(t, v));
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4412
                            }
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4413
                            setExt(result, new CRLDistributionPointsExtension(
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4414
                                    isCritical, Collections.singletonList(
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4415
                                    new DistributionPoint(gnames, null, null))));
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4416
                        } else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4417
                            throw new Exception(rb.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4418
                                    ("Illegal.value.") + extstr);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4419
                        }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4420
                        break;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4421
                    case -1:
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4422
                        ObjectIdentifier oid = new ObjectIdentifier(name);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4423
                        byte[] data = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4424
                        if (value != null) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4425
                            data = new byte[value.length() / 2 + 1];
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4426
                            int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4427
                            for (char c: value.toCharArray()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4428
                                int hex;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4429
                                if (c >= '0' && c <= '9') {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4430
                                    hex = c - '0' ;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4431
                                } else if (c >= 'A' && c <= 'F') {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4432
                                    hex = c - 'A' + 10;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4433
                                } else if (c >= 'a' && c <= 'f') {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4434
                                    hex = c - 'a' + 10;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4435
                                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4436
                                    continue;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4437
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4438
                                if (pos % 2 == 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4439
                                    data[pos/2] = (byte)(hex << 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4440
                                } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4441
                                    data[pos/2] += hex;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4442
                                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4443
                                pos++;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4444
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4445
                            if (pos % 2 != 0) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4446
                                throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4447
                                        "Odd.number.of.hex.digits.found.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4448
                            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4449
                            data = Arrays.copyOf(data, pos/2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4450
                        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4451
                            data = new byte[0];
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4452
                        }
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4453
                        setExt(result, new Extension(oid, isCritical,
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4454
                                new DerValue(DerValue.tag_OctetString, data)
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4455
                                        .toByteArray()));
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4456
                        break;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4457
                    default:
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4458
                        throw new Exception(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4459
                                "Unknown.extension.type.") + extstr);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4460
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4461
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4462
            // always non-critical
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4463
            setExt(result, new SubjectKeyIdentifierExtension(
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4464
                    new KeyIdentifier(pkey).getIdentifier()));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4465
            if (akey != null && !pkey.equals(akey)) {
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4466
                setExt(result, new AuthorityKeyIdentifierExtension(
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4467
                                new KeyIdentifier(akey), null, null));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4468
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4469
        } catch(IOException e) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4470
            throw new RuntimeException(e);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4471
        }
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  4472
        return result;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4473
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4474
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4475
    private boolean isTrustedCert(Certificate cert) throws KeyStoreException {
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4476
        if (caks != null && caks.getCertificateAlias(cert) != null) {
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4477
            return true;
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4478
        } else {
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4479
            String inKS = keyStore.getCertificateAlias(cert);
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4480
            return inKS != null && keyStore.isCertificateEntry(inKS);
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4481
        }
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4482
    }
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4483
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4484
    private void checkWeak(String label, String sigAlg, Key key) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4485
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4486
        if (sigAlg != null && !DISABLED_CHECK.permits(
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4487
                SIG_PRIMITIVE_SET, sigAlg, null)) {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4488
            weakWarnings.add(String.format(
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4489
                    rb.getString("whose.sigalg.risk"), label, sigAlg));
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4490
        }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4491
        if (key != null && !DISABLED_CHECK.permits(SIG_PRIMITIVE_SET, key)) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4492
            weakWarnings.add(String.format(
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4493
                    rb.getString("whose.key.risk"),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4494
                    label,
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4495
                    String.format(rb.getString("key.bit"),
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4496
                            KeyUtil.getKeySize(key), key.getAlgorithm())));
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4497
        }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4498
    }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4499
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4500
    private void checkWeak(String label, Certificate[] certs)
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4501
            throws KeyStoreException {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4502
        for (int i = 0; i < certs.length; i++) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4503
            Certificate cert = certs[i];
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4504
            if (cert instanceof X509Certificate) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4505
                X509Certificate xc = (X509Certificate)cert;
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4506
                String fullLabel = label;
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4507
                if (certs.length > 1) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4508
                    fullLabel = oneInMany(label, i, certs.length);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4509
                }
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4510
                checkWeak(fullLabel, xc);
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4511
            }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4512
        }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4513
    }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4514
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4515
    private void checkWeak(String label, Certificate cert)
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4516
            throws KeyStoreException {
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4517
        if (cert instanceof X509Certificate) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4518
            X509Certificate xc = (X509Certificate)cert;
44419
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4519
            // No need to check the sigalg of a trust anchor
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4520
            String sigAlg = isTrustedCert(cert) ? null : xc.getSigAlgName();
c29f26282ba0 8177569: keytool should not warn if signature algorithm used in cacerts is weak
weijun
parents: 44046
diff changeset
  4521
            checkWeak(label, sigAlg, xc.getPublicKey());
44046
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4522
        }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4523
    }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4524
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4525
    private void checkWeak(String label, PKCS10 p10) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4526
        checkWeak(label, p10.getSigAlg(), p10.getSubjectPublicKeyInfo());
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4527
    }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4528
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4529
    private void checkWeak(String label, CRL crl, Key key) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4530
        if (crl instanceof X509CRLImpl) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4531
            X509CRLImpl impl = (X509CRLImpl)crl;
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4532
            checkWeak(label, impl.getSigAlgName(), key);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4533
        }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4534
    }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4535
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4536
    private void printWeakWarnings(boolean newLine) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4537
        if (!weakWarnings.isEmpty() && !nowarn) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4538
            System.err.println("\nWarning:");
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4539
            for (String warning : weakWarnings) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4540
                System.err.println(warning);
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4541
            }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4542
            if (newLine) {
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4543
                // When calling before a yes/no prompt, add a new line
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4544
                System.err.println();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4545
            }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4546
        }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4547
        weakWarnings.clear();
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4548
    }
762e807bfac1 8171319: keytool should print out warnings when reading or generating cert/cert req using weak algorithms
weijun
parents: 43238
diff changeset
  4549
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4550
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4551
     * Prints the usage of this tool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4553
    private void usage() {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4554
        if (command != null) {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4555
            System.err.println("keytool " + command +
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4556
                    rb.getString(".OPTION."));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4557
            System.err.println();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4558
            System.err.println(rb.getString(command.description));
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4559
            System.err.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4560
            System.err.println(rb.getString("Options."));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4561
            System.err.println();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4562
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4563
            // Left and right sides of the options list. Both might
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4564
            // contain "\n" and span multiple lines
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4565
            String[] left = new String[command.options.length];
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4566
            String[] right = new String[command.options.length];
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4567
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4568
            // Length of left side of options list
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4569
            int lenLeft = 0;
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4570
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4571
            for (int j = 0; j < command.options.length; j++) {
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
  4572
                Option opt = command.options[j];
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
  4573
                left[j] = opt.toString();
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4574
                if (opt.arg != null) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4575
                    left[j] += " " + opt.arg;
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4576
                }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4577
                String[] lefts = left[j].split("\n");
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4578
                for (String s : lefts) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4579
                    if (s.length() > lenLeft) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4580
                        lenLeft = s.length();
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4581
                    }
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4582
                }
4819
c1661a223e19 6922482: keytool's help on -file always shows 'output file'
weijun
parents: 4350
diff changeset
  4583
                right[j] = rb.getString(opt.description);
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4584
            }
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4585
            for (int j = 0; j < left.length; j++) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4586
                String[] lefts = left[j].split("\n");
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4587
                String[] rights = right[j].split("\n");
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4588
                for (int i = 0; i < lefts.length && i < rights.length; i++) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4589
                    String s1 = i < lefts.length ? lefts[i] : "";
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4590
                    String s2 = i < rights.length ? rights[i] : "";
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4591
                    if (i == 0) {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4592
                        System.err.printf(" %-" + lenLeft + "s  %s\n", s1, s2);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4593
                    } else {
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4594
                        System.err.printf("   %-" + lenLeft + "s  %s\n", s1, s2);
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4595
                    }
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 39046
diff changeset
  4596
                }
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4597
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4598
            System.err.println();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4599
            System.err.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4600
                    "Use.keytool.help.for.all.available.commands"));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4601
        } else {
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4602
            System.err.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4603
                    "Key.and.Certificate.Management.Tool"));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4604
            System.err.println();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4605
            System.err.println(rb.getString("Commands."));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4606
            System.err.println();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4607
            for (Command c: Command.values()) {
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4608
                if (c == KEYCLONE) break;
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 5457
diff changeset
  4609
                System.err.printf(" %-20s%s\n", c, rb.getString(c.description));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4610
            }
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4611
            System.err.println();
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4612
            System.err.println(rb.getString(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4613
                    "Use.keytool.command.name.help.for.usage.of.command.name"));
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4614
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4617
    private void tinyHelp() {
3948
736156ecc24c 6324292: keytool -help is unhelpful
weijun
parents: 3484
diff changeset
  4618
        usage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4619
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4620
            throw new RuntimeException("NO BIG ERROR, SORRY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4621
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4622
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4626
    private void errorNeedArgument(String flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4627
        Object[] source = {flag};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4628
        System.err.println(new MessageFormat(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 6688
diff changeset
  4629
                rb.getString("Command.option.flag.needs.an.argument.")).format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4630
        tinyHelp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4631
    }
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4632
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4633
    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
  4634
        char[] output = KeyStoreUtil.getPassWithModifier(modifier, arg, rb);
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4635
        if (output != null) return output;
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4636
        tinyHelp();
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4637
        return null;    // Useless, tinyHelp() already exits.
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4638
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4639
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4641
// This class is exactly the same as com.sun.tools.javac.util.Pair,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4642
// it's copied here since the original one is not included in JRE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4643
class Pair<A, B> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4645
    public final A fst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4646
    public final B snd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4648
    public Pair(A fst, B snd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4649
        this.fst = fst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4650
        this.snd = snd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4653
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4654
        return "Pair[" + fst + "," + snd + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4657
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4658
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4659
            other instanceof Pair &&
10051
b96a8c7fefef 7041252: Use j.u.Objects.equals in security classes
darcy
parents: 9011
diff changeset
  4660
            Objects.equals(fst, ((Pair)other).fst) &&
b96a8c7fefef 7041252: Use j.u.Objects.equals in security classes
darcy
parents: 9011
diff changeset
  4661
            Objects.equals(snd, ((Pair)other).snd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4664
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4665
        if (fst == null) return (snd == null) ? 0 : snd.hashCode() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4666
        else if (snd == null) return fst.hashCode() + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4667
        else return fst.hashCode() * 17 + snd.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4668
    }
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4669
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4670
    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
  4671
        return new Pair<>(a,b);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 909
diff changeset
  4672
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4673
}
3951
e821908c953e 6868579: RFE: jarsigner to support reading password from environment variable
weijun
parents: 3948
diff changeset
  4674