This repository was archived by the owner on Jul 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebauthn_authenticator.ts
More file actions
344 lines (297 loc) · 15 KB
/
webauthn_authenticator.ts
File metadata and controls
344 lines (297 loc) · 15 KB
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
import {ECDSA, ICOSECompatibleKey} from "./webauthn_crypto";
import {CredentialsMap, PSKStorage, PublicKeyCredentialSource} from "./webauth_storage";
import {base64ToByteArray, byteArrayToBase64, counterToBytes} from "./utils";
import * as CBOR from 'cbor';
import {createAttestationSignature, getAttestationCertificate} from "./webauthn_attestation";
import {getLogger} from "./logging";
import {ES256_COSE, PSK_EXTENSION_IDENTIFIER} from "./constants";
import {PSK} from "./webauthn_psk";
const log = getLogger('webauthn_authenticator');
export class AssertionResponse {
public authenticatorData: Uint8Array
public signature: Uint8Array
public userHandle: Uint8Array
public credentialId: string
constructor(credId: string, authData: Uint8Array, sign: Uint8Array, userHandle: Uint8Array) {
this.authenticatorData = authData;
this.signature = sign;
this.userHandle = userHandle;
this.credentialId = credId;
}
}
export class Authenticator {
private static AAGUID: Uint8Array = new Uint8Array([
1214244733, 1205845608, 840015201, 3897052717,
4072880437, 4027233456, 675224361, 2305433287,
74291263, 3461796691, 701523034, 3178201666,
3992003567, 1410532, 4234129691, 1438515639,
]);
private static getSignatureCounter(): number {
return 0;
}
public static async authenticatorGetAssertion(userConsentCallback: Promise<boolean>,
rpId: string,
hash: Uint8Array,
requireUserPresence: boolean,
requireUserVerification: boolean,
allowCredentialDescriptorList?: PublicKeyCredentialDescriptor[],
extensions?: Map<string, string>
): Promise<AssertionResponse> {
log.debug('Called authenticatorGetAssertion');
// Step 2-7 + recovery lookup
let isRecovery: [boolean, string] = [false, ""];
let credentialOptions: PublicKeyCredentialSource[] = [];
if (allowCredentialDescriptorList) {
// Simplified credential lookup
for (let i = 0; i < allowCredentialDescriptorList.length; i++) {
const rawCredId = allowCredentialDescriptorList[i].id as ArrayBuffer;
const credId = byteArrayToBase64(new Uint8Array(rawCredId), true);
const cred = await CredentialsMap.lookup(rpId, credId);
if (cred != null) {
credentialOptions.push(cred);
}
}
} else {
// If no credentials were supplied, load all credentials associated to the RPID
credentialOptions = credentialOptions.concat(await CredentialsMap.load(rpId));
}
if (credentialOptions.length == 0) {
// Check if there is any recovery key that matches the provided credential descriptors
log.debug('No directly managed credentials found');
for (let i = 0; i < allowCredentialDescriptorList.length; i++) {
const rawCredId = allowCredentialDescriptorList[i].id as ArrayBuffer;
const credId = byteArrayToBase64(new Uint8Array(rawCredId), true);
const recExists = await PSKStorage.recoveryKeyExists(credId);
if (recExists) {
log.info('Recovery detected for', credId);
isRecovery = [true, credId];
break;
}
}
if (!isRecovery[0]) {
// No recovery and no associated credential found
throw new Error(`Container does not manage any related credentials`);
}
}
// Note: The authenticator won't let the user select a public key credential source
let credSource;
if (!isRecovery[0]) { // No recovery
credSource = credentialOptions[0];
}
const userConsent = await userConsentCallback;
if (!userConsent) {
throw new Error(`no user consent`);
}
// Step 8
let processedExtensions = undefined;
if (extensions) {
if (extensions.has(PSK_EXTENSION_IDENTIFIER)) {
log.debug('Get: PSK requested');
if (!isRecovery[0]) {
throw new Error('PSK extension requested, but no matching recovery key available');
}
const rawPskInput = base64ToByteArray(extensions.get(PSK_EXTENSION_IDENTIFIER), true);
const pskInput = await CBOR.decode(new Buffer(rawPskInput));
const [newCredId, pskOutput] = await PSK.authenticatorGetCredentialExtensionOutput(isRecovery[1], pskInput.hash, rpId);
processedExtensions = new Map([[PSK_EXTENSION_IDENTIFIER, pskOutput]]);
credSource = await CredentialsMap.lookup(rpId, newCredId);
if (credSource == null) {
// This should never happen
throw new Error('Get: New credential source missing');
}
log.debug('Get: Processed PSK');
} else if (isRecovery[0]) {
throw new Error('Recovery detected, but no PSK requested.')
}
} else if (isRecovery[0]) {
throw new Error('Recovery detected, but no PSK requested.')
}
if (processedExtensions) {
processedExtensions = new Uint8Array(CBOR.encodeCanonical(processedExtensions));
}
// Step 9: The current version does not increment the counter
// Step 10
const authenticatorData = await this.generateAuthenticatorData(rpId,
this.getSignatureCounter(), undefined, processedExtensions);
// Step 11
const concatData = new Uint8Array(authenticatorData.length + hash.length);
concatData.set(authenticatorData);
concatData.set(hash, authenticatorData.length);
const prvKey = await ECDSA.fromKey(credSource.privateKey);
const signature = await prvKey.sign(concatData);
// Step 13
return new AssertionResponse(credSource.id, authenticatorData, signature, credSource.userHandle);
}
public static async authenticatorMakeCredential(userConsentCallback: Promise<boolean>,
hash: Uint8Array,
rpEntity: PublicKeyCredentialRpEntity,
userEntity: PublicKeyCredentialUserEntity,
requireResidentKey: boolean,
requireUserPresence: boolean,
requireUserVerification: boolean,
credTypesAndPubKeyAlgs: PublicKeyCredentialParameters[],
excludeCredentialDescriptorList?: PublicKeyCredentialDescriptor[],
extensions?: Map<string, string>): Promise<[string, Uint8Array]> {
log.debug('Called authenticatorMakeCredential');
// Step 2
let algCheck = false;
for (let i = 0; i < credTypesAndPubKeyAlgs.length; i++) {
if (credTypesAndPubKeyAlgs[i].alg == ES256_COSE) {
algCheck = true;
break;
}
}
if (!algCheck) {
throw new Error(`authenticator does not support requested alg`);
}
// Step 3
if (excludeCredentialDescriptorList) { // Simplified look up
const credMapEntries = await CredentialsMap.load(rpEntity.id);
for (let i = 0; i < excludeCredentialDescriptorList.length; i++) {
const rawCredId = excludeCredentialDescriptorList[i].id as ArrayBuffer;
const credId = byteArrayToBase64(new Uint8Array(rawCredId), true);
if (credMapEntries.findIndex(x =>
(x.id == credId) && (x.type === excludeCredentialDescriptorList[i].type)) >= 0) {
await userConsentCallback;
throw new Error(`authenticator manages credential of excludeCredentialDescriptorList`);
}
}
}
// Step 4 Not needed, because cKey supports resident keys
// Step 5
if (requireUserVerification) {
throw new Error(`authenticator does not support user verification`);
}
// Step 6
const userConsent = await userConsentCallback;
if (!userConsent) {
throw new Error(`no user consent`);
}
userEntity.id
return await this.finishAuthenticatorMakeCredential(rpEntity.id, hash, undefined, extensions, userEntity.id);
}
public static async finishAuthenticatorMakeCredential(rpId: string, hash: Uint8Array, keyPair?: ICOSECompatibleKey, extensions?: Map<string, string>, userHandle?: BufferSource): Promise<[string, Uint8Array]> {
// Step 7
if (!(keyPair)) {
log.debug('No key pair provided, create new one.');
keyPair = await ECDSA.createECDSAKeyPair();
}
let credentialId = this.createCredentialId();
let credentialSource = new PublicKeyCredentialSource(credentialId, keyPair.privateKey, rpId, (<Uint8Array>userHandle));
await CredentialsMap.put(rpId, credentialSource);
// Step 9
let processedExtensions = undefined;
if (extensions) {
log.debug(extensions);
if (extensions.has(PSK_EXTENSION_IDENTIFIER)) {
log.debug('Make: PSK requested');
if (extensions.get(PSK_EXTENSION_IDENTIFIER) !== "9g") { // null in CBOR
log.warn('Make: PSK extension received unexpected input. Skip extension processing.', extensions[PSK_EXTENSION_IDENTIFIER]);
} else {
const [backupKeyCredentialId, pskOutPut] = await PSK.authenticatorMakeCredentialExtensionOutput();
processedExtensions = new Map([[PSK_EXTENSION_IDENTIFIER, pskOutPut]]);
credentialId = backupKeyCredentialId;
credentialSource.id = credentialId;
await CredentialsMap.put(rpId, credentialSource);
log.debug('Make: Processed PSK');
}
}
}
if (processedExtensions) {
processedExtensions = new Uint8Array(CBOR.encodeCanonical(processedExtensions));
}
// Step 10
const sigCnt = this.getSignatureCounter();
// Step 11
const rawCredentialId = base64ToByteArray(credentialId, true);
const attestedCredentialData = await this.generateAttestedCredentialData(rawCredentialId, keyPair);
// Step 12
const authenticatorData = await this.generateAuthenticatorData(rpId, sigCnt, attestedCredentialData, processedExtensions);
// Step 13
const attObj = await this.generateAttestationObject(hash, authenticatorData);
// Return value is not 1:1 WebAuthn conform
log.debug('Created credential', credentialId)
return [credentialId, attObj];
}
private static async generateAttestedCredentialData(credentialId: Uint8Array, publicKey: ICOSECompatibleKey): Promise<Uint8Array> {
const aaguid = this.AAGUID.slice(0, 16);
const credIdLen = new Uint8Array(2);
credIdLen[0] = (credentialId.length >> 8) & 0xff;
credIdLen[1] = credentialId.length & 0xff;
const coseKey = await publicKey.toCOSE(publicKey.publicKey);
const encodedKey = new Uint8Array(CBOR.encodeCanonical(coseKey));
const attestedCredentialDataLength = aaguid.length + credIdLen.length + credentialId.length + encodedKey.length;
const attestedCredentialData = new Uint8Array(attestedCredentialDataLength);
let offset = 0;
attestedCredentialData.set(aaguid, offset);
offset += aaguid.length;
attestedCredentialData.set(credIdLen, offset);
offset += credIdLen.length;
attestedCredentialData.set(credentialId, offset);
offset += credentialId.length;
attestedCredentialData.set(encodedKey, offset);
return attestedCredentialData;
}
private static async generateAuthenticatorData(rpID: string, counter: number, attestedCredentialData?: Uint8Array,
extensionData?: Uint8Array): Promise<Uint8Array> {
const rpIdDigest = await window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(rpID));
const rpIdHash = new Uint8Array(rpIdDigest);
let authenticatorDataLength = rpIdHash.length + 1 + 4;
if (attestedCredentialData) {
authenticatorDataLength += attestedCredentialData.byteLength;
}
if (extensionData) {
authenticatorDataLength += extensionData.byteLength;
}
const authenticatorData = new Uint8Array(authenticatorDataLength);
let offset = 0;
// 32 bytes for the RP ID hash
authenticatorData.set(rpIdHash, offset);
offset += rpIdHash.length;
// 1 byte for flags
authenticatorData[rpIdHash.length] = 1; // UP
if (attestedCredentialData) {
authenticatorData[rpIdHash.length] |= (1 << 6); // AT
}
if (extensionData) {
authenticatorData[rpIdHash.length] |= (1 << 7); // ED
}
offset++;
// 4 bytes for the counter. big-endian uint32
// https://www.w3.org/TR/webauthn/#signature-counter
authenticatorData.set(counterToBytes(counter), offset);
offset += counterToBytes(counter).length;
if (attestedCredentialData) {
authenticatorData.set(attestedCredentialData, offset);
offset += attestedCredentialData.byteLength;
}
if (extensionData) {
authenticatorData.set(extensionData, offset);
}
return authenticatorData;
}
private static async generateAttestationObject(hash: Uint8Array, authenticatorData: Uint8Array): Promise<Uint8Array> {
const attCert = getAttestationCertificate();
const attSignature = await createAttestationSignature(hash, authenticatorData);
const attObjJSON = {
authData: authenticatorData,
fmt: 'packed',
attStmt: {
alg: ES256_COSE,
sig: attSignature,
x5c: [attCert]
}
}
return CBOR.encodeCanonical(attObjJSON);
}
private static createCredentialId(): string{
let enc = new TextEncoder();
let dt = new Date().getTime();
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = (dt + Math.random()*16)%16 | 0;
dt = Math.floor(dt/16);
return (c=='x' ? r :(r&0x3|0x8)).toString(16);
});
return byteArrayToBase64(enc.encode(uuid), true);
}
}