blob: 216e23380d6d5910cad2690527088f4fe2de052b [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>
Adrien Béraud2a4e73d2023-08-27 12:53:55 -040022#include <filesystem>
Adrien Béraud612b55b2023-05-29 10:42:04 -040023
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040024namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040025namespace tls {
26
27DhParams::DhParams(const std::vector<uint8_t>& data)
28{
29 gnutls_dh_params_t new_params_;
30 int ret = gnutls_dh_params_init(&new_params_);
31 if (ret)
32 throw std::runtime_error(std::string("Error initializing DH params: ")
33 + gnutls_strerror(ret));
34 params_.reset(new_params_);
35 const gnutls_datum_t dat {(uint8_t*) data.data(), (unsigned) data.size()};
36 if (int ret_pem = gnutls_dh_params_import_pkcs3(params_.get(), &dat, GNUTLS_X509_FMT_PEM))
37 if (int ret_der = gnutls_dh_params_import_pkcs3(params_.get(), &dat, GNUTLS_X509_FMT_DER))
38 throw std::runtime_error(std::string("Error importing DH params: ")
39 + gnutls_strerror(ret_pem) + " " + gnutls_strerror(ret_der));
40}
41
42DhParams&
43DhParams::operator=(const DhParams& other)
44{
45 if (not params_) {
46 // We need a valid DH params pointer for the copy
47 gnutls_dh_params_t new_params_;
48 auto err = gnutls_dh_params_init(&new_params_);
49 if (err != GNUTLS_E_SUCCESS)
50 throw std::runtime_error(std::string("Error initializing DH params: ")
51 + gnutls_strerror(err));
52 params_.reset(new_params_);
53 }
54
55 auto err = gnutls_dh_params_cpy(params_.get(), other.get());
56 if (err != GNUTLS_E_SUCCESS)
57 throw std::runtime_error(std::string("Error copying DH params: ") + gnutls_strerror(err));
58
59 return *this;
60}
61
62std::vector<uint8_t>
63DhParams::serialize() const
64{
65 if (!params_) {
Morteza Namvar5f639522023-07-04 17:08:58 -040066 // JAMI_WARN("serialize() called on an empty DhParams");
Adrien Béraud612b55b2023-05-29 10:42:04 -040067 return {};
68 }
69 gnutls_datum_t out;
70 if (gnutls_dh_params_export2_pkcs3(params_.get(), GNUTLS_X509_FMT_PEM, &out))
71 return {};
72 std::vector<uint8_t> ret {out.data, out.data + out.size};
73 gnutls_free(out.data);
74 return ret;
75}
76
77DhParams
78DhParams::generate()
79{
80 using clock = std::chrono::high_resolution_clock;
81
82 auto bits = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH,
83 /* GNUTLS_SEC_PARAM_HIGH */ GNUTLS_SEC_PARAM_HIGH);
Morteza Namvar5f639522023-07-04 17:08:58 -040084 // JAMI_DBG("Generating DH params with %u bits", bits);
Adrien Béraud612b55b2023-05-29 10:42:04 -040085 auto start = clock::now();
86
87 gnutls_dh_params_t new_params_;
88 int ret = gnutls_dh_params_init(&new_params_);
89 if (ret != GNUTLS_E_SUCCESS) {
Morteza Namvar5f639522023-07-04 17:08:58 -040090 // JAMI_ERR("Error initializing DH params: %s", gnutls_strerror(ret));
Adrien Béraud612b55b2023-05-29 10:42:04 -040091 return {};
92 }
93 DhParams params {new_params_};
94
95 ret = gnutls_dh_params_generate2(params.get(), bits);
96 if (ret != GNUTLS_E_SUCCESS) {
Morteza Namvar5f639522023-07-04 17:08:58 -040097 // JAMI_ERR("Error generating DH params: %s", gnutls_strerror(ret));
Adrien Béraud612b55b2023-05-29 10:42:04 -040098 return {};
99 }
100
101 std::chrono::duration<double> time_span = clock::now() - start;
Morteza Namvar5f639522023-07-04 17:08:58 -0400102 // JAMI_DBG("Generated DH params with %u bits in %lfs", bits, time_span.count());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400103 return params;
104}
105
106DhParams
Adrien Béraud2a4e73d2023-08-27 12:53:55 -0400107DhParams::loadDhParams(const std::filesystem::path& path)
Adrien Béraud612b55b2023-05-29 10:42:04 -0400108{
109 std::lock_guard<std::mutex> l(fileutils::getFileLock(path));
110 try {
111 // writeTime throw exception if file doesn't exist
Adrien Béraud2a4e73d2023-08-27 12:53:55 -0400112 auto writeTime = std::filesystem::last_write_time(path);
113 auto duration = decltype(writeTime)::clock::now() - writeTime;
Adrien Béraud612b55b2023-05-29 10:42:04 -0400114 if (duration >= std::chrono::hours(24 * 3)) // file is valid only 3 days
115 throw std::runtime_error("file too old");
116
Morteza Namvar5f639522023-07-04 17:08:58 -0400117 // JAMI_DBG("Loading DhParams from file '%s'", path.c_str());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400118 return {fileutils::loadFile(path)};
119 } catch (const std::exception& e) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400120 // JAMI_DBG("Failed to load DhParams file '%s': %s", path.c_str(), e.what());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400121 if (auto params = tls::DhParams::generate()) {
122 try {
123 fileutils::saveFile(path, params.serialize(), 0600);
Morteza Namvar5f639522023-07-04 17:08:58 -0400124 // JAMI_DBG("Saved DhParams to file '%s'", path.c_str());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400125 } catch (const std::exception& ex) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400126 // JAMI_WARN("Failed to save DhParams in file '%s': %s", path.c_str(), ex.what());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400127 }
128 return params;
129 }
Morteza Namvar5f639522023-07-04 17:08:58 -0400130 // JAMI_ERR("Can't generate DH params.");
Adrien Béraud612b55b2023-05-29 10:42:04 -0400131 return {};
132 }
133}
134
135} // namespace tls
Sébastien Blin464bdff2023-07-19 08:02:53 -0400136} // namespace dhtnet