Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_X509_CERT_TYPES_H_ | 5 #ifndef NET_BASE_X509_CERT_TYPES_H_ |
| 6 #define NET_BASE_X509_CERT_TYPES_H_ | 6 #define NET_BASE_X509_CERT_TYPES_H_ |
| 7 | 7 |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | |
| 10 #include <set> | 11 #include <set> |
| 11 #include <string> | 12 #include <string> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 15 #include "base/logging.h" | |
| 14 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
| 15 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 16 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
| 17 | 19 |
| 18 #if defined(OS_MACOSX) && !defined(OS_IOS) | 20 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 19 #include <Security/x509defs.h> | 21 #include <Security/x509defs.h> |
| 20 #endif | 22 #endif |
| 21 | 23 |
| 22 namespace base { | 24 namespace base { |
| 23 class Time; | 25 class Time; |
| 24 } // namespace base | 26 } // namespace base |
| 25 | 27 |
| 26 namespace net { | 28 namespace net { |
| 27 | 29 |
| 28 class X509Certificate; | 30 class X509Certificate; |
| 29 | 31 |
| 30 // SHA-1 fingerprint (160 bits) of a certificate. | 32 // SHA-1 fingerprint (160 bits) of a certificate. |
| 31 struct NET_EXPORT SHA1Fingerprint { | 33 struct NET_EXPORT SHA1HashValue { |
| 32 bool Equals(const SHA1Fingerprint& other) const { | 34 bool Equals(const SHA1HashValue& other) const { |
| 33 return memcmp(data, other.data, sizeof(data)) == 0; | 35 return memcmp(data, other.data, sizeof(data)) == 0; |
| 34 } | 36 } |
| 35 | 37 |
| 36 unsigned char data[20]; | 38 unsigned char data[20]; |
| 37 }; | 39 }; |
| 38 | 40 |
| 39 // In the future there will be a generic Fingerprint type, with at least two | 41 class NET_EXPORT SHA1HashValueLessThan { |
| 40 // implementations: SHA1 and SHA256. See http://crbug.com/117914. Until that | |
| 41 // work is done (in a separate patch) this typedef bridges the gap. | |
| 42 typedef SHA1Fingerprint Fingerprint; | |
| 43 | |
| 44 typedef std::vector<Fingerprint> FingerprintVector; | |
| 45 | |
| 46 class NET_EXPORT SHA1FingerprintLessThan { | |
| 47 public: | 42 public: |
| 48 bool operator() (const SHA1Fingerprint& lhs, | 43 bool operator() (const SHA1HashValue& lhs, |
|
Ryan Sleevi
2012/08/23 22:48:20
nit: "operator() (" -> "operator()("
See also lin
palmer
2012/08/29 00:01:24
Done.
| |
| 49 const SHA1Fingerprint& rhs) const { | 44 const SHA1HashValue& rhs) const { |
| 50 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; | 45 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; |
| 51 } | 46 } |
| 52 }; | 47 }; |
| 53 | 48 |
| 49 struct NET_EXPORT SHA256HashValue { | |
| 50 bool Equals(const SHA256HashValue& other) const { | |
| 51 return memcmp(data, other.data, sizeof(data)) == 0; | |
| 52 } | |
| 53 | |
| 54 unsigned char data[32]; | |
| 55 }; | |
| 56 | |
| 57 class NET_EXPORT SHA256HashValueLessThan { | |
| 58 public: | |
| 59 bool operator() (const SHA256HashValue& lhs, | |
| 60 const SHA256HashValue& rhs) const { | |
| 61 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; | |
| 62 } | |
| 63 }; | |
| 64 | |
| 65 enum HashValueTag { | |
| 66 HASH_VALUE_SHA1, | |
| 67 HASH_VALUE_SHA256, | |
| 68 | |
| 69 // This must always be last. | |
| 70 HASH_VALUE_TAGS_COUNT | |
| 71 }; | |
| 72 | |
| 73 class NET_EXPORT HashValue { | |
| 74 public: | |
| 75 explicit HashValue(HashValueTag tag) : tag(tag) {} | |
| 76 HashValue() : tag(HASH_VALUE_SHA1) {} | |
| 77 | |
| 78 bool Equals(const HashValue& other) const; | |
| 79 size_t size() const; | |
| 80 unsigned char* data(); | |
| 81 const unsigned char* data() const; | |
| 82 const char* label() const; | |
| 83 | |
| 84 HashValueTag tag; | |
| 85 | |
| 86 private: | |
| 87 union { | |
| 88 SHA1HashValue sha1; | |
| 89 SHA256HashValue sha256; | |
| 90 } fingerprint; | |
| 91 }; | |
| 92 | |
| 93 class NET_EXPORT HashValueLessThan { | |
| 94 public: | |
| 95 bool operator() (const HashValue& lhs, | |
| 96 const HashValue& rhs) const { | |
| 97 size_t lhs_size = lhs.size(); | |
| 98 size_t rhs_size = rhs.size(); | |
| 99 | |
| 100 if (lhs_size != rhs_size) | |
| 101 return lhs_size < rhs_size; | |
| 102 | |
| 103 return memcmp(lhs.data(), rhs.data(), std::min(lhs_size, rhs_size)) < 0; | |
|
Ryan Sleevi
2012/08/23 22:48:20
nit: remove use of std::min, you've already guaran
palmer
2012/08/29 00:01:24
Done.
| |
| 104 } | |
| 105 }; | |
| 106 | |
| 107 typedef std::vector<HashValue> HashValueVector; | |
| 108 | |
| 54 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted | 109 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted |
| 55 // array of SHA1 hashes. | 110 // array of SHA1 hashes. |
| 56 bool NET_EXPORT IsSHA1HashInSortedArray(const SHA1Fingerprint& hash, | 111 bool NET_EXPORT IsSHA1HashInSortedArray(const SHA1HashValue& hash, |
| 57 const uint8* array, | 112 const uint8* array, |
| 58 size_t array_byte_len); | 113 size_t array_byte_len); |
| 59 | 114 |
| 60 // CertPrincipal represents the issuer or subject field of an X.509 certificate. | 115 // CertPrincipal represents the issuer or subject field of an X.509 certificate. |
| 61 struct NET_EXPORT CertPrincipal { | 116 struct NET_EXPORT CertPrincipal { |
| 62 CertPrincipal(); | 117 CertPrincipal(); |
| 63 explicit CertPrincipal(const std::string& name); | 118 explicit CertPrincipal(const std::string& name); |
| 64 ~CertPrincipal(); | 119 ~CertPrincipal(); |
| 65 | 120 |
| 66 #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) | 121 #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 void Deny(X509Certificate* cert); | 178 void Deny(X509Certificate* cert); |
| 124 | 179 |
| 125 // Returns true if this policy has allowed at least one certificate. | 180 // Returns true if this policy has allowed at least one certificate. |
| 126 bool HasAllowedCert() const; | 181 bool HasAllowedCert() const; |
| 127 | 182 |
| 128 // Returns true if this policy has denied at least one certificate. | 183 // Returns true if this policy has denied at least one certificate. |
| 129 bool HasDeniedCert() const; | 184 bool HasDeniedCert() const; |
| 130 | 185 |
| 131 private: | 186 private: |
| 132 // The set of fingerprints of allowed certificates. | 187 // The set of fingerprints of allowed certificates. |
| 133 std::set<SHA1Fingerprint, SHA1FingerprintLessThan> allowed_; | 188 std::set<SHA1HashValue, SHA1HashValueLessThan> allowed_; |
| 134 | 189 |
| 135 // The set of fingerprints of denied certificates. | 190 // The set of fingerprints of denied certificates. |
| 136 std::set<SHA1Fingerprint, SHA1FingerprintLessThan> denied_; | 191 std::set<SHA1HashValue, SHA1HashValueLessThan> denied_; |
| 137 }; | 192 }; |
| 138 | 193 |
| 139 #if defined(OS_MACOSX) && !defined(OS_IOS) | 194 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 140 // Compares two OIDs by value. | 195 // Compares two OIDs by value. |
| 141 inline bool CSSMOIDEqual(const CSSM_OID* oid1, const CSSM_OID* oid2) { | 196 inline bool CSSMOIDEqual(const CSSM_OID* oid1, const CSSM_OID* oid2) { |
| 142 return oid1->Length == oid2->Length && | 197 return oid1->Length == oid2->Length && |
| 143 (memcmp(oid1->Data, oid2->Data, oid1->Length) == 0); | 198 (memcmp(oid1->Data, oid2->Data, oid1->Length) == 0); |
| 144 } | 199 } |
| 145 #endif | 200 #endif |
| 146 | 201 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 157 // Attempts to parse |raw_date|, an ASN.1 date/time string encoded as | 212 // Attempts to parse |raw_date|, an ASN.1 date/time string encoded as |
| 158 // |format|, and writes the result into |*time|. If an invalid date is | 213 // |format|, and writes the result into |*time|. If an invalid date is |
| 159 // specified, or if parsing fails, returns false, and |*time| will not be | 214 // specified, or if parsing fails, returns false, and |*time| will not be |
| 160 // updated. | 215 // updated. |
| 161 bool ParseCertificateDate(const base::StringPiece& raw_date, | 216 bool ParseCertificateDate(const base::StringPiece& raw_date, |
| 162 CertDateFormat format, | 217 CertDateFormat format, |
| 163 base::Time* time); | 218 base::Time* time); |
| 164 } // namespace net | 219 } // namespace net |
| 165 | 220 |
| 166 #endif // NET_BASE_X509_CERT_TYPES_H_ | 221 #endif // NET_BASE_X509_CERT_TYPES_H_ |
| OLD | NEW |