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