| 1 |
use super::utils::{from_slice_stream, read_be_u16, read_be_u32, read_byte}; |
| 2 |
use crate::crypto::{COSEAlgorithm, CryptoError, SharedSecret}; |
| 3 |
use crate::ctap2::server::{CredentialProtectionPolicy, HMACGetSecretOutput, RpIdHash}; |
| 4 |
use crate::ctap2::utils::serde_parse_err; |
| 5 |
use crate::{crypto::COSEKey, errors::AuthenticatorError}; |
| 6 |
use base64::Engine; |
| 7 |
use serde::ser::{Error as SerError, SerializeMap, Serializer}; |
| 8 |
use serde::{ |
| 9 |
de::{Error as SerdeError, Unexpected, Visitor}, |
| 10 |
Deserialize, Deserializer, Serialize, |
| 11 |
}; |
| 12 |
use serde_cbor; |
| 13 |
use std::convert::TryInto; |
| 14 |
use std::fmt; |
| 15 |
use std::io::{Cursor, Read}; |
| 16 |
|
| 17 |
#[derive(Debug, PartialEq, Eq)] |
| 18 |
pub enum HmacSecretResponse { |
| 19 |
/// This is returned by MakeCredential calls to display if CredRandom was |
| 20 |
/// successfully generated |
| 21 |
Confirmed(bool), |
| 22 |
/// This is returned by GetAssertion: |
| 23 |
/// AES256-CBC(shared_secret, HMAC-SHA265(CredRandom, salt1) || HMAC-SHA265(CredRandom, salt2)) |
| 24 |
Secret(Vec<u8>), |
| 25 |
} |
| 26 |
|
| 27 |
impl HmacSecretResponse { |
| 28 |
/// Return the decrypted HMAC outputs, if this is an instance of [HmacSecretResponse::Secret]. |
| 29 |
pub fn decrypt_secrets( |
| 30 |
&self, |
| 31 |
shared_secret: &SharedSecret, |
| 32 |
) -> Option<Result<HMACGetSecretOutput, CryptoError>> { |
| 33 |
if let HmacSecretResponse::Secret(hmac_outputs) = self { |
| 34 |
Some(Self::decrypt_secrets_internal(shared_secret, hmac_outputs)) |
| 35 |
} else { |
| 36 |
None |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
fn decrypt_secrets_internal( |
| 41 |
shared_secret: &SharedSecret, |
| 42 |
hmac_outputs: &[u8], |
| 43 |
) -> Result<HMACGetSecretOutput, CryptoError> { |
| 44 |
let output_secrets = shared_secret.decrypt(hmac_outputs)?; |
| 45 |
match if output_secrets.len() < 32 { |
| 46 |
Err(CryptoError::WrongSaltLength) |
| 47 |
} else { |
| 48 |
let (output1, output2) = output_secrets.split_at(32); |
| 49 |
Ok(HMACGetSecretOutput { |
| 50 |
output1: output1 |
| 51 |
.try_into() |
| 52 |
.map_err(|_| CryptoError::WrongSaltLength)?, |
| 53 |
output2: (!output2.is_empty()) |
| 54 |
.then(|| output2.try_into().map_err(|_| CryptoError::WrongSaltLength)) |
| 55 |
.transpose()?, |
| 56 |
}) |
| 57 |
} { |
| 58 |
err @ Err(CryptoError::WrongSaltLength) => { |
| 59 |
// TODO: Use Result::inspect_err when stable |
| 60 |
debug!( |
| 61 |
"Bad hmac-secret output length: {} bytes (expected exactly 32 or 64)", |
| 62 |
output_secrets.len() |
| 63 |
); |
| 64 |
err |
| 65 |
} |
| 66 |
other => other, |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl Serialize for HmacSecretResponse { |
| 72 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 73 |
where |
| 74 |
S: Serializer, |
| 75 |
{ |
| 76 |
match self { |
| 77 |
HmacSecretResponse::Confirmed(x) => serializer.serialize_bool(*x), |
| 78 |
HmacSecretResponse::Secret(x) => serializer.serialize_bytes(x), |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
impl<'de> Deserialize<'de> for HmacSecretResponse { |
| 83 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 84 |
where |
| 85 |
D: Deserializer<'de>, |
| 86 |
{ |
| 87 |
struct HmacSecretResponseVisitor; |
| 88 |
|
| 89 |
impl<'de> Visitor<'de> for HmacSecretResponseVisitor { |
| 90 |
type Value = HmacSecretResponse; |
| 91 |
|
| 92 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 93 |
formatter.write_str("a byte array or a boolean") |
| 94 |
} |
| 95 |
|
| 96 |
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
| 97 |
where |
| 98 |
E: SerdeError, |
| 99 |
{ |
| 100 |
Ok(HmacSecretResponse::Secret(v.to_vec())) |
| 101 |
} |
| 102 |
|
| 103 |
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E> |
| 104 |
where |
| 105 |
E: SerdeError, |
| 106 |
{ |
| 107 |
Ok(HmacSecretResponse::Confirmed(v)) |
| 108 |
} |
| 109 |
} |
| 110 |
deserializer.deserialize_any(HmacSecretResponseVisitor) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Default)] |
| 115 |
pub struct Extension { |
| 116 |
#[serde(rename = "credProtect", skip_serializing_if = "Option::is_none")] |
| 117 |
pub cred_protect: Option<CredentialProtectionPolicy>, |
| 118 |
#[serde(rename = "hmac-secret", skip_serializing_if = "Option::is_none")] |
| 119 |
pub hmac_secret: Option<HmacSecretResponse>, |
| 120 |
#[serde(rename = "minPinLength", skip_serializing_if = "Option::is_none")] |
| 121 |
pub min_pin_length: Option<u64>, |
| 122 |
} |
| 123 |
|
| 124 |
impl Extension { |
| 125 |
pub fn has_some(&self) -> bool { |
| 126 |
self.min_pin_length.is_some() || self.hmac_secret.is_some() || self.cred_protect.is_some() |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#[derive(Serialize, PartialEq, Default, Eq, Clone)] |
| 131 |
pub struct AAGuid(pub [u8; 16]); |
| 132 |
|
| 133 |
impl AAGuid { |
| 134 |
pub fn from(src: &[u8]) -> Result<AAGuid, AuthenticatorError> { |
| 135 |
let mut payload = [0u8; 16]; |
| 136 |
if src.len() != payload.len() { |
| 137 |
Err(AuthenticatorError::InternalError(String::from( |
| 138 |
"Failed to parse AAGuid", |
| 139 |
))) |
| 140 |
} else { |
| 141 |
payload.copy_from_slice(src); |
| 142 |
Ok(AAGuid(payload)) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl fmt::Debug for AAGuid { |
| 148 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 149 |
write!( |
| 150 |
f, |
| 151 |
"AAGuid({:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x})", |
| 152 |
self.0[0], |
| 153 |
self.0[1], |
| 154 |
self.0[2], |
| 155 |
self.0[3], |
| 156 |
self.0[4], |
| 157 |
self.0[5], |
| 158 |
self.0[6], |
| 159 |
self.0[7], |
| 160 |
self.0[8], |
| 161 |
self.0[9], |
| 162 |
self.0[10], |
| 163 |
self.0[11], |
| 164 |
self.0[12], |
| 165 |
self.0[13], |
| 166 |
self.0[14], |
| 167 |
self.0[15] |
| 168 |
) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
impl<'de> Deserialize<'de> for AAGuid { |
| 173 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 174 |
where |
| 175 |
D: Deserializer<'de>, |
| 176 |
{ |
| 177 |
struct AAGuidVisitor; |
| 178 |
|
| 179 |
impl<'de> Visitor<'de> for AAGuidVisitor { |
| 180 |
type Value = AAGuid; |
| 181 |
|
| 182 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 183 |
formatter.write_str("a byte array") |
| 184 |
} |
| 185 |
|
| 186 |
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
| 187 |
where |
| 188 |
E: SerdeError, |
| 189 |
{ |
| 190 |
let mut buf = [0u8; 16]; |
| 191 |
if v.len() != buf.len() { |
| 192 |
return Err(E::invalid_length(v.len(), &"16")); |
| 193 |
} |
| 194 |
|
| 195 |
buf.copy_from_slice(v); |
| 196 |
|
| 197 |
Ok(AAGuid(buf)) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
deserializer.deserialize_bytes(AAGuidVisitor) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
#[derive(Debug, PartialEq, Eq)] |
| 206 |
pub struct AttestedCredentialData { |
| 207 |
pub aaguid: AAGuid, |
| 208 |
pub credential_id: Vec<u8>, |
| 209 |
pub credential_public_key: COSEKey, |
| 210 |
} |
| 211 |
|
| 212 |
fn parse_attested_cred_data<R: Read, E: SerdeError>( |
| 213 |
data: &mut R, |
| 214 |
) -> Result<AttestedCredentialData, E> { |
| 215 |
let mut aaguid_raw = [0u8; 16]; |
| 216 |
data.read_exact(&mut aaguid_raw) |
| 217 |
.map_err(|_| serde_parse_err("AAGuid"))?; |
| 218 |
let aaguid = AAGuid(aaguid_raw); |
| 219 |
let cred_len = read_be_u16(data)?; |
| 220 |
let mut credential_id = vec![0u8; cred_len as usize]; |
| 221 |
data.read_exact(&mut credential_id) |
| 222 |
.map_err(|_| serde_parse_err("CredentialId"))?; |
| 223 |
let credential_public_key = from_slice_stream(data)?; |
| 224 |
Ok(AttestedCredentialData { |
| 225 |
aaguid, |
| 226 |
credential_id, |
| 227 |
credential_public_key, |
| 228 |
}) |
| 229 |
} |
| 230 |
|
| 231 |
bitflags! { |
| 232 |
// Defining an exhaustive list of flags here ensures that `from_bits_truncate` is lossless and |
| 233 |
// that `from_bits` never returns None. |
| 234 |
pub struct AuthenticatorDataFlags: u8 { |
| 235 |
const USER_PRESENT = 0x01; |
| 236 |
const RESERVED_1 = 0x02; |
| 237 |
const USER_VERIFIED = 0x04; |
| 238 |
const RESERVED_3 = 0x08; |
| 239 |
const RESERVED_4 = 0x10; |
| 240 |
const RESERVED_5 = 0x20; |
| 241 |
const ATTESTED = 0x40; |
| 242 |
const EXTENSION_DATA = 0x80; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
#[derive(Debug, PartialEq, Eq)] |
| 247 |
pub struct AuthenticatorData { |
| 248 |
pub rp_id_hash: RpIdHash, |
| 249 |
pub flags: AuthenticatorDataFlags, |
| 250 |
pub counter: u32, |
| 251 |
pub credential_data: Option<AttestedCredentialData>, |
| 252 |
pub extensions: Extension, |
| 253 |
} |
| 254 |
|
| 255 |
impl AuthenticatorData { |
| 256 |
pub fn to_vec(&self) -> Vec<u8> { |
| 257 |
match serde_cbor::value::to_value(self) { |
| 258 |
Ok(serde_cbor::value::Value::Bytes(out)) => out, |
| 259 |
_ => unreachable!(), // Serialize is guaranteed to produce bytes |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
impl<'de> Deserialize<'de> for AuthenticatorData { |
| 265 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 266 |
where |
| 267 |
D: Deserializer<'de>, |
| 268 |
{ |
| 269 |
struct AuthenticatorDataVisitor; |
| 270 |
|
| 271 |
impl<'de> Visitor<'de> for AuthenticatorDataVisitor { |
| 272 |
type Value = AuthenticatorData; |
| 273 |
|
| 274 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 275 |
formatter.write_str("a byte array") |
| 276 |
} |
| 277 |
|
| 278 |
fn visit_bytes<E>(self, input: &[u8]) -> Result<Self::Value, E> |
| 279 |
where |
| 280 |
E: SerdeError, |
| 281 |
{ |
| 282 |
let mut cursor = Cursor::new(input); |
| 283 |
let mut rp_id_hash_raw = [0u8; 32]; |
| 284 |
cursor |
| 285 |
.read_exact(&mut rp_id_hash_raw) |
| 286 |
.map_err(|_| serde_parse_err("32 bytes"))?; |
| 287 |
let rp_id_hash = RpIdHash(rp_id_hash_raw); |
| 288 |
|
| 289 |
// preserve the flags, even if some reserved values are set. |
| 290 |
let flags = AuthenticatorDataFlags::from_bits_truncate(read_byte(&mut cursor)?); |
| 291 |
let counter = read_be_u32(&mut cursor)?; |
| 292 |
let mut credential_data = None; |
| 293 |
if flags.contains(AuthenticatorDataFlags::ATTESTED) { |
| 294 |
credential_data = Some(parse_attested_cred_data(&mut cursor)?); |
| 295 |
} |
| 296 |
|
| 297 |
let extensions = if flags.contains(AuthenticatorDataFlags::EXTENSION_DATA) { |
| 298 |
from_slice_stream(&mut cursor)? |
| 299 |
} else { |
| 300 |
Default::default() |
| 301 |
}; |
| 302 |
|
| 303 |
// TODO(baloo): we should check for end of buffer and raise a parse |
| 304 |
// parse error if data is still in the buffer |
| 305 |
Ok(AuthenticatorData { |
| 306 |
rp_id_hash, |
| 307 |
flags, |
| 308 |
counter, |
| 309 |
credential_data, |
| 310 |
extensions, |
| 311 |
}) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
deserializer.deserialize_bytes(AuthenticatorDataVisitor) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
impl Serialize for AuthenticatorData { |
| 320 |
// see https://www.w3.org/TR/webauthn-2/#sctn-authenticator-data |
| 321 |
// Authenticator Data |
| 322 |
// Name Length (in bytes) |
| 323 |
// rpIdHash 32 |
| 324 |
// flags 1 |
| 325 |
// signCount 4 |
| 326 |
// attestedCredentialData variable (if present) |
| 327 |
// extensions variable (if present) |
| 328 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 329 |
where |
| 330 |
S: Serializer, |
| 331 |
{ |
| 332 |
let mut data = Vec::new(); |
| 333 |
data.extend(self.rp_id_hash.0); // (1) "rpIDHash", len=32 |
| 334 |
data.extend([self.flags.bits()]); // (2) "flags", len=1 (u8) |
| 335 |
data.extend(self.counter.to_be_bytes()); // (3) "signCount", len=4, 32-bit unsigned big-endian integer. |
| 336 |
|
| 337 |
if let Some(cred) = &self.credential_data { |
| 338 |
// see https://www.w3.org/TR/webauthn-2/#sctn-attested-credential-data |
| 339 |
// Attested Credential Data |
| 340 |
// Name Length (in bytes) |
| 341 |
// aaguid 16 |
| 342 |
// credentialIdLength 2 |
| 343 |
// credentialId L |
| 344 |
// credentialPublicKey variable |
| 345 |
data.extend(cred.aaguid.0); // (1) "aaguid", len=16 |
| 346 |
data.extend((cred.credential_id.len() as u16).to_be_bytes()); // (2) "credentialIdLength", len=2, 16-bit unsigned big-endian integer |
| 347 |
data.extend(&cred.credential_id); // (3) "credentialId", len= see (2) |
| 348 |
data.extend( |
| 349 |
// (4) "credentialPublicKey", len=variable |
| 350 |
&serde_cbor::to_vec(&cred.credential_public_key) |
| 351 |
.map_err(|_| SerError::custom("Failed to serialize auth_data"))?, |
| 352 |
); |
| 353 |
} |
| 354 |
// If we have parsed extension data, then we should serialize it even if the authenticator |
| 355 |
// failed to set the extension data flag. |
| 356 |
// If we don't have parsed extension data, then what we output depends on the flag. |
| 357 |
// If the flag is set, we output the empty CBOR map. If it is not set, we output nothing. |
| 358 |
if self.extensions.has_some() || self.flags.contains(AuthenticatorDataFlags::EXTENSION_DATA) |
| 359 |
{ |
| 360 |
data.extend( |
| 361 |
// (5) "extensions", len=variable |
| 362 |
&serde_cbor::to_vec(&self.extensions) |
| 363 |
.map_err(|_| SerError::custom("Failed to serialize auth_data"))?, |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
serializer.serialize_bytes(&data) |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] |
| 372 |
/// x509 encoded attestation certificate |
| 373 |
pub struct AttestationCertificate(#[serde(with = "serde_bytes")] pub Vec<u8>); |
| 374 |
|
| 375 |
impl AsRef<[u8]> for AttestationCertificate { |
| 376 |
fn as_ref(&self) -> &[u8] { |
| 377 |
self.0.as_ref() |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
#[derive(Serialize, Deserialize, PartialEq, Eq)] |
| 382 |
pub struct Signature(#[serde(with = "serde_bytes")] pub Vec<u8>); |
| 383 |
|
| 384 |
impl fmt::Debug for Signature { |
| 385 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 386 |
let value = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(&self.0); |
| 387 |
write!(f, "Signature({value})") |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
impl AsRef<[u8]> for Signature { |
| 392 |
fn as_ref(&self) -> &[u8] { |
| 393 |
self.0.as_ref() |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
impl From<&[u8]> for Signature { |
| 398 |
fn from(sig: &[u8]) -> Signature { |
| 399 |
Signature(sig.to_vec()) |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
#[derive(Debug, PartialEq, Eq, Deserialize)] |
| 404 |
// The tag and content attributes here are really for AttestationObject, which contains an |
| 405 |
// "internally tagged" AttestationStatement. |
| 406 |
#[serde(tag = "fmt", content = "attStmt", rename_all = "lowercase")] |
| 407 |
pub enum AttestationStatement { |
| 408 |
#[serde(deserialize_with = "deserialize_none_att_stmt")] |
| 409 |
None, |
| 410 |
Packed(AttestationStatementPacked), |
| 411 |
#[serde(rename = "fido-u2f")] |
| 412 |
FidoU2F(AttestationStatementFidoU2F), |
| 413 |
// The remaining attestation statement formats are deserialized as serde_cbor::Values---we do |
| 414 |
// not perform any validation of their contents. These are expected to be used primarily when |
| 415 |
// anonymizing attestation objects that contain attestation statements in these formats. |
| 416 |
#[serde(rename = "android-key")] |
| 417 |
AndroidKey(serde_cbor::Value), |
| 418 |
#[serde(rename = "android-safetynet")] |
| 419 |
AndroidSafetyNet(serde_cbor::Value), |
| 420 |
Apple(serde_cbor::Value), |
| 421 |
Tpm(serde_cbor::Value), |
| 422 |
} |
| 423 |
|
| 424 |
impl AttestationStatement { |
| 425 |
/// The [attestation statement format identifier][att-fmt-id]. |
| 426 |
/// |
| 427 |
/// [att-fmt-id]: https://w3c.github.io/webauthn/#attestation-statement-format-identifier |
| 428 |
pub fn id(&self) -> &str { |
| 429 |
match self { |
| 430 |
Self::None => "none", |
| 431 |
Self::Packed(..) => "packed", |
| 432 |
Self::FidoU2F(..) => "fido-u2f", |
| 433 |
Self::AndroidKey(..) => "android-key", |
| 434 |
Self::AndroidSafetyNet(..) => "android-safetynet", |
| 435 |
Self::Apple(..) => "apple", |
| 436 |
Self::Tpm(..) => "tpm", |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
impl Serialize for AttestationStatement { |
| 442 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 443 |
where |
| 444 |
S: Serializer, |
| 445 |
{ |
| 446 |
match self { |
| 447 |
Self::None => serializer.serialize_map(Some(0))?.end(), |
| 448 |
Self::Packed(ref v) => serializer.serialize_some(v), |
| 449 |
Self::FidoU2F(ref v) => serializer.serialize_some(v), |
| 450 |
Self::AndroidKey(ref v) => serializer.serialize_some(v), |
| 451 |
Self::AndroidSafetyNet(ref v) => serializer.serialize_some(v), |
| 452 |
Self::Apple(ref v) => serializer.serialize_some(v), |
| 453 |
Self::Tpm(ref v) => serializer.serialize_some(v), |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
// AttestationStatement::None is serialized as the empty map. We need to enforce |
| 459 |
// the emptyness condition manually while deserializing. |
| 460 |
fn deserialize_none_att_stmt<'de, D>(deserializer: D) -> Result<(), D::Error> |
| 461 |
where |
| 462 |
D: Deserializer<'de>, |
| 463 |
{ |
| 464 |
let map = <std::collections::BTreeMap<(), ()>>::deserialize(deserializer)?; |
| 465 |
|
| 466 |
if !map.is_empty() { |
| 467 |
return Err(D::Error::invalid_value(Unexpected::Map, &"the empty map")); |
| 468 |
} |
| 469 |
|
| 470 |
Ok(()) |
| 471 |
} |
| 472 |
|
| 473 |
// Not all crypto-backends currently provide "crypto::verify()", so we do not implement it yet. |
| 474 |
// Also not sure, if we really need it. Would be a sanity-check only, to verify the signature is valid, |
| 475 |
// before sendig it out. |
| 476 |
// impl AttestationStatement { |
| 477 |
// pub fn verify(&self, data: &[u8]) -> Result<bool, AuthenticatorError> { |
| 478 |
// match self { |
| 479 |
// AttestationStatement::None => Ok(true), |
| 480 |
// AttestationStatement::Unparsed(_) => Err(AuthenticatorError::Custom( |
| 481 |
// "Unparsed attestation object can't be used to verify signature.".to_string(), |
| 482 |
// )), |
| 483 |
// AttestationStatement::FidoU2F(att) => { |
| 484 |
// let res = crypto::verify( |
| 485 |
// crypto::SignatureAlgorithm::ES256, |
| 486 |
// &att.attestation_cert[0].as_ref(), |
| 487 |
// att.sig.as_ref(), |
| 488 |
// data, |
| 489 |
// )?; |
| 490 |
// Ok(res) |
| 491 |
// } |
| 492 |
// AttestationStatement::Packed(att) => { |
| 493 |
// if att.alg != Alg::ES256 { |
| 494 |
// return Err(AuthenticatorError::Custom( |
| 495 |
// "Verification only supported for ES256".to_string(), |
| 496 |
// )); |
| 497 |
// } |
| 498 |
// let res = crypto::verify( |
| 499 |
// crypto::SignatureAlgorithm::ES256, |
| 500 |
// att.attestation_cert[0].as_ref(), |
| 501 |
// att.sig.as_ref(), |
| 502 |
// data, |
| 503 |
// )?; |
| 504 |
// Ok(res) |
| 505 |
// } |
| 506 |
// } |
| 507 |
// } |
| 508 |
// } |
| 509 |
|
| 510 |
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] |
| 511 |
// See https://www.w3.org/TR/webauthn-2/#sctn-fido-u2f-attestation |
| 512 |
// u2fStmtFormat = { |
| 513 |
// x5c: [ attestnCert: bytes ], |
| 514 |
// sig: bytes |
| 515 |
// } |
| 516 |
pub struct AttestationStatementFidoU2F { |
| 517 |
pub sig: Signature, // (1) "sig" |
| 518 |
/// Certificate chain in x509 format |
| 519 |
#[serde(rename = "x5c")] |
| 520 |
pub attestation_cert: Vec<AttestationCertificate>, // (2) "x5c" |
| 521 |
} |
| 522 |
|
| 523 |
impl AttestationStatementFidoU2F { |
| 524 |
pub fn new(cert: &[u8], signature: &[u8]) -> Self { |
| 525 |
AttestationStatementFidoU2F { |
| 526 |
attestation_cert: vec![AttestationCertificate(Vec::from(cert))], |
| 527 |
sig: Signature::from(signature), |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] |
| 533 |
// https://www.w3.org/TR/webauthn-2/#sctn-packed-attestation |
| 534 |
// packedStmtFormat = { |
| 535 |
// alg: COSEAlgorithmIdentifier, |
| 536 |
// sig: bytes, |
| 537 |
// x5c: [ attestnCert: bytes, * (caCert: bytes) ] |
| 538 |
// } // |
| 539 |
// { |
| 540 |
// alg: COSEAlgorithmIdentifier |
| 541 |
// sig: bytes, |
| 542 |
// } |
| 543 |
pub struct AttestationStatementPacked { |
| 544 |
pub alg: COSEAlgorithm, // (1) "alg" |
| 545 |
pub sig: Signature, // (2) "sig" |
| 546 |
/// Certificate chain in x509 format |
| 547 |
#[serde(rename = "x5c", skip_serializing_if = "Vec::is_empty", default)] |
| 548 |
pub attestation_cert: Vec<AttestationCertificate>, // (3) "x5c" |
| 549 |
} |
| 550 |
|
| 551 |
// A WebAuthn attestation object is a CBOR map with keys "fmt", "attStmt", and "authData". The |
| 552 |
// "fmt" field determines the type of "attStmt". The flatten attribute here turns the tag and |
| 553 |
// content attributes on AttestationStatement (defined above) into expected keys for |
| 554 |
// AttestationObject, which allows us to derive Deserialize. Like many of our other structs, the |
| 555 |
// derived Deserialize implementation is permissive: it does not enforce CTAP2 canonical CBOR |
| 556 |
// encoding and it allows repeated keys (the last one wins). |
| 557 |
#[derive(Debug, PartialEq, Eq, Deserialize)] |
| 558 |
#[serde(rename_all = "camelCase")] |
| 559 |
pub struct AttestationObject { |
| 560 |
pub auth_data: AuthenticatorData, |
| 561 |
#[serde(flatten)] |
| 562 |
pub att_stmt: AttestationStatement, |
| 563 |
} |
| 564 |
|
| 565 |
impl AttestationObject { |
| 566 |
pub fn anonymize(&mut self) { |
| 567 |
// Remove the attestation statement and the AAGUID from the authenticator data. |
| 568 |
self.att_stmt = AttestationStatement::None; |
| 569 |
if let Some(credential_data) = self.auth_data.credential_data.as_mut() { |
| 570 |
credential_data.aaguid = AAGuid::default(); |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
impl Serialize for AttestationObject { |
| 576 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 577 |
where |
| 578 |
S: Serializer, |
| 579 |
{ |
| 580 |
serialize_map!( |
| 581 |
serializer, |
| 582 |
// CTAP2 canonical CBOR order for these entries is ("fmt", "attStmt", "authData") |
| 583 |
// as strings are sorted by length and then lexically. |
| 584 |
// see https://www.w3.org/TR/webauthn-2/#attestation-object |
| 585 |
&"fmt" => self.att_stmt.id(), |
| 586 |
&"attStmt" => &self.att_stmt, |
| 587 |
&"authData" => &self.auth_data, |
| 588 |
) |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
#[cfg(test)] |
| 593 |
pub mod test { |
| 594 |
use super::super::utils::from_slice_stream; |
| 595 |
use super::*; |
| 596 |
use crate::crypto::{COSEAlgorithm, COSEEC2Key, COSEKey, COSEKeyType, Curve}; |
| 597 |
use serde_cbor::{from_slice, to_vec}; |
| 598 |
|
| 599 |
const SAMPLE_ATTESTATION_STMT_NONE: [u8; 19] = [ |
| 600 |
0xa2, // map(2) |
| 601 |
0x63, // text(3) |
| 602 |
0x66, 0x6d, 0x74, // "fmt" |
| 603 |
0x64, // text(4) |
| 604 |
0x6e, 0x6f, 0x6e, 0x65, // "none" |
| 605 |
0x67, // text(7) |
| 606 |
0x61, 0x74, 0x74, 0x53, 0x74, 0x6d, 0x74, // "attStmt" |
| 607 |
0xa0, // map(0) |
| 608 |
]; |
| 609 |
|
| 610 |
const SAMPLE_ATTESTATION_STMT_FIDO_U2F: [u8; 840] = [ |
| 611 |
0xa2, // map(2) |
| 612 |
0x63, // text(3) |
| 613 |
0x66, 0x6d, 0x74, // "fmt" |
| 614 |
0x68, // text(8) |
| 615 |
0x66, 0x69, 0x64, 0x6f, 0x2d, 0x75, 0x32, 0x66, // "fido-u2f" |
| 616 |
0x67, // text(7) |
| 617 |
0x61, 0x74, 0x74, 0x53, 0x74, 0x6d, 0x74, // "attStmt" |
| 618 |
0xa2, // map(2) |
| 619 |
0x63, // text(3) |
| 620 |
0x78, 0x35, 0x63, // "x5c" |
| 621 |
0x81, // array(1) |
| 622 |
0x59, 0x02, 0xdd, // bytes(733) |
| 623 |
0x30, 0x82, 0x02, 0xd9, 0x30, 0x82, 0x01, 0xc1, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, |
| 624 |
0x00, 0xdf, 0x92, 0xd9, 0xc4, 0xe2, 0xed, 0x66, 0x0a, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, |
| 625 |
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x2e, 0x31, 0x2c, 0x30, 0x2a, |
| 626 |
0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x23, 0x59, 0x75, 0x62, 0x69, 0x63, 0x6f, 0x20, 0x55, |
| 627 |
0x32, 0x46, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x20, 0x53, 0x65, 0x72, 0x69, |
| 628 |
0x61, 0x6c, 0x20, 0x34, 0x35, 0x37, 0x32, 0x30, 0x30, 0x36, 0x33, 0x31, 0x30, 0x20, 0x17, |
| 629 |
0x0d, 0x31, 0x34, 0x30, 0x38, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x18, |
| 630 |
0x0f, 0x32, 0x30, 0x35, 0x30, 0x30, 0x39, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 631 |
0x5a, 0x30, 0x6f, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x53, |
| 632 |
0x45, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x09, 0x59, 0x75, 0x62, |
| 633 |
0x69, 0x63, 0x6f, 0x20, 0x41, 0x42, 0x31, 0x22, 0x30, 0x20, 0x06, 0x03, 0x55, 0x04, 0x0b, |
| 634 |
0x0c, 0x19, 0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 635 |
0x20, 0x41, 0x74, 0x74, 0x65, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x31, 0x28, 0x30, |
| 636 |
0x26, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x1f, 0x59, 0x75, 0x62, 0x69, 0x63, 0x6f, 0x20, |
| 637 |
0x55, 0x32, 0x46, 0x20, 0x45, 0x45, 0x20, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x20, 0x31, |
| 638 |
0x31, 0x35, 0x35, 0x31, 0x30, 0x39, 0x35, 0x39, 0x39, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, |
| 639 |
0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, |
| 640 |
0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x0a, 0x18, 0x6c, 0x6e, 0x4d, 0x0a, 0x6a, 0x52, 0x8a, |
| 641 |
0x44, 0x90, 0x9a, 0x7a, 0x24, 0x23, 0x68, 0x70, 0x28, 0xd4, 0xc5, 0x7e, 0xcc, 0xb7, 0x17, |
| 642 |
0xba, 0x12, 0x80, 0xb8, 0x5c, 0x2f, 0xc1, 0xe4, 0xe0, 0x61, 0x66, 0x8c, 0x3c, 0x20, 0xae, |
| 643 |
0xf3, 0x33, 0x50, 0xd1, 0x96, 0x45, 0x23, 0x8a, 0x2c, 0x39, 0x0b, 0xf5, 0xdf, 0xfa, 0x34, |
| 644 |
0xff, 0x25, 0x50, 0x2f, 0x47, 0x0f, 0x3d, 0x40, 0xb8, 0x88, 0xa3, 0x81, 0x81, 0x30, 0x7f, |
| 645 |
0x30, 0x13, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xc4, 0x0a, 0x0d, 0x01, 0x04, |
| 646 |
0x05, 0x04, 0x03, 0x05, 0x04, 0x03, 0x30, 0x22, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, |
| 647 |
0x82, 0xc4, 0x0a, 0x02, 0x04, 0x15, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, |
| 648 |
0x2e, 0x31, 0x2e, 0x34, 0x31, 0x34, 0x38, 0x32, 0x2e, 0x31, 0x2e, 0x37, 0x30, 0x13, 0x06, |
| 649 |
0x0b, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xe5, 0x1c, 0x02, 0x01, 0x01, 0x04, 0x04, 0x03, |
| 650 |
0x02, 0x04, 0x30, 0x30, 0x21, 0x06, 0x0b, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xe5, 0x1c, |
| 651 |
0x01, 0x01, 0x04, 0x04, 0x12, 0x04, 0x10, 0x2f, 0xc0, 0x57, 0x9f, 0x81, 0x13, 0x47, 0xea, |
| 652 |
0xb1, 0x16, 0xbb, 0x5a, 0x8d, 0xb9, 0x20, 0x2a, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, |
| 653 |
0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
| 654 |
0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x82, 0xac, 0xaf, |
| 655 |
0x11, 0x30, 0xa9, 0x9b, 0xd1, 0x43, 0x27, 0xd2, 0xf8, 0xf9, 0xb0, 0x41, 0xa2, 0xa0, 0x4a, |
| 656 |
0x66, 0x85, 0x27, 0x24, 0x22, 0xe5, 0x7b, 0x14, 0xb0, 0xb8, 0xf8, 0x3b, 0x6f, 0x15, 0x45, |
| 657 |
0x66, 0x4b, 0xbf, 0x55, 0x68, 0x1e, 0xaf, 0x01, 0x58, 0x72, 0x2a, 0xbf, 0xce, 0xd2, 0xe4, |
| 658 |
0xac, 0x63, 0x3c, 0xec, 0x09, 0x59, 0x56, 0x45, 0x24, 0xb0, 0xf2, 0xe5, 0x17, 0xdd, 0x97, |
| 659 |
0x10, 0x98, 0xb9, 0x89, 0x15, 0x17, 0xec, 0xd0, 0xc5, 0x53, 0xa2, 0xe4, 0x73, 0x9f, 0x9d, |
| 660 |
0xe1, 0x3d, 0xaf, 0xd0, 0xd5, 0xd7, 0xb8, 0xac, 0x4a, 0x37, 0xf4, 0xf2, 0xcc, 0x30, 0xef, |
| 661 |
0x25, 0xcb, 0x00, 0x65, 0x2d, 0x19, 0xdb, 0x69, 0xd7, 0xda, 0x57, 0xbd, 0x1a, 0x9c, 0x1d, |
| 662 |
0x8e, 0xd8, 0x7d, 0x46, 0xd8, 0x0d, 0x2b, 0x3b, 0xdf, 0xd1, 0xd9, 0xef, 0x9d, 0x2b, 0x68, |
| 663 |
0x32, 0xd4, 0xad, 0x5b, 0xcd, 0x74, 0x21, 0x4c, 0xe6, 0xa6, 0x14, 0x1d, 0x16, 0xb2, 0xe9, |
| 664 |
0x3a, 0xcb, 0x2c, 0x88, 0xf6, 0x0a, 0x3e, 0xb6, 0xd5, 0xf6, 0x14, 0x71, 0x97, 0x59, 0x09, |
| 665 |
0x37, 0x3b, 0xc6, 0x77, 0x90, 0x23, 0x24, 0x57, 0x1a, 0x57, 0x3f, 0x60, 0xf0, 0x7b, 0xbe, |
| 666 |
0xd1, 0x7b, 0x92, 0xc8, 0xb5, 0x9f, 0xa2, 0x82, 0x10, 0xbf, 0xa8, 0xc6, 0x01, 0x22, 0x93, |
| 667 |
0x00, 0x1b, 0x39, 0xef, 0xe5, 0x7b, 0xf9, 0xcb, 0x1e, 0x3a, 0xca, 0x8a, 0x41, 0x30, 0xf8, |
| 668 |
0x3a, 0xf8, 0x66, 0x8f, 0x73, 0xde, 0xf2, 0x71, 0x1b, 0x20, 0xdc, 0x99, 0xe8, 0xa8, 0x04, |
| 669 |
0xee, 0xa3, 0xf7, 0x42, 0x71, 0x97, 0xb6, 0xb4, 0x51, 0xb3, 0x73, 0x5c, 0x23, 0xbc, 0x9b, |
| 670 |
0x1b, 0xe2, 0x74, 0xc2, 0x6d, 0x3b, 0xf9, 0x19, 0x6f, 0x8c, 0x4a, 0x4b, 0x71, 0x5f, 0x4b, |
| 671 |
0x95, 0xc4, 0xdb, 0x7b, 0x97, 0xe7, 0x59, 0x4e, 0xb4, 0x65, 0x64, 0x8c, 0x1c, 0x63, 0x73, |
| 672 |
0x69, 0x67, // "sig" |
| 673 |
0x58, 0x46, // bytes(70) |
| 674 |
0x30, 0x44, 0x02, 0x20, 0x48, 0x5a, 0x72, 0x40, 0xdf, 0x2c, 0x1e, 0x31, 0xa5, 0xb3, 0x0b, |
| 675 |
0x3b, 0x2c, 0xd1, 0xad, 0xd0, 0x8d, 0xae, 0x8d, 0x7a, 0x25, 0x3e, 0xf5, 0xa6, 0x25, 0xdb, |
| 676 |
0x2e, 0x22, 0x1b, 0x71, 0xe5, 0x78, 0x02, 0x20, 0x45, 0xbd, 0xdc, 0x30, 0xde, 0xf4, 0x05, |
| 677 |
0x97, 0x5c, 0xac, 0x72, 0x58, 0x96, 0xa6, 0x00, 0x94, 0x57, 0x3a, 0xa5, 0xe8, 0x1e, 0xf4, |
| 678 |
0xfd, 0x30, 0xd3, 0x88, 0x11, 0x8b, 0x49, 0x97, 0xdf, 0x34, |
| 679 |
]; |
| 680 |
|
| 681 |
const SAMPLE_ATTESTATION_OBJ_PACKED: [u8; 677] = [ |
| 682 |
0xa3, // map(3) |
| 683 |
0x63, // text(3) |
| 684 |
0x66, 0x6D, 0x74, // "fmt" |
| 685 |
0x66, // text(6) |
| 686 |
0x70, 0x61, 0x63, 0x6b, 0x65, 0x64, // "packed" |
| 687 |
0x67, // text(7) |
| 688 |
0x61, 0x74, 0x74, 0x53, 0x74, 0x6D, 0x74, // "attStmt" |
| 689 |
0xa3, // map(3) |
| 690 |
0x63, // text(3) |
| 691 |
0x61, 0x6c, 0x67, // "alg" |
| 692 |
0x26, // -7 (ES256) |
| 693 |
0x63, // text(3) |
| 694 |
0x73, 0x69, 0x67, // "sig" |
| 695 |
0x58, 0x47, // bytes(71) |
| 696 |
0x30, 0x45, 0x02, 0x20, 0x13, 0xf7, 0x3c, 0x5d, 0x9d, 0x53, 0x0e, 0x8c, 0xc1, 0x5c, |
| 697 |
0xc9, // signature |
| 698 |
0xbd, 0x96, 0xad, 0x58, 0x6d, 0x39, 0x36, 0x64, 0xe4, 0x62, 0xd5, 0xf0, 0x56, 0x12, |
| 699 |
0x35, // .. |
| 700 |
0xe6, 0x35, 0x0f, 0x2b, 0x72, 0x89, 0x02, 0x21, 0x00, 0x90, 0x35, 0x7f, 0xf9, 0x10, |
| 701 |
0xcc, // .. |
| 702 |
0xb5, 0x6a, 0xc5, 0xb5, 0x96, 0x51, 0x19, 0x48, 0x58, 0x1c, 0x8f, 0xdd, 0xb4, 0xa2, |
| 703 |
0xb7, // .. |
| 704 |
0x99, 0x59, 0x94, 0x80, 0x78, 0xb0, 0x9f, 0x4b, 0xdc, 0x62, 0x29, // .. |
| 705 |
0x63, // text(3) |
| 706 |
0x78, 0x35, 0x63, // "x5c" |
| 707 |
0x81, // array(1) |
| 708 |
0x59, 0x01, 0x97, // bytes(407) |
| 709 |
0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, //certificate... |
| 710 |
0x38, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00, 0x85, 0x9b, 0x72, 0x6c, 0xb2, 0x4b, |
| 711 |
0x4c, 0x29, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, |
| 712 |
0x47, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, |
| 713 |
0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0b, 0x59, 0x75, 0x62, 0x69, 0x63, |
| 714 |
0x6f, 0x20, 0x54, 0x65, 0x73, 0x74, 0x31, 0x22, 0x30, 0x20, 0x06, 0x03, 0x55, 0x04, 0x0b, |
| 715 |
0x0c, 0x19, 0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 716 |
0x20, 0x41, 0x74, 0x74, 0x65, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x30, 0x1e, 0x17, |
| 717 |
0x0d, 0x31, 0x36, 0x31, 0x32, 0x30, 0x34, 0x31, 0x31, 0x35, 0x35, 0x30, 0x30, 0x5a, 0x17, |
| 718 |
0x0d, 0x32, 0x36, 0x31, 0x32, 0x30, 0x32, 0x31, 0x31, 0x35, 0x35, 0x30, 0x30, 0x5a, 0x30, |
| 719 |
0x47, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, |
| 720 |
0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0b, 0x59, 0x75, 0x62, 0x69, 0x63, |
| 721 |
0x6f, 0x20, 0x54, 0x65, 0x73, 0x74, 0x31, 0x22, 0x30, 0x20, 0x06, 0x03, 0x55, 0x04, 0x0b, |
| 722 |
0x0c, 0x19, 0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 723 |
0x20, 0x41, 0x74, 0x74, 0x65, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x30, 0x59, 0x30, |
| 724 |
0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, |
| 725 |
0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xad, 0x11, 0xeb, 0x0e, 0x88, 0x52, |
| 726 |
0xe5, 0x3a, 0xd5, 0xdf, 0xed, 0x86, 0xb4, 0x1e, 0x61, 0x34, 0xa1, 0x8e, 0xc4, 0xe1, 0xaf, |
| 727 |
0x8f, 0x22, 0x1a, 0x3c, 0x7d, 0x6e, 0x63, 0x6c, 0x80, 0xea, 0x13, 0xc3, 0xd5, 0x04, 0xff, |
| 728 |
0x2e, 0x76, 0x21, 0x1b, 0xb4, 0x45, 0x25, 0xb1, 0x96, 0xc4, 0x4c, 0xb4, 0x84, 0x99, 0x79, |
| 729 |
0xcf, 0x6f, 0x89, 0x6e, 0xcd, 0x2b, 0xb8, 0x60, 0xde, 0x1b, 0xf4, 0x37, 0x6b, 0xa3, 0x0d, |
| 730 |
0x30, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0a, |
| 731 |
0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, |
| 732 |
0x02, 0x21, 0x00, 0xe9, 0xa3, 0x9f, 0x1b, 0x03, 0x19, 0x75, 0x25, 0xf7, 0x37, 0x3e, 0x10, |
| 733 |
0xce, 0x77, 0xe7, 0x80, 0x21, 0x73, 0x1b, 0x94, 0xd0, 0xc0, 0x3f, 0x3f, 0xda, 0x1f, 0xd2, |
| 734 |
0x2d, 0xb3, 0xd0, 0x30, 0xe7, 0x02, 0x21, 0x00, 0xc4, 0xfa, 0xec, 0x34, 0x45, 0xa8, 0x20, |
| 735 |
0xcf, 0x43, 0x12, 0x9c, 0xdb, 0x00, 0xaa, 0xbe, 0xfd, 0x9a, 0xe2, 0xd8, 0x74, 0xf9, 0xc5, |
| 736 |
0xd3, 0x43, 0xcb, 0x2f, 0x11, 0x3d, 0xa2, 0x37, 0x23, 0xf3, 0x68, // text(8) |
| 737 |
0x61, 0x75, 0x74, 0x68, 0x44, 0x61, 0x74, 0x61, // "authData" |
| 738 |
0x58, 0x94, // bytes(148) |
| 739 |
// authData |
| 740 |
0xc2, 0x89, 0xc5, 0xca, 0x9b, 0x04, 0x60, 0xf9, 0x34, 0x6a, 0xb4, 0xe4, 0x2d, 0x84, |
| 741 |
0x27, // rp_id_hash |
| 742 |
0x43, 0x40, 0x4d, 0x31, 0xf4, 0x84, 0x68, 0x25, 0xa6, 0xd0, 0x65, 0xbe, 0x59, 0x7a, |
| 743 |
0x87, // rp_id_hash |
| 744 |
0x05, 0x1d, // rp_id_hash |
| 745 |
0x41, // authData Flags |
| 746 |
0x00, 0x00, 0x00, 0x0b, // authData counter |
| 747 |
0xf8, 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, |
| 748 |
0x7d, // AAGUID |
| 749 |
0x00, 0x10, // credential id length |
| 750 |
0x89, 0x59, 0xce, 0xad, 0x5b, 0x5c, 0x48, 0x16, 0x4e, 0x8a, 0xbc, 0xd6, 0xd9, 0x43, 0x5c, |
| 751 |
0x6f, // credential id |
| 752 |
// credential public key |
| 753 |
0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21, 0x58, 0x20, 0xa5, 0xfd, 0x5c, 0xe1, 0xb1, |
| 754 |
0xc4, 0x58, 0xc5, 0x30, 0xa5, 0x4f, 0xa6, 0x1b, 0x31, 0xbf, 0x6b, 0x04, 0xbe, 0x8b, 0x97, |
| 755 |
0xaf, 0xde, 0x54, 0xdd, 0x8c, 0xbb, 0x69, 0x27, 0x5a, 0x8a, 0x1b, 0xe1, 0x22, 0x58, 0x20, |
| 756 |
0xfa, 0x3a, 0x32, 0x31, 0xdd, 0x9d, 0xee, 0xd9, 0xd1, 0x89, 0x7b, 0xe5, 0xa6, 0x22, 0x8c, |
| 757 |
0x59, 0x50, 0x1e, 0x4b, 0xcd, 0x12, 0x97, 0x5d, 0x3d, 0xff, 0x73, 0x0f, 0x01, 0x27, 0x8e, |
| 758 |
0xa6, 0x1c, |
| 759 |
]; |
| 760 |
|
| 761 |
const SAMPLE_CERT_CHAIN: [u8; 709] = [ |
| 762 |
0x81, 0x59, 0x2, 0xc1, 0x30, 0x82, 0x2, 0xbd, 0x30, 0x82, 0x1, 0xa5, 0xa0, 0x3, 0x2, 0x1, |
| 763 |
0x2, 0x2, 0x4, 0x18, 0xac, 0x46, 0xc0, 0x30, 0xd, 0x6, 0x9, 0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 764 |
0xd, 0x1, 0x1, 0xb, 0x5, 0x0, 0x30, 0x2e, 0x31, 0x2c, 0x30, 0x2a, 0x6, 0x3, 0x55, 0x4, 0x3, |
| 765 |
0x13, 0x23, 0x59, 0x75, 0x62, 0x69, 0x63, 0x6f, 0x20, 0x55, 0x32, 0x46, 0x20, 0x52, 0x6f, |
| 766 |
0x6f, 0x74, 0x20, 0x43, 0x41, 0x20, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x20, 0x34, 0x35, |
| 767 |
0x37, 0x32, 0x30, 0x30, 0x36, 0x33, 0x31, 0x30, 0x20, 0x17, 0xd, 0x31, 0x34, 0x30, 0x38, |
| 768 |
0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x18, 0xf, 0x32, 0x30, 0x35, 0x30, |
| 769 |
0x30, 0x39, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x6e, 0x31, 0xb, |
| 770 |
0x30, 0x9, 0x6, 0x3, 0x55, 0x4, 0x6, 0x13, 0x2, 0x53, 0x45, 0x31, 0x12, 0x30, 0x10, 0x6, |
| 771 |
0x3, 0x55, 0x4, 0xa, 0xc, 0x9, 0x59, 0x75, 0x62, 0x69, 0x63, 0x6f, 0x20, 0x41, 0x42, 0x31, |
| 772 |
0x22, 0x30, 0x20, 0x6, 0x3, 0x55, 0x4, 0xb, 0xc, 0x19, 0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, |
| 773 |
0x74, 0x69, 0x63, 0x61, 0x74, 0x6f, 0x72, 0x20, 0x41, 0x74, 0x74, 0x65, 0x73, 0x74, 0x61, |
| 774 |
0x74, 0x69, 0x6f, 0x6e, 0x31, 0x27, 0x30, 0x25, 0x6, 0x3, 0x55, 0x4, 0x3, 0xc, 0x1e, 0x59, |
| 775 |
0x75, 0x62, 0x69, 0x63, 0x6f, 0x20, 0x55, 0x32, 0x46, 0x20, 0x45, 0x45, 0x20, 0x53, 0x65, |
| 776 |
0x72, 0x69, 0x61, 0x6c, 0x20, 0x34, 0x31, 0x33, 0x39, 0x34, 0x33, 0x34, 0x38, 0x38, 0x30, |
| 777 |
0x59, 0x30, 0x13, 0x6, 0x7, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x2, 0x1, 0x6, 0x8, 0x2a, 0x86, |
| 778 |
0x48, 0xce, 0x3d, 0x3, 0x1, 0x7, 0x3, 0x42, 0x0, 0x4, 0x79, 0xea, 0x3b, 0x2c, 0x7c, 0x49, |
| 779 |
0x70, 0x10, 0x62, 0x23, 0xc, 0xd2, 0x3f, 0xeb, 0x60, 0xe5, 0x29, 0x31, 0x71, 0xd4, 0x83, |
| 780 |
0xf1, 0x0, 0xbe, 0x85, 0x9d, 0x6b, 0xf, 0x83, 0x97, 0x3, 0x1, 0xb5, 0x46, 0xcd, 0xd4, 0x6e, |
| 781 |
0xcf, 0xca, 0xe3, 0xe3, 0xf3, 0xf, 0x81, 0xe9, 0xed, 0x62, 0xbd, 0x26, 0x8d, 0x4c, 0x1e, |
| 782 |
0xbd, 0x37, 0xb3, 0xbc, 0xbe, 0x92, 0xa8, 0xc2, 0xae, 0xeb, 0x4e, 0x3a, 0xa3, 0x6c, 0x30, |
| 783 |
0x6a, 0x30, 0x22, 0x6, 0x9, 0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, 0xc4, 0xa, 0x2, 0x4, 0x15, |
| 784 |
0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, 0x34, 0x31, 0x34, |
| 785 |
0x38, 0x32, 0x2e, 0x31, 0x2e, 0x37, 0x30, 0x13, 0x6, 0xb, 0x2b, 0x6, 0x1, 0x4, 0x1, 0x82, |
| 786 |
0xe5, 0x1c, 0x2, 0x1, 0x1, 0x4, 0x4, 0x3, 0x2, 0x5, 0x20, 0x30, 0x21, 0x6, 0xb, 0x2b, 0x6, |
| 787 |
0x1, 0x4, 0x1, 0x82, 0xe5, 0x1c, 0x1, 0x1, 0x4, 0x4, 0x12, 0x4, 0x10, 0xcb, 0x69, 0x48, |
| 788 |
0x1e, 0x8f, 0xf7, 0x40, 0x39, 0x93, 0xec, 0xa, 0x27, 0x29, 0xa1, 0x54, 0xa8, 0x30, 0xc, |
| 789 |
0x6, 0x3, 0x55, 0x1d, 0x13, 0x1, 0x1, 0xff, 0x4, 0x2, 0x30, 0x0, 0x30, 0xd, 0x6, 0x9, 0x2a, |
| 790 |
0x86, 0x48, 0x86, 0xf7, 0xd, 0x1, 0x1, 0xb, 0x5, 0x0, 0x3, 0x82, 0x1, 0x1, 0x0, 0x97, 0x9d, |
| 791 |
0x3, 0x97, 0xd8, 0x60, 0xf8, 0x2e, 0xe1, 0x5d, 0x31, 0x1c, 0x79, 0x6e, 0xba, 0xfb, 0x22, |
| 792 |
0xfa, 0xa7, 0xe0, 0x84, 0xd9, 0xba, 0xb4, 0xc6, 0x1b, 0xbb, 0x57, 0xf3, 0xe6, 0xb4, 0xc1, |
| 793 |
0x8a, 0x48, 0x37, 0xb8, 0x5c, 0x3c, 0x4e, 0xdb, 0xe4, 0x83, 0x43, 0xf4, 0xd6, 0xa5, 0xd9, |
| 794 |
0xb1, 0xce, 0xda, 0x8a, 0xe1, 0xfe, 0xd4, 0x91, 0x29, 0x21, 0x73, 0x5, 0x8e, 0x5e, 0xe1, |
| 795 |
0xcb, 0xdd, 0x6b, 0xda, 0xc0, 0x75, 0x57, 0xc6, 0xa0, 0xe8, 0xd3, 0x68, 0x25, 0xba, 0x15, |
| 796 |
0x9e, 0x7f, 0xb5, 0xad, 0x8c, 0xda, 0xf8, 0x4, 0x86, 0x8c, 0xf9, 0xe, 0x8f, 0x1f, 0x8a, |
| 797 |
0xea, 0x17, 0xc0, 0x16, 0xb5, 0x5c, 0x2a, 0x7a, 0xd4, 0x97, 0xc8, 0x94, 0xfb, 0x71, 0xd7, |
| 798 |
0x53, 0xd7, 0x9b, 0x9a, 0x48, 0x4b, 0x6c, 0x37, 0x6d, 0x72, 0x3b, 0x99, 0x8d, 0x2e, 0x1d, |
| 799 |
0x43, 0x6, 0xbf, 0x10, 0x33, 0xb5, 0xae, 0xf8, 0xcc, 0xa5, 0xcb, 0xb2, 0x56, 0x8b, 0x69, |
| 800 |
0x24, 0x22, 0x6d, 0x22, 0xa3, 0x58, 0xab, 0x7d, 0x87, 0xe4, 0xac, 0x5f, 0x2e, 0x9, 0x1a, |
| 801 |
0xa7, 0x15, 0x79, 0xf3, 0xa5, 0x69, 0x9, 0x49, 0x7d, 0x72, 0xf5, 0x4e, 0x6, 0xba, 0xc1, |
| 802 |
0xc3, 0xb4, 0x41, 0x3b, 0xba, 0x5e, 0xaf, 0x94, 0xc3, 0xb6, 0x4f, 0x34, 0xf9, 0xeb, 0xa4, |
| 803 |
0x1a, 0xcb, 0x6a, 0xe2, 0x83, 0x77, 0x6d, 0x36, 0x46, 0x53, 0x78, 0x48, 0xfe, 0xe8, 0x84, |
| 804 |
0xbd, 0xdd, 0xf5, 0xb1, 0xba, 0x57, 0x98, 0x54, 0xcf, 0xfd, 0xce, 0xba, 0xc3, 0x44, 0x5, |
| 805 |
0x95, 0x27, 0xe5, 0x6d, 0xd5, 0x98, 0xf8, 0xf5, 0x66, 0x71, 0x5a, 0xbe, 0x43, 0x1, 0xdd, |
| 806 |
0x19, 0x11, 0x30, 0xe6, 0xb9, 0xf0, 0xc6, 0x40, 0x39, 0x12, 0x53, 0xe2, 0x29, 0x80, 0x3f, |
| 807 |
0x3a, 0xef, 0x27, 0x4b, 0xed, 0xbf, 0xde, 0x3f, 0xcb, 0xbd, 0x42, 0xea, 0xd6, 0x79, |
| 808 |
]; |
| 809 |
|
| 810 |
const SAMPLE_AUTH_DATA_MAKE_CREDENTIAL: [u8; 164] = [ |
| 811 |
0x58, 0xA2, // bytes(162) |
| 812 |
// authData |
| 813 |
0xc2, 0x89, 0xc5, 0xca, 0x9b, 0x04, 0x60, 0xf9, 0x34, 0x6a, 0xb4, 0xe4, 0x2d, 0x84, |
| 814 |
0x27, // rp_id_hash |
| 815 |
0x43, 0x40, 0x4d, 0x31, 0xf4, 0x84, 0x68, 0x25, 0xa6, 0xd0, 0x65, 0xbe, 0x59, 0x7a, |
| 816 |
0x87, // rp_id_hash |
| 817 |
0x05, 0x1d, // rp_id_hash |
| 818 |
0xC1, // authData Flags |
| 819 |
0x00, 0x00, 0x00, 0x0b, // authData counter |
| 820 |
0xf8, 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, |
| 821 |
0x7d, // AAGUID |
| 822 |
0x00, 0x10, // credential id length |
| 823 |
0x89, 0x59, 0xce, 0xad, 0x5b, 0x5c, 0x48, 0x16, 0x4e, 0x8a, 0xbc, 0xd6, 0xd9, 0x43, 0x5c, |
| 824 |
0x6f, // credential id |
| 825 |
// credential public key |
| 826 |
0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21, 0x58, 0x20, 0xa5, 0xfd, 0x5c, 0xe1, 0xb1, |
| 827 |
0xc4, 0x58, 0xc5, 0x30, 0xa5, 0x4f, 0xa6, 0x1b, 0x31, 0xbf, 0x6b, 0x04, 0xbe, 0x8b, 0x97, |
| 828 |
0xaf, 0xde, 0x54, 0xdd, 0x8c, 0xbb, 0x69, 0x27, 0x5a, 0x8a, 0x1b, 0xe1, 0x22, 0x58, 0x20, |
| 829 |
0xfa, 0x3a, 0x32, 0x31, 0xdd, 0x9d, 0xee, 0xd9, 0xd1, 0x89, 0x7b, 0xe5, 0xa6, 0x22, 0x8c, |
| 830 |
0x59, 0x50, 0x1e, 0x4b, 0xcd, 0x12, 0x97, 0x5d, 0x3d, 0xff, 0x73, 0x0f, 0x01, 0x27, 0x8e, |
| 831 |
0xa6, 0x1c, // pub key end |
| 832 |
// Extensions |
| 833 |
0xA1, // map(1) |
| 834 |
0x6B, // text(11) |
| 835 |
0x68, 0x6D, 0x61, 0x63, 0x2D, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, // "hmac-secret" |
| 836 |
0xF5, // true |
| 837 |
]; |
| 838 |
|
| 839 |
const SAMPLE_AUTH_DATA_GET_ASSERTION: [u8; 229] = [ |
| 840 |
0x58, 0xE3, // bytes(227) |
| 841 |
// authData |
| 842 |
0xc2, 0x89, 0xc5, 0xca, 0x9b, 0x04, 0x60, 0xf9, 0x34, 0x6a, 0xb4, 0xe4, 0x2d, 0x84, |
| 843 |
0x27, // rp_id_hash |
| 844 |
0x43, 0x40, 0x4d, 0x31, 0xf4, 0x84, 0x68, 0x25, 0xa6, 0xd0, 0x65, 0xbe, 0x59, 0x7a, |
| 845 |
0x87, // rp_id_hash |
| 846 |
0x05, 0x1d, // rp_id_hash |
| 847 |
0xC1, // authData Flags |
| 848 |
0x00, 0x00, 0x00, 0x0b, // authData counter |
| 849 |
0xf8, 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, |
| 850 |
0x7d, // AAGUID |
| 851 |
0x00, 0x10, // credential id length |
| 852 |
0x89, 0x59, 0xce, 0xad, 0x5b, 0x5c, 0x48, 0x16, 0x4e, 0x8a, 0xbc, 0xd6, 0xd9, 0x43, 0x5c, |
| 853 |
0x6f, // credential id |
| 854 |
// credential public key |
| 855 |
0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21, 0x58, 0x20, 0xa5, 0xfd, 0x5c, 0xe1, 0xb1, |
| 856 |
0xc4, 0x58, 0xc5, 0x30, 0xa5, 0x4f, 0xa6, 0x1b, 0x31, 0xbf, 0x6b, 0x04, 0xbe, 0x8b, 0x97, |
| 857 |
0xaf, 0xde, 0x54, 0xdd, 0x8c, 0xbb, 0x69, 0x27, 0x5a, 0x8a, 0x1b, 0xe1, 0x22, 0x58, 0x20, |
| 858 |
0xfa, 0x3a, 0x32, 0x31, 0xdd, 0x9d, 0xee, 0xd9, 0xd1, 0x89, 0x7b, 0xe5, 0xa6, 0x22, 0x8c, |
| 859 |
0x59, 0x50, 0x1e, 0x4b, 0xcd, 0x12, 0x97, 0x5d, 0x3d, 0xff, 0x73, 0x0f, 0x01, 0x27, 0x8e, |
| 860 |
0xa6, 0x1c, // pub key end |
| 861 |
// Extensions |
| 862 |
0xA1, // map(1) |
| 863 |
0x6B, // text(11) |
| 864 |
0x68, 0x6D, 0x61, 0x63, 0x2D, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, // "hmac-secret" |
| 865 |
0x58, 0x40, // bytes(64) |
| 866 |
0x1F, 0x91, 0x52, 0x6C, 0xAE, 0x45, 0x6E, 0x4C, 0xBB, 0x71, 0xC4, 0xDD, 0xE7, 0xBB, 0x87, |
| 867 |
0x71, 0x57, 0xE6, 0xE5, 0x4D, 0xFE, 0xD3, 0x01, 0x5D, 0x7D, 0x4D, 0xBB, 0x22, 0x69, 0xAF, |
| 868 |
0xCD, 0xE6, 0xA9, 0x1B, 0x8D, 0x26, 0x7E, 0xBB, 0xF8, 0x48, 0xEB, 0x95, 0xA6, 0x8E, 0x79, |
| 869 |
0xC7, 0xAC, 0x70, 0x5E, 0x35, 0x1D, 0x54, 0x3D, 0xB0, 0x16, 0x58, 0x87, 0xD6, 0x29, 0x0F, |
| 870 |
0xD4, 0x7A, 0x40, 0xC4, |
| 871 |
]; |
| 872 |
|
| 873 |
pub fn create_attestation_obj() -> AttestationObject { |
| 874 |
AttestationObject { |
| 875 |
auth_data: AuthenticatorData { |
| 876 |
rp_id_hash: RpIdHash::from(&[ |
| 877 |
0xc2, 0x89, 0xc5, 0xca, 0x9b, 0x04, 0x60, 0xf9, 0x34, 0x6a, 0xb4, 0xe4, 0x2d, |
| 878 |
0x84, 0x27, 0x43, 0x40, 0x4d, 0x31, 0xf4, 0x84, 0x68, 0x25, 0xa6, 0xd0, 0x65, |
| 879 |
0xbe, 0x59, 0x7a, 0x87, 0x5, 0x1d, |
| 880 |
]) |
| 881 |
.unwrap(), |
| 882 |
flags: AuthenticatorDataFlags::USER_PRESENT | AuthenticatorDataFlags::ATTESTED, |
| 883 |
counter: 11, |
| 884 |
credential_data: Some(AttestedCredentialData { |
| 885 |
aaguid: AAGuid::from(&[ |
| 886 |
0xf8, 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11, |
| 887 |
0x1f, 0x9e, 0xdc, 0x7d, |
| 888 |
]) |
| 889 |
.unwrap(), |
| 890 |
credential_id: vec![ |
| 891 |
0x89, 0x59, 0xce, 0xad, 0x5b, 0x5c, 0x48, 0x16, 0x4e, 0x8a, 0xbc, 0xd6, |
| 892 |
0xd9, 0x43, 0x5c, 0x6f, |
| 893 |
], |
| 894 |
credential_public_key: COSEKey { |
| 895 |
alg: COSEAlgorithm::ES256, |
| 896 |
key: COSEKeyType::EC2(COSEEC2Key { |
| 897 |
curve: Curve::SECP256R1, |
| 898 |
x: vec![ |
| 899 |
0xA5, 0xFD, 0x5C, 0xE1, 0xB1, 0xC4, 0x58, 0xC5, 0x30, 0xA5, 0x4F, |
| 900 |
0xA6, 0x1B, 0x31, 0xBF, 0x6B, 0x04, 0xBE, 0x8B, 0x97, 0xAF, 0xDE, |
| 901 |
0x54, 0xDD, 0x8C, 0xBB, 0x69, 0x27, 0x5A, 0x8A, 0x1B, 0xE1, |
| 902 |
], |
| 903 |
y: vec![ |
| 904 |
0xFA, 0x3A, 0x32, 0x31, 0xDD, 0x9D, 0xEE, 0xD9, 0xD1, 0x89, 0x7B, |
| 905 |
0xE5, 0xA6, 0x22, 0x8C, 0x59, 0x50, 0x1E, 0x4B, 0xCD, 0x12, 0x97, |
| 906 |
0x5D, 0x3D, 0xFF, 0x73, 0x0F, 0x01, 0x27, 0x8E, 0xA6, 0x1C, |
| 907 |
], |
| 908 |
}), |
| 909 |
}, |
| 910 |
}), |
| 911 |
extensions: Default::default(), |
| 912 |
}, |
| 913 |
att_stmt: AttestationStatement::Packed(AttestationStatementPacked { |
| 914 |
alg: COSEAlgorithm::ES256, |
| 915 |
sig: Signature(vec![ |
| 916 |
0x30, 0x45, 0x02, 0x20, 0x13, 0xf7, 0x3c, 0x5d, 0x9d, 0x53, 0x0e, 0x8c, 0xc1, |
| 917 |
0x5c, 0xc9, 0xbd, 0x96, 0xad, 0x58, 0x6d, 0x39, 0x36, 0x64, 0xe4, 0x62, 0xd5, |
| 918 |
0xf0, 0x56, 0x12, 0x35, 0xe6, 0x35, 0x0f, 0x2b, 0x72, 0x89, 0x02, 0x21, 0x00, |
| 919 |
0x90, 0x35, 0x7f, 0xf9, 0x10, 0xcc, 0xb5, 0x6a, 0xc5, 0xb5, 0x96, 0x51, 0x19, |
| 920 |
0x48, 0x58, 0x1c, 0x8f, 0xdd, 0xb4, 0xa2, 0xb7, 0x99, 0x59, 0x94, 0x80, 0x78, |
| 921 |
0xb0, 0x9f, 0x4b, 0xdc, 0x62, 0x29, |
| 922 |
]), |
| 923 |
attestation_cert: vec![AttestationCertificate(vec![ |
| 924 |
0x30, 0x82, 0x01, 0x93, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03, 0x02, 0x01, 0x02, |
| 925 |
0x02, 0x09, 0x00, 0x85, 0x9b, 0x72, 0x6c, 0xb2, 0x4b, 0x4c, 0x29, 0x30, 0x0a, |
| 926 |
0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x47, 0x31, |
| 927 |
0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, |
| 928 |
0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0b, 0x59, 0x75, 0x62, |
| 929 |
0x69, 0x63, 0x6f, 0x20, 0x54, 0x65, 0x73, 0x74, 0x31, 0x22, 0x30, 0x20, 0x06, |
| 930 |
0x03, 0x55, 0x04, 0x0b, 0x0c, 0x19, 0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, |
| 931 |
0x69, 0x63, 0x61, 0x74, 0x6f, 0x72, 0x20, 0x41, 0x74, 0x74, 0x65, 0x73, 0x74, |
| 932 |
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x31, 0x32, |
| 933 |
0x30, 0x34, 0x31, 0x31, 0x35, 0x35, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x32, 0x36, |
| 934 |
0x31, 0x32, 0x30, 0x32, 0x31, 0x31, 0x35, 0x35, 0x30, 0x30, 0x5a, 0x30, 0x47, |
| 935 |
0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, |
| 936 |
0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0b, 0x59, 0x75, |
| 937 |
0x62, 0x69, 0x63, 0x6f, 0x20, 0x54, 0x65, 0x73, 0x74, 0x31, 0x22, 0x30, 0x20, |
| 938 |
0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x19, 0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, |
| 939 |
0x74, 0x69, 0x63, 0x61, 0x74, 0x6f, 0x72, 0x20, 0x41, 0x74, 0x74, 0x65, 0x73, |
| 940 |
0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, |
| 941 |
0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, |
| 942 |
0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xad, 0x11, 0xeb, 0x0e, 0x88, 0x52, |
| 943 |
0xe5, 0x3a, 0xd5, 0xdf, 0xed, 0x86, 0xb4, 0x1e, 0x61, 0x34, 0xa1, 0x8e, 0xc4, |
| 944 |
0xe1, 0xaf, 0x8f, 0x22, 0x1a, 0x3c, 0x7d, 0x6e, 0x63, 0x6c, 0x80, 0xea, 0x13, |
| 945 |
0xc3, 0xd5, 0x04, 0xff, 0x2e, 0x76, 0x21, 0x1b, 0xb4, 0x45, 0x25, 0xb1, 0x96, |
| 946 |
0xc4, 0x4c, 0xb4, 0x84, 0x99, 0x79, 0xcf, 0x6f, 0x89, 0x6e, 0xcd, 0x2b, 0xb8, |
| 947 |
0x60, 0xde, 0x1b, 0xf4, 0x37, 0x6b, 0xa3, 0x0d, 0x30, 0x0b, 0x30, 0x09, 0x06, |
| 948 |
0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0a, 0x06, 0x08, 0x2a, |
| 949 |
0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, |
| 950 |
0x21, 0x00, 0xe9, 0xa3, 0x9f, 0x1b, 0x03, 0x19, 0x75, 0x25, 0xf7, 0x37, 0x3e, |
| 951 |
0x10, 0xce, 0x77, 0xe7, 0x80, 0x21, 0x73, 0x1b, 0x94, 0xd0, 0xc0, 0x3f, 0x3f, |
| 952 |
0xda, 0x1f, 0xd2, 0x2d, 0xb3, 0xd0, 0x30, 0xe7, 0x02, 0x21, 0x00, 0xc4, 0xfa, |
| 953 |
0xec, 0x34, 0x45, 0xa8, 0x20, 0xcf, 0x43, 0x12, 0x9c, 0xdb, 0x00, 0xaa, 0xbe, |
| 954 |
0xfd, 0x9a, 0xe2, 0xd8, 0x74, 0xf9, 0xc5, 0xd3, 0x43, 0xcb, 0x2f, 0x11, 0x3d, |
| 955 |
0xa2, 0x37, 0x23, 0xf3, |
| 956 |
])], |
| 957 |
}), |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
#[test] |
| 962 |
fn parse_cert_chain() { |
| 963 |
let cert: AttestationCertificate = from_slice(&SAMPLE_CERT_CHAIN[1..]).unwrap(); |
| 964 |
assert_eq!(&cert.0, &SAMPLE_CERT_CHAIN[4..]); |
| 965 |
|
| 966 |
let _cert: Vec<AttestationCertificate> = from_slice(&SAMPLE_CERT_CHAIN).unwrap(); |
| 967 |
} |
| 968 |
|
| 969 |
#[test] |
| 970 |
fn parse_attestation_statement() { |
| 971 |
let actual: AttestationStatement = from_slice(&SAMPLE_ATTESTATION_STMT_NONE).unwrap(); |
| 972 |
let expected = AttestationStatement::None; |
| 973 |
assert_eq!(expected, actual); |
| 974 |
|
| 975 |
let actual: AttestationStatement = from_slice(&SAMPLE_ATTESTATION_STMT_FIDO_U2F).unwrap(); |
| 976 |
let expected = AttestationStatement::FidoU2F(AttestationStatementFidoU2F { |
| 977 |
attestation_cert: vec![AttestationCertificate(vec![ |
| 978 |
0x30, 0x82, 0x02, 0xd9, 0x30, 0x82, 0x01, 0xc1, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, |
| 979 |
0x09, 0x00, 0xdf, 0x92, 0xd9, 0xc4, 0xe2, 0xed, 0x66, 0x0a, 0x30, 0x0d, 0x06, 0x09, |
| 980 |
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x2e, 0x31, |
| 981 |
0x2c, 0x30, 0x2a, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x23, 0x59, 0x75, 0x62, 0x69, |
| 982 |
0x63, 0x6f, 0x20, 0x55, 0x32, 0x46, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, |
| 983 |
0x20, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x20, 0x34, 0x35, 0x37, 0x32, 0x30, 0x30, |
| 984 |
0x36, 0x33, 0x31, 0x30, 0x20, 0x17, 0x0d, 0x31, 0x34, 0x30, 0x38, 0x30, 0x31, 0x30, |
| 985 |
0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x32, 0x30, 0x35, 0x30, 0x30, 0x39, |
| 986 |
0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x6f, 0x31, 0x0b, 0x30, |
| 987 |
0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x53, 0x45, 0x31, 0x12, 0x30, 0x10, |
| 988 |
0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x09, 0x59, 0x75, 0x62, 0x69, 0x63, 0x6f, 0x20, |
| 989 |
0x41, 0x42, 0x31, 0x22, 0x30, 0x20, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x19, 0x41, |
| 990 |
0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x6f, 0x72, 0x20, 0x41, |
| 991 |
0x74, 0x74, 0x65, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x31, 0x28, 0x30, 0x26, |
| 992 |
0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x1f, 0x59, 0x75, 0x62, 0x69, 0x63, 0x6f, 0x20, |
| 993 |
0x55, 0x32, 0x46, 0x20, 0x45, 0x45, 0x20, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x20, |
| 994 |
0x31, 0x31, 0x35, 0x35, 0x31, 0x30, 0x39, 0x35, 0x39, 0x39, 0x30, 0x59, 0x30, 0x13, |
| 995 |
0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, |
| 996 |
0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x0a, 0x18, 0x6c, 0x6e, 0x4d, |
| 997 |
0x0a, 0x6a, 0x52, 0x8a, 0x44, 0x90, 0x9a, 0x7a, 0x24, 0x23, 0x68, 0x70, 0x28, 0xd4, |
| 998 |
0xc5, 0x7e, 0xcc, 0xb7, 0x17, 0xba, 0x12, 0x80, 0xb8, 0x5c, 0x2f, 0xc1, 0xe4, 0xe0, |
| 999 |
0x61, 0x66, 0x8c, 0x3c, 0x20, 0xae, 0xf3, 0x33, 0x50, 0xd1, 0x96, 0x45, 0x23, 0x8a, |
| 1000 |
0x2c, 0x39, 0x0b, 0xf5, 0xdf, 0xfa, 0x34, 0xff, 0x25, 0x50, 0x2f, 0x47, 0x0f, 0x3d, |
| 1001 |
0x40, 0xb8, 0x88, 0xa3, 0x81, 0x81, 0x30, 0x7f, 0x30, 0x13, 0x06, 0x0a, 0x2b, 0x06, |
| 1002 |
0x01, 0x04, 0x01, 0x82, 0xc4, 0x0a, 0x0d, 0x01, 0x04, 0x05, 0x04, 0x03, 0x05, 0x04, |
| 1003 |
0x03, 0x30, 0x22, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xc4, 0x0a, 0x02, |
| 1004 |
0x04, 0x15, 0x31, 0x2e, 0x33, 0x2e, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x31, 0x2e, |
| 1005 |
0x34, 0x31, 0x34, 0x38, 0x32, 0x2e, 0x31, 0x2e, 0x37, 0x30, 0x13, 0x06, 0x0b, 0x2b, |
| 1006 |
0x06, 0x01, 0x04, 0x01, 0x82, 0xe5, 0x1c, 0x02, 0x01, 0x01, 0x04, 0x04, 0x03, 0x02, |
| 1007 |
0x04, 0x30, 0x30, 0x21, 0x06, 0x0b, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xe5, 0x1c, |
| 1008 |
0x01, 0x01, 0x04, 0x04, 0x12, 0x04, 0x10, 0x2f, 0xc0, 0x57, 0x9f, 0x81, 0x13, 0x47, |
| 1009 |
0xea, 0xb1, 0x16, 0xbb, 0x5a, 0x8d, 0xb9, 0x20, 0x2a, 0x30, 0x0c, 0x06, 0x03, 0x55, |
| 1010 |
0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, |
| 1011 |
0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, |
| 1012 |
0x00, 0x82, 0xac, 0xaf, 0x11, 0x30, 0xa9, 0x9b, 0xd1, 0x43, 0x27, 0xd2, 0xf8, 0xf9, |
| 1013 |
0xb0, 0x41, 0xa2, 0xa0, 0x4a, 0x66, 0x85, 0x27, 0x24, 0x22, 0xe5, 0x7b, 0x14, 0xb0, |
| 1014 |
0xb8, 0xf8, 0x3b, 0x6f, 0x15, 0x45, 0x66, 0x4b, 0xbf, 0x55, 0x68, 0x1e, 0xaf, 0x01, |
| 1015 |
0x58, 0x72, 0x2a, 0xbf, 0xce, 0xd2, 0xe4, 0xac, 0x63, 0x3c, 0xec, 0x09, 0x59, 0x56, |
| 1016 |
0x45, 0x24, 0xb0, 0xf2, 0xe5, 0x17, 0xdd, 0x97, 0x10, 0x98, 0xb9, 0x89, 0x15, 0x17, |
| 1017 |
0xec, 0xd0, 0xc5, 0x53, 0xa2, 0xe4, 0x73, 0x9f, 0x9d, 0xe1, 0x3d, 0xaf, 0xd0, 0xd5, |
| 1018 |
0xd7, 0xb8, 0xac, 0x4a, 0x37, 0xf4, 0xf2, 0xcc, 0x30, 0xef, 0x25, 0xcb, 0x00, 0x65, |
| 1019 |
0x2d, 0x19, 0xdb, 0x69, 0xd7, 0xda, 0x57, 0xbd, 0x1a, 0x9c, 0x1d, 0x8e, 0xd8, 0x7d, |
| 1020 |
0x46, 0xd8, 0x0d, 0x2b, 0x3b, 0xdf, 0xd1, 0xd9, 0xef, 0x9d, 0x2b, 0x68, 0x32, 0xd4, |
| 1021 |
0xad, 0x5b, 0xcd, 0x74, 0x21, 0x4c, 0xe6, 0xa6, 0x14, 0x1d, 0x16, 0xb2, 0xe9, 0x3a, |
| 1022 |
0xcb, 0x2c, 0x88, 0xf6, 0x0a, 0x3e, 0xb6, 0xd5, 0xf6, 0x14, 0x71, 0x97, 0x59, 0x09, |
| 1023 |
0x37, 0x3b, 0xc6, 0x77, 0x90, 0x23, 0x24, 0x57, 0x1a, 0x57, 0x3f, 0x60, 0xf0, 0x7b, |
| 1024 |
0xbe, 0xd1, 0x7b, 0x92, 0xc8, 0xb5, 0x9f, 0xa2, 0x82, 0x10, 0xbf, 0xa8, 0xc6, 0x01, |
| 1025 |
0x22, 0x93, 0x00, 0x1b, 0x39, 0xef, 0xe5, 0x7b, 0xf9, 0xcb, 0x1e, 0x3a, 0xca, 0x8a, |
| 1026 |
0x41, 0x30, 0xf8, 0x3a, 0xf8, 0x66, 0x8f, 0x73, 0xde, 0xf2, 0x71, 0x1b, 0x20, 0xdc, |
| 1027 |
0x99, 0xe8, 0xa8, 0x04, 0xee, 0xa3, 0xf7, 0x42, 0x71, 0x97, 0xb6, 0xb4, 0x51, 0xb3, |
| 1028 |
0x73, 0x5c, 0x23, 0xbc, 0x9b, 0x1b, 0xe2, 0x74, 0xc2, 0x6d, 0x3b, 0xf9, 0x19, 0x6f, |
| 1029 |
0x8c, 0x4a, 0x4b, 0x71, 0x5f, 0x4b, 0x95, 0xc4, 0xdb, 0x7b, 0x97, 0xe7, 0x59, 0x4e, |
| 1030 |
0xb4, 0x65, 0x64, 0x8c, 0x1c, |
| 1031 |
])], |
| 1032 |
sig: Signature(vec![ |
| 1033 |
0x30, 0x44, 0x02, 0x20, 0x48, 0x5a, 0x72, 0x40, 0xdf, 0x2c, 0x1e, 0x31, 0xa5, 0xb3, |
| 1034 |
0x0b, 0x3b, 0x2c, 0xd1, 0xad, 0xd0, 0x8d, 0xae, 0x8d, 0x7a, 0x25, 0x3e, 0xf5, 0xa6, |
| 1035 |
0x25, 0xdb, 0x2e, 0x22, 0x1b, 0x71, 0xe5, 0x78, 0x02, 0x20, 0x45, 0xbd, 0xdc, 0x30, |
| 1036 |
0xde, 0xf4, 0x05, 0x97, 0x5c, 0xac, 0x72, 0x58, 0x96, 0xa6, 0x00, 0x94, 0x57, 0x3a, |
| 1037 |
0xa5, 0xe8, 0x1e, 0xf4, 0xfd, 0x30, 0xd3, 0x88, 0x11, 0x8b, 0x49, 0x97, 0xdf, 0x34, |
| 1038 |
]), |
| 1039 |
}); |
| 1040 |
assert_eq!(expected, actual); |
| 1041 |
} |
| 1042 |
|
| 1043 |
#[test] |
| 1044 |
fn parse_attestation_object() { |
| 1045 |
let actual: AttestationObject = from_slice(&SAMPLE_ATTESTATION_OBJ_PACKED).unwrap(); |
| 1046 |
let expected = create_attestation_obj(); |
| 1047 |
|
| 1048 |
assert_eq!(expected, actual); |
| 1049 |
} |
| 1050 |
|
| 1051 |
#[test] |
| 1052 |
fn test_anonymize_att_obj() { |
| 1053 |
// Anonymize should prevent identifying data in the attestation statement from being |
| 1054 |
// serialized. |
| 1055 |
let mut att_obj = create_attestation_obj(); |
| 1056 |
|
| 1057 |
// This test assumes that the sample attestation object contains identifying information |
| 1058 |
assert_ne!(att_obj.att_stmt, AttestationStatement::None); |
| 1059 |
assert_ne!( |
| 1060 |
att_obj |
| 1061 |
.auth_data |
| 1062 |
.credential_data |
| 1063 |
.as_ref() |
| 1064 |
.expect("credential_data should be Some") |
| 1065 |
.aaguid, |
| 1066 |
AAGuid::default() |
| 1067 |
); |
| 1068 |
|
| 1069 |
att_obj.anonymize(); |
| 1070 |
|
| 1071 |
// Write the attestation object out to bytes and read it back. The result should not |
| 1072 |
// have an attestation statement, and it should have the default AAGUID. |
| 1073 |
let encoded_att_obj = to_vec(&att_obj).expect("could not serialize anonymized att_obj"); |
| 1074 |
let att_obj: AttestationObject = |
| 1075 |
from_slice(&encoded_att_obj).expect("could not deserialize anonymized att_obj"); |
| 1076 |
|
| 1077 |
assert_eq!(att_obj.att_stmt, AttestationStatement::None); |
| 1078 |
assert_eq!( |
| 1079 |
att_obj |
| 1080 |
.auth_data |
| 1081 |
.credential_data |
| 1082 |
.as_ref() |
| 1083 |
.expect("credential_data should be Some") |
| 1084 |
.aaguid, |
| 1085 |
AAGuid::default() |
| 1086 |
); |
| 1087 |
} |
| 1088 |
|
| 1089 |
#[test] |
| 1090 |
fn parse_reader() { |
| 1091 |
let v: Vec<u8> = vec![ |
| 1092 |
0x66, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72, 0x66, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72, |
| 1093 |
]; |
| 1094 |
let mut data = Cursor::new(v); |
| 1095 |
let value: String = from_slice_stream::<_, _, serde_cbor::Error>(&mut data).unwrap(); |
| 1096 |
assert_eq!(value, "foobar"); |
| 1097 |
let mut remaining = Vec::new(); |
| 1098 |
data.read_to_end(&mut remaining).unwrap(); |
| 1099 |
assert_eq!( |
| 1100 |
remaining.as_slice(), |
| 1101 |
&[0x66, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72] |
| 1102 |
); |
| 1103 |
let mut data = Cursor::new(remaining); |
| 1104 |
let value: String = from_slice_stream::<_, _, serde_cbor::Error>(&mut data).unwrap(); |
| 1105 |
assert_eq!(value, "foobar"); |
| 1106 |
let mut remaining = Vec::new(); |
| 1107 |
data.read_to_end(&mut remaining).unwrap(); |
| 1108 |
assert!(remaining.is_empty()); |
| 1109 |
} |
| 1110 |
|
| 1111 |
#[test] |
| 1112 |
fn parse_extensions() { |
| 1113 |
let auth_make: AuthenticatorData = from_slice(&SAMPLE_AUTH_DATA_MAKE_CREDENTIAL).unwrap(); |
| 1114 |
assert_eq!( |
| 1115 |
auth_make.extensions.hmac_secret, |
| 1116 |
Some(HmacSecretResponse::Confirmed(true)) |
| 1117 |
); |
| 1118 |
let auth_get: AuthenticatorData = from_slice(&SAMPLE_AUTH_DATA_GET_ASSERTION).unwrap(); |
| 1119 |
assert_eq!( |
| 1120 |
auth_get.extensions.hmac_secret, |
| 1121 |
Some(HmacSecretResponse::Secret(vec![ |
| 1122 |
0x1F, 0x91, 0x52, 0x6C, 0xAE, 0x45, 0x6E, 0x4C, 0xBB, 0x71, 0xC4, 0xDD, 0xE7, 0xBB, |
| 1123 |
0x87, 0x71, 0x57, 0xE6, 0xE5, 0x4D, 0xFE, 0xD3, 0x01, 0x5D, 0x7D, 0x4D, 0xBB, 0x22, |
| 1124 |
0x69, 0xAF, 0xCD, 0xE6, 0xA9, 0x1B, 0x8D, 0x26, 0x7E, 0xBB, 0xF8, 0x48, 0xEB, 0x95, |
| 1125 |
0xA6, 0x8E, 0x79, 0xC7, 0xAC, 0x70, 0x5E, 0x35, 0x1D, 0x54, 0x3D, 0xB0, 0x16, 0x58, |
| 1126 |
0x87, 0xD6, 0x29, 0x0F, 0xD4, 0x7A, 0x40, 0xC4, |
| 1127 |
])) |
| 1128 |
); |
| 1129 |
} |
| 1130 |
|
| 1131 |
#[test] |
| 1132 |
fn test_empty_extension_data() { |
| 1133 |
let mut parsed_auth_data: AuthenticatorData = |
| 1134 |
from_slice(&SAMPLE_AUTH_DATA_MAKE_CREDENTIAL).unwrap(); |
| 1135 |
assert!(parsed_auth_data |
| 1136 |
.flags |
| 1137 |
.contains(AuthenticatorDataFlags::EXTENSION_DATA)); |
| 1138 |
|
| 1139 |
// Remove the extension data but keep the extension data flag set. |
| 1140 |
parsed_auth_data.extensions = Default::default(); |
| 1141 |
let with_flag = to_vec(&parsed_auth_data).expect("could not serialize auth data"); |
| 1142 |
// The serialized auth data should end with an empty map (CBOR 0xA0). |
| 1143 |
assert_eq!(with_flag[with_flag.len() - 1], 0xA0); |
| 1144 |
|
| 1145 |
// Remove the extension data flag. |
| 1146 |
parsed_auth_data |
| 1147 |
.flags |
| 1148 |
.remove(AuthenticatorDataFlags::EXTENSION_DATA); |
| 1149 |
let without_flag = to_vec(&parsed_auth_data).expect("could not serialize auth data"); |
| 1150 |
// The serialized auth data should be one byte shorter. |
| 1151 |
assert!(with_flag.len() == without_flag.len() + 1); |
| 1152 |
} |
| 1153 |
|
| 1154 |
/// See: https://github.com/mozilla/authenticator-rs/issues/187 |
| 1155 |
#[test] |
| 1156 |
fn test_aaguid_output() { |
| 1157 |
let input = [ |
| 1158 |
0xcb, 0x69, 0x48, 0x1e, 0x8f, 0xf0, 0x00, 0x39, 0x93, 0xec, 0x0a, 0x27, 0x29, 0xa1, |
| 1159 |
0x54, 0xa8, |
| 1160 |
]; |
| 1161 |
let expected = "AAGuid(cb69481e-8ff0-0039-93ec-0a2729a154a8)"; |
| 1162 |
let result = AAGuid::from(&input).expect("Failed to parse AAGuid"); |
| 1163 |
let res_str = format!("{result:?}"); |
| 1164 |
assert_eq!(expected, &res_str); |
| 1165 |
} |
| 1166 |
|
| 1167 |
#[test] |
| 1168 |
fn test_ad_flags_from_bits() { |
| 1169 |
// Check that AuthenticatorDataFlags is defined on the entire u8 range and that |
| 1170 |
// `from_bits_truncate` is lossless |
| 1171 |
for x in 0..=u8::MAX { |
| 1172 |
assert_eq!( |
| 1173 |
AuthenticatorDataFlags::from_bits(x), |
| 1174 |
Some(AuthenticatorDataFlags::from_bits_truncate(x)) |
| 1175 |
); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
#[test] |
| 1180 |
fn serialize_att_obj_none() -> Result<(), serde_cbor::Error> { |
| 1181 |
let att_stmt = AttestationStatement::None; |
| 1182 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({}))}]") |
| 1183 |
assert_eq!(serde_cbor::ser::to_vec(&att_stmt)?, [0xa0]); |
| 1184 |
|
| 1185 |
let att_obj = AttestationObject { |
| 1186 |
auth_data: serde_cbor::de::from_slice(&SAMPLE_AUTH_DATA_MAKE_CREDENTIAL)?, |
| 1187 |
att_stmt, |
| 1188 |
}; |
| 1189 |
assert_eq!( |
| 1190 |
serde_cbor::ser::to_vec(&att_obj)?, |
| 1191 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"fmt": "none", "attStmt": {}, "authData": SAMPLE_AUTH_DATA_MAKE_CREDENTIAL[2:] }))}]") |
| 1192 |
[ |
| 1193 |
0xa3, 0x63, 0x66, 0x6d, 0x74, 0x64, 0x6e, 0x6f, 0x6e, 0x65, 0x67, 0x61, 0x74, 0x74, |
| 1194 |
0x53, 0x74, 0x6d, 0x74, 0xa0, 0x68, 0x61, 0x75, 0x74, 0x68, 0x44, 0x61, 0x74, 0x61, |
| 1195 |
0x58, 0xa2, 0xc2, 0x89, 0xc5, 0xca, 0x9b, 0x04, 0x60, 0xf9, 0x34, 0x6a, 0xb4, 0xe4, |
| 1196 |
0x2d, 0x84, 0x27, 0x43, 0x40, 0x4d, 0x31, 0xf4, 0x84, 0x68, 0x25, 0xa6, 0xd0, 0x65, |
| 1197 |
0xbe, 0x59, 0x7a, 0x87, 0x05, 0x1d, 0xc1, 0x00, 0x00, 0x00, 0x0b, 0xf8, 0xa0, 0x11, |
| 1198 |
0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, 0x7d, 0x00, |
| 1199 |
0x10, 0x89, 0x59, 0xce, 0xad, 0x5b, 0x5c, 0x48, 0x16, 0x4e, 0x8a, 0xbc, 0xd6, 0xd9, |
| 1200 |
0x43, 0x5c, 0x6f, 0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21, 0x58, 0x20, 0xa5, |
| 1201 |
0xfd, 0x5c, 0xe1, 0xb1, 0xc4, 0x58, 0xc5, 0x30, 0xa5, 0x4f, 0xa6, 0x1b, 0x31, 0xbf, |
| 1202 |
0x6b, 0x04, 0xbe, 0x8b, 0x97, 0xaf, 0xde, 0x54, 0xdd, 0x8c, 0xbb, 0x69, 0x27, 0x5a, |
| 1203 |
0x8a, 0x1b, 0xe1, 0x22, 0x58, 0x20, 0xfa, 0x3a, 0x32, 0x31, 0xdd, 0x9d, 0xee, 0xd9, |
| 1204 |
0xd1, 0x89, 0x7b, 0xe5, 0xa6, 0x22, 0x8c, 0x59, 0x50, 0x1e, 0x4b, 0xcd, 0x12, 0x97, |
| 1205 |
0x5d, 0x3d, 0xff, 0x73, 0x0f, 0x01, 0x27, 0x8e, 0xa6, 0x1c, 0xa1, 0x6b, 0x68, 0x6d, |
| 1206 |
0x61, 0x63, 0x2d, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0xf5 |
| 1207 |
] |
| 1208 |
); |
| 1209 |
|
| 1210 |
Ok(()) |
| 1211 |
} |
| 1212 |
|
| 1213 |
#[test] |
| 1214 |
fn serialize_att_obj_packed() -> Result<(), serde_cbor::Error> { |
| 1215 |
let att_stmt = AttestationStatement::Packed(AttestationStatementPacked { |
| 1216 |
alg: COSEAlgorithm::ES256, |
| 1217 |
sig: Signature(vec![1, 2, 3, 4]), |
| 1218 |
attestation_cert: vec![ |
| 1219 |
AttestationCertificate(vec![5, 6, 7, 8]), |
| 1220 |
AttestationCertificate(vec![9, 10, 11, 12]), |
| 1221 |
], |
| 1222 |
}); |
| 1223 |
assert_eq!( |
| 1224 |
serde_cbor::ser::to_vec(&att_stmt)?, |
| 1225 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"alg":-7, "sig":bytes([1, 2, 3, 4]), "x5c": [bytes([5, 6, 7, 8]), bytes([9, 10, 11, 12])] }))}]") |
| 1226 |
[ |
| 1227 |
0xa3, 0x63, 0x61, 0x6c, 0x67, 0x26, 0x63, 0x73, 0x69, 0x67, 0x44, 0x01, 0x02, 0x03, |
| 1228 |
0x04, 0x63, 0x78, 0x35, 0x63, 0x82, 0x44, 0x05, 0x06, 0x07, 0x08, 0x44, 0x09, 0x0a, |
| 1229 |
0x0b, 0x0c |
| 1230 |
] |
| 1231 |
); |
| 1232 |
|
| 1233 |
let att_obj = AttestationObject { |
| 1234 |
auth_data: serde_cbor::de::from_slice(&SAMPLE_AUTH_DATA_MAKE_CREDENTIAL)?, |
| 1235 |
att_stmt, |
| 1236 |
}; |
| 1237 |
assert_eq!( |
| 1238 |
serde_cbor::ser::to_vec(&att_obj)?, |
| 1239 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"fmt": "packed", "attStmt": {"alg":-7, "sig":bytes([1, 2, 3, 4]), "x5c": [bytes([5, 6, 7, 8]), bytes([9, 10, 11, 12])] }, "authData": SAMPLE_AUTH_DATA_MAKE_CREDENTIAL[2:] }))}]") |
| 1240 |
[ |
| 1241 |
0xa3, 0x63, 0x66, 0x6d, 0x74, 0x66, 0x70, 0x61, 0x63, 0x6b, 0x65, 0x64, 0x67, 0x61, |
| 1242 |
0x74, 0x74, 0x53, 0x74, 0x6d, 0x74, 0xa3, 0x63, 0x61, 0x6c, 0x67, 0x26, 0x63, 0x73, |
| 1243 |
0x69, 0x67, 0x44, 0x01, 0x02, 0x03, 0x04, 0x63, 0x78, 0x35, 0x63, 0x82, 0x44, 0x05, |
| 1244 |
0x06, 0x07, 0x08, 0x44, 0x09, 0x0a, 0x0b, 0x0c, 0x68, 0x61, 0x75, 0x74, 0x68, 0x44, |
| 1245 |
0x61, 0x74, 0x61, 0x58, 0xa2, 0xc2, 0x89, 0xc5, 0xca, 0x9b, 0x04, 0x60, 0xf9, 0x34, |
| 1246 |
0x6a, 0xb4, 0xe4, 0x2d, 0x84, 0x27, 0x43, 0x40, 0x4d, 0x31, 0xf4, 0x84, 0x68, 0x25, |
| 1247 |
0xa6, 0xd0, 0x65, 0xbe, 0x59, 0x7a, 0x87, 0x05, 0x1d, 0xc1, 0x00, 0x00, 0x00, 0x0b, |
| 1248 |
0xf8, 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11, 0x1f, 0x9e, |
| 1249 |
0xdc, 0x7d, 0x00, 0x10, 0x89, 0x59, 0xce, 0xad, 0x5b, 0x5c, 0x48, 0x16, 0x4e, 0x8a, |
| 1250 |
0xbc, 0xd6, 0xd9, 0x43, 0x5c, 0x6f, 0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21, |
| 1251 |
0x58, 0x20, 0xa5, 0xfd, 0x5c, 0xe1, 0xb1, 0xc4, 0x58, 0xc5, 0x30, 0xa5, 0x4f, 0xa6, |
| 1252 |
0x1b, 0x31, 0xbf, 0x6b, 0x04, 0xbe, 0x8b, 0x97, 0xaf, 0xde, 0x54, 0xdd, 0x8c, 0xbb, |
| 1253 |
0x69, 0x27, 0x5a, 0x8a, 0x1b, 0xe1, 0x22, 0x58, 0x20, 0xfa, 0x3a, 0x32, 0x31, 0xdd, |
| 1254 |
0x9d, 0xee, 0xd9, 0xd1, 0x89, 0x7b, 0xe5, 0xa6, 0x22, 0x8c, 0x59, 0x50, 0x1e, 0x4b, |
| 1255 |
0xcd, 0x12, 0x97, 0x5d, 0x3d, 0xff, 0x73, 0x0f, 0x01, 0x27, 0x8e, 0xa6, 0x1c, 0xa1, |
| 1256 |
0x6b, 0x68, 0x6d, 0x61, 0x63, 0x2d, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0xf5 |
| 1257 |
] |
| 1258 |
); |
| 1259 |
|
| 1260 |
Ok(()) |
| 1261 |
} |
| 1262 |
|
| 1263 |
#[test] |
| 1264 |
fn serialize_att_obj_fido_u2f() -> Result<(), serde_cbor::Error> { |
| 1265 |
let att_stmt = AttestationStatement::FidoU2F(AttestationStatementFidoU2F { |
| 1266 |
sig: Signature(vec![1, 2, 3, 4]), |
| 1267 |
attestation_cert: vec![ |
| 1268 |
AttestationCertificate(vec![5, 6, 7, 8]), |
| 1269 |
AttestationCertificate(vec![9, 10, 11, 12]), |
| 1270 |
], |
| 1271 |
}); |
| 1272 |
assert_eq!( |
| 1273 |
serde_cbor::ser::to_vec(&att_stmt)?, |
| 1274 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"x5c": [bytes([5, 6, 7, 8]), bytes([9, 10, 11, 12])], "sig": bytes([1, 2, 3, 4]) }))}]") |
| 1275 |
[ |
| 1276 |
0xa2, 0x63, 0x73, 0x69, 0x67, 0x44, 0x01, 0x02, 0x03, 0x04, 0x63, 0x78, 0x35, 0x63, |
| 1277 |
0x82, 0x44, 0x05, 0x06, 0x07, 0x08, 0x44, 0x09, 0x0a, 0x0b, 0x0c |
| 1278 |
] |
| 1279 |
); |
| 1280 |
|
| 1281 |
let att_obj = AttestationObject { |
| 1282 |
auth_data: serde_cbor::de::from_slice(&SAMPLE_AUTH_DATA_MAKE_CREDENTIAL)?, |
| 1283 |
att_stmt, |
| 1284 |
}; |
| 1285 |
assert_eq!( |
| 1286 |
serde_cbor::ser::to_vec(&att_obj)?, |
| 1287 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"fmt": "fido-u2f", "attStmt": {"x5c": [bytes([5, 6, 7, 8]), bytes([9, 10, 11, 12])], "sig": bytes([1, 2, 3, 4]) }, "authData": SAMPLE_AUTH_DATA_MAKE_CREDENTIAL[2:] }))}]") |
| 1288 |
[ |
| 1289 |
0xa3, 0x63, 0x66, 0x6d, 0x74, 0x68, 0x66, 0x69, 0x64, 0x6f, 0x2d, 0x75, 0x32, 0x66, |
| 1290 |
0x67, 0x61, 0x74, 0x74, 0x53, 0x74, 0x6d, 0x74, 0xa2, 0x63, 0x73, 0x69, 0x67, 0x44, |
| 1291 |
0x01, 0x02, 0x03, 0x04, 0x63, 0x78, 0x35, 0x63, 0x82, 0x44, 0x05, 0x06, 0x07, 0x08, |
| 1292 |
0x44, 0x09, 0x0a, 0x0b, 0x0c, 0x68, 0x61, 0x75, 0x74, 0x68, 0x44, 0x61, 0x74, 0x61, |
| 1293 |
0x58, 0xa2, 0xc2, 0x89, 0xc5, 0xca, 0x9b, 0x04, 0x60, 0xf9, 0x34, 0x6a, 0xb4, 0xe4, |
| 1294 |
0x2d, 0x84, 0x27, 0x43, 0x40, 0x4d, 0x31, 0xf4, 0x84, 0x68, 0x25, 0xa6, 0xd0, 0x65, |
| 1295 |
0xbe, 0x59, 0x7a, 0x87, 0x05, 0x1d, 0xc1, 0x00, 0x00, 0x00, 0x0b, 0xf8, 0xa0, 0x11, |
| 1296 |
0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, 0x7d, 0x00, |
| 1297 |
0x10, 0x89, 0x59, 0xce, 0xad, 0x5b, 0x5c, 0x48, 0x16, 0x4e, 0x8a, 0xbc, 0xd6, 0xd9, |
| 1298 |
0x43, 0x5c, 0x6f, 0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21, 0x58, 0x20, 0xa5, |
| 1299 |
0xfd, 0x5c, 0xe1, 0xb1, 0xc4, 0x58, 0xc5, 0x30, 0xa5, 0x4f, 0xa6, 0x1b, 0x31, 0xbf, |
| 1300 |
0x6b, 0x04, 0xbe, 0x8b, 0x97, 0xaf, 0xde, 0x54, 0xdd, 0x8c, 0xbb, 0x69, 0x27, 0x5a, |
| 1301 |
0x8a, 0x1b, 0xe1, 0x22, 0x58, 0x20, 0xfa, 0x3a, 0x32, 0x31, 0xdd, 0x9d, 0xee, 0xd9, |
| 1302 |
0xd1, 0x89, 0x7b, 0xe5, 0xa6, 0x22, 0x8c, 0x59, 0x50, 0x1e, 0x4b, 0xcd, 0x12, 0x97, |
| 1303 |
0x5d, 0x3d, 0xff, 0x73, 0x0f, 0x01, 0x27, 0x8e, 0xa6, 0x1c, 0xa1, 0x6b, 0x68, 0x6d, |
| 1304 |
0x61, 0x63, 0x2d, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0xf5 |
| 1305 |
] |
| 1306 |
); |
| 1307 |
|
| 1308 |
Ok(()) |
| 1309 |
} |
| 1310 |
|
| 1311 |
#[test] |
| 1312 |
fn serialize_att_obj_android_key() -> Result<(), serde_cbor::Error> { |
| 1313 |
let att_stmt = AttestationStatement::AndroidKey(serde_cbor::Value::Map( |
| 1314 |
vec![ |
| 1315 |
("alg".to_string().into(), serde_cbor::Value::Integer(-7)), |
| 1316 |
( |
| 1317 |
"sig".to_string().into(), |
| 1318 |
serde_cbor::Value::Bytes(vec![1, 2, 3, 4]), |
| 1319 |
), |
| 1320 |
( |
| 1321 |
"x5c".to_string().into(), |
| 1322 |
serde_cbor::Value::Array(vec![ |
| 1323 |
serde_cbor::Value::Bytes(vec![5, 6, 7, 8]), |
| 1324 |
serde_cbor::Value::Bytes(vec![9, 10, 11, 12]), |
| 1325 |
]), |
| 1326 |
), |
| 1327 |
] |
| 1328 |
.into_iter() |
| 1329 |
.collect(), |
| 1330 |
)); |
| 1331 |
assert_eq!( |
| 1332 |
serde_cbor::ser::to_vec(&att_stmt)?, |
| 1333 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"alg": -7, "sig": bytes([1, 2, 3, 4]), "x5c": [bytes([5, 6, 7, 8]), bytes([9, 10, 11, 12])] }))}]") |
| 1334 |
[ |
| 1335 |
0xa3, 0x63, 0x61, 0x6c, 0x67, 0x26, 0x63, 0x73, 0x69, 0x67, 0x44, 0x01, 0x02, 0x03, |
| 1336 |
0x04, 0x63, 0x78, 0x35, 0x63, 0x82, 0x44, 0x05, 0x06, 0x07, 0x08, 0x44, 0x09, 0x0a, |
| 1337 |
0x0b, 0x0c |
| 1338 |
] |
| 1339 |
); |
| 1340 |
|
| 1341 |
let att_obj = AttestationObject { |
| 1342 |
auth_data: serde_cbor::de::from_slice(&SAMPLE_AUTH_DATA_MAKE_CREDENTIAL)?, |
| 1343 |
att_stmt, |
| 1344 |
}; |
| 1345 |
assert_eq!( |
| 1346 |
serde_cbor::ser::to_vec(&att_obj)?, |
| 1347 |
// Python: fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({ "fmt": "android-key", "attStmt": {"alg": -7, "sig": bytes([1, 2, 3, 4]), "x5c": [bytes([5, 6, 7, 8]), bytes([9, 10, 11, 12])] }, "authData": SAMPLE_AUTH_DATA_MAKE_CREDENTIAL[2:] }))}]") |
| 1348 |
[ |
| 1349 |
0xa3, 0x63, 0x66, 0x6d, 0x74, 0x6b, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x2d, |
| 1350 |
0x6b, 0x65, 0x79, 0x67, 0x61, 0x74, 0x74, 0x53, 0x74, 0x6d, 0x74, 0xa3, 0x63, 0x61, |
| 1351 |
0x6c, 0x67, 0x26, 0x63, 0x73, 0x69, 0x67, 0x44, 0x01, 0x02, 0x03, 0x04, 0x63, 0x78, |
| 1352 |
0x35, 0x63, 0x82, 0x44, 0x05, 0x06, 0x07, 0x08, 0x44, 0x09, 0x0a, 0x0b, 0x0c, 0x68, |
| 1353 |
0x61, 0x75, 0x74, 0x68, 0x44, 0x61, 0x74, 0x61, 0x58, 0xa2, 0xc2, 0x89, 0xc5, 0xca, |
| 1354 |
0x9b, 0x04, 0x60, 0xf9, 0x34, 0x6a, 0xb4, 0xe4, 0x2d, 0x84, 0x27, 0x43, 0x40, 0x4d, |
| 1355 |
0x31, 0xf4, 0x84, 0x68, 0x25, 0xa6, 0xd0, 0x65, 0xbe, 0x59, 0x7a, 0x87, 0x05, 0x1d, |
| 1356 |
0xc1, 0x00, 0x00, 0x00, 0x0b, 0xf8, 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, |
| 1357 |
0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, 0x7d, 0x00, 0x10, 0x89, 0x59, 0xce, 0xad, 0x5b, |
| 1358 |
0x5c, 0x48, 0x16, 0x4e, 0x8a, 0xbc, 0xd6, 0xd9, 0x43, 0x5c, 0x6f, 0xa5, 0x01, 0x02, |
| 1359 |
0x03, 0x26, 0x20, 0x01, 0x21, 0x58, 0x20, 0xa5, 0xfd, 0x5c, 0xe1, 0xb1, 0xc4, 0x58, |
| 1360 |
0xc5, 0x30, 0xa5, 0x4f, 0xa6, 0x1b, 0x31, 0xbf, 0x6b, 0x04, 0xbe, 0x8b, 0x97, 0xaf, |
| 1361 |
0xde, 0x54, 0xdd, 0x8c, 0xbb, 0x69, 0x27, 0x5a, 0x8a, 0x1b, 0xe1, 0x22, 0x58, 0x20, |
| 1362 |
0xfa, 0x3a, 0x32, 0x31, 0xdd, 0x9d, 0xee, 0xd9, 0xd1, 0x89, 0x7b, 0xe5, 0xa6, 0x22, |
| 1363 |
0x8c, 0x59, 0x50, 0x1e, 0x4b, 0xcd, 0x12, 0x97, 0x5d, 0x3d, 0xff, 0x73, 0x0f, 0x01, |
| 1364 |
0x27, 0x8e, 0xa6, 0x1c, 0xa1, 0x6b, 0x68, 0x6d, 0x61, 0x63, 0x2d, 0x73, 0x65, 0x63, |
| 1365 |
0x72, 0x65, 0x74, 0xf5 |
| 1366 |
] |
| 1367 |
); |
| 1368 |
|
| 1369 |
Ok(()) |
| 1370 |
} |
| 1371 |
|
| 1372 |
#[test] |
| 1373 |
fn serialize_att_obj_android_safetynet() -> Result<(), serde_cbor::Error> { |
| 1374 |
let att_stmt = AttestationStatement::AndroidSafetyNet(serde_cbor::Value::Map( |
| 1375 |
vec![ |
| 1376 |
( |
| 1377 |
"ver".to_string().into(), |
| 1378 |
"Kom ihåg att du aldrig får snyta dig i mattan!" |
| 1379 |
.to_string() |
| 1380 |
.into(), |
| 1381 |
), |
| 1382 |
( |
| 1383 |
"response".to_string().into(), |
| 1384 |
serde_cbor::Value::Bytes(vec![1, 2, 3, 4]), |
| 1385 |
), |
| 1386 |
] |
| 1387 |
.into_iter() |
| 1388 |
.collect(), |
| 1389 |
)); |
| 1390 |
assert_eq!( |
| 1391 |
serde_cbor::ser::to_vec(&att_stmt)?, |
| 1392 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"ver": "Kom ihåg att du aldrig får snyta dig i mattan!", "response": bytes([1, 2, 3, 4]) }))}]") |
| 1393 |
[ |
| 1394 |
0xa2, 0x63, 0x76, 0x65, 0x72, 0x78, 0x30, 0x4b, 0x6f, 0x6d, 0x20, 0x69, 0x68, 0xc3, |
| 1395 |
0xa5, 0x67, 0x20, 0x61, 0x74, 0x74, 0x20, 0x64, 0x75, 0x20, 0x61, 0x6c, 0x64, 0x72, |
| 1396 |
0x69, 0x67, 0x20, 0x66, 0xc3, 0xa5, 0x72, 0x20, 0x73, 0x6e, 0x79, 0x74, 0x61, 0x20, |
| 1397 |
0x64, 0x69, 0x67, 0x20, 0x69, 0x20, 0x6d, 0x61, 0x74, 0x74, 0x61, 0x6e, 0x21, 0x68, |
| 1398 |
0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x44, 0x01, 0x02, 0x03, 0x04 |
| 1399 |
] |
| 1400 |
); |
| 1401 |
|
| 1402 |
let att_obj = AttestationObject { |
| 1403 |
auth_data: serde_cbor::de::from_slice(&SAMPLE_AUTH_DATA_MAKE_CREDENTIAL)?, |
| 1404 |
att_stmt, |
| 1405 |
}; |
| 1406 |
assert_eq!( |
| 1407 |
serde_cbor::ser::to_vec(&att_obj)?, |
| 1408 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({ "fmt": "android-safetynet", "attStmt": {"ver": "Kom ihåg att du aldrig får snyta dig i mattan!", "response": bytes([1, 2, 3, 4]) }, "authData": SAMPLE_AUTH_DATA_MAKE_CREDENTIAL[2:] }))}]") |
| 1409 |
[ |
| 1410 |
0xa3, 0x63, 0x66, 0x6d, 0x74, 0x71, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x2d, |
| 1411 |
0x73, 0x61, 0x66, 0x65, 0x74, 0x79, 0x6e, 0x65, 0x74, 0x67, 0x61, 0x74, 0x74, 0x53, |
| 1412 |
0x74, 0x6d, 0x74, 0xa2, 0x63, 0x76, 0x65, 0x72, 0x78, 0x30, 0x4b, 0x6f, 0x6d, 0x20, |
| 1413 |
0x69, 0x68, 0xc3, 0xa5, 0x67, 0x20, 0x61, 0x74, 0x74, 0x20, 0x64, 0x75, 0x20, 0x61, |
| 1414 |
0x6c, 0x64, 0x72, 0x69, 0x67, 0x20, 0x66, 0xc3, 0xa5, 0x72, 0x20, 0x73, 0x6e, 0x79, |
| 1415 |
0x74, 0x61, 0x20, 0x64, 0x69, 0x67, 0x20, 0x69, 0x20, 0x6d, 0x61, 0x74, 0x74, 0x61, |
| 1416 |
0x6e, 0x21, 0x68, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x44, 0x01, 0x02, |
| 1417 |
0x03, 0x04, 0x68, 0x61, 0x75, 0x74, 0x68, 0x44, 0x61, 0x74, 0x61, 0x58, 0xa2, 0xc2, |
| 1418 |
0x89, 0xc5, 0xca, 0x9b, 0x04, 0x60, 0xf9, 0x34, 0x6a, 0xb4, 0xe4, 0x2d, 0x84, 0x27, |
| 1419 |
0x43, 0x40, 0x4d, 0x31, 0xf4, 0x84, 0x68, 0x25, 0xa6, 0xd0, 0x65, 0xbe, 0x59, 0x7a, |
| 1420 |
0x87, 0x05, 0x1d, 0xc1, 0x00, 0x00, 0x00, 0x0b, 0xf8, 0xa0, 0x11, 0xf3, 0x8c, 0x0a, |
| 1421 |
0x4d, 0x15, 0x80, 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, 0x7d, 0x00, 0x10, 0x89, 0x59, |
| 1422 |
0xce, 0xad, 0x5b, 0x5c, 0x48, 0x16, 0x4e, 0x8a, 0xbc, 0xd6, 0xd9, 0x43, 0x5c, 0x6f, |
| 1423 |
0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21, 0x58, 0x20, 0xa5, 0xfd, 0x5c, 0xe1, |
| 1424 |
0xb1, 0xc4, 0x58, 0xc5, 0x30, 0xa5, 0x4f, 0xa6, 0x1b, 0x31, 0xbf, 0x6b, 0x04, 0xbe, |
| 1425 |
0x8b, 0x97, 0xaf, 0xde, 0x54, 0xdd, 0x8c, 0xbb, 0x69, 0x27, 0x5a, 0x8a, 0x1b, 0xe1, |
| 1426 |
0x22, 0x58, 0x20, 0xfa, 0x3a, 0x32, 0x31, 0xdd, 0x9d, 0xee, 0xd9, 0xd1, 0x89, 0x7b, |
| 1427 |
0xe5, 0xa6, 0x22, 0x8c, 0x59, 0x50, 0x1e, 0x4b, 0xcd, 0x12, 0x97, 0x5d, 0x3d, 0xff, |
| 1428 |
0x73, 0x0f, 0x01, 0x27, 0x8e, 0xa6, 0x1c, 0xa1, 0x6b, 0x68, 0x6d, 0x61, 0x63, 0x2d, |
| 1429 |
0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0xf5 |
| 1430 |
] |
| 1431 |
); |
| 1432 |
|
| 1433 |
Ok(()) |
| 1434 |
} |
| 1435 |
|
| 1436 |
#[test] |
| 1437 |
fn serialize_att_obj_apple() -> Result<(), serde_cbor::Error> { |
| 1438 |
let att_stmt = AttestationStatement::Apple(serde_cbor::Value::Map( |
| 1439 |
vec![( |
| 1440 |
"x5c".to_string().into(), |
| 1441 |
serde_cbor::Value::Array(vec![ |
| 1442 |
serde_cbor::Value::Bytes(vec![1, 2, 3, 4]), |
| 1443 |
serde_cbor::Value::Bytes(vec![5, 6, 7, 8]), |
| 1444 |
]), |
| 1445 |
)] |
| 1446 |
.into_iter() |
| 1447 |
.collect(), |
| 1448 |
)); |
| 1449 |
assert_eq!( |
| 1450 |
serde_cbor::ser::to_vec(&att_stmt)?, |
| 1451 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"x5c": [bytes([1, 2, 3, 4]), bytes([5, 6, 7, 8])] }))}]") |
| 1452 |
[ |
| 1453 |
0xa1, 0x63, 0x78, 0x35, 0x63, 0x82, 0x44, 0x01, 0x02, 0x03, 0x04, 0x44, 0x05, 0x06, |
| 1454 |
0x07, 0x08 |
| 1455 |
] |
| 1456 |
); |
| 1457 |
|
| 1458 |
let att_obj = AttestationObject { |
| 1459 |
auth_data: serde_cbor::de::from_slice(&SAMPLE_AUTH_DATA_MAKE_CREDENTIAL)?, |
| 1460 |
att_stmt, |
| 1461 |
}; |
| 1462 |
assert_eq!( |
| 1463 |
serde_cbor::ser::to_vec(&att_obj)?, |
| 1464 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"fmt": "apple", "attStmt": {"x5c": [bytes([1, 2, 3, 4]), bytes([5, 6, 7, 8])] }, "authData": SAMPLE_AUTH_DATA_MAKE_CREDENTIAL[2:] }))}]") |
| 1465 |
[ |
| 1466 |
0xa3, 0x63, 0x66, 0x6d, 0x74, 0x65, 0x61, 0x70, 0x70, 0x6c, 0x65, 0x67, 0x61, 0x74, |
| 1467 |
0x74, 0x53, 0x74, 0x6d, 0x74, 0xa1, 0x63, 0x78, 0x35, 0x63, 0x82, 0x44, 0x01, 0x02, |
| 1468 |
0x03, 0x04, 0x44, 0x05, 0x06, 0x07, 0x08, 0x68, 0x61, 0x75, 0x74, 0x68, 0x44, 0x61, |
| 1469 |
0x74, 0x61, 0x58, 0xa2, 0xc2, 0x89, 0xc5, 0xca, 0x9b, 0x04, 0x60, 0xf9, 0x34, 0x6a, |
| 1470 |
0xb4, 0xe4, 0x2d, 0x84, 0x27, 0x43, 0x40, 0x4d, 0x31, 0xf4, 0x84, 0x68, 0x25, 0xa6, |
| 1471 |
0xd0, 0x65, 0xbe, 0x59, 0x7a, 0x87, 0x05, 0x1d, 0xc1, 0x00, 0x00, 0x00, 0x0b, 0xf8, |
| 1472 |
0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11, 0x1f, 0x9e, 0xdc, |
| 1473 |
0x7d, 0x00, 0x10, 0x89, 0x59, 0xce, 0xad, 0x5b, 0x5c, 0x48, 0x16, 0x4e, 0x8a, 0xbc, |
| 1474 |
0xd6, 0xd9, 0x43, 0x5c, 0x6f, 0xa5, 0x01, 0x02, 0x03, 0x26, 0x20, 0x01, 0x21, 0x58, |
| 1475 |
0x20, 0xa5, 0xfd, 0x5c, 0xe1, 0xb1, 0xc4, 0x58, 0xc5, 0x30, 0xa5, 0x4f, 0xa6, 0x1b, |
| 1476 |
0x31, 0xbf, 0x6b, 0x04, 0xbe, 0x8b, 0x97, 0xaf, 0xde, 0x54, 0xdd, 0x8c, 0xbb, 0x69, |
| 1477 |
0x27, 0x5a, 0x8a, 0x1b, 0xe1, 0x22, 0x58, 0x20, 0xfa, 0x3a, 0x32, 0x31, 0xdd, 0x9d, |
| 1478 |
0xee, 0xd9, 0xd1, 0x89, 0x7b, 0xe5, 0xa6, 0x22, 0x8c, 0x59, 0x50, 0x1e, 0x4b, 0xcd, |
| 1479 |
0x12, 0x97, 0x5d, 0x3d, 0xff, 0x73, 0x0f, 0x01, 0x27, 0x8e, 0xa6, 0x1c, 0xa1, 0x6b, |
| 1480 |
0x68, 0x6d, 0x61, 0x63, 0x2d, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0xf5 |
| 1481 |
] |
| 1482 |
); |
| 1483 |
|
| 1484 |
Ok(()) |
| 1485 |
} |
| 1486 |
|
| 1487 |
#[test] |
| 1488 |
fn serialize_att_obj_tpm() -> Result<(), serde_cbor::Error> { |
| 1489 |
let att_stmt = AttestationStatement::Tpm(serde_cbor::Value::Map( |
| 1490 |
vec![ |
| 1491 |
("ver".to_string().into(), "2.0".to_string().into()), |
| 1492 |
("alg".to_string().into(), (-7).into()), |
| 1493 |
( |
| 1494 |
"x5c".to_string().into(), |
| 1495 |
serde_cbor::Value::Array(vec![ |
| 1496 |
serde_cbor::Value::Bytes(vec![1, 2, 3, 4]), |
| 1497 |
serde_cbor::Value::Bytes(vec![5, 6, 7, 8]), |
| 1498 |
]), |
| 1499 |
), |
| 1500 |
( |
| 1501 |
"sig".to_string().into(), |
| 1502 |
serde_cbor::Value::Bytes(vec![9, 10, 11, 12]), |
| 1503 |
), |
| 1504 |
( |
| 1505 |
"certInfo".to_string().into(), |
| 1506 |
serde_cbor::Value::Bytes(vec![13, 14, 15, 16]), |
| 1507 |
), |
| 1508 |
( |
| 1509 |
"pubArea".to_string().into(), |
| 1510 |
serde_cbor::Value::Bytes(vec![17, 18, 19, 20]), |
| 1511 |
), |
| 1512 |
] |
| 1513 |
.into_iter() |
| 1514 |
.collect(), |
| 1515 |
)); |
| 1516 |
assert_eq!( |
| 1517 |
serde_cbor::ser::to_vec(&att_stmt)?, |
| 1518 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"ver": "2.0", "alg": -7, "x5c": [bytes([1, 2, 3, 4]), bytes([5, 6, 7, 8])], "sig": bytes([9, 10, 11, 12]), "certInfo": bytes([13, 14, 15, 16]), "pubArea": bytes([17, 18, 19, 20]) }))}]") |
| 1519 |
[ |
| 1520 |
0xa6, 0x63, 0x61, 0x6c, 0x67, 0x26, 0x63, 0x73, 0x69, 0x67, 0x44, 0x09, 0x0a, 0x0b, |
| 1521 |
0x0c, 0x63, 0x76, 0x65, 0x72, 0x63, 0x32, 0x2e, 0x30, 0x63, 0x78, 0x35, 0x63, 0x82, |
| 1522 |
0x44, 0x01, 0x02, 0x03, 0x04, 0x44, 0x05, 0x06, 0x07, 0x08, 0x67, 0x70, 0x75, 0x62, |
| 1523 |
0x41, 0x72, 0x65, 0x61, 0x44, 0x11, 0x12, 0x13, 0x14, 0x68, 0x63, 0x65, 0x72, 0x74, |
| 1524 |
0x49, 0x6e, 0x66, 0x6f, 0x44, 0x0d, 0x0e, 0x0f, 0x10 |
| 1525 |
] |
| 1526 |
); |
| 1527 |
|
| 1528 |
let att_obj = AttestationObject { |
| 1529 |
auth_data: serde_cbor::de::from_slice(&SAMPLE_AUTH_DATA_MAKE_CREDENTIAL)?, |
| 1530 |
att_stmt, |
| 1531 |
}; |
| 1532 |
assert_eq!( |
| 1533 |
serde_cbor::ser::to_vec(&att_obj)?, |
| 1534 |
// Python: from fido2.cbor import encode; print(f"[{", ".join(f'0x{b:02x}' for b in encode({"fmt": "tpm", "attStmt": {"ver": "2.0", "alg": -7, "x5c": [bytes([1, 2, 3, 4]), bytes([5, 6, 7, 8])], "sig": bytes([9, 10, 11, 12]), "certInfo": bytes([13, 14, 15, 16]), "pubArea": bytes([17, 18, 19, 20]) }, "authData": SAMPLE_AUTH_DATA_MAKE_CREDENTIAL[2:] }))}]") |
| 1535 |
[ |
| 1536 |
0xa3, 0x63, 0x66, 0x6d, 0x74, 0x63, 0x74, 0x70, 0x6d, 0x67, 0x61, 0x74, 0x74, 0x53, |
| 1537 |
0x74, 0x6d, 0x74, 0xa6, 0x63, 0x61, 0x6c, 0x67, 0x26, 0x63, 0x73, 0x69, 0x67, 0x44, |
| 1538 |
0x09, 0x0a, 0x0b, 0x0c, 0x63, 0x76, 0x65, 0x72, 0x63, 0x32, 0x2e, 0x30, 0x63, 0x78, |
| 1539 |
0x35, 0x63, 0x82, 0x44, 0x01, 0x02, 0x03, 0x04, 0x44, 0x05, 0x06, 0x07, 0x08, 0x67, |
| 1540 |
0x70, 0x75, 0x62, 0x41, 0x72, 0x65, 0x61, 0x44, 0x11, 0x12, 0x13, 0x14, 0x68, 0x63, |
| 1541 |
0x65, 0x72, 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x44, 0x0d, 0x0e, 0x0f, 0x10, 0x68, 0x61, |
| 1542 |
0x75, 0x74, 0x68, 0x44, 0x61, 0x74, 0x61, 0x58, 0xa2, 0xc2, 0x89, 0xc5, 0xca, 0x9b, |
| 1543 |
0x04, 0x60, 0xf9, 0x34, 0x6a, 0xb4, 0xe4, 0x2d, 0x84, 0x27, 0x43, 0x40, 0x4d, 0x31, |
| 1544 |
0xf4, 0x84, 0x68, 0x25, 0xa6, 0xd0, 0x65, 0xbe, 0x59, 0x7a, 0x87, 0x05, 0x1d, 0xc1, |
| 1545 |
0x00, 0x00, 0x00, 0x0b, 0xf8, 0xa0, 0x11, 0xf3, 0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, |
| 1546 |
0x17, 0x11, 0x1f, 0x9e, 0xdc, 0x7d, 0x00, 0x10, 0x89, 0x59, 0xce, 0xad, 0x5b, 0x5c, |
| 1547 |
0x48, 0x16, 0x4e, 0x8a, 0xbc, 0xd6, 0xd9, 0x43, 0x5c, 0x6f, 0xa5, 0x01, 0x02, 0x03, |
| 1548 |
0x26, 0x20, 0x01, 0x21, 0x58, 0x20, 0xa5, 0xfd, 0x5c, 0xe1, 0xb1, 0xc4, 0x58, 0xc5, |
| 1549 |
0x30, 0xa5, 0x4f, 0xa6, 0x1b, 0x31, 0xbf, 0x6b, 0x04, 0xbe, 0x8b, 0x97, 0xaf, 0xde, |
| 1550 |
0x54, 0xdd, 0x8c, 0xbb, 0x69, 0x27, 0x5a, 0x8a, 0x1b, 0xe1, 0x22, 0x58, 0x20, 0xfa, |
| 1551 |
0x3a, 0x32, 0x31, 0xdd, 0x9d, 0xee, 0xd9, 0xd1, 0x89, 0x7b, 0xe5, 0xa6, 0x22, 0x8c, |
| 1552 |
0x59, 0x50, 0x1e, 0x4b, 0xcd, 0x12, 0x97, 0x5d, 0x3d, 0xff, 0x73, 0x0f, 0x01, 0x27, |
| 1553 |
0x8e, 0xa6, 0x1c, 0xa1, 0x6b, 0x68, 0x6d, 0x61, 0x63, 0x2d, 0x73, 0x65, 0x63, 0x72, |
| 1554 |
0x65, 0x74, 0xf5 |
| 1555 |
] |
| 1556 |
); |
| 1557 |
|
| 1558 |
Ok(()) |
| 1559 |
} |
| 1560 |
|
| 1561 |
mod hmac_secret { |
| 1562 |
use std::convert::TryFrom; |
| 1563 |
|
| 1564 |
use crate::{ |
| 1565 |
crypto::{ |
| 1566 |
COSEAlgorithm, COSEEC2Key, COSEKey, COSEKeyType, Curve, PinUvAuthProtocol, |
| 1567 |
SharedSecret, |
| 1568 |
}, |
| 1569 |
ctap2::{attestation::HmacSecretResponse, commands::CommandError}, |
| 1570 |
AuthenticatorInfo, |
| 1571 |
}; |
| 1572 |
|
| 1573 |
fn make_test_secret(pin_protocol: u64) -> Result<SharedSecret, CommandError> { |
| 1574 |
let fake_unused_key = COSEKey { |
| 1575 |
alg: COSEAlgorithm::ECDH_ES_HKDF256, |
| 1576 |
key: COSEKeyType::EC2(COSEEC2Key { |
| 1577 |
curve: Curve::SECP256R1, |
| 1578 |
x: vec![], |
| 1579 |
y: vec![], |
| 1580 |
}), |
| 1581 |
}; |
| 1582 |
|
| 1583 |
let pin_protocol = PinUvAuthProtocol::try_from(&AuthenticatorInfo { |
| 1584 |
pin_protocols: Some(vec![pin_protocol]), |
| 1585 |
..Default::default() |
| 1586 |
})?; |
| 1587 |
|
| 1588 |
let key = { |
| 1589 |
let aes_key = 0..32; |
| 1590 |
let hmac_key = 32..64; |
| 1591 |
match pin_protocol.id() { |
| 1592 |
1 => aes_key.collect(), |
| 1593 |
2 => hmac_key.chain(aes_key).collect(), |
| 1594 |
_ => unimplemented!(), |
| 1595 |
} |
| 1596 |
}; |
| 1597 |
|
| 1598 |
Ok(SharedSecret::new_test( |
| 1599 |
pin_protocol, |
| 1600 |
key, |
| 1601 |
fake_unused_key.clone(), |
| 1602 |
fake_unused_key, |
| 1603 |
)) |
| 1604 |
} |
| 1605 |
|
| 1606 |
#[test] |
| 1607 |
fn decrypt_confirmed_returns_none() -> Result<(), CommandError> { |
| 1608 |
let shared_secret = make_test_secret(2)?; |
| 1609 |
for flag in [true, false] { |
| 1610 |
let resp = HmacSecretResponse::Confirmed(flag); |
| 1611 |
let hmac_output = resp.decrypt_secrets(&shared_secret); |
| 1612 |
assert_eq!(hmac_output, None, "Failed for confirmed flag: {:?}", flag); |
| 1613 |
} |
| 1614 |
Ok(()) |
| 1615 |
} |
| 1616 |
|
| 1617 |
#[cfg(not(feature = "crypto_dummy"))] |
| 1618 |
mod requires_crypto { |
| 1619 |
use super::*; |
| 1620 |
|
| 1621 |
use crate::{ |
| 1622 |
crypto::CryptoError, |
| 1623 |
ctap2::{attestation::HmacSecretResponse, commands::CommandError}, |
| 1624 |
}; |
| 1625 |
|
| 1626 |
const PIN_PROTOCOL_2_IV: [u8; 16] = [0; 16]; // PIN protocol 1 uses a hard-coded all-zero IV |
| 1627 |
|
| 1628 |
/// Generated using AES key 0..32 and ciphertext 0..64: |
| 1629 |
/// ``` |
| 1630 |
/// #!/usr/bin/env python3 |
| 1631 |
/// from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
| 1632 |
/// |
| 1633 |
/// key = bytes(range(32)) |
| 1634 |
/// iv = bytes([0] * 16) |
| 1635 |
/// ciphertext = bytes(range(64)) |
| 1636 |
/// |
| 1637 |
/// cipher = Cipher(algorithms.AES256(key), modes.CBC(iv)) |
| 1638 |
/// decryptor = cipher.decryptor() |
| 1639 |
/// outputs = list(decryptor.update(ciphertext) + decryptor.finalize()) |
| 1640 |
/// EXPECTED_OUTPUT1 = outputs[0:32] |
| 1641 |
/// EXPECTED_OUTPUT2 = outputs[32:64] |
| 1642 |
/// print(EXPECTED_OUTPUT1) |
| 1643 |
/// print(EXPECTED_OUTPUT2) |
| 1644 |
/// ``` |
| 1645 |
/// Note: Using WebCrypto to generate these is impractical since they MUST NOT be padded, but WebCrypto inserts PKCS#7 padding. |
| 1646 |
const EXPECTED_OUTPUT1: [u8; 32] = [ |
| 1647 |
145, 61, 188, 229, 73, 58, 253, 192, 87, 114, 133, 138, 173, 74, 68, 50, 105, 3, |
| 1648 |
44, 7, 205, 92, 54, 139, 137, 207, 7, 105, 89, 85, 211, 130, |
| 1649 |
]; |
| 1650 |
|
| 1651 |
/// See [EXPECTED_OUTPUT1] for generation instructions |
| 1652 |
const EXPECTED_OUTPUT2: Option<[u8; 32]> = Some([ |
| 1653 |
155, 19, 88, 255, 192, 226, 50, 42, 243, 22, 42, 12, 146, 77, 108, 29, 71, 72, 149, |
| 1654 |
153, 183, 65, 182, 149, 71, 202, 57, 123, 239, 79, 94, 230, |
| 1655 |
]); |
| 1656 |
|
| 1657 |
#[test] |
| 1658 |
fn decrypt_one_secret_pin_protocol_1() -> Result<(), CommandError> { |
| 1659 |
const CT_LEN: u8 = 32; |
| 1660 |
let shared_secret = make_test_secret(1)?; |
| 1661 |
let resp = HmacSecretResponse::Secret((0..CT_LEN).collect()); |
| 1662 |
let hmac_output = resp.decrypt_secrets(&shared_secret).unwrap()?; |
| 1663 |
assert_eq!(hmac_output.output1, EXPECTED_OUTPUT1, "Incorrect output1"); |
| 1664 |
assert_eq!(hmac_output.output2, None, "Incorrect output2"); |
| 1665 |
Ok(()) |
| 1666 |
} |
| 1667 |
|
| 1668 |
#[test] |
| 1669 |
fn decrypt_two_secrets_pin_protocol_1() -> Result<(), CommandError> { |
| 1670 |
const CT_LEN: u8 = 32 * 2; |
| 1671 |
let shared_secret = make_test_secret(1)?; |
| 1672 |
let resp = HmacSecretResponse::Secret((0..CT_LEN).collect()); |
| 1673 |
let hmac_output = resp.decrypt_secrets(&shared_secret).unwrap()?; |
| 1674 |
assert_eq!(hmac_output.output1, EXPECTED_OUTPUT1, "Incorrect output1"); |
| 1675 |
assert_eq!(hmac_output.output2, EXPECTED_OUTPUT2, "Incorrect output2"); |
| 1676 |
Ok(()) |
| 1677 |
} |
| 1678 |
|
| 1679 |
#[test] |
| 1680 |
fn decrypt_one_secret_pin_protocol_2() -> Result<(), CommandError> { |
| 1681 |
const CT_LEN: u8 = 32; |
| 1682 |
let shared_secret = make_test_secret(2)?; |
| 1683 |
let resp = HmacSecretResponse::Secret( |
| 1684 |
PIN_PROTOCOL_2_IV.iter().copied().chain(0..CT_LEN).collect(), |
| 1685 |
); |
| 1686 |
let hmac_output = resp.decrypt_secrets(&shared_secret).unwrap()?; |
| 1687 |
assert_eq!(hmac_output.output1, EXPECTED_OUTPUT1, "Incorrect output1"); |
| 1688 |
assert_eq!(hmac_output.output2, None, "Incorrect output2"); |
| 1689 |
Ok(()) |
| 1690 |
} |
| 1691 |
|
| 1692 |
#[test] |
| 1693 |
fn decrypt_two_secrets_pin_protocol_2() -> Result<(), CommandError> { |
| 1694 |
const CT_LEN: u8 = 32 * 2; |
| 1695 |
let shared_secret = make_test_secret(2)?; |
| 1696 |
let resp = HmacSecretResponse::Secret( |
| 1697 |
PIN_PROTOCOL_2_IV.iter().copied().chain(0..CT_LEN).collect(), |
| 1698 |
); |
| 1699 |
let hmac_output = resp.decrypt_secrets(&shared_secret).unwrap()?; |
| 1700 |
assert_eq!(hmac_output.output1, EXPECTED_OUTPUT1, "Incorrect output1"); |
| 1701 |
assert_eq!(hmac_output.output2, EXPECTED_OUTPUT2, "Incorrect output2"); |
| 1702 |
Ok(()) |
| 1703 |
} |
| 1704 |
|
| 1705 |
#[test] |
| 1706 |
fn decrypt_wrong_length_pin_protocol_2() -> Result<(), CommandError> { |
| 1707 |
// hmac-secret output can only be multiples of 32 bytes since it operates on whole AES cipher blocks |
| 1708 |
let shared_secret = make_test_secret(2)?; |
| 1709 |
{ |
| 1710 |
// Empty cleartext |
| 1711 |
let resp = HmacSecretResponse::Secret(PIN_PROTOCOL_2_IV.to_vec()); |
| 1712 |
let hmac_output = resp.decrypt_secrets(&shared_secret).unwrap(); |
| 1713 |
assert_eq!(hmac_output, Err(CryptoError::WrongSaltLength)); |
| 1714 |
} |
| 1715 |
{ |
| 1716 |
// Too long cleartext |
| 1717 |
let resp = HmacSecretResponse::Secret( |
| 1718 |
PIN_PROTOCOL_2_IV.iter().copied().chain(0..96).collect(), |
| 1719 |
); |
| 1720 |
let hmac_output = resp.decrypt_secrets(&shared_secret).unwrap(); |
| 1721 |
assert_eq!(hmac_output, Err(CryptoError::WrongSaltLength)); |
| 1722 |
} |
| 1723 |
Ok(()) |
| 1724 |
} |
| 1725 |
} |
| 1726 |
} |
| 1727 |
} |