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
|
// 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 "CryptoAlgorithmAES_CBC.h"
#include "CryptoAlgorithmAesCbcCfbParams.h"
#include "CryptoAlgorithmECDH.h"
#include "CryptoAlgorithmHMAC.h"
#include "CryptoKeyAES.h"
#include "CryptoKeyEC.h"
#include "CryptoKeyHMAC.h"
#include "DeviceResponseConverter.h"
#include "WebAuthenticationConstants.h"
#include "WebAuthenticationUtils.h"
#include <pal/crypto/CryptoDigest.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;
}
// makePinAuth returns `LEFT(HMAC-SHA-256(secret, data), 16)`.
static Vector<uint8_t> makePinAuth(const CryptoKeyHMAC& key, const Vector<uint8_t>& data)
{
auto result = CryptoAlgorithmHMAC::platformSign(key, data);
ASSERT(!result.hasException());
auto pinAuth = result.releaseReturnValue();
pinAuth.shrink(16);
return pinAuth;
}
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, Function<void(CBORValue::MapValue*)> addAdditional = nullptr)
{
CBORValue::MapValue map;
map.emplace(static_cast<int64_t>(RequestKey::kProtocol), kProtocolVersion);
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))
{
}
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);
Vector<uint8_t> x { rawPublicKey.data() + 1, ES256FieldElementLength };
Vector<uint8_t> y { rawPublicKey.data() + 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(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 tokenResult = CryptoAlgorithmAES_CBC::platformDecrypt({ }, sharedKey, encryptedToken, CryptoAlgorithmAES_CBC::Padding::No);
if (tokenResult.hasException())
return std::nullopt;
auto token = tokenResult.releaseReturnValue();
auto tokenKey = CryptoKeyHMAC::importRaw(token.size() * 8, CryptoAlgorithmIdentifier::SHA_256, WTFMove(token), true, CryptoKeyUsageSign);
ASSERT(tokenKey);
return TokenResponse(tokenKey.releaseNonNull());
}
Vector<uint8_t> TokenResponse::pinAuth(const Vector<uint8_t>& clientDataHash) const
{
return makePinAuth(m_token, clientDataHash);
}
const Vector<uint8_t>& TokenResponse::token() const
{
return m_token->key();
}
Vector<uint8_t> encodeAsCBOR(const RetriesRequest&)
{
return encodePinCommand(Subcommand::kGetRetries);
}
Vector<uint8_t> encodeAsCBOR(const KeyAgreementRequest&)
{
return encodePinCommand(Subcommand::kGetKeyAgreement);
}
std::optional<TokenRequest> TokenRequest::tryCreate(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 and SHA-256 to compute the shared AES-CBC key.
auto sharedKeyResult = CryptoAlgorithmECDH::platformDeriveBits(downcast<CryptoKeyEC>(*keyPair.privateKey), peerKey);
if (!sharedKeyResult)
return std::nullopt;
auto crypto = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_256);
crypto->addBytes(sharedKeyResult->data(), sharedKeyResult->size());
auto sharedKeyHash = crypto->computeHash();
auto sharedKey = CryptoKeyAES::importRaw(CryptoAlgorithmIdentifier::AES_CBC, WTFMove(sharedKeyHash), 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.
crypto = PAL::CryptoDigest::create(PAL::CryptoDigest::Algorithm::SHA_256);
crypto->addBytes(pin.data(), pin.length());
auto pinHash = crypto->computeHash();
pinHash.shrink(16);
return TokenRequest(sharedKey.releaseNonNull(), WTFMove(coseKey), WTFMove(pinHash));
}
TokenRequest::TokenRequest(Ref<WebCore::CryptoKeyAES>&& sharedKey, cbor::CBORValue::MapValue&& coseKey, Vector<uint8_t>&& pinHash)
: m_sharedKey(WTFMove(sharedKey))
, m_coseKey(WTFMove(coseKey))
, m_pinHash(WTFMove(pinHash))
{
}
const CryptoKeyAES& TokenRequest::sharedKey() const
{
return m_sharedKey;
}
Vector<uint8_t> encodeAsCBOR(const TokenRequest& request)
{
auto result = CryptoAlgorithmAES_CBC::platformEncrypt({ }, request.sharedKey(), request.m_pinHash, CryptoAlgorithmAES_CBC::Padding::No);
ASSERT(!result.hasException());
return encodePinCommand(Subcommand::kGetPinToken, [coseKey = WTFMove(request.m_coseKey), encryptedPin = result.releaseReturnValue()] (CBORValue::MapValue* map) mutable {
map->emplace(static_cast<int64_t>(RequestKey::kKeyAgreement), WTFMove(coseKey));
map->emplace(static_cast<int64_t>(RequestKey::kPinHashEnc), WTFMove(encryptedPin));
});
}
} // namespace pin
} // namespace fido
#endif // ENABLE(WEB_AUTHN)
|