http_lib.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 = 0;
  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 = strpbrk(host, ":/?#");
  100. if (host_end == NULL) /* the remaining string is just the hostname */
  101. host_end = host + strlen(host);
  102. p = host_end;
  103. }
  104. /* parse optional port specification starting with ':' */
  105. port = "0"; /* default */
  106. if (*p == ':')
  107. port = ++p;
  108. /* remaining port spec handling is also done for the default values */
  109. /* make sure a decimal port number is given */
  110. if (sscanf(port, "%u", &portnum) <= 0 || portnum > 65535) {
  111. ERR_raise_data(ERR_LIB_HTTP, HTTP_R_INVALID_PORT_NUMBER, "%s", port);
  112. goto err;
  113. }
  114. for (port_end = port; '0' <= *port_end && *port_end <= '9'; port_end++)
  115. ;
  116. if (port == p) /* port was given explicitly */
  117. p += port_end - port;
  118. /* check for optional path starting with '/' or '?'. Else must start '#' */
  119. path = p;
  120. if (*path != '\0' && *path != '/' && *path != '?' && *path != '#') {
  121. ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_PATH);
  122. goto parse_err;
  123. }
  124. path_end = query = query_end = frag = frag_end = path + strlen(path);
  125. /* parse optional "?query" */
  126. tmp = strchr(p, '?');
  127. if (tmp != NULL) {
  128. p = tmp;
  129. if (pquery != NULL) {
  130. path_end = p;
  131. query = p + 1;
  132. }
  133. }
  134. /* parse optional "#fragment" */
  135. tmp = strchr(p, '#');
  136. if (tmp != NULL) {
  137. if (query == path_end) /* we did not record a query component */
  138. path_end = tmp;
  139. query_end = tmp;
  140. frag = tmp + 1;
  141. }
  142. if (!copy_substring(pscheme, scheme, scheme_end)
  143. || !copy_substring(phost, host, host_end)
  144. || !copy_substring(pport, port, port_end)
  145. || !copy_substring(puser, user, user_end)
  146. || !copy_substring(pquery, query, query_end)
  147. || !copy_substring(pfrag, frag, frag_end))
  148. goto err;
  149. if (pport_num != NULL)
  150. *pport_num = (int)portnum;
  151. if (*path == '/') {
  152. if (!copy_substring(ppath, path, path_end))
  153. goto err;
  154. } else if (ppath != NULL) { /* must prepend '/' */
  155. size_t buflen = 1 + path_end - path + 1;
  156. if ((*ppath = OPENSSL_malloc(buflen)) == NULL)
  157. goto err;
  158. BIO_snprintf(*ppath, buflen, "/%s", path);
  159. }
  160. return 1;
  161. parse_err:
  162. ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_PARSING_URL);
  163. err:
  164. free_pstring(pscheme);
  165. free_pstring(puser);
  166. free_pstring(phost);
  167. free_pstring(pport);
  168. free_pstring(ppath);
  169. free_pstring(pquery);
  170. free_pstring(pfrag);
  171. return 0;
  172. }
  173. int OSSL_HTTP_parse_url(const char *url, int *pssl, char **puser, char **phost,
  174. char **pport, int *pport_num,
  175. char **ppath, char **pquery, char **pfrag)
  176. {
  177. char *scheme, *port;
  178. int ssl = 0, portnum;
  179. init_pstring(pport);
  180. if (pssl != NULL)
  181. *pssl = 0;
  182. if (!OSSL_parse_url(url, &scheme, puser, phost, &port, pport_num,
  183. ppath, pquery, pfrag))
  184. return 0;
  185. /* check for optional HTTP scheme "http[s]" */
  186. if (strcmp(scheme, OSSL_HTTPS_NAME) == 0) {
  187. ssl = 1;
  188. if (pssl != NULL)
  189. *pssl = ssl;
  190. } else if (*scheme != '\0' && strcmp(scheme, OSSL_HTTP_NAME) != 0) {
  191. ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_SCHEME);
  192. OPENSSL_free(scheme);
  193. OPENSSL_free(port);
  194. goto err;
  195. }
  196. OPENSSL_free(scheme);
  197. if (strcmp(port, "0") == 0) {
  198. /* set default port */
  199. OPENSSL_free(port);
  200. port = ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT;
  201. if (!ossl_assert(sscanf(port, "%d", &portnum) == 1))
  202. goto err;
  203. if (pport_num != NULL)
  204. *pport_num = portnum;
  205. if (pport != NULL) {
  206. *pport = OPENSSL_strdup(port);
  207. if (*pport == NULL)
  208. goto err;
  209. }
  210. } else {
  211. if (pport != NULL)
  212. *pport = port;
  213. else
  214. OPENSSL_free(port);
  215. }
  216. return 1;
  217. err:
  218. free_pstring(puser);
  219. free_pstring(phost);
  220. free_pstring(ppath);
  221. free_pstring(pquery);
  222. free_pstring(pfrag);
  223. return 0;
  224. }
  225. /* Respect no_proxy, taking default value from environment variable(s) */
  226. static int use_proxy(const char *no_proxy, const char *server)
  227. {
  228. size_t sl;
  229. const char *found = NULL;
  230. char host[NI_MAXHOST];
  231. if (!ossl_assert(server != NULL))
  232. return 0;
  233. sl = strlen(server);
  234. if (sl >= 2 && sl < sizeof(host) + 2 && server[0] == '[' && server[sl - 1] == ']') {
  235. /* strip leading '[' and trailing ']' from escaped IPv6 address */
  236. sl -= 2;
  237. strncpy(host, server + 1, sl);
  238. host[sl] = '\0';
  239. server = host;
  240. }
  241. /*
  242. * using environment variable names, both lowercase and uppercase variants,
  243. * compatible with other HTTP client implementations like wget, curl and git
  244. */
  245. if (no_proxy == NULL)
  246. no_proxy = ossl_safe_getenv("no_proxy");
  247. if (no_proxy == NULL)
  248. no_proxy = ossl_safe_getenv(OPENSSL_NO_PROXY);
  249. if (no_proxy != NULL)
  250. found = strstr(no_proxy, server);
  251. while (found != NULL
  252. && ((found != no_proxy && !ossl_isspace(found[-1]) && found[-1] != ',')
  253. || (found[sl] != '\0' && !ossl_isspace(found[sl]) && found[sl] != ',')))
  254. found = strstr(found + 1, server);
  255. return found == NULL;
  256. }
  257. /* Take default value from environment variable(s), respect no_proxy */
  258. const char *OSSL_HTTP_adapt_proxy(const char *proxy, const char *no_proxy,
  259. const char *server, int use_ssl)
  260. {
  261. /*
  262. * using environment variable names, both lowercase and uppercase variants,
  263. * compatible with other HTTP client implementations like wget, curl and git
  264. */
  265. if (proxy == NULL)
  266. proxy = ossl_safe_getenv(use_ssl ? "https_proxy" : "http_proxy");
  267. if (proxy == NULL)
  268. proxy = ossl_safe_getenv(use_ssl ? OPENSSL_HTTPS_PROXY : OPENSSL_HTTP_PROXY);
  269. if (proxy == NULL || *proxy == '\0' || !use_proxy(no_proxy, server))
  270. return NULL;
  271. return proxy;
  272. }