File: key_upload_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 (244 lines) | stat: -rw-r--r-- 8,899 bytes parent folder | download | duplicates (5)
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
// 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 "components/enterprise/client_certificates/core/key_upload_client.h"

#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/types/expected.h"
#include "components/enterprise/client_certificates/core/cloud_management_delegate.h"
#include "components/enterprise/client_certificates/core/private_key.h"
#include "components/enterprise/client_certificates/core/private_key_types.h"
#include "components/policy/core/common/cloud/dmserver_job_configurations.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "crypto/signature_verifier.h"
#include "net/cert/x509_certificate.h"

using BPKUR = enterprise_management::BrowserPublicKeyUploadRequest;

namespace client_certificates {

namespace {

BPKUR::KeyTrustLevel SourceToTrustLevel(PrivateKeySource source) {
  switch (source) {
    case PrivateKeySource::kUnexportableKey:
      return BPKUR::CHROME_BROWSER_HW_KEY;
    case PrivateKeySource::kSoftwareKey:
    case PrivateKeySource::kOsSoftwareKey:
      return BPKUR::CHROME_BROWSER_OS_KEY;
  }
}

BPKUR::KeyType AlgorithmToType(
    crypto::SignatureVerifier::SignatureAlgorithm algorithm) {
  switch (algorithm) {
    case crypto::SignatureVerifier::RSA_PKCS1_SHA1:
    case crypto::SignatureVerifier::RSA_PKCS1_SHA256:
    case crypto::SignatureVerifier::RSA_PSS_SHA256:
      return BPKUR::RSA_KEY;
    case crypto::SignatureVerifier::ECDSA_SHA256:
      return BPKUR::EC_KEY;
  }
}

UploadClientErrorOr<enterprise_management::DeviceManagementRequest>
CreateRequest(scoped_refptr<PrivateKey> private_key, bool create_certificate) {
  if (!private_key) {
    return base::unexpected(UploadClientError::kInvalidKeyParameter);
  }

  // Generate the proof-of-possesion.
  std::vector<uint8_t> pubkey = private_key->GetSubjectPublicKeyInfo();
  std::optional<std::vector<uint8_t>> signature =
      private_key->SignSlowly(pubkey);
  if (!signature.has_value()) {
    return base::unexpected(UploadClientError::kSignatureCreationFailed);
  }

  enterprise_management::DeviceManagementRequest overall_request;
  auto* mutable_upload_request =
      overall_request.mutable_browser_public_key_upload_request();
  mutable_upload_request->set_public_key(pubkey.data(), pubkey.size());
  mutable_upload_request->set_signature(signature->data(), signature->size());
  mutable_upload_request->set_key_trust_level(
      SourceToTrustLevel(private_key->GetSource()));
  mutable_upload_request->set_key_type(
      AlgorithmToType(private_key->GetAlgorithm()));
  mutable_upload_request->set_provision_certificate(create_certificate);

  return overall_request;
}

}  // namespace

class KeyUploadClientImpl : public KeyUploadClient {
 public:
  explicit KeyUploadClientImpl(
      std::unique_ptr<enterprise_attestation::CloudManagementDelegate>
          management_delegate);
  ~KeyUploadClientImpl() override;

  // KeyUploadClient:
  void CreateCertificate(scoped_refptr<PrivateKey> private_key,
                         CreateCertificateCallback callback) override;
  void SyncKey(scoped_refptr<PrivateKey> private_key,
               SyncKeyCallback callback) override;

 private:
  // Asynchronously generates the upload request, involving the creation of a
  // proof-of-poseesion of `private_key`.
  void GetRequest(
      scoped_refptr<PrivateKey> private_key,
      bool create_certificate,
      base::OnceCallback<void(
          UploadClientErrorOr<enterprise_management::DeviceManagementRequest>)>
          callback);

  void OnCertificateRequestCreated(
      CreateCertificateCallback callback,
      UploadClientErrorOr<enterprise_management::DeviceManagementRequest>
          request);

  void OnCertificateResponseReceived(CreateCertificateCallback callback,
                                     policy::DMServerJobResult result);

  void OnSyncRequestCreated(
      SyncKeyCallback callback,
      UploadClientErrorOr<enterprise_management::DeviceManagementRequest>
          request);

  void OnSyncResponseReceived(SyncKeyCallback callback,
                              policy::DMServerJobResult result);

