oauth2-passkey 0.6.1

OAuth2 and Passkey authentication library for Rust web applications
Documentation
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
use super::*;
use ciborium::value::Value;
use ring::digest;

// Test helper functions for creating authentication data and attestation statements

/// Creates a basic auth_data with minimal requirements for testing
fn create_basic_auth_data() -> Vec<u8> {
    let mut auth_data = Vec::new();

    // RP ID hash (32 bytes) - SHA-256 of "example.com"
    let rp_id_hash = digest::digest(&digest::SHA256, "example.com".as_bytes());
    auth_data.extend_from_slice(rp_id_hash.as_ref());

    // Flags (user present, user verified, attested credential data)
    auth_data.push(0x01 | 0x04 | 0x40);

    // Sign count (4 bytes, big-endian)
    auth_data.extend_from_slice(&[0x00, 0x00, 0x00, 0x01]);

    auth_data
}

/// Creates auth_data with specified AAGUID
fn create_auth_data_with_aaguid(aaguid: &[u8; 16]) -> Vec<u8> {
    let mut auth_data = create_basic_auth_data();

    // Add AAGUID (16 bytes)
    auth_data.extend_from_slice(aaguid);

    // Add credential ID length (2 bytes, big-endian) and credential ID (16 bytes)
    auth_data.extend_from_slice(&[0x00, 0x10]); // Length: 16 bytes
    auth_data.extend_from_slice(&[0x02; 16]); // Credential ID

    // Add CBOR-encoded public key
    let public_key_entries = vec![
        (Value::Integer(1i64.into()), Value::Integer(2i64.into())), // kty: EC2
        (Value::Integer(3i64.into()), Value::Integer((-7i64).into())), // alg: ES256
        (Value::Integer((-1i64).into()), Value::Integer(1i64.into())), // crv: P-256
        (Value::Integer((-2i64).into()), Value::Bytes(vec![0x02; 32])), // x coordinate
        (Value::Integer((-3i64).into()), Value::Bytes(vec![0x03; 32])), // y coordinate
    ];

    let public_key = Value::Map(public_key_entries);
    let mut public_key_bytes = Vec::new();
    ciborium::ser::into_writer(&public_key, &mut public_key_bytes).unwrap();
    auth_data.extend_from_slice(&public_key_bytes);

    auth_data
}

/// Creates auth_data without attested credential data flag
fn create_auth_data_no_attested_cred() -> Vec<u8> {
    let mut auth_data = Vec::new();

    // RP ID hash (32 bytes)
    let rp_id_hash = digest::digest(&digest::SHA256, "example.com".as_bytes());
    auth_data.extend_from_slice(rp_id_hash.as_ref());

    // Flags without attested credential data (0x40)
    auth_data.push(0x01 | 0x04); // Only user present and user verified

    // Sign count (4 bytes, big-endian)
    auth_data.extend_from_slice(&[0x00, 0x00, 0x00, 0x01]);

    // Add AAGUID (16 bytes)
    auth_data.extend_from_slice(&[0x01; 16]);

    auth_data
}

/// Creates a client data hash for testing
fn create_client_data_hash() -> Vec<u8> {
    let client_data = r#"{"type":"webauthn.create","challenge":"dGVzdGNoYWxsZW5nZQ","origin":"https://example.com"}"#;
    let hash = digest::digest(&digest::SHA256, client_data.as_bytes());
    hash.as_ref().to_vec()
}

/// Creates an attestation statement with algorithm and signature
fn create_att_stmt(alg: i64, sig: &[u8]) -> Vec<(CborValue, CborValue)> {
    vec![
        (Value::Text("alg".to_string()), Value::Integer(alg.into())),
        (Value::Text("sig".to_string()), Value::Bytes(sig.to_vec())),
    ]
}

/// Creates an attestation statement with x5c certificate chain
fn create_att_stmt_with_x5c(
    alg: i64,
    sig: &[u8],
    cert_bytes: Vec<u8>,
) -> Vec<(CborValue, CborValue)> {
    let mut att_stmt = create_att_stmt(alg, sig);
    let certs = vec![Value::Bytes(cert_bytes)];
    att_stmt.push((Value::Text("x5c".to_string()), Value::Array(certs)));
    att_stmt
}

/// Creates an attestation statement with ecdaaKeyId
fn create_att_stmt_with_ecdaa(
    alg: i64,
    sig: &[u8],
    key_id: Vec<u8>,
) -> Vec<(CborValue, CborValue)> {
    let mut att_stmt = create_att_stmt(alg, sig);
    att_stmt.push((Value::Text("ecdaaKeyId".to_string()), Value::Bytes(key_id)));
    att_stmt
}

