From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id EC4FB41145 for ; Sun, 13 Jul 2025 19:26:51 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id E113968E345; Sun, 13 Jul 2025 22:25:41 +0300 (EEST) Received: from btbn.de (btbn.de [144.76.60.213]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id CCAFC68E310 for ; Sun, 13 Jul 2025 22:25:28 +0300 (EEST) Received: from [authenticated] by btbn.de (Postfix) with ESMTPSA id A1A4427FD3642; Sun, 13 Jul 2025 21:25:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rothenpieler.org; s=mail; t=1752434725; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=inGLhTSVugxS2hh+qoNo+jG7qJEviVeR7BF275trbnE=; b=p6KR99H23eh0Ne/OLQjZxMVn1euUkxDI9e8l5ybjwlIVLKzAy1oh5NGJ5xuPtgQU8RFY78 Xflj04GTottkDRbRYcd1LMypZt+pVVWa2lxvzcAeNCJV2e+cIrc1Wd7Buv9dF2MMWWC2wj TmhkGANfi7JqX1sXWnZoEEoY+hvUTjCEF3RY7jRuNyJfs3OZ4+ZND0eiFvYC9edWLGmaSl Q8U5eAdPpCeIfx5RVeRpiKucrLNf/aKneJcn/BduEObb6DziMvlwE7BUEjx5bHLfyak9EE q73jXJoU6QqaMCeOYAJe8+LJQUQ57ebbO3Yl9Tff1BqpBPKiwNQqr17HLBWy6g== From: Timo Rothenpieler To: ffmpeg-devel@ffmpeg.org Date: Sun, 13 Jul 2025 21:24:48 +0200 Message-ID: <20250713192512.928390-14-timo@rothenpieler.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250713192512.928390-1-timo@rothenpieler.org> References: <20250713192512.928390-1-timo@rothenpieler.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 14/14] avformat/tls_openssl: automatically generate self-signed certificate when none is provided in listen mode X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Timo Rothenpieler Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: --- libavformat/tls_openssl.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/libavformat/tls_openssl.c b/libavformat/tls_openssl.c index 48d8edb08a..07d1af40d8 100644 --- a/libavformat/tls_openssl.c +++ b/libavformat/tls_openssl.c @@ -485,7 +485,6 @@ typedef struct TLSContext { TLSShared tls_shared; SSL_CTX *ctx; SSL *ssl; - EVP_PKEY *pkey; BIO_METHOD* url_bio_method; int io_err; char error_message[256]; @@ -756,7 +755,7 @@ static av_cold int openssl_init_ca_key_cert(URLContext *h) if (SSL_CTX_use_certificate(p->ctx, cert) != 1) { av_log(p, AV_LOG_ERROR, "SSL: Init SSL_CTX_use_certificate failed, %s\n", openssl_get_error(p)); ret = AVERROR(EINVAL); - return ret; + goto fail; } } @@ -769,15 +768,42 @@ static av_cold int openssl_init_ca_key_cert(URLContext *h) goto fail; } } else if (c->key_buf) { - p->pkey = pkey = pkey_from_pem_string(c->key_buf, 1); + pkey = pkey_from_pem_string(c->key_buf, 1); if (SSL_CTX_use_PrivateKey(p->ctx, pkey) != 1) { av_log(p, AV_LOG_ERROR, "TLS: Init SSL_CTX_use_PrivateKey failed, %s\n", openssl_get_error(p)); ret = AVERROR(EINVAL); - return ret; + goto fail; } } + + if (c->listen && !c->cert_file && !c->cert_buf && !c->key_file && !c->key_buf) { + av_log(h, AV_LOG_VERBOSE, "No server certificate provided, using self-signed\n"); + + ret = openssl_gen_private_key(&pkey); + if (ret < 0) + goto fail; + + ret = openssl_gen_certificate(pkey, &cert, NULL); + if (ret < 0) + goto fail; + + if (SSL_CTX_use_certificate(p->ctx, cert) != 1) { + av_log(p, AV_LOG_ERROR, "SSL_CTX_use_certificate failed for self-signed cert, %s\n", openssl_get_error(p)); + ret = AVERROR(EINVAL); + goto fail; + } + + if (SSL_CTX_use_PrivateKey(p->ctx, pkey) != 1) { + av_log(p, AV_LOG_ERROR, "SSL_CTX_use_PrivateKey failed for self-signed cert, %s\n", openssl_get_error(p)); + ret = AVERROR(EINVAL); + goto fail; + } + } + ret = 0; fail: + X509_free(cert); + EVP_PKEY_free(pkey); return ret; } @@ -894,7 +920,6 @@ static av_cold int dtls_close(URLContext *h) SSL_CTX_free(ctx->ctx); av_freep(&ctx->tls_shared.cert_buf); av_freep(&ctx->tls_shared.key_buf); - EVP_PKEY_free(ctx->pkey); return 0; } -- 2.49.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".