Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/bogo_shim/bogo_shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ std::string map_to_bogo_error(const std::string& e, std::string_view test_name)
{"Have data remaining in buffer after Finished", ":EXCESS_HANDSHAKE_DATA:"},
{"Have data remaining in buffer after ServerHelloDone", ":EXCESS_HANDSHAKE_DATA:"},
{"Hello Retry Request does not request any changes to Client Hello", ":EMPTY_HELLO_RETRY_REQUEST:"},
{"Unexpected additional handshake message data found in record", ":EXCESS_HANDSHAKE_DATA:"},
{"Unexpected additional handshake data at epoch boundary", ":EXCESS_HANDSHAKE_DATA:"},
{"Unexpected additional post-handshake data at epoch boundary", ":EXCESS_HANDSHAKE_DATA:"},
{"Inconsistent length in certificate request", ":DECODE_ERROR:"},
{"Inconsistent length in certificate_authorities extension", ":DECODE_ERROR:"},
{"unexpected key_update parameter", ":DECODE_ERROR:"},
Expand Down Expand Up @@ -243,6 +244,7 @@ std::string map_to_bogo_error(const std::string& e, std::string_view test_name)
{"PSK binder does not check out", ":DIGEST_CHECK_FAILED:"},
{"PSK identity selected by server is out of bounds", ":PSK_IDENTITY_NOT_FOUND:"},
{"PSK and ciphersuite selected by server are not compatible", ":OLD_SESSION_PRF_HASH_MISMATCH:"},
{"Received a new post-handshake message under an outdated epoch", ":EXCESS_HANDSHAKE_DATA:"},
{"Received an unexpectedly non-empty Certificate_Status_Request", ":DECODE_ERROR:"},
{"Received a record that exceeds maximum size", ":ENCRYPTED_LENGTH_TOO_LONG:"},
{"Received a protected record that exceeds maximum size", ":ENCRYPTED_LENGTH_TOO_LONG:"},
Expand Down
16 changes: 8 additions & 8 deletions src/fuzzer/tls_13_handshake_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@

void fuzz(std::span<const uint8_t> in) {
static const Botan::TLS::Default_Policy policy;
constexpr auto epoch_appdata_0 = Botan::TLS::EpochNumber::create(3);

const auto in_record = Botan::TLS::Handshake_Record{
{
.type = Botan::TLS::Record_Type::Handshake,
.sequence_number = std::nullopt,
.payload = {in.begin(), in.end()},
},
{.type = Botan::TLS::Record_Type::Handshake,
.sequence_number = std::nullopt,
.payload = {in.begin(), in.end()},
.epoch = std::nullopt},
};

try {
auto hl1 = Botan::TLS::TLS_Handshake_Layer(Botan::TLS::Connection_Side::Client);
hl1.copy_data(policy, in_record);
std::ignore = hl1.copy_data(policy, in_record);
Botan::TLS::Transcript_Hash_State transcript_hash(Botan::TLS::TLS_Flavor::TLS, "SHA-256");
while(hl1.next_message(policy, transcript_hash).has_value()) {};

auto hl2 = Botan::TLS::TLS_Handshake_Layer(Botan::TLS::Connection_Side::Client);
hl2.copy_data(policy, in_record);
while(hl2.next_post_handshake_message(policy).has_value()) {};
std::ignore = hl2.copy_data(policy, in_record);
while(hl2.next_post_handshake_message(policy, epoch_appdata_0).has_value()) {};
} catch(const Botan::Exception& e) {}
}
5 changes: 3 additions & 2 deletions src/lib/tls/msg_client_hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,16 @@ Protocol_Version Client_Hello_Internal::version() const {
// 0x0303 and a supported_versions extension present with 0x0304 as
// the highest version indicated therein.
if(!extensions().has<Supported_Versions>() ||
!extensions().get<Supported_Versions>()->supports(Protocol_Version::TLS_V13)) {
(!extensions().get<Supported_Versions>()->supports(Protocol_Version::TLS_V13) &&
!extensions().get<Supported_Versions>()->supports(Protocol_Version::DTLS_V13))) {
// The exact legacy_version is ignored we just inspect it to
// distinguish TLS and DTLS.
return (m_legacy_version.is_datagram_protocol()) ? Protocol_Version::DTLS_V12 : Protocol_Version::TLS_V12;
}

// Note: The Client_Hello_13 class will make sure that legacy_version
// is exactly 0x0303 (aka ossified TLS 1.2)
return Protocol_Version::TLS_V13;
return (m_legacy_version.is_datagram_protocol()) ? Protocol_Version::DTLS_V13 : Protocol_Version::TLS_V13;
}

