lib.rs (66778B)
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::{ 563 DiscoverableCredentialRequestOptionsErr, NonDiscoverableCredentialRequestOptionsErr, 564 }, 565 error::{AsciiDomainErr, DomainOriginParseErr, PortParseErr, SchemeParseErr, UrlErr}, 566 register::{ 567 ResidentKeyRequirement, USER_HANDLE_MAX_LEN, UserHandle, error::CreationOptionsErr, 568 }, 569 }, 570 response::{ 571 AuthTransports, CredentialId, 572 auth::error::{AuthCeremonyErr, AuthenticatorDataErr as AuthAuthDataErr}, 573 error::CollectedClientDataErr, 574 register::{ 575 CredentialProtectionPolicy, DynamicState, Metadata, StaticState, UncompressedPubKey, 576 error::{ 577 AaguidErr, AttestationObjectErr, AuthenticatorDataErr as RegAuthDataErr, 578 RegCeremonyErr, 579 }, 580 }, 581 }, 582 }; 583 #[cfg(all(doc, feature = "bin"))] 584 use bin::{Decode, Encode}; 585 #[cfg(doc)] 586 use core::str::FromStr; 587 use core::{ 588 convert, 589 error::Error, 590 fmt::{self, Display, Formatter}, 591 ops::Not, 592 }; 593 #[cfg(all(doc, feature = "serde_relaxed"))] 594 use response::register::ser_relaxed::RegistrationRelaxed; 595 #[cfg(all(doc, feature = "serde"))] 596 use serde::Deserialize; 597 #[cfg(all(doc, feature = "serde_relaxed"))] 598 use serde_json::de::{Deserializer, StreamDeserializer}; 599 #[cfg(feature = "serializable_server_state")] 600 use std::time::SystemTimeError; 601 #[cfg(doc)] 602 use std::time::{Instant, SystemTime}; 603 /// Contains functionality to (de)serialize data to a data store. 604 #[cfg(feature = "bin")] 605 pub mod bin; 606 /// Contains functionality for maximum-length hash maps and sets that allocate exactly once. 607 pub mod hash; 608 /// Functionality for starting ceremonies. 609 /// 610 /// # What kind of credential should I create? 611 /// 612 /// Without partitioning the possibilities _too_ much, the following are possible authentication flows: 613 /// 614 /// | Label | Username | Password | Client-side credential | Authenticator-side user verification | Recommended | 615 /// |-------|----------|----------|------------------------|--------------------------------------|:-----------:| 616 /// | 1 | Yes | Yes | Required | Yes | ❌ | 617 /// | 2 | Yes | Yes | Required | No | ❌ | 618 /// | 3 | Yes | Yes | Optional | Yes | ❌ | 619 /// | <a name="label4">4</a> | Yes | Yes | Optional | No | ✅ | 620 /// | 5 | Yes | No | Required | Yes | ❌ | 621 /// | 6 | Yes | No | Required | No | ❌ | 622 /// | <a name="label7">7</a> | Yes | No | Optional | Yes | ❔ | 623 /// | 8 | Yes | No | Optional | No | ❌ | 624 /// | 9 | No | Yes | Required | Yes | ❌ | 625 /// | 10 | No | Yes | Required | No | ❌ | 626 /// | 11 | No | Yes | Optional | Yes | ❌ | 627 /// | 12 | No | Yes | Optional | No | ❌ | 628 /// | <a name="label13">13</a> | No | No | Required | Yes | ✅ | 629 /// | 14 | No | No | Required | No | ❌ | 630 /// | 15 | No | No | Optional | Yes | ❌ | 631 /// | 16 | No | No | Optional | No | ❌ | 632 /// 633 /// * All `Label`s with both `Password` and `Authenticator-side user verification` set to `Yes` are not recommended 634 /// since the verification done on the authenticator is likely the same "factor" as a password; thus it does not 635 /// add benefit but only serves as an annoyance to users. 636 /// * All `Label`s with `Username` or `Password` set to `Yes` and `Client-side credential` set to `Required` are not 637 /// recommended since you may preclude authenticators that are storage constrained (e.g., security keys). 638 /// * All `Label`s with `Username` set to `No` and `Client-side credential` set to `Optional` are not possible since 639 /// RPs would not have a way to identify the set of encrypted credentials to pass to the unknown user. 640 /// * All `Label`s with `Password` and `Authenticator-side user verification` set to `No` are not recommended since 641 /// those are single-factor authentication schemes; thus anyone possessing the credential without also passing 642 /// some form of user verification (e.g., password) would authenticate. 643 /// * [`Label 7`](#label7) is possible for RPs that are comfortable passing an encrypted credential to a potential user 644 /// without having that user first pass another form of authentication. For many RPs passing such information even 645 /// if encrypted is not desirable though. 646 /// * [`Label 4`](#label4) is ideal as a single-factor flow incorporated within a wider multi-factor authentication (MFA) 647 /// setup. The easiest way to register such a credential is with 648 /// [`CredentialCreationOptions::second_factor`]. 649 /// * [`Label 13`](#label13) is ideal for passkey setups as it allows for pleasant UX where a user does not have to type a 650 /// username nor password while still being secured with MFA with one of the factors being based on public-key 651 /// cryptography which for many is the most secure form of single-factor authentication. The easiest way to register 652 /// such a credential is with [`CredentialCreationOptions::passkey`]. 653 /// 654 /// Two other reasons one may prefer to construct client-side credentials is richer support for extensions (e.g., 655 /// [`largeBlobKey`](https://fidoalliance.org/specs/fido-v2.2-rd-20230321/fido-client-to-authenticator-protocol-v2.2-rd-20230321.html#sctn-largeBlobKey-extension) 656 /// for CTAP 2.2 authenticators) and the ability to use both discoverable and nondiscoverable requests. The former is not 657 /// relevant for this library—at least currently—since the only extensions supported are applicable for both 658 /// client-side and server-side credentials. The latter can be important especially if an RP wants the ability to 659 /// seamlessly transition from a username and password scheme to a userless and passwordless one in the future. 660 /// 661 /// Note the table is purely informative. While helper functions 662 /// (e.g., [`CredentialCreationOptions::passkey`]) only exist for [`Label 4`](#label4) and 663 /// [`Label 13`](#label13), one can create any credential since all fields in [`CredentialCreationOptions`] 664 /// and [`PublicKeyCredentialRequestOptions`] are accessible. 665 pub mod request; 666 /// Functionality for completing ceremonies. 667 /// 668 /// Read [`request`] for more information about what credentials one should create. 669 pub mod response; 670 #[doc(inline)] 671 pub use crate::{ 672 request::{ 673 auth::{ 674 DiscoverableAuthenticationClientState, DiscoverableAuthenticationServerState, 675 DiscoverableCredentialRequestOptions, NonDiscoverableAuthenticationClientState, 676 NonDiscoverableAuthenticationServerState, NonDiscoverableCredentialRequestOptions, 677 }, 678 register::{ 679 CredentialCreationOptions, CredentialCreationOptions16, CredentialCreationOptions64, 680 RegistrationClientState, RegistrationClientState16, RegistrationClientState64, 681 RegistrationServerState, RegistrationServerState16, RegistrationServerState64, 682 }, 683 }, 684 response::{ 685 auth::{ 686 DiscoverableAuthentication, DiscoverableAuthentication16, DiscoverableAuthentication64, 687 NonDiscoverableAuthentication, NonDiscoverableAuthentication16, 688 NonDiscoverableAuthentication64, 689 }, 690 register::Registration, 691 }, 692 }; 693 /// Error returned in [`RegCeremonyErr::Credential`] and [`AuthenticatedCredential::new`]. 694 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 695 pub enum CredentialErr { 696 /// Variant when [`CredentialProtectionPolicy::UserVerificationRequired`], but 697 /// [`DynamicState::user_verified`] is `false`. 698 CredProtectUserVerificationRequiredWithoutUserVerified, 699 /// Variant when [`ClientExtensionsOutputs::prf`] is 700 /// `Some(AuthenticationExtensionsPRFOutputs { enabled: true })` and [`DynamicState::user_verified`] is `false`. 701 PrfWithoutUserVerified, 702 /// Variant when [`AuthenticatorExtensionOutput::hmac_secret`] is `Some(true)`, but 703 /// [`ClientExtensionsOutputs::prf`] is `Some(AuthenticationExtensionsPRFOutputs { enabled: false })` 704 /// or `AuthenticatorExtensionOutput::hmac_secret` is `Some`, but 705 /// `ClientExtensionsOutputs::prf` is `None`. 706 HmacSecretWithoutPrf, 707 /// Variant when [`ClientExtensionsOutputs::prf`] is 708 /// `Some(AuthenticationExtensionsPRFOutputs { enabled: true })`, but 709 /// [`AuthenticatorExtensionOutput::hmac_secret`] is `Some(false)`. 710 PrfWithoutHmacSecret, 711 /// Variant when [`ResidentKeyRequirement::Required`] was sent, but 712 /// [`CredentialPropertiesOutput::rk`] is `Some(false)`. 713 ResidentKeyRequiredServerCredentialCreated, 714 } 715 impl Display for CredentialErr { 716 #[inline] 717 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 718 f.write_str(match *self { 719 Self::CredProtectUserVerificationRequiredWithoutUserVerified => { 720 "credProtect requires user verification, but the user is not verified" 721 } 722 Self::PrfWithoutUserVerified => "prf is enabled, but the user is not verified", 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 .client_extension_results 750 .prf 751 .is_some_and(|prf| prf.enabled) 752 { 753 Err(CredentialErr::PrfWithoutUserVerified) 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 [`CredentialCreationOptions::start_ceremony`] errors. 1135 CreationOptions(CreationOptionsErr), 1136 /// Variant when [`DiscoverableCredentialRequestOptions::start_ceremony`] errors. 1137 DiscoverableCredentialRequestOptions(DiscoverableCredentialRequestOptionsErr), 1138 /// Variant when [`NonDiscoverableCredentialRequestOptions::start_ceremony`] errors. 1139 NonDiscoverableCredentialRequestOptions(NonDiscoverableCredentialRequestOptionsErr), 1140 /// Variant when [`RegistrationServerState::verify`] errors. 1141 RegCeremony(RegCeremonyErr), 1142 /// Variant when [`DiscoverableAuthenticationServerState::verify`] or. 1143 /// [`NonDiscoverableAuthenticationServerState::verify`] error. 1144 AuthCeremony(AuthCeremonyErr), 1145 /// Variant when [`AttestationObject::try_from`] errors. 1146 AttestationObject(AttestationObjectErr), 1147 /// Variant when [`register::AuthenticatorData::try_from`] errors. 1148 RegAuthenticatorData(RegAuthDataErr), 1149 /// Variant when [`auth::AuthenticatorData::try_from`] errors. 1150 AuthAuthenticatorData(AuthAuthDataErr), 1151 /// Variant when [`CollectedClientData::from_client_data_json`] errors. 1152 CollectedClientData(CollectedClientDataErr), 1153 /// Variant when [`CollectedClientData::from_client_data_json_relaxed`] errors or any of the [`Deserialize`] 1154 /// implementations error when relying on [`Deserializer`] or [`StreamDeserializer`]. 1155 #[cfg(feature = "serde_relaxed")] 1156 SerdeJson(SerdeJsonErr), 1157 /// Variant when [`Aaguid::try_from`] errors. 1158 Aaguid(AaguidErr), 1159 /// Variant when [`AuthTransports::decode`] errors. 1160 #[cfg(feature = "bin")] 1161 DecodeAuthTransports(DecodeAuthTransportsErr), 1162 /// Variant when [`StaticState::decode`] errors. 1163 #[cfg(feature = "bin")] 1164 DecodeStaticState(DecodeStaticStateErr), 1165 /// Variant when [`DynamicState::decode`] errors. 1166 #[cfg(feature = "bin")] 1167 DecodeDynamicState(DecodeDynamicStateErr), 1168 /// Variant when [`RegistrationServerState::decode`] errors. 1169 #[cfg(feature = "serializable_server_state")] 1170 DecodeRegistrationServerState(DecodeRegistrationServerStateErr), 1171 /// Variant when [`DiscoverableAuthenticationServerState::decode`] errors. 1172 #[cfg(feature = "serializable_server_state")] 1173 DecodeDiscoverableAuthenticationServerState(DecodeDiscoverableAuthenticationServerStateErr), 1174 /// Variant when [`NonDiscoverableAuthenticationServerState::decode`] errors. 1175 #[cfg(feature = "serializable_server_state")] 1176 DecodeNonDiscoverableAuthenticationServerState( 1177 DecodeNonDiscoverableAuthenticationServerStateErr, 1178 ), 1179 /// Variant when [`RegistrationServerState::encode`] errors. 1180 #[cfg(feature = "serializable_server_state")] 1181 EncodeRegistrationServerState(SystemTimeError), 1182 /// Variant when [`DiscoverableAuthenticationServerState::encode`] errors. 1183 #[cfg(feature = "serializable_server_state")] 1184 EncodeDiscoverableAuthenticationServerState(SystemTimeError), 1185 /// Variant when [`NonDiscoverableAuthenticationServerState::encode`] errors. 1186 #[cfg(feature = "serializable_server_state")] 1187 EncodeNonDiscoverableAuthenticationServerState( 1188 EncodeNonDiscoverableAuthenticationServerStateErr, 1189 ), 1190 /// Variant when [`AuthenticatedCredential::new`] errors. 1191 #[cfg(any(feature = "bin", feature = "custom"))] 1192 Credential(CredentialErr), 1193 /// Variant when [`CredentialId::try_from`] or [`CredentialId::decode`] errors. 1194 #[cfg(any(feature = "bin", feature = "custom"))] 1195 CredentialId(CredentialIdErr), 1196 /// Variant when [`PublicKeyCredentialUserEntityOwned`] errors when converted into a 1197 /// [`PublicKeyCredentialUserEntity`]. 1198 #[cfg(feature = "serde")] 1199 PublicKeyCredentialUserEntityOwned(PublicKeyCredentialUserEntityOwnedErr), 1200 /// Variant when [`PublicKeyCredentialCreationOptionsOwned`] errors when converted into a 1201 /// [`PublicKeyCredentialCreationOptions`]. 1202 #[cfg(feature = "serde")] 1203 PublicKeyCredentialCreationOptionsOwned(PublicKeyCredentialCreationOptionsOwnedErr), 1204 } 1205 impl From<AsciiDomainErr> for AggErr { 1206 #[inline] 1207 fn from(value: AsciiDomainErr) -> Self { 1208 Self::AsciiDomain(value) 1209 } 1210 } 1211 impl From<UrlErr> for AggErr { 1212 #[inline] 1213 fn from(value: UrlErr) -> Self { 1214 Self::Url(value) 1215 } 1216 } 1217 impl From<SchemeParseErr> for AggErr { 1218 #[inline] 1219 fn from(value: SchemeParseErr) -> Self { 1220 Self::Scheme(value) 1221 } 1222 } 1223 impl From<DomainOriginParseErr> for AggErr { 1224 #[inline] 1225 fn from(value: DomainOriginParseErr) -> Self { 1226 Self::DomainOrigin(value) 1227 } 1228 } 1229 impl From<PortParseErr> for AggErr { 1230 #[inline] 1231 fn from(value: PortParseErr) -> Self { 1232 Self::Port(value) 1233 } 1234 } 1235 impl From<CreationOptionsErr> for AggErr { 1236 #[inline] 1237 fn from(value: CreationOptionsErr) -> Self { 1238 Self::CreationOptions(value) 1239 } 1240 } 1241 impl From<DiscoverableCredentialRequestOptionsErr> for AggErr { 1242 #[inline] 1243 fn from(value: DiscoverableCredentialRequestOptionsErr) -> Self { 1244 Self::DiscoverableCredentialRequestOptions(value) 1245 } 1246 } 1247 impl From<NonDiscoverableCredentialRequestOptionsErr> for AggErr { 1248 #[inline] 1249 fn from(value: NonDiscoverableCredentialRequestOptionsErr) -> Self { 1250 Self::NonDiscoverableCredentialRequestOptions(value) 1251 } 1252 } 1253 impl From<RegCeremonyErr> for AggErr { 1254 #[inline] 1255 fn from(value: RegCeremonyErr) -> Self { 1256 Self::RegCeremony(value) 1257 } 1258 } 1259 impl From<AuthCeremonyErr> for AggErr { 1260 #[inline] 1261 fn from(value: AuthCeremonyErr) -> Self { 1262 Self::AuthCeremony(value) 1263 } 1264 } 1265 impl From<AttestationObjectErr> for AggErr { 1266 #[inline] 1267 fn from(value: AttestationObjectErr) -> Self { 1268 Self::AttestationObject(value) 1269 } 1270 } 1271 impl From<RegAuthDataErr> for AggErr { 1272 #[inline] 1273 fn from(value: RegAuthDataErr) -> Self { 1274 Self::RegAuthenticatorData(value) 1275 } 1276 } 1277 impl From<AuthAuthDataErr> for AggErr { 1278 #[inline] 1279 fn from(value: AuthAuthDataErr) -> Self { 1280 Self::AuthAuthenticatorData(value) 1281 } 1282 } 1283 impl From<CollectedClientDataErr> for AggErr { 1284 #[inline] 1285 fn from(value: CollectedClientDataErr) -> Self { 1286 Self::CollectedClientData(value) 1287 } 1288 } 1289 #[cfg(feature = "serde_relaxed")] 1290 impl From<SerdeJsonErr> for AggErr { 1291 #[inline] 1292 fn from(value: SerdeJsonErr) -> Self { 1293 Self::SerdeJson(value) 1294 } 1295 } 1296 impl From<AaguidErr> for AggErr { 1297 #[inline] 1298 fn from(value: AaguidErr) -> Self { 1299 Self::Aaguid(value) 1300 } 1301 } 1302 #[cfg(feature = "bin")] 1303 impl From<DecodeAuthTransportsErr> for AggErr { 1304 #[inline] 1305 fn from(value: DecodeAuthTransportsErr) -> Self { 1306 Self::DecodeAuthTransports(value) 1307 } 1308 } 1309 #[cfg(feature = "bin")] 1310 impl From<DecodeStaticStateErr> for AggErr { 1311 #[inline] 1312 fn from(value: DecodeStaticStateErr) -> Self { 1313 Self::DecodeStaticState(value) 1314 } 1315 } 1316 #[cfg(feature = "bin")] 1317 impl From<DecodeDynamicStateErr> for AggErr { 1318 #[inline] 1319 fn from(value: DecodeDynamicStateErr) -> Self { 1320 Self::DecodeDynamicState(value) 1321 } 1322 } 1323 #[cfg(feature = "serializable_server_state")] 1324 impl From<DecodeRegistrationServerStateErr> for AggErr { 1325 #[inline] 1326 fn from(value: DecodeRegistrationServerStateErr) -> Self { 1327 Self::DecodeRegistrationServerState(value) 1328 } 1329 } 1330 #[cfg(feature = "serializable_server_state")] 1331 impl From<DecodeDiscoverableAuthenticationServerStateErr> for AggErr { 1332 #[inline] 1333 fn from(value: DecodeDiscoverableAuthenticationServerStateErr) -> Self { 1334 Self::DecodeDiscoverableAuthenticationServerState(value) 1335 } 1336 } 1337 #[cfg(feature = "serializable_server_state")] 1338 impl From<DecodeNonDiscoverableAuthenticationServerStateErr> for AggErr { 1339 #[inline] 1340 fn from(value: DecodeNonDiscoverableAuthenticationServerStateErr) -> Self { 1341 Self::DecodeNonDiscoverableAuthenticationServerState(value) 1342 } 1343 } 1344 #[cfg(feature = "serializable_server_state")] 1345 impl From<EncodeNonDiscoverableAuthenticationServerStateErr> for AggErr { 1346 #[inline] 1347 fn from(value: EncodeNonDiscoverableAuthenticationServerStateErr) -> Self { 1348 Self::EncodeNonDiscoverableAuthenticationServerState(value) 1349 } 1350 } 1351 #[cfg(any(feature = "bin", feature = "custom"))] 1352 impl From<CredentialErr> for AggErr { 1353 #[inline] 1354 fn from(value: CredentialErr) -> Self { 1355 Self::Credential(value) 1356 } 1357 } 1358 #[cfg(any(feature = "bin", feature = "custom"))] 1359 impl From<CredentialIdErr> for AggErr { 1360 #[inline] 1361 fn from(value: CredentialIdErr) -> Self { 1362 Self::CredentialId(value) 1363 } 1364 } 1365 #[cfg(feature = "serde")] 1366 impl From<PublicKeyCredentialUserEntityOwnedErr> for AggErr { 1367 #[inline] 1368 fn from(value: PublicKeyCredentialUserEntityOwnedErr) -> Self { 1369 Self::PublicKeyCredentialUserEntityOwned(value) 1370 } 1371 } 1372 #[cfg(feature = "serde")] 1373 impl From<PublicKeyCredentialCreationOptionsOwnedErr> for AggErr { 1374 #[inline] 1375 fn from(value: PublicKeyCredentialCreationOptionsOwnedErr) -> Self { 1376 Self::PublicKeyCredentialCreationOptionsOwned(value) 1377 } 1378 } 1379 impl Display for AggErr { 1380 #[inline] 1381 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 1382 match *self { 1383 Self::AsciiDomain(err) => err.fmt(f), 1384 Self::Url(err) => err.fmt(f), 1385 Self::Scheme(err) => err.fmt(f), 1386 Self::DomainOrigin(ref err) => err.fmt(f), 1387 Self::Port(ref err) => err.fmt(f), 1388 Self::CreationOptions(err) => err.fmt(f), 1389 Self::DiscoverableCredentialRequestOptions(err) => err.fmt(f), 1390 Self::NonDiscoverableCredentialRequestOptions(err) => err.fmt(f), 1391 Self::RegCeremony(ref err) => err.fmt(f), 1392 Self::AuthCeremony(ref err) => err.fmt(f), 1393 Self::AttestationObject(err) => err.fmt(f), 1394 Self::RegAuthenticatorData(err) => err.fmt(f), 1395 Self::AuthAuthenticatorData(err) => err.fmt(f), 1396 Self::CollectedClientData(ref err) => err.fmt(f), 1397 #[cfg(feature = "serde_relaxed")] 1398 Self::SerdeJson(ref err) => err.fmt(f), 1399 Self::Aaguid(err) => err.fmt(f), 1400 #[cfg(feature = "bin")] 1401 Self::DecodeAuthTransports(err) => err.fmt(f), 1402 #[cfg(feature = "bin")] 1403 Self::DecodeStaticState(err) => err.fmt(f), 1404 #[cfg(feature = "bin")] 1405 Self::DecodeDynamicState(err) => err.fmt(f), 1406 #[cfg(feature = "serializable_server_state")] 1407 Self::DecodeRegistrationServerState(err) => err.fmt(f), 1408 #[cfg(feature = "serializable_server_state")] 1409 Self::DecodeDiscoverableAuthenticationServerState(err) => err.fmt(f), 1410 #[cfg(feature = "serializable_server_state")] 1411 Self::DecodeNonDiscoverableAuthenticationServerState(err) => err.fmt(f), 1412 #[cfg(feature = "serializable_server_state")] 1413 Self::EncodeRegistrationServerState(ref err) => err.fmt(f), 1414 #[cfg(feature = "serializable_server_state")] 1415 Self::EncodeDiscoverableAuthenticationServerState(ref err) => err.fmt(f), 1416 #[cfg(feature = "serializable_server_state")] 1417 Self::EncodeNonDiscoverableAuthenticationServerState(ref err) => err.fmt(f), 1418 #[cfg(any(feature = "bin", feature = "custom"))] 1419 Self::Credential(err) => err.fmt(f), 1420 #[cfg(any(feature = "bin", feature = "custom"))] 1421 Self::CredentialId(err) => err.fmt(f), 1422 #[cfg(feature = "serde")] 1423 Self::PublicKeyCredentialUserEntityOwned(err) => err.fmt(f), 1424 #[cfg(feature = "serde")] 1425 Self::PublicKeyCredentialCreationOptionsOwned(err) => err.fmt(f), 1426 } 1427 } 1428 } 1429 impl Error for AggErr {}