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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
// 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 "chromeos/ash/components/kcer/chaps/session_chaps_client.h"
#include <stdint.h>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/logging.h"
#include "chromeos/ash/components/kcer/attributes.pb.h"
#include "chromeos/constants/pkcs11_definitions.h"
#include "third_party/cros_system_api/dbus/chaps/dbus-constants.h"
namespace kcer {
namespace {
// Arbitrary limit for the FindObjects method, equals to 8Mb of object handles
// (each handle is 8 bytes), should be more than enough. The main
// consideration is the amount of data sent over D-Bus at once and the time
// required to process it in Chrome. The current implementation doesn't expect
// Chaps to hold too many objects (usually <100 of a given type, <10000 99.9%
// of the time), the processing of the objects is handled on the UI thread. If
// we find a need to process millions of objects, FindObjects can be changed
// to do multiple smaller requests and the objects could be processed on a
// worker thread.
inline constexpr uint64_t kFindObjectsMaxCount = 1 << 20;
// Pass CKF_RW_SESSION so Chrome can modify data, e.g. generate new key pairs.
// CKF_SERIAL_SESSION should always be set according to
// http://docs.oasis-open.org/pkcs11/pkcs11-base/v2.40/os/pkcs11-base-v2.40-os.html#_Toc416959688
// and chaps verifies that.
inline constexpr uint64_t kSessionFlags =
chromeos::PKCS11_CKF_RW_SESSION | chromeos::PKCS11_CKF_SERIAL_SESSION;
} // namespace
SessionChapsClient::SessionChapsClient() = default;
SessionChapsClient::~SessionChapsClient() = default;
SessionChapsClientImpl::SessionChapsClientImpl() = default;
SessionChapsClientImpl::~SessionChapsClientImpl() = default;
// static
std::vector<uint8_t> SessionChapsClient::SerializeToBytes(
const chaps::AttributeList& attr_list) {
std::vector<uint8_t> result;
result.resize(attr_list.ByteSizeLong());
attr_list.SerializeToArray(result.data(), result.size());
return result;
}
// static
bool SessionChapsClient::IsSessionError(uint32_t result_code) {
return result_code == chromeos::PKCS11_CKR_SESSION_HANDLE_INVALID ||
result_code == chromeos::PKCS11_CKR_SESSION_CLOSED;
}
//==============================================================================
void SessionChapsClientImpl::GetMechanismList(
SlotId slot_id,
GetMechanismListCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run({}, chaps::CKR_DBUS_CLIENT_IS_NULL);
}
return chaps_service->GetMechanismList(slot_id.value(), std::move(callback));
}
//==============================================================================
void SessionChapsClientImpl::CreateObject(
SlotId slot_id,
const std::vector<uint8_t>& attributes,
int attempts_left,
CreateObjectCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run(ObjectHandle(0),
chaps::CKR_DBUS_CLIENT_IS_NULL);
}
SessionId session_id = GetSessionForSlot(slot_id);
if (session_id.value() == chromeos::PKCS11_INVALID_SESSION_ID) {
if (attempts_left <= 0) {
return std::move(callback).Run(ObjectHandle(0),
chaps::CKR_FAILED_TO_OPEN_SESSION);
}
attempts_left--;
auto chaps_callback = base::BindOnce(
&SessionChapsClientImpl::CreateObject, weak_factory_.GetWeakPtr(),
slot_id, attributes, attempts_left, std::move(callback));
return chaps_service->OpenSession(
slot_id.value(), kSessionFlags,
base::BindOnce(&SessionChapsClientImpl::SaveSessionId,
weak_factory_.GetWeakPtr(), slot_id,
std::move(chaps_callback)));
}
return chaps_service->CreateObject(
session_id.value(), attributes,
base::BindOnce(&SessionChapsClientImpl::DidCreateObject,
weak_factory_.GetWeakPtr(), slot_id, std::move(callback)));
}
void SessionChapsClientImpl::DidCreateObject(SlotId slot_id,
CreateObjectCallback callback,
uint64_t object_handle,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsSessionError(result_code)) {
sessions_map_.erase(slot_id);
}
return std::move(callback).Run(ObjectHandle(object_handle), result_code);
}
//==============================================================================
void SessionChapsClientImpl::DestroyObject(SlotId slot_id,
ObjectHandle object_handle,
int attempts_left,
DestroyObjectCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run(chaps::CKR_DBUS_CLIENT_IS_NULL);
}
SessionId session_id = GetSessionForSlot(slot_id);
if (session_id.value() == chromeos::PKCS11_INVALID_SESSION_ID) {
if (attempts_left <= 0) {
return std::move(callback).Run(chaps::CKR_FAILED_TO_OPEN_SESSION);
}
attempts_left--;
auto chaps_callback = base::BindOnce(
&SessionChapsClientImpl::DestroyObject, weak_factory_.GetWeakPtr(),
slot_id, object_handle, attempts_left, std::move(callback));
return chaps_service->OpenSession(
slot_id.value(), kSessionFlags,
base::BindOnce(&SessionChapsClientImpl::SaveSessionId,
weak_factory_.GetWeakPtr(), slot_id,
std::move(chaps_callback)));
}
return chaps_service->DestroyObject(
session_id.value(), object_handle.value(),
base::BindOnce(&SessionChapsClientImpl::DidDestroyObject,
weak_factory_.GetWeakPtr(), slot_id, std::move(callback)));
}
void SessionChapsClientImpl::DidDestroyObject(SlotId slot_id,
DestroyObjectCallback callback,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsSessionError(result_code)) {
sessions_map_.erase(slot_id);
}
return std::move(callback).Run(result_code);
}
//==============================================================================
void SessionChapsClientImpl::GetAttributeValue(
SlotId slot_id,
ObjectHandle object_handle,
std::vector<uint8_t> attributes_query,
int attempts_left,
GetAttributeValueCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run({}, chaps::CKR_DBUS_CLIENT_IS_NULL);
}
SessionId session_id = GetSessionForSlot(slot_id);
if (session_id.value() == chromeos::PKCS11_INVALID_SESSION_ID) {
if (attempts_left <= 0) {
return std::move(callback).Run({}, chaps::CKR_FAILED_TO_OPEN_SESSION);
}
attempts_left--;
auto chaps_callback = base::BindOnce(
&SessionChapsClientImpl::GetAttributeValue, weak_factory_.GetWeakPtr(),
slot_id, object_handle, std::move(attributes_query), attempts_left,
std::move(callback));
return chaps_service->OpenSession(
slot_id.value(), kSessionFlags,
base::BindOnce(&SessionChapsClientImpl::SaveSessionId,
weak_factory_.GetWeakPtr(), slot_id,
std::move(chaps_callback)));
}
return chaps_service->GetAttributeValue(
session_id.value(), object_handle.value(), attributes_query,
base::BindOnce(&SessionChapsClientImpl::DidGetAttributeValue,
weak_factory_.GetWeakPtr(), slot_id, std::move(callback)));
}
void SessionChapsClientImpl::DidGetAttributeValue(
SlotId slot_id,
GetAttributeValueCallback callback,
const std::vector<uint8_t>& attributes,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsSessionError(result_code)) {
sessions_map_.erase(slot_id);
}
return std::move(callback).Run(attributes, result_code);
}
//==============================================================================
void SessionChapsClientImpl::SetAttributeValue(
SlotId slot_id,
ObjectHandle object_handle,
std::vector<uint8_t> attributes,
int attempts_left,
SetAttributeValueCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run(chaps::CKR_DBUS_CLIENT_IS_NULL);
}
SessionId session_id = GetSessionForSlot(slot_id);
if (session_id.value() == chromeos::PKCS11_INVALID_SESSION_ID) {
if (attempts_left <= 0) {
return std::move(callback).Run(chaps::CKR_FAILED_TO_OPEN_SESSION);
}
attempts_left--;
auto chaps_callback = base::BindOnce(
&SessionChapsClientImpl::SetAttributeValue, weak_factory_.GetWeakPtr(),
slot_id, object_handle, std::move(attributes), attempts_left,
std::move(callback));
return chaps_service->OpenSession(
slot_id.value(), kSessionFlags,
base::BindOnce(&SessionChapsClientImpl::SaveSessionId,
weak_factory_.GetWeakPtr(), slot_id,
std::move(chaps_callback)));
}
return chaps_service->SetAttributeValue(
session_id.value(), object_handle.value(), attributes,
base::BindOnce(&SessionChapsClientImpl::DidSetAttributeValue,
weak_factory_.GetWeakPtr(), slot_id, std::move(callback)));
}
void SessionChapsClientImpl::DidSetAttributeValue(
SlotId slot_id,
SetAttributeValueCallback callback,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsSessionError(result_code)) {
sessions_map_.erase(slot_id);
}
return std::move(callback).Run(result_code);
}
//==============================================================================
void SessionChapsClientImpl::FindObjects(SlotId slot_id,
std::vector<uint8_t> attributes,
int attempts_left,
FindObjectsCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run({}, chaps::CKR_DBUS_CLIENT_IS_NULL);
}
SessionId session_id = GetSessionForSlot(slot_id);
if (session_id.value() == chromeos::PKCS11_INVALID_SESSION_ID) {
if (attempts_left <= 0) {
return std::move(callback).Run({}, chaps::CKR_FAILED_TO_OPEN_SESSION);
}
attempts_left--;
auto chaps_callback = base::BindOnce(
&SessionChapsClientImpl::FindObjects, weak_factory_.GetWeakPtr(),
slot_id, std::move(attributes), attempts_left, std::move(callback));
return chaps_service->OpenSession(
slot_id.value(), kSessionFlags,
base::BindOnce(&SessionChapsClientImpl::SaveSessionId,
weak_factory_.GetWeakPtr(), slot_id,
std::move(chaps_callback)));
}
return chaps_service->FindObjectsInit(
session_id.value(), attributes,
base::BindOnce(&SessionChapsClientImpl::DidFindObjectsInit,
weak_factory_.GetWeakPtr(), slot_id, std::move(callback)));
}
void SessionChapsClientImpl::DidFindObjectsInit(SlotId slot_id,
FindObjectsCallback callback,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsSessionError(result_code)) {
sessions_map_.erase(slot_id);
return std::move(callback).Run({}, result_code);
}
if (result_code != chromeos::PKCS11_CKR_OK) {
return std::move(callback).Run({}, result_code);
}
SessionId session_id = GetSessionForSlot(slot_id);
CHECK_NE(session_id.value(), chromeos::PKCS11_INVALID_SESSION_ID);
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run({}, chaps::CKR_DBUS_CLIENT_IS_NULL);
}
return chaps_service->FindObjects(
session_id.value(), kFindObjectsMaxCount,
base::BindOnce(&SessionChapsClientImpl::DidFindObjects,
weak_factory_.GetWeakPtr(), slot_id, std::move(callback)));
}
void SessionChapsClientImpl::DidFindObjects(
SlotId slot_id,
FindObjectsCallback callback,
const std::vector<uint64_t>& object_list,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsSessionError(result_code)) {
sessions_map_.erase(slot_id);
return std::move(callback).Run({}, result_code);
}
if (result_code != chromeos::PKCS11_CKR_OK) {
return std::move(callback).Run({}, result_code);
}
SessionId session_id = GetSessionForSlot(slot_id);
CHECK_NE(session_id.value(), chromeos::PKCS11_INVALID_SESSION_ID);
std::vector<ObjectHandle> typed_list(object_list.begin(), object_list.end());
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run({}, chaps::CKR_DBUS_CLIENT_IS_NULL);
}
return chaps_service->FindObjectsFinal(
session_id.value(),
base::BindOnce(&SessionChapsClientImpl::DidFindObjectsFinal,
weak_factory_.GetWeakPtr(), slot_id, std::move(callback),
std::move(typed_list)));
}
void SessionChapsClientImpl::DidFindObjectsFinal(
SlotId slot_id,
FindObjectsCallback callback,
std::vector<ObjectHandle> object_list,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsSessionError(result_code)) {
sessions_map_.erase(slot_id);
}
if (result_code != chromeos::PKCS11_CKR_OK) {
// It's not clear if finishing a search can actually fail. The caller
// shouldn't need to care since the objects were already successfully
// retrieved, but future FindObjects calls within the same PKCS#11 session
// will fail if the current search is not finished properly.
LOG(ERROR) << "Failed to call FindObjectsFinal";
}
return std::move(callback).Run(std::move(object_list),
chromeos::PKCS11_CKR_OK);
}
//==============================================================================
void SessionChapsClientImpl::Sign(SlotId slot_id,
uint64_t mechanism_type,
std::vector<uint8_t> mechanism_parameter,
ObjectHandle key_handle,
std::vector<uint8_t> data,
int attempts_left,
SignCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run({}, chaps::CKR_DBUS_CLIENT_IS_NULL);
}
SessionId session_id = GetSessionForSlot(slot_id);
if (session_id.value() == chromeos::PKCS11_INVALID_SESSION_ID) {
if (attempts_left <= 0) {
return std::move(callback).Run({}, chaps::CKR_FAILED_TO_OPEN_SESSION);
}
attempts_left--;
auto chaps_callback = base::BindOnce(
&SessionChapsClientImpl::Sign, weak_factory_.GetWeakPtr(), slot_id,
mechanism_type, std::move(mechanism_parameter), key_handle,
std::move(data), attempts_left, std::move(callback));
return chaps_service->OpenSession(
slot_id.value(), kSessionFlags,
base::BindOnce(&SessionChapsClientImpl::SaveSessionId,
weak_factory_.GetWeakPtr(), slot_id,
std::move(chaps_callback)));
}
return chaps_service->SignInit(
session_id.value(), mechanism_type, mechanism_parameter,
key_handle.value(),
base::BindOnce(&SessionChapsClientImpl::DidSignInit,
weak_factory_.GetWeakPtr(), slot_id, std::move(data),
std::move(callback)));
}
void SessionChapsClientImpl::DidSignInit(SlotId slot_id,
std::vector<uint8_t> data,
SignCallback callback,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsSessionError(result_code)) {
sessions_map_.erase(slot_id);
return std::move(callback).Run({}, result_code);
}
if (result_code != chromeos::PKCS11_CKR_OK) {
return std::move(callback).Run({}, result_code);
}
SessionId session_id = GetSessionForSlot(slot_id);
CHECK_NE(session_id.value(), chromeos::PKCS11_INVALID_SESSION_ID);
// Maximum supported RSA key is 4096 bits, its signature is 4096 bits, 512
// bytes. The only supported EC key is P-256, its signature is 512 bits, 64
// bytes.
constexpr uint64_t kMaxOutLength = 512;
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run({}, chaps::CKR_DBUS_CLIENT_IS_NULL);
}
return chaps_service->Sign(
session_id.value(), data, kMaxOutLength,
base::BindOnce(&SessionChapsClientImpl::DidSign,
weak_factory_.GetWeakPtr(), slot_id, std::move(callback)));
}
void SessionChapsClientImpl::DidSign(SlotId slot_id,
SignCallback callback,
uint64_t actual_out_length,
const std::vector<uint8_t>& signature,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsSessionError(result_code)) {
sessions_map_.erase(slot_id);
}
return std::move(callback).Run(std::move(signature), result_code);
}
//==============================================================================
void SessionChapsClientImpl::GenerateKeyPair(
SlotId slot_id,
uint64_t mechanism_type,
std::vector<uint8_t> mechanism_parameter,
std::vector<uint8_t> public_attributes,
std::vector<uint8_t> private_attributes,
int attempts_left,
GenerateKeyPairCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ash::ChapsClient* chaps_service = ash::ChapsClient::Get();
if (!chaps_service) {
return std::move(callback).Run(ObjectHandle(0), ObjectHandle(0),
chaps::CKR_DBUS_CLIENT_IS_NULL);
}
SessionId session_id = GetSessionForSlot(slot_id);
if (session_id.value() == chromeos::PKCS11_INVALID_SESSION_ID) {
if (attempts_left <= 0) {
return std::move(callback).Run(ObjectHandle(0), ObjectHandle(0),
chaps::CKR_FAILED_TO_OPEN_SESSION);
}
attempts_left--;
auto chaps_callback = base::BindOnce(
&SessionChapsClientImpl::GenerateKeyPair, weak_factory_.GetWeakPtr(),
slot_id, mechanism_type, std::move(mechanism_parameter),
std::move(public_attributes), std::move(private_attributes),
attempts_left, std::move(callback));
return chaps_service->OpenSession(
slot_id.value(), kSessionFlags,
base::BindOnce(&SessionChapsClientImpl::SaveSessionId,
weak_factory_.GetWeakPtr(), slot_id,
std::move(chaps_callback)));
}
return chaps_service->GenerateKeyPair(
session_id.value(), mechanism_type, mechanism_parameter,
public_attributes, private_attributes,
base::BindOnce(&SessionChapsClientImpl::DidGenerateKeyPair,
weak_factory_.GetWeakPtr(), slot_id, std::move(callback)));
}
void SessionChapsClientImpl::DidGenerateKeyPair(
SlotId slot_id,
GenerateKeyPairCallback callback,
uint64_t public_key_handle,
uint64_t private_key_handle,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsSessionError(result_code)) {
sessions_map_.erase(slot_id);
}
return std::move(callback).Run(ObjectHandle(public_key_handle),
ObjectHandle(private_key_handle), result_code);
}
//==============================================================================
void SessionChapsClientImpl::SaveSessionId(SlotId slot_id,
base::OnceClosure callback,
uint64_t session_id,
uint32_t result_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (result_code == chromeos::PKCS11_CKR_OK) {
sessions_map_[slot_id] = SessionId(session_id);
} else {
LOG(ERROR) << "Failed to open "
"session, error: "
<< result_code;
}
return std::move(callback).Run();
}
SessionChapsClient::SessionId SessionChapsClientImpl::GetSessionForSlot(
SlotId slot_id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto iter = sessions_map_.find(slot_id);
if (iter == sessions_map_.end()) {
return SessionId(chromeos::PKCS11_INVALID_SESSION_ID);
}
return iter->second;
}
} // namespace kcer
|