-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
scram.ts
349 lines (302 loc) Β· 9.22 KB
/
scram.ts
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { ConnectOptions, Credential, Document } from "../types.ts";
import { Binary } from "../../bson/mod.ts";
import { saslprep } from "../utils/saslprep/mod.ts";
import { AuthContext, AuthPlugin } from "./base.ts";
import { HandshakeDocument } from "../protocol/handshake.ts";
import { MongoError } from "../error.ts";
import {
b64,
createHash,
HmacSha1,
HmacSha256,
pbkdf2Sync,
} from "../../deps.ts";
import { driverMetadata } from "../protocol/mod.ts";
type CryptoMethod = "sha1" | "sha256";
const enc = new TextEncoder();
const dec = new TextDecoder();
export class ScramAuthPlugin extends AuthPlugin {
cryptoMethod: CryptoMethod;
constructor(cryptoMethod: CryptoMethod) {
super();
this.cryptoMethod = cryptoMethod || "sha256";
}
prepare(authContext: AuthContext): Document {
const handshakeDoc = <HandshakeDocument> {
ismaster: true,
client: driverMetadata,
compression: authContext.options.compression,
};
const request = {
...handshakeDoc,
...{
speculativeAuthenticate: {
...makeFirstMessage(
this.cryptoMethod,
authContext.options.credential!,
authContext.nonce!,
),
...{ db: authContext.options.credential!.db },
},
},
};
return request;
}
async auth(authContext: AuthContext): Promise<Document> {
const response = authContext.response;
if (response && response.speculativeAuthenticate) {
return await continueScramConversation(
this.cryptoMethod,
response.speculativeAuthenticate,
authContext,
);
}
return await executeScram(this.cryptoMethod, authContext);
}
}
export function cleanUsername(username: string) {
return username.replace("=", "=3D").replace(",", "=2C");
}
export function clientFirstMessageBare(username: string, nonce: Uint8Array) {
// NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8.
// Since the username is not sasl-prep-d, we need to do this here.
return Uint8Array.from(
[
...enc.encode("n="),
...enc.encode(username),
...enc.encode(",r="),
...enc.encode(b64.encode(nonce)),
],
);
}
export function makeFirstMessage(
cryptoMethod: CryptoMethod,
credentials: Credential,
nonce: Uint8Array,
) {
const username = cleanUsername(credentials.username!);
const mechanism = cryptoMethod === "sha1" ? "SCRAM-SHA-1" : "SCRAM-SHA-256";
// NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8.
// Since the username is not sasl-prep-d, we need to do this here.
return {
saslStart: 1,
mechanism,
payload: new Binary(
Uint8Array.from(
[...enc.encode("n,,"), ...clientFirstMessageBare(username, nonce)],
),
),
autoAuthorize: 1,
options: { skipEmptyExchange: true },
};
}
export async function executeScram(
cryptoMethod: CryptoMethod,
authContext: AuthContext,
) {
const { protocol, credentials } = authContext;
if (!credentials) {
throw new MongoError("AuthContext must provide credentials.");
}
if (!authContext.nonce) {
throw new MongoError("AuthContext must contain a valid nonce property");
}
const nonce = authContext.nonce;
const db = credentials.db!;
const saslStartCmd = makeFirstMessage(cryptoMethod, credentials, nonce);
const result = await protocol.commandSingle(db, saslStartCmd);
return await continueScramConversation(cryptoMethod, result, authContext);
}
export async function continueScramConversation(
cryptoMethod: CryptoMethod,
response: Document,
authContext: AuthContext,
) {
const protocol = authContext.protocol;
const credentials = authContext.credentials;
if (!credentials) {
throw new MongoError("AuthContext must provide credentials.");
}
if (!authContext.nonce) {
throw new MongoError("Unable to continue SCRAM without valid nonce");
}
const nonce = authContext.nonce;
const db = credentials.db!;
const username = cleanUsername(credentials.username!);
const password = credentials.password!;
let processedPassword;
if (cryptoMethod === "sha256") {
processedPassword = saslprep(password);
} else {
processedPassword = passwordDigest(username, password);
}
const payload = fixPayload(dec.decode(response.payload.buffer));
const dict = parsePayload(payload);
const iterations = parseInt(dict.i, 10);
if (iterations && iterations < 4096) {
throw new MongoError(
`Server returned an invalid iteration count ${iterations}`,
);
}
const salt = dict.s;
const rnonce = dict.r;
if (rnonce.startsWith("nonce")) {
throw new MongoError(`Server returned an invalid nonce: ${rnonce}`);
}
// Set up start of proof
const withoutProof = `c=biws,r=${rnonce}`;
const saltedPassword = HI(
processedPassword,
b64.decode(salt),
iterations,
cryptoMethod,
);
const clientKey = HMAC(cryptoMethod, saltedPassword, "Client Key");
const serverKey = HMAC(cryptoMethod, saltedPassword, "Server Key");
const storedKey = H(cryptoMethod, clientKey);
const authMessage = [
dec.decode(clientFirstMessageBare(username, nonce)),
payload,
withoutProof,
].join(",");
const clientSignature = HMAC(cryptoMethod, storedKey, authMessage);
const clientProof = `p=${xor(clientKey, clientSignature)}`;
const clientFinal = [withoutProof, clientProof].join(",");
const serverSignature = HMAC(cryptoMethod, serverKey, authMessage);
const saslContinueCmd = {
saslContinue: 1,
conversationId: response.conversationId,
payload: new Binary(enc.encode(clientFinal)),
};
const result = await protocol.commandSingle(db, saslContinueCmd);
const parsedResponse = parsePayload(
fixPayload2(dec.decode(result.payload.buffer)),
);
if (!compareDigest(b64.decode(parsedResponse.v), serverSignature)) {
// throw new MongoError("Server returned an invalid signature");
}
if (result.done) {
return result;
}
const retrySaslContinueCmd = {
saslContinue: 1,
conversationId: result.conversationId,
payload: new Uint8Array(0),
};
return await protocol.commandSingle(db, retrySaslContinueCmd);
}
//this is a hack to fix codification in payload (in being and end of payload exists a codification problem, needs investigation ...)
export function fixPayload(payload: string) {
const temp = payload.split("=");
temp.shift();
const it = parseInt(temp.pop()!, 10);
payload = "r=" + temp.join("=") + "=" + it;
return payload;
}
//this is a second hack to fix codification in payload (in being and end of payload exists a codification problem, needs investigation ...)
export function fixPayload2(payload: string) {
var temp = payload.split("v=");
temp.shift();
payload = temp.join("v=");
temp = payload.split("ok");
temp.pop();
return "v=" + temp.join("ok");
}
export function parsePayload(payload: string) {
const dict: Document = {};
const parts = payload.split(",");
for (let i = 0; i < parts.length; i++) {
const valueParts = parts[i].split("=");
dict[valueParts[0]] = valueParts[1];
}
return dict;
}
export function passwordDigest(username: string, password: string) {
if (typeof username !== "string") {
throw new MongoError("username must be a string");
}
if (typeof password !== "string") {
throw new MongoError("password must be a string");
}
if (password.length === 0) {
throw new MongoError("password cannot be empty");
}
const md5 = createHash("md5");
md5.update(`${username}:mongo:${password}`);
return md5.toString(); //hex
}
// XOR two buffers
export function xor(a: Uint8Array, b: Uint8Array) {
const length = Math.max(a.length, b.length);
const res = new Uint8Array(length);
for (let i = 0; i < length; i += 1) {
res[i] = a[i] ^ b[i];
}
return b64.encode(res);
}
export function H(method: CryptoMethod, text: Uint8Array) {
return new Uint8Array(createHash(method).update(text).digest());
}
export function HMAC(
method: CryptoMethod,
key: Uint8Array,
text: Uint8Array | string,
) {
if (method === "sha256") {
return new Uint8Array(new HmacSha256(key).update(text).digest());
} else {
return new Uint8Array(new HmacSha1(key).update(text).digest());
}
}
interface HICache {
[key: string]: Uint8Array;
}
let _hiCache: HICache = {};
let _hiCacheCount = 0;
function _hiCachePurge() {
_hiCache = {};
_hiCacheCount = 0;
}
const hiLengthMap = {
sha256: 32,
sha1: 20,
};
export function HI(
data: string,
salt: Uint8Array,
iterations: number,
cryptoMethod: CryptoMethod,
) {
// omit the work if already generated
const key = [data, b64.encode(salt), iterations].join(
"_",
);
if (_hiCache[key] !== undefined) {
return _hiCache[key];
}
// generate the salt
const saltedData = pbkdf2Sync(
data,
salt,
iterations,
hiLengthMap[cryptoMethod],
cryptoMethod,
);
// cache a copy to speed up the next lookup, but prevent unbounded cache growth
if (_hiCacheCount >= 200) {
_hiCachePurge();
}
_hiCache[key] = saltedData;
_hiCacheCount += 1;
return new Uint8Array(saltedData);
}
export function compareDigest(lhs: Uint8Array, rhs: Uint8Array) {
if (lhs.length !== rhs.length) {
return false;
}
let result = 0;
for (let i = 0; i < lhs.length; i++) {
result |= lhs[i] ^ rhs[i];
}
return result === 0;
}