Skip to content
Merged
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: 4 additions & 0 deletions builds/gnu/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ src_libbitcoin_network_la_SOURCES = \
${srcdir}/../../src/sessions/session_peer.cpp \
${srcdir}/../../src/sessions/session_seed.cpp \
${srcdir}/../../src/sessions/session_server.cpp \
${srcdir}/../../src/tls/client.cpp \
${srcdir}/../../src/tls/codec.cpp \
${srcdir}/../../src/tls/connection.cpp \
${srcdir}/../../src/tls/context.cpp \
${srcdir}/../../src/tls/record.cpp \
${srcdir}/../../src/tls/schedule.cpp \
Expand Down Expand Up @@ -588,7 +590,9 @@ include_bitcoin_network_tlsdir = \
${includedir}/bitcoin/network/tls

include_bitcoin_network_tls_HEADERS = \
${srcdir}/../../include/bitcoin/network/tls/client.hpp \
${srcdir}/../../include/bitcoin/network/tls/codec.hpp \
${srcdir}/../../include/bitcoin/network/tls/connection.hpp \
${srcdir}/../../include/bitcoin/network/tls/constants.hpp \
${srcdir}/../../include/bitcoin/network/tls/context.hpp \
${srcdir}/../../include/bitcoin/network/tls/record.hpp \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
<ItemGroup>
<ClInclude Include="..\..\..\..\test\functional\peer_setup_fixture.hpp" />
<ClInclude Include="..\..\..\..\test\test.hpp" />
<ClInclude Include="..\..\..\..\test\tls\client.hpp" />
<ClInclude Include="..\..\..\..\test\tls\fixtures.hpp" />
<ClInclude Include="..\..\..\..\test\tls\rfc8448.hpp" />
<ClInclude Include="..\..\..\..\test\zmtp\zmtp_setup_fixture.hpp" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@
<ClInclude Include="..\..\..\..\test\test.hpp">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\test\tls\client.hpp">
<ClInclude Include="..\..\..\..\test\tls\fixtures.hpp">
<Filter>src\tls</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\test\tls\rfc8448.hpp">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@
<ClCompile Include="..\..\..\..\src\sessions\session_seed.cpp" />
<ClCompile Include="..\..\..\..\src\sessions\session_server.cpp" />
<ClCompile Include="..\..\..\..\src\settings.cpp" />
<ClCompile Include="..\..\..\..\src\tls\client.cpp" />
<ClCompile Include="..\..\..\..\src\tls\codec.cpp" />
<ClCompile Include="..\..\..\..\src\tls\connection.cpp" />
<ClCompile Include="..\..\..\..\src\tls\context.cpp">
<ObjectFileName>$(IntDir)src_tls_context.obj</ObjectFileName>
</ClCompile>
Expand Down Expand Up @@ -477,7 +479,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\sessions\session_server.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\sessions\sessions.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\settings.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\tls\client.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\tls\codec.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\tls\connection.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\tls\constants.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\tls\context.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\tls\record.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,15 @@
<ClCompile Include="..\..\..\..\src\settings.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\tls\client.cpp">
<Filter>src\tls</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\tls\codec.cpp">
<Filter>src\tls</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\tls\connection.cpp">
<Filter>src\tls</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\tls\context.cpp">
<Filter>src\tls</Filter>
</ClCompile>
Expand Down Expand Up @@ -1184,9 +1190,15 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\settings.hpp">
<Filter>include\bitcoin\network</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\tls\client.hpp">
<Filter>include\bitcoin\network\tls</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\tls\codec.hpp">
<Filter>include\bitcoin\network\tls</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\tls\connection.hpp">
<Filter>include\bitcoin\network\tls</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\tls\constants.hpp">
<Filter>include\bitcoin\network\tls</Filter>
</ClInclude>
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@
#include <bitcoin/network/sessions/session_seed.hpp>
#include <bitcoin/network/sessions/session_server.hpp>
#include <bitcoin/network/sessions/sessions.hpp>
#include <bitcoin/network/tls/client.hpp>
#include <bitcoin/network/tls/codec.hpp>
#include <bitcoin/network/tls/connection.hpp>
#include <bitcoin/network/tls/constants.hpp>
#include <bitcoin/network/tls/context.hpp>
#include <bitcoin/network/tls/record.hpp>
Expand Down
115 changes: 115 additions & 0 deletions include/bitcoin/network/tls/client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Copyright (c) 2011-2026 libbitcoin developers
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NETWORK_TLS_CLIENT_HPP
#define LIBBITCOIN_NETWORK_TLS_CLIENT_HPP

