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