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