/// Creates a minimal dummy certificate for testing parsing errors
fn create_dummy_cert() -> Vec<u8> {
    // Invalid certificate that will cause parsing to fail
    vec![0x30, 0x82, 0x01, 0x01]
}

/// Creates an empty certificate chain for testing
fn create_empty_cert_chain() -> Vec<u8> {
    vec![]
}

// Tests for main verify_packed_attestation function

#[test]
fn test_verify_packed_attestation_unsupported_alg() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04];
    let att_stmt = create_att_stmt(-8, &sig); // Unsupported algorithm

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Unsupported or unrecognized algorithm"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_packed_attestation_ecdaa_not_supported() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04];
    let key_id = vec![0x01, 0x02, 0x03, 0x04];
    let att_stmt = create_att_stmt_with_ecdaa(ES256_ALG, &sig, key_id);

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("ECDAA attestation not supported"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_packed_attestation_both_x5c_and_ecdaa() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04];

    let mut att_stmt = create_att_stmt_with_x5c(ES256_ALG, &sig, create_dummy_cert());
    att_stmt.push((
        Value::Text("ecdaaKeyId".to_string()),
        Value::Bytes(vec![0x01, 0x02, 0x03, 0x04]),
    ));

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("both x5c and ecdaaKeyId present"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_packed_attestation_invalid_cert() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04];
    let att_stmt = create_att_stmt_with_x5c(ES256_ALG, &sig, create_dummy_cert());

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Failed to parse attestation certificate"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_packed_attestation_empty_cert_chain() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04];
    let att_stmt = create_att_stmt_with_x5c(ES256_ALG, &sig, create_empty_cert_chain());

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Failed to parse attestation certificate"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_packed_attestation_malformed_x5c() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04];

    // Create att_stmt with malformed x5c array containing non-bytes
    let mut att_stmt = create_att_stmt(ES256_ALG, &sig);
    let malformed_certs = vec![Value::Text("not_bytes".to_string())];
    att_stmt.push((
        Value::Text("x5c".to_string()),
        Value::Array(malformed_certs),
    ));

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    // Should fall through to self-attestation path since x5c_opt remains None
    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(
            msg.contains("No attested credential data")
                || msg.contains("Invalid public key CBOR")
                || msg.contains("Self attestation signature verification failed")
        );
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_packed_attestation_self_attestation_no_cred_data() {
    let auth_data = create_auth_data_no_attested_cred();
    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04];
    let att_stmt = create_att_stmt(ES256_ALG, &sig);

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("No attested credential data in self attestation"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_packed_attestation_self_attestation_invalid_sig() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04]; // Invalid signature
    let att_stmt = create_att_stmt(ES256_ALG, &sig);

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Self attestation signature verification failed"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

// Tests for verify_self_attestation function

