yubikit 1.0.0-beta.0

YubiKey management library
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
// Copyright 2026 Yubico AB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! CTAP session for FIDO2 authenticator communication.
//!
//! Provides [`CtapSession`](crate::ctap::CtapSession), a transport-agnostic session for communicating
//! with FIDO2 authenticators using the CTAP2 protocol. Supports both
//! CTAP HID (USB) via [`FidoConnection`](crate::fido::FidoConnection) and NFCCTAP (SmartCard/NFC)
//! via [`SmartCardConnection`](crate::smartcard::SmartCardConnection).

use std::time::Duration;

use crate::core::{Connection, Version, patch_version};
use crate::fido::FidoConnection;
use crate::fido::FidoError;
use crate::smartcard::{Aid, SmartCardConnection, SmartCardError, SmartCardProtocol};

// ---------------------------------------------------------------------------
// CTAP2 constants
// ---------------------------------------------------------------------------

/// CTAP HID command byte for CBOR messages.
const CTAPHID_CBOR: u8 = 0x10;

/// CTAP HID command byte for raw U2F/CTAP1 messages.
const CTAPHID_MSG: u8 = 0x03;

/// NFCCTAP APDU instruction for CBOR commands.
const NFCCTAP_MSG: u8 = 0x10;

/// NFCCTAP APDU instruction for getResponse / cancel.
const NFCCTAP_GETRESPONSE: u8 = 0x11;

/// Status word indicating a keepalive during NFCCTAP polling.
const SW_KEEPALIVE: u16 = 0x9100;

/// Polling interval for NFCCTAP keepalive responses.
const NFCCTAP_POLL_INTERVAL: Duration = Duration::from_millis(100);

/// NFCCTAP P1 value to cancel a pending operation.
const NFCCTAP_CANCEL: u8 = 0x11;

// ---------------------------------------------------------------------------
// CtapError
// ---------------------------------------------------------------------------

/// Error type for [`CtapSession`] operations.
///
/// Generic over the underlying connection error type `E`.
#[derive(Debug)]
pub enum CtapError<E: std::error::Error + Send + Sync + 'static> {
    /// The underlying transport returned an error.
    Connection(E),
    /// The device is not supported (no CTAP2 capabilities).
    NotSupported(String),
    /// The response from the device was malformed.
    InvalidResponse(String),
}

impl<E: std::error::Error + Send + Sync + 'static> std::fmt::Display for CtapError<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Connection(e) => write!(f, "Connection error: {e}"),
            Self::NotSupported(msg) => write!(f, "Not supported: {msg}"),
            Self::InvalidResponse(msg) => write!(f, "Invalid response: {msg}"),
        }
    }
}

impl<E: std::error::Error + Send + Sync + 'static> std::error::Error for CtapError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Connection(e) => Some(e),
            _ => None,
        }
    }
}

impl<E: std::error::Error + Send + Sync + 'static> CtapError<E> {
    /// Erase the generic connection error type for uniform handling.
    pub fn erase(self) -> String {
        self.to_string()
    }
}

impl<E: std::error::Error + Send + Sync + 'static> From<E> for CtapError<E> {
    fn from(e: E) -> Self {
        Self::Connection(e)
    }
}

// ---------------------------------------------------------------------------
// CtapBackend (private, object-safe trait for internal dispatch)
// ---------------------------------------------------------------------------

/// Object-safe trait for transport-specific CTAP2 operations.
///
/// Each transport (CCID/NFC, CTAP HID) implements this trait. The public
/// [`CtapSession`] dispatches through this.
trait CtapBackend<E: std::error::Error + Send + Sync + 'static>: Send {
    /// Device firmware version.
    fn version(&self) -> Version;

    /// Send a CTAP CBOR command and receive the response.
    fn call_cbor(
        &mut self,
        data: &[u8],
        on_keepalive: Option<&mut dyn FnMut(u8)>,
        cancel: Option<&dyn Fn() -> bool>,
    ) -> Result<Vec<u8>, CtapError<E>>;

    /// Send a CTAP1/U2F (MSG) command and receive the response.
    fn call_msg(&mut self, data: &[u8]) -> Result<Vec<u8>, CtapError<E>>;

    /// Whether the device supports CTAP2 CBOR commands.
    fn has_ctap2(&self) -> bool;

    /// Whether the device supports CTAP1/U2F MSG commands.
    fn has_ctap1(&self) -> bool;

    /// Consume the backend and return the connection as `Box<dyn Any>`.
    fn into_connection_any(self: Box<Self>) -> Box<dyn std::any::Any>;
}

