blob: 5ce7cc7a41467b0826c870f4e69c6f72fafad80f [file] [log] [blame]
Ed Tanous0fdddb12017-02-28 11:06:34 -08001#pragma once
2
Ed Tanous62598e32023-07-17 17:06:25 -07003#include "bmcweb_config.h"
4
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "logging.hpp"
Ed Tanous2c6ffdb2023-06-28 11:28:38 -07006#include "ossl_random.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08007
Ed Tanousfca2cbe2021-01-28 14:49:59 -08008extern "C"
9{
10#include <nghttp2/nghttp2.h>
11}
Ed Tanous0fdddb12017-02-28 11:06:34 -080012#include <openssl/bio.h>
13#include <openssl/dh.h>
14#include <openssl/dsa.h>
Ed Tanous0fdddb12017-02-28 11:06:34 -080015#include <openssl/err.h>
16#include <openssl/evp.h>
17#include <openssl/pem.h>
18#include <openssl/rand.h>
19#include <openssl/rsa.h>
20#include <openssl/ssl.h>
Ed Tanous9b65f1f2017-03-07 15:17:13 -080021
Ed Tanous3112a142018-11-29 15:45:10 -080022#include <boost/asio/ssl/context.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050023
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include <optional>
Ed Tanous1abe55e2018-09-05 08:30:59 -070025#include <random>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include <string>
Ed Tanous1abe55e2018-09-05 08:30:59 -070027
28namespace ensuressl
29{
Patrick Williams89492a12023-05-10 07:51:34 -050030constexpr const char* trustStorePath = "/etc/ssl/certs/authority";
31constexpr const char* x509Comment = "Generated from OpenBMC service";
Ed Tanous55c7b7a2018-05-22 15:27:24 -070032static void initOpenssl();
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033static EVP_PKEY* createEcKey();
Ed Tanous0fdddb12017-02-28 11:06:34 -080034
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050035// Trust chain related errors.`
36inline bool isTrustChainError(int errnum)
37{
Ed Tanous55f79e62022-01-25 11:26:16 -080038 return (errnum == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) ||
39 (errnum == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) ||
40 (errnum == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) ||
41 (errnum == X509_V_ERR_CERT_UNTRUSTED) ||
42 (errnum == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE);
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050043}
44
Gunnar Mills1214b7e2020-06-04 10:11:30 -050045inline bool validateCertificate(X509* const cert)
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050046{
47 // Create an empty X509_STORE structure for certificate validation.
Gunnar Mills1214b7e2020-06-04 10:11:30 -050048 X509_STORE* x509Store = X509_STORE_new();
Ed Tanouse662eae2022-01-25 10:39:19 -080049 if (x509Store == nullptr)
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050050 {
Ed Tanous62598e32023-07-17 17:06:25 -070051 BMCWEB_LOG_ERROR("Error occurred during X509_STORE_new call");
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050052 return false;
53 }
54
55 // Load Certificate file into the X509 structure.
Gunnar Mills1214b7e2020-06-04 10:11:30 -050056 X509_STORE_CTX* storeCtx = X509_STORE_CTX_new();
Ed Tanouse662eae2022-01-25 10:39:19 -080057 if (storeCtx == nullptr)
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050058 {
Ed Tanous62598e32023-07-17 17:06:25 -070059 BMCWEB_LOG_ERROR("Error occurred during X509_STORE_CTX_new call");
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050060 X509_STORE_free(x509Store);
61 return false;
62 }
63
Ed Tanous99131cd2019-10-24 11:12:47 -070064 int errCode = X509_STORE_CTX_init(storeCtx, x509Store, cert, nullptr);
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050065 if (errCode != 1)
66 {
Ed Tanous62598e32023-07-17 17:06:25 -070067 BMCWEB_LOG_ERROR("Error occurred during X509_STORE_CTX_init call");
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050068 X509_STORE_CTX_free(storeCtx);
69 X509_STORE_free(x509Store);
70 return false;
71 }
72
73 errCode = X509_verify_cert(storeCtx);
74 if (errCode == 1)
75 {
Ed Tanous62598e32023-07-17 17:06:25 -070076 BMCWEB_LOG_INFO("Certificate verification is success");
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050077 X509_STORE_CTX_free(storeCtx);
78 X509_STORE_free(x509Store);
79 return true;
80 }
81 if (errCode == 0)
82 {
83 errCode = X509_STORE_CTX_get_error(storeCtx);
84 X509_STORE_CTX_free(storeCtx);
85 X509_STORE_free(x509Store);
86 if (isTrustChainError(errCode))
87 {
Ed Tanous62598e32023-07-17 17:06:25 -070088 BMCWEB_LOG_DEBUG("Ignoring Trust Chain error. Reason: {}",
89 X509_verify_cert_error_string(errCode));
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050090 return true;
91 }
Ed Tanous62598e32023-07-17 17:06:25 -070092 BMCWEB_LOG_ERROR("Certificate verification failed. Reason: {}",
93 X509_verify_cert_error_string(errCode));
Ed Tanous3174e4d2020-10-07 11:41:22 -070094 return false;
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050095 }
96
Ed Tanous62598e32023-07-17 17:06:25 -070097 BMCWEB_LOG_ERROR(
98 "Error occurred during X509_verify_cert call. ErrorCode: {}", errCode);
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050099 X509_STORE_CTX_free(storeCtx);
100 X509_STORE_free(x509Store);
101 return false;
102}
103
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500104inline bool verifyOpensslKeyCert(const std::string& filepath)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105{
106 bool privateKeyValid = false;
107 bool certValid = false;
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800108
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 std::cout << "Checking certs in file " << filepath << "\n";
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800110
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500111 FILE* file = fopen(filepath.c_str(), "r");
Ed Tanous99131cd2019-10-24 11:12:47 -0700112 if (file != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500114 EVP_PKEY* pkey = PEM_read_PrivateKey(file, nullptr, nullptr, nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 if (pkey != nullptr)
116 {
Patrick Williams145bb762021-12-07 21:05:04 -0600117#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500118 RSA* rsa = EVP_PKEY_get1_RSA(pkey);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 if (rsa != nullptr)
120 {
121 std::cout << "Found an RSA key\n";
122 if (RSA_check_key(rsa) == 1)
123 {
Jayanth Othayoth4d2849a2019-05-13 01:46:34 -0500124 privateKeyValid = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700125 }
126 else
127 {
128 std::cerr << "Key not valid error number "
129 << ERR_get_error() << "\n";
130 }
131 RSA_free(rsa);
132 }
133 else
134 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500135 EC_KEY* ec = EVP_PKEY_get1_EC_KEY(pkey);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 if (ec != nullptr)
137 {
138 std::cout << "Found an EC key\n";
139 if (EC_KEY_check_key(ec) == 1)
140 {
141 privateKeyValid = true;
142 }
143 else
144 {
145 std::cerr << "Key not valid error number "
146 << ERR_get_error() << "\n";
147 }
148 EC_KEY_free(ec);
149 }
150 }
Patrick Williams145bb762021-12-07 21:05:04 -0600151#else
Patrick Williams89492a12023-05-10 07:51:34 -0500152 EVP_PKEY_CTX* pkeyCtx = EVP_PKEY_CTX_new_from_pkey(nullptr, pkey,
153 nullptr);
Patrick Williams145bb762021-12-07 21:05:04 -0600154
Ed Tanouse662eae2022-01-25 10:39:19 -0800155 if (pkeyCtx == nullptr)
Patrick Williams145bb762021-12-07 21:05:04 -0600156 {
Gunnar Mills91359f92022-01-04 14:50:18 -0600157 std::cerr << "Unable to allocate pkeyCtx " << ERR_get_error()
Patrick Williams145bb762021-12-07 21:05:04 -0600158 << "\n";
159 }
Gunnar Mills91359f92022-01-04 14:50:18 -0600160 else if (EVP_PKEY_check(pkeyCtx) == 1)
Patrick Williams145bb762021-12-07 21:05:04 -0600161 {
162 privateKeyValid = true;
163 }
164 else
165 {
Patrick Williams145bb762021-12-07 21:05:04 -0600166 std::cerr << "Key not valid error number " << ERR_get_error()
167 << "\n";
168 }
169#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170
171 if (privateKeyValid)
172 {
Ramesh Iyyarc0bf8932019-06-22 00:23:29 -0500173 // If the order is certificate followed by key in input file
174 // then, certificate read will fail. So, setting the file
175 // pointer to point beginning of file to avoid certificate and
176 // key order issue.
177 fseek(file, 0, SEEK_SET);
178
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500179 X509* x509 = PEM_read_X509(file, nullptr, nullptr, nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180 if (x509 == nullptr)
181 {
182 std::cout << "error getting x509 cert " << ERR_get_error()
183 << "\n";
184 }
185 else
186 {
Ramesh Iyyar082f28f2019-06-22 03:31:55 -0500187 certValid = validateCertificate(x509);
188 X509_free(x509);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700189 }
190 }
191
Patrick Williams145bb762021-12-07 21:05:04 -0600192#if (OPENSSL_VERSION_NUMBER > 0x30000000L)
Gunnar Mills91359f92022-01-04 14:50:18 -0600193 EVP_PKEY_CTX_free(pkeyCtx);
Patrick Williams145bb762021-12-07 21:05:04 -0600194#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 EVP_PKEY_free(pkey);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800196 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700197 fclose(file);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800198 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700199 return certValid;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800200}
201
Alan Kuoa8220702020-11-26 11:15:29 +0800202inline X509* loadCert(const std::string& filePath)
203{
204 BIO* certFileBio = BIO_new_file(filePath.c_str(), "rb");
Ed Tanouse662eae2022-01-25 10:39:19 -0800205 if (certFileBio == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800206 {
Ed Tanous8ece0e42024-01-02 13:16:50 -0800207 BMCWEB_LOG_ERROR("Error occurred during BIO_new_file call, FILE= {}",
Ed Tanous62598e32023-07-17 17:06:25 -0700208 filePath);
Alan Kuoa8220702020-11-26 11:15:29 +0800209 return nullptr;
210 }
211
212 X509* cert = X509_new();
Ed Tanouse662eae2022-01-25 10:39:19 -0800213 if (cert == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800214 {
Ed Tanous8ece0e42024-01-02 13:16:50 -0800215 BMCWEB_LOG_ERROR("Error occurred during X509_new call, {}",
Ed Tanous62598e32023-07-17 17:06:25 -0700216 ERR_get_error());
Alan Kuoa8220702020-11-26 11:15:29 +0800217 BIO_free(certFileBio);
218 return nullptr;
219 }
220
Ed Tanouse662eae2022-01-25 10:39:19 -0800221 if (PEM_read_bio_X509(certFileBio, &cert, nullptr, nullptr) == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800222 {
Ed Tanous62598e32023-07-17 17:06:25 -0700223 BMCWEB_LOG_ERROR(
Ed Tanous8ece0e42024-01-02 13:16:50 -0800224 "Error occurred during PEM_read_bio_X509 call, FILE= {}", filePath);
Alan Kuoa8220702020-11-26 11:15:29 +0800225
226 BIO_free(certFileBio);
227 X509_free(cert);
228 return nullptr;
229 }
Alan Kuob295bf92021-04-16 14:16:47 +0800230 BIO_free(certFileBio);
Alan Kuoa8220702020-11-26 11:15:29 +0800231 return cert;
232}
233
234inline int addExt(X509* cert, int nid, const char* value)
235{
236 X509_EXTENSION* ex = nullptr;
Ed Tanousb00dcc22021-02-23 12:52:50 -0800237 X509V3_CTX ctx{};
Alan Kuoa8220702020-11-26 11:15:29 +0800238 X509V3_set_ctx(&ctx, cert, cert, nullptr, nullptr, 0);
Ed Tanous4ecc6182022-01-07 09:36:26 -0800239
240 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
Alan Kuoa8220702020-11-26 11:15:29 +0800241 ex = X509V3_EXT_conf_nid(nullptr, &ctx, nid, const_cast<char*>(value));
Ed Tanouse662eae2022-01-25 10:39:19 -0800242 if (ex == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800243 {
Ed Tanous62598e32023-07-17 17:06:25 -0700244 BMCWEB_LOG_ERROR("Error: In X509V3_EXT_conf_nidn: {}", value);
Alan Kuoa8220702020-11-26 11:15:29 +0800245 return -1;
246 }
247 X509_add_ext(cert, ex, -1);
248 X509_EXTENSION_free(ex);
249 return 0;
250}
251
252inline void generateSslCertificate(const std::string& filepath,
253 const std::string& cn)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700254{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500255 FILE* pFile = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700256 std::cout << "Generating new keys\n";
257 initOpenssl();
Ed Tanous0fdddb12017-02-28 11:06:34 -0800258
Ed Tanous1abe55e2018-09-05 08:30:59 -0700259 std::cerr << "Generating EC key\n";
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500260 EVP_PKEY* pPrivKey = createEcKey();
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700261 if (pPrivKey != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700262 {
263 std::cerr << "Generating x509 Certificate\n";
264 // Use this code to directly generate a certificate
Ed Tanous543f4402022-01-06 13:12:53 -0800265 X509* x509 = X509_new();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700266 if (x509 != nullptr)
267 {
268 // get a random number from the RNG for the certificate serial
Ed Tanous8ece0e42024-01-02 13:16:50 -0800269 // number If this is not random, regenerating certs throws browser
Ed Tanous1abe55e2018-09-05 08:30:59 -0700270 // errors
Alan Kuoa8220702020-11-26 11:15:29 +0800271 bmcweb::OpenSSLGenerator gen;
272 std::uniform_int_distribution<int> dis(
273 1, std::numeric_limits<int>::max());
274 int serial = dis(gen);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800275
Ed Tanous1abe55e2018-09-05 08:30:59 -0700276 ASN1_INTEGER_set(X509_get_serialNumber(x509), serial);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800277
Ed Tanous1abe55e2018-09-05 08:30:59 -0700278 // not before this moment
279 X509_gmtime_adj(X509_get_notBefore(x509), 0);
280 // Cert is valid for 10 years
281 X509_gmtime_adj(X509_get_notAfter(x509),
282 60L * 60L * 24L * 365L * 10L);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800283
Ed Tanous1abe55e2018-09-05 08:30:59 -0700284 // set the public key to the key we just generated
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700285 X509_set_pubkey(x509, pPrivKey);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800286
Ed Tanous1abe55e2018-09-05 08:30:59 -0700287 // get the subject name
Ed Tanous543f4402022-01-06 13:12:53 -0800288 X509_NAME* name = X509_get_subject_name(x509);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800289
Ed Tanous46ff87b2022-01-07 09:25:51 -0800290 using x509String = const unsigned char;
291 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
292 x509String* country = reinterpret_cast<x509String*>("US");
293 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
294 x509String* company = reinterpret_cast<x509String*>("OpenBMC");
295 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
296 x509String* cnStr = reinterpret_cast<x509String*>(cn.c_str());
297
298 X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, country, -1, -1,
299 0);
300 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, company, -1, -1,
301 0);
302 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, cnStr, -1, -1,
303 0);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700304 // set the CSR options
305 X509_set_issuer_name(x509, name);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800306
Alan Kuoa8220702020-11-26 11:15:29 +0800307 X509_set_version(x509, 2);
308 addExt(x509, NID_basic_constraints, ("critical,CA:TRUE"));
309 addExt(x509, NID_subject_alt_name, ("DNS:" + cn).c_str());
310 addExt(x509, NID_subject_key_identifier, ("hash"));
311 addExt(x509, NID_authority_key_identifier, ("keyid"));
312 addExt(x509, NID_key_usage, ("digitalSignature, keyEncipherment"));
313 addExt(x509, NID_ext_key_usage, ("serverAuth"));
314 addExt(x509, NID_netscape_comment, (x509Comment));
315
Ed Tanous1abe55e2018-09-05 08:30:59 -0700316 // Sign the certificate with our private key
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700317 X509_sign(x509, pPrivKey, EVP_sha256());
Ed Tanous0fdddb12017-02-28 11:06:34 -0800318
Ed Tanous1abe55e2018-09-05 08:30:59 -0700319 pFile = fopen(filepath.c_str(), "wt");
Ed Tanous0fdddb12017-02-28 11:06:34 -0800320
Ed Tanous1abe55e2018-09-05 08:30:59 -0700321 if (pFile != nullptr)
322 {
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700323 PEM_write_PrivateKey(pFile, pPrivKey, nullptr, nullptr, 0,
Ed Tanous99131cd2019-10-24 11:12:47 -0700324 nullptr, nullptr);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800325
Ed Tanous1abe55e2018-09-05 08:30:59 -0700326 PEM_write_X509(pFile, x509);
327 fclose(pFile);
Ed Tanous99131cd2019-10-24 11:12:47 -0700328 pFile = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700329 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800330
Ed Tanous1abe55e2018-09-05 08:30:59 -0700331 X509_free(x509);
332 }
333
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700334 EVP_PKEY_free(pPrivKey);
335 pPrivKey = nullptr;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800336 }
337
Ed Tanous1abe55e2018-09-05 08:30:59 -0700338 // cleanup_openssl();
Ed Tanous99923322017-03-03 14:21:24 -0800339}
Ed Tanousb01bf292019-03-25 19:25:26 +0000340
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500341EVP_PKEY* createEcKey()
Ed Tanousb01bf292019-03-25 19:25:26 +0000342{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500343 EVP_PKEY* pKey = nullptr;
Patrick Williamsaec70662021-12-13 16:55:46 -0600344
345#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
Ed Tanousb01bf292019-03-25 19:25:26 +0000346 int eccgrp = 0;
Vernon Maueryaaf32062020-03-09 10:41:31 -0700347 eccgrp = OBJ_txt2nid("secp384r1");
Ed Tanous6ea007a2019-02-13 22:48:25 -0800348
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500349 EC_KEY* myecc = EC_KEY_new_by_curve_name(eccgrp);
Ed Tanousb01bf292019-03-25 19:25:26 +0000350 if (myecc != nullptr)
Ed Tanous6ea007a2019-02-13 22:48:25 -0800351 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000352 EC_KEY_set_asn1_flag(myecc, OPENSSL_EC_NAMED_CURVE);
353 EC_KEY_generate_key(myecc);
354 pKey = EVP_PKEY_new();
355 if (pKey != nullptr)
356 {
Szymon Dompkea327dc52022-03-01 14:04:14 +0100357 if (EVP_PKEY_assign(pKey, EVP_PKEY_EC, myecc) != 0)
Ed Tanousb01bf292019-03-25 19:25:26 +0000358 {
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700359 /* pKey owns myecc from now */
Ed Tanousb01bf292019-03-25 19:25:26 +0000360 if (EC_KEY_check_key(myecc) <= 0)
361 {
Szymon Dompkea327dc52022-03-01 14:04:14 +0100362 std::cerr << "EC_check_key failed.\n";
Ed Tanousb01bf292019-03-25 19:25:26 +0000363 }
364 }
365 }
Ed Tanous6ea007a2019-02-13 22:48:25 -0800366 }
Patrick Williamsaec70662021-12-13 16:55:46 -0600367#else
368 // Create context for curve parameter generation.
369 std::unique_ptr<EVP_PKEY_CTX, decltype(&::EVP_PKEY_CTX_free)> ctx{
370 EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr), &::EVP_PKEY_CTX_free};
371 if (!ctx)
372 {
373 return nullptr;
374 }
375
376 // Set up curve parameters.
377 EVP_PKEY* params = nullptr;
378 if ((EVP_PKEY_paramgen_init(ctx.get()) <= 0) ||
379 (EVP_PKEY_CTX_set_ec_param_enc(ctx.get(), OPENSSL_EC_NAMED_CURVE) <=
380 0) ||
381 (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(), NID_secp384r1) <=
382 0) ||
383 (EVP_PKEY_paramgen(ctx.get(), &params) <= 0))
384 {
385 return nullptr;
386 }
387
388 // Set up RAII holder for params.
389 std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)> pparams{
390 params, &::EVP_PKEY_free};
391
392 // Set new context for key generation, using curve parameters.
393 ctx.reset(EVP_PKEY_CTX_new_from_pkey(nullptr, params, nullptr));
394 if (!ctx || (EVP_PKEY_keygen_init(ctx.get()) <= 0))
395 {
396 return nullptr;
397 }
398
399 // Generate key.
400 if (EVP_PKEY_keygen(ctx.get(), &pKey) <= 0)
401 {
402 return nullptr;
403 }
404#endif
405
Ed Tanous1abe55e2018-09-05 08:30:59 -0700406 return pKey;
407}
408
409void initOpenssl()
410{
Ed Tanous911ac312017-08-15 09:37:42 -0700411#if OPENSSL_VERSION_NUMBER < 0x10100000L
Ed Tanous1abe55e2018-09-05 08:30:59 -0700412 SSL_load_error_strings();
413 OpenSSL_add_all_algorithms();
414 RAND_load_file("/dev/urandom", 1024);
Ed Tanousa1e077c2017-04-25 12:42:19 -0700415#endif
Ed Tanous0fdddb12017-02-28 11:06:34 -0800416}
417
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500418inline void ensureOpensslKeyPresentAndValid(const std::string& filepath)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700419{
420 bool pemFileValid = false;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800421
Ed Tanous1abe55e2018-09-05 08:30:59 -0700422 pemFileValid = verifyOpensslKeyCert(filepath);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700423
Ed Tanous1abe55e2018-09-05 08:30:59 -0700424 if (!pemFileValid)
425 {
426 std::cerr << "Error in verifying signature, regenerating\n";
Alan Kuoa8220702020-11-26 11:15:29 +0800427 generateSslCertificate(filepath, "testhost");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700428 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800429}
Ed Tanousc4771fb2017-03-13 13:39:49 -0700430
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800431inline int nextProtoCallback(SSL* /*unused*/, const unsigned char** data,
432 unsigned int* len, void* /*unused*/)
433{
434 // First byte is the length.
435 constexpr std::string_view h2 = "\x02h2";
436 *data = std::bit_cast<const unsigned char*>(h2.data());
437 *len = static_cast<unsigned int>(h2.size());
438 return SSL_TLSEXT_ERR_OK;
439}
440
441inline int alpnSelectProtoCallback(SSL* /*unused*/, const unsigned char** out,
442 unsigned char* outlen,
443 const unsigned char* in, unsigned int inlen,
444 void* /*unused*/)
445{
446 // There's a mismatch in constness for nghttp2_select_next_protocol. The
447 // examples in nghttp2 don't show this problem. Unclear what the right fix
448 // is here.
449
450 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
451 unsigned char** outNew = const_cast<unsigned char**>(out);
452 int rv = nghttp2_select_next_protocol(outNew, outlen, in, inlen);
453 if (rv != 1)
454 {
455 return SSL_TLSEXT_ERR_NOACK;
456 }
457
458 return SSL_TLSEXT_ERR_OK;
459}
460
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600461inline std::shared_ptr<boost::asio::ssl::context>
Ed Tanous81ce6092020-12-17 16:54:55 +0000462 getSslContext(const std::string& sslPemFile)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700463{
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600464 std::shared_ptr<boost::asio::ssl::context> mSslContext =
465 std::make_shared<boost::asio::ssl::context>(
466 boost::asio::ssl::context::tls_server);
467 mSslContext->set_options(boost::asio::ssl::context::default_workarounds |
468 boost::asio::ssl::context::no_sslv2 |
469 boost::asio::ssl::context::no_sslv3 |
470 boost::asio::ssl::context::single_dh_use |
471 boost::asio::ssl::context::no_tlsv1 |
472 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700473
James Feist91243c32019-11-12 14:55:40 -0800474 // BIG WARNING: This needs to stay disabled, as there will always be
475 // unauthenticated endpoints
476 // mSslContext->set_verify_mode(boost::asio::ssl::verify_peer);
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200477
James Feist6a3e1822019-11-06 13:46:35 -0800478 SSL_CTX_set_options(mSslContext->native_handle(), SSL_OP_NO_RENEGOTIATION);
479
Ed Tanous62598e32023-07-17 17:06:25 -0700480 BMCWEB_LOG_DEBUG("Using default TrustStore location: {}", trustStorePath);
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200481 mSslContext->add_verify_path(trustStorePath);
482
Ed Tanous81ce6092020-12-17 16:54:55 +0000483 mSslContext->use_certificate_file(sslPemFile,
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600484 boost::asio::ssl::context::pem);
Ed Tanous81ce6092020-12-17 16:54:55 +0000485 mSslContext->use_private_key_file(sslPemFile,
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600486 boost::asio::ssl::context::pem);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700487
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800488 if constexpr (bmcwebEnableHTTP2)
489 {
490 SSL_CTX_set_next_protos_advertised_cb(mSslContext->native_handle(),
491 nextProtoCallback, nullptr);
492
493 SSL_CTX_set_alpn_select_cb(mSslContext->native_handle(),
494 alpnSelectProtoCallback, nullptr);
495 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700496 // Set up EC curves to auto (boost asio doesn't have a method for this)
497 // There is a pull request to add this. Once this is included in an asio
498 // drop, use the right way
499 // http://stackoverflow.com/questions/18929049/boost-asio-with-ecdsa-certificate-issue
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600500 if (SSL_CTX_set_ecdh_auto(mSslContext->native_handle(), 1) != 1)
Ed Tanous62598e32023-07-17 17:06:25 -0700501 {}
Ed Tanousc4771fb2017-03-13 13:39:49 -0700502
Ed Tanouse96d7fb2023-06-14 14:53:29 -0700503 // Mozilla intermediate cipher suites v5.7
504 // Sourced from: https://ssl-config.mozilla.org/guidelines/5.7.json
505 const char* mozillaIntermediate = "ECDHE-ECDSA-AES128-GCM-SHA256:"
506 "ECDHE-RSA-AES128-GCM-SHA256:"
507 "ECDHE-ECDSA-AES256-GCM-SHA384:"
508 "ECDHE-RSA-AES256-GCM-SHA384:"
509 "ECDHE-ECDSA-CHACHA20-POLY1305:"
510 "ECDHE-RSA-CHACHA20-POLY1305:"
511 "DHE-RSA-AES128-GCM-SHA256:"
512 "DHE-RSA-AES256-GCM-SHA384:"
513 "DHE-RSA-CHACHA20-POLY1305";
Ed Tanousc4771fb2017-03-13 13:39:49 -0700514
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600515 if (SSL_CTX_set_cipher_list(mSslContext->native_handle(),
Ed Tanouse96d7fb2023-06-14 14:53:29 -0700516 mozillaIntermediate) != 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700517 {
Ed Tanous62598e32023-07-17 17:06:25 -0700518 BMCWEB_LOG_ERROR("Error setting cipher list");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700519 }
520 return mSslContext;
Ed Tanousc4771fb2017-03-13 13:39:49 -0700521}
AppaRao Pulie38778a2022-06-27 23:09:03 +0000522
523inline std::optional<boost::asio::ssl::context> getSSLClientContext()
524{
525 boost::asio::ssl::context sslCtx(boost::asio::ssl::context::tls_client);
526
527 boost::system::error_code ec;
528
529 // Support only TLS v1.2 & v1.3
530 sslCtx.set_options(boost::asio::ssl::context::default_workarounds |
531 boost::asio::ssl::context::no_sslv2 |
532 boost::asio::ssl::context::no_sslv3 |
533 boost::asio::ssl::context::single_dh_use |
534 boost::asio::ssl::context::no_tlsv1 |
535 boost::asio::ssl::context::no_tlsv1_1,
536 ec);
537 if (ec)
538 {
Ed Tanous62598e32023-07-17 17:06:25 -0700539 BMCWEB_LOG_ERROR("SSL context set_options failed");
AppaRao Pulie38778a2022-06-27 23:09:03 +0000540 return std::nullopt;
541 }
542
543 // Add a directory containing certificate authority files to be used
544 // for performing verification.
545 sslCtx.set_default_verify_paths(ec);
546 if (ec)
547 {
Ed Tanous62598e32023-07-17 17:06:25 -0700548 BMCWEB_LOG_ERROR("SSL context set_default_verify failed");
AppaRao Pulie38778a2022-06-27 23:09:03 +0000549 return std::nullopt;
550 }
551
552 // Verify the remote server's certificate
553 sslCtx.set_verify_mode(boost::asio::ssl::verify_peer, ec);
554 if (ec)
555 {
Ed Tanous62598e32023-07-17 17:06:25 -0700556 BMCWEB_LOG_ERROR("SSL context set_verify_mode failed");
AppaRao Pulie38778a2022-06-27 23:09:03 +0000557 return std::nullopt;
558 }
559
560 // All cipher suites are set as per OWASP datasheet.
561 // https://cheatsheetseries.owasp.org/cheatsheets/TLS_Cipher_String_Cheat_Sheet.html
562 constexpr const char* sslCiphers = "ECDHE-ECDSA-AES128-GCM-SHA256:"
563 "ECDHE-RSA-AES128-GCM-SHA256:"
564 "ECDHE-ECDSA-AES256-GCM-SHA384:"
565 "ECDHE-RSA-AES256-GCM-SHA384:"
566 "ECDHE-ECDSA-CHACHA20-POLY1305:"
567 "ECDHE-RSA-CHACHA20-POLY1305:"
568 "DHE-RSA-AES128-GCM-SHA256:"
569 "DHE-RSA-AES256-GCM-SHA384"
570 "TLS_AES_128_GCM_SHA256:"
571 "TLS_AES_256_GCM_SHA384:"
572 "TLS_CHACHA20_POLY1305_SHA256";
573
574 if (SSL_CTX_set_cipher_list(sslCtx.native_handle(), sslCiphers) != 1)
575 {
Ed Tanous62598e32023-07-17 17:06:25 -0700576 BMCWEB_LOG_ERROR("SSL_CTX_set_cipher_list failed");
AppaRao Pulie38778a2022-06-27 23:09:03 +0000577 return std::nullopt;
578 }
579
580 return sslCtx;
581}
582
Ed Tanous1abe55e2018-09-05 08:30:59 -0700583} // namespace ensuressl