OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/quic_server_info.h" | |
6 | |
7 #include <limits> | |
8 | |
9 #include "base/pickle.h" | |
10 | |
11 using std::string; | |
12 | |
13 namespace { | |
14 | |
15 // TODO(rtenneti): Delete kQuicCryptoConfigVersionNoChloHash after | |
16 // QUIC_VERSION_31 becomes the default. | |
17 const int kQuicCryptoConfigVersionNoChloHash = 1; | |
18 const int kQuicCryptoConfigVersion = 2; | |
19 | |
20 } // namespace | |
21 | |
22 namespace net { | |
23 | |
24 QuicServerInfo::State::State() {} | |
25 | |
26 QuicServerInfo::State::~State() {} | |
27 | |
28 void QuicServerInfo::State::Clear() { | |
29 server_config.clear(); | |
30 source_address_token.clear(); | |
31 cert_sct.clear(); | |
32 chlo_hash.clear(); | |
33 server_config_sig.clear(); | |
34 certs.clear(); | |
35 } | |
36 | |
37 QuicServerInfo::QuicServerInfo(const QuicServerId& server_id) | |
38 : server_id_(server_id) {} | |
39 | |
40 QuicServerInfo::~QuicServerInfo() {} | |
41 | |
42 const QuicServerInfo::State& QuicServerInfo::state() const { | |
43 return state_; | |
44 } | |
45 | |
46 QuicServerInfo::State* QuicServerInfo::mutable_state() { | |
47 return &state_; | |
48 } | |
49 | |
50 bool QuicServerInfo::Parse(const string& data) { | |
51 State* state = mutable_state(); | |
52 | |
53 state->Clear(); | |
54 | |
55 bool r = ParseInner(data); | |
56 if (!r) | |
57 state->Clear(); | |
58 return r; | |
59 } | |
60 | |
61 bool QuicServerInfo::ParseInner(const string& data) { | |
62 State* state = mutable_state(); | |
63 | |
64 // No data was read from the disk cache. | |
65 if (data.empty()) { | |
66 return false; | |
67 } | |
68 | |
69 base::Pickle p(data.data(), data.size()); | |
70 base::PickleIterator iter(p); | |
71 | |
72 int version = -1; | |
73 if (!iter.ReadInt(&version)) { | |
74 DVLOG(1) << "Missing version"; | |
75 return false; | |
76 } | |
77 | |
78 // TODO(rtenneti): Delete kQuicCryptoConfigVersionNoChloHash after | |
79 // QUIC_VERSION_31 becomes the default. | |
80 if (!(version == kQuicCryptoConfigVersionNoChloHash || | |
81 version == kQuicCryptoConfigVersion)) { | |
82 DVLOG(1) << "Unsupported version"; | |
83 return false; | |
84 } | |
85 | |
86 if (!iter.ReadString(&state->server_config)) { | |
87 DVLOG(1) << "Malformed server_config"; | |
88 return false; | |
89 } | |
90 if (!iter.ReadString(&state->source_address_token)) { | |
91 DVLOG(1) << "Malformed source_address_token"; | |
92 return false; | |
93 } | |
94 // TODO(rtenneti): Delete kQuicCryptoConfigVersionNoChloHash after | |
95 // QUIC_VERSION_31 becomes the default. | |
96 if (version == kQuicCryptoConfigVersionNoChloHash) { | |
97 state->cert_sct.clear(); | |
98 state->chlo_hash.clear(); | |
99 } else { | |
100 if (!iter.ReadString(&state->cert_sct)) { | |
101 DVLOG(1) << "Malformed cert_sct"; | |
102 return false; | |
103 } | |
104 if (!iter.ReadString(&state->chlo_hash)) { | |
105 DVLOG(1) << "Malformed chlo_hash"; | |
106 return false; | |
107 } | |
108 } | |
109 if (!iter.ReadString(&state->server_config_sig)) { | |
110 DVLOG(1) << "Malformed server_config_sig"; | |
111 return false; | |
112 } | |
113 | |
114 // Read certs. | |
115 uint32_t num_certs; | |
116 if (!iter.ReadUInt32(&num_certs)) { | |
117 DVLOG(1) << "Malformed num_certs"; | |
118 return false; | |
119 } | |
120 | |
121 for (uint32_t i = 0; i < num_certs; i++) { | |
122 string cert; | |
123 if (!iter.ReadString(&cert)) { | |
124 DVLOG(1) << "Malformed cert"; | |
125 return false; | |
126 } | |
127 state->certs.push_back(cert); | |
128 } | |
129 | |
130 return true; | |
131 } | |
132 | |
133 string QuicServerInfo::Serialize() { | |
134 string pickled_data = SerializeInner(); | |
135 state_.Clear(); | |
136 return pickled_data; | |
137 } | |
138 | |
139 string QuicServerInfo::SerializeInner() const { | |
140 base::Pickle p(sizeof(base::Pickle::Header)); | |
141 | |
142 if (!p.WriteInt(kQuicCryptoConfigVersion) || | |
143 !p.WriteString(state_.server_config) || | |
144 !p.WriteString(state_.source_address_token) || | |
145 !p.WriteString(state_.cert_sct) || !p.WriteString(state_.chlo_hash) || | |
146 !p.WriteString(state_.server_config_sig) || | |
147 state_.certs.size() > std::numeric_limits<uint32_t>::max() || | |
148 !p.WriteUInt32(state_.certs.size())) { | |
149 return string(); | |
150 } | |
151 | |
152 for (size_t i = 0; i < state_.certs.size(); i++) { | |
153 if (!p.WriteString(state_.certs[i])) { | |
154 return string(); | |
155 } | |
156 } | |
157 | |
158 return string(reinterpret_cast<const char*>(p.data()), p.size()); | |
159 } | |
160 | |
161 QuicServerInfoFactory::~QuicServerInfoFactory() {} | |
162 | |
163 } // namespace net | |
OLD | NEW |