| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/core/crypto/crypto_framer.h" | 5 #include "net/quic/core/crypto/crypto_framer.h" |
| 6 | 6 |
| 7 #include "net/quic/core/crypto/crypto_protocol.h" | 7 #include "net/quic/core/crypto/crypto_protocol.h" |
| 8 #include "net/quic/core/quic_data_reader.h" | 8 #include "net/quic/core/quic_data_reader.h" |
| 9 #include "net/quic/core/quic_data_writer.h" | 9 #include "net/quic/core/quic_data_writer.h" |
| 10 #include "net/quic/core/quic_packets.h" | 10 #include "net/quic/core/quic_packets.h" |
| 11 #include "net/quic/platform/api/quic_str_cat.h" | 11 #include "net/quic/platform/api/quic_str_cat.h" |
| 12 | 12 #include "net/quic/platform/api/quic_string_piece.h" |
| 13 using base::StringPiece; | |
| 14 | 13 |
| 15 namespace net { | 14 namespace net { |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 const size_t kQuicTagSize = sizeof(QuicTag); | 18 const size_t kQuicTagSize = sizeof(QuicTag); |
| 20 const size_t kCryptoEndOffsetSize = sizeof(uint32_t); | 19 const size_t kCryptoEndOffsetSize = sizeof(uint32_t); |
| 21 const size_t kNumEntriesSize = sizeof(uint16_t); | 20 const size_t kNumEntriesSize = sizeof(uint16_t); |
| 22 | 21 |
| 23 // OneShotVisitor is a framer visitor that records a single handshake message. | 22 // OneShotVisitor is a framer visitor that records a single handshake message. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 44 | 43 |
| 45 CryptoFramer::CryptoFramer() | 44 CryptoFramer::CryptoFramer() |
| 46 : visitor_(nullptr), error_detail_(""), num_entries_(0), values_len_(0) { | 45 : visitor_(nullptr), error_detail_(""), num_entries_(0), values_len_(0) { |
| 47 Clear(); | 46 Clear(); |
| 48 } | 47 } |
| 49 | 48 |
| 50 CryptoFramer::~CryptoFramer() {} | 49 CryptoFramer::~CryptoFramer() {} |
| 51 | 50 |
| 52 // static | 51 // static |
| 53 std::unique_ptr<CryptoHandshakeMessage> CryptoFramer::ParseMessage( | 52 std::unique_ptr<CryptoHandshakeMessage> CryptoFramer::ParseMessage( |
| 54 StringPiece in) { | 53 QuicStringPiece in) { |
| 55 OneShotVisitor visitor; | 54 OneShotVisitor visitor; |
| 56 CryptoFramer framer; | 55 CryptoFramer framer; |
| 57 | 56 |
| 58 framer.set_visitor(&visitor); | 57 framer.set_visitor(&visitor); |
| 59 if (!framer.ProcessInput(in) || visitor.error() || | 58 if (!framer.ProcessInput(in) || visitor.error() || |
| 60 framer.InputBytesRemaining()) { | 59 framer.InputBytesRemaining()) { |
| 61 return nullptr; | 60 return nullptr; |
| 62 } | 61 } |
| 63 | 62 |
| 64 return visitor.release(); | 63 return visitor.release(); |
| 65 } | 64 } |
| 66 | 65 |
| 67 bool CryptoFramer::ProcessInput(StringPiece input) { | 66 bool CryptoFramer::ProcessInput(QuicStringPiece input) { |
| 68 DCHECK_EQ(QUIC_NO_ERROR, error_); | 67 DCHECK_EQ(QUIC_NO_ERROR, error_); |
| 69 if (error_ != QUIC_NO_ERROR) { | 68 if (error_ != QUIC_NO_ERROR) { |
| 70 return false; | 69 return false; |
| 71 } | 70 } |
| 72 error_ = Process(input); | 71 error_ = Process(input); |
| 73 if (error_ != QUIC_NO_ERROR) { | 72 if (error_ != QUIC_NO_ERROR) { |
| 74 DCHECK(!error_detail_.empty()); | 73 DCHECK(!error_detail_.empty()); |
| 75 visitor_->OnError(this); | 74 visitor_->OnError(this); |
| 76 return false; | 75 return false; |
| 77 } | 76 } |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 } | 183 } |
| 185 | 184 |
| 186 void CryptoFramer::Clear() { | 185 void CryptoFramer::Clear() { |
| 187 message_.Clear(); | 186 message_.Clear(); |
| 188 tags_and_lengths_.clear(); | 187 tags_and_lengths_.clear(); |
| 189 error_ = QUIC_NO_ERROR; | 188 error_ = QUIC_NO_ERROR; |
| 190 error_detail_ = ""; | 189 error_detail_ = ""; |
| 191 state_ = STATE_READING_TAG; | 190 state_ = STATE_READING_TAG; |
| 192 } | 191 } |
| 193 | 192 |
| 194 QuicErrorCode CryptoFramer::Process(StringPiece input) { | 193 QuicErrorCode CryptoFramer::Process(QuicStringPiece input) { |
| 195 // Add this data to the buffer. | 194 // Add this data to the buffer. |
| 196 buffer_.append(input.data(), input.length()); | 195 buffer_.append(input.data(), input.length()); |
| 197 QuicDataReader reader(buffer_.data(), buffer_.length()); | 196 QuicDataReader reader(buffer_.data(), buffer_.length()); |
| 198 | 197 |
| 199 switch (state_) { | 198 switch (state_) { |
| 200 case STATE_READING_TAG: | 199 case STATE_READING_TAG: |
| 201 if (reader.BytesRemaining() < kQuicTagSize) { | 200 if (reader.BytesRemaining() < kQuicTagSize) { |
| 202 break; | 201 break; |
| 203 } | 202 } |
| 204 QuicTag message_tag; | 203 QuicTag message_tag; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 last_end_offset = end_offset; | 251 last_end_offset = end_offset; |
| 253 } | 252 } |
| 254 values_len_ = last_end_offset; | 253 values_len_ = last_end_offset; |
| 255 state_ = STATE_READING_VALUES; | 254 state_ = STATE_READING_VALUES; |
| 256 } | 255 } |
| 257 case STATE_READING_VALUES: | 256 case STATE_READING_VALUES: |
| 258 if (reader.BytesRemaining() < values_len_) { | 257 if (reader.BytesRemaining() < values_len_) { |
| 259 break; | 258 break; |
| 260 } | 259 } |
| 261 for (const std::pair<QuicTag, size_t>& item : tags_and_lengths_) { | 260 for (const std::pair<QuicTag, size_t>& item : tags_and_lengths_) { |
| 262 StringPiece value; | 261 QuicStringPiece value; |
| 263 reader.ReadStringPiece(&value, item.second); | 262 reader.ReadStringPiece(&value, item.second); |
| 264 message_.SetStringPiece(item.first, value); | 263 message_.SetStringPiece(item.first, value); |
| 265 } | 264 } |
| 266 visitor_->OnHandshakeMessage(message_); | 265 visitor_->OnHandshakeMessage(message_); |
| 267 Clear(); | 266 Clear(); |
| 268 state_ = STATE_READING_TAG; | 267 state_ = STATE_READING_TAG; |
| 269 break; | 268 break; |
| 270 } | 269 } |
| 271 // Save any remaining data. | 270 // Save any remaining data. |
| 272 buffer_ = reader.PeekRemainingPayload().as_string(); | 271 buffer_ = reader.PeekRemainingPayload().as_string(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 283 } | 282 } |
| 284 *end_offset += pad_length; | 283 *end_offset += pad_length; |
| 285 if (!writer->WriteUInt32(*end_offset)) { | 284 if (!writer->WriteUInt32(*end_offset)) { |
| 286 DCHECK(false) << "Failed to write end offset."; | 285 DCHECK(false) << "Failed to write end offset."; |
| 287 return false; | 286 return false; |
| 288 } | 287 } |
| 289 return true; | 288 return true; |
| 290 } | 289 } |
| 291 | 290 |
| 292 } // namespace net | 291 } // namespace net |
| OLD | NEW |