-
Notifications
You must be signed in to change notification settings - Fork 97
/
scram.ts
313 lines (270 loc) · 7.72 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
import { base64 } from "../deps.ts";
/** Number of random bytes used to generate a nonce */
const defaultNonceSize = 16;
const text_encoder = new TextEncoder();
enum AuthenticationState {
Init,
ClientChallenge,
ServerChallenge,
ClientResponse,
ServerResponse,
Failed,
}
/**
* Collection of SCRAM authentication keys derived from a plaintext password
* in HMAC-derived binary format
*/
interface KeySignatures {
client: Uint8Array;
server: Uint8Array;
stored: Uint8Array;
}
/**
* Reason of authentication failure
*/
export enum Reason {
BadMessage = "server sent an ill-formed message",
BadServerNonce = "server sent an invalid nonce",
BadSalt = "server specified an invalid salt",
BadIterationCount = "server specified an invalid iteration count",
BadVerifier = "server sent a bad verifier",
Rejected = "rejected by server",
}
function assert(cond: unknown): asserts cond {
if (!cond) {
throw new Error("Scram protocol assertion failed");
}
}
// TODO
// Handle mapping and maybe unicode normalization.
// Add tests for invalid string values
/**
* Normalizes string per SASLprep.
* @see {@link https://tools.ietf.org/html/rfc3454}
* @see {@link https://tools.ietf.org/html/rfc4013}
*/
function assertValidScramString(str: string) {
const unsafe = /[^\x21-\x7e]/;
if (unsafe.test(str)) {
throw new Error(
"scram username/password is currently limited to safe ascii characters",
);
}
}
async function computeScramSignature(
message: string,
raw_key: Uint8Array,
): Promise<Uint8Array> {
const key = await crypto.subtle.importKey(
"raw",
raw_key,
{ name: "HMAC", hash: "SHA-256" },
false,
["sign"],
);
return new Uint8Array(
await crypto.subtle.sign(
{ name: "HMAC", hash: "SHA-256" },
key,
text_encoder.encode(message),
),
);
}
function computeScramProof(signature: Uint8Array, key: Uint8Array): Uint8Array {
const digest = new Uint8Array(signature.length);
for (let i = 0; i < digest.length; i++) {
digest[i] = signature[i] ^ key[i];
}
return digest;
}
/**
* Derives authentication key signatures from a plaintext password
*/
async function deriveKeySignatures(
password: string,
salt: Uint8Array,
iterations: number,
): Promise<KeySignatures> {
const pbkdf2_password = await crypto.subtle.importKey(
"raw",
text_encoder.encode(password),
"PBKDF2",
false,
["deriveBits", "deriveKey"],
);
const key = await crypto.subtle.deriveKey(
{
hash: "SHA-256",
iterations,
name: "PBKDF2",
salt,
},
pbkdf2_password,
{ name: "HMAC", hash: "SHA-256", length: 256 },
false,
["sign"],
);
const client = new Uint8Array(
await crypto.subtle.sign("HMAC", key, text_encoder.encode("Client Key")),
);
const server = new Uint8Array(
await crypto.subtle.sign("HMAC", key, text_encoder.encode("Server Key")),
);
const stored = new Uint8Array(await crypto.subtle.digest("SHA-256", client));
return { client, server, stored };
}
/** Escapes "=" and "," in a string. */
function escape(str: string): string {
return str.replace(/=/g, "=3D").replace(/,/g, "=2C");
}
function generateRandomNonce(size: number): string {
return base64.encodeBase64(crypto.getRandomValues(new Uint8Array(size)));
}
function parseScramAttributes(message: string): Record<string, string> {
const attrs: Record<string, string> = {};
for (const entry of message.split(",")) {
const pos = entry.indexOf("=");
if (pos < 1) {
throw new Error(Reason.BadMessage);
}
// TODO
// Replace with String.prototype.substring
const key = entry.substr(0, pos);
const value = entry.substr(pos + 1);
attrs[key] = value;
}
return attrs;
}
/**
* Client composes and verifies SCRAM authentication messages, keeping track
* of authentication #state and parameters.
* @see {@link https://tools.ietf.org/html/rfc5802}
*/
export class Client {
#auth_message: string;
#client_nonce: string;
#key_signatures?: KeySignatures;
#password: string;
#server_nonce?: string;
#state: AuthenticationState;
#username: string;
constructor(username: string, password: string, nonce?: string) {
assertValidScramString(password);
assertValidScramString(username);
this.#auth_message = "";
this.#client_nonce = nonce ?? generateRandomNonce(defaultNonceSize);
this.#password = password;
this.#state = AuthenticationState.Init;
this.#username = escape(username);
}
/**
* Composes client-first-message
*/
composeChallenge(): string {
assert(this.#state === AuthenticationState.Init);
try {
// "n" for no channel binding, then an empty authzid option follows.
const header = "n,,";
const challenge = `n=${this.#username},r=${this.#client_nonce}`;
const message = header + challenge;
this.#auth_message += challenge;
this.#state = AuthenticationState.ClientChallenge;
return message;
} catch (e) {
this.#state = AuthenticationState.Failed;
throw e;
}
}
/**
* Processes server-first-message
*/
async receiveChallenge(challenge: string) {
assert(this.#state === AuthenticationState.ClientChallenge);
try {
const attrs = parseScramAttributes(challenge);
const nonce = attrs.r;
if (!attrs.r || !attrs.r.startsWith(this.#client_nonce)) {
throw new Error(Reason.BadServerNonce);
}
this.#server_nonce = nonce;
let salt: Uint8Array | undefined;
if (!attrs.s) {
throw new Error(Reason.BadSalt);
}
try {
salt = base64.decodeBase64(attrs.s);
} catch {
throw new Error(Reason.BadSalt);
}
if (!salt) throw new Error(Reason.BadSalt);
const iterCount = parseInt(attrs.i) | 0;
if (iterCount <= 0) {
throw new Error(Reason.BadIterationCount);
}
this.#key_signatures = await deriveKeySignatures(
this.#password,
salt,
iterCount,
);
this.#auth_message += "," + challenge;
this.#state = AuthenticationState.ServerChallenge;
} catch (e) {
this.#state = AuthenticationState.Failed;
throw e;
}
}
/**
* Composes client-final-message
*/
async composeResponse(): Promise<string> {
assert(this.#state === AuthenticationState.ServerChallenge);
assert(this.#key_signatures);
assert(this.#server_nonce);
try {
// "biws" is the base-64 encoded form of the gs2-header "n,,".
const responseWithoutProof = `c=biws,r=${this.#server_nonce}`;
this.#auth_message += "," + responseWithoutProof;
const proof = base64.encodeBase64(
computeScramProof(
await computeScramSignature(
this.#auth_message,
this.#key_signatures.stored,
),
this.#key_signatures.client,
),
);
const message = `${responseWithoutProof},p=${proof}`;
this.#state = AuthenticationState.ClientResponse;
return message;
} catch (e) {
this.#state = AuthenticationState.Failed;
throw e;
}
}
/**
* Processes server-final-message
*/
async receiveResponse(response: string) {
assert(this.#state === AuthenticationState.ClientResponse);
assert(this.#key_signatures);
try {
const attrs = parseScramAttributes(response);
if (attrs.e) {
throw new Error(attrs.e ?? Reason.Rejected);
}
const verifier = base64.encodeBase64(
await computeScramSignature(
this.#auth_message,
this.#key_signatures.server,
),
);
if (attrs.v !== verifier) {
throw new Error(Reason.BadVerifier);
}
this.#state = AuthenticationState.ServerResponse;
} catch (e) {
this.#state = AuthenticationState.Failed;
throw e;
}
}
}