blob: 10c216bf2349bb6db5df0000f8456af1b39f49a3 [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"
Matt Mueller917b4e12017-09-01 19:15:3518#include "net/cert/scoped_nss_types.h"
mattmbbf7fc02017-06-19 23:38:1919#include "net/ssl/client_cert_identity.h"
[email protected]df8e899b2011-02-22 22:58:2220
isandrk20c70a22016-09-22 21:41:1021namespace chromeos {
22class CertificateProvider;
23} // namespace chromeos
24
[email protected]3065a1f2014-01-22 08:56:3525namespace content {
26class BrowserContext;
27class ResourceContext;
28} // namespace content
29
[email protected]df8e899b2011-02-22 22:58:2230// CertificateManagerModel provides the data to be displayed in the certificate
31// manager dialog, and processes changes from the view.
32class 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 Mueller917b4e12017-09-01 19:15:3537 typedef std::map<std::string, net::ScopedCERTCertificateList> OrgGroupingMap;
[email protected]df8e899b2011-02-22 22:58:2238
dcheng4af48582016-04-19 00:29:3539 typedef base::Callback<void(std::unique_ptr<CertificateManagerModel>)>
[email protected]3065a1f2014-01-22 08:56:3540 CreationCallback;
41
[email protected]df8e899b2011-02-22 22:58:2242 // 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]3065a1f2014-01-22 08:56:3558 // 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]df8e899b2011-02-22 22:58:2265 ~CertificateManagerModel();
66
[email protected]16dad0962014-03-18 01:29:1167 bool is_user_db_available() const { return is_user_db_available_; }
[email protected]3065a1f2014-01-22 08:56:3568 bool is_tpm_available() const { return is_tpm_available_; }
69
[email protected]7fda9a402012-09-10 14:11:0770 // Accessor for read-only access to the underlying NSSCertDatabase.
71 const net::NSSCertDatabase* cert_db() const { return cert_db_; }
[email protected]df8e899b2011-02-22 22:58:2272
[email protected]4c4f7cd2011-03-05 02:20:4473 // 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]df8e899b2011-02-22 22:58:2277 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 Mueller917b4e12017-09-01 19:15:3584 base::string16 GetColumnText(CERTCertificate* cert, Column column) const;
[email protected]df8e899b2011-02-22 22:58:2285
[email protected]6a18d072011-06-29 00:25:4086 // Import private keys and certificates from PKCS #12 encoded
87 // |data|, using the given |password|. If |is_extractable| is false,
tfarinaf58077a2017-01-13 11:40:0588 // mark the private key as unextractable from the slot.
[email protected]6a18d072011-06-29 00:25:4089 // Returns a net error code on failure.
tfarinaf58077a2017-01-13 11:40:0590 int ImportFromPKCS12(PK11SlotInfo* slot_info, const std::string& data,
[email protected]96920152013-12-04 21:00:1691 const base::string16& password, bool is_extractable);
[email protected]df8e899b2011-02-22 22:58:2292
svaldez3e98a712015-11-23 16:21:5793 // 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]df8e899b2011-02-22 22:58:2297 // 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]7fda9a402012-09-10 14:11:07101 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:22102 // 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 Mueller917b4e12017-09-01 19:15:35105 bool ImportCACerts(const net::ScopedCERTCertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07106 net::NSSCertDatabase::TrustBits trust_bits,
107 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22108
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]ad40b212012-06-01 05:59:56114 // |trust_bits| can be set to explicitly trust or distrust the certificate, or
115 // use TRUST_DEFAULT to inherit trust as normal.
[email protected]df8e899b2011-02-22 22:58:22116 // 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 Mueller917b4e12017-09-01 19:15:35120 const net::ScopedCERTCertificateList& certificates,
[email protected]7fda9a402012-09-10 14:11:07121 net::NSSCertDatabase::TrustBits trust_bits,
122 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22123
124 // Set trust values for certificate.
[email protected]7fda9a402012-09-10 14:11:07125 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:22126 // Returns true on success or false on failure.
Matt Mueller917b4e12017-09-01 19:15:35127 bool SetCertTrust(CERTCertificate* cert,
[email protected]df8e899b2011-02-22 22:58:22128 net::CertType type,
[email protected]7fda9a402012-09-10 14:11:07129 net::NSSCertDatabase::TrustBits trust_bits);
[email protected]df8e899b2011-02-22 22:58:22130
131 // Delete the cert. Returns true on success. |cert| is still valid when this
132 // function returns.
Matt Mueller917b4e12017-09-01 19:15:35133 bool Delete(CERTCertificate* cert);
[email protected]df8e899b2011-02-22 22:58:22134
[email protected]e0ad0892012-05-22 19:16:59135 // IsHardwareBacked returns true if |cert| is hardware backed.
Matt Mueller917b4e12017-09-01 19:15:35136 bool IsHardwareBacked(CERTCertificate* cert) const;
[email protected]e0ad0892012-05-22 19:16:59137
[email protected]df8e899b2011-02-22 22:58:22138 private:
isandrk20c70a22016-09-22 21:41:10139 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]3065a1f2014-01-22 08:56:35146
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]16dad0962014-03-18 01:29:11151 bool is_user_db_available,
[email protected]3065a1f2014-01-22 08:56:35152 bool is_tpm_available,
153 CertificateManagerModel::Observer* observer,
isandrk20c70a22016-09-22 21:41:10154 std::unique_ptr<chromeos::CertificateProvider>
155 extension_certificate_provider,
[email protected]3065a1f2014-01-22 08:56:35156 const CreationCallback& callback);
157 static void DidGetCertDBOnIOThread(
158 CertificateManagerModel::Observer* observer,
isandrk20c70a22016-09-22 21:41:10159 std::unique_ptr<chromeos::CertificateProvider>
160 extension_certificate_provider,
[email protected]3065a1f2014-01-22 08:56:35161 const CreationCallback& callback,
162 net::NSSCertDatabase* cert_db);
isandrk20c70a22016-09-22 21:41:10163 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]3065a1f2014-01-22 08:56:35169
[email protected]4c4f7cd2011-03-05 02:20:44170 // Callback used by Refresh() for when the cert slots have been unlocked.
171 // This method does the actual refreshing.
172 void RefreshSlotsUnlocked();
173
isandrk20c70a22016-09-22 21:41:10174 // Callback used to refresh extension provided certificates. Refreshes UI.
mattmbbf7fc02017-06-19 23:38:19175 void RefreshExtensionCertificates(
176 net::ClientCertIdentityList new_cert_identities);
isandrk20c70a22016-09-22 21:41:10177
[email protected]7fda9a402012-09-10 14:11:07178 net::NSSCertDatabase* cert_db_;
Matt Mueller917b4e12017-09-01 19:15:35179 net::ScopedCERTCertificateList cert_list_;
180 net::ScopedCERTCertificateList extension_cert_list_;
[email protected]16dad0962014-03-18 01:29:11181 // 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]3065a1f2014-01-22 08:56:35184 bool is_tpm_available_;
[email protected]df8e899b2011-02-22 22:58:22185
186 // The observer to notify when certificate list is refreshed.
187 Observer* observer_;
188
isandrk20c70a22016-09-22 21:41:10189 // 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]df8e899b2011-02-22 22:58:22195 DISALLOW_COPY_AND_ASSIGN(CertificateManagerModel);
196};
197
[email protected]4f242962011-05-13 22:25:22198#endif // CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_