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