You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2023/10/10 09:11:31 UTC
[santuario-xml-security-java] branch 3.0.x-fixes updated: SANTUARIO-606 - Avoid creating the SecureRandom in XMLSecurityConstants (#218)
This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch 3.0.x-fixes
in repository https://gitbox.apache.org/repos/asf/santuario-xml-security-java.git
The following commit(s) were added to refs/heads/3.0.x-fixes by this push:
new 060922b1 SANTUARIO-606 - Avoid creating the SecureRandom in XMLSecurityConstants (#218)
060922b1 is described below
commit 060922b1c22452d78e82afcd3fd7e04efe5c685e
Author: Peter Palaga <pp...@redhat.com>
AuthorDate: Tue Oct 10 11:11:01 2023 +0200
SANTUARIO-606 - Avoid creating the SecureRandom in XMLSecurityConstants (#218)
during class initialization
---
.../security/stax/ext/XMLSecurityConstants.java | 39 +++++++++++++++-------
1 file changed, 27 insertions(+), 12 deletions(-)
diff --git a/src/main/java/org/apache/xml/security/stax/ext/XMLSecurityConstants.java b/src/main/java/org/apache/xml/security/stax/ext/XMLSecurityConstants.java
index 0c752da7..3368c97a 100644
--- a/src/main/java/org/apache/xml/security/stax/ext/XMLSecurityConstants.java
+++ b/src/main/java/org/apache/xml/security/stax/ext/XMLSecurityConstants.java
@@ -22,7 +22,9 @@ import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
+import java.security.AccessController;
import java.security.NoSuchAlgorithmException;
+import java.security.PrivilegedAction;
import java.security.SecureRandom;
import javax.xml.datatype.DatatypeConfigurationException;
@@ -43,19 +45,13 @@ public class XMLSecurityConstants {
public static final XMLOutputFactory xmlOutputFactory;
public static final XMLOutputFactory xmlOutputFactoryNonRepairingNs;
- private static final SecureRandom SECURE_RANDOM;
+ private static volatile SecureRandom SECURE_RANDOM;
+ private static final Object SECURE_RANDOM_LOCK = new Object();
private static final String RANDOM_ALGORITHM_KEY = "org.apache.xml.security.securerandom.algorithm";
private static JAXBContext jaxbContext;
private static Schema schema;
static {
- try {
- String PrngAlgorithm = System.getProperty(RANDOM_ALGORITHM_KEY);
- SECURE_RANDOM = PrngAlgorithm != null ? SecureRandom.getInstance(PrngAlgorithm) : new SecureRandom();
- } catch (NoSuchAlgorithmException e) {
- throw new RuntimeException(e);
- }
-
try {
datatypeFactory = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
@@ -73,17 +69,36 @@ public class XMLSecurityConstants {
}
/**
- * Generate bytes of the given length using the supplied algorithm in RANDOM_ALGORITHM_KEY or,
- * if not specified, use SecureRandom instance from default constructor. The SecureRandom
+ * Generate bytes of the given length using the algorithm supplied in {@value #RANDOM_ALGORITHM_KEY} or,
+ * if not specified, use a {@code SecureRandom} instance from default constructor. The {@code SecureRandom}
* instance that backs this method is cached for efficiency.
*
- * @return a byte array of the given length
+ * @return a new byte array of the given length
* @throws XMLSecurityException
*/
public static byte[] generateBytes(int length) throws XMLSecurityException {
+
+ SecureRandom rnd = SECURE_RANDOM;
+ if (rnd == null) {
+ synchronized (SECURE_RANDOM_LOCK) {
+ rnd = SECURE_RANDOM;
+ if (rnd == null) {
+ try {
+ final String prngAlgorithm = AccessController.doPrivileged(
+ (PrivilegedAction<String>) () -> System.getProperty(RANDOM_ALGORITHM_KEY));
+ SECURE_RANDOM = rnd = prngAlgorithm != null
+ ? SecureRandom.getInstance(prngAlgorithm)
+ : new SecureRandom();
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+ }
+
try {
byte[] temp = new byte[length];
- SECURE_RANDOM.nextBytes(temp);
+ rnd.nextBytes(temp);
return temp;
} catch (Exception ex) {
throw new XMLSecurityException(ex);