-
Notifications
You must be signed in to change notification settings - Fork 80
/
softpasskey.rs
588 lines (477 loc) · 23.2 KB
/
softpasskey.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#[cfg(doc)]
use crate::stubs::*;
use crate::authenticator_hashed::AuthenticatorBackendHashedClientData;
use crate::crypto::{compute_sha256, get_group};
use crate::error::WebauthnCError;
use crate::BASE64_ENGINE;
use base64::Engine;
use openssl::{bn, ec, hash, pkey, rand, sign};
use serde_cbor_2::value::Value;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::iter;
use base64urlsafedata::Base64UrlSafeData;
use webauthn_rs_proto::{
AllowCredentials, AuthenticationExtensionsClientOutputs, AuthenticatorAssertionResponseRaw,
AuthenticatorAttachment, AuthenticatorAttestationResponseRaw, PublicKeyCredential,
PublicKeyCredentialCreationOptions, PublicKeyCredentialRequestOptions,
RegisterPublicKeyCredential, RegistrationExtensionsClientOutputs, UserVerificationPolicy,
};
pub struct SoftPasskey {
tokens: HashMap<Vec<u8>, Vec<u8>>,
counter: u32,
falsify_uv: bool,
}
impl SoftPasskey {
pub fn new(falsify_uv: bool) -> Self {
SoftPasskey {
tokens: HashMap::new(),
counter: 0,
falsify_uv,
}
}
}
impl Default for SoftPasskey {
fn default() -> Self {
Self::new(false)
}
}
#[derive(Debug)]
pub struct U2FSignData {
key_handle: Vec<u8>,
counter: u32,
signature: Vec<u8>,
flags: u8,
}
impl AuthenticatorBackendHashedClientData for SoftPasskey {
fn perform_register(
&mut self,
client_data_json_hash: Vec<u8>,
options: PublicKeyCredentialCreationOptions,
_timeout_ms: u32,
) -> Result<RegisterPublicKeyCredential, WebauthnCError> {
// Let credTypesAndPubKeyAlgs be a new list whose items are pairs of PublicKeyCredentialType and a COSEAlgorithmIdentifier.
// Done in rust types.
// For each current of options.pubKeyCredParams:
// If current.type does not contain a PublicKeyCredentialType supported by this implementation, then continue.
// Let alg be current.alg.
// Append the pair of current.type and alg to credTypesAndPubKeyAlgs.
let cred_types_and_pub_key_algs: Vec<_> = options
.pub_key_cred_params
.iter()
.filter_map(|param| {
if param.type_ != "public-key" {
None
} else {
Some((param.type_.clone(), param.alg))
}
})
.collect();
trace!("Found -> {:x?}", cred_types_and_pub_key_algs);
// If credTypesAndPubKeyAlgs is empty and options.pubKeyCredParams is not empty, return a DOMException whose name is "NotSupportedError", and terminate this algorithm.
if cred_types_and_pub_key_algs.is_empty() {
return Err(WebauthnCError::NotSupported);
}
// Webauthn-rs doesn't support this yet.
/*
// Let clientExtensions be a new map and let authenticatorExtensions be a new map.
// If the extensions member of options is present, then for each extensionId → clientExtensionInput of options.extensions:
// If extensionId is not supported by this client platform or is not a registration extension, then continue.
// Set clientExtensions[extensionId] to clientExtensionInput.
// If extensionId is not an authenticator extension, then continue.
// Let authenticatorExtensionInput be the (CBOR) result of running extensionId’s client extension processing algorithm on clientExtensionInput. If the algorithm returned an error, continue.
// Set authenticatorExtensions[extensionId] to the base64url encoding of authenticatorExtensionInput.
*/
// Not required.
// If the options.signal is present and its aborted flag is set to true, return a DOMException whose name is "AbortError" and terminate this algorithm.
// Let issuedRequests be a new ordered set.
// Let authenticators represent a value which at any given instant is a set of client platform-specific handles, where each item identifies an authenticator presently available on this client platform at that instant.
// Start lifetimeTimer.
// While lifetimeTimer has not expired, perform the following actions depending upon lifetimeTimer, and the state and response for each authenticator in authenticators:
// If lifetimeTimer expires,
// For each authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove authenticator from issuedRequests.
// If the user exercises a user agent user-interface option to cancel the process,
// For each authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove authenticator from issuedRequests. Return a DOMException whose name is "NotAllowedError".
// If the options.signal is present and its aborted flag is set to true,
// For each authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove authenticator from issuedRequests. Then return a DOMException whose name is "AbortError" and terminate this algorithm.
// If an authenticator becomes available on this client device,
// If options.authenticatorSelection is present:
// If options.authenticatorSelection.authenticatorAttachment is present and its value is not equal to authenticator’s authenticator attachment modality, continue.
// If options.authenticatorSelection.requireResidentKey is set to true and the authenticator is not capable of storing a client-side-resident public key credential source, continue.
// If options.authenticatorSelection.userVerification is set to required and the authenticator is not capable of performing user verification, continue.
// Let userVerification be the effective user verification requirement for credential creation, a Boolean value, as follows. If options.authenticatorSelection.userVerification
// is set to required -> Let userVerification be true.
// is set to preferred
// If the authenticator
// is capable of user verification -> Let userVerification be true.
// is not capable of user verification -> Let userVerification be false.
// is set to discouraged -> Let userVerification be false.
// Let userPresence be a Boolean value set to the inverse of userVerification.
// Let excludeCredentialDescriptorList be a new list.
// For each credential descriptor C in options.excludeCredentials:
// If C.transports is not empty, and authenticator is connected over a transport not mentioned in C.transports, the client MAY continue.
// Otherwise, Append C to excludeCredentialDescriptorList.
// Invoke the authenticatorMakeCredential operation on authenticator with clientDataHash, options.rp, options.user, options.authenticatorSelection.requireResidentKey, userPresence, userVerification, credTypesAndPubKeyAlgs, excludeCredentialDescriptorList, and authenticatorExtensions as parameters.
// Append authenticator to issuedRequests.
// If an authenticator ceases to be available on this client device,
// Remove authenticator from issuedRequests.
// If any authenticator returns a status indicating that the user cancelled the operation,
// Remove authenticator from issuedRequests.
// For each remaining authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove it from issuedRequests.
// If any authenticator returns an error status equivalent to "InvalidStateError",
// Remove authenticator from issuedRequests.
// For each remaining authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove it from issuedRequests.
// Return a DOMException whose name is "InvalidStateError" and terminate this algorithm.
// If any authenticator returns an error status not equivalent to "InvalidStateError",
// Remove authenticator from issuedRequests.
// If any authenticator indicates success,
// Remove authenticator from issuedRequests.
// Let credentialCreationData be a struct whose items are:
// Let constructCredentialAlg be an algorithm that takes a global object global, and whose steps are:
// Let attestationObject be a new ArrayBuffer, created using global’s %ArrayBuffer%, containing the bytes of credentialCreationData.attestationObjectResult’s value.
// Let id be attestationObject.authData.attestedCredentialData.credentialId.
// Let pubKeyCred be a new PublicKeyCredential object associated with global whose fields are:
// For each remaining authenticator in issuedRequests invoke the authenticatorCancel operation on authenticator and remove it from issuedRequests.
// Return constructCredentialAlg and terminate this algorithm.
// For our needs, we let the u2f auth library handle the above, but currently it can't accept
// verified devices for u2f with ctap1/2. We may need to change u2f/authenticator library in the future.
// As a result this really limits our usage to certain device classes. This is why we implement
// this section in a seperate function call.
let (platform_attached, resident_key, user_verification) =
match &options.authenticator_selection {
Some(auth_sel) => {
let pa = auth_sel
.authenticator_attachment
.as_ref()
.map(|v| v == &AuthenticatorAttachment::Platform)
.unwrap_or(false);
let uv = auth_sel.user_verification == UserVerificationPolicy::Required;
(pa, auth_sel.require_resident_key, uv)
}
None => (false, false, false),
};
let rp_id_hash = compute_sha256(options.rp.id.as_bytes()).to_vec();
// =====
if user_verification && !self.falsify_uv {
error!("User Verification not supported by softtoken");
return Err(WebauthnCError::NotSupported);
}
if platform_attached {
error!("Platform Attachement not supported by softtoken");
return Err(WebauthnCError::NotSupported);
}
if resident_key {
error!("Resident Keys not supported by softtoken");
return Err(WebauthnCError::NotSupported);
}
// Generate a random credential id
let mut key_handle: Vec<u8> = Vec::with_capacity(32);
key_handle.resize_with(32, Default::default);
rand::rand_bytes(key_handle.as_mut_slice())?;
// Create a new key.
let ecgroup = get_group()?;
let eckey = ec::EcKey::generate(&ecgroup)?;
// Extract the public x and y coords.
let ecpub_points = eckey.public_key();
let mut bnctx = bn::BigNumContext::new()?;
let mut xbn = bn::BigNum::new()?;
let mut ybn = bn::BigNum::new()?;
ecpub_points.affine_coordinates_gfp(&ecgroup, &mut xbn, &mut ybn, &mut bnctx)?;
let mut public_key_x = Vec::with_capacity(32);
let mut public_key_y = Vec::with_capacity(32);
public_key_x.resize(32, 0);
public_key_y.resize(32, 0);
let xbnv = xbn.to_vec();
let ybnv = ybn.to_vec();
let (_pad, x_fill) = public_key_x.split_at_mut(32 - xbnv.len());
x_fill.copy_from_slice(&xbnv);
let (_pad, y_fill) = public_key_y.split_at_mut(32 - ybnv.len());
y_fill.copy_from_slice(&ybnv);
// Extract the DER cert for later
let ecpriv_der = eckey.private_key_to_der()?;
// Now setup to sign.
let pkey = pkey::PKey::from_ec_key(eckey)?;
let mut signer = sign::Signer::new(hash::MessageDigest::sha256(), &pkey)?;
// =====
// From the u2f response, we now need to assemble the attestation object now.
// cbor encode the public key. We already decomposed this, so just create
// the correct bytes.
let mut map = BTreeMap::new();
// KeyType -> EC2
map.insert(Value::Integer(1), Value::Integer(2));
// Alg -> ES256
map.insert(Value::Integer(3), Value::Integer(-7));
// Curve -> P-256
map.insert(Value::Integer(-1), Value::Integer(1));
// EC X coord
map.insert(Value::Integer(-2), Value::Bytes(public_key_x));
// EC Y coord
map.insert(Value::Integer(-3), Value::Bytes(public_key_y));
let pk_cbor = Value::Map(map);
let pk_cbor_bytes = serde_cbor_2::to_vec(&pk_cbor).map_err(|e| {
error!("PK CBOR -> {:x?}", e);
WebauthnCError::Cbor
})?;
let key_handle_len: u16 = u16::try_from(key_handle.len()).map_err(|e| {
error!("CBOR kh len is not u16 -> {:x?}", e);
WebauthnCError::Cbor
})?;
// combine aaGuid, KeyHandle, CborPubKey into a AttestedCredentialData. (acd)
let aaguid: [u8; 16] = [0; 16];
// make a 00 aaguid
let khlen_be_bytes = key_handle_len.to_be_bytes();
let acd_iter = aaguid
.iter()
.chain(khlen_be_bytes.iter())
.copied()
.chain(key_handle.iter().copied())
.chain(pk_cbor_bytes.iter().copied());
// set counter to 0 during create
// Combine rp_id_hash, flags, counter, acd, into authenticator data.
// The flags are always user_present, att present
let flags = if user_verification {
0b01000101
} else {
0b01000001
};
let authdata: Vec<u8> = rp_id_hash
.iter()
.copied()
.chain(iter::once(flags))
.chain(
// A 0 u32 counter
iter::repeat(0).take(4),
)
.chain(acd_iter)
.collect();
// 4.b. Verify that sig is a valid signature over the concatenation of authenticatorData and clientDataHash using the credential public key with alg.
let verification_data: Vec<u8> = authdata
.iter()
.chain(client_data_json_hash.iter())
.copied()
.collect();
// Do the signature
let signature = signer
.update(verification_data.as_slice())
.and_then(|_| signer.sign_to_vec())?;
let mut attest_map = BTreeMap::new();
/*
match options.attestation {
None | Some(AttestationConveyancePreference::None) => {
}
Some(AttestationConveyancePreference::Indirect)
| Some(AttestationConveyancePreference::Direct) => {
todo!();
}
}
*/
attest_map.insert(
Value::Text("fmt".to_string()),
Value::Text("packed".to_string()),
);
let mut att_stmt_map = BTreeMap::new();
att_stmt_map.insert(Value::Text("alg".to_string()), Value::Integer(-7));
att_stmt_map.insert(Value::Text("sig".to_string()), Value::Bytes(signature));
attest_map.insert(Value::Text("attStmt".to_string()), Value::Map(att_stmt_map));
attest_map.insert(Value::Text("authData".to_string()), Value::Bytes(authdata));
let ao = Value::Map(attest_map);
let ao_bytes = serde_cbor_2::to_vec(&ao).map_err(|e| {
error!("AO CBOR -> {:x?}", e);
WebauthnCError::Cbor
})?;
// Return a DOMException whose name is "NotAllowedError". In order to prevent information leak that could identify the user without consent, this step MUST NOT be executed before lifetimeTimer has expired. See §14.5 Registration Ceremony Privacy for details.
// Okay, now persist the token. We shouldn't fail from here.
self.tokens.insert(key_handle.clone(), ecpriv_der);
let rego = RegisterPublicKeyCredential {
id: BASE64_ENGINE.encode(&key_handle),
raw_id: key_handle.into(),
response: AuthenticatorAttestationResponseRaw {
attestation_object: ao_bytes.into(),
client_data_json: Base64UrlSafeData::new(),
transports: None,
},
type_: "public-key".to_string(),
extensions: RegistrationExtensionsClientOutputs::default(),
};
trace!("rego -> {:x?}", rego);
Ok(rego)
}
fn perform_auth(
&mut self,
client_data_json_hash: Vec<u8>,
options: PublicKeyCredentialRequestOptions,
timeout_ms: u32,
) -> Result<PublicKeyCredential, WebauthnCError> {
// Let clientExtensions be a new map and let authenticatorExtensions be a new map.
// If the extensions member of options is present, then for each extensionId → clientExtensionInput of options.extensions:
// ...
// This is where we deviate from the spec, since we aren't a browser.
let user_verification = options.user_verification == UserVerificationPolicy::Required;
let rp_id_hash = compute_sha256(options.rp_id.as_bytes()).to_vec();
let u2sd = self.perform_u2f_sign(
rp_id_hash.clone(),
client_data_json_hash,
timeout_ms.into(),
options.allow_credentials.as_slice(),
user_verification,
)?;
trace!("u2sd -> {:x?}", u2sd);
// Transform the result to webauthn
// The flags are set from the device.
let authdata: Vec<u8> = rp_id_hash
.iter()
.copied()
.chain(iter::once(u2sd.flags))
.chain(
// A 0 u32 counter
u2sd.counter.to_be_bytes().iter().copied(),
)
.collect();
Ok(PublicKeyCredential {
id: BASE64_ENGINE.encode(&u2sd.key_handle),
raw_id: u2sd.key_handle.into(),
response: AuthenticatorAssertionResponseRaw {
authenticator_data: authdata.into(),
client_data_json: Base64UrlSafeData::new(),
signature: u2sd.signature.into(),
user_handle: None,
},
type_: "public-key".to_string(),
extensions: AuthenticationExtensionsClientOutputs::default(),
})
}
}
pub trait U2FToken {
fn perform_u2f_sign(
&mut self,
// This is rp.id_hash
app_bytes: Vec<u8>,
// This is client_data_json_hash
chal_bytes: Vec<u8>,
// timeout from options
timeout_ms: u64,
// list of creds
allowed_credentials: &[AllowCredentials],
user_verification: bool,
) -> Result<U2FSignData, WebauthnCError>;
}
impl U2FToken for SoftPasskey {
fn perform_u2f_sign(
&mut self,
// This is rp.id_hash
app_bytes: Vec<u8>,
// This is client_data_json_hash
chal_bytes: Vec<u8>,
// timeout from options
_timeout_ms: u64,
// list of creds
allowed_credentials: &[AllowCredentials],
user_verification: bool,
) -> Result<U2FSignData, WebauthnCError> {
if user_verification && !self.falsify_uv {
error!("User Verification not supported by softtoken");
return Err(WebauthnCError::NotSupported);
}
let cred = allowed_credentials
.iter()
.filter_map(|ac| {
self.tokens
.get(ac.id.as_ref())
.map(|v| (ac.id.clone().into(), v.clone()))
})
.take(1)
.next();
let (key_handle, pkder) = if let Some((key_handle, pkder)) = cred {
(key_handle, pkder)
} else {
error!("Credential ID not found");
return Err(WebauthnCError::Internal);
};
debug!("Using -> {:?}", key_handle);
let eckey = ec::EcKey::private_key_from_der(pkder.as_slice())?;
let pkey = pkey::PKey::from_ec_key(eckey)?;
let mut signer = sign::Signer::new(hash::MessageDigest::sha256(), &pkey)?;
// Increment the counter.
self.counter += 1;
let counter = self.counter;
let flags = if user_verification {
0b00000101
} else {
0b00000001
};
let verification_data: Vec<u8> = app_bytes
.iter()
.chain(iter::once(&flags))
.chain(counter.to_be_bytes().iter())
.chain(chal_bytes.iter())
.copied()
.collect();
let signature = signer
.update(verification_data.as_slice())
.and_then(|_| signer.sign_to_vec())?;
Ok(U2FSignData {
key_handle,
counter,
signature,
flags,
})
}
}
#[cfg(test)]
mod tests {
use super::SoftPasskey;
use crate::prelude::{Url, WebauthnAuthenticator};
use std::time::Duration;
use webauthn_rs_core::WebauthnCore as Webauthn;
use webauthn_rs_proto::{
AttestationConveyancePreference, COSEAlgorithm, UserVerificationPolicy,
};
const AUTHENTICATOR_TIMEOUT: Duration = Duration::from_secs(60);
#[test]
fn webauthn_authenticator_wan_softpasskey_self_attest() {
let _ = tracing_subscriber::fmt::try_init();
let wan = Webauthn::new_unsafe_experts_only(
"https://localhost:8080/auth",
"localhost",
vec![url::Url::parse("https://localhost:8080").unwrap()],
AUTHENTICATOR_TIMEOUT,
None,
None,
);
let unique_id = [
158, 170, 228, 89, 68, 28, 73, 194, 134, 19, 227, 153, 107, 220, 150, 238,
];
let name = "william";
let builder = wan
.new_challenge_register_builder(&unique_id, name, name)
.unwrap()
.attestation(AttestationConveyancePreference::Direct)
.user_verification_policy(UserVerificationPolicy::Preferred);
let (chal, reg_state) = wan.generate_challenge_register(builder).unwrap();
info!("🍿 challenge -> {:x?}", chal);
let mut wa = WebauthnAuthenticator::new(SoftPasskey::new(true));
let r = wa
.do_registration(Url::parse("https://localhost:8080").unwrap(), chal)
.map_err(|e| {
error!("Error -> {:x?}", e);
e
})
.expect("Failed to register");
let cred = wan.register_credential(&r, ®_state, None).unwrap();
let (chal, auth_state) = wan
.new_challenge_authenticate_builder(vec![cred], None)
.and_then(|b| wan.generate_challenge_authenticate(b))
.unwrap();
let r = wa
.do_authentication(Url::parse("https://localhost:8080").unwrap(), chal)
.map_err(|e| {
error!("Error -> {:x?}", e);
e
})
.expect("Failed to auth");
let auth_res = wan
.authenticate_credential(&r, &auth_state)
.expect("webauth authentication denied");
info!("auth_res -> {:x?}", auth_res);
}
}