diff --git a/webrtc/DtlsTransport.cpp b/webrtc/DtlsTransport.cpp index 66183f8e..de6c18d1 100644 --- a/webrtc/DtlsTransport.cpp +++ b/webrtc/DtlsTransport.cpp @@ -29,6 +29,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include // std::sprintf(), std::fopen() #include // std::memcpy(), std::strcmp() #include "Util/util.h" +#include "Util/SSLBox.h" using namespace std; @@ -129,15 +130,14 @@ namespace RTC MS_TRACE(); // Generate a X509 certificate and private key (unless PEM files are provided). - if (true /* - Settings::configuration.dtlsCertificateFile.empty() || - Settings::configuration.dtlsPrivateKeyFile.empty()*/) + auto ssl = toolkit::SSL_Initor::Instance().getSSLCtx("", true); + if (!ssl) { GenerateCertificateAndPrivateKey(); } else { - ReadCertificateAndPrivateKeyFromFiles(); + ReadCertificateAndPrivateKeyFromContext(ssl.get()); } // Create a global SSL_CTX. @@ -297,59 +297,29 @@ namespace RTC MS_THROW_ERROR("DTLS certificate and private key generation failed"); } - void DtlsTransport::DtlsEnvironment::ReadCertificateAndPrivateKeyFromFiles() + void DtlsTransport::DtlsEnvironment::ReadCertificateAndPrivateKeyFromContext(SSL_CTX *ctx) { -#if 0 MS_TRACE(); - FILE* file{ nullptr }; - - file = fopen(Settings::configuration.dtlsCertificateFile.c_str(), "r"); - - if (!file) - { - MS_ERROR("error reading DTLS certificate file: %s", std::strerror(errno)); - - goto error; - } - - certificate = PEM_read_X509(file, nullptr, nullptr, nullptr); - + certificate = SSL_CTX_get0_certificate(ctx); if (!certificate) { - LOG_OPENSSL_ERROR("PEM_read_X509() failed"); - + LOG_OPENSSL_ERROR("SSL_CTX_get0_certificate() failed"); goto error; } + X509_up_ref(certificate); - fclose(file); - - file = fopen(Settings::configuration.dtlsPrivateKeyFile.c_str(), "r"); - - if (!file) - { - MS_ERROR("error reading DTLS private key file: %s", std::strerror(errno)); - - goto error; - } - - privateKey = PEM_read_PrivateKey(file, nullptr, nullptr, nullptr); - + privateKey = SSL_CTX_get0_privatekey(ctx); if (!privateKey) { - LOG_OPENSSL_ERROR("PEM_read_PrivateKey() failed"); - + LOG_OPENSSL_ERROR("SSL_CTX_get0_privatekey() failed"); goto error; } - - fclose(file); - + EVP_PKEY_up_ref(privateKey); return; error: - MS_THROW_ERROR("error reading DTLS certificate and private key PEM files"); -#endif } void DtlsTransport::DtlsEnvironment::CreateSslCtx() diff --git a/webrtc/DtlsTransport.hpp b/webrtc/DtlsTransport.hpp index bf57d01d..34fc8fc7 100644 --- a/webrtc/DtlsTransport.hpp +++ b/webrtc/DtlsTransport.hpp @@ -88,7 +88,7 @@ namespace RTC private: DtlsEnvironment(); void GenerateCertificateAndPrivateKey(); - void ReadCertificateAndPrivateKeyFromFiles(); + void ReadCertificateAndPrivateKeyFromContext(SSL_CTX *ctx); void CreateSslCtx(); void GenerateFingerprints();