• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const int kQuicCryptoConfigVersion = 1;
16 
17 }  // namespace
18 
19 namespace net {
20 
State()21 QuicServerInfo::State::State() {}
22 
~State()23 QuicServerInfo::State::~State() {}
24 
Clear()25 void QuicServerInfo::State::Clear() {
26   server_config.clear();
27   source_address_token.clear();
28   server_config_sig.clear();
29   certs.clear();
30 }
31 
QuicServerInfo(const QuicServerId & server_id)32 QuicServerInfo::QuicServerInfo(const QuicServerId& server_id)
33     : server_id_(server_id) {
34 }
35 
~QuicServerInfo()36 QuicServerInfo::~QuicServerInfo() {
37 }
38 
state() const39 const QuicServerInfo::State& QuicServerInfo::state() const {
40   return state_;
41 }
42 
mutable_state()43 QuicServerInfo::State* QuicServerInfo::mutable_state() {
44   return &state_;
45 }
46 
Parse(const string & data)47 bool QuicServerInfo::Parse(const string& data) {
48   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 
ParseInner(const string & data)58 bool QuicServerInfo::ParseInner(const string& data) {
59   State* state = mutable_state();
60 
61   // 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   }
108 
109   return true;
110 }
111 
Serialize()112 string QuicServerInfo::Serialize() {
113   string pickled_data = SerializeInner();
114   state_.Clear();
115   return pickled_data;
116 }
117 
SerializeInner() const118 string QuicServerInfo::SerializeInner() const {
119   Pickle p(sizeof(Pickle::Header));
120 
121   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());
137 }
138 
~QuicServerInfoFactory()139 QuicServerInfoFactory::~QuicServerInfoFactory() {}
140 
141 }  // namespace net
142