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