< prev index next >
src/java.base/share/classes/sun/security/rsa/RSAKeyPairGenerator.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 31,95 ****
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.RSAKeyGenParameterSpec;
import sun.security.jca.JCAUtil;
import static sun.security.util.SecurityProviderConstants.DEF_RSA_KEY_SIZE;
/**
* RSA keypair generation. Standard algorithm, minimum key length 512 bit.
* We generate two random primes until we find two where phi is relative
* prime to the public exponent. Default exponent is 65537. It has only bit 0
* and bit 4 set, which makes it particularly efficient.
*
* @since 1.5
* @author Andreas Sterbenz
*/
! public final class RSAKeyPairGenerator extends KeyPairGeneratorSpi {
// public exponent to use
private BigInteger publicExponent;
// size of the key to generate, >= RSAKeyFactory.MIN_MODLEN
private int keySize;
// PRNG to use
private SecureRandom random;
! public RSAKeyPairGenerator() {
// initialize to default in case the app does not call initialize()
! initialize(DEF_RSA_KEY_SIZE, null);
}
// initialize the generator. See JCA doc
public void initialize(int keySize, SecureRandom random) {
-
- // do not allow unreasonably small or large key sizes,
- // probably user error
try {
! RSAKeyFactory.checkKeyLengths(keySize, RSAKeyGenParameterSpec.F4,
! 512, 64 * 1024);
! } catch (InvalidKeyException e) {
! throw new InvalidParameterException(e.getMessage());
}
-
- this.keySize = keySize;
- this.random = random;
- this.publicExponent = RSAKeyGenParameterSpec.F4;
}
// second initialize method. See JCA doc.
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
throws InvalidAlgorithmParameterException {
-
if (params instanceof RSAKeyGenParameterSpec == false) {
throw new InvalidAlgorithmParameterException
("Params must be instance of RSAKeyGenParameterSpec");
}
RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
int tmpKeySize = rsaSpec.getKeysize();
BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();
if (tmpPublicExponent == null) {
tmpPublicExponent = RSAKeyGenParameterSpec.F4;
} else {
if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
--- 31,95 ----
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.RSAKeyGenParameterSpec;
import sun.security.jca.JCAUtil;
import static sun.security.util.SecurityProviderConstants.DEF_RSA_KEY_SIZE;
+ import static sun.security.util.SecurityProviderConstants.DEF_RSASSA_PSS_KEY_SIZE;
+ import sun.security.x509.AlgorithmId;
+ import static sun.security.rsa.RSAUtil.KeyType;
/**
* RSA keypair generation. Standard algorithm, minimum key length 512 bit.
* We generate two random primes until we find two where phi is relative
* prime to the public exponent. Default exponent is 65537. It has only bit 0
* and bit 4 set, which makes it particularly efficient.
*
* @since 1.5
* @author Andreas Sterbenz
*/
! public abstract class RSAKeyPairGenerator extends KeyPairGeneratorSpi {
// public exponent to use
private BigInteger publicExponent;
// size of the key to generate, >= RSAKeyFactory.MIN_MODLEN
private int keySize;
+ private final KeyType type;
+ private AlgorithmId rsaId;
+
// PRNG to use
private SecureRandom random;
! RSAKeyPairGenerator(KeyType type, int defKeySize) {
! this.type = type;
// initialize to default in case the app does not call initialize()
! initialize(defKeySize, null);
}
// initialize the generator. See JCA doc
public void initialize(int keySize, SecureRandom random) {
try {
! initialize(new RSAKeyGenParameterSpec(keySize,
! RSAKeyGenParameterSpec.F4), null);
! } catch (InvalidAlgorithmParameterException iape) {
! throw new InvalidParameterException(iape.getMessage());
}
}
// second initialize method. See JCA doc.
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
throws InvalidAlgorithmParameterException {
if (params instanceof RSAKeyGenParameterSpec == false) {
throw new InvalidAlgorithmParameterException
("Params must be instance of RSAKeyGenParameterSpec");
}
RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
int tmpKeySize = rsaSpec.getKeysize();
BigInteger tmpPublicExponent = rsaSpec.getPublicExponent();
+ AlgorithmParameterSpec tmpParams = rsaSpec.getKeyParams();
if (tmpPublicExponent == null) {
tmpPublicExponent = RSAKeyGenParameterSpec.F4;
} else {
if (tmpPublicExponent.compareTo(RSAKeyGenParameterSpec.F0) < 0) {
*** 109,118 ****
--- 109,125 ----
} catch (InvalidKeyException e) {
throw new InvalidAlgorithmParameterException(
"Invalid key sizes", e);
}
+ try {
+ this.rsaId = RSAUtil.createAlgorithmId(type, tmpParams);
+ } catch (ProviderException e) {
+ throw new InvalidAlgorithmParameterException(
+ "Invalid key parameters", e);
+ }
+
this.keySize = tmpKeySize;
this.publicExponent = tmpPublicExponent;
this.random = random;
}
*** 164,181 ****
// crt coefficient coeff is the inverse of q mod p
BigInteger coeff = q.modInverse(p);
try {
! PublicKey publicKey = new RSAPublicKeyImpl(n, e);
! PrivateKey privateKey =
! new RSAPrivateCrtKeyImpl(n, e, d, p, q, pe, qe, coeff);
return new KeyPair(publicKey, privateKey);
} catch (InvalidKeyException exc) {
// invalid key exception only thrown for keys < 512 bit,
// will not happen here
throw new RuntimeException(exc);
}
}
}
}
--- 171,199 ----
// crt coefficient coeff is the inverse of q mod p
BigInteger coeff = q.modInverse(p);
try {
! PublicKey publicKey = new RSAPublicKeyImpl(rsaId, n, e);
! PrivateKey privateKey = new RSAPrivateCrtKeyImpl(
! rsaId, n, e, d, p, q, pe, qe, coeff);
return new KeyPair(publicKey, privateKey);
} catch (InvalidKeyException exc) {
// invalid key exception only thrown for keys < 512 bit,
// will not happen here
throw new RuntimeException(exc);
}
}
}
+ public static final class Legacy extends RSAKeyPairGenerator {
+ public Legacy() {
+ super(KeyType.RSA, DEF_RSA_KEY_SIZE);
+ }
+ }
+
+ public static final class PSS extends RSAKeyPairGenerator {
+ public PSS() {
+ super(KeyType.PSS, DEF_RSASSA_PSS_KEY_SIZE);
+ }
+ }
}
< prev index next >