soft-fido2 0.17.0

A pure Rust implementation of FIDO2/WebAuthn CTAP 2.0/2.1 protocol
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
531
532
533
534
use crate::error::{Error, Result};

#[cfg(target_os = "linux")]
use soft_fido2_transport::UhidDevice;
#[cfg(feature = "std")]
use soft_fido2_transport::{Cmd, Message, Packet};
#[cfg(feature = "usb")]
use soft_fido2_transport::{UsbTransport as RawUsbTransport, enumerate_devices, init_usb};

use std::sync::{Arc, Mutex};

use smallvec::SmallVec;

/// Safe Rust wrapper for Transport
pub struct Transport {
    inner: Arc<Mutex<TransportInner>>,
}

#[allow(dead_code)]
enum TransportInner {
    #[cfg(feature = "usb")]
    Usb {
        transport: RawUsbTransport,
        channel_id: Option<u32>,
    },
    #[cfg(target_os = "linux")]
    Uhid {
        device: UhidDevice,
        #[allow(dead_code)]
        opened: bool,
        channel_id: Option<u32>,
    },
}

impl Transport {
    #[cfg(feature = "usb")]
    fn from_usb(transport: RawUsbTransport) -> Self {
        Self {
            inner: Arc::new(Mutex::new(TransportInner::Usb {
                transport,
                channel_id: None,
            })),
        }
    }

    #[cfg(target_os = "linux")]
    #[allow(dead_code)]
    fn from_uhid(device: UhidDevice) -> Self {
        Self {
            inner: Arc::new(Mutex::new(TransportInner::Uhid {
                device,
                opened: false,
                channel_id: None,
            })),
        }
    }

    /// Open the transport for communication
    pub fn open(&mut self) -> Result<()> {
        let mut inner = self.inner.lock().unwrap();
        match &mut *inner {
            #[cfg(feature = "usb")]
            TransportInner::Usb { .. } => {
                // USB transports are opened on construction, nothing to do
                Ok(())
            }
            #[cfg(target_os = "linux")]
            TransportInner::Uhid { opened, .. } => {
                // UHID devices are always "open" after creation
                *opened = true;
                Ok(())
            }

            #[cfg(not(any(feature = "usb", target_os = "linux")))]
            _ => Err(Error::Other),
        }
    }

    /// Close the transport
    pub fn close(&mut self) {
        let mut inner = self.inner.lock().unwrap();
        match &mut *inner {
            #[cfg(feature = "usb")]
            TransportInner::Usb { .. } => {
                // USB transports use Drop for cleanup, nothing to do
            }
            #[cfg(target_os = "linux")]
            TransportInner::Uhid { opened, .. } => {
                *opened = false;
            }

            #[cfg(not(any(feature = "usb", target_os = "linux")))]
            _ => {}
        }
    }

    /// Write data to the transport
    ///
    /// This sends raw packets. For CTAP commands, use send_ctap_command instead.
    pub fn write(&mut self, data: &[u8]) -> Result<()> {
        let inner = self.inner.lock().unwrap();
        match &*inner {
            #[cfg(feature = "usb")]
            TransportInner::Usb { transport, .. } => {
                // USB transport expects Packet, convert from raw bytes
                if data.len() != 64 {
                    return Err(Error::Other);
                }
                let mut buf = [0u8; 64];
                buf.copy_from_slice(data);
                let packet = Packet::from_slice(&buf).map_err(|e| Error::IoError(e.to_string()))?;
                transport
                    .write_packet(&packet)
                    .map_err(|e| Error::IoError(e.to_string()))?;
                Ok(())
            }
            #[cfg(target_os = "linux")]
            TransportInner::Uhid { device, .. } => {
                // UHID requires exactly 64 bytes
                if data.len() != 64 {
                    return Err(Error::Other);
                }
                let mut packet = [0u8; 64];
                packet.copy_from_slice(data);
                device
                    .write_packet(&packet)
                    .map_err(|e| Error::IoError(e.to_string()))?;
                Ok(())
            }
            #[cfg(not(any(feature = "usb", target_os = "linux")))]
            _ => Err(Error::Other),
        }
    }

