1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
use crate::Bytes;
use bitflags::bitflags;
use cosey::EcdhEsHkdf256PublicKey;
use serde_indexed::{DeserializeIndexed, SerializeIndexed};
use serde_repr::{Deserialize_repr, Serialize_repr};
#[derive(Clone, Debug, Eq, PartialEq, Serialize_repr, Deserialize_repr)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
#[repr(u8)]
pub enum PinV1Subcommand {
GetRetries = 0x01,
GetKeyAgreement = 0x02,
SetPin = 0x03,
ChangePin = 0x04,
GetPinToken = 0x05,
GetPinUvAuthTokenUsingUvWithPermissions = 0x06,
GetUVRetries = 0x07,
GetPinUvAuthTokenUsingPinWithPermissions = 0x09,
}
bitflags! {
#[derive(Default)]
pub struct Permissions: u8 {
const MAKE_CREDENTIAL = 0x01;
const GET_ASSERTION = 0x02;
const CREDENTIAL_MANAGEMENT = 0x04;
const BIO_ENROLLMENT = 0x08;
const LARGE_BLOB_WRITE = 0x10;
const AUTHENTICATOR_CONFIGURATION = 0x20;
}
}
// minimum PIN length: 4 unicode
// maximum PIN length: UTF-8 represented by <= 63 bytes
// maximum consecutive incorrect PIN attempts: 8
#[derive(Clone, Debug, Eq, PartialEq, SerializeIndexed, DeserializeIndexed)]
#[non_exhaustive]
#[serde_indexed(offset = 1)]
pub struct Request<'a> {
// 0x01
// PIN protocol version chosen by the client.
// For this version of the spec, this SHALL be the number 1.
pub pin_protocol: u8,
// 0x02
// The authenticator Client PIN sub command currently being requested
pub sub_command: PinV1Subcommand,
// 0x03
// Public key of platformKeyAgreementKey.
// Must contain "alg" parameter, must not contain any other optional parameters
#[serde(skip_serializing_if = "Option::is_none")]
pub key_agreement: Option<EcdhEsHkdf256PublicKey>,
// 0x04
// First 16 bytes of HMAC-SHA-256 of encrypted contents
// using `sharedSecret`.
#[serde(skip_serializing_if = "Option::is_none")]
pub pin_auth: Option<&'a serde_bytes::Bytes>,
// 0x05
// Encrypted new PIN using `sharedSecret`.
// (Encryption over UTF-8 representation of new PIN).
#[serde(skip_serializing_if = "Option::is_none")]
pub new_pin_enc: Option<&'a serde_bytes::Bytes>,
// 0x06
// Encrypted first 16 bytes of SHA-256 of PIN using `sharedSecret`.
#[serde(skip_serializing_if = "Option::is_none")]
pub pin_hash_enc: Option<&'a serde_bytes::Bytes>,
// 0x07
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) _placeholder07: Option<()>,
// 0x08
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) _placeholder08: Option<()>,
// 0x09
// Bitfield of permissions
#[serde(skip_serializing_if = "Option::is_none")]
pub permissions: Option<u8>,
// 0x0A
// The RP ID to assign as the permissions RP ID
#[serde(skip_serializing_if = "Option::is_none")]
pub rp_id: Option<&'a str>,
}
#[derive(Clone, Debug, Default, Eq, PartialEq, SerializeIndexed, DeserializeIndexed)]
#[non_exhaustive]
#[serde_indexed(offset = 1)]
pub struct Response {
// 0x01, like ClientPinParameters::key_agreement
#[serde(skip_serializing_if = "Option::is_none")]
pub key_agreement: Option<EcdhEsHkdf256PublicKey>,
// 0x02, encrypted `pinToken` using `sharedSecret`
#[serde(skip_serializing_if = "Option::is_none")]
pub pin_token: Option<Bytes<48>>,
// 0x03, number of PIN attempts remaining before lockout
#[serde(skip_serializing_if = "Option::is_none")]
pub retries: Option<u8>,
// 0x04, whether a power cycle is required before any future PIN operation
#[serde(skip_serializing_if = "Option::is_none")]
pub power_cycle_state: Option<bool>,
// 0x05, number of uv attempts remaining before lockout
#[serde(skip_serializing_if = "Option::is_none")]
pub uv_retries: Option<u8>,
}
#[cfg(test)]
mod tests {
use super::*;
use hex_literal::hex;
use serde_test::{assert_de_tokens, assert_ser_tokens, assert_tokens, Token};
const KEY_AGREEMENT: &[u8] = &hex!("b174bc49c7ca254b70d2e5c207cee9cf174820ebd77ea3c65508c26da51b657c1cc6b952f8621697936482da0a6d3d3826a59095daf6cd7c03e2e60385d2f6d9");
const NEW_PIN_ENC: &[u8] = &[0xde; 64];
const PIN_AUTH: &[u8] = &[0xad; 32];
const PIN_HASH_ENC: &[u8] = &[0xda; 16];
const PIN_TOKEN: &[u8] = &[0xed; 32];
#[test]
fn test_de_request_get_retries() {
let request = Request {
pin_protocol: 1,
sub_command: PinV1Subcommand::GetRetries,
key_agreement: None,
pin_auth: None,
new_pin_enc: None,
pin_hash_enc: None,
_placeholder07: None,
_placeholder08: None,
permissions: None,
rp_id: None,
};
assert_tokens(
&request,
&[
Token::Map { len: Some(2) },
// 0x01: pinProtocol
Token::U64(0x01),
Token::U8(1),
// 0x02: subCommand
Token::U64(0x02),
Token::U8(0x01),
Token::MapEnd,
],
);
}
#[test]
fn test_de_request_get_key_agreement() {
let request = Request {
pin_protocol: 1,
sub_command: PinV1Subcommand::GetKeyAgreement,
key_agreement: None,
pin_auth: None,
new_pin_enc: None,
pin_hash_enc: None,
_placeholder07: None,
_placeholder08: None,
permissions: None,
rp_id: None,
};
assert_tokens(
&request,
&[
Token::Map { len: Some(2) },
// 0x01: pinProtocol
Token::U64(0x01),
Token::U8(1),
// 0x02: subCommand
Token::U64(0x02),
Token::U8(0x02),
Token::MapEnd,
],
);
}
#[test]
fn test_de_request_set_pin() {
let key_agreement = EcdhEsHkdf256PublicKey {
x: Bytes::from_slice(&KEY_AGREEMENT[..32]).unwrap(),
y: Bytes::from_slice(&KEY_AGREEMENT[32..]).unwrap(),
};
let request = Request {
pin_protocol: 1,
sub_command: PinV1Subcommand::SetPin,
key_agreement: Some(key_agreement),
pin_auth: Some(serde_bytes::Bytes::new(PIN_AUTH)),
new_pin_enc: Some(serde_bytes::Bytes::new(NEW_PIN_ENC)),
pin_hash_enc: None,
_placeholder07: None,
_placeholder08: None,
permissions: None,
rp_id: None,
};
assert_de_tokens(
&request,
&[
Token::Map { len: Some(5) },
// 0x01: pinProtocol
Token::U64(0x01),
Token::U8(1),
// 0x02: subCommand
Token::U64(0x02),
Token::U8(0x03),
// 0x03: keyAgreement
Token::U64(0x03),
Token::Map { len: Some(5) },
// 1: kty
Token::I8(1),
Token::I8(2),
// 3: alg
Token::I8(3),
Token::I8(-25),
// -1: crv
Token::I8(-1),
Token::I8(1),
// -2: x
Token::I8(-2),
Token::BorrowedBytes(&KEY_AGREEMENT[..32]),
// -3: y
Token::I8(-3),
Token::BorrowedBytes(&KEY_AGREEMENT[32..]),
Token::MapEnd,
// 0x04: pinUvAuthParam
Token::U64(0x04),
Token::BorrowedBytes(PIN_AUTH),
// 0x05: newPinEnc
Token::U64(0x05),
Token::BorrowedBytes(NEW_PIN_ENC),
Token::MapEnd,
],
);
}
#[test]
fn test_de_request_change_pin() {
let key_agreement = EcdhEsHkdf256PublicKey {
x: Bytes::from_slice(&KEY_AGREEMENT[..32]).unwrap(),
y: Bytes::from_slice(&KEY_AGREEMENT[32..]).unwrap(),
};
let request = Request {
pin_protocol: 1,
sub_command: PinV1Subcommand::ChangePin,
key_agreement: Some(key_agreement),
pin_auth: Some(serde_bytes::Bytes::new(PIN_AUTH)),
new_pin_enc: Some(serde_bytes::Bytes::new(NEW_PIN_ENC)),
pin_hash_enc: Some(serde_bytes::Bytes::new(PIN_HASH_ENC)),
_placeholder07: None,
_placeholder08: None,
permissions: None,
rp_id: None,
};
assert_de_tokens(
&request,
&[
Token::Map { len: Some(6) },
// 0x01: pinProtocol
Token::U64(0x01),
Token::U8(1),
// 0x02: subCommand
Token::U64(0x02),
Token::U8(0x04),
// 0x03: keyAgreement
Token::U64(0x03),
Token::Map { len: Some(5) },
// 1: kty
Token::I8(1),
Token::I8(2),
// 3: alg
Token::I8(3),
Token::I8(-25),
// -1: crv
Token::I8(-1),
Token::I8(1),
// -2: x
Token::I8(-2),
Token::BorrowedBytes(&KEY_AGREEMENT[..32]),
// -3: y
Token::I8(-3),
Token::BorrowedBytes(&KEY_AGREEMENT[32..]),
Token::MapEnd,
// 0x04: pinUvAuthParam
Token::U64(0x04),
Token::BorrowedBytes(PIN_AUTH),
// 0x05: newPinEnc
Token::U64(0x05),
Token::BorrowedBytes(NEW_PIN_ENC),
// 0x06: pinHashEnc
Token::U64(0x06),
Token::BorrowedBytes(PIN_HASH_ENC),
Token::MapEnd,
],
);
}
#[test]
fn test_de_get_pin_token() {
let key_agreement = EcdhEsHkdf256PublicKey {
x: Bytes::from_slice(&KEY_AGREEMENT[..32]).unwrap(),
y: Bytes::from_slice(&KEY_AGREEMENT[32..]).unwrap(),
};
let request = Request {
pin_protocol: 1,
sub_command: PinV1Subcommand::GetPinToken,
key_agreement: Some(key_agreement),
pin_auth: None,
new_pin_enc: None,
pin_hash_enc: Some(serde_bytes::Bytes::new(PIN_HASH_ENC)),
_placeholder07: None,
_placeholder08: None,
permissions: None,
rp_id: None,
};
assert_de_tokens(
&request,
&[
Token::Map { len: Some(4) },
// 0x01: pinProtocol
Token::U64(0x01),
Token::U8(1),
// 0x02: subCommand
Token::U64(0x02),
Token::U8(0x05),
// 0x03: keyAgreement
Token::U64(0x03),
Token::Map { len: Some(5) },
// 1: kty
Token::I8(1),
Token::I8(2),
// 3: alg
Token::I8(3),
Token::I8(-25),
// -1: crv
Token::I8(-1),
Token::I8(1),
// -2: x
Token::I8(-2),
Token::BorrowedBytes(&KEY_AGREEMENT[..32]),
// -3: y
Token::I8(-3),
Token::BorrowedBytes(&KEY_AGREEMENT[32..]),
Token::MapEnd,
// 0x06: pinHashEnc
Token::U64(0x06),
Token::BorrowedBytes(PIN_HASH_ENC),
Token::MapEnd,
],
);
}
#[test]
fn test_de_get_pin_token_with_permissions() {
let key_agreement = EcdhEsHkdf256PublicKey {
x: Bytes::from_slice(&KEY_AGREEMENT[..32]).unwrap(),
y: Bytes::from_slice(&KEY_AGREEMENT[32..]).unwrap(),
};
let request = Request {
pin_protocol: 1,
sub_command: PinV1Subcommand::GetPinUvAuthTokenUsingPinWithPermissions,
key_agreement: Some(key_agreement),
pin_auth: None,
new_pin_enc: None,
pin_hash_enc: Some(serde_bytes::Bytes::new(PIN_HASH_ENC)),
_placeholder07: None,
_placeholder08: None,
permissions: Some(0x04),
rp_id: Some("example.com"),
};
assert_de_tokens(
&request,
&[
Token::Map { len: Some(6) },
// 0x01: pinProtocol
Token::U64(0x01),
Token::U8(1),
// 0x02: subCommand
Token::U64(0x02),
Token::U8(0x09),
// 0x03: keyAgreement
Token::U64(0x03),
Token::Map { len: Some(5) },
// 1: kty
Token::I8(1),
Token::I8(2),
// 3: alg
Token::I8(3),
Token::I8(-25),
// -1: crv
Token::I8(-1),
Token::I8(1),
// -2: x
Token::I8(-2),
Token::BorrowedBytes(&KEY_AGREEMENT[..32]),
// -3: y
Token::I8(-3),
Token::BorrowedBytes(&KEY_AGREEMENT[32..]),
Token::MapEnd,
// 0x06: pinHashEnc
Token::U64(0x06),
Token::BorrowedBytes(PIN_HASH_ENC),
// 0x09: permissions
Token::U64(0x09),
Token::U8(0x04),
// 0x0A: rpId
Token::U64(0x0A),
Token::BorrowedStr("example.com"),
Token::MapEnd,
],
);
}
#[test]
fn test_ser_response_get_retries() {
let response = Response {
retries: Some(3),
..Default::default()
};
assert_ser_tokens(
&response,
&[
Token::Map { len: Some(1) },
// 0x03: pinRetries
Token::U64(0x03),
Token::Some,
Token::U8(3),
Token::MapEnd,
],
);
}
#[test]
fn test_ser_response_get_key_agreement() {
let key_agreement = EcdhEsHkdf256PublicKey {
x: Bytes::from_slice(&KEY_AGREEMENT[..32]).unwrap(),
y: Bytes::from_slice(&KEY_AGREEMENT[32..]).unwrap(),
};
let response = Response {
key_agreement: Some(key_agreement),
..Default::default()
};
assert_ser_tokens(
&response,
&[
Token::Map { len: Some(1) },
// 0x01: keyAgreement
Token::U64(0x01),
Token::Some,
Token::Map { len: Some(5) },
// 1: kty
Token::I8(1),
Token::I8(2),
// 3: alg
Token::I8(3),
Token::I8(-25),
// -1: crv
Token::I8(-1),
Token::I8(1),
// -2: x
Token::I8(-2),
Token::BorrowedBytes(&KEY_AGREEMENT[..32]),
// -3: y
Token::I8(-3),
Token::BorrowedBytes(&KEY_AGREEMENT[32..]),
Token::MapEnd,
Token::MapEnd,
],
);
}
#[test]
fn test_ser_response_get_pin_token() {
let response = Response {
pin_token: Some(Bytes::from_slice(PIN_TOKEN).unwrap()),
..Default::default()
};
assert_ser_tokens(
&response,
&[
Token::Map { len: Some(1) },
// 0x02: pinAuvAuthToken
Token::U64(0x02),
Token::Some,
Token::BorrowedBytes(PIN_TOKEN),
Token::MapEnd,
],
);
}
#[test]
fn pin_v1_subcommand() {
// NB: This does *not* work without serde_repr, as the
// discriminant of a numerical enum does not have to coincide
// with its assigned value.
// E.g., for PinV1Subcommand, the first entry is set to
// value 1, but its discriminant (which our normal serialization
// to CBOR would output) is 0.
// The following test would then fail, as [1] != [2]
let mut buf = [0u8; 64];
let example = PinV1Subcommand::GetKeyAgreement;
let ser = crate::serde::cbor_serialize(&example, &mut buf).unwrap();
assert_eq!(ser, &[0x02]);
}
}