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

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
« no previous file with comments | « net/base/transport_security_state_unittest.cc ('k') | net/base/x509_cert_types.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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];
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];
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) :
76 tag(tag)
77 { }
hshi1 2012/08/17 22:34:52 nit: I believe the prevalent code style is "{}" wi
palmer 2012/08/17 23:50:31 Done.
78
79 HashValue() :
80 tag(HASH_VALUE_SHA1)
81 { }
hshi1 2012/08/17 22:34:52 nit: same as comment above.
palmer 2012/08/17 23:50:31 Done.
palmer 2012/08/17 23:50:31 Done.
82
83 bool Equals(const HashValue& other) const;
84 size_t size() const;
85 unsigned char* data();
86 const unsigned char* data() const;
87 const char* label() const;
88
89 HashValueTag tag;
90
91 private:
92 union {
93 SHA1HashValue sha1;
94 SHA256HashValue sha256;
95 } fingerprint;
96 };
97
98 class NET_EXPORT HashValueLessThan {
99 public:
100 bool operator() (const HashValue& lhs,
101 const HashValue& rhs) const {
102 size_t lhs_size = lhs.size();
103 size_t rhs_size = rhs.size();
104
105 if (lhs_size != rhs_size)
106 return lhs_size < rhs_size;
107
108 return memcmp(lhs.data(), rhs.data(), std::min(lhs_size, rhs_size)) < 0;
109 }
110 };
111
112 typedef std::vector<HashValue> HashValueVector;
113
54 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted 114 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted
55 // array of SHA1 hashes. 115 // array of SHA1 hashes.
56 bool NET_EXPORT IsSHA1HashInSortedArray(const SHA1Fingerprint& hash, 116 bool NET_EXPORT IsSHA1HashInSortedArray(const SHA1HashValue& hash,
57 const uint8* array, 117 const uint8* array,
58 size_t array_byte_len); 118 size_t array_byte_len);
59 119
60 // CertPrincipal represents the issuer or subject field of an X.509 certificate. 120 // CertPrincipal represents the issuer or subject field of an X.509 certificate.
61 struct NET_EXPORT CertPrincipal { 121 struct NET_EXPORT CertPrincipal {
62 CertPrincipal(); 122 CertPrincipal();
63 explicit CertPrincipal(const std::string& name); 123 explicit CertPrincipal(const std::string& name);
64 ~CertPrincipal(); 124 ~CertPrincipal();
65 125
66 #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) 126 #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); 183 void Deny(X509Certificate* cert);
124 184
125 // Returns true if this policy has allowed at least one certificate. 185 // Returns true if this policy has allowed at least one certificate.
126 bool HasAllowedCert() const; 186 bool HasAllowedCert() const;
127 187
128 // Returns true if this policy has denied at least one certificate. 188 // Returns true if this policy has denied at least one certificate.
129 bool HasDeniedCert() const; 189 bool HasDeniedCert() const;
130 190
131 private: 191 private:
132 // The set of fingerprints of allowed certificates. 192 // The set of fingerprints of allowed certificates.
133 std::set<SHA1Fingerprint, SHA1FingerprintLessThan> allowed_; 193 std::set<SHA1HashValue, SHA1HashValueLessThan> allowed_;
134 194
135 // The set of fingerprints of denied certificates. 195 // The set of fingerprints of denied certificates.
136 std::set<SHA1Fingerprint, SHA1FingerprintLessThan> denied_; 196 std::set<SHA1HashValue, SHA1HashValueLessThan> denied_;
137 }; 197 };
138 198
139 #if defined(OS_MACOSX) && !defined(OS_IOS) 199 #if defined(OS_MACOSX) && !defined(OS_IOS)
140 // Compares two OIDs by value. 200 // Compares two OIDs by value.
141 inline bool CSSMOIDEqual(const CSSM_OID* oid1, const CSSM_OID* oid2) { 201 inline bool CSSMOIDEqual(const CSSM_OID* oid1, const CSSM_OID* oid2) {
142 return oid1->Length == oid2->Length && 202 return oid1->Length == oid2->Length &&
143 (memcmp(oid1->Data, oid2->Data, oid1->Length) == 0); 203 (memcmp(oid1->Data, oid2->Data, oid1->Length) == 0);
144 } 204 }
145 #endif 205 #endif
146 206
(...skipping 10 matching lines...) Expand all
157 // Attempts to parse |raw_date|, an ASN.1 date/time string encoded as 217 // 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 218 // |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 219 // specified, or if parsing fails, returns false, and |*time| will not be
160 // updated. 220 // updated.
161 bool ParseCertificateDate(const base::StringPiece& raw_date, 221 bool ParseCertificateDate(const base::StringPiece& raw_date,
162 CertDateFormat format, 222 CertDateFormat format,
163 base::Time* time); 223 base::Time* time);
164 } // namespace net 224 } // namespace net
165 225
166 #endif // NET_BASE_X509_CERT_TYPES_H_ 226 #endif // NET_BASE_X509_CERT_TYPES_H_
OLDNEW
« no previous file with comments | « net/base/transport_security_state_unittest.cc ('k') | net/base/x509_cert_types.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698