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/quic_framer.h" | 5 #include "net/quic/core/quic_framer.h" |
6 | 6 |
7 #include <cstdint> | 7 #include <cstdint> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "net/quic/core/crypto/crypto_framer.h" | 11 #include "net/quic/core/crypto/crypto_framer.h" |
12 #include "net/quic/core/crypto/crypto_handshake_message.h" | 12 #include "net/quic/core/crypto/crypto_handshake_message.h" |
13 #include "net/quic/core/crypto/crypto_protocol.h" | 13 #include "net/quic/core/crypto/crypto_protocol.h" |
14 #include "net/quic/core/crypto/null_decrypter.h" | 14 #include "net/quic/core/crypto/null_decrypter.h" |
15 #include "net/quic/core/crypto/null_encrypter.h" | 15 #include "net/quic/core/crypto/null_encrypter.h" |
16 #include "net/quic/core/crypto/quic_decrypter.h" | 16 #include "net/quic/core/crypto/quic_decrypter.h" |
17 #include "net/quic/core/crypto/quic_encrypter.h" | 17 #include "net/quic/core/crypto/quic_encrypter.h" |
18 #include "net/quic/core/quic_data_reader.h" | 18 #include "net/quic/core/quic_data_reader.h" |
19 #include "net/quic/core/quic_data_writer.h" | 19 #include "net/quic/core/quic_data_writer.h" |
20 #include "net/quic/core/quic_flags.h" | 20 #include "net/quic/core/quic_flags.h" |
21 #include "net/quic/core/quic_socket_address_coder.h" | 21 #include "net/quic/core/quic_socket_address_coder.h" |
22 #include "net/quic/core/quic_utils.h" | 22 #include "net/quic/core/quic_utils.h" |
23 #include "net/quic/platform/api/quic_aligned.h" | 23 #include "net/quic/platform/api/quic_aligned.h" |
24 #include "net/quic/platform/api/quic_bug_tracker.h" | 24 #include "net/quic/platform/api/quic_bug_tracker.h" |
25 #include "net/quic/platform/api/quic_logging.h" | 25 #include "net/quic/platform/api/quic_logging.h" |
26 #include "net/quic/platform/api/quic_map_util.h" | 26 #include "net/quic/platform/api/quic_map_util.h" |
27 #include "net/quic/platform/api/quic_ptr_util.h" | 27 #include "net/quic/platform/api/quic_ptr_util.h" |
28 | 28 |
29 using base::StringPiece; | |
30 using std::string; | 29 using std::string; |
31 | 30 |
32 namespace net { | 31 namespace net { |
33 | 32 |
34 namespace { | 33 namespace { |
35 | 34 |
36 #define ENDPOINT \ | 35 #define ENDPOINT \ |
37 (perspective_ == Perspective::IS_SERVER ? "Server: " : "Client: ") | 36 (perspective_ == Perspective::IS_SERVER ? "Server: " : "Client: ") |
38 | 37 |
39 // Mask to select the lowest 48 bits of a packet number. | 38 // Mask to select the lowest 48 bits of a packet number. |
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 set_detailed_error("Incorrect message tag."); | 628 set_detailed_error("Incorrect message tag."); |
630 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); | 629 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
631 } | 630 } |
632 | 631 |
633 if (reset->GetUint64(kRNON, &packet.nonce_proof) != QUIC_NO_ERROR) { | 632 if (reset->GetUint64(kRNON, &packet.nonce_proof) != QUIC_NO_ERROR) { |
634 set_detailed_error("Unable to read nonce proof."); | 633 set_detailed_error("Unable to read nonce proof."); |
635 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); | 634 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
636 } | 635 } |
637 // TODO(satyamshekhar): validate nonce to protect against DoS. | 636 // TODO(satyamshekhar): validate nonce to protect against DoS. |
638 | 637 |
639 StringPiece address; | 638 QuicStringPiece address; |
640 if (reset->GetStringPiece(kCADR, &address)) { | 639 if (reset->GetStringPiece(kCADR, &address)) { |
641 QuicSocketAddressCoder address_coder; | 640 QuicSocketAddressCoder address_coder; |
642 if (address_coder.Decode(address.data(), address.length())) { | 641 if (address_coder.Decode(address.data(), address.length())) { |
643 packet.client_address = | 642 packet.client_address = |
644 QuicSocketAddress(address_coder.ip(), address_coder.port()); | 643 QuicSocketAddress(address_coder.ip(), address_coder.port()); |
645 } | 644 } |
646 } | 645 } |
647 | 646 |
648 visitor_->OnPublicResetPacket(packet); | 647 visitor_->OnPublicResetPacket(packet); |
649 return true; | 648 return true; |
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1168 set_detailed_error("Unable to read stream_id."); | 1167 set_detailed_error("Unable to read stream_id."); |
1169 return false; | 1168 return false; |
1170 } | 1169 } |
1171 | 1170 |
1172 frame->offset = 0; | 1171 frame->offset = 0; |
1173 if (!reader->ReadBytes(&frame->offset, offset_length)) { | 1172 if (!reader->ReadBytes(&frame->offset, offset_length)) { |
1174 set_detailed_error("Unable to read offset."); | 1173 set_detailed_error("Unable to read offset."); |
1175 return false; | 1174 return false; |
1176 } | 1175 } |
1177 | 1176 |
1178 // TODO(ianswett): Don't use StringPiece as an intermediary. | 1177 // TODO(ianswett): Don't use QuicStringPiece as an intermediary. |
1179 StringPiece data; | 1178 QuicStringPiece data; |
1180 if (has_data_length) { | 1179 if (has_data_length) { |
1181 if (!reader->ReadStringPiece16(&data)) { | 1180 if (!reader->ReadStringPiece16(&data)) { |
1182 set_detailed_error("Unable to read frame data."); | 1181 set_detailed_error("Unable to read frame data."); |
1183 return false; | 1182 return false; |
1184 } | 1183 } |
1185 } else { | 1184 } else { |
1186 if (!reader->ReadStringPiece(&data, reader->BytesRemaining())) { | 1185 if (!reader->ReadStringPiece(&data, reader->BytesRemaining())) { |
1187 set_detailed_error("Unable to read frame data."); | 1186 set_detailed_error("Unable to read frame data."); |
1188 return false; | 1187 return false; |
1189 } | 1188 } |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1377 return false; | 1376 return false; |
1378 } | 1377 } |
1379 | 1378 |
1380 if (error_code >= QUIC_LAST_ERROR) { | 1379 if (error_code >= QUIC_LAST_ERROR) { |
1381 // Ignore invalid QUIC error code if any. | 1380 // Ignore invalid QUIC error code if any. |
1382 error_code = QUIC_LAST_ERROR; | 1381 error_code = QUIC_LAST_ERROR; |
1383 } | 1382 } |
1384 | 1383 |
1385 frame->error_code = static_cast<QuicErrorCode>(error_code); | 1384 frame->error_code = static_cast<QuicErrorCode>(error_code); |
1386 | 1385 |
1387 StringPiece error_details; | 1386 QuicStringPiece error_details; |
1388 if (!reader->ReadStringPiece16(&error_details)) { | 1387 if (!reader->ReadStringPiece16(&error_details)) { |
1389 set_detailed_error("Unable to read connection close error details."); | 1388 set_detailed_error("Unable to read connection close error details."); |
1390 return false; | 1389 return false; |
1391 } | 1390 } |
1392 frame->error_details = error_details.as_string(); | 1391 frame->error_details = error_details.as_string(); |
1393 | 1392 |
1394 return true; | 1393 return true; |
1395 } | 1394 } |
1396 | 1395 |
1397 bool QuicFramer::ProcessGoAwayFrame(QuicDataReader* reader, | 1396 bool QuicFramer::ProcessGoAwayFrame(QuicDataReader* reader, |
(...skipping 10 matching lines...) Expand all Loading... |
1408 } | 1407 } |
1409 frame->error_code = static_cast<QuicErrorCode>(error_code); | 1408 frame->error_code = static_cast<QuicErrorCode>(error_code); |
1410 | 1409 |
1411 uint32_t stream_id; | 1410 uint32_t stream_id; |
1412 if (!reader->ReadUInt32(&stream_id)) { | 1411 if (!reader->ReadUInt32(&stream_id)) { |
1413 set_detailed_error("Unable to read last good stream id."); | 1412 set_detailed_error("Unable to read last good stream id."); |
1414 return false; | 1413 return false; |
1415 } | 1414 } |
1416 frame->last_good_stream_id = static_cast<QuicStreamId>(stream_id); | 1415 frame->last_good_stream_id = static_cast<QuicStreamId>(stream_id); |
1417 | 1416 |
1418 StringPiece reason_phrase; | 1417 QuicStringPiece reason_phrase; |
1419 if (!reader->ReadStringPiece16(&reason_phrase)) { | 1418 if (!reader->ReadStringPiece16(&reason_phrase)) { |
1420 set_detailed_error("Unable to read goaway reason."); | 1419 set_detailed_error("Unable to read goaway reason."); |
1421 return false; | 1420 return false; |
1422 } | 1421 } |
1423 frame->reason_phrase = reason_phrase.as_string(); | 1422 frame->reason_phrase = reason_phrase.as_string(); |
1424 | 1423 |
1425 return true; | 1424 return true; |
1426 } | 1425 } |
1427 | 1426 |
1428 bool QuicFramer::ProcessWindowUpdateFrame(QuicDataReader* reader, | 1427 bool QuicFramer::ProcessWindowUpdateFrame(QuicDataReader* reader, |
(...skipping 25 matching lines...) Expand all Loading... |
1454 QuicPathCloseFrame* frame) { | 1453 QuicPathCloseFrame* frame) { |
1455 if (!reader->ReadBytes(&frame->path_id, 1)) { | 1454 if (!reader->ReadBytes(&frame->path_id, 1)) { |
1456 set_detailed_error("Unable to read path_id."); | 1455 set_detailed_error("Unable to read path_id."); |
1457 return false; | 1456 return false; |
1458 } | 1457 } |
1459 | 1458 |
1460 return true; | 1459 return true; |
1461 } | 1460 } |
1462 | 1461 |
1463 // static | 1462 // static |
1464 StringPiece QuicFramer::GetAssociatedDataFromEncryptedPacket( | 1463 QuicStringPiece QuicFramer::GetAssociatedDataFromEncryptedPacket( |
1465 QuicVersion version, | 1464 QuicVersion version, |
1466 const QuicEncryptedPacket& encrypted, | 1465 const QuicEncryptedPacket& encrypted, |
1467 QuicConnectionIdLength connection_id_length, | 1466 QuicConnectionIdLength connection_id_length, |
1468 bool includes_version, | 1467 bool includes_version, |
1469 bool includes_diversification_nonce, | 1468 bool includes_diversification_nonce, |
1470 QuicPacketNumberLength packet_number_length) { | 1469 QuicPacketNumberLength packet_number_length) { |
1471 // TODO(ianswett): This is identical to QuicData::AssociatedData. | 1470 // TODO(ianswett): This is identical to QuicData::AssociatedData. |
1472 return StringPiece(encrypted.data(), | 1471 return QuicStringPiece( |
1473 GetStartOfEncryptedData( | 1472 encrypted.data(), |
1474 version, connection_id_length, includes_version, | 1473 GetStartOfEncryptedData(version, connection_id_length, includes_version, |
1475 includes_diversification_nonce, packet_number_length)); | 1474 includes_diversification_nonce, |
| 1475 packet_number_length)); |
1476 } | 1476 } |
1477 | 1477 |
1478 void QuicFramer::SetDecrypter(EncryptionLevel level, QuicDecrypter* decrypter) { | 1478 void QuicFramer::SetDecrypter(EncryptionLevel level, QuicDecrypter* decrypter) { |
1479 DCHECK(alternative_decrypter_.get() == nullptr); | 1479 DCHECK(alternative_decrypter_.get() == nullptr); |
1480 DCHECK_GE(level, decrypter_level_); | 1480 DCHECK_GE(level, decrypter_level_); |
1481 decrypter_.reset(decrypter); | 1481 decrypter_.reset(decrypter); |
1482 decrypter_level_ = level; | 1482 decrypter_level_ = level; |
1483 } | 1483 } |
1484 | 1484 |
1485 void QuicFramer::SetAlternativeDecrypter(EncryptionLevel level, | 1485 void QuicFramer::SetAlternativeDecrypter(EncryptionLevel level, |
(...skipping 20 matching lines...) Expand all Loading... |
1506 | 1506 |
1507 size_t QuicFramer::EncryptInPlace(EncryptionLevel level, | 1507 size_t QuicFramer::EncryptInPlace(EncryptionLevel level, |
1508 QuicPacketNumber packet_number, | 1508 QuicPacketNumber packet_number, |
1509 size_t ad_len, | 1509 size_t ad_len, |
1510 size_t total_len, | 1510 size_t total_len, |
1511 size_t buffer_len, | 1511 size_t buffer_len, |
1512 char* buffer) { | 1512 char* buffer) { |
1513 size_t output_length = 0; | 1513 size_t output_length = 0; |
1514 if (!encrypter_[level]->EncryptPacket( | 1514 if (!encrypter_[level]->EncryptPacket( |
1515 quic_version_, packet_number, | 1515 quic_version_, packet_number, |
1516 StringPiece(buffer, ad_len), // Associated data | 1516 QuicStringPiece(buffer, ad_len), // Associated data |
1517 StringPiece(buffer + ad_len, total_len - ad_len), // Plaintext | 1517 QuicStringPiece(buffer + ad_len, total_len - ad_len), // Plaintext |
1518 buffer + ad_len, // Destination buffer | 1518 buffer + ad_len, // Destination buffer |
1519 &output_length, buffer_len - ad_len)) { | 1519 &output_length, buffer_len - ad_len)) { |
1520 RaiseError(QUIC_ENCRYPTION_FAILURE); | 1520 RaiseError(QUIC_ENCRYPTION_FAILURE); |
1521 return 0; | 1521 return 0; |
1522 } | 1522 } |
1523 | 1523 |
1524 return ad_len + output_length; | 1524 return ad_len + output_length; |
1525 } | 1525 } |
1526 | 1526 |
1527 size_t QuicFramer::EncryptPayload(EncryptionLevel level, | 1527 size_t QuicFramer::EncryptPayload(EncryptionLevel level, |
1528 QuicPacketNumber packet_number, | 1528 QuicPacketNumber packet_number, |
1529 const QuicPacket& packet, | 1529 const QuicPacket& packet, |
1530 char* buffer, | 1530 char* buffer, |
1531 size_t buffer_len) { | 1531 size_t buffer_len) { |
1532 DCHECK(encrypter_[level].get() != nullptr); | 1532 DCHECK(encrypter_[level].get() != nullptr); |
1533 | 1533 |
1534 StringPiece associated_data = packet.AssociatedData(quic_version_); | 1534 QuicStringPiece associated_data = packet.AssociatedData(quic_version_); |
1535 // Copy in the header, because the encrypter only populates the encrypted | 1535 // Copy in the header, because the encrypter only populates the encrypted |
1536 // plaintext content. | 1536 // plaintext content. |
1537 const size_t ad_len = associated_data.length(); | 1537 const size_t ad_len = associated_data.length(); |
1538 memmove(buffer, associated_data.data(), ad_len); | 1538 memmove(buffer, associated_data.data(), ad_len); |
1539 // Encrypt the plaintext into the buffer. | 1539 // Encrypt the plaintext into the buffer. |
1540 size_t output_length = 0; | 1540 size_t output_length = 0; |
1541 if (!encrypter_[level]->EncryptPacket( | 1541 if (!encrypter_[level]->EncryptPacket( |
1542 quic_version_, packet_number, associated_data, | 1542 quic_version_, packet_number, associated_data, |
1543 packet.Plaintext(quic_version_), buffer + ad_len, &output_length, | 1543 packet.Plaintext(quic_version_), buffer + ad_len, &output_length, |
1544 buffer_len - ad_len)) { | 1544 buffer_len - ad_len)) { |
(...skipping 20 matching lines...) Expand all Loading... |
1565 | 1565 |
1566 return min_plaintext_size; | 1566 return min_plaintext_size; |
1567 } | 1567 } |
1568 | 1568 |
1569 bool QuicFramer::DecryptPayload(QuicDataReader* encrypted_reader, | 1569 bool QuicFramer::DecryptPayload(QuicDataReader* encrypted_reader, |
1570 const QuicPacketHeader& header, | 1570 const QuicPacketHeader& header, |
1571 const QuicEncryptedPacket& packet, | 1571 const QuicEncryptedPacket& packet, |
1572 char* decrypted_buffer, | 1572 char* decrypted_buffer, |
1573 size_t buffer_length, | 1573 size_t buffer_length, |
1574 size_t* decrypted_length) { | 1574 size_t* decrypted_length) { |
1575 StringPiece encrypted = encrypted_reader->ReadRemainingPayload(); | 1575 QuicStringPiece encrypted = encrypted_reader->ReadRemainingPayload(); |
1576 DCHECK(decrypter_.get() != nullptr); | 1576 DCHECK(decrypter_.get() != nullptr); |
1577 StringPiece associated_data = GetAssociatedDataFromEncryptedPacket( | 1577 QuicStringPiece associated_data = GetAssociatedDataFromEncryptedPacket( |
1578 quic_version_, packet, header.public_header.connection_id_length, | 1578 quic_version_, packet, header.public_header.connection_id_length, |
1579 header.public_header.version_flag, header.public_header.nonce != nullptr, | 1579 header.public_header.version_flag, header.public_header.nonce != nullptr, |
1580 header.public_header.packet_number_length); | 1580 header.public_header.packet_number_length); |
1581 | 1581 |
1582 bool success = decrypter_->DecryptPacket( | 1582 bool success = decrypter_->DecryptPacket( |
1583 quic_version_, header.packet_number, associated_data, encrypted, | 1583 quic_version_, header.packet_number, associated_data, encrypted, |
1584 decrypted_buffer, decrypted_length, buffer_length); | 1584 decrypted_buffer, decrypted_length, buffer_length); |
1585 if (success) { | 1585 if (success) { |
1586 visitor_->OnDecryptedPacket(decrypter_level_); | 1586 visitor_->OnDecryptedPacket(decrypter_level_); |
1587 } else if (alternative_decrypter_.get() != nullptr) { | 1587 } else if (alternative_decrypter_.get() != nullptr) { |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2145 | 2145 |
2146 bool QuicFramer::RaiseError(QuicErrorCode error) { | 2146 bool QuicFramer::RaiseError(QuicErrorCode error) { |
2147 QUIC_DLOG(INFO) << ENDPOINT << "Error: " << QuicErrorCodeToString(error) | 2147 QUIC_DLOG(INFO) << ENDPOINT << "Error: " << QuicErrorCodeToString(error) |
2148 << " detail: " << detailed_error_; | 2148 << " detail: " << detailed_error_; |
2149 set_error(error); | 2149 set_error(error); |
2150 visitor_->OnError(this); | 2150 visitor_->OnError(this); |
2151 return false; | 2151 return false; |
2152 } | 2152 } |
2153 | 2153 |
2154 } // namespace net | 2154 } // namespace net |
OLD | NEW |