// ---------------------------------------------------------------------------
// CtapSession
// ---------------------------------------------------------------------------

/// CTAP session for FIDO2 authenticator communication.
///
/// Generic over the connection type `C`. Construct with [`CtapSession::new`]
/// for SmartCard/NFC (CCID) or [`CtapSession::new_fido`] for CTAP HID.
pub struct CtapSession<C: Connection> {
    inner: Box<dyn CtapBackend<C::Error>>,
    _phantom: std::marker::PhantomData<C>,
}

impl<C: Connection + 'static> CtapSession<C> {
    /// The firmware version of the device.
    pub fn version(&self) -> Version {
        self.inner.version()
    }

    /// Whether the device supports CTAP2 CBOR commands.
    pub fn has_ctap2(&self) -> bool {
        self.inner.has_ctap2()
    }

    /// Whether the device supports CTAP1/U2F MSG commands.
    pub fn has_ctap1(&self) -> bool {
        self.inner.has_ctap1()
    }

    /// Send a CTAP2 CBOR command and receive the response.
    pub fn call_cbor(
        &mut self,
        data: &[u8],
        on_keepalive: Option<&mut dyn FnMut(u8)>,
        cancel: Option<&dyn Fn() -> bool>,
    ) -> Result<Vec<u8>, CtapError<C::Error>> {
        self.inner.call_cbor(data, on_keepalive, cancel)
    }

    /// Send a CTAP1/U2F MSG command and receive the response.
    pub fn call_msg(&mut self, data: &[u8]) -> Result<Vec<u8>, CtapError<C::Error>> {
        self.inner.call_msg(data)
    }

    /// Consume the session, returning the underlying connection.
    pub fn into_connection(self) -> C {
        *self
            .inner
            .into_connection_any()
            .downcast::<C>()
            .expect("CtapSession inner type mismatch (this is a bug)")
    }

    fn from_inner(inner: Box<dyn CtapBackend<C::Error>>) -> Self {
        Self {
            inner,
            _phantom: std::marker::PhantomData,
        }
    }
}

// SmartCard (CCID / NFC) constructors
impl<C: SmartCardConnection + Send + 'static> CtapSession<C> {
    /// Open a CTAP session over SmartCard (CCID / NFC).
    ///
    /// Selects the FIDO applet and probes for CTAP2 and CTAP1 support.
    /// On error, returns the connection so the caller can recover it.
    pub fn new(connection: C) -> Result<Self, (CtapError<SmartCardError>, C)> {
        CcidCtap::open(connection).map(|inner| Self::from_inner(Box::new(inner)))
    }

    /// Open a CTAP session over SmartCard with SCP (Secure Channel Protocol).
    ///
    /// On error, returns the connection so the caller can recover it.
    pub fn new_with_scp(
        connection: C,
        scp_key_params: &crate::smartcard::ScpKeyParams,
    ) -> Result<Self, (CtapError<SmartCardError>, C)> {
        CcidCtap::open_with_scp(connection, scp_key_params)
            .map(|inner| Self::from_inner(Box::new(inner)))
    }
}

// FIDO HID constructors
impl<C: FidoConnection + Send + 'static> CtapSession<C> {
    /// Open a CTAP session over FIDO HID.
    ///
    /// On error, returns the connection so the caller can recover it.
    pub fn new_fido(connection: C) -> Result<Self, (CtapError<FidoError>, C)> {
        HidCtap::open(connection).map(|inner| Self::from_inner(Box::new(inner)))
    }
}

// ---------------------------------------------------------------------------
// CcidCtap — SmartCard / NFC backend (internal)
// ---------------------------------------------------------------------------

struct CcidCtap<C: SmartCardConnection> {
    protocol: SmartCardProtocol<C>,
    version: Version,
    has_ctap2: bool,
    has_ctap1: bool,
}

impl<C: SmartCardConnection> CcidCtap<C> {
    fn open(connection: C) -> Result<Self, (CtapError<SmartCardError>, C)> {
        let mut protocol = SmartCardProtocol::new(connection);
        let resp = match protocol.select(Aid::FIDO) {
            Ok(resp) => resp,
            Err(e) => return Err((CtapError::Connection(e), protocol.into_connection())),
        };
        Self::init(protocol, &resp)
    }

