Skip to content

Commit 4611ed5

Browse files
JackLau1222michaelni
authored andcommitted
avformat/tls_openssl: fix build error when openssl version < 3
add the missing data structure pkey in the tls_context properly set this pkey and free it Signed-off-by: Jack Lau <[email protected]> Reviewed-by: Martin Storsjö <[email protected]> Signed-off-by: Michael Niedermayer <[email protected]>
1 parent d811966 commit 4611ed5

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

libavformat/tls_openssl.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ typedef struct TLSContext {
467467
TLSShared tls_shared;
468468
SSL_CTX *ctx;
469469
SSL *ssl;
470+
EVP_PKEY *pkey;
470471
#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
471472
BIO_METHOD* url_bio_method;
472473
#endif
@@ -849,7 +850,7 @@ static av_cold int openssl_init_ca_key_cert(URLContext *h)
849850
goto fail;
850851
}
851852
} else if (p->tls_shared.key_buf) {
852-
pkey = pkey_from_pem_string(p->tls_shared.key_buf, 1);
853+
p->pkey = pkey = pkey_from_pem_string(p->tls_shared.key_buf, 1);
853854
if (SSL_CTX_use_PrivateKey(p->ctx, pkey) != 1) {
854855
av_log(p, AV_LOG_ERROR, "TLS: Init SSL_CTX_use_PrivateKey failed, %s\n", openssl_get_error(p));
855856
ret = AVERROR(EINVAL);
@@ -876,6 +877,9 @@ static int dtls_start(URLContext *h, const char *url, int flags, AVDictionary **
876877
int ret = 0;
877878
c->is_dtls = 1;
878879
const char* ciphers = "ALL";
880+
#if OPENSSL_VERSION_NUMBER < 0x10002000L // v1.0.2
881+
EC_KEY *ec_key = NULL;
882+
#endif
879883
/**
880884
* The profile for OpenSSL's SRTP is SRTP_AES128_CM_SHA1_80, see ssl/d1_srtp.c.
881885
* The profile for FFmpeg's SRTP is SRTP_AES128_CM_HMAC_SHA1_80, see libavformat/srtp.c.
@@ -908,15 +912,6 @@ static int dtls_start(URLContext *h, const char *url, int flags, AVDictionary **
908912
}
909913
#endif
910914

911-
#if OPENSSL_VERSION_NUMBER < 0x10100000L // v1.1.x
912-
#if OPENSSL_VERSION_NUMBER < 0x10002000L // v1.0.2
913-
if (ctx->dtls_eckey)
914-
SSL_CTX_set_tmp_ecdh(p->ctx, p->dtls_eckey);
915-
#else
916-
SSL_CTX_set_ecdh_auto(p->ctx, 1);
917-
#endif
918-
#endif
919-
920915
/**
921916
* We activate "ALL" cipher suites to align with the peer's capabilities,
922917
* ensuring maximum compatibility.
@@ -930,6 +925,17 @@ static int dtls_start(URLContext *h, const char *url, int flags, AVDictionary **
930925
ret = openssl_init_ca_key_cert(h);
931926
if (ret < 0) goto fail;
932927

928+
#if OPENSSL_VERSION_NUMBER < 0x10100000L // v1.1.x
929+
#if OPENSSL_VERSION_NUMBER < 0x10002000L // v1.0.2
930+
if (p->pkey)
931+
ec_key = EVP_PKEY_get1_EC_KEY(p->pkey);
932+
if (ec_key)
933+
SSL_CTX_set_tmp_ecdh(p->ctx, ec_key);
934+
#else
935+
SSL_CTX_set_ecdh_auto(p->ctx, 1);
936+
#endif
937+
#endif
938+
933939
/* Server will send Certificate Request. */
934940
SSL_CTX_set_verify(p->ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, openssl_dtls_verify_callback);
935941
/* The depth count is "level 0:peer certificate", "level 1: CA certificate",
@@ -1001,6 +1007,9 @@ static int dtls_start(URLContext *h, const char *url, int flags, AVDictionary **
10011007

10021008
ret = 0;
10031009
fail:
1010+
#if OPENSSL_VERSION_NUMBER < 0x10002000L // v1.0.2
1011+
EC_KEY_free(ec_key);
1012+
#endif
10041013
return ret;
10051014
}
10061015

@@ -1015,9 +1024,7 @@ static av_cold int dtls_close(URLContext *h)
10151024
av_freep(&ctx->tls_shared.fingerprint);
10161025
av_freep(&ctx->tls_shared.cert_buf);
10171026
av_freep(&ctx->tls_shared.key_buf);
1018-
#if OPENSSL_VERSION_NUMBER < 0x30000000L /* OpenSSL 3.0 */
1019-
EC_KEY_free(ctx->dtls_eckey);
1020-
#endif
1027+
EVP_PKEY_free(ctx->pkey);
10211028
return 0;
10221029
}
10231030

0 commit comments

Comments
 (0)