[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" |
isandrk | 20c70a2 | 2016-09-22 21:41:10 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
[email protected] | e746341 | 2013-06-10 22:53:46 | [diff] [blame] | 16 | #include "base/strings/string16.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 17 | #include "net/cert/nss_cert_database.h" |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame^] | 18 | #include "net/cert/scoped_nss_types.h" |
mattm | bbf7fc0 | 2017-06-19 23:38:19 | [diff] [blame] | 19 | #include "net/ssl/client_cert_identity.h" |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 20 | |
isandrk | 20c70a2 | 2016-09-22 21:41:10 | [diff] [blame] | 21 | namespace chromeos { |
| 22 | class CertificateProvider; |
| 23 | } // namespace chromeos |
| 24 | |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 25 | namespace content { |
| 26 | class BrowserContext; |
| 27 | class ResourceContext; |
| 28 | } // namespace content |
| 29 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 30 | // CertificateManagerModel provides the data to be displayed in the certificate |
| 31 | // manager dialog, and processes changes from the view. |
| 32 | class CertificateManagerModel { |
| 33 | public: |
| 34 | // Map from the subject organization name to the list of certs from that |
| 35 | // organization. If a cert does not have an organization name, the |
| 36 | // subject's CertPrincipal::GetDisplayName() value is used instead. |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame^] | 37 | typedef std::map<std::string, net::ScopedCERTCertificateList> OrgGroupingMap; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 38 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 39 | typedef base::Callback<void(std::unique_ptr<CertificateManagerModel>)> |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 40 | CreationCallback; |
| 41 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 42 | // Enumeration of the possible columns in the certificate manager tree view. |
| 43 | enum Column { |
| 44 | COL_SUBJECT_NAME, |
| 45 | COL_CERTIFICATE_STORE, |
| 46 | COL_SERIAL_NUMBER, |
| 47 | COL_EXPIRES_ON, |
| 48 | }; |
| 49 | |
| 50 | class Observer { |
| 51 | public: |
| 52 | // Called to notify the view that the certificate list has been refreshed. |
| 53 | // TODO(mattm): do a more granular updating strategy? Maybe retrieve new |
| 54 | // list of certs, diff against past list, and then notify of the changes? |
| 55 | virtual void CertificatesRefreshed() = 0; |
| 56 | }; |
| 57 | |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 58 | // Creates a CertificateManagerModel. The model will be passed to the callback |
| 59 | // when it is ready. The caller must ensure the model does not outlive the |
| 60 | // |browser_context|. |
| 61 | static void Create(content::BrowserContext* browser_context, |
| 62 | Observer* observer, |
| 63 | const CreationCallback& callback); |
| 64 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 65 | ~CertificateManagerModel(); |
| 66 | |
[email protected] | 16dad096 | 2014-03-18 01:29:11 | [diff] [blame] | 67 | bool is_user_db_available() const { return is_user_db_available_; } |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 68 | bool is_tpm_available() const { return is_tpm_available_; } |
| 69 | |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 70 | // Accessor for read-only access to the underlying NSSCertDatabase. |
| 71 | const net::NSSCertDatabase* cert_db() const { return cert_db_; } |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 72 | |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 73 | // Trigger a refresh of the list of certs, unlock any slots if necessary. |
| 74 | // Following this call, the observer CertificatesRefreshed method will be |
| 75 | // called so the view can call FilterAndBuildOrgGroupingMap as necessary to |
| 76 | // refresh its tree views. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 77 | void Refresh(); |
| 78 | |
| 79 | // Fill |map| with the certificates matching |filter_type|. |
| 80 | void FilterAndBuildOrgGroupingMap(net::CertType filter_type, |
| 81 | OrgGroupingMap* map) const; |
| 82 | |
| 83 | // Get the data to be displayed in |column| for the given |cert|. |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame^] | 84 | base::string16 GetColumnText(CERTCertificate* cert, Column column) const; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 85 | |
[email protected] | 6a18d07 | 2011-06-29 00:25:40 | [diff] [blame] | 86 | // Import private keys and certificates from PKCS #12 encoded |
| 87 | // |data|, using the given |password|. If |is_extractable| is false, |
tfarina | f58077a | 2017-01-13 11:40:05 | [diff] [blame] | 88 | // mark the private key as unextractable from the slot. |
[email protected] | 6a18d07 | 2011-06-29 00:25:40 | [diff] [blame] | 89 | // Returns a net error code on failure. |
tfarina | f58077a | 2017-01-13 11:40:05 | [diff] [blame] | 90 | int ImportFromPKCS12(PK11SlotInfo* slot_info, const std::string& data, |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 91 | const base::string16& password, bool is_extractable); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 92 | |
svaldez | 3e98a71 | 2015-11-23 16:21:57 | [diff] [blame] | 93 | // Import user certificate from DER encoded |data|. |
| 94 | // Returns a net error code on failure. |
| 95 | int ImportUserCert(const std::string& data); |
| 96 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 97 | // Import CA certificates. |
| 98 | // Tries to import all the certificates given. The root will be trusted |
| 99 | // according to |trust_bits|. Any certificates that could not be imported |
| 100 | // will be listed in |not_imported|. |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 101 | // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 102 | // Returns false if there is an internal error, otherwise true is returned and |
| 103 | // |not_imported| should be checked for any certificates that were not |
| 104 | // imported. |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame^] | 105 | bool ImportCACerts(const net::ScopedCERTCertificateList& certificates, |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 106 | net::NSSCertDatabase::TrustBits trust_bits, |
| 107 | net::NSSCertDatabase::ImportCertFailureList* not_imported); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 108 | |
| 109 | // Import server certificate. The first cert should be the server cert. Any |
| 110 | // additional certs should be intermediate/CA certs and will be imported but |
| 111 | // not given any trust. |
| 112 | // Any certificates that could not be imported will be listed in |
| 113 | // |not_imported|. |
[email protected] | ad40b21 | 2012-06-01 05:59:56 | [diff] [blame] | 114 | // |trust_bits| can be set to explicitly trust or distrust the certificate, or |
| 115 | // use TRUST_DEFAULT to inherit trust as normal. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 116 | // Returns false if there is an internal error, otherwise true is returned and |
| 117 | // |not_imported| should be checked for any certificates that were not |
| 118 | // imported. |
| 119 | bool ImportServerCert( |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame^] | 120 | const net::ScopedCERTCertificateList& certificates, |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 121 | net::NSSCertDatabase::TrustBits trust_bits, |
| 122 | net::NSSCertDatabase::ImportCertFailureList* not_imported); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 123 | |
| 124 | // Set trust values for certificate. |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 125 | // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 126 | // Returns true on success or false on failure. |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame^] | 127 | bool SetCertTrust(CERTCertificate* cert, |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 128 | net::CertType type, |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 129 | net::NSSCertDatabase::TrustBits trust_bits); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 130 | |
| 131 | // Delete the cert. Returns true on success. |cert| is still valid when this |
| 132 | // function returns. |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame^] | 133 | bool Delete(CERTCertificate* cert); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 134 | |
[email protected] | e0ad089 | 2012-05-22 19:16:59 | [diff] [blame] | 135 | // IsHardwareBacked returns true if |cert| is hardware backed. |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame^] | 136 | bool IsHardwareBacked(CERTCertificate* cert) const; |
[email protected] | e0ad089 | 2012-05-22 19:16:59 | [diff] [blame] | 137 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 138 | private: |
isandrk | 20c70a2 | 2016-09-22 21:41:10 | [diff] [blame] | 139 | CertificateManagerModel( |
| 140 | net::NSSCertDatabase* nss_cert_database, |
| 141 | bool is_user_db_available, |
| 142 | bool is_tpm_available, |
| 143 | Observer* observer, |
| 144 | std::unique_ptr<chromeos::CertificateProvider> |
| 145 | extension_certificate_provider); |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 146 | |
| 147 | // Methods used during initialization, see the comment at the top of the .cc |
| 148 | // file for details. |
| 149 | static void DidGetCertDBOnUIThread( |
| 150 | net::NSSCertDatabase* cert_db, |
[email protected] | 16dad096 | 2014-03-18 01:29:11 | [diff] [blame] | 151 | bool is_user_db_available, |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 152 | bool is_tpm_available, |
| 153 | CertificateManagerModel::Observer* observer, |
isandrk | 20c70a2 | 2016-09-22 21:41:10 | [diff] [blame] | 154 | std::unique_ptr<chromeos::CertificateProvider> |
| 155 | extension_certificate_provider, |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 156 | const CreationCallback& callback); |
| 157 | static void DidGetCertDBOnIOThread( |
| 158 | CertificateManagerModel::Observer* observer, |
isandrk | 20c70a2 | 2016-09-22 21:41:10 | [diff] [blame] | 159 | std::unique_ptr<chromeos::CertificateProvider> |
| 160 | extension_certificate_provider, |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 161 | const CreationCallback& callback, |
| 162 | net::NSSCertDatabase* cert_db); |
isandrk | 20c70a2 | 2016-09-22 21:41:10 | [diff] [blame] | 163 | static void GetCertDBOnIOThread( |
| 164 | content::ResourceContext* context, |
| 165 | CertificateManagerModel::Observer* observer, |
| 166 | std::unique_ptr<chromeos::CertificateProvider> |
| 167 | extension_certificate_provider, |
| 168 | const CreationCallback& callback); |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 169 | |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 170 | // Callback used by Refresh() for when the cert slots have been unlocked. |
| 171 | // This method does the actual refreshing. |
| 172 | void RefreshSlotsUnlocked(); |
| 173 | |
isandrk | 20c70a2 | 2016-09-22 21:41:10 | [diff] [blame] | 174 | // Callback used to refresh extension provided certificates. Refreshes UI. |
mattm | bbf7fc0 | 2017-06-19 23:38:19 | [diff] [blame] | 175 | void RefreshExtensionCertificates( |
| 176 | net::ClientCertIdentityList new_cert_identities); |
isandrk | 20c70a2 | 2016-09-22 21:41:10 | [diff] [blame] | 177 | |
[email protected] | 7fda9a40 | 2012-09-10 14:11:07 | [diff] [blame] | 178 | net::NSSCertDatabase* cert_db_; |
Matt Mueller | 917b4e1 | 2017-09-01 19:15:35 | [diff] [blame^] | 179 | net::ScopedCERTCertificateList cert_list_; |
| 180 | net::ScopedCERTCertificateList extension_cert_list_; |
[email protected] | 16dad096 | 2014-03-18 01:29:11 | [diff] [blame] | 181 | // Whether the certificate database has a public slot associated with the |
| 182 | // profile. If not set, importing certificates is not allowed with this model. |
| 183 | bool is_user_db_available_; |
[email protected] | 3065a1f | 2014-01-22 08:56:35 | [diff] [blame] | 184 | bool is_tpm_available_; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 185 | |
| 186 | // The observer to notify when certificate list is refreshed. |
| 187 | Observer* observer_; |
| 188 | |
isandrk | 20c70a2 | 2016-09-22 21:41:10 | [diff] [blame] | 189 | // Certificate provider used to fetch extension provided certificates. |
| 190 | std::unique_ptr<chromeos::CertificateProvider> |
| 191 | extension_certificate_provider_; |
| 192 | |
| 193 | base::WeakPtrFactory<CertificateManagerModel> weak_ptr_factory_; |
| 194 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 195 | DISALLOW_COPY_AND_ASSIGN(CertificateManagerModel); |
| 196 | }; |
| 197 | |
[email protected] | 4f24296 | 2011-05-13 22:25:22 | [diff] [blame] | 198 | #endif // CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_ |