Changeset 256201 in webkit


Ignore:
Timestamp:
Feb 10, 2020 12:26:04 PM (4 years ago)
Author:
jiewen_tan@apple.com
Message:

[WebAuthn] Merge some of the CTAP response decoder's code
https://bugs.webkit.org/show_bug.cgi?id=205375

Reviewed by Darin Adler.

This patch makes those code more compact and maintainable.

No change of behaviors.

  • Modules/webauthn/fido/DeviceResponseConverter.cpp:

(fido::decodeResponseMap):
(fido::readCTAPMakeCredentialResponse):
(fido::readCTAPGetAssertionResponse):
(fido::readCTAPGetInfoResponse):

  • Modules/webauthn/fido/DeviceResponseConverter.h:
  • Modules/webauthn/fido/Pin.cpp:

(fido::pin::encodePinCommand):
(fido::pin::RetriesResponse::parse):
(fido::pin::KeyAgreementResponse::parse):
(fido::pin::TokenResponse::parse):

Location:
trunk/Source/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r256196 r256201  
     12020-02-10  Jiewen Tan  <jiewen_tan@apple.com>
     2
     3        [WebAuthn] Merge some of the CTAP response decoder's code
     4        https://bugs.webkit.org/show_bug.cgi?id=205375
     5
     6        Reviewed by Darin Adler.
     7
     8        This patch makes those code more compact and maintainable.
     9
     10        No change of behaviors.
     11
     12        * Modules/webauthn/fido/DeviceResponseConverter.cpp:
     13        (fido::decodeResponseMap):
     14        (fido::readCTAPMakeCredentialResponse):
     15        (fido::readCTAPGetAssertionResponse):
     16        (fido::readCTAPGetInfoResponse):
     17        * Modules/webauthn/fido/DeviceResponseConverter.h:
     18        * Modules/webauthn/fido/Pin.cpp:
     19        (fido::pin::encodePinCommand):
     20        (fido::pin::RetriesResponse::parse):
     21        (fido::pin::KeyAgreementResponse::parse):
     22        (fido::pin::TokenResponse::parse):
     23
    1242020-02-10  Megan Gardner  <megan_gardner@apple.com>
    225
  • trunk/Source/WebCore/Modules/webauthn/fido/DeviceResponseConverter.cpp

    r254356 r256201  
    5555}
    5656
     57Optional<cbor::CBORValue> decodeResponseMap(const Vector<uint8_t>& inBuffer)
     58{
     59    if (inBuffer.size() <= kResponseCodeLength || getResponseCode(inBuffer) != CtapDeviceResponseCode::kSuccess)
     60        return WTF::nullopt;
     61
     62    Vector<uint8_t> buffer;
     63    buffer.append(inBuffer.data() + 1, inBuffer.size() - 1);
     64    Optional<CBOR> decodedResponse = cbor::CBORReader::read(buffer);
     65    if (!decodedResponse || !decodedResponse->isMap())
     66        return WTF::nullopt;
     67    return decodedResponse;
     68}
     69
    5770CtapDeviceResponseCode getResponseCode(const Vector<uint8_t>& buffer)
    5871{
     
    8699RefPtr<AuthenticatorAttestationResponse> readCTAPMakeCredentialResponse(const Vector<uint8_t>& inBuffer, const AttestationConveyancePreference& attestation)
    87100{
    88     if (inBuffer.size() <= kResponseCodeLength)
    89         return nullptr;
    90 
    91     Vector<uint8_t> buffer;
    92     buffer.append(inBuffer.data() + 1, inBuffer.size() - 1);
    93     Optional<CBOR> decodedResponse = cbor::CBORReader::read(buffer);
    94     if (!decodedResponse || !decodedResponse->isMap())
    95         return nullptr;
    96     const auto& decodedMap = decodedResponse->getMap();
    97 
    98     auto it = decodedMap.find(CBOR(1));
    99     if (it == decodedMap.end() || !it->second.isString())
     101    auto decodedMap = decodeResponseMap(inBuffer);
     102    if (!decodedMap)
     103        return nullptr;
     104    const auto& responseMap = decodedMap->getMap();
     105
     106    auto it = responseMap.find(CBOR(1));
     107    if (it == responseMap.end() || !it->second.isString())
    100108        return nullptr;
    101109    auto format = it->second.clone();
    102110
    103     it = decodedMap.find(CBOR(2));
    104     if (it == decodedMap.end() || !it->second.isByteString())
     111    it = responseMap.find(CBOR(2));
     112    if (it == responseMap.end() || !it->second.isByteString())
    105113        return nullptr;
    106114    auto authenticatorData = it->second.clone();
     
    110118        return nullptr;
    111119
    112     it = decodedMap.find(CBOR(3));
    113     if (it == decodedMap.end() || !it->second.isMap())
     120    it = responseMap.find(CBOR(3));
     121    if (it == responseMap.end() || !it->second.isMap())
    114122        return nullptr;
    115123    auto attStmt = it->second.clone();
     
    134142RefPtr<AuthenticatorAssertionResponse> readCTAPGetAssertionResponse(const Vector<uint8_t>& inBuffer)
    135143{
    136     if (inBuffer.size() <= kResponseCodeLength)
    137         return nullptr;
    138 
    139     Vector<uint8_t> buffer;
    140     buffer.append(inBuffer.data() + 1, inBuffer.size() - 1);
    141     Optional<CBOR> decodedResponse = cbor::CBORReader::read(buffer);
    142 
    143     if (!decodedResponse || !decodedResponse->isMap())
    144         return nullptr;
    145 
    146     auto& responseMap = decodedResponse->getMap();
     144    auto decodedMap = decodeResponseMap(inBuffer);
     145    if (!decodedMap)
     146        return nullptr;
     147    const auto& responseMap = decodedMap->getMap();
    147148
    148149    auto it = responseMap.find(CBOR(1));
     
    201202Optional<AuthenticatorGetInfoResponse> readCTAPGetInfoResponse(const Vector<uint8_t>& inBuffer)
    202203{
    203     if (inBuffer.size() <= kResponseCodeLength || getResponseCode(inBuffer) != CtapDeviceResponseCode::kSuccess)
    204         return WTF::nullopt;
    205 
    206     Vector<uint8_t> buffer;
    207     buffer.append(inBuffer.data() + 1, inBuffer.size() - 1);
    208     Optional<CBOR> decodedResponse = cbor::CBORReader::read(buffer);
    209     if (!decodedResponse || !decodedResponse->isMap())
    210         return WTF::nullopt;
    211     const auto& responseMap = decodedResponse->getMap();
     204    auto decodedMap = decodeResponseMap(inBuffer);
     205    if (!decodedMap)
     206        return WTF::nullopt;
     207    const auto& responseMap = decodedMap->getMap();
    212208
    213209    auto it = responseMap.find(CBOR(1));
  • trunk/Source/WebCore/Modules/webauthn/fido/DeviceResponseConverter.h

    r253398 r256201  
    4343namespace fido {
    4444
     45Optional<cbor::CBORValue> decodeResponseMap(const Vector<uint8_t>&);
     46
    4547// Parses response code from response received from the authenticator. If
    4648// unknown response code value is received, then CTAP2_ERR_OTHER is returned.
  • trunk/Source/WebCore/Modules/webauthn/fido/Pin.cpp

    r254439 r256201  
    103103        addAdditional(&map);
    104104
    105     // FIXME(205375)
    106105    auto serializedParam = CBORWriter::write(CBORValue(WTFMove(map)));
    107106    ASSERT(serializedParam);
     
    116115Optional<RetriesResponse> RetriesResponse::parse(const Vector<uint8_t>& inBuffer)
    117116{
    118     // FIXME(205375)
    119     if (inBuffer.size() <= kResponseCodeLength || getResponseCode(inBuffer) != CtapDeviceResponseCode::kSuccess)
    120         return WTF::nullopt;
    121 
    122     Vector<uint8_t> buffer;
    123     buffer.append(inBuffer.data() + 1, inBuffer.size() - 1);
    124     Optional<CBOR> decodedResponse = cbor::CBORReader::read(buffer);
    125     if (!decodedResponse || !decodedResponse->isMap())
    126         return WTF::nullopt;
    127     const auto& responseMap = decodedResponse->getMap();
     117    auto decodedMap = decodeResponseMap(inBuffer);
     118    if (!decodedMap)
     119        return WTF::nullopt;
     120    const auto& responseMap = decodedMap->getMap();
    128121
    129122    auto it = responseMap.find(CBORValue(static_cast<int64_t>(ResponseKey::kRetries)));
     
    143136Optional<KeyAgreementResponse> KeyAgreementResponse::parse(const Vector<uint8_t>& inBuffer)
    144137{
    145     // FIXME(205375)
    146     if (inBuffer.size() <= kResponseCodeLength || getResponseCode(inBuffer) != CtapDeviceResponseCode::kSuccess)
    147         return WTF::nullopt;
    148 
    149     Vector<uint8_t> buffer;
    150     buffer.append(inBuffer.data() + 1, inBuffer.size() - 1);
    151     auto decodedResponse = cbor::CBORReader::read(buffer);
    152     if (!decodedResponse || !decodedResponse->isMap())
    153         return WTF::nullopt;
    154     const auto& responseMap = decodedResponse->getMap();
     138    auto decodedMap = decodeResponseMap(inBuffer);
     139    if (!decodedMap)
     140        return WTF::nullopt;
     141    const auto& responseMap = decodedMap->getMap();
    155142
    156143    // The ephemeral key is encoded as a COSE structure.
     
    217204Optional<TokenResponse> TokenResponse::parse(const WebCore::CryptoKeyAES& sharedKey, const Vector<uint8_t>& inBuffer)
    218205{
    219     // FIXME(205375)
    220     if (inBuffer.size() <= kResponseCodeLength || getResponseCode(inBuffer) != CtapDeviceResponseCode::kSuccess)
    221         return WTF::nullopt;
    222 
    223     Vector<uint8_t> buffer;
    224     buffer.append(inBuffer.data() + 1, inBuffer.size() - 1);
    225     auto decodedResponse = cbor::CBORReader::read(buffer);
    226     if (!decodedResponse || !decodedResponse->isMap())
    227         return WTF::nullopt;
    228     const auto& responseMap = decodedResponse->getMap();
     206    auto decodedMap = decodeResponseMap(inBuffer);
     207    if (!decodedMap)
     208        return WTF::nullopt;
     209    const auto& responseMap = decodedMap->getMap();
    229210
    230211    auto it = responseMap.find(CBORValue(static_cast<int64_t>(ResponseKey::kPinToken)));
Note: See TracChangeset for help on using the changeset viewer.