lib.rs (66687B)
1 //! [![git]](https://git.philomathiclife.com/webauthn_rp/log.html) [![crates-io]](https://crates.io/crates/webauthn_rp) [![docs-rs]](crate) 2 //! 3 //! [git]: https://git.philomathiclife.com/git_badge.svg 4 //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust 5 //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs 6 //! 7 //! `webauthn_rp` is a library for _server-side_ 8 //! [Web Authentication (WebAuthn)](https://www.w3.org/TR/webauthn-3/#sctn-rp-operations) Relying Party 9 //! (RP) operations. 10 //! 11 //! The purpose of a server-side RP library is to be modular so that any client can be used with it as a backend 12 //! _including_ native applications—WebAuthn technically only covers web applications; however it's relatively easy 13 //! to adapt to native applications as well. It achieves this by not assuming how data is sent to/from the client; 14 //! having said that, there are pre-defined serialization formats for "common" deployments which can be used when 15 //! [`serde`](#serde) is enabled. 16 //! 17 //! ## `webauthn_rp` in action 18 //! 19 //! ``` 20 //! use core::convert; 21 //! use webauthn_rp::{ 22 //! AuthenticatedCredential64, DiscoverableAuthentication64, DiscoverableAuthenticationServerState, 23 //! DiscoverableCredentialRequestOptions, CredentialCreationOptions64, RegisteredCredential64, 24 //! Registration, RegistrationServerState64, 25 //! hash::hash_set::{InsertRemoveExpired, MaxLenHashSet}, 26 //! request::{ 27 //! PublicKeyCredentialDescriptor, RpId, 28 //! auth::AuthenticationVerificationOptions, 29 //! register::{ 30 //! PublicKeyCredentialUserEntity64, RegistrationVerificationOptions, 31 //! UserHandle64, 32 //! }, 33 //! }, 34 //! response::{ 35 //! CredentialId, 36 //! auth::error::AuthCeremonyErr, 37 //! register::{CompressedPubKeyOwned, DynamicState, error::RegCeremonyErr}, 38 //! }, 39 //! }; 40 //! # #[cfg(feature = "serde")] 41 //! use serde::de::{Deserialize, Deserializer}; 42 //! # #[cfg(feature = "serde_relaxed")] 43 //! use serde_json::Error as JsonErr; 44 //! /// The RP ID our application uses. 45 //! const RP_ID: &RpId = &RpId::from_static_domain("example.com").unwrap(); 46 //! /// The registration verification options. 47 //! const REG_OPTS: &RegistrationVerificationOptions::<'static, 'static, &'static str, &'static str> = &RegistrationVerificationOptions::new(); 48 //! /// The authentication verification options. 49 //! const AUTH_OPTS: &AuthenticationVerificationOptions::<'static, 'static, &'static str, &'static str> = &AuthenticationVerificationOptions::new(); 50 //! /// Error we return in our application when a function fails. 51 //! enum AppErr { 52 //! /// WebAuthn registration ceremony failed. 53 //! RegCeremony(RegCeremonyErr), 54 //! /// WebAuthn authentication ceremony failed. 55 //! AuthCeremony(AuthCeremonyErr), 56 //! /// Unable to insert a WebAuthn ceremony. 57 //! WebAuthnCeremonyCreation, 58 //! /// WebAuthn ceremony does not exist; thus the ceremony could not be completed. 59 //! MissingWebAuthnCeremony, 60 //! /// General error related to JSON deserialization. 61 //! # #[cfg(feature = "serde_relaxed")] 62 //! Json(JsonErr), 63 //! /// No account exists associated with a particular `UserHandle64`. 64 //! NoAccount, 65 //! /// No credential exists associated with a particular `CredentialId`. 66 //! NoCredential, 67 //! /// `CredentialId` exists but the associated `UserHandle64` does not match. 68 //! CredentialUserIdMismatch, 69 //! } 70 //! # #[cfg(feature = "serde_relaxed")] 71 //! impl From<JsonErr> for AppErr { 72 //! fn from(value: JsonErr) -> Self { 73 //! Self::Json(value) 74 //! } 75 //! } 76 //! impl From<RegCeremonyErr> for AppErr { 77 //! fn from(value: RegCeremonyErr) -> Self { 78 //! Self::RegCeremony(value) 79 //! } 80 //! } 81 //! impl From<AuthCeremonyErr> for AppErr { 82 //! fn from(value: AuthCeremonyErr) -> Self { 83 //! Self::AuthCeremony(value) 84 //! } 85 //! } 86 //! /// First-time account creation. 87 //! /// 88 //! /// This gets sent from the user after an account is created on their side. The registration ceremony 89 //! /// still has to be successfully completed for the account to be created server side. In the event of an error, 90 //! /// the user should delete the created passkey since it won't be usable. 91 //! struct AccountReg { 92 //! registration: Registration, 93 //! user_name: String, 94 //! user_display_name: String, 95 //! } 96 //! # #[cfg(feature = "serde")] 97 //! impl<'de> Deserialize<'de> for AccountReg { 98 //! fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> 99 //! where 100 //! D: Deserializer<'de>, 101 //! { 102 //! // ⋮ 103 //! # panic!(""); 104 //! } 105 //! } 106 //! /// Starts account creation. 107 //! /// 108 //! /// This only makes sense for greenfield deployments since account information (e.g., user name) would likely 109 //! /// already exist otherwise. This is similar to credential creation except a random `UserHandle64` is generated and 110 //! /// will be used for subsequent credential registrations. 111 //! # #[cfg(feature = "serde_relaxed")] 112 //! fn start_account_creation( 113 //! reg_ceremonies: &mut MaxLenHashSet<RegistrationServerState64>, 114 //! ) -> Result<Vec<u8>, AppErr> { 115 //! let user_id = UserHandle64::new(); 116 //! let (server, client) = 117 //! CredentialCreationOptions64::passkey( 118 //! RP_ID, PublicKeyCredentialUserEntity64 { id: &user_id, name: "", display_name: "", }, Vec::new() 119 //! ) 120 //! .start_ceremony() 121 //! .unwrap_or_else(|_e| { 122 //! unreachable!("we don't manually mutate the options and we assume the server clock is functioning; thus this won't error") 123 //! }); 124 //! if matches!(reg_ceremonies.insert_remove_all_expired(server), InsertRemoveExpired::Success) 125 //! { 126 //! Ok(serde_json::to_vec(&client) 127 //! .unwrap_or_else(|_e| unreachable!("bug in RegistrationClientState64::serialize"))) 128 //! } else { 129 //! Err(AppErr::WebAuthnCeremonyCreation) 130 //! } 131 //! } 132 //! /// Finishes account creation. 133 //! /// 134 //! /// Pending a successful registration ceremony, a new account associated with the randomly generated 135 //! /// `UserHandle64` will be created with a corresponding passkey entry. This passkey will be used to 136 //! /// log into the application. 137 //! /// 138 //! /// Note if this errors, then the user should be notified to delete the passkey created on their 139 //! /// authenticator. 140 //! # #[cfg(feature = "serde_relaxed")] 141 //! fn finish_account_creation( 142 //! reg_ceremonies: &mut MaxLenHashSet<RegistrationServerState64>, 143 //! client_data: &[u8], 144 //! ) -> Result<(), AppErr> { 145 //! let account = serde_json::from_slice::<AccountReg>(client_data)?; 146 //! insert_account( 147 //! &account, 148 //! reg_ceremonies 149 //! // `Registration::challenge_relaxed` is available iff `serde_relaxed` is enabled. 150 //! .take(&account.registration.challenge_relaxed()?) 151 //! .ok_or(AppErr::MissingWebAuthnCeremony)? 152 //! .verify( 153 //! RP_ID, 154 //! &account.registration, 155 //! REG_OPTS, 156 //! )?, 157 //! ) 158 //! } 159 //! /// Starts passkey registration. 160 //! /// 161 //! /// This is used for _existing_ accounts where the user is already logged in and wants to register another 162 //! /// passkey. This is similar to account creation except we already have the user entity info and we need to 163 //! /// fetch the registered `PublicKeyCredentialDescriptor`s to avoid accidentally overwriting a passkey on 164 //! /// the authenticator. 165 //! # #[cfg(feature = "serde_relaxed")] 166 //! fn start_cred_registration( 167 //! user_id: &UserHandle64, 168 //! reg_ceremonies: &mut MaxLenHashSet<RegistrationServerState64>, 169 //! ) -> Result<Vec<u8>, AppErr> { 170 //! let (username, user_display_name, creds) = select_user_info(user_id)?.ok_or(AppErr::NoAccount)?; 171 //! let (server, client) = CredentialCreationOptions64::passkey(RP_ID, PublicKeyCredentialUserEntity64 { name: &username, id: user_id, display_name: &user_display_name, }, creds) 172 //! .start_ceremony() 173 //! .unwrap_or_else(|_e| { 174 //! unreachable!("we don't manually mutate the options and we assume the server clock is functioning; thus this won't error") 175 //! }); 176 //! if matches!(reg_ceremonies.insert_remove_all_expired(server), InsertRemoveExpired::Success) 177 //! { 178 //! Ok(serde_json::to_vec(&client) 179 //! .unwrap_or_else(|_e| unreachable!("bug in RegistrationClientState64::serialize"))) 180 //! } else { 181 //! Err(AppErr::WebAuthnCeremonyCreation) 182 //! } 183 //! } 184 //! /// Finishes passkey registration. 185 //! /// 186 //! /// Pending a successful registration ceremony, a new credential associated with the `UserHandle64` 187 //! /// will be created. This passkey can then be used to log into the application just like any other registered 188 //! /// passkey. 189 //! /// 190 //! /// Note if this errors, then the user should be notified to delete the passkey created on their 191 //! /// authenticator. 192 //! # #[cfg(feature = "serde_relaxed")] 193 //! fn finish_cred_registration( 194 //! reg_ceremonies: &mut MaxLenHashSet<RegistrationServerState64>, 195 //! client_data: &[u8], 196 //! ) -> Result<(), AppErr> { 197 //! // `Registration::from_json_custom` is available iff `serde_relaxed` is enabled. 198 //! let registration = Registration::from_json_custom(client_data)?; 199 //! insert_credential( 200 //! reg_ceremonies 201 //! // `Registration::challenge_relaxed` is available iff `serde_relaxed` is enabled. 202 //! .take(®istration.challenge_relaxed()?) 203 //! .ok_or(AppErr::MissingWebAuthnCeremony)? 204 //! .verify( 205 //! RP_ID, 206 //! ®istration, 207 //! REG_OPTS, 208 //! )?, 209 //! ) 210 //! } 211 //! /// Starts the passkey authentication ceremony. 212 //! # #[cfg(feature = "serde_relaxed")] 213 //! fn start_auth( 214 //! auth_ceremonies: &mut MaxLenHashSet<DiscoverableAuthenticationServerState>, 215 //! ) -> Result<Vec<u8>, AppErr> { 216 //! let (server, client) = DiscoverableCredentialRequestOptions::passkey(RP_ID) 217 //! .start_ceremony() 218 //! .unwrap_or_else(|_e| { 219 //! unreachable!("we don't manually mutate the options and we assume the server clock is functioning; thus this won't error") 220 //! }); 221 //! if matches!(auth_ceremonies.insert_remove_all_expired(server), InsertRemoveExpired::Success) 222 //! { 223 //! Ok(serde_json::to_vec(&client).unwrap_or_else(|_e| { 224 //! unreachable!("bug in DiscoverableAuthenticationClientState::serialize") 225 //! })) 226 //! } else { 227 //! Err(AppErr::WebAuthnCeremonyCreation) 228 //! } 229 //! } 230 //! /// Finishes the passkey authentication ceremony. 231 //! # #[cfg(feature = "serde_relaxed")] 232 //! fn finish_auth( 233 //! auth_ceremonies: &mut MaxLenHashSet<DiscoverableAuthenticationServerState>, 234 //! client_data: &[u8], 235 //! ) -> Result<(), AppErr> { 236 //! // `DiscoverableAuthentication64::from_json_custom` is available iff `serde_relaxed` is enabled. 237 //! let authentication = 238 //! DiscoverableAuthentication64::from_json_custom(client_data)?; 239 //! let mut cred = select_credential( 240 //! authentication.raw_id(), 241 //! authentication.response().user_handle(), 242 //! )? 243 //! .ok_or(AppErr::NoCredential)?; 244 //! if auth_ceremonies 245 //! // `DiscoverableAuthentication64::challenge_relaxed` is available iff `serde_relaxed` is enabled. 246 //! .take(&authentication.challenge_relaxed()?) 247 //! .ok_or(AppErr::MissingWebAuthnCeremony)? 248 //! .verify( 249 //! RP_ID, 250 //! &authentication, 251 //! &mut cred, 252 //! AUTH_OPTS, 253 //! )? 254 //! { 255 //! update_credential(cred.id(), cred.dynamic_state()) 256 //! } else { 257 //! Ok(()) 258 //! } 259 //! } 260 //! /// Writes `account` and `cred` to storage. 261 //! /// 262 //! /// # Errors 263 //! /// 264 //! /// Errors iff writing `account` or `cred` errors, there already exists a credential using the same 265 //! /// `CredentialId`, or there already exists an account using the same `UserHandle64`. 266 //! fn insert_account( 267 //! account: &AccountReg, 268 //! cred: RegisteredCredential64<'_>, 269 //! ) -> Result<(), AppErr> { 270 //! // ⋮ 271 //! # Ok(()) 272 //! } 273 //! /// Fetches the user info and registered credentials associated with `user_id`. 274 //! /// 275 //! /// # Errors 276 //! /// 277 //! /// Errors iff fetching the data errors. 278 //! fn select_user_info( 279 //! user_id: &UserHandle64, 280 //! ) -> Result< 281 //! Option<( 282 //! String, 283 //! String, 284 //! Vec<PublicKeyCredentialDescriptor<Box<[u8]>>>, 285 //! )>, 286 //! AppErr, 287 //! > { 288 //! // ⋮ 289 //! # Ok(None) 290 //! } 291 //! /// Writes `cred` to storage. 292 //! /// 293 //! /// # Errors 294 //! /// 295 //! /// Errors iff writing `cred` errors, there already exists a credential using the same `CredentialId`, 296 //! /// or there does not exist an account under the `UserHandle64`. 297 //! fn insert_credential( 298 //! cred: RegisteredCredential64<'_>, 299 //! ) -> Result<(), AppErr> { 300 //! // ⋮ 301 //! # Ok(()) 302 //! } 303 //! /// Fetches the `AuthenticatedCredential` associated with `cred_id` ensuring `user_id` matches the 304 //! /// `UserHandle64` associated with the account. 305 //! /// 306 //! /// # Errors 307 //! /// 308 //! /// Errors iff fetching the data errors or the `user_id` does not match the stored `UserHandle64`. 309 //! fn select_credential<'cred, 'user>( 310 //! cred_id: CredentialId<&'cred [u8]>, 311 //! user_id: &'user UserHandle64, 312 //! ) -> Result< 313 //! Option< 314 //! AuthenticatedCredential64< 315 //! 'cred, 316 //! 'user, 317 //! CompressedPubKeyOwned, 318 //! >, 319 //! >, 320 //! AppErr, 321 //! > { 322 //! // ⋮ 323 //! # Ok(None) 324 //! } 325 //! /// Overwrites the current `DynamicState` associated with `cred_id` with `dynamic_state`. 326 //! /// 327 //! /// # Errors 328 //! /// 329 //! /// Errors iff writing errors or `cred_id` does not exist. 330 //! fn update_credential( 331 //! cred_id: CredentialId<&[u8]>, 332 //! dynamic_state: DynamicState, 333 //! ) -> Result<(), AppErr> { 334 //! // ⋮ 335 //! # Ok(()) 336 //! } 337 //! ``` 338 //! 339 //! ## Cargo "features" 340 //! 341 //! [`custom`](#custom) or both [`bin`](#bin) and [`serde`](#serde) must be enabled; otherwise a [`compile_error`] 342 //! will occur. 343 //! 344 //! ### `bin` 345 //! 346 //! Enables binary (de)serialization via [`Encode`] and [`Decode`]. Since registered credentials will almost always 347 //! have to be saved to persistent storage, _some_ form of (de)serialization is necessary. In the event `bin` is 348 //! unsuitable or only partially suitable (e.g., human-readable output is desired), one will need to enable 349 //! [`custom`](#custom) to allow construction of certain types (e.g., [`AuthenticatedCredential`]). 350 //! 351 //! If possible and desired, one may wish to save the data "directly" to avoid any potential temporary allocations. 352 //! For example [`StaticState::encode`] will return a [`Vec`] containing thousands of bytes if the underlying 353 //! public key is an ML-DSA key. This additional allocation and copy of data is obviously avoided if 354 //! [`StaticState`] is stored as a [composite type](https://www.postgresql.org/docs/current/rowtypes.html) or its 355 //! fields are stored in separate columns when written to a relational database (RDB). 356 //! 357 //! ### `custom` 358 //! 359 //! Exposes functions (e.g., [`AuthenticatedCredential::new`]) that allows one to construct instances of types that 360 //! cannot be constructed when [`bin`](#bin) or [`serde`](#serde) is not enabled. 361 //! 362 //! ### `serde` 363 //! 364 //! This feature _strictly_ adheres to the JSON-motivated definitions. You _will_ encounter clients that send data 365 //! that cannot be deserialized using this feature. For many [`serde_relaxed`](#serde_relaxed) should be used 366 //! instead. 367 //! 368 //! Enables (de)serialization of data sent to/from the client via [`serde`](https://docs.rs/serde/latest/serde/) 369 //! based on the JSON-motivated definitions (e.g., 370 //! [`RegistrationResponseJSON`](https://www.w3.org/TR/webauthn-3/#dictdef-registrationresponsejson)). Since 371 //! data has to be sent to/from the client, _some_ form of (de)serialization is necessary. In the event `serde` 372 //! is unsuitable or only partially suitable, one will need to enable [`custom`](#custom) to allow construction 373 //! of certain types (e.g., [`Registration`]). 374 //! 375 //! Code is _strongly_ encouraged to rely on the [`Deserialize`] implementations as much as possible to reduce the 376 //! chances of improperly deserializing the client data. 377 //! 378 //! Note that clients are free to send data in whatever form works best, so there is no requirement the 379 //! JSON-motivated definitions are used even when JSON is sent. This is especially relevant since the JSON-motivated 380 //! definitions were only added in [WebAuthn Level 3](https://www.w3.org/TR/webauthn-3/); thus many deployments only 381 //! partially conform. Some specific deviations that may require partial customization of deserialization are the 382 //! following: 383 //! 384 //! * [`ArrayBuffer`](https://webidl.spec.whatwg.org/#idl-ArrayBuffer)s encoded using something other than 385 //! base64url. 386 //! * `ArrayBuffer`s that are encoded multiple times (including the use of different encodings each time). 387 //! * Missing fields (e.g., 388 //! [`transports`](https://www.w3.org/TR/webauthn-3/#dom-authenticatorattestationresponsejson-transports)). 389 //! * Different field names (e.g., `extensions` instead of 390 //! [`clientExtensionResults`](https://www.w3.org/TR/webauthn-3/#dom-registrationresponsejson-clientextensionresults)). 391 //! 392 //! ### `serde_relaxed` 393 //! 394 //! Automatically enables [`serde`](#serde) in addition to "relaxed" [`Deserialize`] implementations 395 //! (e.g., [`RegistrationRelaxed`]). Roughly "relaxed" translates to unknown fields being ignored and only 396 //! the fields necessary for construction of the type are required. Case still matters, duplicate fields are still 397 //! forbidden, and interrelated data validation is still performed when applicable. This can be useful when one 398 //! wants to accommodate non-conforming clients or clients that implement older versions of the spec. 399 //! 400 //! ### `serializable_server_state` 401 //! 402 //! Automatically enables [`bin`](#bin) in addition to [`Encode`] and [`Decode`] implementations for 403 //! [`RegistrationServerState`], [`DiscoverableAuthenticationServerState`], and 404 //! [`NonDiscoverableAuthenticationServerState`]. Less accurate [`SystemTime`] is used instead of [`Instant`] for 405 //! timeout enforcement. This should be enabled if you don't desire to use in-memory collections to store the instances 406 //! of those types. 407 //! 408 //! Note even when written to persistent storage, an application should still periodically remove expired ceremonies. 409 //! If one is using a relational database (RDB); then one can achieve this by storing [`SentChallenge`], 410 //! the `Vec` returned from [`Encode::encode`], and [`TimedCeremony::expiration`] and periodically remove all rows 411 //! whose expiration exceeds the current date and time. 412 //! 413 //! ## Registration and authentication 414 //! 415 //! Both [registration](https://www.w3.org/TR/webauthn-3/#registration-ceremony) and 416 //! [authentication](https://www.w3.org/TR/webauthn-3/#authentication-ceremony) ceremonies rely on "challenges", and 417 //! these challenges are inherently temporary. For this reason the data associated with challenge completion can 418 //! often be stored in memory without concern for out-of-memory (OOM) conditions. There are several benefits to 419 //! storing such data in memory: 420 //! 421 //! * No data manipulation 422 //! * By leveraging move semantics, the data sent to the client cannot be mutated once the ceremony begins. 423 //! * Improved timeout enforcement 424 //! * By ensuring the same machine that started the ceremony is also used to finish the ceremony, deviation of 425 //! system clocks is not a concern. Additionally, allowing serialization requires the use of some form of 426 //! cross-platform "timestamp" (e.g., [Unix time](https://en.wikipedia.org/wiki/Unix_time)) which differ in 427 //! implementation (e.g., platforms implement leap seconds in different ways) and are often not monotonically 428 //! increasing. If data resides in memory, a monotonic [`Instant`] can be used instead. 429 //! 430 //! It is for those reasons data like [`RegistrationServerState`] are not serializable by default and require the 431 //! use of in-memory collections (e.g., [`MaxLenHashSet`]). To better ensure OOM is not a concern, RPs should set 432 //! reasonable timeouts. Since ceremonies can only be completed by moving data (e.g., 433 //! [`RegistrationServerState::verify`]), ceremony completion is guaranteed to free up the memory used— 434 //! `RegistrationServerState` instances are as small as 48 bytes on `x86_64-unknown-linux-gnu` platforms. To avoid 435 //! issues related to incomplete ceremonies, RPs can periodically iterate the collection for expired ceremonies and 436 //! remove such data. Other techniques can be employed as well to mitigate OOM, but they are application specific 437 //! and out-of-scope. If this is undesirable, one can enable [`serializable_server_state`](#serializable_server_state) 438 //! so that `RegistrationServerState`, [`DiscoverableAuthenticationServerState`], and 439 //! [`NonDiscoverableAuthenticationServerState`] implement [`Encode`] and [`Decode`]. Another reason one may need to 440 //! store this information persistently is for load-balancing purposes where the server that started the ceremony is 441 //! not guaranteed to be the server that finishes the ceremony. 442 //! 443 //! ## Supported signature algorithms 444 //! 445 //! The only supported signature algorithms are the following: 446 //! 447 //! * ML-DSA-87 as defined in [NIST FIPS 204](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.204.pdf). This 448 //! corresponds to [`CoseAlgorithmIdentifier::Mldsa87`]. 449 //! * ML-DSA-65 as defined in [NIST FIPS 204](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.204.pdf). This 450 //! corresponds to [`CoseAlgorithmIdentifier::Mldsa65`]. 451 //! * ML-DSA-44 as defined in [NIST FIPS 204](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.204.pdf). This 452 //! corresponds to [`CoseAlgorithmIdentifier::Mldsa44`]. 453 //! * Ed25519 as defined in [RFC 8032 § 5.1](https://www.rfc-editor.org/rfc/rfc8032#section-5.1). This corresponds 454 //! to [`CoseAlgorithmIdentifier::Eddsa`]. 455 //! * ECDSA as defined in [SEC 1 Version 2.0 § 4.1](https://www.secg.org/sec1-v2.pdf#subsection.4.1) using SHA-256 456 //! as the hash function and NIST P-256 as defined in 457 //! [NIST SP 800-186 § 3.2.1.3](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-186.pdf#%5B%7B%22num%22%3A229%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C70%2C275%2C0%5D) 458 //! for the underlying elliptic curve. This corresponds to [`CoseAlgorithmIdentifier::Es256`]. 459 //! * ECDSA as defined in SEC 1 Version 2.0 § 4.1 using SHA-384 as the hash function and NIST P-384 as defined in 460 //! [NIST SP 800-186 § 3.2.1.4](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-186.pdf#%5B%7B%22num%22%3A232%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C70%2C264%2C0%5D) 461 //! for the underlying elliptic curve. This corresponds to [`CoseAlgorithmIdentifier::Es384`]. 462 //! * RSASSA-PKCS1-v1_5 as defined in [RFC 8017 § 8.2](https://www.rfc-editor.org/rfc/rfc8017#section-8.2) using 463 //! SHA-256 as the hash function. This corresponds to [`CoseAlgorithmIdentifier::Rs256`]. 464 //! 465 //! ## Correctness of code 466 //! 467 //! This library more strictly adheres to the spec than many other similar libraries including but not limited to 468 //! the following ways: 469 //! 470 //! * [CTAP2 canonical CBOR encoding form](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#ctap2-canonical-cbor-encoding-form). 471 //! * `Deserialize` implementations requiring _exact_ conformance (e.g., not allowing unknown data). 472 //! * More thorough interrelated data validation (e.g., all places a Credential ID exists must match). 473 //! * Implement a lot of recommended (i.e., SHOULD) criteria. 474 //! 475 //! Unfortunately like almost all software, this library has not been formally verified; however great care is 476 //! employed in the following ways: 477 //! 478 //! * Leverage move semantics to prevent mutation of data once in a static state. 479 //! * Ensure a great many invariants via types. 480 //! * Reduce code duplication. 481 //! * Reduce variable mutation allowing for simpler algebraic reasoning. 482 //! * `panic`-free code[^note] (i.e., define true/total functions). 483 //! * Ensure arithmetic "side effects" don't occur (e.g., overflow). 484 //! * Aggressive use of compiler and [Clippy](https://doc.rust-lang.org/stable/clippy/lints.html) lints. 485 //! * Unit tests for common cases, edge cases, and error cases. 486 //! 487 //! ## Cryptographic libraries 488 //! 489 //! This library does not rely on _any_ sensitive data (e.g., private keys) as only signature verification is 490 //! ever performed. This means that the only thing that matters with the libraries used is their algorithmic 491 //! correctness and not other normally essential aspects like susceptibility to side-channel attacks. While I 492 //! personally believe the libraries that are used are at least as "secure" as alternatives even when dealing with 493 //! sensitive data, one only needs to audit the correctness of the libraries to be confident in their use. In fact 494 //! [`curve25519_dalek`](https://docs.rs/curve25519-dalek/latest/curve25519_dalek/#backends) has been formally 495 //! verified when the [`fiat`](https://github.com/mit-plv/fiat-crypto) backend is used making it _objectively_ 496 //! better than many other libraries whose correctness has not been proven. Two additional benefits of the library 497 //! choices are simpler APIs making it more likely their use is correct and better cross-platform compatibility. 498 //! 499 //! [^note]: `panic`s related to memory allocations or stack overflow are possible since such issues are not 500 //! formally guarded against. 501 #![expect( 502 clippy::multiple_crate_versions, 503 reason = "RustCrypto hasn't updated rand yet" 504 )] 505 #![expect( 506 clippy::doc_paragraphs_missing_punctuation, 507 reason = "false positive for crate documentation having image links" 508 )] 509 #![cfg_attr(docsrs, feature(doc_cfg))] 510 //#[cfg(not(any(feature = "custom", all(feature = "bin", feature = "serde"))))] 511 //compile_error!("'custom' must be enabled or both 'bin' and 'serde' must be enabled"); 512 #[cfg(all(doc, feature = "serde"))] 513 use crate::request::register::ser::{ 514 PublicKeyCredentialCreationOptionsOwned, PublicKeyCredentialUserEntityOwned, 515 }; 516 #[cfg(feature = "serde")] 517 use crate::request::register::ser::{ 518 PublicKeyCredentialCreationOptionsOwnedErr, PublicKeyCredentialUserEntityOwnedErr, 519 }; 520 #[cfg(feature = "serializable_server_state")] 521 use crate::request::{ 522 auth::ser_server_state::{ 523 DecodeDiscoverableAuthenticationServerStateErr, 524 DecodeNonDiscoverableAuthenticationServerStateErr, 525 EncodeNonDiscoverableAuthenticationServerStateErr, 526 }, 527 register::ser_server_state::DecodeRegistrationServerStateErr, 528 }; 529 #[cfg(any(feature = "bin", feature = "custom"))] 530 use crate::response::error::CredentialIdErr; 531 #[cfg(feature = "serde_relaxed")] 532 use crate::response::ser_relaxed::SerdeJsonErr; 533 #[cfg(feature = "bin")] 534 use crate::response::{ 535 bin::DecodeAuthTransportsErr, 536 register::bin::{DecodeDynamicStateErr, DecodeStaticStateErr}, 537 }; 538 #[cfg(doc)] 539 use crate::{ 540 hash::hash_set::MaxLenHashSet, 541 request::{ 542 AsciiDomain, DomainOrigin, Port, PublicKeyCredentialDescriptor, RpId, Scheme, 543 TimedCeremony, Url, 544 auth::{AllowedCredential, AllowedCredentials, PublicKeyCredentialRequestOptions}, 545 register::{ 546 CoseAlgorithmIdentifier, PublicKeyCredentialCreationOptions, 547 PublicKeyCredentialUserEntity, UserHandle16, UserHandle64, 548 }, 549 }, 550 response::{ 551 CollectedClientData, Flag, SentChallenge, 552 auth::{self, Authentication, DiscoverableAuthenticatorAssertion}, 553 register::{ 554 self, Aaguid, Attestation, AttestationObject, AttestedCredentialData, 555 AuthenticatorExtensionOutput, ClientExtensionsOutputs, CompressedPubKey, 556 CredentialPropertiesOutput, 557 }, 558 }, 559 }; 560 use crate::{ 561 request::{ 562 auth::error::{InvalidTimeout, NonDiscoverableCredentialRequestOptionsErr}, 563 error::{AsciiDomainErr, DomainOriginParseErr, PortParseErr, SchemeParseErr, UrlErr}, 564 register::{ 565 ResidentKeyRequirement, USER_HANDLE_MAX_LEN, UserHandle, error::CreationOptionsErr, 566 }, 567 }, 568 response::{ 569 AuthTransports, CredentialId, 570 auth::error::{AuthCeremonyErr, AuthenticatorDataErr as AuthAuthDataErr}, 571 error::CollectedClientDataErr, 572 register::{ 573 CredentialProtectionPolicy, DynamicState, Metadata, StaticState, UncompressedPubKey, 574 error::{ 575 AaguidErr, AttestationObjectErr, AuthenticatorDataErr as RegAuthDataErr, 576 RegCeremonyErr, 577 }, 578 }, 579 }, 580 }; 581 #[cfg(all(doc, feature = "bin"))] 582 use bin::{Decode, Encode}; 583 #[cfg(doc)] 584 use core::str::FromStr; 585 use core::{ 586 convert, 587 error::Error, 588 fmt::{self, Display, Formatter}, 589 ops::Not, 590 }; 591 #[cfg(all(doc, feature = "serde_relaxed"))] 592 use response::register::ser_relaxed::RegistrationRelaxed; 593 #[cfg(all(doc, feature = "serde"))] 594 use serde::Deserialize; 595 #[cfg(all(doc, feature = "serde_relaxed"))] 596 use serde_json::de::{Deserializer, StreamDeserializer}; 597 #[cfg(feature = "serializable_server_state")] 598 use std::time::SystemTimeError; 599 #[cfg(doc)] 600 use std::time::{Instant, SystemTime}; 601 /// Contains functionality to (de)serialize data to a data store. 602 #[cfg(feature = "bin")] 603 pub mod bin; 604 /// Contains functionality for maximum-length hash maps and sets that allocate exactly once. 605 pub mod hash; 606 /// Functionality for starting ceremonies. 607 /// 608 /// # What kind of credential should I create? 609 /// 610 /// Without partitioning the possibilities _too_ much, the following are possible authentication flows: 611 /// 612 /// | Label | Username | Password | Client-side credential | Authenticator-side user verification | Recommended | 613 /// |-------|----------|----------|------------------------|--------------------------------------|:-----------:| 614 /// | 1 | Yes | Yes | Required | Yes | ❌ | 615 /// | 2 | Yes | Yes | Required | No | ❌ | 616 /// | 3 | Yes | Yes | Optional | Yes | ❌ | 617 /// | <a name="label4">4</a> | Yes | Yes | Optional | No | ✅ | 618 /// | 5 | Yes | No | Required | Yes | ❌ | 619 /// | 6 | Yes | No | Required | No | ❌ | 620 /// | <a name="label7">7</a> | Yes | No | Optional | Yes | ❔ | 621 /// | 8 | Yes | No | Optional | No | ❌ | 622 /// | 9 | No | Yes | Required | Yes | ❌ | 623 /// | 10 | No | Yes | Required | No | ❌ | 624 /// | 11 | No | Yes | Optional | Yes | ❌ | 625 /// | 12 | No | Yes | Optional | No | ❌ | 626 /// | <a name="label13">13</a> | No | No | Required | Yes | ✅ | 627 /// | 14 | No | No | Required | No | ❌ | 628 /// | 15 | No | No | Optional | Yes | ❌ | 629 /// | 16 | No | No | Optional | No | ❌ | 630 /// 631 /// * All `Label`s with both `Password` and `Authenticator-side user verification` set to `Yes` are not recommended 632 /// since the verification done on the authenticator is likely the same "factor" as a password; thus it does not 633 /// add benefit but only serves as an annoyance to users. 634 /// * All `Label`s with `Username` or `Password` set to `Yes` and `Client-side credential` set to `Required` are not 635 /// recommended since you may preclude authenticators that are storage constrained (e.g., security keys). 636 /// * All `Label`s with `Username` set to `No` and `Client-side credential` set to `Optional` are not possible since 637 /// RPs would not have a way to identify the set of encrypted credentials to pass to the unknown user. 638 /// * All `Label`s with `Password` and `Authenticator-side user verification` set to `No` are not recommended since 639 /// those are single-factor authentication schemes; thus anyone possessing the credential without also passing 640 /// some form of user verification (e.g., password) would authenticate. 641 /// * [`Label 7`](#label7) is possible for RPs that are comfortable passing an encrypted credential to a potential user 642 /// without having that user first pass another form of authentication. For many RPs passing such information even 643 /// if encrypted is not desirable though. 644 /// * [`Label 4`](#label4) is ideal as a single-factor flow incorporated within a wider multi-factor authentication (MFA) 645 /// setup. The easiest way to register such a credential is with 646 /// [`CredentialCreationOptions::second_factor`]. 647 /// * [`Label 13`](#label13) is ideal for passkey setups as it allows for pleasant UX where a user does not have to type a 648 /// username nor password while still being secured with MFA with one of the factors being based on public-key 649 /// cryptography which for many is the most secure form of single-factor authentication. The easiest way to register 650 /// such a credential is with [`CredentialCreationOptions::passkey`]. 651 /// 652 /// Two other reasons one may prefer to construct client-side credentials is richer support for extensions (e.g., 653 /// [`largeBlobKey`](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#sctn-largeBlobKey-extension) 654 /// for CTAP 2.2 authenticators) and the ability to use both discoverable and nondiscoverable requests. The former is not 655 /// relevant for this library—at least currently—since the only extensions supported are applicable for both 656 /// client-side and server-side credentials. The latter can be important especially if an RP wants the ability to 657 /// seamlessly transition from a username and password scheme to a userless and passwordless one in the future. 658 /// 659 /// Note the table is purely informative. While helper functions 660 /// (e.g., [`CredentialCreationOptions::passkey`]) only exist for [`Label 4`](#label4) and 661 /// [`Label 13`](#label13), one can create any credential since all fields in [`CredentialCreationOptions`] 662 /// and [`PublicKeyCredentialRequestOptions`] are accessible. 663 pub mod request; 664 /// Functionality for completing ceremonies. 665 /// 666 /// Read [`request`] for more information about what credentials one should create. 667 pub mod response; 668 #[doc(inline)] 669 pub use crate::{ 670 request::{ 671 auth::{ 672 DiscoverableAuthenticationClientState, DiscoverableAuthenticationServerState, 673 DiscoverableCredentialRequestOptions, NonDiscoverableAuthenticationClientState, 674 NonDiscoverableAuthenticationServerState, NonDiscoverableCredentialRequestOptions, 675 }, 676 register::{ 677 CredentialCreationOptions, CredentialCreationOptions16, CredentialCreationOptions64, 678 RegistrationClientState, RegistrationClientState16, RegistrationClientState64, 679 RegistrationServerState, RegistrationServerState16, RegistrationServerState64, 680 }, 681 }, 682 response::{ 683 auth::{ 684 DiscoverableAuthentication, DiscoverableAuthentication16, DiscoverableAuthentication64, 685 NonDiscoverableAuthentication, NonDiscoverableAuthentication16, 686 NonDiscoverableAuthentication64, 687 }, 688 register::Registration, 689 }, 690 }; 691 /// Error returned in [`RegCeremonyErr::Credential`] and [`AuthenticatedCredential::new`]. 692 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 693 pub enum CredentialErr { 694 /// Variant when [`CredentialProtectionPolicy::UserVerificationRequired`], but 695 /// [`DynamicState::user_verified`] is `false`. 696 CredProtectUserVerificationRequiredWithoutUserVerified, 697 /// Variant when [`AuthenticatorExtensionOutput::hmac_secret`] is `Some(true)` and 698 /// [`DynamicState::user_verified`] is `false`. 699 HmacSecretWithoutUserVerified, 700 /// Variant when [`AuthenticatorExtensionOutput::hmac_secret`] is `Some(true)`, but 701 /// [`ClientExtensionsOutputs::prf`] is `Some(AuthenticationExtensionsPRFOutputs { enabled: false })` 702 /// or `AuthenticatorExtensionOutput::hmac_secret` is `Some`, but 703 /// `ClientExtensionsOutputs::prf` is `None`. 704 HmacSecretWithoutPrf, 705 /// Variant when [`ClientExtensionsOutputs::prf`] is 706 /// `Some(AuthenticationExtensionsPRFOutputs { enabled: true })`, but 707 /// [`AuthenticatorExtensionOutput::hmac_secret`] is `Some(false)`. 708 PrfWithoutHmacSecret, 709 /// Variant when [`ResidentKeyRequirement::Required`] was sent, but 710 /// [`CredentialPropertiesOutput::rk`] is `Some(false)`. 711 ResidentKeyRequiredServerCredentialCreated, 712 } 713 impl Display for CredentialErr { 714 #[inline] 715 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 716 f.write_str(match *self { 717 Self::CredProtectUserVerificationRequiredWithoutUserVerified => { 718 "credProtect requires user verification, but the user is not verified" 719 } 720 Self::HmacSecretWithoutUserVerified => { 721 "hmac-secret was enabled, but the user is not verified" 722 } 723 Self::HmacSecretWithoutPrf => "hmac-secret was enabled but prf was not", 724 Self::PrfWithoutHmacSecret => "prf was enabled, but hmac-secret was not", 725 Self::ResidentKeyRequiredServerCredentialCreated => { 726 "server-side credential was created, but a client-side credential is required" 727 } 728 }) 729 } 730 } 731 impl Error for CredentialErr {} 732 /// Checks if the `static_state` and `dynamic_state` are valid. 733 /// 734 /// # Errors 735 /// 736 /// Errors iff `static_state` or `dynamc_state` are invalid. 737 fn verify_static_and_dynamic_state<T>( 738 static_state: &StaticState<T>, 739 dynamic_state: DynamicState, 740 ) -> Result<(), CredentialErr> { 741 if dynamic_state.user_verified { 742 Ok(()) 743 } else if matches!( 744 static_state.extensions.cred_protect, 745 CredentialProtectionPolicy::UserVerificationRequired 746 ) { 747 Err(CredentialErr::CredProtectUserVerificationRequiredWithoutUserVerified) 748 } else if static_state 749 .extensions 750 .hmac_secret 751 .is_some_and(convert::identity) 752 { 753 Err(CredentialErr::HmacSecretWithoutUserVerified) 754 } else { 755 Ok(()) 756 } 757 .and_then(|()| { 758 static_state.client_extension_results.prf.map_or_else( 759 || { 760 if static_state.extensions.hmac_secret.is_none() { 761 Ok(()) 762 } else { 763 Err(CredentialErr::HmacSecretWithoutPrf) 764 } 765 }, 766 |prf| { 767 if prf.enabled { 768 if static_state.extensions.hmac_secret.is_some_and(Not::not) { 769 Err(CredentialErr::PrfWithoutHmacSecret) 770 } else { 771 Ok(()) 772 } 773 } else if static_state 774 .extensions 775 .hmac_secret 776 .is_some_and(convert::identity) 777 { 778 Err(CredentialErr::HmacSecretWithoutPrf) 779 } else { 780 Ok(()) 781 } 782 }, 783 ) 784 }) 785 } 786 /// Registered credential that needs to be saved server-side to perform future 787 /// [authentication ceremonies](https://www.w3.org/TR/webauthn-3/#authentication-ceremony) with 788 /// [`AuthenticatedCredential`]. 789 /// 790 /// When saving `RegisteredCredential` to persistent storage, one will almost always want to save the contained data 791 /// separately. The reasons for this are the following: 792 /// 793 /// * [`CredentialId`] 794 /// * MUST be globally unique, and it will likely be easier to enforce such uniqueness when it's separate. 795 /// * Fetching the [`AuthenticatedCredential`] by [`Authentication::raw_id`] when completing the 796 /// authentication ceremony via [`DiscoverableAuthenticationServerState::verify`] or 797 /// [`NonDiscoverableAuthenticationServerState::verify`] will likely be easier than alternatives. 798 /// * [`AuthTransports`] 799 /// * Fetching [`CredentialId`]s and associated `AuthTransports` by [`UserHandle`] will likely make credential 800 /// registration easier since one should set [`PublicKeyCredentialCreationOptions::exclude_credentials`] to 801 /// the [`PublicKeyCredentialDescriptor`]s belonging to a `UserHandle` in order to avoid accidentally 802 /// overwriting an existing credential on the authenticator. 803 /// * Fetching `CredentialId`s and associated `AuthTransports` by `UserHandle` will likely make starting 804 /// authentication ceremonies easier for [`NonDiscoverableCredentialRequestOptions`]. 805 /// * [`UserHandle`] 806 /// * Fetching the [`AuthenticatedCredential`] by [`DiscoverableAuthentication::raw_id`] must also coincide with 807 /// verifying the associated `UserHandle` matches [`DiscoverableAuthenticatorAssertion::user_handle`]. 808 /// * Fetching [`CredentialId`]s and associated [`AuthTransports`] by `UserHandle` will likely make credential 809 /// registration easier since one should set [`PublicKeyCredentialCreationOptions::exclude_credentials`] to 810 /// the [`PublicKeyCredentialDescriptor`]s belonging to a `UserHandle` in order to avoid accidentally 811 /// overwriting an existing credential on the authenticator. 812 /// * Fetching `CredentialId`s and associated `AuthTransports` by `UserHandle` will likely make starting 813 /// authentication ceremonies easier for [`NonDiscoverableCredentialRequestOptions`]. 814 /// * [`DynamicState`] 815 /// * `DynamicState` is the only part that is ever updated after a successful authentication ceremony 816 /// via [`DiscoverableAuthenticationServerState::verify`] or 817 /// [`NonDiscoverableAuthenticationServerState::verify`]. It being separate allows for smaller and quicker 818 /// updates. 819 /// * [`Metadata`] 820 /// * Informative data that is never used during authentication ceremonies; consequently, one may wish to 821 /// not even save this information. 822 /// * [`StaticState`] 823 /// * All other data exists as part of `StaticState`. 824 /// 825 /// It is for those reasons that `RegisteredCredential` does not implement [`Encode`] or [`Decode`]; instead its parts 826 /// do. 827 /// 828 /// Note that [`RpId`] and user information other than the `UserHandle` are not stored in `RegisteredCredential`. 829 /// RPs that wish to store such information must do so on their own. Since user information is likely the same 830 /// for a given `UserHandle` and `RpId` is likely static, it makes little sense to store such information 831 /// automatically. 832 /// 833 /// When registering a credential, [`AttestedCredentialData::aaguid`], [`AttestedCredentialData::credential_id`], 834 /// and [`AttestedCredentialData::credential_public_key`] will be the sources for [`Metadata::aaguid`], 835 /// [`Self::id`], and [`StaticState::credential_public_key`] respectively. The [`PublicKeyCredentialUserEntity::id`] 836 /// associated with the [`CredentialCreationOptions`] used to create the `RegisteredCredential` via 837 /// [`RegistrationServerState::verify`] will be the source for [`Self::user_id`]. 838 /// 839 /// The only way to create this is via `RegistrationServerState::verify`. 840 #[derive(Debug)] 841 pub struct RegisteredCredential<'reg, const USER_LEN: usize> { 842 /// The credential ID. 843 /// 844 /// For client-side credentials, this is a unique identifier; but for server-side 845 /// credentials, this _is_ the credential (i.e., the encrypted private key and necessary information). 846 id: CredentialId<&'reg [u8]>, 847 /// Hints for how the client might communicate with the authenticator containing the credential. 848 transports: AuthTransports, 849 /// The identifier for the user. 850 /// 851 /// Unlike [`Self::id`] which is globally unique for an RP, this is unique up to "user" (i.e., 852 /// multiple [`CredentialId`]s will often exist for the same `UserHandle`). 853 user_id: UserHandle<USER_LEN>, 854 /// Immutable state returned during registration. 855 static_state: StaticState<UncompressedPubKey<'reg>>, 856 /// State that can change during authentication ceremonies. 857 dynamic_state: DynamicState, 858 /// Metadata. 859 metadata: Metadata<'reg>, 860 } 861 impl<'reg, const USER_LEN: usize> RegisteredCredential<'reg, USER_LEN> { 862 /// The credential ID. 863 /// 864 /// For client-side credentials, this is a unique identifier; but for server-side 865 /// credentials, this _is_ the credential (i.e., the encrypted private key and necessary information). 866 #[inline] 867 #[must_use] 868 pub const fn id(&self) -> CredentialId<&'reg [u8]> { 869 self.id 870 } 871 /// Hints for how the client might communicate with the authenticator containing the credential. 872 #[inline] 873 #[must_use] 874 pub const fn transports(&self) -> AuthTransports { 875 self.transports 876 } 877 /// The identifier for the user. 878 /// 879 /// Unlike [`Self::id`] which is globally unique for an RP, this is unique up to "user" (i.e., 880 /// multiple [`CredentialId`]s will often exist for the same `UserHandle`). 881 #[inline] 882 #[must_use] 883 pub const fn user_id(&self) -> &UserHandle<USER_LEN> { 884 &self.user_id 885 } 886 /// Immutable state returned during registration. 887 #[inline] 888 #[must_use] 889 pub const fn static_state(&self) -> StaticState<UncompressedPubKey<'reg>> { 890 self.static_state 891 } 892 /// State that can change during authentication ceremonies. 893 #[inline] 894 #[must_use] 895 pub const fn dynamic_state(&self) -> DynamicState { 896 self.dynamic_state 897 } 898 /// Metadata. 899 #[inline] 900 #[must_use] 901 pub const fn metadata(&self) -> Metadata<'reg> { 902 self.metadata 903 } 904 /// Constructs a `RegisteredCredential` based on the passed arguments. 905 /// 906 /// # Errors 907 /// 908 /// Errors iff the passed arguments are invalid. Read [`CredentialErr`] 909 /// for more information. 910 #[inline] 911 fn new<'a: 'reg>( 912 id: CredentialId<&'a [u8]>, 913 transports: AuthTransports, 914 user_id: UserHandle<USER_LEN>, 915 static_state: StaticState<UncompressedPubKey<'a>>, 916 dynamic_state: DynamicState, 917 metadata: Metadata<'a>, 918 ) -> Result<Self, CredentialErr> { 919 verify_static_and_dynamic_state(&static_state, dynamic_state).and_then(|()| { 920 if !matches!(metadata.resident_key, ResidentKeyRequirement::Required) 921 || metadata 922 .client_extension_results 923 .cred_props 924 .as_ref() 925 .is_none_or(|props| props.rk.is_none_or(convert::identity)) 926 { 927 Ok(Self { 928 id, 929 transports, 930 user_id, 931 static_state, 932 dynamic_state, 933 metadata, 934 }) 935 } else { 936 Err(CredentialErr::ResidentKeyRequiredServerCredentialCreated) 937 } 938 }) 939 } 940 /// Returns the contained data consuming `self`. 941 #[inline] 942 #[must_use] 943 pub const fn into_parts( 944 self, 945 ) -> ( 946 CredentialId<&'reg [u8]>, 947 AuthTransports, 948 UserHandle<USER_LEN>, 949 StaticState<UncompressedPubKey<'reg>>, 950 DynamicState, 951 Metadata<'reg>, 952 ) { 953 ( 954 self.id, 955 self.transports, 956 self.user_id, 957 self.static_state, 958 self.dynamic_state, 959 self.metadata, 960 ) 961 } 962 /// Returns the contained data. 963 #[inline] 964 #[must_use] 965 pub const fn as_parts( 966 &self, 967 ) -> ( 968 CredentialId<&'reg [u8]>, 969 AuthTransports, 970 &UserHandle<USER_LEN>, 971 StaticState<UncompressedPubKey<'reg>>, 972 DynamicState, 973 Metadata<'reg>, 974 ) { 975 ( 976 self.id, 977 self.transports, 978 &self.user_id, 979 self.static_state, 980 self.dynamic_state, 981 self.metadata, 982 ) 983 } 984 } 985 /// `RegisteredCredential` based on a [`UserHandle64`]. 986 pub type RegisteredCredential64<'reg> = RegisteredCredential<'reg, USER_HANDLE_MAX_LEN>; 987 /// `RegisteredCredential` based on a [`UserHandle16`]. 988 pub type RegisteredCredential16<'reg> = RegisteredCredential<'reg, 16>; 989 /// Credential used in authentication ceremonies. 990 /// 991 /// Similar to [`RegisteredCredential`] except designed to only contain the necessary data to complete 992 /// authentication ceremonies. In particular there is no [`AuthTransports`] or [`Metadata`], 993 /// [`StaticState::credential_public_key`] is [`CompressedPubKey`] that can own or borrow its data, [`Self::id`] is 994 /// based on the [`CredentialId`] passed to [`Self::new`] which itself must be from [`Authentication::raw_id`], and 995 /// [`Self::user_id`] is based on the [`UserHandle`] passed to [`Self::new`] which itself must be the value in 996 /// persistent storage associated with the `CredentialId`. 997 /// 998 /// When [`DiscoverableAuthentication`] is used, one can use [`DiscoverableAuthenticatorAssertion::user_handle`] 999 /// for `Self::user_id` so long as it matches the value in persistent storage. 1000 /// 1001 /// Note `PublicKey` should be `CompressedPubKey` for this to be useful. 1002 /// 1003 /// The only way to create this is via `Self::new`. 1004 #[derive(Debug)] 1005 pub struct AuthenticatedCredential<'cred, 'user, const USER_LEN: usize, PublicKey> { 1006 /// The credential ID. 1007 /// 1008 /// For client-side credentials, this is a unique identifier; but for server-side 1009 /// credentials, this _is_ the credential (i.e., the encrypted private key and necessary information). 1010 id: CredentialId<&'cred [u8]>, 1011 /// The identifier for the user. 1012 /// 1013 /// Unlike [`Self::id`] which is globally unique for an RP, this is unique up to "user" (i.e., 1014 /// multiple [`CredentialId`]s will often exist for the same `UserHandle`). 1015 user_id: &'user UserHandle<USER_LEN>, 1016 /// Immutable state returned during registration. 1017 static_state: StaticState<PublicKey>, 1018 /// State that can change during authentication ceremonies. 1019 dynamic_state: DynamicState, 1020 } 1021 impl<'cred, 'user, const USER_LEN: usize, PublicKey> 1022 AuthenticatedCredential<'cred, 'user, USER_LEN, PublicKey> 1023 { 1024 /// The credential ID. 1025 /// 1026 /// For client-side credentials, this is a unique identifier; but for server-side 1027 /// credentials, this _is_ the credential (i.e., the encrypted private key and necessary information). 1028 #[inline] 1029 #[must_use] 1030 pub const fn id(&self) -> CredentialId<&'cred [u8]> { 1031 self.id 1032 } 1033 /// The identifier for the user. 1034 /// 1035 /// Unlike [`Self::id`] which is globally unique for an RP, this is unique up to "user" (i.e., 1036 /// multiple [`CredentialId`]s will often exist for the same `UserHandle`). 1037 #[inline] 1038 #[must_use] 1039 pub const fn user_id(&self) -> &'user UserHandle<USER_LEN> { 1040 self.user_id 1041 } 1042 /// Immutable state returned during registration. 1043 #[inline] 1044 #[must_use] 1045 pub const fn static_state(&self) -> &StaticState<PublicKey> { 1046 &self.static_state 1047 } 1048 /// State that can change during authentication ceremonies. 1049 #[inline] 1050 #[must_use] 1051 pub const fn dynamic_state(&self) -> DynamicState { 1052 self.dynamic_state 1053 } 1054 /// Constructs an `AuthenticatedCredential` based on the passed arguments. 1055 /// 1056 /// # Errors 1057 /// 1058 /// Errors iff the passed arguments are invalid. Read [`CredentialErr`] 1059 /// for more information. 1060 #[expect(single_use_lifetimes, reason = "false positive")] 1061 #[cfg(any(feature = "bin", feature = "custom"))] 1062 #[inline] 1063 pub fn new<'a: 'cred, 'b: 'user>( 1064 id: CredentialId<&'a [u8]>, 1065 user_id: &'b UserHandle<USER_LEN>, 1066 static_state: StaticState<PublicKey>, 1067 dynamic_state: DynamicState, 1068 ) -> Result<Self, CredentialErr> { 1069 verify_static_and_dynamic_state(&static_state, dynamic_state).map(|()| Self { 1070 id, 1071 user_id, 1072 static_state, 1073 dynamic_state, 1074 }) 1075 } 1076 /// Returns the contained data consuming `self`. 1077 #[inline] 1078 #[must_use] 1079 pub fn into_parts( 1080 self, 1081 ) -> ( 1082 CredentialId<&'cred [u8]>, 1083 &'user UserHandle<USER_LEN>, 1084 StaticState<PublicKey>, 1085 DynamicState, 1086 ) { 1087 (self.id, self.user_id, self.static_state, self.dynamic_state) 1088 } 1089 /// Returns the contained data. 1090 #[inline] 1091 #[must_use] 1092 pub const fn as_parts( 1093 &self, 1094 ) -> ( 1095 CredentialId<&'cred [u8]>, 1096 &'user UserHandle<USER_LEN>, 1097 &StaticState<PublicKey>, 1098 DynamicState, 1099 ) { 1100 ( 1101 self.id, 1102 self.user_id, 1103 self.static_state(), 1104 self.dynamic_state, 1105 ) 1106 } 1107 } 1108 use response::register::{CompressedPubKeyBorrowed, CompressedPubKeyOwned}; 1109 /// `AuthenticatedCredential` based on a [`UserHandle64`]. 1110 pub type AuthenticatedCredential64<'cred, 'user, PublicKey> = 1111 AuthenticatedCredential<'cred, 'user, USER_HANDLE_MAX_LEN, PublicKey>; 1112 /// `AuthenticatedCredential` based on a [`UserHandle16`]. 1113 pub type AuthenticatedCredential16<'cred, 'user, PublicKey> = 1114 AuthenticatedCredential<'cred, 'user, 16, PublicKey>; 1115 /// `AuthenticatedCredential` that owns the key data. 1116 pub type AuthenticatedCredentialOwned<'cred, 'user, const USER_LEN: usize> = 1117 AuthenticatedCredential<'cred, 'user, USER_LEN, CompressedPubKeyOwned>; 1118 /// `AuthenticatedCredential` that borrows the key data. 1119 pub type AuthenticatedCredentialBorrowed<'cred, 'user, 'key, const USER_LEN: usize> = 1120 AuthenticatedCredential<'cred, 'user, USER_LEN, CompressedPubKeyBorrowed<'key>>; 1121 /// Convenience aggregate error that rolls up all errors into one. 1122 #[derive(Debug)] 1123 pub enum AggErr { 1124 /// Variant when [`AsciiDomain::try_from`] errors. 1125 AsciiDomain(AsciiDomainErr), 1126 /// Variant when [`Url::from_str`] errors. 1127 Url(UrlErr), 1128 /// Variant when [`Scheme::try_from`] errors. 1129 Scheme(SchemeParseErr), 1130 /// Variant when [`DomainOrigin::try_from`] errors. 1131 DomainOrigin(DomainOriginParseErr), 1132 /// Variant when [`Port::from_str`] errors. 1133 Port(PortParseErr), 1134 /// Variant when [`DiscoverableCredentialRequestOptions::start_ceremony`] or 1135 /// [`NonDiscoverableCredentialRequestOptions::start_ceremony`] 1136 /// error. 1137 InvalidTimeout(InvalidTimeout), 1138 /// Variant when [`CredentialCreationOptions::start_ceremony`] errors. 1139 CreationOptions(CreationOptionsErr), 1140 /// Variant when [`NonDiscoverableCredentialRequestOptions::start_ceremony`] errors. 1141 NonDiscoverableCredentialRequestOptions(NonDiscoverableCredentialRequestOptionsErr), 1142 /// Variant when [`RegistrationServerState::verify`] errors. 1143 RegCeremony(RegCeremonyErr), 1144 /// Variant when [`DiscoverableAuthenticationServerState::verify`] or. 1145 /// [`NonDiscoverableAuthenticationServerState::verify`] error. 1146 AuthCeremony(AuthCeremonyErr), 1147 /// Variant when [`AttestationObject::try_from`] errors. 1148 AttestationObject(AttestationObjectErr), 1149 /// Variant when [`register::AuthenticatorData::try_from`] errors. 1150 RegAuthenticatorData(RegAuthDataErr), 1151 /// Variant when [`auth::AuthenticatorData::try_from`] errors. 1152 AuthAuthenticatorData(AuthAuthDataErr), 1153 /// Variant when [`CollectedClientData::from_client_data_json`] errors. 1154 CollectedClientData(CollectedClientDataErr), 1155 /// Variant when [`CollectedClientData::from_client_data_json_relaxed`] errors or any of the [`Deserialize`] 1156 /// implementations error when relying on [`Deserializer`] or [`StreamDeserializer`]. 1157 #[cfg(feature = "serde_relaxed")] 1158 SerdeJson(SerdeJsonErr), 1159 /// Variant when [`Aaguid::try_from`] errors. 1160 Aaguid(AaguidErr), 1161 /// Variant when [`AuthTransports::decode`] errors. 1162 #[cfg(feature = "bin")] 1163 DecodeAuthTransports(DecodeAuthTransportsErr), 1164 /// Variant when [`StaticState::decode`] errors. 1165 #[cfg(feature = "bin")] 1166 DecodeStaticState(DecodeStaticStateErr), 1167 /// Variant when [`DynamicState::decode`] errors. 1168 #[cfg(feature = "bin")] 1169 DecodeDynamicState(DecodeDynamicStateErr), 1170 /// Variant when [`RegistrationServerState::decode`] errors. 1171 #[cfg(feature = "serializable_server_state")] 1172 DecodeRegistrationServerState(DecodeRegistrationServerStateErr), 1173 /// Variant when [`DiscoverableAuthenticationServerState::decode`] errors. 1174 #[cfg(feature = "serializable_server_state")] 1175 DecodeDiscoverableAuthenticationServerState(DecodeDiscoverableAuthenticationServerStateErr), 1176 /// Variant when [`NonDiscoverableAuthenticationServerState::decode`] errors. 1177 #[cfg(feature = "serializable_server_state")] 1178 DecodeNonDiscoverableAuthenticationServerState( 1179 DecodeNonDiscoverableAuthenticationServerStateErr, 1180 ), 1181 /// Variant when [`RegistrationServerState::encode`] errors. 1182 #[cfg(feature = "serializable_server_state")] 1183 EncodeRegistrationServerState(SystemTimeError), 1184 /// Variant when [`DiscoverableAuthenticationServerState::encode`] errors. 1185 #[cfg(feature = "serializable_server_state")] 1186 EncodeDiscoverableAuthenticationServerState(SystemTimeError), 1187 /// Variant when [`NonDiscoverableAuthenticationServerState::encode`] errors. 1188 #[cfg(feature = "serializable_server_state")] 1189 EncodeNonDiscoverableAuthenticationServerState( 1190 EncodeNonDiscoverableAuthenticationServerStateErr, 1191 ), 1192 /// Variant when [`AuthenticatedCredential::new`] errors. 1193 #[cfg(any(feature = "bin", feature = "custom"))] 1194 Credential(CredentialErr), 1195 /// Variant when [`CredentialId::try_from`] or [`CredentialId::decode`] errors. 1196 #[cfg(any(feature = "bin", feature = "custom"))] 1197 CredentialId(CredentialIdErr), 1198 /// Variant when [`PublicKeyCredentialUserEntityOwned`] errors when converted into a 1199 /// [`PublicKeyCredentialUserEntity`]. 1200 #[cfg(feature = "serde")] 1201 PublicKeyCredentialUserEntityOwned(PublicKeyCredentialUserEntityOwnedErr), 1202 /// Variant when [`PublicKeyCredentialCreationOptionsOwned`] errors when converted into a 1203 /// [`PublicKeyCredentialCreationOptions`]. 1204 #[cfg(feature = "serde")] 1205 PublicKeyCredentialCreationOptionsOwned(PublicKeyCredentialCreationOptionsOwnedErr), 1206 } 1207 impl From<AsciiDomainErr> for AggErr { 1208 #[inline] 1209 fn from(value: AsciiDomainErr) -> Self { 1210 Self::AsciiDomain(value) 1211 } 1212 } 1213 impl From<UrlErr> for AggErr { 1214 #[inline] 1215 fn from(value: UrlErr) -> Self { 1216 Self::Url(value) 1217 } 1218 } 1219 impl From<SchemeParseErr> for AggErr { 1220 #[inline] 1221 fn from(value: SchemeParseErr) -> Self { 1222 Self::Scheme(value) 1223 } 1224 } 1225 impl From<DomainOriginParseErr> for AggErr { 1226 #[inline] 1227 fn from(value: DomainOriginParseErr) -> Self { 1228 Self::DomainOrigin(value) 1229 } 1230 } 1231 impl From<PortParseErr> for AggErr { 1232 #[inline] 1233 fn from(value: PortParseErr) -> Self { 1234 Self::Port(value) 1235 } 1236 } 1237 impl From<InvalidTimeout> for AggErr { 1238 #[inline] 1239 fn from(value: InvalidTimeout) -> Self { 1240 Self::InvalidTimeout(value) 1241 } 1242 } 1243 impl From<CreationOptionsErr> for AggErr { 1244 #[inline] 1245 fn from(value: CreationOptionsErr) -> Self { 1246 Self::CreationOptions(value) 1247 } 1248 } 1249 impl From<NonDiscoverableCredentialRequestOptionsErr> for AggErr { 1250 #[inline] 1251 fn from(value: NonDiscoverableCredentialRequestOptionsErr) -> Self { 1252 Self::NonDiscoverableCredentialRequestOptions(value) 1253 } 1254 } 1255 impl From<RegCeremonyErr> for AggErr { 1256 #[inline] 1257 fn from(value: RegCeremonyErr) -> Self { 1258 Self::RegCeremony(value) 1259 } 1260 } 1261 impl From<AuthCeremonyErr> for AggErr { 1262 #[inline] 1263 fn from(value: AuthCeremonyErr) -> Self { 1264 Self::AuthCeremony(value) 1265 } 1266 } 1267 impl From<AttestationObjectErr> for AggErr { 1268 #[inline] 1269 fn from(value: AttestationObjectErr) -> Self { 1270 Self::AttestationObject(value) 1271 } 1272 } 1273 impl From<RegAuthDataErr> for AggErr { 1274 #[inline] 1275 fn from(value: RegAuthDataErr) -> Self { 1276 Self::RegAuthenticatorData(value) 1277 } 1278 } 1279 impl From<AuthAuthDataErr> for AggErr { 1280 #[inline] 1281 fn from(value: AuthAuthDataErr) -> Self { 1282 Self::AuthAuthenticatorData(value) 1283 } 1284 } 1285 impl From<CollectedClientDataErr> for AggErr { 1286 #[inline] 1287 fn from(value: CollectedClientDataErr) -> Self { 1288 Self::CollectedClientData(value) 1289 } 1290 } 1291 #[cfg(feature = "serde_relaxed")] 1292 impl From<SerdeJsonErr> for AggErr { 1293 #[inline] 1294 fn from(value: SerdeJsonErr) -> Self { 1295 Self::SerdeJson(value) 1296 } 1297 } 1298 impl From<AaguidErr> for AggErr { 1299 #[inline] 1300 fn from(value: AaguidErr) -> Self { 1301 Self::Aaguid(value) 1302 } 1303 } 1304 #[cfg(feature = "bin")] 1305 impl From<DecodeAuthTransportsErr> for AggErr { 1306 #[inline] 1307 fn from(value: DecodeAuthTransportsErr) -> Self { 1308 Self::DecodeAuthTransports(value) 1309 } 1310 } 1311 #[cfg(feature = "bin")] 1312 impl From<DecodeStaticStateErr> for AggErr { 1313 #[inline] 1314 fn from(value: DecodeStaticStateErr) -> Self { 1315 Self::DecodeStaticState(value) 1316 } 1317 } 1318 #[cfg(feature = "bin")] 1319 impl From<DecodeDynamicStateErr> for AggErr { 1320 #[inline] 1321 fn from(value: DecodeDynamicStateErr) -> Self { 1322 Self::DecodeDynamicState(value) 1323 } 1324 } 1325 #[cfg(feature = "serializable_server_state")] 1326 impl From<DecodeRegistrationServerStateErr> for AggErr { 1327 #[inline] 1328 fn from(value: DecodeRegistrationServerStateErr) -> Self { 1329 Self::DecodeRegistrationServerState(value) 1330 } 1331 } 1332 #[cfg(feature = "serializable_server_state")] 1333 impl From<DecodeDiscoverableAuthenticationServerStateErr> for AggErr { 1334 #[inline] 1335 fn from(value: DecodeDiscoverableAuthenticationServerStateErr) -> Self { 1336 Self::DecodeDiscoverableAuthenticationServerState(value) 1337 } 1338 } 1339 #[cfg(feature = "serializable_server_state")] 1340 impl From<DecodeNonDiscoverableAuthenticationServerStateErr> for AggErr { 1341 #[inline] 1342 fn from(value: DecodeNonDiscoverableAuthenticationServerStateErr) -> Self { 1343 Self::DecodeNonDiscoverableAuthenticationServerState(value) 1344 } 1345 } 1346 #[cfg(feature = "serializable_server_state")] 1347 impl From<EncodeNonDiscoverableAuthenticationServerStateErr> for AggErr { 1348 #[inline] 1349 fn from(value: EncodeNonDiscoverableAuthenticationServerStateErr) -> Self { 1350 Self::EncodeNonDiscoverableAuthenticationServerState(value) 1351 } 1352 } 1353 #[cfg(any(feature = "bin", feature = "custom"))] 1354 impl From<CredentialErr> for AggErr { 1355 #[inline] 1356 fn from(value: CredentialErr) -> Self { 1357 Self::Credential(value) 1358 } 1359 } 1360 #[cfg(any(feature = "bin", feature = "custom"))] 1361 impl From<CredentialIdErr> for AggErr { 1362 #[inline] 1363 fn from(value: CredentialIdErr) -> Self { 1364 Self::CredentialId(value) 1365 } 1366 } 1367 #[cfg(feature = "serde")] 1368 impl From<PublicKeyCredentialUserEntityOwnedErr> for AggErr { 1369 #[inline] 1370 fn from(value: PublicKeyCredentialUserEntityOwnedErr) -> Self { 1371 Self::PublicKeyCredentialUserEntityOwned(value) 1372 } 1373 } 1374 #[cfg(feature = "serde")] 1375 impl From<PublicKeyCredentialCreationOptionsOwnedErr> for AggErr { 1376 #[inline] 1377 fn from(value: PublicKeyCredentialCreationOptionsOwnedErr) -> Self { 1378 Self::PublicKeyCredentialCreationOptionsOwned(value) 1379 } 1380 } 1381 impl Display for AggErr { 1382 #[inline] 1383 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 1384 match *self { 1385 Self::AsciiDomain(err) => err.fmt(f), 1386 Self::Url(err) => err.fmt(f), 1387 Self::Scheme(err) => err.fmt(f), 1388 Self::DomainOrigin(ref err) => err.fmt(f), 1389 Self::Port(ref err) => err.fmt(f), 1390 Self::InvalidTimeout(err) => err.fmt(f), 1391 Self::CreationOptions(err) => err.fmt(f), 1392 Self::NonDiscoverableCredentialRequestOptions(err) => err.fmt(f), 1393 Self::RegCeremony(ref err) => err.fmt(f), 1394 Self::AuthCeremony(ref err) => err.fmt(f), 1395 Self::AttestationObject(err) => err.fmt(f), 1396 Self::RegAuthenticatorData(err) => err.fmt(f), 1397 Self::AuthAuthenticatorData(err) => err.fmt(f), 1398 Self::CollectedClientData(ref err) => err.fmt(f), 1399 #[cfg(feature = "serde_relaxed")] 1400 Self::SerdeJson(ref err) => err.fmt(f), 1401 Self::Aaguid(err) => err.fmt(f), 1402 #[cfg(feature = "bin")] 1403 Self::DecodeAuthTransports(err) => err.fmt(f), 1404 #[cfg(feature = "bin")] 1405 Self::DecodeStaticState(err) => err.fmt(f), 1406 #[cfg(feature = "bin")] 1407 Self::DecodeDynamicState(err) => err.fmt(f), 1408 #[cfg(feature = "serializable_server_state")] 1409 Self::DecodeRegistrationServerState(err) => err.fmt(f), 1410 #[cfg(feature = "serializable_server_state")] 1411 Self::DecodeDiscoverableAuthenticationServerState(err) => err.fmt(f), 1412 #[cfg(feature = "serializable_server_state")] 1413 Self::DecodeNonDiscoverableAuthenticationServerState(err) => err.fmt(f), 1414 #[cfg(feature = "serializable_server_state")] 1415 Self::EncodeRegistrationServerState(ref err) => err.fmt(f), 1416 #[cfg(feature = "serializable_server_state")] 1417 Self::EncodeDiscoverableAuthenticationServerState(ref err) => err.fmt(f), 1418 #[cfg(feature = "serializable_server_state")] 1419 Self::EncodeNonDiscoverableAuthenticationServerState(ref err) => err.fmt(f), 1420 #[cfg(any(feature = "bin", feature = "custom"))] 1421 Self::Credential(err) => err.fmt(f), 1422 #[cfg(any(feature = "bin", feature = "custom"))] 1423 Self::CredentialId(err) => err.fmt(f), 1424 #[cfg(feature = "serde")] 1425 Self::PublicKeyCredentialUserEntityOwned(err) => err.fmt(f), 1426 #[cfg(feature = "serde")] 1427 Self::PublicKeyCredentialCreationOptionsOwned(err) => err.fmt(f), 1428 } 1429 } 1430 } 1431 impl Error for AggErr {}