http_lib.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #ifndef OPENSSL_NO_HTTP
  178. int OSSL_HTTP_parse_url(const char *url, int *pssl, char **puser, char **phost,
  179. char **pport, int *pport_num,
  180. char **ppath, char **pquery, char **pfrag)
  181. {
  182. char *scheme, *port;
  183. int ssl = 0, portnum;
  184. init_pstring(pport);
  185. if (pssl != NULL)
  186. *pssl = 0;
  187. if (!OSSL_parse_url(url, &scheme, puser, phost, &port, pport_num,
  188. ppath, pquery, pfrag))
  189. return 0;
  190. /* check for optional HTTP scheme "http[s]" */
  191. if (strcmp(scheme, OSSL_HTTPS_NAME) == 0) {
  192. ssl = 1;
  193. if (pssl != NULL)
  194. *pssl = ssl;
  195. } else if (*scheme != '\0' && strcmp(scheme, OSSL_HTTP_NAME) != 0) {
  196. ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_SCHEME);
  197. OPENSSL_free(scheme);
  198. OPENSSL_free(port);
  199. goto err;
  200. }
  201. OPENSSL_free(scheme);
  202. if (strcmp(port, "0") == 0) {
  203. /* set default port */
  204. OPENSSL_free(port);
  205. port = ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT;
  206. if (!ossl_assert(sscanf(port, "%d", &portnum) == 1))
  207. goto err;
  208. if (pport_num != NULL)
  209. *pport_num = portnum;
  210. if (pport != NULL) {
  211. *pport = OPENSSL_strdup(port);
  212. if (*pport == NULL)
  213. goto err;
  214. }
  215. } else {
  216. if (pport != NULL)
  217. *pport = port;
  218. else
  219. OPENSSL_free(port);
  220. }
  221. return 1;
  222. err:
  223. free_pstring(puser);
  224. free_pstring(phost);
  225. free_pstring(ppath);
  226. free_pstring(pquery);
  227. free_pstring(pfrag);
  228. return 0;
  229. }
  230. /* Respect no_proxy, taking default value from environment variable(s) */
  231. static int use_proxy(const char *no_proxy, const char *server)
  232. {
  233. size_t sl;
  234. const char *found = NULL;
  235. char host[NI_MAXHOST];
  236. if (!ossl_assert(server != NULL))
  237. return 0;
  238. sl = strlen(server);
  239. if (sl >= 2 && sl < sizeof(host) + 2 && server[0] == '[' && server[sl - 1] == ']') {
  240. /* strip leading '[' and trailing ']' from escaped IPv6 address */
  241. sl -= 2;
  242. strncpy(host, server + 1, sl);
  243. host[sl] = '\0';
  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. }
  278. #endif /* !defined(OPENSSL_NO_HTTP) */