blob: 66255aa9627ac204c57399af294f4a1ee09a356c [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,
Andreea Costinasa45a6052019-07-22 09:42:3051 bool can_be_deleted,
Pavol Markob429f542018-08-23 06:08:1952 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_; }
Andreea Costinasa45a6052019-07-22 09:42:3062 bool can_be_deleted() const { return can_be_deleted_; }
Pavol Markob429f542018-08-23 06:08:1963 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
Andreea Costinasa45a6052019-07-22 09:42:3083 // false if the certificate is stored on a read-only slot or provided by
84 // enterprise policy or an extension, otherwise true.
85 bool can_be_deleted_;
Pavol Markob429f542018-08-23 06:08:1986
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);
Michael Ershov34668572019-07-31 10:01:12106
107 FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest,
108 CanDeleteCertificateCommonTest);
109 FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest,
110 CanDeleteUserCertificateTest);
111 FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest,
112 CanDeleteCACertificateTest);
113 FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest,
114 CanEditCertificateCommonTest);
115 FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest,
116 CanEditUserCertificateTest);
117 FRIEND_TEST_ALL_PREFIXES(CertificateHandlerTest, CanEditCACertificateTest);
Pavol Markob429f542018-08-23 06:08:19118 };
119
120 class CertsSource;
121
122 // Holds parameters during construction.
123 struct Params {
124#if defined(OS_CHROMEOS)
125 // May be nullptr.
Pavol Marko3bab3afd2018-09-24 14:42:17126 chromeos::PolicyCertificateProvider* policy_certs_provider = nullptr;
Pavol Markob429f542018-08-23 06:08:19127 // May be nullptr.
128 std::unique_ptr<chromeos::CertificateProvider>
129 extension_certificate_provider;
130#endif
131
132 Params();
133 Params(Params&& other);
134 ~Params();
135
136 private:
137 DISALLOW_COPY_AND_ASSIGN(Params);
138 };
139
[email protected]df8e899b2011-02-22 22:58:22140 // Map from the subject organization name to the list of certs from that
141 // organization. If a cert does not have an organization name, the
142 // subject's CertPrincipal::GetDisplayName() value is used instead.
Shelley Vohrb46035be2020-04-15 23:32:19143 using OrgGroupingMap =
144 std::map<std::string, std::vector<std::unique_ptr<CertInfo>>>;
[email protected]df8e899b2011-02-22 22:58:22145
Shelley Vohrb46035be2020-04-15 23:32:19146 using CreationCallback =
147 base::OnceCallback<void(std::unique_ptr<CertificateManagerModel>)>;
[email protected]3065a1f2014-01-22 08:56:35148
[email protected]df8e899b2011-02-22 22:58:22149 class Observer {
150 public:
151 // Called to notify the view that the certificate list has been refreshed.
152 // TODO(mattm): do a more granular updating strategy? Maybe retrieve new
153 // list of certs, diff against past list, and then notify of the changes?
154 virtual void CertificatesRefreshed() = 0;
Pavol Markob429f542018-08-23 06:08:19155
156 protected:
157 virtual ~Observer() = default;
[email protected]df8e899b2011-02-22 22:58:22158 };
159
[email protected]3065a1f2014-01-22 08:56:35160 // Creates a CertificateManagerModel. The model will be passed to the callback
161 // when it is ready. The caller must ensure the model does not outlive the
162 // |browser_context|.
163 static void Create(content::BrowserContext* browser_context,
164 Observer* observer,
Shelley Vohrb46035be2020-04-15 23:32:19165 CreationCallback callback);
[email protected]3065a1f2014-01-22 08:56:35166
Pavol Markob429f542018-08-23 06:08:19167 // Use |Create| instead to create a |CertificateManagerModel| for a
168 // |BrowserContext|.
169 CertificateManagerModel(std::unique_ptr<Params> params,
170 Observer* observer,
171 net::NSSCertDatabase* nss_cert_database,
172 bool is_user_db_available,
173 bool is_tpm_available);
[email protected]df8e899b2011-02-22 22:58:22174 ~CertificateManagerModel();
175
[email protected]16dad0962014-03-18 01:29:11176 bool is_user_db_available() const { return is_user_db_available_; }
[email protected]3065a1f2014-01-22 08:56:35177 bool is_tpm_available() const { return is_tpm_available_; }
178
[email protected]7fda9a402012-09-10 14:11:07179 // Accessor for read-only access to the underlying NSSCertDatabase.
180 const net::NSSCertDatabase* cert_db() const { return cert_db_; }
[email protected]df8e899b2011-02-22 22:58:22181
[email protected]4c4f7cd2011-03-05 02:20:44182 // Trigger a refresh of the list of certs, unlock any slots if necessary.
183 // Following this call, the observer CertificatesRefreshed method will be
184 // called so the view can call FilterAndBuildOrgGroupingMap as necessary to
185 // refresh its tree views.
[email protected]df8e899b2011-02-22 22:58:22186 void Refresh();
187
Pavol Markob429f542018-08-23 06:08:19188 // Fill |*out_org_grouping_map| with the certificates matching |filter_type|.
[email protected]df8e899b2011-02-22 22:58:22189 void FilterAndBuildOrgGroupingMap(net::CertType filter_type,
Pavol Markob429f542018-08-23 06:08:19190 OrgGroupingMap* out_org_grouping_map) const;
[email protected]df8e899b2011-02-22 22:58:22191
[email protected]6a18d072011-06-29 00:25:40192 // Import private keys and certificates from PKCS #12 encoded
193 // |data|, using the given |password|. If |is_extractable| is false,
tfarinaf58077a2017-01-13 11:40:05194 // mark the private key as unextractable from the slot.
[email protected]6a18d072011-06-29 00:25:40195 // Returns a net error code on failure.
Shelley Vohrb46035be2020-04-15 23:32:19196 int ImportFromPKCS12(PK11SlotInfo* slot_info,
197 const std::string& data,
198 const base::string16& password,
199 bool is_extractable);
[email protected]df8e899b2011-02-22 22:58:22200
svaldez3e98a712015-11-23 16:21:57201 // Import user certificate from DER encoded |data|.
202 // Returns a net error code on failure.
203 int ImportUserCert(const std::string& data);
204
[email protected]df8e899b2011-02-22 22:58:22205 // Import CA certificates.
206 // Tries to import all the certificates given. The root will be trusted
207 // according to |trust_bits|. Any certificates that could not be imported
208 // will be listed in |not_imported|.
[email protected]7fda9a402012-09-10 14:11:07209 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[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.
Matt Mueller917b4e12017-09-01 19:15:35213 bool ImportCACerts(const net::ScopedCERTCertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07214 net::NSSCertDatabase::TrustBits trust_bits,
215 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22216
217 // Import server certificate. The first cert should be the server cert. Any
218 // additional certs should be intermediate/CA certs and will be imported but
219 // not given any trust.
220 // Any certificates that could not be imported will be listed in
221 // |not_imported|.
[email protected]ad40b212012-06-01 05:59:56222 // |trust_bits| can be set to explicitly trust or distrust the certificate, or
223 // use TRUST_DEFAULT to inherit trust as normal.
[email protected]df8e899b2011-02-22 22:58:22224 // Returns false if there is an internal error, otherwise true is returned and
225 // |not_imported| should be checked for any certificates that were not
226 // imported.
227 bool ImportServerCert(
Matt Mueller917b4e12017-09-01 19:15:35228 const net::ScopedCERTCertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07229 net::NSSCertDatabase::TrustBits trust_bits,
230 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22231
232 // Set trust values for certificate.
[email protected]7fda9a402012-09-10 14:11:07233 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:22234 // Returns true on success or false on failure.
Matt Mueller917b4e12017-09-01 19:15:35235 bool SetCertTrust(CERTCertificate* cert,
[email protected]df8e899b2011-02-22 22:58:22236 net::CertType type,
[email protected]7fda9a402012-09-10 14:11:07237 net::NSSCertDatabase::TrustBits trust_bits);
[email protected]df8e899b2011-02-22 22:58:22238
239 // Delete the cert. Returns true on success. |cert| is still valid when this
240 // function returns.
Matt Mueller917b4e12017-09-01 19:15:35241 bool Delete(CERTCertificate* cert);
[email protected]df8e899b2011-02-22 22:58:22242
243 private:
Pavol Markob429f542018-08-23 06:08:19244 // Called when one of the |certs_sources_| has been updated. Will notify the
245 // |observer_| that the certificate list has been refreshed.
246 void OnCertsSourceUpdated();
247
248 // Finds the |CertsSource| which provided |cert|. Can return nullptr (e.g. if
249 // the cert has been deleted in the meantime).
250 CertsSource* FindCertsSourceForCert(CERTCertificate* cert);
[email protected]3065a1f2014-01-22 08:56:35251
252 // Methods used during initialization, see the comment at the top of the .cc
253 // file for details.
254 static void DidGetCertDBOnUIThread(
Pavol Markob429f542018-08-23 06:08:19255 std::unique_ptr<Params> params,
256 CertificateManagerModel::Observer* observer,
Shelley Vohrb46035be2020-04-15 23:32:19257 CreationCallback callback,
[email protected]3065a1f2014-01-22 08:56:35258 net::NSSCertDatabase* cert_db,
[email protected]16dad0962014-03-18 01:29:11259 bool is_user_db_available,
Pavol Markob429f542018-08-23 06:08:19260 bool is_tpm_available);
[email protected]3065a1f2014-01-22 08:56:35261 static void DidGetCertDBOnIOThread(
Pavol Markob429f542018-08-23 06:08:19262 std::unique_ptr<Params> params,
[email protected]3065a1f2014-01-22 08:56:35263 CertificateManagerModel::Observer* observer,
Shelley Vohrb46035be2020-04-15 23:32:19264 CreationCallback callback,
[email protected]3065a1f2014-01-22 08:56:35265 net::NSSCertDatabase* cert_db);
Pavol Markob429f542018-08-23 06:08:19266 static void GetCertDBOnIOThread(std::unique_ptr<Params> params,
267 content::ResourceContext* resource_context,
268 CertificateManagerModel::Observer* observer,
Shelley Vohrb46035be2020-04-15 23:32:19269 CreationCallback callback);
isandrk20c70a22016-09-22 21:41:10270
[email protected]7fda9a402012-09-10 14:11:07271 net::NSSCertDatabase* cert_db_;
Pavol Markob429f542018-08-23 06:08:19272
273 // CertsSource instances providing certificates. The order matters - if a
274 // certificate is provided by more than one CertsSource, only the first one is
275 // accepted.
276 std::vector<std::unique_ptr<CertsSource>> certs_sources_;
277
278 bool hold_back_updates_ = false;
279
[email protected]16dad0962014-03-18 01:29:11280 // Whether the certificate database has a public slot associated with the
281 // profile. If not set, importing certificates is not allowed with this model.
282 bool is_user_db_available_;
[email protected]3065a1f2014-01-22 08:56:35283 bool is_tpm_available_;
[email protected]df8e899b2011-02-22 22:58:22284
285 // The observer to notify when certificate list is refreshed.
286 Observer* observer_;
287
288 DISALLOW_COPY_AND_ASSIGN(CertificateManagerModel);
289};
290
[email protected]4f242962011-05-13 22:25:22291#endif // CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_