|
| 1 | +/** |
| 2 | + * \file |
| 3 | + * \author Jakub Antonín Štigler <[email protected]> |
| 4 | + * \brief Helper functions for forming OpenSSL error messages. (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 <array> |
| 14 | +#include <sstream> |
| 15 | +#include <string> |
| 16 | + |
| 17 | +#include <openssl/err.h> |
| 18 | + |
| 19 | +namespace tcp_in { |
| 20 | +namespace tls { |
| 21 | + |
| 22 | +/** |
| 23 | + * @brief Throws exception with the given message appended with arguments and OpenSSL error message |
| 24 | + * based on error code. |
| 25 | + * @param code Error code of the OpenSSL message. |
| 26 | + * @param msg Message describing the error. |
| 27 | + * @throws `std::runtime_error` |
| 28 | + */ |
| 29 | +[[noreturn]] |
| 30 | +static void throw_ssl_err(int code, std::ostringstream msg) { |
| 31 | + std::array<char, 256> err; |
| 32 | + ERR_error_string_n(code, err.data(), err.size()); |
| 33 | + msg << err.data(); |
| 34 | + |
| 35 | + auto res = msg.str(); |
| 36 | + if (*res.rbegin() == '.') { |
| 37 | + res.pop_back(); |
| 38 | + res += ": "; |
| 39 | + } |
| 40 | + |
| 41 | + throw std::runtime_error(res + err.data()); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @brief Throws exception with the given message appended with arguments and OpenSSL error message |
| 46 | + * based on error code. |
| 47 | + * @param code Error code of the OpenSSL message. |
| 48 | + * @param msg Partial message describing the error. |
| 49 | + * @param arg Next argument to be appended to the message. |
| 50 | + * @param args This will be converted to string and appended to the message. |
| 51 | + * @throws `std::runtime_error` |
| 52 | + */ |
| 53 | +template<typename ARG, typename... ARGS> |
| 54 | +[[noreturn]] |
| 55 | +void throw_ssl_err(int code, std::ostringstream msg, ARG &&arg, ARGS &&...args) { |
| 56 | + msg << arg; |
| 57 | + throw_ssl_err(code, std::move(msg), std::forward<ARGS>(args)...); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * @brief Throws exception with the given message appended with arguments and OpenSSL error message |
| 62 | + * based on error code. |
| 63 | + * @param code Error code of the OpenSSL message. |
| 64 | + * @param what Message describing the error. |
| 65 | + * @param args This will be converted to string and appended to the message. |
| 66 | + * @throws `std::runtime_error` |
| 67 | + */ |
| 68 | +template<typename... ARGS> |
| 69 | +[[noreturn]] |
| 70 | +void throw_ssl_err(int code, const std::string &what, ARGS &&...args) { |
| 71 | + std::ostringstream res; |
| 72 | + res << what; |
| 73 | + throw_ssl_err(code, std::move(res), std::forward<ARGS>(args)...); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * @brief Throws exception with the given message appended with arguments and error message from top |
| 78 | + * of the OpenSSL error stack. The error is popped from the error stack. |
| 79 | + * @param what Message describing the error. |
| 80 | + * @param args This will be converted to string and appended to the message. |
| 81 | + * @throws `std::runtime_error` |
| 82 | + */ |
| 83 | +template<typename... ARGS> |
| 84 | +[[noreturn]] |
| 85 | +void throw_ssl_err(const std::string &what, ARGS &&...args) { |
| 86 | + throw_ssl_err(ERR_get_error(), what, std::forward<ARGS>(args)...); |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace tls |
| 90 | +} // namespace tcp_in |
0 commit comments