| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/quic/crypto/common_cert_set.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" |
| 9 #include "net/quic/quic_utils.h" |
| 10 |
| 11 using base::StringPiece; |
| 12 |
| 13 namespace net { |
| 14 |
| 15 namespace common_cert_set_0 { |
| 16 #include "net/quic/crypto/common_cert_set_0.c" |
| 17 } |
| 18 |
| 19 |
| 20 struct CertSet { |
| 21 size_t num_certs; |
| 22 const unsigned char* const* certs; |
| 23 const size_t* lens; |
| 24 uint64 hash; |
| 25 }; |
| 26 |
| 27 static const CertSet kSets[] = { |
| 28 { |
| 29 common_cert_set_0::kNumCerts, |
| 30 common_cert_set_0::kCerts, |
| 31 common_cert_set_0::kLens, |
| 32 common_cert_set_0::kHash, |
| 33 }, |
| 34 }; |
| 35 |
| 36 static const uint64 kSetHashes[] = { |
| 37 common_cert_set_0::kHash, |
| 38 }; |
| 39 |
| 40 CommonCertSet::~CommonCertSet() { |
| 41 } |
| 42 |
| 43 CommonCertSetQUIC::CommonCertSetQUIC() { |
| 44 } |
| 45 |
| 46 StringPiece CommonCertSetQUIC::GetCommonHashes() { |
| 47 return StringPiece(reinterpret_cast<const char*>(kSetHashes), |
| 48 sizeof(uint64) * arraysize(kSetHashes)); |
| 49 } |
| 50 |
| 51 StringPiece CommonCertSetQUIC::GetCert(uint64 hash, uint32 index) { |
| 52 for (size_t i = 0; i < arraysize(kSets); i++) { |
| 53 if (kSets[i].hash == hash) { |
| 54 if (index >= kSets[i].num_certs) { |
| 55 return StringPiece(); |
| 56 } |
| 57 return StringPiece(reinterpret_cast<const char*>(kSets[i].certs[index]), |
| 58 kSets[i].lens[index]); |
| 59 } |
| 60 } |
| 61 |
| 62 return StringPiece(); |
| 63 } |
| 64 |
| 65 // Compare returns a value less than, equal to or greater than zero if |a| is |
| 66 // lexicographically less than, equal to or greater than |b|, respectively. |
| 67 static int Compare(StringPiece a, const unsigned char* b, size_t b_len) { |
| 68 size_t len = a.size(); |
| 69 if (len > b_len) { |
| 70 len = b_len; |
| 71 } |
| 72 int n = memcmp(a.data(), b, len); |
| 73 if (n != 0) { |
| 74 return n; |
| 75 } |
| 76 |
| 77 if (a.size() < b_len) { |
| 78 return -1; |
| 79 } else if (a.size() > b_len) { |
| 80 return 1; |
| 81 } |
| 82 return 0; |
| 83 } |
| 84 |
| 85 bool CommonCertSetQUIC::MatchCert(StringPiece cert, |
| 86 StringPiece common_set_hashes, |
| 87 uint64* out_hash, |
| 88 uint32* out_index) { |
| 89 if (common_set_hashes.size() % sizeof(uint64) != 0) { |
| 90 return false; |
| 91 } |
| 92 |
| 93 for (size_t i = 0; i < common_set_hashes.size() / sizeof(uint64); i++) { |
| 94 uint64 hash; |
| 95 memcpy(&hash, common_set_hashes.data() + i*sizeof(uint64), sizeof(uint64)); |
| 96 |
| 97 for (size_t j = 0; j < arraysize(kSets); j++) { |
| 98 if (kSets[j].hash != hash) { |
| 99 continue; |
| 100 } |
| 101 |
| 102 if (kSets[j].num_certs == 0) { |
| 103 continue; |
| 104 } |
| 105 |
| 106 // Binary search for a matching certificate. |
| 107 size_t min = 0; |
| 108 size_t max = kSets[j].num_certs - 1; |
| 109 for (;;) { |
| 110 if (max < min) { |
| 111 break; |
| 112 } |
| 113 |
| 114 size_t mid = min + ((max - min) / 2); |
| 115 int n = Compare(cert, kSets[j].certs[mid], kSets[j].lens[mid]); |
| 116 if (n < 0) { |
| 117 if (mid == 0) { |
| 118 break; |
| 119 } |
| 120 max = mid - 1; |
| 121 } else if (n > 0) { |
| 122 min = mid + 1; |
| 123 } else { |
| 124 *out_hash = hash; |
| 125 *out_index = mid; |
| 126 return true; |
| 127 } |
| 128 } |
| 129 } |
| 130 } |
| 131 |
| 132 return false; |
| 133 } |
| 134 |
| 135 } // namespace net |
| OLD | NEW |