jdk/src/share/classes/sun/security/x509/CertAndKeyGen.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2067 6f9db5f305cd
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.x509;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.pkcs.PKCS10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Generate a pair of keys, and provide access to them.  This class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * provided primarily for ease of use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <P>This provides some simple certificate management functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Specifically, it allows you to create self-signed X.509 certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * as well as PKCS 10 based certificate signing requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <P>Keys for some public key signature algorithms have algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * parameters, such as DSS/DSA.  Some sites' Certificate Authorities
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * adopt fixed algorithm parameters, which speeds up some operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * including key generation and signing.  <em>At this time, this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * does not provide a way to provide such algorithm parameters, e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * by providing the CA certificate which includes those parameters.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <P>Also, note that at this time only signature-capable keys may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * acquired through this interface.  Diffie-Hellman keys, used for secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * key exchange, may be supported later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see PKCS10
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see X509CertImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public final class CertAndKeyGen {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates a CertAndKeyGen object for a particular key type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * and signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param keyType type of key, e.g. "RSA", "DSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param sigAlg name of the signature algorithm, e.g. "MD5WithRSA",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *          "MD2WithRSA", "SHAwithDSA".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @exception NoSuchAlgorithmException on unrecognized algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public CertAndKeyGen (String keyType, String sigAlg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        keyGen = KeyPairGenerator.getInstance(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.sigAlg = sigAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Creates a CertAndKeyGen object for a particular key type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * signature algorithm, and provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param keyType type of key, e.g. "RSA", "DSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param sigAlg name of the signature algorithm, e.g. "MD5WithRSA",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *          "MD2WithRSA", "SHAwithDSA".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param providerName name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @exception NoSuchAlgorithmException on unrecognized algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @exception NoSuchProviderException on unrecognized providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public CertAndKeyGen (String keyType, String sigAlg, String providerName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    throws NoSuchAlgorithmException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (providerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            keyGen = KeyPairGenerator.getInstance(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                keyGen = KeyPairGenerator.getInstance(keyType, providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                // try first available provider instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                keyGen = KeyPairGenerator.getInstance(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.sigAlg = sigAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Sets the source of random numbers used when generating keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * If you do not provide one, a system default facility is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * You may wish to provide your own source of random numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * to get a reproducible sequence of keys and signatures, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * because you may be able to take advantage of strong sources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * of randomness/entropy in your environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void         setRandom (SecureRandom generator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        prng = generator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // want "public void generate (X509Certificate)" ... inherit DSA/D-H param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Generates a random public/private key pair, with a given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * size.  Different algorithms provide different degrees of security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * for the same key size, because of the "work factor" involved in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * brute force attacks.  As computers become faster, it becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * easier to perform such attacks.  Small keys are to be avoided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <P>Note that not all values of "keyBits" are valid for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * algorithms, and not all public key algorithms are currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * supported for use in X.509 certificates.  If the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * you specified does not produce X.509 compatible keys, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * invalid key exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param keyBits the number of bits in the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @exception InvalidKeyException if the environment does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *  provide X.509 public keys for this signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void generate (int keyBits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        KeyPair pair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (prng == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                prng = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            keyGen.initialize(keyBits, prng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            pair = keyGen.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        publicKey = pair.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        privateKey = pair.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Returns the public key of the generated key pair if it is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <code>X509Key</code>, or null if the public key is of a different type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * XXX Note: This behaviour is needed for backwards compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * What this method really should return is the public key of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * generated key pair, regardless of whether or not it is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <code>X509Key</code>. Accordingly, the return type of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * should be <code>PublicKey</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public X509Key getPublicKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (!(publicKey instanceof X509Key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return (X509Key)publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Returns the private key of the generated key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <P><STRONG><em>Be extremely careful when handling private keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * When private keys are not kept secret, they lose their ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * to securely authenticate specific entities ... that is a huge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * security risk!</em></STRONG>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public PrivateKey getPrivateKey ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns a self-signed X.509v1 certificate for the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * The certificate is immediately valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <P>Such certificates normally are used to identify a "Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Authority" (CA).  Accordingly, they will not always be accepted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * other parties.  However, such certificates are also useful when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * you are bootstrapping your security infrastructure, or deploying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * system prototypes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @deprecated Use the new <a href =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * "#getSelfCertificate(sun.security.x509.X500Name, long)">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param myname X.500 name of the subject (who is also the issuer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param validity how long the certificate should be valid, in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public X509Cert             getSelfCert (X500Name myname, long validity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    throws InvalidKeyException, SignatureException, NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        X509Certificate cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            cert = getSelfCertificate(myname, validity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return new X509Cert(cert.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throw new SignatureException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new NoSuchAlgorithmException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new SignatureException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Returns a self-signed X.509v3 certificate for the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * The certificate is immediately valid. No extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <P>Such certificates normally are used to identify a "Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Authority" (CA).  Accordingly, they will not always be accepted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * other parties.  However, such certificates are also useful when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * you are bootstrapping your security infrastructure, or deploying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * system prototypes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param myname X.500 name of the subject (who is also the issuer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param firstDate the issue time of the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param validity how long the certificate should be valid, in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception CertificateException on certificate handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception InvalidKeyException on key handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @exception SignatureException on signature handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @exception NoSuchAlgorithmException on unrecognized algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @exception NoSuchProviderException on unrecognized providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public X509Certificate getSelfCertificate (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            X500Name myname, Date firstDate, long validity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    throws CertificateException, InvalidKeyException, SignatureException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        NoSuchAlgorithmException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        X500Signer      issuer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        X509CertImpl    cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        Date            lastDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            issuer = getSigner (myname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
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));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            info.set(X509CertInfo.SERIAL_NUMBER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                 new CertificateSerialNumber((int)(firstDate.getTime()/1000)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            AlgorithmId algID = issuer.getAlgorithmId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            info.set(X509CertInfo.ALGORITHM_ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                     new CertificateAlgorithmId(algID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(myname));
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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            info.set(X509CertInfo.ISSUER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                     new CertificateIssuerName(issuer.getSigner()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            CertificateExtensions ext = new CertificateExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                ext.set(SubjectKeyIdentifierExtension.NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        new SubjectKeyIdentifierExtension(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                            new KeyIdentifier(publicKey).getIdentifier()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            info.set(X509CertInfo.EXTENSIONS, ext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            cert = new X509CertImpl(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            cert.sign(privateKey, this.sigAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return (X509Certificate)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
             throw new CertificateEncodingException("getSelfCert: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                                    e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    // Keep the old method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public X509Certificate getSelfCertificate (X500Name myname, long validity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    throws CertificateException, InvalidKeyException, SignatureException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        NoSuchAlgorithmException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return getSelfCertificate(myname, new Date(), validity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Returns a PKCS #10 certificate request.  The caller uses either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <code>PKCS10.print</code> or <code>PKCS10.toByteArray</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * operations on the result, to get the request in an appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * transmission format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <P>PKCS #10 certificate requests are sent, along with some proof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * of identity, to Certificate Authorities (CAs) which then issue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * X.509 public key certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param myname X.500 name of the subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @exception InvalidKeyException on key handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @exception SignatureException on signature handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public PKCS10 getCertRequest (X500Name myname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    throws InvalidKeyException, SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        PKCS10  req = new PKCS10 (publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            req.encodeAndSign (getSigner (myname));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            throw new SignatureException (sigAlg + " CertificateException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            throw new SignatureException (sigAlg + " IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            // "can't happen"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            throw new SignatureException (sigAlg + " unavailable?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return req;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private X500Signer getSigner (X500Name me)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    throws InvalidKeyException, NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        Signature signature = Signature.getInstance(sigAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // XXX should have a way to pass prng to the signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // algorithm ... appropriate for DSS/DSA, not RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        signature.initSign (privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return new X500Signer (signature, me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private SecureRandom        prng;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private String              sigAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private KeyPairGenerator    keyGen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private PublicKey           publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private PrivateKey          privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}