forked from microsoft/webauthntest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fidoAttestation.js
249 lines (223 loc) · 8.6 KB
/
fidoAttestation.js
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
const {derToPEM} = require('./utils.js');
const jsrsasign = require('jsrsasign');
const crypto = require('crypto');
const cbor = require('cbor');
const fidoAttestation = {};
/**
* @typedef {import('./public/types').AuthenticatorData} AuthenticatorData
* @typedef {import('./public/types').AttestedCredentialData} AttestedCredentialData
* @typedef {import('./public/types').AttestationStatement} AttestationStatement
*/
/**
* Parses and verifies an attestation statement
* @param {*} attestationObject cbor decoded attestation object received from the authenticator
* @param {AuthenticatorData} authenticatorData
* @param {Buffer} clientDataHash
* @returns {AttestationStatement}
*/
fidoAttestation.parse = (attestationObject, authenticatorData, clientDataHash) => {
switch (attestationObject.fmt) {
case "tpm":
return parseTPMAttestation(attestationObject, authenticatorData, clientDataHash);
case "fido-u2f":
return parseU2FAttestation(attestationObject, authenticatorData, clientDataHash);
case "packed":
return parsePackedAttestation(attestationObject, authenticatorData, clientDataHash);
case "android-safetynet":
return parseAndroidSafetyNetAttestation(attestationObject, authenticatorData, clientDataHash);
case "android-key":
return parseAndroidKeyAttestation(attestationObject, authenticatorData, clientDataHash);
case "android-key":
return parseAppleAttestation(attestationObject, authenticatorData, clientDataHash);
case "none":
return parseNoneAttestation(attestationObject, authenticatorData, clientDataHash);
default:
return {
summary:attestationObject.fmt,
chainJSON: "none",
hex: cbor.encode(attestationObject.attStmt).toString('hex').toUpperCase()
};
}
}
/**
* Parses TPM attestation statement
* @param {*} attestationObject
* @param {AuthenticatorData} authenticatorData
* @param {Buffer} clientDataHash
* @returns {AttestationStatement}
*/
const parseTPMAttestation = (attestationObject, authenticatorData, clientDataHash) => {
return {
summary: "tpm (unverified)",
chainJSON: "none",
hex: cbor.encode(attestationObject.attStmt).toString('hex').toUpperCase()
}
};
/**
* Parses U2F attestation statement
* @param {*} attestationObject
* @param {AuthenticatorData} authenticatorData
* @param {Buffer} clientDataHash
* @returns {AttestationStatement}
*/
const parseU2FAttestation = (attestationObject, authenticatorData, clientDataHash) => {
const summary = "fido-u2f";
//Check that x5c has exactly one element and let attCert be that element.
if (attestationObject.attStmt.x5c.length !== 1)
throw new Error("Expected only one elementh in x5c");
const attCert = attestationObject.attStmt.x5c[0];
//Convert the COSE_KEY formatted credentialPublicKey (see Section 7 of [RFC8152]) to Raw ANSI X9.62 public key format
//Let publicKeyU2F be the concatenation 0x04 || x || y.
const publicKeyU2F = Buffer.concat([
Buffer.from('04', 'hex'),
Buffer.from(authenticatorData.attestedCredentialData.publicKey.x, 'base64'),
Buffer.from(authenticatorData.attestedCredentialData.publicKey.y, 'base64'),
]);
//Let verificationData be the concatenation of (0x00 || rpIdHash || clientDataHash || credentialId || publicKeyU2F)
const verificationData = Buffer.concat([
Buffer.from('00', 'hex'),
authenticatorData.rpIdHash,
clientDataHash,
authenticatorData.attestedCredentialData.credentialId,
publicKeyU2F
]);
//Verify the sig using verificationData and certificate public key per [SEC1].
const pem = derToPEM(attCert.toString('base64'));
const verify = crypto.createVerify('sha256');
verify.update(verificationData);
if (!verify.verify(pem, attestationObject.attStmt.sig)) {
throw new Error("Attestation signature did not verify");
}
const c = new jsrsasign.X509();
c.readCertPEM(pem);
const chainJSON = JSON.stringify([{
version: c.getVersion(),
subject: c.getSubjectString(),
issuer: c.getIssuerString()
}]);
const hex = cbor.encode(attestationObject.attStmt).toString('hex').toUpperCase();
return {
summary,
chainJSON,
hex
};
}
/**
* Parses packed attestation statement. Only x5c and alg=-7 is supported.
* @param {*} attestationObject
* @param {AuthenticatorData} authenticatorData
* @param {Buffer} clientDataHash
* @returns {AttestationStatement}
*/
const parsePackedAttestation = (attestationObject, authenticatorData, clientDataHash) => {
summary = "packed";
chainJSON: "none";
const hex = cbor.encode(attestationObject.attStmt).toString('hex').toUpperCase();
//https://www.w3.org/TR/webauthn/#packed-attestation
if (attestationObject.attStmt.x5c)
{
const chain = attestationObject.attStmt.x5c.map(x5c => {
const p = derToPEM(x5c.toString('base64'));
const c = new jsrsasign.X509();
c.readCertPEM(p);
return {
version: c.getVersion(),
subject: c.getSubjectString(),
issuer: c.getIssuerString(),
extAaguid: c.getExtInfo("1.3.6.1.4.1.45724.1.1.4")
};
});
chainJSON = JSON.stringify(chain);
//Verify that sig is a valid signature over the concatenation of
//authenticatorData and clientDataHash using the attestation public
//key in attestnCert with the algorithm specified in alg.
if (attestationObject.attStmt.alg == -7)
{
const attCert = attestationObject.attStmt.x5c[0];
const pem = derToPEM(attCert.toString('base64'));
const verify = crypto.createVerify('sha256');
verify.update(attestationObject.authData);
verify.update(clientDataHash);
if (!verify.verify(pem, attestationObject.attStmt.sig)) {
throw new Error("Attestation signature did not verify");
}
}
else if (attestationObject.attStmt.alg == -35)
{
const attCert = attestationObject.attStmt.x5c[0];
const pem = derToPEM(attCert.toString('base64'));
const verify = crypto.createVerify('sha384');
verify.update(attestationObject.authData);
verify.update(clientDataHash);
if (!verify.verify(pem, attestationObject.attStmt.sig)) {
throw new Error("Attestation signature did not verify");
}
}
}
else
{
summary = "packed (unverified)";
}
return {
summary,
chainJSON,
hex
};
}
/**
* Parses Android safetynet attestation statement.
* @param {*} attestationObject
* @param {AuthenticatorData} authenticatorData
* @param {Buffer} clientDataHash
* @returns {AttestationStatement}
*/
const parseAndroidSafetyNetAttestation = (attestationObject, authenticatorData, clientDataHash) => {
return {
summary: "android-safetynet (unverified)",
chainJSON: "none",
hex: cbor.encode(attestationObject.attStmt).toString('hex').toUpperCase()
}
};
/**
* Parses android key attestation statement.
* @param {*} attestationObject
* @param {AuthenticatorData} authenticatorData
* @param {Buffer} clientDataHash
* @returns {AttestationStatement}
*/
const parseAndroidKeyAttestation = (attestationObject, authenticatorData, clientDataHash) => {
return {
summary: "android-key (unverified)",
chainJSON: "none",
hex: cbor.encode(attestationObject.attStmt).toString('hex').toUpperCase()
}
};
/**
* Parses Apple attestation statement.
* @param {*} attestationObject
* @param {AuthenticatorData} authenticatorData
* @param {Buffer} clientDataHash
* @returns {AttestationStatement}
*/
const parseAppleAttestation = (attestationObject, authenticatorData, clientDataHash) => {
return {
summary: "apple (unverified)",
chainJSON: "none",
hex: cbor.encode(attestationObject.attStmt).toString('hex').toUpperCase()
}
};
/**
* Parses Apple attestation statement.
* @param {*} attestationObject
* @param {AuthenticatorData} authenticatorData
* @param {Buffer} clientDataHash
* @returns {AttestationStatement}
*/
const parseNoneAttestation = (attestationObject, authenticatorData, clientDataHash) => {
return {
summary: "none (unverified)",
chainJSON: "none",
hex: cbor.encode(attestationObject.attStmt).toString('hex').toUpperCase()
}
};
module.exports = fidoAttestation;