blob: 64fcb2c467339cbe403850183e33d0ba1d7babbe [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"
[email protected]e7463412013-06-10 22:53:4615#include "base/strings/string16.h"
[email protected]6e7845ae2013-03-29 21:48:1116#include "net/cert/nss_cert_database.h"
[email protected]df8e899b2011-02-22 22:58:2217
[email protected]3065a1f2014-01-22 08:56:3518namespace content {
19class BrowserContext;
20class ResourceContext;
21} // namespace content
22
[email protected]df8e899b2011-02-22 22:58:2223// CertificateManagerModel provides the data to be displayed in the certificate
24// manager dialog, and processes changes from the view.
25class 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
dcheng4af48582016-04-19 00:29:3532 typedef base::Callback<void(std::unique_ptr<CertificateManagerModel>)>
[email protected]3065a1f2014-01-22 08:56:3533 CreationCallback;
34
[email protected]df8e899b2011-02-22 22:58:2235 // 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]3065a1f2014-01-22 08:56:3551 // 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]df8e899b2011-02-22 22:58:2258 ~CertificateManagerModel();
59
[email protected]16dad0962014-03-18 01:29:1160 bool is_user_db_available() const { return is_user_db_available_; }
[email protected]3065a1f2014-01-22 08:56:3561 bool is_tpm_available() const { return is_tpm_available_; }
62
[email protected]7fda9a402012-09-10 14:11:0763 // Accessor for read-only access to the underlying NSSCertDatabase.
64 const net::NSSCertDatabase* cert_db() const { return cert_db_; }
[email protected]df8e899b2011-02-22 22:58:2265
[email protected]4c4f7cd2011-03-05 02:20:4466 // 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]df8e899b2011-02-22 22:58:2270 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]96920152013-12-04 21:00:1677 base::string16 GetColumnText(const net::X509Certificate& cert, Column column) const;
[email protected]df8e899b2011-02-22 22:58:2278
[email protected]6a18d072011-06-29 00:25:4079 // 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]df8e899b2011-02-22 22:58:2283 int ImportFromPKCS12(net::CryptoModule* module, const std::string& data,
[email protected]96920152013-12-04 21:00:1684 const base::string16& password, bool is_extractable);
[email protected]df8e899b2011-02-22 22:58:2285
svaldez3e98a712015-11-23 16:21:5786 // 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]df8e899b2011-02-22 22:58:2290 // 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]7fda9a402012-09-10 14:11:0794 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:2295 // 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]7fda9a402012-09-10 14:11:0799 net::NSSCertDatabase::TrustBits trust_bits,
100 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22101
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]ad40b212012-06-01 05:59:56107 // |trust_bits| can be set to explicitly trust or distrust the certificate, or
108 // use TRUST_DEFAULT to inherit trust as normal.
[email protected]df8e899b2011-02-22 22:58:22109 // 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]7fda9a402012-09-10 14:11:07114 net::NSSCertDatabase::TrustBits trust_bits,
115 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22116
117 // Set trust values for certificate.
[email protected]7fda9a402012-09-10 14:11:07118 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:22119 // Returns true on success or false on failure.
120 bool SetCertTrust(const net::X509Certificate* cert,
121 net::CertType type,
[email protected]7fda9a402012-09-10 14:11:07122 net::NSSCertDatabase::TrustBits trust_bits);
[email protected]df8e899b2011-02-22 22:58:22123
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]e0ad0892012-05-22 19:16:59128 // IsHardwareBacked returns true if |cert| is hardware backed.
129 bool IsHardwareBacked(const net::X509Certificate* cert) const;
130
[email protected]df8e899b2011-02-22 22:58:22131 private:
[email protected]3065a1f2014-01-22 08:56:35132 CertificateManagerModel(net::NSSCertDatabase* nss_cert_database,
[email protected]16dad0962014-03-18 01:29:11133 bool is_user_db_available,
[email protected]3065a1f2014-01-22 08:56:35134 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]16dad0962014-03-18 01:29:11141 bool is_user_db_available,
[email protected]3065a1f2014-01-22 08:56:35142 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]4c4f7cd2011-03-05 02:20:44153 // 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]7fda9a402012-09-10 14:11:07157 net::NSSCertDatabase* cert_db_;
[email protected]df8e899b2011-02-22 22:58:22158 net::CertificateList cert_list_;
[email protected]16dad0962014-03-18 01:29:11159 // 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]3065a1f2014-01-22 08:56:35162 bool is_tpm_available_;
[email protected]df8e899b2011-02-22 22:58:22163
164 // The observer to notify when certificate list is refreshed.
165 Observer* observer_;
166
167 DISALLOW_COPY_AND_ASSIGN(CertificateManagerModel);
168};
169
[email protected]4f242962011-05-13 22:25:22170#endif // CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_