|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +use std::io; |
| 6 | +use std::io::{Read, Write}; |
| 7 | +use std::cmp; |
| 8 | +use rand::{thread_rng, RngCore}; |
| 9 | + |
| 10 | +use platform::device::USBDevice; |
| 11 | +use u2ftypes::*; |
| 12 | +use consts::*; |
| 13 | +use util::{io_err, trace_hex}; |
| 14 | + |
| 15 | +// Represents U2F HID Devices. Requires getters/setters for the |
| 16 | +// channel ID, created during device initialization. |
| 17 | +pub struct HIDDevice<'a> { |
| 18 | + base: &'a mut USBDevice, |
| 19 | + cid: [u8; 4], |
| 20 | +} |
| 21 | + |
| 22 | +impl<'a> HIDDevice<'a> { |
| 23 | + pub fn new(base: &'a mut USBDevice) -> HIDDevice<'a> { |
| 24 | + Self { |
| 25 | + base, |
| 26 | + cid: CID_BROADCAST, |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + pub fn get_cid(&self) -> &[u8; 4] { |
| 31 | + &self.cid |
| 32 | + } |
| 33 | + |
| 34 | + pub fn set_cid(&mut self, cid: [u8; 4]) { |
| 35 | + self.cid = cid; |
| 36 | + } |
| 37 | + |
| 38 | + fn sendrecv(&mut self, cmd: u8, send: &[u8]) -> io::Result<Vec<u8>> |
| 39 | + { |
| 40 | + // Send initialization packet. |
| 41 | + let mut count = U2FHIDInit::write(self, cmd, send)?; |
| 42 | + |
| 43 | + // Send continuation packets. |
| 44 | + let mut sequence = 0u8; |
| 45 | + while count < send.len() { |
| 46 | + count += U2FHIDCont::write(self, sequence, &send[count..])?; |
| 47 | + sequence += 1; |
| 48 | + } |
| 49 | + |
| 50 | + // Now we read. This happens in 2 chunks: The initial packet, which has the |
| 51 | + // size we expect overall, then continuation packets, which will fill in |
| 52 | + // data until we have everything. |
| 53 | + let mut data = U2FHIDInit::read(self)?; |
| 54 | + |
| 55 | + let mut sequence = 0u8; |
| 56 | + while data.len() < data.capacity() { |
| 57 | + let max = data.capacity() - data.len(); |
| 58 | + data.extend_from_slice(&U2FHIDCont::read(self, sequence, max)?); |
| 59 | + sequence += 1; |
| 60 | + } |
| 61 | + |
| 62 | + Ok(data) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl Read for HIDDevice<'_> { |
| 67 | + fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 68 | + self.base.read(buf) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl Write for HIDDevice<'_> { |
| 73 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 74 | + self.base.write(buf) |
| 75 | + } |
| 76 | + |
| 77 | + // USB HID writes don't buffer, so this will be a nop. |
| 78 | + fn flush(&mut self) -> io::Result<()> { |
| 79 | + Ok(()) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +impl APDUDevice for HIDDevice<'_> { |
| 85 | + fn init_apdu(&mut self) -> io::Result<()> { |
| 86 | + self.base.initialize()?; |
| 87 | + |
| 88 | + let mut nonce = [0u8; 8]; |
| 89 | + thread_rng().fill_bytes(&mut nonce); |
| 90 | + assert_eq!(nonce.len(), INIT_NONCE_SIZE); |
| 91 | + let raw = self.sendrecv(U2FHID_INIT, &nonce)?; |
| 92 | + self.set_cid(U2FHIDInitResp::read(&raw, &nonce)?); |
| 93 | + |
| 94 | + Ok(()) |
| 95 | + } |
| 96 | + |
| 97 | + fn send_apdu(&mut self, cmd: u8, p1: u8, send: &[u8]) -> io::Result<(Vec<u8>, [u8; 2])> { |
| 98 | + let out = APDU::serialize_long(cmd, p1, send)?; |
| 99 | + trace_hex("USB send", &out); |
| 100 | + let ret = self.sendrecv(U2FHID_MSG, &out)?; |
| 101 | + trace_hex("USB recv", &ret); |
| 102 | + APDU::deserialize(ret) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Init structure for U2F Communications. Tells the receiver what channel |
| 107 | +// communication is happening on, what command is running, and how much data to |
| 108 | +// expect to receive over all. |
| 109 | +// |
| 110 | +// Spec at https://fidoalliance.org/specs/fido-u2f-v1. |
| 111 | +// 0-nfc-bt-amendment-20150514/fido-u2f-hid-protocol.html#message--and-packet-structure |
| 112 | +pub struct U2FHIDInit {} |
| 113 | + |
| 114 | +impl U2FHIDInit { |
| 115 | + pub fn read(dev: &mut HIDDevice) -> io::Result<Vec<u8>> |
| 116 | + { |
| 117 | + let mut frame = [0u8; HID_RPT_SIZE]; |
| 118 | + let mut count = dev.read(&mut frame)?; |
| 119 | + |
| 120 | + while dev.get_cid() != &frame[..4] { |
| 121 | + count = dev.read(&mut frame)?; |
| 122 | + } |
| 123 | + |
| 124 | + if count != HID_RPT_SIZE { |
| 125 | + return Err(io_err("invalid init packet")); |
| 126 | + } |
| 127 | + |
| 128 | + let cap = (frame[5] as usize) << 8 | (frame[6] as usize); |
| 129 | + let mut data = Vec::with_capacity(cap); |
| 130 | + |
| 131 | + let len = cmp::min(cap, INIT_DATA_SIZE); |
| 132 | + data.extend_from_slice(&frame[7..7 + len]); |
| 133 | + |
| 134 | + Ok(data) |
| 135 | + } |
| 136 | + |
| 137 | + pub fn write(dev: &mut HIDDevice, cmd: u8, data: &[u8]) -> io::Result<usize> |
| 138 | + { |
| 139 | + if data.len() > 0xffff { |
| 140 | + return Err(io_err("payload length > 2^16")); |
| 141 | + } |
| 142 | + |
| 143 | + let mut frame = [0; HID_RPT_SIZE + 1]; |
| 144 | + frame[1..5].copy_from_slice(dev.get_cid()); |
| 145 | + frame[5] = cmd; |
| 146 | + frame[6] = (data.len() >> 8) as u8; |
| 147 | + frame[7] = data.len() as u8; |
| 148 | + |
| 149 | + let count = cmp::min(data.len(), INIT_DATA_SIZE); |
| 150 | + frame[8..8 + count].copy_from_slice(&data[..count]); |
| 151 | + |
| 152 | + if dev.write(&frame)? != frame.len() { |
| 153 | + return Err(io_err("device write failed")); |
| 154 | + } |
| 155 | + |
| 156 | + Ok(count) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// Continuation structure for U2F Communications. After an Init structure is |
| 161 | +// sent, continuation structures are used to transmit all extra data that |
| 162 | +// wouldn't fit in the initial packet. The sequence number increases with every |
| 163 | +// packet, until all data is received. |
| 164 | +// |
| 165 | +// https://fidoalliance.org/specs/fido-u2f-v1.0-nfc-bt-amendment-20150514/fido-u2f-hid-protocol. |
| 166 | +// html#message--and-packet-structure |
| 167 | +pub struct U2FHIDCont {} |
| 168 | + |
| 169 | +impl U2FHIDCont { |
| 170 | + pub fn read(dev: &mut HIDDevice, seq: u8, max: usize) -> io::Result<Vec<u8>> |
| 171 | + { |
| 172 | + let mut frame = [0u8; HID_RPT_SIZE]; |
| 173 | + let mut count = dev.read(&mut frame)?; |
| 174 | + |
| 175 | + while dev.get_cid() != &frame[..4] { |
| 176 | + count = dev.read(&mut frame)?; |
| 177 | + } |
| 178 | + |
| 179 | + if count != HID_RPT_SIZE { |
| 180 | + return Err(io_err("invalid cont packet")); |
| 181 | + } |
| 182 | + |
| 183 | + if seq != frame[4] { |
| 184 | + return Err(io_err("invalid sequence number")); |
| 185 | + } |
| 186 | + |
| 187 | + let max = cmp::min(max, CONT_DATA_SIZE); |
| 188 | + Ok(frame[5..5 + max].to_vec()) |
| 189 | + } |
| 190 | + |
| 191 | + pub fn write(dev: &mut HIDDevice, seq: u8, data: &[u8]) -> io::Result<usize> |
| 192 | + { |
| 193 | + let mut frame = [0; HID_RPT_SIZE + 1]; |
| 194 | + frame[1..5].copy_from_slice(dev.get_cid()); |
| 195 | + frame[5] = seq; |
| 196 | + |
| 197 | + let count = cmp::min(data.len(), CONT_DATA_SIZE); |
| 198 | + frame[6..6 + count].copy_from_slice(&data[..count]); |
| 199 | + |
| 200 | + if dev.write(&frame)? != frame.len() { |
| 201 | + return Err(io_err("device write failed")); |
| 202 | + } |
| 203 | + |
| 204 | + Ok(count) |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +// Reply sent after initialization command. Contains information about U2F USB |
| 209 | +// Key versioning, as well as the communication channel to be used for all |
| 210 | +// further requests. |
| 211 | +// |
| 212 | +// https://fidoalliance.org/specs/fido-u2f-v1.0-nfc-bt-amendment-20150514/fido-u2f-hid-protocol. |
| 213 | +// html#u2fhid_init |
| 214 | +pub struct U2FHIDInitResp {} |
| 215 | + |
| 216 | +impl U2FHIDInitResp { |
| 217 | + pub fn read(data: &[u8], nonce: &[u8]) -> io::Result<[u8; 4]> { |
| 218 | + assert_eq!(nonce.len(), INIT_NONCE_SIZE); |
| 219 | + |
| 220 | + if data.len() != INIT_NONCE_SIZE + 9 { |
| 221 | + return Err(io_err("invalid init response")); |
| 222 | + } |
| 223 | + |
| 224 | + if nonce != &data[..INIT_NONCE_SIZE] { |
| 225 | + return Err(io_err("invalid nonce")); |
| 226 | + } |
| 227 | + |
| 228 | + let mut cid = [0u8; 4]; |
| 229 | + cid.copy_from_slice(&data[INIT_NONCE_SIZE..INIT_NONCE_SIZE + 4]); |
| 230 | + Ok(cid) |
| 231 | + } |
| 232 | +} |
0 commit comments