forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctap_get_assertion_request.cc
306 lines (251 loc) · 10.5 KB
/
ctap_get_assertion_request.cc
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/fido/ctap_get_assertion_request.h"
#include <algorithm>
#include <limits>
#include <utility>
#include "base/numerics/safe_conversions.h"
#include "components/cbor/writer.h"
#include "device/fido/fido_constants.h"
#include "device/fido/fido_parsing_utils.h"
#include "device/fido/pin.h"
namespace device {
namespace {
bool IsGetAssertionOptionMapFormatCorrect(
const cbor::Value::MapValue& option_map) {
return std::all_of(
option_map.begin(), option_map.end(), [](const auto& param) {
return param.first.is_string() &&
(param.first.GetString() == kUserPresenceMapKey ||
param.first.GetString() == kUserVerificationMapKey) &&
param.second.is_bool();
});
}
bool AreGetAssertionRequestMapKeysCorrect(
const cbor::Value::MapValue& request_map) {
return std::all_of(
request_map.begin(), request_map.end(), [](const auto& param) {
return (param.first.is_integer() && 1u <= param.first.GetInteger() &&
param.first.GetInteger() <= 7u);
});
}
} // namespace
CtapGetAssertionRequest::HMACSecret::HMACSecret(
base::span<const uint8_t, kP256X962Length> in_public_key_x962,
base::span<const uint8_t> in_encrypted_salts,
base::span<const uint8_t> in_salts_auth)
: public_key_x962(fido_parsing_utils::Materialize(in_public_key_x962)),
encrypted_salts(fido_parsing_utils::Materialize(in_encrypted_salts)),
salts_auth(fido_parsing_utils::Materialize(in_salts_auth)) {}
CtapGetAssertionRequest::HMACSecret::HMACSecret(const HMACSecret&) = default;
CtapGetAssertionRequest::HMACSecret::~HMACSecret() = default;
CtapGetAssertionRequest::HMACSecret&
CtapGetAssertionRequest::HMACSecret::operator=(const HMACSecret&) = default;
// static
base::Optional<CtapGetAssertionRequest> CtapGetAssertionRequest::Parse(
const cbor::Value::MapValue& request_map,
const ParseOpts& opts) {
if (!AreGetAssertionRequestMapKeysCorrect(request_map))
return base::nullopt;
const auto rp_id_it = request_map.find(cbor::Value(1));
if (rp_id_it == request_map.end() || !rp_id_it->second.is_string())
return base::nullopt;
const auto client_data_hash_it = request_map.find(cbor::Value(2));
if (client_data_hash_it == request_map.end() ||
!client_data_hash_it->second.is_bytestring() ||
client_data_hash_it->second.GetBytestring().size() !=
kClientDataHashLength) {
return base::nullopt;
}
base::span<const uint8_t, kClientDataHashLength> client_data_hash(
client_data_hash_it->second.GetBytestring().data(),
kClientDataHashLength);
CtapGetAssertionRequest request(rp_id_it->second.GetString(),
/*client_data_json=*/std::string());
request.client_data_hash = fido_parsing_utils::Materialize(client_data_hash);
const auto allow_list_it = request_map.find(cbor::Value(3));
if (allow_list_it != request_map.end()) {
if (!allow_list_it->second.is_array())
return base::nullopt;
const auto& credential_descriptors = allow_list_it->second.GetArray();
if (credential_descriptors.empty())
return base::nullopt;
std::vector<PublicKeyCredentialDescriptor> allow_list;
for (const auto& credential_descriptor : credential_descriptors) {
auto allowed_credential =
PublicKeyCredentialDescriptor::CreateFromCBORValue(
credential_descriptor);
if (!allowed_credential)
return base::nullopt;
allow_list.push_back(std::move(*allowed_credential));
}
request.allow_list = std::move(allow_list);
}
const auto extensions_it = request_map.find(cbor::Value(4));
if (extensions_it != request_map.end()) {
if (!extensions_it->second.is_map()) {
return base::nullopt;
}
const cbor::Value::MapValue& extensions = extensions_it->second.GetMap();
if (opts.reject_all_extensions && !extensions.empty()) {
return base::nullopt;
}
for (const auto& extension : extensions) {
if (!extension.first.is_string()) {
return base::nullopt;
}
const std::string& extension_id = extension.first.GetString();
if (extension_id == kExtensionAndroidClientData) {
base::Optional<AndroidClientDataExtensionInput>
android_client_data_ext =
AndroidClientDataExtensionInput::Parse(extension.second);
if (!android_client_data_ext) {
return base::nullopt;
}
request.android_client_data_ext = std::move(*android_client_data_ext);
} else if (extension_id == kExtensionHmacSecret) {
if (!extension.second.is_map()) {
return base::nullopt;
}
const auto& hmac_extension = extension.second.GetMap();
auto hmac_it = hmac_extension.find(cbor::Value(1));
if (hmac_it == hmac_extension.end() || !hmac_it->second.is_map()) {
return base::nullopt;
}
const base::Optional<pin::KeyAgreementResponse> key(
pin::KeyAgreementResponse::ParseFromCOSE(hmac_it->second.GetMap()));
hmac_it = hmac_extension.find(cbor::Value(2));
if (hmac_it == hmac_extension.end() ||
!hmac_it->second.is_bytestring()) {
return base::nullopt;
}
const std::vector<uint8_t>& encrypted_salts =
hmac_it->second.GetBytestring();
hmac_it = hmac_extension.find(cbor::Value(3));
if (hmac_it == hmac_extension.end() ||
!hmac_it->second.is_bytestring()) {
return base::nullopt;
}
const std::vector<uint8_t>& salts_auth =
hmac_it->second.GetBytestring();
if (!key ||
(encrypted_salts.size() != 32 && encrypted_salts.size() != 64) ||
salts_auth.size() != 16) {
return base::nullopt;
}
request.hmac_secret.emplace(key->X962(), encrypted_salts, salts_auth);
}
}
}
const auto option_it = request_map.find(cbor::Value(5));
if (option_it != request_map.end()) {
if (!option_it->second.is_map())
return base::nullopt;
const auto& option_map = option_it->second.GetMap();
if (!IsGetAssertionOptionMapFormatCorrect(option_map))
return base::nullopt;
const auto user_presence_option =
option_map.find(cbor::Value(kUserPresenceMapKey));
if (user_presence_option != option_map.end()) {
request.user_presence_required = user_presence_option->second.GetBool();
}
const auto uv_option =
option_map.find(cbor::Value(kUserVerificationMapKey));
if (uv_option != option_map.end()) {
request.user_verification =
uv_option->second.GetBool()
? UserVerificationRequirement::kRequired
: UserVerificationRequirement::kDiscouraged;
}
}
const auto pin_auth_it = request_map.find(cbor::Value(6));
if (pin_auth_it != request_map.end()) {
if (!pin_auth_it->second.is_bytestring())
return base::nullopt;
request.pin_auth = pin_auth_it->second.GetBytestring();
}
const auto pin_protocol_it = request_map.find(cbor::Value(7));
if (pin_protocol_it != request_map.end()) {
if (!pin_protocol_it->second.is_unsigned() ||
pin_protocol_it->second.GetUnsigned() >
std::numeric_limits<uint8_t>::max()) {
return base::nullopt;
}
request.pin_protocol = pin_protocol_it->second.GetUnsigned();
}
return request;
}
CtapGetAssertionRequest::CtapGetAssertionRequest(
std::string in_rp_id,
std::string in_client_data_json)
: rp_id(std::move(in_rp_id)),
client_data_json(std::move(in_client_data_json)),
client_data_hash(fido_parsing_utils::CreateSHA256Hash(client_data_json)) {
}
CtapGetAssertionRequest::CtapGetAssertionRequest(
const CtapGetAssertionRequest& that) = default;
CtapGetAssertionRequest::CtapGetAssertionRequest(
CtapGetAssertionRequest&& that) = default;
CtapGetAssertionRequest& CtapGetAssertionRequest::operator=(
const CtapGetAssertionRequest& other) = default;
CtapGetAssertionRequest& CtapGetAssertionRequest::operator=(
CtapGetAssertionRequest&& other) = default;
CtapGetAssertionRequest::~CtapGetAssertionRequest() = default;
std::pair<CtapRequestCommand, base::Optional<cbor::Value>>
AsCTAPRequestValuePair(const CtapGetAssertionRequest& request) {
cbor::Value::MapValue cbor_map;
cbor_map[cbor::Value(1)] = cbor::Value(request.rp_id);
cbor_map[cbor::Value(2)] = cbor::Value(request.client_data_hash);
if (!request.allow_list.empty()) {
cbor::Value::ArrayValue allow_list_array;
for (const auto& descriptor : request.allow_list) {
allow_list_array.push_back(AsCBOR(descriptor));
}
cbor_map[cbor::Value(3)] = cbor::Value(std::move(allow_list_array));
}
cbor::Value::MapValue extensions;
if (request.android_client_data_ext) {
extensions.emplace(kExtensionAndroidClientData,
AsCBOR(*request.android_client_data_ext));
}
if (request.hmac_secret) {
const auto& hmac_secret = *request.hmac_secret;
cbor::Value::MapValue hmac_extension;
hmac_extension.emplace(
1, pin::EncodeCOSEPublicKey(hmac_secret.public_key_x962));
hmac_extension.emplace(2, hmac_secret.encrypted_salts);
hmac_extension.emplace(3, hmac_secret.salts_auth);
extensions.emplace(kExtensionHmacSecret, std::move(hmac_extension));
}
if (!extensions.empty()) {
cbor_map[cbor::Value(4)] = cbor::Value(std::move(extensions));
}
if (request.pin_auth) {
cbor_map[cbor::Value(6)] = cbor::Value(*request.pin_auth);
}
if (request.pin_protocol) {
cbor_map[cbor::Value(7)] = cbor::Value(*request.pin_protocol);
}
cbor::Value::MapValue option_map;
// User presence is required by default.
if (!request.user_presence_required) {
option_map[cbor::Value(kUserPresenceMapKey)] =
cbor::Value(request.user_presence_required);
}
// User verification is not required by default.
if (request.user_verification == UserVerificationRequirement::kRequired) {
option_map[cbor::Value(kUserVerificationMapKey)] = cbor::Value(true);
}
if (!option_map.empty()) {
cbor_map[cbor::Value(5)] = cbor::Value(std::move(option_map));
}
return std::make_pair(CtapRequestCommand::kAuthenticatorGetAssertion,
cbor::Value(std::move(cbor_map)));
}
std::pair<CtapRequestCommand, base::Optional<cbor::Value>>
AsCTAPRequestValuePair(const CtapGetNextAssertionRequest&) {
return std::make_pair(CtapRequestCommand::kAuthenticatorGetNextAssertion,
base::nullopt);
}
} // namespace device