    /// Read data from the transport with timeout
    pub fn read(
        &mut self,
        buffer: &mut [u8],
        #[allow(unused_variables)] timeout_ms: i32,
    ) -> Result<usize> {
        let inner = self.inner.lock().unwrap();
        match &*inner {
            #[cfg(feature = "usb")]
            TransportInner::Usb { transport, .. } => {
                // USB transport uses packet-based API with timeout
                match transport
                    .read_packet_timeout(timeout_ms)
                    .map_err(|e| Error::IoError(e.to_string()))?
                {
                    Some(packet) => {
                        let packet_bytes = packet.as_bytes();
                        let len = packet_bytes.len().min(buffer.len());
                        buffer[..len].copy_from_slice(&packet_bytes[..len]);
                        Ok(len)
                    }
                    None => {
                        // Timeout
                        Ok(0)
                    }
                }
            }
            #[cfg(target_os = "linux")]
            TransportInner::Uhid { device, .. } => {
                // UHID requires exactly 64 bytes buffer
                if buffer.len() < 64 {
                    return Err(Error::Other);
                }
                let mut packet = [0u8; 64];
                // UHID doesn't have timeout, use blocking read
                if let Some(len) = device
                    .read_packet(&mut packet)
                    .map_err(|e| Error::IoError(e.to_string()))?
                {
                    buffer[..len].copy_from_slice(&packet[..len]);
                    Ok(len)
                } else {
                    Ok(0)
                }
            }
            #[cfg(not(any(feature = "usb", target_os = "linux")))]
            _ => Err(Error::Other),
        }
    }

    /// Initialize CTAP HID channel
    ///
    /// Sends an INIT command to allocate a channel ID from the authenticator.
    /// This must be called before sending any CTAP commands.
    ///
    /// Returns the allocated channel ID.
    fn init_channel(&mut self) -> Result<u32> {
        // Generate 8-byte nonce for INIT
        let nonce: [u8; 8] = rand::random();

        // Build INIT message on broadcast channel
        let init_message = Message::new(0xffffffff, Cmd::Init, nonce.to_vec(), None);

        // Fragment into packets
        let packets = init_message.to_packets().map_err(|_| Error::Other)?;

        // Send INIT packets
        for packet in &packets {
            self.write(packet.as_bytes())?;
        }

        // Read INIT response (use SmallVec for stack allocation)
        // INIT responses are small (17 bytes), so we can stack-allocate
        let mut response_packets: SmallVec<[Packet; 4]> = SmallVec::new();

        loop {
            let mut buffer = [0u8; 64];
            let bytes_read = self.read(&mut buffer, 5000)?;

            if bytes_read == 0 {
                return Err(Error::Timeout);
            }

            let packet = Packet::from_bytes(buffer);

            // INIT response should be on broadcast channel
            if packet.cid() != 0xffffffff {
                continue;
            }

            response_packets.push(packet);

            // Check if we have complete response
            if let Some(first) = response_packets.first()
                && let Some(total_len) = first.payload_len()
            {
                let mut received_len = first.payload().len();
                for pkt in &response_packets[1..] {
                    received_len += pkt.payload().len();
                }
                if received_len >= total_len as usize {
                    break;
                }
            }
        }

        // Parse INIT response
        let response_message =
            Message::from_packets(&response_packets, None).map_err(|_| Error::Other)?;

        // INIT response format:
        // - 8 bytes: nonce (echo)
        // - 4 bytes: channel ID (big-endian)
        // - 1 byte: protocol version
        // - 1 byte: major device version
        // - 1 byte: minor device version
        // - 1 byte: build device version
        // - 1 byte: capabilities

        if response_message.data.len() < 12 {
            return Err(Error::Other);
        }

        // Verify nonce matches
        if response_message.data[0..8] != nonce {
            return Err(Error::Other);
        }

        // Extract channel ID (bytes 8-11, big-endian)
        let channel_id = u32::from_be_bytes([
            response_message.data[8],
            response_message.data[9],
            response_message.data[10],
            response_message.data[11],
        ]);

        Ok(channel_id)
    }

