| 1 | /************************************************************************************ |
| 2 | * |
| 3 | * D++, A Lightweight C++ library for Discord |
| 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * Copyright 2021 Craig Edwards and D++ contributors |
| 7 | * (https://github.com/brainboxdotcc/DPP/graphs/contributors) |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | * you may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | ************************************************************************************/ |
| 22 | #include <dpp/exception.h> |
| 23 | #include <openssl/ssl.h> |
| 24 | #include <openssl/err.h> |
| 25 | #include <string> |
| 26 | #pragma once |
| 27 | |
| 28 | namespace dpp::detail { |
| 29 | |
| 30 | /** |
| 31 | * @brief This class wraps a raw SSL_CTX pointer, managing moving, |
| 32 | * creation, and RAII destruction. |
| 33 | */ |
| 34 | struct wrapped_ssl_ctx { |
| 35 | |
| 36 | /** |
| 37 | * @brief SSL_CTX pointer, raw C pointer nastiness |
| 38 | */ |
| 39 | SSL_CTX *context{nullptr}; |
| 40 | |
| 41 | /** |
| 42 | * @brief Get last SSL error message |
| 43 | * @return SSL error message |
| 44 | */ |
| 45 | std::string get_ssl_error() { |
| 46 | unsigned long error_code = ERR_get_error(); |
| 47 | if (error_code == 0) { |
| 48 | return "No error"; |
| 49 | } |
| 50 | char error_buffer[1024]{0}; |
| 51 | ERR_error_string_n(error_code, error_buffer, sizeof(error_buffer)); |
| 52 | return std::string(error_buffer); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @brief Create a wrapped SSL context |
| 57 | * @param is_server true to create a server context, false to create a client context |
| 58 | * @throws dpp::connection_exception if context could not be created |
| 59 | */ |
| 60 | explicit wrapped_ssl_ctx(bool is_server = false) : context(SSL_CTX_new(is_server ? TLS_server_method() : TLS_client_method())) { |
| 61 | if (context == nullptr) { |
| 62 | throw dpp::connection_exception(err_ssl_context, "Failed to create SSL client context: " + get_ssl_error()); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @brief Copy constructor |
| 68 | * @note Intentionally deleted |
| 69 | */ |
| 70 | wrapped_ssl_ctx(const wrapped_ssl_ctx&) = delete; |
| 71 | |
| 72 | /** |
| 73 | * @brief Copy assignment operator |
| 74 | * @note Intentionally deleted |
| 75 | */ |
| 76 | wrapped_ssl_ctx& operator=(const wrapped_ssl_ctx&) = delete; |
| 77 | |
| 78 | /** |
| 79 | * @brief Move constructor |
| 80 | * @param other source context |
| 81 | */ |
| 82 | wrapped_ssl_ctx(wrapped_ssl_ctx&& other) noexcept : context(other.context) { |
| 83 | other.context = nullptr; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @brief Move assignment operator |
| 88 | * @param other source context |
| 89 | * @return self |
| 90 | */ |
| 91 | wrapped_ssl_ctx& operator=(wrapped_ssl_ctx&& other) noexcept { |
| 92 | if (this != &other) { |
| 93 | /* Free current context if any and transfer ownership */ |
| 94 | SSL_CTX_free(context); |
| 95 | context = other.context; |
| 96 | other.context = nullptr; |
| 97 | } |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | ~wrapped_ssl_ctx() { |
| 102 | SSL_CTX_free(context); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | }; |