| 1 |
use super::commands::get_assertion::HmacSecretExtension; |
| 2 |
use crate::crypto::{COSEAlgorithm, CryptoError, PinUvAuthToken, SharedSecret}; |
| 3 |
use crate::{errors::AuthenticatorError, AuthenticatorTransports, KeyHandle}; |
| 4 |
use base64::Engine; |
| 5 |
use serde::de::MapAccess; |
| 6 |
use serde::{ |
| 7 |
de::{Error as SerdeError, Unexpected, Visitor}, |
| 8 |
Deserialize, Deserializer, Serialize, Serializer, |
| 9 |
}; |
| 10 |
use serde_bytes::{ByteBuf, Bytes}; |
| 11 |
use sha2::{Digest, Sha256}; |
| 12 |
use std::collections::HashMap; |
| 13 |
use std::convert::{Into, TryFrom}; |
| 14 |
use std::fmt; |
| 15 |
|
| 16 |
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)] |
| 17 |
pub struct RpIdHash(pub [u8; 32]); |
| 18 |
|
| 19 |
impl fmt::Debug for RpIdHash { |
| 20 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 21 |
let value = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(self.0); |
| 22 |
write!(f, "RpIdHash({value})") |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
impl AsRef<[u8]> for RpIdHash { |
| 27 |
fn as_ref(&self) -> &[u8] { |
| 28 |
self.0.as_ref() |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
impl RpIdHash { |
| 33 |
pub fn from(src: &[u8]) -> Result<RpIdHash, AuthenticatorError> { |
| 34 |
let mut payload = [0u8; 32]; |
| 35 |
if src.len() != payload.len() { |
| 36 |
Err(AuthenticatorError::InvalidRelyingPartyInput) |
| 37 |
} else { |
| 38 |
payload.copy_from_slice(src); |
| 39 |
Ok(RpIdHash(payload)) |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// NOTE: WebAuthn requires all fields and CTAP2 does not. |
| 45 |
#[derive(Debug, Serialize, Clone, Default, Deserialize, PartialEq, Eq)] |
| 46 |
pub struct RelyingParty { |
| 47 |
pub id: String, |
| 48 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 49 |
pub name: Option<String>, |
| 50 |
} |
| 51 |
|
| 52 |
impl RelyingParty { |
| 53 |
pub fn from<S>(id: S) -> Self |
| 54 |
where |
| 55 |
S: Into<String>, |
| 56 |
{ |
| 57 |
Self { |
| 58 |
id: id.into(), |
| 59 |
name: None, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
pub fn hash(&self) -> RpIdHash { |
| 64 |
RpIdHash(Sha256::digest(&self.id).into()) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// NOTE: WebAuthn requires all fields and CTAP2 does not. |
| 69 |
#[derive(Debug, Serialize, Clone, Eq, PartialEq, Deserialize, Default)] |
| 70 |
pub struct PublicKeyCredentialUserEntity { |
| 71 |
#[serde(with = "serde_bytes")] |
| 72 |
pub id: Vec<u8>, |
| 73 |
pub name: Option<String>, |
| 74 |
#[serde(skip_serializing_if = "Option::is_none", rename = "displayName")] |
| 75 |
pub display_name: Option<String>, |
| 76 |
} |
| 77 |
|
| 78 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 79 |
pub struct PublicKeyCredentialParameters { |
| 80 |
pub alg: COSEAlgorithm, |
| 81 |
} |
| 82 |
|
| 83 |
impl TryFrom<i32> for PublicKeyCredentialParameters { |
| 84 |
type Error = AuthenticatorError; |
| 85 |
fn try_from(arg: i32) -> Result<Self, Self::Error> { |
| 86 |
let alg = COSEAlgorithm::try_from(arg as i64)?; |
| 87 |
Ok(PublicKeyCredentialParameters { alg }) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
impl Serialize for PublicKeyCredentialParameters { |
| 92 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 93 |
where |
| 94 |
S: Serializer, |
| 95 |
{ |
| 96 |
serialize_map!( |
| 97 |
serializer, |
| 98 |
"alg" => &self.alg, |
| 99 |
"type" => "public-key", |
| 100 |
) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl<'de> Deserialize<'de> for PublicKeyCredentialParameters { |
| 105 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 106 |
where |
| 107 |
D: Deserializer<'de>, |
| 108 |
{ |
| 109 |
struct PublicKeyCredentialParametersVisitor; |
| 110 |
|
| 111 |
impl<'de> Visitor<'de> for PublicKeyCredentialParametersVisitor { |
| 112 |
type Value = PublicKeyCredentialParameters; |
| 113 |
|
| 114 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 115 |
formatter.write_str("a map") |
| 116 |
} |
| 117 |
|
| 118 |
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
| 119 |
where |
| 120 |
M: MapAccess<'de>, |
| 121 |
{ |
| 122 |
let mut found_type = false; |
| 123 |
let mut alg = None; |
| 124 |
while let Some(key) = map.next_key()? { |
| 125 |
match key { |
| 126 |
"alg" => { |
| 127 |
if alg.is_some() { |
| 128 |
return Err(SerdeError::duplicate_field("alg")); |
| 129 |
} |
| 130 |
alg = Some(map.next_value()?); |
| 131 |
} |
| 132 |
"type" => { |
| 133 |
if found_type { |
| 134 |
return Err(SerdeError::duplicate_field("type")); |
| 135 |
} |
| 136 |
|
| 137 |
let v: &str = map.next_value()?; |
| 138 |
if v != "public-key" { |
| 139 |
return Err(SerdeError::custom(format!("invalid value: {v}"))); |
| 140 |
} |
| 141 |
found_type = true; |
| 142 |
} |
| 143 |
v => { |
| 144 |
return Err(SerdeError::unknown_field(v, &[])); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if !found_type { |
| 150 |
return Err(SerdeError::missing_field("type")); |
| 151 |
} |
| 152 |
|
| 153 |
let alg = alg.ok_or_else(|| SerdeError::missing_field("alg"))?; |
| 154 |
|
| 155 |
Ok(PublicKeyCredentialParameters { alg }) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
deserializer.deserialize_bytes(PublicKeyCredentialParametersVisitor) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
#[derive(Debug, PartialEq, Serialize, Deserialize, Eq, Clone)] |
| 164 |
#[serde(rename_all = "lowercase")] |
| 165 |
pub enum Transport { |
| 166 |
USB, |
| 167 |
NFC, |
| 168 |
BLE, |
| 169 |
Internal, |
| 170 |
} |
| 171 |
|
| 172 |
impl From<AuthenticatorTransports> for Vec<Transport> { |
| 173 |
fn from(t: AuthenticatorTransports) -> Self { |
| 174 |
let mut transports = Vec::new(); |
| 175 |
if t.contains(AuthenticatorTransports::USB) { |
| 176 |
transports.push(Transport::USB); |
| 177 |
} |
| 178 |
if t.contains(AuthenticatorTransports::NFC) { |
| 179 |
transports.push(Transport::NFC); |
| 180 |
} |
| 181 |
if t.contains(AuthenticatorTransports::BLE) { |
| 182 |
transports.push(Transport::BLE); |
| 183 |
} |
| 184 |
|
| 185 |
transports |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
pub type PublicKeyCredentialId = Vec<u8>; |
| 190 |
|
| 191 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 192 |
pub struct PublicKeyCredentialDescriptor { |
| 193 |
pub id: PublicKeyCredentialId, |
| 194 |
pub transports: Vec<Transport>, |
| 195 |
} |
| 196 |
|
| 197 |
impl Serialize for PublicKeyCredentialDescriptor { |
| 198 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 199 |
where |
| 200 |
S: Serializer, |
| 201 |
{ |
| 202 |
serialize_map!( |
| 203 |
serializer, |
| 204 |
"id" => Bytes::new(&self.id), |
| 205 |
"type" => "public-key", |
| 206 |
|
| 207 |
// TODO(MS): Transports is OPTIONAL, but some older tokens don't understand it |
| 208 |
// and return a CBOR-Parsing error. It is only a hint for the token, |
| 209 |
// so we'll leave it out for the moment |
| 210 |
// "transports" => &self.transports, |
| 211 |
) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
impl<'de> Deserialize<'de> for PublicKeyCredentialDescriptor { |
| 216 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 217 |
where |
| 218 |
D: Deserializer<'de>, |
| 219 |
{ |
| 220 |
struct PublicKeyCredentialDescriptorVisitor; |
| 221 |
|
| 222 |
impl<'de> Visitor<'de> for PublicKeyCredentialDescriptorVisitor { |
| 223 |
type Value = PublicKeyCredentialDescriptor; |
| 224 |
|
| 225 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 226 |
formatter.write_str("a map") |
| 227 |
} |
| 228 |
|
| 229 |
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
| 230 |
where |
| 231 |
M: MapAccess<'de>, |
| 232 |
{ |
| 233 |
let mut found_type = false; |
| 234 |
let mut id = None; |
| 235 |
let mut transports = None; |
| 236 |
while let Some(key) = map.next_key()? { |
| 237 |
match key { |
| 238 |
"id" => { |
| 239 |
if id.is_some() { |
| 240 |
return Err(SerdeError::duplicate_field("id")); |
| 241 |
} |
| 242 |
let id_bytes: ByteBuf = map.next_value()?; |
| 243 |
id = Some(id_bytes.into_vec()); |
| 244 |
} |
| 245 |
"transports" => { |
| 246 |
if transports.is_some() { |
| 247 |
return Err(SerdeError::duplicate_field("transports")); |
| 248 |
} |
| 249 |
transports = Some(map.next_value()?); |
| 250 |
} |
| 251 |
"type" => { |
| 252 |
if found_type { |
| 253 |
return Err(SerdeError::duplicate_field("type")); |
| 254 |
} |
| 255 |
let v: &str = map.next_value()?; |
| 256 |
if v != "public-key" { |
| 257 |
return Err(SerdeError::custom(format!("invalid value: {v}"))); |
| 258 |
} |
| 259 |
found_type = true; |
| 260 |
} |
| 261 |
v => { |
| 262 |
return Err(SerdeError::unknown_field(v, &[])); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if !found_type { |
| 268 |
return Err(SerdeError::missing_field("type")); |
| 269 |
} |
| 270 |
|
| 271 |
let id = id.ok_or_else(|| SerdeError::missing_field("id"))?; |
| 272 |
let transports = transports.unwrap_or_default(); |
| 273 |
|
| 274 |
Ok(PublicKeyCredentialDescriptor { id, transports }) |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
deserializer.deserialize_any(PublicKeyCredentialDescriptorVisitor) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
impl From<&KeyHandle> for PublicKeyCredentialDescriptor { |
| 283 |
fn from(kh: &KeyHandle) -> Self { |
| 284 |
Self { |
| 285 |
id: kh.credential.clone(), |
| 286 |
transports: kh.transports.into(), |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 292 |
pub enum ResidentKeyRequirement { |
| 293 |
Discouraged, |
| 294 |
Preferred, |
| 295 |
Required, |
| 296 |
} |
| 297 |
|
| 298 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 299 |
pub enum UserVerificationRequirement { |
| 300 |
Discouraged, |
| 301 |
Preferred, |
| 302 |
Required, |
| 303 |
} |
| 304 |
|
| 305 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 306 |
pub enum CredentialProtectionPolicy { |
| 307 |
UserVerificationOptional = 1, |
| 308 |
UserVerificationOptionalWithCredentialIDList = 2, |
| 309 |
UserVerificationRequired = 3, |
| 310 |
} |
| 311 |
|
| 312 |
impl Serialize for CredentialProtectionPolicy { |
| 313 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 314 |
where |
| 315 |
S: Serializer, |
| 316 |
{ |
| 317 |
serializer.serialize_u64(*self as u64) |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
impl<'de> Deserialize<'de> for CredentialProtectionPolicy { |
| 322 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 323 |
where |
| 324 |
D: Deserializer<'de>, |
| 325 |
{ |
| 326 |
struct CredentialProtectionPolicyVisitor; |
| 327 |
|
| 328 |
impl<'de> Visitor<'de> for CredentialProtectionPolicyVisitor { |
| 329 |
type Value = CredentialProtectionPolicy; |
| 330 |
|
| 331 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 332 |
formatter.write_str("an integer") |
| 333 |
} |
| 334 |
|
| 335 |
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> |
| 336 |
where |
| 337 |
E: SerdeError, |
| 338 |
{ |
| 339 |
match v { |
| 340 |
1 => Ok(CredentialProtectionPolicy::UserVerificationOptional), |
| 341 |
2 => Ok( |
| 342 |
CredentialProtectionPolicy::UserVerificationOptionalWithCredentialIDList, |
| 343 |
), |
| 344 |
3 => Ok(CredentialProtectionPolicy::UserVerificationRequired), |
| 345 |
_ => Err(SerdeError::invalid_value( |
| 346 |
Unexpected::Unsigned(v), |
| 347 |
&"valid CredentialProtectionPolicy", |
| 348 |
)), |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
deserializer.deserialize_any(CredentialProtectionPolicyVisitor) |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
#[derive(Clone, Debug, Default)] |
| 358 |
pub struct AuthenticationExtensionsClientInputs { |
| 359 |
pub app_id: Option<String>, |
| 360 |
pub cred_props: Option<bool>, |
| 361 |
pub credential_protection_policy: Option<CredentialProtectionPolicy>, |
| 362 |
pub enforce_credential_protection_policy: Option<bool>, |
| 363 |
pub hmac_create_secret: Option<bool>, |
| 364 |
pub hmac_get_secret: Option<HMACGetSecretInput>, |
| 365 |
pub min_pin_length: Option<bool>, |
| 366 |
pub prf: Option<AuthenticationExtensionsPRFInputs>, |
| 367 |
} |
| 368 |
|
| 369 |
#[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 370 |
pub struct CredentialProperties { |
| 371 |
pub rk: bool, |
| 372 |
} |
| 373 |
|
| 374 |
/// Salt inputs for the `hmac-secret` extension. |
| 375 |
/// https://fidoalliance.org/specs/fido-v2.1-ps-20210615/fido-client-to-authenticator-protocol-v2.1-ps-20210615.html#dictdef-hmacgetsecretinput |
| 376 |
#[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 377 |
pub struct HMACGetSecretInput { |
| 378 |
pub salt1: [u8; 32], |
| 379 |
pub salt2: Option<[u8; 32]>, |
| 380 |
} |
| 381 |
|
| 382 |
/// Decrypted HMAC outputs from the `hmac-secret` extension. |
| 383 |
/// https://fidoalliance.org/specs/fido-v2.1-ps-20210615/fido-client-to-authenticator-protocol-v2.1-ps-20210615.html#dictdef-hmacgetsecretoutput |
| 384 |
#[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 385 |
pub struct HMACGetSecretOutput { |
| 386 |
pub output1: [u8; 32], |
| 387 |
pub output2: Option<[u8; 32]>, |
| 388 |
} |
| 389 |
|
| 390 |
#[derive(Clone, Debug, Default, PartialEq)] |
| 391 |
pub struct AuthenticationExtensionsPRFInputs { |
| 392 |
pub eval: Option<AuthenticationExtensionsPRFValues>, |
| 393 |
pub eval_by_credential: Option<HashMap<Vec<u8>, AuthenticationExtensionsPRFValues>>, |
| 394 |
} |
| 395 |
|
| 396 |
impl AuthenticationExtensionsPRFInputs { |
| 397 |
/// Select an `eval` or `evalByCredential` entry and calculate hmac-secret salt inputs from those inputs. |
| 398 |
/// |
| 399 |
/// Returns [None] if the `eval` input was not given and no credential in `allow_credentials` matched any `evalByCredential` entry. |
| 400 |
/// Otherwise returns the initialized [HmacSecretExtension] and, if an `evalByCredential` entry was used to compute the salt inputs, |
| 401 |
/// the [PublicKeyCredentialDescriptor] matching that `evalByCredential` entry. |
| 402 |
/// If present, `allowCredentials` SHOULD be set to contain only that [PublicKeyCredentialDescriptor] value. |
| 403 |
pub fn calculate<'allow_cred>( |
| 404 |
&self, |
| 405 |
secret: &SharedSecret, |
| 406 |
allow_credentials: &'allow_cred [PublicKeyCredentialDescriptor], |
| 407 |
puat: Option<&PinUvAuthToken>, |
| 408 |
) -> Result< |
| 409 |
Option<( |
| 410 |
HmacSecretExtension, |
| 411 |
Option<&'allow_cred PublicKeyCredentialDescriptor>, |
| 412 |
)>, |
| 413 |
CryptoError, |
| 414 |
> { |
| 415 |
if let Some((selected_credential, ev)) = self.select_eval(allow_credentials) { |
| 416 |
let mut hmac_secret = HmacSecretExtension::new( |
| 417 |
Self::eval_to_salt(&ev.first).to_vec(), |
| 418 |
ev.second |
| 419 |
.as_ref() |
| 420 |
.map(|second| Self::eval_to_salt(second).to_vec()), |
| 421 |
); |
| 422 |
hmac_secret.calculate(secret, puat)?; |
| 423 |
Ok(Some((hmac_secret, selected_credential))) |
| 424 |
} else { |
| 425 |
Ok(None) |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
/// Select an `evalByCredential` entry matching any element of `allow_credentials`, |
| 430 |
/// or otherwise fall back to `eval`, if present, if no match is found. |
| 431 |
fn select_eval<'allow_cred>( |
| 432 |
&self, |
| 433 |
allow_credentials: &'allow_cred [PublicKeyCredentialDescriptor], |
| 434 |
) -> Option<( |
| 435 |
Option<&'allow_cred PublicKeyCredentialDescriptor>, |
| 436 |
&AuthenticationExtensionsPRFValues, |
| 437 |
)> { |
| 438 |
self.select_credential(allow_credentials) |
| 439 |
.map(|(cred, ev)| (Some(cred), ev)) |
| 440 |
.or(self.eval.as_ref().map(|eval| (None, eval))) |
| 441 |
} |
| 442 |
|
| 443 |
/// Select an `evalByCredential` entry matching any element of `allow_credentials`. |
| 444 |
fn select_credential<'allow_cred>( |
| 445 |
&self, |
| 446 |
allow_credentials: &'allow_cred [PublicKeyCredentialDescriptor], |
| 447 |
) -> Option<( |
| 448 |
&'allow_cred PublicKeyCredentialDescriptor, |
| 449 |
&AuthenticationExtensionsPRFValues, |
| 450 |
)> { |
| 451 |
self.eval_by_credential |
| 452 |
.as_ref() |
| 453 |
.and_then(|eval_by_credential| { |
| 454 |
allow_credentials |
| 455 |
.iter() |
| 456 |
.find_map(|pkcd| eval_by_credential.get(&pkcd.id).map(|eval| (pkcd, eval))) |
| 457 |
}) |
| 458 |
} |
| 459 |
|
| 460 |
/// Convert a PRF eval input to an hmac-secret salt input. |
| 461 |
fn eval_to_salt(eval: &[u8]) -> [u8; 32] { |
| 462 |
Sha256::new_with_prefix(b"WebAuthn PRF") |
| 463 |
.chain_update([0x00].iter()) |
| 464 |
.chain_update(eval.iter()) |
| 465 |
.finalize() |
| 466 |
.into() |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
#[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 471 |
pub struct AuthenticationExtensionsPRFValues { |
| 472 |
pub first: Vec<u8>, |
| 473 |
pub second: Option<Vec<u8>>, |
| 474 |
} |
| 475 |
|
| 476 |
impl From<HMACGetSecretOutput> for AuthenticationExtensionsPRFValues { |
| 477 |
fn from(hmac_output: HMACGetSecretOutput) -> Self { |
| 478 |
Self { |
| 479 |
first: hmac_output.output1.to_vec(), |
| 480 |
second: hmac_output.output2.map(|o2| o2.to_vec()), |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
#[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 486 |
pub struct AuthenticationExtensionsPRFOutputs { |
| 487 |
pub enabled: Option<bool>, |
| 488 |
pub results: Option<AuthenticationExtensionsPRFValues>, |
| 489 |
} |
| 490 |
|
| 491 |
#[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 492 |
pub struct AuthenticationExtensionsClientOutputs { |
| 493 |
pub app_id: Option<bool>, |
| 494 |
pub cred_props: Option<CredentialProperties>, |
| 495 |
pub hmac_create_secret: Option<bool>, |
| 496 |
pub hmac_get_secret: Option<HMACGetSecretOutput>, |
| 497 |
pub prf: Option<AuthenticationExtensionsPRFOutputs>, |
| 498 |
} |
| 499 |
|
| 500 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 501 |
pub enum AuthenticatorAttachment { |
| 502 |
CrossPlatform, |
| 503 |
Platform, |
| 504 |
Unknown, |
| 505 |
} |
| 506 |
|
| 507 |
#[cfg(test)] |
| 508 |
mod test { |
| 509 |
use super::{ |
| 510 |
COSEAlgorithm, PublicKeyCredentialDescriptor, PublicKeyCredentialParameters, |
| 511 |
PublicKeyCredentialUserEntity, RelyingParty, Transport, |
| 512 |
}; |
| 513 |
use serde_cbor::from_slice; |
| 514 |
|
| 515 |
fn create_user() -> PublicKeyCredentialUserEntity { |
| 516 |
PublicKeyCredentialUserEntity { |
| 517 |
id: vec![ |
| 518 |
0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x30, |
| 519 |
0x82, 0x01, 0x93, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x30, 0x82, |
| 520 |
0x01, 0x93, 0x30, 0x82, |
| 521 |
], |
| 522 |
name: Some(String::from("[email protected]")), |
| 523 |
display_name: Some(String::from("John P. Smith")), |
| 524 |
} |
| 525 |
} |
| 526 |
#[test] |
| 527 |
fn serialize_rp() { |
| 528 |
let rp = RelyingParty { |
| 529 |
id: String::from("Acme"), |
| 530 |
name: None, |
| 531 |
}; |
| 532 |
|
| 533 |
let payload = ser::to_vec(&rp).unwrap(); |
| 534 |
assert_eq!( |
| 535 |
&payload, |
| 536 |
&[ |
| 537 |
0xa1, // map(1) |
| 538 |
0x62, // text(2) |
| 539 |
0x69, 0x64, // "id" |
| 540 |
0x64, // text(4) |
| 541 |
0x41, 0x63, 0x6d, 0x65 |
| 542 |
] |
| 543 |
); |
| 544 |
} |
| 545 |
|
| 546 |
#[test] |
| 547 |
fn test_deserialize_user() { |
| 548 |
// This includes an obsolete "icon" field to test that deserialization |
| 549 |
// ignores it. |
| 550 |
let input = vec![ |
| 551 |
0xa4, // map(4) |
| 552 |
0x62, // text(2) |
| 553 |
0x69, 0x64, // "id" |
| 554 |
0x58, 0x20, // bytes(32) |
| 555 |
0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03, // userid |
| 556 |
0x02, 0x01, 0x02, 0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, // ... |
| 557 |
0x38, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x30, 0x82, 0x01, 0x93, // ... |
| 558 |
0x30, 0x82, // ... |
| 559 |
0x64, // text(4) |
| 560 |
0x69, 0x63, 0x6f, 0x6e, // "icon" |
| 561 |
0x78, 0x2b, // text(43) |
| 562 |
0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x70, |
| 563 |
0x69, // "https://pics.example.com/00/p/aBjjjpqPb.png" |
| 564 |
0x63, 0x73, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, // ... |
| 565 |
0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x30, 0x30, 0x2f, 0x70, 0x2f, // ... |
| 566 |
0x61, 0x42, 0x6a, 0x6a, 0x6a, 0x70, 0x71, 0x50, 0x62, 0x2e, // ... |
| 567 |
0x70, 0x6e, 0x67, // ... |
| 568 |
0x64, // text(4) |
| 569 |
0x6e, 0x61, 0x6d, 0x65, // "name" |
| 570 |
0x76, // text(22) |
| 571 |
0x6a, 0x6f, 0x68, 0x6e, 0x70, 0x73, 0x6d, 0x69, 0x74, |
| 572 |
0x68, // "[email protected]" |
| 573 |
0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, // ... |
| 574 |
0x6f, 0x6d, // ... |
| 575 |
0x6b, // text(11) |
| 576 |
0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x4e, 0x61, 0x6d, // "displayName" |
| 577 |
0x65, // ... |
| 578 |
0x6d, // text(13) |
| 579 |
0x4a, 0x6f, 0x68, 0x6e, 0x20, 0x50, 0x2e, 0x20, 0x53, 0x6d, // "John P. Smith" |
| 580 |
0x69, 0x74, 0x68, // ... |
| 581 |
]; |
| 582 |
let expected = create_user(); |
| 583 |
let actual: PublicKeyCredentialUserEntity = from_slice(&input).unwrap(); |
| 584 |
assert_eq!(expected, actual); |
| 585 |
} |
| 586 |
|
| 587 |
#[test] |
| 588 |
fn serialize_user() { |
| 589 |
let user = create_user(); |
| 590 |
|
| 591 |
let payload = ser::to_vec(&user).unwrap(); |
| 592 |
println!("payload = {payload:?}"); |
| 593 |
assert_eq!( |
| 594 |
payload, |
| 595 |
vec![ |
| 596 |
0xa3, // map(3) |
| 597 |
0x62, // text(2) |
| 598 |
0x69, 0x64, // "id" |
| 599 |
0x58, 0x20, // bytes(32) |
| 600 |
0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03, // userid |
| 601 |
0x02, 0x01, 0x02, 0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, // ... |
| 602 |
0x38, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x30, 0x82, 0x01, 0x93, // ... |
| 603 |
0x30, 0x82, // ... |
| 604 |
0x64, // text(4) |
| 605 |
0x6e, 0x61, 0x6d, 0x65, // "name" |
| 606 |
0x76, // text(22) |
| 607 |
0x6a, 0x6f, 0x68, 0x6e, 0x70, 0x73, 0x6d, 0x69, 0x74, |
| 608 |
0x68, // "[email protected]" |
| 609 |
0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, // ... |
| 610 |
0x6f, 0x6d, // ... |
| 611 |
0x6b, // text(11) |
| 612 |
0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x4e, 0x61, 0x6d, // "displayName" |
| 613 |
0x65, // ... |
| 614 |
0x6d, // text(13) |
| 615 |
0x4a, 0x6f, 0x68, 0x6e, 0x20, 0x50, 0x2e, 0x20, 0x53, 0x6d, // "John P. Smith" |
| 616 |
0x69, 0x74, 0x68, // ... |
| 617 |
] |
| 618 |
); |
| 619 |
} |
| 620 |
|
| 621 |
#[test] |
| 622 |
fn serialize_user_nodisplayname() { |
| 623 |
let user = PublicKeyCredentialUserEntity { |
| 624 |
id: vec![ |
| 625 |
0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x30, |
| 626 |
0x82, 0x01, 0x93, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x30, 0x82, |
| 627 |
0x01, 0x93, 0x30, 0x82, |
| 628 |
], |
| 629 |
name: Some(String::from("[email protected]")), |
| 630 |
display_name: None, |
| 631 |
}; |
| 632 |
|
| 633 |
let payload = ser::to_vec(&user).unwrap(); |
| 634 |
println!("payload = {payload:?}"); |
| 635 |
assert_eq!( |
| 636 |
payload, |
| 637 |
vec![ |
| 638 |
0xa2, // map(2) |
| 639 |
0x62, // text(2) |
| 640 |
0x69, 0x64, // "id" |
| 641 |
0x58, 0x20, // bytes(32) |
| 642 |
0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03, // userid |
| 643 |
0x02, 0x01, 0x02, 0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, // ... |
| 644 |
0x38, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x30, 0x82, 0x01, 0x93, // ... |
| 645 |
0x30, 0x82, // ... |
| 646 |
0x64, // text(4) |
| 647 |
0x6e, 0x61, 0x6d, 0x65, // "name" |
| 648 |
0x76, // text(22) |
| 649 |
0x6a, 0x6f, 0x68, 0x6e, 0x70, 0x73, 0x6d, 0x69, 0x74, |
| 650 |
0x68, // "[email protected]" |
| 651 |
0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, // ... |
| 652 |
0x6f, 0x6d, // ... |
| 653 |
] |
| 654 |
); |
| 655 |
} |
| 656 |
|
| 657 |
use serde_cbor::ser; |
| 658 |
|
| 659 |
#[test] |
| 660 |
fn public_key() { |
| 661 |
let keys = vec![ |
| 662 |
PublicKeyCredentialParameters { |
| 663 |
alg: COSEAlgorithm::ES256, |
| 664 |
}, |
| 665 |
PublicKeyCredentialParameters { |
| 666 |
alg: COSEAlgorithm::RS256, |
| 667 |
}, |
| 668 |
]; |
| 669 |
|
| 670 |
let payload = ser::to_vec(&keys); |
| 671 |
println!("payload = {payload:?}"); |
| 672 |
let payload = payload.unwrap(); |
| 673 |
assert_eq!( |
| 674 |
payload, |
| 675 |
vec![ |
| 676 |
0x82, // array(2) |
| 677 |
0xa2, // map(2) |
| 678 |
0x63, // text(3) |
| 679 |
0x61, 0x6c, 0x67, // "alg" |
| 680 |
0x26, // -7 (ES256) |
| 681 |
0x64, // text(4) |
| 682 |
0x74, 0x79, 0x70, 0x65, // "type" |
| 683 |
0x6a, // text(10) |
| 684 |
0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, // "public-key" |
| 685 |
0x2D, 0x6B, 0x65, 0x79, // ... |
| 686 |
0xa2, // map(2) |
| 687 |
0x63, // text(3) |
| 688 |
0x61, 0x6c, 0x67, // "alg" |
| 689 |
0x39, 0x01, 0x00, // -257 (RS256) |
| 690 |
0x64, // text(4) |
| 691 |
0x74, 0x79, 0x70, 0x65, // "type" |
| 692 |
0x6a, // text(10) |
| 693 |
0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, // "public-key" |
| 694 |
0x2D, 0x6B, 0x65, 0x79 // ... |
| 695 |
] |
| 696 |
); |
| 697 |
} |
| 698 |
|
| 699 |
#[test] |
| 700 |
fn public_key_desc() { |
| 701 |
let key = PublicKeyCredentialDescriptor { |
| 702 |
id: vec![ |
| 703 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, |
| 704 |
0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, |
| 705 |
0x1c, 0x1d, 0x1e, 0x1f, |
| 706 |
], |
| 707 |
transports: vec![Transport::BLE, Transport::USB], |
| 708 |
}; |
| 709 |
|
| 710 |
let payload = ser::to_vec(&key); |
| 711 |
println!("payload = {payload:?}"); |
| 712 |
let payload = payload.unwrap(); |
| 713 |
|
| 714 |
assert_eq!( |
| 715 |
payload, |
| 716 |
vec![ |
| 717 |
// 0xa3, // map(3) |
| 718 |
0xa2, // map(2) |
| 719 |
0x62, // text(2) |
| 720 |
0x69, 0x64, // "id" |
| 721 |
0x58, 0x20, // bytes(32) |
| 722 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, // key id |
| 723 |
0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, // ... |
| 724 |
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, // ... |
| 725 |
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, // ... |
| 726 |
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, // ... |
| 727 |
0x1e, 0x1f, // ... |
| 728 |
0x64, // text(4) |
| 729 |
0x74, 0x79, 0x70, 0x65, // "type" |
| 730 |
0x6a, // text(10) |
| 731 |
0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, // "public-key" |
| 732 |
0x2D, 0x6B, 0x65, |
| 733 |
0x79, // ... |
| 734 |
|
| 735 |
// Deactivated for now |
| 736 |
//0x6a, // text(10) |
| 737 |
//0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, // "transports" |
| 738 |
//0x6f, 0x72, 0x74, 0x73, // ... |
| 739 |
//0x82, // array(2) |
| 740 |
//0x63, // text(3) |
| 741 |
//0x62, 0x6c, 0x65, // "ble" |
| 742 |
//0x63, // text(3) |
| 743 |
//0x75, 0x73, 0x62 // "usb" |
| 744 |
] |
| 745 |
); |
| 746 |
} |
| 747 |
} |