blob: aade3fb5dd0622057a99abfda24760586ae1b009 [file] [log] [blame]
Ed Tanous0fdddb12017-02-28 11:06:34 -08001#pragma once
Ed Tanous01250f22017-04-18 17:49:51 -07002#ifdef CROW_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>
7#include <openssl/dsa.h>
8#include <openssl/err.h>
9#include <openssl/evp.h>
10#include <openssl/pem.h>
11#include <openssl/rand.h>
12#include <openssl/rsa.h>
13#include <openssl/ssl.h>
Ed Tanous9b65f1f2017-03-07 15:17:13 -080014#include <g3log/g3log.hpp>
Ed Tanous1ff48782017-04-18 12:45:08 -070015#include <random>
Ed Tanousa1e077c2017-04-25 12:42:19 -070016#include <boost/asio.hpp>
Ed Tanous9b65f1f2017-03-07 15:17:13 -080017
Ed Tanous99923322017-03-03 14:21:24 -080018namespace ensuressl {
Ed Tanous0fdddb12017-02-28 11:06:34 -080019static void init_openssl(void);
20static void cleanup_openssl(void);
21static EVP_PKEY *create_rsa_key(void);
Ed Tanous9b65f1f2017-03-07 15:17:13 -080022static EVP_PKEY *create_ec_key(void);
Ed Tanous0fdddb12017-02-28 11:06:34 -080023static void handle_openssl_error(void);
24
Ed Tanous99923322017-03-03 14:21:24 -080025inline bool verify_openssl_key_cert(const std::string &filepath) {
26 bool private_key_valid = false;
27 bool cert_valid = false;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080028
29 LOG(DEBUG) << "Checking certs in file " << filepath;
30
Ed Tanous99923322017-03-03 14:21:24 -080031 FILE *file = fopen(filepath.c_str(), "r");
32 if (file != NULL) {
33 EVP_PKEY *pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL);
34 int rc;
35 if (pkey) {
Ed Tanousa1e077c2017-04-25 12:42:19 -070036 RSA *rsa = EVP_PKEY_get1_RSA(pkey);
37 if (rsa) {
38 LOG(DEBUG) << "Found an RSA key";
39 if (RSA_check_key(rsa) == 1) {
40 // private_key_valid = true;
41 } else {
42 LOG(WARNING) << "Key not valid error number " << ERR_get_error();
Ed Tanous9b65f1f2017-03-07 15:17:13 -080043 }
Ed Tanousa1e077c2017-04-25 12:42:19 -070044 RSA_free(rsa);
45 } else {
46 EC_KEY *ec = EVP_PKEY_get1_EC_KEY(pkey);
47 if (ec) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080048 LOG(DEBUG) << "Found an EC key";
Ed Tanousa1e077c2017-04-25 12:42:19 -070049 if (EC_KEY_check_key(ec) == 1) {
50 private_key_valid = true;
51 } else {
52 LOG(WARNING) << "Key not valid error number " << ERR_get_error();
Ed Tanous9b65f1f2017-03-07 15:17:13 -080053 }
Ed Tanousa1e077c2017-04-25 12:42:19 -070054 EC_KEY_free(ec);
Ed Tanous0fdddb12017-02-28 11:06:34 -080055 }
Ed Tanous99923322017-03-03 14:21:24 -080056 }
57
58 if (private_key_valid) {
59 X509 *x509 = PEM_read_X509(file, NULL, NULL, NULL);
Ed Tanous1ccd57c2017-03-21 13:15:58 -070060 if (!x509) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080061 LOG(DEBUG) << "error getting x509 cert " << ERR_get_error();
62 } else {
63 rc = X509_verify(x509, pkey);
64 if (rc == 1) {
65 cert_valid = true;
66 } else {
Ed Tanous1ccd57c2017-03-21 13:15:58 -070067 LOG(WARNING) << "Error in verifying private key signature "
68 << ERR_get_error();
Ed Tanous9b65f1f2017-03-07 15:17:13 -080069 }
Ed Tanous99923322017-03-03 14:21:24 -080070 }
71 }
72
73 EVP_PKEY_free(pkey);
Ed Tanous0fdddb12017-02-28 11:06:34 -080074 }
Ed Tanous99923322017-03-03 14:21:24 -080075 fclose(file);
76 }
77 return cert_valid;
Ed Tanous0fdddb12017-02-28 11:06:34 -080078}
79
Ed Tanous99923322017-03-03 14:21:24 -080080inline void generate_ssl_certificate(const std::string &filepath) {
Ed Tanous99923322017-03-03 14:21:24 -080081 FILE *pFile = NULL;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080082 LOG(WARNING) << "Generating new keys";
Ed Tanous99923322017-03-03 14:21:24 -080083 init_openssl();
Ed Tanous0fdddb12017-02-28 11:06:34 -080084
Ed Tanous1ccd57c2017-03-21 13:15:58 -070085 // LOG(WARNING) << "Generating RSA key";
86 // EVP_PKEY *pRsaPrivKey = create_rsa_key();
Ed Tanous0fdddb12017-02-28 11:06:34 -080087
Ed Tanousc4771fb2017-03-13 13:39:49 -070088 LOG(WARNING) << "Generating EC key";
89 EVP_PKEY *pRsaPrivKey = create_ec_key();
Ed Tanous1ccd57c2017-03-21 13:15:58 -070090 if (pRsaPrivKey) {
91 LOG(WARNING) << "Generating x509 Certificate";
92 // Use this code to directly generate a certificate
93 X509 *x509;
94 x509 = X509_new();
95 if (x509) {
96 // Get a random number from the RNG for the certificate serial number
97 // If this is not random, regenerating certs throws broswer errors
98 std::random_device rd;
99 int serial = rd();
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800100
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700101 ASN1_INTEGER_set(X509_get_serialNumber(x509), serial);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800102
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700103 // not before this moment
104 X509_gmtime_adj(X509_get_notBefore(x509), 0);
105 // Cert is valid for 10 years
106 X509_gmtime_adj(X509_get_notAfter(x509), 60L * 60L * 24L * 365L * 10L);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800107
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700108 // set the public key to the key we just generated
109 X509_set_pubkey(x509, pRsaPrivKey);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800110
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700111 // Get the subject name
112 X509_NAME *name;
113 name = X509_get_subject_name(x509);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800114
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700115 X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char *)"US",
Ed Tanous1ff48782017-04-18 12:45:08 -0700116 -1, -1, 0);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700117 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC,
Ed Tanous1ff48782017-04-18 12:45:08 -0700118 (unsigned char *)"Intel BMC", -1, -1, 0);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700119 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
Ed Tanous1ff48782017-04-18 12:45:08 -0700120 (unsigned char *)"testhost", -1, -1, 0);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700121 // set the CSR options
122 X509_set_issuer_name(x509, name);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800123
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700124 // Sign the certificate with our private key
125 X509_sign(x509, pRsaPrivKey, EVP_sha256());
Ed Tanous0fdddb12017-02-28 11:06:34 -0800126
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700127 pFile = fopen(filepath.c_str(), "wt");
Ed Tanous0fdddb12017-02-28 11:06:34 -0800128
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700129 if (pFile) {
130 PEM_write_PrivateKey(pFile, pRsaPrivKey, NULL, NULL, 0, 0, NULL);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800131
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700132 PEM_write_X509(pFile, x509);
133 fclose(pFile);
134 pFile = NULL;
135 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800136
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700137 X509_free(x509);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800138 }
139
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800140 EVP_PKEY_free(pRsaPrivKey);
141 pRsaPrivKey = NULL;
Ed Tanous99923322017-03-03 14:21:24 -0800142 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700143
Ed Tanous99923322017-03-03 14:21:24 -0800144 // cleanup_openssl();
145}
146
147EVP_PKEY *create_rsa_key(void) {
148 RSA *pRSA = NULL;
Ed Tanousa1e077c2017-04-25 12:42:19 -0700149#if OPENSSL_VERSION_NUMBER < 0x00908000L
Ed Tanous99923322017-03-03 14:21:24 -0800150 pRSA = RSA_generate_key(2048, RSA_3, NULL, NULL);
Ed Tanousa1e077c2017-04-25 12:42:19 -0700151#else
152 RSA_generate_key_ex(pRSA, 2048, NULL, NULL);
153#endif
154
155 EVP_PKEY *pKey = EVP_PKEY_new();
Ed Tanous99923322017-03-03 14:21:24 -0800156 if (pRSA && pKey && EVP_PKEY_assign_RSA(pKey, pRSA)) {
157 /* pKey owns pRSA from now */
158 if (RSA_check_key(pRSA) <= 0) {
159 fprintf(stderr, "RSA_check_key failed.\n");
160 handle_openssl_error();
161 EVP_PKEY_free(pKey);
162 pKey = NULL;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800163 }
Ed Tanous99923322017-03-03 14:21:24 -0800164 } else {
165 handle_openssl_error();
166 if (pRSA) {
167 RSA_free(pRSA);
168 pRSA = NULL;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800169 }
Ed Tanous99923322017-03-03 14:21:24 -0800170 if (pKey) {
171 EVP_PKEY_free(pKey);
172 pKey = NULL;
173 }
174 }
175 return pKey;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800176}
177
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800178EVP_PKEY *create_ec_key(void) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800179 EVP_PKEY *pKey = NULL;
180 int eccgrp = 0;
181 eccgrp = OBJ_txt2nid("prime256v1");
182
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700183 EC_KEY *myecc = EC_KEY_new_by_curve_name(eccgrp);
184 if (myecc) {
185 EC_KEY_set_asn1_flag(myecc, OPENSSL_EC_NAMED_CURVE);
186 EC_KEY_generate_key(myecc);
Ed Tanous01250f22017-04-18 17:49:51 -0700187 pKey = EVP_PKEY_new();
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800188 if (pKey) {
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700189 if (EVP_PKEY_assign_EC_KEY(pKey, myecc)) {
190 /* pKey owns pRSA from now */
191 if (EC_KEY_check_key(myecc) <= 0) {
192 fprintf(stderr, "EC_check_key failed.\n");
193 }
194 }
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800195 }
196 }
197 return pKey;
198}
199
Ed Tanous99923322017-03-03 14:21:24 -0800200void init_openssl(void) {
Ed Tanousa1e077c2017-04-25 12:42:19 -0700201 #if OPENSSL_VERSION_NUMBER < 0x10100000L
Ed Tanous99923322017-03-03 14:21:24 -0800202 SSL_load_error_strings();
203 OpenSSL_add_all_algorithms();
204 RAND_load_file("/dev/urandom", 1024);
Ed Tanousa1e077c2017-04-25 12:42:19 -0700205#endif
Ed Tanous0fdddb12017-02-28 11:06:34 -0800206}
207
Ed Tanous99923322017-03-03 14:21:24 -0800208void cleanup_openssl(void) {
209 CRYPTO_cleanup_all_ex_data();
210 ERR_free_strings();
Ed Tanousa1e077c2017-04-25 12:42:19 -0700211#if OPENSSL_VERSION_NUMBER < 0x10100000L
Ed Tanous99923322017-03-03 14:21:24 -0800212 ERR_remove_thread_state(0);
Ed Tanousa1e077c2017-04-25 12:42:19 -0700213#endif
Ed Tanous99923322017-03-03 14:21:24 -0800214 EVP_cleanup();
Ed Tanous0fdddb12017-02-28 11:06:34 -0800215}
216
217void handle_openssl_error(void) { ERR_print_errors_fp(stderr); }
Ed Tanous99923322017-03-03 14:21:24 -0800218inline void ensure_openssl_key_present_and_valid(const std::string &filepath) {
219 bool pem_file_valid = false;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800220
Ed Tanous99923322017-03-03 14:21:24 -0800221 pem_file_valid = verify_openssl_key_cert(filepath);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700222
Ed Tanous99923322017-03-03 14:21:24 -0800223 if (!pem_file_valid) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800224 LOG(WARNING) << "Error in verifying signature, regenerating";
Ed Tanous99923322017-03-03 14:21:24 -0800225 generate_ssl_certificate(filepath);
226 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800227}
Ed Tanousc4771fb2017-03-13 13:39:49 -0700228
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700229boost::asio::ssl::context get_ssl_context(std::string ssl_pem_file) {
Ed Tanousc4771fb2017-03-13 13:39:49 -0700230 boost::asio::ssl::context m_ssl_context{boost::asio::ssl::context::sslv23};
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700231 m_ssl_context.set_options(boost::asio::ssl::context::default_workarounds |
232 boost::asio::ssl::context::no_sslv2 |
233 boost::asio::ssl::context::no_sslv3 |
234 boost::asio::ssl::context::single_dh_use |
235 boost::asio::ssl::context::no_tlsv1 |
236 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700237
238 // m_ssl_context.set_verify_mode(boost::asio::ssl::verify_peer);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700239 m_ssl_context.use_certificate_file(ssl_pem_file,
240 boost::asio::ssl::context::pem);
241 m_ssl_context.use_private_key_file(ssl_pem_file,
242 boost::asio::ssl::context::pem);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700243
244 // Set up EC curves to auto (boost asio doesn't have a method for this)
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700245 // There is a pull request to add this. Once this is included in an asio
246 // drop, use the right way
Ed Tanousc4771fb2017-03-13 13:39:49 -0700247 // http://stackoverflow.com/questions/18929049/boost-asio-with-ecdsa-certificate-issue
248 if (SSL_CTX_set_ecdh_auto(m_ssl_context.native_handle(), 1) != 1) {
249 CROW_LOG_ERROR << "Error setting tmp ecdh list\n";
250 }
251
252 // From mozilla "compatibility"
253 std::string ciphers =
254 "ECDHE-ECDSA-CHACHA20-POLY1305:"
255 "ECDHE-RSA-CHACHA20-POLY1305:"
256 "ECDHE-ECDSA-AES128-GCM-SHA256:"
257 "ECDHE-RSA-AES128-GCM-SHA256:"
258 "ECDHE-ECDSA-AES256-GCM-SHA384:"
259 "ECDHE-RSA-AES256-GCM-SHA384:"
260 "DHE-RSA-AES128-GCM-SHA256:"
261 "DHE-RSA-AES256-GCM-SHA384:"
262 "ECDHE-ECDSA-AES128-SHA256:"
263 "ECDHE-RSA-AES128-SHA256:"
264 "ECDHE-ECDSA-AES128-SHA:"
265 "ECDHE-RSA-AES256-SHA384:"
266 "ECDHE-RSA-AES128-SHA:"
267 "ECDHE-ECDSA-AES256-SHA384:"
268 "ECDHE-ECDSA-AES256-SHA:"
269 "ECDHE-RSA-AES256-SHA:"
270 "DHE-RSA-AES128-SHA256:"
271 "DHE-RSA-AES128-SHA:"
272 "DHE-RSA-AES256-SHA256:"
273 "DHE-RSA-AES256-SHA:"
274 "ECDHE-ECDSA-DES-CBC3-SHA:"
275 "ECDHE-RSA-DES-CBC3-SHA:"
276 "EDH-RSA-DES-CBC3-SHA:"
277 "AES128-GCM-SHA256:"
278 "AES256-GCM-SHA384:"
279 "AES128-SHA256:"
280 "AES256-SHA256:"
281 "AES128-SHA:"
282 "AES256-SHA:"
283 "DES-CBC3-SHA:"
284 "!DSS";
285
286 // From mozilla "modern"
287 std::string modern_ciphers =
288 "ECDHE-ECDSA-AES256-GCM-SHA384:"
289 "ECDHE-RSA-AES256-GCM-SHA384:"
290 "ECDHE-ECDSA-CHACHA20-POLY1305:"
291 "ECDHE-RSA-CHACHA20-POLY1305:"
292 "ECDHE-ECDSA-AES128-GCM-SHA256:"
293 "ECDHE-RSA-AES128-GCM-SHA256:"
294 "ECDHE-ECDSA-AES256-SHA384:"
295 "ECDHE-RSA-AES256-SHA384:"
296 "ECDHE-ECDSA-AES128-SHA256:"
297 "ECDHE-RSA-AES128-SHA256";
298
299 std::string lighttp_ciphers = "AES128+EECDH:AES128+EDH:!aNULL:!eNULL";
300
Ed Tanous0d485ef2017-05-23 09:23:53 -0700301 if (SSL_CTX_set_cipher_list(m_ssl_context.native_handle(), lighttp_ciphers.c_str()) !=
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700302 1) {
Ed Tanousc4771fb2017-03-13 13:39:49 -0700303 CROW_LOG_ERROR << "Error setting cipher list\n";
304 }
305 return m_ssl_context;
306}
Ed Tanous01250f22017-04-18 17:49:51 -0700307}
308
309#endif