Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 4 | * This program is free software: you can redistribute it and/or modify |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 5 | * it under the terms of the GNU General Public License as published by |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 6 | * the Free Software Foundation, either version 3 of the License, or |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 7 | * (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éraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 16 | */ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 17 | #include "diffie-hellman.h" |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 18 | #include "fileutils.h" |
| 19 | |
| 20 | #include <chrono> |
| 21 | #include <ciso646> |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 22 | #include <filesystem> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 23 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 24 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 25 | namespace tls { |
| 26 | |
| 27 | DhParams::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 | |
| 42 | DhParams& |
| 43 | DhParams::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 | |
| 62 | std::vector<uint8_t> |
| 63 | DhParams::serialize() const |
| 64 | { |
| 65 | if (!params_) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 66 | // JAMI_WARN("serialize() called on an empty DhParams"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 67 | 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 | |
| 77 | DhParams |
| 78 | DhParams::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 Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 84 | // JAMI_DBG("Generating DH params with %u bits", bits); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 85 | 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 Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 90 | // JAMI_ERR("Error initializing DH params: %s", gnutls_strerror(ret)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 91 | return {}; |
| 92 | } |
| 93 | DhParams params {new_params_}; |
| 94 | |
| 95 | ret = gnutls_dh_params_generate2(params.get(), bits); |
| 96 | if (ret != GNUTLS_E_SUCCESS) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 97 | // JAMI_ERR("Error generating DH params: %s", gnutls_strerror(ret)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 98 | return {}; |
| 99 | } |
| 100 | |
| 101 | std::chrono::duration<double> time_span = clock::now() - start; |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 102 | // JAMI_DBG("Generated DH params with %u bits in %lfs", bits, time_span.count()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 103 | return params; |
| 104 | } |
| 105 | |
| 106 | DhParams |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 107 | DhParams::loadDhParams(const std::filesystem::path& path) |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 108 | { |
| 109 | std::lock_guard<std::mutex> l(fileutils::getFileLock(path)); |
| 110 | try { |
| 111 | // writeTime throw exception if file doesn't exist |
Adrien Béraud | 2a4e73d | 2023-08-27 12:53:55 -0400 | [diff] [blame] | 112 | auto writeTime = std::filesystem::last_write_time(path); |
| 113 | auto duration = decltype(writeTime)::clock::now() - writeTime; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 114 | if (duration >= std::chrono::hours(24 * 3)) // file is valid only 3 days |
| 115 | throw std::runtime_error("file too old"); |
| 116 | |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 117 | // JAMI_DBG("Loading DhParams from file '%s'", path.c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 118 | return {fileutils::loadFile(path)}; |
| 119 | } catch (const std::exception& e) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 120 | // JAMI_DBG("Failed to load DhParams file '%s': %s", path.c_str(), e.what()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 121 | if (auto params = tls::DhParams::generate()) { |
| 122 | try { |
| 123 | fileutils::saveFile(path, params.serialize(), 0600); |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 124 | // JAMI_DBG("Saved DhParams to file '%s'", path.c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 125 | } catch (const std::exception& ex) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 126 | // JAMI_WARN("Failed to save DhParams in file '%s': %s", path.c_str(), ex.what()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 127 | } |
| 128 | return params; |
| 129 | } |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 130 | // JAMI_ERR("Can't generate DH params."); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 131 | return {}; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } // namespace tls |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 136 | } // namespace dhtnet |