| 1 |
use super::{Command, CommandError, RequestCtap2, StatusCode}; |
| 2 |
use crate::transport::errors::HIDError; |
| 3 |
use crate::transport::{FidoDevice, VirtualFidoDevice}; |
| 4 |
use serde_cbor::{de::from_slice, Value}; |
| 5 |
|
| 6 |
#[derive(Debug, Default)] |
| 7 |
pub struct Reset {} |
| 8 |
|
| 9 |
impl RequestCtap2 for Reset { |
| 10 |
type Output = (); |
| 11 |
|
| 12 |
fn command(&self) -> Command { |
| 13 |
Command::Reset |
| 14 |
} |
| 15 |
|
| 16 |
fn wire_format(&self) -> Result<Vec<u8>, HIDError> { |
| 17 |
Ok(Vec::new()) |
| 18 |
} |
| 19 |
|
| 20 |
fn handle_response_ctap2<Dev: FidoDevice>( |
| 21 |
&self, |
| 22 |
_dev: &mut Dev, |
| 23 |
input: &[u8], |
| 24 |
) -> Result<Self::Output, HIDError> { |
| 25 |
if input.is_empty() { |
| 26 |
return Err(CommandError::InputTooSmall.into()); |
| 27 |
} |
| 28 |
|
| 29 |
let status: StatusCode = input[0].into(); |
| 30 |
|
| 31 |
if status.is_ok() { |
| 32 |
Ok(()) |
| 33 |
} else { |
| 34 |
let msg = if input.len() > 1 { |
| 35 |
let data: Value = from_slice(&input[1..]).map_err(CommandError::Deserializing)?; |
| 36 |
Some(data) |
| 37 |
} else { |
| 38 |
None |
| 39 |
}; |
| 40 |
Err(CommandError::StatusCode(status, msg).into()) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
fn send_to_virtual_device<Dev: VirtualFidoDevice>( |
| 45 |
&self, |
| 46 |
dev: &mut Dev, |
| 47 |
) -> Result<Self::Output, HIDError> { |
| 48 |
dev.reset(self) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
#[cfg(test)] |
| 53 |
pub mod tests { |
| 54 |
use super::*; |
| 55 |
use crate::consts::HIDCmd; |
| 56 |
use crate::transport::device_selector::Device; |
| 57 |
use crate::transport::{hid::HIDDevice, FidoDevice, FidoDeviceIO, FidoProtocol}; |
| 58 |
use rand::{thread_rng, RngCore}; |
| 59 |
use serde_cbor::{de::from_slice, Value}; |
| 60 |
|
| 61 |
fn issue_command_and_get_response(cmd: u8, add: &[u8]) -> Result<(), HIDError> { |
| 62 |
let mut device = Device::new("commands/Reset").unwrap(); |
| 63 |
assert_eq!(device.get_protocol(), FidoProtocol::CTAP2); |
| 64 |
// ctap2 request |
| 65 |
let mut cid = [0u8; 4]; |
| 66 |
thread_rng().fill_bytes(&mut cid); |
| 67 |
device.set_cid(cid); |
| 68 |
|
| 69 |
let mut msg = cid.to_vec(); |
| 70 |
msg.extend(vec![HIDCmd::Cbor.into(), 0x00, 0x1]); // cmd + bcnt |
| 71 |
msg.extend(vec![0x07]); // authenticatorReset |
| 72 |
device.add_write(&msg, 0); |
| 73 |
|
| 74 |
// ctap2 response |
| 75 |
let len = 0x1 + add.len() as u8; |
| 76 |
let mut msg = cid.to_vec(); |
| 77 |
msg.extend(vec![HIDCmd::Cbor.into(), 0x00, len]); // cmd + bcnt |
| 78 |
msg.push(cmd); // Status code |
| 79 |
msg.extend(add); // + maybe additional data |
| 80 |
device.add_read(&msg, 0); |
| 81 |
|
| 82 |
device.send_cbor(&Reset {}) |
| 83 |
} |
| 84 |
|
| 85 |
#[test] |
| 86 |
fn test_select_ctap2_only() { |
| 87 |
// Test, if we can parse the status codes specified by the spec |
| 88 |
|
| 89 |
// Ok() |
| 90 |
issue_command_and_get_response(0, &[]).expect("Unexpected error"); |
| 91 |
|
| 92 |
// Denied by the user |
| 93 |
let response = issue_command_and_get_response(0x27, &[]).expect_err("Not an error!"); |
| 94 |
assert!(matches!( |
| 95 |
response, |
| 96 |
HIDError::Command(CommandError::StatusCode(StatusCode::OperationDenied, None)) |
| 97 |
)); |
| 98 |
|
| 99 |
// Timeout |
| 100 |
let response = issue_command_and_get_response(0x2F, &[]).expect_err("Not an error!"); |
| 101 |
assert!(matches!( |
| 102 |
response, |
| 103 |
HIDError::Command(CommandError::StatusCode( |
| 104 |
StatusCode::UserActionTimeout, |
| 105 |
None |
| 106 |
)) |
| 107 |
)); |
| 108 |
|
| 109 |
// Unexpected error with more random CBOR-data |
| 110 |
let add_data = vec![ |
| 111 |
0x63, // text(3) |
| 112 |
0x61, 0x6c, 0x67, // "alg" |
| 113 |
]; |
| 114 |
let response = issue_command_and_get_response(0x02, &add_data).expect_err("Not an error!"); |
| 115 |
match response { |
| 116 |
HIDError::Command(CommandError::StatusCode(StatusCode::InvalidParameter, Some(d))) => { |
| 117 |
let expected: Value = from_slice(&add_data).unwrap(); |
| 118 |
assert_eq!(d, expected) |
| 119 |
} |
| 120 |
e => panic!("Not the expected response: {:?}", e), |
| 121 |
} |
| 122 |
} |
| 123 |
} |