Botan 3.11.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
16#if defined(BOTAN_HAS_TLS_12)
17 #include <botan/internal/tls_server_impl_12.h>
18#endif
19
20#if defined(BOTAN_HAS_TLS_13)
21 #include <botan/internal/tls_server_impl_13.h>
22#endif
23
24namespace Botan::TLS {
25
26/*
27* TLS Server Constructor
28*/
29Server::Server(const std::shared_ptr<Callbacks>& callbacks,
30 const std::shared_ptr<Session_Manager>& session_manager,
31 const std::shared_ptr<Credentials_Manager>& creds,
32 const std::shared_ptr<const Policy>& policy,
33 const std::shared_ptr<RandomNumberGenerator>& rng,
34 bool is_datagram,
35 size_t io_buf_sz) {
36 const auto max_version = policy->latest_supported_version(is_datagram);
37
38#if defined(BOTAN_HAS_TLS_13)
39 if(!max_version.is_pre_tls_13()) {
40 m_impl = std::make_unique<Server_Impl_13>(callbacks, session_manager, creds, policy, rng);
41
42 if(m_impl->expects_downgrade()) {
43 m_impl->set_io_buffer_size(io_buf_sz);
44 }
45
46 return;
47 }
48#endif
49
50#if defined(BOTAN_HAS_TLS_12)
51 if(max_version.is_pre_tls_13()) {
52 m_impl = std::make_unique<Server_Impl_12>(callbacks, session_manager, creds, policy, rng, is_datagram, io_buf_sz);
53 return;
54 }
55#endif
56
57 BOTAN_UNUSED(max_version, callbacks, session_manager, creds, policy, rng, is_datagram, io_buf_sz);
58 throw Not_Implemented("Requested TLS server version is not available in this build");
59}
60
61Server::~Server() = default;
62
63size_t Server::from_peer(std::span<const uint8_t> data) {
64 auto read = m_impl->from_peer(data);
65
66#if defined(BOTAN_HAS_TLS_12)
67 // If TLS 1.2 is not available, we will never downgrade, the downgrade info
68 // won't even be created and `is_downgrading()` would always return false.
69 if(m_impl->is_downgrading()) {
70 auto info = m_impl->extract_downgrade_info();
71 m_impl = std::make_unique<Server_Impl_12>(*info);
72
73 // replay peer data received so far
74 read = m_impl->from_peer(info->peer_transcript);
75 }
76#endif
77
78 return read;
79}
80
82 return m_impl->is_handshake_complete();
83}
84
85bool Server::is_active() const {
86 return m_impl->is_active();
87}
88
89bool Server::is_closed() const {
90 return m_impl->is_closed();
91}
92
94 return m_impl->is_closed_for_reading();
95}
96
98 return m_impl->is_closed_for_writing();
99}
100
101std::vector<X509_Certificate> Server::peer_cert_chain() const {
102 return m_impl->peer_cert_chain();
103}
104
105std::shared_ptr<const Public_Key> Server::peer_raw_public_key() const {
106 return m_impl->peer_raw_public_key();
107}
108
109std::optional<std::string> Server::external_psk_identity() const {
110 return m_impl->external_psk_identity();
111}
112
113SymmetricKey Server::key_material_export(std::string_view label, std::string_view context, size_t length) const {
114 return m_impl->key_material_export(label, context, length);
115}
116
117void Server::renegotiate(bool force_full_renegotiation) {
118 m_impl->renegotiate(force_full_renegotiation);
119}
120
122 return m_impl->new_session_ticket_supported();
123}
124
125size_t Server::send_new_session_tickets(const size_t tickets) {
126 return m_impl->send_new_session_tickets(tickets);
127}
128
129void Server::update_traffic_keys(bool request_peer_update) {
130 m_impl->update_traffic_keys(request_peer_update);
131}
132
134 return m_impl->secure_renegotiation_supported();
135}
136
137void Server::to_peer(std::span<const uint8_t> data) {
138 m_impl->to_peer(data);
139}
140
141void Server::send_alert(const Alert& alert) {
142 m_impl->send_alert(alert);
143}
144
146 m_impl->send_warning_alert(type);
147}
148
150 m_impl->send_fatal_alert(type);
151}
152
154 m_impl->close();
155}
156
158 return m_impl->timeout_check();
159}
160
161std::string Server::application_protocol() const {
162 return m_impl->application_protocol();
163}
164} // 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
OctetString SymmetricKey
Definition symkey.h:140