Coverage Report

Created: 2026-06-18 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/http/http_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2001-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h> /* for sscanf() */
11
#include <string.h>
12
#include <openssl/http.h>
13
#include <openssl/httperr.h>
14
#include <openssl/bio.h> /* for BIO_snprintf() */
15
#include <openssl/err.h>
16
#include "internal/cryptlib.h" /* for ossl_assert() */
17
#ifndef OPENSSL_NO_SOCK
18
#include "internal/bio_addr.h" /* for NI_MAXHOST */
19
#endif
20
#ifndef NI_MAXHOST
21
#define NI_MAXHOST 255
22
#endif
23
#include "crypto/ctype.h" /* for ossl_isspace() */
24
0
#define OSSL_URL_SCHEME_SUFFIX "://"
25
26
static void init_pstring(char **pstr)
27
0
{
28
0
    if (pstr != NULL) {
29
0
        *pstr = NULL;
30
0
    }
31
0
}
32
33
static void init_pint(int *pint)
34
0
{
35
0
    if (pint != NULL) {
36
0
        *pint = 0;
37
0
    }
38
0
}
39
40
static int copy_substring(char **dest, const char *start, const char *end)
41
0
{
42
0
    return dest == NULL
43
0
        || (*dest = OPENSSL_strndup(start, end - start)) != NULL;
44
0
}
45
46
static void free_pstring(char **pstr)
47
0
{
48
0
    if (pstr != NULL) {
49
0
        OPENSSL_free(*pstr);
50
0
        *pstr = NULL;
51
0
    }
52
0
}
53
54
int OSSL_parse_url(const char *url, char **pscheme, char **puser, char **phost,
55
    char **pport, int *pport_num,
56
    char **ppath, char **pquery, char **pfrag)
57
0
{
58
0
    const char *p, *tmp;
59
0
    const char *authority_end;
60
0
    const char *scheme, *scheme_end;
61
0
    const char *user, *user_end;
62
0
    const char *host, *host_end;
63
0
    const char *port, *port_end;
64
0
    unsigned int portnum = 0;
65
0
    const char *path, *path_end;
66
0
    const char *query, *query_end;
67
0
    const char *frag, *frag_end;
68
69
0
    init_pstring(pscheme);
70
0
    init_pstring(puser);
71
0
    init_pstring(phost);
72
0
    init_pstring(pport);
73
0
    init_pint(pport_num);
74
0
    init_pstring(ppath);
75
0
    init_pstring(pfrag);
76
0
    init_pstring(pquery);
77
78
0
    if (url == NULL) {
79
0
        ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
80
0
        return 0;
81
0
    }
82
83
    /* check for optional prefix "<scheme>://" as per RFC 3986 */
84
0
    scheme = scheme_end = p = url;
85
0
    if (ossl_isalpha(*p)) {
86
0
        while (*p != '\0'
87
0
            && (ossl_isalpha(*p)
88
0
                || ossl_isdigit(*p)
89
0
                || strchr("+-.", *p) != NULL))
90
0
            p++;
91
0
        if (HAS_PREFIX(p, OSSL_URL_SCHEME_SUFFIX)) {
92
0
            scheme_end = p;
93
0
            p += sizeof(OSSL_URL_SCHEME_SUFFIX) - 1;
94
0
        } else {
95
0
            p = url;
96
0
        }
97
0
    }
98
99
    /* parse optional "userinfo@" */
100
0
    user = user_end = host = p;
101
0
    authority_end = strpbrk(p, "/?#");
102
0
    if (authority_end == NULL)
103
0
        authority_end = p + strlen(p);
104
0
    host = memchr(p, '@', authority_end - p);
105
0
    if (host != NULL)
106
0
        user_end = host++;
107
0
    else
108
0
        host = p;
109
110
    /* parse hostname/address as far as needed here */
111
0
    if (host[0] == '[') {
112
        /* IPv6 literal, which may include ':' */
113
0
        host_end = memchr(host + 1, ']', authority_end - host - 1);
114
0
        if (host_end == NULL)
115
0
            goto parse_err;
116
0
        p = ++host_end;
117
0
    } else {
118
        /* look for start of optional port, path, query, or fragment */
119
0
        host_end = strpbrk(host, ":/?#");
120
0
        if (host_end == NULL) /* the remaining string is just the hostname */
121
0
            host_end = host + strlen(host);
122
0
        p = host_end;
123
0
    }
124
125
    /* parse optional port specification starting with ':' */
126
0
    port = "0"; /* default */
127
0
    if (*p == ':')
128
0
        port = ++p;
129
    /* remaining port spec handling is also done for the default values */
130
    /* make sure a decimal port number is given */
131
0
    if (sscanf(port, "%u", &portnum) <= 0 || portnum > 65535) {
132
0
        ERR_raise_data(ERR_LIB_HTTP, HTTP_R_INVALID_PORT_NUMBER, "%s", port);
133
0
        goto err;
134
0
    }
135
0
    for (port_end = port; '0' <= *port_end && *port_end <= '9'; port_end++)
136
0
        ;
137
0
    if (port == p) /* port was given explicitly */
138
0
        p += port_end - port;
139
140
    /* check for optional path starting with '/' or '?'. Else must start '#' */
141
0
    path = p;
142
0
    if (*path != '\0' && *path != '/' && *path != '?' && *path != '#') {
143
0
        ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_PATH);
144
0
        goto parse_err;
145
0
    }
