Botan 3.14.1
Crypto and TLS for C&
tls_server.cpp
Go to the documentation of this file.
1/*
2* TLS Server
3* (C) 2004-2011,2012,2016 Jack Lloyd
4* 2016 Matthias Gierlings
5* 2021 Elektrobit Automotive GmbH
6* 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11#include <botan/tls_server.h>
12
13#include <botan/tls_policy.h>
14#include <botan/x509cert.h>
15#include <botan/internal/tls_channel_impl.h>
16
17#if defined(BOTAN_HAS_TLS_12)
18 #include <botan/internal/tls_server_impl_12.h>
19#endif
20
21#if defined(BOTAN_HAS_TLS_13)
22 #include <botan/internal/tls_server_impl_13.h>
23#endif
24
25namespace Botan::TLS {
26
27/*
28* TLS Server Constructor
29*/
30Server::Server(const std::shared_ptr<Callbacks>& callbacks,
31 const std::shared_ptr<Session_Manager>& session_manager,
32 const std::shared_ptr<Credentials_Manager>& creds,
33 const std::shared_ptr<const Policy>& policy,
34 const std::shared_ptr<RandomNumberGenerator>& rng,
35 bool is_datagram,
36 size_t io_buf_sz) {
37 BOTAN_ARG_CHECK(policy != nullptr, "Policy must not be null");
38 const auto max_version = policy->latest_supported_version(is_datagram);
39 const auto flavor = is_datagram ? TLS_Flavor::DTLS : TLS_Flavor::TLS;
40
41#if defined(BOTAN_HAS_TLS_13)
42 if(!max_version.is_pre_tls_13()) {
43 m_impl = Server_Impl_13::create(callbacks, session_manager, creds, policy, rng);
44
45 #if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
46 if(m_impl->expects_downgrade()) {
47 m_impl->set_io_buffer_size(io_buf_sz);
48 }
49 #endif
50
51 return;
52 }
53#endif
54
55#if defined(BOTAN_HAS_TLS_12)
56
57 if(max_version.is_pre_tls_13()) {
58 m_impl = Server_Impl_12::create(callbacks, session_manager, creds, policy, rng, flavor, io_buf_sz);
59 return;
60 }
61#endif
62
63 BOTAN_UNUSED(max_version, callbacks, session_manager, creds, policy, rng, flavor, io_buf_sz);
64 throw Not_Implemented("Requested TLS server version is not available in this build");
65}
66
67Server::~Server() = default;
68
69size_t Server::from_peer(std::span<const uint8_t> data) {
70 auto read = m_impl->from_peer(data);
71
72#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
73 // If TLS 1.2 is not available, we will never downgrade, the downgrade info
74 // won't even be created and `is_downgrading()` would always return false.
75 if(m_impl->is_downgrading()) {
76 auto info = m_impl->extract_downgrade_info();
77 m_impl = Server_Impl_12::create_for_downgrade(*info);
78
79 // replay peer data received so far
80 read = m_impl->from_peer(info->peer_transcript);
81 }
82#endif
83
84 return read;
85}
86
88 return m_impl->is_handshake_complete();
89}
90
91bool Server::is_active() const {
92 return m_impl->is_active();
93}
94
95std::optional<std::chrono::milliseconds> Server::next_retransmission_timeout() const {
96 return m_impl->next_retransmission_timeout();
97}
98
99bool Server::is_closed() const {
100 return m_impl->is_closed();
101}
102
104 return m_impl->is_closed_for_reading();
105}
106
108 return m_impl->is_closed_for_writing();
109}
110
111std::vector<X509_Certificate> Server::peer_cert_chain() const {
112 return m_impl->peer_cert_chain();
113}
114
115std::shared_ptr<const Public_Key> Server::peer_raw_public_key() const {
116 return m_impl->peer_raw_public_key();
117}
118
119std::optional<std::string> Server::external_psk_identity() const {
120 return m_impl->external_psk_identity();
121}
122
123SymmetricKey Server::key_material_export(std::string_view label, std::string_view context, size_t length) const {
124 return m_impl->key_material_export(label, context, length);
125}
126
127void Server::renegotiate(bool force_full_renegotiation) {
128 m_impl->renegotiate(force_full_renegotiation);
129}
130
132 return m_impl->new_session_ticket_supported();
133}
134
135size_t Server::send_new_session_tickets(const size_t tickets) {
136 return m_impl->send_new_session_tickets(tickets);
137}
138
139void Server::update_traffic_keys(bool request_peer_update) {
140 m_impl->update_traffic_keys(request_peer_update);
141}
142
144 return m_impl->secure_renegotiation_supported();
145}
146
147void Server::to_peer(std::span<const uint8_t> data) {
148 m_impl->to_peer(data);
149}
150
151void Server::send_alert(const Alert& alert) {
152 m_impl->send_alert(alert);
153}
154
156 m_impl->send_warning_alert(type);
157}
158
160 m_impl->send_fatal_alert(type);
161}
162
164 m_impl->close();
165}
166
168 return m_impl->timeout_check();
169}
170
171std::string Server::application_protocol() const {
172 return m_impl->application_protocol();
173}
174} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
AlertType Type
Definition tls_alert.h:73
static std::shared_ptr< Server_Impl_12 > create(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< Credentials_Manager > &creds, const std::shared_ptr< const Policy > &policy, const std::shared_ptr< RandomNumberGenerator > &rng, TLS_Flavor flavor=TLS_Flavor::TLS, size_t reserved_io_buffer_size=TLS::Channel::IO_BUF_DEFAULT_SIZE)
static std::shared_ptr< Server_Impl_13 > create(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< Credentials_Manager > &credentials_manager, const std::shared_ptr< const Policy > &policy, const std::shared_ptr< RandomNumberGenerator > &rng)
void update_traffic_keys(bool request_peer_update=false) override
SymmetricKey key_material_export(std::string_view label, std::string_view context, size_t length) const override
~Server() override
bool timeout_check() override
void close() override
bool secure_renegotiation_supported() const override
void send_fatal_alert(Alert::Type type) override
bool is_closed() const override
bool is_closed_for_writing() const override
std::string application_protocol() const override
size_t send_new_session_tickets(size_t tickets=1)
Server(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< Credentials_Manager > &creds, const std::shared_ptr< const Policy > &policy, const std::shared_ptr< RandomNumberGenerator > &rng, bool is_datagram=false, size_t reserved_io_buffer_size=TLS::Channel::IO_BUF_DEFAULT_SIZE)
bool is_handshake_complete() const override
void renegotiate(bool force_full_renegotiation=false) override
std::optional< std::string > external_psk_identity() const override
std::shared_ptr< const Public_Key > peer_raw_public_key() const override
void send_alert(const Alert &alert) override
void send_warning_alert(Alert::Type type) override
bool new_session_ticket_supported() const
std::vector< X509_Certificate > peer_cert_chain() const override
void to_peer(std::span< const uint8_t > data) override
bool is_active() const override
size_t from_peer(std::span< const uint8_t > data) override
bool is_closed_for_reading() const override
std::optional< std::chrono::milliseconds > next_retransmission_timeout() const override
OctetString SymmetricKey
Definition symkey.h:153