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
485485 TLSShared tls_shared;
486486 SSL_CTX *ctx;
487487 SSL *ssl;
488− EVP_PKEY *pkey;
489488 BIO_METHOD* url_bio_method;
490489 int io_err;
491490 char error_message[256];
@@ -756,7 +755,7 @@ static av_cold int openssl_init_ca_key_cert(URLContext *h) · 264 unmodified lines
756755 if (SSL_CTX_use_certificate(p->ctx, cert) != 1) {
757756 av_log(p, AV_LOG_ERROR, "SSL: Init SSL_CTX_use_certificate failed, %s\n", openssl_get_error(p));
758757 ret = AVERROR(EINVAL);
759− return ret;
758+ goto fail;
760759 }
761760 }
762761
@@ -769,15 +768,42 @@ static av_cold int openssl_init_ca_key_cert(URLContext *h) · 6 unmodified lines
769768 goto fail;
770769 }
771770 } 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);
773772 if (SSL_CTX_use_PrivateKey(p->ctx, pkey) != 1) {
774773 av_log(p, AV_LOG_ERROR, "TLS: Init SSL_CTX_use_PrivateKey failed, %s\n", openssl_get_error(p));
775774 ret = AVERROR(EINVAL);
776− return ret;
775+ goto fail;
777776 }
778777 }
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+
779803 ret = 0;
780804 fail:
805+ X509_free(cert);
806+ EVP_PKEY_free(pkey);
781807 return ret;
782808 }
783809
@@ -894,7 +920,6 @@ static av_cold int dtls_close(URLContext *h) · 110 unmodified lines
894920 SSL_CTX_free(ctx->ctx);
895921 av_freep(&ctx->tls_shared.cert_buf);
896922 av_freep(&ctx->tls_shared.key_buf);
897− EVP_PKEY_free(ctx->pkey);
898923 return 0;
899924 }
900925

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