blob: 7364b32714bbf2727e32319515812af7c7d89866 [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>
9#include <string>
10
[email protected]3065a1f2014-01-22 08:56:3511#include "base/callback.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
[email protected]3065a1f2014-01-22 08:56:3513#include "base/memory/scoped_ptr.h"
[email protected]e7463412013-06-10 22:53:4614#include "base/strings/string16.h"
[email protected]6e7845ae2013-03-29 21:48:1115#include "net/cert/nss_cert_database.h"
[email protected]df8e899b2011-02-22 22:58:2216
[email protected]3065a1f2014-01-22 08:56:3517namespace content {
18class BrowserContext;
19class ResourceContext;
20} // namespace content
21
[email protected]df8e899b2011-02-22 22:58:2222// CertificateManagerModel provides the data to be displayed in the certificate
23// manager dialog, and processes changes from the view.
24class 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]3065a1f2014-01-22 08:56:3531 typedef base::Callback<void(scoped_ptr<CertificateManagerModel>)>
32 CreationCallback;
33
[email protected]df8e899b2011-02-22 22:58:2234 // 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]3065a1f2014-01-22 08:56:3550 // 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]df8e899b2011-02-22 22:58:2257 ~CertificateManagerModel();
58
[email protected]3065a1f2014-01-22 08:56:3559 bool is_tpm_available() const { return is_tpm_available_; }
60
[email protected]7fda9a402012-09-10 14:11:0761 // Accessor for read-only access to the underlying NSSCertDatabase.
62 const net::NSSCertDatabase* cert_db() const { return cert_db_; }
[email protected]df8e899b2011-02-22 22:58:2263
[email protected]4c4f7cd2011-03-05 02:20:4464 // 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]df8e899b2011-02-22 22:58:2268 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]96920152013-12-04 21:00:1675 base::string16 GetColumnText(const net::X509Certificate& cert, Column column) const;
[email protected]df8e899b2011-02-22 22:58:2276
[email protected]6a18d072011-06-29 00:25:4077 // 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]df8e899b2011-02-22 22:58:2281 int ImportFromPKCS12(net::CryptoModule* module, const std::string& data,
[email protected]96920152013-12-04 21:00:1682 const base::string16& password, bool is_extractable);
[email protected]df8e899b2011-02-22 22:58:2283
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]7fda9a402012-09-10 14:11:0788 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:2289 // 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]7fda9a402012-09-10 14:11:0793 net::NSSCertDatabase::TrustBits trust_bits,
94 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:2295
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]ad40b212012-06-01 05:59:56101 // |trust_bits| can be set to explicitly trust or distrust the certificate, or
102 // use TRUST_DEFAULT to inherit trust as normal.
[email protected]df8e899b2011-02-22 22:58:22103 // 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]7fda9a402012-09-10 14:11:07108 net::NSSCertDatabase::TrustBits trust_bits,
109 net::NSSCertDatabase::ImportCertFailureList* not_imported);
[email protected]df8e899b2011-02-22 22:58:22110
111 // Set trust values for certificate.
[email protected]7fda9a402012-09-10 14:11:07112 // |trust_bits| should be a bit field of TRUST* values from NSSCertDatabase.
[email protected]df8e899b2011-02-22 22:58:22113 // Returns true on success or false on failure.
114 bool SetCertTrust(const net::X509Certificate* cert,
115 net::CertType type,
[email protected]7fda9a402012-09-10 14:11:07116 net::NSSCertDatabase::TrustBits trust_bits);
[email protected]df8e899b2011-02-22 22:58:22117
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]e0ad0892012-05-22 19:16:59122 // IsHardwareBacked returns true if |cert| is hardware backed.
123 bool IsHardwareBacked(const net::X509Certificate* cert) const;
124
[email protected]df8e899b2011-02-22 22:58:22125 private:
[email protected]3065a1f2014-01-22 08:56:35126 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]4c4f7cd2011-03-05 02:20:44145 // 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]7fda9a402012-09-10 14:11:07149 net::NSSCertDatabase* cert_db_;
[email protected]df8e899b2011-02-22 22:58:22150 net::CertificateList cert_list_;
[email protected]3065a1f2014-01-22 08:56:35151 bool is_tpm_available_;
[email protected]df8e899b2011-02-22 22:58:22152
153 // The observer to notify when certificate list is refreshed.
154 Observer* observer_;
155
156 DISALLOW_COPY_AND_ASSIGN(CertificateManagerModel);
157};
158
[email protected]4f242962011-05-13 22:25:22159#endif // CHROME_BROWSER_CERTIFICATE_MANAGER_MODEL_H_