blob: 450a7a0c55c3030c073327c183678ca030e34556 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
4 * Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include "diffie-hellman.h"
22#include "logger.h"
23#include "fileutils.h"
24
25#include <chrono>
26#include <ciso646>
27
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040028namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040029namespace tls {
30
31DhParams::DhParams(const std::vector<uint8_t>& data)
32{
33 gnutls_dh_params_t new_params_;
34 int ret = gnutls_dh_params_init(&new_params_);
35 if (ret)
36 throw std::runtime_error(std::string("Error initializing DH params: ")
37 + gnutls_strerror(ret));
38 params_.reset(new_params_);
39 const gnutls_datum_t dat {(uint8_t*) data.data(), (unsigned) data.size()};
40 if (int ret_pem = gnutls_dh_params_import_pkcs3(params_.get(), &dat, GNUTLS_X509_FMT_PEM))
41 if (int ret_der = gnutls_dh_params_import_pkcs3(params_.get(), &dat, GNUTLS_X509_FMT_DER))
42 throw std::runtime_error(std::string("Error importing DH params: ")
43 + gnutls_strerror(ret_pem) + " " + gnutls_strerror(ret_der));
44}
45
46DhParams&
47DhParams::operator=(const DhParams& other)
48{
49 if (not params_) {
50 // We need a valid DH params pointer for the copy
51 gnutls_dh_params_t new_params_;
52 auto err = gnutls_dh_params_init(&new_params_);
53 if (err != GNUTLS_E_SUCCESS)
54 throw std::runtime_error(std::string("Error initializing DH params: ")
55 + gnutls_strerror(err));
56 params_.reset(new_params_);
57 }
58
59 auto err = gnutls_dh_params_cpy(params_.get(), other.get());
60 if (err != GNUTLS_E_SUCCESS)
61 throw std::runtime_error(std::string("Error copying DH params: ") + gnutls_strerror(err));
62
63 return *this;
64}
65
66std::vector<uint8_t>
67DhParams::serialize() const
68{
69 if (!params_) {
Morteza Namvar5f639522023-07-04 17:08:58 -040070 // JAMI_WARN("serialize() called on an empty DhParams");
Adrien Béraud612b55b2023-05-29 10:42:04 -040071 return {};
72 }
73 gnutls_datum_t out;
74 if (gnutls_dh_params_export2_pkcs3(params_.get(), GNUTLS_X509_FMT_PEM, &out))
75 return {};
76 std::vector<uint8_t> ret {out.data, out.data + out.size};
77 gnutls_free(out.data);
78 return ret;
79}
80
81DhParams
82DhParams::generate()
83{
84 using clock = std::chrono::high_resolution_clock;
85
86 auto bits = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH,
87 /* GNUTLS_SEC_PARAM_HIGH */ GNUTLS_SEC_PARAM_HIGH);
Morteza Namvar5f639522023-07-04 17:08:58 -040088 // JAMI_DBG("Generating DH params with %u bits", bits);
Adrien Béraud612b55b2023-05-29 10:42:04 -040089 auto start = clock::now();
90
91 gnutls_dh_params_t new_params_;
92 int ret = gnutls_dh_params_init(&new_params_);
93 if (ret != GNUTLS_E_SUCCESS) {
Morteza Namvar5f639522023-07-04 17:08:58 -040094 // JAMI_ERR("Error initializing DH params: %s", gnutls_strerror(ret));
Adrien Béraud612b55b2023-05-29 10:42:04 -040095 return {};
96 }
97 DhParams params {new_params_};
98
99 ret = gnutls_dh_params_generate2(params.get(), bits);
100 if (ret != GNUTLS_E_SUCCESS) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400101 // JAMI_ERR("Error generating DH params: %s", gnutls_strerror(ret));
Adrien Béraud612b55b2023-05-29 10:42:04 -0400102 return {};
103 }
104
105 std::chrono::duration<double> time_span = clock::now() - start;
Morteza Namvar5f639522023-07-04 17:08:58 -0400106 // JAMI_DBG("Generated DH params with %u bits in %lfs", bits, time_span.count());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400107 return params;
108}
109
110DhParams
111DhParams::loadDhParams(const std::string& path)
112{
113 std::lock_guard<std::mutex> l(fileutils::getFileLock(path));
114 try {
115 // writeTime throw exception if file doesn't exist
116 auto duration = std::chrono::system_clock::now() - fileutils::writeTime(path);
117 if (duration >= std::chrono::hours(24 * 3)) // file is valid only 3 days
118 throw std::runtime_error("file too old");
119
Morteza Namvar5f639522023-07-04 17:08:58 -0400120 // JAMI_DBG("Loading DhParams from file '%s'", path.c_str());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400121 return {fileutils::loadFile(path)};
122 } catch (const std::exception& e) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400123 // JAMI_DBG("Failed to load DhParams file '%s': %s", path.c_str(), e.what());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400124 if (auto params = tls::DhParams::generate()) {
125 try {
126 fileutils::saveFile(path, params.serialize(), 0600);
Morteza Namvar5f639522023-07-04 17:08:58 -0400127 // JAMI_DBG("Saved DhParams to file '%s'", path.c_str());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400128 } catch (const std::exception& ex) {
Morteza Namvar5f639522023-07-04 17:08:58 -0400129 // JAMI_WARN("Failed to save DhParams in file '%s': %s", path.c_str(), ex.what());
Adrien Béraud612b55b2023-05-29 10:42:04 -0400130 }
131 return params;
132 }
Morteza Namvar5f639522023-07-04 17:08:58 -0400133 // JAMI_ERR("Can't generate DH params.");
Adrien Béraud612b55b2023-05-29 10:42:04 -0400134 return {};
135 }
136}
137
138} // namespace tls
139} // namespace jami