Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: net/base/x509_cert_types.h

Issue 10826257: Implement SHA-256 fingerprint support (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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];
hshi1 2012/08/11 01:05:40 Nit: would it be better to use base::kSHA1Length i
Ryan Sleevi 2012/08/11 01:39:55 I'm not sure how much it will help readability, an
palmer 2012/08/14 19:40:42 I'll leave it as-is then.
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,
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];
hshi1 2012/08/11 01:05:40 Nit: would it be better to use crypto::kSHA256Leng
palmer 2012/08/14 19:40:42 Leaving as-is, for the reason given above.
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 struct NET_EXPORT HashValue {
74 bool Equals(const HashValue& other) const {
75 if (tag != other.tag)
76 return false;
77 switch (tag) {
78 case HASH_VALUE_SHA1:
79 return fingerprint.sha1.Equals(other.fingerprint.sha1);
80 break;
81 case HASH_VALUE_SHA256:
82 return fingerprint.sha256.Equals(other.fingerprint.sha256);
83 break;
84 default:
85 NOTREACHED() << "Unknown HashValueTag " << tag;
86 return false;
87 }
Ryan Sleevi 2012/08/11 01:39:55 style nit: At this point, the complexity is such t
palmer 2012/08/14 19:40:42 Done.
88 }
89
90 size_t size() const {
91 switch (tag) {
92 case HASH_VALUE_SHA1:
93 return sizeof(fingerprint.sha1.data);
94 break;
95 case HASH_VALUE_SHA256:
96 return sizeof(fingerprint.sha256.data);
97 break;
98 default:
99 NOTREACHED() << "Unknown HashValueTag " << tag;
100 // Although this is NOTREACHED, this function is inlined and its
101 // return value is passed to memset as the length argument. This may
102 // result in what appears (in some stages of compilation) to be a
103 // call to to memset with a length argument of 0, which results in a
104 // warning. Therefore, we return a dummy value here.
105 return sizeof(fingerprint.sha1.data);
106 }
107 }
108
109 unsigned char* data() {
110 switch (tag) {
Ryan Sleevi 2012/08/11 01:39:55 Rather than duping the code, can you not return c
palmer 2012/08/14 19:40:42 Done.
111 case HASH_VALUE_SHA1:
112 return fingerprint.sha1.data;
113 break;
114 case HASH_VALUE_SHA256:
115 return fingerprint.sha256.data;
116 break;
117 default:
118 NOTREACHED() << "Unknown HashValueTag " << tag;
119 return NULL;
120 }
121 }
122
123 const unsigned char* data() const {
124 switch (tag) {
125 case HASH_VALUE_SHA1:
126 return fingerprint.sha1.data;
127 break;
128 case HASH_VALUE_SHA256:
129 return fingerprint.sha256.data;
130 break;
131 default:
132 NOTREACHED() << "Unknown HashValueTag " << tag;
133 return NULL;
134 }
135 }
136
137 HashValueTag tag;
138
139 union {
140 SHA1HashValue sha1;
141 SHA256HashValue sha256;
142 } fingerprint;
143 };
144
145 class NET_EXPORT HashValueLessThan {
146 public:
147 bool operator() (const HashValue& lhs,
148 const HashValue& rhs) const {
149 size_t lhs_size = lhs.size();
150 size_t rhs_size = rhs.size();
151 int r = memcmp(lhs.data(), rhs.data(), std::min(lhs_size, rhs_size));
152
153 if (r == 0 && lhs_size != rhs_size)
154 return lhs_size < rhs_size;
155 return r < 0;
Ryan Sleevi 2012/08/11 01:39:55 Seems like this can be re-ordered size_t lhs_size
palmer 2012/08/14 19:40:42 Done.
156 }
157 };
158
159 typedef std::vector<HashValue> HashValueVector;
160
54 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted 161 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted
55 // array of SHA1 hashes. 162 // array of SHA1 hashes.
56 bool NET_EXPORT IsSHA1HashInSortedArray(const SHA1Fingerprint& hash, 163 bool NET_EXPORT IsSHA1HashInSortedArray(const SHA1HashValue& hash,
57 const uint8* array, 164 const uint8* array,
58 size_t array_byte_len); 165 size_t array_byte_len);
59 166
60 // CertPrincipal represents the issuer or subject field of an X.509 certificate. 167 // CertPrincipal represents the issuer or subject field of an X.509 certificate.
61 struct NET_EXPORT CertPrincipal { 168 struct NET_EXPORT CertPrincipal {
62 CertPrincipal(); 169 CertPrincipal();
63 explicit CertPrincipal(const std::string& name); 170 explicit CertPrincipal(const std::string& name);
64 ~CertPrincipal(); 171 ~CertPrincipal();
65 172
66 #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) 173 #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN)
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 void Deny(X509Certificate* cert); 230 void Deny(X509Certificate* cert);
124 231
125 // Returns true if this policy has allowed at least one certificate. 232 // Returns true if this policy has allowed at least one certificate.
126 bool HasAllowedCert() const; 233 bool HasAllowedCert() const;
127 234
128 // Returns true if this policy has denied at least one certificate. 235 // Returns true if this policy has denied at least one certificate.
129 bool HasDeniedCert() const; 236 bool HasDeniedCert() const;
130 237
131 private: 238 private:
132 // The set of fingerprints of allowed certificates. 239 // The set of fingerprints of allowed certificates.
133 std::set<SHA1Fingerprint, SHA1FingerprintLessThan> allowed_; 240 std::set<SHA1HashValue, SHA1HashValueLessThan> allowed_;
134 241
135 // The set of fingerprints of denied certificates. 242 // The set of fingerprints of denied certificates.
136 std::set<SHA1Fingerprint, SHA1FingerprintLessThan> denied_; 243 std::set<SHA1HashValue, SHA1HashValueLessThan> denied_;
137 }; 244 };
138 245
139 #if defined(OS_MACOSX) && !defined(OS_IOS) 246 #if defined(OS_MACOSX) && !defined(OS_IOS)
140 // Compares two OIDs by value. 247 // Compares two OIDs by value.
141 inline bool CSSMOIDEqual(const CSSM_OID* oid1, const CSSM_OID* oid2) { 248 inline bool CSSMOIDEqual(const CSSM_OID* oid1, const CSSM_OID* oid2) {
142 return oid1->Length == oid2->Length && 249 return oid1->Length == oid2->Length &&
143 (memcmp(oid1->Data, oid2->Data, oid1->Length) == 0); 250 (memcmp(oid1->Data, oid2->Data, oid1->Length) == 0);
144 } 251 }
145 #endif 252 #endif
146 253
(...skipping 10 matching lines...) Expand all
157 // Attempts to parse |raw_date|, an ASN.1 date/time string encoded as 264 // 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 265 // |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 266 // specified, or if parsing fails, returns false, and |*time| will not be
160 // updated. 267 // updated.
161 bool ParseCertificateDate(const base::StringPiece& raw_date, 268 bool ParseCertificateDate(const base::StringPiece& raw_date,
162 CertDateFormat format, 269 CertDateFormat format,
163 base::Time* time); 270 base::Time* time);
164 } // namespace net 271 } // namespace net
165 272
166 #endif // NET_BASE_X509_CERT_TYPES_H_ 273 #endif // NET_BASE_X509_CERT_TYPES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698