/* */ #include "LibgnutlsTLSContext.h" #ifdef HAVE_LIBGNUTLS # include #endif // HAVE_LIBGNUTLS #include "LogFactory.h" #include "Logger.h" #include "StringFormat.h" #include "message.h" namespace aria2 { TLSContext::TLSContext():_certCred(0), _peerVerificationEnabled(false), _logger(LogFactory::getInstance()) { int r = gnutls_certificate_allocate_credentials(&_certCred); if(r == GNUTLS_E_SUCCESS) { _good = true; gnutls_certificate_set_verify_flags(_certCred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT); } else { _good =false; _logger->error("gnutls_certificate_allocate_credentials() failed." " Cause: %s", gnutls_strerror(r)); } } TLSContext::~TLSContext() { if(_certCred) { gnutls_certificate_free_credentials(_certCred); } } 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) { int ret = gnutls_certificate_set_x509_key_file(_certCred, certfile.c_str(), keyfile.c_str(), GNUTLS_X509_FMT_PEM); if(ret != GNUTLS_E_SUCCESS) { throw DL_ABORT_EX (StringFormat("Failed to load client certificate from %s and" " private key from %s. Cause: %s", certfile.c_str(), keyfile.c_str(), gnutls_strerror(ret)).str()); } } void TLSContext::addTrustedCACertFile(const std::string& certfile) throw(DlAbortEx) { int ret = gnutls_certificate_set_x509_trust_file(_certCred, certfile.c_str(), GNUTLS_X509_FMT_PEM); if(ret < 0) { throw DL_ABORT_EX (StringFormat (MSG_LOADING_TRUSTED_CA_CERT_FAILED, certfile.c_str(), gnutls_strerror(ret)).str()); } _logger->info("%d certificate(s) were imported.", ret); } gnutls_certificate_credentials_t TLSContext::getCertCred() const { return _certCred; } void TLSContext::enablePeerVerification() { _peerVerificationEnabled = true; } void TLSContext::disablePeerVerification() { _peerVerificationEnabled = false; } bool TLSContext::peerVerificationEnabled() const { return _peerVerificationEnabled; } } // namespace aria2