blob: d53c239c43605bcd64e62d04ebb702912e13bb2f [file] [log] [blame]
Ed Tanous0fdddb12017-02-28 11:06:34 -08001#pragma once
2
Ed Tanous1ccd57c2017-03-21 13:15:58 -07003#include <random>
4
Ed Tanous0fdddb12017-02-28 11:06:34 -08005#include <openssl/bio.h>
6#include <openssl/dh.h>
7#include <openssl/dsa.h>
8#include <openssl/dsa.h>
9#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>
15
Ed Tanousc4771fb2017-03-13 13:39:49 -070016#include <boost/asio.hpp>
17
Ed Tanous9b65f1f2017-03-07 15:17:13 -080018#include <g3log/g3log.hpp>
19
Ed Tanous99923322017-03-03 14:21:24 -080020namespace ensuressl {
Ed Tanous0fdddb12017-02-28 11:06:34 -080021static void init_openssl(void);
22static void cleanup_openssl(void);
23static EVP_PKEY *create_rsa_key(void);
Ed Tanous9b65f1f2017-03-07 15:17:13 -080024static EVP_PKEY *create_ec_key(void);
Ed Tanous0fdddb12017-02-28 11:06:34 -080025static void handle_openssl_error(void);
26
Ed Tanous99923322017-03-03 14:21:24 -080027inline bool verify_openssl_key_cert(const std::string &filepath) {
28 bool private_key_valid = false;
29 bool cert_valid = false;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080030
31 LOG(DEBUG) << "Checking certs in file " << filepath;
32
Ed Tanous99923322017-03-03 14:21:24 -080033 FILE *file = fopen(filepath.c_str(), "r");
34 if (file != NULL) {
35 EVP_PKEY *pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL);
36 int rc;
37 if (pkey) {
38 int type = EVP_PKEY_type(pkey->type);
39 switch (type) {
40 case EVP_PKEY_RSA:
41 case EVP_PKEY_RSA2: {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080042 LOG(DEBUG) << "Found an RSA key";
Ed Tanous99923322017-03-03 14:21:24 -080043 RSA *rsa = EVP_PKEY_get1_RSA(pkey);
Ed Tanous1ccd57c2017-03-21 13:15:58 -070044 if (rsa) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080045 if (RSA_check_key(rsa) == 1) {
Ed Tanous1ccd57c2017-03-21 13:15:58 -070046 // private_key_valid = true;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080047 } else {
48 LOG(WARNING) << "Key not valid error number " << ERR_get_error();
49 }
50 RSA_free(rsa);
Ed Tanous99923322017-03-03 14:21:24 -080051 }
Ed Tanous9b65f1f2017-03-07 15:17:13 -080052 break;
53 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -070054 case EVP_PKEY_EC: {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080055 LOG(DEBUG) << "Found an EC key";
Ed Tanous1ccd57c2017-03-21 13:15:58 -070056 EC_KEY *ec = EVP_PKEY_get1_EC_KEY(pkey);
57 if (ec) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080058 if (EC_KEY_check_key(ec) == 1) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070059 private_key_valid = true;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080060 } else {
61 LOG(WARNING) << "Key not valid error number " << ERR_get_error();
62 }
63 EC_KEY_free(ec);
64 }
Ed Tanous99923322017-03-03 14:21:24 -080065 break;
Ed Tanous0fdddb12017-02-28 11:06:34 -080066 }
Ed Tanous99923322017-03-03 14:21:24 -080067 default:
Ed Tanous9b65f1f2017-03-07 15:17:13 -080068 LOG(WARNING) << "Found an unrecognized key type " << type;
Ed Tanous99923322017-03-03 14:21:24 -080069 break;
70 }
71
72 if (private_key_valid) {
73 X509 *x509 = PEM_read_X509(file, NULL, NULL, NULL);
Ed Tanous1ccd57c2017-03-21 13:15:58 -070074 if (!x509) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080075 LOG(DEBUG) << "error getting x509 cert " << ERR_get_error();
76 } else {
77 rc = X509_verify(x509, pkey);
78 if (rc == 1) {
79 cert_valid = true;
80 } else {
Ed Tanous1ccd57c2017-03-21 13:15:58 -070081 LOG(WARNING) << "Error in verifying private key signature "
82 << ERR_get_error();
Ed Tanous9b65f1f2017-03-07 15:17:13 -080083 }
Ed Tanous99923322017-03-03 14:21:24 -080084 }
85 }
86
87 EVP_PKEY_free(pkey);
Ed Tanous0fdddb12017-02-28 11:06:34 -080088 }
Ed Tanous99923322017-03-03 14:21:24 -080089 fclose(file);
90 }
91 return cert_valid;
Ed Tanous0fdddb12017-02-28 11:06:34 -080092}
93
Ed Tanous99923322017-03-03 14:21:24 -080094inline void generate_ssl_certificate(const std::string &filepath) {
Ed Tanous99923322017-03-03 14:21:24 -080095 FILE *pFile = NULL;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080096 LOG(WARNING) << "Generating new keys";
Ed Tanous99923322017-03-03 14:21:24 -080097 init_openssl();
Ed Tanous0fdddb12017-02-28 11:06:34 -080098
Ed Tanous1ccd57c2017-03-21 13:15:58 -070099 // LOG(WARNING) << "Generating RSA key";
100 // EVP_PKEY *pRsaPrivKey = create_rsa_key();
Ed Tanous0fdddb12017-02-28 11:06:34 -0800101
Ed Tanousc4771fb2017-03-13 13:39:49 -0700102 LOG(WARNING) << "Generating EC key";
103 EVP_PKEY *pRsaPrivKey = create_ec_key();
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700104 if (pRsaPrivKey) {
105 LOG(WARNING) << "Generating x509 Certificate";
106 // Use this code to directly generate a certificate
107 X509 *x509;
108 x509 = X509_new();
109 if (x509) {
110 // Get a random number from the RNG for the certificate serial number
111 // If this is not random, regenerating certs throws broswer errors
112 std::random_device rd;
113 int serial = rd();
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800114
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700115 ASN1_INTEGER_set(X509_get_serialNumber(x509), serial);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800116
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700117 // not before this moment
118 X509_gmtime_adj(X509_get_notBefore(x509), 0);
119 // Cert is valid for 10 years
120 X509_gmtime_adj(X509_get_notAfter(x509), 60L * 60L * 24L * 365L * 10L);
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800121
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700122 // set the public key to the key we just generated
123 X509_set_pubkey(x509, pRsaPrivKey);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800124
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700125 // Get the subject name
126 X509_NAME *name;
127 name = X509_get_subject_name(x509);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800128
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700129 X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char *)"US",
130 -1, -1, 0);
131 X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC,
132 (unsigned char *)"Intel BMC", -1, -1, 0);
133 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
134 (unsigned char *)"testhost", -1, -1, 0);
135 // set the CSR options
136 X509_set_issuer_name(x509, name);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800137
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700138 // Sign the certificate with our private key
139 X509_sign(x509, pRsaPrivKey, EVP_sha256());
Ed Tanous0fdddb12017-02-28 11:06:34 -0800140
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700141 pFile = fopen(filepath.c_str(), "wt");
Ed Tanous0fdddb12017-02-28 11:06:34 -0800142
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700143 if (pFile) {
144 PEM_write_PrivateKey(pFile, pRsaPrivKey, NULL, NULL, 0, 0, NULL);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800145
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700146 PEM_write_X509(pFile, x509);
147 fclose(pFile);
148 pFile = NULL;
149 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800150
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700151 X509_free(x509);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800152 }
153
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800154 EVP_PKEY_free(pRsaPrivKey);
155 pRsaPrivKey = NULL;
Ed Tanous99923322017-03-03 14:21:24 -0800156 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700157
Ed Tanous99923322017-03-03 14:21:24 -0800158 // cleanup_openssl();
159}
160
161EVP_PKEY *create_rsa_key(void) {
162 RSA *pRSA = NULL;
163 EVP_PKEY *pKey = NULL;
164 pRSA = RSA_generate_key(2048, RSA_3, NULL, NULL);
165 pKey = EVP_PKEY_new();
166 if (pRSA && pKey && EVP_PKEY_assign_RSA(pKey, pRSA)) {
167 /* pKey owns pRSA from now */
168 if (RSA_check_key(pRSA) <= 0) {
169 fprintf(stderr, "RSA_check_key failed.\n");
170 handle_openssl_error();
171 EVP_PKEY_free(pKey);
172 pKey = NULL;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800173 }
Ed Tanous99923322017-03-03 14:21:24 -0800174 } else {
175 handle_openssl_error();
176 if (pRSA) {
177 RSA_free(pRSA);
178 pRSA = NULL;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800179 }
Ed Tanous99923322017-03-03 14:21:24 -0800180 if (pKey) {
181 EVP_PKEY_free(pKey);
182 pKey = NULL;
183 }
184 }
185 return pKey;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800186}
187
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800188EVP_PKEY *create_ec_key(void) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800189 EVP_PKEY *pKey = NULL;
190 int eccgrp = 0;
191 eccgrp = OBJ_txt2nid("prime256v1");
192
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700193 EC_KEY *myecc = EC_KEY_new_by_curve_name(eccgrp);
194 if (myecc) {
195 EC_KEY_set_asn1_flag(myecc, OPENSSL_EC_NAMED_CURVE);
196 EC_KEY_generate_key(myecc);
197 EVP_PKEY *pKey = EVP_PKEY_new();
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800198 if (pKey) {
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700199 if (EVP_PKEY_assign_EC_KEY(pKey, myecc)) {
200 /* pKey owns pRSA from now */
201 if (EC_KEY_check_key(myecc) <= 0) {
202 fprintf(stderr, "EC_check_key failed.\n");
203 }
204 }
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800205 EVP_PKEY_free(pKey);
206 pKey = NULL;
207 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700208 EC_KEY_free(myecc);
209 myecc = NULL;
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800210 }
211 return pKey;
212}
213
Ed Tanous99923322017-03-03 14:21:24 -0800214void init_openssl(void) {
215 if (SSL_library_init()) {
216 SSL_load_error_strings();
217 OpenSSL_add_all_algorithms();
218 RAND_load_file("/dev/urandom", 1024);
219 } else
220 exit(EXIT_FAILURE);
Ed Tanous0fdddb12017-02-28 11:06:34 -0800221}
222
Ed Tanous99923322017-03-03 14:21:24 -0800223void cleanup_openssl(void) {
224 CRYPTO_cleanup_all_ex_data();
225 ERR_free_strings();
226 ERR_remove_thread_state(0);
227 EVP_cleanup();
Ed Tanous0fdddb12017-02-28 11:06:34 -0800228}
229
230void handle_openssl_error(void) { ERR_print_errors_fp(stderr); }
Ed Tanous99923322017-03-03 14:21:24 -0800231inline void ensure_openssl_key_present_and_valid(const std::string &filepath) {
232 bool pem_file_valid = false;
Ed Tanous0fdddb12017-02-28 11:06:34 -0800233
Ed Tanous99923322017-03-03 14:21:24 -0800234 pem_file_valid = verify_openssl_key_cert(filepath);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700235
Ed Tanous99923322017-03-03 14:21:24 -0800236 if (!pem_file_valid) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800237 LOG(WARNING) << "Error in verifying signature, regenerating";
Ed Tanous99923322017-03-03 14:21:24 -0800238 generate_ssl_certificate(filepath);
239 }
Ed Tanous0fdddb12017-02-28 11:06:34 -0800240}
Ed Tanousc4771fb2017-03-13 13:39:49 -0700241
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700242boost::asio::ssl::context get_ssl_context(std::string ssl_pem_file) {
Ed Tanousc4771fb2017-03-13 13:39:49 -0700243 boost::asio::ssl::context m_ssl_context{boost::asio::ssl::context::sslv23};
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700244 m_ssl_context.set_options(boost::asio::ssl::context::default_workarounds |
245 boost::asio::ssl::context::no_sslv2 |
246 boost::asio::ssl::context::no_sslv3 |
247 boost::asio::ssl::context::single_dh_use |
248 boost::asio::ssl::context::no_tlsv1 |
249 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700250
251 // m_ssl_context.set_verify_mode(boost::asio::ssl::verify_peer);
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700252 m_ssl_context.use_certificate_file(ssl_pem_file,
253 boost::asio::ssl::context::pem);
254 m_ssl_context.use_private_key_file(ssl_pem_file,
255 boost::asio::ssl::context::pem);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700256
257 // Set up EC curves to auto (boost asio doesn't have a method for this)
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700258 // There is a pull request to add this. Once this is included in an asio
259 // drop, use the right way
Ed Tanousc4771fb2017-03-13 13:39:49 -0700260 // http://stackoverflow.com/questions/18929049/boost-asio-with-ecdsa-certificate-issue
261 if (SSL_CTX_set_ecdh_auto(m_ssl_context.native_handle(), 1) != 1) {
262 CROW_LOG_ERROR << "Error setting tmp ecdh list\n";
263 }
264
265 // From mozilla "compatibility"
266 std::string ciphers =
267 "ECDHE-ECDSA-CHACHA20-POLY1305:"
268 "ECDHE-RSA-CHACHA20-POLY1305:"
269 "ECDHE-ECDSA-AES128-GCM-SHA256:"
270 "ECDHE-RSA-AES128-GCM-SHA256:"
271 "ECDHE-ECDSA-AES256-GCM-SHA384:"
272 "ECDHE-RSA-AES256-GCM-SHA384:"
273 "DHE-RSA-AES128-GCM-SHA256:"
274 "DHE-RSA-AES256-GCM-SHA384:"
275 "ECDHE-ECDSA-AES128-SHA256:"
276 "ECDHE-RSA-AES128-SHA256:"
277 "ECDHE-ECDSA-AES128-SHA:"
278 "ECDHE-RSA-AES256-SHA384:"
279 "ECDHE-RSA-AES128-SHA:"
280 "ECDHE-ECDSA-AES256-SHA384:"
281 "ECDHE-ECDSA-AES256-SHA:"
282 "ECDHE-RSA-AES256-SHA:"
283 "DHE-RSA-AES128-SHA256:"
284 "DHE-RSA-AES128-SHA:"
285 "DHE-RSA-AES256-SHA256:"
286 "DHE-RSA-AES256-SHA:"
287 "ECDHE-ECDSA-DES-CBC3-SHA:"
288 "ECDHE-RSA-DES-CBC3-SHA:"
289 "EDH-RSA-DES-CBC3-SHA:"
290 "AES128-GCM-SHA256:"
291 "AES256-GCM-SHA384:"
292 "AES128-SHA256:"
293 "AES256-SHA256:"
294 "AES128-SHA:"
295 "AES256-SHA:"
296 "DES-CBC3-SHA:"
297 "!DSS";
298
299 // From mozilla "modern"
300 std::string modern_ciphers =
301 "ECDHE-ECDSA-AES256-GCM-SHA384:"
302 "ECDHE-RSA-AES256-GCM-SHA384:"
303 "ECDHE-ECDSA-CHACHA20-POLY1305:"
304 "ECDHE-RSA-CHACHA20-POLY1305:"
305 "ECDHE-ECDSA-AES128-GCM-SHA256:"
306 "ECDHE-RSA-AES128-GCM-SHA256:"
307 "ECDHE-ECDSA-AES256-SHA384:"
308 "ECDHE-RSA-AES256-SHA384:"
309 "ECDHE-ECDSA-AES128-SHA256:"
310 "ECDHE-RSA-AES128-SHA256";
311
312 std::string lighttp_ciphers = "AES128+EECDH:AES128+EDH:!aNULL:!eNULL";
313
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700314 if (SSL_CTX_set_cipher_list(m_ssl_context.native_handle(), ciphers.c_str()) !=
315 1) {
Ed Tanousc4771fb2017-03-13 13:39:49 -0700316 CROW_LOG_ERROR << "Error setting cipher list\n";
317 }
318 return m_ssl_context;
319}
Ed Tanous0fdddb12017-02-28 11:06:34 -0800320}