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