blob: 71781956ad7f9220a725624a0d08b18b05f33440 [file] [log] [blame]
Ed Tanous0fdddb12017-02-28 11:06:34 -08001#pragma once
Ed Tanous55c7b7a2018-05-22 15:27:24 -07002#ifdef BMCWEB_ENABLE_SSL
Ed Tanous0fdddb12017-02-28 11:06:34 -08003
4#include <openssl/bio.h>
5#include <openssl/dh.h>
6#include <openssl/dsa.h>
Ed Tanous0fdddb12017-02-28 11:06:34 -08007#include <openssl/err.h>
8#include <openssl/evp.h>
9#include <openssl/pem.h>
10#include <openssl/rand.h>
11#include <openssl/rsa.h>
12#include <openssl/ssl.h>
Ed Tanous9b65f1f2017-03-07 15:17:13 -080013
Ed Tanous3112a142018-11-29 15:45:10 -080014#include <boost/asio/ssl/context.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050015
Ed Tanous1abe55e2018-09-05 08:30:59 -070016#include <random>
17
18namespace ensuressl
19{
Gunnar Mills1214b7e2020-06-04 10:11:30 -050020constexpr char const* trustStorePath = "/etc/ssl/certs/authority";
Ed Tanous55c7b7a2018-05-22 15:27:24 -070021static void initOpenssl();
Gunnar Mills1214b7e2020-06-04 10:11:30 -050022static EVP_PKEY* createEcKey();
Ed Tanous0fdddb12017-02-28 11:06:34 -080023
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050024// Trust chain related errors.`
25inline bool isTrustChainError(int errnum)
26{
27 if ((errnum == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) ||
28 (errnum == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) ||
29 (errnum == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) ||
30 (errnum == X509_V_ERR_CERT_UNTRUSTED) ||
31 (errnum == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE))
32 {
33 return true;
34 }
35 else
36 {
37 return false;
38 }
39}
40
Gunnar Mills1214b7e2020-06-04 10:11:30 -050041inline bool validateCertificate(X509* const cert)
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050042{
43 // Create an empty X509_STORE structure for certificate validation.
Gunnar Mills1214b7e2020-06-04 10:11:30 -050044 X509_STORE* x509Store = X509_STORE_new();
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050045 if (!x509Store)
46 {
47 BMCWEB_LOG_ERROR << "Error occured during X509_STORE_new call";
48 return false;
49 }
50
51 // Load Certificate file into the X509 structure.
Gunnar Mills1214b7e2020-06-04 10:11:30 -050052 X509_STORE_CTX* storeCtx = X509_STORE_CTX_new();
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050053 if (!storeCtx)
54 {
55 BMCWEB_LOG_ERROR << "Error occured during X509_STORE_CTX_new call";
56 X509_STORE_free(x509Store);
57 return false;
58 }
59
Ed Tanous99131cd2019-10-24 11:12:47 -070060 int errCode = X509_STORE_CTX_init(storeCtx, x509Store, cert, nullptr);
Ramesh Iyyar082f28f2019-06-22 03:31:55 -050061 if (errCode != 1)
62 {
63 BMCWEB_LOG_ERROR << "Error occured during X509_STORE_CTX_init call";
64 X509_STORE_CTX_free(storeCtx);
65 X509_STORE_free(x509Store);
66 return false;
67 }
68
69 errCode = X509_verify_cert(storeCtx);
70 if (errCode == 1)
71 {
72 BMCWEB_LOG_INFO << "Certificate verification is success";
73 X509_STORE_CTX_free(storeCtx);
74 X509_STORE_free(x509Store);
75 return true;
76 }
77 if (errCode == 0)
78 {
79 errCode = X509_STORE_CTX_get_error(storeCtx);
80 X509_STORE_CTX_free(storeCtx);
81 X509_STORE_free(x509Store);
82 if (isTrustChainError(errCode))
83 {
84 BMCWEB_LOG_DEBUG << "Ignoring Trust Chain error. Reason: "
85 << X509_verify_cert_error_string(errCode);
86 return true;
87 }
88 else
89 {
90 BMCWEB_LOG_ERROR << "Certificate verification failed. Reason: "
91 << X509_verify_cert_error_string(errCode);
92 return false;
93 }
94 }
95
96 BMCWEB_LOG_ERROR
97 << "Error occured during X509_verify_cert call. ErrorCode: " << errCode;
98 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 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500116 RSA* rsa = EVP_PKEY_get1_RSA(pkey);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 if (rsa != nullptr)
118 {
119 std::cout << "Found an RSA key\n";
120 if (RSA_check_key(rsa) == 1)
121 {
Jayanth Othayoth4d2849a2019-05-13 01:46:34 -0500122 privateKeyValid = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123 }
124 else
125 {
126 std::cerr << "Key not valid error number "
127 << ERR_get_error() << "\n";
128 }
129 RSA_free(rsa);
130 }
131 else
132 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500133 EC_KEY* ec = EVP_PKEY_get1_EC_KEY(pkey);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700134 if (ec != nullptr)
135 {
136 std::cout << "Found an EC key\n";
137 if (EC_KEY_check_key(ec) == 1)
138 {
139 privateKeyValid = true;
140 }
141 else
142 {
143 std::cerr << "Key not valid error number "
144 << ERR_get_error() << "\n";
145 }
146 EC_KEY_free(ec);
147 }
148 }
149
150 if (privateKeyValid)
151 {
Ramesh Iyyarc0bf8932019-06-22 00:23:29 -0500152 // If the order is certificate followed by key in input file
153 // then, certificate read will fail. So, setting the file
154 // pointer to point beginning of file to avoid certificate and
155 // key order issue.
156 fseek(file, 0, SEEK_SET);
157
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500158 X509* x509 = PEM_read_X509(file, nullptr, nullptr, nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700159 if (x509 == nullptr)
160 {
161 std::cout << "error getting x509 cert " << ERR_get_error()
162 << "\n";
163 }
164 else
165 {
Ramesh Iyyar082f28f2019-06-22 03:31:55 -0500166 certValid = validateCertificate(x509);
167 X509_free(x509);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700168 }
169 }
170
171 EVP_PKEY_free(pkey);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800172 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700173 fclose(file);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800174 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 return certValid;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800176}
177
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500178inline void generateSslCertificate(const std::string& filepath)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700179{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500180 FILE* pFile = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181 std::cout << "Generating new keys\n";
182 initOpenssl();
Ed Tanous0fdddb12017-02-28 11:06:34 -0800183
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184 std::cerr << "Generating EC key\n";
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500185 EVP_PKEY* pPrivKey = createEcKey();
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700186 if (pPrivKey != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700187 {
188 std::cerr << "Generating x509 Certificate\n";
189 // Use this code to directly generate a certificate
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500190 X509* x509;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700191 x509 = X509_new();
192 if (x509 != nullptr)
193 {
194 // get a random number from the RNG for the certificate serial
195 // number If this is not random, regenerating certs throws broswer
196 // errors
197 std::random_device rd;
Ed Tanous271584a2019-07-09 16:24:22 -0700198 int serial = static_cast<int>(rd());
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800199
Ed Tanous1abe55e2018-09-05 08:30:59 -0700200 ASN1_INTEGER_set(X509_get_serialNumber(x509), serial);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800201
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202 // not before this moment
203 X509_gmtime_adj(X509_get_notBefore(x509), 0);
204 // Cert is valid for 10 years
205 X509_gmtime_adj(X509_get_notAfter(x509),
206 60L * 60L * 24L * 365L * 10L);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800207
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208 // set the public key to the key we just generated
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700209 X509_set_pubkey(x509, pPrivKey);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800210
Ed Tanous1abe55e2018-09-05 08:30:59 -0700211 // get the subject name
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500212 X509_NAME* name;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 name = X509_get_subject_name(x509);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800214
Ed Tanous1abe55e2018-09-05 08:30:59 -0700215 X509_NAME_add_entry_by_txt(
216 name, "C", MBSTRING_ASC,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500217 reinterpret_cast<const unsigned char*>("US"), -1, -1, 0);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 X509_NAME_add_entry_by_txt(
219 name, "O", MBSTRING_ASC,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500220 reinterpret_cast<const unsigned char*>("OpenBMC"), -1, -1, 0);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700221 X509_NAME_add_entry_by_txt(
222 name, "CN", MBSTRING_ASC,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500223 reinterpret_cast<const unsigned char*>("testhost"), -1, -1, 0);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700224 // set the CSR options
225 X509_set_issuer_name(x509, name);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800226
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 // Sign the certificate with our private key
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700228 X509_sign(x509, pPrivKey, EVP_sha256());
Ed Tanous0fdddb12017-02-28 11:06:34 -0800229
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 pFile = fopen(filepath.c_str(), "wt");
Ed Tanous0fdddb12017-02-28 11:06:34 -0800231
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 if (pFile != nullptr)
233 {
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700234 PEM_write_PrivateKey(pFile, pPrivKey, nullptr, nullptr, 0,
Ed Tanous99131cd2019-10-24 11:12:47 -0700235 nullptr, nullptr);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800236
Ed Tanous1abe55e2018-09-05 08:30:59 -0700237 PEM_write_X509(pFile, x509);
238 fclose(pFile);
Ed Tanous99131cd2019-10-24 11:12:47 -0700239 pFile = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800241
Ed Tanous1abe55e2018-09-05 08:30:59 -0700242 X509_free(x509);
243 }
244
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700245 EVP_PKEY_free(pPrivKey);
246 pPrivKey = nullptr;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800247 }
248
Ed Tanous1abe55e2018-09-05 08:30:59 -0700249 // cleanup_openssl();
Ed Tanous99923322017-03-03 14:21:24 -0800250}
Ed Tanousb01bf292019-03-25 19:25:26 +0000251
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500252EVP_PKEY* createEcKey()
Ed Tanousb01bf292019-03-25 19:25:26 +0000253{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500254 EVP_PKEY* pKey = nullptr;
Ed Tanousb01bf292019-03-25 19:25:26 +0000255 int eccgrp = 0;
Vernon Maueryaaf32062020-03-09 10:41:31 -0700256 eccgrp = OBJ_txt2nid("secp384r1");
Ed Tanous6ea007a2019-02-13 22:48:25 -0800257
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500258 EC_KEY* myecc = EC_KEY_new_by_curve_name(eccgrp);
Ed Tanousb01bf292019-03-25 19:25:26 +0000259 if (myecc != nullptr)
Ed Tanous6ea007a2019-02-13 22:48:25 -0800260 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000261 EC_KEY_set_asn1_flag(myecc, OPENSSL_EC_NAMED_CURVE);
262 EC_KEY_generate_key(myecc);
263 pKey = EVP_PKEY_new();
264 if (pKey != nullptr)
265 {
266 if (EVP_PKEY_assign_EC_KEY(pKey, myecc))
267 {
Vernon Mauery0185c7f2020-03-09 10:56:53 -0700268 /* pKey owns myecc from now */
Ed Tanousb01bf292019-03-25 19:25:26 +0000269 if (EC_KEY_check_key(myecc) <= 0)
270 {
271 fprintf(stderr, "EC_check_key failed.\n");
272 }
273 }
274 }
Ed Tanous6ea007a2019-02-13 22:48:25 -0800275 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700276 return pKey;
277}
278
279void initOpenssl()
280{
Ed Tanous911ac312017-08-15 09:37:42 -0700281#if OPENSSL_VERSION_NUMBER < 0x10100000L
Ed Tanous1abe55e2018-09-05 08:30:59 -0700282 SSL_load_error_strings();
283 OpenSSL_add_all_algorithms();
284 RAND_load_file("/dev/urandom", 1024);
Ed Tanousa1e077c2017-04-25 12:42:19 -0700285#endif
Ed Tanous0fdddb12017-02-28 11:06:34 -0800286}
287
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500288inline void ensureOpensslKeyPresentAndValid(const std::string& filepath)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700289{
290 bool pemFileValid = false;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800291
Ed Tanous1abe55e2018-09-05 08:30:59 -0700292 pemFileValid = verifyOpensslKeyCert(filepath);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700293
Ed Tanous1abe55e2018-09-05 08:30:59 -0700294 if (!pemFileValid)
295 {
296 std::cerr << "Error in verifying signature, regenerating\n";
297 generateSslCertificate(filepath);
298 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800299}
Ed Tanousc4771fb2017-03-13 13:39:49 -0700300
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600301inline std::shared_ptr<boost::asio::ssl::context>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500302 getSslContext(const std::string& ssl_pem_file)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700303{
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600304 std::shared_ptr<boost::asio::ssl::context> mSslContext =
305 std::make_shared<boost::asio::ssl::context>(
306 boost::asio::ssl::context::tls_server);
307 mSslContext->set_options(boost::asio::ssl::context::default_workarounds |
308 boost::asio::ssl::context::no_sslv2 |
309 boost::asio::ssl::context::no_sslv3 |
310 boost::asio::ssl::context::single_dh_use |
311 boost::asio::ssl::context::no_tlsv1 |
312 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700313
James Feist91243c32019-11-12 14:55:40 -0800314 // BIG WARNING: This needs to stay disabled, as there will always be
315 // unauthenticated endpoints
316 // mSslContext->set_verify_mode(boost::asio::ssl::verify_peer);
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200317
James Feist6a3e1822019-11-06 13:46:35 -0800318 SSL_CTX_set_options(mSslContext->native_handle(), SSL_OP_NO_RENEGOTIATION);
319
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200320 BMCWEB_LOG_DEBUG << "Using default TrustStore location: " << trustStorePath;
321 mSslContext->add_verify_path(trustStorePath);
322
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600323 mSslContext->use_certificate_file(ssl_pem_file,
324 boost::asio::ssl::context::pem);
325 mSslContext->use_private_key_file(ssl_pem_file,
326 boost::asio::ssl::context::pem);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700327
Ed Tanous1abe55e2018-09-05 08:30:59 -0700328 // Set up EC curves to auto (boost asio doesn't have a method for this)
329 // There is a pull request to add this. Once this is included in an asio
330 // drop, use the right way
331 // http://stackoverflow.com/questions/18929049/boost-asio-with-ecdsa-certificate-issue
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600332 if (SSL_CTX_set_ecdh_auto(mSslContext->native_handle(), 1) != 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700333 {
334 BMCWEB_LOG_ERROR << "Error setting tmp ecdh list\n";
335 }
Ed Tanousc4771fb2017-03-13 13:39:49 -0700336
Ed Tanous457207f2019-02-07 15:27:14 -0800337 std::string mozillaModern = "ECDHE-ECDSA-AES256-GCM-SHA384:"
338 "ECDHE-RSA-AES256-GCM-SHA384:"
339 "ECDHE-ECDSA-CHACHA20-POLY1305:"
340 "ECDHE-RSA-CHACHA20-POLY1305:"
341 "ECDHE-ECDSA-AES128-GCM-SHA256:"
342 "ECDHE-RSA-AES128-GCM-SHA256:"
343 "ECDHE-ECDSA-AES256-SHA384:"
344 "ECDHE-RSA-AES256-SHA384:"
345 "ECDHE-ECDSA-AES128-SHA256:"
346 "ECDHE-RSA-AES128-SHA256";
Ed Tanousc4771fb2017-03-13 13:39:49 -0700347
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600348 if (SSL_CTX_set_cipher_list(mSslContext->native_handle(),
Ed Tanous457207f2019-02-07 15:27:14 -0800349 mozillaModern.c_str()) != 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700350 {
351 BMCWEB_LOG_ERROR << "Error setting cipher list\n";
352 }
353 return mSslContext;
Ed Tanousc4771fb2017-03-13 13:39:49 -0700354}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700355} // namespace ensuressl
Ed Tanous01250f22017-04-18 17:49:51 -0700356
Brad Bishop85d2bb52019-04-05 08:31:14 -0400357#endif