@intellectual-frontiers/crypto 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/cms.d.ts +106 -0
- package/dist/cms.js +350 -0
- package/dist/cose.d.ts +34 -0
- package/dist/cose.js +204 -0
- package/dist/crypt.d.ts +30 -0
- package/dist/crypt.js +117 -0
- package/dist/dsse.d.ts +137 -0
- package/dist/dsse.js +243 -0
- package/dist/md5.d.ts +19 -0
- package/dist/md5.js +146 -0
- package/dist/mod.d.ts +25 -0
- package/dist/mod.js +25 -0
- package/dist/sha256.d.ts +25 -0
- package/dist/sha256.js +34 -0
- package/dist/sigv4.d.ts +67 -0
- package/dist/sigv4.js +169 -0
- package/dist/uuid.d.ts +53 -0
- package/dist/uuid.js +92 -0
- package/dist/webauthn.d.ts +212 -0
- package/dist/webauthn.js +381 -0
- package/package.json +84 -4
- package/src/cms.ts +387 -0
- package/src/cose.ts +199 -0
- package/src/crypt.ts +117 -0
- package/src/dsse.ts +339 -0
- package/src/md5.ts +150 -0
- package/src/mod.ts +26 -0
- package/src/sha256.ts +55 -0
- package/src/sigv4.ts +216 -0
- package/src/uuid.ts +111 -0
- package/src/webauthn.ts +548 -0
package/dist/webauthn.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
// Spec: 0001-crypto FR-008
|
|
2
|
+
/**
|
|
3
|
+
* WebAuthn (Web Authentication Level 3) for a relying party, pure and with no outside package (commons 0014-webauthn): the options a
|
|
4
|
+
* browser's `navigator.credentials.create()` and `.get()` take, and the verification of what they answer, in the JSON shapes of the
|
|
5
|
+
* Level 3 `toJSON()` methods (every binary field as base64url). Registration checks the client data (`webauthn.create`, the challenge,
|
|
6
|
+
* the origin), the attestation object (CBOR: `fmt` `none`, or `packed` with no more than self-attestation's trust), the authenticator
|
|
7
|
+
* data (the SHA-256 of the RP ID, user presence and user verification, the attested credential) and the credential's COSE key (ES256 or
|
|
8
|
+
* EdDSA). Authentication checks the client data (`webauthn.get`), the authenticator data, the signature over the authenticator data and
|
|
9
|
+
* the SHA-256 of the client data by the stored key, and that the signature counter went up. Every refusal is a plain sentence, never an
|
|
10
|
+
* exception: what a browser sends is input from outside.
|
|
11
|
+
*
|
|
12
|
+
* The kernel has no randomness and no store: the caller makes each challenge (16 or more random bytes, base64url), keeps it until the
|
|
13
|
+
* answer comes, and keeps the credential (`credentialId`, `publicKey`, `alg`, `signCount`) that registration gives back.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { registrationOptions, verifyRegistration } from "./webauthn.ts";
|
|
18
|
+
* const options = registrationOptions({
|
|
19
|
+
* rpId: "example.com",
|
|
20
|
+
* rpName: "Example",
|
|
21
|
+
* user: { id: "dXNlci0x", name: "ada", displayName: "Ada" },
|
|
22
|
+
* challenge: "q83vEjRWeJq83vEjRWeJqw",
|
|
23
|
+
* });
|
|
24
|
+
* // send `options` to the browser; then, with what it answers:
|
|
25
|
+
* // const r = await verifyRegistration({ response, expectedChallenge: options.challenge, expectedOrigin: "https://example.com", rpId: "example.com" });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @limits
|
|
29
|
+
* Attestation is not judged: commons asks for `none`, accepts `none`, and accepts `packed` with self-attestation (its signature checked) or
|
|
30
|
+
* with a certificate chain that is not checked, so that every credential has the trust of `none`; any other format is refused. Only ES256
|
|
31
|
+
* and EdDSA keys (see `cose.ts`). Extensions are read past, not interpreted; `topOrigin` is not checked, and a `crossOrigin` ceremony is
|
|
32
|
+
* refused.
|
|
33
|
+
*
|
|
34
|
+
* @module
|
|
35
|
+
*/
|
|
36
|
+
import { fromBase64Url, toBase64Url } from "@intellectual-frontiers/formats/base64";
|
|
37
|
+
import { decodeCbor, decodeCborItem, formatCborError, isMap } from "@intellectual-frontiers/formats/cbor";
|
|
38
|
+
import { ALGORITHMS, EDDSA, ES256, parseCoseKey, verifyCose } from "./cose.js";
|
|
39
|
+
import { array, boolean, formatDecodeError, integer, json, literal, nullable, object, record, string, unknownValue, err, ok, } from "@intellectual-frontiers/core";
|
|
40
|
+
/** The default time a ceremony may take: five minutes, within what Level 3 recommends where user verification is required. */
|
|
41
|
+
export const DEFAULT_TIMEOUT_MS = 300_000;
|
|
42
|
+
function bytesOfB64u(text, what) {
|
|
43
|
+
if (typeof text !== "string")
|
|
44
|
+
return err(`${what} is not a string`);
|
|
45
|
+
const r = fromBase64Url(text);
|
|
46
|
+
return r.kind === "ok" ? r : err(`${what} is not base64url`);
|
|
47
|
+
}
|
|
48
|
+
function checkedChallenge(challenge) {
|
|
49
|
+
const r = fromBase64Url(challenge);
|
|
50
|
+
if (r.kind === "err" || r.value.length < 16)
|
|
51
|
+
throw new RangeError("WebAuthn: a challenge is at least 16 random bytes, as base64url");
|
|
52
|
+
}
|
|
53
|
+
function descriptors(ids) {
|
|
54
|
+
return ids.map((id) => {
|
|
55
|
+
if (fromBase64Url(id).kind === "err")
|
|
56
|
+
throw new RangeError(`WebAuthn: the credential id ${id} is not base64url`);
|
|
57
|
+
return { type: "public-key", id };
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The options for registering a credential: ES256 and EdDSA offered, a discoverable credential preferred, user verification required, no
|
|
62
|
+
* attestation asked for, and the credentials the user already has excluded. A challenge shorter than 16 bytes or a user id longer than 64
|
|
63
|
+
* is a programming error and throws a `RangeError`.
|
|
64
|
+
*/
|
|
65
|
+
export function registrationOptions(o) {
|
|
66
|
+
checkedChallenge(o.challenge);
|
|
67
|
+
const uid = fromBase64Url(o.user.id);
|
|
68
|
+
if (uid.kind === "err" || uid.value.length === 0 || uid.value.length > 64)
|
|
69
|
+
throw new RangeError("WebAuthn: a user id is 1 to 64 bytes, as base64url");
|
|
70
|
+
return {
|
|
71
|
+
rp: { id: o.rpId, name: o.rpName },
|
|
72
|
+
user: { id: o.user.id, name: o.user.name, displayName: o.user.displayName },
|
|
73
|
+
challenge: o.challenge,
|
|
74
|
+
pubKeyCredParams: ALGORITHMS.map((alg) => ({ type: "public-key", alg })),
|
|
75
|
+
timeout: o.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
76
|
+
excludeCredentials: descriptors(o.excludeCredentials ?? []),
|
|
77
|
+
authenticatorSelection: { residentKey: "preferred", requireResidentKey: false, userVerification: "required" },
|
|
78
|
+
attestation: "none",
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** The options for authenticating: user verification required, and the credentials allowed where the user is known (none for a passkey's discoverable sign-in). */
|
|
82
|
+
export function authenticationOptions(o) {
|
|
83
|
+
checkedChallenge(o.challenge);
|
|
84
|
+
const base = { challenge: o.challenge, timeout: o.timeoutMs ?? DEFAULT_TIMEOUT_MS, rpId: o.rpId, userVerification: "required" };
|
|
85
|
+
return o.allowCredentials === undefined ? base : { ...base, allowCredentials: descriptors(o.allowCredentials) };
|
|
86
|
+
}
|
|
87
|
+
/** The flags of authenticator data (WebAuthn §6.1). */
|
|
88
|
+
export const FLAGS = { up: 0x01, uv: 0x04, be: 0x08, bs: 0x10, at: 0x40, ed: 0x80 };
|
|
89
|
+
/** Authenticator data read: the RP ID hash, the flags, the counter, the attested credential (its COSE key's bytes as given) and extensions; every byte accounted for. */
|
|
90
|
+
export function parseAuthenticatorData(bytes) {
|
|
91
|
+
if (bytes.length < 37)
|
|
92
|
+
return err(`the authenticator data is ${bytes.length} bytes, fewer than 37`);
|
|
93
|
+
const flags = bytes[32] ?? 0;
|
|
94
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
95
|
+
const signCount = view.getUint32(33);
|
|
96
|
+
let at = 37;
|
|
97
|
+
let attested;
|
|
98
|
+
if (flags & FLAGS.at) {
|
|
99
|
+
if (bytes.length < at + 18)
|
|
100
|
+
return err("the authenticator data ends inside its attested credential data");
|
|
101
|
+
const aaguid = bytes.slice(at, at + 16);
|
|
102
|
+
const idLength = view.getUint16(at + 16);
|
|
103
|
+
at += 18;
|
|
104
|
+
if (idLength > 1023)
|
|
105
|
+
return err(`a credential id of ${idLength} bytes (at most 1023)`);
|
|
106
|
+
if (bytes.length < at + idLength)
|
|
107
|
+
return err("the authenticator data ends inside the credential id");
|
|
108
|
+
const credentialId = bytes.slice(at, at + idLength);
|
|
109
|
+
at += idLength;
|
|
110
|
+
const key = decodeCborItem(bytes, at);
|
|
111
|
+
if (key.kind === "err")
|
|
112
|
+
return err(`the credential public key is not CBOR (${formatCborError(key.error)})`);
|
|
113
|
+
const publicKey = bytes.slice(at, key.value.end);
|
|
114
|
+
at = key.value.end;
|
|
115
|
+
attested = { aaguid, credentialId, publicKey };
|
|
116
|
+
}
|
|
117
|
+
let extensions;
|
|
118
|
+
if (flags & FLAGS.ed) {
|
|
119
|
+
const ext = decodeCborItem(bytes, at);
|
|
120
|
+
if (ext.kind === "err")
|
|
121
|
+
return err(`the extensions are not CBOR (${formatCborError(ext.error)})`);
|
|
122
|
+
if (!isMap(ext.value.value))
|
|
123
|
+
return err("the extensions are not a CBOR map");
|
|
124
|
+
extensions = ext.value.value;
|
|
125
|
+
at = ext.value.end;
|
|
126
|
+
}
|
|
127
|
+
if (at !== bytes.length)
|
|
128
|
+
return err(`${bytes.length - at} bytes after the authenticator data that its flags do not account for`);
|
|
129
|
+
return ok({
|
|
130
|
+
rpIdHash: bytes.slice(0, 32),
|
|
131
|
+
flags,
|
|
132
|
+
userPresent: (flags & FLAGS.up) !== 0,
|
|
133
|
+
userVerified: (flags & FLAGS.uv) !== 0,
|
|
134
|
+
backupEligible: (flags & FLAGS.be) !== 0,
|
|
135
|
+
backedUp: (flags & FLAGS.bs) !== 0,
|
|
136
|
+
signCount,
|
|
137
|
+
...(attested === undefined ? {} : { attested }),
|
|
138
|
+
...(extensions === undefined ? {} : { extensions }),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
const clientData = object({ type: string, challenge: string, origin: string }, { crossOrigin: boolean, topOrigin: string });
|
|
142
|
+
/** Client data JSON (its bytes, UTF-8) read. */
|
|
143
|
+
export function parseClientData(bytes) {
|
|
144
|
+
let text;
|
|
145
|
+
try {
|
|
146
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return err("the client data is not UTF-8");
|
|
150
|
+
}
|
|
151
|
+
const r = json(text, clientData);
|
|
152
|
+
return r.kind === "ok" ? r : err(`the client data is not WebAuthn's (${formatDecodeError(r.error)})`);
|
|
153
|
+
}
|
|
154
|
+
function sameBytes(a, b) {
|
|
155
|
+
if (a.length !== b.length)
|
|
156
|
+
return false;
|
|
157
|
+
let d = 0;
|
|
158
|
+
for (let i = 0; i < a.length; i++)
|
|
159
|
+
d |= (a[i] ?? 0) ^ (b[i] ?? 0);
|
|
160
|
+
return d === 0;
|
|
161
|
+
}
|
|
162
|
+
async function digest(b) {
|
|
163
|
+
return new Uint8Array(await crypto.subtle.digest("SHA-256", new Uint8Array(b)));
|
|
164
|
+
}
|
|
165
|
+
function concatBytes(a, b) {
|
|
166
|
+
const out = new Uint8Array(a.length + b.length);
|
|
167
|
+
out.set(a, 0);
|
|
168
|
+
out.set(b, a.length);
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
/** The client data checked for its type, challenge, origin and cross-origin flag; its bytes, to hash. */
|
|
172
|
+
function checkClientData(type, c) {
|
|
173
|
+
const raw = bytesOfB64u(c.clientDataJSON, "clientDataJSON");
|
|
174
|
+
if (raw.kind === "err")
|
|
175
|
+
return raw;
|
|
176
|
+
const cd = parseClientData(raw.value);
|
|
177
|
+
if (cd.kind === "err")
|
|
178
|
+
return cd;
|
|
179
|
+
if (cd.value.type !== type)
|
|
180
|
+
return err(`the client data's type is ${cd.value.type}, not ${type}`);
|
|
181
|
+
const got = fromBase64Url(cd.value.challenge);
|
|
182
|
+
const want = fromBase64Url(c.expectedChallenge);
|
|
183
|
+
if (got.kind === "err" || want.kind === "err" || !sameBytes(got.value, want.value))
|
|
184
|
+
return err("the challenge is not the one this ceremony issued");
|
|
185
|
+
const origins = typeof c.expectedOrigin === "string" ? [c.expectedOrigin] : c.expectedOrigin;
|
|
186
|
+
if (!origins.includes(cd.value.origin))
|
|
187
|
+
return err(`the origin ${cd.value.origin} is not one expected`);
|
|
188
|
+
if (cd.value.crossOrigin === true)
|
|
189
|
+
return err("the ceremony ran in a cross-origin frame");
|
|
190
|
+
return ok(raw.value);
|
|
191
|
+
}
|
|
192
|
+
/** The authenticator data checked for the RP ID's hash, user presence and user verification, and the backup flags' consistency. */
|
|
193
|
+
async function checkAuthenticatorData(data, rpId) {
|
|
194
|
+
if (!sameBytes(data.rpIdHash, await digest(new TextEncoder().encode(rpId))))
|
|
195
|
+
return err(`the authenticator data is not for the RP ID ${rpId}`);
|
|
196
|
+
if (!data.userPresent)
|
|
197
|
+
return err("the user was not present (the UP flag is not set)");
|
|
198
|
+
if (!data.userVerified)
|
|
199
|
+
return err("the user was not verified (the UV flag is not set)");
|
|
200
|
+
if (data.backedUp && !data.backupEligible)
|
|
201
|
+
return err("the credential says it is backed up but not eligible to be (BS without BE)");
|
|
202
|
+
return ok(undefined);
|
|
203
|
+
}
|
|
204
|
+
function uuidOf(b) {
|
|
205
|
+
const h = [...b].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
206
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
207
|
+
}
|
|
208
|
+
function algOfCose(key) {
|
|
209
|
+
return key.kty === "EC2" ? ES256 : EDDSA;
|
|
210
|
+
}
|
|
211
|
+
/** The attestation statement checked: `none` empty; `packed` with self-attestation verified by the credential's own key, or with `x5c` taken at the trust of `none`. */
|
|
212
|
+
async function checkAttestation(fmt, stmt, key, authData, clientDataHash) {
|
|
213
|
+
if (!isMap(stmt))
|
|
214
|
+
return err("the attestation statement is not a CBOR map");
|
|
215
|
+
if (fmt === "none")
|
|
216
|
+
return stmt.size === 0 ? ok(undefined) : err("an attestation of format none with a statement");
|
|
217
|
+
if (fmt !== "packed")
|
|
218
|
+
return err(`the attestation format ${fmt} is not accepted (commons asks for none, and accepts none and packed)`);
|
|
219
|
+
const alg = stmt.get("alg");
|
|
220
|
+
const sig = stmt.get("sig");
|
|
221
|
+
if (typeof alg !== "number" || !(sig instanceof Uint8Array))
|
|
222
|
+
return err("a packed attestation statement without alg and sig");
|
|
223
|
+
if (stmt.has("x5c"))
|
|
224
|
+
return ok(undefined); // a certificate chain: not judged, the credential has the trust of none
|
|
225
|
+
if (alg !== algOfCose(key))
|
|
226
|
+
return err("a packed self-attestation whose algorithm is not the credential's");
|
|
227
|
+
const v = await verifyCose(key, sig, concatBytes(authData, clientDataHash));
|
|
228
|
+
if (v.kind === "err")
|
|
229
|
+
return err(`the packed self-attestation cannot be checked (${v.error})`);
|
|
230
|
+
return v.value ? ok(undefined) : err("the packed self-attestation's signature does not verify");
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* A registration verified (WebAuthn §7.1): the client data, the attestation object, the authenticator data and the credential's key; what
|
|
234
|
+
* to keep, or why it is refused.
|
|
235
|
+
*/
|
|
236
|
+
export async function verifyRegistration(o) {
|
|
237
|
+
const r = o.response;
|
|
238
|
+
if (r.type !== "public-key")
|
|
239
|
+
return err("the credential is not of type public-key");
|
|
240
|
+
const rawId = bytesOfB64u(r.rawId, "rawId");
|
|
241
|
+
if (rawId.kind === "err")
|
|
242
|
+
return rawId;
|
|
243
|
+
if (r.id !== r.rawId)
|
|
244
|
+
return err("the credential's id and rawId differ");
|
|
245
|
+
const clientDataJSON = checkClientData("webauthn.create", {
|
|
246
|
+
clientDataJSON: r.response.clientDataJSON,
|
|
247
|
+
expectedChallenge: o.expectedChallenge,
|
|
248
|
+
expectedOrigin: o.expectedOrigin,
|
|
249
|
+
});
|
|
250
|
+
if (clientDataJSON.kind === "err")
|
|
251
|
+
return clientDataJSON;
|
|
252
|
+
const attObjBytes = bytesOfB64u(r.response.attestationObject, "attestationObject");
|
|
253
|
+
if (attObjBytes.kind === "err")
|
|
254
|
+
return attObjBytes;
|
|
255
|
+
const attObj = decodeCbor(attObjBytes.value);
|
|
256
|
+
if (attObj.kind === "err")
|
|
257
|
+
return err(`the attestation object is not CBOR (${formatCborError(attObj.error)})`);
|
|
258
|
+
if (!isMap(attObj.value))
|
|
259
|
+
return err("the attestation object is not a CBOR map");
|
|
260
|
+
const fmt = attObj.value.get("fmt");
|
|
261
|
+
const authBytes = attObj.value.get("authData");
|
|
262
|
+
const stmt = attObj.value.get("attStmt");
|
|
263
|
+
if (typeof fmt !== "string" || !(authBytes instanceof Uint8Array) || stmt === undefined)
|
|
264
|
+
return err("the attestation object lacks fmt, attStmt or authData");
|
|
265
|
+
const data = parseAuthenticatorData(authBytes);
|
|
266
|
+
if (data.kind === "err")
|
|
267
|
+
return data;
|
|
268
|
+
const checked = await checkAuthenticatorData(data.value, o.rpId);
|
|
269
|
+
if (checked.kind === "err")
|
|
270
|
+
return checked;
|
|
271
|
+
const attested = data.value.attested;
|
|
272
|
+
if (attested === undefined)
|
|
273
|
+
return err("the authenticator data holds no attested credential (the AT flag is not set)");
|
|
274
|
+
if (!sameBytes(attested.credentialId, rawId.value))
|
|
275
|
+
return err("the credential id in the authenticator data is not the credential's rawId");
|
|
276
|
+
const key = parseCoseKey(attested.publicKey);
|
|
277
|
+
if (key.kind === "err")
|
|
278
|
+
return key;
|
|
279
|
+
const clientDataHash = await digest(clientDataJSON.value);
|
|
280
|
+
const att = await checkAttestation(fmt, stmt, key.value, authBytes, clientDataHash);
|
|
281
|
+
if (att.kind === "err")
|
|
282
|
+
return att;
|
|
283
|
+
const transports = Array.isArray(r.response.transports)
|
|
284
|
+
? r.response.transports.filter((t) => typeof t === "string")
|
|
285
|
+
: undefined;
|
|
286
|
+
return ok({
|
|
287
|
+
credentialId: toBase64Url(rawId.value),
|
|
288
|
+
publicKey: toBase64Url(attested.publicKey),
|
|
289
|
+
alg: algOfCose(key.value),
|
|
290
|
+
signCount: data.value.signCount,
|
|
291
|
+
userVerified: data.value.userVerified,
|
|
292
|
+
backedUp: data.value.backedUp,
|
|
293
|
+
backupEligible: data.value.backupEligible,
|
|
294
|
+
aaguid: uuidOf(attested.aaguid),
|
|
295
|
+
fmt,
|
|
296
|
+
...(transports === undefined ? {} : { transports }),
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* An authentication verified (WebAuthn §7.2) against the credential kept at registration: the client data, the authenticator data, the
|
|
301
|
+
* signature and the counter; the counter and flags to keep, or why it is refused.
|
|
302
|
+
*/
|
|
303
|
+
export async function verifyAuthentication(o) {
|
|
304
|
+
const r = o.response;
|
|
305
|
+
if (r.type !== "public-key")
|
|
306
|
+
return err("the credential is not of type public-key");
|
|
307
|
+
const rawId = bytesOfB64u(r.rawId, "rawId");
|
|
308
|
+
if (rawId.kind === "err")
|
|
309
|
+
return rawId;
|
|
310
|
+
if (r.id !== r.rawId)
|
|
311
|
+
return err("the credential's id and rawId differ");
|
|
312
|
+
const storedId = bytesOfB64u(o.credential.id, "the stored credential's id");
|
|
313
|
+
if (storedId.kind === "err")
|
|
314
|
+
return storedId;
|
|
315
|
+
if (!sameBytes(rawId.value, storedId.value))
|
|
316
|
+
return err("the assertion is by another credential than the one given");
|
|
317
|
+
const clientDataJSON = checkClientData("webauthn.get", {
|
|
318
|
+
clientDataJSON: r.response.clientDataJSON,
|
|
319
|
+
expectedChallenge: o.expectedChallenge,
|
|
320
|
+
expectedOrigin: o.expectedOrigin,
|
|
321
|
+
});
|
|
322
|
+
if (clientDataJSON.kind === "err")
|
|
323
|
+
return clientDataJSON;
|
|
324
|
+
const authBytes = bytesOfB64u(r.response.authenticatorData, "authenticatorData");
|
|
325
|
+
if (authBytes.kind === "err")
|
|
326
|
+
return authBytes;
|
|
327
|
+
const data = parseAuthenticatorData(authBytes.value);
|
|
328
|
+
if (data.kind === "err")
|
|
329
|
+
return data;
|
|
330
|
+
const checked = await checkAuthenticatorData(data.value, o.rpId);
|
|
331
|
+
if (checked.kind === "err")
|
|
332
|
+
return checked;
|
|
333
|
+
const keyBytes = bytesOfB64u(o.credential.publicKey, "the stored public key");
|
|
334
|
+
if (keyBytes.kind === "err")
|
|
335
|
+
return keyBytes;
|
|
336
|
+
const key = parseCoseKey(keyBytes.value);
|
|
337
|
+
if (key.kind === "err")
|
|
338
|
+
return err(`the stored public key cannot be used (${key.error})`);
|
|
339
|
+
if (algOfCose(key.value) !== o.credential.alg)
|
|
340
|
+
return err("the stored algorithm is not the stored key's");
|
|
341
|
+
const signature = bytesOfB64u(r.response.signature, "signature");
|
|
342
|
+
if (signature.kind === "err")
|
|
343
|
+
return signature;
|
|
344
|
+
const signed = concatBytes(authBytes.value, await digest(clientDataJSON.value));
|
|
345
|
+
const v = await verifyCose(key.value, signature.value, signed);
|
|
346
|
+
if (v.kind === "err")
|
|
347
|
+
return v;
|
|
348
|
+
if (!v.value)
|
|
349
|
+
return err("the signature does not verify");
|
|
350
|
+
const now = data.value.signCount;
|
|
351
|
+
const before = o.credential.signCount;
|
|
352
|
+
if ((now !== 0 || before !== 0) && now <= before)
|
|
353
|
+
return err(`the signature counter went from ${before} to ${now}, not up: the authenticator may be cloned`);
|
|
354
|
+
return ok({ signCount: now, userVerified: data.value.userVerified, backedUp: data.value.backedUp });
|
|
355
|
+
}
|
|
356
|
+
const kind = literal("public-key");
|
|
357
|
+
const extensionResults = record(unknownValue);
|
|
358
|
+
/** JSON from a browser read as a `RegistrationResponseJSON`, or why it is not one: the boundary before `verifyRegistration`. */
|
|
359
|
+
export function parseRegistrationResponse(input) {
|
|
360
|
+
const d = object({
|
|
361
|
+
id: string,
|
|
362
|
+
rawId: string,
|
|
363
|
+
type: kind,
|
|
364
|
+
response: object({ clientDataJSON: string, attestationObject: string }, { authenticatorData: string, transports: array(string), publicKey: string, publicKeyAlgorithm: integer }),
|
|
365
|
+
}, { authenticatorAttachment: nullable(string), clientExtensionResults: extensionResults })(input);
|
|
366
|
+
if (d.kind === "err")
|
|
367
|
+
return err(`not a registration response (${formatDecodeError(d.error)})`);
|
|
368
|
+
return ok({ ...d.value, clientExtensionResults: d.value.clientExtensionResults ?? {} });
|
|
369
|
+
}
|
|
370
|
+
/** JSON from a browser read as an `AuthenticationResponseJSON`, or why it is not one: the boundary before `verifyAuthentication`. */
|
|
371
|
+
export function parseAuthenticationResponse(input) {
|
|
372
|
+
const d = object({
|
|
373
|
+
id: string,
|
|
374
|
+
rawId: string,
|
|
375
|
+
type: kind,
|
|
376
|
+
response: object({ clientDataJSON: string, authenticatorData: string, signature: string }, { userHandle: nullable(string) }),
|
|
377
|
+
}, { authenticatorAttachment: nullable(string), clientExtensionResults: extensionResults })(input);
|
|
378
|
+
if (d.kind === "err")
|
|
379
|
+
return err(`not an authentication response (${formatDecodeError(d.error)})`);
|
|
380
|
+
return ok({ ...d.value, clientExtensionResults: d.value.clientExtensionResults ?? {} });
|
|
381
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,86 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intellectual-frontiers/crypto",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Crypto: SHA-256, MD5, SHA-512 crypt, UUIDs, AWS Signature Version 4, CMS, DSSE, COSE and WebAuthn, in pure code on Web Crypto, with core and formats as its dependencies.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=26"
|
|
9
|
+
},
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"types": "./dist/mod.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/mod.d.ts",
|
|
15
|
+
"bun": "./src/mod.ts",
|
|
16
|
+
"default": "./dist/mod.js"
|
|
17
|
+
},
|
|
18
|
+
"./cms": {
|
|
19
|
+
"types": "./dist/cms.d.ts",
|
|
20
|
+
"bun": "./src/cms.ts",
|
|
21
|
+
"default": "./dist/cms.js"
|
|
22
|
+
},
|
|
23
|
+
"./cose": {
|
|
24
|
+
"types": "./dist/cose.d.ts",
|
|
25
|
+
"bun": "./src/cose.ts",
|
|
26
|
+
"default": "./dist/cose.js"
|
|
27
|
+
},
|
|
28
|
+
"./crypt": {
|
|
29
|
+
"types": "./dist/crypt.d.ts",
|
|
30
|
+
"bun": "./src/crypt.ts",
|
|
31
|
+
"default": "./dist/crypt.js"
|
|
32
|
+
},
|
|
33
|
+
"./dsse": {
|
|
34
|
+
"types": "./dist/dsse.d.ts",
|
|
35
|
+
"bun": "./src/dsse.ts",
|
|
36
|
+
"default": "./dist/dsse.js"
|
|
37
|
+
},
|
|
38
|
+
"./md5": {
|
|
39
|
+
"types": "./dist/md5.d.ts",
|
|
40
|
+
"bun": "./src/md5.ts",
|
|
41
|
+
"default": "./dist/md5.js"
|
|
42
|
+
},
|
|
43
|
+
"./sha256": {
|
|
44
|
+
"types": "./dist/sha256.d.ts",
|
|
45
|
+
"bun": "./src/sha256.ts",
|
|
46
|
+
"default": "./dist/sha256.js"
|
|
47
|
+
},
|
|
48
|
+
"./sigv4": {
|
|
49
|
+
"types": "./dist/sigv4.d.ts",
|
|
50
|
+
"bun": "./src/sigv4.ts",
|
|
51
|
+
"default": "./dist/sigv4.js"
|
|
52
|
+
},
|
|
53
|
+
"./uuid": {
|
|
54
|
+
"types": "./dist/uuid.d.ts",
|
|
55
|
+
"bun": "./src/uuid.ts",
|
|
56
|
+
"default": "./dist/uuid.js"
|
|
57
|
+
},
|
|
58
|
+
"./webauthn": {
|
|
59
|
+
"types": "./dist/webauthn.d.ts",
|
|
60
|
+
"bun": "./src/webauthn.ts",
|
|
61
|
+
"default": "./dist/webauthn.js"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"@intellectual-frontiers/core": "0.1.1",
|
|
66
|
+
"@intellectual-frontiers/formats": "0.1.1"
|
|
67
|
+
},
|
|
68
|
+
"files": [
|
|
69
|
+
"dist",
|
|
70
|
+
"src",
|
|
71
|
+
"README.md",
|
|
72
|
+
"LICENSE"
|
|
73
|
+
],
|
|
74
|
+
"repository": {
|
|
75
|
+
"type": "git",
|
|
76
|
+
"url": "git+https://github.com/intellectual-frontiers/.github.git",
|
|
77
|
+
"directory": "commons/crypto"
|
|
78
|
+
},
|
|
79
|
+
"homepage": "https://github.com/intellectual-frontiers/.github/tree/main/commons/crypto",
|
|
80
|
+
"bugs": {
|
|
81
|
+
"url": "https://github.com/intellectual-frontiers/.github/issues"
|
|
82
|
+
},
|
|
83
|
+
"publishConfig": {
|
|
84
|
+
"access": "public"
|
|
85
|
+
}
|
|
86
|
+
}
|