http_lib.c 8.5 KB

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