Client_Hello::Client_Hello(Client_Hello&&) noexcept = default;
Expand Down
46 changes: 31 additions & 15 deletions src/lib/tls/msg_server_hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <botan/tls_exceptn.h>
#include <botan/tls_policy.h>
#include <botan/internal/ct_utils.h>
#include <botan/internal/loadstor.h>
#include <botan/internal/tls_reader.h>

namespace Botan::TLS {
Expand All @@ -37,8 +38,9 @@ std::vector<uint8_t> make_server_hello_random(RandomNumberGenerator& rng,
// If negotiating TLS 1.2, TLS 1.3 servers MUST set the last 8 bytes of
// their Random value to the bytes: [DOWNGRADE_TLS12]
//
// DTLS 1.3 is not implemented, so a DTLS server never sets these.
if(flavor == TLS_Flavor::TLS && offered_version.is_pre_tls_13() && policy.allow_tls13()) {
// DTLS 1.3 servers signal downgrades just as TLS 1.3 servers do.
const bool server_supports_13 = flavor == TLS_Flavor::DTLS ? policy.allow_dtls13() : policy.allow_tls13();
if(offered_version.is_pre_tls_13() && server_supports_13) {
constexpr size_t downgrade_signal_length = sizeof(DOWNGRADE_TLS12);
BOTAN_ASSERT_NOMSG(random.size() >= downgrade_signal_length);
const auto lastbytes = std::span{random}.last(downgrade_signal_length);
Expand Down Expand Up @@ -91,7 +93,21 @@ Protocol_Version Server_Hello_Internal::version() const {
//
// Note: Here we just take a message parsing decision, further validation of
// the extension's contents is done later.
return (extensions().has<Supported_Versions>()) ? Protocol_Version::TLS_V13 : m_legacy_version;
if(const auto* sv = extensions().get<Supported_Versions>()) {
const auto versions = sv->versions();
BOTAN_ASSERT_NOMSG(versions.size() == 1);
const auto v = versions.front(); // May be DTLS_V13 or TLS_V13
if(!v.is_tls_13_or_later()) {
// RFC 8446 4.2.1
// If the "supported_versions" extension in the ServerHello contains
// a version [...] prior to TLS 1.3, the client MUST abort the
// handshake with an "illegal_parameter" alert.
throw TLS_Exception(Alert::IllegalParameter, "supported_versions extension contains pre-TLS13 version");
}
return v;
}

return m_legacy_version;
}

Server_Hello::Server_Hello(std::unique_ptr<Server_Hello_Internal> data) : m_data(std::move(data)) {}
Expand Down Expand Up @@ -156,6 +172,18 @@ const Extensions& Server_Hello::extensions() const {
return m_data->extensions();
}

std::optional<Protocol_Version> Server_Hello::random_signals_downgrade() const {
const uint64_t last8 = load_be<uint64_t>(m_data->random().data(), 3);
if(last8 == DOWNGRADE_TLS11) {
return Protocol_Version::TLS_V11;
}
if(last8 == DOWNGRADE_TLS12) {
return Protocol_Version::TLS_V12;
}

return std::nullopt;
}

Server_Hello_12_Shim::Server_Hello_12_Shim(std::span<const uint8_t> buf) :
Server_Hello_12_Shim(std::make_unique<Server_Hello_Internal>(buf)) {}

Expand All @@ -170,16 +198,4 @@ Protocol_Version Server_Hello_12_Shim::selected_version() const {
return legacy_version();
}

std::optional<Protocol_Version> Server_Hello_12_Shim::random_signals_downgrade() const {
const uint64_t last8 = load_be<uint64_t>(m_data->random().data(), 3);
if(last8 == DOWNGRADE_TLS11) {
return Protocol_Version::TLS_V11;
}
if(last8 == DOWNGRADE_TLS12) {
return Protocol_Version::TLS_V12;
}

return std::nullopt;
}

} // namespace Botan::TLS
51 changes: 37 additions & 14 deletions src/lib/tls/tls13/msg_client_hello_13.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,41 @@ Client_Hello_13::Client_Hello_13(const Policy& policy,
// "supported_versions" extension (Section 4.2.1) and the
// legacy_version field MUST be set to 0x0303, which is the version
// number for TLS 1.2.
m_data->m_legacy_version = Protocol_Version::TLS_V12;
// RFC 9147 5.3
// In DTLS 1.3, the client indicates its version preferences in the
// "supported_versions" extension (see Section 4.2.1 of [TLS13]) and
// the legacy_version field MUST be set to {254, 253} [...]
m_data->m_legacy_version = flavor == TLS_Flavor::DTLS ? Protocol_Version::DTLS_V12 : Protocol_Version::TLS_V12;
m_data->m_random = make_hello_random(rng, cb, policy);
m_data->m_suites = policy.ciphersuite_list(Protocol_Version::TLS_V13);

if(policy.allow_tls12()) {
// Note: DTLS 1.3 is NYI, hence dtls_12 is not checked
// When (D)TLS 1.2 is also allowed, advertise its ciphersuites so a legacy
// peer can select one and trigger a downgrade to Client_Impl_12.
if(flavor == TLS_Flavor::DTLS) {
if(policy.allow_dtls12()) {
const auto legacy_suites = policy.ciphersuite_list(Protocol_Version::DTLS_V12);
m_data->m_suites.insert(m_data->m_suites.end(), legacy_suites.cbegin(), legacy_suites.cend());
}
} else if(policy.allow_tls12()) {
const auto legacy_suites = policy.ciphersuite_list(Protocol_Version::TLS_V12);
m_data->m_suites.insert(m_data->m_suites.end(), legacy_suites.cbegin(), legacy_suites.cend());
}

if(policy.tls_13_middlebox_compatibility_mode()) {
// RFC 9147 5.
// DTLS implementations do not use the TLS 1.3 "compatibility mode" [...].
//
// RFC 9147 5.3
// A client which has a cached session ID set by a pre-DTLS 1.3 server
// SHOULD set this field to that value. Otherwise, it MUST be set as a
// zero-length vector [...].
//
// Note: we won't ever offer a (D)TLS 1.2 session. In such a case we would
// have instantiated a (D)TLS 1.2 client in the first place.
if(policy.tls_13_middlebox_compatibility_mode() && flavor != TLS_Flavor::DTLS) {
// RFC 8446 4.1.2
// In compatibility mode (see Appendix D.4), this field MUST be non-empty,
// so a client not offering a pre-TLS 1.3 session MUST generate a new
// 32-byte value.
//
// Note: we won't ever offer a TLS 1.2 session. In such a case we would
// have instantiated a TLS 1.2 client in the first place.
m_data->m_session_id = Session_ID(make_hello_random(rng, cb, policy));
}

Expand All @@ -200,7 +217,8 @@ Client_Hello_13::Client_Hello_13(const Policy& policy,

m_data->extensions().add(new Key_Share(policy, cb, rng));

m_data->extensions().add(new Supported_Versions(Protocol_Version::TLS_V13, policy));
m_data->extensions().add(new Supported_Versions(
flavor == TLS_Flavor::DTLS ? Protocol_Version::DTLS_V13 : Protocol_Version::TLS_V13, policy));

m_data->extensions().add(new Signature_Algorithms(policy.acceptable_signature_schemes()));
if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
Expand All @@ -220,9 +238,10 @@ Client_Hello_13::Client_Hello_13(const Policy& policy,
}

// We currently support "record_size_limit" for TLS 1.3 exclusively. Hence,
// when TLS 1.2 is advertised as a supported protocol, we must not offer this
// when (D)TLS 1.2 is advertised as a supported protocol, we must not offer this
// extension.
if(policy.record_size_limit().has_value() && !policy.allow_tls12()) {
const bool offers_pre_tls_13 = (flavor == TLS_Flavor::DTLS) ? policy.allow_dtls12() : policy.allow_tls12();
if(policy.record_size_limit().has_value() && !offers_pre_tls_13) {
m_data->extensions().add(new Record_Size_Limit(policy.record_size_limit().value()));
}

Expand All @@ -232,7 +251,7 @@ Client_Hello_13::Client_Hello_13(const Policy& policy,
* support is disabled. Otherwise a peer might reply with a 1.2 server hello + a certificate_type
* extension indicating it wishes to use RPK, which would lead to errors later.
*/
if(!policy.allow_tls12()) {
if(!offers_pre_tls_13) {
m_data->extensions().add(new Client_Certificate_Type(policy.accepted_client_certificate_types()));
m_data->extensions().add(new Server_Certificate_Type(policy.accepted_server_certificate_types()));
}
Expand All @@ -241,12 +260,16 @@ Client_Hello_13::Client_Hello_13(const Policy& policy,
m_data->extensions().add(new Application_Layer_Protocol_Notification(std::move(next_protocols)));
}

if(flavor == TLS_Flavor::DTLS) {
m_data->extensions().add(new SRTP_Protection_Profiles(policy.srtp_profiles()));
}

#if defined(BOTAN_HAS_TLS_12)
if(policy.allow_tls12()) {
if(offers_pre_tls_13) {
m_data->extensions().add(new Renegotiation_Extension());
m_data->extensions().add(new Session_Ticket_Extension());

// EMS must always be used with TLS 1.2, regardless of the policy
// EMS must always be used with (D)TLS 1.2, regardless of the policy
m_data->extensions().add(new Extended_Master_Secret);

if(policy.negotiate_encrypt_then_mac()) {
Expand All @@ -261,7 +284,7 @@ Client_Hello_13::Client_Hello_13(const Policy& policy,
#endif

if(session.has_value() || !psks.empty()) {
m_data->extensions().add(new PSK(session, std::move(psks), cb));
m_data->extensions().add(new PSK(session, std::move(psks), cb, flavor));
}
// NOLINTEND(*-owning-memory)

Expand Down
28 changes: 24 additions & 4 deletions src/lib/tls/tls13/msg_encrypted_extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Encrypted_Extensions::Encrypted_Extensions(const Client_Hello_13& client_hello,
const Policy& policy,
Callbacks& cb,
bool is_resumption,
bool requesting_client_auth) {
bool requesting_client_auth,
TLS_Flavor flavor) {
const auto& exts = client_hello.extensions();

// NOLINTBEGIN(*-owning-memory)
Expand Down Expand Up @@ -109,10 +110,29 @@ Encrypted_Extensions::Encrypted_Extensions(const Client_Hello_13& client_hello,
}
}

// NOLINTEND(*-owning-memory)
// DTLS-SRTP is not defined for plain TLS, hence the "use_srtp" extension
// is only negotiated on datagram connections and silently ignored otherwise.
if(const auto* client_srtp = exts.get<SRTP_Protection_Profiles>();
client_srtp != nullptr && flavor == TLS_Flavor::DTLS) {
const auto& server_profiles = policy.srtp_profiles();
const auto& client_profiles = client_srtp->profiles();

// RFC 5764 4.1.1
// The extension_data field MUST contain a UseSRTPData value with a
// single SRTPProtectionProfile value that the server has chosen for
// use with this connection. [...] If there is no shared profile,
// the server SHOULD NOT return the use_srtp extension [...].
//
// We're always using the server preference.
for(const auto profile : server_profiles) {
if(profile != 0 && value_exists(client_profiles, profile)) {
m_extensions.add(new SRTP_Protection_Profiles(profile));
break;
}
}
}

// TODO: Implement handling for (at least)
// * SRTP
// NOLINTEND(*-owning-memory)

cb.tls_modify_extensions(m_extensions, Connection_Side::Server, type());

Expand Down
Loading
Loading