@@ -730,15 +730,17 @@ static av_cold int openssl_dtls_init_context(DTLSContext *ctx)
730
730
dtls_ctx = ctx -> dtls_ctx = SSL_CTX_new (DTLS_method ());
731
731
#endif
732
732
if (!dtls_ctx ) {
733
- return AVERROR (ENOMEM );
733
+ ret = AVERROR (ENOMEM );
734
+ goto end ;
734
735
}
735
736
736
737
#if OPENSSL_VERSION_NUMBER >= 0x10002000L /* OpenSSL 1.0.2 */
737
738
/* For ECDSA, we could set the curves list. */
738
739
if (SSL_CTX_set1_curves_list (dtls_ctx , curves ) != 1 ) {
739
740
av_log (ctx , AV_LOG_ERROR , "DTLS: Init SSL_CTX_set1_curves_list failed, curves=%s, %s\n" ,
740
741
curves , openssl_get_error (ctx ));
741
- return AVERROR (EINVAL );
742
+ ret = AVERROR (EINVAL );
743
+ return ret ;
742
744
}
743
745
#endif
744
746
@@ -758,16 +760,19 @@ static av_cold int openssl_dtls_init_context(DTLSContext *ctx)
758
760
if (SSL_CTX_set_cipher_list (dtls_ctx , ciphers ) != 1 ) {
759
761
av_log (ctx , AV_LOG_ERROR , "DTLS: Init SSL_CTX_set_cipher_list failed, ciphers=%s, %s\n" ,
760
762
ciphers , openssl_get_error (ctx ));
761
- return AVERROR (EINVAL );
763
+ ret = AVERROR (EINVAL );
764
+ return ret ;
762
765
}
763
766
/* Setup the certificate. */
764
767
if (SSL_CTX_use_certificate (dtls_ctx , dtls_cert ) != 1 ) {
765
768
av_log (ctx , AV_LOG_ERROR , "DTLS: Init SSL_CTX_use_certificate failed, %s\n" , openssl_get_error (ctx ));
766
- return AVERROR (EINVAL );
769
+ ret = AVERROR (EINVAL );
770
+ return ret ;
767
771
}
768
772
if (SSL_CTX_use_PrivateKey (dtls_ctx , dtls_pkey ) != 1 ) {
769
773
av_log (ctx , AV_LOG_ERROR , "DTLS: Init SSL_CTX_use_PrivateKey failed, %s\n" , openssl_get_error (ctx ));
770
- return AVERROR (EINVAL );
774
+ ret = AVERROR (EINVAL );
775
+ return ret ;
771
776
}
772
777
773
778
/* Server will send Certificate Request. */
@@ -781,13 +786,15 @@ static av_cold int openssl_dtls_init_context(DTLSContext *ctx)
781
786
if (SSL_CTX_set_tlsext_use_srtp (dtls_ctx , profiles )) {
782
787
av_log (ctx , AV_LOG_ERROR , "DTLS: Init SSL_CTX_set_tlsext_use_srtp failed, profiles=%s, %s\n" ,
783
788
profiles , openssl_get_error (ctx ));
784
- return AVERROR (EINVAL );
789
+ ret = AVERROR (EINVAL );
790
+ return ret ;
785
791
}
786
792
787
793
/* The dtls should not be created unless the dtls_ctx has been initialized. */
788
794
dtls = ctx -> dtls = SSL_new (dtls_ctx );
789
795
if (!dtls ) {
790
- return AVERROR (ENOMEM );
796
+ ret = AVERROR (ENOMEM );
797
+ goto end ;
791
798
}
792
799
793
800
/* Setup the callback for logging. */
@@ -804,14 +811,16 @@ static av_cold int openssl_dtls_init_context(DTLSContext *ctx)
804
811
DTLS_set_link_mtu (dtls , ctx -> mtu );
805
812
#endif
806
813
807
- bio_in = ctx -> bio_in = BIO_new (BIO_s_mem ());
814
+ bio_in = BIO_new (BIO_s_mem ());
808
815
if (!bio_in ) {
809
- return AVERROR (ENOMEM );
816
+ ret = AVERROR (ENOMEM );
817
+ goto end ;
810
818
}
811
819
812
820
bio_out = BIO_new (BIO_s_mem ());
813
821
if (!bio_out ) {
814
- return AVERROR (ENOMEM );
822
+ ret = AVERROR (ENOMEM );
823
+ goto end ;
815
824
}
816
825
817
826
/**
@@ -835,8 +844,14 @@ static av_cold int openssl_dtls_init_context(DTLSContext *ctx)
835
844
#endif
836
845
BIO_set_callback_arg (bio_out , (char * )ctx );
837
846
847
+ ctx -> bio_in = bio_in ;
838
848
SSL_set_bio (dtls , bio_in , bio_out );
849
+ /* Now the bio_in and bio_out are owned by dtls, so we should set them to NULL. */
850
+ bio_in = bio_out = NULL ;
839
851
852
+ end :
853
+ BIO_free (bio_in );
854
+ BIO_free (bio_out );
840
855
return ret ;
841
856
}
842
857
0 commit comments