From 40f279684ed0cc0b689de298d873424810ed09d6 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Tue, 25 Jan 2022 13:01:15 +0100 Subject: [PATCH] workaround and test for "ledger nano s" quirks --- openpgp-card/src/card_do/historical.rs | 44 +++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/openpgp-card/src/card_do/historical.rs b/openpgp-card/src/card_do/historical.rs index 6ea6981..9debcdd 100644 --- a/openpgp-card/src/card_do/historical.rs +++ b/openpgp-card/src/card_do/historical.rs @@ -80,7 +80,17 @@ impl HistoricalBytes { impl TryFrom<&[u8]> for HistoricalBytes { type Error = Error; - fn try_from(data: &[u8]) -> Result { + fn try_from(mut data: &[u8]) -> Result { + // workaround-hack for "ledger" with zero-padded historical bytes + if data.ends_with(&[0x90, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]) { + data = data.strip_suffix(&[0x0, 0x0, 0x0, 0x0, 0x0]).unwrap(); + } + + // workaround-hack for "ledger": fix status indicator byte 7 + if data == &[0x0, 0x31, 0xc5, 0x73, 0xc0, 0x1, 0x80, 0x7, 0x90, 0x0] { + data = &[0x0, 0x31, 0xc5, 0x73, 0xc0, 0x1, 0x80, 0x5, 0x90, 0x0]; + } + let len = data.len(); if len < 4 { @@ -341,4 +351,36 @@ mod test { Ok(()) } + + #[test] + fn test_ledger_nano_s() -> Result<()> { + let data: &[u8] = &[ + 0x0, 0x31, 0xc5, 0x73, 0xc0, 0x1, 0x80, 0x7, 0x90, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, + ]; + let hist: HistoricalBytes = data.try_into()?; + + assert_eq!( + hist, + HistoricalBytes { + cib: 0, + csd: Some(CardServiceData { + select_by_full_df_name: true, + select_by_partial_df_name: true, + dos_available_in_ef_dir: false, + dos_available_in_ef_atr_info: false, + access_services: [false, true, false], + mf: true + }), + cc: Some(CardCapabilities { + command_chaining: true, + extended_lc_le: false, + extended_length_information: false + }), + sib: 5 + } + ); + + Ok(()) + } }