WebAuthnArgs.cpp (13507B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "WebAuthnArgs.h" 8 9 #include "WebAuthnEnumStrings.h" 10 #include "WebAuthnUtil.h" 11 #include "mozilla/dom/PWebAuthnTransactionParent.h" 12 13 namespace mozilla::dom { 14 15 NS_IMPL_ISUPPORTS(WebAuthnRegisterArgs, nsIWebAuthnRegisterArgs) 16 17 NS_IMETHODIMP 18 WebAuthnRegisterArgs::GetOrigin(nsAString& aOrigin) { 19 aOrigin = NS_ConvertUTF8toUTF16(mOrigin); 20 return NS_OK; 21 } 22 23 NS_IMETHODIMP 24 WebAuthnRegisterArgs::GetChallenge(nsTArray<uint8_t>& aChallenge) { 25 aChallenge.Assign(mInfo.Challenge()); 26 return NS_OK; 27 } 28 29 NS_IMETHODIMP 30 WebAuthnRegisterArgs::GetClientDataJSON(nsACString& aClientDataJSON) { 31 aClientDataJSON = mClientDataJSON; 32 return NS_OK; 33 } 34 35 NS_IMETHODIMP 36 WebAuthnRegisterArgs::GetClientDataHash(nsTArray<uint8_t>& aClientDataHash) { 37 nsresult rv = HashCString(mClientDataJSON, aClientDataHash); 38 if (NS_WARN_IF(NS_FAILED(rv))) { 39 return NS_ERROR_FAILURE; 40 } 41 42 return NS_OK; 43 } 44 45 NS_IMETHODIMP 46 WebAuthnRegisterArgs::GetRpId(nsAString& aRpId) { 47 aRpId = NS_ConvertUTF8toUTF16(mInfo.RpId()); 48 return NS_OK; 49 } 50 51 NS_IMETHODIMP 52 WebAuthnRegisterArgs::GetRpName(nsAString& aRpName) { 53 aRpName = mInfo.Rp().Name(); 54 return NS_OK; 55 } 56 57 NS_IMETHODIMP 58 WebAuthnRegisterArgs::GetUserId(nsTArray<uint8_t>& aUserId) { 59 aUserId.Assign(mInfo.User().Id()); 60 return NS_OK; 61 } 62 63 NS_IMETHODIMP 64 WebAuthnRegisterArgs::GetUserName(nsAString& aUserName) { 65 aUserName = mInfo.User().Name(); 66 return NS_OK; 67 } 68 69 NS_IMETHODIMP 70 WebAuthnRegisterArgs::GetUserDisplayName(nsAString& aUserDisplayName) { 71 aUserDisplayName = mInfo.User().DisplayName(); 72 return NS_OK; 73 } 74 75 NS_IMETHODIMP 76 WebAuthnRegisterArgs::GetCoseAlgs(nsTArray<int32_t>& aCoseAlgs) { 77 aCoseAlgs.Clear(); 78 for (const CoseAlg& coseAlg : mInfo.coseAlgs()) { 79 aCoseAlgs.AppendElement(coseAlg.alg()); 80 } 81 return NS_OK; 82 } 83 84 NS_IMETHODIMP 85 WebAuthnRegisterArgs::GetExcludeList( 86 nsTArray<nsTArray<uint8_t>>& aExcludeList) { 87 aExcludeList.Clear(); 88 for (const WebAuthnScopedCredential& cred : mInfo.ExcludeList()) { 89 aExcludeList.AppendElement(cred.id().Clone()); 90 } 91 return NS_OK; 92 } 93 94 NS_IMETHODIMP 95 WebAuthnRegisterArgs::GetExcludeListTransports( 96 nsTArray<uint8_t>& aExcludeListTransports) { 97 aExcludeListTransports.Clear(); 98 for (const WebAuthnScopedCredential& cred : mInfo.ExcludeList()) { 99 aExcludeListTransports.AppendElement(cred.transports()); 100 } 101 return NS_OK; 102 } 103 104 NS_IMETHODIMP 105 WebAuthnRegisterArgs::GetCredentialProtectionPolicy( 106 nsACString& aCredentialProtectionPolicy) { 107 if (mCredentialProtectionPolicy.isSome()) { 108 aCredentialProtectionPolicy = 109 GetEnumString(mCredentialProtectionPolicy.ref()); 110 return NS_OK; 111 } 112 return NS_ERROR_NOT_AVAILABLE; 113 } 114 115 NS_IMETHODIMP 116 WebAuthnRegisterArgs::GetEnforceCredentialProtectionPolicy( 117 bool* aEnforceCredentialProtectionPolicy) { 118 *aEnforceCredentialProtectionPolicy = mEnforceCredentialProtectionPolicy; 119 120 return NS_OK; 121 } 122 123 NS_IMETHODIMP 124 WebAuthnRegisterArgs::GetCredProps(bool* aCredProps) { 125 *aCredProps = mCredProps; 126 127 return NS_OK; 128 } 129 130 NS_IMETHODIMP 131 WebAuthnRegisterArgs::GetHmacCreateSecret(bool* aHmacCreateSecret) { 132 *aHmacCreateSecret = mHmacCreateSecret; 133 134 return NS_OK; 135 } 136 137 NS_IMETHODIMP 138 WebAuthnRegisterArgs::GetPrf(bool* aPrf) { 139 *aPrf = mPrf; 140 return NS_OK; 141 } 142 143 NS_IMETHODIMP 144 WebAuthnRegisterArgs::GetPrfEvalFirst(nsTArray<uint8_t>& aEvalFirst) { 145 for (const WebAuthnExtension& ext : mInfo.Extensions()) { 146 if (ext.type() == WebAuthnExtension::TWebAuthnExtensionPrf) { 147 Maybe<WebAuthnExtensionPrfValues> eval = 148 ext.get_WebAuthnExtensionPrf().eval(); 149 if (eval.isSome()) { 150 aEvalFirst.Assign(eval->first()); 151 return NS_OK; 152 } 153 break; 154 } 155 } 156 157 return NS_ERROR_NOT_AVAILABLE; 158 } 159 160 NS_IMETHODIMP 161 WebAuthnRegisterArgs::GetPrfEvalSecond(nsTArray<uint8_t>& aEvalSecond) { 162 for (const WebAuthnExtension& ext : mInfo.Extensions()) { 163 if (ext.type() == WebAuthnExtension::TWebAuthnExtensionPrf) { 164 Maybe<WebAuthnExtensionPrfValues> eval = 165 ext.get_WebAuthnExtensionPrf().eval(); 166 if (eval.isSome() && eval->secondMaybe()) { 167 aEvalSecond.Assign(eval->second()); 168 return NS_OK; 169 } 170 break; 171 } 172 } 173 174 return NS_ERROR_NOT_AVAILABLE; 175 } 176 177 NS_IMETHODIMP 178 WebAuthnRegisterArgs::GetMinPinLength(bool* aMinPinLength) { 179 *aMinPinLength = mMinPinLength; 180 181 return NS_OK; 182 } 183 184 NS_IMETHODIMP 185 WebAuthnRegisterArgs::GetResidentKey(nsAString& aResidentKey) { 186 aResidentKey = mInfo.AuthenticatorSelection().residentKey(); 187 return NS_OK; 188 } 189 190 NS_IMETHODIMP 191 WebAuthnRegisterArgs::GetUserVerification( 192 nsAString& aUserVerificationRequirement) { 193 aUserVerificationRequirement = 194 mInfo.AuthenticatorSelection().userVerificationRequirement(); 195 return NS_OK; 196 } 197 198 NS_IMETHODIMP 199 WebAuthnRegisterArgs::GetAuthenticatorAttachment( 200 nsAString& aAuthenticatorAttachment) { 201 if (mInfo.AuthenticatorSelection().authenticatorAttachment().isNothing()) { 202 return NS_ERROR_NOT_AVAILABLE; 203 } 204 aAuthenticatorAttachment = 205 *mInfo.AuthenticatorSelection().authenticatorAttachment(); 206 return NS_OK; 207 } 208 209 NS_IMETHODIMP 210 WebAuthnRegisterArgs::GetTimeoutMS(uint32_t* aTimeoutMS) { 211 *aTimeoutMS = mInfo.TimeoutMS(); 212 return NS_OK; 213 } 214 215 NS_IMETHODIMP 216 WebAuthnRegisterArgs::GetAttestationConveyancePreference( 217 nsAString& aAttestationConveyancePreference) { 218 const nsString& attPref = mInfo.attestationConveyancePreference(); 219 if (attPref.EqualsLiteral( 220 MOZ_WEBAUTHN_ATTESTATION_CONVEYANCE_PREFERENCE_INDIRECT) || 221 attPref.EqualsLiteral( 222 MOZ_WEBAUTHN_ATTESTATION_CONVEYANCE_PREFERENCE_DIRECT) || 223 attPref.EqualsLiteral( 224 MOZ_WEBAUTHN_ATTESTATION_CONVEYANCE_PREFERENCE_ENTERPRISE)) { 225 aAttestationConveyancePreference.Assign(attPref); 226 } else { 227 aAttestationConveyancePreference.AssignLiteral( 228 MOZ_WEBAUTHN_ATTESTATION_CONVEYANCE_PREFERENCE_NONE); 229 } 230 return NS_OK; 231 } 232 233 NS_IMETHODIMP 234 WebAuthnRegisterArgs::GetPrivateBrowsing(bool* aPrivateBrowsing) { 235 *aPrivateBrowsing = mPrivateBrowsing; 236 return NS_OK; 237 } 238 239 NS_IMETHODIMP 240 WebAuthnRegisterArgs::GetLargeBlobSupportRequired( 241 bool* aLargeBlobSupportRequired) { 242 if (mLargeBlobSupportRequired.isSome()) { 243 *aLargeBlobSupportRequired = mLargeBlobSupportRequired.ref(); 244 return NS_OK; 245 } 246 return NS_ERROR_NOT_AVAILABLE; 247 } 248 249 NS_IMETHODIMP 250 WebAuthnRegisterArgs::GetHints(nsTArray<nsString>& aHints) { 251 aHints.Assign(mInfo.Hints()); 252 return NS_OK; 253 } 254 255 NS_IMPL_ISUPPORTS(WebAuthnSignArgs, nsIWebAuthnSignArgs) 256 257 NS_IMETHODIMP 258 WebAuthnSignArgs::GetOrigin(nsAString& aOrigin) { 259 aOrigin = NS_ConvertUTF8toUTF16(mOrigin); 260 return NS_OK; 261 } 262 263 NS_IMETHODIMP 264 WebAuthnSignArgs::GetRpId(nsAString& aRpId) { 265 aRpId = NS_ConvertUTF8toUTF16(mInfo.RpId()); 266 return NS_OK; 267 } 268 269 NS_IMETHODIMP 270 WebAuthnSignArgs::GetChallenge(nsTArray<uint8_t>& aChallenge) { 271 aChallenge.Assign(mInfo.Challenge()); 272 return NS_OK; 273 } 274 275 NS_IMETHODIMP 276 WebAuthnSignArgs::GetClientDataJSON(nsACString& aClientDataJSON) { 277 aClientDataJSON = mClientDataJSON; 278 return NS_OK; 279 } 280 281 NS_IMETHODIMP 282 WebAuthnSignArgs::GetClientDataHash(nsTArray<uint8_t>& aClientDataHash) { 283 nsresult rv = HashCString(mClientDataJSON, aClientDataHash); 284 if (NS_WARN_IF(NS_FAILED(rv))) { 285 return NS_ERROR_FAILURE; 286 } 287 288 return NS_OK; 289 } 290 291 NS_IMETHODIMP 292 WebAuthnSignArgs::GetAllowList(nsTArray<nsTArray<uint8_t>>& aAllowList) { 293 aAllowList.Clear(); 294 for (const WebAuthnScopedCredential& cred : mInfo.AllowList()) { 295 aAllowList.AppendElement(cred.id().Clone()); 296 } 297 return NS_OK; 298 } 299 300 NS_IMETHODIMP 301 WebAuthnSignArgs::GetAllowListTransports( 302 nsTArray<uint8_t>& aAllowListTransports) { 303 aAllowListTransports.Clear(); 304 for (const WebAuthnScopedCredential& cred : mInfo.AllowList()) { 305 aAllowListTransports.AppendElement(cred.transports()); 306 } 307 return NS_OK; 308 } 309 310 NS_IMETHODIMP 311 WebAuthnSignArgs::GetHmacCreateSecret(bool* aHmacCreateSecret) { 312 for (const WebAuthnExtension& ext : mInfo.Extensions()) { 313 if (ext.type() == WebAuthnExtension::TWebAuthnExtensionHmacSecret) { 314 *aHmacCreateSecret = 315 ext.get_WebAuthnExtensionHmacSecret().hmacCreateSecret(); 316 return NS_OK; 317 } 318 } 319 320 return NS_ERROR_NOT_AVAILABLE; 321 } 322 323 NS_IMETHODIMP 324 WebAuthnSignArgs::GetAppId(nsAString& aAppId) { 325 if (mInfo.AppId().isNothing()) { 326 return NS_ERROR_NOT_AVAILABLE; 327 } 328 aAppId = NS_ConvertUTF8toUTF16(mInfo.AppId().ref()); 329 return NS_OK; 330 } 331 332 NS_IMETHODIMP 333 WebAuthnSignArgs::GetPrf(bool* aPrf) { 334 *aPrf = mPrf; 335 return NS_OK; 336 } 337 338 NS_IMETHODIMP 339 WebAuthnSignArgs::GetPrfEvalFirst(nsTArray<uint8_t>& aEvalFirst) { 340 for (const WebAuthnExtension& ext : mInfo.Extensions()) { 341 if (ext.type() == WebAuthnExtension::TWebAuthnExtensionPrf) { 342 Maybe<WebAuthnExtensionPrfValues> eval = 343 ext.get_WebAuthnExtensionPrf().eval(); 344 if (eval.isSome()) { 345 aEvalFirst.Assign(eval->first()); 346 return NS_OK; 347 } 348 break; 349 } 350 } 351 352 return NS_ERROR_NOT_AVAILABLE; 353 } 354 355 NS_IMETHODIMP 356 WebAuthnSignArgs::GetPrfEvalSecond(nsTArray<uint8_t>& aEvalSecond) { 357 for (const WebAuthnExtension& ext : mInfo.Extensions()) { 358 if (ext.type() == WebAuthnExtension::TWebAuthnExtensionPrf) { 359 Maybe<WebAuthnExtensionPrfValues> eval = 360 ext.get_WebAuthnExtensionPrf().eval(); 361 if (eval.isSome() && eval->secondMaybe()) { 362 aEvalSecond.Assign(eval->second()); 363 return NS_OK; 364 } 365 break; 366 } 367 } 368 369 return NS_ERROR_NOT_AVAILABLE; 370 } 371 372 NS_IMETHODIMP 373 WebAuthnSignArgs::GetPrfEvalByCredentialCredentialId( 374 nsTArray<nsTArray<uint8_t>>& aCredentialIds) { 375 for (const WebAuthnExtension& ext : mInfo.Extensions()) { 376 if (ext.type() == WebAuthnExtension::TWebAuthnExtensionPrf) { 377 if (ext.get_WebAuthnExtensionPrf().evalByCredentialMaybe()) { 378 for (const WebAuthnExtensionPrfEvalByCredentialEntry& entry : 379 ext.get_WebAuthnExtensionPrf().evalByCredential()) { 380 aCredentialIds.AppendElement(entry.credentialId().Clone()); 381 } 382 return NS_OK; 383 } 384 break; 385 } 386 } 387 388 return NS_ERROR_NOT_AVAILABLE; 389 } 390 391 NS_IMETHODIMP 392 WebAuthnSignArgs::GetPrfEvalByCredentialEvalFirst( 393 nsTArray<nsTArray<uint8_t>>& aEvalFirsts) { 394 for (const WebAuthnExtension& ext : mInfo.Extensions()) { 395 if (ext.type() == WebAuthnExtension::TWebAuthnExtensionPrf) { 396 if (ext.get_WebAuthnExtensionPrf().evalByCredentialMaybe()) { 397 for (const WebAuthnExtensionPrfEvalByCredentialEntry& entry : 398 ext.get_WebAuthnExtensionPrf().evalByCredential()) { 399 aEvalFirsts.AppendElement(entry.eval().first().Clone()); 400 } 401 return NS_OK; 402 } 403 break; 404 } 405 } 406 407 return NS_ERROR_NOT_AVAILABLE; 408 } 409 410 NS_IMETHODIMP 411 WebAuthnSignArgs::GetPrfEvalByCredentialEvalSecondMaybe( 412 nsTArray<bool>& aEvalSecondMaybes) { 413 for (const WebAuthnExtension& ext : mInfo.Extensions()) { 414 if (ext.type() == WebAuthnExtension::TWebAuthnExtensionPrf) { 415 if (ext.get_WebAuthnExtensionPrf().evalByCredentialMaybe()) { 416 for (const WebAuthnExtensionPrfEvalByCredentialEntry& entry : 417 ext.get_WebAuthnExtensionPrf().evalByCredential()) { 418 aEvalSecondMaybes.AppendElement(entry.eval().secondMaybe()); 419 } 420 return NS_OK; 421 } 422 break; 423 } 424 } 425 426 return NS_ERROR_NOT_AVAILABLE; 427 } 428 429 NS_IMETHODIMP 430 WebAuthnSignArgs::GetPrfEvalByCredentialEvalSecond( 431 nsTArray<nsTArray<uint8_t>>& aEvalSeconds) { 432 for (const WebAuthnExtension& ext : mInfo.Extensions()) { 433 if (ext.type() == WebAuthnExtension::TWebAuthnExtensionPrf) { 434 if (ext.get_WebAuthnExtensionPrf().evalByCredentialMaybe()) { 435 for (const WebAuthnExtensionPrfEvalByCredentialEntry& entry : 436 ext.get_WebAuthnExtensionPrf().evalByCredential()) { 437 if (entry.eval().secondMaybe()) { 438 aEvalSeconds.AppendElement(entry.eval().second().Clone()); 439 } else { 440 aEvalSeconds.AppendElement(nsTArray<uint8_t>()); 441 } 442 } 443 return NS_OK; 444 } 445 break; 446 } 447 } 448 449 return NS_ERROR_NOT_AVAILABLE; 450 } 451 452 NS_IMETHODIMP 453 WebAuthnSignArgs::GetUserVerification(nsAString& aUserVerificationRequirement) { 454 aUserVerificationRequirement = mInfo.userVerificationRequirement(); 455 return NS_OK; 456 } 457 458 NS_IMETHODIMP 459 WebAuthnSignArgs::GetTimeoutMS(uint32_t* aTimeoutMS) { 460 *aTimeoutMS = mInfo.TimeoutMS(); 461 return NS_OK; 462 } 463 464 NS_IMETHODIMP 465 WebAuthnSignArgs::GetConditionallyMediated(bool* aConditionallyMediated) { 466 *aConditionallyMediated = mInfo.ConditionallyMediated(); 467 return NS_OK; 468 } 469 470 NS_IMETHODIMP 471 WebAuthnSignArgs::GetPrivateBrowsing(bool* aPrivateBrowsing) { 472 *aPrivateBrowsing = mPrivateBrowsing; 473 return NS_OK; 474 } 475 476 NS_IMETHODIMP 477 WebAuthnSignArgs::GetLargeBlobRead(bool* aLargeBlobRead) { 478 if (mLargeBlobRead.isSome()) { 479 *aLargeBlobRead = mLargeBlobRead.ref(); 480 return NS_OK; 481 } 482 return NS_ERROR_NOT_AVAILABLE; 483 } 484 485 NS_IMETHODIMP 486 WebAuthnSignArgs::GetLargeBlobWrite(nsTArray<uint8_t>& aLargeBlobWrite) { 487 if (mLargeBlobRead.isSome() && mLargeBlobRead.ref() == false) { 488 aLargeBlobWrite.Assign(mLargeBlobWrite); 489 return NS_OK; 490 } 491 return NS_ERROR_NOT_AVAILABLE; 492 } 493 494 NS_IMETHODIMP 495 WebAuthnSignArgs::GetHints(nsTArray<nsString>& aHints) { 496 aHints.Assign(mInfo.Hints()); 497 return NS_OK; 498 } 499 500 } // namespace mozilla::dom