1
0

http_lib.c 8.9 KB

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