blob: 81e65e48cf889b40e069157737e5d6df24a7e853 [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"
isandrk20c70a22016-09-22 21:41:1015#include "base/memory/weak_ptr.h"
[email protected]e7463412013-06-10 22:53:4616#include "base/strings/string16.h"
[email protected]6e7845ae2013-03-29 21:48:1117#include "net/cert/nss_cert_database.h"
[email protected]df8e899b2011-02-22 22:58:2218
isandrk20c70a22016-09-22 21:41:1019namespace chromeos {
20class CertificateProvider;
21} // namespace chromeos
22
[email protected]3065a1f2014-01-22 08:56:3523namespace content {
24class BrowserContext;
25class ResourceContext;
26} // namespace content
27
[email protected]df8e899b2011-02-22 22:58:2228// CertificateManagerModel provides the data to be displayed in the certificate
29// manager dialog, and processes changes from the view.
30class CertificateManagerModel {
31 public:
32 // Map from the subject organization name to the list of certs from that
33 // organization. If a cert does not have an organization name, the
34 // subject's CertPrincipal::GetDisplayName() value is used instead.
35 typedef std::map<std::string, net::CertificateList> OrgGroupingMap;
36
dcheng4af48582016-04-19 00:29:3537 typedef base::Callback<void(std::unique_ptr<CertificateManagerModel>)>
[email protected]3065a1f2014-01-22 08:56:3538 CreationCallback;
39
[email protected]df8e899b2011-02-22 22:58:2240 // Enumeration of the possible columns in the certificate manager tree view.
41 enum Column {
42 COL_SUBJECT_NAME,
43 COL_CERTIFICATE_STORE,
44 COL_SERIAL_NUMBER,
45 COL_EXPIRES_ON,
46 };
47
48 class Observer {
49 public:
50 // Called to notify the view that the certificate list has been refreshed.
51 // TODO(mattm): do a more granular updating strategy? Maybe retrieve new
52 // list of certs, diff against past list, and then notify of the changes?
53 virtual void CertificatesRefreshed() = 0;
54 };
55
[email protected]3065a1f2014-01-22 08:56:3556 // Creates a CertificateManagerModel. The model will be passed to the callback
57 // when it is ready. The caller must ensure the model does not outlive the
58 // |browser_context|.
59 static void Create(content::BrowserContext* browser_context,
60 Observer* observer,
61 const CreationCallback& callback);
62
[email protected]df8e899b2011-02-22 22:58:2263 ~CertificateManagerModel();
64
[email protected]16dad0962014-03-18 01:29:1165 bool is_user_db_available() const { return is_user_db_available_; }
[email protected]3065a1f2014-01-22 08:56:3566 bool is_tpm_available() const { return is_tpm_available_; }
67
[email protected]7fda9a402012-09-10 14:11:0768 // Accessor for read-only access to the underlying NSSCertDatabase.
69 const net::NSSCertDatabase* cert_db() const { return cert_db_; }
[email protected]df8e899b2011-02-22 22:58:2270
[email protected]4c4f7cd2011-03-05 02:20:4471 // Trigger a refresh of the list of certs, unlock any slots if necessary.
72 // Following this call, the observer CertificatesRefreshed method will be
73 // called so the view can call FilterAndBuildOrgGroupingMap as necessary to
74 // refresh its tree views.
[email protected]df8e899b2011-02-22 22:58:2275 void Refresh();
76
77 // Fill |map| with the certificates matching |filter_type|.
78 void FilterAndBuildOrgGroupingMap(net::CertType filter_type,
79 OrgGroupingMap* map) const;
80
81 // Get the data to be displayed in |column| for the given |cert|.
[email protected]96920152013-12-04 21:00:1682 base::string16 GetColumnText(const net::X509Certificate& cert, Column column) const;
[email protected]df8e899b2011-02-22 22:58:2283
[email protected]6a18d072011-06-29 00:25:4084 // Import private keys and certificates from PKCS #12 encoded
85 // |data|, using the given |password|. If |is_extractable| is false,
86 // mark the private key as unextractable from the module.
87 // Returns a net error code on failure.
[email protected]df8e899b2011-02-22 22:58:2288 int ImportFromPKCS12(net::CryptoModule* module, const std::string& data,
[email protected]96920152013-12-04 21:00:1689 const base::string16& password, bool is_extractable);
[email protected]df8e899b2011-02-22 22:58:2290
svaldez3e98a712015-11-23 16:21:5791 // Import user certificate from DER encoded |data|.
92 // Returns a net error code on failure.
93 int ImportUserCert(const std::string& data);
94
[email protected]df8e899b2011-02-22 22:58:2295 // Import CA certificates.
96 // Tries to import all the certificates given. The root will be trusted
97 // according to |trust_bits|. Any certificates that could not be imported
98 // will be listed in |not_imported|.
[email protected]7fda9a402012-09-10 14:11:0799 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:22100 // Returns false if there is an internal error, otherwise true is returned and
101 // |not_imported| should be checked for any certificates that were not
102 // imported.
103 bool ImportCACerts(const net::CertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07104 net::NSSCertDatabase::TrustBits trust_bits,
105 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22106
107 // Import server certificate. The first cert should be the server cert. Any
108 // additional certs should be intermediate/CA certs and will be imported but
109 // not given any trust.
110 // Any certificates that could not be imported will be listed in
111 // |not_imported|.
[email protected]ad40b212012-06-01 05:59:56112 // |trust_bits| can be set to explicitly trust or distrust the certificate, or
113 // use TRUST_DEFAULT to inherit trust as normal.
[email protected]df8e899b2011-02-22 22:58:22114 // Returns false if there is an internal error, otherwise true is returned and
115 // |not_imported| should be checked for any certificates that were not
116 // imported.
117 bool ImportServerCert(
118 const net::CertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07119 net::NSSCertDatabase::TrustBits trust_bits,
120 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22121
122 // Set trust values for certificate.
[email protected]7fda9a402012-09-10 14:11:07123 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:22124 // Returns true on success or false on failure.
125 bool SetCertTrust(const net::X509Certificate* cert,
126 net::CertType type,
[email protected]7fda9a402012-09-10 14:11:07127 net::NSSCertDatabase::TrustBits trust_bits);
[email protected]df8e899b2011-02-22 22:58:22128
129 // Delete the cert. Returns true on success. |cert| is still valid when this
130 // function returns.
131 bool Delete(net::X509Certificate* cert);
132
[email protected]e0ad0892012-05-22 19:16:59133 // IsHardwareBacked returns true if |cert| is hardware backed.
134 bool IsHardwareBacked(const net::X509Certificate* cert) const;
135
[email protected]df8e899b2011-02-22 22:58:22136 private:
isandrk20c70a22016-09-22 21:41:10137 CertificateManagerModel(
138 net::NSSCertDatabase* nss_cert_database,
139 bool is_user_db_available,
140 bool is_tpm_available,
141 Observer* observer,
142 std::unique_ptr<chromeos::CertificateProvider>
143 extension_certificate_provider);
[email protected]3065a1f2014-01-22 08:56:35144
145 // Methods used during initialization, see the comment at the top of the .cc
146 // file for details.
147 static void DidGetCertDBOnUIThread(
148 net::NSSCertDatabase* cert_db,
[email protected]16dad0962014-03-18 01:29:11149 bool is_user_db_available,
[email protected]3065a1f2014-01-22 08:56:35150 bool is_tpm_available,
151 CertificateManagerModel::Observer* observer,
isandrk20c70a22016-09-22 21:41:10152 std::unique_ptr<chromeos::CertificateProvider>
153 extension_certificate_provider,
[email protected]3065a1f2014-01-22 08:56:35154 const CreationCallback& callback);
155 static void DidGetCertDBOnIOThread(
156 CertificateManagerModel::Observer* observer,
isandrk20c70a22016-09-22 21:41:10157 std::unique_ptr<chromeos::CertificateProvider>
158 extension_certificate_provider,
[email protected]3065a1f2014-01-22 08:56:35159 const CreationCallback& callback,
160 net::NSSCertDatabase* cert_db);
isandrk20c70a22016-09-22 21:41:10161 static void GetCertDBOnIOThread(
162 content::ResourceContext* context,
163 CertificateManagerModel::Observer* observer,
164 std::unique_ptr<chromeos::CertificateProvider>
165 extension_certificate_provider,
166 const CreationCallback& callback);
[email protected]3065a1f2014-01-22 08:56:35167
[email protected]4c4f7cd2011-03-05 02:20:44168 // Callback used by Refresh() for when the cert slots have been unlocked.
169 // This method does the actual refreshing.
170 void RefreshSlotsUnlocked();
171
isandrk20c70a22016-09-22 21:41:10172 // Callback used to refresh extension provided certificates. Refreshes UI.
173 void RefreshExtensionCertificates(const net::CertificateList& new_certs);
174
[email protected]7fda9a402012-09-10 14:11:07175 net::NSSCertDatabase* cert_db_;
[email protected]df8e899b2011-02-22 22:58:22176 net::CertificateList cert_list_;
isandrk20c70a22016-09-22 21:41:10177 net::CertificateList extension_cert_list_;
[email protected]16dad0962014-03-18 01:29:11178 // Whether the certificate database has a public slot associated with the
179 // profile. If not set, importing certificates is not allowed with this model.
180 bool is_user_db_available_;
[email protected]3065a1f2014-01-22 08:56:35181 bool is_tpm_available_;
[email protected]df8e899b2011-02-22 22:58:22182
183 // The observer to notify when certificate list is refreshed.
184 Observer* observer_;
185
isandrk20c70a22016-09-22 21:41:10186 // Certificate provider used to fetch extension provided certificates.
187 std::unique_ptr<chromeos::CertificateProvider>
188 extension_certificate_provider_;
189
190 base::WeakPtrFactory<CertificateManagerModel> weak_ptr_factory_;
191
[email protected]df8e899b2011-02-22 22:58:22192 DISALLOW_COPY_AND_ASSIGN(CertificateManagerModel);
193};
194
[email protected]4f242962011-05-13 22:25:22195#endif // CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_