| 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> | 10 #include <map> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/ref_counted.h" | 15 #include "base/ref_counted.h" |
| 16 #include "base/singleton.h" | 16 #include "base/singleton.h" |
| 17 #include "base/time.h" | 17 #include "base/time.h" |
| 18 #include "net/base/x509_cert_types.h" |
| 18 #include "testing/gtest/include/gtest/gtest_prod.h" | 19 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 19 | 20 |
| 20 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
| 21 #include <windows.h> | 22 #include <windows.h> |
| 22 #include <wincrypt.h> | 23 #include <wincrypt.h> |
| 23 #elif defined(OS_MACOSX) | 24 #elif defined(OS_MACOSX) |
| 24 #include <Security/Security.h> | 25 #include <CoreFoundation/CFArray.h> |
| 26 #include <Security/SecBase.h> |
| 25 #elif defined(USE_NSS) | 27 #elif defined(USE_NSS) |
| 26 // Forward declaration; real one in <cert.h> | 28 // Forward declaration; real one in <cert.h> |
| 27 struct CERTCertificateStr; | 29 struct CERTCertificateStr; |
| 28 #endif | 30 #endif |
| 29 | 31 |
| 30 class Pickle; | 32 class Pickle; |
| 31 | 33 |
| 32 namespace net { | 34 namespace net { |
| 33 | 35 |
| 34 class CertVerifyResult; | 36 class CertVerifyResult; |
| 35 | 37 |
| 36 // X509Certificate represents an X.509 certificate used by SSL. | 38 // X509Certificate represents an X.509 certificate used by SSL. |
| 37 class X509Certificate : public base::RefCountedThreadSafe<X509Certificate> { | 39 class X509Certificate : public base::RefCountedThreadSafe<X509Certificate> { |
| 38 public: | 40 public: |
| 39 // SHA-1 fingerprint (160 bits) of a certificate. | |
| 40 struct Fingerprint { | |
| 41 bool Equals(const Fingerprint& other) const { | |
| 42 return memcmp(data, other.data, sizeof(data)) == 0; | |
| 43 } | |
| 44 | |
| 45 unsigned char data[20]; | |
| 46 }; | |
| 47 | |
| 48 class FingerprintLessThan | |
| 49 : public std::binary_function<Fingerprint, Fingerprint, bool> { | |
| 50 public: | |
| 51 bool operator() (const Fingerprint& lhs, const Fingerprint& rhs) const; | |
| 52 }; | |
| 53 | |
| 54 // Predicate functor used in maps when X509Certificate is used as the key. | |
| 55 class LessThan | |
| 56 : public std::binary_function<X509Certificate*, X509Certificate*, bool> { | |
| 57 public: | |
| 58 bool operator() (X509Certificate* lhs, X509Certificate* rhs) const; | |
| 59 }; | |
| 60 | |
| 61 // A handle to the certificate object in the underlying crypto library. | 41 // A handle to the certificate object in the underlying crypto library. |
| 62 // We assume that OSCertHandle is a pointer type on all platforms and | 42 // We assume that OSCertHandle is a pointer type on all platforms and |
| 63 // NULL is an invalid OSCertHandle. | 43 // NULL is an invalid OSCertHandle. |
| 64 #if defined(OS_WIN) | 44 #if defined(OS_WIN) |
| 65 typedef PCCERT_CONTEXT OSCertHandle; | 45 typedef PCCERT_CONTEXT OSCertHandle; |
| 66 #elif defined(OS_MACOSX) | 46 #elif defined(OS_MACOSX) |
| 67 typedef SecCertificateRef OSCertHandle; | 47 typedef SecCertificateRef OSCertHandle; |
| 68 #elif defined(USE_NSS) | 48 #elif defined(USE_NSS) |
| 69 typedef struct CERTCertificateStr* OSCertHandle; | 49 typedef struct CERTCertificateStr* OSCertHandle; |
| 70 #else | 50 #else |
| 71 // TODO(ericroman): not implemented | 51 // TODO(ericroman): not implemented |
| 72 typedef void* OSCertHandle; | 52 typedef void* OSCertHandle; |
| 73 #endif | 53 #endif |
| 74 | 54 |
| 75 typedef std::vector<OSCertHandle> OSCertHandles; | 55 typedef std::vector<OSCertHandle> OSCertHandles; |
| 76 | 56 |
| 77 // Principal represent an X.509 principal. | 57 // Legacy names for types now defined in x509_cert_types.h. |
| 78 struct Principal { | 58 // TODO(snej): Clean up existing code using these names to use the new names. |
| 79 Principal() { } | 59 typedef CertPrincipal Principal; |
| 80 explicit Principal(const std::string& name) : common_name(name) { } | 60 typedef CertPolicy Policy; |
| 61 typedef SHA1Fingerprint Fingerprint; |
| 62 typedef SHA1FingerprintLessThan FingerprintLessThan; |
| 81 | 63 |
| 82 // The different attributes for a principal. They may be "". | 64 // Predicate functor used in maps when X509Certificate is used as the key. |
| 83 // Note that some of them can have several values. | 65 class LessThan |
| 84 | 66 : public std::binary_function<X509Certificate*, X509Certificate*, bool> { |
| 85 std::string common_name; | |
| 86 std::string locality_name; | |
| 87 std::string state_or_province_name; | |
| 88 std::string country_name; | |
| 89 | |
| 90 std::vector<std::string> street_addresses; | |
| 91 std::vector<std::string> organization_names; | |
| 92 std::vector<std::string> organization_unit_names; | |
| 93 std::vector<std::string> domain_components; | |
| 94 }; | |
| 95 | |
| 96 // This class is useful for maintaining policies about which certificates are | |
| 97 // permitted or forbidden for a particular purpose. | |
| 98 class Policy { | |
| 99 public: | 67 public: |
| 100 // The judgments this policy can reach. | 68 bool operator() (X509Certificate* lhs, X509Certificate* rhs) const; |
| 101 enum Judgment { | |
| 102 // We don't have policy information for this certificate. | |
| 103 UNKNOWN, | |
| 104 | |
| 105 // This certificate is allowed. | |
| 106 ALLOWED, | |
| 107 | |
| 108 // This certificate is denied. | |
| 109 DENIED, | |
| 110 }; | |
| 111 | |
| 112 // Returns the judgment this policy makes about this certificate. | |
| 113 Judgment Check(X509Certificate* cert) const; | |
| 114 | |
| 115 // Causes the policy to allow this certificate. | |
| 116 void Allow(X509Certificate* cert); | |
| 117 | |
| 118 // Causes the policy to deny this certificate. | |
| 119 void Deny(X509Certificate* cert); | |
| 120 | |
| 121 // Returns true if this policy has allowed at least one certificate. | |
| 122 bool HasAllowedCert() const; | |
| 123 | |
| 124 // Returns true if this policy has denied at least one certificate. | |
| 125 bool HasDeniedCert() const; | |
| 126 | |
| 127 private: | |
| 128 // The set of fingerprints of allowed certificates. | |
| 129 std::set<Fingerprint, FingerprintLessThan> allowed_; | |
| 130 | |
| 131 // The set of fingerprints of denied certificates. | |
| 132 std::set<Fingerprint, FingerprintLessThan> denied_; | |
| 133 }; | 69 }; |
| 134 | 70 |
| 135 // Where the certificate comes from. The enumeration constants are | 71 // Where the certificate comes from. The enumeration constants are |
| 136 // listed in increasing order of preference. | 72 // listed in increasing order of preference. |
| 137 enum Source { | 73 enum Source { |
| 138 SOURCE_UNUSED = 0, // The source_ member is not used. | 74 SOURCE_UNUSED = 0, // The source_ member is not used. |
| 139 SOURCE_LONE_CERT_IMPORT = 1, // From importing a certificate without | 75 SOURCE_LONE_CERT_IMPORT = 1, // From importing a certificate without |
| 140 // its intermediate CA certificates. | 76 // its intermediate CA certificates. |
| 141 SOURCE_FROM_NETWORK = 2, // From the network. | 77 SOURCE_FROM_NETWORK = 2, // From the network. |
| 142 }; | 78 }; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 // Returns true if I already contain the given intermediate cert. | 160 // Returns true if I already contain the given intermediate cert. |
| 225 bool HasIntermediateCertificate(OSCertHandle cert); | 161 bool HasIntermediateCertificate(OSCertHandle cert); |
| 226 | 162 |
| 227 // Returns true if I already contain all the given intermediate certs. | 163 // Returns true if I already contain all the given intermediate certs. |
| 228 bool HasIntermediateCertificates(const OSCertHandles& certs); | 164 bool HasIntermediateCertificates(const OSCertHandles& certs); |
| 229 | 165 |
| 230 #if defined(OS_MACOSX) | 166 #if defined(OS_MACOSX) |
| 231 // Does this certificate's usage allow SSL client authentication? | 167 // Does this certificate's usage allow SSL client authentication? |
| 232 bool SupportsSSLClientAuth() const; | 168 bool SupportsSSLClientAuth() const; |
| 233 | 169 |
| 170 // Do any of the given issuer names appear in this cert's chain of trust? |
| 171 bool IsIssuedBy(const std::vector<CertPrincipal>& valid_issuers); |
| 172 |
| 234 // Creates a security policy for SSL client certificates. | 173 // Creates a security policy for SSL client certificates. |
| 235 static OSStatus CreateSSLClientPolicy(SecPolicyRef* outPolicy); | 174 static OSStatus CreateSSLClientPolicy(SecPolicyRef* outPolicy); |
| 236 | 175 |
| 237 // Adds all available SSL client identity certs to the given vector. | 176 // Adds all available SSL client identity certs to the given vector. |
| 238 // |server_domain| is a hint for which domain the cert is to be sent to | 177 // |server_domain| is a hint for which domain the cert is to be sent to |
| 239 // (a cert previously specified as the default for that domain will be given | 178 // (a cert previously specified as the default for that domain will be given |
| 240 // precedence and returned first in the output vector.) | 179 // precedence and returned first in the output vector.) |
| 180 // If valid_issuers is non-empty, only certs that were transitively issued by |
| 181 // one of the given names will be included in the list. |
| 241 static bool GetSSLClientCertificates( | 182 static bool GetSSLClientCertificates( |
| 242 const std::string& server_domain, | 183 const std::string& server_domain, |
| 184 const std::vector<CertPrincipal>& valid_issuers, |
| 243 std::vector<scoped_refptr<X509Certificate> >* certs); | 185 std::vector<scoped_refptr<X509Certificate> >* certs); |
| 244 | 186 |
| 245 // Creates the chain of certs to use for this client identity cert. | 187 // Creates the chain of certs to use for this client identity cert. |
| 246 CFArrayRef CreateClientCertificateChain() const; | 188 CFArrayRef CreateClientCertificateChain() const; |
| 247 #endif | 189 #endif |
| 248 | 190 |
| 249 // Verifies the certificate against the given hostname. Returns OK if | 191 // Verifies the certificate against the given hostname. Returns OK if |
| 250 // successful or an error code upon failure. | 192 // successful or an error code upon failure. |
| 251 // | 193 // |
| 252 // The |*verify_result| structure, including the |verify_result->cert_status| | 194 // The |*verify_result| structure, including the |verify_result->cert_status| |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 | 295 |
| 354 // Where the certificate comes from. | 296 // Where the certificate comes from. |
| 355 Source source_; | 297 Source source_; |
| 356 | 298 |
| 357 DISALLOW_COPY_AND_ASSIGN(X509Certificate); | 299 DISALLOW_COPY_AND_ASSIGN(X509Certificate); |
| 358 }; | 300 }; |
| 359 | 301 |
| 360 } // namespace net | 302 } // namespace net |
| 361 | 303 |
| 362 #endif // NET_BASE_X509_CERTIFICATE_H_ | 304 #endif // NET_BASE_X509_CERTIFICATE_H_ |
| OLD | NEW |