Coverage Report

Created: 2026-08-31 06:56

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