| 1 |
use super::{Command, CommandError, CtapResponse, PinUvAuthCommand, RequestCtap2, StatusCode}; |
| 2 |
use crate::{ |
| 3 |
crypto::{COSEKey, PinUvAuthParam, PinUvAuthToken}, |
| 4 |
ctap2::server::{ |
| 5 |
PublicKeyCredentialDescriptor, PublicKeyCredentialUserEntity, RelyingParty, RpIdHash, |
| 6 |
UserVerificationRequirement, |
| 7 |
}, |
| 8 |
errors::AuthenticatorError, |
| 9 |
transport::errors::HIDError, |
| 10 |
FidoDevice, |
| 11 |
}; |
| 12 |
use serde::{ |
| 13 |
de::{Error as SerdeError, IgnoredAny, MapAccess, Visitor}, |
| 14 |
Deserialize, Deserializer, Serialize, Serializer, |
| 15 |
}; |
| 16 |
use serde_bytes::ByteBuf; |
| 17 |
use serde_cbor::{de::from_slice, to_vec, Value}; |
| 18 |
use std::fmt; |
| 19 |
|
| 20 |
#[derive(Debug, Clone, Deserialize, Default)] |
| 21 |
struct CredManagementParams { |
| 22 |
rp_id_hash: Option<RpIdHash>, // RP ID SHA-256 hash |
| 23 |
credential_id: Option<PublicKeyCredentialDescriptor>, // Credential Identifier |
| 24 |
user: Option<PublicKeyCredentialUserEntity>, // User Entity |
| 25 |
} |
| 26 |
|
| 27 |
impl CredManagementParams { |
| 28 |
fn has_some(&self) -> bool { |
| 29 |
self.rp_id_hash.is_some() || self.credential_id.is_some() || self.user.is_some() |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl Serialize for CredManagementParams { |
| 34 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 35 |
where |
| 36 |
S: Serializer, |
| 37 |
{ |
| 38 |
serialize_map_optional!( |
| 39 |
serializer, |
| 40 |
&0x01 => self.rp_id_hash.as_ref().map(|r| ByteBuf::from(r.as_ref())), |
| 41 |
&0x02 => &self.credential_id, |
| 42 |
&0x03 => &self.user, |
| 43 |
) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
#[derive(Debug, Clone, Deserialize, Serialize)] |
| 48 |
pub(crate) enum CredManagementCommand { |
| 49 |
GetCredsMetadata, |
| 50 |
EnumerateRPsBegin, |
| 51 |
EnumerateRPsGetNextRP, |
| 52 |
EnumerateCredentialsBegin(RpIdHash), |
| 53 |
EnumerateCredentialsGetNextCredential, |
| 54 |
DeleteCredential(PublicKeyCredentialDescriptor), |
| 55 |
UpdateUserInformation((PublicKeyCredentialDescriptor, PublicKeyCredentialUserEntity)), |
| 56 |
} |
| 57 |
|
| 58 |
impl CredManagementCommand { |
| 59 |
fn to_id_and_param(&self) -> (u8, CredManagementParams) { |
| 60 |
let mut params = CredManagementParams::default(); |
| 61 |
match &self { |
| 62 |
CredManagementCommand::GetCredsMetadata => (0x01, params), |
| 63 |
CredManagementCommand::EnumerateRPsBegin => (0x02, params), |
| 64 |
CredManagementCommand::EnumerateRPsGetNextRP => (0x03, params), |
| 65 |
CredManagementCommand::EnumerateCredentialsBegin(rp_id_hash) => { |
| 66 |
params.rp_id_hash = Some(rp_id_hash.clone()); |
| 67 |
(0x04, params) |
| 68 |
} |
| 69 |
CredManagementCommand::EnumerateCredentialsGetNextCredential => (0x05, params), |
| 70 |
CredManagementCommand::DeleteCredential(cred_id) => { |
| 71 |
params.credential_id = Some(cred_id.clone()); |
| 72 |
(0x06, params) |
| 73 |
} |
| 74 |
CredManagementCommand::UpdateUserInformation((cred_id, user)) => { |
| 75 |
params.credential_id = Some(cred_id.clone()); |
| 76 |
params.user = Some(user.clone()); |
| 77 |
(0x07, params) |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
#[derive(Debug)] |
| 83 |
pub struct CredentialManagement { |
| 84 |
pub(crate) subcommand: CredManagementCommand, // subCommand currently being requested |
| 85 |
pin_uv_auth_param: Option<PinUvAuthParam>, // First 16 bytes of HMAC-SHA-256 of contents using pinUvAuthToken. |
| 86 |
use_legacy_preview: bool, |
| 87 |
} |
| 88 |
|
| 89 |
impl CredentialManagement { |
| 90 |
pub(crate) fn new(subcommand: CredManagementCommand, use_legacy_preview: bool) -> Self { |
| 91 |
Self { |
| 92 |
subcommand, |
| 93 |
pin_uv_auth_param: None, |
| 94 |
use_legacy_preview, |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
impl Serialize for CredentialManagement { |
| 100 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 101 |
where |
| 102 |
S: Serializer, |
| 103 |
{ |
| 104 |
let (id, params) = self.subcommand.to_id_and_param(); |
| 105 |
serialize_map_optional!( |
| 106 |
serializer, |
| 107 |
&0x01 => Some(&id), |
| 108 |
&0x02 => params.has_some().then_some(¶ms), |
| 109 |
&0x03 => self.pin_uv_auth_param.as_ref().map(|p| p.pin_protocol.id()), |
| 110 |
&0x04 => &self.pin_uv_auth_param, |
| 111 |
) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
#[derive(Debug, Default)] |
| 116 |
pub struct CredentialManagementResponse { |
| 117 |
/// Number of existing discoverable credentials present on the authenticator. |
| 118 |
pub existing_resident_credentials_count: Option<u64>, |
| 119 |
/// Number of maximum possible remaining discoverable credentials which can be created on the authenticator. |
| 120 |
pub max_possible_remaining_resident_credentials_count: Option<u64>, |
| 121 |
/// RP Information |
| 122 |
pub rp: Option<RelyingParty>, |
| 123 |
/// RP ID SHA-256 hash |
| 124 |
pub rp_id_hash: Option<RpIdHash>, |
| 125 |
/// Total number of RPs present on the authenticator |
| 126 |
pub total_rps: Option<u64>, |
| 127 |
/// User Information |
| 128 |
pub user: Option<PublicKeyCredentialUserEntity>, |
| 129 |
/// Credential ID |
| 130 |
pub credential_id: Option<PublicKeyCredentialDescriptor>, |
| 131 |
/// Public key of the credential. |
| 132 |
pub public_key: Option<COSEKey>, |
| 133 |
/// Total number of credentials present on the authenticator for the RP in question |
| 134 |
pub total_credentials: Option<u64>, |
| 135 |
/// Credential protection policy. |
| 136 |
pub cred_protect: Option<u64>, |
| 137 |
/// Large blob encryption key. |
| 138 |
pub large_blob_key: Option<Vec<u8>>, |
| 139 |
} |
| 140 |
|
| 141 |
impl CtapResponse for CredentialManagementResponse {} |
| 142 |
|
| 143 |
#[derive(Debug, PartialEq, Eq, Serialize)] |
| 144 |
pub struct CredentialRpListEntry { |
| 145 |
/// RP Information |
| 146 |
pub rp: RelyingParty, |
| 147 |
/// RP ID SHA-256 hash |
| 148 |
pub rp_id_hash: RpIdHash, |
| 149 |
pub credentials: Vec<CredentialListEntry>, |
| 150 |
} |
| 151 |
|
| 152 |
#[derive(Debug, PartialEq, Eq, Serialize)] |
| 153 |
pub struct CredentialListEntry { |
| 154 |
/// User Information |
| 155 |
pub user: PublicKeyCredentialUserEntity, |
| 156 |
/// Credential ID |
| 157 |
pub credential_id: PublicKeyCredentialDescriptor, |
| 158 |
/// Public key of the credential. |
| 159 |
pub public_key: COSEKey, |
| 160 |
/// Credential protection policy. |
| 161 |
pub cred_protect: u64, |
| 162 |
/// Large blob encryption key. |
| 163 |
pub large_blob_key: Option<Vec<u8>>, |
| 164 |
} |
| 165 |
|
| 166 |
#[derive(Debug, Serialize)] |
| 167 |
pub enum CredentialManagementResult { |
| 168 |
CredentialList(CredentialList), |
| 169 |
DeleteSucess, |
| 170 |
UpdateSuccess, |
| 171 |
} |
| 172 |
|
| 173 |
#[derive(Debug, Default, Serialize)] |
| 174 |
pub struct CredentialList { |
| 175 |
/// Number of existing discoverable credentials present on the authenticator. |
| 176 |
pub existing_resident_credentials_count: u64, |
| 177 |
/// Number of maximum possible remaining discoverable credentials which can be created on the authenticator. |
| 178 |
pub max_possible_remaining_resident_credentials_count: u64, |
| 179 |
/// The found credentials |
| 180 |
pub credential_list: Vec<CredentialRpListEntry>, |
| 181 |
} |
| 182 |
|
| 183 |
impl CredentialList { |
| 184 |
pub fn new() -> Self { |
| 185 |
Default::default() |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
impl<'de> Deserialize<'de> for CredentialManagementResponse { |
| 190 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 191 |
where |
| 192 |
D: Deserializer<'de>, |
| 193 |
{ |
| 194 |
struct CredentialManagementResponseVisitor; |
| 195 |
|
| 196 |
impl<'de> Visitor<'de> for CredentialManagementResponseVisitor { |
| 197 |
type Value = CredentialManagementResponse; |
| 198 |
|
| 199 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 200 |
formatter.write_str("a map") |
| 201 |
} |
| 202 |
|
| 203 |
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
| 204 |
where |
| 205 |
M: MapAccess<'de>, |
| 206 |
{ |
| 207 |
let mut existing_resident_credentials_count = None; // (0x01) Unsigned Integer Number of existing discoverable credentials present on the authenticator. |
| 208 |
let mut max_possible_remaining_resident_credentials_count = None; // (0x02) Unsigned Integer Number of maximum possible remaining discoverable credentials which can be created on the authenticator. |
| 209 |
let mut rp = None; // (0x03) PublicKeyCredentialRpEntity RP Information |
| 210 |
let mut rp_id_hash = None; // (0x04) Byte String RP ID SHA-256 hash |
| 211 |
let mut total_rps = None; // (0x05) Unsigned Integer total number of RPs present on the authenticator |
| 212 |
let mut user = None; // (0x06) PublicKeyCredentialUserEntity User Information |
| 213 |
let mut credential_id = None; // (0x07) PublicKeyCredentialDescriptor PublicKeyCredentialDescriptor |
| 214 |
let mut public_key = None; // (0x08) COSE_Key Public key of the credential. |
| 215 |
let mut total_credentials = None; // (0x09) Unsigned Integer Total number of credentials present on the authenticator for the RP in question |
| 216 |
let mut cred_protect = None; // (0x0A) Unsigned Integer Credential protection policy. |
| 217 |
let mut large_blob_key = None; // (0x0B) Byte string Large blob encryption key. |
| 218 |
|
| 219 |
while let Some(key) = map.next_key()? { |
| 220 |
match key { |
| 221 |
0x01 => { |
| 222 |
if existing_resident_credentials_count.is_some() { |
| 223 |
return Err(SerdeError::duplicate_field( |
| 224 |
"existing_resident_credentials_count", |
| 225 |
)); |
| 226 |
} |
| 227 |
existing_resident_credentials_count = Some(map.next_value()?); |
| 228 |
} |
| 229 |
0x02 => { |
| 230 |
if max_possible_remaining_resident_credentials_count.is_some() { |
| 231 |
return Err(SerdeError::duplicate_field( |
| 232 |
"max_possible_remaining_resident_credentials_count", |
| 233 |
)); |
| 234 |
} |
| 235 |
max_possible_remaining_resident_credentials_count = |
| 236 |
Some(map.next_value()?); |
| 237 |
} |
| 238 |
0x03 => { |
| 239 |
if rp.is_some() { |
| 240 |
return Err(SerdeError::duplicate_field("rp")); |
| 241 |
} |
| 242 |
rp = Some(map.next_value()?); |
| 243 |
} |
| 244 |
0x04 => { |
| 245 |
if rp_id_hash.is_some() { |
| 246 |
return Err(SerdeError::duplicate_field("rp_id_hash")); |
| 247 |
} |
| 248 |
let rp_raw = map.next_value::<ByteBuf>()?; |
| 249 |
rp_id_hash = |
| 250 |
Some(RpIdHash::from(rp_raw.as_slice()).map_err(|_| { |
| 251 |
SerdeError::invalid_length(rp_raw.len(), &"32") |
| 252 |
})?); |
| 253 |
} |
| 254 |
0x05 => { |
| 255 |
if total_rps.is_some() { |
| 256 |
return Err(SerdeError::duplicate_field("total_rps")); |
| 257 |
} |
| 258 |
total_rps = Some(map.next_value()?); |
| 259 |
} |
| 260 |
0x06 => { |
| 261 |
if user.is_some() { |
| 262 |
return Err(SerdeError::duplicate_field("user")); |
| 263 |
} |
| 264 |
user = Some(map.next_value()?); |
| 265 |
} |
| 266 |
0x07 => { |
| 267 |
if credential_id.is_some() { |
| 268 |
return Err(SerdeError::duplicate_field("credential_id")); |
| 269 |
} |
| 270 |
credential_id = Some(map.next_value()?); |
| 271 |
} |
| 272 |
0x08 => { |
| 273 |
if public_key.is_some() { |
| 274 |
return Err(SerdeError::duplicate_field("public_key")); |
| 275 |
} |
| 276 |
public_key = Some(map.next_value()?); |
| 277 |
} |
| 278 |
0x09 => { |
| 279 |
if total_credentials.is_some() { |
| 280 |
return Err(SerdeError::duplicate_field("total_credentials")); |
| 281 |
} |
| 282 |
total_credentials = Some(map.next_value()?); |
| 283 |
} |
| 284 |
0x0A => { |
| 285 |
if cred_protect.is_some() { |
| 286 |
return Err(SerdeError::duplicate_field("cred_protect")); |
| 287 |
} |
| 288 |
cred_protect = Some(map.next_value()?); |
| 289 |
} |
| 290 |
0x0B => { |
| 291 |
if large_blob_key.is_some() { |
| 292 |
return Err(SerdeError::duplicate_field("large_blob_key")); |
| 293 |
} |
| 294 |
// Using into_vec, to avoid any copy of large_blob_key |
| 295 |
large_blob_key = Some(map.next_value::<ByteBuf>()?.into_vec()); |
| 296 |
} |
| 297 |
|
| 298 |
k => { |
| 299 |
warn!("ClientPinResponse: unexpected key: {:?}", k); |
| 300 |
let _ = map.next_value::<IgnoredAny>()?; |
| 301 |
continue; |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
Ok(CredentialManagementResponse { |
| 307 |
existing_resident_credentials_count, |
| 308 |
max_possible_remaining_resident_credentials_count, |
| 309 |
rp, |
| 310 |
rp_id_hash, |
| 311 |
total_rps, |
| 312 |
user, |
| 313 |
credential_id, |
| 314 |
public_key, |
| 315 |
total_credentials, |
| 316 |
cred_protect, |
| 317 |
large_blob_key, |
| 318 |
}) |
| 319 |
} |
| 320 |
} |
| 321 |
deserializer.deserialize_bytes(CredentialManagementResponseVisitor) |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
impl RequestCtap2 for CredentialManagement { |
| 326 |
type Output = CredentialManagementResponse; |
| 327 |
|
| 328 |
fn command(&self) -> Command { |
| 329 |
if self.use_legacy_preview { |
| 330 |
Command::CredentialManagementPreview |
| 331 |
} else { |
| 332 |
Command::CredentialManagement |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
fn wire_format(&self) -> Result<Vec<u8>, HIDError> { |
| 337 |
let output = to_vec(&self).map_err(CommandError::Serializing)?; |
| 338 |
trace!("client subcommmand: {:04X?}", &output); |
| 339 |
Ok(output) |
| 340 |
} |
| 341 |
|
| 342 |
fn handle_response_ctap2<Dev>( |
| 343 |
&self, |
| 344 |
_dev: &mut Dev, |
| 345 |
input: &[u8], |
| 346 |
) -> Result<Self::Output, HIDError> |
| 347 |
where |
| 348 |
Dev: FidoDevice, |
| 349 |
{ |
| 350 |
if input.is_empty() { |
| 351 |
return Err(CommandError::InputTooSmall.into()); |
| 352 |
} |
| 353 |
|
| 354 |
let status: StatusCode = input[0].into(); |
| 355 |
|
| 356 |
if status.is_ok() { |
| 357 |
if input.len() > 1 { |
| 358 |
trace!("parsing credential management data: {:#04X?}", &input); |
| 359 |
let credential_management = |
| 360 |
from_slice(&input[1..]).map_err(CommandError::Deserializing)?; |
| 361 |
Ok(credential_management) |
| 362 |
} else { |
| 363 |
// Some subcommands return only an OK-status without any data |
| 364 |
Ok(CredentialManagementResponse::default()) |
| 365 |
} |
| 366 |
} else { |
| 367 |
let data: Option<Value> = if input.len() > 1 { |
| 368 |
Some(from_slice(&input[1..]).map_err(CommandError::Deserializing)?) |
| 369 |
} else { |
| 370 |
None |
| 371 |
}; |
| 372 |
Err(CommandError::StatusCode(status, data).into()) |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
fn send_to_virtual_device<Dev: crate::VirtualFidoDevice>( |
| 377 |
&self, |
| 378 |
_dev: &mut Dev, |
| 379 |
) -> Result<Self::Output, HIDError> { |
| 380 |
unimplemented!() |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
impl PinUvAuthCommand for CredentialManagement { |
| 385 |
fn get_rp_id(&self) -> Option<&String> { |
| 386 |
None |
| 387 |
} |
| 388 |
|
| 389 |
fn set_pin_uv_auth_param( |
| 390 |
&mut self, |
| 391 |
pin_uv_auth_token: Option<PinUvAuthToken>, |
| 392 |
) -> Result<(), AuthenticatorError> { |
| 393 |
let mut param = None; |
| 394 |
if let Some(token) = pin_uv_auth_token { |
| 395 |
// pinUvAuthParam (0x04): the result of calling |
| 396 |
// authenticate(pinUvAuthToken, uint8(subCommand) || subCommandParams). |
| 397 |
let (id, params) = self.subcommand.to_id_and_param(); |
| 398 |
let mut data = vec![id]; |
| 399 |
if params.has_some() { |
| 400 |
data.extend(to_vec(¶ms).map_err(CommandError::Serializing)?); |
| 401 |
} |
| 402 |
param = Some(token.derive(&data).map_err(CommandError::Crypto)?); |
| 403 |
} |
| 404 |
self.pin_uv_auth_param = param; |
| 405 |
Ok(()) |
| 406 |
} |
| 407 |
|
| 408 |
fn can_skip_user_verification( |
| 409 |
&mut self, |
| 410 |
_info: &crate::AuthenticatorInfo, |
| 411 |
_uv: UserVerificationRequirement, |
| 412 |
) -> bool { |
| 413 |
// "discouraged" does not exist for AuthenticatorConfig |
| 414 |
false |
| 415 |
} |
| 416 |
|
| 417 |
fn set_uv_option(&mut self, _uv: Option<bool>) { |
| 418 |
/* No-op */ |
| 419 |
} |
| 420 |
|
| 421 |
fn get_pin_uv_auth_param(&self) -> Option<&PinUvAuthParam> { |
| 422 |
self.pin_uv_auth_param.as_ref() |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
#[cfg(test)] |
| 427 |
mod test { |
| 428 |
use crate::ctap2::server::{ |
| 429 |
PublicKeyCredentialDescriptor, PublicKeyCredentialUserEntity, RelyingParty, Transport, |
| 430 |
}; |
| 431 |
|
| 432 |
use super::CredManagementParams; |
| 433 |
|
| 434 |
#[test] |
| 435 |
fn test_serialize_cred_management_params() { |
| 436 |
let cred_management_params = CredManagementParams { |
| 437 |
rp_id_hash: Some(RelyingParty::from("example.org").hash()), |
| 438 |
credential_id: Some(PublicKeyCredentialDescriptor { |
| 439 |
id: vec![1, 2, 3, 4], |
| 440 |
transports: vec![Transport::USB, Transport::NFC], |
| 441 |
}), |
| 442 |
user: Some(PublicKeyCredentialUserEntity { |
| 443 |
id: vec![5, 6, 7, 8], |
| 444 |
name: Some("testuser".to_string()), |
| 445 |
display_name: Some("Test User".to_string()), |
| 446 |
}), |
| 447 |
}; |
| 448 |
let serialized = |
| 449 |
serde_cbor::ser::to_vec(&cred_management_params).expect("Failed to serialize to CBOR"); |
| 450 |
assert_eq!( |
| 451 |
serialized, |
| 452 |
[ |
| 453 |
// Value copied from test failure output as regression test snapshot |
| 454 |
163, 1, 88, 32, 191, 171, 195, 116, 50, 149, 139, 6, 51, 96, 211, 173, 100, 97, 201, |
| 455 |
196, 115, 90, 231, 248, 237, 212, 101, 146, 165, 224, 240, 20, 82, 178, 228, 181, |
| 456 |
2, 162, 98, 105, 100, 68, 1, 2, 3, 4, 100, 116, 121, 112, 101, 106, 112, 117, 98, |
| 457 |
108, 105, 99, 45, 107, 101, 121, 3, 163, 98, 105, 100, 68, 5, 6, 7, 8, 100, 110, |
| 458 |
97, 109, 101, 104, 116, 101, 115, 116, 117, 115, 101, 114, 107, 100, 105, 115, 112, |
| 459 |
108, 97, 121, 78, 97, 109, 101, 105, 84, 101, 115, 116, 32, 85, 115, 101, 114 |
| 460 |
] |
| 461 |
); |
| 462 |
} |
| 463 |
} |