Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: net/base/cert_database.h

Issue 10916094: Move the NSS functions out of CertDatabase into a new NSSCertDatabase class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_BASE_CERT_DATABASE_H_ 5 #ifndef NET_BASE_CERT_DATABASE_H_
6 #define NET_BASE_CERT_DATABASE_H_ 6 #define NET_BASE_CERT_DATABASE_H_
7 7
8 #include <string>
9 #include <vector>
10
11 #include "base/basictypes.h" 8 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/string16.h"
14 #include "net/base/cert_type.h"
15 #include "net/base/net_export.h" 9 #include "net/base/net_export.h"
16 #include "net/base/x509_certificate.h" 10 #include "net/base/x509_certificate.h"
17 11
18 namespace net { 12 namespace net {
19 13
20 class CryptoModule; 14 // This class provides cross-platform functions to verify and add user
21 typedef std::vector<scoped_refptr<CryptoModule> > CryptoModuleList; 15 // certificates, and to observe changes to the underlying certificate stores.
22
23 // This class provides functions to manipulate the local
24 // certificate store.
25 16
26 // TODO(gauravsh): This class could be augmented with methods 17 // TODO(gauravsh): This class could be augmented with methods
27 // for all operations that manipulate the underlying system 18 // for all operations that manipulate the underlying system
28 // certificate store. 19 // certificate store.
29 20
30 class NET_EXPORT CertDatabase { 21 class NET_EXPORT CertDatabase {
31 public: 22 public:
32 23
33 // A CertDatabase::Observer will be notified on certificate database changes. 24 // A CertDatabase::Observer will be notified on certificate database changes.
34 // The change could be either a new user certificate is added or trust on 25 // The change could be either a new user certificate is added or trust on
35 // a certificate is changed. Observers can register themselves 26 // a certificate is changed. Observers can register themselves
36 // via CertDatabase::AddObserver, and can un-register with 27 // via CertDatabase::AddObserver, and can un-register with
37 // CertDatabase::RemoveObserver. 28 // CertDatabase::RemoveObserver.
38 class NET_EXPORT Observer { 29 class NET_EXPORT Observer {
39 public: 30 public:
40 virtual ~Observer() {} 31 virtual ~Observer() {}
41 32
42 // Will be called when a new user certificate is added. 33 // Will be called when a new certificate is added.
43 // Called with |cert| == NULL after importing a list of certificates 34 virtual void OnCertAdded(const X509Certificate* cert) {}
44 // in ImportFromPKCS12().
45 virtual void OnUserCertAdded(const X509Certificate* cert) {}
46 35
47 // Will be called when a user certificate is removed. 36 // Will be called when a certificate is removed.
48 virtual void OnUserCertRemoved(const X509Certificate* cert) {} 37 virtual void OnCertRemoved(const X509Certificate* cert) {}
49 38
50 // Will be called when a certificate's trust is changed. 39 // Will be called when a certificate's trust is changed.
51 // Called with |cert| == NULL after importing a list of certificates
52 // in ImportCACerts().
53 virtual void OnCertTrustChanged(const X509Certificate* cert) {} 40 virtual void OnCertTrustChanged(const X509Certificate* cert) {}
54 41
55 protected: 42 protected:
56 Observer() {} 43 Observer() {}
57 44
58 private: 45 private:
59 DISALLOW_COPY_AND_ASSIGN(Observer); 46 DISALLOW_COPY_AND_ASSIGN(Observer);
60 }; 47 };
61 48
62 // Stores per-certificate error codes for import failures.
63 struct NET_EXPORT ImportCertFailure {
64 public:
65 ImportCertFailure(X509Certificate* cert, int err);
66 ~ImportCertFailure();
67
68 scoped_refptr<X509Certificate> certificate;
69 int net_error;
70 };
71 typedef std::vector<ImportCertFailure> ImportCertFailureList;
72
73 // Constants that define which usages a certificate is trusted for.
74 // They are used in combination with CertType to specify trust for each type
75 // of certificate.
76 // For a CA_CERT, they specify that the CA is trusted for issuing server and
77 // client certs of each type.
78 // For SERVER_CERT, only TRUSTED_SSL makes sense, and specifies the cert is
79 // trusted as a server.
80 // For EMAIL_CERT, only TRUSTED_EMAIL makes sense, and specifies the cert is
81 // trusted for email.
82 // DISTRUSTED_* specifies that the cert should not be trusted for the given
83 // usage, regardless of whether it would otherwise inherit trust from the
84 // issuer chain.
85 // Use TRUST_DEFAULT to inherit trust as normal.
86 // NOTE: The actual constants are defined using an enum instead of static
87 // consts due to compilation/linkage constraints with template functions.
88 typedef uint32 TrustBits;
89 enum {
90 TRUST_DEFAULT = 0,
91 TRUSTED_SSL = 1 << 0,
92 TRUSTED_EMAIL = 1 << 1,
93 TRUSTED_OBJ_SIGN = 1 << 2,
94 DISTRUSTED_SSL = 1 << 3,
95 DISTRUSTED_EMAIL = 1 << 4,
96 DISTRUSTED_OBJ_SIGN = 1 << 5,
97 };
98
99 CertDatabase(); 49 CertDatabase();
100 50
101 // Check whether this is a valid user cert that we have the private key for. 51 // Check whether this is a valid user cert that we have the private key for.
102 // Returns OK or a network error code such as ERR_CERT_CONTAINS_ERRORS. 52 // Returns OK or a network error code such as ERR_CERT_CONTAINS_ERRORS.
103 int CheckUserCert(X509Certificate* cert); 53 int CheckUserCert(X509Certificate* cert);
104 54
105 // Store user (client) certificate. Assumes CheckUserCert has already passed. 55 // Store user (client) certificate. Assumes CheckUserCert has already passed.
106 // Returns OK, or ERR_ADD_USER_CERT_FAILED if there was a problem saving to 56 // Returns OK, or ERR_ADD_USER_CERT_FAILED if there was a problem saving to
107 // the platform cert database, or possibly other network error codes. 57 // the platform cert database, or possibly other network error codes.
108 int AddUserCert(X509Certificate* cert); 58 int AddUserCert(X509Certificate* cert);
109 59
110 #if defined(USE_NSS)
111 // Get a list of unique certificates in the certificate database (one
112 // instance of all certificates).
113 void ListCerts(CertificateList* certs);
114
115 // Get the default module for public key data.
116 // The returned pointer must be stored in a scoped_refptr<CryptoModule>.
117 CryptoModule* GetPublicModule() const;
118
119 // Get the default module for private key or mixed private/public key data.
120 // The returned pointer must be stored in a scoped_refptr<CryptoModule>.
121 CryptoModule* GetPrivateModule() const;
122
123 // Get all modules.
124 // If |need_rw| is true, only writable modules will be returned.
125 void ListModules(CryptoModuleList* modules, bool need_rw) const;
126
127 // Import certificates and private keys from PKCS #12 blob into the module.
128 // If |is_extractable| is false, mark the private key as being unextractable
129 // from the module.
130 // Returns OK or a network error code such as ERR_PKCS12_IMPORT_BAD_PASSWORD
131 // or ERR_PKCS12_IMPORT_ERROR. |imported_certs|, if non-NULL, returns a list
132 // of certs that were imported.
133 int ImportFromPKCS12(CryptoModule* module,
134 const std::string& data,
135 const string16& password,
136 bool is_extractable,
137 CertificateList* imported_certs);
138
139 // Export the given certificates and private keys into a PKCS #12 blob,
140 // storing into |output|.
141 // Returns the number of certificates successfully exported.
142 int ExportToPKCS12(const CertificateList& certs, const string16& password,
143 std::string* output) const;
144
145 // Uses similar logic to nsNSSCertificateDB::handleCACertDownload to find the
146 // root. Assumes the list is an ordered hierarchy with the root being either
147 // the first or last element.
148 // TODO(mattm): improve this to handle any order.
149 X509Certificate* FindRootInList(const CertificateList& certificates) const;
150
151 // Import CA certificates.
152 // Tries to import all the certificates given. The root will be trusted
153 // according to |trust_bits|. Any certificates that could not be imported
154 // will be listed in |not_imported|.
155 // Returns false if there is an internal error, otherwise true is returned and
156 // |not_imported| should be checked for any certificates that were not
157 // imported.
158 bool ImportCACerts(const CertificateList& certificates,
159 TrustBits trust_bits,
160 ImportCertFailureList* not_imported);
161
162 // Import server certificate. The first cert should be the server cert. Any
163 // additional certs should be intermediate/CA certs and will be imported but
164 // not given any trust.
165 // Any certificates that could not be imported will be listed in
166 // |not_imported|.
167 // |trust_bits| can be set to explicitly trust or distrust the certificate, or
168 // use TRUST_DEFAULT to inherit trust as normal.
169 // Returns false if there is an internal error, otherwise true is returned and
170 // |not_imported| should be checked for any certificates that were not
171 // imported.
172 bool ImportServerCert(const CertificateList& certificates,
173 TrustBits trust_bits,
174 ImportCertFailureList* not_imported);
175
176 // Get trust bits for certificate.
177 TrustBits GetCertTrust(const X509Certificate* cert, CertType type) const;
178
179 // IsUntrusted returns true if |cert| is specifically untrusted. These
180 // certificates are stored in the database for the specific purpose of
181 // rejecting them.
182 bool IsUntrusted(const X509Certificate* cert) const;
183
184 // Set trust values for certificate.
185 // Returns true on success or false on failure.
186 bool SetCertTrust(const X509Certificate* cert,
187 CertType type,
188 TrustBits trust_bits);
189
190 // Delete certificate and associated private key (if one exists).
191 // |cert| is still valid when this function returns. Returns true on
192 // success.
193 bool DeleteCertAndKey(const X509Certificate* cert);
194
195 // Check whether cert is stored in a readonly slot.
196 bool IsReadOnly(const X509Certificate* cert) const;
197 #endif
198
199 // Registers |observer| to receive notifications of certificate changes. The 60 // Registers |observer| to receive notifications of certificate changes. The
200 // thread on which this is called is the thread on which |observer| will be 61 // thread on which this is called is the thread on which |observer| will be
201 // called back with notifications. 62 // called back with notifications.
202 static void AddObserver(Observer* observer); 63 void AddObserver(Observer* observer);
203 64
204 // Unregisters |observer| from receiving notifications. This must be called 65 // Unregisters |observer| from receiving notifications. This must be called
205 // on the same thread on which AddObserver() was called. 66 // on the same thread on which AddObserver() was called.
206 static void RemoveObserver(Observer* observer); 67 void RemoveObserver(Observer* observer);
207 68
208 private: 69 private:
209 // Broadcasts notifications to all registered observers. 70 // Broadcasts notifications to all registered observers.
210 static void NotifyObserversOfUserCertAdded(const X509Certificate* cert); 71 void NotifyObserversOfCertAdded(const X509Certificate* cert);
211 static void NotifyObserversOfUserCertRemoved(const X509Certificate* cert); 72 void NotifyObserversOfCertRemoved(const X509Certificate* cert);
212 static void NotifyObserversOfCertTrustChanged(const X509Certificate* cert); 73 void NotifyObserversOfCertTrustChanged(const X509Certificate* cert);
213 74
214 DISALLOW_COPY_AND_ASSIGN(CertDatabase); 75 DISALLOW_COPY_AND_ASSIGN(CertDatabase);
215 }; 76 };
216 77
217 } // namespace net 78 } // namespace net
218 79
219 #endif // NET_BASE_CERT_DATABASE_H_ 80 #endif // NET_BASE_CERT_DATABASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698