blob: bf59d114f1376dca0b1e2e88964ab1f970c6bd4d [file] [log] [blame]
[email protected]e0ad0892012-05-22 19:16:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]df8e899b2011-02-22 22:58:222// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4f242962011-05-13 22:25:225#ifndef CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_
6#define CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_
[email protected]df8e899b2011-02-22 22:58:227
8#include <map>
dcheng4af48582016-04-19 00:29:359#include <memory>
[email protected]df8e899b2011-02-22 22:58:2210#include <string>
11
[email protected]3065a1f2014-01-22 08:56:3512#include "base/callback.h"
avie4d7b6f2015-12-26 00:59:1813#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
[email protected]e7463412013-06-10 22:53:4615#include "base/strings/string16.h"
[email protected]6e7845ae2013-03-29 21:48:1116#include "net/cert/nss_cert_database.h"
Matt Mueller917b4e12017-09-01 19:15:3517#include "net/cert/scoped_nss_types.h"
mattmbbf7fc02017-06-19 23:38:1918#include "net/ssl/client_cert_identity.h"
[email protected]df8e899b2011-02-22 22:58:2219
[email protected]3065a1f2014-01-22 08:56:3520namespace content {
21class BrowserContext;
22class ResourceContext;
23} // namespace content
24
Pavol Markob429f542018-08-23 06:08:1925#if defined(OS_CHROMEOS)
26namespace chromeos {
27class CertificateProvider;
Pavol Markob429f542018-08-23 06:08:1928class PolicyCertificateProvider;
29}
30#endif
31
[email protected]df8e899b2011-02-22 22:58:2232// CertificateManagerModel provides the data to be displayed in the certificate
33// manager dialog, and processes changes from the view.
34class CertificateManagerModel {
35 public:
Pavol Markob429f542018-08-23 06:08:1936 // Holds information about a certificate, along with the certificate itself.
37 class CertInfo {
38 public:
39 enum class Source {
40 // This certificate is installed in the platform certificate database.
41 kPlatform,
42 // This certificate is provided by enterprise policy.
43 kPolicy,
44 // This certificate is provided by an extension.
45 kExtension
46 };
47
48 CertInfo(net::ScopedCERTCertificate cert,
49 net::CertType type,
50 base::string16 name,
51 bool read_only,
52 bool untrusted,
53 Source source,
54 bool web_trust_anchor,
Andreea Costinaseaec4c172019-02-04 11:57:4355 bool hardware_backed,
56 bool device_wide);
Pavol Markob429f542018-08-23 06:08:1957 ~CertInfo();
58
59 CERTCertificate* cert() const { return cert_.get(); }
60 net::CertType type() const { return type_; }
61 const base::string16& name() const { return name_; }
62 bool read_only() const { return read_only_; }
63 bool untrusted() const { return untrusted_; }
64 Source source() const { return source_; }
65 bool web_trust_anchor() const { return web_trust_anchor_; }
66 bool hardware_backed() const { return hardware_backed_; }
Andreea Costinaseaec4c172019-02-04 11:57:4367 bool device_wide() const { return device_wide_; }
Pavol Markob429f542018-08-23 06:08:1968
69 // Clones a CertInfo, duplicating the contained NSS certificate.
70 static std::unique_ptr<CertInfo> Clone(const CertInfo* cert_info);
71
72 private:
73 // The certificate itself.
74 net::ScopedCERTCertificate cert_;
75
76 // The type of the certificate. Used to filter certificates to be displayed
77 // on the tabs of the certificate manager UI.
78 net::CertType type_;
79
80 // A user readable certificate name.
81 base::string16 name_;
82
83 // true if the certificate is stored on a read-only slot or provided by
84 // enterprise policy or an extension.
85 bool read_only_;
86
87 // true if the certificate is untrusted.
88 bool untrusted_;
89
90 // Describes where this certificate originates from.
91 Source source_;
92
93 // true if the certificate is given web trust (either by its platform trust
94 // settings, or by enterprise policy).
95 bool web_trust_anchor_;
96
97 // true if the certificate is hardware-backed. Note that extension-provided
98 // certificates are not regarded as hardware-backed.
99 bool hardware_backed_;
100
Andreea Costinaseaec4c172019-02-04 11:57:43101 // true if the certificate is device-wide.
102 // Note: can be true only on Chrome OS.
103 bool device_wide_;
104
Pavol Markob429f542018-08-23 06:08:19105 DISALLOW_COPY_AND_ASSIGN(CertInfo);
106 };
107
108 class CertsSource;
109
110 // Holds parameters during construction.
111 struct Params {
112#if defined(OS_CHROMEOS)
113 // May be nullptr.
Pavol Marko3bab3afd2018-09-24 14:42:17114 chromeos::PolicyCertificateProvider* policy_certs_provider = nullptr;
Pavol Markob429f542018-08-23 06:08:19115 // May be nullptr.
116 std::unique_ptr<chromeos::CertificateProvider>
117 extension_certificate_provider;
118#endif
119
120 Params();
121 Params(Params&& other);
122 ~Params();
123
124 private:
125 DISALLOW_COPY_AND_ASSIGN(Params);
126 };
127
[email protected]df8e899b2011-02-22 22:58:22128 // Map from the subject organization name to the list of certs from that
129 // organization. If a cert does not have an organization name, the
130 // subject's CertPrincipal::GetDisplayName() value is used instead.
Pavol Markob429f542018-08-23 06:08:19131 typedef std::map<std::string, std::vector<std::unique_ptr<CertInfo>>>
132 OrgGroupingMap;
[email protected]df8e899b2011-02-22 22:58:22133
dcheng4af48582016-04-19 00:29:35134 typedef base::Callback<void(std::unique_ptr<CertificateManagerModel>)>
[email protected]3065a1f2014-01-22 08:56:35135 CreationCallback;
136
[email protected]df8e899b2011-02-22 22:58:22137 class Observer {
138 public:
139 // Called to notify the view that the certificate list has been refreshed.
140 // TODO(mattm): do a more granular updating strategy? Maybe retrieve new
141 // list of certs, diff against past list, and then notify of the changes?
142 virtual void CertificatesRefreshed() = 0;
Pavol Markob429f542018-08-23 06:08:19143
144 protected:
145 virtual ~Observer() = default;
[email protected]df8e899b2011-02-22 22:58:22146 };
147
[email protected]3065a1f2014-01-22 08:56:35148 // Creates a CertificateManagerModel. The model will be passed to the callback
149 // when it is ready. The caller must ensure the model does not outlive the
150 // |browser_context|.
151 static void Create(content::BrowserContext* browser_context,
152 Observer* observer,
153 const CreationCallback& callback);
154
Pavol Markob429f542018-08-23 06:08:19155 // Use |Create| instead to create a |CertificateManagerModel| for a
156 // |BrowserContext|.
157 CertificateManagerModel(std::unique_ptr<Params> params,
158 Observer* observer,
159 net::NSSCertDatabase* nss_cert_database,
160 bool is_user_db_available,
161 bool is_tpm_available);
[email protected]df8e899b2011-02-22 22:58:22162 ~CertificateManagerModel();
163
[email protected]16dad0962014-03-18 01:29:11164 bool is_user_db_available() const { return is_user_db_available_; }
[email protected]3065a1f2014-01-22 08:56:35165 bool is_tpm_available() const { return is_tpm_available_; }
166
[email protected]7fda9a402012-09-10 14:11:07167 // Accessor for read-only access to the underlying NSSCertDatabase.
168 const net::NSSCertDatabase* cert_db() const { return cert_db_; }
[email protected]df8e899b2011-02-22 22:58:22169
[email protected]4c4f7cd2011-03-05 02:20:44170 // Trigger a refresh of the list of certs, unlock any slots if necessary.
171 // Following this call, the observer CertificatesRefreshed method will be
172 // called so the view can call FilterAndBuildOrgGroupingMap as necessary to
173 // refresh its tree views.
[email protected]df8e899b2011-02-22 22:58:22174 void Refresh();
175
Pavol Markob429f542018-08-23 06:08:19176 // Fill |*out_org_grouping_map| with the certificates matching |filter_type|.
[email protected]df8e899b2011-02-22 22:58:22177 void FilterAndBuildOrgGroupingMap(net::CertType filter_type,
Pavol Markob429f542018-08-23 06:08:19178 OrgGroupingMap* out_org_grouping_map) const;
[email protected]df8e899b2011-02-22 22:58:22179
[email protected]6a18d072011-06-29 00:25:40180 // Import private keys and certificates from PKCS #12 encoded
181 // |data|, using the given |password|. If |is_extractable| is false,
tfarinaf58077a2017-01-13 11:40:05182 // mark the private key as unextractable from the slot.
[email protected]6a18d072011-06-29 00:25:40183 // Returns a net error code on failure.
tfarinaf58077a2017-01-13 11:40:05184 int ImportFromPKCS12(PK11SlotInfo* slot_info, const std::string& data,
[email protected]96920152013-12-04 21:00:16185 const base::string16& password, bool is_extractable);
[email protected]df8e899b2011-02-22 22:58:22186
svaldez3e98a712015-11-23 16:21:57187 // Import user certificate from DER encoded |data|.
188 // Returns a net error code on failure.
189 int ImportUserCert(const std::string& data);
190
[email protected]df8e899b2011-02-22 22:58:22191 // Import CA certificates.
192 // Tries to import all the certificates given. The root will be trusted
193 // according to |trust_bits|. Any certificates that could not be imported
194 // will be listed in |not_imported|.
[email protected]7fda9a402012-09-10 14:11:07195 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:22196 // Returns false if there is an internal error, otherwise true is returned and
197 // |not_imported| should be checked for any certificates that were not
198 // imported.
Matt Mueller917b4e12017-09-01 19:15:35199 bool ImportCACerts(const net::ScopedCERTCertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07200 net::NSSCertDatabase::TrustBits trust_bits,
201 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22202
203 // Import server certificate. The first cert should be the server cert. Any
204 // additional certs should be intermediate/CA certs and will be imported but
205 // not given any trust.
206 // Any certificates that could not be imported will be listed in
207 // |not_imported|.
[email protected]ad40b212012-06-01 05:59:56208 // |trust_bits| can be set to explicitly trust or distrust the certificate, or
209 // use TRUST_DEFAULT to inherit trust as normal.
[email protected]df8e899b2011-02-22 22:58:22210 // Returns false if there is an internal error, otherwise true is returned and
211 // |not_imported| should be checked for any certificates that were not
212 // imported.
213 bool ImportServerCert(
Matt Mueller917b4e12017-09-01 19:15:35214 const net::ScopedCERTCertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07215 net::NSSCertDatabase::TrustBits trust_bits,
216 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22217
218 // Set trust values for certificate.
[email protected]7fda9a402012-09-10 14:11:07219 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:22220 // Returns true on success or false on failure.
Matt Mueller917b4e12017-09-01 19:15:35221 bool SetCertTrust(CERTCertificate* cert,
[email protected]df8e899b2011-02-22 22:58:22222 net::CertType type,
[email protected]7fda9a402012-09-10 14:11:07223 net::NSSCertDatabase::TrustBits trust_bits);
[email protected]df8e899b2011-02-22 22:58:22224
225 // Delete the cert. Returns true on success. |cert| is still valid when this
226 // function returns.
Matt Mueller917b4e12017-09-01 19:15:35227 bool Delete(CERTCertificate* cert);
[email protected]df8e899b2011-02-22 22:58:22228
229 private:
Pavol Markob429f542018-08-23 06:08:19230 // Called when one of the |certs_sources_| has been updated. Will notify the
231 // |observer_| that the certificate list has been refreshed.
232 void OnCertsSourceUpdated();
233
234 // Finds the |CertsSource| which provided |cert|. Can return nullptr (e.g. if
235 // the cert has been deleted in the meantime).
236 CertsSource* FindCertsSourceForCert(CERTCertificate* cert);
[email protected]3065a1f2014-01-22 08:56:35237
238 // Methods used during initialization, see the comment at the top of the .cc
239 // file for details.
240 static void DidGetCertDBOnUIThread(
Pavol Markob429f542018-08-23 06:08:19241 std::unique_ptr<Params> params,
242 CertificateManagerModel::Observer* observer,
243 const CreationCallback& callback,
[email protected]3065a1f2014-01-22 08:56:35244 net::NSSCertDatabase* cert_db,
[email protected]16dad0962014-03-18 01:29:11245 bool is_user_db_available,
Pavol Markob429f542018-08-23 06:08:19246 bool is_tpm_available);
[email protected]3065a1f2014-01-22 08:56:35247 static void DidGetCertDBOnIOThread(
Pavol Markob429f542018-08-23 06:08:19248 std::unique_ptr<Params> params,
[email protected]3065a1f2014-01-22 08:56:35249 CertificateManagerModel::Observer* observer,
250 const CreationCallback& callback,
251 net::NSSCertDatabase* cert_db);
Pavol Markob429f542018-08-23 06:08:19252 static void GetCertDBOnIOThread(std::unique_ptr<Params> params,
253 content::ResourceContext* resource_context,
254 CertificateManagerModel::Observer* observer,
255 const CreationCallback& callback);
isandrk20c70a22016-09-22 21:41:10256
[email protected]7fda9a402012-09-10 14:11:07257 net::NSSCertDatabase* cert_db_;
Pavol Markob429f542018-08-23 06:08:19258
259 // CertsSource instances providing certificates. The order matters - if a
260 // certificate is provided by more than one CertsSource, only the first one is
261 // accepted.
262 std::vector<std::unique_ptr<CertsSource>> certs_sources_;
263
264 bool hold_back_updates_ = false;
265
[email protected]16dad0962014-03-18 01:29:11266 // Whether the certificate database has a public slot associated with the
267 // profile. If not set, importing certificates is not allowed with this model.
268 bool is_user_db_available_;
[email protected]3065a1f2014-01-22 08:56:35269 bool is_tpm_available_;
[email protected]df8e899b2011-02-22 22:58:22270
271 // The observer to notify when certificate list is refreshed.
272 Observer* observer_;
273
274 DISALLOW_COPY_AND_ASSIGN(CertificateManagerModel);
275};
276
[email protected]4f242962011-05-13 22:25:22277#endif // CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_