146
0
    path_end = query = query_end = frag = frag_end = path + strlen(path);
147
148
    /* parse optional "?query" */
149
0
    tmp = strchr(p, '?');
150
0
    if (tmp != NULL) {
151
0
        p = tmp;
152
0
        if (pquery != NULL) {
153
0
            path_end = p;
154
0
            query = p + 1;
155
0
        }
156
0
    }
157
158
    /* parse optional "#fragment" */
159
0
    tmp = strchr(p, '#');
160
0
    if (tmp != NULL) {
161
0
        if (query == path_end) /* we did not record a query component */
162
0
            path_end = tmp;
163
0
        query_end = tmp;
164
0
        frag = tmp + 1;
165
0
    }
166
167
0
    if (!copy_substring(pscheme, scheme, scheme_end)
168
0
        || !copy_substring(phost, host, host_end)
169
0
        || !copy_substring(pport, port, port_end)
170
0
        || !copy_substring(puser, user, user_end)
171
0
        || !copy_substring(pquery, query, query_end)
172
0
        || !copy_substring(pfrag, frag, frag_end))
173
0
        goto err;
174
0
    if (pport_num != NULL)
175
0
        *pport_num = (int)portnum;
176
0
    if (*path == '/') {
177
0
        if (!copy_substring(ppath, path, path_end))
178
0
            goto err;
179
0
    } else if (ppath != NULL) { /* must prepend '/' */
180
0
        size_t buflen = 1 + path_end - path + 1;
181
182
0
        if ((*ppath = OPENSSL_malloc(buflen)) == NULL)
183
0
            goto err;
184
0
        BIO_snprintf(*ppath, buflen, "/%s", path);
185
0
    }
186
0
    return 1;
187
188
0
parse_err:
189
0
    ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_PARSING_URL);
190
191
0
err:
192
0
    free_pstring(pscheme);
193
0
    free_pstring(puser);
194
0
    free_pstring(phost);
195
0
    free_pstring(pport);
196
0
    free_pstring(ppath);
197
0
    free_pstring(pquery);
198
0
    free_pstring(pfrag);
199
0
    return 0;
200
0
}
201
202
#ifndef OPENSSL_NO_HTTP
203
204
int OSSL_HTTP_parse_url(const char *url, int *pssl, char **puser, char **phost,
205
    char **pport, int *pport_num,
206
    char **ppath, char **pquery, char **pfrag)
207
0
{
208
0
    char *scheme, *port;
209
0
    int ssl = 0, portnum;
210
211
0
    init_pstring(pport);
212
0
    if (pssl != NULL)
213
0
        *pssl = 0;
214
0
    if (!OSSL_parse_url(url, &scheme, puser, phost, &port, pport_num,
215
0
            ppath, pquery, pfrag))
216
0
        return 0;
217
218
    /* check for optional HTTP scheme "http[s]" */
219
0
    if (strcmp(scheme, OSSL_HTTPS_NAME) == 0) {
220
0
        ssl = 1;
221
0
        if (pssl != NULL)
222
0
            *pssl = ssl;
223
0
    } else if (*scheme != '\0' && strcmp(scheme, OSSL_HTTP_NAME) != 0) {
224
0
        ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_SCHEME);
225
0
        OPENSSL_free(scheme);
226
0
        OPENSSL_free(port);
227
0
        goto err;
228
0
    }