    fn open_with_scp(
        connection: C,
        scp_key_params: &crate::smartcard::ScpKeyParams,
    ) -> Result<Self, (CtapError<SmartCardError>, C)> {
        let mut protocol = SmartCardProtocol::new(connection);
        let resp = match protocol.select(Aid::FIDO) {
            Ok(resp) => resp,
            Err(e) => return Err((CtapError::Connection(e), protocol.into_connection())),
        };
        if let Err(e) = protocol.init_scp(scp_key_params) {
            return Err((CtapError::Connection(e), protocol.into_connection()));
        }
        Self::init(protocol, &resp)
    }

    fn init(
        mut protocol: SmartCardProtocol<C>,
        select_resp: &[u8],
    ) -> Result<Self, (CtapError<SmartCardError>, C)> {
        log::debug!("Opening CcidCtap (SmartCard/NFC)");

        let has_ctap1 = select_resp == b"U2F_V2";

        // Probe for CTAP2 via authenticatorGetInfo (0x04)
        let has_ctap2 = protocol
            .send_apdu(0x80, NFCCTAP_MSG, 0x80, 0x00, &[0x04])
            .is_ok();

        if !has_ctap2 && !has_ctap1 {
            return Err((
                CtapError::NotSupported("Device supports neither CTAP2 nor CTAP1".into()),
                protocol.into_connection(),
            ));
        }

        // Derive version from the CTAP HID equivalent (not available over NFC,
        // so default to 0.0.0 and let callers override from DeviceInfo).
        let version = patch_version(Version(0, 0, 0));

        Ok(Self {
            protocol,
            version,
            has_ctap2,
            has_ctap1,
        })
    }

    /// NFCCTAP CBOR framing with keepalive polling and cancel support.
    fn nfcctap_call_cbor(
        &mut self,
        data: &[u8],
        on_keepalive: Option<&mut dyn FnMut(u8)>,
        cancel: Option<&dyn Fn() -> bool>,
    ) -> Result<Vec<u8>, CtapError<SmartCardError>> {
        // Initial CBOR command
        match self.protocol.send_apdu(0x80, NFCCTAP_MSG, 0x80, 0x00, data) {
            Ok(resp) => Ok(resp),
            Err(SmartCardError::Apdu { data: ka_data, sw }) if sw == SW_KEEPALIVE => {
                // Enter keepalive polling loop
                self.nfcctap_poll(ka_data, on_keepalive, cancel)
            }
            Err(SmartCardError::Apdu { sw, .. }) => Err(CtapError::InvalidResponse(format!(
                "NFCCTAP error: SW={sw:04X}"
            ))),
            Err(e) => Err(CtapError::Connection(e)),
        }
    }

    /// Keepalive polling loop for NFCCTAP.
    fn nfcctap_poll(
        &mut self,
        initial_ka: Vec<u8>,
        mut on_keepalive: Option<&mut dyn FnMut(u8)>,
        cancel: Option<&dyn Fn() -> bool>,
    ) -> Result<Vec<u8>, CtapError<SmartCardError>> {
        let mut last_ka: Option<u8> = None;

        // Handle initial keepalive data
        if let Some(&status) = initial_ka.first() {
            last_ka = Some(status);
            if let Some(ref mut cb) = on_keepalive {
                cb(status);
            }
        }

        loop {
            std::thread::sleep(NFCCTAP_POLL_INTERVAL);

            let p1 = if cancel.is_some_and(|f| f()) {
                NFCCTAP_CANCEL
            } else {
                0x00
            };

            match self
                .protocol
                .send_apdu(0x80, NFCCTAP_GETRESPONSE, p1, 0x00, &[])
            {
                Ok(resp) => return Ok(resp),
                Err(SmartCardError::Apdu { data, sw }) if sw == SW_KEEPALIVE => {
                    if let Some(&status) = data.first()
                        && last_ka != Some(status)
                    {
                        last_ka = Some(status);
                        if let Some(ref mut cb) = on_keepalive {
                            cb(status);
                        }
                    }
                }
                Err(SmartCardError::Apdu { sw, .. }) => {
                    return Err(CtapError::InvalidResponse(format!(
                        "NFCCTAP error: SW={sw:04X}"
                    )));
                }
                Err(e) => return Err(CtapError::Connection(e)),
            }
        }
    }

