Debian Codegraph @

third_party/rust/authenticator/src/ctap2/commands/get_version.rs

raw
1 use super::{CommandError, CtapResponse, RequestCtap1, Retryable};
2 use crate::consts::U2F_VERSION;
3 use crate::transport::errors::{ApduErrorStatus, HIDError};
4 use crate::transport::{FidoDevice, VirtualFidoDevice};
5 use crate::u2ftypes::CTAP1RequestAPDU;
6
7 #[allow(non_camel_case_types)]
8 #[derive(Debug, Clone, PartialEq, Eq)]
9 pub enum U2FInfo {
10 U2F_V2,
11 }
12
13 impl CtapResponse for U2FInfo {}
14
15 #[derive(Debug, Default)]
16 // TODO(baloo): if one does not issue U2F_VERSION before makecredentials or getassertion, token
17 // will return error (ConditionsNotSatified), test this in unit tests
18 pub struct GetVersion {}
19
20 impl RequestCtap1 for GetVersion {
21 type Output = U2FInfo;
22 type AdditionalInfo = ();
23
24 fn handle_response_ctap1<Dev: FidoDevice>(
25 &self,
26 _dev: &mut Dev,
27 _status: Result<(), ApduErrorStatus>,
28 input: &[u8],
29 _add_info: &(),
30 ) -> Result<Self::Output, Retryable<HIDError>> {
31 if input.is_empty() {
32 return Err(Retryable::Error(HIDError::Command(
33 CommandError::InputTooSmall,
34 )));
35 }
36
37 let expected = String::from("U2F_V2");
38 let result = String::from_utf8_lossy(input);
39 match result {
40 ref data if data == &expected => Ok(U2FInfo::U2F_V2),
41 _ => Err(Retryable::Error(HIDError::UnexpectedVersion)),
42 }
43 }
44
45 fn ctap1_format(&self) -> Result<(Vec<u8>, ()), HIDError> {
46 let flags = 0;
47
48 let cmd = U2F_VERSION;
49 let data = CTAP1RequestAPDU::serialize(cmd, flags, &[])?;
50 Ok((data, ()))
51 }
52
53 fn send_to_virtual_device<Dev: VirtualFidoDevice>(
54 &self,
55 dev: &mut Dev,
56 ) -> Result<Self::Output, HIDError> {
57 dev.get_version(self)
58 }
59 }
60
61 #[cfg(test)]
62 pub mod tests {
63 use crate::consts::{Capability, HIDCmd, CID_BROADCAST, SW_NO_ERROR};
64 use crate::transport::device_selector::Device;
65 use crate::transport::{hid::HIDDevice, FidoDevice, FidoProtocol};
66 use rand::{thread_rng, RngCore};
67
68 #[test]
69 fn test_get_version_ctap1_only() {
70 let mut device = Device::new("commands/get_version").unwrap();
71 device.downgrade_to_ctap1();
72 assert_eq!(device.get_protocol(), FidoProtocol::CTAP1);
73 let nonce = [0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01];
74
75 // channel id
76 let mut cid = [0u8; 4];
77 thread_rng().fill_bytes(&mut cid);
78
79 // init packet
80 let mut msg = CID_BROADCAST.to_vec();
81 msg.extend([HIDCmd::Init.into(), 0x00, 0x08]); // cmd + bcnt
82 msg.extend_from_slice(&nonce);
83 device.add_write(&msg, 0);
84
85 // init_resp packet
86 let mut msg = CID_BROADCAST.to_vec();
87 msg.extend(vec![
88 0x06, /* HIDCmd::Init without !TYPE_INIT */
89 0x00, 0x11,
90 ]); // cmd + bcnt
91 msg.extend_from_slice(&nonce);
92 msg.extend_from_slice(&cid); // new channel id
93
94 // We are not setting CBOR, to signal that the device does not support CTAP1
95 msg.extend([0x02, 0x04, 0x01, 0x08, 0x01]); // versions + flags (wink)
96 device.add_read(&msg, 0);
97
98 // ctap1 U2F_VERSION request
99 let mut msg = cid.to_vec();
100 msg.extend([HIDCmd::Msg.into(), 0x0, 0x7]); // cmd + bcnt
101 msg.extend([0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0]);
102 device.add_write(&msg, 0);
103
104 // fido response
105 let mut msg = cid.to_vec();
106 msg.extend([HIDCmd::Msg.into(), 0x0, 0x08]); // cmd + bcnt
107 msg.extend([0x55, 0x32, 0x46, 0x5f, 0x56, 0x32]); // 'U2F_V2'
108 msg.extend(SW_NO_ERROR);
109 device.add_read(&msg, 0);
110
111 device.init().expect("Failed to init device");
112
113 assert_eq!(device.get_cid(), &cid);
114
115 let dev_info = device.get_device_info();
116 assert_eq!(dev_info.cap_flags, Capability::WINK);
117
118 let result = device.get_authenticator_info();
119 assert!(result.is_none());
120 }
121 }