tls.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Copyright (c) 2019-2020 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "tls.hpp"
  19. #if USE_GNUTLS
  20. namespace rtc::gnutls {
  21. bool check(int ret, const string &message) {
  22. if (ret < 0) {
  23. if (!gnutls_error_is_fatal(ret)) {
  24. PLOG_INFO << gnutls_strerror(ret);
  25. return false;
  26. }
  27. PLOG_ERROR << message << ": " << gnutls_strerror(ret);
  28. throw std::runtime_error(message + ": " + gnutls_strerror(ret));
  29. }
  30. return true;
  31. }
  32. gnutls_certificate_credentials_t *new_credentials() {
  33. auto creds = new gnutls_certificate_credentials_t;
  34. gnutls::check(gnutls_certificate_allocate_credentials(creds));
  35. return creds;
  36. }
  37. void free_credentials(gnutls_certificate_credentials_t *creds) {
  38. gnutls_certificate_free_credentials(*creds);
  39. delete creds;
  40. }
  41. gnutls_x509_crt_t *new_crt() {
  42. auto crt = new gnutls_x509_crt_t;
  43. gnutls::check(gnutls_x509_crt_init(crt));
  44. return crt;
  45. }
  46. void free_crt(gnutls_x509_crt_t *crt) {
  47. gnutls_x509_crt_deinit(*crt);
  48. delete crt;
  49. }
  50. gnutls_x509_privkey_t *new_privkey() {
  51. auto privkey = new gnutls_x509_privkey_t;
  52. gnutls::check(gnutls_x509_privkey_init(privkey));
  53. return privkey;
  54. }
  55. void free_privkey(gnutls_x509_privkey_t *privkey) {
  56. gnutls_x509_privkey_deinit(*privkey);
  57. delete privkey;
  58. }
  59. gnutls_datum_t make_datum(char *data, size_t size) {
  60. gnutls_datum_t datum;
  61. datum.data = reinterpret_cast<unsigned char *>(data);
  62. datum.size = size;
  63. return datum;
  64. }
  65. } // namespace rtc::gnutls
  66. #else // USE_GNUTLS==0
  67. namespace rtc::openssl {
  68. void init() {
  69. static std::mutex mutex;
  70. static bool done = false;
  71. std::lock_guard lock(mutex);
  72. if (!std::exchange(done, true)) {
  73. OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, nullptr);
  74. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, nullptr);
  75. }
  76. }
  77. string error_string(unsigned long err) {
  78. const size_t bufferSize = 256;
  79. char buffer[bufferSize];
  80. ERR_error_string_n(err, buffer, bufferSize);
  81. return string(buffer);
  82. }
  83. bool check(int success, const string &message) {
  84. if (success)
  85. return true;
  86. string str = error_string(ERR_get_error());
  87. PLOG_ERROR << message << ": " << str;
  88. throw std::runtime_error(message + ": " + str);
  89. }
  90. bool check(SSL *ssl, int ret, const string &message) {
  91. if (ret == BIO_EOF)
  92. return true;
  93. unsigned long err = SSL_get_error(ssl, ret);
  94. if (err == SSL_ERROR_NONE || err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
  95. return true;
  96. }
  97. if (err == SSL_ERROR_ZERO_RETURN) {
  98. PLOG_DEBUG << "DTLS connection cleanly closed";
  99. return false;
  100. }
  101. string str = error_string(err);
  102. PLOG_ERROR << str;
  103. throw std::runtime_error(message + ": " + str);
  104. }
  105. } // namespace rtc::openssl
  106. #endif