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
use std::convert::TryFrom;
use crate::crypto;
use crate::crypto::COSEContentType;
use crate::error::WebauthnError;
use crate::proto::{AttestedCredentialData, Credential};
use serde_cbor::{ObjectKey, Value};
use std::collections::BTreeMap;
use log::debug;
#[derive(Debug)]
pub(crate) enum AttestationFormat {
Packed,
TPM,
AndroidKey,
AndroidSafetyNet,
FIDOU2F,
None,
}
impl TryFrom<&str> for AttestationFormat {
type Error = WebauthnError;
fn try_from(a: &str) -> Result<AttestationFormat, Self::Error> {
match a {
"packed" => Ok(AttestationFormat::Packed),
"tpm" => Ok(AttestationFormat::TPM),
"android-key" => Ok(AttestationFormat::AndroidKey),
"android-safetynet" => Ok(AttestationFormat::AndroidSafetyNet),
"fido-u2f" => Ok(AttestationFormat::FIDOU2F),
"none" => Ok(AttestationFormat::None),
_ => Err(WebauthnError::AttestationNotSupported),
}
}
}
#[derive(Debug)]
pub enum AttestationType {
Basic(Credential, crypto::X509PublicKey),
Self_(Credential),
AttCa(
Credential,
crypto::X509PublicKey,
Vec<crypto::X509PublicKey>,
),
ECDAA,
None(Credential),
Uncertain(Credential),
}
fn is_matching_agorithm(
att_stmt_map: &BTreeMap<ObjectKey, Value>,
alg_from_auhenticator_data: &COSEContentType,
) -> bool {
att_stmt_map
.get(&serde_cbor::ObjectKey::String("alg".to_string()))
.and_then(|alg| alg.as_i64())
.map(|alg| alg == i64::from(alg_from_auhenticator_data))
.unwrap_or(false)
}
pub(crate) fn verify_packed_attestation(
att_stmt: &serde_cbor::Value,
acd: &AttestedCredentialData,
auth_data_bytes: Vec<u8>,
client_data_hash: &Vec<u8>,
counter: u32,
) -> Result<AttestationType, WebauthnError> {
let att_stmt_map = att_stmt
.as_object()
.ok_or(WebauthnError::AttestationStatementMapInvalid)?;
let x5c_key = &serde_cbor::ObjectKey::String("x5c".to_string());
let ecdaa_key_id_key = &serde_cbor::ObjectKey::String("ecdaaKeyId".to_string());
match (
att_stmt_map.get(x5c_key),
att_stmt_map.get(ecdaa_key_id_key),
) {
(Some(x5c), _) => {
debug!("x5c");
let credential_public_key = crypto::COSEKey::try_from(&acd.credential_pk)?;
let x5c_array_ref = x5c
.as_array()
.ok_or(WebauthnError::AttestationStatementX5CInvalid)?;
let arr_x509: Result<Vec<_>, _> = x5c_array_ref
.iter()
.map(|values| {
values
.as_bytes()
.ok_or(WebauthnError::AttestationStatementX5CInvalid)
.and_then(|b| crypto::X509PublicKey::try_from(b.as_slice()))
})
.collect();
let mut arr_x509 = arr_x509?;
if arr_x509.len() == 0 {
return Err(WebauthnError::AttestationStatementX5CInvalid);
}
let attestn_cert = arr_x509.remove(0);
let verification_data: Vec<u8> = auth_data_bytes
.iter()
.chain(client_data_hash.iter())
.map(|b| *b)
.collect();
let is_valid_signature = att_stmt_map
.get(&serde_cbor::ObjectKey::String("sig".to_string()))
.and_then(|s| s.as_bytes())
.ok_or(WebauthnError::AttestationStatementSigMissing)
.and_then(|sig| attestn_cert.verify_signature(&sig, &verification_data))?;
if !is_valid_signature {
return Err(WebauthnError::AttestationStatementSigInvalid);
}
attestn_cert.assert_packed_attest_req()?;
if let Some(aaguid) = attestn_cert.get_fido_gen_ce_aaguid() {
if acd.aaguid != aaguid {
return Err(WebauthnError::AttestationCertificateAAGUIDMismatch);
}
}
Ok(AttestationType::Basic(
Credential::new(acd, credential_public_key, counter),
attestn_cert,
))
}
(None, Some(_ecdaa_key_id)) => {
debug!("_ecdaa_key_id");
Err(WebauthnError::AttestationNotSupported)
}
(None, None) => {
let credential_public_key = crypto::COSEKey::try_from(&acd.credential_pk)?;
if !is_matching_agorithm(att_stmt_map, &credential_public_key.type_) {
return Err(WebauthnError::AttestationStatementAlgMismatch);
}
let verification_data: Vec<u8> = auth_data_bytes
.iter()
.chain(client_data_hash.iter())
.map(|b| *b)
.collect();
let is_valid_signature = att_stmt_map
.get(&serde_cbor::ObjectKey::String("sig".to_string()))
.and_then(|s| s.as_bytes())
.ok_or(WebauthnError::AttestationStatementSigMissing)
.and_then(|sig| credential_public_key.verify_signature(&sig, &verification_data))?;
if !is_valid_signature {
return Err(WebauthnError::AttestationStatementSigInvalid);
}
Ok(AttestationType::Self_(Credential::new(
acd,
credential_public_key,
counter,
)))
}
}
}
pub(crate) fn verify_fidou2f_attestation(
att_stmt: &serde_cbor::Value,
acd: &AttestedCredentialData,
client_data_hash: &Vec<u8>,
rp_id_hash: &Vec<u8>,
counter: u32,
) -> Result<AttestationType, WebauthnError> {
let att_stmt_map = att_stmt
.as_object()
.ok_or(WebauthnError::AttestationStatementMapInvalid)?;
let x5c = att_stmt_map
.get(&serde_cbor::ObjectKey::String("x5c".to_string()))
.ok_or(WebauthnError::AttestationStatementX5CMissing)?;
let sig_value = att_stmt_map
.get(&serde_cbor::ObjectKey::String("sig".to_string()))
.ok_or(WebauthnError::AttestationStatementSigMissing)?;
let sig = sig_value
.as_bytes()
.ok_or(WebauthnError::AttestationStatementSigMissing)?;
let att_cert_array = x5c
.as_array()
.ok_or(WebauthnError::AttestationStatementX5CInvalid)?;
if att_cert_array.len() != 1 {
return Err(WebauthnError::AttestationStatementX5CInvalid);
}
let att_cert_bytes = att_cert_array
.first()
.ok_or(WebauthnError::AttestationStatementX5CInvalid)?;
let att_cert = att_cert_bytes
.as_bytes()
.ok_or(WebauthnError::AttestationStatementX5CInvalid)?;
let cerificate_public_key = crypto::X509PublicKey::try_from(att_cert.as_slice())?;
if !(cerificate_public_key.is_secp256r1()?) {
return Err(WebauthnError::CertificatePublicKeyInvalid);
}
let credential_public_key = crypto::COSEKey::try_from(&acd.credential_pk)?;
let public_key_u2f = credential_public_key.get_alg_key_ecc_x962_raw()?;
let r: [u8; 1] = [0x00];
let verification_data: Vec<u8> = (&r)
.iter()
.chain(rp_id_hash.iter())
.chain(client_data_hash.iter())
.chain(acd.credential_id.iter())
.chain(public_key_u2f.iter())
.map(|b| *b)
.collect();
let verified = cerificate_public_key.verify_signature(&sig, &verification_data)?;
if !verified {
log::error!("signature verification failed!");
return Err(WebauthnError::AttestationStatementSigInvalid);
}
let credential = Credential::new(acd, credential_public_key, counter);
Ok(AttestationType::Basic(credential, cerificate_public_key))
}