From 8315e1e5055bebc6e31fe93b88c3cf32292359e4 Mon Sep 17 00:00:00 2001 From: David von Oheimb Date: Thu, 1 Aug 2024 21:25:44 +0200 Subject: [PATCH] OSSL_HTTP_open(): fix completion with default port for IPv6 host addresses Reviewed-by: Viktor Dukhovni Reviewed-by: Tomas Mraz Reviewed-by: Neil Horman Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/25533) (cherry picked from commit a78da17491c4d9a8230508d13c047c4da736cc25) --- crypto/http/http_client.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/crypto/http/http_client.c b/crypto/http/http_client.c index cd4266ae27f..5e2be1df664 100644 --- a/crypto/http/http_client.c +++ b/crypto/http/http_client.c @@ -875,6 +875,20 @@ int OSSL_HTTP_REQ_CTX_nbio_d2i(OSSL_HTTP_REQ_CTX *rctx, #ifndef OPENSSL_NO_SOCK +static const char *explict_or_default_port(const char *hostserv, const char *port, int use_ssl) +{ + if (port == NULL) { + char *service = NULL; + + if (!BIO_parse_hostserv(hostserv, NULL, &service, BIO_PARSE_PRIO_HOST)) + return NULL; + if (service == NULL) /* implicit port */ + port = use_ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT; + OPENSSL_free(service); + } /* otherwise take the explicitly given port */ + return port; +} + /* set up a new connection BIO, to HTTP server or to HTTP(S) proxy if given */ static BIO *http_new_bio(const char *server /* optionally includes ":port" */, const char *server_port /* explicit server port */, @@ -894,8 +908,7 @@ static BIO *http_new_bio(const char *server /* optionally includes ":port" */, port = proxy_port; } - if (port == NULL && strchr(host, ':') == NULL) - port = use_ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT; + port = explict_or_default_port(host, port, use_ssl); cbio = BIO_new_connect(host /* optionally includes ":port" */); if (cbio == NULL) @@ -982,8 +995,6 @@ OSSL_HTTP_REQ_CTX *OSSL_HTTP_open(const char *server, const char *port, } if (port != NULL && *port == '\0') port = NULL; - if (port == NULL && strchr(server, ':') == NULL) - port = use_ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT; proxy = OSSL_HTTP_adapt_proxy(proxy, no_proxy, server, use_ssl); if (proxy != NULL && !OSSL_HTTP_parse_url(proxy, NULL /* use_ssl */, NULL /* user */, -- 2.47.2