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