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 #include "net/base/x509_cert_types.h" | 5 #include "net/base/x509_cert_types.h" |
6 | 6 |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 #include <cstring> | 8 #include <cstring> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/sha1.h" | |
12 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
13 #include "base/string_piece.h" | 12 #include "base/string_piece.h" |
14 #include "base/time.h" | 13 #include "base/time.h" |
15 #include "net/base/x509_certificate.h" | 14 #include "net/base/x509_certificate.h" |
16 | 15 |
17 namespace net { | 16 namespace net { |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 // Helper for ParseCertificateDate. |*field| must contain at least | 20 // Helper for ParseCertificateDate. |*field| must contain at least |
22 // |field_len| characters. |*field| will be advanced by |field_len| on exit. | 21 // |field_len| characters. |*field| will be advanced by |field_len| on exit. |
23 // |*ok| is set to false if there is an error in parsing the number, but left | 22 // |*ok| is set to false if there is an error in parsing the number, but left |
24 // untouched otherwise. Returns the parsed integer. | 23 // untouched otherwise. Returns the parsed integer. |
25 int ParseIntAndAdvance(const char** field, size_t field_len, bool* ok) { | 24 int ParseIntAndAdvance(const char** field, size_t field_len, bool* ok) { |
26 int result = 0; | 25 int result = 0; |
27 *ok &= base::StringToInt(base::StringPiece(*field, field_len), &result); | 26 *ok &= base::StringToInt(base::StringPiece(*field, field_len), &result); |
28 *field += field_len; | 27 *field += field_len; |
29 return result; | 28 return result; |
30 } | 29 } |
31 | 30 |
32 // CompareSHA1Hashes is a helper function for using bsearch() with an array of | |
33 // SHA1 hashes. | |
34 int CompareSHA1Hashes(const void* a, const void* b) { | |
35 return memcmp(a, b, base::kSHA1Length); | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 // static | |
41 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, | |
42 const uint8* array, | |
43 size_t array_byte_len) { | |
44 DCHECK_EQ(0u, array_byte_len % base::kSHA1Length); | |
45 const size_t arraylen = array_byte_len / base::kSHA1Length; | |
46 return NULL != bsearch(hash.data, array, arraylen, base::kSHA1Length, | |
47 CompareSHA1Hashes); | |
48 } | 31 } |
49 | 32 |
50 CertPrincipal::CertPrincipal() { | 33 CertPrincipal::CertPrincipal() { |
51 } | 34 } |
52 | 35 |
53 CertPrincipal::CertPrincipal(const std::string& name) : common_name(name) {} | 36 CertPrincipal::CertPrincipal(const std::string& name) : common_name(name) {} |
54 | 37 |
55 CertPrincipal::~CertPrincipal() { | 38 CertPrincipal::~CertPrincipal() { |
56 } | 39 } |
57 | 40 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 119 |
137 valid &= exploded.HasValidValues(); | 120 valid &= exploded.HasValidValues(); |
138 | 121 |
139 if (!valid) | 122 if (!valid) |
140 return false; | 123 return false; |
141 | 124 |
142 *time = base::Time::FromUTCExploded(exploded); | 125 *time = base::Time::FromUTCExploded(exploded); |
143 return true; | 126 return true; |
144 } | 127 } |
145 | 128 |
146 bool HashValue::Equals(const HashValue& other) const { | |
147 if (tag != other.tag) | |
148 return false; | |
149 switch (tag) { | |
150 case HASH_VALUE_SHA1: | |
151 return fingerprint.sha1.Equals(other.fingerprint.sha1); | |
152 case HASH_VALUE_SHA256: | |
153 return fingerprint.sha256.Equals(other.fingerprint.sha256); | |
154 default: | |
155 NOTREACHED() << "Unknown HashValueTag " << tag; | |
156 return false; | |
157 } | |
158 } | |
159 | |
160 size_t HashValue::size() const { | |
161 switch (tag) { | |
162 case HASH_VALUE_SHA1: | |
163 return sizeof(fingerprint.sha1.data); | |
164 case HASH_VALUE_SHA256: | |
165 return sizeof(fingerprint.sha256.data); | |
166 default: | |
167 NOTREACHED() << "Unknown HashValueTag " << tag; | |
168 // Although this is NOTREACHED, this function might be inlined and its | |
169 // return value can be passed to memset as the length argument. If we | |
170 // returned 0 here, it might result in what appears (in some stages of | |
171 // compilation) to be a call to to memset with a length argument of 0, | |
172 // which results in a warning. Therefore, we return a dummy value | |
173 // here. | |
174 return sizeof(fingerprint.sha1.data); | |
175 } | |
176 } | |
177 | |
178 unsigned char* HashValue::data() { | |
179 return const_cast<unsigned char*>(const_cast<const HashValue*>(this)->data()); | |
180 } | |
181 | |
182 const unsigned char* HashValue::data() const { | |
183 switch (tag) { | |
184 case HASH_VALUE_SHA1: | |
185 return fingerprint.sha1.data; | |
186 case HASH_VALUE_SHA256: | |
187 return fingerprint.sha256.data; | |
188 default: | |
189 NOTREACHED() << "Unknown HashValueTag " << tag; | |
190 return NULL; | |
191 } | |
192 } | |
193 | |
194 } // namespace net | 129 } // namespace net |
OLD | NEW |