8260274: Cipher.init(int, key) does not use highest priority provider for random bytes

Reviewed-by: ascarpino, xuelei
This commit is contained in:
Valerie Peng 2021-03-18 23:23:19 +00:00
parent 6aa28b3bdb
commit 434a399bea
8 changed files with 243 additions and 19 deletions

View file

@ -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
@ -309,7 +309,7 @@ public class AlgorithmParameterGenerator {
* @param size the size (number of bits).
*/
public final void init(int size) {
paramGenSpi.engineInit(size, JCAUtil.getSecureRandom());
paramGenSpi.engineInit(size, JCAUtil.getDefSecureRandom());
}
/**
@ -340,7 +340,7 @@ public class AlgorithmParameterGenerator {
*/
public final void init(AlgorithmParameterSpec genParamSpec)
throws InvalidAlgorithmParameterException {
paramGenSpi.engineInit(genParamSpec, JCAUtil.getSecureRandom());
paramGenSpi.engineInit(genParamSpec, JCAUtil.getDefSecureRandom());
}
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2019, 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
@ -373,7 +373,7 @@ public abstract class KeyPairGenerator extends KeyPairGeneratorSpi {
* supported by this KeyPairGenerator object.
*/
public void initialize(int keysize) {
initialize(keysize, JCAUtil.getSecureRandom());
initialize(keysize, JCAUtil.getDefSecureRandom());
}
/**
@ -433,7 +433,7 @@ public abstract class KeyPairGenerator extends KeyPairGeneratorSpi {
*/
public void initialize(AlgorithmParameterSpec params)
throws InvalidAlgorithmParameterException {
initialize(params, JCAUtil.getSecureRandom());
initialize(params, JCAUtil.getDefSecureRandom());
}
/**

View file

@ -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
@ -1233,7 +1233,7 @@ public class Cipher {
* 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());
}
/**
@ -1372,7 +1372,7 @@ public class Cipher {
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());
}
/**
@ -1513,7 +1513,7 @@ public class Cipher {
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());
}
/**
@ -1659,7 +1659,7 @@ public class Cipher {
public final void init(int opmode, Certificate certificate)
throws InvalidKeyException
{
init(opmode, certificate, JCAUtil.getSecureRandom());
init(opmode, certificate, JCAUtil.getDefSecureRandom());
}
/**

View file

@ -448,7 +448,7 @@ public class KeyAgreement {
* has an incompatible algorithm type.
*/
public final void init(Key key) throws InvalidKeyException {
init(key, JCAUtil.getSecureRandom());
init(key, JCAUtil.getDefSecureRandom());
}
/**
@ -516,7 +516,7 @@ public class KeyAgreement {
public final void init(Key key, AlgorithmParameterSpec params)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
init(key, params, JCAUtil.getSecureRandom());
init(key, params, JCAUtil.getDefSecureRandom());
}
private String getProviderName() {

View file

@ -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
@ -449,7 +449,7 @@ public class KeyGenerator {
public final void init(AlgorithmParameterSpec params)
throws InvalidAlgorithmParameterException
{
init(params, JCAUtil.getSecureRandom());
init(params, JCAUtil.getDefSecureRandom());
}
/**
@ -513,7 +513,7 @@ public class KeyGenerator {
* supported.
*/
public final void init(int keysize) {
init(keysize, JCAUtil.getSecureRandom());
init(keysize, JCAUtil.getDefSecureRandom());
}
/**

View file

@ -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
@ -26,7 +26,6 @@
package sun.security.jca;
import java.lang.ref.*;
import java.security.*;
/**
@ -59,6 +58,8 @@ public final class JCAUtil {
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
@ -69,4 +70,27 @@ public final class JCAUtil {
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;
}
}

View file

@ -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
@ -150,6 +150,7 @@ public class Providers {
} else {
changeThreadProviderList(newList);
}
JCAUtil.clearDefSecureRandom();
}
/**