Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 2 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 3 | |
| 4 | #include <openssl/bio.h> |
| 5 | #include <openssl/dh.h> |
| 6 | #include <openssl/dsa.h> |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 7 | #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 Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 13 | #include <random> |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 14 | #include <boost/asio.hpp> |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 15 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 16 | namespace ensuressl { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 17 | static void initOpenssl(); |
| 18 | static void cleanupOpenssl(); |
| 19 | static EVP_PKEY *createRsaKey(); |
| 20 | static EVP_PKEY *createEcKey(); |
| 21 | static void handleOpensslError(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 22 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 23 | inline bool verifyOpensslKeyCert(const std::string &filepath) { |
| 24 | bool privateKeyValid = false; |
| 25 | bool certValid = false; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 26 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 27 | std::cout << "Checking certs in file " << filepath << "\n"; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 28 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 29 | FILE *file = fopen(filepath.c_str(), "r"); |
| 30 | if (file != NULL) { |
| 31 | EVP_PKEY *pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL); |
| 32 | int rc; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 33 | if (pkey != nullptr) { |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 34 | RSA *rsa = EVP_PKEY_get1_RSA(pkey); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 35 | if (rsa != nullptr) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 36 | std::cout << "Found an RSA key\n"; |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 37 | if (RSA_check_key(rsa) == 1) { |
| 38 | // private_key_valid = true; |
| 39 | } else { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 40 | std::cerr << "Key not valid error number " << ERR_get_error() << "\n"; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 41 | } |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 42 | RSA_free(rsa); |
| 43 | } else { |
| 44 | EC_KEY *ec = EVP_PKEY_get1_EC_KEY(pkey); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 45 | if (ec != nullptr) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 46 | std::cout << "Found an EC key\n"; |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 47 | if (EC_KEY_check_key(ec) == 1) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 48 | privateKeyValid = true; |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 49 | } else { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 50 | std::cerr << "Key not valid error number " << ERR_get_error() |
| 51 | << "\n"; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 52 | } |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 53 | EC_KEY_free(ec); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 54 | } |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 57 | if (privateKeyValid) { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 58 | X509 *x509 = PEM_read_X509(file, NULL, NULL, NULL); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 59 | if (x509 == nullptr) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 60 | std::cout << "error getting x509 cert " << ERR_get_error() << "\n"; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 61 | } else { |
| 62 | rc = X509_verify(x509, pkey); |
| 63 | if (rc == 1) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 64 | certValid = true; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 65 | } else { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 66 | std::cerr << "Error in verifying private key signature " |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 67 | << ERR_get_error() << "\n"; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 68 | } |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | EVP_PKEY_free(pkey); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 73 | } |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 74 | fclose(file); |
| 75 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 76 | return certValid; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 79 | inline void generateSslCertificate(const std::string &filepath) { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 80 | FILE *pFile = NULL; |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 81 | std::cout << "Generating new keys\n"; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 82 | initOpenssl(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 83 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 84 | // std::cerr << "Generating RSA key"; |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 85 | // EVP_PKEY *pRsaPrivKey = create_rsa_key(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 86 | |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 87 | std::cerr << "Generating EC key\n"; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 88 | EVP_PKEY *pRsaPrivKey = createEcKey(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 89 | if (pRsaPrivKey != nullptr) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 90 | std::cerr << "Generating x509 Certificate\n"; |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 91 | // Use this code to directly generate a certificate |
| 92 | X509 *x509; |
| 93 | x509 = X509_new(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 94 | if (x509 != nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 95 | // get a random number from the RNG for the certificate serial number |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 96 | // If this is not random, regenerating certs throws broswer errors |
| 97 | std::random_device rd; |
| 98 | int serial = rd(); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 99 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 100 | ASN1_INTEGER_set(X509_get_serialNumber(x509), serial); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 101 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 102 | // not before this moment |
| 103 | X509_gmtime_adj(X509_get_notBefore(x509), 0); |
| 104 | // Cert is valid for 10 years |
| 105 | X509_gmtime_adj(X509_get_notAfter(x509), 60L * 60L * 24L * 365L * 10L); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 106 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 107 | // set the public key to the key we just generated |
| 108 | X509_set_pubkey(x509, pRsaPrivKey); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 109 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 110 | // get the subject name |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 111 | X509_NAME *name; |
| 112 | name = X509_get_subject_name(x509); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 113 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 114 | X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, |
| 115 | reinterpret_cast<const unsigned char *>("US"), |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 116 | -1, -1, 0); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 117 | X509_NAME_add_entry_by_txt( |
| 118 | name, "O", MBSTRING_ASC, |
| 119 | reinterpret_cast<const unsigned char *>("Intel BMC"), -1, -1, 0); |
| 120 | X509_NAME_add_entry_by_txt( |
| 121 | name, "CN", MBSTRING_ASC, |
| 122 | reinterpret_cast<const unsigned char *>("testhost"), -1, -1, 0); |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 123 | // set the CSR options |
| 124 | X509_set_issuer_name(x509, name); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 125 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 126 | // Sign the certificate with our private key |
| 127 | X509_sign(x509, pRsaPrivKey, EVP_sha256()); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 128 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 129 | pFile = fopen(filepath.c_str(), "wt"); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 130 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 131 | if (pFile != nullptr) { |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 132 | PEM_write_PrivateKey(pFile, pRsaPrivKey, NULL, NULL, 0, 0, NULL); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 133 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 134 | PEM_write_X509(pFile, x509); |
| 135 | fclose(pFile); |
| 136 | pFile = NULL; |
| 137 | } |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 138 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 139 | X509_free(x509); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 142 | EVP_PKEY_free(pRsaPrivKey); |
| 143 | pRsaPrivKey = NULL; |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 144 | } |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 145 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 146 | // cleanup_openssl(); |
| 147 | } |
| 148 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 149 | EVP_PKEY *createRsaKey() { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 150 | RSA *pRSA = NULL; |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 151 | #if OPENSSL_VERSION_NUMBER < 0x00908000L |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 152 | pRSA = RSA_generate_key(2048, RSA_3, NULL, NULL); |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 153 | #else |
| 154 | RSA_generate_key_ex(pRSA, 2048, NULL, NULL); |
| 155 | #endif |
| 156 | |
| 157 | EVP_PKEY *pKey = EVP_PKEY_new(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 158 | if ((pRSA != nullptr) && (pKey != nullptr) && |
| 159 | EVP_PKEY_assign_RSA(pKey, pRSA)) { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 160 | /* pKey owns pRSA from now */ |
| 161 | if (RSA_check_key(pRSA) <= 0) { |
| 162 | fprintf(stderr, "RSA_check_key failed.\n"); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 163 | handleOpensslError(); |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 164 | EVP_PKEY_free(pKey); |
| 165 | pKey = NULL; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 166 | } |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 167 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 168 | handleOpensslError(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 169 | if (pRSA != nullptr) { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 170 | RSA_free(pRSA); |
| 171 | pRSA = NULL; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 172 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 173 | if (pKey != nullptr) { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 174 | EVP_PKEY_free(pKey); |
| 175 | pKey = NULL; |
| 176 | } |
| 177 | } |
| 178 | return pKey; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 181 | EVP_PKEY *createEcKey() { |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 182 | EVP_PKEY *pKey = NULL; |
| 183 | int eccgrp = 0; |
| 184 | eccgrp = OBJ_txt2nid("prime256v1"); |
| 185 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 186 | EC_KEY *myecc = EC_KEY_new_by_curve_name(eccgrp); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 187 | if (myecc != nullptr) { |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 188 | EC_KEY_set_asn1_flag(myecc, OPENSSL_EC_NAMED_CURVE); |
| 189 | EC_KEY_generate_key(myecc); |
Ed Tanous | 01250f2 | 2017-04-18 17:49:51 -0700 | [diff] [blame] | 190 | pKey = EVP_PKEY_new(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 191 | if (pKey != nullptr) { |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 192 | if (EVP_PKEY_assign_EC_KEY(pKey, myecc)) { |
| 193 | /* pKey owns pRSA from now */ |
| 194 | if (EC_KEY_check_key(myecc) <= 0) { |
| 195 | fprintf(stderr, "EC_check_key failed.\n"); |
| 196 | } |
| 197 | } |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | return pKey; |
| 201 | } |
| 202 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 203 | void initOpenssl() { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 204 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 205 | SSL_load_error_strings(); |
| 206 | OpenSSL_add_all_algorithms(); |
| 207 | RAND_load_file("/dev/urandom", 1024); |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 208 | #endif |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 211 | void cleanupOpenssl() { |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 212 | CRYPTO_cleanup_all_ex_data(); |
| 213 | ERR_free_strings(); |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 214 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 215 | ERR_remove_thread_state(0); |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 216 | #endif |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 217 | EVP_cleanup(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 220 | void handleOpensslError() { ERR_print_errors_fp(stderr); } |
| 221 | inline void ensureOpensslKeyPresentAndValid(const std::string &filepath) { |
| 222 | bool pemFileValid = false; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 223 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 224 | pemFileValid = verifyOpensslKeyCert(filepath); |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 225 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 226 | if (!pemFileValid) { |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 227 | std::cerr << "Error in verifying signature, regenerating\n"; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 228 | generateSslCertificate(filepath); |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 229 | } |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 230 | } |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 231 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 232 | inline boost::asio::ssl::context getSslContext( |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 233 | const std::string &ssl_pem_file) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 234 | boost::asio::ssl::context mSslContext{boost::asio::ssl::context::sslv23}; |
| 235 | mSslContext.set_options(boost::asio::ssl::context::default_workarounds | |
| 236 | boost::asio::ssl::context::no_sslv2 | |
| 237 | boost::asio::ssl::context::no_sslv3 | |
| 238 | boost::asio::ssl::context::single_dh_use | |
| 239 | boost::asio::ssl::context::no_tlsv1 | |
| 240 | boost::asio::ssl::context::no_tlsv1_1); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 241 | |
| 242 | // m_ssl_context.set_verify_mode(boost::asio::ssl::verify_peer); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 243 | mSslContext.use_certificate_file(ssl_pem_file, |
| 244 | boost::asio::ssl::context::pem); |
| 245 | mSslContext.use_private_key_file(ssl_pem_file, |
| 246 | boost::asio::ssl::context::pem); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 247 | |
| 248 | // Set up EC curves to auto (boost asio doesn't have a method for this) |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 249 | // There is a pull request to add this. Once this is included in an asio |
| 250 | // drop, use the right way |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 251 | // http://stackoverflow.com/questions/18929049/boost-asio-with-ecdsa-certificate-issue |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 252 | if (SSL_CTX_set_ecdh_auto(mSslContext.native_handle(), 1) != 1) { |
| 253 | BMCWEB_LOG_ERROR << "Error setting tmp ecdh list\n"; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // From mozilla "compatibility" |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 257 | std::string mozillaCompatibilityCiphers = |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 258 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 259 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 260 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 261 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 262 | "ECDHE-ECDSA-AES256-GCM-SHA384:" |
| 263 | "ECDHE-RSA-AES256-GCM-SHA384:" |
| 264 | "DHE-RSA-AES128-GCM-SHA256:" |
| 265 | "DHE-RSA-AES256-GCM-SHA384:" |
| 266 | "ECDHE-ECDSA-AES128-SHA256:" |
| 267 | "ECDHE-RSA-AES128-SHA256:" |
| 268 | "ECDHE-ECDSA-AES128-SHA:" |
| 269 | "ECDHE-RSA-AES256-SHA384:" |
| 270 | "ECDHE-RSA-AES128-SHA:" |
| 271 | "ECDHE-ECDSA-AES256-SHA384:" |
| 272 | "ECDHE-ECDSA-AES256-SHA:" |
| 273 | "ECDHE-RSA-AES256-SHA:" |
| 274 | "DHE-RSA-AES128-SHA256:" |
| 275 | "DHE-RSA-AES128-SHA:" |
| 276 | "DHE-RSA-AES256-SHA256:" |
| 277 | "DHE-RSA-AES256-SHA:" |
| 278 | "ECDHE-ECDSA-DES-CBC3-SHA:" |
| 279 | "ECDHE-RSA-DES-CBC3-SHA:" |
| 280 | "EDH-RSA-DES-CBC3-SHA:" |
| 281 | "AES128-GCM-SHA256:" |
| 282 | "AES256-GCM-SHA384:" |
| 283 | "AES128-SHA256:" |
| 284 | "AES256-SHA256:" |
| 285 | "AES128-SHA:" |
| 286 | "AES256-SHA:" |
| 287 | "DES-CBC3-SHA:" |
| 288 | "!DSS"; |
| 289 | |
| 290 | // From mozilla "modern" |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 291 | std::string mozillaModernCiphers = |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 292 | "ECDHE-ECDSA-AES256-GCM-SHA384:" |
| 293 | "ECDHE-RSA-AES256-GCM-SHA384:" |
| 294 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 295 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 296 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 297 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 298 | "ECDHE-ECDSA-AES256-SHA384:" |
| 299 | "ECDHE-RSA-AES256-SHA384:" |
| 300 | "ECDHE-ECDSA-AES128-SHA256:" |
| 301 | "ECDHE-RSA-AES128-SHA256"; |
| 302 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 303 | std::string aesOnlyCiphers = "AES128+EECDH:AES128+EDH:!aNULL:!eNULL"; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 304 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 305 | if (SSL_CTX_set_cipher_list(mSslContext.native_handle(), |
| 306 | mozillaCompatibilityCiphers.c_str()) != 1) { |
| 307 | BMCWEB_LOG_ERROR << "Error setting cipher list\n"; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 308 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 309 | return mSslContext; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 310 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 311 | } // namespace ensuressl |
Ed Tanous | 01250f2 | 2017-04-18 17:49:51 -0700 | [diff] [blame] | 312 | |
| 313 | #endif |