Botan 3.13.0
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 const auto max_version = policy->latest_supported_version(is_datagram);
38
39#if defined(BOTAN_HAS_TLS_13)
40 if(!max_version.is_pre_tls_13()) {
41 m_impl = std::make_unique<Server_Impl_13>(callbacks, session_manager, creds, policy, rng);
42
43 #if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
44 if(m_impl->expects_downgrade()) {
45 m_impl->set_io_buffer_size(io_buf_sz);
46 }
47 #endif
48
49 return;
50 }
51#endif
52
53#if defined(BOTAN_HAS_TLS_12)
54 if(max_version.is_pre_tls_13()) {
55 m_impl = std::make_unique<Server_Impl_12>(callbacks, session_manager, creds, policy, rng, is_datagram, io_buf_sz);
56 return;
57 }
58#endif
59
60 BOTAN_UNUSED(max_version, callbacks, session_manager, creds, policy, rng, is_datagram, io_buf_sz);
61 throw Not_Implemented("Requested TLS server version is not available in this build");
62}
63
64Server::~Server() = default;
65
66size_t Server::from_peer(std::span<const uint8_t> data) {
67 auto read = m_impl->from_peer(data);
68
69#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
70 // If TLS 1.2 is not available, we will never downgrade, the downgrade info
71 // won't even be created and `is_downgrading()` would always return false.
72 if(m_impl->is_downgrading()) {
73 auto info = m_impl->extract_downgrade_info();
74 m_impl = std::make_unique<Server_Impl_12>(*info);
75
76 // replay peer data received so far
77 read = m_impl->from_peer(info->peer_transcript);
78 }
79#endif
80
81 return read;
82}
83
85 return m_impl->is_handshake_complete();
86}
87
88bool Server::is_active() const {
89 return m_impl->is_active();
90}
91
92std::optional<std::chrono::milliseconds> Server::next_retransmission_timeout() const {
93 return m_impl->next_retransmission_timeout();
94}
95
96bool Server::is_closed() const {
97 return m_impl->is_closed();
98}
99
101 return m_impl->is_closed_for_reading();
102}
103
105 return m_impl->is_closed_for_writing();
106}
107
108std::vector<X509_Certificate> Server::peer_cert_chain() const {
109 return m_impl->peer_cert_chain();
110}
111
112std::shared_ptr<const Public_Key> Server::peer_raw_public_key() const {
113 return m_impl->peer_raw_public_key();
114}
115
116std::optional<std::string> Server::external_psk_identity() const {
117 return m_impl->external_psk_identity();
118}
119
120SymmetricKey Server::key_material_export(std::string_view label, std::string_view context, size_t length) const {
121 return m_impl->key_material_export(label, context, length);
122}
123
124void Server::renegotiate(bool force_full_renegotiation) {
125 m_impl->renegotiate(force_full_renegotiation);
126}
127
129 return m_impl->new_session_ticket_supported();
130}
131
132size_t Server::send_new_session_tickets(const size_t tickets) {
133 return m_impl->send_new_session_tickets(tickets);
134}
135
136void Server::update_traffic_keys(bool request_peer_update) {
137 m_impl->update_traffic_keys(request_peer_update);
138}
139
141 return m_impl->secure_renegotiation_supported();
142}
143
144void Server::to_peer(std::span<const uint8_t> data) {
145 m_impl->to_peer(data);
146}
147
148void Server::send_alert(const Alert& alert) {
149 m_impl->send_alert(alert);
150}
151
153 m_impl->send_warning_alert(type);
154}
155
157 m_impl->send_fatal_alert(type);
158}
159
161 m_impl->close();
162}
163
165 return m_impl->timeout_check();
166}
167
168std::string Server::application_protocol() const {
169 return m_impl->application_protocol();
170}
171} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:144
AlertType Type
Definition tls_alert.h:72
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