File: attestation_client.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (361 lines) | stat: -rw-r--r-- 12,913 bytes parent folder | download | duplicates (7)
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
// Copyright 2020 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/dbus/attestation/attestation_client.h"

#include <utility>

#include <google/protobuf/message_lite.h>

#include "base/check_op.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "chromeos/ash/components/dbus/attestation/fake_attestation_client.h"
#include "chromeos/dbus/constants/dbus_switches.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/attestation/dbus-constants.h"

namespace ash {
namespace {

// Values for the attestation server switch.
const char kAttestationServerDefault[] = "default";
const char kAttestationServerTest[] = "test";

// An arbitrary timeout for getting a certificate.
constexpr base::TimeDelta kGetCertificateTimeout = base::Seconds(80);

AttestationClient* g_instance = nullptr;

// Tries to parse a proto message from |response| into |proto|.
// Returns false if |response| is nullptr or the message cannot be parsed.
bool ParseProto(dbus::Response* response,
                google::protobuf::MessageLite* proto) {
  if (!response) {
    LOG(ERROR) << "Failed to call attestationd";
    return false;
  }

  dbus::MessageReader reader(response);
  if (!reader.PopArrayOfBytesAsProto(proto)) {
    LOG(ERROR) << "Failed to parse response message from attestationd";
    return false;
  }

  return true;
}

// "Real" implementation of AttestationClient talking to the Attestation daemon
// on the Chrome OS side.
class AttestationClientImpl : public AttestationClient {
 public:
  AttestationClientImpl() = default;
  ~AttestationClientImpl() override = default;

  // Not copyable or movable.
  AttestationClientImpl(const AttestationClientImpl&) = delete;
  AttestationClientImpl& operator=(const AttestationClientImpl&) = delete;
  AttestationClientImpl(AttestationClientImpl&&) = delete;
  AttestationClientImpl& operator=(AttestationClientImpl&&) = delete;

  // AttestationClient overrides:
  void GetKeyInfo(const ::attestation::GetKeyInfoRequest& request,
                  GetKeyInfoCallback callback) override {
    CallProtoMethod(attestation::kGetKeyInfo, request, std::move(callback));
  }

  void GetEndorsementInfo(
      const ::attestation::GetEndorsementInfoRequest& request,
      GetEndorsementInfoCallback callback) override {
    CallProtoMethod(attestation::kGetEndorsementInfo, request,
                    std::move(callback));
  }

  void GetAttestationKeyInfo(
      const ::attestation::GetAttestationKeyInfoRequest& request,
      GetAttestationKeyInfoCallback callback) override {
    CallProtoMethod(attestation::kGetAttestationKeyInfo, request,
                    std::move(callback));
  }

  void ActivateAttestationKey(
      const ::attestation::ActivateAttestationKeyRequest& request,
      ActivateAttestationKeyCallback callback) override {
    CallProtoMethod(attestation::kActivateAttestationKey, request,
                    std::move(callback));
  }

  void CreateCertifiableKey(
      const ::attestation::CreateCertifiableKeyRequest& request,
      CreateCertifiableKeyCallback callback) override {
    CallProtoMethod(attestation::kCreateCertifiableKey, request,
                    std::move(callback));
  }

  void Decrypt(const ::attestation::DecryptRequest& request,
               DecryptCallback callback) override {
    CallProtoMethod(attestation::kDecrypt, request, std::move(callback));
  }

  void Sign(const ::attestation::SignRequest& request,
            SignCallback callback) override {
    CallProtoMethod(attestation::kSign, request, std::move(callback));
  }

  void RegisterKeyWithChapsToken(
      const ::attestation::RegisterKeyWithChapsTokenRequest& request,
      RegisterKeyWithChapsTokenCallback callback) override {
    CallProtoMethod(attestation::kRegisterKeyWithChapsToken, request,
                    std::move(callback));
  }

  void GetEnrollmentPreparations(
      const ::attestation::GetEnrollmentPreparationsRequest& request,
      GetEnrollmentPreparationsCallback callback) override {
    CallProtoMethod(attestation::kGetEnrollmentPreparations, request,
                    std::move(callback));
  }

