certificate.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Copyright (c) 2019 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_IMPL_CERTIFICATE_H
  9. #define RTC_IMPL_CERTIFICATE_H
  10. #include "description.hpp" // for CertificateFingerprint
  11. #include "common.hpp"
  12. #include "configuration.hpp" // for CertificateType
  13. #include "init.hpp"
  14. #include "tls.hpp"
  15. #include <future>
  16. #include <tuple>
  17. namespace rtc::impl {
  18. class Certificate {
  19. public:
  20. static Certificate FromString(string crt_pem, string key_pem);
  21. static Certificate FromFile(const string &crt_pem_file, const string &key_pem_file,
  22. const string &pass = "");
  23. static Certificate Generate(CertificateType type, const string &commonName);
  24. #if USE_GNUTLS
  25. Certificate(gnutls_x509_crt_t crt, gnutls_x509_privkey_t privkey);
  26. gnutls_certificate_credentials_t credentials() const;
  27. #elif USE_MBEDTLS
  28. Certificate(shared_ptr<mbedtls_x509_crt> crt, shared_ptr<mbedtls_pk_context> pk);
  29. std::tuple<shared_ptr<mbedtls_x509_crt>, shared_ptr<mbedtls_pk_context>> credentials() const;
  30. #else // OPENSSL
  31. Certificate(shared_ptr<X509> x509, shared_ptr<EVP_PKEY> pkey, std::vector<shared_ptr<X509>> chain = {});
  32. std::tuple<X509 *, EVP_PKEY *> credentials() const;
  33. std::vector<X509 *> chain() const;
  34. #endif
  35. CertificateFingerprint fingerprint() const;
  36. private:
  37. const init_token mInitToken = Init::Instance().token();
  38. #if USE_GNUTLS
  39. Certificate(shared_ptr<gnutls_certificate_credentials_t> creds);
  40. const shared_ptr<gnutls_certificate_credentials_t> mCredentials;
  41. #elif USE_MBEDTLS
  42. const shared_ptr<mbedtls_x509_crt> mCrt;
  43. const shared_ptr<mbedtls_pk_context> mPk;
  44. #else
  45. const shared_ptr<X509> mX509;
  46. const shared_ptr<EVP_PKEY> mPKey;
  47. const std::vector<shared_ptr<X509>> mChain;
  48. #endif
  49. const string mFingerprint;
  50. };
  51. #if USE_GNUTLS
  52. string make_fingerprint(gnutls_certificate_credentials_t credentials, CertificateFingerprint::Algorithm fingerprintAlgorithm);
  53. string make_fingerprint(gnutls_x509_crt_t crt, CertificateFingerprint::Algorithm fingerprintAlgorithm);
  54. #elif USE_MBEDTLS
  55. string make_fingerprint(mbedtls_x509_crt *crt, CertificateFingerprint::Algorithm fingerprintAlgorithm);
  56. #else
  57. string make_fingerprint(X509 *x509, CertificateFingerprint::Algorithm fingerprintAlgorithm);
  58. #endif
  59. using certificate_ptr = shared_ptr<Certificate>;
  60. using future_certificate_ptr = std::shared_future<certificate_ptr>;
  61. future_certificate_ptr make_certificate(CertificateType type = CertificateType::Default);
  62. } // namespace rtc::impl
  63. #endif