From 39a5759d1288d15f2a67b2f48dab68279eb14933 Mon Sep 17 00:00:00 2001 From: Nick Zana Date: Tue, 16 May 2023 13:32:35 -0400 Subject: [PATCH] fido-common: fix credential::public_key::Parameters signature type The credential::public_key::Parameters field algorithm designates the public key algorithm represented by the parameter. This replaces the incorrect registry::algorithms::Signature type with the correct coset::iana::Algorithm type and adds Serialization/Deserialization methods for this type. --- .../fido-common/src/credential/public_key.rs | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/fido-common/src/credential/public_key.rs b/crates/fido-common/src/credential/public_key.rs index 0d65a0b..8bc6c4c 100644 --- a/crates/fido-common/src/credential/public_key.rs +++ b/crates/fido-common/src/credential/public_key.rs @@ -1,10 +1,34 @@ -use crate::registry::algorithms; use crate::{authenticator::Transport, credential}; use std::collections::BTreeSet; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "serde")] +pub(crate) mod algorithm { + use coset::iana::{Algorithm, EnumI64}; + use serde::{Deserialize, Serialize}; + + pub(crate) fn serialize(algorithm: &Algorithm, serializer: S) -> Result + where + S: serde::Serializer, + { + let i = algorithm.to_i64(); + i64::serialize(&i, serializer) + } + + pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let i = i64::deserialize(deserializer)?; + coset::iana::Algorithm::from_i64(i).ok_or(serde::de::Error::invalid_value( + serde::de::Unexpected::Signed(i), + &"an IANA-registered COSE algorithm value", + )) + } +} + /// > This dictionary is used to supply additional parameters when /// > creating a new credential. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -19,8 +43,8 @@ pub struct Parameters { /// > algorithm with which the newly generated credential will /// > be used, and thus also the type of asymmetric key pair to /// > be generated, e.g., RSA or Elliptic Curve. - #[cfg_attr(feature = "serde", serde(rename = "alg"))] - pub algorithm: algorithms::Signature, + #[cfg_attr(feature = "serde", serde(rename = "alg", with = "algorithm"))] + pub algorithm: coset::iana::Algorithm, } /// > This dictionary identifies a specific public key credential.