1// Copyright 2018 The Chromium Authors. All rights reserved.
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 are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include "config.h"
31#include "AuthenticatorGetInfoResponse.h"
32
33#if ENABLE(WEB_AUTHN)
34
35#include "CBORValue.h"
36#include "CBORWriter.h"
37
38namespace fido {
39
40template <typename Container>
41cbor::CBORValue toArrayValue(const Container& container)
42{
43 cbor::CBORValue::ArrayValue value;
44 value.reserveInitialCapacity(container.size());
45 for (const auto& item : container)
46 value.append(cbor::CBORValue(item));
47 return cbor::CBORValue(WTFMove(value));
48}
49
50AuthenticatorGetInfoResponse::AuthenticatorGetInfoResponse(StdSet<ProtocolVersion>&& versions, Vector<uint8_t>&& aaguid)
51 : m_versions(WTFMove(versions))
52 , m_aaguid(WTFMove(aaguid))
53{
54}
55
56AuthenticatorGetInfoResponse& AuthenticatorGetInfoResponse::setMaxMsgSize(uint32_t maxMsgSize)
57{
58 m_maxMsgSize = maxMsgSize;
59 return *this;
60}
61
62AuthenticatorGetInfoResponse& AuthenticatorGetInfoResponse::setPinProtocols(Vector<uint8_t>&& pinProtocols)
63{
64 m_pinProtocols = WTFMove(pinProtocols);
65 return *this;
66}
67
68AuthenticatorGetInfoResponse& AuthenticatorGetInfoResponse::setExtensions(Vector<String>&& extensions)
69{
70 m_extensions = WTFMove(extensions);
71 return *this;
72}
73
74AuthenticatorGetInfoResponse& AuthenticatorGetInfoResponse::setOptions(AuthenticatorSupportedOptions&& options)
75{
76 m_options = WTFMove(options);
77 return *this;
78}
79
80Vector<uint8_t> encodeAsCBOR(const AuthenticatorGetInfoResponse& response)
81{
82 using namespace cbor;
83
84 CBORValue::MapValue deviceInfoMap;
85
86 CBORValue::ArrayValue versionArray;
87 for (const auto& version : response.versions())
88 versionArray.append(version == ProtocolVersion::kCtap ? kCtap2Version : kU2fVersion);
89 deviceInfoMap.emplace(CBORValue(1), CBORValue(WTFMove(versionArray)));
90
91 if (response.extensions())
92 deviceInfoMap.emplace(CBORValue(2), toArrayValue(*response.extensions()));
93
94 deviceInfoMap.emplace(CBORValue(3), CBORValue(response.aaguid()));
95 deviceInfoMap.emplace(CBORValue(4), convertToCBOR(response.options()));
96
97 if (response.maxMsgSize())
98 deviceInfoMap.emplace(CBORValue(5), CBORValue(static_cast<int64_t>(*response.maxMsgSize())));
99
100 if (response.pinProtocol())
101 deviceInfoMap.emplace(CBORValue(6), toArrayValue(*response.pinProtocol()));
102
103 auto encodedBytes = CBORWriter::write(CBORValue(WTFMove(deviceInfoMap)));
104 ASSERT(encodedBytes);
105 return *encodedBytes;
106}
107
108} // namespace fido
109
110#endif // ENABLE(WEB_AUTHN)
111