blob: 71528417de415fd67af979b8c86dad8d4d30341c [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 Tanous0fdddb12017-02-28 11:06:34 -08006#include <openssl/bio.h>
7#include <openssl/dh.h>
8#include <openssl/dsa.h>
Ed Tanous0fdddb12017-02-28 11:06:34 -08009#include <openssl/err.h>
10#include <openssl/evp.h>
11#include <openssl/pem.h>
12#include <openssl/rand.h>
13#include <openssl/rsa.h>
14#include <openssl/ssl.h>
Ed Tanous9b65f1f2017-03-07 15:17:13 -080015
Ed Tanous3112a142018-11-29 15:45:10 -080016#include <boost/asio/ssl/context.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050017
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include <optional>
Ed Tanous1abe55e2018-09-05 08:30:59 -070019#include <random>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include <string>
Ed Tanous1abe55e2018-09-05 08:30:59 -070021
22namespace ensuressl
23{
Patrick Williams89492a12023-05-10 07:51:34 -050024constexpr const char* trustStorePath = "/etc/ssl/certs/authority";
25constexpr const char* x509Comment = "Generated from OpenBMC service";
Ed Tanous55c7b7a2018-05-22 15:27:24 -070026static void initOpenssl();
Gunnar Mills1214b7e2020-06-04 10:11:30 -050027static EVP_PKEY* createEcKey();
Ed Tanous0fdddb12017-02-28 11:06:34 -080028
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050029// Trust chain related errors.`
30inline bool isTrustChainError(int errnum)
31{
Ed Tanous55f79e62022-01-25 11:26:16 -080032 return (errnum == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) ||
33 (errnum == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) ||
34 (errnum == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) ||
35 (errnum == X509_V_ERR_CERT_UNTRUSTED) ||
36 (errnum == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE);
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050037}
38
Gunnar Mills1214b7e2020-06-04 10:11:30 -050039inline bool validateCertificate(X509* const cert)
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050040{
41 // Create an empty X509_STORE structure for certificate validation.
Gunnar Mills1214b7e2020-06-04 10:11:30 -050042 X509_STORE* x509Store = X509_STORE_new();
Ed Tanouse662eae2022-01-25 10:39:19 -080043 if (x509Store == nullptr)
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050044 {
Gunnar Millsc61704a2020-07-08 13:47:06 -050045 BMCWEB_LOG_ERROR << "Error occurred during X509_STORE_new call";
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050046 return false;
47 }
48
49 // Load Certificate file into the X509 structure.
Gunnar Mills1214b7e2020-06-04 10:11:30 -050050 X509_STORE_CTX* storeCtx = X509_STORE_CTX_new();
Ed Tanouse662eae2022-01-25 10:39:19 -080051 if (storeCtx == nullptr)
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050052 {
Gunnar Millsc61704a2020-07-08 13:47:06 -050053 BMCWEB_LOG_ERROR << "Error occurred during X509_STORE_CTX_new call";
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050054 X509_STORE_free(x509Store);
55 return false;
56 }
57
Ed Tanous99131cd2019-10-24 11:12:47 -070058 int errCode = X509_STORE_CTX_init(storeCtx, x509Store, cert, nullptr);
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050059 if (errCode != 1)
60 {
Gunnar Millsc61704a2020-07-08 13:47:06 -050061 BMCWEB_LOG_ERROR << "Error occurred during X509_STORE_CTX_init call";
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050062 X509_STORE_CTX_free(storeCtx);
63 X509_STORE_free(x509Store);
64 return false;
65 }
66
67 errCode = X509_verify_cert(storeCtx);
68 if (errCode == 1)
69 {
70 BMCWEB_LOG_INFO << "Certificate verification is success";
71 X509_STORE_CTX_free(storeCtx);
72 X509_STORE_free(x509Store);
73 return true;
74 }
75 if (errCode == 0)
76 {
77 errCode = X509_STORE_CTX_get_error(storeCtx);
78 X509_STORE_CTX_free(storeCtx);
79 X509_STORE_free(x509Store);
80 if (isTrustChainError(errCode))
81 {
82 BMCWEB_LOG_DEBUG << "Ignoring Trust Chain error. Reason: "
83 << X509_verify_cert_error_string(errCode);
84 return true;
85 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070086 BMCWEB_LOG_ERROR << "Certificate verification failed. Reason: "
87 << X509_verify_cert_error_string(errCode);
88 return false;
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050089 }
90
91 BMCWEB_LOG_ERROR
Gunnar Millsc61704a2020-07-08 13:47:06 -050092 << "Error occurred during X509_verify_cert call. ErrorCode: "
93 << errCode;
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050094 X509_STORE_CTX_free(storeCtx);
95 X509_STORE_free(x509Store);
96 return false;
97}
98
Gunnar Mills1214b7e2020-06-04 10:11:30 -050099inline bool verifyOpensslKeyCert(const std::string& filepath)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100{
101 bool privateKeyValid = false;
102 bool certValid = false;
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800103
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 std::cout << "Checking certs in file " << filepath << "\n";
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800105
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500106 FILE* file = fopen(filepath.c_str(), "r");
Ed Tanous99131cd2019-10-24 11:12:47 -0700107 if (file != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500109 EVP_PKEY* pkey = PEM_read_PrivateKey(file, nullptr, nullptr, nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110 if (pkey != nullptr)
111 {
Patrick Williams145bb762021-12-07 21:05:04 -0600112#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500113 RSA* rsa = EVP_PKEY_get1_RSA(pkey);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 if (rsa != nullptr)
115 {
116 std::cout << "Found an RSA key\n";
117 if (RSA_check_key(rsa) == 1)
118 {
Jayanth Othayoth4d2849a2019-05-13 01:46:34 -0500119 privateKeyValid = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 }
121 else
122 {
123 std::cerr << "Key not valid error number "
124 << ERR_get_error() << "\n";
125 }
126 RSA_free(rsa);
127 }
128 else
129 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500130 EC_KEY* ec = EVP_PKEY_get1_EC_KEY(pkey);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700131 if (ec != nullptr)
132 {
133 std::cout << "Found an EC key\n";
134 if (EC_KEY_check_key(ec) == 1)
135 {
136 privateKeyValid = true;
137 }
138 else
139 {
140 std::cerr << "Key not valid error number "
141 << ERR_get_error() << "\n";
142 }
143 EC_KEY_free(ec);
144 }
145 }
Patrick Williams145bb762021-12-07 21:05:04 -0600146#else
Patrick Williams89492a12023-05-10 07:51:34 -0500147 EVP_PKEY_CTX* pkeyCtx = EVP_PKEY_CTX_new_from_pkey(nullptr, pkey,
148 nullptr);
Patrick Williams145bb762021-12-07 21:05:04 -0600149
Ed Tanouse662eae2022-01-25 10:39:19 -0800150 if (pkeyCtx == nullptr)
Patrick Williams145bb762021-12-07 21:05:04 -0600151 {
Gunnar Mills91359f92022-01-04 14:50:18 -0600152 std::cerr << "Unable to allocate pkeyCtx " << ERR_get_error()
Patrick Williams145bb762021-12-07 21:05:04 -0600153 << "\n";
154 }
Gunnar Mills91359f92022-01-04 14:50:18 -0600155 else if (EVP_PKEY_check(pkeyCtx) == 1)
Patrick Williams145bb762021-12-07 21:05:04 -0600156 {
157 privateKeyValid = true;
158 }
159 else
160 {
Patrick Williams145bb762021-12-07 21:05:04 -0600161 std::cerr << "Key not valid error number " << ERR_get_error()
162 << "\n";
163 }
164#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165
166 if (privateKeyValid)
167 {
Ramesh Iyyarc0bf8932019-06-22 00:23:29 -0500168 // If the order is certificate followed by key in input file
169 // then, certificate read will fail. So, setting the file
170 // pointer to point beginning of file to avoid certificate and
171 // key order issue.
172 fseek(file, 0, SEEK_SET);
173
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500174 X509* x509 = PEM_read_X509(file, nullptr, nullptr, nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 if (x509 == nullptr)
176 {
177 std::cout << "error getting x509 cert " << ERR_get_error()
178 << "\n";
179 }
180 else
181 {
Ramesh Iyyar082f28f2019-06-22 03:31:55 -0500182 certValid = validateCertificate(x509);
183 X509_free(x509);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184 }
185 }
186
Patrick Williams145bb762021-12-07 21:05:04 -0600187#if (OPENSSL_VERSION_NUMBER > 0x30000000L)
Gunnar Mills91359f92022-01-04 14:50:18 -0600188 EVP_PKEY_CTX_free(pkeyCtx);
Patrick Williams145bb762021-12-07 21:05:04 -0600189#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700190 EVP_PKEY_free(pkey);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800191 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700192 fclose(file);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800193 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700194 return certValid;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800195}
196
Alan Kuoa8220702020-11-26 11:15:29 +0800197inline X509* loadCert(const std::string& filePath)
198{
199 BIO* certFileBio = BIO_new_file(filePath.c_str(), "rb");
Ed Tanouse662eae2022-01-25 10:39:19 -0800200 if (certFileBio == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800201 {
202 BMCWEB_LOG_ERROR << "Error occured during BIO_new_file call, "
203 << "FILE= " << filePath;
204 return nullptr;
205 }
206
207 X509* cert = X509_new();
Ed Tanouse662eae2022-01-25 10:39:19 -0800208 if (cert == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800209 {
210 BMCWEB_LOG_ERROR << "Error occured during X509_new call, "
211 << ERR_get_error();
212 BIO_free(certFileBio);
213 return nullptr;
214 }
215
Ed Tanouse662eae2022-01-25 10:39:19 -0800216 if (PEM_read_bio_X509(certFileBio, &cert, nullptr, nullptr) == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800217 {
218 BMCWEB_LOG_ERROR << "Error occured during PEM_read_bio_X509 call, "
219 << "FILE= " << filePath;
220
221 BIO_free(certFileBio);
222 X509_free(cert);
223 return nullptr;
224 }
Alan Kuob295bf92021-04-16 14:16:47 +0800225 BIO_free(certFileBio);
Alan Kuoa8220702020-11-26 11:15:29 +0800226 return cert;
227}
228
229inline int addExt(X509* cert, int nid, const char* value)
230{
231 X509_EXTENSION* ex = nullptr;
Ed Tanousb00dcc22021-02-23 12:52:50 -0800232 X509V3_CTX ctx{};
Alan Kuoa8220702020-11-26 11:15:29 +0800233 X509V3_set_ctx(&ctx, cert, cert, nullptr, nullptr, 0);
Ed Tanous4ecc6182022-01-07 09:36:26 -0800234
235 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
Alan Kuoa8220702020-11-26 11:15:29 +0800236 ex = X509V3_EXT_conf_nid(nullptr, &ctx, nid, const_cast<char*>(value));
Ed Tanouse662eae2022-01-25 10:39:19 -0800237 if (ex == nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800238 {
239 BMCWEB_LOG_ERROR << "Error: In X509V3_EXT_conf_nidn: " << value;
240 return -1;
241 }
242 X509_add_ext(cert, ex, -1);
243 X509_EXTENSION_free(ex);
244 return 0;
245}
246
247inline void generateSslCertificate(const std::string& filepath,
248 const std::string& cn)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700249{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500250 FILE* pFile = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700251 std::cout << "Generating new keys\n";
252 initOpenssl();
Ed Tanous0fdddb12017-02-28 11:06:34 -0800253
Ed Tanous1abe55e2018-09-05 08:30:59 -0700254 std::cerr << "Generating EC key\n";
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500255 EVP_PKEY* pPrivKey = createEcKey();
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700256 if (pPrivKey != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700257 {
258 std::cerr << "Generating x509 Certificate\n";
259 // Use this code to directly generate a certificate
Ed Tanous543f4402022-01-06 13:12:53 -0800260 X509* x509 = X509_new();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700261 if (x509 != nullptr)
262 {
263 // get a random number from the RNG for the certificate serial
264 // number If this is not random, regenerating certs throws broswer
265 // errors
Alan Kuoa8220702020-11-26 11:15:29 +0800266 bmcweb::OpenSSLGenerator gen;
267 std::uniform_int_distribution<int> dis(
268 1, std::numeric_limits<int>::max());
269 int serial = dis(gen);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800270
Ed Tanous1abe55e2018-09-05 08:30:59 -0700271 ASN1_INTEGER_set(X509_get_serialNumber(x509), serial);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800272
Ed Tanous1abe55e2018-09-05 08:30:59 -0700273 // not before this moment
274 X509_gmtime_adj(X509_get_notBefore(x509), 0);
275 // Cert is valid for 10 years
276 X509_gmtime_adj(X509_get_notAfter(x509),
277 60L * 60L * 24L * 365L * 10L);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800278
Ed Tanous1abe55e2018-09-05 08:30:59 -0700279 // set the public key to the key we just generated
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700280 X509_set_pubkey(x509, pPrivKey);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800281
Ed Tanous1abe55e2018-09-05 08:30:59 -0700282 // get the subject name
Ed Tanous543f4402022-01-06 13:12:53 -0800283 X509_NAME* name = X509_get_subject_name(x509);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800284
Ed Tanous46ff87b2022-01-07 09:25:51 -0800285 using x509String = const unsigned char;
286 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
287 x509String* country = reinterpret_cast<x509String*>("US");
288 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
289 x509String* company = reinterpret_cast<x509String*>("OpenBMC");
290 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
291 x509String* cnStr = reinterpret_cast<x509String*>(cn.c_str());
292
293 X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, country, -1, -1,
294 0);
295 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, company, -1, -1,
296 0);
297 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, cnStr, -1, -1,
298 0);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700299 // set the CSR options
300 X509_set_issuer_name(x509, name);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800301
Alan Kuoa8220702020-11-26 11:15:29 +0800302 X509_set_version(x509, 2);
303 addExt(x509, NID_basic_constraints, ("critical,CA:TRUE"));
304 addExt(x509, NID_subject_alt_name, ("DNS:" + cn).c_str());
305 addExt(x509, NID_subject_key_identifier, ("hash"));
306 addExt(x509, NID_authority_key_identifier, ("keyid"));
307 addExt(x509, NID_key_usage, ("digitalSignature, keyEncipherment"));
308 addExt(x509, NID_ext_key_usage, ("serverAuth"));
309 addExt(x509, NID_netscape_comment, (x509Comment));
310
Ed Tanous1abe55e2018-09-05 08:30:59 -0700311 // Sign the certificate with our private key
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700312 X509_sign(x509, pPrivKey, EVP_sha256());
Ed Tanous0fdddb12017-02-28 11:06:34 -0800313
Ed Tanous1abe55e2018-09-05 08:30:59 -0700314 pFile = fopen(filepath.c_str(), "wt");
Ed Tanous0fdddb12017-02-28 11:06:34 -0800315
Ed Tanous1abe55e2018-09-05 08:30:59 -0700316 if (pFile != nullptr)
317 {
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700318 PEM_write_PrivateKey(pFile, pPrivKey, nullptr, nullptr, 0,
Ed Tanous99131cd2019-10-24 11:12:47 -0700319 nullptr, nullptr);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800320
Ed Tanous1abe55e2018-09-05 08:30:59 -0700321 PEM_write_X509(pFile, x509);
322 fclose(pFile);
Ed Tanous99131cd2019-10-24 11:12:47 -0700323 pFile = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700324 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800325
Ed Tanous1abe55e2018-09-05 08:30:59 -0700326 X509_free(x509);
327 }
328
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700329 EVP_PKEY_free(pPrivKey);
330 pPrivKey = nullptr;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800331 }
332
Ed Tanous1abe55e2018-09-05 08:30:59 -0700333 // cleanup_openssl();
Ed Tanous99923322017-03-03 14:21:24 -0800334}
Ed Tanousb01bf292019-03-25 19:25:26 +0000335
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500336EVP_PKEY* createEcKey()
Ed Tanousb01bf292019-03-25 19:25:26 +0000337{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500338 EVP_PKEY* pKey = nullptr;
Patrick Williamsaec70662021-12-13 16:55:46 -0600339
340#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
Ed Tanousb01bf292019-03-25 19:25:26 +0000341 int eccgrp = 0;
Vernon Maueryaaf32062020-03-09 10:41:31 -0700342 eccgrp = OBJ_txt2nid("secp384r1");
Ed Tanous6ea007a2019-02-13 22:48:25 -0800343
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500344 EC_KEY* myecc = EC_KEY_new_by_curve_name(eccgrp);
Ed Tanousb01bf292019-03-25 19:25:26 +0000345 if (myecc != nullptr)
Ed Tanous6ea007a2019-02-13 22:48:25 -0800346 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000347 EC_KEY_set_asn1_flag(myecc, OPENSSL_EC_NAMED_CURVE);
348 EC_KEY_generate_key(myecc);
349 pKey = EVP_PKEY_new();
350 if (pKey != nullptr)
351 {
Szymon Dompkea327dc52022-03-01 14:04:14 +0100352 if (EVP_PKEY_assign(pKey, EVP_PKEY_EC, myecc) != 0)
Ed Tanousb01bf292019-03-25 19:25:26 +0000353 {
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700354 /* pKey owns myecc from now */
Ed Tanousb01bf292019-03-25 19:25:26 +0000355 if (EC_KEY_check_key(myecc) <= 0)
356 {
Szymon Dompkea327dc52022-03-01 14:04:14 +0100357 std::cerr << "EC_check_key failed.\n";
Ed Tanousb01bf292019-03-25 19:25:26 +0000358 }
359 }
360 }
Ed Tanous6ea007a2019-02-13 22:48:25 -0800361 }
Patrick Williamsaec70662021-12-13 16:55:46 -0600362#else
363 // Create context for curve parameter generation.
364 std::unique_ptr<EVP_PKEY_CTX, decltype(&::EVP_PKEY_CTX_free)> ctx{
365 EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr), &::EVP_PKEY_CTX_free};
366 if (!ctx)
367 {
368 return nullptr;
369 }
370
371 // Set up curve parameters.
372 EVP_PKEY* params = nullptr;
373 if ((EVP_PKEY_paramgen_init(ctx.get()) <= 0) ||
374 (EVP_PKEY_CTX_set_ec_param_enc(ctx.get(), OPENSSL_EC_NAMED_CURVE) <=
375 0) ||
376 (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(), NID_secp384r1) <=
377 0) ||
378 (EVP_PKEY_paramgen(ctx.get(), &params) <= 0))
379 {
380 return nullptr;
381 }
382
383 // Set up RAII holder for params.
384 std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)> pparams{
385 params, &::EVP_PKEY_free};
386
387 // Set new context for key generation, using curve parameters.
388 ctx.reset(EVP_PKEY_CTX_new_from_pkey(nullptr, params, nullptr));
389 if (!ctx || (EVP_PKEY_keygen_init(ctx.get()) <= 0))
390 {
391 return nullptr;
392 }
393
394 // Generate key.
395 if (EVP_PKEY_keygen(ctx.get(), &pKey) <= 0)
396 {
397 return nullptr;
398 }
399#endif
400
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401 return pKey;
402}
403
404void initOpenssl()
405{
Ed Tanous911ac312017-08-15 09:37:42 -0700406#if OPENSSL_VERSION_NUMBER < 0x10100000L
Ed Tanous1abe55e2018-09-05 08:30:59 -0700407 SSL_load_error_strings();
408 OpenSSL_add_all_algorithms();
409 RAND_load_file("/dev/urandom", 1024);
Ed Tanousa1e077c2017-04-25 12:42:19 -0700410#endif
Ed Tanous0fdddb12017-02-28 11:06:34 -0800411}
412
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500413inline void ensureOpensslKeyPresentAndValid(const std::string& filepath)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700414{
415 bool pemFileValid = false;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800416
Ed Tanous1abe55e2018-09-05 08:30:59 -0700417 pemFileValid = verifyOpensslKeyCert(filepath);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700418
Ed Tanous1abe55e2018-09-05 08:30:59 -0700419 if (!pemFileValid)
420 {
421 std::cerr << "Error in verifying signature, regenerating\n";
Alan Kuoa8220702020-11-26 11:15:29 +0800422 generateSslCertificate(filepath, "testhost");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700423 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800424}
Ed Tanousc4771fb2017-03-13 13:39:49 -0700425
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600426inline std::shared_ptr<boost::asio::ssl::context>
Ed Tanous81ce6092020-12-17 16:54:55 +0000427 getSslContext(const std::string& sslPemFile)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700428{
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600429 std::shared_ptr<boost::asio::ssl::context> mSslContext =
430 std::make_shared<boost::asio::ssl::context>(
431 boost::asio::ssl::context::tls_server);
432 mSslContext->set_options(boost::asio::ssl::context::default_workarounds |
433 boost::asio::ssl::context::no_sslv2 |
434 boost::asio::ssl::context::no_sslv3 |
435 boost::asio::ssl::context::single_dh_use |
436 boost::asio::ssl::context::no_tlsv1 |
437 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700438
James Feist91243c32019-11-12 14:55:40 -0800439 // BIG WARNING: This needs to stay disabled, as there will always be
440 // unauthenticated endpoints
441 // mSslContext->set_verify_mode(boost::asio::ssl::verify_peer);
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200442
James Feist6a3e1822019-11-06 13:46:35 -0800443 SSL_CTX_set_options(mSslContext->native_handle(), SSL_OP_NO_RENEGOTIATION);
444
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200445 BMCWEB_LOG_DEBUG << "Using default TrustStore location: " << trustStorePath;
446 mSslContext->add_verify_path(trustStorePath);
447
Ed Tanous81ce6092020-12-17 16:54:55 +0000448 mSslContext->use_certificate_file(sslPemFile,
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600449 boost::asio::ssl::context::pem);
Ed Tanous81ce6092020-12-17 16:54:55 +0000450 mSslContext->use_private_key_file(sslPemFile,
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600451 boost::asio::ssl::context::pem);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700452
Ed Tanous1abe55e2018-09-05 08:30:59 -0700453 // Set up EC curves to auto (boost asio doesn't have a method for this)
454 // There is a pull request to add this. Once this is included in an asio
455 // drop, use the right way
456 // http://stackoverflow.com/questions/18929049/boost-asio-with-ecdsa-certificate-issue
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600457 if (SSL_CTX_set_ecdh_auto(mSslContext->native_handle(), 1) != 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700458 {
459 BMCWEB_LOG_ERROR << "Error setting tmp ecdh list\n";
460 }
Ed Tanousc4771fb2017-03-13 13:39:49 -0700461
Ed Tanous457207f2019-02-07 15:27:14 -0800462 std::string mozillaModern = "ECDHE-ECDSA-AES256-GCM-SHA384:"
463 "ECDHE-RSA-AES256-GCM-SHA384:"
464 "ECDHE-ECDSA-CHACHA20-POLY1305:"
465 "ECDHE-RSA-CHACHA20-POLY1305:"
466 "ECDHE-ECDSA-AES128-GCM-SHA256:"
467 "ECDHE-RSA-AES128-GCM-SHA256:"
468 "ECDHE-ECDSA-AES256-SHA384:"
469 "ECDHE-RSA-AES256-SHA384:"
470 "ECDHE-ECDSA-AES128-SHA256:"
471 "ECDHE-RSA-AES128-SHA256";
Ed Tanousc4771fb2017-03-13 13:39:49 -0700472
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600473 if (SSL_CTX_set_cipher_list(mSslContext->native_handle(),
Ed Tanous457207f2019-02-07 15:27:14 -0800474 mozillaModern.c_str()) != 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700475 {
476 BMCWEB_LOG_ERROR << "Error setting cipher list\n";
477 }
478 return mSslContext;
Ed Tanousc4771fb2017-03-13 13:39:49 -0700479}
AppaRao Pulie38778a2022-06-27 23:09:03 +0000480
481inline std::optional<boost::asio::ssl::context> getSSLClientContext()
482{
483 boost::asio::ssl::context sslCtx(boost::asio::ssl::context::tls_client);
484
485 boost::system::error_code ec;
486
487 // Support only TLS v1.2 & v1.3
488 sslCtx.set_options(boost::asio::ssl::context::default_workarounds |
489 boost::asio::ssl::context::no_sslv2 |
490 boost::asio::ssl::context::no_sslv3 |
491 boost::asio::ssl::context::single_dh_use |
492 boost::asio::ssl::context::no_tlsv1 |
493 boost::asio::ssl::context::no_tlsv1_1,
494 ec);
495 if (ec)
496 {
497 BMCWEB_LOG_ERROR << "SSL context set_options failed";
498 return std::nullopt;
499 }
500
501 // Add a directory containing certificate authority files to be used
502 // for performing verification.
503 sslCtx.set_default_verify_paths(ec);
504 if (ec)
505 {
506 BMCWEB_LOG_ERROR << "SSL context set_default_verify failed";
507 return std::nullopt;
508 }
509
510 // Verify the remote server's certificate
511 sslCtx.set_verify_mode(boost::asio::ssl::verify_peer, ec);
512 if (ec)
513 {
514 BMCWEB_LOG_ERROR << "SSL context set_verify_mode failed";
515 return std::nullopt;
516 }
517
518 // All cipher suites are set as per OWASP datasheet.
519 // https://cheatsheetseries.owasp.org/cheatsheets/TLS_Cipher_String_Cheat_Sheet.html
520 constexpr const char* sslCiphers = "ECDHE-ECDSA-AES128-GCM-SHA256:"
521 "ECDHE-RSA-AES128-GCM-SHA256:"
522 "ECDHE-ECDSA-AES256-GCM-SHA384:"
523 "ECDHE-RSA-AES256-GCM-SHA384:"
524 "ECDHE-ECDSA-CHACHA20-POLY1305:"
525 "ECDHE-RSA-CHACHA20-POLY1305:"
526 "DHE-RSA-AES128-GCM-SHA256:"
527 "DHE-RSA-AES256-GCM-SHA384"
528 "TLS_AES_128_GCM_SHA256:"
529 "TLS_AES_256_GCM_SHA384:"
530 "TLS_CHACHA20_POLY1305_SHA256";
531
532 if (SSL_CTX_set_cipher_list(sslCtx.native_handle(), sslCiphers) != 1)
533 {
534 BMCWEB_LOG_ERROR << "SSL_CTX_set_cipher_list failed";
535 return std::nullopt;
536 }
537
538 return sslCtx;
539}
540
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541} // namespace ensuressl