Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(306)

Side by Side Diff: net/quic/crypto/common_cert_set.cc

Issue 15937012: Land Recent QUIC changes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Small bug fixes Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "net/quic/quic_utils.h" 10 #include "net/quic/quic_utils.h"
10 11
11 using base::StringPiece; 12 using base::StringPiece;
12 13
13 namespace net { 14 namespace net {
14 15
15 namespace common_cert_set_0 { 16 namespace common_cert_set_0 {
16 #include "net/quic/crypto/common_cert_set_0.c" 17 #include "net/quic/crypto/common_cert_set_0.c"
17 } 18 }
18 19
20 namespace {
21
19 struct CertSet { 22 struct CertSet {
20 // num_certs contains the number of certificates in this set. 23 // num_certs contains the number of certificates in this set.
21 size_t num_certs; 24 size_t num_certs;
22 // certs is an array of |num_certs| pointers to the DER encoded certificates. 25 // certs is an array of |num_certs| pointers to the DER encoded certificates.
23 const unsigned char* const* certs; 26 const unsigned char* const* certs;
24 // lens is an array of |num_certs| integers describing the length, in bytes, 27 // lens is an array of |num_certs| integers describing the length, in bytes,
25 // of each certificate. 28 // of each certificate.
26 const size_t* lens; 29 const size_t* lens;
27 // hash contains the 64-bit, FNV-1a hash of this set. 30 // hash contains the 64-bit, FNV-1a hash of this set.
28 uint64 hash; 31 uint64 hash;
29 }; 32 };
30 33
31 static const CertSet kSets[] = { 34 const CertSet kSets[] = {
32 { 35 {
33 common_cert_set_0::kNumCerts, 36 common_cert_set_0::kNumCerts,
34 common_cert_set_0::kCerts, 37 common_cert_set_0::kCerts,
35 common_cert_set_0::kLens, 38 common_cert_set_0::kLens,
36 common_cert_set_0::kHash, 39 common_cert_set_0::kHash,
37 }, 40 },
38 }; 41 };
39 42
40 static const uint64 kSetHashes[] = { 43 const uint64 kSetHashes[] = {
41 common_cert_set_0::kHash, 44 common_cert_set_0::kHash,
42 }; 45 };
43 46
44 CommonCertSets::~CommonCertSets() {
45 }
46
47 CommonCertSetsQUIC::CommonCertSetsQUIC() {
48 }
49
50 StringPiece CommonCertSetsQUIC::GetCommonHashes() const {
51 return StringPiece(reinterpret_cast<const char*>(kSetHashes),
52 sizeof(uint64) * arraysize(kSetHashes));
53 }
54
55 StringPiece CommonCertSetsQUIC::GetCert(uint64 hash, uint32 index) const {
56 for (size_t i = 0; i < arraysize(kSets); i++) {
57 if (kSets[i].hash == hash) {
58 if (index < kSets[i].num_certs) {
59 return StringPiece(reinterpret_cast<const char*>(kSets[i].certs[index]),
60 kSets[i].lens[index]);
61 }
62 break;
63 }
64 }
65
66 return StringPiece();
67 }
68
69 // Compare returns a value less than, equal to or greater than zero if |a| is 47 // Compare returns a value less than, equal to or greater than zero if |a| is
70 // lexicographically less than, equal to or greater than |b|, respectively. 48 // lexicographically less than, equal to or greater than |b|, respectively.
71 static int Compare(StringPiece a, const unsigned char* b, size_t b_len) { 49 int Compare(StringPiece a, const unsigned char* b, size_t b_len) {
72 size_t len = a.size(); 50 size_t len = a.size();
73 if (len > b_len) { 51 if (len > b_len) {
74 len = b_len; 52 len = b_len;
75 } 53 }
76 int n = memcmp(a.data(), b, len); 54 int n = memcmp(a.data(), b, len);
77 if (n != 0) { 55 if (n != 0) {
78 return n; 56 return n;
79 } 57 }
80 58
81 if (a.size() < b_len) { 59 if (a.size() < b_len) {
82 return -1; 60 return -1;
83 } else if (a.size() > b_len) { 61 } else if (a.size() > b_len) {
84 return 1; 62 return 1;
85 } 63 }
86 return 0; 64 return 0;
87 } 65 }
88 66
89 bool CommonCertSetsQUIC::MatchCert(StringPiece cert, 67 // CommonCertSetsQUIC implements the CommonCertSets interface using the default
90 StringPiece common_set_hashes, 68 // certificate sets.
91 uint64* out_hash, 69 class CommonCertSetsQUIC : public CommonCertSets {
92 uint32* out_index) const { 70 public:
93 if (common_set_hashes.size() % sizeof(uint64) != 0) { 71 // CommonCertSets interface.
72 virtual StringPiece GetCommonHashes() const OVERRIDE {
73 return StringPiece(reinterpret_cast<const char*>(kSetHashes),
74 sizeof(uint64) * arraysize(kSetHashes));
75 }
76
77 virtual StringPiece GetCert(uint64 hash, uint32 index) const OVERRIDE {
78 for (size_t i = 0; i < arraysize(kSets); i++) {
79 if (kSets[i].hash == hash) {
80 if (index < kSets[i].num_certs) {
81 return StringPiece(
82 reinterpret_cast<const char*>(kSets[i].certs[index]),
83 kSets[i].lens[index]);
84 }
85 break;
86 }
87 }
88
89 return StringPiece();
90 }
91
92 virtual bool MatchCert(StringPiece cert, StringPiece common_set_hashes,
93 uint64* out_hash, uint32* out_index) const OVERRIDE {
94 if (common_set_hashes.size() % sizeof(uint64) != 0) {
95 return false;
96 }
97
98 for (size_t i = 0; i < common_set_hashes.size() / sizeof(uint64); i++) {
99 uint64 hash;
100 memcpy(&hash, common_set_hashes.data() + i * sizeof(uint64),
101 sizeof(uint64));
102
103 for (size_t j = 0; j < arraysize(kSets); j++) {
104 if (kSets[j].hash != hash) {
105 continue;
106 }
107
108 if (kSets[j].num_certs == 0) {
109 continue;
110 }
111
112 // Binary search for a matching certificate.
113 size_t min = 0;
114 size_t max = kSets[j].num_certs - 1;
115 while (max >= min) {
116 size_t mid = min + ((max - min) / 2);
117 int n = Compare(cert, kSets[j].certs[mid], kSets[j].lens[mid]);
118 if (n < 0) {
119 if (mid == 0) {
120 break;
121 }
122 max = mid - 1;
123 } else if (n > 0) {
124 min = mid + 1;
125 } else {
126 *out_hash = hash;
127 *out_index = mid;
128 return true;
129 }
130 }
131 }
132 }
133
94 return false; 134 return false;
95 } 135 }
96 136
97 for (size_t i = 0; i < common_set_hashes.size() / sizeof(uint64); i++) { 137 static CommonCertSetsQUIC* GetInstance() {
98 uint64 hash; 138 return Singleton<CommonCertSetsQUIC>::get();
99 memcpy(&hash, common_set_hashes.data() + i*sizeof(uint64), sizeof(uint64));
100
101 for (size_t j = 0; j < arraysize(kSets); j++) {
102 if (kSets[j].hash != hash) {
103 continue;
104 }
105
106 if (kSets[j].num_certs == 0) {
107 continue;
108 }
109
110 // Binary search for a matching certificate.
111 size_t min = 0;
112 size_t max = kSets[j].num_certs - 1;
113 while (max >= min) {
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 } 139 }
131 140
132 return false; 141 private:
142 CommonCertSetsQUIC() {}
143 virtual ~CommonCertSetsQUIC() {}
144
145 friend struct DefaultSingletonTraits<CommonCertSetsQUIC>;
146 DISALLOW_COPY_AND_ASSIGN(CommonCertSetsQUIC);
147 };
148
149 } // anonymous namespace
150
151 CommonCertSets::~CommonCertSets() {}
152
153 // static
154 const CommonCertSets* CommonCertSets::GetInstanceQUIC() {
155 return CommonCertSetsQUIC::GetInstance();
133 } 156 }
134 157
135 } // namespace net 158 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698