File: high_level_chaps_client.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (395 lines) | stat: -rw-r--r-- 16,242 bytes parent folder | download | duplicates (6)
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
// 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/high_level_chaps_client.h"

#include "base/sequence_checker.h"
#include "chromeos/ash/components/kcer/chaps/session_chaps_client.h"
#include "chromeos/ash/components/kcer/key_permissions.pb.h"
#include "chromeos/constants/pkcs11_definitions.h"
#include "third_party/cros_system_api/dbus/chaps/dbus-constants.h"

namespace kcer {

void AddAttribute(chaps::AttributeList& attr_list,
                  chromeos::PKCS11_CK_ATTRIBUTE_TYPE type,
                  base::span<const uint8_t> data) {
  chaps::Attribute* new_attr = attr_list.add_attributes();
  new_attr->set_type(type);
  new_attr->set_value(std::string(data.begin(), data.end()));
  new_attr->set_length(data.size());
}

namespace {

using AttributeId = HighLevelChapsClient::AttributeId;

constexpr int kDefaultAttempts = 5;

// Returns the expected size of an attribute with `attribute_id`. In case the
// actual attribute is bigger, the retrieval should still succeed, but will take
// two additional D-Bus calls.
int GetDefaultLength(AttributeId attribute_id) {
  switch (attribute_id) {
    case AttributeId::kModulus:
      // The size of the modulus for a 2048 RSA public key. All other supported
      // keys are expected to be smaller or equal to this.
      return 256;
    case AttributeId::kPublicExponent:
      return 3;
    case AttributeId::kEcPoint:
      return 67;
    case AttributeId::kPkcs11Id:
      // The size of a SHA-1 hash, a typical size for CKA_ID.
      return 20;
    case AttributeId::kLabel:
      // An arbitrary length, label is just a user readable string. In same
      // cases it contains a GUID (38 characters).
      return 40;
    case AttributeId::kKeyType:
      return sizeof(chromeos::PKCS11_CK_KEY_TYPE);
    case AttributeId::kValue:
      return 800;
    case AttributeId::kKeyInSoftware:
      return sizeof(chromeos::PKCS11_CK_BBOOL);
    case AttributeId::kKeyPermissions:
      return sizeof(chaps::KeyPermissions);
    case AttributeId::kCertProvisioningId:
      // An arbitrary length, the id is just a user readable string. In same
      // cases it contains a GUID (38 characters).
      return 40;
  }
}

}  // namespace

HighLevelChapsClientImpl::HighLevelChapsClientImpl(
    SessionChapsClient* session_chaps_client)
    : session_chaps_client_(session_chaps_client) {}
HighLevelChapsClientImpl::~HighLevelChapsClientImpl() = default;

//==============================================================================

void HighLevelChapsClientImpl::GetMechanismList(
    SessionChapsClient::SlotId slot_id,
    SessionChapsClient::GetMechanismListCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  session_chaps_client_->GetMechanismList(slot_id, std::move(callback));
}

//==============================================================================

void HighLevelChapsClientImpl::CreateObject(
    SessionChapsClient::SlotId slot_id,
    const chaps::AttributeList& attributes,
    SessionChapsClient::CreateObjectCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  session_chaps_client_->CreateObject(
      slot_id, SessionChapsClient::SerializeToBytes(attributes),
      kDefaultAttempts, std::move(callback));
}

//==============================================================================

void HighLevelChapsClientImpl::DestroyObject(
    SessionChapsClient::SlotId slot_id,
    SessionChapsClient::ObjectHandle object_handle,
    SessionChapsClient::DestroyObjectCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  session_chaps_client_->DestroyObject(slot_id, object_handle, kDefaultAttempts,
                                       std::move(callback));
}

//==============================================================================

void HighLevelChapsClientImpl::DestroyObjectsWithRetries(
    SessionChapsClient::SlotId slot_id,
    std::vector<SessionChapsClient::ObjectHandle> object_handles,
    SessionChapsClient::DestroyObjectCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DestroyObjectsWithRetriesImpl(slot_id, std::move(object_handles),
                                /*failed_handles=*/{},
                                /*last_error=*/chromeos::PKCS11_CKR_OK,
                                /*retries_left=*/2, std::move(callback));
}

void HighLevelChapsClientImpl::DestroyObjectsWithRetriesImpl(
    SessionChapsClient::SlotId slot_id,
    std::vector<SessionChapsClient::ObjectHandle> object_handles,
    std::vector<SessionChapsClient::ObjectHandle> failed_handles,
    uint32_t last_error,
    int retries_left,
    SessionChapsClient::DestroyObjectCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!object_handles.empty()) {
    SessionChapsClient::ObjectHandle next_handle = object_handles.back();
    auto chaps_callback = base::BindOnce(
        &HighLevelChapsClientImpl::DestroyObjectsWithRetriesHandleOneResult,
        weak_factory_.GetWeakPtr(), slot_id, object_handles, failed_handles,
        last_error, retries_left, std::move(callback));
    return DestroyObject(slot_id, next_handle, std::move(chaps_callback));
  }

