Coverage Report

Created: 2023-09-25 06:45

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