File: certificate_viewer_webui.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 (177 lines) | stat: -rw-r--r-- 6,811 bytes parent folder | download | duplicates (3)
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
// Copyright 2012 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_WEBUI_CERTIFICATE_VIEWER_CERTIFICATE_VIEWER_WEBUI_H_
#define CHROME_BROWSER_UI_WEBUI_CERTIFICATE_VIEWER_CERTIFICATE_VIEWER_WEBUI_H_

#include <optional>
#include <string>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/values.h"
#include "chrome/common/net/x509_certificate_model.h"
#include "components/server_certificate_database/server_certificate_database.h"
#include "components/server_certificate_database/server_certificate_database.pb.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "crypto/crypto_buildflags.h"
#include "net/cert/x509_certificate.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/web_dialogs/web_dialog_delegate.h"

#if BUILDFLAG(USE_NSS_CERTS)
#include "net/cert/scoped_nss_types.h"
#endif

namespace content {
class WebContents;
}

using chrome_browser_server_certificate_database::CertificateTrust;

typedef base::RepeatingCallback<void(
    net::ServerCertificateDatabase::CertInformation,
    base::OnceCallback<void(bool)>)>
    CertMetadataModificationsCallback;

class ConstrainedWebDialogDelegate;

// Dialog for displaying detailed certificate information. This is used on
// desktop builds to display detailed information in a floating dialog when the
// user clicks on "Certificate Information" from the lock icon of a web site or
// "View" from the Certificate Manager.
class CertificateViewerDialog : public ui::WebDialogDelegate {
 public:
#if BUILDFLAG(USE_NSS_CERTS)
  static CertificateViewerDialog* ShowConstrained(
      net::ScopedCERTCertificateList nss_certs,
      content::WebContents* web_contents,
      gfx::NativeWindow parent);
#endif

  static CertificateViewerDialog* ShowConstrained(
      std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> certs,
      std::vector<std::string> cert_nicknames,
      content::WebContents* web_contents,
      gfx::NativeWindow parent);

  static CertificateViewerDialog* ShowConstrained(
      bssl::UniquePtr<CRYPTO_BUFFER> cert,
      content::WebContents* web_contents,
      gfx::NativeWindow parent);

  static CertificateViewerDialog* ShowConstrainedWithMetadata(
      bssl::UniquePtr<CRYPTO_BUFFER> cert,
      chrome_browser_server_certificate_database::CertificateMetadata
          cert_metadata,
      CertMetadataModificationsCallback modifications_callback,
      content::WebContents* web_contents,
      gfx::NativeWindow parent);

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

  ~CertificateViewerDialog() override;

  gfx::NativeWindow GetNativeWebContentsModalDialog();

 private:
  friend class CertificateViewerUITest;

  // If |cert_metadata| is present, exactly one cert should be in |certs|.
  // If |modifications_callback| is not null, |cert_metadata| must be present.
  static CertificateViewerDialog* ShowConstrained(
      std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> certs,
      std::vector<std::string> cert_nicknames,
      std::optional<
          chrome_browser_server_certificate_database::CertificateMetadata>
          cert_metadata,
      CertMetadataModificationsCallback modifications_callback,
      content::WebContents* web_contents,
      gfx::NativeWindow parent);

  // Construct a certificate viewer for the passed in certificate. A reference
  // to the certificate pointer is added for the lifetime of the certificate
  // viewer.
  CertificateViewerDialog(
      std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> certs,
      std::vector<std::string> cert_nicknames,
      std::optional<
          chrome_browser_server_certificate_database::CertificateMetadata>
          cert_metadata,
      CertMetadataModificationsCallback modifications_callback);

  raw_ptr<ConstrainedWebDialogDelegate, DanglingUntriaged> delegate_ = nullptr;
};

// Dialog handler which handles calls from the JS WebUI code to view certificate
// details and export the certificate.
class CertificateViewerDialogHandler : public content::WebUIMessageHandler {
 public:
  CertificateViewerDialogHandler(
      CertificateViewerDialog* dialog,
      std::vector<x509_certificate_model::X509CertificateModel> certs,
      std::optional<
          chrome_browser_server_certificate_database::CertificateMetadata>
          cert_metadata,
      CertMetadataModificationsCallback modifications_callback);

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

  ~CertificateViewerDialogHandler() override;

  // Overridden from WebUIMessageHandler
  void RegisterMessages() override;

 private:
  // Brings up the export certificate dialog for the chosen certificate in the
  // chain.
  //
  // The input is an integer index to the certificate in the chain to export.
  void HandleExportCertificate(const base::Value::List& args);

  // Gets the details for a specific certificate in the certificate chain.
  // Responds with a tree structure containing the fields and values for certain
  // nodes.
  //
  // The input is an integer index to the certificate in the chain to view.
  void HandleRequestCertificateFields(const base::Value::List& args);

  // Update the trust state of the certificate.
  void HandleUpdateTrustState(const base::Value::List& args);
  void UpdateTrustStateDone(const base::Value& callback_id,
                            CertificateTrust::CertificateTrustType new_trust,
                            bool success);

  void HandleAddConstraint(const base::Value::List& args);
  void HandleDeleteConstraint(const base::Value::List& args);
  void UpdateConstraintsDone(
      const base::Value& callback_id,
      const chrome_browser_server_certificate_database::Constraints
          new_constraints,
      bool success);

  bool CanModifyMetadata() const;

  // Helper function to get the certificate index. Returns -1 if the index is
  // out of range.
  int GetCertificateIndex(int requested_index) const;

  // The dialog.
  raw_ptr<CertificateViewerDialog> dialog_;

  std::vector<x509_certificate_model::X509CertificateModel> certs_;
  std::optional<chrome_browser_server_certificate_database::CertificateMetadata>
      cert_metadata_;
  // Cert Metadata modifications callback. If null, then no modifications are
  // allowed for this certificate.
  CertMetadataModificationsCallback modifications_callback_;
  base::WeakPtrFactory<CertificateViewerDialogHandler> weak_ptr_factory_{this};
};

#endif  // CHROME_BROWSER_UI_WEBUI_CERTIFICATE_VIEWER_CERTIFICATE_VIEWER_WEBUI_H_