1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
|
// Copyright 2019 The Chromium Authors. All rights reserved.
// Copyright (C) 2019 Apple Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "config.h"
#include "Pin.h"
#if ENABLE(WEB_AUTHN)
#include "CBORReader.h"
#include "CBORWriter.h"
#include "CryptoAlgorithmAESCBC.h"
#include "CryptoAlgorithmAesCbcCfbParams.h"
#include "CryptoAlgorithmECDH.h"
#include "CryptoAlgorithmHKDF.h"
#include "CryptoAlgorithmHMAC.h"
#include "CryptoAlgorithmHkdfParams.h"
#include "CryptoKeyAES.h"
#include "CryptoKeyEC.h"
#include "CryptoKeyHMAC.h"
#include "CryptoKeyRaw.h"
#include "DeviceResponseConverter.h"
#include "ExceptionOr.h"
#include "WebAuthenticationConstants.h"
#include "WebAuthenticationUtils.h"
#include <pal/crypto/CryptoDigest.h>
#include <wtf/CryptographicallyRandomNumber.h>
namespace fido {
using namespace WebCore;
using CBOR = cbor::CBORValue;
namespace pin {
using namespace cbor;
// hasAtLeastFourCodepoints returns true if |pin| contains
// four or more code points. This reflects the "4 Unicode characters"
// requirement in CTAP2.
static bool hasAtLeastFourCodepoints(const String& pin)
{
return pin.length() >= 4;
}
static Vector<uint8_t> decryptForProtocol(PINUVAuthProtocol protocol, const CryptoKeyAES& key, const Vector<uint8_t>& ciphertext)
{
if (protocol == PINUVAuthProtocol::kPinProtocol2) {
// CTAP 2.1 spec 6.5.7: Protocol 2 decrypt
// Split ciphertext into IV (first 16 bytes) and ct (remaining bytes)
if (ciphertext.size() < 16)
return { };
Vector<uint8_t> iv(ciphertext.subspan(0, 16));
Vector<uint8_t> ct(ciphertext.subspan(16));
CryptoAlgorithmAesCbcCfbParams params;
params.iv = BufferSource(iv);
auto result = CryptoAlgorithmAESCBC::platformDecrypt(params, key, ct, CryptoAlgorithmAESCBC::Padding::No);
if (result.hasException())
return { };
return result.releaseReturnValue();
}
// CTAP 2.1 spec 6.5.6: Protocol 1 decrypt with zero IV
auto result = CryptoAlgorithmAESCBC::platformDecrypt({ }, key, ciphertext, CryptoAlgorithmAESCBC::Padding::No);
if (result.hasException())
return { };
return result.releaseReturnValue();
}
static Vector<uint8_t> authenticateForProtocol(PINUVAuthProtocol protocol, const CryptoKeyHMAC& key, const Vector<uint8_t>& message)
{
auto result = CryptoAlgorithmHMAC::platformSign(key, message);
ASSERT(!result.hasException());
auto signature = result.releaseReturnValue();
// https://fidoalliance.org/specs/fido-v2.1-ps-20210615/fido-client-to-authenticator-protocol-v2.1-ps-20210615.html#pinProto1
// Pin Protocol 1 should trim to 16 bytes, Pin Protocol 2 uses full 32.
if (protocol == PINUVAuthProtocol::kPinProtocol1)
signature.shrink(16);
return signature;
}
std::optional<CString> validateAndConvertToUTF8(const String& pin)
{
if (!hasAtLeastFourCodepoints(pin))
return std::nullopt;
auto result = pin.utf8();
if (result.length() < kMinBytes || result.length() > kMaxBytes)
return std::nullopt;
return result;
}
// encodePINCommand returns a CTAP2 PIN command for the operation |subcommand|.
// Additional elements of the top-level CBOR map can be added with the optional
// |addAdditional| callback.
static Vector<uint8_t> encodePinCommand(Subcommand subcommand, PINUVAuthProtocol protocol, Function<void(CBORValue::MapValue*)> addAdditional = nullptr)
{
CBORValue::MapValue map;
map.emplace(static_cast<int64_t>(RequestKey::kProtocol), static_cast<int64_t>(protocol));
map.emplace(static_cast<int64_t>(RequestKey::kSubcommand), static_cast<int64_t>(subcommand));
if (addAdditional)
addAdditional(&map);
auto serializedParam = CBORWriter::write(CBORValue(WTFMove(map)));
ASSERT(serializedParam);
Vector<uint8_t> cborRequest({ static_cast<uint8_t>(CtapRequestCommand::kAuthenticatorClientPin) });
cborRequest.appendVector(*serializedParam);
return cborRequest;
}
RetriesResponse::RetriesResponse() = default;
std::optional<RetriesResponse> RetriesResponse::parse(const Vector<uint8_t>& inBuffer)
{
auto decodedMap = decodeResponseMap(inBuffer);
if (!decodedMap)
return std::nullopt;
const auto& responseMap = decodedMap->getMap();
auto it = responseMap.find(CBORValue(static_cast<int64_t>(ResponseKey::kRetries)));
if (it == responseMap.end() || !it->second.isUnsigned())
return std::nullopt;
RetriesResponse ret;
ret.retries = static_cast<uint64_t>(it->second.getUnsigned());
return ret;
}
KeyAgreementResponse::KeyAgreementResponse(Ref<CryptoKeyEC>&& peerKey)
: peerKey(WTFMove(peerKey))
{
}
KeyAgreementResponse::~KeyAgreementResponse() = default;
KeyAgreementResponse::KeyAgreementResponse(KeyAgreementResponse&&) = default;
KeyAgreementResponse& KeyAgreementResponse::operator=(KeyAgreementResponse&&) = default;
std::optional<KeyAgreementResponse> KeyAgreementResponse::parse(const Vector<uint8_t>& inBuffer)
{
auto decodedMap = decodeResponseMap(inBuffer);
if (!decodedMap)
return std::nullopt;
const auto& responseMap = decodedMap->getMap();
// The ephemeral key is encoded as a COSE structure.
auto it = responseMap.find(CBORValue(static_cast<int64_t>(ResponseKey::kKeyAgreement)));
if (it == responseMap.end() || !it->second.isMap())
return std::nullopt;
const auto& coseKey = it->second.getMap();
return parseFromCOSE(coseKey);
}
std::optional<KeyAgreementResponse> KeyAgreementResponse::parseFromCOSE(const CBORValue::MapValue& coseKey)
{
// The COSE key must be a P-256 point. See
// https://tools.ietf.org/html/rfc8152#section-7.1
for (const auto& pair : Vector<std::pair<int64_t, int64_t>>({
{ static_cast<int64_t>(COSE::kty), static_cast<int64_t>(COSE::EC2) },
{ static_cast<int64_t>(COSE::alg), static_cast<int64_t>(COSE::ECDH256) },
{ static_cast<int64_t>(COSE::crv), static_cast<int64_t>(COSE::P_256) },
})) {
auto it = coseKey.find(CBORValue(pair.first));
if (it == coseKey.end() || !it->second.isInteger() || it->second.getInteger() != pair.second)
return std::nullopt;
}
// See https://tools.ietf.org/html/rfc8152#section-13.1.1
const auto& xIt = coseKey.find(CBORValue(static_cast<int64_t>(COSE::x)));
const auto& yIt = coseKey.find(CBORValue(static_cast<int64_t>(COSE::y)));
if (xIt == coseKey.end() || yIt == coseKey.end() || !xIt->second.isByteString() || !yIt->second.isByteString())
return std::nullopt;
const auto& x = xIt->second.getByteString();
const auto& y = yIt->second.getByteString();
auto peerKey = CryptoKeyEC::importRaw(CryptoAlgorithmIdentifier::ECDH, "P-256"_s, encodeRawPublicKey(x, y), true, CryptoKeyUsageDeriveBits);
if (!peerKey)
return std::nullopt;
return KeyAgreementResponse(peerKey.releaseNonNull());
}
cbor::CBORValue::MapValue encodeCOSEPublicKey(const Vector<uint8_t>& rawPublicKey)
{
ASSERT(rawPublicKey.size() == 65);
auto x = rawPublicKey.subvector(1, ES256FieldElementLength);
auto y = rawPublicKey.subvector(1 + ES256FieldElementLength, ES256FieldElementLength);
cbor::CBORValue::MapValue publicKeyMap;
publicKeyMap[cbor::CBORValue(COSE::kty)] = cbor::CBORValue(COSE::EC2);
publicKeyMap[cbor::CBORValue(COSE::alg)] = cbor::CBORValue(COSE::ECDH256);
publicKeyMap[cbor::CBORValue(COSE::crv)] = cbor::CBORValue(COSE::P_256);
publicKeyMap[cbor::CBORValue(COSE::x)] = cbor::CBORValue(WTFMove(x));
publicKeyMap[cbor::CBORValue(COSE::y)] = cbor::CBORValue(WTFMove(y));
return publicKeyMap;
}
TokenResponse::TokenResponse(Ref<WebCore::CryptoKeyHMAC>&& token)
: m_token(WTFMove(token))
{
}
std::optional<TokenResponse> TokenResponse::parse(PINUVAuthProtocol protocol, const WebCore::CryptoKeyAES& sharedKey, const Vector<uint8_t>& inBuffer)
{
auto decodedMap = decodeResponseMap(inBuffer);
if (!decodedMap)
return std::nullopt;
const auto& responseMap = decodedMap->getMap();
auto it = responseMap.find(CBORValue(static_cast<int64_t>(ResponseKey::kPinToken)));
if (it == responseMap.end() || !it->second.isByteString())
return std::nullopt;
const auto& encryptedToken = it->second.getByteString();
auto token = decryptForProtocol(protocol, sharedKey, encryptedToken);
if (token.isEmpty())
return std::nullopt;
auto tokenKey = CryptoKeyHMAC::importRaw(token.size() * 8, CryptoAlgorithmIdentifier::SHA_256, WTFMove(token), true, CryptoKeyUsageSign);
if (!tokenKey)
return std::nullopt;
return TokenResponse(tokenKey.releaseNonNull());
}
Vector<uint8_t> TokenResponse::pinAuth(PINUVAuthProtocol protocol, const Vector<uint8_t>& clientDataHash) const
{
return authenticateForProtocol(protocol, m_token, clientDataHash);
}
const Vector<uint8_t>& TokenResponse::token() const
{
return m_token->key();
}
Vector<uint8_t> encodeAsCBOR(const RetriesRequest& request)
{
return encodePinCommand(Subcommand::kGetRetries, request.protocol);
}
Vector<uint8_t> encodeAsCBOR(const KeyAgreementRequest& request)
{
return encodePinCommand(Subcommand::kGetKeyAgreement, request.protocol);
}
static Vector<uint8_t> deriveProtocolSharedSecret(PINUVAuthProtocol protocol, Vector<uint8_t>&& ecdhResult)
{
// CTAP spec 6.5.6 (Protocol 1) and 6.5.7 (Protocol 2).
Vector<uint8_t> sharedSecret;
if (protocol == PINUVAuthProtocol::kPinProtocol1) {
auto crypto = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_256);
crypto->addBytes(ecdhResult.span());
sharedSecret = crypto->computeHash();
} else if (protocol == PINUVAuthProtocol::kPinProtocol2) {
sharedSecret.reserveInitialCapacity(64);
auto hkdfKey = CryptoKeyRaw::create(CryptoAlgorithmIdentifier::HKDF, WTFMove(ecdhResult), CryptoKeyUsageDeriveBits);
CryptoAlgorithmHkdfParams hmacHkdfParams;
hmacHkdfParams.hashIdentifier = CryptoAlgorithmIdentifier::SHA_256;
Vector<uint8_t> hkdfSalt(32, 0);
hmacHkdfParams.salt = toBufferSource(hkdfSalt.span());
hmacHkdfParams.info = toBufferSource(std::span { kHKDFInfoHMACKey });
auto hmacKeyMaterial = CryptoAlgorithmHKDF::deriveBits(hmacHkdfParams, hkdfKey.get(), 32 * 8);
if (hmacKeyMaterial.hasException())
return { };
sharedSecret.appendVector(hmacKeyMaterial.releaseReturnValue());
CryptoAlgorithmHkdfParams aesHkdfParams;
aesHkdfParams.hashIdentifier = CryptoAlgorithmIdentifier::SHA_256;
aesHkdfParams.salt = toBufferSource(hkdfSalt.span());
aesHkdfParams.info = toBufferSource(std::span { kHKDFInfoAESKey });
auto aesKeyMaterial = CryptoAlgorithmHKDF::deriveBits(aesHkdfParams, hkdfKey.get(), 32 * 8);
if (aesKeyMaterial.hasException())
return { };
sharedSecret.appendVector(aesKeyMaterial.releaseReturnValue());
} else {
ASSERT_NOT_REACHED();
return { };
}
return sharedSecret;
}
static Vector<uint8_t> encryptForProtocol(PINUVAuthProtocol protocol, const CryptoKeyAES& key, const Vector<uint8_t>& plaintext)
{
if (protocol == PINUVAuthProtocol::kPinProtocol2) {
Vector<uint8_t> iv(16);
cryptographicallyRandomValues(iv.mutableSpan());
CryptoAlgorithmAesCbcCfbParams params;
params.iv = BufferSource(iv);
auto result = CryptoAlgorithmAESCBC::platformEncrypt(params, key, plaintext, CryptoAlgorithmAESCBC::Padding::No);
ASSERT(!result.hasException());
Vector<uint8_t> output;
output.reserveInitialCapacity(iv.size() + result.returnValue().size());
output.appendVector(iv);
output.appendVector(result.releaseReturnValue());
return output;
}
auto result = CryptoAlgorithmAESCBC::platformEncrypt({ }, key, plaintext, CryptoAlgorithmAESCBC::Padding::No);
ASSERT(!result.hasException());
return result.releaseReturnValue();
}
std::optional<TokenRequest> TokenRequest::tryCreate(PINUVAuthProtocol protocol, const CString& pin, const CryptoKeyEC& peerKey)
{
// The following implements Section 5.5.4 Getting sharedSecret from Authenticator.
// https://fidoalliance.org/specs/fido-v2.0-ps-20190130/fido-client-to-authenticator-protocol-v2.0-ps-20190130.html#gettingSharedSecret
// 1. Generate a P256 key pair.
auto keyPairResult = CryptoKeyEC::generatePair(CryptoAlgorithmIdentifier::ECDH, "P-256"_s, true, CryptoKeyUsageDeriveBits);
ASSERT(!keyPairResult.hasException());
auto keyPair = keyPairResult.releaseReturnValue();
// 2. Use ECDH to compute the shared secret, then apply protocol-specific KDF.
auto sharedKeyResult = CryptoAlgorithmECDH::platformDeriveBits(downcast<CryptoKeyEC>(*keyPair.privateKey), peerKey);
if (!sharedKeyResult)
return std::nullopt;
auto sharedSecret = deriveProtocolSharedSecret(protocol, WTFMove(*sharedKeyResult));
if (sharedSecret.isEmpty())
return std::nullopt;
Vector<uint8_t> aesKeyMaterial;
if (protocol == PINUVAuthProtocol::kPinProtocol2) {
ASSERT(sharedSecret.size() == 64);
aesKeyMaterial = Vector<uint8_t>(sharedSecret.span().last(32));
} else
aesKeyMaterial = sharedSecret;
auto sharedKey = CryptoKeyAES::importRaw(CryptoAlgorithmIdentifier::AES_CBC, WTFMove(aesKeyMaterial), true, CryptoKeyUsageEncrypt | CryptoKeyUsageDecrypt);
ASSERT(sharedKey);
// The following encodes the public key of the above key pair into COSE format.
auto rawPublicKeyResult = downcast<CryptoKeyEC>(*keyPair.publicKey).exportRaw();
ASSERT(!rawPublicKeyResult.hasException());
auto coseKey = encodeCOSEPublicKey(rawPublicKeyResult.returnValue());
// The following calculates a SHA-256 digest of the PIN, and shrink to the left 16 bytes.
auto crypto = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_256);
crypto->addBytes(byteCast<uint8_t>(pin.span()));
auto pinHash = crypto->computeHash();
pinHash.shrink(16);
return TokenRequest(sharedKey.releaseNonNull(), WTFMove(coseKey), WTFMove(pinHash), protocol);
}
TokenRequest::TokenRequest(Ref<WebCore::CryptoKeyAES>&& sharedKey, cbor::CBORValue::MapValue&& coseKey, Vector<uint8_t>&& pinHash, PINUVAuthProtocol protocol)
: m_sharedKey(WTFMove(sharedKey))
, m_coseKey(WTFMove(coseKey))
, m_pinHash(WTFMove(pinHash))
, m_protocol(protocol)
{
}
SetPinRequest::SetPinRequest(Ref<WebCore::CryptoKeyAES>&& sharedKey, cbor::CBORValue::MapValue&& coseKey, Vector<uint8_t>&& newPinEnc, Vector<uint8_t>&& pinUvAuthParam, PINUVAuthProtocol protocol)
: m_sharedKey(WTFMove(sharedKey))
, m_coseKey(WTFMove(coseKey))
, m_newPinEnc(WTFMove(newPinEnc))
, m_pinUvAuthParam(WTFMove(pinUvAuthParam))
, m_protocol(protocol)
{
}
const Vector<uint8_t>& SetPinRequest::pinAuth() const
{
return m_pinUvAuthParam;
}
std::optional<SetPinRequest> SetPinRequest::tryCreate(PINUVAuthProtocol protocol, const String& inputPin, const WebCore::CryptoKeyEC& peerKey)
{
std::optional<CString> newPin = validateAndConvertToUTF8(inputPin);
if (!newPin)
return std::nullopt;
// The following implements Section 5.5.4 Getting sharedSecret from Authenticator.
// https://fidoalliance.org/specs/fido-v2.0-ps-20190130/fido-client-to-authenticator-protocol-v2.0-ps-20190130.html#gettingSharedSecret
// 1. Generate a P256 key pair.
auto keyPairResult = CryptoKeyEC::generatePair(CryptoAlgorithmIdentifier::ECDH, "P-256"_s, true, CryptoKeyUsageDeriveBits);
ASSERT(!keyPairResult.hasException());
auto keyPair = keyPairResult.releaseReturnValue();
auto sharedKeyResult = CryptoAlgorithmECDH::platformDeriveBits(downcast<CryptoKeyEC>(*keyPair.privateKey), peerKey);
if (!sharedKeyResult)
return std::nullopt;
auto sharedSecret = deriveProtocolSharedSecret(protocol, WTFMove(*sharedKeyResult));
if (sharedSecret.isEmpty())
return std::nullopt;
Vector<uint8_t> hmacKeyMaterial, aesKeyMaterial;
if (protocol == PINUVAuthProtocol::kPinProtocol2) {
ASSERT(sharedSecret.size() == 64);
hmacKeyMaterial = Vector<uint8_t>(sharedSecret.span().first(32));
aesKeyMaterial = Vector<uint8_t>(sharedSecret.span().last(32));
} else {
hmacKeyMaterial = sharedSecret;
aesKeyMaterial = sharedSecret;
}
auto sharedKey = CryptoKeyAES::importRaw(CryptoAlgorithmIdentifier::AES_CBC, WTFMove(aesKeyMaterial), true, CryptoKeyUsageEncrypt | CryptoKeyUsageDecrypt);
ASSERT(sharedKey);
// The following encodes the public key of the above key pair into COSE format.
auto rawPublicKeyResult = downcast<CryptoKeyEC>(*keyPair.publicKey).exportRaw();
ASSERT(!rawPublicKeyResult.hasException());
auto coseKey = encodeCOSEPublicKey(rawPublicKeyResult.returnValue());
const size_t minPaddedPinLength = 64;
Vector<uint8_t> paddedPin;
paddedPin.reserveInitialCapacity(minPaddedPinLength);
paddedPin.append(inputPin.utf8().span());
for (int i = paddedPin.size(); i < 64; i++)
paddedPin.append('\0');
auto hmacKey = CryptoKeyHMAC::importRaw(hmacKeyMaterial.size() * 8 /* lengthInBits */, CryptoAlgorithmIdentifier::SHA_256, WTFMove(hmacKeyMaterial), true, CryptoKeyUsageSign);
auto newPinEnc = encryptForProtocol(protocol, *sharedKey, paddedPin);
auto pinUvAuthParam = authenticateForProtocol(protocol, *hmacKey, newPinEnc);
return SetPinRequest(sharedKey.releaseNonNull(), WTFMove(coseKey), WTFMove(newPinEnc), WTFMove(pinUvAuthParam), protocol);
}
Vector<uint8_t> encodeAsCBOR(const TokenRequest& request)
{
auto encryptedPin = encryptForProtocol(request.m_protocol, request.sharedKey(), request.m_pinHash);
return encodePinCommand(Subcommand::kGetPinToken, request.m_protocol, [coseKey = WTFMove(request.m_coseKey), encryptedPin = WTFMove(encryptedPin)] (CBORValue::MapValue* map) mutable {
map->emplace(static_cast<int64_t>(RequestKey::kKeyAgreement), WTFMove(coseKey));
map->emplace(static_cast<int64_t>(RequestKey::kPinHashEnc), WTFMove(encryptedPin));
});
}
Vector<uint8_t> encodeAsCBOR(const SetPinRequest& request)
{
return encodePinCommand(Subcommand::kSetPin, request.m_protocol, [coseKey = WTFMove(request.m_coseKey), encryptedPin = request.m_newPinEnc, pinUvAuthParam = request.m_pinUvAuthParam] (CBORValue::MapValue* map) mutable {
map->emplace(static_cast<int64_t>(RequestKey::kKeyAgreement), WTFMove(coseKey));
map->emplace(static_cast<int64_t>(RequestKey::kNewPinEnc), WTFMove(encryptedPin));
map->emplace(static_cast<int64_t>(RequestKey::kPinAuth), WTFMove(pinUvAuthParam));
});
}
// HmacSecretRequest implementation
HmacSecretRequest::HmacSecretRequest(Ref<CryptoKeyAES>&& sharedKey, CBORValue::MapValue&& coseKey, Vector<uint8_t>&& saltEnc, Vector<uint8_t>&& saltAuth, PINUVAuthProtocol protocol)
: m_sharedKey(WTFMove(sharedKey))
, m_coseKey(WTFMove(coseKey))
, m_saltEnc(WTFMove(saltEnc))
, m_saltAuth(WTFMove(saltAuth))
, m_protocol(protocol)
{
}
std::optional<HmacSecretRequest> HmacSecretRequest::create(PINUVAuthProtocol protocol, const Vector<uint8_t>& salt1, const std::optional<Vector<uint8_t>>& salt2, RefPtr<CryptoKeyEC>&& peerKey)
{
if (salt1.size() != 32)
return std::nullopt;
if (salt2 && salt2->size() != 32)
return std::nullopt;
// The following implements Section 5.5.4 Getting sharedSecret from Authenticator
auto keyPairResult = CryptoKeyEC::generatePair(CryptoAlgorithmIdentifier::ECDH, "P-256"_s, true, CryptoKeyUsageDeriveBits);
if (keyPairResult.hasException())
return std::nullopt;
auto keyPair = keyPairResult.releaseReturnValue();
auto sharedKeyResult = CryptoAlgorithmECDH::platformDeriveBits(downcast<CryptoKeyEC>(*keyPair.privateKey), *peerKey);
if (!sharedKeyResult)
return std::nullopt;
auto sharedSecret = deriveProtocolSharedSecret(protocol, WTFMove(*sharedKeyResult));
if (sharedSecret.isEmpty())
return std::nullopt;
Vector<uint8_t> hmacKeyMaterial, aesKeyMaterial;
if (protocol == PINUVAuthProtocol::kPinProtocol2) {
ASSERT(sharedSecret.size() == 64);
hmacKeyMaterial = Vector<uint8_t>(sharedSecret.span().first(32));
aesKeyMaterial = Vector<uint8_t>(sharedSecret.span().last(32));
} else {
hmacKeyMaterial = sharedSecret;
aesKeyMaterial = sharedSecret;
}
auto sharedKey = CryptoKeyAES::importRaw(CryptoAlgorithmIdentifier::AES_CBC, WTFMove(aesKeyMaterial), true, CryptoKeyUsageEncrypt | CryptoKeyUsageDecrypt);
if (!sharedKey)
return std::nullopt;
auto rawPublicKeyResult = downcast<CryptoKeyEC>(*keyPair.publicKey).exportRaw();
if (rawPublicKeyResult.hasException())
return std::nullopt;
auto coseKey = encodeCOSEPublicKey(rawPublicKeyResult.returnValue());
Vector<uint8_t> saltsBuffer = salt1;
if (salt2)
saltsBuffer.appendVector(*salt2);
auto saltEnc = encryptForProtocol(protocol, *sharedKey, saltsBuffer);
auto hmacKey = CryptoKeyHMAC::importRaw(hmacKeyMaterial.size() * 8, CryptoAlgorithmIdentifier::SHA_256, WTFMove(hmacKeyMaterial), true, CryptoKeyUsageSign);
if (!hmacKey)
return std::nullopt;
auto saltAuth = authenticateForProtocol(protocol, *hmacKey, saltEnc);
return HmacSecretRequest(sharedKey.releaseNonNull(), WTFMove(coseKey), WTFMove(saltEnc), WTFMove(saltAuth), protocol);
}
// HmacSecretResponse implementation
HmacSecretResponse::HmacSecretResponse(Vector<uint8_t>&& decryptedOutput)
: m_output(WTFMove(decryptedOutput))
{
}
std::optional<HmacSecretResponse> HmacSecretResponse::parse(PINUVAuthProtocol protocol, const CryptoKeyAES& sharedKey, const Vector<uint8_t>& encryptedOutput)
{
auto output = decryptForProtocol(protocol, sharedKey, encryptedOutput);
if (output.isEmpty())
return std::nullopt;
if (output.size() != 32 && output.size() != 64)
return std::nullopt;
return HmacSecretResponse(WTFMove(output));
}
const Vector<uint8_t>& HmacSecretResponse::output() const
{
return m_output;
}
} // namespace pin
} // namespace fido
#endif // ENABLE(WEB_AUTHN)
|