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> |
| 22 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 23 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 24 | namespace tls { |
| 25 | |
| 26 | DhParams::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 | |
| 41 | DhParams& |
| 42 | DhParams::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 | |
| 61 | std::vector<uint8_t> |
| 62 | DhParams::serialize() const |
| 63 | { |
| 64 | if (!params_) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 65 | // JAMI_WARN("serialize() called on an empty DhParams"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 66 | 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 | |
| 76 | DhParams |
| 77 | DhParams::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 Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 83 | // JAMI_DBG("Generating DH params with %u bits", bits); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 84 | 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 Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 89 | // JAMI_ERR("Error initializing DH params: %s", gnutls_strerror(ret)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 90 | return {}; |
| 91 | } |
| 92 | DhParams params {new_params_}; |
| 93 | |
| 94 | ret = gnutls_dh_params_generate2(params.get(), bits); |
| 95 | if (ret != GNUTLS_E_SUCCESS) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 96 | // JAMI_ERR("Error generating DH params: %s", gnutls_strerror(ret)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 97 | return {}; |
| 98 | } |
| 99 | |
| 100 | std::chrono::duration<double> time_span = clock::now() - start; |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 101 | // 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] | 102 | return params; |
| 103 | } |
| 104 | |
| 105 | DhParams |
| 106 | DhParams::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 Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 115 | // JAMI_DBG("Loading DhParams from file '%s'", path.c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 116 | return {fileutils::loadFile(path)}; |
| 117 | } catch (const std::exception& e) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 118 | // 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] | 119 | if (auto params = tls::DhParams::generate()) { |
| 120 | try { |
| 121 | fileutils::saveFile(path, params.serialize(), 0600); |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 122 | // JAMI_DBG("Saved DhParams to file '%s'", path.c_str()); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 123 | } catch (const std::exception& ex) { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 124 | // 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] | 125 | } |
| 126 | return params; |
| 127 | } |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 128 | // JAMI_ERR("Can't generate DH params."); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 129 | return {}; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } // namespace tls |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 134 | } // namespace dhtnet |