Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
/ jdk22u Public archive

Commit

Permalink
8324646: Avoid Class.forName in SecureRandom constructor
Browse files Browse the repository at this point in the history
Backport-of: 8ef918d6678437a5b351b172bb4cf144eeaa975f
  • Loading branch information
olivergillespie authored and shipilev committed Apr 2, 2024
1 parent 39948e5 commit 5cb863d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/java.base/share/classes/java/security/Provider.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -27,7 +27,9 @@

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.*;
Expand Down Expand Up @@ -1556,20 +1558,20 @@ public String toString() {
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);
Expand All @@ -1585,13 +1587,13 @@ private static void addEngine(String name, boolean sp, String paramName) {
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);
Expand All @@ -1610,18 +1612,20 @@ private static void addEngine(String name, boolean sp, String paramName) {
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
Expand Down Expand Up @@ -1895,8 +1899,7 @@ public Object newInstance(Object constructorParameter)
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
Expand All @@ -1907,7 +1910,7 @@ public Object newInstance(Object constructorParameter)
if (!ctrParamClz.isAssignableFrom(argClass)) {
throw new InvalidParameterException
("constructorParameter must be instanceof "
+ cap.constructorParameterClassName.replace('$', '.')
+ cap.constructorParameterClass.getName().replace('$', '.')
+ " for engine type " + type);
}
}
Expand Down
43 changes: 43 additions & 0 deletions test/micro/org/openjdk/bench/java/security/SecureRandomBench.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

1 comment on commit 5cb863d

@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.