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