    /// Send a CTAP command and receive response
    ///
    /// This handles CTAP HID framing automatically.
    ///
    /// # Arguments
    /// * `cmd` - CTAP authenticator command (0x01=makeCredential, 0x02=getAssertion, 0x04=getInfo, etc.)
    /// * `data` - CBOR-encoded command parameters
    /// * `timeout_ms` - Timeout in milliseconds for reading response
    pub fn send_ctap_command(&mut self, cmd: u8, data: &[u8], timeout_ms: i32) -> Result<Vec<u8>> {
        // Use zero-allocation variant and convert to Vec
        let mut buffer = vec![0u8; 7609]; // Max CTAP response size
        let len = self.send_ctap_command_buf(cmd, data, &mut buffer, timeout_ms)?;
        buffer.truncate(len);
        Ok(buffer)
    }

    /// Send a CTAP command and write response to provided buffer (zero-allocation variant)
    ///
    /// This is the zero-allocation version of `send_ctap_command`. The caller provides
    /// a buffer to write the response into, and this method returns the number of bytes written.
    ///
    /// # Arguments
    ///
    /// * `cmd` - CTAP command byte (e.g., 0x01 for makeCredential, 0x02 for getAssertion)
    /// * `data` - CBOR-encoded command parameters
    /// * `response` - Buffer to write the response into (should be at least 7609 bytes for max CTAP response)
    /// * `timeout_ms` - Timeout in milliseconds for reading response
    ///
    /// # Returns
    ///
    /// Number of bytes written to the response buffer
    ///
    /// # Errors
    ///
    /// Returns `Error::Other` if the buffer is too small for the response
    pub fn send_ctap_command_buf(
        &mut self,
        cmd: u8,
        data: &[u8],
        response: &mut [u8],
        timeout_ms: i32,
    ) -> Result<usize> {
        // CTAP authenticator commands are sent via CTAP HID Cbor (0x10) command
        // Payload format: [ctap_cmd, ...cbor_data]
        // Use SmallVec: most CTAP requests are <256 bytes (getInfo, PIN ops, etc.)
        let mut payload: SmallVec<[u8; 256]> = SmallVec::new();
        payload.push(cmd);
        payload.extend_from_slice(data);

        let cmd_enum = Cmd::Cbor;

        // Get or allocate channel
        let mut inner = self.inner.lock().unwrap();

        // Initialize channel if needed
        let needs_init = match &*inner {
            #[cfg(feature = "usb")]
            TransportInner::Usb { channel_id, .. } => channel_id.is_none(),
            #[cfg(target_os = "linux")]
            TransportInner::Uhid { channel_id, .. } => channel_id.is_none(),

            #[cfg(not(any(feature = "usb", target_os = "linux")))]
            _ => false,
        };

        if needs_init {
            drop(inner); // Release lock before calling init_channel
            let allocated_channel = self.init_channel()?;
            inner = self.inner.lock().unwrap();

            // Store the allocated channel
            match &mut *inner {
                #[cfg(feature = "usb")]
                TransportInner::Usb { channel_id, .. } => {
                    *channel_id = Some(allocated_channel);
                }
                #[cfg(target_os = "linux")]
                TransportInner::Uhid { channel_id, .. } => {
                    *channel_id = Some(allocated_channel);
                }

                #[cfg(not(any(feature = "usb", target_os = "linux")))]
                _ => {}
            }
        }

        // Get the channel ID to use (all CTAP2 commands use allocated channel)
        let channel_id = match &*inner {
            #[cfg(feature = "usb")]
            TransportInner::Usb { channel_id, .. } => channel_id.ok_or(Error::Other)?,
            #[cfg(target_os = "linux")]
            TransportInner::Uhid { channel_id, .. } => channel_id.ok_or(Error::Other)?,

            #[cfg(not(any(feature = "usb", target_os = "linux")))]
            _ => return Err(Error::Other),
        };

        // Build CTAP HID message (convert SmallVec to Vec for Message API)
        let message = Message::new(channel_id, cmd_enum, payload.to_vec(), None);

        // Fragment into packets
        let packets = message.to_packets().map_err(|_| Error::Other)?;

        // Send packets
        for packet in &packets {
            let packet_bytes = packet.as_bytes();
            drop(inner); // Release lock before write
            self.write(packet_bytes)?;
            inner = self.inner.lock().unwrap();
        }

        // Read response packets (use SmallVec to avoid allocation for small responses)
        // Most CTAP responses are ≤200 bytes (~4 packets), so we can stack-allocate
        let mut response_packets: SmallVec<[Packet; 4]> = SmallVec::new();

        loop {
            drop(inner); // Release lock before read
            let mut buffer = [0u8; 64];
            let bytes_read = self.read(&mut buffer, timeout_ms)?;
            inner = self.inner.lock().unwrap();

            if bytes_read == 0 {
                return Err(Error::Timeout);
            }

            // Parse packet
            let packet = Packet::from_bytes(buffer);

            // Check channel matches
            if packet.cid() != channel_id {
                continue; // Wrong channel, ignore
            }

            // Check for errors
            if let Some(cmd) = packet.cmd()
                && matches!(cmd, Cmd::Error)
            {
                return Err(Error::Other);
            }

            response_packets.push(packet);

            // Check if we have all packets
            if let Some(first) = response_packets.first()
                && let Some(total_len) = first.payload_len()
            {
                let mut received_len = first.payload().len();

                for pkt in &response_packets[1..] {
                    received_len += pkt.payload().len();
                }

                if received_len >= total_len as usize {
                    break;
                }
            }
        }

        // Reassemble message directly into response buffer
        let response_message =
            Message::from_packets(&response_packets, None).map_err(|_| Error::Other)?;

        let response_data = response_message.data;

        // Parse CTAP response (status byte + optional CBOR data)
        let cbor_data = Error::parse_ctap_response(&response_data)?;
        let cbor_len = cbor_data.len();
        if cbor_len > response.len() {
            return Err(Error::Other); // Buffer too small
        }
        response[..cbor_len].copy_from_slice(cbor_data);
        Ok(cbor_len)
    }

