forked from solokeys/admin-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.rs
411 lines (371 loc) · 12.1 KB
/
admin.rs
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
use super::Client as TrussedClient;
use apdu_dispatch::iso7816::Status;
use apdu_dispatch::{app as apdu, command, response, Command as ApduCommand};
use cbor_smol::cbor_deserialize;
use core::{convert::TryInto, marker::PhantomData, time::Duration};
use ctaphid_dispatch::app::{self as hid, Command as HidCommand, Message};
use ctaphid_dispatch::command::VendorCommand;
use serde::Deserialize;
use trussed::{interrupt::InterruptFlag, store::filestore::Filestore, syscall, types::Vec};
use crate::config::{self, Config, ConfigError};
pub const USER_PRESENCE_TIMEOUT_SECS: u32 = 15;
// New commands are only available over this vendor command (acting as a namespace for this
// application). The actual application command is stored in the first byte of the packet data.
const ADMIN: VendorCommand = VendorCommand::H72;
const STATUS: u8 = 0x80;
const TEST_SE050: u8 = 0x81;
const GET_CONFIG: u8 = 0x82;
const SET_CONFIG: u8 = 0x83;
// For compatibility, old commands are also available directly as separate vendor commands.
const UPDATE: VendorCommand = VendorCommand::H51;
const REBOOT: VendorCommand = VendorCommand::H53;
const RNG: VendorCommand = VendorCommand::H60;
const VERSION: VendorCommand = VendorCommand::H61;
const UUID: VendorCommand = VendorCommand::H62;
const LOCKED: VendorCommand = VendorCommand::H63;
// We also handle the standard wink command.
const WINK: HidCommand = HidCommand::Wink; // 0x08
const RNG_DATA_LEN: usize = 57;
const CONFIG_OK: u8 = 0x00;
#[derive(PartialEq, Debug)]
enum Command {
Update,
Reboot,
Rng,
Version,
Uuid,
Locked,
Wink,
Status,
TestSe05X,
GetConfig,
SetConfig,
}
impl TryFrom<u8> for Command {
type Error = Error;
fn try_from(command: u8) -> Result<Self, Self::Error> {
// First, check the old commands.
if let Ok(command) = HidCommand::try_from(command) {
if let Ok(command) = command.try_into() {
return Ok(command);
}
}
// Now check the new commands.
match command {
STATUS => Ok(Command::Status),
TEST_SE050 => Ok(Command::TestSe05X),
GET_CONFIG => Ok(Command::GetConfig),
SET_CONFIG => Ok(Command::SetConfig),
_ => Err(Error::UnsupportedCommand),
}
}
}
impl TryFrom<HidCommand> for Command {
type Error = Error;
fn try_from(command: HidCommand) -> Result<Self, Self::Error> {
match command {
WINK => Ok(Command::Wink),
HidCommand::Vendor(command) => command.try_into(),
_ => Err(Error::UnsupportedCommand),
}
}
}
impl TryFrom<VendorCommand> for Command {
type Error = Error;
fn try_from(command: VendorCommand) -> Result<Self, Self::Error> {
match command {
UPDATE => Ok(Command::Update),
REBOOT => Ok(Command::Reboot),
RNG => Ok(Command::Rng),
VERSION => Ok(Command::Version),
UUID => Ok(Command::Uuid),
LOCKED => Ok(Command::Locked),
_ => Err(Error::UnsupportedCommand),
}
}
}
enum Error {
InvalidLength,
NotAvailable,
UnsupportedCommand,
}
impl From<Error> for hid::Error {
fn from(error: Error) -> Self {
match error {
Error::InvalidLength => Self::InvalidLength,
// TODO: use more appropriate error code
Error::NotAvailable => Self::InvalidLength,
Error::UnsupportedCommand => Self::InvalidCommand,
}
}
}
impl From<Error> for Status {
fn from(error: Error) -> Self {
match error {
Error::InvalidLength => Self::WrongLength,
Error::NotAvailable => Self::ConditionsOfUseNotSatisfied,
Error::UnsupportedCommand => Self::InstructionNotSupportedOrInvalid,
}
}
}
#[derive(Debug, Deserialize)]
struct SetConfigRequest<'a> {
key: &'a str,
value: &'a str,
}
pub trait Reboot {
/// Reboots the device.
fn reboot() -> !;
/// Reboots the device.
///
/// Presuming the device has a separate mode of operation that
/// allows updating its firmware (for instance, a bootloader),
/// reboots the device into this mode.
fn reboot_to_firmware_update() -> !;
/// Reboots the device.
///
/// Presuming the device has a separate destructive but more
/// reliable way of rebooting into the firmware mode of operation,
/// does so.
fn reboot_to_firmware_update_destructive() -> !;
/// Is device bootloader locked down?
/// E.g., is secure boot enabled?
fn locked() -> bool;
}
pub struct App<T, R, S, C> {
trussed: T,
uuid: [u8; 16],
version: u32,
full_version: &'static str,
status: S,
boot_interface: PhantomData<R>,
config: C,
}
impl<T, R, S, C> App<T, R, S, C>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
C: Config,
{
pub fn load<F: Filestore>(
client: T,
filestore: &mut F,
uuid: [u8; 16],
version: u32,
full_version: &'static str,
status: S,
) -> Self {
config::load(filestore)
.map(|config| Self::new(client, uuid, version, full_version, status, config))
.unwrap()
}
fn new(
client: T,
uuid: [u8; 16],
version: u32,
full_version: &'static str,
status: S,
config: C,
) -> Self {
Self {
trussed: client,
uuid,
version,
full_version,
status,
boot_interface: PhantomData,
config,
}
}
pub fn config(&self) -> &C {
&self.config
}
fn user_present(&mut self) -> bool {
let user_present = syscall!(self
.trussed
.confirm_user_present(USER_PRESENCE_TIMEOUT_SECS * 1000))
.result;
user_present.is_ok()
}
fn exec<const N: usize>(
&mut self,
command: Command,
input: &[u8],
response: &mut Vec<u8, N>,
) -> Result<(), Error> {
debug_now!("Executing command: {command:?}");
match command {
Command::Reboot => R::reboot(),
Command::Locked => {
response.push(R::locked().into()).ok();
}
Command::Rng => {
// Fill the HID packet (57 bytes)
response
.extend_from_slice(&syscall!(self.trussed.random_bytes(RNG_DATA_LEN)).bytes)
.ok();
}
Command::Update => {
if self.user_present() {
if input.first().copied() == Some(0x01) {
R::reboot_to_firmware_update_destructive();
} else {
R::reboot_to_firmware_update();
}
} else {
return Err(Error::NotAvailable);
}
}
Command::Uuid => {
// Get UUID
response.extend_from_slice(&self.uuid).ok();
}
Command::Version => {
// GET VERSION
if input.first().copied() == Some(0x01) {
response
.extend_from_slice(self.full_version.as_bytes())
.ok();
} else {
response.extend_from_slice(&self.version.to_be_bytes()).ok();
}
}
Command::Wink => {
debug_now!("winking");
syscall!(self.trussed.wink(Duration::from_secs(10)));
}
Command::Status => {
response.extend_from_slice(self.status.as_ref()).ok();
}
Command::TestSe05X => {
#[cfg(feature = "se050")]
{
let rep = syscall!(self.trussed.test_se050());
response.extend_from_slice(&rep.reply).ok();
return Ok(());
}
#[cfg(not(feature = "se050"))]
{
return Err(Error::NotAvailable);
}
}
Command::GetConfig => {
// Response: 1 status byte, then data if status == 0
response.push(CONFIG_OK).ok();
if let Err(error) = self.get_config(input, response) {
response.clear();
response.push(error.into()).ok();
}
}
Command::SetConfig => {
// Response: 1 status byte
let status = match self.set_config(input) {
Ok(()) => CONFIG_OK,
Err(error) => error.into(),
};
response.push(status).ok();
}
}
Ok(())
}
fn get_config<const N: usize>(
&mut self,
input: &[u8],
response: &mut Vec<u8, N>,
) -> Result<(), ConfigError> {
let key = core::str::from_utf8(input).map_err(|_| ConfigError::InvalidKey)?;
config::get(&mut self.config, key, response)
}
fn set_config(&mut self, input: &[u8]) -> Result<(), ConfigError> {
let request: SetConfigRequest<'_> =
cbor_deserialize(input).map_err(|_| ConfigError::DeserializationFailed)?;
config::set(&mut self.config, request.key, request.value)?;
config::save(&mut self.trussed, &self.config)
}
}
impl<T, R, S, C> hid::App<'static> for App<T, R, S, C>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
C: Config,
{
fn commands(&self) -> &'static [HidCommand] {
&[
HidCommand::Wink,
HidCommand::Vendor(ADMIN),
HidCommand::Vendor(UPDATE),
HidCommand::Vendor(REBOOT),
HidCommand::Vendor(RNG),
HidCommand::Vendor(VERSION),
HidCommand::Vendor(UUID),
HidCommand::Vendor(LOCKED),
]
}
fn call(
&mut self,
command: HidCommand,
input_data: &Message,
response: &mut Message,
) -> hid::AppResult {
let (command, input) = if command == HidCommand::Vendor(ADMIN) {
// new mode: first input byte specifies the actual command
let (command, input) = input_data.split_first().ok_or(Error::InvalidLength)?;
let command = Command::try_from(*command)?;
(command, input)
} else {
// old mode: directly use vendor commands + wink
(Command::try_from(command)?, input_data.as_slice())
};
self.exec(command, input, response).map_err(From::from)
}
fn interrupt(&self) -> Option<&'static InterruptFlag> {
self.trussed.interrupt()
}
}
impl<T, R, S, C> iso7816::App for App<T, R, S, C>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
{
// Solo management app
fn aid(&self) -> iso7816::Aid {
iso7816::Aid::new(&[0xA0, 0x00, 0x00, 0x08, 0x47, 0x00, 0x00, 0x00, 0x01])
}
}
impl<T, R, S, C> apdu::App<{ command::SIZE }, { response::SIZE }> for App<T, R, S, C>
where
T: TrussedClient,
R: Reboot,
S: AsRef<[u8]>,
C: Config,
{
fn select(&mut self, _apdu: &ApduCommand, _reply: &mut response::Data) -> apdu::Result {
Ok(())
}
fn deselect(&mut self) {}
fn call(
&mut self,
interface: apdu::Interface,
apdu: &ApduCommand,
reply: &mut response::Data,
) -> apdu::Result {
let instruction: u8 = apdu.instruction().into();
let command = Command::try_from(instruction)?;
// Reboot may only be called over USB
if command == Command::Reboot && interface != apdu::Interface::Contact {
return Err(Status::ConditionsOfUseNotSatisfied);
}
// The Update and Version commands use the P1 value to select an operation mode. As we
// cannot model this in the CTAPHID application, we pretend that we received the flag as
// the command payload.
if command == Command::Update || command == Command::Version {
self.exec(command, &[apdu.p1], reply)
} else {
self.exec(command, apdu.data().as_slice(), reply)
}
.map_err(From::from)
}
}