@metamask-previews/passkey-controller 0.0.0-preview-4c0846313
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/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +155 -0
- package/dist/PasskeyController.cjs +448 -0
- package/dist/PasskeyController.cjs.map +1 -0
- package/dist/PasskeyController.d.cts +168 -0
- package/dist/PasskeyController.d.cts.map +1 -0
- package/dist/PasskeyController.d.mts +168 -0
- package/dist/PasskeyController.d.mts.map +1 -0
- package/dist/PasskeyController.mjs +443 -0
- package/dist/PasskeyController.mjs.map +1 -0
- package/dist/ceremony-manager.cjs +134 -0
- package/dist/ceremony-manager.cjs.map +1 -0
- package/dist/ceremony-manager.d.cts +71 -0
- package/dist/ceremony-manager.d.cts.map +1 -0
- package/dist/ceremony-manager.d.mts +71 -0
- package/dist/ceremony-manager.d.mts.map +1 -0
- package/dist/ceremony-manager.mjs +130 -0
- package/dist/ceremony-manager.mjs.map +1 -0
- package/dist/constants.cjs +33 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +30 -0
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +30 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/constants.mjs +30 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/errors.cjs +57 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.cts +34 -0
- package/dist/errors.d.cts.map +1 -0
- package/dist/errors.d.mts +34 -0
- package/dist/errors.d.mts.map +1 -0
- package/dist/errors.mjs +53 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/index.cjs +19 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +5 -0
- package/dist/index.mjs.map +1 -0
- package/dist/key-derivation.cjs +76 -0
- package/dist/key-derivation.cjs.map +1 -0
- package/dist/key-derivation.d.cts +43 -0
- package/dist/key-derivation.d.cts.map +1 -0
- package/dist/key-derivation.d.mts +43 -0
- package/dist/key-derivation.d.mts.map +1 -0
- package/dist/key-derivation.mjs +71 -0
- package/dist/key-derivation.mjs.map +1 -0
- package/dist/logger.cjs +9 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +5 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.mts +5 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/logger.mjs +6 -0
- package/dist/logger.mjs.map +1 -0
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +92 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +92 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/dist/utils/crypto.cjs +55 -0
- package/dist/utils/crypto.cjs.map +1 -0
- package/dist/utils/crypto.d.cts +30 -0
- package/dist/utils/crypto.d.cts.map +1 -0
- package/dist/utils/crypto.d.mts +30 -0
- package/dist/utils/crypto.d.mts.map +1 -0
- package/dist/utils/crypto.mjs +49 -0
- package/dist/utils/crypto.mjs.map +1 -0
- package/dist/utils/encoding.cjs +42 -0
- package/dist/utils/encoding.cjs.map +1 -0
- package/dist/utils/encoding.d.cts +22 -0
- package/dist/utils/encoding.d.cts.map +1 -0
- package/dist/utils/encoding.d.mts +22 -0
- package/dist/utils/encoding.d.mts.map +1 -0
- package/dist/utils/encoding.mjs +36 -0
- package/dist/utils/encoding.mjs.map +1 -0
- package/dist/webauthn/constants.cjs +74 -0
- package/dist/webauthn/constants.cjs.map +1 -0
- package/dist/webauthn/constants.d.cts +68 -0
- package/dist/webauthn/constants.d.cts.map +1 -0
- package/dist/webauthn/constants.d.mts +68 -0
- package/dist/webauthn/constants.d.mts.map +1 -0
- package/dist/webauthn/constants.mjs +71 -0
- package/dist/webauthn/constants.mjs.map +1 -0
- package/dist/webauthn/decode-attestation-object.cjs +18 -0
- package/dist/webauthn/decode-attestation-object.cjs.map +1 -0
- package/dist/webauthn/decode-attestation-object.d.cts +10 -0
- package/dist/webauthn/decode-attestation-object.d.cts.map +1 -0
- package/dist/webauthn/decode-attestation-object.d.mts +10 -0
- package/dist/webauthn/decode-attestation-object.d.mts.map +1 -0
- package/dist/webauthn/decode-attestation-object.mjs +14 -0
- package/dist/webauthn/decode-attestation-object.mjs.map +1 -0
- package/dist/webauthn/decode-client-data-json.cjs +17 -0
- package/dist/webauthn/decode-client-data-json.cjs.map +1 -0
- package/dist/webauthn/decode-client-data-json.d.cts +9 -0
- package/dist/webauthn/decode-client-data-json.d.cts.map +1 -0
- package/dist/webauthn/decode-client-data-json.d.mts +9 -0
- package/dist/webauthn/decode-client-data-json.d.mts.map +1 -0
- package/dist/webauthn/decode-client-data-json.mjs +13 -0
- package/dist/webauthn/decode-client-data-json.mjs.map +1 -0
- package/dist/webauthn/match-expected-rp-id.cjs +43 -0
- package/dist/webauthn/match-expected-rp-id.cjs.map +1 -0
- package/dist/webauthn/match-expected-rp-id.d.cts +11 -0
- package/dist/webauthn/match-expected-rp-id.d.cts.map +1 -0
- package/dist/webauthn/match-expected-rp-id.d.mts +11 -0
- package/dist/webauthn/match-expected-rp-id.d.mts.map +1 -0
- package/dist/webauthn/match-expected-rp-id.mjs +39 -0
- package/dist/webauthn/match-expected-rp-id.mjs.map +1 -0
- package/dist/webauthn/parse-authenticator-data.cjs +69 -0
- package/dist/webauthn/parse-authenticator-data.cjs.map +1 -0
- package/dist/webauthn/parse-authenticator-data.d.cts +10 -0
- package/dist/webauthn/parse-authenticator-data.d.cts.map +1 -0
- package/dist/webauthn/parse-authenticator-data.d.mts +10 -0
- package/dist/webauthn/parse-authenticator-data.d.mts.map +1 -0
- package/dist/webauthn/parse-authenticator-data.mjs +65 -0
- package/dist/webauthn/parse-authenticator-data.mjs.map +1 -0
- package/dist/webauthn/types.cjs +3 -0
- package/dist/webauthn/types.cjs.map +1 -0
- package/dist/webauthn/types.d.cts +113 -0
- package/dist/webauthn/types.d.cts.map +1 -0
- package/dist/webauthn/types.d.mts +113 -0
- package/dist/webauthn/types.d.mts.map +1 -0
- package/dist/webauthn/types.mjs +2 -0
- package/dist/webauthn/types.mjs.map +1 -0
- package/dist/webauthn/verify-authentication-response.cjs +134 -0
- package/dist/webauthn/verify-authentication-response.cjs.map +1 -0
- package/dist/webauthn/verify-authentication-response.d.cts +63 -0
- package/dist/webauthn/verify-authentication-response.d.cts.map +1 -0
- package/dist/webauthn/verify-authentication-response.d.mts +63 -0
- package/dist/webauthn/verify-authentication-response.d.mts.map +1 -0
- package/dist/webauthn/verify-authentication-response.mjs +130 -0
- package/dist/webauthn/verify-authentication-response.mjs.map +1 -0
- package/dist/webauthn/verify-registration-response.cjs +205 -0
- package/dist/webauthn/verify-registration-response.cjs.map +1 -0
- package/dist/webauthn/verify-registration-response.d.cts +60 -0
- package/dist/webauthn/verify-registration-response.d.cts.map +1 -0
- package/dist/webauthn/verify-registration-response.d.mts +60 -0
- package/dist/webauthn/verify-registration-response.d.mts.map +1 -0
- package/dist/webauthn/verify-registration-response.mjs +201 -0
- package/dist/webauthn/verify-registration-response.mjs.map +1 -0
- package/dist/webauthn/verify-signature.cjs +176 -0
- package/dist/webauthn/verify-signature.cjs.map +1 -0
- package/dist/webauthn/verify-signature.d.cts +21 -0
- package/dist/webauthn/verify-signature.d.cts.map +1 -0
- package/dist/webauthn/verify-signature.d.mts +21 -0
- package/dist/webauthn/verify-signature.d.mts.map +1 -0
- package/dist/webauthn/verify-signature.mjs +172 -0
- package/dist/webauthn/verify-signature.mjs.map +1 -0
- package/package.json +78 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { decodePartialCBOR } from "@levischuck/tiny-cbor";
|
|
2
|
+
import { concatBytes } from "@metamask/utils";
|
|
3
|
+
import { sha256 } from "@noble/hashes/sha2";
|
|
4
|
+
import { base64URLToBytes, bytesToBase64URL, bytesToHex } from "../utils/encoding.mjs";
|
|
5
|
+
import { COSEALG, COSEKEYS } from "./constants.mjs";
|
|
6
|
+
import { decodeAttestationObject } from "./decode-attestation-object.mjs";
|
|
7
|
+
import { decodeClientDataJSON } from "./decode-client-data-json.mjs";
|
|
8
|
+
import { matchExpectedRPID } from "./match-expected-rp-id.mjs";
|
|
9
|
+
import { parseAuthenticatorData } from "./parse-authenticator-data.mjs";
|
|
10
|
+
import { verifySignature } from "./verify-signature.mjs";
|
|
11
|
+
/**
|
|
12
|
+
* Verifies a WebAuthn registration (attestation) response per
|
|
13
|
+
* W3C WebAuthn Level 3 §7.1.
|
|
14
|
+
*
|
|
15
|
+
* Performs the following checks in order:
|
|
16
|
+
* 1. Credential ID presence and base64url consistency (`id === rawId`), and
|
|
17
|
+
* that `id` matches the credential id inside parsed authenticator data.
|
|
18
|
+
* 2. Credential type is `"public-key"`.
|
|
19
|
+
* 3. `clientDataJSON` -- type is `"webauthn.create"`, challenge and origin
|
|
20
|
+
* match the expected values.
|
|
21
|
+
* 4. Attestation object -- CBOR-decodes and parses `authData` to verify
|
|
22
|
+
* the RP ID hash, user-presence flag, optional user-verification flag,
|
|
23
|
+
* and the attested credential public key algorithm.
|
|
24
|
+
* 5. Attestation statement -- supports `"none"` (no signature) and
|
|
25
|
+
* `"packed"` self-attestation (signature verified against the
|
|
26
|
+
* credential's own public key).
|
|
27
|
+
*
|
|
28
|
+
* @param opts - Verification options.
|
|
29
|
+
* @param opts.response - The `PublicKeyCredential` result from
|
|
30
|
+
* `navigator.credentials.create()`, serialized as JSON.
|
|
31
|
+
* @param opts.expectedChallenge - The base64url challenge that was passed
|
|
32
|
+
* to the authenticator (must match `clientDataJSON.challenge`).
|
|
33
|
+
* @param opts.expectedOrigin - One or more acceptable origins (e.g.
|
|
34
|
+
* `"chrome-extension://..."` or `"https://metamask.io"`).
|
|
35
|
+
* @param opts.expectedRPID - The Relying Party ID domain. The
|
|
36
|
+
* authenticator's `rpIdHash` is compared against `SHA-256(expectedRPID)`.
|
|
37
|
+
* @param opts.requireUserVerification - When `true`, verification fails
|
|
38
|
+
* if the UV flag is not set. Defaults to `false`.
|
|
39
|
+
* @param opts.supportedAlgorithmIDs - COSE algorithm identifiers accepted
|
|
40
|
+
* for the credential public key. Defaults to EdDSA, ES256, and RS256.
|
|
41
|
+
* @returns On success, `{ verified: true, registrationInfo }` with the
|
|
42
|
+
* parsed credential ID, public key, counter, AAGUID, and transport
|
|
43
|
+
* hints. On failure, `{ verified: false }`.
|
|
44
|
+
*/
|
|
45
|
+
export async function verifyRegistrationResponse(opts) {
|
|
46
|
+
const { response, expectedChallenge, expectedOrigin, expectedRPID, requireUserVerification = false, supportedAlgorithmIDs = [COSEALG.EdDSA, COSEALG.ES256, COSEALG.RS256], } = opts;
|
|
47
|
+
const { id, rawId, type: credentialType, response: attestationResponse, } = response;
|
|
48
|
+
// Ensure credential specified an ID
|
|
49
|
+
if (!id) {
|
|
50
|
+
throw new Error('Missing credential ID');
|
|
51
|
+
}
|
|
52
|
+
// Ensure ID is base64url-encoded
|
|
53
|
+
if (id !== rawId) {
|
|
54
|
+
throw new Error('Credential ID was not base64url-encoded');
|
|
55
|
+
}
|
|
56
|
+
// Make sure credential type is public-key
|
|
57
|
+
if (credentialType !== 'public-key') {
|
|
58
|
+
throw new Error(`Unexpected credential type ${String(credentialType)}, expected "public-key"`);
|
|
59
|
+
}
|
|
60
|
+
const clientDataJSON = decodeClientDataJSON(attestationResponse.clientDataJSON);
|
|
61
|
+
const { type, challenge, origin, tokenBinding } = clientDataJSON;
|
|
62
|
+
// Make sure we're handling an registration
|
|
63
|
+
if (type !== 'webauthn.create') {
|
|
64
|
+
throw new Error(`Unexpected registration response type: ${type}`);
|
|
65
|
+
}
|
|
66
|
+
// Ensure the device provided the challenge we gave it
|
|
67
|
+
if (challenge !== expectedChallenge) {
|
|
68
|
+
throw new Error(`Unexpected registration response challenge "${challenge}", expected "${expectedChallenge}"`);
|
|
69
|
+
}
|
|
70
|
+
// Check that the origin is our site
|
|
71
|
+
const expectedOrigins = Array.isArray(expectedOrigin)
|
|
72
|
+
? expectedOrigin
|
|
73
|
+
: [expectedOrigin];
|
|
74
|
+
if (!expectedOrigins.includes(origin)) {
|
|
75
|
+
throw new Error(`Unexpected registration response origin "${origin}", expected one of: ${expectedOrigins.join(', ')}`);
|
|
76
|
+
}
|
|
77
|
+
if (tokenBinding) {
|
|
78
|
+
if (typeof tokenBinding !== 'object') {
|
|
79
|
+
throw new Error('ClientDataJSON tokenBinding was not an object');
|
|
80
|
+
}
|
|
81
|
+
if (!['present', 'supported', 'not-supported'].includes(tokenBinding.status)) {
|
|
82
|
+
throw new Error(`Unexpected tokenBinding.status value of "${tokenBinding.status}"`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const attestationObjectBytes = base64URLToBytes(attestationResponse.attestationObject);
|
|
86
|
+
const decodedAttObj = decodeAttestationObject(attestationObjectBytes);
|
|
87
|
+
const fmt = decodedAttObj.get('fmt');
|
|
88
|
+
const authData = decodedAttObj.get('authData');
|
|
89
|
+
const attStmt = decodedAttObj.get('attStmt');
|
|
90
|
+
const parsedAuthData = parseAuthenticatorData(authData);
|
|
91
|
+
const { rpIdHash, flags, counter, credentialID, credentialPublicKey, aaguid, } = parsedAuthData;
|
|
92
|
+
matchExpectedRPID(rpIdHash, [expectedRPID]);
|
|
93
|
+
// Make sure someone was physically present
|
|
94
|
+
if (!flags.up) {
|
|
95
|
+
throw new Error('User presence was required, but user was not present');
|
|
96
|
+
}
|
|
97
|
+
// Enforce user verification if specified
|
|
98
|
+
if (requireUserVerification && !flags.uv) {
|
|
99
|
+
throw new Error('User verification was required, but user could not be verified');
|
|
100
|
+
}
|
|
101
|
+
if (!credentialID) {
|
|
102
|
+
throw new Error('No credential ID was provided by authenticator');
|
|
103
|
+
}
|
|
104
|
+
const attestedCredentialId = bytesToBase64URL(credentialID);
|
|
105
|
+
if (id !== attestedCredentialId) {
|
|
106
|
+
throw new Error('Credential id does not match the credential id in authenticator data');
|
|
107
|
+
}
|
|
108
|
+
if (!credentialPublicKey) {
|
|
109
|
+
throw new Error('No public key was provided by authenticator');
|
|
110
|
+
}
|
|
111
|
+
if (!aaguid) {
|
|
112
|
+
throw new Error('No AAGUID was present during registration');
|
|
113
|
+
}
|
|
114
|
+
const decodedPublicKey = decodePartialCBOR(new Uint8Array(credentialPublicKey), 0)[0];
|
|
115
|
+
const alg = decodedPublicKey.get(COSEKEYS.Alg);
|
|
116
|
+
if (typeof alg !== 'number') {
|
|
117
|
+
throw new Error('Credential public key was missing numeric alg');
|
|
118
|
+
}
|
|
119
|
+
// Make sure the key algorithm is one we specified within the registration options
|
|
120
|
+
if (!supportedAlgorithmIDs.includes(alg)) {
|
|
121
|
+
throw new Error(`Unexpected public key alg "${alg}", expected one of "${supportedAlgorithmIDs.join(', ')}"`);
|
|
122
|
+
}
|
|
123
|
+
let verified = false;
|
|
124
|
+
if (fmt === 'none') {
|
|
125
|
+
if (attStmt.size > 0) {
|
|
126
|
+
throw new Error('None attestation had unexpected attestation statement');
|
|
127
|
+
}
|
|
128
|
+
verified = true;
|
|
129
|
+
}
|
|
130
|
+
else if (fmt === 'packed') {
|
|
131
|
+
verified = await verifyPackedAttestation(attStmt, authData, attestationResponse.clientDataJSON, decodedPublicKey);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
throw new Error(`Unsupported attestation format: ${fmt}`);
|
|
135
|
+
}
|
|
136
|
+
if (!verified) {
|
|
137
|
+
return { verified: false };
|
|
138
|
+
}
|
|
139
|
+
const aaguidHex = bytesToHex(aaguid);
|
|
140
|
+
const aaguidStr = [
|
|
141
|
+
aaguidHex.slice(0, 8),
|
|
142
|
+
aaguidHex.slice(8, 12),
|
|
143
|
+
aaguidHex.slice(12, 16),
|
|
144
|
+
aaguidHex.slice(16, 20),
|
|
145
|
+
aaguidHex.slice(20),
|
|
146
|
+
].join('-');
|
|
147
|
+
return {
|
|
148
|
+
verified: true,
|
|
149
|
+
registrationInfo: {
|
|
150
|
+
credentialId: attestedCredentialId,
|
|
151
|
+
publicKey: credentialPublicKey,
|
|
152
|
+
counter,
|
|
153
|
+
transports: attestationResponse.transports,
|
|
154
|
+
aaguid: aaguidStr,
|
|
155
|
+
attestationFormat: fmt,
|
|
156
|
+
userVerified: flags.uv,
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Verify packed self-attestation per WebAuthn §8.2: no x5c certificate
|
|
162
|
+
* chain, signature over `authData || SHA-256(clientDataJSON)` verified
|
|
163
|
+
* with the credential's own public key, and `alg` in the attestation
|
|
164
|
+
* statement must match the credential key's algorithm.
|
|
165
|
+
*
|
|
166
|
+
* @param attStmt - The attestation statement map from the attestation
|
|
167
|
+
* object.
|
|
168
|
+
* @param attStmt.get - Accessor to retrieve statement fields by key.
|
|
169
|
+
* @param attStmt.size - Number of entries in the statement.
|
|
170
|
+
* @param authData - Raw authenticator data bytes.
|
|
171
|
+
* @param clientDataJSONB64url - Base64url-encoded clientDataJSON.
|
|
172
|
+
* @param cosePublicKey - Decoded COSE public key map from authenticator
|
|
173
|
+
* data.
|
|
174
|
+
* @returns Whether the packed attestation signature is valid.
|
|
175
|
+
*/
|
|
176
|
+
async function verifyPackedAttestation(attStmt, authData, clientDataJSONB64url, cosePublicKey) {
|
|
177
|
+
const attStmtAlg = attStmt.get('alg');
|
|
178
|
+
const signature = attStmt.get('sig');
|
|
179
|
+
const x5c = attStmt.get('x5c');
|
|
180
|
+
if (typeof attStmtAlg !== 'number') {
|
|
181
|
+
throw new Error('Packed attestation statement missing alg');
|
|
182
|
+
}
|
|
183
|
+
if (!signature) {
|
|
184
|
+
throw new Error('Packed attestation missing signature');
|
|
185
|
+
}
|
|
186
|
+
if (x5c && x5c.length > 0) {
|
|
187
|
+
throw new Error('Packed attestation with certificate chain (x5c) is not supported; only self-attestation is accepted');
|
|
188
|
+
}
|
|
189
|
+
const credAlg = cosePublicKey.get(COSEKEYS.Alg);
|
|
190
|
+
if (attStmtAlg !== credAlg) {
|
|
191
|
+
throw new Error(`Packed attestation alg ${attStmtAlg} does not match credential alg ${credAlg}`);
|
|
192
|
+
}
|
|
193
|
+
const clientDataHash = sha256(base64URLToBytes(clientDataJSONB64url));
|
|
194
|
+
const signatureBase = concatBytes([authData, clientDataHash]);
|
|
195
|
+
return verifySignature({
|
|
196
|
+
cosePublicKey,
|
|
197
|
+
signature,
|
|
198
|
+
data: signatureBase,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=verify-registration-response.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify-registration-response.mjs","sourceRoot":"","sources":["../../src/webauthn/verify-registration-response.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAC1D,OAAO,EAAE,WAAW,EAAE,wBAAwB;AAC9C,OAAO,EAAE,MAAM,EAAE,2BAA2B;AAG5C,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACX,8BAA0B;AAC3B,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,wBAAoB;AAChD,OAAO,EAAE,uBAAuB,EAAE,wCAAoC;AACtE,OAAO,EAAE,oBAAoB,EAAE,sCAAkC;AACjE,OAAO,EAAE,iBAAiB,EAAE,mCAA+B;AAC3D,OAAO,EAAE,sBAAsB,EAAE,uCAAmC;AAEpE,OAAO,EAAE,eAAe,EAAE,+BAA2B;AAiBrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAC,IAOhD;IACC,MAAM,EACJ,QAAQ,EACR,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,uBAAuB,GAAG,KAAK,EAC/B,qBAAqB,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,GACtE,GAAG,IAAI,CAAC;IAET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CAAC,GAAG,CAAC,KAAK,CAA6B,CAAC;IAE3D,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAC9D,CAAC;IAED,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;IAC1D,CAAC;IAED,IAAI,GAAG,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CACb,qGAAqG,CACtG,CAAC;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAW,CAAC;IAC1D,IAAI,UAAU,KAAK,OAAO,EAAE,CAAC;QAC3B,MAAM,IAAI,KAAK,CACb,0BAA0B,UAAU,kCAAkC,OAAO,EAAE,CAChF,CAAC;IACJ,CAAC;IAED,MAAM,cAAc,GAAG,MAAM,CAAC,gBAAgB,CAAC,oBAAoB,CAAC,CAAC,CAAC;IACtE,MAAM,aAAa,GAAG,WAAW,CAAC,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC,CAAC;IAE9D,OAAO,eAAe,CAAC;QACrB,aAAa;QACb,SAAS;QACT,IAAI,EAAE,aAAa;KACpB,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { decodePartialCBOR } from '@levischuck/tiny-cbor';\nimport { concatBytes } from '@metamask/utils';\nimport { sha256 } from '@noble/hashes/sha2';\n\nimport type { AuthenticatorTransportFuture } from '../types';\nimport {\n base64URLToBytes,\n bytesToBase64URL,\n bytesToHex,\n} from '../utils/encoding';\nimport { COSEALG, COSEKEYS } from './constants';\nimport { decodeAttestationObject } from './decode-attestation-object';\nimport { decodeClientDataJSON } from './decode-client-data-json';\nimport { matchExpectedRPID } from './match-expected-rp-id';\nimport { parseAuthenticatorData } from './parse-authenticator-data';\nimport type { PasskeyRegistrationResponse } from './types';\nimport { verifySignature } from './verify-signature';\n\nexport type VerifiedRegistrationResponse =\n | { verified: false; registrationInfo?: never }\n | {\n verified: true;\n registrationInfo: {\n credentialId: string;\n publicKey: Uint8Array;\n counter: number;\n transports?: AuthenticatorTransportFuture[];\n aaguid: string;\n attestationFormat: string;\n userVerified: boolean;\n };\n };\n\n/**\n * Verifies a WebAuthn registration (attestation) response per\n * W3C WebAuthn Level 3 §7.1.\n *\n * Performs the following checks in order:\n * 1. Credential ID presence and base64url consistency (`id === rawId`), and\n * that `id` matches the credential id inside parsed authenticator data.\n * 2. Credential type is `\"public-key\"`.\n * 3. `clientDataJSON` -- type is `\"webauthn.create\"`, challenge and origin\n * match the expected values.\n * 4. Attestation object -- CBOR-decodes and parses `authData` to verify\n * the RP ID hash, user-presence flag, optional user-verification flag,\n * and the attested credential public key algorithm.\n * 5. Attestation statement -- supports `\"none\"` (no signature) and\n * `\"packed\"` self-attestation (signature verified against the\n * credential's own public key).\n *\n * @param opts - Verification options.\n * @param opts.response - The `PublicKeyCredential` result from\n * `navigator.credentials.create()`, serialized as JSON.\n * @param opts.expectedChallenge - The base64url challenge that was passed\n * to the authenticator (must match `clientDataJSON.challenge`).\n * @param opts.expectedOrigin - One or more acceptable origins (e.g.\n * `\"chrome-extension://...\"` or `\"https://metamask.io\"`).\n * @param opts.expectedRPID - The Relying Party ID domain. The\n * authenticator's `rpIdHash` is compared against `SHA-256(expectedRPID)`.\n * @param opts.requireUserVerification - When `true`, verification fails\n * if the UV flag is not set. Defaults to `false`.\n * @param opts.supportedAlgorithmIDs - COSE algorithm identifiers accepted\n * for the credential public key. Defaults to EdDSA, ES256, and RS256.\n * @returns On success, `{ verified: true, registrationInfo }` with the\n * parsed credential ID, public key, counter, AAGUID, and transport\n * hints. On failure, `{ verified: false }`.\n */\nexport async function verifyRegistrationResponse(opts: {\n response: PasskeyRegistrationResponse;\n expectedChallenge: string;\n expectedOrigin: string | string[];\n expectedRPID: string;\n requireUserVerification?: boolean;\n supportedAlgorithmIDs?: number[];\n}): Promise<VerifiedRegistrationResponse> {\n const {\n response,\n expectedChallenge,\n expectedOrigin,\n expectedRPID,\n requireUserVerification = false,\n supportedAlgorithmIDs = [COSEALG.EdDSA, COSEALG.ES256, COSEALG.RS256],\n } = opts;\n\n const {\n id,\n rawId,\n type: credentialType,\n response: attestationResponse,\n } = response;\n\n // Ensure credential specified an ID\n if (!id) {\n throw new Error('Missing credential ID');\n }\n\n // Ensure ID is base64url-encoded\n if (id !== rawId) {\n throw new Error('Credential ID was not base64url-encoded');\n }\n\n // Make sure credential type is public-key\n if (credentialType !== 'public-key') {\n throw new Error(\n `Unexpected credential type ${String(credentialType)}, expected \"public-key\"`,\n );\n }\n\n const clientDataJSON = decodeClientDataJSON(\n attestationResponse.clientDataJSON,\n );\n const { type, challenge, origin, tokenBinding } = clientDataJSON;\n\n // Make sure we're handling an registration\n if (type !== 'webauthn.create') {\n throw new Error(`Unexpected registration response type: ${type}`);\n }\n\n // Ensure the device provided the challenge we gave it\n if (challenge !== expectedChallenge) {\n throw new Error(\n `Unexpected registration response challenge \"${challenge}\", expected \"${expectedChallenge}\"`,\n );\n }\n\n // Check that the origin is our site\n const expectedOrigins = Array.isArray(expectedOrigin)\n ? expectedOrigin\n : [expectedOrigin];\n if (!expectedOrigins.includes(origin)) {\n throw new Error(\n `Unexpected registration response origin \"${origin}\", expected one of: ${expectedOrigins.join(', ')}`,\n );\n }\n\n if (tokenBinding) {\n if (typeof tokenBinding !== 'object') {\n throw new Error('ClientDataJSON tokenBinding was not an object');\n }\n\n if (\n !['present', 'supported', 'not-supported'].includes(tokenBinding.status)\n ) {\n throw new Error(\n `Unexpected tokenBinding.status value of \"${tokenBinding.status}\"`,\n );\n }\n }\n\n const attestationObjectBytes = base64URLToBytes(\n attestationResponse.attestationObject,\n );\n const decodedAttObj = decodeAttestationObject(attestationObjectBytes);\n const fmt = decodedAttObj.get('fmt');\n const authData = decodedAttObj.get('authData');\n const attStmt = decodedAttObj.get('attStmt');\n\n const parsedAuthData = parseAuthenticatorData(authData);\n const {\n rpIdHash,\n flags,\n counter,\n credentialID,\n credentialPublicKey,\n aaguid,\n } = parsedAuthData;\n\n matchExpectedRPID(rpIdHash, [expectedRPID]);\n\n // Make sure someone was physically present\n if (!flags.up) {\n throw new Error('User presence was required, but user was not present');\n }\n\n // Enforce user verification if specified\n if (requireUserVerification && !flags.uv) {\n throw new Error(\n 'User verification was required, but user could not be verified',\n );\n }\n\n if (!credentialID) {\n throw new Error('No credential ID was provided by authenticator');\n }\n\n const attestedCredentialId = bytesToBase64URL(credentialID);\n if (id !== attestedCredentialId) {\n throw new Error(\n 'Credential id does not match the credential id in authenticator data',\n );\n }\n\n if (!credentialPublicKey) {\n throw new Error('No public key was provided by authenticator');\n }\n if (!aaguid) {\n throw new Error('No AAGUID was present during registration');\n }\n\n const decodedPublicKey = decodePartialCBOR(\n new Uint8Array(credentialPublicKey),\n 0,\n )[0] as Map<number, number | Uint8Array>;\n const alg = decodedPublicKey.get(COSEKEYS.Alg);\n\n if (typeof alg !== 'number') {\n throw new Error('Credential public key was missing numeric alg');\n }\n\n // Make sure the key algorithm is one we specified within the registration options\n if (!supportedAlgorithmIDs.includes(alg)) {\n throw new Error(\n `Unexpected public key alg \"${alg}\", expected one of \"${supportedAlgorithmIDs.join(', ')}\"`,\n );\n }\n\n let verified = false;\n if (fmt === 'none') {\n if (attStmt.size > 0) {\n throw new Error('None attestation had unexpected attestation statement');\n }\n verified = true;\n } else if (fmt === 'packed') {\n verified = await verifyPackedAttestation(\n attStmt,\n authData,\n attestationResponse.clientDataJSON,\n decodedPublicKey,\n );\n } else {\n throw new Error(`Unsupported attestation format: ${fmt}`);\n }\n\n if (!verified) {\n return { verified: false };\n }\n\n const aaguidHex = bytesToHex(aaguid);\n const aaguidStr = [\n aaguidHex.slice(0, 8),\n aaguidHex.slice(8, 12),\n aaguidHex.slice(12, 16),\n aaguidHex.slice(16, 20),\n aaguidHex.slice(20),\n ].join('-');\n\n return {\n verified: true,\n registrationInfo: {\n credentialId: attestedCredentialId,\n publicKey: credentialPublicKey,\n counter,\n transports:\n attestationResponse.transports as AuthenticatorTransportFuture[],\n aaguid: aaguidStr,\n attestationFormat: fmt,\n userVerified: flags.uv,\n },\n };\n}\n\n/**\n * Verify packed self-attestation per WebAuthn §8.2: no x5c certificate\n * chain, signature over `authData || SHA-256(clientDataJSON)` verified\n * with the credential's own public key, and `alg` in the attestation\n * statement must match the credential key's algorithm.\n *\n * @param attStmt - The attestation statement map from the attestation\n * object.\n * @param attStmt.get - Accessor to retrieve statement fields by key.\n * @param attStmt.size - Number of entries in the statement.\n * @param authData - Raw authenticator data bytes.\n * @param clientDataJSONB64url - Base64url-encoded clientDataJSON.\n * @param cosePublicKey - Decoded COSE public key map from authenticator\n * data.\n * @returns Whether the packed attestation signature is valid.\n */\nasync function verifyPackedAttestation(\n attStmt: { get(key: string): unknown; size: number },\n authData: Uint8Array,\n clientDataJSONB64url: string,\n cosePublicKey: Map<number, number | Uint8Array>,\n): Promise<boolean> {\n const attStmtAlg = attStmt.get('alg') as number | undefined;\n const signature = attStmt.get('sig') as Uint8Array | undefined;\n const x5c = attStmt.get('x5c') as Uint8Array[] | undefined;\n\n if (typeof attStmtAlg !== 'number') {\n throw new Error('Packed attestation statement missing alg');\n }\n\n if (!signature) {\n throw new Error('Packed attestation missing signature');\n }\n\n if (x5c && x5c.length > 0) {\n throw new Error(\n 'Packed attestation with certificate chain (x5c) is not supported; only self-attestation is accepted',\n );\n }\n\n const credAlg = cosePublicKey.get(COSEKEYS.Alg) as number;\n if (attStmtAlg !== credAlg) {\n throw new Error(\n `Packed attestation alg ${attStmtAlg} does not match credential alg ${credAlg}`,\n );\n }\n\n const clientDataHash = sha256(base64URLToBytes(clientDataJSONB64url));\n const signatureBase = concatBytes([authData, clientDataHash]);\n\n return verifySignature({\n cosePublicKey,\n signature,\n data: signatureBase,\n });\n}\n"]}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.verifySignature = void 0;
|
|
4
|
+
const utils_1 = require("@metamask/utils");
|
|
5
|
+
const ed25519_1 = require("@noble/curves/ed25519");
|
|
6
|
+
const nist_1 = require("@noble/curves/nist");
|
|
7
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
8
|
+
const encoding_1 = require("../utils/encoding.cjs");
|
|
9
|
+
const constants_1 = require("./constants.cjs");
|
|
10
|
+
/**
|
|
11
|
+
* Get the key type from a COSE public key map.
|
|
12
|
+
*
|
|
13
|
+
* @param cosePublicKey - COSE public key map.
|
|
14
|
+
* @returns The COSEKTY value.
|
|
15
|
+
*/
|
|
16
|
+
function getKeyType(cosePublicKey) {
|
|
17
|
+
const kty = cosePublicKey.get(constants_1.COSEKEYS.Kty);
|
|
18
|
+
if (typeof kty !== 'number') {
|
|
19
|
+
throw new Error('COSE public key missing kty');
|
|
20
|
+
}
|
|
21
|
+
return kty;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Verify an EC2 (P-256, P-384, P-521) signature using @noble/curves.
|
|
25
|
+
*
|
|
26
|
+
* ECDSA requires the data to be hashed with the curve-appropriate
|
|
27
|
+
* algorithm before verification: SHA-256 for P-256 and SHA-384 for P-384.
|
|
28
|
+
*
|
|
29
|
+
* @param cosePublicKey - COSE-encoded EC2 public key.
|
|
30
|
+
* @param signature - DER-encoded ECDSA signature.
|
|
31
|
+
* @param data - Data that was signed.
|
|
32
|
+
* @returns Whether the signature is valid.
|
|
33
|
+
*/
|
|
34
|
+
function verifyEC2(cosePublicKey, signature, data) {
|
|
35
|
+
const alg = cosePublicKey.get(constants_1.COSEKEYS.Alg);
|
|
36
|
+
const crv = cosePublicKey.get(constants_1.COSEKEYS.Crv);
|
|
37
|
+
const xCoord = cosePublicKey.get(constants_1.COSEKEYS.X);
|
|
38
|
+
const yCoord = cosePublicKey.get(constants_1.COSEKEYS.Y);
|
|
39
|
+
if (typeof alg !== 'number') {
|
|
40
|
+
throw new Error('EC2 public key missing alg');
|
|
41
|
+
}
|
|
42
|
+
if (!xCoord || !yCoord) {
|
|
43
|
+
throw new Error('EC2 public key missing x or y coordinate');
|
|
44
|
+
}
|
|
45
|
+
const uncompressed = (0, utils_1.concatBytes)([new Uint8Array([0x04]), xCoord, yCoord]);
|
|
46
|
+
switch (crv) {
|
|
47
|
+
case constants_1.COSECRV.P256:
|
|
48
|
+
return nist_1.p256.verify(signature, (0, sha2_1.sha256)(data), uncompressed);
|
|
49
|
+
case constants_1.COSECRV.P384:
|
|
50
|
+
return nist_1.p384.verify(signature, (0, sha2_1.sha384)(data), uncompressed);
|
|
51
|
+
case constants_1.COSECRV.P521:
|
|
52
|
+
return nist_1.p521.verify(signature, (0, sha2_1.sha512)(data), uncompressed);
|
|
53
|
+
default:
|
|
54
|
+
throw new Error(`Unsupported EC2 curve: ${crv}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Verify an OKP (Ed25519) signature using @noble/curves.
|
|
59
|
+
*
|
|
60
|
+
* @param cosePublicKey - COSE-encoded OKP public key.
|
|
61
|
+
* @param signature - Raw Ed25519 signature (64 bytes).
|
|
62
|
+
* @param data - Data that was signed.
|
|
63
|
+
* @returns Whether the signature is valid.
|
|
64
|
+
*/
|
|
65
|
+
function verifyOKP(cosePublicKey, signature, data) {
|
|
66
|
+
const alg = cosePublicKey.get(constants_1.COSEKEYS.Alg);
|
|
67
|
+
const crv = cosePublicKey.get(constants_1.COSEKEYS.Crv);
|
|
68
|
+
const xCoord = cosePublicKey.get(constants_1.COSEKEYS.X);
|
|
69
|
+
if (alg !== constants_1.COSEALG.EdDSA) {
|
|
70
|
+
throw new Error(`Unexpected OKP algorithm: ${String(alg)}`);
|
|
71
|
+
}
|
|
72
|
+
if (crv !== constants_1.COSECRV.ED25519) {
|
|
73
|
+
throw new Error(`Unsupported OKP curve: ${String(crv)}`);
|
|
74
|
+
}
|
|
75
|
+
if (!xCoord) {
|
|
76
|
+
throw new Error('OKP public key missing x coordinate');
|
|
77
|
+
}
|
|
78
|
+
return ed25519_1.ed25519.verify(signature, data, xCoord);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Verify an RSA signature using Web Crypto API.
|
|
82
|
+
*
|
|
83
|
+
* @param cosePublicKey - COSE-encoded RSA public key.
|
|
84
|
+
* @param signature - RSA PKCS#1 v1.5 signature.
|
|
85
|
+
* @param data - Data that was signed.
|
|
86
|
+
* @returns Whether the signature is valid.
|
|
87
|
+
*/
|
|
88
|
+
async function verifyRSA(cosePublicKey, signature, data) {
|
|
89
|
+
const alg = cosePublicKey.get(constants_1.COSEKEYS.Alg);
|
|
90
|
+
const modulus = cosePublicKey.get(constants_1.COSEKEYS.N);
|
|
91
|
+
const exponent = cosePublicKey.get(constants_1.COSEKEYS.E);
|
|
92
|
+
if (typeof alg !== 'number') {
|
|
93
|
+
throw new Error('RSA public key missing alg');
|
|
94
|
+
}
|
|
95
|
+
if (!modulus || !exponent) {
|
|
96
|
+
throw new Error('RSA public key missing n or e');
|
|
97
|
+
}
|
|
98
|
+
let keyAlgorithmName;
|
|
99
|
+
let hashAlg;
|
|
100
|
+
let saltLength;
|
|
101
|
+
switch (alg) {
|
|
102
|
+
case constants_1.COSEALG.RS1:
|
|
103
|
+
keyAlgorithmName = 'RSASSA-PKCS1-v1_5';
|
|
104
|
+
hashAlg = 'SHA-1';
|
|
105
|
+
break;
|
|
106
|
+
case constants_1.COSEALG.RS256:
|
|
107
|
+
keyAlgorithmName = 'RSASSA-PKCS1-v1_5';
|
|
108
|
+
hashAlg = 'SHA-256';
|
|
109
|
+
break;
|
|
110
|
+
case constants_1.COSEALG.RS384:
|
|
111
|
+
keyAlgorithmName = 'RSASSA-PKCS1-v1_5';
|
|
112
|
+
hashAlg = 'SHA-384';
|
|
113
|
+
break;
|
|
114
|
+
case constants_1.COSEALG.RS512:
|
|
115
|
+
keyAlgorithmName = 'RSASSA-PKCS1-v1_5';
|
|
116
|
+
hashAlg = 'SHA-512';
|
|
117
|
+
break;
|
|
118
|
+
case constants_1.COSEALG.PS256:
|
|
119
|
+
keyAlgorithmName = 'RSA-PSS';
|
|
120
|
+
hashAlg = 'SHA-256';
|
|
121
|
+
saltLength = 32;
|
|
122
|
+
break;
|
|
123
|
+
case constants_1.COSEALG.PS384:
|
|
124
|
+
keyAlgorithmName = 'RSA-PSS';
|
|
125
|
+
hashAlg = 'SHA-384';
|
|
126
|
+
saltLength = 48;
|
|
127
|
+
break;
|
|
128
|
+
case constants_1.COSEALG.PS512:
|
|
129
|
+
keyAlgorithmName = 'RSA-PSS';
|
|
130
|
+
hashAlg = 'SHA-512';
|
|
131
|
+
saltLength = 64;
|
|
132
|
+
break;
|
|
133
|
+
default:
|
|
134
|
+
throw new Error(`Unsupported RSA algorithm: ${alg}`);
|
|
135
|
+
}
|
|
136
|
+
const key = await globalThis.crypto.subtle.importKey('jwk', {
|
|
137
|
+
kty: 'RSA',
|
|
138
|
+
n: (0, encoding_1.bytesToBase64URL)(modulus),
|
|
139
|
+
e: (0, encoding_1.bytesToBase64URL)(exponent),
|
|
140
|
+
}, { name: keyAlgorithmName, hash: { name: hashAlg } }, false, ['verify']);
|
|
141
|
+
const verifyAlgorithm = keyAlgorithmName === 'RSA-PSS'
|
|
142
|
+
? { name: 'RSA-PSS', saltLength: saltLength }
|
|
143
|
+
: 'RSASSA-PKCS1-v1_5';
|
|
144
|
+
const signatureBytes = Uint8Array.from(signature);
|
|
145
|
+
const dataBytes = Uint8Array.from(data);
|
|
146
|
+
return globalThis.crypto.subtle.verify(verifyAlgorithm, key, signatureBytes, dataBytes);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Verify a WebAuthn signature using the appropriate algorithm based on
|
|
150
|
+
* the COSE key type.
|
|
151
|
+
*
|
|
152
|
+
* Uses @noble/curves for EC2 and OKP (synchronous, audited, handles DER
|
|
153
|
+
* natively). Falls back to Web Crypto API for RSA.
|
|
154
|
+
*
|
|
155
|
+
* @param opts - Options object.
|
|
156
|
+
* @param opts.cosePublicKey - COSE-encoded public key as a Map.
|
|
157
|
+
* @param opts.signature - The signature bytes.
|
|
158
|
+
* @param opts.data - The data that was signed.
|
|
159
|
+
* @returns Whether the signature is valid.
|
|
160
|
+
*/
|
|
161
|
+
async function verifySignature(opts) {
|
|
162
|
+
const { cosePublicKey, signature, data } = opts;
|
|
163
|
+
const kty = getKeyType(cosePublicKey);
|
|
164
|
+
switch (kty) {
|
|
165
|
+
case constants_1.COSEKTY.EC2:
|
|
166
|
+
return verifyEC2(cosePublicKey, signature, data);
|
|
167
|
+
case constants_1.COSEKTY.OKP:
|
|
168
|
+
return verifyOKP(cosePublicKey, signature, data);
|
|
169
|
+
case constants_1.COSEKTY.RSA:
|
|
170
|
+
return verifyRSA(cosePublicKey, signature, data);
|
|
171
|
+
default:
|
|
172
|
+
throw new Error(`Unsupported COSE key type: ${kty}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
exports.verifySignature = verifySignature;
|
|
176
|
+
//# sourceMappingURL=verify-signature.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify-signature.cjs","sourceRoot":"","sources":["../../src/webauthn/verify-signature.ts"],"names":[],"mappings":";;;AAAA,2CAA8C;AAC9C,mDAAgD;AAChD,6CAAsD;AACtD,6CAA4D;AAE5D,oDAAqD;AACrD,+CAAkE;AAIlE;;;;;GAKG;AACH,SAAS,UAAU,CAAC,aAA4B;IAC9C,MAAM,GAAG,GAAG,aAAa,CAAC,GAAG,CAAC,oBAAQ,CAAC,GAAG,CAAC,CAAC;IAC5C,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACjD,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAS,SAAS,CAChB,aAA4B,EAC5B,SAAqB,EACrB,IAAgB;I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,CAAC;IACzD,CAAC;AACH,CAAC;AAlBD,0CAkBC","sourcesContent":["import { concatBytes } from '@metamask/utils';\nimport { ed25519 } from '@noble/curves/ed25519';\nimport { p256, p384, p521 } from '@noble/curves/nist';\nimport { sha256, sha384, sha512 } from '@noble/hashes/sha2';\n\nimport { bytesToBase64URL } from '../utils/encoding';\nimport { COSEALG, COSECRV, COSEKEYS, COSEKTY } from './constants';\n\ntype COSEPublicKey = Map<number, number | Uint8Array>;\n\n/**\n * Get the key type from a COSE public key map.\n *\n * @param cosePublicKey - COSE public key map.\n * @returns The COSEKTY value.\n */\nfunction getKeyType(cosePublicKey: COSEPublicKey): number {\n const kty = cosePublicKey.get(COSEKEYS.Kty);\n if (typeof kty !== 'number') {\n throw new Error('COSE public key missing kty');\n }\n return kty;\n}\n\n/**\n * Verify an EC2 (P-256, P-384, P-521) signature using @noble/curves.\n *\n * ECDSA requires the data to be hashed with the curve-appropriate\n * algorithm before verification: SHA-256 for P-256 and SHA-384 for P-384.\n *\n * @param cosePublicKey - COSE-encoded EC2 public key.\n * @param signature - DER-encoded ECDSA signature.\n * @param data - Data that was signed.\n * @returns Whether the signature is valid.\n */\nfunction verifyEC2(\n cosePublicKey: COSEPublicKey,\n signature: Uint8Array,\n data: Uint8Array,\n): boolean {\n const alg = cosePublicKey.get(COSEKEYS.Alg);\n const crv = cosePublicKey.get(COSEKEYS.Crv) as number;\n const xCoord = cosePublicKey.get(COSEKEYS.X) as Uint8Array;\n const yCoord = cosePublicKey.get(COSEKEYS.Y) as Uint8Array;\n\n if (typeof alg !== 'number') {\n throw new Error('EC2 public key missing alg');\n }\n\n if (!xCoord || !yCoord) {\n throw new Error('EC2 public key missing x or y coordinate');\n }\n\n const uncompressed = concatBytes([new Uint8Array([0x04]), xCoord, yCoord]);\n\n switch (crv) {\n case COSECRV.P256:\n return p256.verify(signature, sha256(data), uncompressed);\n case COSECRV.P384:\n return p384.verify(signature, sha384(data), uncompressed);\n case COSECRV.P521:\n return p521.verify(signature, sha512(data), uncompressed);\n default:\n throw new Error(`Unsupported EC2 curve: ${crv}`);\n }\n}\n\n/**\n * Verify an OKP (Ed25519) signature using @noble/curves.\n *\n * @param cosePublicKey - COSE-encoded OKP public key.\n * @param signature - Raw Ed25519 signature (64 bytes).\n * @param data - Data that was signed.\n * @returns Whether the signature is valid.\n */\nfunction verifyOKP(\n cosePublicKey: COSEPublicKey,\n signature: Uint8Array,\n data: Uint8Array,\n): boolean {\n const alg = cosePublicKey.get(COSEKEYS.Alg);\n const crv = cosePublicKey.get(COSEKEYS.Crv);\n const xCoord = cosePublicKey.get(COSEKEYS.X) as Uint8Array;\n\n if (alg !== COSEALG.EdDSA) {\n throw new Error(`Unexpected OKP algorithm: ${String(alg)}`);\n }\n\n if (crv !== COSECRV.ED25519) {\n throw new Error(`Unsupported OKP curve: ${String(crv)}`);\n }\n\n if (!xCoord) {\n throw new Error('OKP public key missing x coordinate');\n }\n\n return ed25519.verify(signature, data, xCoord);\n}\n\n/**\n * Verify an RSA signature using Web Crypto API.\n *\n * @param cosePublicKey - COSE-encoded RSA public key.\n * @param signature - RSA PKCS#1 v1.5 signature.\n * @param data - Data that was signed.\n * @returns Whether the signature is valid.\n */\nasync function verifyRSA(\n cosePublicKey: COSEPublicKey,\n signature: Uint8Array,\n data: Uint8Array,\n): Promise<boolean> {\n const alg = cosePublicKey.get(COSEKEYS.Alg);\n const modulus = cosePublicKey.get(COSEKEYS.N) as Uint8Array;\n const exponent = cosePublicKey.get(COSEKEYS.E) as Uint8Array;\n\n if (typeof alg !== 'number') {\n throw new Error('RSA public key missing alg');\n }\n\n if (!modulus || !exponent) {\n throw new Error('RSA public key missing n or e');\n }\n\n let keyAlgorithmName: 'RSASSA-PKCS1-v1_5' | 'RSA-PSS';\n let hashAlg: string;\n let saltLength: number | undefined;\n switch (alg) {\n case COSEALG.RS1:\n keyAlgorithmName = 'RSASSA-PKCS1-v1_5';\n hashAlg = 'SHA-1';\n break;\n case COSEALG.RS256:\n keyAlgorithmName = 'RSASSA-PKCS1-v1_5';\n hashAlg = 'SHA-256';\n break;\n case COSEALG.RS384:\n keyAlgorithmName = 'RSASSA-PKCS1-v1_5';\n hashAlg = 'SHA-384';\n break;\n case COSEALG.RS512:\n keyAlgorithmName = 'RSASSA-PKCS1-v1_5';\n hashAlg = 'SHA-512';\n break;\n case COSEALG.PS256:\n keyAlgorithmName = 'RSA-PSS';\n hashAlg = 'SHA-256';\n saltLength = 32;\n break;\n case COSEALG.PS384:\n keyAlgorithmName = 'RSA-PSS';\n hashAlg = 'SHA-384';\n saltLength = 48;\n break;\n case COSEALG.PS512:\n keyAlgorithmName = 'RSA-PSS';\n hashAlg = 'SHA-512';\n saltLength = 64;\n break;\n default:\n throw new Error(`Unsupported RSA algorithm: ${alg}`);\n }\n\n const key = await globalThis.crypto.subtle.importKey(\n 'jwk',\n {\n kty: 'RSA',\n n: bytesToBase64URL(modulus),\n e: bytesToBase64URL(exponent),\n },\n { name: keyAlgorithmName, hash: { name: hashAlg } },\n false,\n ['verify'],\n );\n\n const verifyAlgorithm =\n keyAlgorithmName === 'RSA-PSS'\n ? { name: 'RSA-PSS', saltLength: saltLength as number }\n : 'RSASSA-PKCS1-v1_5';\n\n const signatureBytes = Uint8Array.from(signature);\n const dataBytes = Uint8Array.from(data);\n return globalThis.crypto.subtle.verify(\n verifyAlgorithm,\n key,\n signatureBytes,\n dataBytes,\n );\n}\n\n/**\n * Verify a WebAuthn signature using the appropriate algorithm based on\n * the COSE key type.\n *\n * Uses @noble/curves for EC2 and OKP (synchronous, audited, handles DER\n * natively). Falls back to Web Crypto API for RSA.\n *\n * @param opts - Options object.\n * @param opts.cosePublicKey - COSE-encoded public key as a Map.\n * @param opts.signature - The signature bytes.\n * @param opts.data - The data that was signed.\n * @returns Whether the signature is valid.\n */\nexport async function verifySignature(opts: {\n cosePublicKey: COSEPublicKey;\n signature: Uint8Array;\n data: Uint8Array;\n}): Promise<boolean> {\n const { cosePublicKey, signature, data } = opts;\n const kty = getKeyType(cosePublicKey);\n\n switch (kty) {\n case COSEKTY.EC2:\n return verifyEC2(cosePublicKey, signature, data);\n case COSEKTY.OKP:\n return verifyOKP(cosePublicKey, signature, data);\n case COSEKTY.RSA:\n return verifyRSA(cosePublicKey, signature, data);\n default:\n throw new Error(`Unsupported COSE key type: ${kty}`);\n }\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type COSEPublicKey = Map<number, number | Uint8Array>;
|
|
2
|
+
/**
|
|
3
|
+
* Verify a WebAuthn signature using the appropriate algorithm based on
|
|
4
|
+
* the COSE key type.
|
|
5
|
+
*
|
|
6
|
+
* Uses @noble/curves for EC2 and OKP (synchronous, audited, handles DER
|
|
7
|
+
* natively). Falls back to Web Crypto API for RSA.
|
|
8
|
+
*
|
|
9
|
+
* @param opts - Options object.
|
|
10
|
+
* @param opts.cosePublicKey - COSE-encoded public key as a Map.
|
|
11
|
+
* @param opts.signature - The signature bytes.
|
|
12
|
+
* @param opts.data - The data that was signed.
|
|
13
|
+
* @returns Whether the signature is valid.
|
|
14
|
+
*/
|
|
15
|
+
export declare function verifySignature(opts: {
|
|
16
|
+
cosePublicKey: COSEPublicKey;
|
|
17
|
+
signature: Uint8Array;
|
|
18
|
+
data: Uint8Array;
|
|
19
|
+
}): Promise<boolean>;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=verify-signature.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify-signature.d.cts","sourceRoot":"","sources":["../../src/webauthn/verify-signature.ts"],"names":[],"mappings":"AAQA,KAAK,aAAa,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CAAC;AAsLtD;;;;;;;;;;;;GAYG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE;IAC1C,aAAa,EAAE,aAAa,CAAC;IAC7B,SAAS,EAAE,UAAU,CAAC;IACtB,IAAI,EAAE,UAAU,CAAC;CAClB,GAAG,OAAO,CAAC,OAAO,CAAC,CAcnB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type COSEPublicKey = Map<number, number | Uint8Array>;
|
|
2
|
+
/**
|
|
3
|
+
* Verify a WebAuthn signature using the appropriate algorithm based on
|
|
4
|
+
* the COSE key type.
|
|
5
|
+
*
|
|
6
|
+
* Uses @noble/curves for EC2 and OKP (synchronous, audited, handles DER
|
|
7
|
+
* natively). Falls back to Web Crypto API for RSA.
|
|
8
|
+
*
|
|
9
|
+
* @param opts - Options object.
|
|
10
|
+
* @param opts.cosePublicKey - COSE-encoded public key as a Map.
|
|
11
|
+
* @param opts.signature - The signature bytes.
|
|
12
|
+
* @param opts.data - The data that was signed.
|
|
13
|
+
* @returns Whether the signature is valid.
|
|
14
|
+
*/
|
|
15
|
+
export declare function verifySignature(opts: {
|
|
16
|
+
cosePublicKey: COSEPublicKey;
|
|
17
|
+
signature: Uint8Array;
|
|
18
|
+
data: Uint8Array;
|
|
19
|
+
}): Promise<boolean>;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=verify-signature.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify-signature.d.mts","sourceRoot":"","sources":["../../src/webauthn/verify-signature.ts"],"names":[],"mappings":"AAQA,KAAK,aAAa,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CAAC;AAsLtD;;;;;;;;;;;;GAYG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE;IAC1C,aAAa,EAAE,aAAa,CAAC;IAC7B,SAAS,EAAE,UAAU,CAAC;IACtB,IAAI,EAAE,UAAU,CAAC;CAClB,GAAG,OAAO,CAAC,OAAO,CAAC,CAcnB"}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { concatBytes } from "@metamask/utils";
|
|
2
|
+
import { ed25519 } from "@noble/curves/ed25519";
|
|
3
|
+
import { p256, p384, p521 } from "@noble/curves/nist";
|
|
4
|
+
import { sha256, sha384, sha512 } from "@noble/hashes/sha2";
|
|
5
|
+
import { bytesToBase64URL } from "../utils/encoding.mjs";
|
|
6
|
+
import { COSEALG, COSECRV, COSEKEYS, COSEKTY } from "./constants.mjs";
|
|
7
|
+
/**
|
|
8
|
+
* Get the key type from a COSE public key map.
|
|
9
|
+
*
|
|
10
|
+
* @param cosePublicKey - COSE public key map.
|
|
11
|
+
* @returns The COSEKTY value.
|
|
12
|
+
*/
|
|
13
|
+
function getKeyType(cosePublicKey) {
|
|
14
|
+
const kty = cosePublicKey.get(COSEKEYS.Kty);
|
|
15
|
+
if (typeof kty !== 'number') {
|
|
16
|
+
throw new Error('COSE public key missing kty');
|
|
17
|
+
}
|
|
18
|
+
return kty;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Verify an EC2 (P-256, P-384, P-521) signature using @noble/curves.
|
|
22
|
+
*
|
|
23
|
+
* ECDSA requires the data to be hashed with the curve-appropriate
|
|
24
|
+
* algorithm before verification: SHA-256 for P-256 and SHA-384 for P-384.
|
|
25
|
+
*
|
|
26
|
+
* @param cosePublicKey - COSE-encoded EC2 public key.
|
|
27
|
+
* @param signature - DER-encoded ECDSA signature.
|
|
28
|
+
* @param data - Data that was signed.
|
|
29
|
+
* @returns Whether the signature is valid.
|
|
30
|
+
*/
|
|
31
|
+
function verifyEC2(cosePublicKey, signature, data) {
|
|
32
|
+
const alg = cosePublicKey.get(COSEKEYS.Alg);
|
|
33
|
+
const crv = cosePublicKey.get(COSEKEYS.Crv);
|
|
34
|
+
const xCoord = cosePublicKey.get(COSEKEYS.X);
|
|
35
|
+
const yCoord = cosePublicKey.get(COSEKEYS.Y);
|
|
36
|
+
if (typeof alg !== 'number') {
|
|
37
|
+
throw new Error('EC2 public key missing alg');
|
|
38
|
+
}
|
|
39
|
+
if (!xCoord || !yCoord) {
|
|
40
|
+
throw new Error('EC2 public key missing x or y coordinate');
|
|
41
|
+
}
|
|
42
|
+
const uncompressed = concatBytes([new Uint8Array([0x04]), xCoord, yCoord]);
|
|
43
|
+
switch (crv) {
|
|
44
|
+
case COSECRV.P256:
|
|
45
|
+
return p256.verify(signature, sha256(data), uncompressed);
|
|
46
|
+
case COSECRV.P384:
|
|
47
|
+
return p384.verify(signature, sha384(data), uncompressed);
|
|
48
|
+
case COSECRV.P521:
|
|
49
|
+
return p521.verify(signature, sha512(data), uncompressed);
|
|
50
|
+
default:
|
|
51
|
+
throw new Error(`Unsupported EC2 curve: ${crv}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Verify an OKP (Ed25519) signature using @noble/curves.
|
|
56
|
+
*
|
|
57
|
+
* @param cosePublicKey - COSE-encoded OKP public key.
|
|
58
|
+
* @param signature - Raw Ed25519 signature (64 bytes).
|
|
59
|
+
* @param data - Data that was signed.
|
|
60
|
+
* @returns Whether the signature is valid.
|
|
61
|
+
*/
|
|
62
|
+
function verifyOKP(cosePublicKey, signature, data) {
|
|
63
|
+
const alg = cosePublicKey.get(COSEKEYS.Alg);
|
|
64
|
+
const crv = cosePublicKey.get(COSEKEYS.Crv);
|
|
65
|
+
const xCoord = cosePublicKey.get(COSEKEYS.X);
|
|
66
|
+
if (alg !== COSEALG.EdDSA) {
|
|
67
|
+
throw new Error(`Unexpected OKP algorithm: ${String(alg)}`);
|
|
68
|
+
}
|
|
69
|
+
if (crv !== COSECRV.ED25519) {
|
|
70
|
+
throw new Error(`Unsupported OKP curve: ${String(crv)}`);
|
|
71
|
+
}
|
|
72
|
+
if (!xCoord) {
|
|
73
|
+
throw new Error('OKP public key missing x coordinate');
|
|
74
|
+
}
|
|
75
|
+
return ed25519.verify(signature, data, xCoord);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Verify an RSA signature using Web Crypto API.
|
|
79
|
+
*
|
|
80
|
+
* @param cosePublicKey - COSE-encoded RSA public key.
|
|
81
|
+
* @param signature - RSA PKCS#1 v1.5 signature.
|
|
82
|
+
* @param data - Data that was signed.
|
|
83
|
+
* @returns Whether the signature is valid.
|
|
84
|
+
*/
|
|
85
|
+
async function verifyRSA(cosePublicKey, signature, data) {
|
|
86
|
+
const alg = cosePublicKey.get(COSEKEYS.Alg);
|
|
87
|
+
const modulus = cosePublicKey.get(COSEKEYS.N);
|
|
88
|
+
const exponent = cosePublicKey.get(COSEKEYS.E);
|
|
89
|
+
if (typeof alg !== 'number') {
|
|
90
|
+
throw new Error('RSA public key missing alg');
|
|
91
|
+
}
|
|
92
|
+
if (!modulus || !exponent) {
|
|
93
|
+
throw new Error('RSA public key missing n or e');
|
|
94
|
+
}
|
|
95
|
+
let keyAlgorithmName;
|
|
96
|
+
let hashAlg;
|
|
97
|
+
let saltLength;
|
|
98
|
+
switch (alg) {
|
|
99
|
+
case COSEALG.RS1:
|
|
100
|
+
keyAlgorithmName = 'RSASSA-PKCS1-v1_5';
|
|
101
|
+
hashAlg = 'SHA-1';
|
|
102
|
+
break;
|
|
103
|
+
case COSEALG.RS256:
|
|
104
|
+
keyAlgorithmName = 'RSASSA-PKCS1-v1_5';
|
|
105
|
+
hashAlg = 'SHA-256';
|
|
106
|
+
break;
|
|
107
|
+
case COSEALG.RS384:
|
|
108
|
+
keyAlgorithmName = 'RSASSA-PKCS1-v1_5';
|
|
109
|
+
hashAlg = 'SHA-384';
|
|
110
|
+
break;
|
|
111
|
+
case COSEALG.RS512:
|
|
112
|
+
keyAlgorithmName = 'RSASSA-PKCS1-v1_5';
|
|
113
|
+
hashAlg = 'SHA-512';
|
|
114
|
+
break;
|
|
115
|
+
case COSEALG.PS256:
|
|
116
|
+
keyAlgorithmName = 'RSA-PSS';
|
|
117
|
+
hashAlg = 'SHA-256';
|
|
118
|
+
saltLength = 32;
|
|
119
|
+
break;
|
|
120
|
+
case COSEALG.PS384:
|
|
121
|
+
keyAlgorithmName = 'RSA-PSS';
|
|
122
|
+
hashAlg = 'SHA-384';
|
|
123
|
+
saltLength = 48;
|
|
124
|
+
break;
|
|
125
|
+
case COSEALG.PS512:
|
|
126
|
+
keyAlgorithmName = 'RSA-PSS';
|
|
127
|
+
hashAlg = 'SHA-512';
|
|
128
|
+
saltLength = 64;
|
|
129
|
+
break;
|
|
130
|
+
default:
|
|
131
|
+
throw new Error(`Unsupported RSA algorithm: ${alg}`);
|
|
132
|
+
}
|
|
133
|
+
const key = await globalThis.crypto.subtle.importKey('jwk', {
|
|
134
|
+
kty: 'RSA',
|
|
135
|
+
n: bytesToBase64URL(modulus),
|
|
136
|
+
e: bytesToBase64URL(exponent),
|
|
137
|
+
}, { name: keyAlgorithmName, hash: { name: hashAlg } }, false, ['verify']);
|
|
138
|
+
const verifyAlgorithm = keyAlgorithmName === 'RSA-PSS'
|
|
139
|
+
? { name: 'RSA-PSS', saltLength: saltLength }
|
|
140
|
+
: 'RSASSA-PKCS1-v1_5';
|
|
141
|
+
const signatureBytes = Uint8Array.from(signature);
|
|
142
|
+
const dataBytes = Uint8Array.from(data);
|
|
143
|
+
return globalThis.crypto.subtle.verify(verifyAlgorithm, key, signatureBytes, dataBytes);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Verify a WebAuthn signature using the appropriate algorithm based on
|
|
147
|
+
* the COSE key type.
|
|
148
|
+
*
|
|
149
|
+
* Uses @noble/curves for EC2 and OKP (synchronous, audited, handles DER
|
|
150
|
+
* natively). Falls back to Web Crypto API for RSA.
|
|
151
|
+
*
|
|
152
|
+
* @param opts - Options object.
|
|
153
|
+
* @param opts.cosePublicKey - COSE-encoded public key as a Map.
|
|
154
|
+
* @param opts.signature - The signature bytes.
|
|
155
|
+
* @param opts.data - The data that was signed.
|
|
156
|
+
* @returns Whether the signature is valid.
|
|
157
|
+
*/
|
|
158
|
+
export async function verifySignature(opts) {
|
|
159
|
+
const { cosePublicKey, signature, data } = opts;
|
|
160
|
+
const kty = getKeyType(cosePublicKey);
|
|
161
|
+
switch (kty) {
|
|
162
|
+
case COSEKTY.EC2:
|
|
163
|
+
return verifyEC2(cosePublicKey, signature, data);
|
|
164
|
+
case COSEKTY.OKP:
|
|
165
|
+
return verifyOKP(cosePublicKey, signature, data);
|
|
166
|
+
case COSEKTY.RSA:
|
|
167
|
+
return verifyRSA(cosePublicKey, signature, data);
|
|
168
|
+
default:
|
|
169
|
+
throw new Error(`Unsupported COSE key type: ${kty}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=verify-signature.mjs.map
|