LibgnutlsTLSContext.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "LibgnutlsTLSContext.h"
  36. #include <sstream>
  37. #ifdef HAVE_LIBGNUTLS
  38. # include <gnutls/x509.h>
  39. # include <gnutls/pkcs12.h>
  40. #endif // HAVE_LIBGNUTLS
  41. #include "LogFactory.h"
  42. #include "Logger.h"
  43. #include "fmt.h"
  44. #include "message.h"
  45. #include "BufferedFile.h"
  46. namespace aria2 {
  47. TLSContext* TLSContext::make(TLSSessionSide side)
  48. {
  49. return new GnuTLSContext(side);
  50. }
  51. GnuTLSContext::GnuTLSContext(TLSSessionSide side)
  52. : certCred_(0),
  53. side_(side),
  54. verifyPeer_(true)
  55. {
  56. int r = gnutls_certificate_allocate_credentials(&certCred_);
  57. if(r == GNUTLS_E_SUCCESS) {
  58. good_ = true;
  59. gnutls_certificate_set_verify_flags(certCred_,
  60. GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
  61. } else {
  62. good_ =false;
  63. A2_LOG_ERROR(fmt("gnutls_certificate_allocate_credentials() failed."
  64. " Cause: %s",
  65. gnutls_strerror(r)));
  66. }
  67. }
  68. GnuTLSContext::~GnuTLSContext()
  69. {
  70. if(certCred_) {
  71. gnutls_certificate_free_credentials(certCred_);
  72. }
  73. }
  74. bool GnuTLSContext::good() const
  75. {
  76. return good_;
  77. }
  78. bool GnuTLSContext::addCredentialFile(const std::string& certfile,
  79. const std::string& keyfile)
  80. {
  81. if (keyfile.empty()) {
  82. return addP12CredentialFile(certfile);
  83. }
  84. int ret = gnutls_certificate_set_x509_key_file(certCred_,
  85. certfile.c_str(),
  86. keyfile.c_str(),
  87. GNUTLS_X509_FMT_PEM);
  88. if(ret == GNUTLS_E_SUCCESS) {
  89. A2_LOG_INFO(fmt
  90. ("Credential files(cert=%s, key=%s) were successfully added.",
  91. certfile.c_str(), keyfile.c_str()));
  92. return true;
  93. } else {
  94. A2_LOG_ERROR(fmt("Failed to load certificate from %s and"
  95. " private key from %s. Cause: %s",
  96. certfile.c_str(), keyfile.c_str(),
  97. gnutls_strerror(ret)));
  98. return false;
  99. }
  100. }
  101. bool GnuTLSContext::addP12CredentialFile(const std::string& p12file)
  102. {
  103. std::stringstream ss;
  104. BufferedFile(p12file.c_str(), BufferedFile::READ).transfer(ss);
  105. auto datastr = ss.str();
  106. const gnutls_datum_t data = {
  107. (unsigned char*)datastr.c_str(),
  108. (unsigned int)datastr.length()
  109. };
  110. int err = gnutls_certificate_set_x509_simple_pkcs12_mem(
  111. certCred_, &data, GNUTLS_X509_FMT_DER, "");
  112. if (err != GNUTLS_E_SUCCESS) {
  113. A2_LOG_ERROR("Failed to import PKCS12 file. "
  114. "If you meant to use PEM, you'll also have to specify "
  115. "--rpc-private-key. See the manual.");
  116. return false;
  117. }
  118. return true;
  119. }
  120. bool GnuTLSContext::addSystemTrustedCACerts()
  121. {
  122. #ifdef HAVE_GNUTLS_CERTIFICATE_SET_X509_SYSTEM_TRUST
  123. int ret = gnutls_certificate_set_x509_system_trust(certCred_);
  124. if(ret < 0) {
  125. A2_LOG_INFO(fmt(MSG_LOADING_SYSTEM_TRUSTED_CA_CERTS_FAILED,
  126. gnutls_strerror(ret)));
  127. return false;
  128. } else {
  129. A2_LOG_INFO(fmt("%d certificate(s) were imported.", ret));
  130. return true;
  131. }
  132. #else
  133. A2_LOG_INFO("System certificates not supported");
  134. return false;
  135. #endif
  136. }
  137. bool GnuTLSContext::addTrustedCACertFile(const std::string& certfile)
  138. {
  139. int ret = gnutls_certificate_set_x509_trust_file(certCred_,
  140. certfile.c_str(),
  141. GNUTLS_X509_FMT_PEM);
  142. if(ret < 0) {
  143. A2_LOG_ERROR(fmt(MSG_LOADING_TRUSTED_CA_CERT_FAILED,
  144. certfile.c_str(), gnutls_strerror(ret)));
  145. return false;
  146. } else {
  147. A2_LOG_INFO(fmt("%d certificate(s) were imported.", ret));
  148. return true;
  149. }
  150. }
  151. gnutls_certificate_credentials_t GnuTLSContext::getCertCred() const
  152. {
  153. return certCred_;
  154. }
  155. } // namespace aria2