Kea: botan_tls.h Source File

| Kea 3.2.0-git

botan_tls.h
Go to the documentation of this file.
1// Copyright (C) 2021-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7// Do not include this header directly: use crypto_tls.h instead.
8
9#ifndef BOTAN_TLS_H
10#define BOTAN_TLS_H
11
13
14#ifdef WITH_BOTAN
15
16#include <asiolink/asio_wrapper.h>
17#include <asiolink/io_asio_socket.h>
18#include <asiolink/io_service.h>
19#include <asiolink/common_tls.h>
20#include <exceptions/exceptions.h>
21
22#include <asiolink/botan_wrapper.h>
23
24namespace isc {
25namespace asiolink {
26
28inline Botan::TLS::Connection_Side roleToImpl(TlsRole role) {
29 if (role == TlsRole::SERVER) {
30 return (Botan::TLS::Connection_Side::Server);
31 } else {
32 return (Botan::TLS::Connection_Side::Client);
33 }
34}
35
37class TlsContextImpl;
38
40class TlsContext : public TlsContextBase {
41public:
42
47 virtual ~TlsContext();
48
52 explicit TlsContext(TlsRole role);
53
55 std::shared_ptr<Botan::TLS::Context> getContext();
56
61 virtual bool getCertRequired() const;
62
63protected:
68 virtual void setCertRequired(bool cert_required);
69
73 virtual void loadCaFile(const std::string& ca_file);
74
78 virtual void loadCaPath(const std::string& ca_path);
79
83 virtual void loadCertFile(const std::string& cert_file);
84
88 virtual void loadKeyFile(const std::string& key_file);
89
91 std::unique_ptr<TlsContextImpl> impl_;
92
94 friend class TlsContextBase;
95};
96
98typedef Botan::TLS::Stream<boost::asio::ip::tcp::socket> TlsStreamImpl;
99
107template <typename Callback, typename TlsStreamImpl>
108 TlsStreamBase<Callback, TlsStreamImpl>::
109 TlsStreamBase(const IOServicePtr& io_service, TlsContextPtr context)
110 : StreamService(io_service, context),
111 TlsStreamImpl(io_service->getInternalIOService(),
112 context->getContext()), role_(context->getRole()) {
113}
114
118template <typename Callback>
119class TlsStream : public TlsStreamBase<Callback, TlsStreamImpl>
120{
121public:
122
124 typedef TlsStreamBase<Callback, TlsStreamImpl> Base;
125
131 TlsStream(const IOServicePtr& service, TlsContextPtr context)
132 : Base(service, context) {
133 }
134
136 virtual ~TlsStream() { }
137
141 virtual void handshake(Callback& callback) {
142 Base::async_handshake(roleToImpl(Base::getRole()), callback);
143 }
144
148 virtual void shutdown(Callback& callback) {
149 Base::async_shutdown(callback);
150 }
151
157 virtual void clear() {
158 }
159
169 virtual std::string getSubject() {
170 const std::vector<Botan::X509_Certificate>& cert_chain =
171 Base::native_handle()->peer_cert_chain();
172 if (cert_chain.empty()) {
173 return ("");
174 }
175 const Botan::X509_DN& subject = cert_chain[0].subject_dn();
176 return (subject.get_first_attribute("CommonName"));
177 }
178
188 virtual std::string getIssuer() {
189 const std::vector<Botan::X509_Certificate>& cert_chain =
190 Base::native_handle()->peer_cert_chain();
191 if (cert_chain.empty()) {
192 return ("");
193 }
194 const Botan::X509_DN& issuer = cert_chain[0].issuer_dn();
195 return (issuer.get_first_attribute("CommonName"));
196 }
197};
198
199// Stream truncated error code.
200const int STREAM_TRUNCATED = Botan::TLS::StreamError::StreamTruncated;
201
202} // namespace asiolink
203} // namespace isc
204
205#endif // WITH_BOTAN
206
207#endif // BOTAN_TLS_H
asio_wrapper.h
botan_wrapper.h
Botan ASIO wrapper.
isc::asiolink::StreamService
Definition common_tls.h:121
isc::asiolink::TlsContextBase
TLS context base class.
Definition common_tls.h:40
isc::asiolink::TlsStreamBase::TlsStreamBase
TlsStreamBase(const IOServicePtr &service, TlsContextPtr context)
Constructor.
common_tls.h
Common TLS API.
exceptions.h
io_asio_socket.h
io_service.h
isc::asiolink
Definition addr_utilities.cc:170
isc::asiolink::TlsContextPtr
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
Definition common_tls.h:37
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc::asiolink::TlsRole
TlsRole
Client and server roles.
Definition common_tls.h:31
isc::asiolink::SERVER
@ SERVER
Definition common_tls.h:31
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18