http_lib.c 8.8 KB

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