From e4d160aaeeb4c209908a1c9a1eb073273d2792df Mon Sep 17 00:00:00 2001 From: Kamran Khan Date: Mon, 7 Dec 2020 23:32:04 -0800 Subject: [PATCH] Use TryFrom to convert between APDU and CTAP status codes --- src/ctap/ctap1.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/ctap/ctap1.rs b/src/ctap/ctap1.rs index ea5f894..b0ed506 100644 --- a/src/ctap/ctap1.rs +++ b/src/ctap/ctap1.rs @@ -60,6 +60,24 @@ impl TryFrom for Ctap1StatusCode { } } +impl TryFrom for Ctap1StatusCode { + type Error = (); + + fn try_from(apdu_status_code: ApduStatusCode) -> Result { + match apdu_status_code { + ApduStatusCode::SW_WRONG_LENGTH => Ok(Ctap1StatusCode::SW_WRONG_LENGTH), + ApduStatusCode::SW_WRONG_DATA => Ok(Ctap1StatusCode::SW_WRONG_DATA), + ApduStatusCode::SW_CLA_INVALID => Ok(Ctap1StatusCode::SW_CLA_NOT_SUPPORTED), + ApduStatusCode::SW_INS_INVALID => Ok(Ctap1StatusCode::SW_INS_NOT_SUPPORTED), + ApduStatusCode::SW_COND_USE_NOT_SATISFIED => { + Ok(Ctap1StatusCode::SW_CONDITIONS_NOT_SATISFIED) + } + ApduStatusCode::SW_SUCCESS => Ok(Ctap1StatusCode::SW_NO_ERROR), + _ => Ok(Ctap1StatusCode::SW_COMMAND_ABORTED), + } + } +} + impl Into for Ctap1StatusCode { fn into(self) -> u16 { self as u16 @@ -121,13 +139,9 @@ impl TryFrom<&[u8]> for U2fCommand { fn try_from(message: &[u8]) -> Result { let apdu: APDU = match APDU::try_from(message) { Ok(apdu) => apdu, - // Todo: Better conversion between ApduStatusCode and Ctap1StatusCode - // Maybe use TryFrom? - Err(apdu_status_code) => match apdu_status_code { - ApduStatusCode::SW_WRONG_LENGTH => return Err(Ctap1StatusCode::SW_WRONG_LENGTH), - ApduStatusCode::SW_WRONG_DATA => return Err(Ctap1StatusCode::SW_WRONG_DATA), - _ => return Err(Ctap1StatusCode::SW_COMMAND_ABORTED), - }, + Err(apdu_status_code) => { + return Err(Ctap1StatusCode::try_from(apdu_status_code).unwrap()) + } }; // ISO7816 APDU Header format. Each cell is 1 byte. Note that the CTAP flavor always