Skip to content

Commit db7af2b

Browse files
koushikthirupatturseanjmullan
authored andcommitted
8349550: Improve SASL random usage
Reviewed-by: mullan
1 parent 9982995 commit db7af2b

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/java.base/share/classes/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@
318318
exports sun.security.internal.spec to
319319
jdk.crypto.cryptoki;
320320
exports sun.security.jca to
321+
java.security.sasl,
321322
java.smartcardio,
322323
jdk.crypto.cryptoki,
323324
jdk.naming.dns;

src/java.security.sasl/share/classes/com/sun/security/sasl/CramMD5Server.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,11 +25,13 @@
2525

2626
package com.sun.security.sasl;
2727

28+
import sun.security.jca.JCAUtil;
29+
2830
import java.io.IOException;
2931
import java.security.NoSuchAlgorithmException;
32+
import java.security.SecureRandom;
3033
import java.util.logging.Level;
3134
import java.util.Map;
32-
import java.util.Random;
3335
import javax.security.sasl.*;
3436
import javax.security.auth.callback.*;
3537

@@ -52,6 +54,10 @@
5254
* @author Rosanna Lee
5355
*/
5456
final class CramMD5Server extends CramMD5Base implements SaslServer {
57+
58+
/* SecureRandom instance to generate random digits used in challenge */
59+
private static final SecureRandom SECURE_RANDOM = JCAUtil.getDefSecureRandom();
60+
5561
private String fqdn;
5662
private byte[] challengeData = null;
5763
private String authzid;
@@ -113,8 +119,7 @@ public byte[] evaluateResponse(byte[] responseData)
113119
}
114120

115121
// Generate challenge {random, timestamp, fqdn}
116-
Random random = new Random();
117-
long rand = random.nextLong();
122+
long rand = SECURE_RANDOM.nextLong();
118123
long timestamp = System.currentTimeMillis();
119124

120125
StringBuilder sb = new StringBuilder();

src/java.security.sasl/share/classes/com/sun/security/sasl/digest/DigestMD5Base.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,10 +33,10 @@
3333
import java.util.Arrays;
3434
import java.util.List;
3535
import java.util.logging.Level;
36-
import java.util.Random;
3736
import java.security.MessageDigest;
3837
import java.security.NoSuchAlgorithmException;
3938
import java.security.InvalidKeyException;
39+
import java.security.SecureRandom;
4040
import java.security.spec.KeySpec;
4141
import java.security.spec.InvalidKeySpecException;
4242
import java.security.InvalidAlgorithmParameterException;
@@ -59,6 +59,7 @@
5959
import javax.security.sasl.*;
6060

6161
import com.sun.security.sasl.util.AbstractSaslImpl;
62+
import sun.security.jca.JCAUtil;
6263

6364
/**
6465
* Utility class for DIGEST-MD5 mechanism. Provides utility methods
@@ -132,6 +133,9 @@ abstract class DigestMD5Base extends AbstractSaslImpl {
132133

133134
protected static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
134135

136+
/* SecureRandom instance to generate nonce */
137+
private static final SecureRandom SECURE_RANDOM = JCAUtil.getDefSecureRandom();
138+
135139
/* ------------------- Variable Fields ----------------------- */
136140

137141
/* Used to track progress of authentication; step numbers from RFC 2831 */
@@ -269,7 +273,6 @@ public Object getNegotiatedProperty(String propName) {
269273
* is slightly faster and a more compact representation of the same info.
270274
* @return A non-null byte array containing the nonce value for the
271275
* digest challenge or response.
272-
* Could use SecureRandom to be more secure but it is very slow.
273276
*/
274277

275278
/** This array maps the characters to their 6 bit values */
@@ -293,10 +296,8 @@ public Object getNegotiatedProperty(String propName) {
293296

294297
protected static final byte[] generateNonce() {
295298

296-
// SecureRandom random = new SecureRandom();
297-
Random random = new Random();
298299
byte[] randomData = new byte[RAW_NONCE_SIZE];
299-
random.nextBytes(randomData);
300+
SECURE_RANDOM.nextBytes(randomData);
300301

301302
byte[] nonce = new byte[ENCODED_NONCE_SIZE];
302303

0 commit comments

Comments
 (0)