| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/quic/crypto/common_cert_set.h" | 5 #include "net/quic/crypto/common_cert_set.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 10 #include "net/quic/quic_utils.h" | 10 #include "net/quic/quic_utils.h" |
| 11 | 11 |
| 12 using base::StringPiece; | 12 using base::StringPiece; |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 namespace common_cert_set_0 { | 16 namespace common_cert_set_0 { |
| 17 #include "net/quic/crypto/common_cert_set_0.c" | 17 #include "net/quic/crypto/common_cert_set_0.c" |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace common_cert_set_1 { |
| 21 #include "net/quic/crypto/common_cert_set_1.c" |
| 22 } |
| 23 |
| 20 namespace { | 24 namespace { |
| 21 | 25 |
| 22 struct CertSet { | 26 struct CertSet { |
| 23 // num_certs contains the number of certificates in this set. | 27 // num_certs contains the number of certificates in this set. |
| 24 size_t num_certs; | 28 size_t num_certs; |
| 25 // certs is an array of |num_certs| pointers to the DER encoded certificates. | 29 // certs is an array of |num_certs| pointers to the DER encoded certificates. |
| 26 const unsigned char* const* certs; | 30 const unsigned char* const* certs; |
| 27 // lens is an array of |num_certs| integers describing the length, in bytes, | 31 // lens is an array of |num_certs| integers describing the length, in bytes, |
| 28 // of each certificate. | 32 // of each certificate. |
| 29 const size_t* lens; | 33 const size_t* lens; |
| 30 // hash contains the 64-bit, FNV-1a hash of this set. | 34 // hash contains the 64-bit, FNV-1a hash of this set. |
| 31 uint64 hash; | 35 uint64 hash; |
| 32 }; | 36 }; |
| 33 | 37 |
| 34 const CertSet kSets[] = { | 38 const CertSet kSets[] = { |
| 35 { | 39 { |
| 36 common_cert_set_0::kNumCerts, | 40 common_cert_set_0::kNumCerts, |
| 37 common_cert_set_0::kCerts, | 41 common_cert_set_0::kCerts, |
| 38 common_cert_set_0::kLens, | 42 common_cert_set_0::kLens, |
| 39 common_cert_set_0::kHash, | 43 common_cert_set_0::kHash, |
| 40 }, | 44 }, |
| 45 { |
| 46 common_cert_set_1::kNumCerts, |
| 47 common_cert_set_1::kCerts, |
| 48 common_cert_set_1::kLens, |
| 49 common_cert_set_1::kHash, |
| 50 }, |
| 41 }; | 51 }; |
| 42 | 52 |
| 43 const uint64 kSetHashes[] = { | 53 const uint64 kSetHashes[] = { |
| 44 common_cert_set_0::kHash, | 54 common_cert_set_0::kHash, |
| 55 common_cert_set_1::kHash, |
| 45 }; | 56 }; |
| 46 | 57 |
| 47 // Compare returns a value less than, equal to or greater than zero if |a| is | 58 // Compare returns a value less than, equal to or greater than zero if |a| is |
| 48 // lexicographically less than, equal to or greater than |b|, respectively. | 59 // lexicographically less than, equal to or greater than |b|, respectively. |
| 49 int Compare(StringPiece a, const unsigned char* b, size_t b_len) { | 60 int Compare(StringPiece a, const unsigned char* b, size_t b_len) { |
| 50 size_t len = a.size(); | 61 size_t len = a.size(); |
| 51 if (len > b_len) { | 62 if (len > b_len) { |
| 52 len = b_len; | 63 len = b_len; |
| 53 } | 64 } |
| 54 int n = memcmp(a.data(), b, len); | 65 int n = memcmp(a.data(), b, len); |
| 55 if (n != 0) { | 66 if (n != 0) { |
| 56 return n; | 67 return n; |
| 57 } | 68 } |
| 58 | 69 |
| 59 if (a.size() < b_len) { | 70 if (a.size() < b_len) { |
| 60 return -1; | 71 return -1; |
| 61 } else if (a.size() > b_len) { | 72 } else if (a.size() > b_len) { |
| 62 return 1; | 73 return 1; |
| 63 } | 74 } |
| 64 return 0; | 75 return 0; |
| 65 } | 76 } |
| 66 | 77 |
| 67 // CommonCertSetsQUIC implements the CommonCertSets interface using the default | 78 // CommonCertSetsQUIC implements the CommonCertSets interface using the default |
| 68 // certificate sets. | 79 // certificate sets. |
| 69 class CommonCertSetsQUIC : public CommonCertSets { | 80 class CommonCertSetsQUIC : public CommonCertSets { |
| 70 public: | 81 public: |
| 71 // CommonCertSets interface. | 82 // CommonCertSets interface. |
| 72 StringPiece GetCommonHashes() const override { | 83 virtual StringPiece GetCommonHashes() const override { |
| 73 return StringPiece(reinterpret_cast<const char*>(kSetHashes), | 84 return StringPiece(reinterpret_cast<const char*>(kSetHashes), |
| 74 sizeof(uint64) * arraysize(kSetHashes)); | 85 sizeof(uint64) * arraysize(kSetHashes)); |
| 75 } | 86 } |
| 76 | 87 |
| 77 StringPiece GetCert(uint64 hash, uint32 index) const override { | 88 virtual StringPiece GetCert(uint64 hash, uint32 index) const override { |
| 78 for (size_t i = 0; i < arraysize(kSets); i++) { | 89 for (size_t i = 0; i < arraysize(kSets); i++) { |
| 79 if (kSets[i].hash == hash) { | 90 if (kSets[i].hash == hash) { |
| 80 if (index < kSets[i].num_certs) { | 91 if (index < kSets[i].num_certs) { |
| 81 return StringPiece( | 92 return StringPiece( |
| 82 reinterpret_cast<const char*>(kSets[i].certs[index]), | 93 reinterpret_cast<const char*>(kSets[i].certs[index]), |
| 83 kSets[i].lens[index]); | 94 kSets[i].lens[index]); |
| 84 } | 95 } |
| 85 break; | 96 break; |
| 86 } | 97 } |
| 87 } | 98 } |
| 88 | 99 |
| 89 return StringPiece(); | 100 return StringPiece(); |
| 90 } | 101 } |
| 91 | 102 |
| 92 bool MatchCert(StringPiece cert, | 103 virtual bool MatchCert(StringPiece cert, |
| 93 StringPiece common_set_hashes, | 104 StringPiece common_set_hashes, |
| 94 uint64* out_hash, | 105 uint64* out_hash, |
| 95 uint32* out_index) const override { | 106 uint32* out_index) const override { |
| 96 if (common_set_hashes.size() % sizeof(uint64) != 0) { | 107 if (common_set_hashes.size() % sizeof(uint64) != 0) { |
| 97 return false; | 108 return false; |
| 98 } | 109 } |
| 99 | 110 |
| 100 for (size_t i = 0; i < common_set_hashes.size() / sizeof(uint64); i++) { | 111 for (size_t i = 0; i < common_set_hashes.size() / sizeof(uint64); i++) { |
| 101 uint64 hash; | 112 uint64 hash; |
| 102 memcpy(&hash, common_set_hashes.data() + i * sizeof(uint64), | 113 memcpy(&hash, common_set_hashes.data() + i * sizeof(uint64), |
| 103 sizeof(uint64)); | 114 sizeof(uint64)); |
| 104 | 115 |
| 105 for (size_t j = 0; j < arraysize(kSets); j++) { | 116 for (size_t j = 0; j < arraysize(kSets); j++) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 135 | 146 |
| 136 return false; | 147 return false; |
| 137 } | 148 } |
| 138 | 149 |
| 139 static CommonCertSetsQUIC* GetInstance() { | 150 static CommonCertSetsQUIC* GetInstance() { |
| 140 return Singleton<CommonCertSetsQUIC>::get(); | 151 return Singleton<CommonCertSetsQUIC>::get(); |
| 141 } | 152 } |
| 142 | 153 |
| 143 private: | 154 private: |
| 144 CommonCertSetsQUIC() {} | 155 CommonCertSetsQUIC() {} |
| 145 ~CommonCertSetsQUIC() override {} | 156 virtual ~CommonCertSetsQUIC() {} |
| 146 | 157 |
| 147 friend struct DefaultSingletonTraits<CommonCertSetsQUIC>; | 158 friend struct DefaultSingletonTraits<CommonCertSetsQUIC>; |
| 148 DISALLOW_COPY_AND_ASSIGN(CommonCertSetsQUIC); | 159 DISALLOW_COPY_AND_ASSIGN(CommonCertSetsQUIC); |
| 149 }; | 160 }; |
| 150 | 161 |
| 151 } // anonymous namespace | 162 } // anonymous namespace |
| 152 | 163 |
| 153 CommonCertSets::~CommonCertSets() {} | 164 CommonCertSets::~CommonCertSets() {} |
| 154 | 165 |
| 155 // static | 166 // static |
| 156 const CommonCertSets* CommonCertSets::GetInstanceQUIC() { | 167 const CommonCertSets* CommonCertSets::GetInstanceQUIC() { |
| 157 return CommonCertSetsQUIC::GetInstance(); | 168 return CommonCertSetsQUIC::GetInstance(); |
| 158 } | 169 } |
| 159 | 170 |
| 160 } // namespace net | 171 } // namespace net |
| OLD | NEW |