| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_X509_CERTIFICATE_H_ | 5 #ifndef NET_BASE_X509_CERTIFICATE_H_ |
| 6 #define NET_BASE_X509_CERTIFICATE_H_ | 6 #define NET_BASE_X509_CERTIFICATE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include <Security/Security.h> | 22 #include <Security/Security.h> |
| 23 #elif defined(OS_LINUX) | 23 #elif defined(OS_LINUX) |
| 24 // Forward declaration; real one in <cert.h> | 24 // Forward declaration; real one in <cert.h> |
| 25 struct CERTCertificateStr; | 25 struct CERTCertificateStr; |
| 26 #endif | 26 #endif |
| 27 | 27 |
| 28 class Pickle; | 28 class Pickle; |
| 29 | 29 |
| 30 namespace net { | 30 namespace net { |
| 31 | 31 |
| 32 class CertVerifyResult; |
| 33 |
| 32 // X509Certificate represents an X.509 certificate used by SSL. | 34 // X509Certificate represents an X.509 certificate used by SSL. |
| 33 class X509Certificate : public base::RefCountedThreadSafe<X509Certificate> { | 35 class X509Certificate : public base::RefCountedThreadSafe<X509Certificate> { |
| 34 public: | 36 public: |
| 35 // SHA-1 fingerprint (160 bits) of a certificate. | 37 // SHA-1 fingerprint (160 bits) of a certificate. |
| 36 struct Fingerprint { | 38 struct Fingerprint { |
| 37 unsigned char data[20]; | 39 unsigned char data[20]; |
| 38 }; | 40 }; |
| 39 | 41 |
| 40 class FingerprintLessThan | 42 class FingerprintLessThan |
| 41 : public std::binary_function<Fingerprint, Fingerprint, bool> { | 43 : public std::binary_function<Fingerprint, Fingerprint, bool> { |
| 42 public: | 44 public: |
| 43 bool operator() (const Fingerprint& lhs, const Fingerprint& rhs) const; | 45 bool operator() (const Fingerprint& lhs, const Fingerprint& rhs) const; |
| 44 }; | 46 }; |
| 45 | 47 |
| 46 // Predicate functor used in maps when X509Certificate is used as the key. | 48 // Predicate functor used in maps when X509Certificate is used as the key. |
| 47 class LessThan | 49 class LessThan |
| 48 : public std::binary_function<X509Certificate*, X509Certificate*, bool> { | 50 : public std::binary_function<X509Certificate*, X509Certificate*, bool> { |
| 49 public: | 51 public: |
| 50 bool operator() (X509Certificate* lhs, X509Certificate* rhs) const; | 52 bool operator() (X509Certificate* lhs, X509Certificate* rhs) const; |
| 51 }; | 53 }; |
| 52 | 54 |
| 55 // A handle to the certificate object in the underlying crypto library. |
| 56 // We assume that OSCertHandle is a pointer type on all platforms and |
| 57 // NULL is an invalid OSCertHandle. |
| 53 #if defined(OS_WIN) | 58 #if defined(OS_WIN) |
| 54 typedef PCCERT_CONTEXT OSCertHandle; | 59 typedef PCCERT_CONTEXT OSCertHandle; |
| 55 #elif defined(OS_MACOSX) | 60 #elif defined(OS_MACOSX) |
| 56 typedef SecCertificateRef OSCertHandle; | 61 typedef SecCertificateRef OSCertHandle; |
| 57 #elif defined(OS_LINUX) | 62 #elif defined(OS_LINUX) |
| 58 typedef struct CERTCertificateStr* OSCertHandle; | 63 typedef struct CERTCertificateStr* OSCertHandle; |
| 59 #else | 64 #else |
| 60 // TODO(ericroman): not implemented | 65 // TODO(ericroman): not implemented |
| 61 typedef void* OSCertHandle; | 66 typedef void* OSCertHandle; |
| 62 #endif | 67 #endif |
| 63 » | 68 |
| 64 // Principal represent an X.509 principal. | 69 // Principal represent an X.509 principal. |
| 65 struct Principal { | 70 struct Principal { |
| 66 Principal() { } | 71 Principal() { } |
| 67 explicit Principal(std::string name) : common_name(name) { } | 72 explicit Principal(std::string name) : common_name(name) { } |
| 68 | 73 |
| 69 // The different attributes for a principal. They may be "". | 74 // The different attributes for a principal. They may be "". |
| 70 // Note that some of them can have several values. | 75 // Note that some of them can have several values. |
| 71 | 76 |
| 72 std::string common_name; | 77 std::string common_name; |
| 73 std::string locality_name; | 78 std::string locality_name; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 // Gets the DNS names in the certificate. Pursuant to RFC 2818, Section 3.1 | 179 // Gets the DNS names in the certificate. Pursuant to RFC 2818, Section 3.1 |
| 175 // Server Identity, if the certificate has a subjectAltName extension of | 180 // Server Identity, if the certificate has a subjectAltName extension of |
| 176 // type dNSName, this method gets the DNS names in that extension. | 181 // type dNSName, this method gets the DNS names in that extension. |
| 177 // Otherwise, it gets the common name in the subject field. | 182 // Otherwise, it gets the common name in the subject field. |
| 178 void GetDNSNames(std::vector<std::string>* dns_names) const; | 183 void GetDNSNames(std::vector<std::string>* dns_names) const; |
| 179 | 184 |
| 180 // Convenience method that returns whether this certificate has expired as of | 185 // Convenience method that returns whether this certificate has expired as of |
| 181 // now. | 186 // now. |
| 182 bool HasExpired() const; | 187 bool HasExpired() const; |
| 183 | 188 |
| 189 // Verifies the certificate against the given hostname. Returns OK if |
| 190 // successful or an error code upon failure. |
| 191 // |
| 192 // The |*verify_result| structure, including the |verify_result->cert_status| |
| 193 // bitmask, is always filled out regardless of the return value. If the |
| 194 // certificate has multiple errors, the corresponding status flags are set in |
| 195 // |verify_result->cert_status|, and the error code for the most serious |
| 196 // error is returned. |
| 197 // |
| 198 // If |rev_checking_enabled| is true, certificate revocation checking is |
| 199 // performed. |
| 200 int Verify(const std::string& hostname, |
| 201 bool rev_checking_enabled, |
| 202 CertVerifyResult* verify_result) const; |
| 203 |
| 184 // Returns true if the certificate is an extended-validation (EV) | 204 // Returns true if the certificate is an extended-validation (EV) |
| 185 // certificate. | 205 // certificate. |
| 186 bool IsEV(int cert_status) const; | 206 bool IsEV(int cert_status) const; |
| 187 | 207 |
| 188 OSCertHandle os_cert_handle() const { return cert_handle_; } | 208 OSCertHandle os_cert_handle() const { return cert_handle_; } |
| 189 | 209 |
| 190 private: | 210 private: |
| 191 friend class base::RefCountedThreadSafe<X509Certificate>; | 211 friend class base::RefCountedThreadSafe<X509Certificate>; |
| 192 FRIEND_TEST(X509CertificateTest, Cache); | 212 FRIEND_TEST(X509CertificateTest, Cache); |
| 193 | 213 |
| 194 // A cache of X509Certificate objects. | 214 // A cache of X509Certificate objects. |
| 195 class Cache { | 215 class Cache { |
| 196 public: | 216 public: |
| 197 static Cache* GetInstance(); | 217 static Cache* GetInstance(); |
| 198 void Insert(X509Certificate* cert); | 218 void Insert(X509Certificate* cert); |
| 199 void Remove(X509Certificate* cert); | 219 void Remove(X509Certificate* cert); |
| 200 X509Certificate* Find(const Fingerprint& fingerprint); | 220 X509Certificate* Find(const Fingerprint& fingerprint); |
| 201 | 221 |
| 202 private: | 222 private: |
| 203 typedef std::map<Fingerprint, X509Certificate*, FingerprintLessThan> | 223 typedef std::map<Fingerprint, X509Certificate*, FingerprintLessThan> |
| 204 CertMap; | 224 CertMap; |
| 205 | 225 |
| 206 // Obtain an instance of X509Certificate::Cache via GetInstance(). | 226 // Obtain an instance of X509Certificate::Cache via GetInstance(). |
| 207 Cache() { } | 227 Cache() { } |
| 208 friend struct DefaultSingletonTraits<Cache>; | 228 friend struct DefaultSingletonTraits<Cache>; |
| 209 | 229 |
| 210 // You must acquire this lock before using any private data of this object. | 230 // You must acquire this lock before using any private data of this object. |
| 211 // You must not block while holding this lock. | 231 // You must not block while holding this lock. |
| 212 Lock lock_; | 232 Lock lock_; |
| 213 | 233 |
| 214 // The certificate cache. You must acquire |lock_| before using |cache_|. | 234 // The certificate cache. You must acquire |lock_| before using |cache_|. |
| 215 CertMap cache_; | 235 CertMap cache_; |
| 216 | 236 |
| 217 DISALLOW_COPY_AND_ASSIGN(Cache); | 237 DISALLOW_COPY_AND_ASSIGN(Cache); |
| 218 }; | 238 }; |
| 219 | 239 |
| 220 // Construct an X509Certificate from a handle to the certificate object | 240 // Construct an X509Certificate from a handle to the certificate object |
| 221 // in the underlying crypto library. | 241 // in the underlying crypto library. |
| 222 X509Certificate(OSCertHandle cert_handle, Source source); | 242 X509Certificate(OSCertHandle cert_handle, Source source); |
| 223 | 243 |
| 224 ~X509Certificate(); | 244 ~X509Certificate(); |
| 225 | 245 |
| 226 // Common object initialization code. Called by the constructors only. | 246 // Common object initialization code. Called by the constructors only. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 // Where the certificate comes from. | 279 // Where the certificate comes from. |
| 260 Source source_; | 280 Source source_; |
| 261 | 281 |
| 262 DISALLOW_COPY_AND_ASSIGN(X509Certificate); | 282 DISALLOW_COPY_AND_ASSIGN(X509Certificate); |
| 263 }; | 283 }; |
| 264 | 284 |
| 265 } // namespace net | 285 } // namespace net |
| 266 | 286 |
| 267 #endif // NET_BASE_X509_CERTIFICATE_H_ | 287 #endif // NET_BASE_X509_CERTIFICATE_H_ |
| 268 | 288 |
| OLD | NEW |