#[test]
fn test_verify_self_attestation_missing_attested_cred_flag() {
    let auth_data = create_auth_data_no_attested_cred();
    let signed_data = vec![0x01, 0x02, 0x03];
    let signature = vec![0x04, 0x05, 0x06];

    let result = verify_self_attestation(&auth_data, &signed_data, &signature);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("No attested credential data in self attestation"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_self_attestation_truncated_auth_data() {
    // Create auth_data that's too short to contain credential data
    let mut auth_data = create_basic_auth_data();
    // The basic auth data already has flags, so we need to modify the flag at position 32
    auth_data[32] |= 0x40; // Set attested credential data flag
    auth_data.extend_from_slice(&[0x01; 16]); // AAGUID
    // Missing credential ID length and data - total length = 54 bytes

    let signed_data = vec![0x01, 0x02, 0x03];
    let signature = vec![0x04, 0x05, 0x06];

    let result = verify_self_attestation(&auth_data, &signed_data, &signature);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Auth data too short for credential ID length"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_self_attestation_invalid_cbor() {
    let mut auth_data = create_basic_auth_data();
    auth_data[32] |= 0x40; // Set attested credential data flag
    auth_data.extend_from_slice(&[0x01; 16]); // AAGUID
    auth_data.extend_from_slice(&[0x00, 0x10]); // Cred ID length
    auth_data.extend_from_slice(&[0x02; 16]); // Cred ID
    auth_data.extend_from_slice(&[0xFF, 0xFF, 0xFF]); // Invalid CBOR

    let signed_data = vec![0x01, 0x02, 0x03];
    let signature = vec![0x04, 0x05, 0x06];

    let result = verify_self_attestation(&auth_data, &signed_data, &signature);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Invalid public key CBOR"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

// Tests for verify_certificate_chain function

#[test]
fn test_verify_certificate_chain_empty() {
    let x5c: Vec<Vec<u8>> = vec![];
    let result = verify_certificate_chain(&x5c);
    assert!(result.is_ok());
}

#[test]
fn test_verify_certificate_chain_invalid_cert() {
    let x5c = vec![create_dummy_cert()];
    let result = verify_certificate_chain(&x5c);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Failed to parse certificate in chain"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_certificate_chain_multiple_invalid_certs() {
    let x5c = vec![create_dummy_cert(), create_dummy_cert()];
    let result = verify_certificate_chain(&x5c);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Failed to parse certificate in chain"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

// Tests for verify_packed_attestation_cert function

#[test]
fn test_verify_packed_attestation_cert_with_dummy_data() {
    // This test verifies that the function rejects invalid certificate data
    // We can't easily create a valid X509Certificate for testing without external dependencies
    // But we can test error handling paths

    let _auth_data = create_auth_data_with_aaguid(&[0x01; 16]);

    // Since we can't easily create a valid X509Certificate instance without
    // significant test certificate infrastructure, we document that this
    // function requires valid certificate data to test properly.

    // The function is tested indirectly through verify_packed_attestation tests
    // that exercise the certificate parsing and validation code paths.

    // For comprehensive testing, we would need:
    // 1. Valid test certificates with proper FIDO extensions
    // 2. Certificates with CA bit set for testing CA rejection
    // 3. Certificates with AAGUID extensions for AAGUID validation
    // 4. Test certificate chains for expiration testing
}

// Additional edge case tests

#[test]
fn test_verify_packed_attestation_missing_alg() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();

    // Create att_stmt without 'alg' field
    let att_stmt = vec![(
        Value::Text("sig".to_string()),
        Value::Bytes(vec![0x01, 0x02, 0x03, 0x04]),
    )];

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    // Should fail in get_sig_from_stmt() helper function
}

#[test]
fn test_verify_packed_attestation_missing_sig() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();

    // Create att_stmt without 'sig' field
    let att_stmt = vec![(
        Value::Text("alg".to_string()),
        Value::Integer(ES256_ALG.into()),
    )];

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    // Should fail in get_sig_from_stmt() helper function
}

#[test]
fn test_verify_packed_attestation_empty_att_stmt() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();
    let att_stmt: Vec<(CborValue, CborValue)> = vec![];

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    // Should fail in get_sig_from_stmt() helper function
}

#[test]
fn test_verify_packed_attestation_x5c_empty_array() {
    let auth_data = create_auth_data_with_aaguid(&[0x01; 16]);
    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04];

    let mut att_stmt = create_att_stmt(ES256_ALG, &sig);
    let empty_certs: Vec<Value> = vec![];
    att_stmt.push((Value::Text("x5c".to_string()), Value::Array(empty_certs)));

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    // Should fall through to self-attestation path since x5c_opt remains None
    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Self attestation signature verification failed"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_packed_attestation_large_credential_id() {
    let mut auth_data = create_basic_auth_data();

    // Add AAGUID
    auth_data.extend_from_slice(&[0x01; 16]);

    // Add large credential ID length (65535 bytes) but limited auth_data
    auth_data.extend_from_slice(&[0xFF, 0xFF]);

    // Don't add the actual credential ID data (auth_data would be massive)
    // This should cause an error when parsing due to insufficient data

    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04];
    let att_stmt = create_att_stmt(ES256_ALG, &sig);

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Auth data too short for credential ID"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}

#[test]
fn test_verify_packed_attestation_zero_credential_id_length() {
    let mut auth_data = create_basic_auth_data();

    // Add AAGUID
    auth_data.extend_from_slice(&[0x01; 16]);

    // Add zero credential ID length
    auth_data.extend_from_slice(&[0x00, 0x00]);

    // Add CBOR-encoded public key immediately after
    let public_key_entries = vec![
        (Value::Integer(1i64.into()), Value::Integer(2i64.into())),
        (Value::Integer(3i64.into()), Value::Integer((-7i64).into())),
        (Value::Integer((-1i64).into()), Value::Integer(1i64.into())),
        (Value::Integer((-2i64).into()), Value::Bytes(vec![0x02; 32])),
        (Value::Integer((-3i64).into()), Value::Bytes(vec![0x03; 32])),
    ];

    let public_key = Value::Map(public_key_entries);
    let mut public_key_bytes = Vec::new();
    ciborium::ser::into_writer(&public_key, &mut public_key_bytes).unwrap();
    auth_data.extend_from_slice(&public_key_bytes);

    let client_data_hash = create_client_data_hash();
    let sig = vec![0x01, 0x02, 0x03, 0x04];
    let att_stmt = create_att_stmt(ES256_ALG, &sig);

    let result = verify_packed_attestation(&auth_data, &client_data_hash, &att_stmt);

    assert!(result.is_err());
    if let Err(PasskeyError::Verification(msg)) = result {
        assert!(msg.contains("Self attestation signature verification failed"));
    } else {
        panic!("Expected PasskeyError::Verification");
    }
}