http_lib.c 8.4 KB

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