  std::unique_ptr<enterprise_attestation::CloudManagementDelegate>
      management_delegate_;

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

// static
std::unique_ptr<KeyUploadClient> KeyUploadClient::Create(
    std::unique_ptr<enterprise_attestation::CloudManagementDelegate>
        management_delegate) {
  return std::make_unique<KeyUploadClientImpl>(std::move(management_delegate));
}

KeyUploadClientImpl::KeyUploadClientImpl(
    std::unique_ptr<enterprise_attestation::CloudManagementDelegate>
        management_delegate)
    : management_delegate_(std::move(management_delegate)) {
  CHECK(management_delegate_);
}

KeyUploadClientImpl::~KeyUploadClientImpl() = default;

void KeyUploadClientImpl::CreateCertificate(
    scoped_refptr<PrivateKey> private_key,
    CreateCertificateCallback callback) {
  GetRequest(private_key, /*create_certificate=*/true,
             base::BindOnce(&KeyUploadClientImpl::OnCertificateRequestCreated,
                            weak_factory_.GetWeakPtr(), std::move(callback)));
}

void KeyUploadClientImpl::SyncKey(scoped_refptr<PrivateKey> private_key,
                                  SyncKeyCallback callback) {
  GetRequest(private_key, /*create_certificate=*/false,
             base::BindOnce(&KeyUploadClientImpl::OnSyncRequestCreated,
                            weak_factory_.GetWeakPtr(), std::move(callback)));
}

void KeyUploadClientImpl::GetRequest(
    scoped_refptr<PrivateKey> private_key,
    bool create_certificate,
    base::OnceCallback<void(
        UploadClientErrorOr<enterprise_management::DeviceManagementRequest>)>
        callback) {
  if (!management_delegate_->GetDMToken().has_value()) {
    std::move(callback).Run(
        base::unexpected(UploadClientError::kMissingDMToken));
    return;
  }

  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(CreateRequest, std::move(private_key), create_certificate),
      std::move(callback));
}

void KeyUploadClientImpl::OnCertificateRequestCreated(
    CreateCertificateCallback callback,
    UploadClientErrorOr<enterprise_management::DeviceManagementRequest>
        request) {
  if (!request.has_value()) {
    std::move(callback).Run(base::unexpected(request.error()), nullptr);
    return;
  }

  management_delegate_->UploadBrowserPublicKey(
      std::move(request.value()),
      base::BindOnce(&KeyUploadClientImpl::OnCertificateResponseReceived,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}

void KeyUploadClientImpl::OnCertificateResponseReceived(
    CreateCertificateCallback callback,
    policy::DMServerJobResult result) {
  scoped_refptr<net::X509Certificate> certificate = nullptr;
  if (result.dm_status == policy::DM_STATUS_SUCCESS &&
      result.response.has_browser_public_key_upload_response() &&
      result.response.browser_public_key_upload_response()
          .has_pem_encoded_certificate()) {
    // Try to parse the client certificate.
    std::string_view pem_encoded_certificate =
        result.response.browser_public_key_upload_response()
            .pem_encoded_certificate();
    net::CertificateList certs =
        net::X509Certificate::CreateCertificateListFromBytes(
            base::as_byte_span(pem_encoded_certificate),
            net::X509Certificate::FORMAT_AUTO);
    if (!certs.empty()) {
      certificate = certs[0];
    }
  }
  // TODO(b/347949238): return a new UploadClientError::kNetworkError when there
  // is a net error.
  std::move(callback).Run(result.response_code, std::move(certificate));
}

void KeyUploadClientImpl::OnSyncRequestCreated(
    SyncKeyCallback callback,
    UploadClientErrorOr<enterprise_management::DeviceManagementRequest>
        request) {
  if (!request.has_value()) {
    std::move(callback).Run(base::unexpected(request.error()));
    return;
  }

  management_delegate_->UploadBrowserPublicKey(
      std::move(request.value()),
      base::BindOnce(&KeyUploadClientImpl::OnSyncResponseReceived,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}

void KeyUploadClientImpl::OnSyncResponseReceived(
    SyncKeyCallback callback,
    policy::DMServerJobResult result) {
  // TODO(b/347949238): return a new UploadClientError::kNetworkError when there
  // is a net error.
  std::move(callback).Run(result.response_code);
}

}  // namespace client_certificates