/* */ #include "LibsslTLSContext.h" #include #include "LogFactory.h" #include "Logger.h" #include "StringFormat.h" #include "message.h" namespace aria2 { TLSContext::TLSContext():_sslCtx(0), _logger(LogFactory::getInstance()) { _sslCtx = SSL_CTX_new(SSLv23_client_method()); if(_sslCtx) { _good = true; } else { _good = false; _logger->error("SSL_CTX_new() failed. Cause: %s", ERR_error_string(ERR_get_error(), 0)); } SSL_CTX_set_mode(_sslCtx, SSL_MODE_AUTO_RETRY); } TLSContext::~TLSContext() { SSL_CTX_free(_sslCtx); } bool TLSContext::good() const { return _good; } bool TLSContext::bad() const { return !_good; } void TLSContext::addClientKeyFile(const std::string& certfile, const std::string& keyfile) throw(DlAbortEx) { if(SSL_CTX_use_PrivateKey_file(_sslCtx, keyfile.c_str(), SSL_FILETYPE_PEM) != 1) { throw DlAbortEx (StringFormat ("Failed to load client private key from %s. Cause: %s", keyfile.c_str(), ERR_error_string(ERR_get_error(), 0)).str()); } if(SSL_CTX_use_certificate_chain_file(_sslCtx, certfile.c_str()) != 1) { throw DlAbortEx (StringFormat ("Failed to load client certificate from %s. Cause: %s", certfile.c_str(), ERR_error_string(ERR_get_error(), 0)).str()); } } void TLSContext::addTrustedCACertFile(const std::string& certfile) throw(DlAbortEx) { if(SSL_CTX_load_verify_locations(_sslCtx, certfile.c_str(), 0) != 1) { throw DlAbortEx (StringFormat (MSG_LOADING_TRUSTED_CA_CERT_FAILED, certfile.c_str(), ERR_error_string(ERR_get_error(), 0)).str()); } } SSL_CTX* TLSContext::getSSLCtx() const { return _sslCtx; } } // namespace aria2