  if (!failed_handles.empty() && (retries_left > 0)) {
    return DestroyObjectsWithRetriesImpl(
        slot_id, /*object_handles=*/std::move(failed_handles),
        /*failed_handles=*/{}, last_error, retries_left - 1,
        std::move(callback));
  }

  uint32_t final_result =
      failed_handles.empty() ? chromeos::PKCS11_CKR_OK : last_error;
  return std::move(callback).Run(final_result);
}

void HighLevelChapsClientImpl::DestroyObjectsWithRetriesHandleOneResult(
    SessionChapsClient::SlotId slot_id,
    std::vector<SessionChapsClient::ObjectHandle> object_handles,
    std::vector<SessionChapsClient::ObjectHandle> failed_handles,
    uint32_t last_error,
    int retries_left,
    SessionChapsClient::DestroyObjectCallback callback,
    uint32_t result_code) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (SessionChapsClient::IsSessionError(result_code)) {
    return std::move(callback).Run(result_code);
  }

  uint32_t new_last_error = last_error;
  if (result_code != chromeos::PKCS11_CKR_OK) {
    failed_handles.push_back(object_handles.back());
    new_last_error = result_code;
  }
  object_handles.pop_back();

  return DestroyObjectsWithRetriesImpl(
      slot_id, std::move(object_handles), std::move(failed_handles),
      new_last_error, retries_left, std::move(callback));
}

//==============================================================================

void HighLevelChapsClientImpl::GetAttributeValue(
    SessionChapsClient::SlotId slot_id,
    SessionChapsClient::ObjectHandle object_handle,
    std::vector<AttributeId> attribute_ids,
    HighLevelChapsClient::GetAttributeValueCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  chaps::AttributeList attributes_query;
  for (AttributeId attr_id : attribute_ids) {
    chaps::Attribute* attr = attributes_query.add_attributes();
    attr->set_type(static_cast<uint32_t>(attr_id));

    int length = GetDefaultLength(attr_id);
    attr->set_length(length);
    // The "value" represents the buffer from the PKCS#11 interface. The buffer
    // needs to be big enough to store the attribute value. While communicating
    // over D-Bus this actual buffer won't be used and the reply will be sent in
    // a separate message, but Chaps looks at the size of it to allocate the
    // memory for the reply.
    // TODO(miersh): Since Chaps only needs the size, it could just looks at the
    // "length" field. Investigate why it's not doing that and optimize the
    // protocol.
    attr->set_value(std::string(/*count=*/length, /*ch=*/'\0'));
  }

  auto chaps_callback = base::BindOnce(
      &HighLevelChapsClientImpl::DidGetAttributeValue,
      weak_factory_.GetWeakPtr(), slot_id, object_handle, std::move(callback));
  session_chaps_client_->GetAttributeValue(
      slot_id, object_handle,
      SessionChapsClient::SerializeToBytes(attributes_query), kDefaultAttempts,
      std::move(chaps_callback));
}