  void GetFeatures(const ::attestation::GetFeaturesRequest& request,
                   GetFeaturesCallback callback) override {
    CallProtoMethod(attestation::kGetFeatures, request, std::move(callback));
  }

  void GetStatus(const ::attestation::GetStatusRequest& request,
                 GetStatusCallback callback) override {
    CallProtoMethod(attestation::kGetStatus, request, std::move(callback));
  }

  void Verify(const ::attestation::VerifyRequest& request,
              VerifyCallback callback) override {
    CallProtoMethod(attestation::kVerify, request, std::move(callback));
  }

  void CreateEnrollRequest(
      const ::attestation::CreateEnrollRequestRequest& request,
      CreateEnrollRequestCallback callback) override {
    CallProtoMethod(attestation::kCreateEnrollRequest, request,
                    std::move(callback));
  }

  void FinishEnroll(const ::attestation::FinishEnrollRequest& request,
                    FinishEnrollCallback callback) override {
    CallProtoMethod(attestation::kFinishEnroll, request, std::move(callback));
  }

  void CreateCertificateRequest(
      const ::attestation::CreateCertificateRequestRequest& request,
      CreateCertificateRequestCallback callback) override {
    CallProtoMethod(attestation::kCreateCertificateRequest, request,
                    std::move(callback));
  }

  void FinishCertificateRequest(
      const ::attestation::FinishCertificateRequestRequest& request,
      FinishCertificateRequestCallback callback) override {
    CallProtoMethod(attestation::kFinishCertificateRequest, request,
                    std::move(callback));
  }

  void Enroll(const ::attestation::EnrollRequest& request,
              EnrollCallback callback) override {
    CallProtoMethod(attestation::kEnroll, request, std::move(callback));
  }

  void GetCertificate(const ::attestation::GetCertificateRequest& request,
                      GetCertificateCallback callback) override {
    CallProtoMethodWithTimeout(attestation::kGetCertificate,
                               kGetCertificateTimeout.InMilliseconds(), request,
                               std::move(callback));
  }

  void SignEnterpriseChallenge(
      const ::attestation::SignEnterpriseChallengeRequest& request,
      SignEnterpriseChallengeCallback callback) override {
    CallProtoMethod(attestation::kSignEnterpriseChallenge, request,
                    std::move(callback));
  }

  void SignSimpleChallenge(
      const ::attestation::SignSimpleChallengeRequest& request,
      SignSimpleChallengeCallback callback) override {
    CallProtoMethod(attestation::kSignSimpleChallenge, request,
                    std::move(callback));
  }

  void SetKeyPayload(const ::attestation::SetKeyPayloadRequest& request,
                     SetKeyPayloadCallback callback) override {
    CallProtoMethod(attestation::kSetKeyPayload, request, std::move(callback));
  }

  void DeleteKeys(const ::attestation::DeleteKeysRequest& request,
                  DeleteKeysCallback callback) override {
    CallProtoMethod(attestation::kDeleteKeys, request, std::move(callback));
  }

  void ResetIdentity(const ::attestation::ResetIdentityRequest& request,
                     ResetIdentityCallback callback) override {
    CallProtoMethod(attestation::kResetIdentity, request, std::move(callback));
  }

  void GetEnrollmentId(const ::attestation::GetEnrollmentIdRequest& request,
                       GetEnrollmentIdCallback callback) override {
    CallProtoMethod(attestation::kGetEnrollmentId, request,
                    std::move(callback));
  }

  void GetCertifiedNvIndex(
      const ::attestation::GetCertifiedNvIndexRequest& request,
      GetCertifiedNvIndexCallback callback) override {
    CallProtoMethod(attestation::kGetCertifiedNvIndex, request,
                    std::move(callback));
  }

  void WaitForServiceToBeAvailable(
      chromeos::WaitForServiceToBeAvailableCallback callback) override {
    proxy_->WaitForServiceToBeAvailable(std::move(callback));
  }

  void Init(dbus::Bus* bus) {
    proxy_ = bus->GetObjectProxy(
        ::attestation::kAttestationServiceName,
        dbus::ObjectPath(attestation::kAttestationServicePath));
  }

