Readability improvements

This commit is contained in:
Kamran Khan
2020-12-07 23:38:21 -08:00
parent 373464b72d
commit 2d17bb2afa
2 changed files with 9 additions and 8 deletions

View File

@@ -172,9 +172,9 @@ impl TryFrom<&[u8]> for APDU {
if payload.len() < extended_apdu_lc + 3 { if payload.len() < extended_apdu_lc + 3 {
return Err(ApduStatusCode::SW_WRONG_LENGTH); 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 // 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 // There are more bytes than even Le3 can consume, return an error
_ => return Err(ApduStatusCode::SW_WRONG_LENGTH), _ => return Err(ApduStatusCode::SW_WRONG_LENGTH),
}; };

View File

@@ -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 // 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). // 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. // 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 // Since there is always request data, the expected length is either omitted or
// encoded in 2 bytes. // encoded in 2 bytes.
// Todo: support extended APDUs now that the new parser can work with those if lc != apdu.data.len() && (lc as usize) + 2 != apdu.data.len() {
if apdu.lc as usize != apdu.data.len() && (apdu.lc as usize) + 2 != apdu.data.len() {
return Err(Ctap1StatusCode::SW_WRONG_LENGTH); return Err(Ctap1StatusCode::SW_WRONG_LENGTH);
} }
@@ -168,7 +169,7 @@ impl TryFrom<&[u8]> for U2fCommand {
// + Challenge (32B) | Application (32B) | // + Challenge (32B) | Application (32B) |
// +-----------------+-------------------+ // +-----------------+-------------------+
Ctap1Command::U2F_REGISTER => { Ctap1Command::U2F_REGISTER => {
if apdu.lc != 64 { if lc != 64 {
return Err(Ctap1StatusCode::SW_WRONG_LENGTH); return Err(Ctap1StatusCode::SW_WRONG_LENGTH);
} }
Ok(Self::Register { Ok(Self::Register {
@@ -182,11 +183,11 @@ impl TryFrom<&[u8]> for U2fCommand {
// + Challenge (32B) | Application (32B) | key handle len (1B) | key handle | // + Challenge (32B) | Application (32B) | key handle len (1B) | key handle |
// +-----------------+-------------------+---------------------+------------+ // +-----------------+-------------------+---------------------+------------+
Ctap1Command::U2F_AUTHENTICATE => { Ctap1Command::U2F_AUTHENTICATE => {
if apdu.lc < 65 { if lc < 65 {
return Err(Ctap1StatusCode::SW_WRONG_LENGTH); return Err(Ctap1StatusCode::SW_WRONG_LENGTH);
} }
let handle_length = apdu.data[64] as usize; 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); return Err(Ctap1StatusCode::SW_WRONG_LENGTH);
} }
let flag = Ctap1Flags::try_from(apdu.header.p1)?; 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 // U2F raw message format specification, Section 6.1
Ctap1Command::U2F_VERSION => { Ctap1Command::U2F_VERSION => {
if apdu.lc != 0 { if lc != 0 {
return Err(Ctap1StatusCode::SW_WRONG_LENGTH); return Err(Ctap1StatusCode::SW_WRONG_LENGTH);
} }
Ok(Self::Version) Ok(Self::Version)