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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
| 11 #include <algorithm> |
11 #include <set> | 12 #include <set> |
12 #include <string> | 13 #include <string> |
13 #include <vector> | 14 #include <vector> |
14 | 15 |
| 16 #include "base/logging.h" |
15 #include "base/string_piece.h" | 17 #include "base/string_piece.h" |
16 #include "build/build_config.h" | 18 #include "build/build_config.h" |
17 #include "net/base/net_export.h" | 19 #include "net/base/net_export.h" |
18 | 20 |
19 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
20 #include <Security/x509defs.h> | 22 #include <Security/x509defs.h> |
21 #endif | 23 #endif |
22 | 24 |
23 namespace base { | 25 namespace base { |
24 class Time; | 26 class Time; |
25 } // namespace base | 27 } // namespace base |
26 | 28 |
27 namespace net { | 29 namespace net { |
28 | 30 |
29 class X509Certificate; | 31 class X509Certificate; |
30 | 32 |
31 // SHA-1 fingerprint (160 bits) of a certificate. | 33 // SHA-1 fingerprint (160 bits) of a certificate. |
32 struct NET_EXPORT SHA1Fingerprint { | 34 struct NET_EXPORT SHA1Fingerprint { |
33 bool Equals(const SHA1Fingerprint& other) const { | 35 bool Equals(const SHA1Fingerprint& other) const { |
34 return memcmp(data, other.data, sizeof(data)) == 0; | 36 return memcmp(data, other.data, sizeof(data)) == 0; |
35 } | 37 } |
36 | 38 |
37 unsigned char data[20]; | 39 unsigned char data[20]; |
38 }; | 40 }; |
39 | 41 |
40 // In the future there will be a generic Fingerprint type, with at least two | |
41 // implementations: SHA1 and SHA256. See http://crbug.com/117914. Until that | |
42 // work is done (in a separate patch) this typedef bridges the gap. | |
43 typedef SHA1Fingerprint Fingerprint; | |
44 | |
45 typedef std::vector<Fingerprint> FingerprintVector; | |
46 | |
47 class NET_EXPORT SHA1FingerprintLessThan { | 42 class NET_EXPORT SHA1FingerprintLessThan { |
48 public: | 43 public: |
49 bool operator() (const SHA1Fingerprint& lhs, | 44 bool operator() (const SHA1Fingerprint& lhs, |
50 const SHA1Fingerprint& rhs) const { | 45 const SHA1Fingerprint& rhs) const { |
51 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; | 46 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; |
52 } | 47 } |
53 }; | 48 }; |
54 | 49 |
| 50 struct NET_EXPORT SHA256Fingerprint { |
| 51 bool Equals(const SHA256Fingerprint& other) const { |
| 52 return memcmp(data, other.data, sizeof(data)) == 0; |
| 53 } |
| 54 |
| 55 unsigned char data[32]; |
| 56 }; |
| 57 |
| 58 class NET_EXPORT SHA256FingerprintLessThan { |
| 59 public: |
| 60 bool operator() (const SHA256Fingerprint& lhs, |
| 61 const SHA256Fingerprint& rhs) const { |
| 62 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0; |
| 63 } |
| 64 }; |
| 65 |
| 66 enum FingerprintTag { |
| 67 FINGERPRINT_SHA1, |
| 68 FINGERPRINT_SHA256, |
| 69 }; |
| 70 |
| 71 struct NET_EXPORT Fingerprint { |
| 72 bool Equals(const Fingerprint& other) const { |
| 73 if (tag != other.tag) |
| 74 return false; |
| 75 switch (tag) { |
| 76 case FINGERPRINT_SHA1: |
| 77 return fingerprint.sha1.Equals(other.fingerprint.sha1); |
| 78 break; |
| 79 case FINGERPRINT_SHA256: |
| 80 return fingerprint.sha256.Equals(other.fingerprint.sha256); |
| 81 break; |
| 82 default: |
| 83 DCHECK(false) << "Unknown FingerprintTag " << tag; |
| 84 return false; |
| 85 } |
| 86 } |
| 87 |
| 88 size_t size() const { |
| 89 switch (tag) { |
| 90 case FINGERPRINT_SHA1: |
| 91 return sizeof(fingerprint.sha1.data); |
| 92 break; |
| 93 case FINGERPRINT_SHA256: |
| 94 return sizeof(fingerprint.sha256.data); |
| 95 break; |
| 96 default: |
| 97 DCHECK(false) << "Unknown FingerprintTag " << tag; |
| 98 return false; |
| 99 } |
| 100 } |
| 101 |
| 102 unsigned char* data() const { |
| 103 switch (tag) { |
| 104 case FINGERPRINT_SHA1: |
| 105 return const_cast<unsigned char*>(fingerprint.sha1.data); |
| 106 break; |
| 107 case FINGERPRINT_SHA256: |
| 108 return const_cast<unsigned char*>(fingerprint.sha256.data); |
| 109 break; |
| 110 default: |
| 111 DCHECK(false) << "Unknown FingerprintTag " << tag; |
| 112 return NULL; |
| 113 } |
| 114 } |
| 115 |
| 116 FingerprintTag tag; |
| 117 |
| 118 union { |
| 119 SHA1Fingerprint sha1; |
| 120 SHA256Fingerprint sha256; |
| 121 } fingerprint; |
| 122 }; |
| 123 |
| 124 class NET_EXPORT FingerprintLessThan { |
| 125 public: |
| 126 bool operator() (const Fingerprint& lhs, |
| 127 const Fingerprint& rhs) const { |
| 128 size_t lhs_size = lhs.size(); |
| 129 size_t rhs_size = rhs.size(); |
| 130 int r = memcmp(lhs.data(), rhs.data(), std::min(lhs_size, rhs_size)); |
| 131 |
| 132 if (r == 0 && lhs_size != rhs_size) |
| 133 return lhs_size < rhs_size; |
| 134 return r < 0; |
| 135 } |
| 136 }; |
| 137 |
| 138 typedef std::vector<Fingerprint> FingerprintVector; |
| 139 |
55 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted | 140 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted |
56 // array of SHA1 hashes. | 141 // array of SHA1 hashes. |
57 bool NET_EXPORT IsSHA1HashInSortedArray(const SHA1Fingerprint& hash, | 142 bool NET_EXPORT IsSHA1HashInSortedArray(const SHA1Fingerprint& hash, |
58 const uint8* array, | 143 const uint8* array, |
59 size_t array_byte_len); | 144 size_t array_byte_len); |
60 | 145 |
61 // CertPrincipal represents the issuer or subject field of an X.509 certificate. | 146 // CertPrincipal represents the issuer or subject field of an X.509 certificate. |
62 struct NET_EXPORT CertPrincipal { | 147 struct NET_EXPORT CertPrincipal { |
63 CertPrincipal(); | 148 CertPrincipal(); |
64 explicit CertPrincipal(const std::string& name); | 149 explicit CertPrincipal(const std::string& name); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 // Attempts to parse |raw_date|, an ASN.1 date/time string encoded as | 243 // Attempts to parse |raw_date|, an ASN.1 date/time string encoded as |
159 // |format|, and writes the result into |*time|. If an invalid date is | 244 // |format|, and writes the result into |*time|. If an invalid date is |
160 // specified, or if parsing fails, returns false, and |*time| will not be | 245 // specified, or if parsing fails, returns false, and |*time| will not be |
161 // updated. | 246 // updated. |
162 bool ParseCertificateDate(const base::StringPiece& raw_date, | 247 bool ParseCertificateDate(const base::StringPiece& raw_date, |
163 CertDateFormat format, | 248 CertDateFormat format, |
164 base::Time* time); | 249 base::Time* time); |
165 } // namespace net | 250 } // namespace net |
166 | 251 |
167 #endif // NET_BASE_X509_CERT_TYPES_H_ | 252 #endif // NET_BASE_X509_CERT_TYPES_H_ |
OLD | NEW |