blob: 0794fdcfac3d263078e99033b186e91c3d5990fd [file] [log] [blame]
Ed Tanous0fdddb12017-02-28 11:06:34 -08001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "logging.hpp"
4#include "random.hpp"
5
Ed Tanousfca2cbe2021-01-28 14:49:59 -08006extern "C"
7{
8#include <nghttp2/nghttp2.h>
9}
Ed Tanous0fdddb12017-02-28 11:06:34 -080010#include <openssl/bio.h>
11#include <openssl/dh.h>
12#include <openssl/dsa.h>
Ed Tanous0fdddb12017-02-28 11:06:34 -080013#include <openssl/err.h>
14#include <openssl/evp.h>
15#include <openssl/pem.h>
16#include <openssl/rand.h>
17#include <openssl/rsa.h>
18#include <openssl/ssl.h>
Ed Tanous9b65f1f2017-03-07 15:17:13 -080019
Ed Tanous3112a142018-11-29 15:45:10 -080020#include <boost/asio/ssl/context.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050021
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080022#include <optional>
Ed Tanous1abe55e2018-09-05 08:30:59 -070023#include <random>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include <string>
Ed Tanous1abe55e2018-09-05 08:30:59 -070025
26namespace ensuressl
27{
Patrick Williams89492a12023-05-10 07:51:34 -050028constexpr const char* trustStorePath = "/etc/ssl/certs/authority";
29constexpr const char* x509Comment = "Generated from OpenBMC service";
Ed Tanous55c7b7a2018-05-22 15:27:24 -070030static void initOpenssl();
Gunnar Mills1214b7e2020-06-04 10:11:30 -050031static EVP_PKEY* createEcKey();
Ed Tanous0fdddb12017-02-28 11:06:34 -080032
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050033// Trust chain related errors.`
34inline bool isTrustChainError(int errnum)
35{
Ed Tanous55f79e62022-01-25 11:26:16 -080036 return (errnum == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) ||
37 (errnum == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) ||
38 (errnum == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) ||
39 (errnum == X509_V_ERR_CERT_UNTRUSTED) ||
40 (errnum == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE);
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050041}
42
Gunnar Mills1214b7e2020-06-04 10:11:30 -050043inline bool validateCertificate(X509* const cert)
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050044{
45 // Create an empty X509_STORE structure for certificate validation.
Gunnar Mills1214b7e2020-06-04 10:11:30 -050046 X509_STORE* x509Store = X509_STORE_new();
Ed Tanouse662eae2022-01-25 10:39:19 -080047 if (x509Store == nullptr)
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050048 {
Gunnar Millsc61704a2020-07-08 13:47:06 -050049 BMCWEB_LOG_ERROR << "Error occurred during X509_STORE_new call";
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050050 return false;
51 }
52
53 // Load Certificate file into the X509 structure.
Gunnar Mills1214b7e2020-06-04 10:11:30 -050054 X509_STORE_CTX* storeCtx = X509_STORE_CTX_new();
Ed Tanouse662eae2022-01-25 10:39:19 -080055 if (storeCtx == nullptr)
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050056 {
Gunnar Millsc61704a2020-07-08 13:47:06 -050057 BMCWEB_LOG_ERROR << "Error occurred during X509_STORE_CTX_new call";
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050058 X509_STORE_free(x509Store);
59 return false;
60 }
61
Ed Tanous99131cd2019-10-24 11:12:47 -070062 int errCode = X509_STORE_CTX_init(storeCtx, x509Store, cert, nullptr);
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050063 if (errCode != 1)
64 {
Gunnar Millsc61704a2020-07-08 13:47:06 -050065 BMCWEB_LOG_ERROR << "Error occurred during X509_STORE_CTX_init call";
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050066 X509_STORE_CTX_free(storeCtx);
67 X509_STORE_free(x509Store);
68 return false;
69 }
70
71 errCode = X509_verify_cert(storeCtx);
72 if (errCode == 1)
73 {
74 BMCWEB_LOG_INFO << "Certificate verification is success";
75 X509_STORE_CTX_free(storeCtx);
76 X509_STORE_free(x509Store);
77 return true;
78 }
79 if (errCode == 0)
80 {
81 errCode = X509_STORE_CTX_get_error(storeCtx);
82 X509_STORE_CTX_free(storeCtx);
83 X509_STORE_free(x509Store);
84 if (isTrustChainError(errCode))
85 {
86 BMCWEB_LOG_DEBUG << "Ignoring Trust Chain error. Reason: "
87 << X509_verify_cert_error_string(errCode);
88 return true;
89 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070090 BMCWEB_LOG_ERROR << "Certificate verification failed. Reason: "
91 << X509_verify_cert_error_string(errCode);
92 return false;
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050093 }
94
95 BMCWEB_LOG_ERROR
Gunnar Millsc61704a2020-07-08 13:47:06 -050096 << "Error occurred during X509_verify_cert call. ErrorCode: "
97 << errCode;
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050098 X509_STORE_CTX_free(storeCtx);
99 X509_STORE_free(x509Store);
100 return false;
101}
102
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500103inline bool verifyOpensslKeyCert(const std::string& filepath)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104{
105 bool privateKeyValid = false;
106 bool certValid = false;
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800107
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 std::cout << "Checking certs in file " << filepath << "\n";
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800109
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500110 FILE* file = fopen(filepath.c_str(), "r");
Ed Tanous99131cd2019-10-24 11:12:47 -0700111 if (file != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500113 EVP_PKEY* pkey = PEM_read_PrivateKey(file, nullptr, nullptr, nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 if (pkey != nullptr)
115 {
Patrick Williams145bb762021-12-07 21:05:04 -0600116#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500117 RSA* rsa = EVP_PKEY_get1_RSA(pkey);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 if (rsa != nullptr)
119 {
120 std::cout << "Found an RSA key\n";
121 if (RSA_check_key(rsa) == 1)
122 {
Jayanth Othayoth4d2849a2019-05-13 01:46:34 -0500123 privateKeyValid = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700124 }
125 else
126 {
127 std::cerr << "Key not valid error number "
128 << ERR_get_error() << "\n";
129 }
130 RSA_free(rsa);
131 }
132 else
133 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500134 EC_KEY* ec = EVP_PKEY_get1_EC_KEY(pkey);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700135 if (ec != nullptr)
136 {
137 std::cout << "Found an EC key\n";
138 if (EC_KEY_check_key(ec) == 1)
139 {
140 privateKeyValid = true;
141 }
142 else
143 {
144 std::cerr << "Key not valid error number "
145 << ERR_get_error() << "\n";
146 }
147 EC_KEY_free(ec);
148 }
149 }
Patrick Williams145bb762021-12-07 21:05:04 -0600150#else
Patrick Williams89492a12023-05-10 07:51:34 -0500151 EVP_PKEY_CTX* pkeyCtx = EVP_PKEY_CTX_new_from_pkey(nullptr, pkey,
152 nullptr);
Patrick Williams145bb762021-12-07 21:05:04 -0600153
Ed Tanouse662eae2022-01-25 10:39:19 -0800154 if (pkeyCtx == nullptr)
Patrick Williams145bb762021-12-07 21:05:04 -0600155 {
Gunnar Mills91359f92022-01-04 14:50:18 -0600156 std::cerr << "Unable to allocate pkeyCtx " << ERR_get_error()
Patrick Williams145bb762021-12-07 21:05:04 -0600157 << "\n";
158 }
Gunnar Mills91359f92022-01-04 14:50:18 -0600159 else if (EVP_PKEY_check(pkeyCtx) == 1)
Patrick Williams145bb762021-12-07 21:05:04 -0600160 {
161 privateKeyValid = true;
162 }
163 else
164 {
Patrick Williams145bb762021-12-07 21:05:04 -0600165 std::cerr << "Key not valid error number " << ERR_get_error()
166 << "\n";
167 }
168#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700169
170 if (privateKeyValid)
171 {
Ramesh Iyyarc0bf8932019-06-22 00:23:29 -0500172 // If the order is certificate followed by key in input file
173 // then, certificate read will fail. So, setting the file
174 // pointer to point beginning of file to avoid certificate and
175 // key order issue.
176 fseek(file, 0, SEEK_SET);
177
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500178 X509* x509 = PEM_read_X509(file, nullptr, nullptr, nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700179 if (x509 == nullptr)
180 {
181 std::cout << "error getting x509 cert " << ERR_get_error()
182 << "\n";
183 }
184 else
185 {
Ramesh Iyyar082f28f2019-06-22 03:31:55 -0500186 certValid = validateCertificate(x509);
187 X509_free(x509);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700188 }
189 }
190
Patrick Williams145bb762021-12-07 21:05:04 -0600191#if (OPENSSL_VERSION_NUMBER > 0x30000000L)
Gunnar Mills91359f92022-01-04 14:50:18 -0600192 EVP_PKEY_CTX_free(pkeyCtx);
Patrick Williams145bb762021-12-07 21:05:04 -0600193#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700194 EVP_PKEY_free(pkey);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800195 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700196 fclose(file);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800197 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700198 return certValid;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800199}
200
Alan Kuoa8220702020-11-26 11:15:29 +0800201inline X509* loadCert(const std::string& filePath)
202{
203 BIO* certFileBio = BIO_new_file(filePath.c_str(), "rb");
Ed Tanouse662eae2022-01-25 10:39:19 -0800204 if (certFileBio == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800205 {
206 BMCWEB_LOG_ERROR << "Error occured during BIO_new_file call, "
207 << "FILE= " << filePath;
208 return nullptr;
209 }
210
211 X509* cert = X509_new();
Ed Tanouse662eae2022-01-25 10:39:19 -0800212 if (cert == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800213 {
214 BMCWEB_LOG_ERROR << "Error occured during X509_new call, "
215 << ERR_get_error();
216 BIO_free(certFileBio);
217 return nullptr;
218 }
219
Ed Tanouse662eae2022-01-25 10:39:19 -0800220 if (PEM_read_bio_X509(certFileBio, &cert, nullptr, nullptr) == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800221 {
222 BMCWEB_LOG_ERROR << "Error occured during PEM_read_bio_X509 call, "
223 << "FILE= " << filePath;
224
225 BIO_free(certFileBio);
226 X509_free(cert);
227 return nullptr;
228 }
Alan Kuob295bf92021-04-16 14:16:47 +0800229 BIO_free(certFileBio);
Alan Kuoa8220702020-11-26 11:15:29 +0800230 return cert;
231}
232
233inline int addExt(X509* cert, int nid, const char* value)
234{
235 X509_EXTENSION* ex = nullptr;
Ed Tanousb00dcc22021-02-23 12:52:50 -0800236 X509V3_CTX ctx{};
Alan Kuoa8220702020-11-26 11:15:29 +0800237 X509V3_set_ctx(&ctx, cert, cert, nullptr, nullptr, 0);
Ed Tanous4ecc6182022-01-07 09:36:26 -0800238
239 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
Alan Kuoa8220702020-11-26 11:15:29 +0800240 ex = X509V3_EXT_conf_nid(nullptr, &ctx, nid, const_cast<char*>(value));
Ed Tanouse662eae2022-01-25 10:39:19 -0800241 if (ex == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800242 {
243 BMCWEB_LOG_ERROR << "Error: In X509V3_EXT_conf_nidn: " << value;
244 return -1;
245 }
246 X509_add_ext(cert, ex, -1);
247 X509_EXTENSION_free(ex);
248 return 0;
249}
250
251inline void generateSslCertificate(const std::string& filepath,
252 const std::string& cn)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700253{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500254 FILE* pFile = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700255 std::cout << "Generating new keys\n";
256 initOpenssl();
Ed Tanous0fdddb12017-02-28 11:06:34 -0800257
Ed Tanous1abe55e2018-09-05 08:30:59 -0700258 std::cerr << "Generating EC key\n";
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500259 EVP_PKEY* pPrivKey = createEcKey();
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700260 if (pPrivKey != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700261 {
262 std::cerr << "Generating x509 Certificate\n";
263 // Use this code to directly generate a certificate
Ed Tanous543f4402022-01-06 13:12:53 -0800264 X509* x509 = X509_new();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700265 if (x509 != nullptr)
266 {
267 // get a random number from the RNG for the certificate serial
268 // number If this is not random, regenerating certs throws broswer
269 // errors
Alan Kuoa8220702020-11-26 11:15:29 +0800270 bmcweb::OpenSSLGenerator gen;
271 std::uniform_int_distribution<int> dis(
272 1, std::numeric_limits<int>::max());
273 int serial = dis(gen);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800274
Ed Tanous1abe55e2018-09-05 08:30:59 -0700275 ASN1_INTEGER_set(X509_get_serialNumber(x509), serial);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800276
Ed Tanous1abe55e2018-09-05 08:30:59 -0700277 // not before this moment
278 X509_gmtime_adj(X509_get_notBefore(x509), 0);
279 // Cert is valid for 10 years
280 X509_gmtime_adj(X509_get_notAfter(x509),
281 60L * 60L * 24L * 365L * 10L);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800282
Ed Tanous1abe55e2018-09-05 08:30:59 -0700283 // set the public key to the key we just generated
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700284 X509_set_pubkey(x509, pPrivKey);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800285
Ed Tanous1abe55e2018-09-05 08:30:59 -0700286 // get the subject name
Ed Tanous543f4402022-01-06 13:12:53 -0800287 X509_NAME* name = X509_get_subject_name(x509);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800288
Ed Tanous46ff87b2022-01-07 09:25:51 -0800289 using x509String = const unsigned char;
290 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
291 x509String* country = reinterpret_cast<x509String*>("US");
292 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
293 x509String* company = reinterpret_cast<x509String*>("OpenBMC");
294 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
295 x509String* cnStr = reinterpret_cast<x509String*>(cn.c_str());
296
297 X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, country, -1, -1,
298 0);
299 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, company, -1, -1,
300 0);
301 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, cnStr, -1, -1,
302 0);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700303 // set the CSR options
304 X509_set_issuer_name(x509, name);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800305
Alan Kuoa8220702020-11-26 11:15:29 +0800306 X509_set_version(x509, 2);
307 addExt(x509, NID_basic_constraints, ("critical,CA:TRUE"));
308 addExt(x509, NID_subject_alt_name, ("DNS:" + cn).c_str());
309 addExt(x509, NID_subject_key_identifier, ("hash"));
310 addExt(x509, NID_authority_key_identifier, ("keyid"));
311 addExt(x509, NID_key_usage, ("digitalSignature, keyEncipherment"));
312 addExt(x509, NID_ext_key_usage, ("serverAuth"));
313 addExt(x509, NID_netscape_comment, (x509Comment));
314
Ed Tanous1abe55e2018-09-05 08:30:59 -0700315 // Sign the certificate with our private key
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700316 X509_sign(x509, pPrivKey, EVP_sha256());
Ed Tanous0fdddb12017-02-28 11:06:34 -0800317
Ed Tanous1abe55e2018-09-05 08:30:59 -0700318 pFile = fopen(filepath.c_str(), "wt");
Ed Tanous0fdddb12017-02-28 11:06:34 -0800319
Ed Tanous1abe55e2018-09-05 08:30:59 -0700320 if (pFile != nullptr)
321 {
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700322 PEM_write_PrivateKey(pFile, pPrivKey, nullptr, nullptr, 0,
Ed Tanous99131cd2019-10-24 11:12:47 -0700323 nullptr, nullptr);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800324
Ed Tanous1abe55e2018-09-05 08:30:59 -0700325 PEM_write_X509(pFile, x509);
326 fclose(pFile);
Ed Tanous99131cd2019-10-24 11:12:47 -0700327 pFile = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700328 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800329
Ed Tanous1abe55e2018-09-05 08:30:59 -0700330 X509_free(x509);
331 }
332
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700333 EVP_PKEY_free(pPrivKey);
334 pPrivKey = nullptr;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800335 }
336
Ed Tanous1abe55e2018-09-05 08:30:59 -0700337 // cleanup_openssl();
Ed Tanous99923322017-03-03 14:21:24 -0800338}
Ed Tanousb01bf292019-03-25 19:25:26 +0000339
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500340EVP_PKEY* createEcKey()
Ed Tanousb01bf292019-03-25 19:25:26 +0000341{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500342 EVP_PKEY* pKey = nullptr;
Patrick Williamsaec70662021-12-13 16:55:46 -0600343
344#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
Ed Tanousb01bf292019-03-25 19:25:26 +0000345 int eccgrp = 0;
Vernon Maueryaaf32062020-03-09 10:41:31 -0700346 eccgrp = OBJ_txt2nid("secp384r1");
Ed Tanous6ea007a2019-02-13 22:48:25 -0800347
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500348 EC_KEY* myecc = EC_KEY_new_by_curve_name(eccgrp);
Ed Tanousb01bf292019-03-25 19:25:26 +0000349 if (myecc != nullptr)
Ed Tanous6ea007a2019-02-13 22:48:25 -0800350 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000351 EC_KEY_set_asn1_flag(myecc, OPENSSL_EC_NAMED_CURVE);
352 EC_KEY_generate_key(myecc);
353 pKey = EVP_PKEY_new();
354 if (pKey != nullptr)
355 {
Szymon Dompkea327dc52022-03-01 14:04:14 +0100356 if (EVP_PKEY_assign(pKey, EVP_PKEY_EC, myecc) != 0)
Ed Tanousb01bf292019-03-25 19:25:26 +0000357 {
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700358 /* pKey owns myecc from now */
Ed Tanousb01bf292019-03-25 19:25:26 +0000359 if (EC_KEY_check_key(myecc) <= 0)
360 {
Szymon Dompkea327dc52022-03-01 14:04:14 +0100361 std::cerr << "EC_check_key failed.\n";
Ed Tanousb01bf292019-03-25 19:25:26 +0000362 }
363 }
364 }
Ed Tanous6ea007a2019-02-13 22:48:25 -0800365 }
Patrick Williamsaec70662021-12-13 16:55:46 -0600366#else
367 // Create context for curve parameter generation.
368 std::unique_ptr<EVP_PKEY_CTX, decltype(&::EVP_PKEY_CTX_free)> ctx{
369 EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr), &::EVP_PKEY_CTX_free};
370 if (!ctx)
371 {
372 return nullptr;
373 }
374
375 // Set up curve parameters.
376 EVP_PKEY* params = nullptr;
377 if ((EVP_PKEY_paramgen_init(ctx.get()) <= 0) ||
378 (EVP_PKEY_CTX_set_ec_param_enc(ctx.get(), OPENSSL_EC_NAMED_CURVE) <=
379 0) ||
380 (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(), NID_secp384r1) <=
381 0) ||
382 (EVP_PKEY_paramgen(ctx.get(), &params) <= 0))
383 {
384 return nullptr;
385 }
386
387 // Set up RAII holder for params.
388 std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)> pparams{
389 params, &::EVP_PKEY_free};
390
391 // Set new context for key generation, using curve parameters.
392 ctx.reset(EVP_PKEY_CTX_new_from_pkey(nullptr, params, nullptr));
393 if (!ctx || (EVP_PKEY_keygen_init(ctx.get()) <= 0))
394 {
395 return nullptr;
396 }
397
398 // Generate key.
399 if (EVP_PKEY_keygen(ctx.get(), &pKey) <= 0)
400 {
401 return nullptr;
402 }
403#endif
404
Ed Tanous1abe55e2018-09-05 08:30:59 -0700405 return pKey;
406}
407
408void initOpenssl()
409{
Ed Tanous911ac312017-08-15 09:37:42 -0700410#if OPENSSL_VERSION_NUMBER < 0x10100000L
Ed Tanous1abe55e2018-09-05 08:30:59 -0700411 SSL_load_error_strings();
412 OpenSSL_add_all_algorithms();
413 RAND_load_file("/dev/urandom", 1024);
Ed Tanousa1e077c2017-04-25 12:42:19 -0700414#endif
Ed Tanous0fdddb12017-02-28 11:06:34 -0800415}
416
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500417inline void ensureOpensslKeyPresentAndValid(const std::string& filepath)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700418{
419 bool pemFileValid = false;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800420
Ed Tanous1abe55e2018-09-05 08:30:59 -0700421 pemFileValid = verifyOpensslKeyCert(filepath);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700422
Ed Tanous1abe55e2018-09-05 08:30:59 -0700423 if (!pemFileValid)
424 {
425 std::cerr << "Error in verifying signature, regenerating\n";
Alan Kuoa8220702020-11-26 11:15:29 +0800426 generateSslCertificate(filepath, "testhost");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700427 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800428}
Ed Tanousc4771fb2017-03-13 13:39:49 -0700429
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800430inline int nextProtoCallback(SSL* /*unused*/, const unsigned char** data,
431 unsigned int* len, void* /*unused*/)
432{
433 // First byte is the length.
434 constexpr std::string_view h2 = "\x02h2";
435 *data = std::bit_cast<const unsigned char*>(h2.data());
436 *len = static_cast<unsigned int>(h2.size());
437 return SSL_TLSEXT_ERR_OK;
438}
439
440inline int alpnSelectProtoCallback(SSL* /*unused*/, const unsigned char** out,
441 unsigned char* outlen,
442 const unsigned char* in, unsigned int inlen,
443 void* /*unused*/)
444{
445 // There's a mismatch in constness for nghttp2_select_next_protocol. The
446 // examples in nghttp2 don't show this problem. Unclear what the right fix
447 // is here.
448
449 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
450 unsigned char** outNew = const_cast<unsigned char**>(out);
451 int rv = nghttp2_select_next_protocol(outNew, outlen, in, inlen);
452 if (rv != 1)
453 {
454 return SSL_TLSEXT_ERR_NOACK;
455 }
456
457 return SSL_TLSEXT_ERR_OK;
458}
459
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600460inline std::shared_ptr<boost::asio::ssl::context>
Ed Tanous81ce6092020-12-17 16:54:55 +0000461 getSslContext(const std::string& sslPemFile)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700462{
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600463 std::shared_ptr<boost::asio::ssl::context> mSslContext =
464 std::make_shared<boost::asio::ssl::context>(
465 boost::asio::ssl::context::tls_server);
466 mSslContext->set_options(boost::asio::ssl::context::default_workarounds |
467 boost::asio::ssl::context::no_sslv2 |
468 boost::asio::ssl::context::no_sslv3 |
469 boost::asio::ssl::context::single_dh_use |
470 boost::asio::ssl::context::no_tlsv1 |
471 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700472
James Feist91243c32019-11-12 14:55:40 -0800473 // BIG WARNING: This needs to stay disabled, as there will always be
474 // unauthenticated endpoints
475 // mSslContext->set_verify_mode(boost::asio::ssl::verify_peer);
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200476
James Feist6a3e1822019-11-06 13:46:35 -0800477 SSL_CTX_set_options(mSslContext->native_handle(), SSL_OP_NO_RENEGOTIATION);
478
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200479 BMCWEB_LOG_DEBUG << "Using default TrustStore location: " << trustStorePath;
480 mSslContext->add_verify_path(trustStorePath);
481
Ed Tanous81ce6092020-12-17 16:54:55 +0000482 mSslContext->use_certificate_file(sslPemFile,
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600483 boost::asio::ssl::context::pem);
Ed Tanous81ce6092020-12-17 16:54:55 +0000484 mSslContext->use_private_key_file(sslPemFile,
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600485 boost::asio::ssl::context::pem);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700486
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800487 if constexpr (bmcwebEnableHTTP2)
488 {
489 SSL_CTX_set_next_protos_advertised_cb(mSslContext->native_handle(),
490 nextProtoCallback, nullptr);
491
492 SSL_CTX_set_alpn_select_cb(mSslContext->native_handle(),
493 alpnSelectProtoCallback, nullptr);
494 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700495 // Set up EC curves to auto (boost asio doesn't have a method for this)
496 // There is a pull request to add this. Once this is included in an asio
497 // drop, use the right way
498 // http://stackoverflow.com/questions/18929049/boost-asio-with-ecdsa-certificate-issue
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600499 if (SSL_CTX_set_ecdh_auto(mSslContext->native_handle(), 1) != 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700500 {
501 BMCWEB_LOG_ERROR << "Error setting tmp ecdh list\n";
502 }
Ed Tanousc4771fb2017-03-13 13:39:49 -0700503
Ed Tanouse96d7fb2023-06-14 14:53:29 -0700504 // Mozilla intermediate cipher suites v5.7
505 // Sourced from: https://ssl-config.mozilla.org/guidelines/5.7.json
506 const char* mozillaIntermediate = "ECDHE-ECDSA-AES128-GCM-SHA256:"
507 "ECDHE-RSA-AES128-GCM-SHA256:"
508 "ECDHE-ECDSA-AES256-GCM-SHA384:"
509 "ECDHE-RSA-AES256-GCM-SHA384:"
510 "ECDHE-ECDSA-CHACHA20-POLY1305:"
511 "ECDHE-RSA-CHACHA20-POLY1305:"
512 "DHE-RSA-AES128-GCM-SHA256:"
513 "DHE-RSA-AES256-GCM-SHA384:"
514 "DHE-RSA-CHACHA20-POLY1305";
Ed Tanousc4771fb2017-03-13 13:39:49 -0700515
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600516 if (SSL_CTX_set_cipher_list(mSslContext->native_handle(),
Ed Tanouse96d7fb2023-06-14 14:53:29 -0700517 mozillaIntermediate) != 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700518 {
519 BMCWEB_LOG_ERROR << "Error setting cipher list\n";
520 }
521 return mSslContext;
Ed Tanousc4771fb2017-03-13 13:39:49 -0700522}
AppaRao Pulie38778a2022-06-27 23:09:03 +0000523
524inline std::optional<boost::asio::ssl::context> getSSLClientContext()
525{
526 boost::asio::ssl::context sslCtx(boost::asio::ssl::context::tls_client);
527
528 boost::system::error_code ec;
529
530 // Support only TLS v1.2 & v1.3
531 sslCtx.set_options(boost::asio::ssl::context::default_workarounds |
532 boost::asio::ssl::context::no_sslv2 |
533 boost::asio::ssl::context::no_sslv3 |
534 boost::asio::ssl::context::single_dh_use |
535 boost::asio::ssl::context::no_tlsv1 |
536 boost::asio::ssl::context::no_tlsv1_1,
537 ec);
538 if (ec)
539 {
540 BMCWEB_LOG_ERROR << "SSL context set_options failed";
541 return std::nullopt;
542 }
543
544 // Add a directory containing certificate authority files to be used
545 // for performing verification.
546 sslCtx.set_default_verify_paths(ec);
547 if (ec)
548 {
549 BMCWEB_LOG_ERROR << "SSL context set_default_verify failed";
550 return std::nullopt;
551 }
552
553 // Verify the remote server's certificate
554 sslCtx.set_verify_mode(boost::asio::ssl::verify_peer, ec);
555 if (ec)
556 {
557 BMCWEB_LOG_ERROR << "SSL context set_verify_mode failed";
558 return std::nullopt;
559 }
560
561 // All cipher suites are set as per OWASP datasheet.
562 // https://cheatsheetseries.owasp.org/cheatsheets/TLS_Cipher_String_Cheat_Sheet.html
563 constexpr const char* sslCiphers = "ECDHE-ECDSA-AES128-GCM-SHA256:"
564 "ECDHE-RSA-AES128-GCM-SHA256:"
565 "ECDHE-ECDSA-AES256-GCM-SHA384:"
566 "ECDHE-RSA-AES256-GCM-SHA384:"
567 "ECDHE-ECDSA-CHACHA20-POLY1305:"
568 "ECDHE-RSA-CHACHA20-POLY1305:"
569 "DHE-RSA-AES128-GCM-SHA256:"
570 "DHE-RSA-AES256-GCM-SHA384"
571 "TLS_AES_128_GCM_SHA256:"
572 "TLS_AES_256_GCM_SHA384:"
573 "TLS_CHACHA20_POLY1305_SHA256";
574
575 if (SSL_CTX_set_cipher_list(sslCtx.native_handle(), sslCiphers) != 1)
576 {
577 BMCWEB_LOG_ERROR << "SSL_CTX_set_cipher_list failed";
578 return std::nullopt;
579 }
580
581 return sslCtx;
582}
583
Ed Tanous1abe55e2018-09-05 08:30:59 -0700584} // namespace ensuressl