From a7da04be0c0e1b8f4126705417f8f56e82d3a54e Mon Sep 17 00:00:00 2001 From: GroM Date: Mon, 8 Apr 2024 18:42:17 +0200 Subject: [PATCH 1/3] Manage BOLOS default APDUs --- ledger_device_sdk/src/io.rs | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/ledger_device_sdk/src/io.rs b/ledger_device_sdk/src/io.rs index 0c86b08e..fd3d757e 100644 --- a/ledger_device_sdk/src/io.rs +++ b/ledger_device_sdk/src/io.rs @@ -331,6 +331,44 @@ impl Comm { return None; } + // Default BOLOS APDU Handling + let apdu_header = self.get_apdu_metadata(); + if apdu_header.cla == 0xB0 && apdu_header.p1 == 0x00 && apdu_header.p2 == 0x00 { + match apdu_header.ins { + 0x01 => { + unsafe { + self.apdu_buffer[0] = 0x01; + self.tx += 1; + let len = os_registry_get_current_app_tag( + BOLOS_TAG_APPNAME, + &mut self.apdu_buffer[self.tx + 1] as *mut u8, + (260 - self.tx - 1) as u32, + ); + self.apdu_buffer[self.tx] = len as u8; + self.tx += (1 + len) as usize; + + let len = os_registry_get_current_app_tag( + BOLOS_TAG_APPVERSION, + &mut self.apdu_buffer[self.tx + 1] as *mut u8, + (260 - self.tx - 1) as u32, + ); + self.apdu_buffer[self.tx] = len as u8; + self.tx += (1 + len) as usize; + } + self.reply_ok(); + return None; + } + 0xa7 => { + self.reply_ok(); + crate::exit_app(0); + } + _ => { + self.reply(StatusWords::BadIns); + 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 { From bab327ea56c98cc1cffe3214cc071adc7670b89e Mon Sep 17 00:00:00 2001 From: GroM Date: Mon, 8 Apr 2024 18:55:29 +0200 Subject: [PATCH 2/3] [ledger_device_sdk] Bump version --- Cargo.lock | 2 +- ledger_device_sdk/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 512c28c5..ee45ca7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -296,7 +296,7 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "ledger_device_sdk" -version = "1.8.0" +version = "1.8.1" dependencies = [ "include_gif", "ledger_device_sdk", diff --git a/ledger_device_sdk/Cargo.toml b/ledger_device_sdk/Cargo.toml index 82a942b9..dfd44f46 100644 --- a/ledger_device_sdk/Cargo.toml +++ b/ledger_device_sdk/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ledger_device_sdk" -version = "1.8.0" +version = "1.8.1" authors = ["yhql", "yogh333"] edition = "2021" license.workspace = true From 79a718d64c28090ec32c1ab85b64bc2a9f3f5b49 Mon Sep 17 00:00:00 2001 From: GroM Date: Tue, 9 Apr 2024 10:29:42 +0200 Subject: [PATCH 3/3] Move BOLOS APDUs handling into a private function --- ledger_device_sdk/src/io.rs | 80 ++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/ledger_device_sdk/src/io.rs b/ledger_device_sdk/src/io.rs index fd3d757e..cc6e481f 100644 --- a/ledger_device_sdk/src/io.rs +++ b/ledger_device_sdk/src/io.rs @@ -331,42 +331,13 @@ impl Comm { return None; } - // Default BOLOS APDU Handling - let apdu_header = self.get_apdu_metadata(); - if apdu_header.cla == 0xB0 && apdu_header.p1 == 0x00 && apdu_header.p2 == 0x00 { - match apdu_header.ins { - 0x01 => { - unsafe { - self.apdu_buffer[0] = 0x01; - self.tx += 1; - let len = os_registry_get_current_app_tag( - BOLOS_TAG_APPNAME, - &mut self.apdu_buffer[self.tx + 1] as *mut u8, - (260 - self.tx - 1) as u32, - ); - self.apdu_buffer[self.tx] = len as u8; - self.tx += (1 + len) as usize; - - let len = os_registry_get_current_app_tag( - BOLOS_TAG_APPVERSION, - &mut self.apdu_buffer[self.tx + 1] as *mut u8, - (260 - self.tx - 1) as u32, - ); - self.apdu_buffer[self.tx] = len as u8; - self.tx += (1 + len) as usize; - } - self.reply_ok(); - return None; - } - 0xa7 => { - self.reply_ok(); - crate::exit_app(0); - } - _ => { - self.reply(StatusWords::BadIns); - 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 @@ -505,6 +476,43 @@ impl Comm { } } +// 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 for Comm { type Output = u8; fn index(&self, idx: usize) -> &Self::Output {