From a4becf9acafbda05b2d99c2545221ae7bdd96328 Mon Sep 17 00:00:00 2001 From: Fabian Kaczmarczyck Date: Thu, 18 Jun 2020 15:58:12 +0200 Subject: [PATCH] new error codes and enum cleanups --- src/ctap/data_formats.rs | 69 +++++++++++++++++++--------------------- src/ctap/status_code.rs | 8 +++++ 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/src/ctap/data_formats.rs b/src/ctap/data_formats.rs index b5799b4..ed54389 100644 --- a/src/ctap/data_formats.rs +++ b/src/ctap/data_formats.rs @@ -176,11 +176,12 @@ pub enum AuthenticatorTransport { impl From for cbor::Value { fn from(transport: AuthenticatorTransport) -> Self { + use AuthenticatorTransport::*; match transport { - AuthenticatorTransport::Usb => "usb", - AuthenticatorTransport::Nfc => "nfc", - AuthenticatorTransport::Ble => "ble", - AuthenticatorTransport::Internal => "internal", + Usb => "usb", + Nfc => "nfc", + Ble => "ble", + Internal => "internal", } .into() } @@ -190,12 +191,13 @@ impl TryFrom for AuthenticatorTransport { type Error = Ctap2StatusCode; fn try_from(cbor_value: cbor::Value) -> Result { + use AuthenticatorTransport::*; let transport_string = extract_text_string(cbor_value)?; match &transport_string[..] { - "usb" => Ok(AuthenticatorTransport::Usb), - "nfc" => Ok(AuthenticatorTransport::Nfc), - "ble" => Ok(AuthenticatorTransport::Ble), - "internal" => Ok(AuthenticatorTransport::Internal), + "usb" => Ok(Usb), + "nfc" => Ok(Nfc), + "ble" => Ok(Ble), + "internal" => Ok(Internal), _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE), } } @@ -469,10 +471,11 @@ impl TryFrom for CredentialProtectionPolicy { type Error = Ctap2StatusCode; fn try_from(cbor_value: cbor::Value) -> Result { + use CredentialProtectionPolicy::*; match extract_integer(cbor_value)? { - 0x01 => Ok(CredentialProtectionPolicy::UserVerificationOptional), - 0x02 => Ok(CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIdList), - 0x03 => Ok(CredentialProtectionPolicy::UserVerificationRequired), + 0x01 => Ok(UserVerificationOptional), + 0x02 => Ok(UserVerificationOptionalWithCredentialIdList), + 0x03 => Ok(UserVerificationRequired), _ => Err(Ctap2StatusCode::CTAP2_ERR_CBOR_UNEXPECTED_TYPE), } } @@ -683,27 +686,18 @@ impl TryFrom for ecdh::PubKey { #[cfg_attr(any(test, feature = "debug_ctap"), derive(Debug, PartialEq))] pub enum ClientPinSubCommand { - GetPinRetries, - GetKeyAgreement, - SetPin, - ChangePin, - GetPinUvAuthTokenUsingPin, - GetPinUvAuthTokenUsingUv, - GetUvRetries, + GetPinRetries = 0x01, + GetKeyAgreement = 0x02, + SetPin = 0x03, + ChangePin = 0x04, + GetPinUvAuthTokenUsingPin = 0x05, + GetPinUvAuthTokenUsingUv = 0x06, + GetUvRetries = 0x07, } impl From for cbor::Value { fn from(subcommand: ClientPinSubCommand) -> Self { - match subcommand { - ClientPinSubCommand::GetPinRetries => 0x01, - ClientPinSubCommand::GetKeyAgreement => 0x02, - ClientPinSubCommand::SetPin => 0x03, - ClientPinSubCommand::ChangePin => 0x04, - ClientPinSubCommand::GetPinUvAuthTokenUsingPin => 0x05, - ClientPinSubCommand::GetPinUvAuthTokenUsingUv => 0x06, - ClientPinSubCommand::GetUvRetries => 0x07, - } - .into() + (subcommand as u64).into() } } @@ -711,16 +705,19 @@ impl TryFrom for ClientPinSubCommand { type Error = Ctap2StatusCode; fn try_from(cbor_value: cbor::Value) -> Result { + use ClientPinSubCommand::*; let subcommand_int = extract_unsigned(cbor_value)?; match subcommand_int { - 0x01 => Ok(ClientPinSubCommand::GetPinRetries), - 0x02 => Ok(ClientPinSubCommand::GetKeyAgreement), - 0x03 => Ok(ClientPinSubCommand::SetPin), - 0x04 => Ok(ClientPinSubCommand::ChangePin), - 0x05 => Ok(ClientPinSubCommand::GetPinUvAuthTokenUsingPin), - 0x06 => Ok(ClientPinSubCommand::GetPinUvAuthTokenUsingUv), - 0x07 => Ok(ClientPinSubCommand::GetUvRetries), - // TODO(kaczmarczyck) what is the correct status code for this error? + 0x01 => Ok(GetPinRetries), + 0x02 => Ok(GetKeyAgreement), + 0x03 => Ok(SetPin), + 0x04 => Ok(ChangePin), + 0x05 => Ok(GetPinUvAuthTokenUsingPin), + 0x06 => Ok(GetPinUvAuthTokenUsingUv), + 0x07 => Ok(GetUvRetries), + #[cfg(feature = "with_ctap2_1")] + _ => Err(Ctap2StatusCode::CTAP2_ERR_INVALID_SUBCOMMAND), + #[cfg(not(feature = "with_ctap2_1"))] _ => Err(Ctap2StatusCode::CTAP1_ERR_INVALID_PARAMETER), } } diff --git a/src/ctap/status_code.rs b/src/ctap/status_code.rs index b58b8d0..adb84fd 100644 --- a/src/ctap/status_code.rs +++ b/src/ctap/status_code.rs @@ -32,6 +32,10 @@ pub enum Ctap2StatusCode { CTAP2_ERR_MISSING_PARAMETER = 0x14, CTAP2_ERR_LIMIT_EXCEEDED = 0x15, CTAP2_ERR_UNSUPPORTED_EXTENSION = 0x16, + #[cfg(feature = "with_ctap2_1")] + CTAP2_ERR_FP_DATABASE_FULL = 0x17, + #[cfg(feature = "with_ctap2_1")] + CTAP2_ERR_PC_STORAGE_FULL = 0x18, CTAP2_ERR_CREDENTIAL_EXCLUDED = 0x19, CTAP2_ERR_PROCESSING = 0x21, CTAP2_ERR_INVALID_CREDENTIAL = 0x22, @@ -60,6 +64,10 @@ pub enum Ctap2StatusCode { CTAP2_ERR_ACTION_TIMEOUT = 0x3A, CTAP2_ERR_UP_REQUIRED = 0x3B, CTAP2_ERR_UV_BLOCKED = 0x3C, + #[cfg(feature = "with_ctap2_1")] + CTAP2_ERR_INTEGRITY_FAILURE = 0x3D, + #[cfg(feature = "with_ctap2_1")] + CTAP2_ERR_INVALID_SUBCOMMAND = 0x3E, CTAP1_ERR_OTHER = 0x7F, CTAP2_ERR_SPEC_LAST = 0xDF, CTAP2_ERR_EXTENSION_FIRST = 0xE0,