|
| 1 | +//! CTAP 2.1 `authenticatorLargeBlobs` command (cmd `0x0C`). |
| 2 | +//! |
| 3 | +//! Spec: CTAP 2.2 §6.10. The command provides paginated read/write access to a |
| 4 | +//! single authenticator-stored byte array (the "serialized largeBlobArray"), |
| 5 | +//! which is shared across all credentials on the device. Individual blobs are |
| 6 | +//! encrypted with the per-credential `largeBlobKey`. |
| 7 | +//! |
| 8 | +//! This module implements the wire-level request/response model. Higher-level |
| 9 | +//! helpers that drive paginated reads (and, in the future, writes) live in the |
| 10 | +//! `ops::webauthn::large_blob` module. |
| 11 | +
|
| 12 | +use serde_bytes::ByteBuf; |
| 13 | +use serde_indexed::{DeserializeIndexed, SerializeIndexed}; |
| 14 | + |
| 15 | +/// `authenticatorLargeBlobs` request parameters. The map shape is defined in |
| 16 | +/// CTAP 2.2 §6.10.1. Per the spec the platform sends EITHER `get` (read path) |
| 17 | +/// OR `set` (write path) per request; the unused field MUST be absent. |
| 18 | +#[derive(Debug, Clone, SerializeIndexed)] |
| 19 | +pub struct Ctap2LargeBlobsRequest { |
| 20 | + /// `get` (0x01): when present, requests up to this many bytes starting at |
| 21 | + /// `offset`. Mutually exclusive with `set`. |
| 22 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 23 | + #[serde(index = 0x01)] |
| 24 | + pub get: Option<u32>, |
| 25 | + |
| 26 | + /// `set` (0x02): when present, this chunk of the serialized largeBlobArray |
| 27 | + /// is written at `offset`. Mutually exclusive with `get`. |
| 28 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 29 | + #[serde(index = 0x02)] |
| 30 | + pub set: Option<ByteBuf>, |
| 31 | + |
| 32 | + /// `offset` (0x03): byte offset for the read/write window. Required. |
| 33 | + #[serde(index = 0x03)] |
| 34 | + pub offset: u32, |
| 35 | + |
| 36 | + /// `length` (0x04): on the first `set` request only, the total length of |
| 37 | + /// the serialized array being written. |
| 38 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 39 | + #[serde(index = 0x04)] |
| 40 | + pub length: Option<u32>, |
| 41 | + |
| 42 | + /// `pinUvAuthParam` (0x05): required for `set`; absent for `get` unless |
| 43 | + /// the device requires it. |
| 44 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 45 | + #[serde(index = 0x05)] |
| 46 | + pub pin_uv_auth_param: Option<ByteBuf>, |
| 47 | + |
| 48 | + /// `pinUvAuthProtocol` (0x06): version selected by the platform. |
| 49 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 50 | + #[serde(index = 0x06)] |
| 51 | + pub pin_uv_auth_protocol: Option<u32>, |
| 52 | +} |
| 53 | + |
| 54 | +impl Ctap2LargeBlobsRequest { |
| 55 | + /// Build a `get` request for `length` bytes at `offset`. |
| 56 | + pub fn new_get(offset: u32, length: u32) -> Self { |
| 57 | + Self { |
| 58 | + get: Some(length), |
| 59 | + set: None, |
| 60 | + offset, |
| 61 | + length: None, |
| 62 | + pin_uv_auth_param: None, |
| 63 | + pin_uv_auth_protocol: None, |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// `authenticatorLargeBlobs` response. The map carries a single optional |
| 69 | +/// `config` field (CTAP 2.2 §6.10.2), which holds the requested slice of the |
| 70 | +/// serialized largeBlobArray on the read path. Empty on the write path. |
| 71 | +#[cfg_attr(test, derive(SerializeIndexed))] |
| 72 | +#[derive(Debug, Default, Clone, DeserializeIndexed)] |
| 73 | +pub struct Ctap2LargeBlobsResponse { |
| 74 | + /// `config` (0x01): the partial blob array. None on write responses, or on |
| 75 | + /// devices that omit the field for an empty array. |
| 76 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 77 | + #[serde(index = 0x01)] |
| 78 | + pub config: Option<ByteBuf>, |
| 79 | +} |
| 80 | + |
| 81 | +#[cfg(test)] |
| 82 | +mod tests { |
| 83 | + use super::*; |
| 84 | + use crate::proto::ctap2::cbor; |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn get_request_round_trips_through_cbor() { |
| 88 | + let req = Ctap2LargeBlobsRequest::new_get(0, 1024); |
| 89 | + let bytes = cbor::to_vec(&req).expect("serialize"); |
| 90 | + // CTAP 2.2 §6.10.1 mandates an integer-keyed map; verify that our |
| 91 | + // encoding produces one. Tag 0xa3 = map of 3 items (get=1, offset=3, |
| 92 | + // implicit absence of set/length/pinUvAuthParam/pinUvAuthProtocol). |
| 93 | + assert_eq!( |
| 94 | + bytes[0], 0xa2, |
| 95 | + "expected CBOR map of two items, got {:#x}", |
| 96 | + bytes[0] |
| 97 | + ); |
| 98 | + // Sanity: re-decoding via the loose Value parser should yield the same |
| 99 | + // logical content. |
| 100 | + let value: cbor::Value = cbor::from_slice(&bytes).expect("deserialize"); |
| 101 | + let cbor::Value::Map(map) = value else { |
| 102 | + panic!("expected map"); |
| 103 | + }; |
| 104 | + assert_eq!(map.len(), 2); |
| 105 | + } |
| 106 | +} |
0 commit comments