| 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 <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <map> | |
| 11 #include <set> | |
| 12 #include <string> | 10 #include <string> |
| 13 #include <vector> | 11 #include <vector> |
| 14 | 12 |
| 15 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
| 16 #include "base/singleton.h" | |
| 17 #include "base/time.h" | 14 #include "base/time.h" |
| 18 #include "net/base/x509_cert_types.h" | 15 #include "net/base/x509_cert_types.h" |
| 19 #include "testing/gtest/include/gtest/gtest_prod.h" | 16 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 20 | 17 |
| 21 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
| 22 #include <windows.h> | 19 #include <windows.h> |
| 23 #include <wincrypt.h> | 20 #include <wincrypt.h> |
| 24 #elif defined(OS_MACOSX) | 21 #elif defined(OS_MACOSX) |
| 25 #include <CoreFoundation/CFArray.h> | 22 #include <CoreFoundation/CFArray.h> |
| 26 #include <Security/SecBase.h> | 23 #include <Security/SecBase.h> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 47 typedef SecCertificateRef OSCertHandle; | 44 typedef SecCertificateRef OSCertHandle; |
| 48 #elif defined(USE_NSS) | 45 #elif defined(USE_NSS) |
| 49 typedef struct CERTCertificateStr* OSCertHandle; | 46 typedef struct CERTCertificateStr* OSCertHandle; |
| 50 #else | 47 #else |
| 51 // TODO(ericroman): not implemented | 48 // TODO(ericroman): not implemented |
| 52 typedef void* OSCertHandle; | 49 typedef void* OSCertHandle; |
| 53 #endif | 50 #endif |
| 54 | 51 |
| 55 typedef std::vector<OSCertHandle> OSCertHandles; | 52 typedef std::vector<OSCertHandle> OSCertHandles; |
| 56 | 53 |
| 57 // Legacy names for types now defined in x509_cert_types.h. | |
| 58 // TODO(snej): Clean up existing code using these names to use the new names. | |
| 59 typedef CertPrincipal Principal; | |
| 60 typedef CertPolicy Policy; | |
| 61 typedef SHA1Fingerprint Fingerprint; | |
| 62 typedef SHA1FingerprintLessThan FingerprintLessThan; | |
| 63 | |
| 64 // Predicate functor used in maps when X509Certificate is used as the key. | 54 // Predicate functor used in maps when X509Certificate is used as the key. |
| 65 class LessThan | 55 class LessThan |
| 66 : public std::binary_function<X509Certificate*, X509Certificate*, bool> { | 56 : public std::binary_function<X509Certificate*, X509Certificate*, bool> { |
| 67 public: | 57 public: |
| 68 bool operator() (X509Certificate* lhs, X509Certificate* rhs) const; | 58 bool operator() (X509Certificate* lhs, X509Certificate* rhs) const; |
| 69 }; | 59 }; |
| 70 | 60 |
| 71 // Where the certificate comes from. The enumeration constants are | 61 // Where the certificate comes from. The enumeration constants are |
| 72 // listed in increasing order of preference. | 62 // listed in increasing order of preference. |
| 73 enum Source { | 63 enum Source { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 // SSL connections. | 103 // SSL connections. |
| 114 X509Certificate(const std::string& subject, const std::string& issuer, | 104 X509Certificate(const std::string& subject, const std::string& issuer, |
| 115 base::Time start_date, base::Time expiration_date); | 105 base::Time start_date, base::Time expiration_date); |
| 116 | 106 |
| 117 // Appends a representation of this object to the given pickle. | 107 // Appends a representation of this object to the given pickle. |
| 118 void Persist(Pickle* pickle); | 108 void Persist(Pickle* pickle); |
| 119 | 109 |
| 120 // The subject of the certificate. For HTTPS server certificates, this | 110 // The subject of the certificate. For HTTPS server certificates, this |
| 121 // represents the web server. The common name of the subject should match | 111 // represents the web server. The common name of the subject should match |
| 122 // the host name of the web server. | 112 // the host name of the web server. |
| 123 const Principal& subject() const { return subject_; } | 113 const CertPrincipal& subject() const { return subject_; } |
| 124 | 114 |
| 125 // The issuer of the certificate. | 115 // The issuer of the certificate. |
| 126 const Principal& issuer() const { return issuer_; } | 116 const CertPrincipal& issuer() const { return issuer_; } |
| 127 | 117 |
| 128 // Time period during which the certificate is valid. More precisely, this | 118 // Time period during which the certificate is valid. More precisely, this |
| 129 // certificate is invalid before the |valid_start| date and invalid after | 119 // certificate is invalid before the |valid_start| date and invalid after |
| 130 // the |valid_expiry| date. | 120 // the |valid_expiry| date. |
| 131 // If we were unable to parse either date from the certificate (or if the cert | 121 // If we were unable to parse either date from the certificate (or if the cert |
| 132 // lacks either date), the date will be null (i.e., is_null() will be true). | 122 // lacks either date), the date will be null (i.e., is_null() will be true). |
| 133 const base::Time& valid_start() const { return valid_start_; } | 123 const base::Time& valid_start() const { return valid_start_; } |
| 134 const base::Time& valid_expiry() const { return valid_expiry_; } | 124 const base::Time& valid_expiry() const { return valid_expiry_; } |
| 135 | 125 |
| 136 // The fingerprint of this certificate. | 126 // The fingerprint of this certificate. |
| 137 const Fingerprint& fingerprint() const { return fingerprint_; } | 127 const SHA1Fingerprint& fingerprint() const { return fingerprint_; } |
| 138 | 128 |
| 139 // Gets the DNS names in the certificate. Pursuant to RFC 2818, Section 3.1 | 129 // Gets the DNS names in the certificate. Pursuant to RFC 2818, Section 3.1 |
| 140 // Server Identity, if the certificate has a subjectAltName extension of | 130 // Server Identity, if the certificate has a subjectAltName extension of |
| 141 // type dNSName, this method gets the DNS names in that extension. | 131 // type dNSName, this method gets the DNS names in that extension. |
| 142 // Otherwise, it gets the common name in the subject field. | 132 // Otherwise, it gets the common name in the subject field. |
| 143 void GetDNSNames(std::vector<std::string>* dns_names) const; | 133 void GetDNSNames(std::vector<std::string>* dns_names) const; |
| 144 | 134 |
| 145 // Convenience method that returns whether this certificate has expired as of | 135 // Convenience method that returns whether this certificate has expired as of |
| 146 // now. | 136 // now. |
| 147 bool HasExpired() const; | 137 bool HasExpired() const; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 static OSCertHandle DupOSCertHandle(OSCertHandle cert_handle); | 207 static OSCertHandle DupOSCertHandle(OSCertHandle cert_handle); |
| 218 | 208 |
| 219 // Frees (or releases a reference to) an OS certificate handle. | 209 // Frees (or releases a reference to) an OS certificate handle. |
| 220 static void FreeOSCertHandle(OSCertHandle cert_handle); | 210 static void FreeOSCertHandle(OSCertHandle cert_handle); |
| 221 | 211 |
| 222 private: | 212 private: |
| 223 friend class base::RefCountedThreadSafe<X509Certificate>; | 213 friend class base::RefCountedThreadSafe<X509Certificate>; |
| 224 FRIEND_TEST(X509CertificateTest, Cache); | 214 FRIEND_TEST(X509CertificateTest, Cache); |
| 225 FRIEND_TEST(X509CertificateTest, IntermediateCertificates); | 215 FRIEND_TEST(X509CertificateTest, IntermediateCertificates); |
| 226 | 216 |
| 227 // A cache of X509Certificate objects. | 217 class Cache; |
| 228 class Cache { | |
| 229 public: | |
| 230 static Cache* GetInstance(); | |
| 231 void Insert(X509Certificate* cert); | |
| 232 void Remove(X509Certificate* cert); | |
| 233 X509Certificate* Find(const Fingerprint& fingerprint); | |
| 234 | |
| 235 private: | |
| 236 typedef std::map<Fingerprint, X509Certificate*, FingerprintLessThan> | |
| 237 CertMap; | |
| 238 | |
| 239 // Obtain an instance of X509Certificate::Cache via GetInstance(). | |
| 240 Cache() { } | |
| 241 friend struct DefaultSingletonTraits<Cache>; | |
| 242 | |
| 243 // You must acquire this lock before using any private data of this object. | |
| 244 // You must not block while holding this lock. | |
| 245 Lock lock_; | |
| 246 | |
| 247 // The certificate cache. You must acquire |lock_| before using |cache_|. | |
| 248 CertMap cache_; | |
| 249 | |
| 250 DISALLOW_COPY_AND_ASSIGN(Cache); | |
| 251 }; | |
| 252 | 218 |
| 253 // Construct an X509Certificate from a handle to the certificate object | 219 // Construct an X509Certificate from a handle to the certificate object |
| 254 // in the underlying crypto library. | 220 // in the underlying crypto library. |
| 255 X509Certificate(OSCertHandle cert_handle, Source source, | 221 X509Certificate(OSCertHandle cert_handle, Source source, |
| 256 const OSCertHandles& intermediates); | 222 const OSCertHandles& intermediates); |
| 257 | 223 |
| 258 ~X509Certificate(); | 224 ~X509Certificate(); |
| 259 | 225 |
| 260 // Common object initialization code. Called by the constructors only. | 226 // Common object initialization code. Called by the constructors only. |
| 261 void Initialize(); | 227 void Initialize(); |
| 262 | 228 |
| 263 bool VerifyEV() const; | 229 bool VerifyEV() const; |
| 264 | 230 |
| 265 // Calculates the SHA-1 fingerprint of the certificate. Returns an empty | 231 // Calculates the SHA-1 fingerprint of the certificate. Returns an empty |
| 266 // (all zero) fingerprint on failure. | 232 // (all zero) fingerprint on failure. |
| 267 static Fingerprint CalculateFingerprint(OSCertHandle cert_handle); | 233 static SHA1Fingerprint CalculateFingerprint(OSCertHandle cert_handle); |
| 268 | 234 |
| 269 // The subject of the certificate. | 235 // The subject of the certificate. |
| 270 Principal subject_; | 236 CertPrincipal subject_; |
| 271 | 237 |
| 272 // The issuer of the certificate. | 238 // The issuer of the certificate. |
| 273 Principal issuer_; | 239 CertPrincipal issuer_; |
| 274 | 240 |
| 275 // This certificate is not valid before |valid_start_| | 241 // This certificate is not valid before |valid_start_| |
| 276 base::Time valid_start_; | 242 base::Time valid_start_; |
| 277 | 243 |
| 278 // This certificate is not valid after |valid_expiry_| | 244 // This certificate is not valid after |valid_expiry_| |
| 279 base::Time valid_expiry_; | 245 base::Time valid_expiry_; |
| 280 | 246 |
| 281 // The fingerprint of this certificate. | 247 // The fingerprint of this certificate. |
| 282 Fingerprint fingerprint_; | 248 SHA1Fingerprint fingerprint_; |
| 283 | 249 |
| 284 // A handle to the certificate object in the underlying crypto library. | 250 // A handle to the certificate object in the underlying crypto library. |
| 285 OSCertHandle cert_handle_; | 251 OSCertHandle cert_handle_; |
| 286 | 252 |
| 287 #if defined(OS_MACOSX) || defined(OS_WIN) | 253 #if defined(OS_MACOSX) || defined(OS_WIN) |
| 288 // Untrusted intermediate certificates associated with this certificate | 254 // Untrusted intermediate certificates associated with this certificate |
| 289 // that may be needed for chain building. (NSS impl does not need these.) | 255 // that may be needed for chain building. (NSS impl does not need these.) |
| 290 OSCertHandles intermediate_ca_certs_; | 256 OSCertHandles intermediate_ca_certs_; |
| 291 #endif | 257 #endif |
| 292 | 258 |
| 293 #if defined(OS_MACOSX) | 259 #if defined(OS_MACOSX) |
| 294 // Blocks multiple threads from verifying the cert simultaneously. | 260 // Blocks multiple threads from verifying the cert simultaneously. |
| 295 // (Marked mutable because it's used in a const method.) | 261 // (Marked mutable because it's used in a const method.) |
| 296 mutable Lock verification_lock_; | 262 mutable Lock verification_lock_; |
| 297 #endif | 263 #endif |
| 298 | 264 |
| 299 // Where the certificate comes from. | 265 // Where the certificate comes from. |
| 300 Source source_; | 266 Source source_; |
| 301 | 267 |
| 302 DISALLOW_COPY_AND_ASSIGN(X509Certificate); | 268 DISALLOW_COPY_AND_ASSIGN(X509Certificate); |
| 303 }; | 269 }; |
| 304 | 270 |
| 305 } // namespace net | 271 } // namespace net |
| 306 | 272 |
| 307 #endif // NET_BASE_X509_CERTIFICATE_H_ | 273 #endif // NET_BASE_X509_CERTIFICATE_H_ |
| OLD | NEW |