From df78c9e3034ac2ab7ac6f8fe13f309a0279d7fa2 Mon Sep 17 00:00:00 2001 From: Nick Zana Date: Wed, 14 Jun 2023 14:38:36 -0400 Subject: [PATCH] ctap2-proto: Add authenticator::client_pin::raw::PublicKey type for deserialization The cosey::PublicKey type does not properly implement deserialize for the general PublicKey type. the client_pin::raw::PublicKey type is used as an intermediate to allow deserialization of COSE public keys in the client_pin protocol. --- .../src/authenticator/client_pin/mod.rs | 3 ++ .../src/authenticator/client_pin/raw/mod.rs | 6 ++++ .../client_pin/raw/public_key.rs | 32 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 crates/ctap2-proto/src/authenticator/client_pin/raw/mod.rs create mode 100644 crates/ctap2-proto/src/authenticator/client_pin/raw/public_key.rs diff --git a/crates/ctap2-proto/src/authenticator/client_pin/mod.rs b/crates/ctap2-proto/src/authenticator/client_pin/mod.rs index b2c9df1..1dd30a5 100644 --- a/crates/ctap2-proto/src/authenticator/client_pin/mod.rs +++ b/crates/ctap2-proto/src/authenticator/client_pin/mod.rs @@ -7,6 +7,9 @@ use serde::{Deserialize, Serialize}; pub mod auth_protocol; +#[cfg(feature = "serde")] +mod raw; + pub type PinUvAuthParam = [u8; 16]; diff --git a/crates/ctap2-proto/src/authenticator/client_pin/raw/mod.rs b/crates/ctap2-proto/src/authenticator/client_pin/raw/mod.rs new file mode 100644 index 0000000..587d7ea --- /dev/null +++ b/crates/ctap2-proto/src/authenticator/client_pin/raw/mod.rs @@ -0,0 +1,6 @@ +//! Used to make serialization and deseriazation of the request and response +//! possible in CBOR format while maintaining ergonomic enum variants for public +//! API. + +mod public_key; + diff --git a/crates/ctap2-proto/src/authenticator/client_pin/raw/public_key.rs b/crates/ctap2-proto/src/authenticator/client_pin/raw/public_key.rs new file mode 100644 index 0000000..1efd974 --- /dev/null +++ b/crates/ctap2-proto/src/authenticator/client_pin/raw/public_key.rs @@ -0,0 +1,32 @@ +use cosey::{EcdhEsHkdf256PublicKey, Ed25519PublicKey, P256PublicKey}; + +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +pub(crate) enum PublicKey { + P256Key(P256PublicKey), + EcdhEsHkdf256Key(EcdhEsHkdf256PublicKey), + Ed25519Key(Ed25519PublicKey), +} + +impl Into for PublicKey { + fn into(self) -> cosey::PublicKey { + match self { + PublicKey::P256Key(key) => cosey::PublicKey::P256Key(key), + PublicKey::EcdhEsHkdf256Key(key) => cosey::PublicKey::EcdhEsHkdf256Key(key), + PublicKey::Ed25519Key(key) => cosey::PublicKey::Ed25519Key(key), + } + } +} + +pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + PublicKey::deserialize(deserializer) + .map(Into::into) + .map(Some) +} +