Skip to content

Commit a15c328

Browse files
committed
TCP input TLS - Add SslBio, wrapper around BIO.
1 parent b307fd6 commit a15c328

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* \file
3+
* \author Jakub Antonín Štigler <[email protected]>
4+
* \brief Wrapper around BIO from OpenSSL. (header file)
5+
* \date 2025
6+
*
7+
* Copyright: (C) 2023 CESNET, z.s.p.o.
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
#pragma once
12+
13+
#include <memory>
14+
15+
#include <openssl/bio.h>
16+
17+
#include "../UniqueFd.hpp"
18+
#include "throw_ssl_err.hpp"
19+
20+
namespace tcp_in {
21+
namespace tls {
22+
23+
/**
24+
* @brief Wrapper around `BIO` from OpenSSL.
25+
*
26+
* This is basically extension of file descriptor used in OpenSSL.
27+
*/
28+
class SslBio {
29+
public:
30+
/**
31+
* @brief Constructs new `SslBio` for the given `BIO_METHOD`.
32+
* @param method Method created for example by `BIO_s_socket()`.
33+
* @throws `std::runtime_error` on failure.
34+
*/
35+
SslBio(const BIO_METHOD *method) : m_bio(BIO_new(method)) {
36+
if (!m_bio.get()) {
37+
throw_ssl_err("Failed to create bio.");
38+
}
39+
}
40+
41+
/**
42+
* @brief Set file descriptor of the bio.
43+
* @param fd File descriptor for the bio. This will NOT take ownership of the file descriptor.
44+
*/
45+
void set_fd(int fd) noexcept { BIO_set_fd(m_bio.get(), fd, BIO_NOCLOSE); }
46+
47+
/**
48+
* @brief Gets the pointer to bio and releases ownership of it.
49+
* @return Pointer to the inner bio.
50+
*/
51+
BIO *release_ptr() noexcept { return m_bio.release(); }
52+
53+
private:
54+
/** Deleter for BIO. */
55+
class Deleter {
56+
public:
57+
void operator()(BIO *bio) { BIO_free(bio); }
58+
};
59+
60+
std::unique_ptr<BIO, Deleter> m_bio;
61+
};
62+
63+
64+
} // namespace tls
65+
} // namespace tcp_in

0 commit comments

Comments
 (0)