blob: 82ddc642a311be85d48cc08b800d647ff5fa0b35 [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]f9ca77a2014-02-20 18:02:3032QuicServerInfo::QuicServerInfo(const string& hostname) : hostname_(hostname) {
[email protected]662626642014-01-25 00:54:4133}
34
35QuicServerInfo::~QuicServerInfo() {
36}
37
38const QuicServerInfo::State& QuicServerInfo::state() const {
39 return state_;
40}
41
42QuicServerInfo::State* QuicServerInfo::mutable_state() {
43 return &state_;
44}
45
[email protected]f9ca77a2014-02-20 18:02:3046bool QuicServerInfo::Parse(const string& data) {
[email protected]662626642014-01-25 00:54:4147 State* state = mutable_state();
48
49 state->Clear();
50
51 bool r = ParseInner(data);
52 if (!r)
53 state->Clear();
54 return r;
55}
56
[email protected]f9ca77a2014-02-20 18:02:3057bool QuicServerInfo::ParseInner(const string& data) {
58 State* state = mutable_state();
[email protected]662626642014-01-25 00:54:4159
[email protected]f9ca77a2014-02-20 18:02:3060 // No data was read from the disk cache.
61 if (data.empty()) {
62 return false;
63 }
64
65 Pickle p(data.data(), data.size());
66 PickleIterator iter(p);
67
68 int version = -1;
69 if (!p.ReadInt(&iter, &version)) {
70 DVLOG(1) << "Missing version";
71 return false;
72 }
73
74 if (version != kQuicCryptoConfigVersion) {
75 DVLOG(1) << "Unsupported version";
76 return false;
77 }
78
79 if (!p.ReadString(&iter, &state->server_config)) {
80 DVLOG(1) << "Malformed server_config";
81 return false;
82 }
83 if (!p.ReadString(&iter, &state->source_address_token)) {
84 DVLOG(1) << "Malformed source_address_token";
85 return false;
86 }
87 if (!p.ReadString(&iter, &state->server_config_sig)) {
88 DVLOG(1) << "Malformed server_config_sig";
89 return false;
90 }
91
92 // Read certs.
93 uint32 num_certs;
94 if (!p.ReadUInt32(&iter, &num_certs)) {
95 DVLOG(1) << "Malformed num_certs";
96 return false;
97 }
98
99 for (uint32 i = 0; i < num_certs; i++) {
100 string cert;
101 if (!p.ReadString(&iter, &cert)) {
102 DVLOG(1) << "Malformed cert";
103 return false;
104 }
105 state->certs.push_back(cert);
106 }
[email protected]662626642014-01-25 00:54:41107
108 return true;
109}
110
[email protected]f9ca77a2014-02-20 18:02:30111string QuicServerInfo::Serialize() const {
[email protected]662626642014-01-25 00:54:41112 Pickle p(sizeof(Pickle::Header));
113
[email protected]f9ca77a2014-02-20 18:02:30114 if (!p.WriteInt(kQuicCryptoConfigVersion) ||
115 !p.WriteString(state_.server_config) ||
116 !p.WriteString(state_.source_address_token) ||
117 !p.WriteString(state_.server_config_sig) ||
118 state_.certs.size() > std::numeric_limits<uint32>::max() ||
119 !p.WriteUInt32(state_.certs.size())) {
120 return string();
121 }
122
123 for (size_t i = 0; i < state_.certs.size(); i++) {
124 if (!p.WriteString(state_.certs[i])) {
125 return string();
126 }
127 }
128
129 return string(reinterpret_cast<const char *>(p.data()), p.size());
[email protected]662626642014-01-25 00:54:41130}
131
132QuicServerInfoFactory::~QuicServerInfoFactory() {}
133
134} // namespace net