void HighLevelChapsClientImpl::DidGetAttributeValue(
    SessionChapsClient::SlotId slot_id,
    SessionChapsClient::ObjectHandle object_handle,
    HighLevelChapsClient::GetAttributeValueCallback callback,
    std::vector<uint8_t> attributes,
    uint32_t result_code) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  chaps::AttributeList decoded_attributes;
  if (!decoded_attributes.ParseFromArray(attributes.data(),
                                         attributes.size())) {
    return std::move(callback).Run({}, chaps::CKR_DBUS_DECODING_ERROR);
  }

  if (result_code != chromeos::PKCS11_CKR_BUFFER_TOO_SMALL) {
    // If `result_code` is ok, then `decoded_attributes` should contain the
    // result. If `result_code` is an error, just forward it to the caller
    // together with all attributes that chaps managed to find (if any).
    return std::move(callback).Run(std::move(decoded_attributes), result_code);
  }

  // One or more provided lengths for the attributes were too small, Chaps
  // returned only some of the attributes. In theory only such attributes need
  // to be processed specially, but for simplicity this class requests correct
  // lengths for all attributes and then queries the attributes again.
  for (int i = 0; i < decoded_attributes.attributes_size(); i++) {
    // Clearing the value requests the size of the stored attribute.
    decoded_attributes.mutable_attributes(i)->clear_value();
    decoded_attributes.mutable_attributes(i)->set_length(0);
  }

  auto chaps_callback = base::BindOnce(
      &HighLevelChapsClientImpl::DidGetAttributeLength,
      weak_factory_.GetWeakPtr(), slot_id, object_handle, std::move(callback));
  session_chaps_client_->GetAttributeValue(
      slot_id, object_handle,
      SessionChapsClient::SerializeToBytes(decoded_attributes),
      kDefaultAttempts, std::move(chaps_callback));
}

void HighLevelChapsClientImpl::DidGetAttributeLength(
    SessionChapsClient::SlotId slot_id,
    SessionChapsClient::ObjectHandle object_handle,
    HighLevelChapsClient::GetAttributeValueCallback callback,
    std::vector<uint8_t> attributes,
    uint32_t result_code) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (result_code != chromeos::PKCS11_CKR_OK) {
    return std::move(callback).Run({}, result_code);
  }

  chaps::AttributeList decoded_attributes;
  if (!decoded_attributes.ParseFromArray(attributes.data(),
                                         attributes.size())) {
    return std::move(callback).Run({}, chaps::CKR_DBUS_DECODING_ERROR);
  }
  // Set values to requests the attributes with the returned lengths.
  for (int i = 0; i < decoded_attributes.attributes_size(); i++) {
    decoded_attributes.mutable_attributes(i)->set_value(std::string(
        /*count=*/decoded_attributes.attributes(i).length(), /*ch=*/'\0'));
  }

  auto chaps_callback = base::BindOnce(
      &HighLevelChapsClientImpl::DidGetAttributeValueWithLength,
      weak_factory_.GetWeakPtr(), slot_id, object_handle, std::move(callback));
  session_chaps_client_->GetAttributeValue(
      slot_id, object_handle,
      SessionChapsClient::SerializeToBytes(decoded_attributes),
      kDefaultAttempts, std::move(chaps_callback));
}

void HighLevelChapsClientImpl::DidGetAttributeValueWithLength(
    SessionChapsClient::SlotId slot_id,
    SessionChapsClient::ObjectHandle object_handle,
    HighLevelChapsClient::GetAttributeValueCallback callback,
    std::vector<uint8_t> attributes,
    uint32_t result_code) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  chaps::AttributeList decoded_attributes;
  if (!decoded_attributes.ParseFromArray(attributes.data(),
                                         attributes.size())) {
    return std::move(callback).Run({}, chaps::CKR_DBUS_DECODING_ERROR);
  }
  return std::move(callback).Run(std::move(decoded_attributes), result_code);
}

