tls.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Copyright (c) 2019-2020 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include "tls.hpp"
  9. #include <fstream>
  10. #include <stdexcept>
  11. #if USE_GNUTLS
  12. namespace rtc::gnutls {
  13. // Return false on non-fatal error
  14. bool check(int ret, const string &message) {
  15. if (ret < 0) {
  16. if (!gnutls_error_is_fatal(ret)) {
  17. return false;
  18. }
  19. throw std::runtime_error(message + ": " + gnutls_strerror(ret));
  20. }
  21. return true;
  22. }
  23. gnutls_certificate_credentials_t *new_credentials() {
  24. auto creds = new gnutls_certificate_credentials_t;
  25. gnutls::check(gnutls_certificate_allocate_credentials(creds));
  26. return creds;
  27. }
  28. void free_credentials(gnutls_certificate_credentials_t *creds) {
  29. gnutls_certificate_free_credentials(*creds);
  30. delete creds;
  31. }
  32. gnutls_x509_crt_t *new_crt() {
  33. auto crt = new gnutls_x509_crt_t;
  34. gnutls::check(gnutls_x509_crt_init(crt));
  35. return crt;
  36. }
  37. void free_crt(gnutls_x509_crt_t *crt) {
  38. gnutls_x509_crt_deinit(*crt);
  39. delete crt;
  40. }
  41. gnutls_x509_privkey_t *new_privkey() {
  42. auto privkey = new gnutls_x509_privkey_t;
  43. gnutls::check(gnutls_x509_privkey_init(privkey));
  44. return privkey;
  45. }
  46. void free_privkey(gnutls_x509_privkey_t *privkey) {
  47. gnutls_x509_privkey_deinit(*privkey);
  48. delete privkey;
  49. }
  50. gnutls_datum_t make_datum(char *data, size_t size) {
  51. gnutls_datum_t datum;
  52. datum.data = reinterpret_cast<unsigned char *>(data);
  53. datum.size = size;
  54. return datum;
  55. }
  56. } // namespace rtc::gnutls
  57. #elif USE_MBEDTLS
  58. #include <time.h>
  59. namespace {
  60. // Safe gmtime
  61. int my_gmtime(const time_t *t, struct tm *buf) {
  62. #ifdef _WIN32
  63. return ::gmtime_s(buf, t) == 0 ? 0 : -1;
  64. #else // POSIX
  65. return ::gmtime_r(t, buf) != NULL ? 0 : -1;
  66. #endif
  67. }
  68. // Format time_t as UTC
  69. size_t my_strftme(char *buf, size_t size, const char *format, const time_t *t) {
  70. struct tm g;
  71. if (my_gmtime(t, &g) != 0)
  72. return 0;
  73. return ::strftime(buf, size, format, &g);
  74. }
  75. } // namespace
  76. namespace rtc::mbedtls {
  77. // Return false on non-fatal error
  78. bool check(int ret, const string &message) {
  79. if (ret < 0) {
  80. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
  81. ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS || ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ||
  82. ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
  83. return false;
  84. const size_t bufferSize = 1024;
  85. char buffer[bufferSize];
  86. mbedtls_strerror(ret, reinterpret_cast<char *>(buffer), bufferSize);
  87. throw std::runtime_error(message + ": " + std::string(buffer));
  88. }
  89. return true;
  90. }
  91. string format_time(const std::chrono::system_clock::time_point &tp) {
  92. time_t t = std::chrono::system_clock::to_time_t(tp);
  93. const size_t bufferSize = 256;
  94. char buffer[bufferSize];
  95. if (my_strftme(buffer, bufferSize, "%Y%m%d%H%M%S", &t) == 0)
  96. throw std::runtime_error("Time conversion failed");
  97. return string(buffer);
  98. };
  99. std::shared_ptr<mbedtls_pk_context> new_pk_context() {
  100. return std::shared_ptr<mbedtls_pk_context>{[]() {
  101. auto p = new mbedtls_pk_context;
  102. mbedtls_pk_init(p);
  103. return p;
  104. }(),
  105. [](mbedtls_pk_context *p) {
  106. mbedtls_pk_free(p);
  107. delete p;
  108. }};
  109. }
  110. std::shared_ptr<mbedtls_x509_crt> new_x509_crt() {
  111. return std::shared_ptr<mbedtls_x509_crt>{[]() {
  112. auto p = new mbedtls_x509_crt;
  113. mbedtls_x509_crt_init(p);
  114. return p;
  115. }(),
  116. [](mbedtls_x509_crt *crt) {
  117. mbedtls_x509_crt_free(crt);
  118. delete crt;
  119. }};
  120. }
  121. } // namespace rtc::mbedtls
  122. #else // OPENSSL
  123. namespace rtc::openssl {
  124. void init() {
  125. static std::mutex mutex;
  126. static bool done = false;
  127. std::lock_guard lock(mutex);
  128. if (!std::exchange(done, true)) {
  129. OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, nullptr);
  130. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, nullptr);
  131. }
  132. }
  133. string error_string(unsigned long err) {
  134. const size_t bufferSize = 256;
  135. char buffer[bufferSize];
  136. ERR_error_string_n(err, buffer, bufferSize);
  137. return string(buffer);
  138. }
  139. bool check(int success, const string &message) {
  140. if (success)
  141. return true;
  142. string str = error_string(ERR_get_error());
  143. throw std::runtime_error(message + ": " + str);
  144. }
  145. // Return false on EOF
  146. bool check(SSL *ssl, int ret, const string &message) {
  147. unsigned long err = SSL_get_error(ssl, ret);
  148. if (err == SSL_ERROR_NONE || err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
  149. return true;
  150. }
  151. if (err == SSL_ERROR_ZERO_RETURN) {
  152. return false;
  153. }
  154. string str = error_string(err);
  155. throw std::runtime_error(message + ": " + str);
  156. }
  157. BIO *BIO_new_from_file(const string &filename) {
  158. BIO *bio = nullptr;
  159. try {
  160. std::ifstream ifs(filename, std::ifstream::in | std::ifstream::binary);
  161. if (!ifs.is_open())
  162. return nullptr;
  163. bio = BIO_new(BIO_s_mem());
  164. const size_t bufferSize = 4096;
  165. char buffer[bufferSize];
  166. while (ifs.good()) {
  167. ifs.read(buffer, bufferSize);
  168. BIO_write(bio, buffer, int(ifs.gcount()));
  169. }
  170. ifs.close();
  171. return bio;
  172. } catch (const std::exception &) {
  173. BIO_free(bio);
  174. return nullptr;
  175. }
  176. }
  177. } // namespace rtc::openssl
  178. #endif