 private:
  TestInterface* GetTestInterface() override { return nullptr; }

  // Calls attestationd's |method_name| method, passing in |request| as input
  // with |timeout_ms|. Once the (asynchronous) call finishes, |callback| is
  // called with the response proto.
  template <typename RequestType, typename ReplyType>
  void CallProtoMethodWithTimeout(
      const char* method_name,
      int timeout_ms,
      const RequestType& request,
      base::OnceCallback<void(const ReplyType&)> callback) {
    dbus::MethodCall method_call(attestation::kAttestationInterface,
                                 method_name);
    dbus::MessageWriter writer(&method_call);
    if (!writer.AppendProtoAsArrayOfBytes(request)) {
      ReplyType reply;
      reply.set_status(attestation::STATUS_INVALID_PARAMETER);
      base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
          FROM_HERE, base::BindOnce(std::move(callback), reply));
      return;
    }
    // Bind with the weak pointer of |this| so the response is not
    // handled once |this| is already destroyed.
    proxy_->CallMethod(
        &method_call, timeout_ms,
        base::BindOnce(&AttestationClientImpl::HandleResponse<ReplyType>,
                       weak_factory_.GetWeakPtr(), std::move(callback)));
  }

  // Calls attestationd's |method_name| method, passing in |request| as input
  // with the default timeout. Once the (asynchronous) call finishes, |callback|
  // is called with the response proto.
  template <typename RequestType, typename ReplyType>
  void CallProtoMethod(const char* method_name,
                       const RequestType& request,
                       base::OnceCallback<void(const ReplyType&)> callback) {
    CallProtoMethodWithTimeout(method_name,
                               dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, request,
                               std::move(callback));
  }

  // Parses the response proto message from |response| and calls |callback| with
  // the decoded message. Calls |callback| with an |STATUS_DBUS_ERROR| message
  // on error, including timeout.
  template <typename ReplyType>
  void HandleResponse(base::OnceCallback<void(const ReplyType&)> callback,
                      dbus::Response* response) {
    ReplyType reply_proto;
    if (!ParseProto(response, &reply_proto))
      reply_proto.set_status(attestation::STATUS_DBUS_ERROR);
    std::move(callback).Run(reply_proto);
  }

  // D-Bus proxy for the Attestation daemon, not owned.
  raw_ptr<dbus::ObjectProxy> proxy_ = nullptr;

  base::WeakPtrFactory<AttestationClientImpl> weak_factory_{this};
};

}  // namespace

AttestationClient::AttestationClient() {
  CHECK(!g_instance);
  g_instance = this;
}

AttestationClient::~AttestationClient() {
  CHECK_EQ(this, g_instance);
  g_instance = nullptr;
}

// static
void AttestationClient::Initialize(dbus::Bus* bus) {
  CHECK(bus);
  (new AttestationClientImpl())->Init(bus);
}

// static
void AttestationClient::InitializeFake() {
  new FakeAttestationClient();
}

// static
void AttestationClient::Shutdown() {
  CHECK(g_instance);
  delete g_instance;
  // The destructor resets |g_instance|.
  DCHECK(!g_instance);
}

// static
AttestationClient* AttestationClient::Get() {
  return g_instance;
}

// static
bool AttestationClient::IsAttestationPrepared(
    const ::attestation::GetEnrollmentPreparationsReply& reply) {
  if (reply.status() != ::attestation::STATUS_SUCCESS) {
    return false;
  }
  for (const auto& preparation : reply.enrollment_preparations()) {
    if (preparation.second) {
      return true;
    }
  }
  return false;
}

// static
::attestation::VAType AttestationClient::GetVerifiedAccessServerType() {
  std::string value =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          chromeos::switches::kAttestationServer);
  if (value.empty() || value == kAttestationServerDefault) {
    return ::attestation::DEFAULT_VA;
  }
  if (value == kAttestationServerTest) {
    return ::attestation::TEST_VA;
  }
  LOG(WARNING) << "Invalid Verified Access server value: " << value
               << ". Using default.";
  return attestation::DEFAULT_VA;
}

}  // namespace ash