blob: cdf6e0bc4a21870a202b6cf983929dae28541b89 [file] [log] [blame]
[email protected]662626642014-01-25 00:54:411// 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
Ryan Hamiltona3ee93a72018-08-01 22:03:085#include "net/quic/quic_server_info.h"
[email protected]662626642014-01-25 00:54:416
[email protected]f9ca77a2014-02-20 18:02:307#include <limits>
8
[email protected]662626642014-01-25 00:54:419#include "base/pickle.h"
xunjielic3c226022017-02-08 14:03:1010#include "base/stl_util.h"
[email protected]f9ca77a2014-02-20 18:02:3011
12using std::string;
13
14namespace {
15
rtenneti61de3682016-03-24 00:50:0216const int kQuicCryptoConfigVersion = 2;
[email protected]f9ca77a2014-02-20 18:02:3017
18} // namespace
[email protected]662626642014-01-25 00:54:4119
20namespace net {
21
22QuicServerInfo::State::State() {}
23
24QuicServerInfo::State::~State() {}
25
26void QuicServerInfo::State::Clear() {
xunjielic3c226022017-02-08 14:03:1027 base::STLClearObject(&server_config);
28 base::STLClearObject(&source_address_token);
29 base::STLClearObject(&cert_sct);
30 base::STLClearObject(&chlo_hash);
31 base::STLClearObject(&server_config_sig);
32 base::STLClearObject(&certs);
[email protected]662626642014-01-25 00:54:4133}
34
Ryan Hamilton8d9ee76e2018-05-29 23:52:5235QuicServerInfo::QuicServerInfo(const quic::QuicServerId& server_id)
rjshaded5ced072015-12-18 19:26:0236 : server_id_(server_id) {}
[email protected]662626642014-01-25 00:54:4137
rjshaded5ced072015-12-18 19:26:0238QuicServerInfo::~QuicServerInfo() {}
[email protected]662626642014-01-25 00:54:4139
40const QuicServerInfo::State& QuicServerInfo::state() const {
41 return state_;
42}
43
44QuicServerInfo::State* QuicServerInfo::mutable_state() {
45 return &state_;
46}
47
[email protected]f9ca77a2014-02-20 18:02:3048bool QuicServerInfo::Parse(const string& data) {
[email protected]662626642014-01-25 00:54:4149 State* state = mutable_state();
50
51 state->Clear();
52
53 bool r = ParseInner(data);
54 if (!r)
55 state->Clear();
56 return r;
57}
58
[email protected]f9ca77a2014-02-20 18:02:3059bool QuicServerInfo::ParseInner(const string& data) {
60 State* state = mutable_state();
[email protected]662626642014-01-25 00:54:4161
[email protected]f9ca77a2014-02-20 18:02:3062 // No data was read from the disk cache.
63 if (data.empty()) {
64 return false;
65 }
66
brettwbd4d7112015-06-03 04:29:2567 base::Pickle p(data.data(), data.size());
68 base::PickleIterator iter(p);
[email protected]f9ca77a2014-02-20 18:02:3069
70 int version = -1;
avi48fc13b2014-12-28 23:31:4871 if (!iter.ReadInt(&version)) {
[email protected]f9ca77a2014-02-20 18:02:3072 DVLOG(1) << "Missing version";
73 return false;
74 }
75
zhongyi7cbefb312016-10-11 19:03:4776 if (version != kQuicCryptoConfigVersion) {
[email protected]f9ca77a2014-02-20 18:02:3077 DVLOG(1) << "Unsupported version";
78 return false;
79 }
80
avi48fc13b2014-12-28 23:31:4881 if (!iter.ReadString(&state->server_config)) {
[email protected]f9ca77a2014-02-20 18:02:3082 DVLOG(1) << "Malformed server_config";
83 return false;
84 }
avi48fc13b2014-12-28 23:31:4885 if (!iter.ReadString(&state->source_address_token)) {
[email protected]f9ca77a2014-02-20 18:02:3086 DVLOG(1) << "Malformed source_address_token";
87 return false;
88 }
zhongyi7cbefb312016-10-11 19:03:4789 if (!iter.ReadString(&state->cert_sct)) {
90 DVLOG(1) << "Malformed cert_sct";
91 return false;
92 }
93 if (!iter.ReadString(&state->chlo_hash)) {
94 DVLOG(1) << "Malformed chlo_hash";
95 return false;
rtenneti61de3682016-03-24 00:50:0296 }
avi48fc13b2014-12-28 23:31:4897 if (!iter.ReadString(&state->server_config_sig)) {
[email protected]f9ca77a2014-02-20 18:02:3098 DVLOG(1) << "Malformed server_config_sig";
99 return false;
100 }
101
102 // Read certs.
Avi Drissman13fc8932015-12-20 04:40:46103 uint32_t num_certs;
avi48fc13b2014-12-28 23:31:48104 if (!iter.ReadUInt32(&num_certs)) {
[email protected]f9ca77a2014-02-20 18:02:30105 DVLOG(1) << "Malformed num_certs";
106 return false;
107 }
108
Avi Drissman13fc8932015-12-20 04:40:46109 for (uint32_t i = 0; i < num_certs; i++) {
[email protected]f9ca77a2014-02-20 18:02:30110 string cert;
avi48fc13b2014-12-28 23:31:48111 if (!iter.ReadString(&cert)) {
[email protected]f9ca77a2014-02-20 18:02:30112 DVLOG(1) << "Malformed cert";
113 return false;
114 }
115 state->certs.push_back(cert);
116 }
[email protected]662626642014-01-25 00:54:41117
118 return true;
119}
120
[email protected]db1505e2014-02-26 15:23:17121string QuicServerInfo::Serialize() {
122 string pickled_data = SerializeInner();
123 state_.Clear();
124 return pickled_data;
125}
126
127string QuicServerInfo::SerializeInner() const {
Daniel Cheng0d89f9222017-09-22 05:05:07128 if (state_.certs.size() > std::numeric_limits<uint32_t>::max())
129 return std::string();
[email protected]662626642014-01-25 00:54:41130
Daniel Cheng0d89f9222017-09-22 05:05:07131 base::Pickle p;
132 p.WriteInt(kQuicCryptoConfigVersion);
133 p.WriteString(state_.server_config);
134 p.WriteString(state_.source_address_token);
135 p.WriteString(state_.cert_sct);
136 p.WriteString(state_.chlo_hash);
137 p.WriteString(state_.server_config_sig);
138 p.WriteUInt32(state_.certs.size());
[email protected]f9ca77a2014-02-20 18:02:30139
Daniel Cheng0d89f9222017-09-22 05:05:07140 for (size_t i = 0; i < state_.certs.size(); i++)
141 p.WriteString(state_.certs[i]);
[email protected]f9ca77a2014-02-20 18:02:30142
rjshaded5ced072015-12-18 19:26:02143 return string(reinterpret_cast<const char*>(p.data()), p.size());
[email protected]662626642014-01-25 00:54:41144}
145
[email protected]662626642014-01-25 00:54:41146} // namespace net