Skip to content

Commit 4b1fe47

Browse files
committed
HTTP client: make server/proxy and port params more consistent; minor other improvements
Reviewed-by: Matt Caswell <[email protected]> Reviewed-by: David von Oheimb <[email protected]> (Merged from openssl#11404)
1 parent afe554c commit 4b1fe47

18 files changed

+191
-117
lines changed

crypto/cmp/cmp_ctx.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ OSSL_CMP_CTX *OSSL_CMP_CTX_new(void)
9595
ctx->status = -1;
9696
ctx->failInfoCode = -1;
9797

98-
ctx->serverPort = OSSL_CMP_DEFAULT_PORT;
9998
ctx->msg_timeout = 2 * 60;
10099

101100
if ((ctx->untrusted_certs = sk_X509_new_null()) == NULL)
@@ -146,7 +145,7 @@ void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
146145
return;
147146

148147
OPENSSL_free(ctx->serverPath);
149-
OPENSSL_free(ctx->serverName);
148+
OPENSSL_free(ctx->server);
150149
OPENSSL_free(ctx->proxy);
151150
OPENSSL_free(ctx->no_proxy);
152151

@@ -775,7 +774,7 @@ int OSSL_CMP_CTX_set1_senderNonce(OSSL_CMP_CTX *ctx,
775774
DEFINE_OSSL_CMP_CTX_set1(proxy, char)
776775

777776
/* Set the (HTTP) host name of the CMP server */
778-
DEFINE_OSSL_CMP_CTX_set1(serverName, char)
777+
DEFINE_OSSL_CMP_CTX_set1(server, char)
779778

780779
/* Set the server exclusion list of the HTTP proxy server */
781780
DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)

crypto/cmp/cmp_http.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,24 @@
3535
OSSL_CMP_MSG *OSSL_CMP_MSG_http_perform(OSSL_CMP_CTX *ctx,
3636
const OSSL_CMP_MSG *req)
3737
{
38-
char server_port[32];
38+
char server_port[32] = { '\0' };
3939
STACK_OF(CONF_VALUE) *headers = NULL;
40-
OSSL_CMP_MSG *res = NULL;
4140
const char *const content_type_pkix = "application/pkixcmp";
41+
OSSL_CMP_MSG *res;
4242

43-
if (ctx == NULL || req == NULL
44-
|| ctx->serverName == NULL || ctx->serverPort == 0) {
43+
if (ctx == NULL || req == NULL) {
4544
CMPerr(0, CMP_R_NULL_ARGUMENT);
46-
return 0;
45+
return NULL;
4746
}
4847

4948
if (!X509V3_add_value("Pragma", "no-cache", &headers))
5049
return NULL;
5150

52-
BIO_snprintf(server_port, sizeof(server_port), "%d", ctx->serverPort);
51+
if (ctx->serverPort != 0)
52+
BIO_snprintf(server_port, sizeof(server_port), "%d", ctx->serverPort);
5353

5454
res = (OSSL_CMP_MSG *)
55-
OSSL_HTTP_post_asn1(ctx->serverName, server_port, ctx->serverPath,
55+
OSSL_HTTP_post_asn1(ctx->server, server_port, ctx->serverPath,
5656
OSSL_CMP_CTX_get_http_cb_arg(ctx) != NULL,
5757
ctx->proxy, ctx->no_proxy, NULL, NULL,
5858
ctx->http_cb, OSSL_CMP_CTX_get_http_cb_arg(ctx),

crypto/cmp/cmp_local.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct ossl_cmp_ctx_st {
3636
void *transfer_cb_arg; /* allows to store optional argument to cb */
3737
/* HTTP-based transfer */
3838
char *serverPath;
39-
char *serverName;
39+
char *server;
4040
int serverPort;
4141
char *proxy;
4242
char *no_proxy;

crypto/err/openssl.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,7 @@ HTTP_R_REDIRECTION_FROM_HTTPS_TO_HTTP:112:redirection from https to http
26062606
HTTP_R_REDIRECTION_NOT_ENABLED:116:redirection not enabled
26072607
HTTP_R_RESPONSE_LINE_TOO_LONG:113:response line too long
26082608
HTTP_R_RESPONSE_PARSE_ERROR:104:response parse error
2609+
HTTP_R_SOCK_NOT_SUPPORTED:122:sock not supported
26092610
HTTP_R_STATUS_CODE_UNSUPPORTED:114:status code unsupported
26102611
HTTP_R_TLS_NOT_ENABLED:107:tls not enabled
26112612
HTTP_R_TOO_MANY_REDIRECTIONS:115:too many redirections

crypto/http/http_client.c

+29-21
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <openssl/buffer.h>
2222
#include <openssl/http.h>
2323
#include "internal/sockets.h"
24-
#include "internal/cryptlib.h"
24+
#include "internal/cryptlib.h" /* for ossl_assert() */
2525

2626
#include "http_local.h"
2727

@@ -157,7 +157,7 @@ int OSSL_HTTP_REQ_CTX_header(OSSL_HTTP_REQ_CTX *rctx, const char *server,
157157
* Section 5.1.2 of RFC 1945 states that the absoluteURI form is only
158158
* allowed when using a proxy
159159
*/
160-
if (BIO_printf(rctx->mem, "http://%s", server) <= 0)
160+
if (BIO_printf(rctx->mem, OSSL_HTTP_PREFIX"%s", server) <= 0)
161161
return 0;
162162
if (port != NULL && BIO_printf(rctx->mem, ":%s", port) <= 0)
163163
return 0;
@@ -701,10 +701,8 @@ static BIO *HTTP_new_bio(const char *server /* optionally includes ":port" */,
701701
const char *port = server_port;
702702
BIO *cbio;
703703

704-
if (server == NULL) {
705-
HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER);
704+
if (!ossl_assert(server != NULL))
706705
return NULL;
707-
}
708706

709707
if (proxy != NULL) {
710708
host = proxy;
@@ -714,7 +712,7 @@ static BIO *HTTP_new_bio(const char *server /* optionally includes ":port" */,
714712
host_end = strchr(host, '/');
715713
if (host_end != NULL && (size_t)(host_end - host) < sizeof(host_name)) {
716714
/* chop trailing string starting with '/' */
717-
strncpy(host_name, host, host_end - host);
715+
strncpy(host_name, host, host_end - host + 1);
718716
host = host_name;
719717
}
720718

@@ -849,18 +847,28 @@ BIO *OSSL_HTTP_transfer(const char *server, const char *port, const char *path,
849847
HTTPerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
850848
return NULL;
851849
}
852-
/* remaining parameters are checked indirectly by the functions called */
853850

854-
proxy = http_adapt_proxy(proxy, no_proxy, server, use_ssl);
855-
if (bio != NULL)
851+
if (bio != NULL) {
856852
cbio = bio;
857-
else
853+
} else {
858854
#ifndef OPENSSL_NO_SOCK
855+
if (server == NULL) {
856+
HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER);
857+
return NULL;
858+
}
859+
if (*port == '\0')
860+
port = NULL;
861+
if (port == NULL && strchr(server, ':') == NULL)
862+
port = use_ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT;
863+
proxy = http_adapt_proxy(proxy, no_proxy, server, use_ssl);
859864
if ((cbio = HTTP_new_bio(server, port, proxy)) == NULL)
860865
return NULL;
861866
#else
867+
HTTPerr(0, HTTP_R_SOCK_NOT_SUPPORTED);
862868
return NULL;
863869
#endif
870+
}
871+
/* remaining parameters are checked indirectly by the functions called */
864872

865873
(void)ERR_set_mark(); /* prepare removing any spurious libssl errors */
866874
if (rbio == NULL && BIO_connect_retry(cbio, timeout) <= 0)
@@ -902,10 +910,10 @@ BIO *OSSL_HTTP_transfer(const char *server, const char *port, const char *path,
902910
if (lib == ERR_LIB_SSL || lib == ERR_LIB_HTTP
903911
|| (lib == ERR_LIB_BIO && reason == BIO_R_CONNECT_TIMEOUT)
904912
|| (lib == ERR_LIB_BIO && reason == BIO_R_CONNECT_ERROR)
905-
# ifndef OPENSSL_NO_CMP
913+
#ifndef OPENSSL_NO_CMP
906914
|| (lib == ERR_LIB_CMP
907915
&& reason == CMP_R_POTENTIALLY_INVALID_CERTIFICATE)
908-
# endif
916+
#endif
909917
) {
910918
BIO_snprintf(buf, 200, "server=%s:%s", server, port);
911919
ERR_add_error_data(1, buf);
@@ -949,17 +957,16 @@ BIO *OSSL_HTTP_transfer(const char *server, const char *port, const char *path,
949957

950958
static int redirection_ok(int n_redir, const char *old_url, const char *new_url)
951959
{
952-
static const char https[] = "https:";
953-
int https_len = 6; /* strlen(https) */
960+
size_t https_len = strlen(OSSL_HTTPS_NAME":");
954961

955962
if (n_redir >= HTTP_VERSION_MAX_REDIRECTIONS) {
956963
HTTPerr(0, HTTP_R_TOO_MANY_REDIRECTIONS);
957964
return 0;
958965
}
959966
if (*new_url == '/') /* redirection to same server => same protocol */
960967
return 1;
961-
if (strncmp(old_url, https, https_len) == 0 &&
962-
strncmp(new_url, https, https_len) != 0) {
968+
if (strncmp(old_url, OSSL_HTTPS_NAME":", https_len) == 0 &&
969+
strncmp(new_url, OSSL_HTTPS_NAME":", https_len) != 0) {
963970
HTTPerr(0, HTTP_R_REDIRECTION_FROM_HTTPS_TO_HTTP);
964971
return 0;
965972
}
@@ -1122,8 +1129,8 @@ int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
11221129
const char *proxyuser, const char *proxypass,
11231130
int timeout, BIO *bio_err, const char *prog)
11241131
{
1125-
# undef BUF_SIZE
1126-
# define BUF_SIZE (8 * 1024)
1132+
#undef BUF_SIZE
1133+
#define BUF_SIZE (8 * 1024)
11271134
char *mbuf = OPENSSL_malloc(BUF_SIZE);
11281135
char *mbufp;
11291136
int read_len = 0;
@@ -1132,11 +1139,13 @@ int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
11321139
int rv;
11331140
time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
11341141

1135-
if (bio == NULL || server == NULL || port == NULL
1142+
if (bio == NULL || server == NULL
11361143
|| (bio_err != NULL && prog == NULL)) {
11371144
HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER);
11381145
goto end;
11391146
}
1147+
if (port == NULL || *port == '\0')
1148+
port = OSSL_HTTPS_PORT;
11401149

11411150
if (mbuf == NULL || fbio == NULL) {
11421151
BIO_printf(bio_err /* may be NULL */, "%s: out of memory", prog);
@@ -1256,6 +1265,5 @@ int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
12561265
}
12571266
OPENSSL_free(mbuf);
12581267
return ret;
1259-
# undef BUF_SIZE
1268+
#undef BUF_SIZE
12601269
}
1261-

crypto/http/http_err.c

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ static const ERR_STRING_DATA HTTP_str_reasons[] = {
4545
"response line too long"},
4646
{ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_RESPONSE_PARSE_ERROR),
4747
"response parse error"},
48+
{ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_SOCK_NOT_SUPPORTED),
49+
"sock not supported"},
4850
{ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_STATUS_CODE_UNSUPPORTED),
4951
"status code unsupported"},
5052
{ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_TLS_NOT_ENABLED), "tls not enabled"},

crypto/http/http_lib.c

+38-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <openssl/httperr.h>
1212
#include <openssl/err.h>
1313
#include <string.h>
14+
#include "internal/cryptlib.h" /* for ossl_assert() */
1415

1516
#include "http_local.h"
1617

@@ -24,8 +25,11 @@ int OSSL_HTTP_parse_url(const char *url, char **phost, char **pport,
2425
{
2526
char *p, *buf;
2627
char *host;
27-
char *port = "80";
28+
const char *port = OSSL_HTTP_PORT;
29+
size_t https_len = strlen(OSSL_HTTPS_NAME);
2830

31+
if (!ossl_assert(https_len >= strlen(OSSL_HTTP_NAME)))
32+
return 0;
2933
if (url == NULL) {
3034
HTTPerr(0, ERR_R_PASSED_NULL_PARAMETER);
3135
return 0;
@@ -46,16 +50,16 @@ int OSSL_HTTP_parse_url(const char *url, char **phost, char **pport,
4650

4751
/* Check for initial colon */
4852
p = strchr(buf, ':');
49-
if (p == NULL || p - buf > 5 /* strlen("https") */) {
53+
if (p == NULL || (size_t)(p - buf) > https_len) {
5054
p = buf;
5155
} else {
5256
*(p++) = '\0';
5357

54-
if (strcmp(buf, "https") == 0) {
58+
if (strcmp(buf, OSSL_HTTPS_NAME) == 0) {
5559
if (pssl != NULL)
5660
*pssl = 1;
57-
port = "443";
58-
} else if (strcmp(buf, "http") != 0) {
61+
port = OSSL_HTTPS_PORT;
62+
} else if (strcmp(buf, OSSL_HTTP_NAME) != 0) {
5963
goto parse_err;
6064
}
6165

@@ -119,13 +123,21 @@ int OSSL_HTTP_parse_url(const char *url, char **phost, char **pport,
119123

120124
int http_use_proxy(const char *no_proxy, const char *server)
121125
{
122-
size_t sl = strlen(server);
126+
size_t sl;
123127
const char *found = NULL;
124128

129+
if (!ossl_assert(server != NULL))
130+
return 0;
131+
sl = strlen(server);
132+
133+
/*
134+
* using environment variable names, both lowercase and uppercase variants,
135+
* compatible with other HTTP client implementations like wget, curl and git
136+
*/
125137
if (no_proxy == NULL)
126138
no_proxy = getenv("no_proxy");
127139
if (no_proxy == NULL)
128-
no_proxy = getenv("NO_PROXY");
140+
no_proxy = getenv(OPENSSL_NO_PROXY);
129141
if (no_proxy != NULL)
130142
found = strstr(no_proxy, server);
131143
while (found != NULL
@@ -138,17 +150,28 @@ int http_use_proxy(const char *no_proxy, const char *server)
138150
const char *http_adapt_proxy(const char *proxy, const char *no_proxy,
139151
const char *server, int use_ssl)
140152
{
141-
int prefix_len = strlen(HTTP_URL_PREFIX);
153+
const int http_len = strlen(OSSL_HTTP_PREFIX);
154+
const int https_len = strlen(OSSL_HTTPS_PREFIX);
142155

156+
/*
157+
* using environment variable names, both lowercase and uppercase variants,
158+
* compatible with other HTTP client implementations like wget, curl and git
159+
*/
143160
if (proxy == NULL)
144161
proxy = getenv(use_ssl ? "https_proxy" : "http_proxy");
145162
if (proxy == NULL)
146-
proxy = getenv(use_ssl ? "HTTPS_PROXY" : "HTTP_PROXY");
147-
if (proxy != NULL && strncmp(proxy, HTTP_URL_PREFIX, prefix_len) == 0)
148-
proxy += prefix_len; /* skip any leading "http://" */
149-
if (proxy != NULL && *proxy == '\0')
150-
proxy = NULL;
151-
if (proxy != NULL && !http_use_proxy(no_proxy, server))
152-
proxy = NULL;
163+
proxy = getenv(use_ssl ? OPENSSL_HTTP_PROXY :
164+
OPENSSL_HTTPS_PROXY);
165+
if (proxy == NULL)
166+
return NULL;
167+
168+
/* skip any leading "http://" or "https://" */
169+
if (strncmp(proxy, OSSL_HTTP_PREFIX, http_len) == 0)
170+
proxy += http_len;
171+
else if (strncmp(proxy, OSSL_HTTPS_PREFIX, https_len) == 0)
172+
proxy += https_len;
173+
174+
if (*proxy == '\0' || !http_use_proxy(no_proxy, server))
175+
return NULL;
153176
return proxy;
154177
}

crypto/http/http_local.h

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ typedef OCSP_REQ_CTX OSSL_HTTP_REQ_CTX;
2727
# define OSSL_HTTP_REQ_CTX_get0_mem_bio OCSP_REQ_CTX_get0_mem_bio /* undoc'd */
2828
# define OSSL_HTTP_REQ_CTX_set_max_response_length OCSP_set_max_response_length
2929

30-
# define HTTP_URL_PREFIX "http://"
31-
3230
BIO *HTTP_asn1_item2bio(const ASN1_ITEM *it, ASN1_VALUE *val);
3331
OSSL_HTTP_REQ_CTX *HTTP_REQ_CTX_new(BIO *wbio, BIO *rbio, int use_http_proxy,
3432
const char *server, const char *port,

0 commit comments

Comments
 (0)