8324646: Avoid Class.forName in SecureRandom constructor

Reviewed-by: shade, weijun
This commit is contained in:
Oli Gillespie 2024-02-09 14:38:09 +00:00 committed by Weijun Wang
parent 69b2674c68
commit 8ef918d667
2 changed files with 60 additions and 14 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2024, 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
@ -27,7 +27,9 @@ package java.security;
import jdk.internal.event.SecurityProviderServiceEvent;
import javax.security.auth.login.Configuration;
import java.io.*;
import java.security.cert.CertStoreParameters;
import java.util.*;
import static java.util.Locale.ENGLISH;
import java.lang.ref.*;
@ -1558,20 +1560,20 @@ public abstract class Provider extends Properties {
private static class EngineDescription {
final String name;
final boolean supportsParameter;
final String constructorParameterClassName;
final Class<?> constructorParameterClass;
EngineDescription(String name, boolean sp, String paramName) {
EngineDescription(String name, boolean sp, Class<?> constructorParameterClass) {
this.name = name;
this.supportsParameter = sp;
this.constructorParameterClassName = paramName;
this.constructorParameterClass = constructorParameterClass;
}
}
// built in knowledge of the engine types shipped as part of the JDK
private static final Map<String,EngineDescription> knownEngines;
private static void addEngine(String name, boolean sp, String paramName) {
EngineDescription ed = new EngineDescription(name, sp, paramName);
private static void addEngine(String name, boolean sp, Class<?> constructorParameterClass) {
EngineDescription ed = new EngineDescription(name, sp, constructorParameterClass);
// also index by canonical name to avoid toLowerCase() for some lookups
knownEngines.put(name.toLowerCase(ENGLISH), ed);
knownEngines.put(name, ed);
@ -1587,13 +1589,13 @@ public abstract class Provider extends Properties {
addEngine("KeyStore", false, null);
addEngine("MessageDigest", false, null);
addEngine("SecureRandom", false,
"java.security.SecureRandomParameters");
SecureRandomParameters.class);
addEngine("Signature", true, null);
addEngine("CertificateFactory", false, null);
addEngine("CertPathBuilder", false, null);
addEngine("CertPathValidator", false, null);
addEngine("CertStore", false,
"java.security.cert.CertStoreParameters");
CertStoreParameters.class);
// JCE
addEngine("Cipher", true, null);
addEngine("ExemptionMechanism", false, null);
@ -1612,18 +1614,20 @@ public abstract class Provider extends Properties {
addEngine("SaslClientFactory", false, null);
addEngine("SaslServerFactory", false, null);
// POLICY
@SuppressWarnings("removal")
Class<Policy.Parameters> policyParams = Policy.Parameters.class;
addEngine("Policy", false,
"java.security.Policy$Parameters");
policyParams);
// CONFIGURATION
addEngine("Configuration", false,
"javax.security.auth.login.Configuration$Parameters");
Configuration.Parameters.class);
// XML DSig
addEngine("XMLSignatureFactory", false, null);
addEngine("KeyInfoFactory", false, null);
addEngine("TransformService", false, null);
// Smart Card I/O
addEngine("TerminalFactory", false,
"java.lang.Object");
Object.class);
}
// get the "standard" (mixed-case) engine name for arbitrary case engine name
@ -1897,8 +1901,7 @@ public abstract class Provider extends Properties {
ctrParamClz = constructorParameter == null?
null : constructorParameter.getClass();
} else {
ctrParamClz = cap.constructorParameterClassName == null?
null : Class.forName(cap.constructorParameterClassName);
ctrParamClz = cap.constructorParameterClass;
if (constructorParameter != null) {
if (ctrParamClz == null) {
throw new InvalidParameterException
@ -1909,7 +1912,7 @@ public abstract class Provider extends Properties {
if (!ctrParamClz.isAssignableFrom(argClass)) {
throw new InvalidParameterException
("constructorParameter must be instanceof "
+ cap.constructorParameterClassName.replace('$', '.')
+ cap.constructorParameterClass.getName().replace('$', '.')
+ " for engine type " + type);
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright Amazon.com Inc. 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.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
package org.openjdk.bench.java.security;
import org.openjdk.jmh.annotations.*;
import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(value = 3)
public class SecureRandomBench {
@Benchmark
public SecureRandom create() throws Exception {
return new SecureRandom();
}
}