jdk/src/java.base/share/classes/sun/security/tools/keytool/CertAndKeyGen.java
author weijun
Tue, 07 Jun 2016 10:33:38 +0800
changeset 38855 4e587173e37e
parent 38439 8a3871cd7fca
permissions -rw-r--r--
8151836: keytool -importkeystore -help does not list option -destprotected Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26361
869d2905615d 8057076: Correct exception message in CertAndKeyGen.java
coffeys
parents: 25859
diff changeset
     2
 * Copyright (c) 1996, 2014, 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: 4350
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: 4350
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: 4350
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4350
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4350
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: 11012
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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.CertificateEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10336
diff changeset
    35
import sun.security.pkcs10.PKCS10;
11012
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
    36
import sun.security.x509.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Generate a pair of keys, and provide access to them.  This class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * provided primarily for ease of use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <P>This provides some simple certificate management functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Specifically, it allows you to create self-signed X.509 certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * as well as PKCS 10 based certificate signing requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <P>Keys for some public key signature algorithms have algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * parameters, such as DSS/DSA.  Some sites' Certificate Authorities
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * adopt fixed algorithm parameters, which speeds up some operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * including key generation and signing.  <em>At this time, this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * does not provide a way to provide such algorithm parameters, e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * by providing the CA certificate which includes those parameters.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <P>Also, note that at this time only signature-capable keys may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * acquired through this interface.  Diffie-Hellman keys, used for secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * key exchange, may be supported later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see PKCS10
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see X509CertImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public final class CertAndKeyGen {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Creates a CertAndKeyGen object for a particular key type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * and signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param keyType type of key, e.g. "RSA", "DSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param sigAlg name of the signature algorithm, e.g. "MD5WithRSA",
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
    70
     *          "MD2WithRSA", "SHAwithDSA". If set to null, a default
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
    71
     *          algorithm matching the private key will be chosen after
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
    72
     *          the first keypair is generated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @exception NoSuchAlgorithmException on unrecognized algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public CertAndKeyGen (String keyType, String sigAlg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        keyGen = KeyPairGenerator.getInstance(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.sigAlg = sigAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Creates a CertAndKeyGen object for a particular key type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * signature algorithm, and provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param keyType type of key, e.g. "RSA", "DSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param sigAlg name of the signature algorithm, e.g. "MD5WithRSA",
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
    88
     *          "MD2WithRSA", "SHAwithDSA". If set to null, a default
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
    89
     *          algorithm matching the private key will be chosen after
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
    90
     *          the first keypair is generated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param providerName name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @exception NoSuchAlgorithmException on unrecognized algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @exception NoSuchProviderException on unrecognized providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public CertAndKeyGen (String keyType, String sigAlg, String providerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    throws NoSuchAlgorithmException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (providerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            keyGen = KeyPairGenerator.getInstance(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                keyGen = KeyPairGenerator.getInstance(keyType, providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                // try first available provider instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                keyGen = KeyPairGenerator.getInstance(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.sigAlg = sigAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Sets the source of random numbers used when generating keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * If you do not provide one, a system default facility is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * You may wish to provide your own source of random numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * to get a reproducible sequence of keys and signatures, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * because you may be able to take advantage of strong sources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * of randomness/entropy in your environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void         setRandom (SecureRandom generator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        prng = generator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // want "public void generate (X509Certificate)" ... inherit DSA/D-H param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Generates a random public/private key pair, with a given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * size.  Different algorithms provide different degrees of security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * for the same key size, because of the "work factor" involved in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * brute force attacks.  As computers become faster, it becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * easier to perform such attacks.  Small keys are to be avoided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <P>Note that not all values of "keyBits" are valid for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * algorithms, and not all public key algorithms are currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * supported for use in X.509 certificates.  If the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * you specified does not produce X.509 compatible keys, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * invalid key exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param keyBits the number of bits in the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception InvalidKeyException if the environment does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *  provide X.509 public keys for this signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void generate (int keyBits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        KeyPair pair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (prng == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                prng = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            keyGen.initialize(keyBits, prng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            pair = keyGen.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        publicKey = pair.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        privateKey = pair.getPrivate();
11012
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   161
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   162
        // publicKey's format must be X.509 otherwise
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   163
        // the whole CertGen part of this class is broken.
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   164
        if (!"X.509".equalsIgnoreCase(publicKey.getFormat())) {
26361
869d2905615d 8057076: Correct exception message in CertAndKeyGen.java
coffeys
parents: 25859
diff changeset
   165
            throw new IllegalArgumentException("Public key format is "
869d2905615d 8057076: Correct exception message in CertAndKeyGen.java
coffeys
parents: 25859
diff changeset
   166
                + publicKey.getFormat() + ", must be X.509");
11012
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   167
        }
38439
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
   168
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
   169
        if (sigAlg == null) {
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
   170
            sigAlg = AlgorithmId.getDefaultSigAlgForKey(privateKey);
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
   171
            if (sigAlg == null) {
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
   172
                throw new IllegalArgumentException(
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
   173
                        "Cannot derive signature algorithm from "
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
   174
                                + privateKey.getAlgorithm());
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
   175
            }
8a3871cd7fca 8138766: New default -sigalg for keytool
weijun
parents: 26361
diff changeset
   176
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Returns the public key of the generated key pair if it is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <code>X509Key</code>, or null if the public key is of a different type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * XXX Note: This behaviour is needed for backwards compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * What this method really should return is the public key of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * generated key pair, regardless of whether or not it is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <code>X509Key</code>. Accordingly, the return type of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * should be <code>PublicKey</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public X509Key getPublicKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (!(publicKey instanceof X509Key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return (X509Key)publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
11012
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   197
    /**
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   198
     * Always returns the public key of the generated key pair. Used
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   199
     * by KeyTool only.
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   200
     *
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   201
     * The publicKey is not necessarily to be an instance of
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   202
     * X509Key in some JCA/JCE providers, for example SunPKCS11.
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   203
     */
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   204
    public PublicKey getPublicKeyAnyway() {
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   205
        return publicKey;
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   206
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Returns the private key of the generated key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <P><STRONG><em>Be extremely careful when handling private keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * When private keys are not kept secret, they lose their ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * to securely authenticate specific entities ... that is a huge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * security risk!</em></STRONG>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public PrivateKey getPrivateKey ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns a self-signed X.509v3 certificate for the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * The certificate is immediately valid. No extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <P>Such certificates normally are used to identify a "Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Authority" (CA).  Accordingly, they will not always be accepted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * other parties.  However, such certificates are also useful when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * you are bootstrapping your security infrastructure, or deploying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * system prototypes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param myname X.500 name of the subject (who is also the issuer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param firstDate the issue time of the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param validity how long the certificate should be valid, in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @exception CertificateException on certificate handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @exception InvalidKeyException on key handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @exception SignatureException on signature handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @exception NoSuchAlgorithmException on unrecognized algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @exception NoSuchProviderException on unrecognized providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public X509Certificate getSelfCertificate (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            X500Name myname, Date firstDate, long validity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    throws CertificateException, InvalidKeyException, SignatureException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        NoSuchAlgorithmException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    {
11012
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   245
        return getSelfCertificate(myname, firstDate, validity, null);
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   246
    }
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   247
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   248
    // Like above, plus a CertificateExtensions argument, which can be null.
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   249
    public X509Certificate getSelfCertificate (X500Name myname, Date firstDate,
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   250
            long validity, CertificateExtensions ext)
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   251
    throws CertificateException, InvalidKeyException, SignatureException,
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   252
        NoSuchAlgorithmException, NoSuchProviderException
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   253
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        X509CertImpl    cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        Date            lastDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            lastDate = new Date ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            lastDate.setTime (firstDate.getTime () + validity * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            CertificateValidity interval =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                   new CertificateValidity(firstDate,lastDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            X509CertInfo info = new X509CertInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // Add all mandatory attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            info.set(X509CertInfo.VERSION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                     new CertificateVersion(CertificateVersion.V3));
2293
cb6d01cb3c3d 6820606: keytool can generate serialno more randomly
weijun
parents: 2067
diff changeset
   268
            info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(
cb6d01cb3c3d 6820606: keytool can generate serialno more randomly
weijun
parents: 2067
diff changeset
   269
                    new java.util.Random().nextInt() & 0x7fffffff));
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   270
            AlgorithmId algID = AlgorithmId.get(sigAlg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            info.set(X509CertInfo.ALGORITHM_ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                     new CertificateAlgorithmId(algID));
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14182
diff changeset
   273
            info.set(X509CertInfo.SUBJECT, myname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            info.set(X509CertInfo.KEY, new CertificateX509Key(publicKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            info.set(X509CertInfo.VALIDITY, interval);
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 14182
diff changeset
   276
            info.set(X509CertInfo.ISSUER, myname);
11012
13ee761ef120 7109096: keytool -genkeypair needn't call -selfcert
weijun
parents: 10782
diff changeset
   277
            if (ext != null) info.set(X509CertInfo.EXTENSIONS, ext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            cert = new X509CertImpl(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            cert.sign(privateKey, this.sigAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return (X509Certificate)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
             throw new CertificateEncodingException("getSelfCert: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                                    e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    // Keep the old method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public X509Certificate getSelfCertificate (X500Name myname, long validity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    throws CertificateException, InvalidKeyException, SignatureException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        NoSuchAlgorithmException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return getSelfCertificate(myname, new Date(), validity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns a PKCS #10 certificate request.  The caller uses either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <code>PKCS10.print</code> or <code>PKCS10.toByteArray</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * operations on the result, to get the request in an appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * transmission format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <P>PKCS #10 certificate requests are sent, along with some proof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * of identity, to Certificate Authorities (CAs) which then issue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * X.509 public key certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param myname X.500 name of the subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @exception InvalidKeyException on key handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @exception SignatureException on signature handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public PKCS10 getCertRequest (X500Name myname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    throws InvalidKeyException, SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        PKCS10  req = new PKCS10 (publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        try {
4350
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 2293
diff changeset
   318
            Signature signature = Signature.getInstance(sigAlg);
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 2293
diff changeset
   319
            signature.initSign (privateKey);
2a593a20d962 6876158: Remove dependencies on Signer, Certificate, Identity, IdentityScope classes from java.security pkg
vinnie
parents: 2293
diff changeset
   320
            req.encodeAndSign(myname, signature);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throw new SignatureException (sigAlg + " CertificateException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw new SignatureException (sigAlg + " IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // "can't happen"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            throw new SignatureException (sigAlg + " unavailable?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return req;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private SecureRandom        prng;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private String              sigAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private KeyPairGenerator    keyGen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private PublicKey           publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private PrivateKey          privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}