| 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/base64.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/sha1.h" | 12 #include "base/sha1.h" |
| 12 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 13 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
| 15 #include "base/string_split.h" |
| 16 #include "base/string_util.h" |
| 14 #include "base/time.h" | 17 #include "base/time.h" |
| 15 #include "net/base/x509_certificate.h" | 18 #include "net/base/x509_certificate.h" |
| 16 | 19 |
| 17 namespace net { | 20 namespace net { |
| 18 | 21 |
| 19 namespace { | 22 namespace { |
| 20 | 23 |
| 21 // Helper for ParseCertificateDate. |*field| must contain at least | 24 // Helper for ParseCertificateDate. |*field| must contain at least |
| 22 // |field_len| characters. |*field| will be advanced by |field_len| on exit. | 25 // |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 | 26 // |*ok| is set to false if there is an error in parsing the number, but left |
| 24 // untouched otherwise. Returns the parsed integer. | 27 // untouched otherwise. Returns the parsed integer. |
| 25 int ParseIntAndAdvance(const char** field, size_t field_len, bool* ok) { | 28 int ParseIntAndAdvance(const char** field, size_t field_len, bool* ok) { |
| 26 int result = 0; | 29 int result = 0; |
| 27 *ok &= base::StringToInt(base::StringPiece(*field, field_len), &result); | 30 *ok &= base::StringToInt(base::StringPiece(*field, field_len), &result); |
| 28 *field += field_len; | 31 *field += field_len; |
| 29 return result; | 32 return result; |
| 30 } | 33 } |
| 31 | 34 |
| 32 // CompareSHA1Hashes is a helper function for using bsearch() with an array of | 35 // CompareSHA1Hashes is a helper function for using bsearch() with an array of |
| 33 // SHA1 hashes. | 36 // SHA1 hashes. |
| 34 int CompareSHA1Hashes(const void* a, const void* b) { | 37 int CompareSHA1Hashes(const void* a, const void* b) { |
| 35 return memcmp(a, b, base::kSHA1Length); | 38 return memcmp(a, b, base::kSHA1Length); |
| 36 } | 39 } |
| 37 | 40 |
| 38 } // namespace | 41 } // namespace |
| 39 | 42 |
| 40 // static | |
| 41 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, | 43 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash, |
| 42 const uint8* array, | 44 const uint8* array, |
| 43 size_t array_byte_len) { | 45 size_t array_byte_len) { |
| 44 DCHECK_EQ(0u, array_byte_len % base::kSHA1Length); | 46 DCHECK_EQ(0u, array_byte_len % base::kSHA1Length); |
| 45 const size_t arraylen = array_byte_len / base::kSHA1Length; | 47 const size_t arraylen = array_byte_len / base::kSHA1Length; |
| 46 return NULL != bsearch(hash.data, array, arraylen, base::kSHA1Length, | 48 return NULL != bsearch(hash.data, array, arraylen, base::kSHA1Length, |
| 47 CompareSHA1Hashes); | 49 CompareSHA1Hashes); |
| 48 } | 50 } |
| 49 | 51 |
| 52 bool HashesIntersect(const HashValueVector& a, |
| 53 const HashValueVector& b) { |
| 54 for (HashValueVector::const_iterator i = a.begin(); i != a.end(); ++i) { |
| 55 HashValueVector::const_iterator j = |
| 56 std::find_if(b.begin(), b.end(), HashValuesEqual(*i)); |
| 57 if (j != b.end()) |
| 58 return true; |
| 59 } |
| 60 return false; |
| 61 } |
| 62 |
| 63 std::string HashesToBase64String(const HashValueVector& hashes) { |
| 64 std::string str; |
| 65 for (size_t i = 0; i != hashes.size(); ++i) { |
| 66 if (i != 0) |
| 67 str += ","; |
| 68 str += hashes[i].WriteBase64String(); |
| 69 } |
| 70 return str; |
| 71 } |
| 72 |
| 73 bool Base64StringToHashes(const std::string& hashes_str, |
| 74 HashValueVector* hashes) { |
| 75 hashes->clear(); |
| 76 if (hashes_str.empty()) |
| 77 return true; |
| 78 std::vector<std::string> vector_hash_str; |
| 79 base::SplitString(hashes_str, ',', &vector_hash_str); |
| 80 |
| 81 for (size_t i = 0; i != vector_hash_str.size(); ++i) { |
| 82 std::string hash_str; |
| 83 RemoveChars(vector_hash_str[i], " \t\r\n", &hash_str); |
| 84 net::HashValue hash; |
| 85 if (!hash.ParseBase64String(hash_str)) |
| 86 return false; |
| 87 hashes->push_back(hash); |
| 88 } |
| 89 return true; |
| 90 } |
| 91 |
| 50 CertPrincipal::CertPrincipal() { | 92 CertPrincipal::CertPrincipal() { |
| 51 } | 93 } |
| 52 | 94 |
| 53 CertPrincipal::CertPrincipal(const std::string& name) : common_name(name) {} | 95 CertPrincipal::CertPrincipal(const std::string& name) : common_name(name) {} |
| 54 | 96 |
| 55 CertPrincipal::~CertPrincipal() { | 97 CertPrincipal::~CertPrincipal() { |
| 56 } | 98 } |
| 57 | 99 |
| 58 std::string CertPrincipal::GetDisplayName() const { | 100 std::string CertPrincipal::GetDisplayName() const { |
| 59 if (!common_name.empty()) | 101 if (!common_name.empty()) |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 case HASH_VALUE_SHA1: | 192 case HASH_VALUE_SHA1: |
| 151 return fingerprint.sha1.Equals(other.fingerprint.sha1); | 193 return fingerprint.sha1.Equals(other.fingerprint.sha1); |
| 152 case HASH_VALUE_SHA256: | 194 case HASH_VALUE_SHA256: |
| 153 return fingerprint.sha256.Equals(other.fingerprint.sha256); | 195 return fingerprint.sha256.Equals(other.fingerprint.sha256); |
| 154 default: | 196 default: |
| 155 NOTREACHED() << "Unknown HashValueTag " << tag; | 197 NOTREACHED() << "Unknown HashValueTag " << tag; |
| 156 return false; | 198 return false; |
| 157 } | 199 } |
| 158 } | 200 } |
| 159 | 201 |
| 202 bool HashValue::ParseBase64String(const std::string& value) { |
| 203 std::string base64_str; |
| 204 if (value.substr(0, 5) == "sha1/") { |
| 205 tag = HASH_VALUE_SHA1; |
| 206 base64_str = value.substr(5, 28); // length of base64 string |
| 207 } else if (value.substr(0, 7) == "sha256/") { |
| 208 tag = HASH_VALUE_SHA256; |
| 209 base64_str = value.substr(7, 44); // length of base64 string |
| 210 } else { |
| 211 return false; |
| 212 } |
| 213 |
| 214 std::string decoded; |
| 215 if (!base::Base64Decode(base64_str, &decoded) || |
| 216 decoded.size() != size()) { |
| 217 return false; |
| 218 } |
| 219 memcpy(data(), decoded.data(), size()); |
| 220 return true; |
| 221 } |
| 222 |
| 223 std::string HashValue::WriteBase64String() const { |
| 224 std::string base64_str; |
| 225 base::Base64Encode(base::StringPiece(reinterpret_cast<const char*>(data()), |
| 226 size()), &base64_str); |
| 227 switch (tag) { |
| 228 case HASH_VALUE_SHA1: |
| 229 return std::string("sha1/" + base64_str); |
| 230 case HASH_VALUE_SHA256: |
| 231 return std::string("sha256/" + base64_str); |
| 232 default: |
| 233 NOTREACHED() << "Unknown HashValueTag " << tag; |
| 234 return std::string("unknown/" + base64_str); |
| 235 } |
| 236 } |
| 237 |
| 160 size_t HashValue::size() const { | 238 size_t HashValue::size() const { |
| 161 switch (tag) { | 239 switch (tag) { |
| 162 case HASH_VALUE_SHA1: | 240 case HASH_VALUE_SHA1: |
| 163 return sizeof(fingerprint.sha1.data); | 241 return sizeof(fingerprint.sha1.data); |
| 164 case HASH_VALUE_SHA256: | 242 case HASH_VALUE_SHA256: |
| 165 return sizeof(fingerprint.sha256.data); | 243 return sizeof(fingerprint.sha256.data); |
| 166 default: | 244 default: |
| 167 NOTREACHED() << "Unknown HashValueTag " << tag; | 245 NOTREACHED() << "Unknown HashValueTag " << tag; |
| 168 // Although this is NOTREACHED, this function might be inlined and its | 246 // 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 | 247 // return value can be passed to memset as the length argument. If we |
| (...skipping 15 matching lines...) Expand all Loading... |
| 185 return fingerprint.sha1.data; | 263 return fingerprint.sha1.data; |
| 186 case HASH_VALUE_SHA256: | 264 case HASH_VALUE_SHA256: |
| 187 return fingerprint.sha256.data; | 265 return fingerprint.sha256.data; |
| 188 default: | 266 default: |
| 189 NOTREACHED() << "Unknown HashValueTag " << tag; | 267 NOTREACHED() << "Unknown HashValueTag " << tag; |
| 190 return NULL; | 268 return NULL; |
| 191 } | 269 } |
| 192 } | 270 } |
| 193 | 271 |
| 194 } // namespace net | 272 } // namespace net |
| OLD | NEW |