tls.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef RTC_TLS_H
  9. #define RTC_TLS_H
  10. #include "common.hpp"
  11. #include <chrono>
  12. #if USE_GNUTLS
  13. #include <gnutls/gnutls.h>
  14. #include <gnutls/crypto.h>
  15. #include <gnutls/dtls.h>
  16. #include <gnutls/x509.h>
  17. namespace rtc::gnutls {
  18. bool check(int ret, const string &message = "GnuTLS error");
  19. gnutls_certificate_credentials_t *new_credentials();
  20. void free_credentials(gnutls_certificate_credentials_t *creds);
  21. gnutls_x509_crt_t *new_crt();
  22. void free_crt(gnutls_x509_crt_t *crt);
  23. gnutls_x509_privkey_t *new_privkey();
  24. void free_privkey(gnutls_x509_privkey_t *privkey);
  25. gnutls_datum_t make_datum(char *data, size_t size);
  26. } // namespace rtc::gnutls
  27. #elif USE_MBEDTLS
  28. #include "mbedtls/ctr_drbg.h"
  29. #include "mbedtls/ecdsa.h"
  30. #include "mbedtls/entropy.h"
  31. #include "mbedtls/error.h"
  32. #include "mbedtls/pk.h"
  33. #include "mbedtls/rsa.h"
  34. #include "mbedtls/sha256.h"
  35. #include "mbedtls/ssl.h"
  36. #include "mbedtls/x509_crt.h"
  37. namespace rtc::mbedtls {
  38. void check(int ret, const string &message = "MbedTLS error");
  39. string format_time(const std::chrono::system_clock::time_point &tp);
  40. std::shared_ptr<mbedtls_pk_context> new_pk_context();
  41. std::shared_ptr<mbedtls_x509_crt> new_x509_crt();
  42. } // namespace rtc::mbedtls
  43. #else // OPENSSL
  44. #ifdef _WIN32
  45. // Include winsock2.h header first since OpenSSL may include winsock.h
  46. #include <winsock2.h>
  47. #endif
  48. #include <openssl/ssl.h>
  49. #include <openssl/bio.h>
  50. #include <openssl/err.h>
  51. #include <openssl/pem.h>
  52. #include <openssl/x509.h>
  53. #ifndef BIO_EOF
  54. #define BIO_EOF -1
  55. #endif
  56. namespace rtc::openssl {
  57. void init();
  58. string error_string(unsigned long err);
  59. bool check(int success, const string &message = "OpenSSL error");
  60. bool check(SSL *ssl, int ret, const string &message = "OpenSSL error");
  61. BIO *BIO_new_from_file(const string &filename);
  62. } // namespace rtc::openssl
  63. #endif
  64. #endif