blob: 519d7b5fd6015bd2c0a42b2c7148fc06b125ea91 [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
5#include "net/quic/crypto/quic_server_info.h"
6
[email protected]f9ca77a2014-02-20 18:02:307#include <limits>
8
[email protected]662626642014-01-25 00:54:419#include "base/pickle.h"
[email protected]f9ca77a2014-02-20 18:02:3010
11using std::string;
12
13namespace {
14
15const int kQuicCryptoConfigVersion = 1;
16
17} // namespace
[email protected]662626642014-01-25 00:54:4118
19namespace net {
20
21QuicServerInfo::State::State() {}
22
23QuicServerInfo::State::~State() {}
24
25void QuicServerInfo::State::Clear() {
[email protected]f9ca77a2014-02-20 18:02:3026 server_config.clear();
27 source_address_token.clear();
28 server_config_sig.clear();
29 certs.clear();
[email protected]662626642014-01-25 00:54:4130}
31
[email protected]257f24f2014-04-01 09:15:3732QuicServerInfo::QuicServerInfo(const QuicServerId& server_id)
33 : server_id_(server_id) {
[email protected]662626642014-01-25 00:54:4134}
35
36QuicServerInfo::~QuicServerInfo() {
37}
38
39const QuicServerInfo::State& QuicServerInfo::state() const {
40 return state_;
41}
42
43QuicServerInfo::State* QuicServerInfo::mutable_state() {
44 return &state_;
45}
46
[email protected]f9ca77a2014-02-20 18:02:3047bool QuicServerInfo::Parse(const string& data) {
[email protected]662626642014-01-25 00:54:4148 State* state = mutable_state();
49
50 state->Clear();
51
52 bool r = ParseInner(data);
53 if (!r)
54 state->Clear();
55 return r;
56}
57
[email protected]f9ca77a2014-02-20 18:02:3058bool QuicServerInfo::ParseInner(const string& data) {
59 State* state = mutable_state();
[email protected]662626642014-01-25 00:54:4160
[email protected]f9ca77a2014-02-20 18:02:3061 // No data was read from the disk cache.
62 if (data.empty()) {
63 return false;
64 }
65
66 Pickle p(data.data(), data.size());
67 PickleIterator iter(p);
68
69 int version = -1;
70 if (!p.ReadInt(&iter, &version)) {
71 DVLOG(1) << "Missing version";
72 return false;
73 }
74
75 if (version != kQuicCryptoConfigVersion) {
76 DVLOG(1) << "Unsupported version";
77 return false;
78 }
79
80 if (!p.ReadString(&iter, &state->server_config)) {
81 DVLOG(1) << "Malformed server_config";
82 return false;
83 }
84 if (!p.ReadString(&iter, &state->source_address_token)) {
85 DVLOG(1) << "Malformed source_address_token";
86 return false;
87 }
88 if (!p.ReadString(&iter, &state->server_config_sig)) {
89 DVLOG(1) << "Malformed server_config_sig";
90 return false;
91 }
92
93 // Read certs.
94 uint32 num_certs;
95 if (!p.ReadUInt32(&iter, &num_certs)) {
96 DVLOG(1) << "Malformed num_certs";
97 return false;
98 }
99
100 for (uint32 i = 0; i < num_certs; i++) {
101 string cert;
102 if (!p.ReadString(&iter, &cert)) {
103 DVLOG(1) << "Malformed cert";
104 return false;
105 }
106 state->certs.push_back(cert);
107 }
[email protected]662626642014-01-25 00:54:41108
109 return true;
110}
111
[email protected]db1505e2014-02-26 15:23:17112string QuicServerInfo::Serialize() {
113 string pickled_data = SerializeInner();
114 state_.Clear();
115 return pickled_data;
116}
117
118string QuicServerInfo::SerializeInner() const {
[email protected]662626642014-01-25 00:54:41119 Pickle p(sizeof(Pickle::Header));
120
[email protected]f9ca77a2014-02-20 18:02:30121 if (!p.WriteInt(kQuicCryptoConfigVersion) ||
122 !p.WriteString(state_.server_config) ||
123 !p.WriteString(state_.source_address_token) ||
124 !p.WriteString(state_.server_config_sig) ||
125 state_.certs.size() > std::numeric_limits<uint32>::max() ||
126 !p.WriteUInt32(state_.certs.size())) {
127 return string();
128 }
129
130 for (size_t i = 0; i < state_.certs.size(); i++) {
131 if (!p.WriteString(state_.certs[i])) {
132 return string();
133 }
134 }
135
136 return string(reinterpret_cast<const char *>(p.data()), p.size());
[email protected]662626642014-01-25 00:54:41137}
138
139QuicServerInfoFactory::~QuicServerInfoFactory() {}
140
141} // namespace net