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 | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 13 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 14 | #include <boost/asio/ssl/context.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 15 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | #include <random> |
| 17 | |
| 18 | namespace ensuressl |
| 19 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 20 | constexpr char const* trustStorePath = "/etc/ssl/certs/authority"; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 21 | static void initOpenssl(); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 22 | static EVP_PKEY* createEcKey(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 23 | |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 24 | // Trust chain related errors.` |
| 25 | inline 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 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 35 | return false; |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 38 | inline bool validateCertificate(X509* const cert) |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 39 | { |
| 40 | // Create an empty X509_STORE structure for certificate validation. |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 41 | X509_STORE* x509Store = X509_STORE_new(); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 42 | if (!x509Store) |
| 43 | { |
Gunnar Mills | c61704a | 2020-07-08 13:47:06 -0500 | [diff] [blame] | 44 | BMCWEB_LOG_ERROR << "Error occurred during X509_STORE_new call"; |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | |
| 48 | // Load Certificate file into the X509 structure. |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 49 | X509_STORE_CTX* storeCtx = X509_STORE_CTX_new(); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 50 | if (!storeCtx) |
| 51 | { |
Gunnar Mills | c61704a | 2020-07-08 13:47:06 -0500 | [diff] [blame] | 52 | BMCWEB_LOG_ERROR << "Error occurred during X509_STORE_CTX_new call"; |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 53 | X509_STORE_free(x509Store); |
| 54 | return false; |
| 55 | } |
| 56 | |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 57 | int errCode = X509_STORE_CTX_init(storeCtx, x509Store, cert, nullptr); |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 58 | if (errCode != 1) |
| 59 | { |
Gunnar Mills | c61704a | 2020-07-08 13:47:06 -0500 | [diff] [blame] | 60 | BMCWEB_LOG_ERROR << "Error occurred during X509_STORE_CTX_init call"; |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 61 | X509_STORE_CTX_free(storeCtx); |
| 62 | X509_STORE_free(x509Store); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | errCode = X509_verify_cert(storeCtx); |
| 67 | if (errCode == 1) |
| 68 | { |
| 69 | BMCWEB_LOG_INFO << "Certificate verification is success"; |
| 70 | X509_STORE_CTX_free(storeCtx); |
| 71 | X509_STORE_free(x509Store); |
| 72 | return true; |
| 73 | } |
| 74 | if (errCode == 0) |
| 75 | { |
| 76 | errCode = X509_STORE_CTX_get_error(storeCtx); |
| 77 | X509_STORE_CTX_free(storeCtx); |
| 78 | X509_STORE_free(x509Store); |
| 79 | if (isTrustChainError(errCode)) |
| 80 | { |
| 81 | BMCWEB_LOG_DEBUG << "Ignoring Trust Chain error. Reason: " |
| 82 | << X509_verify_cert_error_string(errCode); |
| 83 | return true; |
| 84 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 85 | BMCWEB_LOG_ERROR << "Certificate verification failed. Reason: " |
| 86 | << X509_verify_cert_error_string(errCode); |
| 87 | return false; |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | BMCWEB_LOG_ERROR |
Gunnar Mills | c61704a | 2020-07-08 13:47:06 -0500 | [diff] [blame] | 91 | << "Error occurred during X509_verify_cert call. ErrorCode: " |
| 92 | << errCode; |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 93 | X509_STORE_CTX_free(storeCtx); |
| 94 | X509_STORE_free(x509Store); |
| 95 | return false; |
| 96 | } |
| 97 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 98 | inline bool verifyOpensslKeyCert(const std::string& filepath) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 99 | { |
| 100 | bool privateKeyValid = false; |
| 101 | bool certValid = false; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 102 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | std::cout << "Checking certs in file " << filepath << "\n"; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 104 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 105 | FILE* file = fopen(filepath.c_str(), "r"); |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 106 | if (file != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 107 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 108 | EVP_PKEY* pkey = PEM_read_PrivateKey(file, nullptr, nullptr, nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | if (pkey != nullptr) |
| 110 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 111 | RSA* rsa = EVP_PKEY_get1_RSA(pkey); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 112 | if (rsa != nullptr) |
| 113 | { |
| 114 | std::cout << "Found an RSA key\n"; |
| 115 | if (RSA_check_key(rsa) == 1) |
| 116 | { |
Jayanth Othayoth | 4d2849a | 2019-05-13 01:46:34 -0500 | [diff] [blame] | 117 | privateKeyValid = true; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | { |
| 121 | std::cerr << "Key not valid error number " |
| 122 | << ERR_get_error() << "\n"; |
| 123 | } |
| 124 | RSA_free(rsa); |
| 125 | } |
| 126 | else |
| 127 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 128 | EC_KEY* ec = EVP_PKEY_get1_EC_KEY(pkey); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 129 | if (ec != nullptr) |
| 130 | { |
| 131 | std::cout << "Found an EC key\n"; |
| 132 | if (EC_KEY_check_key(ec) == 1) |
| 133 | { |
| 134 | privateKeyValid = true; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | std::cerr << "Key not valid error number " |
| 139 | << ERR_get_error() << "\n"; |
| 140 | } |
| 141 | EC_KEY_free(ec); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (privateKeyValid) |
| 146 | { |
Ramesh Iyyar | c0bf893 | 2019-06-22 00:23:29 -0500 | [diff] [blame] | 147 | // If the order is certificate followed by key in input file |
| 148 | // then, certificate read will fail. So, setting the file |
| 149 | // pointer to point beginning of file to avoid certificate and |
| 150 | // key order issue. |
| 151 | fseek(file, 0, SEEK_SET); |
| 152 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 153 | X509* x509 = PEM_read_X509(file, nullptr, nullptr, nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 154 | if (x509 == nullptr) |
| 155 | { |
| 156 | std::cout << "error getting x509 cert " << ERR_get_error() |
| 157 | << "\n"; |
| 158 | } |
| 159 | else |
| 160 | { |
Ramesh Iyyar | 082f28f | 2019-06-22 03:31:55 -0500 | [diff] [blame] | 161 | certValid = validateCertificate(x509); |
| 162 | X509_free(x509); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | EVP_PKEY_free(pkey); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 167 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 168 | fclose(file); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 169 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 170 | return certValid; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 173 | inline void generateSslCertificate(const std::string& filepath) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 174 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 175 | FILE* pFile = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 176 | std::cout << "Generating new keys\n"; |
| 177 | initOpenssl(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 178 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 179 | std::cerr << "Generating EC key\n"; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 180 | EVP_PKEY* pPrivKey = createEcKey(); |
Vernon Mauery | 0185c7f | 2020-03-09 10:56:53 -0700 | [diff] [blame] | 181 | if (pPrivKey != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 182 | { |
| 183 | std::cerr << "Generating x509 Certificate\n"; |
| 184 | // Use this code to directly generate a certificate |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 185 | X509* x509; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 186 | x509 = X509_new(); |
| 187 | if (x509 != nullptr) |
| 188 | { |
| 189 | // get a random number from the RNG for the certificate serial |
| 190 | // number If this is not random, regenerating certs throws broswer |
| 191 | // errors |
| 192 | std::random_device rd; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 193 | int serial = static_cast<int>(rd()); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 194 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 195 | ASN1_INTEGER_set(X509_get_serialNumber(x509), serial); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 196 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 197 | // not before this moment |
| 198 | X509_gmtime_adj(X509_get_notBefore(x509), 0); |
| 199 | // Cert is valid for 10 years |
| 200 | X509_gmtime_adj(X509_get_notAfter(x509), |
| 201 | 60L * 60L * 24L * 365L * 10L); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 202 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 203 | // set the public key to the key we just generated |
Vernon Mauery | 0185c7f | 2020-03-09 10:56:53 -0700 | [diff] [blame] | 204 | X509_set_pubkey(x509, pPrivKey); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 205 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 206 | // get the subject name |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 207 | X509_NAME* name; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 208 | name = X509_get_subject_name(x509); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 209 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 210 | X509_NAME_add_entry_by_txt( |
| 211 | name, "C", MBSTRING_ASC, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 212 | reinterpret_cast<const unsigned char*>("US"), -1, -1, 0); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 213 | X509_NAME_add_entry_by_txt( |
| 214 | name, "O", MBSTRING_ASC, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 215 | reinterpret_cast<const unsigned char*>("OpenBMC"), -1, -1, 0); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 216 | X509_NAME_add_entry_by_txt( |
| 217 | name, "CN", MBSTRING_ASC, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 218 | reinterpret_cast<const unsigned char*>("testhost"), -1, -1, 0); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 219 | // set the CSR options |
| 220 | X509_set_issuer_name(x509, name); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 221 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 222 | // Sign the certificate with our private key |
Vernon Mauery | 0185c7f | 2020-03-09 10:56:53 -0700 | [diff] [blame] | 223 | X509_sign(x509, pPrivKey, EVP_sha256()); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 224 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 225 | pFile = fopen(filepath.c_str(), "wt"); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 226 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 227 | if (pFile != nullptr) |
| 228 | { |
Vernon Mauery | 0185c7f | 2020-03-09 10:56:53 -0700 | [diff] [blame] | 229 | PEM_write_PrivateKey(pFile, pPrivKey, nullptr, nullptr, 0, |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 230 | nullptr, nullptr); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 231 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 232 | PEM_write_X509(pFile, x509); |
| 233 | fclose(pFile); |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 234 | pFile = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 235 | } |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 236 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 237 | X509_free(x509); |
| 238 | } |
| 239 | |
Vernon Mauery | 0185c7f | 2020-03-09 10:56:53 -0700 | [diff] [blame] | 240 | EVP_PKEY_free(pPrivKey); |
| 241 | pPrivKey = nullptr; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 244 | // cleanup_openssl(); |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 245 | } |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 246 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 247 | EVP_PKEY* createEcKey() |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 248 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 249 | EVP_PKEY* pKey = nullptr; |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 250 | int eccgrp = 0; |
Vernon Mauery | aaf3206 | 2020-03-09 10:41:31 -0700 | [diff] [blame] | 251 | eccgrp = OBJ_txt2nid("secp384r1"); |
Ed Tanous | 6ea007a | 2019-02-13 22:48:25 -0800 | [diff] [blame] | 252 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 253 | EC_KEY* myecc = EC_KEY_new_by_curve_name(eccgrp); |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 254 | if (myecc != nullptr) |
Ed Tanous | 6ea007a | 2019-02-13 22:48:25 -0800 | [diff] [blame] | 255 | { |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 256 | EC_KEY_set_asn1_flag(myecc, OPENSSL_EC_NAMED_CURVE); |
| 257 | EC_KEY_generate_key(myecc); |
| 258 | pKey = EVP_PKEY_new(); |
| 259 | if (pKey != nullptr) |
| 260 | { |
| 261 | if (EVP_PKEY_assign_EC_KEY(pKey, myecc)) |
| 262 | { |
Vernon Mauery | 0185c7f | 2020-03-09 10:56:53 -0700 | [diff] [blame] | 263 | /* pKey owns myecc from now */ |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 264 | if (EC_KEY_check_key(myecc) <= 0) |
| 265 | { |
| 266 | fprintf(stderr, "EC_check_key failed.\n"); |
| 267 | } |
| 268 | } |
| 269 | } |
Ed Tanous | 6ea007a | 2019-02-13 22:48:25 -0800 | [diff] [blame] | 270 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 271 | return pKey; |
| 272 | } |
| 273 | |
| 274 | void initOpenssl() |
| 275 | { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 276 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 277 | SSL_load_error_strings(); |
| 278 | OpenSSL_add_all_algorithms(); |
| 279 | RAND_load_file("/dev/urandom", 1024); |
Ed Tanous | a1e077c | 2017-04-25 12:42:19 -0700 | [diff] [blame] | 280 | #endif |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 283 | inline void ensureOpensslKeyPresentAndValid(const std::string& filepath) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 284 | { |
| 285 | bool pemFileValid = false; |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 286 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 287 | pemFileValid = verifyOpensslKeyCert(filepath); |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 288 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 289 | if (!pemFileValid) |
| 290 | { |
| 291 | std::cerr << "Error in verifying signature, regenerating\n"; |
| 292 | generateSslCertificate(filepath); |
| 293 | } |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 294 | } |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 295 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 296 | inline std::shared_ptr<boost::asio::ssl::context> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 297 | getSslContext(const std::string& ssl_pem_file) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 298 | { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 299 | std::shared_ptr<boost::asio::ssl::context> mSslContext = |
| 300 | std::make_shared<boost::asio::ssl::context>( |
| 301 | boost::asio::ssl::context::tls_server); |
| 302 | mSslContext->set_options(boost::asio::ssl::context::default_workarounds | |
| 303 | boost::asio::ssl::context::no_sslv2 | |
| 304 | boost::asio::ssl::context::no_sslv3 | |
| 305 | boost::asio::ssl::context::single_dh_use | |
| 306 | boost::asio::ssl::context::no_tlsv1 | |
| 307 | boost::asio::ssl::context::no_tlsv1_1); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 308 | |
James Feist | 91243c3 | 2019-11-12 14:55:40 -0800 | [diff] [blame] | 309 | // BIG WARNING: This needs to stay disabled, as there will always be |
| 310 | // unauthenticated endpoints |
| 311 | // mSslContext->set_verify_mode(boost::asio::ssl::verify_peer); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 312 | |
James Feist | 6a3e182 | 2019-11-06 13:46:35 -0800 | [diff] [blame] | 313 | SSL_CTX_set_options(mSslContext->native_handle(), SSL_OP_NO_RENEGOTIATION); |
| 314 | |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 315 | BMCWEB_LOG_DEBUG << "Using default TrustStore location: " << trustStorePath; |
| 316 | mSslContext->add_verify_path(trustStorePath); |
| 317 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 318 | mSslContext->use_certificate_file(ssl_pem_file, |
| 319 | boost::asio::ssl::context::pem); |
| 320 | mSslContext->use_private_key_file(ssl_pem_file, |
| 321 | boost::asio::ssl::context::pem); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 322 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 323 | // Set up EC curves to auto (boost asio doesn't have a method for this) |
| 324 | // There is a pull request to add this. Once this is included in an asio |
| 325 | // drop, use the right way |
| 326 | // http://stackoverflow.com/questions/18929049/boost-asio-with-ecdsa-certificate-issue |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 327 | if (SSL_CTX_set_ecdh_auto(mSslContext->native_handle(), 1) != 1) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 328 | { |
| 329 | BMCWEB_LOG_ERROR << "Error setting tmp ecdh list\n"; |
| 330 | } |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 331 | |
Ed Tanous | 457207f | 2019-02-07 15:27:14 -0800 | [diff] [blame] | 332 | std::string mozillaModern = "ECDHE-ECDSA-AES256-GCM-SHA384:" |
| 333 | "ECDHE-RSA-AES256-GCM-SHA384:" |
| 334 | "ECDHE-ECDSA-CHACHA20-POLY1305:" |
| 335 | "ECDHE-RSA-CHACHA20-POLY1305:" |
| 336 | "ECDHE-ECDSA-AES128-GCM-SHA256:" |
| 337 | "ECDHE-RSA-AES128-GCM-SHA256:" |
| 338 | "ECDHE-ECDSA-AES256-SHA384:" |
| 339 | "ECDHE-RSA-AES256-SHA384:" |
| 340 | "ECDHE-ECDSA-AES128-SHA256:" |
| 341 | "ECDHE-RSA-AES128-SHA256"; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 342 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 343 | if (SSL_CTX_set_cipher_list(mSslContext->native_handle(), |
Ed Tanous | 457207f | 2019-02-07 15:27:14 -0800 | [diff] [blame] | 344 | mozillaModern.c_str()) != 1) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 345 | { |
| 346 | BMCWEB_LOG_ERROR << "Error setting cipher list\n"; |
| 347 | } |
| 348 | return mSslContext; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 349 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 350 | } // namespace ensuressl |
Ed Tanous | 01250f2 | 2017-04-18 17:49:51 -0700 | [diff] [blame] | 351 | |
Brad Bishop | 85d2bb5 | 2019-04-05 08:31:14 -0400 | [diff] [blame] | 352 | #endif |