diff --git a/p2p/dtls/dtls_utils.cc b/p2p/dtls/dtls_utils.cc index 050ecd9fac..e34143113b 100644 --- a/p2p/dtls/dtls_utils.cc +++ b/p2p/dtls/dtls_utils.cc @@ -20,8 +20,14 @@ namespace { // https://datatracker.ietf.org/doc/html/rfc5246#appendix-A.1 -const uint8_t kDtlsHandshakeRecord = 22; const uint8_t kDtlsChangeCipherSpecRecord = 20; +const uint8_t kDtlsHandshakeRecord = 22; + +// https://www.rfc-editor.org/rfc/rfc9147.html#section-4 +const uint8_t kFixedBitmask = 0b00100000; +const uint8_t kConnectionBitmask = 0b00010000; +const uint8_t kSequenceNumberBitmask = 0b00001000; +const uint8_t kLengthPresentBitmask = 0b00000100; } // namespace namespace cricket { @@ -61,10 +67,38 @@ std::optional> GetDtlsHandshakeAcks( uint8_t content_type; uint64_t epoch_and_seq; uint16_t len; - // Read content_type(1), skip version(2), read epoch+seq(2+6), - // read len(2) - if (!(record_buf.ReadUInt8(&content_type) && record_buf.Consume(2) && - record_buf.ReadUInt64(&epoch_and_seq) && + // Read content_type(1). + if (!record_buf.ReadUInt8(&content_type)) { + return std::nullopt; + } + + // DTLS 1.3 rules: + // https://www.rfc-editor.org/rfc/rfc9147.html#section-4.1 + if ((content_type & kFixedBitmask) == kFixedBitmask) { + // Interpret as DTLSCipherText: + // https://www.rfc-editor.org/rfc/rfc9147.html#appendix-A.1 + // We assume no connection id is used so C must be 0. + if ((content_type & kConnectionBitmask) != 0) { + return std::nullopt; + } + // Skip sequence_number(1 or 2 bytes depending on S bit). + if (!record_buf.Consume((content_type & kSequenceNumberBitmask) == + kSequenceNumberBitmask + ? 2 + : 1)) { + return std::nullopt; + } + // If the L bit is set, consume the 16 bit length field. + if ((content_type & kLengthPresentBitmask) == kLengthPresentBitmask) { + if (!(record_buf.ReadUInt16(&len) && record_buf.Consume(len))) { + return std::nullopt; + } + } + // DTLSCipherText is encrypted so we can not read it. + continue; + } + // Skip version(2), read epoch+seq(2+6), read len(2) + if (!(record_buf.Consume(2) && record_buf.ReadUInt64(&epoch_and_seq) && record_buf.ReadUInt16(&len) && record_buf.Length() >= len)) { return std::nullopt; } diff --git a/p2p/dtls/dtls_utils_unittest.cc b/p2p/dtls/dtls_utils_unittest.cc index 9f3e25b7c4..410e3b19d8 100644 --- a/p2p/dtls/dtls_utils_unittest.cc +++ b/p2p/dtls/dtls_utils_unittest.cc @@ -128,50 +128,54 @@ TEST(DtlsUtils, GetDtlsHandshakeAcksPackedRecords) { 0x40, 0x29, 0x30, 0x58, 0x37, 0x26, 0x0d, 0xe8, 0xc0, 0x2b, 0x00, 0x00, 0x18, 0x00, 0x17, 0x00, 0x00, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x05, 0x00, 0x02, 0x00, 0x01, - 0x00, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, - 0x01, 0x28, 0x0b, 0x00, 0x01, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, - 0x01, 0x1c, 0x00, 0x01, 0x19, 0x00, 0x01, 0x16, 0x30, 0x82, 0x01, 0x12, - 0x30, 0x81, 0xb8, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x08, 0x15, 0xc9, - 0xcc, 0xd0, 0x55, 0x57, 0xa2, 0x32, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, - 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, + 0x00, // + 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, + 0x28, 0x0b, 0x00, 0x01, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x1c, 0x00, 0x01, 0x19, 0x00, 0x01, 0x16, 0x30, 0x82, 0x01, 0x12, 0x30, + 0x81, 0xb8, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x08, 0x15, 0xc9, 0xcc, + 0xd0, 0x55, 0x57, 0xa2, 0x32, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, + 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, + 0x03, 0x55, 0x04, 0x03, 0x0c, 0x04, 0x74, 0x65, 0x73, 0x74, 0x30, 0x1e, + 0x17, 0x0d, 0x32, 0x34, 0x31, 0x31, 0x30, 0x35, 0x32, 0x30, 0x35, 0x33, + 0x34, 0x38, 0x5a, 0x17, 0x0d, 0x32, 0x34, 0x31, 0x32, 0x30, 0x36, 0x32, + 0x30, 0x35, 0x33, 0x34, 0x38, 0x5a, 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x04, 0x74, 0x65, 0x73, 0x74, 0x30, - 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x31, 0x31, 0x30, 0x35, 0x32, 0x30, 0x35, - 0x33, 0x34, 0x38, 0x5a, 0x17, 0x0d, 0x32, 0x34, 0x31, 0x32, 0x30, 0x36, - 0x32, 0x30, 0x35, 0x33, 0x34, 0x38, 0x5a, 0x30, 0x0f, 0x31, 0x0d, 0x30, - 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x04, 0x74, 0x65, 0x73, 0x74, - 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, - 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, - 0x42, 0x00, 0x04, 0x8f, 0x43, 0xeb, 0x7b, 0x88, 0x73, 0x4f, 0xe7, 0x69, - 0x06, 0x81, 0xb6, 0xb9, 0xf3, 0xca, 0x73, 0x32, 0x69, 0xb2, 0xc5, 0xe6, - 0x4e, 0xf0, 0x8c, 0xf4, 0xdd, 0x4e, 0x5b, 0xea, 0x06, 0x52, 0x94, 0x9a, - 0x12, 0x77, 0x11, 0xde, 0xf9, 0x12, 0x9a, 0xeb, 0x3c, 0x7c, 0xe4, 0xcf, - 0x58, 0x4c, 0x74, 0x44, 0x84, 0x0a, 0x84, 0xeb, 0xe6, 0xa4, 0xd5, 0xd3, - 0x06, 0xca, 0x52, 0x15, 0x7e, 0xeb, 0x19, 0x30, 0x0a, 0x06, 0x08, 0x2a, - 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, - 0x02, 0x21, 0x00, 0xab, 0xc7, 0x06, 0x7e, 0x36, 0x9b, 0xad, 0xe0, 0x26, - 0x61, 0x6b, 0x59, 0xa0, 0x1c, 0x70, 0x0c, 0xa6, 0xd3, 0xff, 0x8a, 0xc7, - 0xba, 0xe4, 0x23, 0x0a, 0x8b, 0x22, 0x82, 0xcd, 0x5a, 0x5c, 0x56, 0x02, - 0x21, 0x00, 0xc7, 0xe8, 0x57, 0x04, 0xb5, 0x44, 0x69, 0x42, 0xa2, 0xa2, - 0x1e, 0xde, 0x7f, 0xc4, 0x44, 0x98, 0xa4, 0x5c, 0x84, 0x41, 0xa1, 0x31, - 0x38, 0x3c, 0xe5, 0x4f, 0xf5, 0xc0, 0xa9, 0xa8, 0xbc, 0x16, 0x16, 0xfe, - 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x7a, 0x0c, - 0x00, 0x00, 0x6e, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6e, 0x03, - 0x00, 0x1d, 0x20, 0x89, 0xb9, 0xeb, 0x39, 0x29, 0xa0, 0x31, 0x08, 0x9a, - 0xbf, 0xc3, 0xc0, 0x20, 0x60, 0xbb, 0xea, 0x73, 0x19, 0xcf, 0x63, 0xe4, - 0x5a, 0xa8, 0xa9, 0x56, 0x77, 0xe8, 0x81, 0x48, 0xae, 0x9f, 0x34, 0x04, - 0x03, 0x00, 0x46, 0x30, 0x44, 0x02, 0x20, 0x23, 0x34, 0xc6, 0x39, 0x94, - 0x84, 0xcc, 0x67, 0xeb, 0x44, 0xf9, 0xc3, 0x5c, 0x52, 0xb3, 0x99, 0x52, - 0xf7, 0x4f, 0xff, 0x8b, 0xc5, 0xea, 0xb5, 0xd0, 0xf9, 0x36, 0xb3, 0xe6, - 0xfc, 0x37, 0x50, 0x02, 0x20, 0x4c, 0xe2, 0x29, 0xf5, 0x4a, 0x4c, 0x7a, - 0x01, 0x37, 0xce, 0xc1, 0xb0, 0x15, 0x23, 0xfd, 0xa5, 0xd9, 0xac, 0x75, - 0xcb, 0x55, 0x56, 0x99, 0x97, 0xe3, 0x13, 0xbd, 0x5b, 0xcc, 0x5d, 0x0c, - 0xa8, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, - 0x00, 0x25, 0x0d, 0x00, 0x00, 0x19, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x19, 0x02, 0x01, 0x40, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04, - 0x01, 0x05, 0x03, 0x08, 0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, - 0x01, 0x00, 0x00, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x04, 0x00, 0x0c, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00}; + 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, + 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, + 0x00, 0x04, 0x8f, 0x43, 0xeb, 0x7b, 0x88, 0x73, 0x4f, 0xe7, 0x69, 0x06, + 0x81, 0xb6, 0xb9, 0xf3, 0xca, 0x73, 0x32, 0x69, 0xb2, 0xc5, 0xe6, 0x4e, + 0xf0, 0x8c, 0xf4, 0xdd, 0x4e, 0x5b, 0xea, 0x06, 0x52, 0x94, 0x9a, 0x12, + 0x77, 0x11, 0xde, 0xf9, 0x12, 0x9a, 0xeb, 0x3c, 0x7c, 0xe4, 0xcf, 0x58, + 0x4c, 0x74, 0x44, 0x84, 0x0a, 0x84, 0xeb, 0xe6, 0xa4, 0xd5, 0xd3, 0x06, + 0xca, 0x52, 0x15, 0x7e, 0xeb, 0x19, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, + 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, + 0x21, 0x00, 0xab, 0xc7, 0x06, 0x7e, 0x36, 0x9b, 0xad, 0xe0, 0x26, 0x61, + 0x6b, 0x59, 0xa0, 0x1c, 0x70, 0x0c, 0xa6, 0xd3, 0xff, 0x8a, 0xc7, 0xba, + 0xe4, 0x23, 0x0a, 0x8b, 0x22, 0x82, 0xcd, 0x5a, 0x5c, 0x56, 0x02, 0x21, + 0x00, 0xc7, 0xe8, 0x57, 0x04, 0xb5, 0x44, 0x69, 0x42, 0xa2, 0xa2, 0x1e, + 0xde, 0x7f, 0xc4, 0x44, 0x98, 0xa4, 0x5c, 0x84, 0x41, 0xa1, 0x31, 0x38, + 0x3c, 0xe5, 0x4f, 0xf5, 0xc0, 0xa9, 0xa8, 0xbc, 0x16, // + 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, + 0x7a, 0x0c, 0x00, 0x00, 0x6e, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x6e, 0x03, 0x00, 0x1d, 0x20, 0x89, 0xb9, 0xeb, 0x39, 0x29, 0xa0, 0x31, + 0x08, 0x9a, 0xbf, 0xc3, 0xc0, 0x20, 0x60, 0xbb, 0xea, 0x73, 0x19, 0xcf, + 0x63, 0xe4, 0x5a, 0xa8, 0xa9, 0x56, 0x77, 0xe8, 0x81, 0x48, 0xae, 0x9f, + 0x34, 0x04, 0x03, 0x00, 0x46, 0x30, 0x44, 0x02, 0x20, 0x23, 0x34, 0xc6, + 0x39, 0x94, 0x84, 0xcc, 0x67, 0xeb, 0x44, 0xf9, 0xc3, 0x5c, 0x52, 0xb3, + 0x99, 0x52, 0xf7, 0x4f, 0xff, 0x8b, 0xc5, 0xea, 0xb5, 0xd0, 0xf9, 0x36, + 0xb3, 0xe6, 0xfc, 0x37, 0x50, 0x02, 0x20, 0x4c, 0xe2, 0x29, 0xf5, 0x4a, + 0x4c, 0x7a, 0x01, 0x37, 0xce, 0xc1, 0xb0, 0x15, 0x23, 0xfd, 0xa5, 0xd9, + 0xac, 0x75, 0xcb, 0x55, 0x56, 0x99, 0x97, 0xe3, 0x13, 0xbd, 0x5b, 0xcc, + 0x5d, 0x0c, + 0xa8, // + 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, + 0x25, 0x0d, 0x00, 0x00, 0x19, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x19, 0x02, 0x01, 0x40, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04, 0x01, + 0x05, 0x03, 0x08, 0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01, + 0x00, 0x00, // + 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, + 0x0c, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00}; std::optional> acks2 = GetDtlsHandshakeAcks(packet2); ASSERT_TRUE(acks2); @@ -179,4 +183,19 @@ TEST(DtlsUtils, GetDtlsHandshakeAcksPackedRecords) { EXPECT_THAT(*acks2, ::testing::ElementsAreArray({0, 1, 2, 3, 4})); } +TEST(DtlsUtils, GetDtls13HandshakeAcks) { + // DTLS 1.3 encrypted data, captured with Wireshark. This is a single + // encrypted record which can not be parsed and should be skipped. + std::vector packet = { + 0x2f, 0x5b, 0x4c, 0x00, 0x23, 0x47, 0xab, 0xe7, 0x90, 0x96, + 0xc0, 0xac, 0x2f, 0x25, 0x40, 0x35, 0x35, 0xa3, 0x81, 0x50, + 0x0c, 0x38, 0x0a, 0xf6, 0xd4, 0xd5, 0x7d, 0xbe, 0x9a, 0xa3, + 0xcb, 0xcb, 0x67, 0xb0, 0x77, 0x79, 0x8b, 0x48, 0x60, 0xf8, + }; + + std::optional> acks = GetDtlsHandshakeAcks(packet); + ASSERT_TRUE(acks); + EXPECT_EQ(acks->size(), 0u); +} + } // namespace cricket