-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathio.rs
629 lines (577 loc) · 19.1 KB
/
io.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#[cfg(target_os = "nanox")]
use crate::ble;
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
use ledger_secure_sdk_sys::buttons::{get_button_event, ButtonEvent, ButtonsState};
use ledger_secure_sdk_sys::seph as sys_seph;
use ledger_secure_sdk_sys::*;
#[cfg(feature = "ccid")]
use crate::ccid;
use crate::seph;
use core::convert::{Infallible, TryFrom};
use core::ops::{Index, IndexMut};
#[derive(Copy, Clone)]
#[repr(u16)]
pub enum StatusWords {
Ok = 0x9000,
NothingReceived = 0x6982,
BadCla = 0x6e00,
BadIns = 0x6e01,
BadP1P2 = 0x6e02,
BadLen = 0x6e03,
UserCancelled = 0x6e04,
Unknown = 0x6d00,
Panic = 0xe000,
}
#[derive(Debug)]
#[repr(u8)]
pub enum SyscallError {
InvalidParameter = 2,
Overflow,
Security,
InvalidCrc,
InvalidChecksum,
InvalidCounter,
NotSupported,
InvalidState,
Timeout,
Unspecified,
}
impl From<u32> for SyscallError {
fn from(e: u32) -> SyscallError {
match e {
2 => SyscallError::InvalidParameter,
3 => SyscallError::Overflow,
4 => SyscallError::Security,
5 => SyscallError::InvalidCrc,
6 => SyscallError::InvalidChecksum,
7 => SyscallError::InvalidCounter,
8 => SyscallError::NotSupported,
9 => SyscallError::InvalidState,
10 => SyscallError::Timeout,
_ => SyscallError::Unspecified,
}
}
}
/// Provide a type that will be used for replying
/// an APDU with either a StatusWord or an SyscallError
#[derive(Debug)]
#[repr(transparent)]
pub struct Reply(pub u16);
impl From<StatusWords> for Reply {
fn from(sw: StatusWords) -> Reply {
Reply(sw as u16)
}
}
impl From<SyscallError> for Reply {
fn from(exc: SyscallError) -> Reply {
Reply(0x6800 + exc as u16)
}
}
// Needed because some methods use `TryFrom<ApduHeader>::Error`, and for `ApduHeader` we have
// `Error` as `Infallible`. Since we need to convert such error in a status word (`Reply`) we need
// to implement this trait here.
impl From<Infallible> for Reply {
fn from(_value: Infallible) -> Self {
Reply(0x9000)
}
}
/// Possible events returned by [`Comm::next_event`]
#[derive(Eq, PartialEq)]
pub enum Event<T> {
/// APDU event
Command(T),
/// Button press or release event
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
Button(ButtonEvent),
#[cfg(any(target_os = "stax", target_os = "flex"))]
TouchEvent,
/// Ticker
Ticker,
}
/// Manages the communication of the device: receives events such as button presses, incoming
/// APDU requests, and provides methods to build and transmit APDU responses.
pub struct Comm {
pub apdu_buffer: [u8; 260],
pub rx: usize,
pub tx: usize,
pub event_pending: bool,
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
buttons: ButtonsState,
/// Expected value for the APDU CLA byte.
/// If defined, [`Comm`] will automatically reply with [`StatusWords::BadCla`] when an APDU
/// with wrong CLA byte is received. If set to [`None`], all CLA are accepted.
/// Can be set using [`Comm::set_expected_cla`] method.
pub expected_cla: Option<u8>,
}
impl Default for Comm {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct ApduHeader {
/// Class
pub cla: u8,
/// Instruction
pub ins: u8,
/// Parameter 1
pub p1: u8,
/// Parameter 2
pub p2: u8,
}
impl Comm {
/// Creates a new [`Comm`] instance, which accepts any CLA APDU by default.
pub const fn new() -> Self {
Self {
apdu_buffer: [0u8; 260],
rx: 0,
tx: 0,
event_pending: false,
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
buttons: ButtonsState::new(),
expected_cla: None,
}
}
/// Defines [`Comm::expected_cla`] in order to reply automatically [`StatusWords::BadCla`] when
/// an incoming APDU has a CLA byte different from the given value.
///
/// # Arguments
///
/// * `cla` - Expected value for APDUs CLA byte.
///
/// # Examples
///
/// This method can be used when building an instance of [`Comm`]:
///
/// ```
/// let mut comm = Comm::new().set_expected_cla(0xe0);
/// ```
pub fn set_expected_cla(mut self, cla: u8) -> Self {
self.expected_cla = Some(cla);
self
}
/// Send the currently held APDU
// This is private. Users should call reply to set the satus word and
// transmit the response.
fn apdu_send(&mut self) {
if !sys_seph::is_status_sent() {
sys_seph::send_general_status()
}
let mut spi_buffer = [0u8; 128];
while sys_seph::is_status_sent() {
sys_seph::seph_recv(&mut spi_buffer, 0);
seph::handle_event(&mut self.apdu_buffer, &spi_buffer);
}
match unsafe { G_io_app.apdu_state } {
APDU_USB_HID => unsafe {
ledger_secure_sdk_sys::io_usb_hid_send(
Some(io_usb_send_apdu_data),
self.tx as u16,
self.apdu_buffer.as_mut_ptr(),
);
},
APDU_RAW => {
let len = (self.tx as u16).to_be_bytes();
sys_seph::seph_send(&[sys_seph::SephTags::RawAPDU as u8, len[0], len[1]]);
sys_seph::seph_send(&self.apdu_buffer[..self.tx]);
}
#[cfg(feature = "ccid")]
APDU_USB_CCID => {
ccid::send(&self.apdu_buffer[..self.tx]);
}
#[cfg(target_os = "nanox")]
APDU_BLE => {
ble::send(&self.apdu_buffer[..self.tx]);
}
_ => (),
}
self.tx = 0;
self.rx = 0;
unsafe {
G_io_app.apdu_state = APDU_IDLE;
G_io_app.apdu_media = IO_APDU_MEDIA_NONE;
G_io_app.apdu_length = 0;
}
}
/// Wait and return next button press event or APDU command.
///
/// `T` can be any type built from a [`ApduHeader`] using the [`TryFrom<ApduHeader>`] trait.
/// The conversion can embed complex parsing logic, including checks on CLA, INS, P1 and P2
/// bytes, and may return an error with a status word for invalid APDUs.
///
/// In particular, it is recommended to use an enumeration for the possible INS values.
///
/// # Examples
///
/// ```
/// enum Instruction {
/// Select,
/// ReadBinary
/// }
///
/// impl TryFrom<ApduHeader> for Instruction {
/// type Error = StatusWords;
///
/// fn try_from(h: ApduHeader) -> Result<Self, Self::Error> {
/// match h.ins {
/// 0xa4 => Ok(Self::Select),
/// 0xb0 => Ok(Self::ReadBinary)
/// _ => Err(StatusWords::BadIns)
/// }
/// }
/// }
///
/// loop {
/// match comm.next_event() {
/// Event::Button(button) => { ... }
/// Event::Command(Instruction::Select) => { ... }
/// Event::Command(Instruction::ReadBinary) => { ... }
/// }
/// }
/// ```
pub fn next_event<T>(&mut self) -> Event<T>
where
T: TryFrom<ApduHeader>,
Reply: From<<T as TryFrom<ApduHeader>>::Error>,
{
let mut spi_buffer = [0u8; 128];
unsafe {
G_io_app.apdu_state = APDU_IDLE;
G_io_app.apdu_media = IO_APDU_MEDIA_NONE;
G_io_app.apdu_length = 0;
}
loop {
// Signal end of command stream from SE to MCU
// And prepare reception
if !sys_seph::is_status_sent() {
sys_seph::send_general_status();
}
// Fetch the next message from the MCU
let _rx = sys_seph::seph_recv(&mut spi_buffer, 0);
if let Some(value) = self.decode_event(&mut spi_buffer) {
return value;
}
}
}
pub fn next_event_ahead<T>(&mut self) -> bool
where
T: TryFrom<ApduHeader>,
Reply: From<<T as TryFrom<ApduHeader>>::Error>,
{
let mut spi_buffer = [0u8; 128];
// Signal end of command stream from SE to MCU
// And prepare reception
if !sys_seph::is_status_sent() {
sys_seph::send_general_status();
}
// Fetch the next message from the MCU
let _rx = sys_seph::seph_recv(&mut spi_buffer, 0);
return self.detect_apdu::<T>(&mut spi_buffer);
}
pub fn check_event<T>(&mut self) -> Option<Event<T>>
where
T: TryFrom<ApduHeader>,
Reply: From<<T as TryFrom<ApduHeader>>::Error>,
{
if self.event_pending {
self.event_pending = false;
// Reject incomplete APDUs
if self.rx < 4 {
self.reply(StatusWords::BadLen);
return None;
}
// Check for data length by using `get_data`
if let Err(sw) = self.get_data() {
self.reply(sw);
return None;
}
// Manage BOLOS specific APDUs B0xx0000
if self.apdu_buffer[0] == 0xB0
&& self.apdu_buffer[2] == 0x00
&& self.apdu_buffer[3] == 0x00
{
handle_bolos_apdu(self, self.apdu_buffer[1]);
return None;
}
// If CLA filtering is enabled, automatically reject APDUs with wrong CLA
if let Some(cla) = self.expected_cla {
if self.apdu_buffer[0] != cla {
self.reply(StatusWords::BadCla);
return None;
}
}
let res = T::try_from(*self.get_apdu_metadata());
match res {
Ok(ins) => {
return Some(Event::Command(ins));
}
Err(sw) => {
// Invalid Ins code. Send automatically an error, mask
// the bad instruction to the application and just
// discard this event.
self.reply(sw);
}
}
}
None
}
pub fn process_event<T>(&mut self, spi_buffer: &mut [u8; 128]) -> Option<Event<T>>
where
T: TryFrom<ApduHeader>,
Reply: From<<T as TryFrom<ApduHeader>>::Error>,
{
// message = [ tag, len_hi, len_lo, ... ]
let tag = spi_buffer[0];
let len = u16::from_be_bytes([spi_buffer[1], spi_buffer[2]]);
// XXX: check whether this is necessary
// if rx < 3 && rx != len+3 {
// unsafe {
// G_io_app.apdu_state = APDU_IDLE;
// G_io_app.apdu_length = 0;
// }
// return None
// }
// Treat all possible events.
// If this is a button push, return with the associated event
// If this is an APDU, return with the "received command" event
// Any other event (usb, xfer, ticker) is silently handled
match seph::Events::from(tag) {
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
seph::Events::ButtonPush => {
let button_info = spi_buffer[3] >> 1;
if let Some(btn_evt) = get_button_event(&mut self.buttons, button_info) {
return Some(Event::Button(btn_evt));
}
}
seph::Events::USBEvent => {
if len == 1 {
seph::handle_usb_event(spi_buffer[3]);
}
}
seph::Events::USBXFEREvent => {
if len >= 3 {
seph::handle_usb_ep_xfer_event(&mut self.apdu_buffer, spi_buffer);
}
}
seph::Events::CAPDUEvent => seph::handle_capdu_event(&mut self.apdu_buffer, spi_buffer),
#[cfg(target_os = "nanox")]
seph::Events::BleReceive => ble::receive(&mut self.apdu_buffer, spi_buffer),
seph::Events::TickerEvent => {
#[cfg(any(target_os = "stax", target_os = "flex"))]
unsafe {
ux_process_ticker_event();
}
return Some(Event::Ticker);
}
#[cfg(any(target_os = "stax", target_os = "flex"))]
seph::Events::ScreenTouch => unsafe {
ux_process_finger_event(spi_buffer.as_mut_ptr());
return Some(Event::TouchEvent);
},
_ => {
#[cfg(any(target_os = "stax", target_os = "flex"))]
unsafe {
ux_process_default_event();
}
}
}
None
}
pub fn decode_event<T>(&mut self, spi_buffer: &mut [u8; 128]) -> Option<Event<T>>
where
T: TryFrom<ApduHeader>,
Reply: From<<T as TryFrom<ApduHeader>>::Error>,
{
if let Some(event) = self.process_event(spi_buffer) {
return Some(event);
}
if unsafe { G_io_app.apdu_state } != APDU_IDLE && unsafe { G_io_app.apdu_length } > 0 {
self.rx = unsafe { G_io_app.apdu_length as usize };
self.event_pending = true;
return self.check_event();
}
None
}
fn detect_apdu<T>(&mut self, spi_buffer: &mut [u8; 128]) -> bool
where
T: TryFrom<ApduHeader>,
Reply: From<<T as TryFrom<ApduHeader>>::Error>,
{
let _: Option<Event<T>> = self.decode_event(spi_buffer);
if unsafe { G_io_app.apdu_state } != APDU_IDLE && unsafe { G_io_app.apdu_length } > 0 {
self.rx = unsafe { G_io_app.apdu_length as usize };
self.event_pending = true;
return true;
}
false
}
/// Wait for the next Command event. Discards received button events.
///
/// Like `next_event`, `T` can be any type, an enumeration, or any type
/// which implements `TryFrom<ApduHeader>`.
///
/// # Examples
///
/// ```
/// enum Instruction {
/// Select,
/// ReadBinary
/// }
///
/// impl TryFrom<ApduHeader> for Instruction {
/// type Error = StatusWords;
///
/// fn try_from(h: ApduHeader) -> Result<Self, Self::Error> {
/// match h.ins {
/// 0xa4 => Ok(Self::Select),
/// 0xb0 => Ok(Self::ReadBinary)
/// _ => Err(StatusWords::BadIns)
/// }
/// }
/// }
///
/// loop {
/// match comm.next_command() {
/// Instruction::Select => { ... }
/// Instruction::ReadBinary => { ... }
/// }
/// }
/// ```
pub fn next_command<T>(&mut self) -> T
where
T: TryFrom<ApduHeader>,
Reply: From<<T as TryFrom<ApduHeader>>::Error>,
{
loop {
if let Event::Command(ins) = self.next_event() {
return ins;
}
}
}
/// Set the Status Word of the response to the previous Command event, and
/// transmit the response.
///
/// # Arguments
///
/// * `sw` - Status Word to be transmitted after the Data. Can be a
/// StatusWords, a SyscallError, or any type which can be converted to a
/// Reply.
pub fn reply<T: Into<Reply>>(&mut self, reply: T) {
let sw = reply.into().0;
// Append status word
self.apdu_buffer[self.tx] = (sw >> 8) as u8;
self.apdu_buffer[self.tx + 1] = sw as u8;
self.tx += 2;
// Transmit the response
self.apdu_send();
}
/// Set the Status Word of the response to `StatusWords::OK` (which is equal
/// to `0x9000`, and transmit the response.
pub fn reply_ok(&mut self) {
self.reply(StatusWords::Ok);
}
/// Return APDU Metadata
pub fn get_apdu_metadata(&self) -> &ApduHeader {
assert!(self.apdu_buffer.len() >= 4);
let ptr = &self.apdu_buffer[0] as &u8 as *const u8 as *const ApduHeader;
unsafe { &*ptr }
}
pub fn get_data(&self) -> Result<&[u8], StatusWords> {
if self.rx == 4 {
Ok(&[]) // Conforming zero-data APDU
} else {
let first_len_byte = self.apdu_buffer[4] as usize;
let get_data_from_buffer = |len, offset| {
if len == 0 || len + offset > self.rx {
Err(StatusWords::BadLen)
} else {
Ok(&self.apdu_buffer[offset..offset + len])
}
};
match (first_len_byte, self.rx) {
(0, 5) => Ok(&[]), // Non-conforming zero-data APDU
(0, 6) => Err(StatusWords::BadLen),
(0, _) => {
let len =
u16::from_le_bytes([self.apdu_buffer[5], self.apdu_buffer[6]]) as usize;
get_data_from_buffer(len, 7)
}
(len, _) => get_data_from_buffer(len, 5),
}
}
}
pub fn get(&self, start: usize, end: usize) -> &[u8] {
&self.apdu_buffer[start..end]
}
pub fn append(&mut self, m: &[u8]) {
for c in m.iter() {
self.apdu_buffer[self.tx] = *c;
self.tx += 1;
}
}
}
// BOLOS APDU Handling (see https://developers.ledger.com/docs/connectivity/ledgerJS/open-close-info-on-apps)
fn handle_bolos_apdu(com: &mut Comm, ins: u8) {
match ins {
// Get Information INS: retrieve App name and version
0x01 => {
unsafe {
com.apdu_buffer[0] = 0x01;
com.tx += 1;
let len = os_registry_get_current_app_tag(
BOLOS_TAG_APPNAME,
&mut com.apdu_buffer[com.tx + 1] as *mut u8,
(260 - com.tx - 1) as u32,
);
com.apdu_buffer[com.tx] = len as u8;
com.tx += (1 + len) as usize;
let len = os_registry_get_current_app_tag(
BOLOS_TAG_APPVERSION,
&mut com.apdu_buffer[com.tx + 1] as *mut u8,
(260 - com.tx - 1) as u32,
);
com.apdu_buffer[com.tx] = len as u8;
com.tx += (1 + len) as usize;
}
com.reply_ok();
}
// Quit Application INS
0xa7 => {
com.reply_ok();
crate::exit_app(0);
}
_ => {
com.reply(StatusWords::BadIns);
}
}
}
impl Index<usize> for Comm {
type Output = u8;
fn index(&self, idx: usize) -> &Self::Output {
&self.apdu_buffer[idx]
}
}
impl IndexMut<usize> for Comm {
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
self.tx = idx.max(self.tx);
&mut self.apdu_buffer[idx]
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::assert_eq_err as assert_eq;
use crate::testing::TestType;
use testmacro::test_item as test;
/// Basic "smoke test" that the casting is done correctly.
#[test]
fn apdu_metadata() {
let c = Comm::new();
let m = c.get_apdu_metadata();
assert_eq!(m.cla, 0);
assert_eq!(m.ins, 0);
assert_eq!(m.p1, 0);
assert_eq!(m.p2, 0);
}
}