J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.security.mscapi; |
| 27 | |
| 28 | import java.util.UUID; |
| 29 | import java.security.*; |
| 30 | import java.security.spec.AlgorithmParameterSpec; |
| 31 | import java.security.spec.RSAKeyGenParameterSpec; |
| 32 | |
| 33 | import sun.security.jca.JCAUtil; |
| 34 | |
| 35 | /** |
| 36 | * RSA keypair generator. |
| 37 | * |
| 38 | * Standard algorithm, minimum key length is 512 bit, maximum is 16,384. |
| 39 | * Generates a private key that is exportable. |
| 40 | * |
| 41 | * @since 1.6 |
| 42 | */ |
| 43 | public final class RSAKeyPairGenerator extends KeyPairGeneratorSpi { |
| 44 | |
| 45 | // Supported by Microsoft Base, Strong and Enhanced Cryptographic Providers |
| 46 | private static final int KEY_SIZE_MIN = 512; // disallow MSCAPI min. of 384 |
| 47 | private static final int KEY_SIZE_MAX = 16384; |
| 48 | private static final int KEY_SIZE_DEFAULT = 1024; |
| 49 | |
| 50 | // size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX |
| 51 | private int keySize; |
| 52 | |
| 53 | public RSAKeyPairGenerator() { |
| 54 | // initialize to default in case the app does not call initialize() |
| 55 | initialize(KEY_SIZE_DEFAULT, null); |
| 56 | } |
| 57 | |
| 58 | // initialize the generator. See JCA doc |
| 59 | // random is always ignored |
| 60 | public void initialize(int keySize, SecureRandom random) { |
| 61 | |
| 62 | checkKeySize(keySize); |
| 63 | } |
| 64 | |
| 65 | // second initialize method. See JCA doc |
| 66 | // random and exponent are always ignored |
| 67 | public void initialize(AlgorithmParameterSpec params, SecureRandom random) |
| 68 | throws InvalidAlgorithmParameterException { |
| 69 | |
| 70 | if (params == null) { |
| 71 | checkKeySize(KEY_SIZE_DEFAULT); |
| 72 | |
| 73 | } else if (params instanceof RSAKeyGenParameterSpec) { |
| 74 | |
| 75 | if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) { |
| 76 | throw new InvalidAlgorithmParameterException |
| 77 | ("Exponent parameter is not supported"); |
| 78 | } |
| 79 | checkKeySize(((RSAKeyGenParameterSpec) params).getKeysize()); |
| 80 | |
| 81 | } else { |
| 82 | throw new InvalidAlgorithmParameterException |
| 83 | ("Params must be an instance of RSAKeyGenParameterSpec"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // generate the keypair. See JCA doc |
| 88 | public KeyPair generateKeyPair() { |
| 89 | |
| 90 | // Generate each keypair in a unique key container |
| 91 | RSAKeyPair keys = |
| 92 | generateRSAKeyPair(keySize, |
| 93 | "{" + UUID.randomUUID().toString() + "}"); |
| 94 | |
| 95 | return new KeyPair(keys.getPublic(), keys.getPrivate()); |
| 96 | } |
| 97 | |
| 98 | private void checkKeySize(int keySize) throws InvalidParameterException { |
| 99 | if (keySize < KEY_SIZE_MIN) { |
| 100 | throw new InvalidParameterException |
| 101 | ("Key size must be at least " + KEY_SIZE_MIN + " bits"); |
| 102 | } |
| 103 | if (keySize > KEY_SIZE_MAX) { |
| 104 | throw new InvalidParameterException |
| 105 | ("Key size must be " + KEY_SIZE_MAX + " bits or less"); |
| 106 | } |
| 107 | this.keySize = keySize; |
| 108 | } |
| 109 | |
| 110 | private static native RSAKeyPair generateRSAKeyPair(int keySize, |
| 111 | String keyContainerName); |
| 112 | } |