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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/device_bound_sessions/registration_fetcher.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/functional/callback_helpers.h"
#include "components/unexportable_keys/background_task_priority.h"
#include "components/unexportable_keys/unexportable_key_service.h"
#include "net/base/io_buffer.h"
#include "net/device_bound_sessions/registration_request_param.h"
#include "net/device_bound_sessions/session_binding_utils.h"
#include "net/device_bound_sessions/session_challenge_param.h"
#include "net/device_bound_sessions/session_json_utils.h"
#include "net/log/net_log_event_type.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "url/origin.h"
namespace net::device_bound_sessions {
namespace {
constexpr char kSessionIdHeaderName[] = "Sec-Session-Id";
constexpr char kJwtSessionHeaderName[] = "Sec-Session-Response";
constexpr net::NetworkTrafficAnnotationTag kRegistrationTrafficAnnotation =
net::DefineNetworkTrafficAnnotation("dbsc_registration", R"(
semantics {
sender: "Device Bound Session Credentials API"
description:
"Device Bound Session Credentials (DBSC) let a server create a "
"session with the local device. For more info see "
"https://github.com/WICG/dbsc."
trigger:
"Server sending a response with a Sec-Session-Registration header."
data: "A signed JWT with the new key created for this session."
destination: WEBSITE
last_reviewed: "2024-04-10"
user_data {
type: ACCESS_TOKEN
}
internal {
contacts {
email: "kristianm@chromium.org"
}
contacts {
email: "chrome-counter-abuse-alerts@google.com"
}
}
}
policy {
cookies_allowed: YES
cookies_store: "user"
setting: "There is no separate setting for this feature, but it will "
"follow the cookie settings."
policy_exception_justification: "Not implemented."
})");
constexpr int kBufferSize = 4096;
// New session registration doesn't block the user and can be done with a delay.
constexpr unexportable_keys::BackgroundTaskPriority kTaskPriority =
unexportable_keys::BackgroundTaskPriority::kBestEffort;
void OnDataSigned(
crypto::SignatureVerifier::SignatureAlgorithm algorithm,
unexportable_keys::UnexportableKeyService& unexportable_key_service,
std::string header_and_payload,
unexportable_keys::UnexportableKeyId key_id,
base::OnceCallback<void(
std::optional<RegistrationFetcher::RegistrationTokenResult>)> callback,
unexportable_keys::ServiceErrorOr<std::vector<uint8_t>> result) {
if (!result.has_value()) {
std::move(callback).Run(std::nullopt);
return;
}
const std::vector<uint8_t>& signature = result.value();
std::optional<std::string> registration_token =
AppendSignatureToHeaderAndPayload(header_and_payload, algorithm,
signature);
if (!registration_token.has_value()) {
std::move(callback).Run(std::nullopt);
return;
}
std::move(callback).Run(RegistrationFetcher::RegistrationTokenResult(
registration_token.value(), key_id));
}
void SignChallengeWithKey(
unexportable_keys::UnexportableKeyService& unexportable_key_service,
unexportable_keys::UnexportableKeyId& key_id,
const GURL& registration_url,
std::string_view challenge,
std::optional<std::string> authorization,
std::optional<std::string> session_identifier,
base::OnceCallback<
void(std::optional<RegistrationFetcher::RegistrationTokenResult>)>
callback) {
auto expected_algorithm = unexportable_key_service.GetAlgorithm(key_id);
auto expected_public_key =
unexportable_key_service.GetSubjectPublicKeyInfo(key_id);
if (!expected_algorithm.has_value() || !expected_public_key.has_value()) {
std::move(callback).Run(std::nullopt);
return;
}
std::optional<std::string> optional_header_and_payload =
CreateKeyRegistrationHeaderAndPayload(
challenge, registration_url, expected_algorithm.value(),
expected_public_key.value(), base::Time::Now(),
std::move(authorization), std::move(session_identifier));
if (!optional_header_and_payload.has_value()) {
std::move(callback).Run(std::nullopt);
return;
}
std::string header_and_payload =
std::move(optional_header_and_payload.value());
unexportable_key_service.SignSlowlyAsync(
key_id, base::as_byte_span(header_and_payload), kTaskPriority,
/*max_retries=*/0,
base::BindOnce(&OnDataSigned, expected_algorithm.value(),
std::ref(unexportable_key_service), header_and_payload,
key_id, std::move(callback)));
}
class RegistrationFetcherImpl : public URLRequest::Delegate {
public:
// URLRequest::Delegate
void OnReceivedRedirect(URLRequest* request,
const RedirectInfo& redirect_info,
bool* defer_redirect) override {
if (!IsSecure(redirect_info.new_url)) {
request->Cancel();
OnResponseCompleted(
/*error_on_no_data=*/SessionError::ErrorType::kPersistentHttpError);
// *this is deleted here
}
}
// TODO(kristianm): Look into if OnAuthRequired might need to be customize
// for DBSC
// TODO(kristianm): Think about what to do for DBSC with
// OnCertificateRequested, leaning towards not supporting it but not sure.
// Always cancel requests on SSL errors, this is the default implementation
// of OnSSLCertificateError.
// This is always called unless the request is deleted before it is called.
void OnResponseStarted(URLRequest* request, int net_error) override {
if (net_error != OK) {
OnResponseCompleted(
/*error_on_no_data=*/SessionError::ErrorType::kNetError);
// *this is deleted here
return;
}
HttpResponseHeaders* headers = request->response_headers();
const int response_code = headers ? headers->response_code() : 0;
if (response_code == 401) {
auto challenge_params =
device_bound_sessions::SessionChallengeParam::CreateIfValid(
fetcher_endpoint_, headers);
OnChallengeNeeded(std::move(challenge_params));
// *this is preserved here.
return;
}
if (response_code < 200) {
OnResponseCompleted(
/*error_on_no_data=*/SessionError::ErrorType::kPersistentHttpError);
// *this is deleted here
return;
} else if (response_code == 407) {
// Proxy errors are treated as network errors
OnResponseCompleted(
/*error_on_no_data=*/SessionError::ErrorType::kNetError);
// *this is deleted here
return;
} else if (300 <= response_code && response_code < 500) {
OnResponseCompleted(
/*error_on_no_data=*/SessionError::ErrorType::kPersistentHttpError);
// *this is deleted here
return;
} else if (response_code >= 500) {
OnResponseCompleted(
/*error_on_no_data=*/SessionError::ErrorType::kTransientHttpError);
// *this is deleted here
return;
}
// Initiate the first read.
int bytes_read = request->Read(buf_.get(), kBufferSize);
if (bytes_read >= 0) {
OnReadCompleted(request, bytes_read);
} else if (bytes_read != ERR_IO_PENDING) {
OnResponseCompleted(
/*error_on_no_data=*/SessionError::ErrorType::kNetError);
// *this is deleted here
}
}
void OnReadCompleted(URLRequest* request, int bytes_read) override {
data_received_.append(buf_->data(), bytes_read);
while (bytes_read > 0) {
bytes_read = request->Read(buf_.get(), kBufferSize);
if (bytes_read > 0) {
data_received_.append(buf_->data(), bytes_read);
}
}
if (bytes_read != ERR_IO_PENDING) {
OnResponseCompleted(
/*error_on_no_data=*/SessionError::ErrorType::kNetError);
// *this is deleted here
}
}
RegistrationFetcherImpl(
const GURL& fetcher_endpoint,
std::optional<std::string> session_identifier,
unexportable_keys::UnexportableKeyService& key_service,
const unexportable_keys::UnexportableKeyId& key_id,
const URLRequestContext* context,
const IsolationInfo& isolation_info,
std::optional<NetLogSource> net_log_source,
const std::optional<url::Origin>& original_request_initiator,
RegistrationFetcher::RegistrationCompleteCallback callback)
: fetcher_endpoint_(fetcher_endpoint),
session_identifier_(std::move(session_identifier)),
key_service_(key_service),
key_id_(key_id),
context_(context),
isolation_info_(isolation_info),
net_log_source_(std::move(net_log_source)),
original_request_initiator_(original_request_initiator),
callback_(std::move(callback)),
buf_(base::MakeRefCounted<IOBufferWithSize>(kBufferSize)) {}
~RegistrationFetcherImpl() override { CHECK(!callback_); }
void StartFetch(std::optional<std::string> challenge,
std::optional<std::string> authorization) {
current_challenge_ = std::move(challenge);
current_authorization_ = std::move(authorization);
if (current_challenge_.has_value()) {
number_of_challenges_++;
if (number_of_challenges_ < kMaxChallenges) {
AttemptChallengeSigning();
return;
} else {
RunCallbackAndDeleteSelf(base::unexpected(SessionError{
SessionError::ErrorType::kTooManyChallenges,
net::SchemefulSite(fetcher_endpoint_), session_identifier_}));
return;
}
}
// Start a request to get a challenge with the session identifier. The
// `RegistrationRequestParam` constructors guarantee `session_identifier_`
// is set when `challenge_` is missing.
CHECK(IsForRefreshRequest());
request_ = CreateBaseRequest();
request_->Start();
}
private:
static constexpr size_t kMaxSigningFailures = 2;
static constexpr size_t kMaxChallenges = 5;
void AttemptChallengeSigning() {
SignChallengeWithKey(
*key_service_, key_id_, fetcher_endpoint_, *current_challenge_,
current_authorization_, session_identifier_,
base::BindOnce(&RegistrationFetcherImpl::OnRegistrationTokenCreated,
base::Unretained(this)));
}
void OnRegistrationTokenCreated(
std::optional<RegistrationFetcher::RegistrationTokenResult> result) {
if (!result) {
number_of_signing_failures_++;
if (number_of_signing_failures_ < kMaxSigningFailures) {
AttemptChallengeSigning();
return;
} else {
RunCallbackAndDeleteSelf(base::unexpected(SessionError{
SessionError::ErrorType::kSigningError,
net::SchemefulSite(url::Origin::Create(fetcher_endpoint_)),
session_identifier_}));
return;
}
}
request_ = CreateBaseRequest();
request_->SetExtraRequestHeaderByName(
kJwtSessionHeaderName, result->registration_token, /*overwrite*/ true);
request_->Start();
}
std::unique_ptr<net::URLRequest> CreateBaseRequest() {
CHECK(IsSecure(fetcher_endpoint_));
std::unique_ptr<net::URLRequest> request = context_->CreateRequest(
fetcher_endpoint_, IDLE, this, kRegistrationTrafficAnnotation,
/*is_for_websockets=*/false, net_log_source_);
request->set_method("POST");
request->SetLoadFlags(LOAD_DISABLE_CACHE);
request->set_allow_credentials(true);
request->set_site_for_cookies(isolation_info_.site_for_cookies());
request->set_initiator(original_request_initiator_);
request->set_isolation_info(isolation_info_);
if (IsForRefreshRequest()) {
request->SetExtraRequestHeaderByName(
kSessionIdHeaderName, *session_identifier_, /*overwrite*/ true);
}
return request;
}
void OnChallengeNeeded(
std::optional<std::vector<SessionChallengeParam>> challenge_params) {
if (!challenge_params || challenge_params->empty()) {
RunCallbackAndDeleteSelf(base::unexpected(SessionError{
SessionError::ErrorType::kInvalidChallenge,
net::SchemefulSite(url::Origin::Create(fetcher_endpoint_)),
session_identifier_}));
return;
}
// TODO(kristianm): Log if there is more than one challenge
// TODO(kristianm): Handle if session identifiers don't match
const std::string& challenge = (*challenge_params)[0].challenge();
StartFetch(challenge, std::nullopt);
}
void OnResponseCompleted(SessionError::ErrorType error_on_no_data) {
if (data_received_.empty()) {
RunCallbackAndDeleteSelf(base::unexpected(SessionError{
error_on_no_data,
net::SchemefulSite(url::Origin::Create(fetcher_endpoint_)),
session_identifier_}));
return;
}
RunCallbackAndDeleteSelf(
ParseSessionInstructionJson(request_->url(), key_id_, data_received_));
}
// Running callback when fetching is complete or on error.
// Deletes `this` afterwards.
void RunCallbackAndDeleteSelf(
base::expected<SessionParams, SessionError> params_or_error) {
AddNetLogResult(params_or_error);
std::move(callback_).Run(std::move(params_or_error));
delete this;
}
void AddNetLogResult(
const base::expected<SessionParams, SessionError>& params_or_error) {
if (!request_) {
return;
}
NetLogEventType result_event_type =
IsForRefreshRequest() ? NetLogEventType::DBSC_REFRESH_RESULT
: NetLogEventType::DBSC_REGISTRATION_RESULT;
request_->net_log().AddEvent(result_event_type, [&]() {
std::string result;
if (params_or_error.has_value()) {
result = IsForRefreshRequest() ? "refreshed" : "registered";
} else {
const SessionError& error = params_or_error.error();
if (error.IsFatal()) {
result = "session_ended";
} else {
result = "failed_continue";
}
}
base::Value::Dict dict;
dict.Set("status", std::move(result));
return dict;
});
}
// Returns true if we're fetching for a refresh request. False means this is
// for a registration request.
bool IsForRefreshRequest() { return session_identifier_.has_value(); }
//// This section of fields is state passed into the constructor. ////
// Refers to the endpoint this class will use when triggering a registration
// or refresh request.
GURL fetcher_endpoint_;
// Populated iff this is a refresh request (not a registration request).
std::optional<std::string> session_identifier_;
const raw_ref<unexportable_keys::UnexportableKeyService> key_service_;
unexportable_keys::UnexportableKeyId key_id_;
raw_ptr<const URLRequestContext> context_;
IsolationInfo isolation_info_;
std::optional<net::NetLogSource> net_log_source_;
std::optional<url::Origin> original_request_initiator_;
// This is called once the registration or refresh request completes, whether
// or not it was successful.
RegistrationFetcher::RegistrationCompleteCallback callback_;
// Created to fetch data
std::unique_ptr<URLRequest> request_;
scoped_refptr<IOBuffer> buf_;
std::string data_received_;
std::optional<std::string> current_challenge_;
std::optional<std::string> current_authorization_;
size_t number_of_signing_failures_ = 0;
size_t number_of_challenges_ = 0;
};
RegistrationFetcher::FetcherType* g_mock_fetcher = nullptr;
} // namespace
// static
void RegistrationFetcher::StartCreateTokenAndFetch(
RegistrationFetcherParam registration_params,
unexportable_keys::UnexportableKeyService& key_service,
// TODO(kristianm): Check the lifetime of context and make sure this use
// is safe.
const URLRequestContext* context,
const IsolationInfo& isolation_info,
std::optional<NetLogSource> net_log_source,
const std::optional<url::Origin>& original_request_initiator,
RegistrationCompleteCallback callback) {
// Using mock fetcher for testing
if (g_mock_fetcher) {
std::move(callback).Run(g_mock_fetcher->Run());
return;
}
const auto supported_algos = registration_params.supported_algos();
auto request_params = RegistrationRequestParam::CreateForRegistration(
std::move(registration_params));
// `key_service` is created along with `SessionService` and will be valid
// until the browser ends, hence `std::ref` is safe here.
key_service.GenerateSigningKeySlowlyAsync(
supported_algos, kTaskPriority,
base::BindOnce(&RegistrationFetcher::StartFetchWithExistingKey,
std::move(request_params), std::ref(key_service), context,
isolation_info, net_log_source, original_request_initiator,
std::move(callback)));
}
// static
void RegistrationFetcher::StartFetchWithExistingKey(
RegistrationRequestParam request_params,
unexportable_keys::UnexportableKeyService& unexportable_key_service,
const URLRequestContext* context,
const IsolationInfo& isolation_info,
std::optional<net::NetLogSource> net_log_source,
const std::optional<url::Origin>& original_request_initiator,
RegistrationFetcher::RegistrationCompleteCallback callback,
unexportable_keys::ServiceErrorOr<unexportable_keys::UnexportableKeyId>
key_id) {
// Using mock fetcher for testing.
if (g_mock_fetcher) {
std::move(callback).Run(g_mock_fetcher->Run());
return;
}
if (!key_id.has_value()) {
std::move(callback).Run(base::unexpected(
SessionError{SessionError::ErrorType::kKeyError,
net::SchemefulSite(request_params.registration_endpoint()),
request_params.session_identifier()}));
return;
}
// RegistrationFetcherImpl manages its own lifetime.
RegistrationFetcherImpl* fetcher = new RegistrationFetcherImpl(
request_params.TakeRegistrationEndpoint(),
request_params.TakeSessionIdentifier(), unexportable_key_service,
key_id.value(), context, isolation_info, net_log_source,
original_request_initiator, std::move(callback));
fetcher->StartFetch(request_params.TakeChallenge(),
request_params.TakeAuthorization());
}
void RegistrationFetcher::SetFetcherForTesting(FetcherType* func) {
CHECK(!g_mock_fetcher || !func);
g_mock_fetcher = func;
}
void RegistrationFetcher::CreateTokenAsyncForTesting(
unexportable_keys::UnexportableKeyService& unexportable_key_service,
std::string challenge,
const GURL& registration_url,
std::optional<std::string> authorization,
std::optional<std::string> session_identifier,
base::OnceCallback<
void(std::optional<RegistrationFetcher::RegistrationTokenResult>)>
callback) {
static constexpr crypto::SignatureVerifier::SignatureAlgorithm
kSupportedAlgos[] = {crypto::SignatureVerifier::ECDSA_SHA256,
crypto::SignatureVerifier::RSA_PKCS1_SHA256};
unexportable_key_service.GenerateSigningKeySlowlyAsync(
kSupportedAlgos, kTaskPriority,
base::BindOnce(
[](unexportable_keys::UnexportableKeyService& key_service,
const GURL& registration_url, const std::string& challenge,
std::optional<std::string>&& authorization,
std::optional<std::string>&& session_identifier,
base::OnceCallback<void(
std::optional<RegistrationFetcher::RegistrationTokenResult>)>
callback,
unexportable_keys::ServiceErrorOr<
unexportable_keys::UnexportableKeyId> key_result) {
if (!key_result.has_value()) {
std::move(callback).Run(std::nullopt);
return;
}
SignChallengeWithKey(
key_service, key_result.value(), registration_url, challenge,
std::move(authorization), std::move(session_identifier),
std::move(callback));
},
std::ref(unexportable_key_service), registration_url,
std::move(challenge), std::move(authorization),
std::move(session_identifier), std::move(callback)));
}
} // namespace net::device_bound_sessions
|