use anyhow::{Context, Result};
use ciborium::value::Value;
use ring::rand::SystemRandom;
use ring::signature::{
EcdsaKeyPair, Ed25519KeyPair, KeyPair, ECDSA_P256_SHA256_ASN1_SIGNING,
ECDSA_P384_SHA384_ASN1_SIGNING,
};
use webauthn::{
AuthenticatorAssertionResponse, AuthenticatorAttestationResponse, Challenge, RelyingParty,
};
const RP_ID: &str = "localhost";
const ORIGIN: &str = "http://localhost";
const USER_ID: &[u8] = b"demo-user-001";
const RSA_PKCS8_DER: &[u8] = &[
0x30, 0x82, 0x04, 0xbc, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x04, 0xa6, 0x30, 0x82, 0x04, 0xa2, 0x02, 0x01,
0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb7, 0xe0, 0x0f, 0xc9, 0xdb, 0xfa, 0xce, 0x64, 0xa6, 0xe2,
0xb7, 0xfb, 0xa2, 0x1c, 0x09, 0x14, 0xfb, 0xd6, 0x26, 0xe5, 0x17, 0xcc, 0xf6, 0x6b, 0xf5, 0x8e,
0xbb, 0x69, 0x07, 0x50, 0xc0, 0xbb, 0x4c, 0xe7, 0x6e, 0xd8, 0xa4, 0x6a, 0x69, 0x29, 0xfc, 0xc9,
0x52, 0x0c, 0xdb, 0x04, 0xec, 0xa2, 0xef, 0x27, 0x7d, 0x8f, 0xfa, 0x9d, 0xaa, 0x10, 0x59, 0x54,
0x7b, 0x42, 0x78, 0xdb, 0xae, 0xd4, 0x24, 0x0a, 0xd4, 0x06, 0x69, 0xb0, 0xe2, 0xa5, 0x68, 0xca,
0x2d, 0x41, 0x34, 0xb0, 0x64, 0xaf, 0x61, 0x13, 0xc9, 0x32, 0xfc, 0x93, 0x56, 0x4f, 0x82, 0x7b,
0xea, 0xff, 0x20, 0xe5, 0x1c, 0x56, 0xb6, 0xe0, 0xf4, 0xaa, 0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b, 0x4b, 0xa4, 0xf2, 0x4a, 0x09, 0xb3, 0x0d, 0xe6, 0xd9, 0x55, 0xfe, 0x9c, 0x14, 0xdf,
0x2b, 0xed, 0x56, 0x60, 0x45, 0x05, 0x9e, 0x93, 0x22, 0x23, 0x90, 0x4b, 0x81, 0x02, 0x81, 0x80,
0x71, 0x49, 0x07, 0xff, 0x86, 0xc5, 0xf7, 0xe4, 0xbd, 0xa8, 0xbc, 0xa4, 0xbe, 0x1f, 0x8e, 0x73,
0xb0, 0xea, 0x71, 0x85, 0x61, 0xb1, 0x8d, 0x30, 0xe3, 0xac, 0x67, 0xfc, 0x2c, 0x5a, 0x36, 0xcc,
0x66, 0xe7, 0x2f, 0x32, 0x97, 0x54, 0x97, 0xe9, 0xd6, 0x5d, 0xd9, 0xe5, 0xbb, 0x1a, 0x06, 0x15,
0x95, 0x2d, 0x8e, 0xca, 0x27, 0x8e, 0x2e, 0x39, 0xab, 0x45, 0x2d, 0x94, 0x26, 0x93, 0xd0, 0x7b,
0xda, 0x62, 0x02, 0xba, 0xe6, 0xd9, 0x87, 0xad, 0xf7, 0x2a, 0x33, 0x1d, 0x5a, 0xd9, 0xa8, 0xf3,
0x38, 0x0e, 0x0f, 0xd1, 0x24, 0x25, 0x69, 0x2a, 0x2c, 0x99, 0xf7, 0xea, 0x0d, 0x3b, 0x51, 0x8b,
0x2a, 0x72, 0xb0, 0x51, 0xd3, 0x07, 0x63, 0x9e, 0x9d, 0x15, 0xe6, 0xa8, 0xf5, 0xce, 0x69, 0x74,
0x53, 0xd3, 0xb1, 0x26, 0x77, 0xfa, 0x0e, 0x8f, 0xdd, 0x1f, 0x0e, 0x76, 0x51, 0x56, 0x85, 0xb7,
];
fn main() -> Result<()> {
println!("🔑 Caden demo");
println!("─────────────────────────────────────");
let rng = SystemRandom::new();
let rp = RelyingParty::new(RP_ID, ORIGIN, "Caden Demo");
let pkcs8 = EcdsaKeyPair::generate_pkcs8(&ECDSA_P256_SHA256_ASN1_SIGNING, &rng)
.map_err(|e| anyhow::anyhow!("failed to generate PKCS8 keypair: {e}"))?;
let key_pair = EcdsaKeyPair::from_pkcs8(&ECDSA_P256_SHA256_ASN1_SIGNING, pkcs8.as_ref(), &rng)
.map_err(|e| anyhow::anyhow!("failed to load key pair: {e}"))?;
let public_key_bytes = key_pair.public_key().as_ref().to_vec();
let mut cred_id = vec![0u8; 16];
ring::rand::SecureRandom::fill(&rng, &mut cred_id)
.map_err(|e| anyhow::anyhow!("failed to generate credential ID: {e}"))?;
println!("\n[ES256 Registration]");
let reg_challenge = Challenge::new().context("failed to generate challenge")?;
let client_data_json_bytes =
make_client_data_json_bytes("webauthn.create", ®_challenge.bytes, ORIGIN);
let auth_data_bytes = make_authenticator_data(
RP_ID,
0x41, 0, Some((&cred_id, &encode_es256_cose_key(&public_key_bytes))),
);
let attestation_object_bytes = make_attestation_object(&auth_data_bytes);
let reg_response = AuthenticatorAttestationResponse {
client_data_json: client_data_json_bytes,
attestation_object: attestation_object_bytes,
};
let reg_result = rp
.verify_registration(®_challenge, ®_response, USER_ID)
.context("ES256 registration verification failed")?;
let cred_id_hex: String = reg_result
.credential
.id
.iter()
.map(|b| format!("{b:02x}"))
.collect();
println!(" Registration successful");
println!(" Credential ID: {cred_id_hex}");
println!(" Algorithm: ES256");
println!(" Sign count: {}", reg_result.credential.sign_count);
let mut stored_es256 = reg_result.credential;
println!("\n[ES256 Authentication]");
let auth_challenge = Challenge::new().context("failed to generate challenge")?;
let auth_response = make_es256_auth_response(
&key_pair,
&rng,
&auth_challenge.bytes,
RP_ID,
ORIGIN,
1, )?;
let auth_result = rp
.verify_authentication(&stored_es256, &auth_challenge, &auth_response)
.context("ES256 authentication failed")?;
println!(" Authentication successful");
println!(
" Sign count: {} → updated to {}",
stored_es256.sign_count, auth_result.new_sign_count
);
println!(" User present: {}", auth_result.user_present);
stored_es256.sign_count = auth_result.new_sign_count;
println!("\n[ES256 Replay Attack Prevention]");
let replay_challenge = Challenge::new().context("failed to generate challenge")?;
let replay_response = make_es256_auth_response(
&key_pair,
&rng,
&replay_challenge.bytes,
RP_ID,
ORIGIN,
1, )?;
match rp.verify_authentication(&stored_es256, &replay_challenge, &replay_response) {
Err(webauthn::WebAuthnError::SignCountInvalid { stored, received }) => {
println!(" Replay attack correctly rejected");
println!(" Error: Sign count invalid: stored {stored}, received {received}");
}
Ok(_) => panic!("BUG: ES256 replay attack should have been rejected!"),
Err(e) => return Err(e.into()),
}
println!("\n[RS256 Registration]");
let rsa_key_pair = ring::rsa::KeyPair::from_pkcs8(RSA_PKCS8_DER)
.map_err(|e| anyhow::anyhow!("failed to load RSA key: {e}"))?;
let (rsa_n, rsa_e) = extract_rsa_components(rsa_key_pair.public().as_ref());
let mut rsa_cred_id = vec![0u8; 16];
ring::rand::SecureRandom::fill(&rng, &mut rsa_cred_id)
.map_err(|e| anyhow::anyhow!("failed to generate RSA credential ID: {e}"))?;
let rsa_reg_challenge = Challenge::new().context("failed to generate RSA challenge")?;
let rsa_client_data_json =
make_client_data_json_bytes("webauthn.create", &rsa_reg_challenge.bytes, ORIGIN);
let rsa_cose_key = encode_rs256_cose_key(&rsa_n, &rsa_e);
let rsa_reg_auth_data =
make_authenticator_data(RP_ID, 0x41, 0, Some((&rsa_cred_id, &rsa_cose_key)));
let rsa_att_obj = make_attestation_object(&rsa_reg_auth_data);
let rsa_reg_response = AuthenticatorAttestationResponse {
client_data_json: rsa_client_data_json,
attestation_object: rsa_att_obj,
};
let rsa_reg_result = rp
.verify_registration(&rsa_reg_challenge, &rsa_reg_response, USER_ID)
.context("RS256 registration verification failed")?;
let rsa_cred_id_hex: String = rsa_reg_result
.credential
.id
.iter()
.map(|b| format!("{b:02x}"))
.collect();
println!(" Registration successful");
println!(" Credential ID: {rsa_cred_id_hex}");
println!(" Algorithm: RS256");
println!(" Sign count: {}", rsa_reg_result.credential.sign_count);
let mut stored_rs256 = rsa_reg_result.credential;
println!("\n[RS256 Authentication]");
let rsa_auth_challenge = Challenge::new().context("failed to generate challenge")?;
let rsa_auth_response = make_rs256_auth_response(
&rsa_key_pair,
&rng,
&rsa_auth_challenge.bytes,
RP_ID,
ORIGIN,
1,
)?;
let rsa_auth_result = rp
.verify_authentication(&stored_rs256, &rsa_auth_challenge, &rsa_auth_response)
.context("RS256 authentication failed")?;
println!(" Authentication successful");
println!(
" Sign count: {} → updated to {}",
stored_rs256.sign_count, rsa_auth_result.new_sign_count
);
println!(" User present: {}", rsa_auth_result.user_present);
stored_rs256.sign_count = rsa_auth_result.new_sign_count;
println!("\n[RS256 Replay Attack Prevention]");
let rsa_replay_challenge = Challenge::new().context("failed to generate challenge")?;
let rsa_replay_response = make_rs256_auth_response(
&rsa_key_pair,
&rng,
&rsa_replay_challenge.bytes,
RP_ID,
ORIGIN,
1, )?;
match rp.verify_authentication(&stored_rs256, &rsa_replay_challenge, &rsa_replay_response) {
Err(webauthn::WebAuthnError::SignCountInvalid { stored, received }) => {
println!(" Replay attack correctly rejected");
println!(" Error: Sign count invalid: stored {stored}, received {received}");
}
Ok(_) => panic!("BUG: RS256 replay attack should have been rejected!"),
Err(e) => return Err(e.into()),
}
println!("\n[ES384 Registration]");
let p384_pkcs8 = EcdsaKeyPair::generate_pkcs8(&ECDSA_P384_SHA384_ASN1_SIGNING, &rng)
.map_err(|e| anyhow::anyhow!("failed to generate P-384 PKCS8 keypair: {e}"))?;
let p384_key_pair =
EcdsaKeyPair::from_pkcs8(&ECDSA_P384_SHA384_ASN1_SIGNING, p384_pkcs8.as_ref(), &rng)
.map_err(|e| anyhow::anyhow!("failed to load P-384 key pair: {e}"))?;
let p384_public_key_bytes = p384_key_pair.public_key().as_ref().to_vec();
let mut p384_cred_id = vec![0u8; 16];
ring::rand::SecureRandom::fill(&rng, &mut p384_cred_id)
.map_err(|e| anyhow::anyhow!("failed to generate ES384 credential ID: {e}"))?;
let p384_reg_challenge = Challenge::new().context("failed to generate ES384 challenge")?;
let p384_client_data_json =
make_client_data_json_bytes("webauthn.create", &p384_reg_challenge.bytes, ORIGIN);
let p384_cose_key = encode_es384_cose_key(&p384_public_key_bytes);
let p384_reg_auth_data =
make_authenticator_data(RP_ID, 0x41, 0, Some((&p384_cred_id, &p384_cose_key)));
let p384_att_obj = make_attestation_object(&p384_reg_auth_data);
let p384_reg_response = AuthenticatorAttestationResponse {
client_data_json: p384_client_data_json,
attestation_object: p384_att_obj,
};
let p384_reg_result = rp
.verify_registration(&p384_reg_challenge, &p384_reg_response, USER_ID)
.context("ES384 registration verification failed")?;
let p384_cred_id_hex: String = p384_reg_result
.credential
.id
.iter()
.map(|b| format!("{b:02x}"))
.collect();
println!(" Registration successful");
println!(" Credential ID: {p384_cred_id_hex}");
println!(" Algorithm: ES384");
println!(" Sign count: {}", p384_reg_result.credential.sign_count);
let mut stored_es384 = p384_reg_result.credential;
println!("\n[ES384 Authentication]");
let p384_auth_challenge = Challenge::new().context("failed to generate challenge")?;
let p384_auth_response = make_es384_auth_response(
&p384_key_pair,
&rng,
&p384_auth_challenge.bytes,
RP_ID,
ORIGIN,
1,
)?;
let p384_auth_result = rp
.verify_authentication(&stored_es384, &p384_auth_challenge, &p384_auth_response)
.context("ES384 authentication failed")?;
println!(" Authentication successful");
println!(
" Sign count: {} → updated to {}",
stored_es384.sign_count, p384_auth_result.new_sign_count
);
println!(" User present: {}", p384_auth_result.user_present);
stored_es384.sign_count = p384_auth_result.new_sign_count;
println!("\n[ES384 Replay Attack Prevention]");
let p384_replay_challenge = Challenge::new().context("failed to generate challenge")?;
let p384_replay_response = make_es384_auth_response(
&p384_key_pair,
&rng,
&p384_replay_challenge.bytes,
RP_ID,
ORIGIN,
1, )?;
match rp.verify_authentication(&stored_es384, &p384_replay_challenge, &p384_replay_response) {
Err(webauthn::WebAuthnError::SignCountInvalid { stored, received }) => {
println!(" Replay attack correctly rejected");
println!(" Error: Sign count invalid: stored {stored}, received {received}");
}
Ok(_) => panic!("BUG: ES384 replay attack should have been rejected!"),
Err(e) => return Err(e.into()),
}
println!("\n[EdDSA Registration]");
let ed_pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng)
.map_err(|e| anyhow::anyhow!("failed to generate Ed25519 PKCS8 keypair: {e}"))?;
let ed_key_pair = Ed25519KeyPair::from_pkcs8(ed_pkcs8.as_ref())
.map_err(|e| anyhow::anyhow!("failed to load Ed25519 key pair: {e}"))?;
let ed_public_key_bytes = ed_key_pair.public_key().as_ref().to_vec();
let mut ed_cred_id = vec![0u8; 16];
ring::rand::SecureRandom::fill(&rng, &mut ed_cred_id)
.map_err(|e| anyhow::anyhow!("failed to generate EdDSA credential ID: {e}"))?;
let ed_reg_challenge = Challenge::new().context("failed to generate EdDSA challenge")?;
let ed_client_data_json =
make_client_data_json_bytes("webauthn.create", &ed_reg_challenge.bytes, ORIGIN);
let ed_cose_key = encode_eddsa_cose_key(&ed_public_key_bytes);
let ed_reg_auth_data =
make_authenticator_data(RP_ID, 0x41, 0, Some((&ed_cred_id, &ed_cose_key)));
let ed_att_obj = make_attestation_object(&ed_reg_auth_data);
let ed_reg_response = AuthenticatorAttestationResponse {
client_data_json: ed_client_data_json,
attestation_object: ed_att_obj,
};
let ed_reg_result = rp
.verify_registration(&ed_reg_challenge, &ed_reg_response, USER_ID)
.context("EdDSA registration verification failed")?;
let ed_cred_id_hex: String = ed_reg_result
.credential
.id
.iter()
.map(|b| format!("{b:02x}"))
.collect();
println!(" Registration successful");
println!(" Credential ID: {ed_cred_id_hex}");
println!(" Algorithm: EdDSA");
println!(" Sign count: {}", ed_reg_result.credential.sign_count);
let mut stored_eddsa = ed_reg_result.credential;
println!("\n[EdDSA Authentication]");
let ed_auth_challenge = Challenge::new().context("failed to generate challenge")?;
let ed_auth_response =
make_eddsa_auth_response(&ed_key_pair, &ed_auth_challenge.bytes, RP_ID, ORIGIN, 1)?;
let ed_auth_result = rp
.verify_authentication(&stored_eddsa, &ed_auth_challenge, &ed_auth_response)
.context("EdDSA authentication failed")?;
println!(" Authentication successful");
println!(
" Sign count: {} → updated to {}",
stored_eddsa.sign_count, ed_auth_result.new_sign_count
);
println!(" User present: {}", ed_auth_result.user_present);
stored_eddsa.sign_count = ed_auth_result.new_sign_count;
println!("\n[EdDSA Replay Attack Prevention]");
let ed_replay_challenge = Challenge::new().context("failed to generate challenge")?;
let ed_replay_response = make_eddsa_auth_response(
&ed_key_pair,
&ed_replay_challenge.bytes,
RP_ID,
ORIGIN,
1, )?;
match rp.verify_authentication(&stored_eddsa, &ed_replay_challenge, &ed_replay_response) {
Err(webauthn::WebAuthnError::SignCountInvalid { stored, received }) => {
println!(" Replay attack correctly rejected");
println!(" Error: Sign count invalid: stored {stored}, received {received}");
}
Ok(_) => panic!("BUG: EdDSA replay attack should have been rejected!"),
Err(e) => return Err(e.into()),
}
println!("\n─────────────────────────────────────");
println!("All checks passed.");
Ok(())
}
fn make_client_data_json_bytes(type_: &str, challenge: &[u8], origin: &str) -> Vec<u8> {
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
let challenge_b64 = URL_SAFE_NO_PAD.encode(challenge);
format!(
r#"{{"type":"{type_}","challenge":"{challenge_b64}","origin":"{origin}","crossOrigin":false}}"#
)
.into_bytes()
}
fn make_authenticator_data(
rp_id: &str,
flags: u8,
sign_count: u32,
cred_data: Option<(&[u8], &[u8])>, ) -> Vec<u8> {
let rp_id_hash = webauthn::crypto::sha256(rp_id.as_bytes());
let mut out = Vec::new();
out.extend_from_slice(&rp_id_hash);
out.push(flags);
out.extend_from_slice(&sign_count.to_be_bytes());
if let Some((cred_id, cose_key_cbor)) = cred_data {
out.extend_from_slice(&[0u8; 16]); let id_len = cred_id.len() as u16;
out.extend_from_slice(&id_len.to_be_bytes());
out.extend_from_slice(cred_id);
out.extend_from_slice(cose_key_cbor);
}
out
}
fn encode_es256_cose_key(uncompressed_point: &[u8]) -> Vec<u8> {
assert_eq!(
uncompressed_point.len(),
65,
"expected 0x04 || x(32) || y(32)"
);
let x = uncompressed_point[1..33].to_vec();
let y = uncompressed_point[33..65].to_vec();
let cose_key = Value::Map(vec![
(Value::Integer(1i64.into()), Value::Integer(2i64.into())), (Value::Integer(3i64.into()), Value::Integer((-7i64).into())), (Value::Integer((-1i64).into()), Value::Integer(1i64.into())), (Value::Integer((-2i64).into()), Value::Bytes(x)), (Value::Integer((-3i64).into()), Value::Bytes(y)), ]);
let mut buf = Vec::new();
ciborium::into_writer(&cose_key, &mut buf).expect("CBOR encoding should not fail");
buf
}
fn encode_rs256_cose_key(n: &[u8], e: &[u8]) -> Vec<u8> {
let cose_key = Value::Map(vec![
(Value::Integer(1i64.into()), Value::Integer(3i64.into())), (
Value::Integer(3i64.into()),
Value::Integer((-257i64).into()),
), (Value::Integer((-1i64).into()), Value::Bytes(n.to_vec())), (Value::Integer((-2i64).into()), Value::Bytes(e.to_vec())), ]);
let mut buf = Vec::new();
ciborium::into_writer(&cose_key, &mut buf).expect("CBOR encoding should not fail");
buf
}
fn extract_rsa_components(der: &[u8]) -> (Vec<u8>, Vec<u8>) {
let mut pos = 0;
assert_eq!(der[pos], 0x30);
pos += 1;
if der[pos] < 0x80 {
pos += 1;
} else {
let extra = (der[pos] & 0x7f) as usize;
pos += 1 + extra;
}
assert_eq!(der[pos], 0x02);
pos += 1;
let n_len = if der[pos] < 0x80 {
let l = der[pos] as usize;
pos += 1;
l
} else {
let extra = (der[pos] & 0x7f) as usize;
pos += 1;
let mut l = 0usize;
for _ in 0..extra {
l = (l << 8) | der[pos] as usize;
pos += 1;
}
l
};
let n_start = if der[pos] == 0x00 { pos + 1 } else { pos };
let n = der[n_start..pos + n_len].to_vec();
pos += n_len;
assert_eq!(der[pos], 0x02);
pos += 1;
let e_len = der[pos] as usize;
pos += 1;
let e = der[pos..pos + e_len].to_vec();
(n, e)
}
fn encode_es384_cose_key(uncompressed_point: &[u8]) -> Vec<u8> {
assert_eq!(
uncompressed_point.len(),
97,
"expected 0x04 || x(48) || y(48)"
);
let x = uncompressed_point[1..49].to_vec();
let y = uncompressed_point[49..97].to_vec();
let cose_key = Value::Map(vec![
(Value::Integer(1i64.into()), Value::Integer(2i64.into())), (Value::Integer(3i64.into()), Value::Integer((-35i64).into())), (Value::Integer((-1i64).into()), Value::Integer(2i64.into())), (Value::Integer((-2i64).into()), Value::Bytes(x)), (Value::Integer((-3i64).into()), Value::Bytes(y)), ]);
let mut buf = Vec::new();
ciborium::into_writer(&cose_key, &mut buf).expect("CBOR encoding should not fail");
buf
}
fn make_attestation_object(auth_data: &[u8]) -> Vec<u8> {
let att_obj = Value::Map(vec![
(
Value::Text("fmt".to_string()),
Value::Text("none".to_string()),
),
(Value::Text("attStmt".to_string()), Value::Map(vec![])),
(
Value::Text("authData".to_string()),
Value::Bytes(auth_data.to_vec()),
),
]);
let mut buf = Vec::new();
ciborium::into_writer(&att_obj, &mut buf).expect("CBOR encoding should not fail");
buf
}
fn make_es256_auth_response(
key_pair: &EcdsaKeyPair,
rng: &SystemRandom,
challenge: &[u8],
rp_id: &str,
origin: &str,
sign_count: u32,
) -> Result<AuthenticatorAssertionResponse> {
let client_data_bytes = make_client_data_json_bytes("webauthn.get", challenge, origin);
let auth_data_bytes = make_authenticator_data(rp_id, 0x01, sign_count, None);
let client_data_hash = webauthn::crypto::sha256(&client_data_bytes);
let mut signed_data = auth_data_bytes.clone();
signed_data.extend_from_slice(&client_data_hash);
let sig = key_pair
.sign(rng, &signed_data)
.map_err(|e| anyhow::anyhow!("ECDSA signing failed: {e}"))?;
Ok(AuthenticatorAssertionResponse {
client_data_json: client_data_bytes,
authenticator_data: auth_data_bytes,
signature: sig.as_ref().to_vec(),
user_handle: None,
})
}
fn make_es384_auth_response(
key_pair: &EcdsaKeyPair,
rng: &ring::rand::SystemRandom,
challenge: &[u8],
rp_id: &str,
origin: &str,
sign_count: u32,
) -> Result<AuthenticatorAssertionResponse> {
let client_data_bytes = make_client_data_json_bytes("webauthn.get", challenge, origin);
let auth_data_bytes = make_authenticator_data(rp_id, 0x01, sign_count, None);
let client_data_hash = webauthn::crypto::sha256(&client_data_bytes);
let mut signed_data = auth_data_bytes.clone();
signed_data.extend_from_slice(&client_data_hash);
let sig = key_pair
.sign(rng, &signed_data)
.map_err(|e| anyhow::anyhow!("ECDSA P-384 signing failed: {e}"))?;
Ok(AuthenticatorAssertionResponse {
client_data_json: client_data_bytes,
authenticator_data: auth_data_bytes,
signature: sig.as_ref().to_vec(),
user_handle: None,
})
}
fn make_rs256_auth_response(
key_pair: &ring::rsa::KeyPair,
rng: &SystemRandom,
challenge: &[u8],
rp_id: &str,
origin: &str,
sign_count: u32,
) -> Result<AuthenticatorAssertionResponse> {
use ring::signature::RSA_PKCS1_SHA256;
let client_data_bytes = make_client_data_json_bytes("webauthn.get", challenge, origin);
let auth_data_bytes = make_authenticator_data(rp_id, 0x01, sign_count, None);
let client_data_hash = webauthn::crypto::sha256(&client_data_bytes);
let mut signed_data = auth_data_bytes.clone();
signed_data.extend_from_slice(&client_data_hash);
let mut sig = vec![0u8; key_pair.public().modulus_len()];
key_pair
.sign(&RSA_PKCS1_SHA256, rng, &signed_data, &mut sig)
.map_err(|e| anyhow::anyhow!("RSA signing failed: {e}"))?;
Ok(AuthenticatorAssertionResponse {
client_data_json: client_data_bytes,
authenticator_data: auth_data_bytes,
signature: sig,
user_handle: None,
})
}
fn encode_eddsa_cose_key(public_key: &[u8]) -> Vec<u8> {
assert_eq!(public_key.len(), 32, "expected 32-byte Ed25519 public key");
let cose_key = Value::Map(vec![
(Value::Integer(1i64.into()), Value::Integer(1i64.into())), (Value::Integer(3i64.into()), Value::Integer((-8i64).into())), (Value::Integer((-1i64).into()), Value::Integer(6i64.into())), (
Value::Integer((-2i64).into()),
Value::Bytes(public_key.to_vec()),
), ]);
let mut buf = Vec::new();
ciborium::into_writer(&cose_key, &mut buf).expect("CBOR encoding should not fail");
buf
}
fn make_eddsa_auth_response(
key_pair: &Ed25519KeyPair,
challenge: &[u8],
rp_id: &str,
origin: &str,
sign_count: u32,
) -> Result<AuthenticatorAssertionResponse> {
let client_data_bytes = make_client_data_json_bytes("webauthn.get", challenge, origin);
let auth_data_bytes = make_authenticator_data(rp_id, 0x01, sign_count, None);
let client_data_hash = webauthn::crypto::sha256(&client_data_bytes);
let mut signed_data = auth_data_bytes.clone();
signed_data.extend_from_slice(&client_data_hash);
let sig = key_pair.sign(&signed_data);
Ok(AuthenticatorAssertionResponse {
client_data_json: client_data_bytes,
authenticator_data: auth_data_bytes,
signature: sig.as_ref().to_vec(),
user_handle: None,
})
}