229
0
    OPENSSL_free(scheme);
230
231
0
    if (strcmp(port, "0") == 0) {
232
        /* set default port */
233
0
        OPENSSL_free(port);
234
0
        port = ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT;
235
0
        if (!ossl_assert(sscanf(port, "%d", &portnum) == 1))
236
0
            goto err;
237
0
        if (pport_num != NULL)
238
0
            *pport_num = portnum;
239
0
        if (pport != NULL) {
240
0
            *pport = OPENSSL_strdup(port);
241
0
            if (*pport == NULL)
242
0
                goto err;
243
0
        }
244
0
    } else {
245
0
        if (pport != NULL)
246
0
            *pport = port;
247
0
        else
248
0
            OPENSSL_free(port);
249
0
    }
250
0
    return 1;
251
252
0
err:
253
0
    free_pstring(puser);
254
0
    free_pstring(phost);
255
0
    free_pstring(ppath);
256
0
    free_pstring(pquery);
257
0
    free_pstring(pfrag);
258
0
    return 0;
259
0
}
260
261
/* Respect no_proxy, taking default value from environment variable(s) */
262
static int use_proxy(const char *no_proxy, const char *server)
263
0
{
264
0
    size_t sl;
265
0
    const char *found = NULL;
266
0
    char host[NI_MAXHOST];
267
268
0
    if (!ossl_assert(server != NULL))
269
0
        return 0;
270
0
    sl = strlen(server);
271
0
    if (sl >= 2 && sl < sizeof(host) + 2 && server[0] == '[' && server[sl - 1] == ']') {
272
        /* strip leading '[' and trailing ']' from escaped IPv6 address */
273
0
        sl -= 2;
274
0
        strncpy(host, server + 1, sl);
275
0
        host[sl] = '\0';
276
0
        server = host;
277
0
    }
278
279
0
    if (sl == 0)
280
0
        return 1;
281
282
    /*
283
     * using environment variable names, both lowercase and uppercase variants,
284
     * compatible with other HTTP client implementations like wget, curl and git
285
     */
286
0
    if (no_proxy == NULL)
287
0
        no_proxy = ossl_safe_getenv("no_proxy");
288
0
    if (no_proxy == NULL)
289
0
        no_proxy = ossl_safe_getenv(OPENSSL_NO_PROXY);
290
291
0
    if (no_proxy != NULL)
292
0
        found = strstr(no_proxy, server);
293
0
    while (found != NULL
294
0
        && ((found != no_proxy && !ossl_isspace(found[-1]) && found[-1] != ',')
295
0
            || (found[sl] != '\0' && !ossl_isspace(found[sl]) && found[sl] != ',')))
296
0
        found = strstr(found + 1, server);
297
0
    return found == NULL;
298
0
}
299
300
/* Take default value from environment variable(s), respect no_proxy */
301
const char *OSSL_HTTP_adapt_proxy(const char *proxy, const char *no_proxy,
302
    const char *server, int use_ssl)
303
0
{
304
    /*
305
     * using environment variable names, both lowercase and uppercase variants,
306
     * compatible with other HTTP client implementations like wget, curl and git
307
     */
308
0
    if (proxy == NULL)
309
0
        proxy = ossl_safe_getenv(use_ssl ? "https_proxy" : "http_proxy");
310
0
    if (proxy == NULL)
311
0
        proxy = ossl_safe_getenv(use_ssl ? OPENSSL_HTTPS_PROXY : OPENSSL_HTTP_PROXY);
312
313
0
    if (proxy == NULL || *proxy == '\0' || !use_proxy(no_proxy, server))
314
0
        return NULL;
315
0
    return proxy;
316
0
}
317
318
#endif /* !defined(OPENSSL_NO_HTTP) */