File: test_certificate_provider_extension.h

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 (125 lines) | stat: -rw-r--r-- 4,764 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
// Copyright 2019 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_CERTIFICATE_PROVIDER_TEST_CERTIFICATE_PROVIDER_EXTENSION_H_
#define CHROME_BROWSER_CERTIFICATE_PROVIDER_TEST_CERTIFICATE_PROVIDER_EXTENSION_H_

#include <memory>
#include <optional>
#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/values.h"
#include "extensions/common/extension_id.h"
#include "extensions/test/extension_test_message_listener.h"
#include "net/cert/x509_certificate.h"
#include "third_party/boringssl/src/include/openssl/base.h"

namespace base {
class FilePath;
class Value;
}  // namespace base

namespace content {
class BrowserContext;
}

namespace crypto {
class RSAPrivateKey;
}

namespace ash {

// This class provides the C++ side of the test certificate provider extension's
// implementation (the JavaScript side is in
// chrome/test/data/extensions/test_certificate_provider).
//
// It subscribes itself for requests from the JavaScript side of the extension,
// and implements the cryptographic operations using the "client_1" test
// certificate and private key (see src/net/data/ssl/certificates). The
// supported signature algorithms are currently hardcoded to PKCS #1 v1.5 with
// SHA-1 and SHA-256.
class TestCertificateProviderExtension final {
 public:
  static extensions::ExtensionId extension_id();
  static base::FilePath GetExtensionSourcePath();
  static base::FilePath GetExtensionPemPath();
  // Returns the certificate provided by the extension.
  static scoped_refptr<net::X509Certificate> GetCertificate();
  static std::string GetCertificateSpki();

  explicit TestCertificateProviderExtension(
      content::BrowserContext* browser_context);

  TestCertificateProviderExtension(const TestCertificateProviderExtension&) =
      delete;
  TestCertificateProviderExtension& operator=(
      const TestCertificateProviderExtension&) = delete;

  ~TestCertificateProviderExtension();

  // Causes the extension to call chrome.certificateProvider.setCertificates,
  // providing the certificates that are currently available.
  void TriggerSetCertificates();

  int certificate_request_count() const { return certificate_request_count_; }

  // Sets the PIN that will be required when doing every signature request.
  // (By default, no PIN is requested.)
  void set_require_pin(const std::string& pin) { required_pin_ = pin; }

  // Sets the number of remaining PIN attempts.
  // Zero number means the lockout state, when no attempts are allowed anymore.
  // A negative number denotes infinite number of attempts, which is the default
  // behavior.
  void set_remaining_pin_attempts(int remaining_pin_attempts) {
    remaining_pin_attempts_ = remaining_pin_attempts;
  }

  // Sets whether the extension should return any certificates in response to a
  // onCertificatesRequested request or a TriggerSetCertificates() call.
  void set_should_provide_certificates(bool should_provide_certificates) {
    should_provide_certificates_ = should_provide_certificates;
  }

  // Sets whether the extension should respond with a failure to the
  // onSignDigestRequested requests.
  void set_should_fail_sign_digest_requests(
      bool should_fail_sign_digest_requests) {
    should_fail_sign_digest_requests_ = should_fail_sign_digest_requests;
  }

 private:
  using ReplyToJsCallback =
      base::OnceCallback<void(const base::Value& response)>;

  void HandleMessage(const std::string& message);

  void HandleCertificatesRequest(ReplyToJsCallback callback);
  void HandleSignatureRequest(const base::Value& sign_request,
                              const base::Value& pin_status,
                              const base::Value& pin,
                              ReplyToJsCallback callback);

  const raw_ptr<content::BrowserContext> browser_context_;
  const scoped_refptr<net::X509Certificate> certificate_;
  std::unique_ptr<crypto::RSAPrivateKey> private_key_;
  int certificate_request_count_ = 0;
  // When non-empty, contains the expected PIN; the implementation will request
  // the PIN on every signature request in this case.
  std::optional<std::string> required_pin_;
  // The number of remaining PIN attempts.
  // When equal to zero, signature requests will be failed immediately; when is
  // negative, infinite number of attempts is allowed.
  int remaining_pin_attempts_ = -1;
  bool should_provide_certificates_ = true;
  bool should_fail_sign_digest_requests_ = false;
  ExtensionTestMessageListener message_listener_;
};

}  // namespace ash

#endif  // CHROME_BROWSER_CERTIFICATE_PROVIDER_TEST_CERTIFICATE_PROVIDER_EXTENSION_H_