Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/security/mscapi/CKeyPairGenerator.java
32288 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.mscapi;2627import java.util.UUID;28import java.security.*;29import java.security.spec.AlgorithmParameterSpec;30import java.security.spec.RSAKeyGenParameterSpec;3132import sun.security.rsa.RSAKeyFactory;33import static sun.security.util.SecurityProviderConstants.DEF_RSA_KEY_SIZE;3435/**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.642*/43public abstract class CKeyPairGenerator extends KeyPairGeneratorSpi {4445protected String keyAlg;4647public CKeyPairGenerator(String keyAlg) {48this.keyAlg = keyAlg;49}5051public static class RSA extends CKeyPairGenerator {52public RSA() {53super("RSA");54// initialize to default in case the app does not call initialize()55initialize(DEF_RSA_KEY_SIZE, null);56}5758// Supported by Microsoft Base, Strong and Enhanced Cryptographic Providers59static final int KEY_SIZE_MIN = 512; // disallow MSCAPI min. of 38460static final int KEY_SIZE_MAX = 16384;6162// size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX63private int keySize;6465// initialize the generator. See JCA doc66// random is always ignored67@Override68public void initialize(int keySize, SecureRandom random) {6970try {71RSAKeyFactory.checkKeyLengths(keySize, null,72KEY_SIZE_MIN, KEY_SIZE_MAX);73} catch (InvalidKeyException e) {74throw new InvalidParameterException(e.getMessage());75}7677this.keySize = keySize;78}7980// second initialize method. See JCA doc81// random and exponent are always ignored82@Override83public void initialize(AlgorithmParameterSpec params, SecureRandom random)84throws InvalidAlgorithmParameterException {8586int tmpSize;87if (params == null) {88tmpSize = DEF_RSA_KEY_SIZE;89} else if (params instanceof RSAKeyGenParameterSpec) {9091if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) {92throw new InvalidAlgorithmParameterException93("Exponent parameter is not supported");94}95tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize();9697} else {98throw new InvalidAlgorithmParameterException99("Params must be an instance of RSAKeyGenParameterSpec");100}101102try {103RSAKeyFactory.checkKeyLengths(tmpSize, null,104KEY_SIZE_MIN, KEY_SIZE_MAX);105} catch (InvalidKeyException e) {106throw new InvalidAlgorithmParameterException(107"Invalid Key sizes", e);108}109110this.keySize = tmpSize;111}112113// generate the keypair. See JCA doc114@Override115public KeyPair generateKeyPair() {116117try {118// Generate each keypair in a unique key container119CKeyPair keys =120generateCKeyPair(keyAlg, keySize,121"{" + UUID.randomUUID().toString() + "}");122return new KeyPair(keys.getPublic(), keys.getPrivate());123124} catch (KeyException e) {125throw new ProviderException(e);126}127}128129private static native CKeyPair generateCKeyPair(String alg, int keySize,130String keyContainerName) throws KeyException;131}132}133134135