    /// Get a description of the transport
    pub fn get_description(&self) -> Result<String> {
        let inner = self.inner.lock().unwrap();
        match &*inner {
            #[cfg(feature = "usb")]
            TransportInner::Usb { .. } => Ok("USB HID Transport".to_string()),
            #[cfg(target_os = "linux")]
            TransportInner::Uhid { .. } => Ok("UHID Virtual Device".to_string()),

            #[cfg(not(any(feature = "usb", target_os = "linux")))]
            _ => Ok("Unknown Transport".to_string()),
        }
    }
}

impl Drop for Transport {
    fn drop(&mut self) {
        self.close();
    }
}

/// Safe Rust wrapper for TransportList
pub struct TransportList {
    transports: Vec<Transport>,
}

impl TransportList {
    /// Enumerate all available transports
    pub fn enumerate() -> Result<Self> {
        #[allow(unused_mut)]
        let mut transports = Vec::new();

        #[cfg(feature = "usb")]
        {
            // Initialize USB library
            if let Ok(api) = init_usb() {
                // Enumerate USB FIDO devices
                if let Ok(devices) = enumerate_devices(&api) {
                    for device_info in devices {
                        // Open the device using its path
                        if let Ok(transport) = RawUsbTransport::open(&api, &device_info.path) {
                            transports.push(Transport::from_usb(transport));
                        }
                    }
                }
            }
        }

        Ok(TransportList { transports })
    }

    /// Get the number of transports
    pub fn len(&self) -> usize {
        self.transports.len()
    }

    /// Check if the list is empty
    pub fn is_empty(&self) -> bool {
        self.transports.is_empty()
    }

    /// Get a transport at the given index
    pub fn get(&self, index: usize) -> Option<Transport> {
        self.transports.get(index).map(|t| Transport {
            inner: Arc::clone(&t.inner),
        })
    }

    /// Iterate over all transports
    pub fn iter(&self) -> impl Iterator<Item = Transport> + '_ {
        self.transports.iter().map(|t| Transport {
            inner: Arc::clone(&t.inner),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_transport_list_enumerate() {
        // Should not panic even if no devices available
        let list = TransportList::enumerate();
        assert!(list.is_ok());
    }
}