#[cfg(feature = "serde_relaxed")]
use self::{
super::ser_relaxed::{RelaxedClientDataJsonParser, SerdeJsonErr},
ser_relaxed::{AuthenticationRelaxed, CustomAuthentication},
};
#[cfg(doc)]
use super::super::{
AuthenticatedCredential, RegisteredCredential, StaticState,
hash::hash_set::MaxLenHashSet,
request::{
Challenge,
auth::{
CredentialSpecificExtension, DiscoverableAuthenticationServerState, Extension,
NonDiscoverableAuthenticationServerState, PublicKeyCredentialRequestOptions,
},
register::{self, UserHandle16, UserHandle64},
},
};
use super::{
super::{UserHandle, request::register::USER_HANDLE_MAX_LEN},
AuthData, AuthDataContainer, AuthExtOutput, AuthRespErr, AuthResponse, AuthenticatorAttachment,
CborSuccess, ClientDataJsonParser as _, CollectedClientData, CredentialId, Flag, FromCbor,
HmacSecretGet, HmacSecretGetErr, LimitedVerificationParser, ParsedAuthData, Response,
SentChallenge,
auth::error::{AuthenticatorDataErr, AuthenticatorExtensionOutputErr, MissingUserHandleErr},
cbor,
error::CollectedClientDataErr,
register::CompressedPubKeyBorrowed,
};
use core::convert::Infallible;
use ed25519_dalek::{Signature, Verifier as _};
use ml_dsa::{MlDsa44, MlDsa65, MlDsa87, Signature as MlDsaSig};
use p256::ecdsa::DerSignature as P256DerSig;
use p384::ecdsa::DerSignature as P384DerSig;
use rsa::{
pkcs1v15,
sha2::{Sha256, digest::Digest as _},
};
#[cfg(feature = "serde_relaxed")]
use serde::Deserialize;
pub mod error;
#[cfg(feature = "serde")]
pub(super) mod ser;
#[cfg(feature = "serde_relaxed")]
pub mod ser_relaxed;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HmacSecret {
None,
One,
Two,
}
#[derive(Clone, Copy, Debug)]
pub struct AuthenticatorExtensionOutput {
pub hmac_secret: HmacSecret,
}
impl AuthExtOutput for AuthenticatorExtensionOutput {
fn missing(self) -> bool {
matches!(self.hmac_secret, HmacSecret::None)
}
}
impl From<HmacSecretGetErr> for AuthenticatorExtensionOutputErr {
#[inline]
fn from(value: HmacSecretGetErr) -> Self {
match value {
HmacSecretGetErr::Len => Self::Len,
HmacSecretGetErr::Type => Self::HmacSecretType,
HmacSecretGetErr::Value => Self::HmacSecretValue,
}
}
}
impl FromCbor<'_> for AuthenticatorExtensionOutput {
type Err = AuthenticatorExtensionOutputErr;
fn from_cbor(cbor: &[u8]) -> Result<CborSuccess<'_, Self>, Self::Err> {
let mut hmac_secret = HmacSecret::None;
let mut remaining = cbor;
cbor.split_first()
.map_or(Ok(()), |(map, map_rem)| {
if *map == cbor::MAP_1 {
HmacSecretGet::<false>::from_cbor(map_rem)
.map_err(AuthenticatorExtensionOutputErr::from)
.and_then(|hmac_succ| match hmac_succ.value {
HmacSecretGet::None => Err(AuthenticatorExtensionOutputErr::Missing),
HmacSecretGet::One => {
hmac_secret = HmacSecret::One;
remaining = hmac_succ.remaining;
Ok(())
}
HmacSecretGet::Two => {
hmac_secret = HmacSecret::Two;
remaining = hmac_succ.remaining;
Ok(())
}
})
} else {
Err(AuthenticatorExtensionOutputErr::CborHeader)
}
})
.map(|()| CborSuccess {
value: Self { hmac_secret },
remaining,
})
}
}
pub(crate) struct NoCred;
impl FromCbor<'_> for NoCred {
type Err = Infallible;
fn from_cbor(cbor: &[u8]) -> Result<CborSuccess<'_, Self>, Self::Err> {
Ok(CborSuccess {
value: Self,
remaining: cbor,
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct AuthenticatorData<'a> {
rp_id_hash: &'a [u8],
flags: Flag,
sign_count: u32,
extensions: AuthenticatorExtensionOutput,
}
impl<'a> AuthenticatorData<'a> {
#[inline]
#[must_use]
pub const fn rp_id_hash(&self) -> &'a [u8] {
self.rp_id_hash
}
#[inline]
#[must_use]
pub const fn flags(&self) -> Flag {
self.flags
}
#[inline]
#[must_use]
pub const fn sign_count(&self) -> u32 {
self.sign_count
}
#[inline]
#[must_use]
pub const fn extensions(&self) -> AuthenticatorExtensionOutput {
self.extensions
}
}
impl<'a> AuthData<'a> for AuthenticatorData<'a> {
type UpBitErr = ();
type CredData = NoCred;
type Ext = AuthenticatorExtensionOutput;
fn contains_at_bit() -> bool {
false
}
fn user_is_not_present() -> Result<(), Self::UpBitErr> {
Err(())
}
fn new(
rp_id_hash: &'a [u8],
flags: Flag,
sign_count: u32,
_: Self::CredData,
extensions: Self::Ext,
) -> Self {
Self {
rp_id_hash,
flags,
sign_count,
extensions,
}
}
fn rp_hash(&self) -> &'a [u8] {
self.rp_id_hash
}
fn flag(&self) -> Flag {
self.flags
}
}
impl<'a> AuthDataContainer<'a> for AuthenticatorData<'a> {
type Auth = Self;
type Err = AuthenticatorDataErr;
#[expect(clippy::unreachable, reason = "we want to crash when there is a bug")]
#[expect(clippy::indexing_slicing, reason = "comment justifies its correctness")]
fn from_data(data: &'a [u8]) -> Result<ParsedAuthData<'a, Self>, Self::Err> {
Self::try_from(&data[..data.len().checked_sub(Sha256::output_size()).unwrap_or_else(|| unreachable!("AuthenticatorData::from_data must be passed a slice with 32 bytes of trailing data"))]).map(|auth_data| ParsedAuthData { data: auth_data, auth_data_and_32_trailing_bytes: data, })
}
fn authenticator_data(&self) -> &Self::Auth {
self
}
}
impl<'a: 'b, 'b> TryFrom<&'a [u8]> for AuthenticatorData<'b> {
type Error = AuthenticatorDataErr;
#[expect(
clippy::panic_in_result_fn,
reason = "we want to crash when there is a bug"
)]
#[inline]
fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
Self::from_cbor(value)
.map_err(AuthenticatorDataErr::from)
.map(|auth_data| {
assert!(
auth_data.remaining.is_empty(),
"there is a bug in AuthenticatorData::from_cbor"
);
auth_data.value
})
}
}
#[derive(Debug)]
pub struct AuthenticatorAssertion<const USER_LEN: usize, const DISCOVERABLE: bool> {
client_data_json: Vec<u8>,
authenticator_data_and_c_data_hash: Vec<u8>,
signature: Vec<u8>,
user_handle: Option<UserHandle<USER_LEN>>,
}
impl<const USER_LEN: usize, const DISCOVERABLE: bool>
AuthenticatorAssertion<USER_LEN, DISCOVERABLE>
{
#[inline]
#[must_use]
pub const fn client_data_json(&self) -> &[u8] {
self.client_data_json.as_slice()
}
#[expect(
clippy::arithmetic_side_effects,
clippy::indexing_slicing,
reason = "comment justifies their correctness"
)]
#[inline]
#[must_use]
pub fn authenticator_data(&self) -> &[u8] {
&self.authenticator_data_and_c_data_hash
[..self.authenticator_data_and_c_data_hash.len() - Sha256::output_size()]
}
#[inline]
#[must_use]
pub const fn signature(&self) -> &[u8] {
self.signature.as_slice()
}
fn new_inner(
client_data_json: Vec<u8>,
mut authenticator_data: Vec<u8>,
signature: Vec<u8>,
user_handle: Option<UserHandle<USER_LEN>>,
) -> Self {
authenticator_data.extend_from_slice(&Sha256::digest(client_data_json.as_slice()));
Self {
client_data_json,
authenticator_data_and_c_data_hash: authenticator_data,
signature,
user_handle,
}
}
}
impl<const USER_LEN: usize> AuthenticatorAssertion<USER_LEN, false> {
#[inline]
#[must_use]
pub const fn user_handle(&self) -> Option<&UserHandle<USER_LEN>> {
self.user_handle.as_ref()
}
#[inline]
#[must_use]
pub fn with_optional_user(
client_data_json: Vec<u8>,
authenticator_data: Vec<u8>,
signature: Vec<u8>,
user_handle: Option<UserHandle<USER_LEN>>,
) -> Self {
Self::new_inner(client_data_json, authenticator_data, signature, user_handle)
}
#[inline]
#[must_use]
pub fn without_user(
client_data_json: Vec<u8>,
authenticator_data: Vec<u8>,
signature: Vec<u8>,
) -> Self {
Self::with_optional_user(client_data_json, authenticator_data, signature, None)
}
#[inline]
#[must_use]
pub fn with_user(
client_data_json: Vec<u8>,
authenticator_data: Vec<u8>,
signature: Vec<u8>,
user_handle: UserHandle<USER_LEN>,
) -> Self {
Self::with_optional_user(
client_data_json,
authenticator_data,
signature,
Some(user_handle),
)
}
}
impl<const USER_LEN: usize> AuthenticatorAssertion<USER_LEN, true> {
#[expect(clippy::unreachable, reason = "want to crash when there is a bug")]
#[inline]
#[must_use]
pub fn user_handle(&self) -> &UserHandle<USER_LEN> {
self.user_handle
.as_ref()
.unwrap_or_else(|| unreachable!("bug in AuthenticatorAssertion<USER_LEN, true>"))
}
#[inline]
#[must_use]
pub fn new(
client_data_json: Vec<u8>,
authenticator_data: Vec<u8>,
signature: Vec<u8>,
user_handle: UserHandle<USER_LEN>,
) -> Self {
Self::new_inner(
client_data_json,
authenticator_data,
signature,
Some(user_handle),
)
}
}
impl<const USER_LEN: usize, const DISCOVERABLE: bool> AuthResponse
for AuthenticatorAssertion<USER_LEN, DISCOVERABLE>
{
type Auth<'a>
= AuthenticatorData<'a>
where
Self: 'a;
type CredKey<'a> = CompressedPubKeyBorrowed<'a>;
#[expect(clippy::too_many_lines, reason = "134 lines is OK")]
fn parse_data_and_verify_sig(
&self,
key: Self::CredKey<'_>,
relaxed: bool,
) -> Result<
(CollectedClientData<'_>, Self::Auth<'_>),
AuthRespErr<<Self::Auth<'_> as AuthDataContainer<'_>>::Err>,
> {
#[expect(clippy::unreachable, reason = "we want to crash when there is a bug")]
#[cfg(not(feature = "serde_relaxed"))]
fn get_client_collected_data<const LEN: usize, const DISC: bool>(_: &[u8]) -> ! {
unreachable!(
"AuthenticatorAssertion::parse_data_and_verify_sig must be passed false when serde_relaxed is not enabled"
);
}
#[cfg(feature = "serde_relaxed")]
fn get_client_collected_data<const LEN: usize, const DISC: bool>(
data: &[u8],
) -> Result<
CollectedClientData<'_>,
AuthRespErr<
<<AuthenticatorAssertion<LEN, DISC> as AuthResponse>::Auth<'_> as AuthDataContainer<'_>>::Err,
>,
>{
CollectedClientData::from_client_data_json_relaxed::<false>(data)
.map_err(AuthRespErr::CollectedClientDataRelaxed)
}
if relaxed {
get_client_collected_data::<USER_LEN, DISCOVERABLE>(self.client_data_json.as_slice())
} else {
CollectedClientData::from_client_data_json::<false>(self.client_data_json.as_slice())
.map_err(AuthRespErr::CollectedClientData)
}
.and_then(|client_data_json| {
Self::Auth::from_data(self.authenticator_data_and_c_data_hash.as_slice())
.map_err(AuthRespErr::Auth)
.and_then(|val| {
match key {
CompressedPubKeyBorrowed::MlDsa87(k) => self
.signature
.as_slice()
.try_into()
.map_err(|_e| AuthRespErr::Signature)
.and_then(|s| {
MlDsaSig::<MlDsa87>::decode(s)
.ok_or(AuthRespErr::Signature)
.and_then(|sig| {
k.into_ver_key()
.verify(
self.authenticator_data_and_c_data_hash.as_slice(),
&sig,
)
.map_err(|_e| AuthRespErr::Signature)
})
}),
CompressedPubKeyBorrowed::MlDsa65(k) => self
.signature
.as_slice()
.try_into()
.map_err(|_e| AuthRespErr::Signature)
.and_then(|s| {
MlDsaSig::<MlDsa65>::decode(s)
.ok_or(AuthRespErr::Signature)
.and_then(|sig| {
k.into_ver_key()
.verify(
self.authenticator_data_and_c_data_hash.as_slice(),
&sig,
)
.map_err(|_e| AuthRespErr::Signature)
})
}),
CompressedPubKeyBorrowed::MlDsa44(k) => self
.signature
.as_slice()
.try_into()
.map_err(|_e| AuthRespErr::Signature)
.and_then(|s| {
MlDsaSig::<MlDsa44>::decode(s)
.ok_or(AuthRespErr::Signature)
.and_then(|sig| {
k.into_ver_key()
.verify(
self.authenticator_data_and_c_data_hash.as_slice(),
&sig,
)
.map_err(|_e| AuthRespErr::Signature)
})
}),
CompressedPubKeyBorrowed::Ed25519(k) => k
.into_ver_key()
.map_err(AuthRespErr::PubKey)
.and_then(|ver_key| {
Signature::from_slice(self.signature.as_slice())
.and_then(|sig| {
ver_key.verify(
self.authenticator_data_and_c_data_hash.as_slice(),
&sig,
)
})
.map_err(|_e| AuthRespErr::Signature)
}),
CompressedPubKeyBorrowed::P256(k) => k
.into_ver_key()
.map_err(AuthRespErr::PubKey)
.and_then(|ver_key| {
P256DerSig::from_bytes(self.signature.as_slice())
.and_then(|sig| {
ver_key.verify(
self.authenticator_data_and_c_data_hash.as_slice(),
&sig,
)
})
.map_err(|_e| AuthRespErr::Signature)
}),
CompressedPubKeyBorrowed::P384(k) => k
.into_ver_key()
.map_err(AuthRespErr::PubKey)
.and_then(|ver_key| {
P384DerSig::from_bytes(self.signature.as_slice())
.and_then(|sig| {
ver_key.verify(
self.authenticator_data_and_c_data_hash.as_slice(),
&sig,
)
})
.map_err(|_e| AuthRespErr::Signature)
}),
CompressedPubKeyBorrowed::Rsa(k) => {
pkcs1v15::Signature::try_from(self.signature.as_slice())
.and_then(|sig| {
k.as_ver_key().verify(
self.authenticator_data_and_c_data_hash.as_slice(),
&sig,
)
})
.map_err(|_e| AuthRespErr::Signature)
}
}
.map(|()| (client_data_json, val.data))
})
})
}
}
pub type DiscoverableAuthenticatorAssertion<const USER_LEN: usize> =
AuthenticatorAssertion<USER_LEN, true>;
pub type NonDiscoverableAuthenticatorAssertion<const USER_LEN: usize> =
AuthenticatorAssertion<USER_LEN, false>;
impl<const USER_LEN: usize> From<DiscoverableAuthenticatorAssertion<USER_LEN>>
for NonDiscoverableAuthenticatorAssertion<USER_LEN>
{
#[inline]
fn from(value: DiscoverableAuthenticatorAssertion<USER_LEN>) -> Self {
Self {
client_data_json: value.client_data_json,
authenticator_data_and_c_data_hash: value.authenticator_data_and_c_data_hash,
signature: value.signature,
user_handle: value.user_handle,
}
}
}
impl<const USER_LEN: usize> TryFrom<NonDiscoverableAuthenticatorAssertion<USER_LEN>>
for DiscoverableAuthenticatorAssertion<USER_LEN>
{
type Error = MissingUserHandleErr;
#[inline]
fn try_from(
value: NonDiscoverableAuthenticatorAssertion<USER_LEN>,
) -> Result<Self, MissingUserHandleErr> {
if value.user_handle.is_some() {
Ok(Self {
client_data_json: value.client_data_json,
authenticator_data_and_c_data_hash: value.authenticator_data_and_c_data_hash,
signature: value.signature,
user_handle: value.user_handle,
})
} else {
Err(MissingUserHandleErr)
}
}
}
#[expect(
clippy::field_scoped_visibility_modifiers,
reason = "no invariants to uphold"
)]
#[derive(Debug)]
pub struct Authentication<const USER_LEN: usize, const DISCOVERABLE: bool> {
pub(crate) raw_id: CredentialId<Box<[u8]>>,
pub(crate) response: AuthenticatorAssertion<USER_LEN, DISCOVERABLE>,
pub(crate) authenticator_attachment: AuthenticatorAttachment,
}
impl<const USER_LEN: usize, const DISCOVERABLE: bool> Authentication<USER_LEN, DISCOVERABLE> {
#[inline]
#[must_use]
pub fn raw_id(&self) -> CredentialId<&[u8]> {
(&self.raw_id).into()
}
#[inline]
#[must_use]
pub const fn response(&self) -> &AuthenticatorAssertion<USER_LEN, DISCOVERABLE> {
&self.response
}
#[inline]
#[must_use]
pub const fn authenticator_attachment(&self) -> AuthenticatorAttachment {
self.authenticator_attachment
}
#[cfg(feature = "custom")]
#[inline]
#[must_use]
pub const fn new(
raw_id: CredentialId<Box<[u8]>>,
response: AuthenticatorAssertion<USER_LEN, DISCOVERABLE>,
authenticator_attachment: AuthenticatorAttachment,
) -> Self {
Self {
raw_id,
response,
authenticator_attachment,
}
}
#[inline]
pub fn challenge(&self) -> Result<SentChallenge, CollectedClientDataErr> {
LimitedVerificationParser::<false>::get_sent_challenge(
self.response.client_data_json.as_slice(),
)
}
#[cfg(feature = "serde_relaxed")]
#[inline]
pub fn challenge_relaxed(&self) -> Result<SentChallenge, SerdeJsonErr> {
RelaxedClientDataJsonParser::<false>::get_sent_challenge(
self.response.client_data_json.as_slice(),
)
}
#[cfg(feature = "serde_relaxed")]
#[inline]
pub fn from_json_relaxed<'a>(json: &'a [u8]) -> Result<Self, SerdeJsonErr>
where
UserHandle<USER_LEN>: Deserialize<'a>,
{
serde_json::from_slice::<AuthenticationRelaxed<USER_LEN, DISCOVERABLE>>(json)
.map(|val| val.0)
}
#[cfg(feature = "serde_relaxed")]
#[inline]
pub fn from_json_custom<'a>(json: &'a [u8]) -> Result<Self, SerdeJsonErr>
where
UserHandle<USER_LEN>: Deserialize<'a>,
{
serde_json::from_slice::<CustomAuthentication<USER_LEN, DISCOVERABLE>>(json)
.map(|val| val.0)
}
}
impl<const USER_LEN: usize, const DISCOVERABLE: bool> Response
for Authentication<USER_LEN, DISCOVERABLE>
{
type Auth = AuthenticatorAssertion<USER_LEN, DISCOVERABLE>;
fn auth(&self) -> &Self::Auth {
&self.response
}
}
pub type DiscoverableAuthentication<const USER_LEN: usize> = Authentication<USER_LEN, true>;
pub type DiscoverableAuthentication64 = Authentication<USER_HANDLE_MAX_LEN, true>;
pub type DiscoverableAuthentication16 = Authentication<16, true>;
pub type NonDiscoverableAuthentication<const USER_LEN: usize> = Authentication<USER_LEN, false>;
impl<const USER_LEN: usize> From<DiscoverableAuthentication<USER_LEN>>
for NonDiscoverableAuthentication<USER_LEN>
{
#[inline]
fn from(value: DiscoverableAuthentication<USER_LEN>) -> Self {
Self {
raw_id: value.raw_id,
response: value.response.into(),
authenticator_attachment: value.authenticator_attachment,
}
}
}
impl<const USER_LEN: usize> TryFrom<NonDiscoverableAuthentication<USER_LEN>>
for DiscoverableAuthentication<USER_LEN>
{
type Error = MissingUserHandleErr;
#[inline]
fn try_from(
value: NonDiscoverableAuthentication<USER_LEN>,
) -> Result<Self, MissingUserHandleErr> {
value.response.try_into().map(|response| Self {
raw_id: value.raw_id,
response,
authenticator_attachment: value.authenticator_attachment,
})
}
}
pub type NonDiscoverableAuthentication64 = Authentication<USER_HANDLE_MAX_LEN, false>;
pub type NonDiscoverableAuthentication16 = Authentication<16, false>;