Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 4 | * This program is free software: you can redistribute it and/or modify |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 5 | * it under the terms of the GNU General Public License as published by |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 6 | * the Free Software Foundation, either version 3 of the License, or |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 16 | */ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include <gnutls/gnutls.h> |
| 20 | |
| 21 | #include <vector> |
| 22 | #include <memory> |
| 23 | #include <cstdint> |
| 24 | #include <string> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 25 | #include <filesystem> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 26 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 27 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 28 | namespace tls { |
| 29 | |
| 30 | class DhParams |
| 31 | { |
| 32 | public: |
| 33 | DhParams() = default; |
| 34 | DhParams(DhParams&&) = default; |
| 35 | DhParams(const DhParams& other) { *this = other; } |
| 36 | |
| 37 | DhParams& operator=(DhParams&& other) = default; |
| 38 | DhParams& operator=(const DhParams& other); |
| 39 | |
| 40 | /// \brief Construct by taking ownership of given gnutls DH params |
| 41 | /// |
| 42 | /// User should not call gnutls_dh_params_deinit on given \a raw_params. |
| 43 | /// The object is stolen and its live is manager by our object. |
| 44 | explicit DhParams(gnutls_dh_params_t p) |
| 45 | : params_ {p, gnutls_dh_params_deinit} |
| 46 | {} |
| 47 | |
| 48 | /** Deserialize DER or PEM encoded DH-params */ |
| 49 | DhParams(const std::vector<uint8_t>& data); |
| 50 | |
| 51 | gnutls_dh_params_t get() { return params_.get(); } |
| 52 | gnutls_dh_params_t get() const { return params_.get(); } |
| 53 | |
| 54 | explicit inline operator bool() const { return bool(params_); } |
| 55 | |
| 56 | /** Serialize data in PEM format */ |
| 57 | std::vector<uint8_t> serialize() const; |
| 58 | |
| 59 | static DhParams generate(); |
| 60 | |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 61 | static DhParams loadDhParams(const std::filesystem::path& path); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | std::unique_ptr<gnutls_dh_params_int, decltype(gnutls_dh_params_deinit)*> |
| 65 | params_ {nullptr, gnutls_dh_params_deinit}; |
| 66 | }; |
| 67 | |
| 68 | } // namespace tls |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 69 | } // namespace dhtnet |