| 1 | /* |
| 2 | * Copyright (C) 2018 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "CtapHidAuthenticator.h" |
| 28 | |
| 29 | #if ENABLE(WEB_AUTHN) && PLATFORM(MAC) |
| 30 | |
| 31 | #include "CtapHidDriver.h" |
| 32 | #include "U2fHidAuthenticator.h" |
| 33 | #include <WebCore/DeviceRequestConverter.h> |
| 34 | #include <WebCore/DeviceResponseConverter.h> |
| 35 | #include <WebCore/ExceptionData.h> |
| 36 | #include <wtf/RunLoop.h> |
| 37 | #include <wtf/text/StringConcatenateNumbers.h> |
| 38 | |
| 39 | namespace WebKit { |
| 40 | using namespace WebCore; |
| 41 | using namespace fido; |
| 42 | |
| 43 | CtapHidAuthenticator::CtapHidAuthenticator(std::unique_ptr<CtapHidDriver>&& driver, AuthenticatorGetInfoResponse&& info) |
| 44 | : m_driver(WTFMove(driver)) |
| 45 | , m_info(WTFMove(info)) |
| 46 | { |
| 47 | // FIXME(191520): We need a way to convert std::unique_ptr to UniqueRef. |
| 48 | ASSERT(m_driver); |
| 49 | } |
| 50 | |
| 51 | void CtapHidAuthenticator::makeCredential() |
| 52 | { |
| 53 | ASSERT(!m_isDowngraded); |
| 54 | auto cborCmd = encodeMakeCredenitalRequestAsCBOR(requestData().hash, requestData().creationOptions, m_info.options().userVerificationAvailability()); |
| 55 | m_driver->transact(WTFMove(cborCmd), [weakThis = makeWeakPtr(*this)](Vector<uint8_t>&& data) { |
| 56 | ASSERT(RunLoop::isMain()); |
| 57 | if (!weakThis) |
| 58 | return; |
| 59 | weakThis->continueMakeCredentialAfterResponseReceived(WTFMove(data)); |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | void CtapHidAuthenticator::continueMakeCredentialAfterResponseReceived(Vector<uint8_t>&& data) const |
| 64 | { |
| 65 | auto response = readCTAPMakeCredentialResponse(data); |
| 66 | if (!response) { |
| 67 | auto error = getResponseCode(data); |
| 68 | if (error == CtapDeviceResponseCode::kCtap2ErrCredentialExcluded) |
| 69 | receiveRespond(ExceptionData { InvalidStateError, "At least one credential matches an entry of the excludeCredentials list in the authenticator."_s }); |
| 70 | else |
| 71 | receiveRespond(ExceptionData { UnknownError, makeString("Unknown internal error. Error code: " , static_cast<uint8_t>(error)) }); |
| 72 | return; |
| 73 | } |
| 74 | receiveRespond(WTFMove(*response)); |
| 75 | } |
| 76 | |
| 77 | void CtapHidAuthenticator::getAssertion() |
| 78 | { |
| 79 | ASSERT(!m_isDowngraded); |
| 80 | auto cborCmd = encodeGetAssertionRequestAsCBOR(requestData().hash, requestData().requestOptions, m_info.options().userVerificationAvailability()); |
| 81 | m_driver->transact(WTFMove(cborCmd), [weakThis = makeWeakPtr(*this)](Vector<uint8_t>&& data) { |
| 82 | ASSERT(RunLoop::isMain()); |
| 83 | if (!weakThis) |
| 84 | return; |
| 85 | weakThis->continueGetAssertionAfterResponseReceived(WTFMove(data)); |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | void CtapHidAuthenticator::continueGetAssertionAfterResponseReceived(Vector<uint8_t>&& data) |
| 90 | { |
| 91 | auto response = readCTAPGetAssertionResponse(data); |
| 92 | if (!response) { |
| 93 | auto error = getResponseCode(data); |
| 94 | if (error != CtapDeviceResponseCode::kCtap2ErrInvalidCBOR && tryDowngrade()) |
| 95 | return; |
| 96 | receiveRespond(ExceptionData { UnknownError, makeString("Unknown internal error. Error code: " , static_cast<uint8_t>(error)) }); |
| 97 | return; |
| 98 | } |
| 99 | receiveRespond(WTFMove(*response)); |
| 100 | } |
| 101 | |
| 102 | bool CtapHidAuthenticator::tryDowngrade() |
| 103 | { |
| 104 | if (m_info.versions().find(ProtocolVersion::kU2f) == m_info.versions().end()) |
| 105 | return false; |
| 106 | if (!observer()) |
| 107 | return false; |
| 108 | |
| 109 | m_isDowngraded = true; |
| 110 | m_driver->setProtocol(ProtocolVersion::kU2f); |
| 111 | observer()->downgrade(this, U2fHidAuthenticator::create(WTFMove(m_driver))); |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | } // namespace WebKit |
| 116 | |
| 117 | #endif // ENABLE(WEB_AUTHN) && PLATFORM(MAC) |
| 118 | |