#include <bitcoin/network/define.hpp>
#include <bitcoin/network/tls/connection.hpp>
#include <bitcoin/network/tls/context.hpp>

namespace libbitcoin {
namespace network {
namespace tls {

/// The client side of one TLS 1.3 connection (rfc8446): the suites, groups
/// and signature schemes of the context offer, server authentication by
/// ECDSA P-256 or P-384 or RSA-PSS (to anchors, or unanchored), optional
/// client authentication (P-256 or P-384), without pre-shared keys or early
/// data. The client hello is output on construction. Not thread safe.
class BCT_API client final
: public connection
{
public:
DELETE_COPY_MOVE(client);

typedef system::x25519::key key;
typedef system::data_array<32> random;

/// The context must outlive the client.
client(const context& context) NOEXCEPT;

/// Fixed client random and x25519 ephemeral secret (test vectors).
client(const context& context, const random& random,
const key& ephemeral) NOEXCEPT;
~client() NOEXCEPT override;

/// The server sent a HelloRetryRequest.
bool is_retried() const NOEXCEPT;

/// The server requested a client certificate.
bool is_requested() const NOEXCEPT;

protected:
bool handle_message(uint8_t type, const span& message,
const span& body) NOEXCEPT override;
bool is_handshaking() const NOEXCEPT override;

private:
typedef context::codes codes;

enum class state
{
server_hello,
encrypted_extensions,
certificate,
certificate_verify,
finished
};

void send_hello() NOEXCEPT;
void send_compatibility() NOEXCEPT;
void send_flight() NOEXCEPT;
bool handle_server_hello(const span& message, const span& body) NOEXCEPT;
bool handle_retry(const span& message, bool keyed, uint16_t group,
const span& cookie) NOEXCEPT;
bool handle_encrypted_extensions(const span& message,
const span& body) NOEXCEPT;
bool handle_certificate_request(const span& message,
const span& body) NOEXCEPT;
bool handle_certificate(const span& message, const span& body) NOEXCEPT;
bool handle_certificate_verify(const span& message,
const span& body) NOEXCEPT;
bool handle_finished(const span& message, const span& body) NOEXCEPT;
system::data_chunk share(uint16_t group) NOEXCEPT;
bool agree(system::data_chunk& shared, uint16_t group,
const span& peer) const NOEXCEPT;

// Configuration.
const random random_;
key secret_;
system::data_chunk session_{};
system::secp256r1::secret_t secret256_{};
system::secp384r1::secret_t secret384_{};

// Handshake state.
codes shared_;
state state_{ state::server_hello };
system::data_chunk hello_{};
system::data_chunk cookie_{};
bool retried_{};
bool requested_{};
bool authenticate_{};
bool compatibility_sent_{};
};

} // namespace tls
} // namespace network
} // namespace libbitcoin

