| Index: net/quic/crypto/common_cert_set.cc
|
| diff --git a/net/quic/crypto/common_cert_set.cc b/net/quic/crypto/common_cert_set.cc
|
| index 01a54ceec0c69f2c169344da282eb92b2545ddb0..f631cd6fc46a81cc4bf9bd3b83af0d5a9b557786 100644
|
| --- a/net/quic/crypto/common_cert_set.cc
|
| +++ b/net/quic/crypto/common_cert_set.cc
|
| @@ -6,6 +6,7 @@
|
|
|
| #include "base/basictypes.h"
|
| #include "base/logging.h"
|
| +#include "base/memory/singleton.h"
|
| #include "net/quic/quic_utils.h"
|
|
|
| using base::StringPiece;
|
| @@ -16,6 +17,8 @@ namespace common_cert_set_0 {
|
| #include "net/quic/crypto/common_cert_set_0.c"
|
| }
|
|
|
| +namespace {
|
| +
|
| struct CertSet {
|
| // num_certs contains the number of certificates in this set.
|
| size_t num_certs;
|
| @@ -28,7 +31,7 @@ struct CertSet {
|
| uint64 hash;
|
| };
|
|
|
| -static const CertSet kSets[] = {
|
| +const CertSet kSets[] = {
|
| {
|
| common_cert_set_0::kNumCerts,
|
| common_cert_set_0::kCerts,
|
| @@ -37,38 +40,13 @@ static const CertSet kSets[] = {
|
| },
|
| };
|
|
|
| -static const uint64 kSetHashes[] = {
|
| +const uint64 kSetHashes[] = {
|
| common_cert_set_0::kHash,
|
| };
|
|
|
| -CommonCertSets::~CommonCertSets() {
|
| -}
|
| -
|
| -CommonCertSetsQUIC::CommonCertSetsQUIC() {
|
| -}
|
| -
|
| -StringPiece CommonCertSetsQUIC::GetCommonHashes() const {
|
| - return StringPiece(reinterpret_cast<const char*>(kSetHashes),
|
| - sizeof(uint64) * arraysize(kSetHashes));
|
| -}
|
| -
|
| -StringPiece CommonCertSetsQUIC::GetCert(uint64 hash, uint32 index) const {
|
| - for (size_t i = 0; i < arraysize(kSets); i++) {
|
| - if (kSets[i].hash == hash) {
|
| - if (index < kSets[i].num_certs) {
|
| - return StringPiece(reinterpret_cast<const char*>(kSets[i].certs[index]),
|
| - kSets[i].lens[index]);
|
| - }
|
| - break;
|
| - }
|
| - }
|
| -
|
| - return StringPiece();
|
| -}
|
| -
|
| // Compare returns a value less than, equal to or greater than zero if |a| is
|
| // lexicographically less than, equal to or greater than |b|, respectively.
|
| -static int Compare(StringPiece a, const unsigned char* b, size_t b_len) {
|
| +int Compare(StringPiece a, const unsigned char* b, size_t b_len) {
|
| size_t len = a.size();
|
| if (len > b_len) {
|
| len = b_len;
|
| @@ -86,50 +64,95 @@ static int Compare(StringPiece a, const unsigned char* b, size_t b_len) {
|
| return 0;
|
| }
|
|
|
| -bool CommonCertSetsQUIC::MatchCert(StringPiece cert,
|
| - StringPiece common_set_hashes,
|
| - uint64* out_hash,
|
| - uint32* out_index) const {
|
| - if (common_set_hashes.size() % sizeof(uint64) != 0) {
|
| - return false;
|
| +// CommonCertSetsQUIC implements the CommonCertSets interface using the default
|
| +// certificate sets.
|
| +class CommonCertSetsQUIC : public CommonCertSets {
|
| + public:
|
| + // CommonCertSets interface.
|
| + virtual StringPiece GetCommonHashes() const OVERRIDE {
|
| + return StringPiece(reinterpret_cast<const char*>(kSetHashes),
|
| + sizeof(uint64) * arraysize(kSetHashes));
|
| }
|
|
|
| - for (size_t i = 0; i < common_set_hashes.size() / sizeof(uint64); i++) {
|
| - uint64 hash;
|
| - memcpy(&hash, common_set_hashes.data() + i*sizeof(uint64), sizeof(uint64));
|
| -
|
| - for (size_t j = 0; j < arraysize(kSets); j++) {
|
| - if (kSets[j].hash != hash) {
|
| - continue;
|
| + virtual StringPiece GetCert(uint64 hash, uint32 index) const OVERRIDE {
|
| + for (size_t i = 0; i < arraysize(kSets); i++) {
|
| + if (kSets[i].hash == hash) {
|
| + if (index < kSets[i].num_certs) {
|
| + return StringPiece(
|
| + reinterpret_cast<const char*>(kSets[i].certs[index]),
|
| + kSets[i].lens[index]);
|
| + }
|
| + break;
|
| }
|
| + }
|
|
|
| - if (kSets[j].num_certs == 0) {
|
| - continue;
|
| - }
|
| + return StringPiece();
|
| + }
|
| +
|
| + virtual bool MatchCert(StringPiece cert, StringPiece common_set_hashes,
|
| + uint64* out_hash, uint32* out_index) const OVERRIDE {
|
| + if (common_set_hashes.size() % sizeof(uint64) != 0) {
|
| + return false;
|
| + }
|
|
|
| - // Binary search for a matching certificate.
|
| - size_t min = 0;
|
| - size_t max = kSets[j].num_certs - 1;
|
| - while (max >= min) {
|
| - size_t mid = min + ((max - min) / 2);
|
| - int n = Compare(cert, kSets[j].certs[mid], kSets[j].lens[mid]);
|
| - if (n < 0) {
|
| - if (mid == 0) {
|
| - break;
|
| + for (size_t i = 0; i < common_set_hashes.size() / sizeof(uint64); i++) {
|
| + uint64 hash;
|
| + memcpy(&hash, common_set_hashes.data() + i * sizeof(uint64),
|
| + sizeof(uint64));
|
| +
|
| + for (size_t j = 0; j < arraysize(kSets); j++) {
|
| + if (kSets[j].hash != hash) {
|
| + continue;
|
| + }
|
| +
|
| + if (kSets[j].num_certs == 0) {
|
| + continue;
|
| + }
|
| +
|
| + // Binary search for a matching certificate.
|
| + size_t min = 0;
|
| + size_t max = kSets[j].num_certs - 1;
|
| + while (max >= min) {
|
| + size_t mid = min + ((max - min) / 2);
|
| + int n = Compare(cert, kSets[j].certs[mid], kSets[j].lens[mid]);
|
| + if (n < 0) {
|
| + if (mid == 0) {
|
| + break;
|
| + }
|
| + max = mid - 1;
|
| + } else if (n > 0) {
|
| + min = mid + 1;
|
| + } else {
|
| + *out_hash = hash;
|
| + *out_index = mid;
|
| + return true;
|
| }
|
| - max = mid - 1;
|
| - } else if (n > 0) {
|
| - min = mid + 1;
|
| - } else {
|
| - *out_hash = hash;
|
| - *out_index = mid;
|
| - return true;
|
| }
|
| }
|
| }
|
| +
|
| + return false;
|
| + }
|
| +
|
| + static CommonCertSetsQUIC* GetInstance() {
|
| + return Singleton<CommonCertSetsQUIC>::get();
|
| }
|
|
|
| - return false;
|
| + private:
|
| + CommonCertSetsQUIC() {}
|
| + virtual ~CommonCertSetsQUIC() {}
|
| +
|
| + friend struct DefaultSingletonTraits<CommonCertSetsQUIC>;
|
| + DISALLOW_COPY_AND_ASSIGN(CommonCertSetsQUIC);
|
| +};
|
| +
|
| +} // anonymous namespace
|
| +
|
| +CommonCertSets::~CommonCertSets() {}
|
| +
|
| +// static
|
| +const CommonCertSets* CommonCertSets::GetInstanceQUIC() {
|
| + return CommonCertSetsQUIC::GetInstance();
|
| }
|
|
|
| } // namespace net
|
|
|