blob: 9a2e877b35ad66411b1315896cf4eba2b47bc21a [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>
Ed Tanous1abe55e2018-09-05 08:30:59 -070015#include <random>
16
17namespace ensuressl
18{
Ed Tanous55c7b7a2018-05-22 15:27:24 -070019static void initOpenssl();
Ed Tanousb01bf292019-03-25 19:25:26 +000020static void cleanupOpenssl();
21static EVP_PKEY *createRsaKey();
22static EVP_PKEY *createEcKey();
Ed Tanous55c7b7a2018-05-22 15:27:24 -070023static void handleOpensslError();
Ed Tanous0fdddb12017-02-28 11:06:34 -080024
Ed Tanous1abe55e2018-09-05 08:30:59 -070025inline bool verifyOpensslKeyCert(const std::string &filepath)
26{
27 bool privateKeyValid = false;
28 bool certValid = false;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080029
Ed Tanous1abe55e2018-09-05 08:30:59 -070030 std::cout << "Checking certs in file " << filepath << "\n";
Ed Tanous9b65f1f2017-03-07 15:17:13 -080031
Ed Tanous1abe55e2018-09-05 08:30:59 -070032 FILE *file = fopen(filepath.c_str(), "r");
33 if (file != NULL)
34 {
35 EVP_PKEY *pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL);
36 int rc;
37 if (pkey != nullptr)
38 {
39 RSA *rsa = EVP_PKEY_get1_RSA(pkey);
40 if (rsa != nullptr)
41 {
42 std::cout << "Found an RSA key\n";
43 if (RSA_check_key(rsa) == 1)
44 {
45 // private_key_valid = true;
46 }
47 else
48 {
49 std::cerr << "Key not valid error number "
50 << ERR_get_error() << "\n";
51 }
52 RSA_free(rsa);
53 }
54 else
55 {
56 EC_KEY *ec = EVP_PKEY_get1_EC_KEY(pkey);
57 if (ec != nullptr)
58 {
59 std::cout << "Found an EC key\n";
60 if (EC_KEY_check_key(ec) == 1)
61 {
62 privateKeyValid = true;
63 }
64 else
65 {
66 std::cerr << "Key not valid error number "
67 << ERR_get_error() << "\n";
68 }
69 EC_KEY_free(ec);
70 }
71 }
72
73 if (privateKeyValid)
74 {
75 X509 *x509 = PEM_read_X509(file, NULL, NULL, NULL);
76 if (x509 == nullptr)
77 {
78 std::cout << "error getting x509 cert " << ERR_get_error()
79 << "\n";
80 }
81 else
82 {
83 rc = X509_verify(x509, pkey);
84 if (rc == 1)
85 {
86 certValid = true;
87 }
88 else
89 {
90 std::cerr << "Error in verifying private key signature "
91 << ERR_get_error() << "\n";
92 }
93 }
94 }
95
96 EVP_PKEY_free(pkey);
Ed Tanous9b65f1f2017-03-07 15:17:13 -080097 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070098 fclose(file);
Ed Tanous0fdddb12017-02-28 11:06:34 -080099 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 return certValid;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800101}
102
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103inline void generateSslCertificate(const std::string &filepath)
104{
105 FILE *pFile = NULL;
106 std::cout << "Generating new keys\n";
107 initOpenssl();
Ed Tanous0fdddb12017-02-28 11:06:34 -0800108
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 // std::cerr << "Generating RSA key";
110 // EVP_PKEY *pRsaPrivKey = create_rsa_key();
Ed Tanous0fdddb12017-02-28 11:06:34 -0800111
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 std::cerr << "Generating EC key\n";
Ed Tanousb01bf292019-03-25 19:25:26 +0000113 EVP_PKEY *pRsaPrivKey = createEcKey();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 if (pRsaPrivKey != nullptr)
115 {
116 std::cerr << "Generating x509 Certificate\n";
117 // Use this code to directly generate a certificate
118 X509 *x509;
119 x509 = X509_new();
120 if (x509 != nullptr)
121 {
122 // get a random number from the RNG for the certificate serial
123 // number If this is not random, regenerating certs throws broswer
124 // errors
125 std::random_device rd;
126 int serial = rd();
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800127
Ed Tanous1abe55e2018-09-05 08:30:59 -0700128 ASN1_INTEGER_set(X509_get_serialNumber(x509), serial);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800129
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130 // not before this moment
131 X509_gmtime_adj(X509_get_notBefore(x509), 0);
132 // Cert is valid for 10 years
133 X509_gmtime_adj(X509_get_notAfter(x509),
134 60L * 60L * 24L * 365L * 10L);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800135
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 // set the public key to the key we just generated
137 X509_set_pubkey(x509, pRsaPrivKey);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800138
Ed Tanous1abe55e2018-09-05 08:30:59 -0700139 // get the subject name
140 X509_NAME *name;
141 name = X509_get_subject_name(x509);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800142
Ed Tanous1abe55e2018-09-05 08:30:59 -0700143 X509_NAME_add_entry_by_txt(
144 name, "C", MBSTRING_ASC,
145 reinterpret_cast<const unsigned char *>("US"), -1, -1, 0);
146 X509_NAME_add_entry_by_txt(
147 name, "O", MBSTRING_ASC,
Brad Bishop85d2bb52019-04-05 08:31:14 -0400148 reinterpret_cast<const unsigned char *>("OpenBMC"), -1, -1, 0);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700149 X509_NAME_add_entry_by_txt(
150 name, "CN", MBSTRING_ASC,
151 reinterpret_cast<const unsigned char *>("testhost"), -1, -1, 0);
152 // set the CSR options
153 X509_set_issuer_name(x509, name);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800154
Ed Tanous1abe55e2018-09-05 08:30:59 -0700155 // Sign the certificate with our private key
156 X509_sign(x509, pRsaPrivKey, EVP_sha256());
Ed Tanous0fdddb12017-02-28 11:06:34 -0800157
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158 pFile = fopen(filepath.c_str(), "wt");
Ed Tanous0fdddb12017-02-28 11:06:34 -0800159
Ed Tanous1abe55e2018-09-05 08:30:59 -0700160 if (pFile != nullptr)
161 {
162 PEM_write_PrivateKey(pFile, pRsaPrivKey, NULL, NULL, 0, 0,
163 NULL);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800164
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165 PEM_write_X509(pFile, x509);
166 fclose(pFile);
167 pFile = NULL;
168 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800169
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170 X509_free(x509);
171 }
172
173 EVP_PKEY_free(pRsaPrivKey);
174 pRsaPrivKey = NULL;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800175 }
176
Ed Tanous1abe55e2018-09-05 08:30:59 -0700177 // cleanup_openssl();
Ed Tanous99923322017-03-03 14:21:24 -0800178}
Ed Tanousb01bf292019-03-25 19:25:26 +0000179
180EVP_PKEY *createRsaKey()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181{
182 RSA *pRSA = NULL;
Ed Tanousa1e077c2017-04-25 12:42:19 -0700183#if OPENSSL_VERSION_NUMBER < 0x00908000L
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184 pRSA = RSA_generate_key(2048, RSA_3, NULL, NULL);
Ed Tanousa1e077c2017-04-25 12:42:19 -0700185#else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700186 RSA_generate_key_ex(pRSA, 2048, NULL, NULL);
Ed Tanousa1e077c2017-04-25 12:42:19 -0700187#endif
188
Ed Tanousb01bf292019-03-25 19:25:26 +0000189 EVP_PKEY *pKey = EVP_PKEY_new();
190 if ((pRSA != nullptr) && (pKey != nullptr) &&
191 EVP_PKEY_assign_RSA(pKey, pRSA))
192 {
193 /* pKey owns pRSA from now */
194 if (RSA_check_key(pRSA) <= 0)
195 {
196 fprintf(stderr, "RSA_check_key failed.\n");
197 handleOpensslError();
198 EVP_PKEY_free(pKey);
199 pKey = NULL;
200 }
201 }
202 else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700203 {
204 handleOpensslError();
205 if (pRSA != nullptr)
206 {
207 RSA_free(pRSA);
Ed Tanousb01bf292019-03-25 19:25:26 +0000208 pRSA = NULL;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700209 }
210 if (pKey != nullptr)
211 {
212 EVP_PKEY_free(pKey);
Ed Tanousb01bf292019-03-25 19:25:26 +0000213 pKey = NULL;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700214 }
215 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000216 return pKey;
217}
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800218
Ed Tanousb01bf292019-03-25 19:25:26 +0000219EVP_PKEY *createEcKey()
220{
221 EVP_PKEY *pKey = NULL;
222 int eccgrp = 0;
223 eccgrp = OBJ_txt2nid("prime256v1");
Ed Tanous6ea007a2019-02-13 22:48:25 -0800224
Ed Tanous6ea007a2019-02-13 22:48:25 -0800225 EC_KEY *myecc = EC_KEY_new_by_curve_name(eccgrp);
Ed Tanousb01bf292019-03-25 19:25:26 +0000226 if (myecc != nullptr)
Ed Tanous6ea007a2019-02-13 22:48:25 -0800227 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000228 EC_KEY_set_asn1_flag(myecc, OPENSSL_EC_NAMED_CURVE);
229 EC_KEY_generate_key(myecc);
230 pKey = EVP_PKEY_new();
231 if (pKey != nullptr)
232 {
233 if (EVP_PKEY_assign_EC_KEY(pKey, myecc))
234 {
235 /* pKey owns pRSA from now */
236 if (EC_KEY_check_key(myecc) <= 0)
237 {
238 fprintf(stderr, "EC_check_key failed.\n");
239 }
240 }
241 }
Ed Tanous6ea007a2019-02-13 22:48:25 -0800242 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700243 return pKey;
244}
245
246void initOpenssl()
247{
Ed Tanous911ac312017-08-15 09:37:42 -0700248#if OPENSSL_VERSION_NUMBER < 0x10100000L
Ed Tanous1abe55e2018-09-05 08:30:59 -0700249 SSL_load_error_strings();
250 OpenSSL_add_all_algorithms();
251 RAND_load_file("/dev/urandom", 1024);
Ed Tanousa1e077c2017-04-25 12:42:19 -0700252#endif
Ed Tanous0fdddb12017-02-28 11:06:34 -0800253}
254
Ed Tanousb01bf292019-03-25 19:25:26 +0000255void cleanupOpenssl()
256{
257 CRYPTO_cleanup_all_ex_data();
258 ERR_free_strings();
259#if OPENSSL_VERSION_NUMBER < 0x10100000L
260 ERR_remove_thread_state(0);
261#endif
262 EVP_cleanup();
263}
264
Ed Tanous1abe55e2018-09-05 08:30:59 -0700265void handleOpensslError()
266{
267 ERR_print_errors_fp(stderr);
268}
269inline void ensureOpensslKeyPresentAndValid(const std::string &filepath)
270{
271 bool pemFileValid = false;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800272
Ed Tanous1abe55e2018-09-05 08:30:59 -0700273 pemFileValid = verifyOpensslKeyCert(filepath);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700274
Ed Tanous1abe55e2018-09-05 08:30:59 -0700275 if (!pemFileValid)
276 {
277 std::cerr << "Error in verifying signature, regenerating\n";
278 generateSslCertificate(filepath);
279 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800280}
Ed Tanousc4771fb2017-03-13 13:39:49 -0700281
Ed Tanous1abe55e2018-09-05 08:30:59 -0700282inline boost::asio::ssl::context getSslContext(const std::string &ssl_pem_file)
283{
Ed Tanous457207f2019-02-07 15:27:14 -0800284 boost::asio::ssl::context mSslContext{
285 boost::asio::ssl::context::tls_server};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700286 mSslContext.set_options(boost::asio::ssl::context::default_workarounds |
287 boost::asio::ssl::context::no_sslv2 |
288 boost::asio::ssl::context::no_sslv3 |
289 boost::asio::ssl::context::single_dh_use |
290 boost::asio::ssl::context::no_tlsv1 |
291 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700292
Ed Tanous1abe55e2018-09-05 08:30:59 -0700293 // m_ssl_context.set_verify_mode(boost::asio::ssl::verify_peer);
294 mSslContext.use_certificate_file(ssl_pem_file,
295 boost::asio::ssl::context::pem);
296 mSslContext.use_private_key_file(ssl_pem_file,
297 boost::asio::ssl::context::pem);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700298
Ed Tanous1abe55e2018-09-05 08:30:59 -0700299 // Set up EC curves to auto (boost asio doesn't have a method for this)
300 // There is a pull request to add this. Once this is included in an asio
301 // drop, use the right way
302 // http://stackoverflow.com/questions/18929049/boost-asio-with-ecdsa-certificate-issue
303 if (SSL_CTX_set_ecdh_auto(mSslContext.native_handle(), 1) != 1)
304 {
305 BMCWEB_LOG_ERROR << "Error setting tmp ecdh list\n";
306 }
Ed Tanousc4771fb2017-03-13 13:39:49 -0700307
Ed Tanous457207f2019-02-07 15:27:14 -0800308 std::string mozillaModern = "ECDHE-ECDSA-AES256-GCM-SHA384:"
309 "ECDHE-RSA-AES256-GCM-SHA384:"
310 "ECDHE-ECDSA-CHACHA20-POLY1305:"
311 "ECDHE-RSA-CHACHA20-POLY1305:"
312 "ECDHE-ECDSA-AES128-GCM-SHA256:"
313 "ECDHE-RSA-AES128-GCM-SHA256:"
314 "ECDHE-ECDSA-AES256-SHA384:"
315 "ECDHE-RSA-AES256-SHA384:"
316 "ECDHE-ECDSA-AES128-SHA256:"
317 "ECDHE-RSA-AES128-SHA256";
Ed Tanousc4771fb2017-03-13 13:39:49 -0700318
Ed Tanous1abe55e2018-09-05 08:30:59 -0700319 if (SSL_CTX_set_cipher_list(mSslContext.native_handle(),
Ed Tanous457207f2019-02-07 15:27:14 -0800320 mozillaModern.c_str()) != 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700321 {
322 BMCWEB_LOG_ERROR << "Error setting cipher list\n";
323 }
324 return mSslContext;
Ed Tanousc4771fb2017-03-13 13:39:49 -0700325}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700326} // namespace ensuressl
Ed Tanous01250f22017-04-18 17:49:51 -0700327
Brad Bishop85d2bb52019-04-05 08:31:14 -0400328#endif