Skip to content

Commit 308c5fb

Browse files
committed
JAVA-2740: Work around JDK bug that can cause SCRAM-SHA-1 authentication to intermittently fail, by synchronizing on the SecretKey in order to avoid premature finalization.
See https://bugs.openjdk.java.net/browse/JDK-8191177 for the bug report and https://bugs.openjdk.java.net/browse/JDK-8055183 for the suggested workaround
1 parent c8d0906 commit 308c5fb

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

driver-core/src/main/com/mongodb/connection/ScramSha1Authenticator.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.bson.internal.Base64;
2323

2424
import javax.crypto.Mac;
25+
import javax.crypto.SecretKey;
2526
import javax.crypto.SecretKeyFactory;
2627
import javax.crypto.spec.PBEKeySpec;
2728
import javax.crypto.spec.SecretKeySpec;
@@ -230,7 +231,13 @@ private byte[] hi(final String password, final byte[] salt, final int iterations
230231
}
231232

232233
try {
233-
return keyFactory.generateSecret(spec).getEncoded();
234+
SecretKey secretKey = keyFactory.generateSecret(spec);
235+
// Workaround for https://bugs.openjdk.java.net/browse/JDK-8191177, as suggested in
236+
// https://bugs.openjdk.java.net/browse/JDK-8055183
237+
//noinspection SynchronizationOnLocalVariableOrMethodParameter
238+
synchronized (secretKey) {
239+
return secretKey.getEncoded();
240+
}
234241
}
235242
catch (InvalidKeySpecException e) {
236243
throw new SaslException("Invalid key spec for PBKDC2WithHmacSHA1.", e);

0 commit comments

Comments
 (0)