[email protected] | e0ad089 | 2012-05-22 19:16:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4f24296 | 2011-05-13 22:25:22 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_ |
| 6 | #define CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_ |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 7 | |
| 8 | #include <map> |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 9 | #include <memory> |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 10 | #include <string> |
| 11 | |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 12 | #include "base/callback.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | e746341 | 2013-06-10 22:53:46 | [diff] [blame] | 15 | #include "base/strings/string16.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 16 | #include "net/cert/nss_cert_database.h" |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame] | 17 | #include "net/cert/scoped_nss_types.h" |
mattm | bbf7fc0 | 2017-06-19 23:38:19 | [diff] [blame] | 18 | #include "net/ssl/client_cert_identity.h" |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 19 | |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 20 | namespace content { |
| 21 | class BrowserContext; |
| 22 | class ResourceContext; |
| 23 | } // namespace content |
| 24 | |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 25 | #if defined(OS_CHROMEOS) |
| 26 | namespace chromeos { |
| 27 | class CertificateProvider; |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 28 | class PolicyCertificateProvider; |
| 29 | } |
| 30 | #endif |
| 31 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 32 | // CertificateManagerModel provides the data to be displayed in the certificate |
| 33 | // manager dialog, and processes changes from the view. |
| 34 | class CertificateManagerModel { |
| 35 | public: |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 36 | // 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 Costinas | a45a605 | 2019-07-22 09:42:30 | [diff] [blame] | 51 | bool can_be_deleted, |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 52 | bool untrusted, |
| 53 | Source source, |
| 54 | bool web_trust_anchor, |
Andreea Costinas | eaec4c17 | 2019-02-04 11:57:43 | [diff] [blame] | 55 | bool hardware_backed, |
| 56 | bool device_wide); |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 57 | ~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 Costinas | a45a605 | 2019-07-22 09:42:30 | [diff] [blame] | 62 | bool can_be_deleted() const { return can_be_deleted_; } |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 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 Costinas | eaec4c17 | 2019-02-04 11:57:43 | [diff] [blame] | 67 | bool device_wide() const { return device_wide_; } |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 68 | |
| 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 Costinas | a45a605 | 2019-07-22 09:42:30 | [diff] [blame] | 83 | // 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 Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 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 Costinas | eaec4c17 | 2019-02-04 11:57:43 | [diff] [blame] | 101 | // true if the certificate is device-wide. |
| 102 | // Note: can be true only on Chrome OS. |
| 103 | bool device_wide_; |
| 104 | |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 105 | DISALLOW_COPY_AND_ASSIGN(CertInfo); |
Michael Ershov | 3466857 | 2019-07-31 10:01:12 | [diff] [blame] | 106 | |
| 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 Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | class CertsSource; |
| 121 | |
| 122 | // Holds parameters during construction. |
| 123 | struct Params { |
| 124 | #if defined(OS_CHROMEOS) |
| 125 | // May be nullptr. |
Pavol Marko | 3bab3afd | 2018-09-24 14:42:17 | [diff] [blame] | 126 | chromeos::PolicyCertificateProvider* policy_certs_provider = nullptr; |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 127 | // 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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 140 | // 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 Vohr | b46035be | 2020-04-15 23:32:19 | [diff] [blame] | 143 | using OrgGroupingMap = |
| 144 | std::map<std::string, std::vector<std::unique_ptr<CertInfo>>>; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 145 | |
Shelley Vohr | b46035be | 2020-04-15 23:32:19 | [diff] [blame] | 146 | using CreationCallback = |
| 147 | base::OnceCallback<void(std::unique_ptr<CertificateManagerModel>)>; |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 148 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 149 | 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 Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 155 | |
| 156 | protected: |
| 157 | virtual ~Observer() = default; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 158 | }; |
| 159 | |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 160 | // 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 Vohr | b46035be | 2020-04-15 23:32:19 | [diff] [blame] | 165 | CreationCallback callback); |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 166 | |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 167 | // 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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 174 | ~CertificateManagerModel(); |
| 175 | |
[email protected] | 16dad096 | 2014-03-18 01:29:11 | [diff] [blame] | 176 | bool is_user_db_available() const { return is_user_db_available_; } |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 177 | bool is_tpm_available() const { return is_tpm_available_; } |
| 178 | |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 179 | // Accessor for read-only access to the underlying NSSCertDatabase. |
| 180 | const net::NSSCertDatabase* cert_db() const { return cert_db_; } |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 181 | |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 182 | // 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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 186 | void Refresh(); |
| 187 | |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 188 | // Fill |*out_org_grouping_map| with the certificates matching |filter_type|. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 189 | void FilterAndBuildOrgGroupingMap(net::CertType filter_type, |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 190 | OrgGroupingMap* out_org_grouping_map) const; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 191 | |
[email protected] | 6a18d07 | 2011-06-29 00:25:40 | [diff] [blame] | 192 | // Import private keys and certificates from PKCS #12 encoded |
| 193 | // |data|, using the given |password|. If |is_extractable| is false, |
tfarina | f58077a | 2017-01-13 11:40:05 | [diff] [blame] | 194 | // mark the private key as unextractable from the slot. |
[email protected] | 6a18d07 | 2011-06-29 00:25:40 | [diff] [blame] | 195 | // Returns a net error code on failure. |
Shelley Vohr | b46035be | 2020-04-15 23:32:19 | [diff] [blame] | 196 | int ImportFromPKCS12(PK11SlotInfo* slot_info, |
| 197 | const std::string& data, |
| 198 | const base::string16& password, |
| 199 | bool is_extractable); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 200 | |
svaldez | 3e98a71 | 2015-11-23 16:21:57 | [diff] [blame] | 201 | // 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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 205 | // 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] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 209 | // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 210 | // 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 Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame] | 213 | bool ImportCACerts(const net::ScopedCERTCertificateList& certificates, |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 214 | net::NSSCertDatabase::TrustBits trust_bits, |
| 215 | net::NSSCertDatabase::ImportCertFailureList* not_imported); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 216 | |
| 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] | ad40b21 | 2012-06-01 05:59:56 | [diff] [blame] | 222 | // |trust_bits| can be set to explicitly trust or distrust the certificate, or |
| 223 | // use TRUST_DEFAULT to inherit trust as normal. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 224 | // 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 Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame] | 228 | const net::ScopedCERTCertificateList& certificates, |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 229 | net::NSSCertDatabase::TrustBits trust_bits, |
| 230 | net::NSSCertDatabase::ImportCertFailureList* not_imported); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 231 | |
| 232 | // Set trust values for certificate. |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 233 | // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 234 | // Returns true on success or false on failure. |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame] | 235 | bool SetCertTrust(CERTCertificate* cert, |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 236 | net::CertType type, |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 237 | net::NSSCertDatabase::TrustBits trust_bits); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 238 | |
| 239 | // Delete the cert. Returns true on success. |cert| is still valid when this |
| 240 | // function returns. |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame] | 241 | bool Delete(CERTCertificate* cert); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 242 | |
| 243 | private: |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 244 | // 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] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 251 | |
| 252 | // Methods used during initialization, see the comment at the top of the .cc |
| 253 | // file for details. |
| 254 | static void DidGetCertDBOnUIThread( |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 255 | std::unique_ptr<Params> params, |
| 256 | CertificateManagerModel::Observer* observer, |
Shelley Vohr | b46035be | 2020-04-15 23:32:19 | [diff] [blame] | 257 | CreationCallback callback, |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 258 | net::NSSCertDatabase* cert_db, |
[email protected] | 16dad096 | 2014-03-18 01:29:11 | [diff] [blame] | 259 | bool is_user_db_available, |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 260 | bool is_tpm_available); |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 261 | static void DidGetCertDBOnIOThread( |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 262 | std::unique_ptr<Params> params, |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 263 | CertificateManagerModel::Observer* observer, |
Shelley Vohr | b46035be | 2020-04-15 23:32:19 | [diff] [blame] | 264 | CreationCallback callback, |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 265 | net::NSSCertDatabase* cert_db); |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 266 | static void GetCertDBOnIOThread(std::unique_ptr<Params> params, |
| 267 | content::ResourceContext* resource_context, |
| 268 | CertificateManagerModel::Observer* observer, |
Shelley Vohr | b46035be | 2020-04-15 23:32:19 | [diff] [blame] | 269 | CreationCallback callback); |
isandrk | 20c70a2 | 2016-09-22 21:41:10 | [diff] [blame] | 270 | |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 271 | net::NSSCertDatabase* cert_db_; |
Pavol Marko | b429f54 | 2018-08-23 06:08:19 | [diff] [blame] | 272 | |
| 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] | 16dad096 | 2014-03-18 01:29:11 | [diff] [blame] | 280 | // 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] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 283 | bool is_tpm_available_; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 284 | |
| 285 | // The observer to notify when certificate list is refreshed. |
| 286 | Observer* observer_; |
| 287 | |
| 288 | DISALLOW_COPY_AND_ASSIGN(CertificateManagerModel); |
| 289 | }; |
| 290 | |
[email protected] | 4f24296 | 2011-05-13 22:25:22 | [diff] [blame] | 291 | #endif // CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_ |