/
isachkov
/
crypto_server
Обзор
Документация
Войти
/
isachkov
/
crypto_server
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/ssl_provider.cpp
66 строк
2 KB
Ivan Sachkov
CryptoServer
15 май 2024, 20:26
15 май 2024, 20:26
9de0f1f
Код
Авторство
О чём код?
#include <crypto_chat/ssl_provider.hpp> #include <openssl/err.h> #include <iostream> void SSLInstanceDeleter::operator()(SSL* ptr) { SSL_free(ptr); } SSLProvider::~SSLProvider() { if (context_) { SSL_CTX_free(context_); } } bool SSLProvider::init(std::string cert, std::string private_key) { const auto init_status = SSL_library_init(); if (init_status != 1) { std::cerr << "SSL_library_init has failed\n"; return false; } OpenSSL_add_all_algorithms(); SSL_load_error_strings(); ERR_load_crypto_strings(); context_ = SSL_CTX_new(TLS_method()); if (!context_) { std::cerr << "Could not init ssl context\n"; return false; } if (!cert.empty() && !private_key.empty()) { const auto cert_status = SSL_CTX_use_certificate_file(context_, cert.c_str(), SSL_FILETYPE_PEM); if (cert_status != 1) { std::cerr << "Could not load cert file!\n"; return false; } const auto private_key_status = SSL_CTX_use_PrivateKey_file(context_, private_key.c_str(), SSL_FILETYPE_PEM); if (private_key_status != 1) { std::cerr << "Could not load private key!\n"; return false; } const auto check_status = SSL_CTX_check_private_key(context_); if (check_status != 1) { std::cerr << "Private key check has failed!\n"; return false; } } // Uncomment in case of security audit :D // const auto option_status = SSL_CTX_set_options(context_, SSL_OP_ALL); // if(option_status != 1) { // std::cerr << "SSL Ctx option modification has failed! " << option_status << "\n"; // return false; // } return true; } SSLInstancePtr SSLProvider::make_ssl_instance() { return SSLInstancePtr{SSL_new(context_)}; }