|
24 | 24 |
|
25 | 25 | import java.nio.charset.Charset; |
26 | 26 | import java.security.MessageDigest; |
27 | | -import java.util.Arrays; |
| 27 | +import java.security.SecureRandom; |
28 | 28 | import java.util.Map; |
29 | | -import java.util.concurrent.ThreadLocalRandom; |
30 | 29 |
|
31 | 30 | import static java.nio.charset.StandardCharsets.ISO_8859_1; |
32 | 31 | import static java.nio.charset.StandardCharsets.UTF_8; |
@@ -283,6 +282,9 @@ public enum AuthScheme { |
283 | 282 | */ |
284 | 283 | public static class Builder { |
285 | 284 |
|
| 285 | + // cnonce must be unpredictable (RFC 7616 section 3.3), like the NTLM and SCRAM nonces |
| 286 | + private static final ThreadLocal<SecureRandom> CNONCE_RANDOM = ThreadLocal.withInitial(SecureRandom::new); |
| 287 | + |
286 | 288 | private final @Nullable String principal; |
287 | 289 | private final @Nullable String password; |
288 | 290 | private @Nullable AuthScheme scheme; |
@@ -608,13 +610,10 @@ public Builder parseProxyAuthenticateHeader(String headerLine) { |
608 | 610 | return null; |
609 | 611 | } |
610 | 612 |
|
611 | | - private void newCnonce(MessageDigest md) { |
| 613 | + private void newCnonce() { |
612 | 614 | byte[] b = new byte[8]; |
613 | | - ThreadLocalRandom.current().nextBytes(b); |
614 | | - byte[] full = md.digest(b); |
615 | | - // trim to first 8 bytes → 16 hex chars |
616 | | - byte[] small = Arrays.copyOf(full, Math.min(8, full.length)); |
617 | | - cnonce = toHexString(small); |
| 615 | + CNONCE_RANDOM.get().nextBytes(b); |
| 616 | + cnonce = toHexString(b); |
618 | 617 | } |
619 | 618 |
|
620 | 619 | private static byte[] digestFromRecycledStringBuilder(StringBuilder sb, MessageDigest md, Charset enc) { |
@@ -719,7 +718,7 @@ public Realm build() { |
719 | 718 | // Defensive: if algorithm is null, default to MD5 |
720 | 719 | String algo = (algorithm != null) ? algorithm : "MD5"; |
721 | 720 | MessageDigest md = getDigestInstance(algo); |
722 | | - newCnonce(md); |
| 721 | + newCnonce(); |
723 | 722 | newResponse(md); |
724 | 723 | } |
725 | 724 |
|
|
0 commit comments