[email protected] | 66262664 | 2014-01-25 00:54:41 | [diff] [blame] | 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 | |||||
[email protected] | f9ca77a | 2014-02-20 18:02:30 | [diff] [blame^] | 7 | #include <limits> |
8 | |||||
[email protected] | 66262664 | 2014-01-25 00:54:41 | [diff] [blame] | 9 | #include "base/pickle.h" |
[email protected] | f9ca77a | 2014-02-20 18:02:30 | [diff] [blame^] | 10 | |
11 | using std::string; | ||||
12 | |||||
13 | namespace { | ||||
14 | |||||
15 | const int kQuicCryptoConfigVersion = 1; | ||||
16 | |||||
17 | } // namespace | ||||
[email protected] | 66262664 | 2014-01-25 00:54:41 | [diff] [blame] | 18 | |
19 | namespace net { | ||||
20 | |||||
21 | QuicServerInfo::State::State() {} | ||||
22 | |||||
23 | QuicServerInfo::State::~State() {} | ||||
24 | |||||
25 | void QuicServerInfo::State::Clear() { | ||||
[email protected] | f9ca77a | 2014-02-20 18:02:30 | [diff] [blame^] | 26 | server_config.clear(); |
27 | source_address_token.clear(); | ||||
28 | server_config_sig.clear(); | ||||
29 | certs.clear(); | ||||
[email protected] | 66262664 | 2014-01-25 00:54:41 | [diff] [blame] | 30 | } |
31 | |||||
[email protected] | f9ca77a | 2014-02-20 18:02:30 | [diff] [blame^] | 32 | QuicServerInfo::QuicServerInfo(const string& hostname) : hostname_(hostname) { |
[email protected] | 66262664 | 2014-01-25 00:54:41 | [diff] [blame] | 33 | } |
34 | |||||
35 | QuicServerInfo::~QuicServerInfo() { | ||||
36 | } | ||||
37 | |||||
38 | const QuicServerInfo::State& QuicServerInfo::state() const { | ||||
39 | return state_; | ||||
40 | } | ||||
41 | |||||
42 | QuicServerInfo::State* QuicServerInfo::mutable_state() { | ||||
43 | return &state_; | ||||
44 | } | ||||
45 | |||||
[email protected] | f9ca77a | 2014-02-20 18:02:30 | [diff] [blame^] | 46 | bool QuicServerInfo::Parse(const string& data) { |
[email protected] | 66262664 | 2014-01-25 00:54:41 | [diff] [blame] | 47 | 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] | f9ca77a | 2014-02-20 18:02:30 | [diff] [blame^] | 57 | bool QuicServerInfo::ParseInner(const string& data) { |
58 | State* state = mutable_state(); | ||||
[email protected] | 66262664 | 2014-01-25 00:54:41 | [diff] [blame] | 59 | |
[email protected] | f9ca77a | 2014-02-20 18:02:30 | [diff] [blame^] | 60 | // 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] | 66262664 | 2014-01-25 00:54:41 | [diff] [blame] | 107 | |
108 | return true; | ||||
109 | } | ||||
110 | |||||
[email protected] | f9ca77a | 2014-02-20 18:02:30 | [diff] [blame^] | 111 | string QuicServerInfo::Serialize() const { |
[email protected] | 66262664 | 2014-01-25 00:54:41 | [diff] [blame] | 112 | Pickle p(sizeof(Pickle::Header)); |
113 | |||||
[email protected] | f9ca77a | 2014-02-20 18:02:30 | [diff] [blame^] | 114 | 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] | 66262664 | 2014-01-25 00:54:41 | [diff] [blame] | 130 | } |
131 | |||||
132 | QuicServerInfoFactory::~QuicServerInfoFactory() {} | ||||
133 | |||||
134 | } // namespace net |