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