Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 3 | #include "bmcweb_config.h" |
| 4 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "logging.hpp" |
Ed Tanous | 2c6ffdb | 2023-06-28 11:28:38 -0700 | [diff] [blame] | 6 | #include "ossl_random.hpp" |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 7 | #include "persistent_data.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 8 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 9 | #include <boost/beast/core/file_posix.hpp> |
| 10 | |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 11 | extern "C" |
| 12 | { |
| 13 | #include <nghttp2/nghttp2.h> |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 14 | #include <openssl/bio.h> |
| 15 | #include <openssl/dh.h> |
| 16 | #include <openssl/dsa.h> |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 17 | #include <openssl/err.h> |
| 18 | #include <openssl/evp.h> |
| 19 | #include <openssl/pem.h> |
| 20 | #include <openssl/rand.h> |
| 21 | #include <openssl/rsa.h> |
| 22 | #include <openssl/ssl.h> |
Ed Tanous | 6dbe9be | 2024-04-14 10:24:20 -0700 | [diff] [blame] | 23 | } |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 24 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 25 | #include <boost/asio/ssl/context.hpp> |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 26 | #include <boost/system/error_code.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 27 | |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 28 | #include <filesystem> |
| 29 | #include <memory> |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 30 | #include <optional> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | #include <random> |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 32 | #include <string> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | |
| 34 | namespace ensuressl |
| 35 | { |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 36 | constexpr const char* trustStorePath = "/etc/ssl/certs/authority"; |
| 37 | constexpr const char* x509Comment = "Generated from OpenBMC service"; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 38 | static EVP_PKEY* createEcKey(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 39 | |
Ed Tanous | d3e9c3a | 2024-05-21 10:02:05 -0700 | [diff] [blame^] | 40 | // Mozilla intermediate cipher suites v5.7 |
| 41 | // Sourced from: https://ssl-config.mozilla.org/guidelines/5.7.json |
| 42 | constexpr const char* mozillaIntermediate = "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 43 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 44 | "ECDHE-ECDSA-AES256-GCM-SHA384:" |
| 45 | "ECDHE-RSA-AES256-GCM-SHA384:" |
| 46 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 47 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 48 | "DHE-RSA-AES128-GCM-SHA256:" |
| 49 | "DHE-RSA-AES256-GCM-SHA384:" |
| 50 | "DHE-RSA-CHACHA20-POLY1305"; |
| 51 | |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 52 | // Trust chain related errors.` |
| 53 | inline bool isTrustChainError(int errnum) |
| 54 | { |
Ed Tanous | 55f79e6 | 2022-01-25 11:26:16 -0800 | [diff] [blame] | 55 | return (errnum == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) || |
| 56 | (errnum == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) || |
| 57 | (errnum == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) || |
| 58 | (errnum == X509_V_ERR_CERT_UNTRUSTED) || |
| 59 | (errnum == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 62 | inline bool validateCertificate(X509* const cert) |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 63 | { |
| 64 | // Create an empty X509_STORE structure for certificate validation. |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 65 | X509_STORE* x509Store = X509_STORE_new(); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 66 | if (x509Store == nullptr) |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 67 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 68 | BMCWEB_LOG_ERROR("Error occurred during X509_STORE_new call"); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
| 72 | // Load Certificate file into the X509 structure. |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 73 | X509_STORE_CTX* storeCtx = X509_STORE_CTX_new(); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 74 | if (storeCtx == nullptr) |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 75 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 76 | BMCWEB_LOG_ERROR("Error occurred during X509_STORE_CTX_new call"); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 77 | X509_STORE_free(x509Store); |
| 78 | return false; |
| 79 | } |
| 80 | |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 81 | int errCode = X509_STORE_CTX_init(storeCtx, x509Store, cert, nullptr); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 82 | if (errCode != 1) |
| 83 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 84 | BMCWEB_LOG_ERROR("Error occurred during X509_STORE_CTX_init call"); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 85 | X509_STORE_CTX_free(storeCtx); |
| 86 | X509_STORE_free(x509Store); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | errCode = X509_verify_cert(storeCtx); |
| 91 | if (errCode == 1) |
| 92 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 93 | BMCWEB_LOG_INFO("Certificate verification is success"); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 94 | X509_STORE_CTX_free(storeCtx); |
| 95 | X509_STORE_free(x509Store); |
| 96 | return true; |
| 97 | } |
| 98 | if (errCode == 0) |
| 99 | { |
| 100 | errCode = X509_STORE_CTX_get_error(storeCtx); |
| 101 | X509_STORE_CTX_free(storeCtx); |
| 102 | X509_STORE_free(x509Store); |
| 103 | if (isTrustChainError(errCode)) |
| 104 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 105 | BMCWEB_LOG_DEBUG("Ignoring Trust Chain error. Reason: {}", |
| 106 | X509_verify_cert_error_string(errCode)); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 107 | return true; |
| 108 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 109 | BMCWEB_LOG_ERROR("Certificate verification failed. Reason: {}", |
| 110 | X509_verify_cert_error_string(errCode)); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 111 | return false; |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 114 | BMCWEB_LOG_ERROR( |
| 115 | "Error occurred during X509_verify_cert call. ErrorCode: {}", errCode); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 116 | X509_STORE_CTX_free(storeCtx); |
| 117 | X509_STORE_free(x509Store); |
| 118 | return false; |
| 119 | } |
| 120 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 121 | inline std::string verifyOpensslKeyCert(const std::string& filepath) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 | { |
| 123 | bool privateKeyValid = false; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 124 | |
Ed Tanous | c160ae7 | 2024-03-27 18:45:20 -0700 | [diff] [blame] | 125 | BMCWEB_LOG_INFO("Checking certs in file {}", filepath); |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 126 | boost::beast::file_posix file; |
| 127 | boost::system::error_code ec; |
| 128 | file.open(filepath.c_str(), boost::beast::file_mode::read, ec); |
| 129 | if (ec) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | { |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 131 | return ""; |
| 132 | } |
| 133 | bool certValid = false; |
| 134 | std::string fileContents; |
| 135 | fileContents.resize(static_cast<size_t>(file.size(ec)), '\0'); |
| 136 | file.read(fileContents.data(), fileContents.size(), ec); |
| 137 | if (ec) |
| 138 | { |
| 139 | BMCWEB_LOG_ERROR("Failed to read file"); |
| 140 | return ""; |
| 141 | } |
Patrick Williams | 145bb76 | 2021-12-07 21:05:04 -0600 | [diff] [blame] | 142 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 143 | BIO* bufio = BIO_new_mem_buf(static_cast<void*>(fileContents.data()), |
| 144 | static_cast<int>(fileContents.size())); |
| 145 | EVP_PKEY* pkey = PEM_read_bio_PrivateKey(bufio, nullptr, nullptr, nullptr); |
| 146 | BIO_free(bufio); |
| 147 | if (pkey != nullptr) |
| 148 | { |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 149 | EVP_PKEY_CTX* pkeyCtx = EVP_PKEY_CTX_new_from_pkey(nullptr, pkey, |
| 150 | nullptr); |
| 151 | |
| 152 | if (pkeyCtx == nullptr) |
| 153 | { |
| 154 | BMCWEB_LOG_ERROR("Unable to allocate pkeyCtx {}", ERR_get_error()); |
| 155 | } |
| 156 | else if (EVP_PKEY_check(pkeyCtx) == 1) |
| 157 | { |
| 158 | privateKeyValid = true; |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | BMCWEB_LOG_ERROR("Key not valid error number {}", ERR_get_error()); |
| 163 | } |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 164 | |
| 165 | if (privateKeyValid) |
| 166 | { |
| 167 | BIO* bufio2 = |
| 168 | BIO_new_mem_buf(static_cast<void*>(fileContents.data()), |
| 169 | static_cast<int>(fileContents.size())); |
| 170 | X509* x509 = PEM_read_bio_X509(bufio2, nullptr, nullptr, nullptr); |
| 171 | BIO_free(bufio2); |
| 172 | if (x509 == nullptr) |
| 173 | { |
| 174 | BMCWEB_LOG_ERROR("error getting x509 cert {}", ERR_get_error()); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | certValid = validateCertificate(x509); |
| 179 | X509_free(x509); |
| 180 | } |
| 181 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 182 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 183 | EVP_PKEY_CTX_free(pkeyCtx); |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 184 | EVP_PKEY_free(pkey); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 185 | } |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 186 | if (!certValid) |
| 187 | { |
| 188 | return ""; |
| 189 | } |
| 190 | return fileContents; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 193 | inline X509* loadCert(const std::string& filePath) |
| 194 | { |
| 195 | BIO* certFileBio = BIO_new_file(filePath.c_str(), "rb"); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 196 | if (certFileBio == nullptr) |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 197 | { |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 198 | BMCWEB_LOG_ERROR("Error occurred during BIO_new_file call, FILE= {}", |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 199 | filePath); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 200 | return nullptr; |
| 201 | } |
| 202 | |
| 203 | X509* cert = X509_new(); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 204 | if (cert == nullptr) |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 205 | { |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 206 | BMCWEB_LOG_ERROR("Error occurred during X509_new call, {}", |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 207 | ERR_get_error()); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 208 | BIO_free(certFileBio); |
| 209 | return nullptr; |
| 210 | } |
| 211 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 212 | if (PEM_read_bio_X509(certFileBio, &cert, nullptr, nullptr) == nullptr) |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 213 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 214 | BMCWEB_LOG_ERROR( |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 215 | "Error occurred during PEM_read_bio_X509 call, FILE= {}", filePath); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 216 | |
| 217 | BIO_free(certFileBio); |
| 218 | X509_free(cert); |
| 219 | return nullptr; |
| 220 | } |
Alan Kuo | b295bf9 | 2021-04-16 14:16:47 +0800 | [diff] [blame] | 221 | BIO_free(certFileBio); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 222 | return cert; |
| 223 | } |
| 224 | |
| 225 | inline int addExt(X509* cert, int nid, const char* value) |
| 226 | { |
| 227 | X509_EXTENSION* ex = nullptr; |
Ed Tanous | b00dcc2 | 2021-02-23 12:52:50 -0800 | [diff] [blame] | 228 | X509V3_CTX ctx{}; |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 229 | X509V3_set_ctx(&ctx, cert, cert, nullptr, nullptr, 0); |
Ed Tanous | 4ecc618 | 2022-01-07 09:36:26 -0800 | [diff] [blame] | 230 | |
| 231 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 232 | ex = X509V3_EXT_conf_nid(nullptr, &ctx, nid, const_cast<char*>(value)); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 233 | if (ex == nullptr) |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 234 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 235 | BMCWEB_LOG_ERROR("Error: In X509V3_EXT_conf_nidn: {}", value); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 236 | return -1; |
| 237 | } |
| 238 | X509_add_ext(cert, ex, -1); |
| 239 | X509_EXTENSION_free(ex); |
| 240 | return 0; |
| 241 | } |
| 242 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 243 | // Writes a certificate to a path, ignoring errors |
| 244 | inline void writeCertificateToFile(const std::string& filepath, |
| 245 | const std::string& certificate) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 246 | { |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 247 | boost::system::error_code ec; |
| 248 | boost::beast::file_posix file; |
| 249 | file.open(filepath.c_str(), boost::beast::file_mode::write, ec); |
| 250 | if (!ec) |
| 251 | { |
| 252 | file.write(certificate.data(), certificate.size(), ec); |
| 253 | // ignore result |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | inline std::string generateSslCertificate(const std::string& cn) |
| 258 | { |
Ed Tanous | c160ae7 | 2024-03-27 18:45:20 -0700 | [diff] [blame] | 259 | BMCWEB_LOG_INFO("Generating new keys"); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 260 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 261 | std::string buffer; |
Ed Tanous | c160ae7 | 2024-03-27 18:45:20 -0700 | [diff] [blame] | 262 | BMCWEB_LOG_INFO("Generating EC key"); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 263 | EVP_PKEY* pPrivKey = createEcKey(); |
Vernon Mauery | 0185c7f | 2020-03-09 10:56:53 -0700 | [diff] [blame] | 264 | if (pPrivKey != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 265 | { |
Ed Tanous | c160ae7 | 2024-03-27 18:45:20 -0700 | [diff] [blame] | 266 | BMCWEB_LOG_INFO("Generating x509 Certificates"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 267 | // Use this code to directly generate a certificate |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 268 | X509* x509 = X509_new(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 269 | if (x509 != nullptr) |
| 270 | { |
| 271 | // get a random number from the RNG for the certificate serial |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 272 | // number If this is not random, regenerating certs throws browser |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 273 | // errors |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 274 | bmcweb::OpenSSLGenerator gen; |
| 275 | std::uniform_int_distribution<int> dis( |
| 276 | 1, std::numeric_limits<int>::max()); |
| 277 | int serial = dis(gen); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 278 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 279 | ASN1_INTEGER_set(X509_get_serialNumber(x509), serial); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 280 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 281 | // not before this moment |
| 282 | X509_gmtime_adj(X509_get_notBefore(x509), 0); |
| 283 | // Cert is valid for 10 years |
| 284 | X509_gmtime_adj(X509_get_notAfter(x509), |
| 285 | 60L * 60L * 24L * 365L * 10L); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 286 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 287 | // set the public key to the key we just generated |
Vernon Mauery | 0185c7f | 2020-03-09 10:56:53 -0700 | [diff] [blame] | 288 | X509_set_pubkey(x509, pPrivKey); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 289 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 290 | // get the subject name |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 291 | X509_NAME* name = X509_get_subject_name(x509); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 292 | |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 293 | using x509String = const unsigned char; |
| 294 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 295 | x509String* country = reinterpret_cast<x509String*>("US"); |
| 296 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 297 | x509String* company = reinterpret_cast<x509String*>("OpenBMC"); |
| 298 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 299 | x509String* cnStr = reinterpret_cast<x509String*>(cn.c_str()); |
| 300 | |
| 301 | X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, country, -1, -1, |
| 302 | 0); |
| 303 | X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, company, -1, -1, |
| 304 | 0); |
| 305 | X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, cnStr, -1, -1, |
| 306 | 0); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 307 | // set the CSR options |
| 308 | X509_set_issuer_name(x509, name); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 309 | |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 310 | X509_set_version(x509, 2); |
| 311 | addExt(x509, NID_basic_constraints, ("critical,CA:TRUE")); |
| 312 | addExt(x509, NID_subject_alt_name, ("DNS:" + cn).c_str()); |
| 313 | addExt(x509, NID_subject_key_identifier, ("hash")); |
| 314 | addExt(x509, NID_authority_key_identifier, ("keyid")); |
| 315 | addExt(x509, NID_key_usage, ("digitalSignature, keyEncipherment")); |
| 316 | addExt(x509, NID_ext_key_usage, ("serverAuth")); |
| 317 | addExt(x509, NID_netscape_comment, (x509Comment)); |
| 318 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 319 | // Sign the certificate with our private key |
Vernon Mauery | 0185c7f | 2020-03-09 10:56:53 -0700 | [diff] [blame] | 320 | X509_sign(x509, pPrivKey, EVP_sha256()); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 321 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 322 | BIO* bufio = BIO_new(BIO_s_mem()); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 323 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 324 | int pkeyRet = PEM_write_bio_PrivateKey( |
| 325 | bufio, pPrivKey, nullptr, nullptr, 0, nullptr, nullptr); |
| 326 | if (pkeyRet <= 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 327 | { |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 328 | BMCWEB_LOG_ERROR( |
| 329 | "Failed to write pkey with code {}. Ignoring.", pkeyRet); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 330 | } |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 331 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 332 | char* data = nullptr; |
| 333 | long int dataLen = BIO_get_mem_data(bufio, &data); |
| 334 | buffer += std::string_view(data, static_cast<size_t>(dataLen)); |
| 335 | BIO_free(bufio); |
| 336 | |
| 337 | bufio = BIO_new(BIO_s_mem()); |
| 338 | pkeyRet = PEM_write_bio_X509(bufio, x509); |
| 339 | if (pkeyRet <= 0) |
| 340 | { |
| 341 | BMCWEB_LOG_ERROR( |
| 342 | "Failed to write X509 with code {}. Ignoring.", pkeyRet); |
| 343 | } |
| 344 | dataLen = BIO_get_mem_data(bufio, &data); |
| 345 | buffer += std::string_view(data, static_cast<size_t>(dataLen)); |
| 346 | |
| 347 | BIO_free(bufio); |
| 348 | BMCWEB_LOG_INFO("Cert size is {}", buffer.size()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 349 | X509_free(x509); |
| 350 | } |
| 351 | |
Vernon Mauery | 0185c7f | 2020-03-09 10:56:53 -0700 | [diff] [blame] | 352 | EVP_PKEY_free(pPrivKey); |
| 353 | pPrivKey = nullptr; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 356 | // cleanup_openssl(); |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 357 | return buffer; |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 358 | } |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 359 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 360 | EVP_PKEY* createEcKey() |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 361 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 362 | EVP_PKEY* pKey = nullptr; |
Patrick Williams | aec7066 | 2021-12-13 16:55:46 -0600 | [diff] [blame] | 363 | |
Patrick Williams | aec7066 | 2021-12-13 16:55:46 -0600 | [diff] [blame] | 364 | // Create context for curve parameter generation. |
| 365 | std::unique_ptr<EVP_PKEY_CTX, decltype(&::EVP_PKEY_CTX_free)> ctx{ |
| 366 | EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr), &::EVP_PKEY_CTX_free}; |
| 367 | if (!ctx) |
| 368 | { |
| 369 | return nullptr; |
| 370 | } |
| 371 | |
| 372 | // Set up curve parameters. |
| 373 | EVP_PKEY* params = nullptr; |
| 374 | if ((EVP_PKEY_paramgen_init(ctx.get()) <= 0) || |
| 375 | (EVP_PKEY_CTX_set_ec_param_enc(ctx.get(), OPENSSL_EC_NAMED_CURVE) <= |
| 376 | 0) || |
| 377 | (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(), NID_secp384r1) <= |
| 378 | 0) || |
| 379 | (EVP_PKEY_paramgen(ctx.get(), ¶ms) <= 0)) |
| 380 | { |
| 381 | return nullptr; |
| 382 | } |
| 383 | |
| 384 | // Set up RAII holder for params. |
| 385 | std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)> pparams{ |
| 386 | params, &::EVP_PKEY_free}; |
| 387 | |
| 388 | // Set new context for key generation, using curve parameters. |
| 389 | ctx.reset(EVP_PKEY_CTX_new_from_pkey(nullptr, params, nullptr)); |
| 390 | if (!ctx || (EVP_PKEY_keygen_init(ctx.get()) <= 0)) |
| 391 | { |
| 392 | return nullptr; |
| 393 | } |
| 394 | |
| 395 | // Generate key. |
| 396 | if (EVP_PKEY_keygen(ctx.get(), &pKey) <= 0) |
| 397 | { |
| 398 | return nullptr; |
| 399 | } |
Patrick Williams | aec7066 | 2021-12-13 16:55:46 -0600 | [diff] [blame] | 400 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 401 | return pKey; |
| 402 | } |
| 403 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 404 | inline std::string ensureOpensslKeyPresentAndValid(const std::string& filepath) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 405 | { |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 406 | std::string cert = verifyOpensslKeyCert(filepath); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 407 | |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 408 | if (cert.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 409 | { |
Ed Tanous | c160ae7 | 2024-03-27 18:45:20 -0700 | [diff] [blame] | 410 | BMCWEB_LOG_WARNING("Error in verifying signature, regenerating"); |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 411 | cert = generateSslCertificate("testhost"); |
| 412 | if (cert.empty()) |
| 413 | { |
| 414 | BMCWEB_LOG_ERROR("Failed to generate cert"); |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | writeCertificateToFile(filepath, cert); |
| 419 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 420 | } |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 421 | return cert; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 422 | } |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 423 | |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 424 | inline std::string ensureCertificate() |
| 425 | { |
| 426 | namespace fs = std::filesystem; |
| 427 | // Cleanup older certificate file existing in the system |
| 428 | fs::path oldcertPath = fs::path("/home/root/server.pem"); |
| 429 | std::error_code ec; |
| 430 | fs::remove(oldcertPath, ec); |
| 431 | // Ignore failure to remove; File might not exist. |
| 432 | |
| 433 | fs::path certPath = "/etc/ssl/certs/https/"; |
| 434 | // if path does not exist create the path so that |
| 435 | // self signed certificate can be created in the |
| 436 | // path |
| 437 | fs::path certFile = certPath / "server.pem"; |
| 438 | |
| 439 | if (!fs::exists(certPath, ec)) |
| 440 | { |
Andrew Geissler | 00a0fe4 | 2024-06-11 11:21:30 -0400 | [diff] [blame] | 441 | fs::create_directories(certPath, ec); |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 442 | } |
| 443 | BMCWEB_LOG_INFO("Building SSL Context file= {}", certFile.string()); |
| 444 | std::string sslPemFile(certFile); |
| 445 | return ensuressl::ensureOpensslKeyPresentAndValid(sslPemFile); |
| 446 | } |
| 447 | |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 448 | inline int nextProtoCallback(SSL* /*unused*/, const unsigned char** data, |
| 449 | unsigned int* len, void* /*unused*/) |
| 450 | { |
| 451 | // First byte is the length. |
| 452 | constexpr std::string_view h2 = "\x02h2"; |
| 453 | *data = std::bit_cast<const unsigned char*>(h2.data()); |
| 454 | *len = static_cast<unsigned int>(h2.size()); |
| 455 | return SSL_TLSEXT_ERR_OK; |
| 456 | } |
| 457 | |
| 458 | inline int alpnSelectProtoCallback(SSL* /*unused*/, const unsigned char** out, |
| 459 | unsigned char* outlen, |
| 460 | const unsigned char* in, unsigned int inlen, |
| 461 | void* /*unused*/) |
| 462 | { |
| 463 | // There's a mismatch in constness for nghttp2_select_next_protocol. The |
| 464 | // examples in nghttp2 don't show this problem. Unclear what the right fix |
| 465 | // is here. |
| 466 | |
| 467 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 468 | unsigned char** outNew = const_cast<unsigned char**>(out); |
| 469 | int rv = nghttp2_select_next_protocol(outNew, outlen, in, inlen); |
| 470 | if (rv != 1) |
| 471 | { |
| 472 | return SSL_TLSEXT_ERR_NOACK; |
| 473 | } |
| 474 | |
| 475 | return SSL_TLSEXT_ERR_OK; |
| 476 | } |
| 477 | |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 478 | inline bool getSslContext(boost::asio::ssl::context& mSslContext, |
| 479 | const std::string& sslPemFile) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 480 | { |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 481 | mSslContext.set_options(boost::asio::ssl::context::default_workarounds | |
| 482 | boost::asio::ssl::context::no_sslv2 | |
| 483 | boost::asio::ssl::context::no_sslv3 | |
| 484 | boost::asio::ssl::context::single_dh_use | |
| 485 | boost::asio::ssl::context::no_tlsv1 | |
| 486 | boost::asio::ssl::context::no_tlsv1_1); |
James Feist | 6a3e182 | 2019-11-06 13:46:35 -0800 | [diff] [blame] | 487 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 488 | BMCWEB_LOG_DEBUG("Using default TrustStore location: {}", trustStorePath); |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 489 | mSslContext.add_verify_path(trustStorePath); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 490 | |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 491 | if (!sslPemFile.empty()) |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 492 | { |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 493 | boost::system::error_code ec; |
| 494 | |
| 495 | boost::asio::const_buffer buf(sslPemFile.data(), sslPemFile.size()); |
| 496 | mSslContext.use_certificate(buf, boost::asio::ssl::context::pem, ec); |
| 497 | if (ec) |
| 498 | { |
| 499 | return false; |
| 500 | } |
| 501 | mSslContext.use_private_key(buf, boost::asio::ssl::context::pem, ec); |
| 502 | if (ec) |
| 503 | { |
| 504 | BMCWEB_LOG_CRITICAL("Failed to open ssl pkey"); |
| 505 | return false; |
| 506 | } |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 507 | } |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 508 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 509 | // Set up EC curves to auto (boost asio doesn't have a method for this) |
| 510 | // There is a pull request to add this. Once this is included in an asio |
| 511 | // drop, use the right way |
| 512 | // http://stackoverflow.com/questions/18929049/boost-asio-with-ecdsa-certificate-issue |
Ed Tanous | d3e9c3a | 2024-05-21 10:02:05 -0700 | [diff] [blame^] | 513 | if (SSL_CTX_set_ecdh_auto(mSslContext.native_handle(), 1) != 1) |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 514 | {} |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 515 | |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 516 | if (SSL_CTX_set_cipher_list(mSslContext.native_handle(), |
Ed Tanous | e96d7fb | 2023-06-14 14:53:29 -0700 | [diff] [blame] | 517 | mozillaIntermediate) != 1) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 518 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 519 | BMCWEB_LOG_ERROR("Error setting cipher list"); |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 520 | return false; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 521 | } |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 522 | return true; |
| 523 | } |
| 524 | |
| 525 | inline std::shared_ptr<boost::asio::ssl::context> getSslServerContext() |
| 526 | { |
| 527 | boost::asio::ssl::context sslCtx(boost::asio::ssl::context::tls_server); |
| 528 | |
| 529 | auto certFile = ensureCertificate(); |
| 530 | if (!getSslContext(sslCtx, certFile)) |
| 531 | { |
| 532 | BMCWEB_LOG_CRITICAL("Couldn't get server context"); |
| 533 | return nullptr; |
| 534 | } |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 535 | const persistent_data::AuthConfigMethods& c = |
| 536 | persistent_data::SessionStore::getInstance().getAuthMethodsConfig(); |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 537 | |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 538 | boost::asio::ssl::verify_mode mode = boost::asio::ssl::verify_peer; |
| 539 | if (c.tlsStrict) |
| 540 | { |
| 541 | BMCWEB_LOG_DEBUG("Setting verify peer"); |
| 542 | mode |= boost::asio::ssl::verify_fail_if_no_peer_cert; |
| 543 | } |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 544 | |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 545 | boost::system::error_code ec; |
| 546 | sslCtx.set_verify_mode(mode, ec); |
| 547 | if (ec) |
| 548 | { |
| 549 | BMCWEB_LOG_DEBUG("Failed to set verify mode {}", ec.message()); |
| 550 | return nullptr; |
| 551 | } |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 552 | SSL_CTX_set_options(sslCtx.native_handle(), SSL_OP_NO_RENEGOTIATION); |
| 553 | |
| 554 | if constexpr (BMCWEB_EXPERIMENTAL_HTTP2) |
| 555 | { |
| 556 | SSL_CTX_set_next_protos_advertised_cb(sslCtx.native_handle(), |
| 557 | nextProtoCallback, nullptr); |
| 558 | |
| 559 | SSL_CTX_set_alpn_select_cb(sslCtx.native_handle(), |
| 560 | alpnSelectProtoCallback, nullptr); |
| 561 | } |
| 562 | |
| 563 | return std::make_shared<boost::asio::ssl::context>(std::move(sslCtx)); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 564 | } |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 565 | |
Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 566 | enum class VerifyCertificate |
| 567 | { |
| 568 | Verify, |
| 569 | NoVerify |
| 570 | }; |
| 571 | |
| 572 | inline std::optional<boost::asio::ssl::context> |
| 573 | getSSLClientContext(VerifyCertificate verifyCertificate) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 574 | { |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 575 | namespace fs = std::filesystem; |
| 576 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 577 | boost::asio::ssl::context sslCtx(boost::asio::ssl::context::tls_client); |
| 578 | |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 579 | // NOTE, this path is temporary; In the future it will need to change to |
| 580 | // be set per subscription. Do not rely on this. |
| 581 | fs::path certPath = "/etc/ssl/certs/https/client.pem"; |
| 582 | std::string cert = verifyOpensslKeyCert(certPath); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 583 | |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 584 | if (!getSslContext(sslCtx, cert)) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 585 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 586 | return std::nullopt; |
| 587 | } |
| 588 | |
| 589 | // Add a directory containing certificate authority files to be used |
| 590 | // for performing verification. |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 591 | boost::system::error_code ec; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 592 | sslCtx.set_default_verify_paths(ec); |
| 593 | if (ec) |
| 594 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 595 | BMCWEB_LOG_ERROR("SSL context set_default_verify failed"); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 596 | return std::nullopt; |
| 597 | } |
| 598 | |
Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 599 | int mode = boost::asio::ssl::verify_peer; |
| 600 | if (verifyCertificate == VerifyCertificate::NoVerify) |
| 601 | { |
| 602 | mode = boost::asio::ssl::verify_none; |
| 603 | } |
| 604 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 605 | // Verify the remote server's certificate |
Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 606 | sslCtx.set_verify_mode(mode, ec); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 607 | if (ec) |
| 608 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 609 | BMCWEB_LOG_ERROR("SSL context set_verify_mode failed"); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 610 | return std::nullopt; |
| 611 | } |
| 612 | |
Ed Tanous | d3e9c3a | 2024-05-21 10:02:05 -0700 | [diff] [blame^] | 613 | if (SSL_CTX_set_cipher_list(sslCtx.native_handle(), mozillaIntermediate) != |
| 614 | 1) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 615 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 616 | BMCWEB_LOG_ERROR("SSL_CTX_set_cipher_list failed"); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 617 | return std::nullopt; |
| 618 | } |
| 619 | |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 620 | return {std::move(sslCtx)}; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 623 | } // namespace ensuressl |