From 2d17bb2afafe1d94abd55978c6863d2015ff219b Mon Sep 17 00:00:00 2001 From: Kamran Khan Date: Mon, 7 Dec 2020 23:38:21 -0800 Subject: [PATCH] Readability improvements --- src/ctap/apdu.rs | 4 ++-- src/ctap/ctap1.rs | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/ctap/apdu.rs b/src/ctap/apdu.rs index e3dfe23..70b17eb 100644 --- a/src/ctap/apdu.rs +++ b/src/ctap/apdu.rs @@ -172,9 +172,9 @@ impl TryFrom<&[u8]> for APDU { if payload.len() < extended_apdu_lc + 3 { return Err(ApduStatusCode::SW_WRONG_LENGTH); } - let extended_apdu_le_len: usize = match payload.len() - extended_apdu_lc { + let extended_apdu_le_len: usize = match payload.len() - extended_apdu_lc - 3 { // There's some possible Le bytes at the end - 2..=5 => payload.len() - extended_apdu_lc - 3, + 0..=3 => payload.len() - extended_apdu_lc - 3, // There are more bytes than even Le3 can consume, return an error _ => return Err(ApduStatusCode::SW_WRONG_LENGTH), }; diff --git a/src/ctap/ctap1.rs b/src/ctap/ctap1.rs index b0ed506..2677201 100644 --- a/src/ctap/ctap1.rs +++ b/src/ctap/ctap1.rs @@ -144,6 +144,8 @@ impl TryFrom<&[u8]> for U2fCommand { } }; + let lc = apdu.lc as usize; + // ISO7816 APDU Header format. Each cell is 1 byte. Note that the CTAP flavor always // encodes the length on 3 bytes and doesn't use the field "Le" (Length Expected). // We keep the 2 byte of "Le" for the packet length in mind, but always ignore its value. @@ -157,8 +159,7 @@ impl TryFrom<&[u8]> for U2fCommand { // Since there is always request data, the expected length is either omitted or // encoded in 2 bytes. - // Todo: support extended APDUs now that the new parser can work with those - if apdu.lc as usize != apdu.data.len() && (apdu.lc as usize) + 2 != apdu.data.len() { + if lc != apdu.data.len() && (lc as usize) + 2 != apdu.data.len() { return Err(Ctap1StatusCode::SW_WRONG_LENGTH); } @@ -168,7 +169,7 @@ impl TryFrom<&[u8]> for U2fCommand { // + Challenge (32B) | Application (32B) | // +-----------------+-------------------+ Ctap1Command::U2F_REGISTER => { - if apdu.lc != 64 { + if lc != 64 { return Err(Ctap1StatusCode::SW_WRONG_LENGTH); } Ok(Self::Register { @@ -182,11 +183,11 @@ impl TryFrom<&[u8]> for U2fCommand { // + Challenge (32B) | Application (32B) | key handle len (1B) | key handle | // +-----------------+-------------------+---------------------+------------+ Ctap1Command::U2F_AUTHENTICATE => { - if apdu.lc < 65 { + if lc < 65 { return Err(Ctap1StatusCode::SW_WRONG_LENGTH); } let handle_length = apdu.data[64] as usize; - if apdu.lc as usize != 65 + handle_length { + if lc as usize != 65 + handle_length { return Err(Ctap1StatusCode::SW_WRONG_LENGTH); } let flag = Ctap1Flags::try_from(apdu.header.p1)?; @@ -200,7 +201,7 @@ impl TryFrom<&[u8]> for U2fCommand { // U2F raw message format specification, Section 6.1 Ctap1Command::U2F_VERSION => { - if apdu.lc != 0 { + if lc != 0 { return Err(Ctap1StatusCode::SW_WRONG_LENGTH); } Ok(Self::Version)