Skip to main content

webauthn_kit/
attestation.rs

1//! Attestation statement parsing and verification (WebAuthn Level 2 §8).
2//!
3//! Tranche 1 implements the three formats that cover the overwhelming
4//! majority of real registrations:
5//!
6//! - **`none`** (§8.1): no attestation statement; the ceremony reduces to
7//!   proving possession of the credential private key. Enforced strictly:
8//!   the statement must be an empty CBOR map ([`AttestationFormat::None`]).
9//! - **`packed`** (§8.2): self-attestation (signature with the credential
10//!   key) or basic/AttCA attestation (signature with the first certificate's
11//!   key, optionally chained to a configured trust anchor).
12//! - **`fido-u2f`** (§8.6): the legacy FIDO U2F attestation, verified over
13//!   the U2F transport encoding with exactly one attestation certificate.
14//!
15//! # Trust levels
16//!
17//! [`TrustLevel`] reports what a *verified* attestation actually proves:
18//!
19//! - [`TrustLevel::None`]: key possession only (`none` format).
20//! - [`TrustLevel::SelfAttested`]: the credential key signed the statement
21//!   (packed self-attestation) — possession, not provenance.
22//! - [`TrustLevel::BasicAtt`]: a certificate chain internally consistent and
23//!   rooted at a self-signed certificate, but **not** anchored to any RP-
24//!   configured trust anchor. Anyone can mint such a chain; treat exactly
25//!   like self-attestation. A warning is always emitted (see
26//!   [`warnings::UNANCHORED_CHAIN`]).
27//! - [`TrustLevel::AttCa`]: the chain terminates at a caller-configured
28//!   trust anchor — the only level that attests device provenance.
29//!
30//! # Security notes / threat assumptions
31//!
32//! - Every statement field arrives from the hostile client. Parsing is total:
33//!   malformed input yields `Err`, never a panic (fuzz-tested).
34//! - Signature checks are fail-closed: any unexpected certificate property
35//!   (unsupported algorithm, non-v3, expired, non-CA intermediate, AAGUID
36//!   extension mismatch) aborts verification.
37//! - Trust-anchor matching is caller policy: `AttestationPolicy::trust_anchors`
38//!   empty accepts unanchored chains *flagged* as [`TrustLevel::BasicAtt`]
39//!   with [`warnings::UNANCHORED_CHAIN`] — callers enforcing provenance MUST
40//!   configure anchors and inspect [`AttestationResult::trust_level`].
41//! - Certificate revocation (OCSP/CRL) is out of scope; see THREAT-MODEL.md.
42//!
43//! # Requirements
44//! REQ-WA-119, REQ-WA-120, REQ-WA-121, REQ-WA-122, REQ-WA-123, REQ-WA-124,
45//! REQ-WA-125, REQ-WA-126, REQ-WA-127, REQ-WA-128, REQ-WA-129, REQ-WA-130,
46//! REQ-WA-132
47
48use std::time::{SystemTime, UNIX_EPOCH};
49
50use ciborium::Value;
51use x509_parser::prelude::*;
52
53use crate::crypto::{
54    cbor_bytes, cbor_map_entries, verify_cose_signature, CosePublicKey, COSE_ALG_ES256,
55    COSE_ALG_RS256,
56};
57use crate::error::WebauthnError;
58
59// ---------------------------------------------------------------------------
60// Public API types
61// ---------------------------------------------------------------------------
62
63/// WebAuthn attestation statement format identifier (WebAuthn L2 §5.1.4).
64///
65/// Parsed from the `fmt` string of the attestation object. [`AttestationFormat::Unknown`]
66/// exists so a registration can be accepted under the documented
67/// `allow_unknown_formats` escape hatch while still reporting honestly that
68/// its statement was **not** verified.
69#[derive(Debug, Clone, PartialEq, Eq)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize))]
71pub enum AttestationFormat {
72    /// `fmt = "none"`: no attestation statement.
73    None,
74    /// `fmt = "packed"`: self-attestation or basic/AttCA attestation.
75    Packed,
76    /// `fmt = "fido-u2f"`: legacy FIDO U2F attestation.
77    FidoU2f,
78    /// Any other `fmt` value (accepted only via the `allow_unknown_formats`
79    /// escape hatch; the statement is never verified).
80    Unknown,
81}
82
83impl AttestationFormat {
84    /// Parse an attestation-object `fmt` string.
85    pub fn from_fmt(fmt: &str) -> Self {
86        match fmt {
87            "none" => AttestationFormat::None,
88            "packed" => AttestationFormat::Packed,
89            "fido-u2f" => AttestationFormat::FidoU2f,
90            _ => AttestationFormat::Unknown,
91        }
92    }
93
94    /// Canonical `fmt` string for this format (unknown formats report
95    /// `"unknown"`; see [`AttestationFormat::Unknown`]).
96    pub fn as_fmt(&self) -> &'static str {
97        match self {
98            AttestationFormat::None => "none",
99            AttestationFormat::Packed => "packed",
100            AttestationFormat::FidoU2f => "fido-u2f",
101            AttestationFormat::Unknown => "unknown",
102        }
103    }
104}
105
106/// What a verified attestation proves about the credential's provenance.
107///
108/// Ordered from weakest to strongest. See the module docs for the exact
109/// semantics — in particular [`TrustLevel::BasicAtt`] chains are *not*
110/// anchored to a configured trust anchor and must be treated as
111/// [`TrustLevel::SelfAttested`] by policy.
112#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
113#[cfg_attr(feature = "serde", derive(serde::Serialize))]
114pub enum TrustLevel {
115    /// No attestation claim (`none` format): key possession only.
116    None,
117    /// The credential key itself signed the statement (packed
118    /// self-attestation): key possession only.
119    SelfAttested,
120    /// A syntactically valid, internally consistent certificate chain rooted
121    /// at a self-signed certificate that is **not** an RP-configured trust
122    /// anchor. Equivalent in trust to [`TrustLevel::SelfAttested`]; always
123    /// accompanied by [`warnings::UNANCHORED_CHAIN`].
124    BasicAtt,
125    /// The chain terminates at an RP-configured trust anchor: real device
126    /// provenance (AttCA / basic attestation with a trusted root).
127    AttCa,
128}
129
130/// Warning strings emitted into [`AttestationResult::warnings`].
131///
132/// Warnings are stable string constants so callers can match on them.
133pub mod warnings {
134    /// A certificate chain was accepted without any configured trust anchor;
135    /// its provenance is equivalent to self-attestation.
136    pub const UNANCHORED_CHAIN: &str =
137        "attestation chain accepted without a configured trust anchor; \
138         treat as self-attested";
139    /// An unknown attestation format was accepted via the
140    /// `allow_unknown_formats` escape hatch; its statement was NOT verified.
141    pub const UNVERIFIED_FORMAT: &str =
142        "unknown attestation format accepted via allow_unknown_formats; \
143         statement was not verified";
144}
145
146/// Result of attestation verification for a registration.
147#[derive(Debug, Clone)]
148#[cfg_attr(feature = "serde", derive(serde::Serialize))]
149pub struct AttestationResult {
150    /// The attestation format that was used.
151    pub format: AttestationFormat,
152    /// AAGUID from attested credential data (always present during
153    /// registration; U2F authenticators report the all-zero AAGUID).
154    pub aaguid: Option<[u8; 16]>,
155    /// What the verified attestation proves. Callers enforcing device
156    /// provenance MUST require [`TrustLevel::AttCa`].
157    pub trust_level: TrustLevel,
158    /// Trust-relevant warnings (e.g. [`warnings::UNANCHORED_CHAIN`]).
159    pub warnings: Vec<&'static str>,
160}
161
162/// Caller policy for attestation verification.
163///
164/// # Security notes
165///
166/// - `trust_anchors` empty means *any* internally-consistent, self-rooted
167///   chain is accepted at [`TrustLevel::BasicAtt`] with
168///   [`warnings::UNANCHORED_CHAIN`] — a deliberate flag-not-block default so
169///   unmodified integrations keep working, documented as trust-weakening.
170///   Configure your attestation root certificates here to upgrade verified
171///   chains to [`TrustLevel::AttCa`].
172/// - `allow_unknown_formats = true` is an explicit trust-weakening escape
173///   hatch: registrations with unrecognized `fmt` values are accepted with
174///   [`warnings::UNVERIFIED_FORMAT`] instead of rejected. Leave it `false`
175///   unless you know you need it.
176#[derive(Debug, Clone, Default)]
177pub struct AttestationPolicy {
178    /// DER-encoded X.509 attestation root (trust anchor) certificates.
179    /// Empty accepts any self-rooted chain, flagged (see struct docs).
180    pub trust_anchors: Vec<Vec<u8>>,
181    /// Accept unknown attestation formats with a warning instead of
182    /// rejecting them (trust-weakening; see struct docs).
183    pub allow_unknown_formats: bool,
184}
185
186impl AttestationPolicy {
187    /// Strict policy: no trust anchors, unknown formats rejected.
188    ///
189    /// This still verifies `none`/`packed`/`fido-u2f` self-attestation and
190    /// unanchored chains (flagged); add `trust_anchors` to enforce
191    /// provenance ([`TrustLevel::AttCa`] only).
192    pub fn strict() -> Self {
193        Self::default()
194    }
195}
196
197/// Parsed attestation statement (CBOR `attStmt` map).
198#[derive(Debug, Clone, PartialEq, Eq)]
199pub(crate) enum AttestationStatement {
200    /// Empty statement (`none` format).
201    None,
202    /// Packed statement: COSE algorithm, signature, optional cert chain.
203    Packed {
204        /// COSE algorithm identifier used for `sig`.
205        alg: i32,
206        /// Signature over `authData || clientDataHash`.
207        sig: Vec<u8>,
208        /// Optional X.509 certificate chain (DER), leaf first.
209        x5c: Vec<Vec<u8>>,
210    },
211    /// FIDO U2F statement: signature plus exactly one certificate.
212    FidoU2f {
213        /// Signature over the U2F registration message.
214        sig: Vec<u8>,
215        /// The single attestation certificate (DER).
216        cert: Vec<u8>,
217    },
218}
219
220// ---------------------------------------------------------------------------
221// OID constants (compared as DER-encoded bytes via `Oid::as_bytes`)
222// ---------------------------------------------------------------------------
223
224/// OID 1.2.840.113549.1.1.11 — sha256WithRSAEncryption.
225const OID_SIG_SHA256_WITH_RSA: &[u8] = &[0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B];
226/// OID 1.2.840.10045.4.3.2 — ecdsa-with-SHA256.
227const OID_SIG_ECDSA_WITH_SHA256: &[u8] = &[0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02];
228/// OID 1.2.840.113549.1.1.1 — rsaEncryption.
229const OID_ALG_RSA_ENCRYPTION: &[u8] = &[0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01];
230/// OID 1.2.840.10045.2.1 — id-ecPublicKey.
231const OID_ALG_EC_PUBLIC_KEY: &[u8] = &[0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01];
232/// OID 2.5.29.19 — basicConstraints.
233const OID_EXT_BASIC_CONSTRAINTS: &[u8] = &[0x55, 0x1D, 0x13];
234/// OID 1.3.6.1.4.1.45724.1.1.4 — id-fido-gen-ce-aaguid (FIDO 2.1 §8.2.1).
235const OID_EXT_FIDO_GEN_CE_AAGUID: &[u8] =
236    &[0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0xE5, 0x14, 0x01, 0x04];
237
238// ---------------------------------------------------------------------------
239// Statement parsing
240// ---------------------------------------------------------------------------
241
242/// CBOR `attStmt` label: `alg` (COSE algorithm identifier).
243const STMT_LABEL_ALG: i64 = 3;
244/// CBOR `attStmt` label: `sig` (signature byte string).
245const STMT_LABEL_SIG: i64 = 8;
246/// CBOR `attStmt` label: `x5c` (X.509 certificate chain array).
247const STMT_LABEL_X5C: i64 = 33;
248
249/// Parse the raw `attStmt` CBOR value for the given format.
250///
251/// `att_stmt` is `None` when the attestation object carried no statement
252/// entry at all (only legal for the `none` format).
253///
254/// # Requirements
255/// REQ-WA-119, REQ-WA-128, REQ-WA-132
256pub(crate) fn parse_statement(
257    format: &AttestationFormat,
258    att_stmt: Option<&Value>,
259) -> Result<AttestationStatement, WebauthnError> {
260    match format {
261        AttestationFormat::None => {
262            // WebAuthn L2 §8.1: attStmt MUST be an empty CBOR map.
263            match att_stmt {
264                None => Ok(AttestationStatement::None),
265                Some(Value::Map(entries)) if entries.is_empty() => Ok(AttestationStatement::None),
266                Some(_) => Err(WebauthnError::AttestationError(
267                    "attStmt must be an empty map for the 'none' format".to_string(),
268                )),
269            }
270        }
271        AttestationFormat::Packed => {
272            let val = att_stmt.ok_or_else(|| {
273                WebauthnError::AttestationError(
274                    "missing attStmt for the 'packed' format".to_string(),
275                )
276            })?;
277            let entries = cbor_map_entries(val).ok_or_else(|| {
278                WebauthnError::AttestationError(
279                    "attStmt is not a CBOR map for the 'packed' format".to_string(),
280                )
281            })?;
282
283            let mut alg: Option<i32> = None;
284            let mut sig: Option<Vec<u8>> = None;
285            let mut x5c: Vec<Vec<u8>> = Vec::new();
286
287            for (label, v) in &entries {
288                match *label {
289                    STMT_LABEL_ALG => alg = cbor_alg(v),
290                    STMT_LABEL_SIG => sig = cbor_bytes(v),
291                    STMT_LABEL_X5C => x5c = cbor_cert_array(v)?,
292                    _ => {}
293                }
294            }
295
296            let alg = alg.ok_or_else(|| {
297                WebauthnError::AttestationError("packed attStmt missing 'alg'".to_string())
298            })?;
299            let sig = sig.ok_or_else(|| {
300                WebauthnError::AttestationError("packed attStmt missing 'sig'".to_string())
301            })?;
302
303            Ok(AttestationStatement::Packed { alg, sig, x5c })
304        }
305        AttestationFormat::FidoU2f => {
306            let val = att_stmt.ok_or_else(|| {
307                WebauthnError::AttestationError(
308                    "missing attStmt for the 'fido-u2f' format".to_string(),
309                )
310            })?;
311            let entries = cbor_map_entries(val).ok_or_else(|| {
312                WebauthnError::AttestationError(
313                    "attStmt is not a CBOR map for the 'fido-u2f' format".to_string(),
314                )
315            })?;
316
317            let mut sig: Option<Vec<u8>> = None;
318            let mut x5c: Vec<Vec<u8>> = Vec::new();
319
320            for (label, v) in &entries {
321                match *label {
322                    STMT_LABEL_SIG => sig = cbor_bytes(v),
323                    STMT_LABEL_X5C => x5c = cbor_cert_array(v)?,
324                    _ => {}
325                }
326            }
327
328            let sig = sig.ok_or_else(|| {
329                WebauthnError::AttestationError("fido-u2f attStmt missing 'sig'".to_string())
330            })?;
331            // WebAuthn L2 §8.6: x5c carries exactly one attestation certificate.
332            let [cert] = x5c.as_slice() else {
333                return Err(WebauthnError::AttestationError(
334                    "fido-u2f attStmt must carry exactly one x5c certificate".to_string(),
335                ));
336            };
337
338            Ok(AttestationStatement::FidoU2f {
339                sig,
340                cert: cert.clone(),
341            })
342        }
343        // Unknown formats are a policy decision, never parsed.
344        AttestationFormat::Unknown => Err(WebauthnError::AttestationError(
345            "unknown attestation format cannot be parsed".to_string(),
346        )),
347    }
348}
349
350/// Extract an `i32` COSE algorithm from a CBOR value.
351fn cbor_alg(val: &Value) -> Option<i32> {
352    match val {
353        Value::Integer(i) => (*i).try_into().ok(),
354        _ => None,
355    }
356}
357
358/// Extract the `x5c` certificate array: a CBOR array of byte strings.
359fn cbor_cert_array(val: &Value) -> Result<Vec<Vec<u8>>, WebauthnError> {
360    match val {
361        Value::Array(items) => {
362            let mut certs = Vec::with_capacity(items.len());
363            for item in items {
364                certs.push(cbor_bytes(item).ok_or_else(|| {
365                    WebauthnError::AttestationError(
366                        "x5c contains a non-byte-string certificate".to_string(),
367                    )
368                })?);
369            }
370            Ok(certs)
371        }
372        _ => Err(WebauthnError::AttestationError(
373            "x5c is not a CBOR array".to_string(),
374        )),
375    }
376}
377
378// ---------------------------------------------------------------------------
379// Verification entry point
380// ---------------------------------------------------------------------------
381
382/// Verify an attestation statement for a registration ceremony.
383///
384/// Dispatches on the parsed format (see the module docs for the per-format
385/// verification procedures) and returns the attestation result on success.
386///
387/// Parameters:
388///
389/// - `fmt`: the raw `fmt` string from the attestation object.
390/// - `att_stmt`: the raw CBOR `attStmt` value (`None` if absent).
391/// - `auth_data`: the raw authenticator data bytes.
392/// - `client_data_hash`: SHA-256 of the raw `clientDataJSON`.
393/// - `credential_id`: the raw credential ID from attested credential data.
394/// - `credential_alg` / `credential_key`: the COSE algorithm and parsed
395///   public key of the *new credential* (from attested credential data).
396/// - `aaguid`: the 16-byte AAGUID from attested credential data.
397/// - `policy`: the caller's trust-anchor / unknown-format policy.
398///
399/// # Requirements
400/// REQ-WA-119, REQ-WA-120, REQ-WA-121, REQ-WA-122, REQ-WA-123, REQ-WA-124,
401/// REQ-WA-126, REQ-WA-127, REQ-WA-128, REQ-WA-129, REQ-WA-130
402#[allow(clippy::too_many_arguments)]
403pub fn verify_attestation(
404    fmt: &str,
405    att_stmt: Option<&Value>,
406    auth_data: &[u8],
407    client_data_hash: &[u8],
408    credential_id: &[u8],
409    credential_alg: i32,
410    credential_key: &CosePublicKey,
411    aaguid: [u8; 16],
412    policy: &AttestationPolicy,
413) -> Result<AttestationResult, WebauthnError> {
414    let format = AttestationFormat::from_fmt(fmt);
415
416    // Unknown format: reject unless the caller opted in (REQ-WA-126).
417    if format == AttestationFormat::Unknown {
418        if policy.allow_unknown_formats {
419            return Ok(AttestationResult {
420                format,
421                aaguid: Some(aaguid),
422                trust_level: TrustLevel::None,
423                warnings: vec![warnings::UNVERIFIED_FORMAT],
424            });
425        }
426        return Err(WebauthnError::AttestationError(format!(
427            "unsupported attestation format: '{fmt}'"
428        )));
429    }
430
431    let statement = parse_statement(&format, att_stmt)?;
432
433    match statement {
434        AttestationStatement::None => verify_none_statement(aaguid),
435        AttestationStatement::Packed { alg, sig, x5c } => {
436            if x5c.is_empty() {
437                verify_packed_self(
438                    &alg,
439                    &sig,
440                    auth_data,
441                    client_data_hash,
442                    credential_alg,
443                    credential_key,
444                    aaguid,
445                )
446            } else {
447                verify_packed_x5c(
448                    &alg,
449                    &sig,
450                    auth_data,
451                    client_data_hash,
452                    &x5c,
453                    aaguid,
454                    policy,
455                )
456            }
457        }
458        AttestationStatement::FidoU2f { sig, cert } => verify_fido_u2f(
459            &sig,
460            &cert,
461            auth_data,
462            client_data_hash,
463            credential_id,
464            credential_alg,
465            credential_key,
466            aaguid,
467            policy,
468        ),
469    }
470}
471
472/// `none` format: the statement itself was already checked to be an empty
473/// map by [`parse_statement`] — the registration ceremony establishes key
474/// possession and nothing more.
475///
476/// # Requirements
477/// REQ-WA-119, REQ-WA-128
478fn verify_none_statement(aaguid: [u8; 16]) -> Result<AttestationResult, WebauthnError> {
479    Ok(AttestationResult {
480        format: AttestationFormat::None,
481        aaguid: Some(aaguid),
482        trust_level: TrustLevel::None,
483        warnings: Vec::new(),
484    })
485}
486
487/// Packed self-attestation (no x5c): the signature over
488/// `authData || clientDataHash` MUST verify with the credential public key
489/// and the statement `alg` MUST match the credential key's algorithm.
490///
491/// # Requirements
492/// REQ-WA-120, REQ-WA-127
493fn verify_packed_self(
494    alg: &i32,
495    sig: &[u8],
496    auth_data: &[u8],
497    client_data_hash: &[u8],
498    credential_alg: i32,
499    credential_key: &CosePublicKey,
500    aaguid: [u8; 16],
501) -> Result<AttestationResult, WebauthnError> {
502    if *alg != credential_alg {
503        return Err(WebauthnError::AttestationError(format!(
504            "packed self-attestation alg {alg} does not match credential key alg {credential_alg}"
505        )));
506    }
507
508    let signed = signed_data(auth_data, client_data_hash);
509    // Cross-checks key type vs alg and verifies with ring (constant-time).
510    verify_cose_signature(*alg, credential_key, &signed, sig)?;
511
512    Ok(AttestationResult {
513        format: AttestationFormat::Packed,
514        aaguid: Some(aaguid),
515        trust_level: TrustLevel::SelfAttested,
516        warnings: Vec::new(),
517    })
518}
519
520/// Packed basic/AttCA attestation (x5c present): the signature MUST verify
521/// with the first certificate's public key, the statement `alg` MUST match
522/// the certificate key type, and the chain MUST satisfy the policy.
523///
524/// # Requirements
525/// REQ-WA-121, REQ-WA-122, REQ-WA-123, REQ-WA-125, REQ-WA-127
526fn verify_packed_x5c(
527    alg: &i32,
528    sig: &[u8],
529    auth_data: &[u8],
530    client_data_hash: &[u8],
531    x5c: &[Vec<u8>],
532    aaguid: [u8; 16],
533    policy: &AttestationPolicy,
534) -> Result<AttestationResult, WebauthnError> {
535    // The statement signs with the attestation certificate's key; its alg
536    // must be one of the algorithms this crate verifies at all.
537    if *alg != COSE_ALG_ES256 && *alg != COSE_ALG_RS256 {
538        return Err(WebauthnError::UnsupportedAlgorithm(*alg));
539    }
540
541    let leaf = Cert::from_der(x5c.first().ok_or_else(|| {
542        WebauthnError::AttestationError("packed attStmt has an empty x5c chain".to_string())
543    })?)?;
544
545    // Statement alg vs certificate key type cross-check (fail closed on
546    // mismatch, mirroring REQ-WA-107 semantics for COSE keys).
547    let expected = match *alg {
548        COSE_ALG_ES256 => KeyKind::Ec2,
549        _ => KeyKind::Rsa,
550    };
551    if leaf.kind != expected {
552        return Err(WebauthnError::AttestationError(
553            "packed attestation alg does not match the attestation certificate key type"
554                .to_string(),
555        ));
556    }
557
558    let signed = signed_data(auth_data, client_data_hash);
559    verify_signature_with_cert(&leaf, &signed, sig)?;
560
561    // AAGUID extension: if present, MUST match the attested credential data
562    // AAGUID (FIDO 2.1 §8.2.1).
563    check_aaguid_extension(&leaf, &aaguid)?;
564
565    let (trust_level, warnings) = verify_chain(x5c, policy)?;
566
567    Ok(AttestationResult {
568        format: AttestationFormat::Packed,
569        aaguid: Some(aaguid),
570        trust_level,
571        warnings,
572    })
573}
574
575/// FIDO U2F attestation (WebAuthn L2 §8.6): exactly one certificate; the
576/// signature MUST verify over
577/// `0x00 || rpIdHash || clientDataHash || credentialId || publicKeyU2F`
578/// with the certificate's ES256 public key, where `publicKeyU2F` is the
579/// 65-byte `0x04 || X || Y` transport form of the credential's EC2 key.
580///
581/// # Requirements
582/// REQ-WA-124, REQ-WA-127, REQ-WA-129
583#[allow(clippy::too_many_arguments)]
584fn verify_fido_u2f(
585    sig: &[u8],
586    cert_der: &[u8],
587    auth_data: &[u8],
588    client_data_hash: &[u8],
589    credential_id: &[u8],
590    credential_alg: i32,
591    credential_key: &CosePublicKey,
592    aaguid: [u8; 16],
593    policy: &AttestationPolicy,
594) -> Result<AttestationResult, WebauthnError> {
595    // U2F credentials are always ES256 on P-256; anything else in the
596    // credential slot means this is not a U2F transport registration.
597    if credential_alg != COSE_ALG_ES256 {
598        return Err(WebauthnError::AttestationError(format!(
599            "fido-u2f registration requires an ES256 credential key, got alg {credential_alg}"
600        )));
601    }
602    let CosePublicKey::Ec2 { x, y } = credential_key else {
603        return Err(WebauthnError::AttestationError(
604            "fido-u2f registration requires an EC2 credential key".to_string(),
605        ));
606    };
607
608    let cert = Cert::from_der(cert_der)?;
609    if cert.kind != KeyKind::Ec2 {
610        return Err(WebauthnError::AttestationError(
611            "fido-u2f attestation certificate must carry an EC P-256 key".to_string(),
612        ));
613    }
614
615    // publicKeyU2F: 0x04 || X || Y (FIDO U2F Raw Message Formats §4.3).
616    if x.len() != 32 || y.len() != 32 {
617        return Err(WebauthnError::AttestationError(
618            "fido-u2f credential key coordinates must be 32 bytes (P-256)".to_string(),
619        ));
620    }
621    let mut public_key_u2f = Vec::with_capacity(65);
622    public_key_u2f.push(0x04);
623    public_key_u2f.extend_from_slice(x);
624    public_key_u2f.extend_from_slice(y);
625
626    if auth_data.len() < 32 {
627        return Err(WebauthnError::AttestationError(
628            "authenticator data too short for fido-u2f verification".to_string(),
629        ));
630    }
631    let mut verification_data = Vec::with_capacity(1 + 32 + 32 + credential_id.len() + 65);
632    verification_data.push(0x00);
633    verification_data.extend_from_slice(&auth_data[..32]); // rpIdHash
634    verification_data.extend_from_slice(client_data_hash);
635    verification_data.extend_from_slice(credential_id);
636    verification_data.extend_from_slice(&public_key_u2f);
637
638    verify_signature_with_cert(&cert, &verification_data, sig)?;
639
640    let (trust_level, warnings) = verify_chain(&[cert_der.to_vec()], policy)?;
641
642    Ok(AttestationResult {
643        format: AttestationFormat::FidoU2f,
644        aaguid: Some(aaguid),
645        trust_level,
646        warnings,
647    })
648}
649
650// ---------------------------------------------------------------------------
651// X.509 certificate handling and chain verification
652// ---------------------------------------------------------------------------
653
654/// Key material kinds this crate can verify with (mirrors `CosePublicKey`).
655#[derive(Debug, Clone, Copy, PartialEq, Eq)]
656enum KeyKind {
657    /// EC P-256 (`id-ecPublicKey`, ES256).
658    Ec2,
659    /// RSA (`rsaEncryption`, RS256, ≥2048-bit enforced by ring).
660    Rsa,
661}
662
663/// Certificate public key in the form `ring` consumes.
664enum CertPublicKey {
665    /// Uncompressed EC point `0x04 || X || Y`.
666    EcPoint(Vec<u8>),
667    /// SubjectPublicKeyInfo DER (ring RSA verifiers take SPKI).
668    SpkiDer(Vec<u8>),
669}
670
671/// A parsed, structurally validated X.509 certificate.
672struct Cert {
673    /// Raw TBS bytes (the signed content).
674    tbs: Vec<u8>,
675    /// DER signature value (without the BIT STRING wrapper).
676    signature: Vec<u8>,
677    /// Signature algorithm OID (DER bytes).
678    signature_alg_oid: Vec<u8>,
679    /// Verifier input for this certificate's own public key.
680    verifier_key: CertPublicKey,
681    /// Issuer name (raw DER, for chain linking).
682    issuer: Vec<u8>,
683    /// Subject name (raw DER, for chain linking).
684    subject: Vec<u8>,
685    /// This certificate's key kind.
686    kind: KeyKind,
687    /// Not-before / not-after (unix seconds).
688    not_before: i64,
689    not_after: i64,
690    /// Raw `(oid, value)` extension list.
691    extensions: Vec<(Vec<u8>, Vec<u8>)>,
692}
693
694impl Cert {
695    /// Parse and structurally validate a DER certificate.
696    ///
697    /// Rejects: malformed DER, trailing bytes after the certificate,
698    /// non-v3 certificates, and unsupported public key algorithms.
699    ///
700    /// # Requirements
701    /// REQ-WA-122, REQ-WA-130, REQ-WA-132
702    fn from_der(der: &[u8]) -> Result<Self, WebauthnError> {
703        let (rem, parsed) = parse_x509_certificate(der).map_err(|e| {
704            WebauthnError::AttestationError(format!("attestation certificate parse error: {e}"))
705        })?;
706        if !rem.is_empty() {
707            return Err(WebauthnError::AttestationError(
708                "trailing bytes after attestation certificate".to_string(),
709            ));
710        }
711
712        let tbs = &parsed.tbs_certificate;
713        if tbs.version() != X509Version::V3 {
714            return Err(WebauthnError::AttestationError(
715                "attestation certificate must be X.509 v3".to_string(),
716            ));
717        }
718
719        let spki = tbs.public_key();
720        let spki_alg = spki.algorithm.algorithm.as_bytes();
721        let (verifier_key, kind) = if spki_alg == OID_ALG_EC_PUBLIC_KEY {
722            let point = spki.subject_public_key.data.to_vec();
723            if point.first() != Some(&0x04) || point.len() != 65 {
724                return Err(WebauthnError::AttestationError(
725                    "attestation certificate EC key is not an uncompressed P-256 point".to_string(),
726                ));
727            }
728            (CertPublicKey::EcPoint(point), KeyKind::Ec2)
729        } else if spki_alg == OID_ALG_RSA_ENCRYPTION {
730            (CertPublicKey::SpkiDer(spki.raw.to_vec()), KeyKind::Rsa)
731        } else {
732            return Err(WebauthnError::AttestationError(
733                "attestation certificate uses an unsupported public key algorithm".to_string(),
734            ));
735        };
736
737        let extensions = tbs
738            .extensions()
739            .iter()
740            .map(|ext| (ext.oid.as_bytes().to_vec(), ext.value.to_vec()))
741            .collect();
742
743        Ok(Self {
744            tbs: tbs.as_ref().to_vec(),
745            signature: parsed.signature_value.data.to_vec(),
746            signature_alg_oid: parsed.signature_algorithm.algorithm.as_bytes().to_vec(),
747            verifier_key,
748            issuer: tbs.issuer().as_raw().to_vec(),
749            subject: tbs.subject().as_raw().to_vec(),
750            kind,
751            not_before: tbs.validity().not_before.timestamp(),
752            not_after: tbs.validity().not_after.timestamp(),
753            extensions,
754        })
755    }
756
757    /// Validity window contains `now` (unix seconds).
758    fn is_valid_at(&self, now: i64) -> bool {
759        self.not_before <= now && now <= self.not_after
760    }
761
762    /// Verify this certificate's own signature with the parent's key.
763    ///
764    /// Only `ecdsa-with-SHA256` (EC P-256 parent) and
765    /// `sha256WithRSAEncryption` (RSA parent) are accepted — anything else
766    /// fails closed.
767    ///
768    /// # Requirements
769    /// REQ-WA-122
770    fn verify_signed_by(&self, parent: &Cert) -> Result<(), WebauthnError> {
771        if self.signature_alg_oid == OID_SIG_ECDSA_WITH_SHA256 {
772            if parent.kind != KeyKind::Ec2 {
773                return Err(WebauthnError::AttestationError(
774                    "certificate signature algorithm does not match issuer key type".to_string(),
775                ));
776            }
777            let CertPublicKey::EcPoint(point) = &parent.verifier_key else {
778                return Err(WebauthnError::AttestationError(
779                    "issuer key is not an EC point".to_string(),
780                ));
781            };
782            let verifier = ring::signature::UnparsedPublicKey::new(
783                &ring::signature::ECDSA_P256_SHA256_FIXED,
784                point,
785            );
786            verifier
787                .verify(&self.tbs, &self.signature)
788                .map_err(|_| WebauthnError::SignatureVerificationFailed)
789        } else if self.signature_alg_oid == OID_SIG_SHA256_WITH_RSA {
790            if parent.kind != KeyKind::Rsa {
791                return Err(WebauthnError::AttestationError(
792                    "certificate signature algorithm does not match issuer key type".to_string(),
793                ));
794            }
795            let CertPublicKey::SpkiDer(spki) = &parent.verifier_key else {
796                return Err(WebauthnError::AttestationError(
797                    "issuer key is not an RSA SPKI".to_string(),
798                ));
799            };
800            let verifier = ring::signature::UnparsedPublicKey::new(
801                &ring::signature::RSA_PKCS1_2048_8192_SHA256,
802                spki,
803            );
804            verifier
805                .verify(&self.tbs, &self.signature)
806                .map_err(|_| WebauthnError::SignatureVerificationFailed)
807        } else {
808            Err(WebauthnError::AttestationError(
809                "certificate uses an unsupported signature algorithm (only ecdsa-with-SHA256 \
810                 and sha256WithRSAEncryption are accepted)"
811                    .to_string(),
812            ))
813        }
814    }
815
816    /// basicConstraints `cA` flag; `None` when the extension is absent.
817    fn is_ca(&self) -> Option<bool> {
818        let (_, value) = self
819            .extensions
820            .iter()
821            .find(|(oid, _)| oid == OID_EXT_BASIC_CONSTRAINTS)?;
822        parse_basic_constraints(value)
823    }
824}
825
826/// Parse a basicConstraints extension value:
827/// `SEQUENCE { BOOLEAN DEFAULT FALSE, INTEGER OPTIONAL }`.
828///
829/// Returns the `cA` flag — `false` when the BOOLEAN is omitted (DER
830/// defaulting) or when only a pathLenConstraint INTEGER is present.
831fn parse_basic_constraints(value: &[u8]) -> Option<bool> {
832    let content = der_content(value, 0x30)?;
833    if content.first() == Some(&0x01) && content.len() >= 3 {
834        // Explicit BOOLEAN: tag (0x01), length (0x01), value.
835        return Some(content[2] == 0xFF);
836    }
837    Some(false)
838}
839
840/// Return the content octets of a DER TLV with the given tag byte, strictly
841/// validating that the declared length matches the available bytes.
842fn der_content(bytes: &[u8], tag: u8) -> Option<&[u8]> {
843    if bytes.first() != Some(&tag) {
844        return None;
845    }
846    let first = *bytes.get(1)?;
847    if first < 0x80 {
848        bytes.get(2..2 + first as usize)
849    } else {
850        let n = (first & 0x7F) as usize;
851        if n == 0 || n > 4 {
852            return None;
853        }
854        let mut len = 0usize;
855        for b in bytes.get(2..2 + n)? {
856            len = (len << 8) | (*b as usize);
857        }
858        bytes.get(2 + n..2 + n + len)
859    }
860}
861
862/// Verify the AAGUID extension (`id-fido-gen-ce-aaguid`) on the leaf, when
863/// present: its value is a DER OCTET STRING wrapping exactly 16 bytes that
864/// MUST equal the attested credential data AAGUID.
865///
866/// # Requirements
867/// REQ-WA-125
868fn check_aaguid_extension(cert: &Cert, aaguid: &[u8; 16]) -> Result<(), WebauthnError> {
869    let Some((_, value)) = cert
870        .extensions
871        .iter()
872        .find(|(oid, _)| oid == OID_EXT_FIDO_GEN_CE_AAGUID)
873    else {
874        return Ok(()); // extension is optional
875    };
876
877    // extnValue content: OCTET STRING (tag 0x04) of length 16 wrapping the
878    // AAGUID (FIDO 2.1 §8.2.1).
879    let bad = || {
880        WebauthnError::AttestationError("id-fido-gen-ce-aaguid extension is malformed".to_string())
881    };
882    let content = der_content(value, 0x04).ok_or_else(bad)?;
883    if content.len() != 16 {
884        return Err(bad());
885    }
886    if content != aaguid.as_slice() {
887        return Err(WebauthnError::AttestationError(
888            "id-fido-gen-ce-aaguid extension does not match the attested credential data AAGUID"
889                .to_string(),
890        ));
891    }
892    Ok(())
893}
894
895/// Verify the x5c chain and return the resulting trust level.
896///
897/// Checks, in order (fail closed):
898///
899/// 1. Every certificate parses and is X.509 v3 ([`Cert::from_der`]).
900/// 2. Every certificate is within its validity window *now* (REQ-WA-130).
901/// 3. Each certificate is signed by the next with a supported algorithm;
902///    issuer/subject names link byte-exactly; intermediates declare
903///    basicConstraints `cA = true` (REQ-WA-122).
904/// 4. Anchor policy (REQ-WA-123):
905///    - anchors configured → the chain root must equal an anchor, or be
906///      signed by one, → [`TrustLevel::AttCa`]; otherwise reject;
907///    - no anchors → the chain root must be self-signed (self-signature
908///      verified) → [`TrustLevel::BasicAtt`] +
909///      [`warnings::UNANCHORED_CHAIN`].
910fn verify_chain(
911    x5c: &[Vec<u8>],
912    policy: &AttestationPolicy,
913) -> Result<(TrustLevel, Vec<&'static str>), WebauthnError> {
914    let now = SystemTime::now()
915        .duration_since(UNIX_EPOCH)
916        .map(|d| d.as_secs() as i64)
917        .map_err(|_| {
918            WebauthnError::AttestationError("system clock before unix epoch".to_string())
919        })?;
920
921    let mut certs = Vec::with_capacity(x5c.len());
922    for der in x5c {
923        let cert = Cert::from_der(der)?;
924        if !cert.is_valid_at(now) {
925            return Err(WebauthnError::AttestationError(
926                "attestation certificate is expired or not yet valid".to_string(),
927            ));
928        }
929        certs.push(cert);
930    }
931
932    // Signature chain: leaf → intermediate(s) → root.
933    for pair in certs.windows(2) {
934        let (child, parent) = (&pair[0], &pair[1]);
935        if child.issuer != parent.subject {
936            return Err(WebauthnError::AttestationError(
937                "attestation chain issuer/subject names do not link".to_string(),
938            ));
939        }
940        child.verify_signed_by(parent)?;
941    }
942
943    // Non-leaf certificates must declare CA: true (basicConstraints).
944    for parent in certs.iter().skip(1) {
945        if parent.is_ca() != Some(true) {
946            return Err(WebauthnError::AttestationError(
947                "attestation chain intermediate lacks basicConstraints cA=true".to_string(),
948            ));
949        }
950    }
951
952    let root = certs
953        .last()
954        .ok_or_else(|| WebauthnError::AttestationError("attestation chain is empty".to_string()))?;
955    let root_der = x5c
956        .last()
957        .ok_or_else(|| WebauthnError::AttestationError("attestation chain is empty".to_string()))?;
958
959    // Anchor policy (REQ-WA-123).
960    for anchor in &policy.trust_anchors {
961        // Case 1: the chain carries the anchor certificate itself.
962        if root_der == anchor {
963            return Ok((TrustLevel::AttCa, Vec::new()));
964        }
965        // Case 2: the chain root is issued directly by the anchor.
966        let anchor_cert = Cert::from_der(anchor)?;
967        if root.issuer == anchor_cert.subject && root.verify_signed_by(&anchor_cert).is_ok() {
968            return Ok((TrustLevel::AttCa, Vec::new()));
969        }
970    }
971    if !policy.trust_anchors.is_empty() {
972        return Err(WebauthnError::AttestationError(
973            "attestation chain does not terminate at a configured trust anchor".to_string(),
974        ));
975    }
976
977    // No anchors configured: accept only a chain rooted at a self-signed
978    // certificate (self-signature verified), and flag it.
979    let self_signed = root.issuer == root.subject && root.verify_signed_by(root).is_ok();
980    if !self_signed {
981        return Err(WebauthnError::AttestationError(
982            "attestation chain root is not self-signed and no trust anchors are configured"
983                .to_string(),
984        ));
985    }
986
987    Ok((TrustLevel::BasicAtt, vec![warnings::UNANCHORED_CHAIN]))
988}
989
990// ---------------------------------------------------------------------------
991// Shared helpers
992// ---------------------------------------------------------------------------
993
994/// The bytes covered by a WebAuthn registration attestation signature:
995/// `authenticatorData || SHA-256(clientDataJSON)` (the caller supplies the
996/// precomputed hash).
997fn signed_data(auth_data: &[u8], client_data_hash: &[u8]) -> Vec<u8> {
998    let mut signed = Vec::with_capacity(auth_data.len() + client_data_hash.len());
999    signed.extend_from_slice(auth_data);
1000    signed.extend_from_slice(client_data_hash);
1001    signed
1002}
1003
1004/// Verify `data`/`sig` with an X.509 certificate's public key via `ring`.
1005///
1006/// Only ES256 (EC P-256) and RS256 (RSA PKCS#1 v1.5, ≥2048-bit) are
1007/// supported, matching this crate's COSE algorithm support.
1008///
1009/// # Requirements
1010/// REQ-WA-121, REQ-WA-124, REQ-WA-127
1011fn verify_signature_with_cert(cert: &Cert, data: &[u8], sig: &[u8]) -> Result<(), WebauthnError> {
1012    match &cert.verifier_key {
1013        CertPublicKey::EcPoint(point) => {
1014            let verifier = ring::signature::UnparsedPublicKey::new(
1015                &ring::signature::ECDSA_P256_SHA256_FIXED,
1016                point,
1017            );
1018            verifier
1019                .verify(data, sig)
1020                .map_err(|_| WebauthnError::SignatureVerificationFailed)
1021        }
1022        CertPublicKey::SpkiDer(spki) => {
1023            let verifier = ring::signature::UnparsedPublicKey::new(
1024                &ring::signature::RSA_PKCS1_2048_8192_SHA256,
1025                spki,
1026            );
1027            verifier
1028                .verify(data, sig)
1029                .map_err(|_| WebauthnError::SignatureVerificationFailed)
1030        }
1031    }
1032}