| 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 |
// Allow dead code in this module, since it's all packet consts anyways. |
| 6 |
#![allow(dead_code)] |
| 7 |
|
| 8 |
use serde::Serialize; |
| 9 |
|
| 10 |
pub const MAX_HID_RPT_SIZE: usize = 64; |
| 11 |
|
| 12 |
/// Minimum size of the U2F Raw Message header (FIDO v1.x) in extended mode, |
| 13 |
/// including expected response length (L<sub>e</sub>). |
| 14 |
/// |
| 15 |
/// Fields `CLA`, `INS`, `P1` and `P2` are 1 byte each, and L<sub>e</sub> is 3 |
| 16 |
/// bytes. If there is a data payload, add 2 bytes (L<sub>c</sub> is 3 bytes, |
| 17 |
/// and L<sub>e</sub> is 2 bytes). |
| 18 |
pub const U2FAPDUHEADER_SIZE: usize = 7; |
| 19 |
|
| 20 |
pub const CID_BROADCAST: [u8; 4] = [0xff, 0xff, 0xff, 0xff]; |
| 21 |
pub const TYPE_MASK: u8 = 0x80; |
| 22 |
pub const TYPE_INIT: u8 = 0x80; |
| 23 |
pub const TYPE_CONT: u8 = 0x80; |
| 24 |
|
| 25 |
// Size of header in U2F Init USB HID Packets |
| 26 |
pub const INIT_HEADER_SIZE: usize = 7; |
| 27 |
// Size of header in U2F Cont USB HID Packets |
| 28 |
pub const CONT_HEADER_SIZE: usize = 5; |
| 29 |
|
| 30 |
pub const PARAMETER_SIZE: usize = 32; |
| 31 |
|
| 32 |
pub const FIDO_USAGE_PAGE: u16 = 0xf1d0; // FIDO alliance HID usage page |
| 33 |
pub const FIDO_USAGE_U2FHID: u16 = 0x01; // U2FHID usage for top-level collection |
| 34 |
pub const FIDO_USAGE_DATA_IN: u8 = 0x20; // Raw IN data report |
| 35 |
pub const FIDO_USAGE_DATA_OUT: u8 = 0x21; // Raw OUT data report |
| 36 |
|
| 37 |
// General pub constants |
| 38 |
|
| 39 |
pub const U2FHID_IF_VERSION: u32 = 2; // Current interface implementation version |
| 40 |
pub const U2FHID_FRAME_TIMEOUT: u32 = 500; // Default frame timeout in ms |
| 41 |
pub const U2FHID_TRANS_TIMEOUT: u32 = 3000; // Default message timeout in ms |
| 42 |
|
| 43 |
// CTAPHID native commands |
| 44 |
const CTAPHID_PING: u8 = TYPE_INIT | 0x01; // Echo data through local processor only |
| 45 |
const CTAPHID_MSG: u8 = TYPE_INIT | 0x03; // Send U2F message frame |
| 46 |
const CTAPHID_LOCK: u8 = TYPE_INIT | 0x04; // Send lock channel command |
| 47 |
const CTAPHID_INIT: u8 = TYPE_INIT | 0x06; // Channel initialization |
| 48 |
const CTAPHID_WINK: u8 = TYPE_INIT | 0x08; // Send device identification wink |
| 49 |
const CTAPHID_CBOR: u8 = TYPE_INIT | 0x10; // Encapsulated CBOR encoded message |
| 50 |
const CTAPHID_CANCEL: u8 = TYPE_INIT | 0x11; // Cancel outstanding requests |
| 51 |
const CTAPHID_KEEPALIVE: u8 = TYPE_INIT | 0x3b; // Keepalive sent to authenticator every 100ms and whenever a status changes |
| 52 |
const CTAPHID_ERROR: u8 = TYPE_INIT | 0x3f; // Error response |
| 53 |
|
| 54 |
#[derive(Debug, PartialEq, Eq, Copy, Clone)] |
| 55 |
#[repr(u8)] |
| 56 |
pub enum HIDCmd { |
| 57 |
Ping, |
| 58 |
Msg, |
| 59 |
Lock, |
| 60 |
Init, |
| 61 |
Wink, |
| 62 |
Cbor, |
| 63 |
Cancel, |
| 64 |
Keepalive, |
| 65 |
Error, |
| 66 |
Unknown(u8), |
| 67 |
} |
| 68 |
|
| 69 |
impl From<HIDCmd> for u8 { |
| 70 |
fn from(v: HIDCmd) -> u8 { |
| 71 |
match v { |
| 72 |
HIDCmd::Ping => CTAPHID_PING, |
| 73 |
HIDCmd::Msg => CTAPHID_MSG, |
| 74 |
HIDCmd::Lock => CTAPHID_LOCK, |
| 75 |
HIDCmd::Init => CTAPHID_INIT, |
| 76 |
HIDCmd::Wink => CTAPHID_WINK, |
| 77 |
HIDCmd::Cbor => CTAPHID_CBOR, |
| 78 |
HIDCmd::Cancel => CTAPHID_CANCEL, |
| 79 |
HIDCmd::Keepalive => CTAPHID_KEEPALIVE, |
| 80 |
HIDCmd::Error => CTAPHID_ERROR, |
| 81 |
HIDCmd::Unknown(v) => v, |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
impl From<u8> for HIDCmd { |
| 87 |
fn from(v: u8) -> HIDCmd { |
| 88 |
match v { |
| 89 |
CTAPHID_PING => HIDCmd::Ping, |
| 90 |
CTAPHID_MSG => HIDCmd::Msg, |
| 91 |
CTAPHID_LOCK => HIDCmd::Lock, |
| 92 |
CTAPHID_INIT => HIDCmd::Init, |
| 93 |
CTAPHID_WINK => HIDCmd::Wink, |
| 94 |
CTAPHID_CBOR => HIDCmd::Cbor, |
| 95 |
CTAPHID_CANCEL => HIDCmd::Cancel, |
| 96 |
CTAPHID_KEEPALIVE => HIDCmd::Keepalive, |
| 97 |
CTAPHID_ERROR => HIDCmd::Error, |
| 98 |
v => HIDCmd::Unknown(v), |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// U2FHID_MSG commands |
| 104 |
pub const U2F_VENDOR_FIRST: u8 = TYPE_INIT | 0x40; // First vendor defined command |
| 105 |
pub const U2F_VENDOR_LAST: u8 = TYPE_INIT | 0x7f; // Last vendor defined command |
| 106 |
pub const U2F_REGISTER: u8 = 0x01; // Registration command |
| 107 |
pub const U2F_AUTHENTICATE: u8 = 0x02; // Authenticate/sign command |
| 108 |
pub const U2F_VERSION: u8 = 0x03; // Read version string command |
| 109 |
|
| 110 |
pub const YKPIV_INS_GET_VERSION: u8 = 0xfd; // Get firmware version, yubico ext |
| 111 |
|
| 112 |
// U2F_REGISTER command defines |
| 113 |
pub const U2F_REGISTER_ID: u8 = 0x05; // Version 2 registration identifier |
| 114 |
pub const U2F_REGISTER_HASH_ID: u8 = 0x00; // Version 2 hash identintifier |
| 115 |
|
| 116 |
// U2F_AUTHENTICATE command defines |
| 117 |
pub const U2F_REQUEST_USER_PRESENCE: u8 = 0x03; // Verify user presence and sign |
| 118 |
pub const U2F_CHECK_IS_REGISTERED: u8 = 0x07; // Check if the key handle is registered |
| 119 |
pub const U2F_DONT_ENFORCE_USER_PRESENCE_AND_SIGN: u8 = 0x08; // Sign, but don't verify user presence |
| 120 |
|
| 121 |
// U2FHID_INIT command defines |
| 122 |
pub const INIT_NONCE_SIZE: usize = 8; // Size of channel initialization challenge |
| 123 |
|
| 124 |
bitflags! { |
| 125 |
#[derive(Serialize)] |
| 126 |
pub struct Capability: u8 { |
| 127 |
const WINK = 0x01; |
| 128 |
const LOCK = 0x02; |
| 129 |
const CBOR = 0x04; |
| 130 |
const NMSG = 0x08; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// Low-level error codes. Return as negatives. |
| 135 |
|
| 136 |
pub const ERR_NONE: u8 = 0x00; // No error |
| 137 |
pub const ERR_INVALID_CMD: u8 = 0x01; // Invalid command |
| 138 |
pub const ERR_INVALID_PAR: u8 = 0x02; // Invalid parameter |
| 139 |
pub const ERR_INVALID_LEN: u8 = 0x03; // Invalid message length |
| 140 |
pub const ERR_INVALID_SEQ: u8 = 0x04; // Invalid message sequencing |
| 141 |
pub const ERR_MSG_TIMEOUT: u8 = 0x05; // Message has timed out |
| 142 |
pub const ERR_CHANNEL_BUSY: u8 = 0x06; // Channel busy |
| 143 |
pub const ERR_LOCK_REQUIRED: u8 = 0x0a; // Command requires channel lock |
| 144 |
pub const ERR_INVALID_CID: u8 = 0x0b; // Command not allowed on this cid |
| 145 |
pub const ERR_OTHER: u8 = 0x7f; // Other unspecified error |
| 146 |
|
| 147 |
// These are ISO 7816-4 defined response status words. |
| 148 |
pub const SW_NO_ERROR: [u8; 2] = [0x90, 0x00]; |
| 149 |
pub const SW_CONDITIONS_NOT_SATISFIED: [u8; 2] = [0x69, 0x85]; |
| 150 |
pub const SW_WRONG_DATA: [u8; 2] = [0x6A, 0x80]; |
| 151 |
pub const SW_WRONG_LENGTH: [u8; 2] = [0x67, 0x00]; |