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" | |
19 #include "testing/gtest/include/gtest/gtest_prod.h" | 18 #include "testing/gtest/include/gtest/gtest_prod.h" |
20 | 19 |
21 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
22 #include <windows.h> | 21 #include <windows.h> |
23 #include <wincrypt.h> | 22 #include <wincrypt.h> |
24 #elif defined(OS_MACOSX) | 23 #elif defined(OS_MACOSX) |
25 #include <CoreFoundation/CFArray.h> | 24 #include <Security/Security.h> |
26 #include <Security/SecBase.h> | |
27 #elif defined(USE_NSS) | 25 #elif defined(USE_NSS) |
28 // Forward declaration; real one in <cert.h> | 26 // Forward declaration; real one in <cert.h> |
29 struct CERTCertificateStr; | 27 struct CERTCertificateStr; |
30 #endif | 28 #endif |
31 | 29 |
32 class Pickle; | 30 class Pickle; |
33 | 31 |
34 namespace net { | 32 namespace net { |
35 | 33 |
36 class CertVerifyResult; | 34 class CertVerifyResult; |
37 | 35 |
38 // X509Certificate represents an X.509 certificate used by SSL. | 36 // X509Certificate represents an X.509 certificate used by SSL. |
39 class X509Certificate : public base::RefCountedThreadSafe<X509Certificate> { | 37 class X509Certificate : public base::RefCountedThreadSafe<X509Certificate> { |
40 public: | 38 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 |
41 // A handle to the certificate object in the underlying crypto library. | 61 // A handle to the certificate object in the underlying crypto library. |
42 // We assume that OSCertHandle is a pointer type on all platforms and | 62 // We assume that OSCertHandle is a pointer type on all platforms and |
43 // NULL is an invalid OSCertHandle. | 63 // NULL is an invalid OSCertHandle. |
44 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
45 typedef PCCERT_CONTEXT OSCertHandle; | 65 typedef PCCERT_CONTEXT OSCertHandle; |
46 #elif defined(OS_MACOSX) | 66 #elif defined(OS_MACOSX) |
47 typedef SecCertificateRef OSCertHandle; | 67 typedef SecCertificateRef OSCertHandle; |
48 #elif defined(USE_NSS) | 68 #elif defined(USE_NSS) |
49 typedef struct CERTCertificateStr* OSCertHandle; | 69 typedef struct CERTCertificateStr* OSCertHandle; |
50 #else | 70 #else |
51 // TODO(ericroman): not implemented | 71 // TODO(ericroman): not implemented |
52 typedef void* OSCertHandle; | 72 typedef void* OSCertHandle; |
53 #endif | 73 #endif |
54 | 74 |
55 typedef std::vector<OSCertHandle> OSCertHandles; | 75 typedef std::vector<OSCertHandle> OSCertHandles; |
56 | 76 |
57 // Legacy names for types now defined in x509_cert_types.h. | 77 // Principal represent an X.509 principal. |
58 // TODO(snej): Clean up existing code using these names to use the new names. | 78 struct Principal { |
59 typedef CertPrincipal Principal; | 79 Principal() { } |
60 typedef CertPolicy Policy; | 80 explicit Principal(const std::string& name) : common_name(name) { } |
61 typedef SHA1Fingerprint Fingerprint; | |
62 typedef SHA1FingerprintLessThan FingerprintLessThan; | |
63 | 81 |
64 // Predicate functor used in maps when X509Certificate is used as the key. | 82 // The different attributes for a principal. They may be "". |
65 class LessThan | 83 // Note that some of them can have several values. |
66 : public std::binary_function<X509Certificate*, X509Certificate*, bool> { | 84 |
| 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 { |
67 public: | 99 public: |
68 bool operator() (X509Certificate* lhs, X509Certificate* rhs) const; | 100 // The judgments this policy can reach. |
| 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_; |
69 }; | 133 }; |
70 | 134 |
71 // Where the certificate comes from. The enumeration constants are | 135 // Where the certificate comes from. The enumeration constants are |
72 // listed in increasing order of preference. | 136 // listed in increasing order of preference. |
73 enum Source { | 137 enum Source { |
74 SOURCE_UNUSED = 0, // The source_ member is not used. | 138 SOURCE_UNUSED = 0, // The source_ member is not used. |
75 SOURCE_LONE_CERT_IMPORT = 1, // From importing a certificate without | 139 SOURCE_LONE_CERT_IMPORT = 1, // From importing a certificate without |
76 // its intermediate CA certificates. | 140 // its intermediate CA certificates. |
77 SOURCE_FROM_NETWORK = 2, // From the network. | 141 SOURCE_FROM_NETWORK = 2, // From the network. |
78 }; | 142 }; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 // Returns true if I already contain the given intermediate cert. | 224 // Returns true if I already contain the given intermediate cert. |
161 bool HasIntermediateCertificate(OSCertHandle cert); | 225 bool HasIntermediateCertificate(OSCertHandle cert); |
162 | 226 |
163 // Returns true if I already contain all the given intermediate certs. | 227 // Returns true if I already contain all the given intermediate certs. |
164 bool HasIntermediateCertificates(const OSCertHandles& certs); | 228 bool HasIntermediateCertificates(const OSCertHandles& certs); |
165 | 229 |
166 #if defined(OS_MACOSX) | 230 #if defined(OS_MACOSX) |
167 // Does this certificate's usage allow SSL client authentication? | 231 // Does this certificate's usage allow SSL client authentication? |
168 bool SupportsSSLClientAuth() const; | 232 bool SupportsSSLClientAuth() const; |
169 | 233 |
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 | |
173 // Creates a security policy for SSL client certificates. | 234 // Creates a security policy for SSL client certificates. |
174 static OSStatus CreateSSLClientPolicy(SecPolicyRef* outPolicy); | 235 static OSStatus CreateSSLClientPolicy(SecPolicyRef* outPolicy); |
175 | 236 |
176 // Adds all available SSL client identity certs to the given vector. | 237 // Adds all available SSL client identity certs to the given vector. |
177 // |server_domain| is a hint for which domain the cert is to be sent to | 238 // |server_domain| is a hint for which domain the cert is to be sent to |
178 // (a cert previously specified as the default for that domain will be given | 239 // (a cert previously specified as the default for that domain will be given |
179 // precedence and returned first in the output vector.) | 240 // 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. | |
182 static bool GetSSLClientCertificates( | 241 static bool GetSSLClientCertificates( |
183 const std::string& server_domain, | 242 const std::string& server_domain, |
184 const std::vector<CertPrincipal>& valid_issuers, | |
185 std::vector<scoped_refptr<X509Certificate> >* certs); | 243 std::vector<scoped_refptr<X509Certificate> >* certs); |
186 | 244 |
187 // Creates the chain of certs to use for this client identity cert. | 245 // Creates the chain of certs to use for this client identity cert. |
188 CFArrayRef CreateClientCertificateChain() const; | 246 CFArrayRef CreateClientCertificateChain() const; |
189 #endif | 247 #endif |
190 | 248 |
191 // Verifies the certificate against the given hostname. Returns OK if | 249 // Verifies the certificate against the given hostname. Returns OK if |
192 // successful or an error code upon failure. | 250 // successful or an error code upon failure. |
193 // | 251 // |
194 // The |*verify_result| structure, including the |verify_result->cert_status| | 252 // The |*verify_result| structure, including the |verify_result->cert_status| |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 | 353 |
296 // Where the certificate comes from. | 354 // Where the certificate comes from. |
297 Source source_; | 355 Source source_; |
298 | 356 |
299 DISALLOW_COPY_AND_ASSIGN(X509Certificate); | 357 DISALLOW_COPY_AND_ASSIGN(X509Certificate); |
300 }; | 358 }; |
301 | 359 |
302 } // namespace net | 360 } // namespace net |
303 | 361 |
304 #endif // NET_BASE_X509_CERTIFICATE_H_ | 362 #endif // NET_BASE_X509_CERTIFICATE_H_ |
OLD | NEW |