#endif
182 changes: 182 additions & 0 deletions include/bitcoin/network/tls/connection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/**
* Copyright (c) 2011-2026 libbitcoin developers
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NETWORK_TLS_CONNECTION_HPP
#define LIBBITCOIN_NETWORK_TLS_CONNECTION_HPP

#include <bitcoin/network/define.hpp>
#include <bitcoin/network/tls/context.hpp>
#include <bitcoin/network/tls/record.hpp>
#include <bitcoin/network/tls/schedule.hpp>

namespace libbitcoin {
namespace network {
namespace tls {

/// One TLS 1.3 connection (rfc8446): the record layer, alerts, handshake
/// framing, application data and key updates, under the handshake of the
/// client or server. Transport bytes are consumed by receive and produced into
/// output. Not thread safe.
class BCT_API connection
{
public:
DELETE_COPY_MOVE(connection);

/// Consume received transport bytes. False once failed, when output holds
/// any alert to be sent, or once the peer has sent close_notify.
bool receive(const const_byte_span& data) NOEXCEPT;

/// Transport bytes to be sent (the caller consumes them).
system::data_chunk& output() NOEXCEPT;

/// Protect application data into output (once established).
bool write(const const_byte_span& data) NOEXCEPT;

/// Move up to out.size() received application bytes into out.
size_t read(const byte_span& out) NOEXCEPT;

/// Received application bytes not yet read.
size_t readable() const NOEXCEPT;

/// Queue a key update into output, optionally requesting the peer's.
bool update(bool request) NOEXCEPT;

/// Queue close_notify into output (once).
void close() NOEXCEPT;

/// The handshake is complete (peer finished verified).
bool is_established() const NOEXCEPT;

/// The peer sent close_notify.
bool is_closed() const NOEXCEPT;

/// This side sent close_notify.
bool is_close_sent() const NOEXCEPT;

/// The connection failed, with the alert sent or received.
bool is_failed() const NOEXCEPT;
uint8_t failure() const NOEXCEPT;
bool is_failure_received() const NOEXCEPT;

/// Negotiated cipher suite and key exchange group.
uint16_t suite() const NOEXCEPT;
uint16_t group() const NOEXCEPT;

/// Peer certificate chain (empty if not authenticated).
const system::x509::certificates& peer() const NOEXCEPT;

protected:
typedef const_byte_span span;

/// The context must outlive the connection.
connection(const context& context, bool server) NOEXCEPT;
virtual ~connection() NOEXCEPT;

/// Handshake messages.
/// -----------------------------------------------------------------------

static system::data_chunk make_message(uint8_t type,
const system::data_chunk& body) NOEXCEPT;
static system::data_chunk make_certificate(
const context::chain& chain) NOEXCEPT;
static bool parse_certificate(system::x509::certificates& out,
uint8_t& alert, const span& body) NOEXCEPT;
static uint8_t to_alert(const code& ec) NOEXCEPT;

/// CertificateVerify by the context key, of the transcript (4.4.3).
system::data_chunk make_verify(std::string_view label) const NOEXCEPT;

/// True if the leaf signed the transcript by the scheme (4.4.3).
bool is_verified(const system::x509::certificate& leaf, uint16_t scheme,
const span& signature, std::string_view label) const NOEXCEPT;

/// Handshake of the role, from the received handshake message.
virtual bool handle_message(uint8_t type, const span& message,
const span& body) NOEXCEPT = 0;

/// True if a compatibility change_cipher_spec may be received (5).
virtual bool is_handshaking() const NOEXCEPT = 0;

/// Records and keys.
/// -----------------------------------------------------------------------

bool fail(uint8_t alert) NOEXCEPT;
void establish() NOEXCEPT;
void seal(uint8_t type, const span& data) NOEXCEPT;
void send(uint8_t type, const span& data) NOEXCEPT;
void set_send(const schedule::secret& traffic) NOEXCEPT;
void set_receive(const schedule::secret& traffic) NOEXCEPT;
bool handle_key_update(const span& body) NOEXCEPT;
void add_transcript(const span& message) NOEXCEPT;
schedule::secret transcript() const NOEXCEPT;

/// Application traffic secrets (7.1), from the handshake secret.
void derive_traffic() NOEXCEPT;

// Configuration.
const context& context_;

// Negotiation.
uint16_t suite_{};
uint16_t group_{};
system::x509::certificates peer_{};
tls::transcript transcript_{};

// Keys.
schedule::secret handshake_secret_{};
schedule::secret client_handshake_{};
schedule::secret server_handshake_{};
schedule::secret client_traffic_{};
schedule::secret server_traffic_{};

private:
bool handle_record(uint8_t type, const span& header,
const span& fragment) NOEXCEPT;
bool handle_content(uint8_t type, const span& content) NOEXCEPT;
bool handle_alert(const span& content) NOEXCEPT;
bool handle_handshake(const span& content) NOEXCEPT;
void send_update(bool request) NOEXCEPT;
schedule::secret& send_traffic() NOEXCEPT;
schedule::secret& receive_traffic() NOEXCEPT;

// State.
const bool server_;
uint8_t failure_{};
bool failed_{};
bool failure_received_{};
bool established_{};
bool closed_{};
bool close_sent_{};
size_t receive_epoch_{};

// Buffers.
system::data_chunk input_{};
system::data_chunk handshake_{};
system::data_chunk application_{};
system::data_chunk output_{};

// Records.
record send_{};
record receive_{};
};

} // namespace tls
} // namespace network
} // namespace libbitcoin

#endif
Loading
Loading