File: certificate_manager.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (100 lines) | stat: -rw-r--r-- 3,656 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
// 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.

#ifndef CHROME_BROWSER_UI_ASH_FOCUS_MODE_CERTIFICATE_MANAGER_H_
#define CHROME_BROWSER_UI_ASH_FOCUS_MODE_CERTIFICATE_MANAGER_H_

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

#include "base/functional/callback.h"
#include "base/time/time.h"
#include "components/account_id/account_id.h"

namespace ash {

namespace attestation {
class AttestationFlow;
}  // namespace attestation

class AttestationClient;

}  // namespace ash

// Manages certificates from AttestationService for request signing with a
// Content Protection Certificate.
class CertificateManager {
 public:
  // An opaque identifier for certificates to ensure that certificate from
  // `GetCertificate()` can be used for `Sign()`.
  struct Key {
    explicit Key(const std::string& label, base::Time expiration);
    Key(const Key&);

    bool operator==(const Key&);

    // Name of the certificate in attestationd. Used to lookup the key for
    // future signing requests.
    const std::string label;

    // The expiration time of the certificate. Certificates should be renewed
    // before they expire or they can be rejected by the server.
    const base::Time expiration;
  };

  enum class CertificateResult {
    kSuccess,
    kDisallowedByPolicy,
    kInvalidKey,
    kCertificateExpired
  };

  // `expiration_buffer` defines the time allowed before a certificate is
  // considered expired. If a certificate expires in less than
  // `expiration_buffer`, a new certificate will be retrieved. Otherwise, the
  // cached certificate will be provided.
  static std::unique_ptr<CertificateManager> Create(
      const AccountId& account_id,
      base::TimeDelta expiration_buffer);

  // Create a `CertificateManager` for testing where dependencies can be
  // replaced with fakes.
  static std::unique_ptr<CertificateManager> CreateForTesting(
      const AccountId& account_id,
      base::TimeDelta expiration_buffer,
      std::unique_ptr<ash::attestation::AttestationFlow> attestation_flow,
      ash::AttestationClient* attestation_client);

  CertificateManager() = default;
  CertificateManager(const CertificateManager&) = delete;
  CertificateManager& operator=(const CertificateManager&) = delete;
  virtual ~CertificateManager() = default;

  // Attempts to retrieve a certificate via the attestation service. If
  // successful, returns an identifier for use with `Sign()`. If a previously
  // retrieved certificate is still valid, a new certificate will not be
  // retrieved.
  using CertificateCallback = base::OnceCallback<void(
      const std::optional<CertificateManager::Key>& certificate_key)>;
  virtual bool GetCertificate(bool force_update,
                              CertificateCallback callback) = 0;

  // Uses a previously obtained certificate that matches `certificate_key` to
  // sign `data`. Returns the signature for `data` as well as the certificates.
  // Returns false if the certificate is expired or signing is not supported on
  // the device.
  using SigningCallback = base::OnceCallback<void(
      bool success,
      const std::string& signature,
      const std::string& client_certificate,
      const std::vector<std::string>& intermediate_certificates)>;
  virtual CertificateResult Sign(const CertificateManager::Key& certificate_key,
                                 std::string_view data,
                                 SigningCallback callback) = 0;
};

#endif  // CHROME_BROWSER_UI_ASH_FOCUS_MODE_CERTIFICATE_MANAGER_H_