Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8260274: Cipher.init(int, key) does not use highest priority provider…
… for random bytes

Reviewed-by: goetz, mdoerr
Backport-of: 434a399
  • Loading branch information
RealLucy committed Jun 15, 2023
1 parent d7701cd commit c1939dd
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 19 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, 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
Expand Down Expand Up @@ -309,7 +309,7 @@ public final Provider getProvider() {
* @param size the size (number of bits).
*/
public final void init(int size) {
paramGenSpi.engineInit(size, JCAUtil.getSecureRandom());
paramGenSpi.engineInit(size, JCAUtil.getDefSecureRandom());
}

/**
Expand Down Expand Up @@ -340,7 +340,7 @@ public final void init(int size, SecureRandom random) {
*/
public final void init(AlgorithmParameterSpec genParamSpec)
throws InvalidAlgorithmParameterException {
paramGenSpi.engineInit(genParamSpec, JCAUtil.getSecureRandom());
paramGenSpi.engineInit(genParamSpec, JCAUtil.getDefSecureRandom());
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, 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
Expand Down Expand Up @@ -373,7 +373,7 @@ void disableFailover() {
* supported by this KeyPairGenerator object.
*/
public void initialize(int keysize) {
initialize(keysize, JCAUtil.getSecureRandom());
initialize(keysize, JCAUtil.getDefSecureRandom());
}

/**
Expand Down Expand Up @@ -433,7 +433,7 @@ public void initialize(int keysize, SecureRandom random) {
*/
public void initialize(AlgorithmParameterSpec params)
throws InvalidAlgorithmParameterException {
initialize(params, JCAUtil.getSecureRandom());
initialize(params, JCAUtil.getDefSecureRandom());
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/java.base/share/classes/javax/crypto/Cipher.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, 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
Expand Down Expand Up @@ -1220,7 +1220,7 @@ private static String getOpmodeString(int opmode) {
* by the underlying {@code CipherSpi}.
*/
public final void init(int opmode, Key key) throws InvalidKeyException {
init(opmode, key, JCAUtil.getSecureRandom());
init(opmode, key, JCAUtil.getDefSecureRandom());
}

/**
Expand Down Expand Up @@ -1361,7 +1361,7 @@ public final void init(int opmode, Key key, SecureRandom random)
public final void init(int opmode, Key key, AlgorithmParameterSpec params)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
init(opmode, key, params, JCAUtil.getSecureRandom());
init(opmode, key, params, JCAUtil.getDefSecureRandom());
}

/**
Expand Down Expand Up @@ -1504,7 +1504,7 @@ public final void init(int opmode, Key key, AlgorithmParameterSpec params,
public final void init(int opmode, Key key, AlgorithmParameters params)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
init(opmode, key, params, JCAUtil.getSecureRandom());
init(opmode, key, params, JCAUtil.getDefSecureRandom());
}

/**
Expand Down Expand Up @@ -1652,7 +1652,7 @@ public final void init(int opmode, Key key, AlgorithmParameters params,
public final void init(int opmode, Certificate certificate)
throws InvalidKeyException
{
init(opmode, certificate, JCAUtil.getSecureRandom());
init(opmode, certificate, JCAUtil.getDefSecureRandom());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/javax/crypto/KeyAgreement.java
Expand Up @@ -446,7 +446,7 @@ public final Provider getProvider() {
* has an incompatible algorithm type.
*/
public final void init(Key key) throws InvalidKeyException {
init(key, JCAUtil.getSecureRandom());
init(key, JCAUtil.getDefSecureRandom());
}

/**
Expand Down Expand Up @@ -514,7 +514,7 @@ public final void init(Key key, SecureRandom random)
public final void init(Key key, AlgorithmParameterSpec params)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
init(key, params, JCAUtil.getSecureRandom());
init(key, params, JCAUtil.getDefSecureRandom());
}

private String getProviderName() {
Expand Down
6 changes: 3 additions & 3 deletions src/java.base/share/classes/javax/crypto/KeyGenerator.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, 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
Expand Down Expand Up @@ -450,7 +450,7 @@ public final void init(SecureRandom random) {
public final void init(AlgorithmParameterSpec params)
throws InvalidAlgorithmParameterException
{
init(params, JCAUtil.getSecureRandom());
init(params, JCAUtil.getDefSecureRandom());
}

/**
Expand Down Expand Up @@ -514,7 +514,7 @@ public final void init(AlgorithmParameterSpec params, SecureRandom random)
* supported.
*/
public final void init(int keysize) {
init(keysize, JCAUtil.getSecureRandom());
init(keysize, JCAUtil.getDefSecureRandom());
}

/**
Expand Down
28 changes: 26 additions & 2 deletions src/java.base/share/classes/sun/security/jca/JCAUtil.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, 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
Expand All @@ -26,7 +26,6 @@
package sun.security.jca;

import java.lang.ref.*;

import java.security.*;

/**
Expand Down Expand Up @@ -59,6 +58,8 @@ private static class CachedSecureRandomHolder {
public static SecureRandom instance = new SecureRandom();
}

private static volatile SecureRandom def = null;

/**
* Get a SecureRandom instance. This method should be used by JDK
* internal code in favor of calling "new SecureRandom()". That needs to
Expand All @@ -69,4 +70,27 @@ public static SecureRandom getSecureRandom() {
return CachedSecureRandomHolder.instance;
}

// called by sun.security.jca.Providers class when provider list is changed
static void clearDefSecureRandom() {
def = null;
}

/**
* Get the default SecureRandom instance. This method is the
* optimized version of "new SecureRandom()" which re-uses the default
* SecureRandom impl if the provider table is the same.
*/
public static SecureRandom getDefSecureRandom() {
SecureRandom result = def;
if (result == null) {
synchronized (JCAUtil.class) {
result = def;
if (result == null) {
def = result = new SecureRandom();
}
}
}
return result;

}
}
3 changes: 2 additions & 1 deletion src/java.base/share/classes/sun/security/jca/Providers.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, 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
Expand Down Expand Up @@ -150,6 +150,7 @@ public static void setProviderList(ProviderList newList) {
} else {
changeThreadProviderList(newList);
}
JCAUtil.clearDefSecureRandom();
}

/**
Expand Down

1 comment on commit c1939dd

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.