Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: net/quic/quic_framer.cc

Issue 912163002: Change the QuicEncrypter to accept a pre-allocated buffer instead of (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@Change_QuicPacketCreator_85897935
Patch Set: Reverting logging changes Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/quic/quic_connection_test.cc ('k') | net/quic/quic_framer_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/quic_framer.h" 5 #include "net/quic/quic_framer.h"
6 6
7 #include "base/containers/hash_tables.h" 7 #include "base/containers/hash_tables.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "net/quic/crypto/crypto_framer.h" 9 #include "net/quic/crypto/crypto_framer.h"
10 #include "net/quic/crypto/crypto_handshake_message.h" 10 #include "net/quic/crypto/crypto_handshake_message.h"
(...skipping 1607 matching lines...) Expand 10 before | Expand all | Expand 10 after
1618 DCHECK(encrypter_[level].get() != nullptr); 1618 DCHECK(encrypter_[level].get() != nullptr);
1619 return encrypter_[level].get(); 1619 return encrypter_[level].get();
1620 } 1620 }
1621 1621
1622 QuicEncryptedPacket* QuicFramer::EncryptPacket( 1622 QuicEncryptedPacket* QuicFramer::EncryptPacket(
1623 EncryptionLevel level, 1623 EncryptionLevel level,
1624 QuicPacketSequenceNumber packet_sequence_number, 1624 QuicPacketSequenceNumber packet_sequence_number,
1625 const QuicPacket& packet) { 1625 const QuicPacket& packet) {
1626 DCHECK(encrypter_[level].get() != nullptr); 1626 DCHECK(encrypter_[level].get() != nullptr);
1627 1627
1628 scoped_ptr<QuicData> out(encrypter_[level]->EncryptPacket( 1628 // Allocate a large enough buffer for the header and the encrypted data.
1629 packet_sequence_number, packet.AssociatedData(), packet.Plaintext())); 1629 const size_t encrypted_len =
1630 if (out.get() == nullptr) { 1630 encrypter_[level]->GetCiphertextSize(packet.Plaintext().length());
1631 StringPiece header_data = packet.BeforePlaintext();
1632 const size_t len = header_data.length() + encrypted_len;
1633 // TODO(ianswett): Consider allocating this on the stack in the typical case.
1634 char* buffer = new char[len];
1635 // Copy in the header, because the encrypter only populates the encrypted
1636 // plaintext content.
1637 memcpy(buffer, header_data.data(), header_data.length());
1638 // Encrypt the plaintext into the buffer.
1639 size_t output_length = 0;
1640 if (!encrypter_[level]->EncryptPacket(
1641 packet_sequence_number, packet.AssociatedData(), packet.Plaintext(),
1642 buffer + header_data.length(), &output_length, encrypted_len)) {
1631 RaiseError(QUIC_ENCRYPTION_FAILURE); 1643 RaiseError(QUIC_ENCRYPTION_FAILURE);
1632 return nullptr; 1644 return nullptr;
1633 } 1645 }
1634 StringPiece header_data = packet.BeforePlaintext(); 1646
1635 size_t len = header_data.length() + out->length(); 1647 return new QuicEncryptedPacket(buffer, header_data.length() + output_length,
1636 char* buffer = new char[len]; 1648 true);
1637 // TODO(rch): eliminate this buffer copy by passing in a buffer to Encrypt().
1638 memcpy(buffer, header_data.data(), header_data.length());
1639 memcpy(buffer + header_data.length(), out->data(), out->length());
1640 return new QuicEncryptedPacket(buffer, len, true);
1641 } 1649 }
1642 1650
1643 size_t QuicFramer::GetMaxPlaintextSize(size_t ciphertext_size) { 1651 size_t QuicFramer::GetMaxPlaintextSize(size_t ciphertext_size) {
1644 // In order to keep the code simple, we don't have the current encryption 1652 // In order to keep the code simple, we don't have the current encryption
1645 // level to hand. Both the NullEncrypter and AES-GCM have a tag length of 12. 1653 // level to hand. Both the NullEncrypter and AES-GCM have a tag length of 12.
1646 size_t min_plaintext_size = ciphertext_size; 1654 size_t min_plaintext_size = ciphertext_size;
1647 1655
1648 for (int i = ENCRYPTION_NONE; i < NUM_ENCRYPTION_LEVELS; i++) { 1656 for (int i = ENCRYPTION_NONE; i < NUM_ENCRYPTION_LEVELS; i++) {
1649 if (encrypter_[i].get() != nullptr) { 1657 if (encrypter_[i].get() != nullptr) {
1650 size_t size = encrypter_[i]->GetMaxPlaintextSize(ciphertext_size); 1658 size_t size = encrypter_[i]->GetMaxPlaintextSize(ciphertext_size);
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after
2208 2216
2209 bool QuicFramer::RaiseError(QuicErrorCode error) { 2217 bool QuicFramer::RaiseError(QuicErrorCode error) {
2210 DVLOG(1) << "Error detail: " << detailed_error_; 2218 DVLOG(1) << "Error detail: " << detailed_error_;
2211 set_error(error); 2219 set_error(error);
2212 visitor_->OnError(this); 2220 visitor_->OnError(this);
2213 reader_.reset(nullptr); 2221 reader_.reset(nullptr);
2214 return false; 2222 return false;
2215 } 2223 }
2216 2224
2217 } // namespace net 2225 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/quic_connection_test.cc ('k') | net/quic/quic_framer_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698