Skip to content

Commit 0984c28

Browse files
committed
Code cleanup for ScramShaAuthenticator
1 parent 0def8cc commit 0984c28

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

driver-core/src/main/com/mongodb/internal/connection/ScramShaAuthenticator.java

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import javax.crypto.spec.SecretKeySpec;
3131
import javax.security.sasl.SaslClient;
3232
import javax.security.sasl.SaslException;
33-
import java.io.UnsupportedEncodingException;
33+
import java.nio.charset.StandardCharsets;
3434
import java.security.InvalidKeyException;
3535
import java.security.MessageDigest;
3636
import java.security.NoSuchAlgorithmException;
@@ -53,9 +53,9 @@ class ScramShaAuthenticator extends SaslAuthenticator {
5353
private static final int MINIMUM_ITERATION_COUNT = 4096;
5454
private static final String GS2_HEADER = "n,,";
5555
private static final int RANDOM_LENGTH = 24;
56-
private static final byte[] INT_1 = new byte[]{0, 0, 0, 1};
56+
private static final byte[] INT_1 = {0, 0, 0, 1};
5757

58-
ScramShaAuthenticator(final MongoCredentialWithCache credential, final @Nullable ServerApi serverApi) {
58+
ScramShaAuthenticator(final MongoCredentialWithCache credential, @Nullable final ServerApi serverApi) {
5959
this(credential, new DefaultRandomStringGenerator(), getAuthenicationHashGenerator(credential.getAuthenticationMechanism()),
6060
serverApi);
6161
}
@@ -65,7 +65,7 @@ class ScramShaAuthenticator extends SaslAuthenticator {
6565
}
6666

6767
ScramShaAuthenticator(final MongoCredentialWithCache credential, final RandomStringGenerator randomStringGenerator,
68-
final AuthenticationHashGenerator authenticationHashGenerator, final @Nullable ServerApi serverApi) {
68+
final AuthenticationHashGenerator authenticationHashGenerator, @Nullable final ServerApi serverApi) {
6969
super(credential, serverApi);
7070
this.randomStringGenerator = randomStringGenerator;
7171
this.authenticationHashGenerator = authenticationHashGenerator;
@@ -171,7 +171,7 @@ public byte[] evaluateChallenge(final byte[] challenge) throws SaslException {
171171
}
172172

173173
private byte[] validateServerSignature(final byte[] challenge) throws SaslException {
174-
String serverResponse = encodeUTF8(challenge);
174+
String serverResponse = new String(challenge, StandardCharsets.UTF_8);
175175
HashMap<String, String> map = parseServerResponse(serverResponse);
176176
if (!MessageDigest.isEqual(Base64.getDecoder().decode(map.get("v")), serverSignature)) {
177177
throw new SaslException("Server signature was invalid.");
@@ -199,15 +199,15 @@ public void dispose() {
199199
// nothing to do
200200
}
201201

202-
private byte[] computeClientFirstMessage() throws SaslException {
202+
private byte[] computeClientFirstMessage() {
203203
clientNonce = randomStringGenerator.generate(RANDOM_LENGTH);
204204
String clientFirstMessage = "n=" + getUserName() + ",r=" + clientNonce;
205205
clientFirstMessageBare = clientFirstMessage;
206-
return decodeUTF8(GS2_HEADER + clientFirstMessage);
206+
return (GS2_HEADER + clientFirstMessage).getBytes(StandardCharsets.UTF_8);
207207
}
208208

209209
private byte[] computeClientFinalMessage(final byte[] challenge) throws SaslException {
210-
String serverFirstMessage = encodeUTF8(challenge);
210+
String serverFirstMessage = new String(challenge, StandardCharsets.UTF_8);
211211
HashMap<String, String> map = parseServerResponse(serverFirstMessage);
212212
String serverNonce = map.get("r");
213213
if (!serverNonce.startsWith(clientNonce)) {
@@ -220,11 +220,11 @@ private byte[] computeClientFinalMessage(final byte[] challenge) throws SaslExce
220220
throw new SaslException("Invalid iteration count.");
221221
}
222222

223-
String clientFinalMessageWithoutProof = "c=" + Base64.getEncoder().encodeToString(decodeUTF8(GS2_HEADER)) + ",r=" + serverNonce;
223+
String clientFinalMessageWithoutProof = "c=" + Base64.getEncoder().encodeToString(GS2_HEADER.getBytes(StandardCharsets.UTF_8)) + ",r=" + serverNonce;
224224
String authMessage = clientFirstMessageBare + "," + serverFirstMessage + "," + clientFinalMessageWithoutProof;
225225
String clientFinalMessage = clientFinalMessageWithoutProof + ",p="
226226
+ getClientProof(getAuthenicationHash(), salt, iterationCount, authMessage);
227-
return decodeUTF8(clientFinalMessage);
227+
return clientFinalMessage.getBytes(StandardCharsets.UTF_8);
228228
}
229229

230230
/**
@@ -241,12 +241,12 @@ private byte[] computeClientFinalMessage(final byte[] challenge) throws SaslExce
241241
*/
242242
String getClientProof(final String password, final String salt, final int iterationCount, final String authMessage)
243243
throws SaslException {
244-
String hashedPasswordAndSalt = encodeUTF8(h(decodeUTF8(password + salt)));
244+
String hashedPasswordAndSalt = new String(h((password + salt).getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
245245

246246
CacheKey cacheKey = new CacheKey(hashedPasswordAndSalt, salt, iterationCount);
247247
CacheValue cachedKeys = getMongoCredentialWithCache().getFromCache(cacheKey, CacheValue.class);
248248
if (cachedKeys == null) {
249-
byte[] saltedPassword = hi(decodeUTF8(password), Base64.getDecoder().decode(salt), iterationCount);
249+
byte[] saltedPassword = hi(password.getBytes(StandardCharsets.UTF_8), Base64.getDecoder().decode(salt), iterationCount);
250250
byte[] clientKey = hmac(saltedPassword, "Client Key");
251251
byte[] serverKey = hmac(saltedPassword, "Server Key");
252252
cachedKeys = new CacheValue(clientKey, serverKey);
@@ -260,22 +260,6 @@ String getClientProof(final String password, final String salt, final int iterat
260260
return Base64.getEncoder().encodeToString(clientProof);
261261
}
262262

263-
private byte[] decodeUTF8(final String str) throws SaslException {
264-
try {
265-
return str.getBytes("UTF-8");
266-
} catch (UnsupportedEncodingException e) {
267-
throw new SaslException("UTF-8 is not a supported encoding.", e);
268-
}
269-
}
270-
271-
private String encodeUTF8(final byte[] bytes) throws SaslException {
272-
try {
273-
return new String(bytes, "UTF-8");
274-
} catch (UnsupportedEncodingException e) {
275-
throw new SaslException("UTF-8 is not a supported encoding.", e);
276-
}
277-
}
278-
279263
private byte[] h(final byte[] data) throws SaslException {
280264
try {
281265
return MessageDigest.getInstance(hAlgorithm).digest(data);
@@ -310,7 +294,7 @@ private byte[] hmac(final byte[] bytes, final String key) throws SaslException {
310294
try {
311295
Mac mac = Mac.getInstance(hmacAlgorithm);
312296
mac.init(new SecretKeySpec(bytes, hmacAlgorithm));
313-
return mac.doFinal(decodeUTF8(key));
297+
return mac.doFinal(key.getBytes(StandardCharsets.UTF_8));
314298
} catch (NoSuchAlgorithmException e) {
315299
throw new SaslException(format("Algorithm for '%s' could not be found.", hmacAlgorithm), e);
316300
} catch (InvalidKeyException e) {
@@ -462,8 +446,8 @@ public int hashCode() {
462446
}
463447

464448
private static class CacheValue {
465-
private byte[] clientKey;
466-
private byte[] serverKey;
449+
private final byte[] clientKey;
450+
private final byte[] serverKey;
467451

468452
CacheValue(final byte[] clientKey, final byte[] serverKey) {
469453
this.clientKey = clientKey;

0 commit comments

Comments
 (0)