修复https证书为空时的异常问题

This commit is contained in:
xia-chu 2023-10-25 17:49:31 +08:00
parent 0d0b32f5e9
commit 8294b7a175
3 changed files with 9 additions and 23 deletions

View File

@ -132,14 +132,9 @@ namespace RTC
// Generate a X509 certificate and private key (unless PEM files are provided). // Generate a X509 certificate and private key (unless PEM files are provided).
auto ssl = toolkit::SSL_Initor::Instance().getSSLCtx("", true); auto ssl = toolkit::SSL_Initor::Instance().getSSLCtx("", true);
if (!ssl) if (!ssl || !ReadCertificateAndPrivateKeyFromContext(ssl.get())) {
{
GenerateCertificateAndPrivateKey(); GenerateCertificateAndPrivateKey();
} }
else
{
ReadCertificateAndPrivateKeyFromContext(ssl.get());
}
// Create a global SSL_CTX. // Create a global SSL_CTX.
CreateSslCtx(); CreateSslCtx();
@ -298,31 +293,22 @@ namespace RTC
MS_THROW_ERROR("DTLS certificate and private key generation failed"); MS_THROW_ERROR("DTLS certificate and private key generation failed");
} }
void DtlsTransport::DtlsEnvironment::ReadCertificateAndPrivateKeyFromContext(SSL_CTX *ctx) bool DtlsTransport::DtlsEnvironment::ReadCertificateAndPrivateKeyFromContext(SSL_CTX *ctx)
{ {
MS_TRACE(); MS_TRACE();
certificate = SSL_CTX_get0_certificate(ctx); certificate = SSL_CTX_get0_certificate(ctx);
if (!certificate) if (!certificate) {
{ return false;
LOG_OPENSSL_ERROR("SSL_CTX_get0_certificate() failed");
goto error;
} }
X509_up_ref(certificate); X509_up_ref(certificate);
privateKey = SSL_CTX_get0_privatekey(ctx); privateKey = SSL_CTX_get0_privatekey(ctx);
if (!privateKey) if (!privateKey) {
{ return false;
LOG_OPENSSL_ERROR("SSL_CTX_get0_privatekey() failed");
goto error;
} }
EVP_PKEY_up_ref(privateKey); EVP_PKEY_up_ref(privateKey);
InfoL << "Load webrtc dtls certificate: " << toolkit::SSLUtil::getServerName(certificate); InfoL << "Load webrtc dtls certificate: " << toolkit::SSLUtil::getServerName(certificate);
return; return true;
error:
MS_THROW_ERROR("error reading DTLS certificate and private key PEM files");
} }
void DtlsTransport::DtlsEnvironment::CreateSslCtx() void DtlsTransport::DtlsEnvironment::CreateSslCtx()

View File

@ -88,7 +88,7 @@ namespace RTC
private: private:
DtlsEnvironment(); DtlsEnvironment();
void GenerateCertificateAndPrivateKey(); void GenerateCertificateAndPrivateKey();
void ReadCertificateAndPrivateKeyFromContext(SSL_CTX *ctx); bool ReadCertificateAndPrivateKeyFromContext(SSL_CTX *ctx);
void CreateSslCtx(); void CreateSslCtx();
void GenerateFingerprints(); void GenerateFingerprints();

View File

@ -251,7 +251,7 @@ void WebRtcTransport::sendSockData(const char *buf, size_t len, RTC::TransportTu
} }
Session::Ptr WebRtcTransport::getSession() const { Session::Ptr WebRtcTransport::getSession() const {
auto tuple = _ice_server->GetSelectedTuple(true); auto tuple = _ice_server ? _ice_server->GetSelectedTuple(true) : nullptr;
return tuple ? static_pointer_cast<Session>(tuple->shared_from_this()) : nullptr; return tuple ? static_pointer_cast<Session>(tuple->shared_from_this()) : nullptr;
} }