jdk/src/windows/classes/sun/security/mscapi/RSAKeyPairGenerator.java
author wetmore
Fri, 22 Aug 2008 18:48:00 -0700
changeset 2596 a1964c157e68
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6497740: Limit the size of RSA public keys Reviewed-by: andreas, valeriep, vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  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
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.mscapi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.UUID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.RSAKeyGenParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.jca.JCAUtil;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    34
import sun.security.rsa.RSAKeyFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * RSA keypair generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Standard algorithm, minimum key length is 512 bit, maximum is 16,384.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Generates a private key that is exportable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public final class RSAKeyPairGenerator extends KeyPairGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // Supported by Microsoft Base, Strong and Enhanced Cryptographic Providers
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    47
    static final int KEY_SIZE_MIN = 512; // disallow MSCAPI min. of 384
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    48
    static final int KEY_SIZE_MAX = 16384;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int KEY_SIZE_DEFAULT = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int keySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public RSAKeyPairGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // initialize to default in case the app does not call initialize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        initialize(KEY_SIZE_DEFAULT, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // initialize the generator. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // random is always ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public void initialize(int keySize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    63
        try {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    64
            RSAKeyFactory.checkKeyLengths(keySize, null,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    65
                KEY_SIZE_MIN, KEY_SIZE_MAX);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    66
        } catch (InvalidKeyException e) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    67
            throw new InvalidParameterException(e.getMessage());
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    68
        }
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    69
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    70
        this.keySize = keySize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // second initialize method. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // random and exponent are always ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public void initialize(AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    78
        int tmpSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (params == null) {
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    80
            tmpSize = KEY_SIZE_DEFAULT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } else if (params instanceof RSAKeyGenParameterSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    ("Exponent parameter is not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    87
            tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                ("Params must be an instance of RSAKeyGenParameterSpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    93
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    94
        try {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    95
            RSAKeyFactory.checkKeyLengths(tmpSize, null,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    96
                KEY_SIZE_MIN, KEY_SIZE_MAX);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    97
        } catch (InvalidKeyException e) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    98
            throw new InvalidAlgorithmParameterException(
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    99
                "Invalid Key sizes", e);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   100
        }
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   101
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   102
        this.keySize = tmpSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // generate the keypair. See JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public KeyPair generateKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // Generate each keypair in a unique key container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        RSAKeyPair keys =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            generateRSAKeyPair(keySize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                "{" + UUID.randomUUID().toString() + "}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return new KeyPair(keys.getPublic(), keys.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static native RSAKeyPair generateRSAKeyPair(int keySize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        String keyContainerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}