blob: 92f29067f3b5c0313079057bcc9839d3c26e242b [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraud612b55b2023-05-29 10:42:04 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraud612b55b2023-05-29 10:42:04 -04007 * (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éraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraud612b55b2023-05-29 10:42:04 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Adrien Béraudcb753622023-07-17 22:32:49 -040015 * along with this program. If not, see <https://www.gnu.org/licenses/>.
Adrien Béraud612b55b2023-05-29 10:42:04 -040016 */
Adrien Béraud612b55b2023-05-29 10:42:04 -040017#pragma once
18
19#include <gnutls/gnutls.h>
20
21#include <vector>
22#include <memory>
23#include <cstdint>
24#include <string>
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040025#include <filesystem>
Adrien Béraud612b55b2023-05-29 10:42:04 -040026
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040027namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040028namespace tls {
29
30class DhParams
31{
32public:
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éraud2a4e73d2023-08-27 12:53:55 -040061 static DhParams loadDhParams(const std::filesystem::path& path);
Adrien Béraud612b55b2023-05-29 10:42:04 -040062
63private:
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 Blin464bdff2023-07-19 08:02:53 -040069} // namespace dhtnet