001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.security.mscapi;
027:
028: import java.util.UUID;
029: import java.security.*;
030: import java.security.spec.AlgorithmParameterSpec;
031: import java.security.spec.RSAKeyGenParameterSpec;
032:
033: import sun.security.jca.JCAUtil;
034:
035: /**
036: * RSA keypair generator.
037: *
038: * Standard algorithm, minimum key length is 512 bit, maximum is 16,384.
039: * Generates a private key that is exportable.
040: *
041: * @since 1.6
042: */
043: public final class RSAKeyPairGenerator extends KeyPairGeneratorSpi {
044:
045: // Supported by Microsoft Base, Strong and Enhanced Cryptographic Providers
046: private static final int KEY_SIZE_MIN = 512; // disallow MSCAPI min. of 384
047: private static final int KEY_SIZE_MAX = 16384;
048: private static final int KEY_SIZE_DEFAULT = 1024;
049:
050: // size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX
051: private int keySize;
052:
053: public RSAKeyPairGenerator() {
054: // initialize to default in case the app does not call initialize()
055: initialize(KEY_SIZE_DEFAULT, null);
056: }
057:
058: // initialize the generator. See JCA doc
059: // random is always ignored
060: public void initialize(int keySize, SecureRandom random) {
061:
062: checkKeySize(keySize);
063: }
064:
065: // second initialize method. See JCA doc
066: // random and exponent are always ignored
067: public void initialize(AlgorithmParameterSpec params,
068: SecureRandom random)
069: throws InvalidAlgorithmParameterException {
070:
071: if (params == null) {
072: checkKeySize(KEY_SIZE_DEFAULT);
073:
074: } else if (params instanceof RSAKeyGenParameterSpec) {
075:
076: if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) {
077: throw new InvalidAlgorithmParameterException(
078: "Exponent parameter is not supported");
079: }
080: checkKeySize(((RSAKeyGenParameterSpec) params).getKeysize());
081:
082: } else {
083: throw new InvalidAlgorithmParameterException(
084: "Params must be an instance of RSAKeyGenParameterSpec");
085: }
086: }
087:
088: // generate the keypair. See JCA doc
089: public KeyPair generateKeyPair() {
090:
091: // Generate each keypair in a unique key container
092: RSAKeyPair keys = generateRSAKeyPair(keySize, "{"
093: + UUID.randomUUID().toString() + "}");
094:
095: return new KeyPair(keys.getPublic(), keys.getPrivate());
096: }
097:
098: private void checkKeySize(int keySize)
099: throws InvalidParameterException {
100: if (keySize < KEY_SIZE_MIN) {
101: throw new InvalidParameterException(
102: "Key size must be at least " + KEY_SIZE_MIN
103: + " bits");
104: }
105: if (keySize > KEY_SIZE_MAX) {
106: throw new InvalidParameterException("Key size must be "
107: + KEY_SIZE_MAX + " bits or less");
108: }
109: this .keySize = keySize;
110: }
111:
112: private static native RSAKeyPair generateRSAKeyPair(int keySize,
113: String keyContainerName);
114: }
|