0N/A/*
4111N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.mscapi;
0N/A
0N/Aimport java.util.UUID;
0N/Aimport java.security.*;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/Aimport java.security.spec.RSAKeyGenParameterSpec;
0N/A
0N/Aimport sun.security.jca.JCAUtil;
1111N/Aimport sun.security.rsa.RSAKeyFactory;
0N/A
0N/A/**
0N/A * RSA keypair generator.
0N/A *
0N/A * Standard algorithm, minimum key length is 512 bit, maximum is 16,384.
0N/A * Generates a private key that is exportable.
0N/A *
0N/A * @since 1.6
0N/A */
0N/Apublic final class RSAKeyPairGenerator extends KeyPairGeneratorSpi {
0N/A
0N/A // Supported by Microsoft Base, Strong and Enhanced Cryptographic Providers
1111N/A static final int KEY_SIZE_MIN = 512; // disallow MSCAPI min. of 384
1111N/A static final int KEY_SIZE_MAX = 16384;
0N/A private static final int KEY_SIZE_DEFAULT = 1024;
0N/A
0N/A // size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX
0N/A private int keySize;
0N/A
0N/A public RSAKeyPairGenerator() {
0N/A // initialize to default in case the app does not call initialize()
0N/A initialize(KEY_SIZE_DEFAULT, null);
0N/A }
0N/A
0N/A // initialize the generator. See JCA doc
0N/A // random is always ignored
0N/A public void initialize(int keySize, SecureRandom random) {
0N/A
1111N/A try {
1111N/A RSAKeyFactory.checkKeyLengths(keySize, null,
1111N/A KEY_SIZE_MIN, KEY_SIZE_MAX);
1111N/A } catch (InvalidKeyException e) {
1111N/A throw new InvalidParameterException(e.getMessage());
1111N/A }
1111N/A
1111N/A this.keySize = keySize;
0N/A }
0N/A
0N/A // second initialize method. See JCA doc
0N/A // random and exponent are always ignored
0N/A public void initialize(AlgorithmParameterSpec params, SecureRandom random)
0N/A throws InvalidAlgorithmParameterException {
0N/A
1111N/A int tmpSize;
0N/A if (params == null) {
1111N/A tmpSize = KEY_SIZE_DEFAULT;
0N/A } else if (params instanceof RSAKeyGenParameterSpec) {
0N/A
0N/A if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Exponent parameter is not supported");
0N/A }
1111N/A tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize();
0N/A
0N/A } else {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Params must be an instance of RSAKeyGenParameterSpec");
0N/A }
1111N/A
1111N/A try {
1111N/A RSAKeyFactory.checkKeyLengths(tmpSize, null,
1111N/A KEY_SIZE_MIN, KEY_SIZE_MAX);
1111N/A } catch (InvalidKeyException e) {
1111N/A throw new InvalidAlgorithmParameterException(
1111N/A "Invalid Key sizes", e);
1111N/A }
1111N/A
1111N/A this.keySize = tmpSize;
0N/A }
0N/A
0N/A // generate the keypair. See JCA doc
0N/A public KeyPair generateKeyPair() {
0N/A
4111N/A try {
0N/A
4111N/A // Generate each keypair in a unique key container
4111N/A RSAKeyPair keys =
4111N/A generateRSAKeyPair(keySize,
4111N/A "{" + UUID.randomUUID().toString() + "}");
4111N/A
4111N/A return new KeyPair(keys.getPublic(), keys.getPrivate());
4111N/A
4111N/A } catch (KeyException e) {
4111N/A throw new ProviderException(e);
4111N/A }
0N/A }
0N/A
0N/A private static native RSAKeyPair generateRSAKeyPair(int keySize,
4111N/A String keyContainerName) throws KeyException;
0N/A}