-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathScram.java
304 lines (272 loc) · 11.4 KB
/
Scram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud;
import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.ByteString;
import io.grpc.stub.StreamObserver;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.crypto.generators.Argon2BytesGenerator;
import org.bouncycastle.crypto.params.Argon2Parameters;
import spanner.experimental.AccessToken;
import spanner.experimental.FinalScramRequest;
import spanner.experimental.FinalScramResponse;
import spanner.experimental.InitialScramRequest;
import spanner.experimental.InitialScramResponse;
import spanner.experimental.LoginRequest;
import spanner.experimental.LoginResponse;
import spanner.experimental.LoginServiceGrpc;
public class Scram {
private final String username;
private final String password;
private final byte[] clientNonce;
private final X509Certificate certificate;
private byte[] saltedPassword;
private byte[] authMessage;
private byte[] serverNonce;
private byte[] salt;
private int iterationCount;
private GrpcClient grpcClient;
private static final int ARGON_MEMORY_LIMIT = 64 * 1024;
private static final int ARGON_THREADS = 4;
private static final int ARGON_KEY_LENGTH = 32;
private static final String CLIENT_KEY_MESSAGE = "Client Key";
private static final String SERVER_KEY_MESSAGE = "Server Key";
private static final String HMAC_SHA256 = "HmacSHA256";
private static final String SHA256 = "SHA-256";
private static final int NONCE_LENGTH = 16;
private final LoginServiceGrpc.LoginServiceStub loginService;
public Scram(String username, String password, GrpcClient grpcClient){
this.username = username;
this.password = password;
this.certificate = grpcClient.getServerCertificate();
this.clientNonce = nonce();
this.loginService = grpcClient.getLoginService();
this.grpcClient = grpcClient;
}
public AccessToken login() throws Exception {
InitialScramRequest initialScramRequest =
InitialScramRequest.newBuilder().setClientNonce(ByteString.copyFrom(clientNonce)).build();
final LoginRequest initialLoginRequest =
LoginRequest.newBuilder()
.setUsername(username)
.setInitialScramRequest(initialScramRequest)
.build();
AccessToken[] accessToken = new AccessToken[1];
final StreamObserver<LoginRequest>[] requestObserverContainer = new StreamObserver[1];
final CountDownLatch latch = new CountDownLatch(1);
requestObserverContainer[0] =
loginService.login(
new StreamObserver<LoginResponse>() {
@Override
public void onNext(LoginResponse loginResponse) {
try {
if (loginResponse.hasInitialScramResponse()) {
addServerFirstMessage(loginResponse.getInitialScramResponse());
byte[] clientProof =
clientProof(
initialLoginRequest.getInitialScramRequest(),
loginResponse.getInitialScramResponse());
LoginRequest finalLoginRequest =
LoginRequest.newBuilder()
.setUsername(username)
.setFinalScramRequest(
FinalScramRequest.newBuilder()
.setCredential(ByteString.copyFrom(clientProof))
.build())
.build();
requestObserverContainer[0].onNext(finalLoginRequest);
} else {
verifyLoginResponse(loginResponse.getFinalScramResponse());
accessToken[0] = loginResponse.getAccessToken();
latch.countDown(); // Signal completion
}
} catch (Exception e) {
System.err.println("Exception in onNext: " + e.getMessage());
requestObserverContainer[0].onError(e);
}
}
@Override
public void onError(Throwable t) {
System.err.println("Error during login: " + t.getMessage());
latch.countDown();
}
@Override
public void onCompleted() {
requestObserverContainer[0].onCompleted();
}
});
requestObserverContainer[0].onNext(initialLoginRequest);
try {
latch.await();
} catch (InterruptedException e) {
throw new Exception("Login process interrupted", e);
} finally {
grpcClient.close();
}
return accessToken[0];
}
private void addServerFirstMessage(InitialScramResponse initialScramResponse) throws Exception {
serverNonce = initialScramResponse.getServerNonce().toByteArray();
salt = initialScramResponse.getSalt().toByteArray();
iterationCount = initialScramResponse.getIterationCount();
if (salt.length == 0) {
throw new Exception("No salt found in the response");
}
if (iterationCount == 0) {
throw new Exception("No iteration count found in response");
}
}
private void verifyLoginResponse(FinalScramResponse finalScramResponse) throws Exception {
byte[] serverKey = serverKey(saltedPassword);
byte[] serverSignature = serverSignature(serverKey, authMessage);
byte[] candidateServerSignature = finalScramResponse.getServerSignature().toByteArray();
if (!Arrays.equals(serverSignature, candidateServerSignature)) {
throw new Exception("Server signature does not match");
}
}
private byte[] clientProof(
InitialScramRequest initialScramRequest, InitialScramResponse initialScramResponse)
throws Exception {
saltedPassword =
saltPassword(
password,
initialScramResponse.getSalt().toByteArray(),
initialScramResponse.getIterationCount());
byte[] clientKey = clientKey(saltedPassword);
byte[] storedKey = storedKey(clientKey);
authMessage =
authMessage(
username, initialScramRequest, initialScramResponse, certificate.getSignature());
byte[] clientSignature = clientSignature(storedKey, authMessage);
return xorBytes(clientKey, clientSignature);
}
@VisibleForTesting
public static byte[] saltPassword(String password, byte[] salt, int iterationCount)
throws Exception {
if (salt.length == 0) {
throw new Exception("No salt found");
}
if (iterationCount == 0) {
throw new Exception("No iteration count found");
}
Argon2Parameters parameters =
new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id)
.withSalt(salt)
.withIterations(iterationCount)
.withMemoryAsKB(ARGON_MEMORY_LIMIT)
.withParallelism(ARGON_THREADS)
.build();
Argon2BytesGenerator argon2BytesGenerator = new Argon2BytesGenerator();
argon2BytesGenerator.init(parameters);
byte[] saltedPassword = new byte[ARGON_KEY_LENGTH];
argon2BytesGenerator.generateBytes(
password.getBytes(StandardCharsets.UTF_8), saltedPassword, 0, saltedPassword.length);
return saltedPassword;
}
@VisibleForTesting
public static byte[] nonce() {
byte[] nonce = new byte[NONCE_LENGTH];
SecureRandom random = new SecureRandom();
random.nextBytes(nonce);
return nonce;
}
private static byte[] xorBytes(byte[] a, byte[] b) {
int minLength = Math.min(a.length, b.length);
byte[] result = new byte[minLength];
for (int i = 0; i < minLength; i++) {
result[i] = (byte) (a[i] ^ b[i]);
}
return result;
}
private static byte[] clientSignature(byte[] storedKey, byte[] authMessage) throws Exception {
try {
Mac hmac = Mac.getInstance(HMAC_SHA256);
hmac.init(new SecretKeySpec(storedKey, HMAC_SHA256));
hmac.update(authMessage);
return hmac.doFinal();
} catch (java.security.GeneralSecurityException e) {
throw new Exception("Failed to generate client signature due to: " + e.getMessage(), e);
}
}
private static byte[] clientKey(byte[] saltedPassword) throws Exception {
return hmacSha256(saltedPassword, CLIENT_KEY_MESSAGE.getBytes());
}
private static byte[] serverKey(byte[] saltedPassword) throws Exception {
return hmacSha256(saltedPassword, SERVER_KEY_MESSAGE.getBytes());
}
private static byte[] hmacSha256(byte[] key, byte[] message) throws Exception {
try {
Mac hmac = Mac.getInstance(HMAC_SHA256);
hmac.init(new SecretKeySpec(key, HMAC_SHA256));
hmac.update(message);
return hmac.doFinal();
} catch (java.security.GeneralSecurityException e) {
throw new Exception("Failed to create hmac-sha256 due to: " + e.getMessage(), e);
}
}
private static byte[] serverSignature(byte[] serverKey, byte[] authMessage) throws Exception {
return hmacSha256(serverKey, authMessage);
}
private static byte[] storedKey(byte[] clientKey) throws Exception {
try {
MessageDigest digest = MessageDigest.getInstance(SHA256);
return digest.digest(clientKey);
} catch (NoSuchAlgorithmException e) {
throw new Exception("Failed to create stored key due to: " + e.getMessage(), e);
}
}
private static byte[] authMessage(
String username,
InitialScramRequest initialScramRequest,
InitialScramResponse initialScramResponse,
byte[] certificateSignature) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
outputStream.write(username.getBytes(StandardCharsets.UTF_8));
outputStream.write(',');
outputStream.write(initialScramRequest.getClientNonce().toByteArray());
outputStream.write(',');
outputStream.write(initialScramRequest.getClientNonce().toByteArray());
outputStream.write(',');
outputStream.write(initialScramResponse.getServerNonce().toByteArray());
outputStream.write(',');
outputStream.write(initialScramResponse.getSalt().toByteArray());
outputStream.write(',');
outputStream.write(
String.valueOf(initialScramResponse.getIterationCount())
.getBytes(StandardCharsets.UTF_8));
outputStream.write(',');
outputStream.write(certificateSignature);
outputStream.write(',');
outputStream.write(initialScramRequest.getClientNonce().toByteArray());
outputStream.write(',');
outputStream.write(initialScramResponse.getServerNonce().toByteArray());
} catch (IOException e) {
throw new RuntimeException("Failed to construct authMessage", e);
}
return outputStream.toByteArray();
}
}