//==============================================================================

void HighLevelChapsClientImpl::SetAttributeValue(
    SessionChapsClient::SlotId slot_id,
    SessionChapsClient::ObjectHandle object_handle,
    const chaps::AttributeList& attributes,
    SessionChapsClient::SetAttributeValueCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  session_chaps_client_->SetAttributeValue(
      slot_id, object_handle, SessionChapsClient::SerializeToBytes(attributes),
      kDefaultAttempts, std::move(callback));
}

//==============================================================================

void HighLevelChapsClientImpl::SetAttributeValue(
    SessionChapsClient::SlotId slot_id,
    std::vector<SessionChapsClient::ObjectHandle> object_handles,
    const chaps::AttributeList& attributes,
    SessionChapsClient::SetAttributeValueCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SetAttributeValueImpl(slot_id, object_handles, std::move(attributes),
                        std::move(callback),
                        /*last_error=*/chromeos::PKCS11_CKR_OK,
                        /*new_result_code=*/chromeos::PKCS11_CKR_OK);
}

void HighLevelChapsClientImpl::SetAttributeValueImpl(
    SessionChapsClient::SlotId slot_id,
    std::vector<SessionChapsClient::ObjectHandle> object_handles,
    const chaps::AttributeList& attributes,
    SessionChapsClient::SetAttributeValueCallback callback,
    uint32_t last_error,
    uint32_t new_result_code) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (new_result_code != chromeos::PKCS11_CKR_OK) {
    last_error = new_result_code;
  }

  if (object_handles.empty()) {
    return std::move(callback).Run(last_error);
  }

  SessionChapsClient::ObjectHandle next_handle = object_handles.back();
  chaps::AttributeList attributes_copy = attributes;
  object_handles.pop_back();
  auto chaps_callback =
      base::BindOnce(&HighLevelChapsClientImpl::SetAttributeValueImpl,
                     weak_factory_.GetWeakPtr(), slot_id, object_handles,
                     std::move(attributes), std::move(callback), last_error);
  SetAttributeValue(slot_id, next_handle, std::move(attributes_copy),
                    std::move(chaps_callback));
}

//==============================================================================

void HighLevelChapsClientImpl::FindObjects(
    SessionChapsClient::SlotId slot_id,
    const chaps::AttributeList& attributes,
    SessionChapsClient::FindObjectsCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  session_chaps_client_->FindObjects(
      slot_id, SessionChapsClient::SerializeToBytes(attributes),
      kDefaultAttempts, std::move(callback));
}

//==============================================================================

void HighLevelChapsClientImpl::Sign(
    SessionChapsClient::SlotId slot_id,
    uint64_t mechanism_type,
    const std::vector<uint8_t>& mechanism_parameter,
    SessionChapsClient::ObjectHandle key_handle,
    std::vector<uint8_t> data,
    SessionChapsClient::SignCallback callback) {
  session_chaps_client_->Sign(slot_id, mechanism_type, mechanism_parameter,
                              key_handle, std::move(data), kDefaultAttempts,
                              std::move(callback));
}

//==============================================================================

void HighLevelChapsClientImpl::GenerateKeyPair(
    SessionChapsClient::SlotId slot_id,
    uint64_t mechanism_type,
    const std::vector<uint8_t>& mechanism_parameter,
    const chaps::AttributeList& public_attributes,
    const chaps::AttributeList& private_attributes,
    SessionChapsClient::GenerateKeyPairCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  session_chaps_client_->GenerateKeyPair(
      slot_id, mechanism_type, mechanism_parameter,
      SessionChapsClient::SerializeToBytes(public_attributes),
      SessionChapsClient::SerializeToBytes(private_attributes),
      kDefaultAttempts, std::move(callback));
}

}  // namespace kcer