use std::fmt;
use ed25519_dalek::VerifyingKey;
use p256::ecdsa::signature::Verifier;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha2::{Digest, Sha256};
use super::{PortableUserId, Signature, parse_hex, write_hex};
pub const PORTABLE_RP: &str = "portable.spoolframe.app";
pub const MAX_ROOTS: usize = 8;
pub const MAX_ROOT_CHANGES: usize = 64;
pub const MAX_CREDENTIAL_ID: usize = 128;
pub const MAX_AUTHENTICATOR_DATA: usize = 512;
pub const MAX_DER_SIGNATURE: usize = 72;
pub const P256_PUBLIC_KEY_LEN: usize = 65;
pub const GENESIS_DOMAIN: &[u8] = b"spoolframe identity genesis v1\0";
pub const ROOT_ADD_DOMAIN: &[u8] = b"spoolframe root add v1\0";
pub const ROOT_ENDORSE_DOMAIN: &[u8] = b"spoolframe root endorse v1\0";
pub const USER_ID_CONTEXT: &str = "spoolframe portable user id v1";
const FLAG_UP: u8 = 0x01;
const FLAG_UV: u8 = 0x04;
pub const MAX_CLIENT_DATA_JSON: usize = 4096;
pub const MAX_ATTESTATION_OBJECT: usize = 8192;
const WEBAUTHN_GET: &str = "webauthn.get";
const WEBAUTHN_CREATE: &str = "webauthn.create";
const FLAG_AT: u8 = 0x40;
const FLAG_ED: u8 = 0x80;
const RP_ID_HASH_LEN: usize = 32;
const MIN_AUTHENTICATOR_DATA: usize = RP_ID_HASH_LEN + 1 + 4;
pub fn portable_rp_id_hash() -> [u8; 32] {
Sha256::digest(PORTABLE_RP.as_bytes()).into()
}
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum RootKey {
Phrase([u8; 32]),
SecurityKey {
public_key: [u8; P256_PUBLIC_KEY_LEN],
credential_id: Vec<u8>,
},
}
impl RootKey {
pub fn phrase_of(user: &PortableUserId) -> Self {
RootKey::Phrase(*user.as_bytes())
}
pub fn bytes(&self) -> Vec<u8> {
match self {
RootKey::Phrase(key) => {
let mut bytes = Vec::with_capacity(33);
bytes.push(1);
bytes.extend_from_slice(key);
bytes
}
RootKey::SecurityKey {
public_key,
credential_id,
} => {
let mut bytes = Vec::with_capacity(1 + 65 + 1 + credential_id.len());
bytes.push(2);
bytes.extend_from_slice(public_key);
bytes.push(u8::try_from(credential_id.len()).expect("a credential id fits a byte"));
bytes.extend_from_slice(credential_id);
bytes
}
}
}
pub fn is_valid(&self) -> bool {
match self {
RootKey::Phrase(key) => VerifyingKey::from_bytes(key).is_ok(),
RootKey::SecurityKey {
public_key,
credential_id,
} => {
!credential_id.is_empty()
&& credential_id.len() <= MAX_CREDENTIAL_ID
&& p256::ecdsa::VerifyingKey::from_sec1_bytes(public_key).is_ok()
}
}
}
pub fn verify(&self, message: &[u8], signature: &RootSignature) -> bool {
match (self, signature) {
(RootKey::Phrase(key), RootSignature::Ed25519(signature)) => {
VerifyingKey::from_bytes(key)
.is_ok_and(|key| key.verify_strict(message, signature).is_ok())
}
(
RootKey::SecurityKey { public_key, .. },
RootSignature::Ctap {
authenticator_data,
signature,
},
) => verify_ctap(public_key, message, authenticator_data, signature),
(
RootKey::SecurityKey { public_key, .. },
RootSignature::WebAuthn {
authenticator_data,
client_data_json,
signature,
},
) => verify_webauthn(
public_key,
message,
authenticator_data,
client_data_json,
signature,
)
.is_ok(),
_ => false,
}
}
pub fn fingerprint(&self) -> String {
match self {
RootKey::Phrase(key) => crate::fingerprint::fingerprint(key),
RootKey::SecurityKey { .. } => {
crate::fingerprint::fingerprint(blake3::hash(&self.bytes()).as_bytes())
}
}
}
pub fn kind(&self) -> &'static str {
match self {
RootKey::Phrase(_) => "phrase",
RootKey::SecurityKey { .. } => "security_key",
}
}
}
impl fmt::Debug for RootKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "RootKey({})", self.fingerprint())
}
}
fn verify_ctap(
public_key: &[u8; P256_PUBLIC_KEY_LEN],
message: &[u8],
authenticator_data: &[u8],
der: &[u8],
) -> bool {
if authenticator_data.len() < MIN_AUTHENTICATOR_DATA
|| authenticator_data.len() > MAX_AUTHENTICATOR_DATA
|| der.len() > MAX_DER_SIGNATURE
{
return false;
}
if authenticator_data[..RP_ID_HASH_LEN] != portable_rp_id_hash() {
return false;
}
let flags = authenticator_data[RP_ID_HASH_LEN];
if flags & FLAG_UP == 0 || flags & FLAG_UV == 0 {
return false;
}
let (Ok(key), Ok(signature)) = (
p256::ecdsa::VerifyingKey::from_sec1_bytes(public_key),
p256::ecdsa::Signature::from_der(der),
) else {
return false;
};
let mut signed = Vec::with_capacity(authenticator_data.len() + 32);
signed.extend_from_slice(authenticator_data);
signed.extend_from_slice(&Sha256::digest(message));
key.verify(&signed, &signature).is_ok()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum WebAuthnError {
#[error("a WebAuthn field is too long")]
TooLong,
#[error("the clientDataJSON is not WebAuthn client data")]
MalformedClientData,
#[error("the clientDataJSON is of the wrong type")]
WrongType,
#[error("the clientDataJSON's challenge is not this message's")]
WrongChallenge,
#[error("the clientDataJSON is cross-origin")]
CrossOrigin,
#[error("the authenticatorData does not parse")]
MalformedAuthenticatorData,
#[error("the authenticatorData is another relying party's")]
WrongRp,
#[error("the key was not touched, or no PIN was given")]
NotVerified,
#[error("the signature does not check out")]
BadSignature,
#[error("the attestationObject does not parse")]
MalformedAttestation,
#[error("the registration carries no credential")]
NoCredential,
#[error("the credential id is empty or too long")]
BadCredentialId,
#[error("the credential is not an ES256 P-256 key")]
UnsupportedKey,
}
fn check_client_data(
client_data_json: &[u8],
kind: &str,
challenge: &[u8],
) -> Result<(), WebAuthnError> {
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
if client_data_json.len() > MAX_CLIENT_DATA_JSON {
return Err(WebAuthnError::TooLong);
}
let data =
super::client_data::parse(client_data_json).ok_or(WebAuthnError::MalformedClientData)?;
let (Some(kind_read), Some(challenge_read)) = (data.kind, data.challenge) else {
return Err(WebAuthnError::MalformedClientData);
};
let cross_origin = data
.cross_origin
.transpose()
.map_err(|()| WebAuthnError::MalformedClientData)?;
if kind_read != kind {
return Err(WebAuthnError::WrongType);
}
if challenge_read != URL_SAFE_NO_PAD.encode(challenge) {
return Err(WebAuthnError::WrongChallenge);
}
if cross_origin == Some(true) {
return Err(WebAuthnError::CrossOrigin);
}
Ok(())
}
fn check_authenticator_data(authenticator_data: &[u8]) -> Result<u8, WebAuthnError> {
if authenticator_data.len() < MIN_AUTHENTICATOR_DATA {
return Err(WebAuthnError::MalformedAuthenticatorData);
}
if authenticator_data[..RP_ID_HASH_LEN] != portable_rp_id_hash() {
return Err(WebAuthnError::WrongRp);
}
let flags = authenticator_data[RP_ID_HASH_LEN];
if flags & FLAG_UP == 0 || flags & FLAG_UV == 0 {
return Err(WebAuthnError::NotVerified);
}
Ok(flags)
}
pub(crate) fn verify_webauthn(
public_key: &[u8; P256_PUBLIC_KEY_LEN],
message: &[u8],
authenticator_data: &[u8],
client_data_json: &[u8],
der: &[u8],
) -> Result<(), WebAuthnError> {
if client_data_json.len() > MAX_CLIENT_DATA_JSON
|| authenticator_data.len() > MAX_AUTHENTICATOR_DATA
|| der.len() > MAX_DER_SIGNATURE
{
return Err(WebAuthnError::TooLong);
}
check_client_data(client_data_json, WEBAUTHN_GET, &Sha256::digest(message))?;
check_authenticator_data(authenticator_data)?;
let (Ok(key), Ok(signature)) = (
p256::ecdsa::VerifyingKey::from_sec1_bytes(public_key),
p256::ecdsa::Signature::from_der(der),
) else {
return Err(WebAuthnError::BadSignature);
};
let mut signed = Vec::with_capacity(authenticator_data.len() + 32);
signed.extend_from_slice(authenticator_data);
signed.extend_from_slice(&Sha256::digest(client_data_json));
key.verify(&signed, &signature)
.map_err(|_| WebAuthnError::BadSignature)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RootRegistration {
Ctap(RootKey),
WebAuthn {
attestation_object: Vec<u8>,
client_data_json: Vec<u8>,
},
}
impl RootRegistration {
pub fn root(&self, handle: &[u8; HANDLE_LEN]) -> Result<RootKey, WebAuthnError> {
match self {
RootRegistration::Ctap(root) => Ok(root.clone()),
RootRegistration::WebAuthn {
attestation_object,
client_data_json,
} => webauthn_registration_root(attestation_object, client_data_json, handle),
}
}
}
fn webauthn_registration_root(
attestation_object: &[u8],
client_data_json: &[u8],
handle: &[u8; HANDLE_LEN],
) -> Result<RootKey, WebAuthnError> {
if attestation_object.len() > MAX_ATTESTATION_OBJECT
|| client_data_json.len() > MAX_CLIENT_DATA_JSON
{
return Err(WebAuthnError::TooLong);
}
check_client_data(client_data_json, WEBAUTHN_CREATE, handle)?;
let auth_data = attestation_auth_data(attestation_object)?;
let flags = check_authenticator_data(&auth_data)?;
if flags & FLAG_AT == 0 {
return Err(WebAuthnError::NoCredential);
}
let malformed = WebAuthnError::MalformedAuthenticatorData;
let attested = &auth_data[MIN_AUTHENTICATOR_DATA..];
let id_len = attested.get(16..18).ok_or(malformed)?;
let id_len = usize::from(u16::from_be_bytes([id_len[0], id_len[1]]));
if id_len == 0 || id_len > MAX_CREDENTIAL_ID {
return Err(WebAuthnError::BadCredentialId);
}
let credential_id = attested.get(18..18 + id_len).ok_or(malformed)?.to_vec();
let mut rest = &attested[18 + id_len..];
let cose: ciborium::Value =
ciborium::de::from_reader_with_recursion_limit(&mut rest, 4).map_err(|_| malformed)?;
if flags & FLAG_ED == 0 && !rest.is_empty() {
return Err(malformed);
}
let public_key = cose_es256_key(cose)?;
let root = RootKey::SecurityKey {
public_key,
credential_id,
};
if !root.is_valid() {
return Err(WebAuthnError::UnsupportedKey);
}
Ok(root)
}
fn attestation_auth_data(attestation_object: &[u8]) -> Result<Vec<u8>, WebAuthnError> {
use ciborium::Value;
let malformed = WebAuthnError::MalformedAttestation;
let mut rest = attestation_object;
let Value::Map(members) =
ciborium::de::from_reader_with_recursion_limit(&mut rest, 16).map_err(|_| malformed)?
else {
return Err(malformed);
};
if !rest.is_empty() {
return Err(malformed);
}
let (mut fmt, mut statement, mut auth_data) = (false, false, None);
for (key, value) in members {
let seen = match (key.as_text(), value) {
(Some("fmt"), Value::Text(_)) => std::mem::replace(&mut fmt, true),
(Some("attStmt"), Value::Map(_)) => std::mem::replace(&mut statement, true),
(Some("authData"), Value::Bytes(bytes)) => auth_data.replace(bytes).is_some(),
(Some("fmt" | "attStmt" | "authData"), _) => return Err(malformed),
_ => false,
};
if seen {
return Err(malformed);
}
}
match auth_data {
Some(auth_data) if fmt && statement => Ok(auth_data),
_ => Err(malformed),
}
}
fn cose_es256_key(cose: ciborium::Value) -> Result<[u8; P256_PUBLIC_KEY_LEN], WebAuthnError> {
use ciborium::Value;
let malformed = WebAuthnError::MalformedAuthenticatorData;
let Value::Map(members) = cose else {
return Err(malformed);
};
let mut seen: Vec<i128> = Vec::with_capacity(members.len());
let (mut kty, mut alg, mut crv, mut x, mut y) = (None, None, None, None, None);
for (key, value) in members {
let key = i128::from(key.as_integer().ok_or(malformed)?);
if seen.contains(&key) {
return Err(malformed);
}
seen.push(key);
let integer = value.as_integer().map(i128::from);
match key {
1 => kty = integer,
3 => alg = integer,
-1 => crv = integer,
-2 => x = value.into_bytes().ok(),
-3 => y = value.into_bytes().ok(),
_ => {}
}
}
let (Some(2), Some(-7), Some(1), Some(x), Some(y)) = (kty, alg, crv, x, y) else {
return Err(WebAuthnError::UnsupportedKey);
};
if x.len() != 32 || y.len() != 32 {
return Err(WebAuthnError::UnsupportedKey);
}
let mut point = [0u8; P256_PUBLIC_KEY_LEN];
point[0] = 4;
point[1..33].copy_from_slice(&x);
point[33..].copy_from_slice(&y);
Ok(point)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RootSignature {
Ed25519(Signature),
Ctap {
authenticator_data: Vec<u8>,
signature: Vec<u8>,
},
WebAuthn {
authenticator_data: Vec<u8>,
client_data_json: Vec<u8>,
signature: Vec<u8>,
},
}
impl RootSignature {
pub fn as_ed25519(&self) -> Option<&Signature> {
match self {
RootSignature::Ed25519(signature) => Some(signature),
RootSignature::Ctap { .. } | RootSignature::WebAuthn { .. } => None,
}
}
}
impl From<Signature> for RootSignature {
fn from(signature: Signature) -> Self {
RootSignature::Ed25519(signature)
}
}
pub(crate) mod hex_vec {
use super::*;
struct Hex<'a>(&'a [u8]);
impl fmt::Display for Hex<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write_hex(f, self.0)
}
}
pub fn serialize<S: Serializer>(bytes: &[u8], s: S) -> Result<S::Ok, S::Error> {
s.collect_str(&Hex(bytes))
}
pub fn parse(text: &str, max: usize) -> Option<Vec<u8>> {
if !text.len().is_multiple_of(2) || text.len() > 2 * max || !text.is_ascii() {
return None;
}
(0..text.len() / 2)
.map(|i| u8::from_str_radix(&text[2 * i..2 * i + 2], 16).ok())
.collect()
}
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct CtapWire {
#[serde(serialize_with = "hex_vec::serialize")]
#[serde(deserialize_with = "authenticator_data_hex")]
authenticator_data: Vec<u8>,
#[serde(serialize_with = "hex_vec::serialize")]
#[serde(deserialize_with = "der_hex")]
signature: Vec<u8>,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct WebAuthnWire {
#[serde(serialize_with = "hex_vec::serialize")]
#[serde(deserialize_with = "authenticator_data_hex")]
authenticator_data: Vec<u8>,
#[serde(serialize_with = "hex_vec::serialize")]
#[serde(deserialize_with = "client_data_json_hex")]
client_data_json: Vec<u8>,
#[serde(serialize_with = "hex_vec::serialize")]
#[serde(deserialize_with = "der_hex")]
signature: Vec<u8>,
}
fn client_data_json_hex<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
let text = String::deserialize(d)?;
hex_vec::parse(&text, MAX_CLIENT_DATA_JSON)
.ok_or_else(|| serde::de::Error::custom("client data JSON is at most 4096 bytes of hex"))
}
fn authenticator_data_hex<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
let text = String::deserialize(d)?;
hex_vec::parse(&text, MAX_AUTHENTICATOR_DATA)
.ok_or_else(|| serde::de::Error::custom("authenticator data is at most 512 bytes of hex"))
}
fn der_hex<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
let text = String::deserialize(d)?;
hex_vec::parse(&text, MAX_DER_SIGNATURE)
.ok_or_else(|| serde::de::Error::custom("a DER signature is at most 72 bytes of hex"))
}
#[derive(Deserialize)]
#[serde(untagged)]
enum RootSignatureWire {
Ed25519(String),
WebAuthn(WebAuthnWire),
Ctap(CtapWire),
}
impl Serialize for RootSignature {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
match self {
RootSignature::Ed25519(signature) => super::signature_hex::serialize(signature, s),
RootSignature::Ctap {
authenticator_data,
signature,
} => CtapWire {
authenticator_data: authenticator_data.clone(),
signature: signature.clone(),
}
.serialize(s),
RootSignature::WebAuthn {
authenticator_data,
client_data_json,
signature,
} => WebAuthnWire {
authenticator_data: authenticator_data.clone(),
client_data_json: client_data_json.clone(),
signature: signature.clone(),
}
.serialize(s),
}
}
}
impl<'de> Deserialize<'de> for RootSignature {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
match RootSignatureWire::deserialize(d)? {
RootSignatureWire::Ed25519(text) => parse_hex::<{ Signature::BYTE_SIZE }>(&text)
.map(|bytes| RootSignature::Ed25519(Signature::from_bytes(&bytes)))
.ok_or_else(|| serde::de::Error::custom("a signature is 128 hex digits")),
RootSignatureWire::Ctap(CtapWire {
authenticator_data,
signature,
}) => Ok(RootSignature::Ctap {
authenticator_data,
signature,
}),
RootSignatureWire::WebAuthn(WebAuthnWire {
authenticator_data,
client_data_json,
signature,
}) => Ok(RootSignature::WebAuthn {
authenticator_data,
client_data_json,
signature,
}),
}
}
}
#[derive(Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum RootKeyWire {
Phrase {
public_key: String,
},
SecurityKey {
public_key: String,
credential_id: String,
},
}
impl Serialize for RootKey {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
let hex = |bytes: &[u8]| bytes.iter().map(|b| format!("{b:02x}")).collect::<String>();
match self {
RootKey::Phrase(key) => RootKeyWire::Phrase {
public_key: hex(key),
},
RootKey::SecurityKey {
public_key,
credential_id,
} => RootKeyWire::SecurityKey {
public_key: hex(public_key),
credential_id: hex(credential_id),
},
}
.serialize(s)
}
}
impl<'de> Deserialize<'de> for RootKey {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let bad = |what: &'static str| serde::de::Error::custom(what);
match RootKeyWire::deserialize(d)? {
RootKeyWire::Phrase { public_key } => parse_hex::<32>(&public_key)
.map(RootKey::Phrase)
.ok_or_else(|| bad("a phrase root is 64 hex digits")),
RootKeyWire::SecurityKey {
public_key,
credential_id,
} => {
let public_key = parse_hex::<P256_PUBLIC_KEY_LEN>(&public_key)
.ok_or_else(|| bad("a security key's public key is 130 hex digits"))?;
let credential_id = hex_vec::parse(&credential_id, MAX_CREDENTIAL_ID)
.filter(|id| !id.is_empty())
.ok_or_else(|| bad("a credential id is 1 to 128 bytes of hex"))?;
Ok(RootKey::SecurityKey {
public_key,
credential_id,
})
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum GenesisError {
#[error("an identity has 1 to {MAX_ROOTS} roots")]
RootCount,
#[error("an identity's genesis has one signature for each root")]
SignatureCount,
#[error("an identity's genesis names a root twice")]
Duplicate,
#[error("an identity's genesis names a root that is not a valid key")]
InvalidRoot,
#[error("a root did not sign its identity's genesis")]
BadSignature,
}
pub const HANDLE_LEN: usize = 32;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IdentityGenesis {
#[serde(with = "handle_hex")]
pub handle: [u8; HANDLE_LEN],
pub roots: Vec<RootKey>,
pub signatures: Vec<RootSignature>,
}
pub(crate) mod handle_hex {
use super::*;
pub fn serialize<S: Serializer>(handle: &[u8; HANDLE_LEN], s: S) -> Result<S::Ok, S::Error> {
hex_vec::serialize(handle, s)
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<[u8; HANDLE_LEN], D::Error> {
let text = String::deserialize(d)?;
parse_hex(&text).ok_or_else(|| serde::de::Error::custom("a handle is 64 hex digits"))
}
}
impl IdentityGenesis {
pub fn message(handle: &[u8; HANDLE_LEN], roots: &[RootKey]) -> Option<Vec<u8>> {
let count = u8::try_from(roots.len()).ok()?;
let mut message = Vec::with_capacity(GENESIS_DOMAIN.len() + 33 + 100 * roots.len());
message.extend_from_slice(GENESIS_DOMAIN);
message.extend_from_slice(handle);
message.push(count);
for root in roots {
message.extend_from_slice(&root.bytes());
}
Some(message)
}
pub fn verify(&self) -> Result<(), GenesisError> {
if self.roots.is_empty() || self.roots.len() > MAX_ROOTS {
return Err(GenesisError::RootCount);
}
if self.signatures.len() != self.roots.len() {
return Err(GenesisError::SignatureCount);
}
if !self.roots.iter().all(RootKey::is_valid) {
return Err(GenesisError::InvalidRoot);
}
let mut seen = self.roots.clone();
seen.sort();
seen.dedup();
if seen.len() != self.roots.len() {
return Err(GenesisError::Duplicate);
}
let message = Self::message(&self.handle, &self.roots).ok_or(GenesisError::RootCount)?;
if self
.roots
.iter()
.zip(&self.signatures)
.all(|(root, signature)| root.verify(&message, signature))
{
Ok(())
} else {
Err(GenesisError::BadSignature)
}
}
pub fn user(&self) -> PortableUserId {
if let [RootKey::Phrase(key)] = self.roots.as_slice() {
return PortableUserId(*key);
}
let message =
Self::message(&self.handle, &self.roots).expect("at most 255 roots in a genesis");
PortableUserId(blake3::derive_key(USER_ID_CONTEXT, &message))
}
}
pub fn root_add_message(user: &PortableUserId, root: &RootKey) -> Vec<u8> {
let mut message = Vec::with_capacity(ROOT_ADD_DOMAIN.len() + 32 + 100);
message.extend_from_slice(ROOT_ADD_DOMAIN);
message.extend_from_slice(user.as_bytes());
message.extend_from_slice(&root.bytes());
message
}
pub fn root_endorse_message(user: &PortableUserId, root: &RootKey, added_at: i64) -> Vec<u8> {
let mut message = Vec::with_capacity(ROOT_ENDORSE_DOMAIN.len() + 32 + 100 + 8);
message.extend_from_slice(ROOT_ENDORSE_DOMAIN);
message.extend_from_slice(user.as_bytes());
message.extend_from_slice(&root.bytes());
message.extend_from_slice(&added_at.to_be_bytes());
message
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AddedRoot {
pub root: RootKey,
pub added_by: RootKey,
pub added_at: i64,
pub endorsement: RootSignature,
pub possession: RootSignature,
}
impl AddedRoot {
pub fn verify(&self, user: &PortableUserId) -> bool {
self.root.is_valid()
&& self
.root
.verify(&root_add_message(user, &self.root), &self.possession)
&& self.added_by.verify(
&root_endorse_message(user, &self.root, self.added_at),
&self.endorsement,
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RemovedRoot {
pub root: RootKey,
pub removed_at: i64,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RootSet {
pub ever: Vec<RootKey>,
pub removed: Vec<(RootKey, i64)>,
pub members: Vec<RootKey>,
}
pub struct RootChanges<'a> {
pub signer: &'a RootKey,
pub added: &'a [AddedRoot],
pub removed: &'a [RemovedRoot],
}
impl RootSet {
pub fn of(genesis: &[RootKey], states: &[RootChanges<'_>]) -> Self {
let additions: Vec<&AddedRoot> = states.iter().flat_map(|s| s.added).collect();
let closure = |admit: &dyn Fn(&[RootKey], &AddedRoot) -> bool| {
let mut set = genesis.to_vec();
loop {
let before = set.len();
for added in &additions {
if !set.contains(&added.root) && admit(&set, added) {
set.push(added.root.clone());
}
}
if set.len() == before {
return set;
}
}
};
let ever = closure(&|set, added| set.contains(&added.added_by));
let mut removed: Vec<(RootKey, i64)> = Vec::new();
for state in states.iter().filter(|s| ever.contains(s.signer)) {
for root in state.removed {
match removed.iter_mut().find(|(r, _)| *r == root.root) {
Some((_, at)) => *at = (*at).min(root.removed_at),
None => removed.push((root.root.clone(), root.removed_at)),
}
}
}
let removed_at =
|root: &RootKey| removed.iter().find(|(r, _)| r == root).map(|(_, at)| *at);
let members = closure(&|set, added| {
set.contains(&added.added_by)
&& removed_at(&added.added_by).is_none_or(|at| added.added_at <= at)
})
.into_iter()
.filter(|root| removed_at(root).is_none())
.collect();
RootSet {
ever,
removed,
members,
}
}
pub fn holds(&self, root: &RootKey) -> bool {
self.members.contains(root)
}
pub fn knew(&self, root: &RootKey) -> bool {
self.ever.contains(root)
}
pub fn is_removed(&self, root: &RootKey) -> bool {
self.removed.iter().any(|(r, _)| r == root)
}
pub fn refuses_additions(&self, added: &[AddedRoot]) -> bool {
added
.iter()
.any(|added| !self.knew(&added.root) && self.is_removed(&added.added_by))
}
}
#[cfg(test)]
mod tests {
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use p256::ecdsa::signature::Signer;
use p256::ecdsa::{DerSignature, SigningKey};
use super::*;
const MESSAGE: &[u8] = b"spoolframe root add v1\0some bytes";
fn signing(seed: u8) -> SigningKey {
SigningKey::from_bytes(&[seed; 32].into()).unwrap()
}
fn public(key: &SigningKey) -> [u8; P256_PUBLIC_KEY_LEN] {
key.verifying_key()
.to_sec1_point(false)
.as_bytes()
.try_into()
.unwrap()
}
fn client_data(kind: &str, challenge: &[u8]) -> Vec<u8> {
format!(
r#"{{"type":"{kind}","challenge":"{}","origin":"https://portable.spoolframe.app"}}"#,
URL_SAFE_NO_PAD.encode(challenge)
)
.into_bytes()
}
fn authenticator_data(rp: &str, flags: u8) -> Vec<u8> {
let mut data = Sha256::digest(rp.as_bytes()).to_vec();
data.push(flags);
data.extend_from_slice(&7u32.to_be_bytes());
data
}
fn signed(
key: &SigningKey,
authenticator_data: Vec<u8>,
client_data_json: Vec<u8>,
) -> RootSignature {
let mut bytes = authenticator_data.clone();
bytes.extend_from_slice(&Sha256::digest(&client_data_json));
let signature: DerSignature = key.sign(&bytes);
RootSignature::WebAuthn {
authenticator_data,
client_data_json,
signature: signature.as_bytes().to_vec(),
}
}
fn good(key: &SigningKey, message: &[u8]) -> RootSignature {
signed(
key,
authenticator_data(PORTABLE_RP, 0x05),
client_data(WEBAUTHN_GET, &Sha256::digest(message)),
)
}
fn check(
key: &SigningKey,
message: &[u8],
signature: &RootSignature,
) -> Result<(), WebAuthnError> {
let RootSignature::WebAuthn {
authenticator_data,
client_data_json,
signature,
} = signature
else {
unreachable!()
};
verify_webauthn(
&public(key),
message,
authenticator_data,
client_data_json,
signature,
)
}
fn root(key: &SigningKey) -> RootKey {
RootKey::SecurityKey {
public_key: public(key),
credential_id: vec![1; 32],
}
}
#[test]
fn a_webauthn_assertion_over_the_message_s_hash_verifies() {
let key = signing(3);
assert_eq!(check(&key, MESSAGE, &good(&key, MESSAGE)), Ok(()));
assert!(root(&key).verify(MESSAGE, &good(&key, MESSAGE)));
}
#[test]
fn a_webauthn_assertion_of_the_wrong_type_is_refused() {
let key = signing(3);
let made = signed(
&key,
authenticator_data(PORTABLE_RP, 0x05),
client_data("payment.get", &Sha256::digest(MESSAGE)),
);
assert_eq!(check(&key, MESSAGE, &made), Err(WebAuthnError::WrongType));
}
#[test]
fn a_webauthn_create_presented_as_an_assertion_is_refused() {
let key = signing(3);
let made = signed(
&key,
authenticator_data(PORTABLE_RP, 0x45),
client_data(WEBAUTHN_CREATE, &Sha256::digest(MESSAGE)),
);
assert_eq!(check(&key, MESSAGE, &made), Err(WebAuthnError::WrongType));
}
#[test]
fn a_webauthn_assertion_with_a_wrong_challenge_is_refused() {
let key = signing(3);
let made = signed(
&key,
authenticator_data(PORTABLE_RP, 0x05),
client_data(WEBAUTHN_GET, MESSAGE),
);
assert_eq!(
check(&key, MESSAGE, &made),
Err(WebAuthnError::WrongChallenge)
);
let padded = format!(
r#"{{"type":"webauthn.get","challenge":"{}="}}"#,
URL_SAFE_NO_PAD.encode(Sha256::digest(MESSAGE))
);
let made = signed(
&key,
authenticator_data(PORTABLE_RP, 0x05),
padded.into_bytes(),
);
assert_eq!(
check(&key, MESSAGE, &made),
Err(WebAuthnError::WrongChallenge)
);
}
#[test]
fn a_webauthn_assertion_over_another_message_is_refused() {
let key = signing(3);
let other = good(&key, b"spoolframe root add v1\0other bytes");
assert_eq!(
check(&key, MESSAGE, &other),
Err(WebAuthnError::WrongChallenge)
);
assert!(!root(&key).verify(MESSAGE, &other));
}
#[test]
fn a_cross_origin_webauthn_assertion_is_refused_and_a_false_one_is_not() {
let key = signing(3);
let challenge = URL_SAFE_NO_PAD.encode(Sha256::digest(MESSAGE));
let with = |cross: &str| {
signed(
&key,
authenticator_data(PORTABLE_RP, 0x05),
format!(
r#"{{"type":"webauthn.get","challenge":"{challenge}","crossOrigin":{cross}}}"#
)
.into_bytes(),
)
};
assert_eq!(
check(&key, MESSAGE, &with("true")),
Err(WebAuthnError::CrossOrigin)
);
assert_eq!(check(&key, MESSAGE, &with("false")), Ok(()));
}
#[test]
fn a_webauthn_assertion_with_another_origin_verifies() {
let key = signing(3);
let challenge = URL_SAFE_NO_PAD.encode(Sha256::digest(MESSAGE));
let made = signed(
&key,
authenticator_data(PORTABLE_RP, 0x05),
format!(r#"{{"type":"webauthn.get","challenge":"{challenge}","origin":"ios:bundle-id:app.spoolframe"}}"#)
.into_bytes(),
);
assert_eq!(check(&key, MESSAGE, &made), Ok(()));
}
#[test]
fn a_webauthn_assertion_under_another_rp_is_refused() {
let key = signing(3);
let made = signed(
&key,
authenticator_data("spoolframe.app", 0x05),
client_data(WEBAUTHN_GET, &Sha256::digest(MESSAGE)),
);
assert_eq!(check(&key, MESSAGE, &made), Err(WebAuthnError::WrongRp));
}
#[test]
fn a_webauthn_assertion_without_up_or_uv_is_refused() {
let key = signing(3);
for flags in [0x01, 0x04, 0x00] {
let made = signed(
&key,
authenticator_data(PORTABLE_RP, flags),
client_data(WEBAUTHN_GET, &Sha256::digest(MESSAGE)),
);
assert_eq!(
check(&key, MESSAGE, &made),
Err(WebAuthnError::NotVerified),
"{flags:#x}"
);
}
}
#[test]
fn a_webauthn_assertion_with_a_bad_signature_is_refused() {
let key = signing(3);
let RootSignature::WebAuthn {
authenticator_data,
client_data_json,
mut signature,
} = good(&key, MESSAGE)
else {
unreachable!()
};
let last = signature.len() - 1;
signature[last] ^= 1;
let made = RootSignature::WebAuthn {
authenticator_data,
client_data_json,
signature,
};
assert_eq!(
check(&key, MESSAGE, &made),
Err(WebAuthnError::BadSignature)
);
}
#[test]
fn a_webauthn_assertion_from_another_credential_is_refused() {
let (key, other) = (signing(3), signing(4));
assert_eq!(
check(&key, MESSAGE, &good(&other, MESSAGE)),
Err(WebAuthnError::BadSignature)
);
assert!(!root(&key).verify(MESSAGE, &good(&other, MESSAGE)));
}
#[test]
fn an_oversize_client_data_json_is_refused_before_it_parses() {
let key = signing(3);
let challenge = URL_SAFE_NO_PAD.encode(Sha256::digest(MESSAGE));
let mut json =
format!(r#"{{"type":"webauthn.get","challenge":"{challenge}","pad":""#).into_bytes();
json.resize(MAX_CLIENT_DATA_JSON - 2, b'x');
json.extend_from_slice(b"\"}");
assert_eq!(json.len(), MAX_CLIENT_DATA_JSON);
let at_bound = signed(&key, authenticator_data(PORTABLE_RP, 0x05), json.clone());
assert_eq!(check(&key, MESSAGE, &at_bound), Ok(()));
json.insert(json.len() - 2, b'x');
let over = signed(&key, authenticator_data(PORTABLE_RP, 0x05), json);
assert_eq!(check(&key, MESSAGE, &over), Err(WebAuthnError::TooLong));
}
#[test]
fn malformed_client_data_json_is_refused() {
let key = signing(3);
let challenge = URL_SAFE_NO_PAD.encode(Sha256::digest(MESSAGE));
for json in [
b"".to_vec(),
b"not json".to_vec(),
b"[]".to_vec(),
b"\xff\xfe".to_vec(),
format!(r#"{{"challenge":"{challenge}"}}"#).into_bytes(),
br#"{"type":"webauthn.get"}"#.to_vec(),
format!(r#"{{"type":1,"challenge":"{challenge}"}}"#).into_bytes(),
format!(r#"{{"type":"webauthn.get","challenge":"{challenge}","crossOrigin":"no"}}"#)
.into_bytes(),
format!(
r#"{{"type":"webauthn.create","type":"webauthn.get","challenge":"{challenge}"}}"#
)
.into_bytes(),
format!(r#"{{"type":"webauthn.get","challenge":"{challenge}""#).into_bytes(),
] {
let made = signed(&key, authenticator_data(PORTABLE_RP, 0x05), json.clone());
assert_eq!(
check(&key, MESSAGE, &made),
Err(WebAuthnError::MalformedClientData),
"{}",
String::from_utf8_lossy(&json)
);
}
}
#[test]
fn oversize_or_short_webauthn_fields_are_refused() {
let key = signing(3);
let RootSignature::WebAuthn {
authenticator_data,
client_data_json,
signature,
} = good(&key, MESSAGE)
else {
unreachable!()
};
let mut long_data = authenticator_data.clone();
long_data.resize(MAX_AUTHENTICATOR_DATA + 1, 0);
let mut long_signature = signature.clone();
long_signature.resize(MAX_DER_SIGNATURE + 1, 0);
let public = public(&key);
assert_eq!(
verify_webauthn(&public, MESSAGE, &long_data, &client_data_json, &signature),
Err(WebAuthnError::TooLong)
);
assert_eq!(
verify_webauthn(
&public,
MESSAGE,
&authenticator_data,
&client_data_json,
&long_signature
),
Err(WebAuthnError::TooLong)
);
assert_eq!(
verify_webauthn(
&public,
MESSAGE,
&authenticator_data[..36],
&client_data_json,
&signature
),
Err(WebAuthnError::MalformedAuthenticatorData)
);
}
#[test]
fn a_webauthn_signature_is_no_phrase_root_s() {
let key = signing(3);
let phrase = RootKey::Phrase(
ed25519_dalek::SigningKey::from_bytes(&[5; 32])
.verifying_key()
.to_bytes(),
);
assert!(!phrase.verify(MESSAGE, &good(&key, MESSAGE)));
}
#[test]
fn a_webauthn_signature_round_trips_and_reads_as_neither_other_form() {
let key = signing(3);
let made = good(&key, MESSAGE);
let json = serde_json::to_value(&made).unwrap();
assert_eq!(
json.as_object().unwrap().keys().collect::<Vec<_>>(),
["authenticator_data", "client_data_json", "signature"]
);
assert_eq!(
serde_json::from_value::<RootSignature>(json.clone()).unwrap(),
made
);
let mut cbor = Vec::new();
ciborium::into_writer(&made, &mut cbor).unwrap();
assert_eq!(
ciborium::from_reader::<RootSignature, _>(cbor.as_slice()).unwrap(),
made
);
let mut extra = json.clone();
extra.as_object_mut().unwrap().remove("client_data_json");
extra
.as_object_mut()
.unwrap()
.insert("origin".into(), "00".into());
assert!(serde_json::from_value::<RootSignature>(extra).is_err());
let mut over = json;
over["client_data_json"] = "00".repeat(MAX_CLIENT_DATA_JSON + 1).into();
assert!(serde_json::from_value::<RootSignature>(over).is_err());
}
const HANDLE: [u8; HANDLE_LEN] = [0x42; HANDLE_LEN];
fn cose_key(key: &SigningKey) -> ciborium::Value {
use ciborium::Value;
let point = key.verifying_key().to_sec1_point(false);
Value::Map(vec![
(Value::from(1), Value::from(2)),
(Value::from(3), Value::from(-7)),
(Value::from(-1), Value::from(1)),
(Value::from(-2), Value::Bytes(point.x().unwrap().to_vec())),
(Value::from(-3), Value::Bytes(point.y().unwrap().to_vec())),
])
}
fn cbor(value: &ciborium::Value) -> Vec<u8> {
let mut bytes = Vec::new();
ciborium::into_writer(value, &mut bytes).unwrap();
bytes
}
fn registration_data(rp: &str, flags: u8, credential_id: &[u8], cose: &[u8]) -> Vec<u8> {
let mut data = authenticator_data(rp, flags);
data.extend_from_slice(&[0xaa; 16]);
data.extend_from_slice(&u16::try_from(credential_id.len()).unwrap().to_be_bytes());
data.extend_from_slice(credential_id);
data.extend_from_slice(cose);
data
}
fn attestation(fmt: &str, auth_data: Vec<u8>) -> Vec<u8> {
use ciborium::Value;
cbor(&Value::Map(vec![
(Value::from("fmt"), Value::from(fmt)),
(Value::from("attStmt"), Value::Map(vec![])),
(Value::from("authData"), Value::Bytes(auth_data)),
]))
}
fn registration(attestation_object: Vec<u8>, client_data_json: Vec<u8>) -> RootRegistration {
RootRegistration::WebAuthn {
attestation_object,
client_data_json,
}
}
fn good_registration(key: &SigningKey) -> RootRegistration {
registration(
attestation(
"none",
registration_data(PORTABLE_RP, 0x45, &[1; 32], &cbor(&cose_key(key))),
),
client_data(WEBAUTHN_CREATE, &HANDLE),
)
}
#[test]
fn a_webauthn_registration_gives_the_credential_s_root() {
let key = signing(3);
assert_eq!(good_registration(&key).root(&HANDLE), Ok(root(&key)));
}
#[test]
fn a_ctap_registration_gives_its_root_as_it_is() {
let key = signing(3);
assert_eq!(
RootRegistration::Ctap(root(&key)).root(&HANDLE),
Ok(root(&key))
);
}
#[test]
fn a_webauthn_registration_s_attestation_statement_is_not_checked() {
use ciborium::Value;
let key = signing(3);
let object = cbor(&Value::Map(vec![
(Value::from("fmt"), Value::from("packed")),
(
Value::from("attStmt"),
Value::Map(vec![
(Value::from("alg"), Value::from(-7)),
(Value::from("sig"), Value::Bytes(vec![0; 70])),
]),
),
(
Value::from("authData"),
Value::Bytes(registration_data(
PORTABLE_RP,
0x45,
&[1; 32],
&cbor(&cose_key(&key)),
)),
),
]));
assert_eq!(
registration(object, client_data(WEBAUTHN_CREATE, &HANDLE)).root(&HANDLE),
Ok(root(&key))
);
}
#[test]
fn a_webauthn_get_presented_as_a_registration_is_refused() {
let key = signing(3);
let RootRegistration::WebAuthn {
attestation_object, ..
} = good_registration(&key)
else {
unreachable!()
};
let made = registration(attestation_object, client_data(WEBAUTHN_GET, &HANDLE));
assert_eq!(made.root(&HANDLE), Err(WebAuthnError::WrongType));
}
#[test]
fn a_webauthn_registration_for_another_handle_is_refused() {
let key = signing(3);
assert_eq!(
good_registration(&key).root(&[0x43; HANDLE_LEN]),
Err(WebAuthnError::WrongChallenge)
);
}
#[test]
fn a_webauthn_registration_refuses_another_rp_no_up_or_uv_and_no_credential() {
let key = signing(3);
let cose = cbor(&cose_key(&key));
for (rp, flags, refusal) in [
("spoolframe.app", 0x45, WebAuthnError::WrongRp),
(PORTABLE_RP, 0x41, WebAuthnError::NotVerified),
(PORTABLE_RP, 0x44, WebAuthnError::NotVerified),
(PORTABLE_RP, 0x05, WebAuthnError::NoCredential),
] {
let data = if flags & FLAG_AT == 0 {
authenticator_data(rp, flags)
} else {
registration_data(rp, flags, &[1; 32], &cose)
};
let made = registration(
attestation("none", data),
client_data(WEBAUTHN_CREATE, &HANDLE),
);
assert_eq!(made.root(&HANDLE), Err(refusal), "{rp} {flags:#x}");
}
}
#[test]
fn a_webauthn_registration_of_a_key_that_is_not_es256_p256_is_refused() {
use ciborium::Value;
let key = signing(3);
let with = |at: usize, value: Value| {
let Value::Map(mut members) = cose_key(&key) else {
unreachable!()
};
members[at].1 = value;
Value::Map(members)
};
let off_curve = {
let Value::Map(mut members) = cose_key(&key) else {
unreachable!()
};
members[4].1 = Value::Bytes(vec![0; 32]);
Value::Map(members)
};
for cose in [
with(0, Value::from(1)), with(1, Value::from(-8)), with(2, Value::from(2)), with(3, Value::Bytes(vec![1; 31])),
off_curve,
] {
let made = registration(
attestation(
"none",
registration_data(PORTABLE_RP, 0x45, &[1; 32], &cbor(&cose)),
),
client_data(WEBAUTHN_CREATE, &HANDLE),
);
assert_eq!(
made.root(&HANDLE),
Err(WebAuthnError::UnsupportedKey),
"{cose:?}"
);
}
}
#[test]
fn a_webauthn_registration_with_an_empty_or_long_credential_id_is_refused() {
let key = signing(3);
let cose = cbor(&cose_key(&key));
for id in [vec![], vec![1; MAX_CREDENTIAL_ID + 1]] {
let made = registration(
attestation("none", registration_data(PORTABLE_RP, 0x45, &id, &cose)),
client_data(WEBAUTHN_CREATE, &HANDLE),
);
assert_eq!(made.root(&HANDLE), Err(WebAuthnError::BadCredentialId));
}
}
#[test]
fn an_oversize_webauthn_registration_is_refused_before_it_parses() {
let key = signing(3);
let made = registration(
vec![0; MAX_ATTESTATION_OBJECT + 1],
client_data(WEBAUTHN_CREATE, &HANDLE),
);
assert_eq!(made.root(&HANDLE), Err(WebAuthnError::TooLong));
let RootRegistration::WebAuthn {
attestation_object, ..
} = good_registration(&key)
else {
unreachable!()
};
let made = registration(attestation_object, vec![b' '; MAX_CLIENT_DATA_JSON + 1]);
assert_eq!(made.root(&HANDLE), Err(WebAuthnError::TooLong));
}
#[test]
fn a_malformed_attestation_object_is_refused() {
use ciborium::Value;
let key = signing(3);
let cose = cbor(&cose_key(&key));
let data = registration_data(PORTABLE_RP, 0x45, &[1; 32], &cose);
let mut trailing = attestation("none", data.clone());
trailing.push(0);
let mut nested = Value::Bytes(data.clone());
for _ in 0..1000 {
nested = Value::Array(vec![nested]);
}
for object in [
vec![],
vec![0xff],
b"not cbor".to_vec(),
trailing,
cbor(&Value::Array(vec![])),
cbor(&nested),
cbor(&Value::Map(vec![
(Value::from("fmt"), Value::from("none")),
(Value::from("attStmt"), Value::Map(vec![])),
])),
cbor(&Value::Map(vec![
(Value::from("fmt"), Value::from("none")),
(Value::from("attStmt"), Value::Map(vec![])),
(Value::from("authData"), Value::from("text")),
])),
cbor(&Value::Map(vec![
(Value::from("fmt"), Value::from(1)),
(Value::from("attStmt"), Value::Map(vec![])),
(Value::from("authData"), Value::Bytes(data.clone())),
])),
cbor(&Value::Map(vec![
(Value::from("fmt"), Value::from("none")),
(Value::from("attStmt"), Value::Map(vec![])),
(Value::from("authData"), Value::Bytes(data.clone())),
(Value::from("authData"), Value::Bytes(data.clone())),
])),
] {
let made = registration(object.clone(), client_data(WEBAUTHN_CREATE, &HANDLE));
assert_eq!(
made.root(&HANDLE),
Err(WebAuthnError::MalformedAttestation),
"{object:02x?}"
);
}
}
#[test]
fn malformed_attested_credential_data_is_refused() {
let key = signing(3);
let cose = cbor(&cose_key(&key));
let data = registration_data(PORTABLE_RP, 0x45, &[1; 32], &cose);
let mut trailing = data.clone();
trailing.push(0);
let mut cose_twice = registration_data(PORTABLE_RP, 0x45, &[1; 32], &[]);
let ciborium::Value::Map(mut members) = cose_key(&key) else {
unreachable!()
};
members.push(members[0].clone());
cose_twice.extend_from_slice(&cbor(&ciborium::Value::Map(members)));
for data in [
data[..37 + 10].to_vec(),
data[..37 + 18 + 10].to_vec(),
data[..data.len() - 1].to_vec(),
trailing,
cose_twice,
] {
let made = registration(
attestation("none", data),
client_data(WEBAUTHN_CREATE, &HANDLE),
);
assert_eq!(
made.root(&HANDLE),
Err(WebAuthnError::MalformedAuthenticatorData)
);
}
let mut extended = registration_data(PORTABLE_RP, 0xc5, &[1; 32], &cose);
extended.extend_from_slice(&cbor(&ciborium::Value::Map(vec![])));
let made = registration(
attestation("none", extended),
client_data(WEBAUTHN_CREATE, &HANDLE),
);
assert_eq!(made.root(&HANDLE), Ok(root(&key)));
}
}