| 83 | |
| 84 | #ifdef BRYNET_USE_OPENSSL |
| 85 | bool initSSL(const std::string& certificate, |
| 86 | const std::string& privatekey) |
| 87 | { |
| 88 | std::call_once(initCryptoThreadSafeSupportOnceFlag, |
| 89 | InitCryptoThreadSafeSupport); |
| 90 | |
| 91 | if (mOpenSSLCTX != nullptr) |
| 92 | { |
| 93 | return false; |
| 94 | } |
| 95 | if (certificate.empty() || privatekey.empty()) |
| 96 | { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | mOpenSSLCTX = SSL_CTX_new(SSLv23_method()); |
| 101 | SSL_CTX_set_client_CA_list(mOpenSSLCTX, |
| 102 | SSL_load_client_CA_file(certificate.c_str())); |
| 103 | SSL_CTX_set_verify_depth(mOpenSSLCTX, 10); |
| 104 | |
| 105 | if (SSL_CTX_use_certificate_chain_file(mOpenSSLCTX, |
| 106 | certificate.c_str()) <= 0) |
| 107 | { |
| 108 | SSL_CTX_free(mOpenSSLCTX); |
| 109 | mOpenSSLCTX = nullptr; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (SSL_CTX_use_PrivateKey_file(mOpenSSLCTX, |
| 114 | privatekey.c_str(), |
| 115 | SSL_FILETYPE_PEM) <= 0) |
| 116 | { |
| 117 | SSL_CTX_free(mOpenSSLCTX); |
| 118 | mOpenSSLCTX = nullptr; |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | if (!SSL_CTX_check_private_key(mOpenSSLCTX)) |
| 123 | { |
| 124 | SSL_CTX_free(mOpenSSLCTX); |
| 125 | mOpenSSLCTX = nullptr; |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | void destroySSL() |
| 133 | { |