    /// NFCCTAP MSG (CTAP1/U2F) framing — simple APDU pass-through.
    fn nfcctap_call_msg(&mut self, data: &[u8]) -> Result<Vec<u8>, CtapError<SmartCardError>> {
        if data.len() < 4 {
            return Err(CtapError::InvalidResponse("APDU too short".into()));
        }
        let (cla, ins, p1, p2) = (data[0], data[1], data[2], data[3]);
        let payload = if data.len() > 5 {
            let end = 5 + data[4] as usize;
            if end > data.len() {
                return Err(CtapError::InvalidResponse(
                    "Payload length exceeds data".into(),
                ));
            }
            &data[5..end]
        } else {
            &[]
        };

        match self.protocol.send_apdu(cla, ins, p1, p2, payload) {
            Ok(resp) => {
                let mut result = resp;
                result.push(0x90);
                result.push(0x00);
                Ok(result)
            }
            Err(SmartCardError::Apdu { data, sw }) => {
                let mut result = data;
                result.push((sw >> 8) as u8);
                result.push((sw & 0xFF) as u8);
                Ok(result)
            }
            Err(e) => Err(CtapError::Connection(e)),
        }
    }
}

impl<C: SmartCardConnection + Send + 'static> CtapBackend<SmartCardError> for CcidCtap<C> {
    fn version(&self) -> Version {
        self.version
    }

    fn call_cbor(
        &mut self,
        data: &[u8],
        on_keepalive: Option<&mut dyn FnMut(u8)>,
        cancel: Option<&dyn Fn() -> bool>,
    ) -> Result<Vec<u8>, CtapError<SmartCardError>> {
        self.nfcctap_call_cbor(data, on_keepalive, cancel)
    }

    fn call_msg(&mut self, data: &[u8]) -> Result<Vec<u8>, CtapError<SmartCardError>> {
        self.nfcctap_call_msg(data)
    }

    fn has_ctap2(&self) -> bool {
        self.has_ctap2
    }

    fn has_ctap1(&self) -> bool {
        self.has_ctap1
    }

    fn into_connection_any(self: Box<Self>) -> Box<dyn std::any::Any> {
        Box::new(self.protocol.into_connection())
    }
}

// ---------------------------------------------------------------------------
// HidCtap — FIDO HID backend (internal)
// ---------------------------------------------------------------------------

struct HidCtap<C: FidoConnection> {
    connection: C,
    version: Version,
    has_ctap2: bool,
    has_ctap1: bool,
}

impl<C: FidoConnection> HidCtap<C> {
    fn open(connection: C) -> Result<Self, (CtapError<FidoError>, C)> {
        log::debug!("Opening HidCtap (FIDO HID)");
        let caps = connection.capabilities();
        let has_ctap2 = caps.has_cbor();
        let has_ctap1 = !caps.has_nmsg();

        if !has_ctap2 && !has_ctap1 {
            return Err((
                CtapError::NotSupported("Device supports neither CTAP2 nor CTAP1".into()),
                connection,
            ));
        }

        let version = patch_version(connection.device_version());

        Ok(Self {
            connection,
            version,
            has_ctap2,
            has_ctap1,
        })
    }
}

impl<C: FidoConnection + Send + 'static> CtapBackend<FidoError> for HidCtap<C> {
    fn version(&self) -> Version {
        self.version
    }

    fn call_cbor(
        &mut self,
        data: &[u8],
        on_keepalive: Option<&mut dyn FnMut(u8)>,
        cancel: Option<&dyn Fn() -> bool>,
    ) -> Result<Vec<u8>, CtapError<FidoError>> {
        self.connection
            .call(CTAPHID_CBOR, data, on_keepalive, cancel)
            .map_err(CtapError::Connection)
    }

    fn call_msg(&mut self, data: &[u8]) -> Result<Vec<u8>, CtapError<FidoError>> {
        self.connection
            .call(CTAPHID_MSG, data, None, None)
            .map_err(CtapError::Connection)
    }

    fn has_ctap2(&self) -> bool {
        self.has_ctap2
    }

    fn has_ctap1(&self) -> bool {
        self.has_ctap1
    }

    fn into_connection_any(self: Box<Self>) -> Box<dyn std::any::Any> {
        Box::new(self.connection)
    }
}