blob: 78f901fbb2afe8f3d5e342e140095aeb3d9c912a [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#include "diffie-hellman.h"
Adrien Béraud612b55b2023-05-29 10:42:04 -040018#include "fileutils.h"
19
20#include <chrono>
21#include <ciso646>
22
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040023namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040024namespace tls {
25
26DhParams::DhParams(const std::vector<uint8_t>& data)
27{
28 gnutls_dh_params_t new_params_;
29 int ret = gnutls_dh_params_init(&new_params_);
30 if (ret)
31 throw std::runtime_error(std::string("Error initializing DH params: ")
32 + gnutls_strerror(ret));
33 params_.reset(new_params_);
34 const gnutls_datum_t dat {(uint8_t*) data.data(), (unsigned) data.size()};
35 if (int ret_pem = gnutls_dh_params_import_pkcs3(params_.get(), &dat, GNUTLS_X509_FMT_PEM))
36 if (int ret_der = gnutls_dh_params_import_pkcs3(params_.get(), &dat, GNUTLS_X509_FMT_DER))
37 throw std::runtime_error(std::string("Error importing DH params: ")
38 + gnutls_strerror(ret_pem) + " " + gnutls_strerror(ret_der));
39}
40
41DhParams&
42DhParams::operator=(const DhParams& other)
43{
44 if (not params_) {
45 // We need a valid DH params pointer for the copy
46 gnutls_dh_params_t new_params_;
47 auto err = gnutls_dh_params_init(&new_params_);
48 if (err != GNUTLS_E_SUCCESS)
49 throw std::runtime_error(std::string("Error initializing DH params: ")
50 + gnutls_strerror(err));
51 params_.reset(new_params_);
52 }
53
54 auto err = gnutls_dh_params_cpy(params_.get(), other.get());
55 if (err != GNUTLS_E_SUCCESS)
56 throw std::runtime_error(std::string("Error copying DH params: ") + gnutls_strerror(err));
57
58 return *this;
59}
60
61std::vector<uint8_t>
62DhParams::serialize() const
63{
64 if (!params_) {
Morteza Namvar5f639522023-07-04 17:08:58 -040065 // JAMI_WARN("serialize() called on an empty DhParams");
Adrien Béraud612b55b2023-05-29 10:42:04 -040066 return {};
67 }
68 gnutls_datum_t out;
69 if (gnutls_dh_params_export2_pkcs3(params_.get(), GNUTLS_X509_FMT_PEM, &out))
70 return {};
71 std::vector<uint8_t> ret {out.data, out.data + out.size};
72 gnutls_free(out.data);
73 return ret;
74}
75
76DhParams
77DhParams::generate()
78{
79 using clock = std::chrono::high_resolution_clock;
80
81 auto bits = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH,
82 /* GNUTLS_SEC_PARAM_HIGH */ GNUTLS_SEC_PARAM_HIGH);
Morteza Namvar5f639522023-07-04 17:08:58 -040083 // JAMI_DBG("Generating DH params with %u bits", bits);
Adrien Béraud612b55b2023-05-29 10:42:04 -040084 auto start = clock::now();
85
86 gnutls_dh_params_t new_params_;
87 int ret = gnutls_dh_params_init(&new_params_);
88 if (ret != GNUTLS_E_SUCCESS) {
Morteza Namvar5f639522023-07-04 17:08:58 -040089 // JAMI_ERR("Error initializing DH params: %s", gnutls_strerror(ret));
Adrien Béraud612b55b2023-05-29 10:42:04 -040090 return {};
91 }
92 DhParams params {new_params_};
93
94 ret = gnutls_dh_params_generate2(params.get(), bits);
95 if (ret != GNUTLS_E_SUCCESS) {
Morteza Namvar5f639522023-07-04 17:08:58 -040096 // JAMI_ERR("Error generating DH params: %s", gnutls_strerror(ret));
Adrien Béraud612b55b2023-05-29 10:42:04 -040097 return {};
98 }
99
100 std::chrono::duration<double> time_span = clock::now() - start;
Morteza Namvar5f639522023-07-04 17:08:58 -0400101 // JAMI_DBG("Generated DH params with %u bits in %lfs", bits, time_span.count());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400102 return params;
103}
104
105DhParams
106DhParams::loadDhParams(const std::string& path)
107{
108 std::lock_guard<std::mutex> l(fileutils::getFileLock(path));
109 try {
110 // writeTime throw exception if file doesn't exist
111 auto duration = std::chrono::system_clock::now() - fileutils::writeTime(path);
112 if (duration >= std::chrono::hours(24 * 3)) // file is valid only 3 days
113 throw std::runtime_error("file too old");
114
Morteza Namvar5f639522023-07-04 17:08:58 -0400115 // JAMI_DBG("Loading DhParams from file '%s'", path.c_str());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400116 return {fileutils::loadFile(path)};
117 } catch (const std::exception& e) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400118 // JAMI_DBG("Failed to load DhParams file '%s': %s", path.c_str(), e.what());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400119 if (auto params = tls::DhParams::generate()) {
120 try {
121 fileutils::saveFile(path, params.serialize(), 0600);
Morteza Namvar5f639522023-07-04 17:08:58 -0400122 // JAMI_DBG("Saved DhParams to file '%s'", path.c_str());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400123 } catch (const std::exception& ex) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400124 // JAMI_WARN("Failed to save DhParams in file '%s': %s", path.c_str(), ex.what());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400125 }
126 return params;
127 }
Morteza Namvar5f639522023-07-04 17:08:58 -0400128 // JAMI_ERR("Can't generate DH params.");
Adrien Béraud612b55b2023-05-29 10:42:04 -0400129 return {};
130 }
131}
132
133} // namespace tls
Sébastien Blin464bdff2023-07-19 08:02:53 -0400134} // namespace dhtnet