blob: 56fc06422250039208f76dc54ddee52646e94abe [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
4 * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
5 * Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#pragma once
23
24#include <gnutls/gnutls.h>
25
26#include <vector>
27#include <memory>
28#include <cstdint>
29#include <string>
30
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040031namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040032namespace tls {
33
34class DhParams
35{
36public:
37 DhParams() = default;
38 DhParams(DhParams&&) = default;
39 DhParams(const DhParams& other) { *this = other; }
40
41 DhParams& operator=(DhParams&& other) = default;
42 DhParams& operator=(const DhParams& other);
43
44 /// \brief Construct by taking ownership of given gnutls DH params
45 ///
46 /// User should not call gnutls_dh_params_deinit on given \a raw_params.
47 /// The object is stolen and its live is manager by our object.
48 explicit DhParams(gnutls_dh_params_t p)
49 : params_ {p, gnutls_dh_params_deinit}
50 {}
51
52 /** Deserialize DER or PEM encoded DH-params */
53 DhParams(const std::vector<uint8_t>& data);
54
55 gnutls_dh_params_t get() { return params_.get(); }
56 gnutls_dh_params_t get() const { return params_.get(); }
57
58 explicit inline operator bool() const { return bool(params_); }
59
60 /** Serialize data in PEM format */
61 std::vector<uint8_t> serialize() const;
62
63 static DhParams generate();
64
65 static DhParams loadDhParams(const std::string& path);
66
67private:
68 std::unique_ptr<gnutls_dh_params_int, decltype(gnutls_dh_params_deinit)*>
69 params_ {nullptr, gnutls_dh_params_deinit};
70};
71
72} // namespace tls
73} // namespace jami