Adrien BĂ©raud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | namespace jami { |
| 29 | namespace tls { |
| 30 | |
| 31 | DhParams::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 | |
| 46 | DhParams& |
| 47 | DhParams::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 | |
| 66 | std::vector<uint8_t> |
| 67 | DhParams::serialize() const |
| 68 | { |
| 69 | if (!params_) { |
| 70 | JAMI_WARN("serialize() called on an empty DhParams"); |
| 71 | 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 | |
| 81 | DhParams |
| 82 | DhParams::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); |
| 88 | JAMI_DBG("Generating DH params with %u bits", bits); |
| 89 | 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) { |
| 94 | JAMI_ERR("Error initializing DH params: %s", gnutls_strerror(ret)); |
| 95 | return {}; |
| 96 | } |
| 97 | DhParams params {new_params_}; |
| 98 | |
| 99 | ret = gnutls_dh_params_generate2(params.get(), bits); |
| 100 | if (ret != GNUTLS_E_SUCCESS) { |
| 101 | JAMI_ERR("Error generating DH params: %s", gnutls_strerror(ret)); |
| 102 | return {}; |
| 103 | } |
| 104 | |
| 105 | std::chrono::duration<double> time_span = clock::now() - start; |
| 106 | JAMI_DBG("Generated DH params with %u bits in %lfs", bits, time_span.count()); |
| 107 | return params; |
| 108 | } |
| 109 | |
| 110 | DhParams |
| 111 | DhParams::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 | |
| 120 | JAMI_DBG("Loading DhParams from file '%s'", path.c_str()); |
| 121 | return {fileutils::loadFile(path)}; |
| 122 | } catch (const std::exception& e) { |
| 123 | JAMI_DBG("Failed to load DhParams file '%s': %s", path.c_str(), e.what()); |
| 124 | if (auto params = tls::DhParams::generate()) { |
| 125 | try { |
| 126 | fileutils::saveFile(path, params.serialize(), 0600); |
| 127 | JAMI_DBG("Saved DhParams to file '%s'", path.c_str()); |
| 128 | } catch (const std::exception& ex) { |
| 129 | JAMI_WARN("Failed to save DhParams in file '%s': %s", path.c_str(), ex.what()); |
| 130 | } |
| 131 | return params; |
| 132 | } |
| 133 | JAMI_ERR("Can't generate DH params."); |
| 134 | return {}; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | } // namespace tls |
| 139 | } // namespace jami |