Skip to content

Commit

Permalink
Merge pull request #744 from KostasTsiounis/secure_random
Browse files Browse the repository at this point in the history
Revert to Java impl when non-default SecureRandom present
  • Loading branch information
pshipton authored Dec 12, 2023
2 parents b9a45e8 + 158ea3f commit 6f6d6a5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.security.KeyPair;
import java.security.KeyPairGeneratorSpi;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.ProviderException;
import java.security.PublicKey;
import java.security.SecureRandom;
Expand All @@ -56,6 +57,7 @@

import sun.security.ec.point.*;
import sun.security.jca.JCAUtil;
import sun.security.provider.Sun;
import sun.security.util.ECUtil;

import static sun.security.ec.ECOperations.IntermediateValueException;
Expand Down Expand Up @@ -97,6 +99,28 @@ public NativeECKeyPairGenerator() {

@Override
public void initialize(int keySize, SecureRandom random) {
if (random == null) {
if (nativeCryptTrace) {
System.err.println("No SecureRandom implementation was provided during"
+ " initialization. Using OpenSSL.");
}
} else if ((random.getProvider() instanceof Sun)
&& ("NativePRNG".equals(random.getAlgorithm()) || "DRBG".equals(random.getAlgorithm()))
) {
if (nativeCryptTrace) {
System.err.println("Default SecureRandom implementation was provided during"
+ " initialization. Using OpenSSL.");
}
} else {
if (nativeCryptTrace) {
System.err.println("SecureRandom implementation was provided during"
+ " initialization. Using Java implementation instead of OpenSSL.");
}
this.javaImplementation = new ECKeyPairGenerator();
this.javaImplementation.initialize(keySize, random);
return;
}

if (keySize < KEY_SIZE_MIN) {
throw new InvalidParameterException
("Key size must be at least " + KEY_SIZE_MIN + " bits");
Expand Down Expand Up @@ -125,6 +149,28 @@ public void initialize(int keySize, SecureRandom random) {
@Override
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
throws InvalidAlgorithmParameterException {
if (random == null) {
if (nativeCryptTrace) {
System.err.println("No SecureRandom implementation was provided during"
+ " initialization. Using OpenSSL.");
}
} else if ((random.getProvider() instanceof Sun)
&& ("NativePRNG".equals(random.getAlgorithm()) || "DRBG".equals(random.getAlgorithm()))
) {
if (nativeCryptTrace) {
System.err.println("Default SecureRandom implementation was provided during"
+ " initialization. Using OpenSSL.");
}
} else {
if (nativeCryptTrace) {
System.err.println("SecureRandom implementation was provided during"
+ " initialization. Using Java implementation instead of OpenSSL.");
}
this.javaImplementation = new ECKeyPairGenerator();
this.javaImplementation.initialize(params, random);
return;
}

ECParameterSpec ecSpec = null;

if (params instanceof ECParameterSpec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGeneratorSpi;
import java.security.Provider;
import java.security.ProviderException;
import java.security.PublicKey;
import java.security.SecureRandom;
Expand All @@ -46,6 +47,8 @@

import jdk.crypto.jniprovider.NativeCrypto;

import sun.security.jca.JCAUtil;
import sun.security.provider.Sun;
import sun.security.util.BitArray;
import sun.security.x509.AlgorithmId;
import sun.security.x509.X509Key;
Expand All @@ -59,6 +62,7 @@ public class NativeXDHKeyPairGenerator extends KeyPairGeneratorSpi {
private final XECParameters lockedParams;

private XDHKeyPairGenerator javaImplementation;
private boolean useJavaImpl;

public NativeXDHKeyPairGenerator() {
tryInitialize(NamedParameterSpec.X25519);
Expand Down Expand Up @@ -105,10 +109,42 @@ private void initializeImpl(XECParameters params, SecureRandom random) {
}

ops = new XECOperations(params);
this.random = (random != null) ? random : JCAUtil.getSecureRandom();

useJavaImpl = false;
if (random == null) {
if (nativeCryptTrace) {
System.err.println("No SecureRandom implementation was provided during"
+ " initialization. Using OpenSSL.");
}
} else if ((random.getProvider() instanceof Sun)
&& ("NativePRNG".equals(random.getAlgorithm()) || "DRBG".equals(random.getAlgorithm()))
) {
if (nativeCryptTrace) {
System.err.println("Default SecureRandom implementation was provided during"
+ " initialization. Using OpenSSL.");
}
} else {
if (nativeCryptTrace) {
System.err.println("SecureRandom implementation was provided during"
+ " initialization. Using Java implementation instead of OpenSSL.");
}
useJavaImpl = true;
}
}

@Override
public KeyPair generateKeyPair() {
/*
* When the keypair generator is initialized with
* anything other than the default SecureRandom
* implementation, use the Java implementation
* to generate the keypair.
*/
if (useJavaImpl) {
return javaImplGenerateKeyPair();
}

/* If library isn't loaded, use Java implementation. */
if (!NativeCrypto.isAllowedAndLoaded()) {
if (nativeCryptTrace) {
Expand Down Expand Up @@ -177,12 +213,16 @@ public KeyPair generateKeyPair() {
*/
private void initializeJavaImplementation() {
if (javaImplementation == null) {
if (isX25519(ops.getParameters())) {
if (lockedParams == null) {
javaImplementation = new XDHKeyPairGenerator();
} else if (isX25519(lockedParams)) {
javaImplementation = new XDHKeyPairGenerator.X25519();
} else {
javaImplementation = new XDHKeyPairGenerator.X448();
}
}

javaImplementation.initialize(ops.getParameters().getBits(), random);
}

/*
Expand Down
3 changes: 2 additions & 1 deletion src/java.base/share/classes/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved
* (c) Copyright IBM Corp. 2022, 2023 All Rights Reserved
* ===========================================================================
*/

Expand Down Expand Up @@ -298,6 +298,7 @@
java.rmi,
java.security.jgss,
jdk.crypto.cryptoki,
jdk.crypto.ec,
jdk.security.auth;
exports sun.security.provider.certpath to
java.naming,
Expand Down

0 comments on commit 6f6d6a5

Please sign in to comment.