1 // Copyright (c) 2013 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.
5 #include "net/quic/crypto/crypto_handshake.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "crypto/secure_hash.h"
14 #include "net/base/net_util.h"
15 #include "net/quic/crypto/common_cert_set.h"
16 #include "net/quic/crypto/crypto_framer.h"
17 #include "net/quic/crypto/key_exchange.h"
18 #include "net/quic/crypto/quic_decrypter.h"
19 #include "net/quic/crypto/quic_encrypter.h"
20 #include "net/quic/crypto/quic_random.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_utils.h"
24 using base::StringPiece;
25 using base::StringPrintf;
31 CryptoHandshakeMessage::CryptoHandshakeMessage()
35 CryptoHandshakeMessage::CryptoHandshakeMessage(
36 const CryptoHandshakeMessage& other)
38 tag_value_map_(other.tag_value_map_),
39 minimum_size_(other.minimum_size_) {
40 // Don't copy serialized_. scoped_ptr doesn't have a copy constructor.
41 // The new object can lazily reconstruct serialized_.
44 CryptoHandshakeMessage::~CryptoHandshakeMessage() {}
46 CryptoHandshakeMessage& CryptoHandshakeMessage::operator=(
47 const CryptoHandshakeMessage& other) {
49 tag_value_map_ = other.tag_value_map_;
50 // Don't copy serialized_. scoped_ptr doesn't have an assignment operator.
51 // However, invalidate serialized_.
53 minimum_size_ = other.minimum_size_;
57 void CryptoHandshakeMessage::Clear() {
59 tag_value_map_.clear();
64 const QuicData& CryptoHandshakeMessage::GetSerialized() const {
65 if (!serialized_.get()) {
66 serialized_.reset(CryptoFramer::ConstructHandshakeMessage(*this));
68 return *serialized_.get();
71 void CryptoHandshakeMessage::MarkDirty() {
75 void CryptoHandshakeMessage::SetTaglist(QuicTag tag, ...) {
76 // Warning, if sizeof(QuicTag) > sizeof(int) then this function will break
77 // because the terminating 0 will only be promoted to int.
78 COMPILE_ASSERT(sizeof(QuicTag) <= sizeof(int),
79 crypto_tag_may_not_be_larger_than_int_or_varargs_will_break);
86 QuicTag list_item = va_arg(ap, QuicTag);
90 tags.push_back(list_item);
93 // Because of the way that we keep tags in memory, we can copy the contents
94 // of the vector and get the correct bytes in wire format. See
95 // crypto_protocol.h. This assumes that the system is little-endian.
101 void CryptoHandshakeMessage::SetStringPiece(QuicTag tag, StringPiece value) {
102 tag_value_map_[tag] = value.as_string();
105 void CryptoHandshakeMessage::Erase(QuicTag tag) {
106 tag_value_map_.erase(tag);
109 QuicErrorCode CryptoHandshakeMessage::GetTaglist(QuicTag tag,
110 const QuicTag** out_tags,
111 size_t* out_len) const {
112 QuicTagValueMap::const_iterator it = tag_value_map_.find(tag);
113 QuicErrorCode ret = QUIC_NO_ERROR;
115 if (it == tag_value_map_.end()) {
116 ret = QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND;
117 } else if (it->second.size() % sizeof(QuicTag) != 0) {
118 ret = QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
121 if (ret != QUIC_NO_ERROR) {
127 *out_tags = reinterpret_cast<const QuicTag*>(it->second.data());
128 *out_len = it->second.size() / sizeof(QuicTag);
132 bool CryptoHandshakeMessage::GetStringPiece(QuicTag tag,
133 StringPiece* out) const {
134 QuicTagValueMap::const_iterator it = tag_value_map_.find(tag);
135 if (it == tag_value_map_.end()) {
142 QuicErrorCode CryptoHandshakeMessage::GetNthValue24(QuicTag tag,
144 StringPiece* out) const {
146 if (!GetStringPiece(tag, &value)) {
147 return QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND;
150 for (unsigned i = 0;; i++) {
152 return QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND;
154 if (value.size() < 3) {
155 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
158 const unsigned char* data =
159 reinterpret_cast<const unsigned char*>(value.data());
160 size_t size = static_cast<size_t>(data[0]) |
161 (static_cast<size_t>(data[1]) << 8) |
162 (static_cast<size_t>(data[2]) << 16);
163 value.remove_prefix(3);
165 if (value.size() < size) {
166 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
170 *out = StringPiece(value.data(), size);
171 return QUIC_NO_ERROR;
174 value.remove_prefix(size);
178 QuicErrorCode CryptoHandshakeMessage::GetUint16(QuicTag tag,
180 return GetPOD(tag, out, sizeof(uint16));
183 QuicErrorCode CryptoHandshakeMessage::GetUint32(QuicTag tag,
185 return GetPOD(tag, out, sizeof(uint32));
188 QuicErrorCode CryptoHandshakeMessage::GetUint64(QuicTag tag,
190 return GetPOD(tag, out, sizeof(uint64));
193 size_t CryptoHandshakeMessage::size() const {
194 size_t ret = sizeof(QuicTag) +
195 sizeof(uint16) /* number of entries */ +
196 sizeof(uint16) /* padding */;
197 ret += (sizeof(QuicTag) + sizeof(uint32) /* end offset */) *
198 tag_value_map_.size();
199 for (QuicTagValueMap::const_iterator i = tag_value_map_.begin();
200 i != tag_value_map_.end(); ++i) {
201 ret += i->second.size();
207 void CryptoHandshakeMessage::set_minimum_size(size_t min_bytes) {
208 if (min_bytes == minimum_size_) {
212 minimum_size_ = min_bytes;
215 size_t CryptoHandshakeMessage::minimum_size() const {
216 return minimum_size_;
219 string CryptoHandshakeMessage::DebugString() const {
220 return DebugStringInternal(0);
223 QuicErrorCode CryptoHandshakeMessage::GetPOD(
224 QuicTag tag, void* out, size_t len) const {
225 QuicTagValueMap::const_iterator it = tag_value_map_.find(tag);
226 QuicErrorCode ret = QUIC_NO_ERROR;
228 if (it == tag_value_map_.end()) {
229 ret = QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND;
230 } else if (it->second.size() != len) {
231 ret = QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
234 if (ret != QUIC_NO_ERROR) {
239 memcpy(out, it->second.data(), len);
243 string CryptoHandshakeMessage::DebugStringInternal(size_t indent) const {
244 string ret = string(2 * indent, ' ') + QuicUtils::TagToString(tag_) + "<\n";
246 for (QuicTagValueMap::const_iterator it = tag_value_map_.begin();
247 it != tag_value_map_.end(); ++it) {
248 ret += string(2 * indent, ' ') + QuicUtils::TagToString(it->first) + ": ";
258 if (it->second.size() == 4) {
260 memcpy(&value, it->second.data(), sizeof(value));
261 ret += base::UintToString(value);
267 if (it->second.size() == 2) {
269 memcpy(&value, it->second.data(), sizeof(value));
270 ret += base::UintToString(value);
280 if (it->second.size() % sizeof(QuicTag) == 0) {
281 for (size_t j = 0; j < it->second.size(); j += sizeof(QuicTag)) {
283 memcpy(&tag, it->second.data() + j, sizeof(tag));
287 ret += "'" + QuicUtils::TagToString(tag) + "'";
294 if (!it->second.empty()) {
295 scoped_ptr<CryptoHandshakeMessage> msg(
296 CryptoFramer::ParseMessage(it->second));
299 ret += msg->DebugStringInternal(indent + 1);
306 ret += StringPrintf("(%d bytes of padding)",
307 static_cast<int>(it->second.size()));
313 // If there's no specific format for this tag, or the value is invalid,
314 // then just use hex.
315 ret += "0x" + base::HexEncode(it->second.data(), it->second.size());
320 ret += string(2 * indent, ' ') + ">";
324 QuicCryptoNegotiatedParameters::QuicCryptoNegotiatedParameters()
329 QuicCryptoNegotiatedParameters::~QuicCryptoNegotiatedParameters() {}
331 CrypterPair::CrypterPair() {}
333 CrypterPair::~CrypterPair() {}
336 const char QuicCryptoConfig::kInitialLabel[] = "QUIC key expansion";
339 const char QuicCryptoConfig::kCETVLabel[] = "QUIC CETV block";
342 const char QuicCryptoConfig::kForwardSecureLabel[] =
343 "QUIC forward secure key expansion";
345 QuicCryptoConfig::QuicCryptoConfig()
346 : common_cert_sets(CommonCertSets::GetInstanceQUIC()) {
349 QuicCryptoConfig::~QuicCryptoConfig() {}