History

avformat/tls_openssl: automatically generate self-signed certificate when none is provided in listen mode

Timo Rothenpieler committed 1 year ago

Commit 483e5091 parent 454f1611 file · +30 −5Browse files

Unified · Split

libavformat/tls_openssl.c+30−5
@@ -485,7 +485,6 @@ typedef struct TLSContext { · 484 unmodified lines
485 TLSShared tls_shared;485 TLSShared tls_shared;
486 SSL_CTX *ctx;486 SSL_CTX *ctx;
487 SSL *ssl;487 SSL *ssl;
488 EVP_PKEY *pkey;
489 BIO_METHOD* url_bio_method;488 BIO_METHOD* url_bio_method;
490 int io_err;489 int io_err;
491 char error_message[256];490 char error_message[256];
@@ -756,7 +755,7 @@ static av_cold int openssl_init_ca_key_cert(URLContext *h) · 264 unmodified lines
756 if (SSL_CTX_use_certificate(p->ctx, cert) != 1) {755 if (SSL_CTX_use_certificate(p->ctx, cert) != 1) {
757 av_log(p, AV_LOG_ERROR, "SSL: Init SSL_CTX_use_certificate failed, %s\n", openssl_get_error(p));756 av_log(p, AV_LOG_ERROR, "SSL: Init SSL_CTX_use_certificate failed, %s\n", openssl_get_error(p));
758 ret = AVERROR(EINVAL);757 ret = AVERROR(EINVAL);
759 return ret;758 goto fail;
760 }759 }
761 }760 }
762 761
@@ -769,15 +768,42 @@ static av_cold int openssl_init_ca_key_cert(URLContext *h) · 6 unmodified lines
769 goto fail;768 goto fail;
770 }769 }
771 } else if (c->key_buf) {770 } else if (c->key_buf) {
772 p->pkey = pkey = pkey_from_pem_string(c->key_buf, 1);771 pkey = pkey_from_pem_string(c->key_buf, 1);
773 if (SSL_CTX_use_PrivateKey(p->ctx, pkey) != 1) {772 if (SSL_CTX_use_PrivateKey(p->ctx, pkey) != 1) {
774 av_log(p, AV_LOG_ERROR, "TLS: Init SSL_CTX_use_PrivateKey failed, %s\n", openssl_get_error(p));773 av_log(p, AV_LOG_ERROR, "TLS: Init SSL_CTX_use_PrivateKey failed, %s\n", openssl_get_error(p));
775 ret = AVERROR(EINVAL);774 ret = AVERROR(EINVAL);
776 return ret;775 goto fail;
777 }776 }
778 }777 }
778
779 if (c->listen && !c->cert_file && !c->cert_buf && !c->key_file && !c->key_buf) {
780 av_log(h, AV_LOG_VERBOSE, "No server certificate provided, using self-signed\n");
781
782 ret = openssl_gen_private_key(&pkey);
783 if (ret < 0)
784 goto fail;
785
786 ret = openssl_gen_certificate(pkey, &cert, NULL);
787 if (ret < 0)
788 goto fail;
789
790 if (SSL_CTX_use_certificate(p->ctx, cert) != 1) {
791 av_log(p, AV_LOG_ERROR, "SSL_CTX_use_certificate failed for self-signed cert, %s\n", openssl_get_error(p));
792 ret = AVERROR(EINVAL);
793 goto fail;
794 }
795
796 if (SSL_CTX_use_PrivateKey(p->ctx, pkey) != 1) {
797 av_log(p, AV_LOG_ERROR, "SSL_CTX_use_PrivateKey failed for self-signed cert, %s\n", openssl_get_error(p));
798 ret = AVERROR(EINVAL);
799 goto fail;
800 }
801 }
802
779 ret = 0;803 ret = 0;
780fail:804fail:
805 X509_free(cert);
806 EVP_PKEY_free(pkey);
781 return ret;807 return ret;
782}808}
783 809
@@ -894,7 +920,6 @@ static av_cold int dtls_close(URLContext *h) · 110 unmodified lines
894 SSL_CTX_free(ctx->ctx);920 SSL_CTX_free(ctx->ctx);
895 av_freep(&ctx->tls_shared.cert_buf);921 av_freep(&ctx->tls_shared.cert_buf);
896 av_freep(&ctx->tls_shared.key_buf);922 av_freep(&ctx->tls_shared.key_buf);
897 EVP_PKEY_free(ctx->pkey);
898 return 0;923 return 0;
899}924}
900 925

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t