OSSL_HTTP_transfer.pod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. =pod
  2. =head1 NAME
  3. OSSL_HTTP_get,
  4. OSSL_HTTP_get_asn1,
  5. OSSL_HTTP_post_asn1,
  6. OSSL_HTTP_transfer,
  7. OSSL_HTTP_bio_cb_t,
  8. OSSL_HTTP_proxy_connect,
  9. OSSL_HTTP_parse_url
  10. - http client functions
  11. =head1 SYNOPSIS
  12. #include <openssl/http.h>
  13. typedef BIO *(*OSSL_HTTP_bio_cb_t)(BIO *bio, void *arg,
  14. int connect, int detail);
  15. BIO *OSSL_HTTP_get(const char *url, const char *proxy, const char *no_proxy,
  16. BIO *bio, BIO *rbio,
  17. OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
  18. const STACK_OF(CONF_VALUE) *headers,
  19. int maxline, unsigned long max_resp_len, int timeout,
  20. const char *expected_content_type, int expect_asn1);
  21. ASN1_VALUE *OSSL_HTTP_get_asn1(const char *url,
  22. const char *proxy, const char *no_proxy,
  23. BIO *bio, BIO *rbio,
  24. OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
  25. const STACK_OF(CONF_VALUE) *headers,
  26. int maxline, unsigned long max_resp_len,
  27. int timeout, const char *expected_content_type,
  28. const ASN1_ITEM *it);
  29. ASN1_VALUE *OSSL_HTTP_post_asn1(const char *server, const char *port,
  30. const char *path, int use_ssl,
  31. const char *proxy, const char *no_proxy,
  32. BIO *bio, BIO *rbio,
  33. OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
  34. const STACK_OF(CONF_VALUE) *headers,
  35. const char *content_type,
  36. const ASN1_VALUE *req, const ASN1_ITEM *req_it,
  37. int maxline, unsigned long max_resp_len,
  38. int timeout, const char *expected_ct,
  39. const ASN1_ITEM *rsp_it);
  40. BIO *OSSL_HTTP_transfer(const char *server, const char *port, const char *path,
  41. int use_ssl, const char *proxy, const char *no_proxy,
  42. BIO *bio, BIO *rbio,
  43. OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
  44. const STACK_OF(CONF_VALUE) *headers,
  45. const char *content_type, BIO *req_mem,
  46. int maxline, unsigned long max_resp_len, int timeout,
  47. const char *expected_ct, int expect_asn1,
  48. char **redirection_url);
  49. int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
  50. const char *proxyuser, const char *proxypass,
  51. int timeout, BIO *bio_err, const char *prog);
  52. int OSSL_HTTP_parse_url(const char *url, char **phost, char **pport,
  53. char **ppath, int *pssl);
  54. =head1 DESCRIPTION
  55. OSSL_HTTP_get() uses HTTP GET to obtain data (of any type) from the given B<url>
  56. and returns it as a memory BIO.
  57. OSSL_HTTP_get_asn1() uses HTTP GET to obtain an ASN.1-encoded value
  58. (e.g., an X.509 certificate) with the expected structure specified by B<it>
  59. (e.g., I<ASN1_ITEM_rptr(X509)>) from the given B<url>
  60. and returns it on success as a pointer to I<ASN1_VALUE>.
  61. OSSL_HTTP_post_asn1() uses the HTTP POST method to send a request B<req>
  62. with the ASN.1 structure defined in B<req_it> and the given B<content_type> to
  63. the given B<server> and optional B<port> and B<path>.
  64. If B<use_ssl> is nonzero a TLS connection is requested and the B<bio_update_fn>
  65. parameter, described below, must be provided.
  66. The optional list B<headers> may contain additional custom HTTP header lines.
  67. The expected structure of the response is specified by B<rsp_it>.
  68. On success it returns the response as a pointer to B<ASN1_VALUE>.
  69. OSSL_HTTP_transfer() exchanges any form of HTTP request and response.
  70. It implements the core of the functions described above.
  71. If B<path> parameter is NULL it defaults to "/".
  72. If B<use_ssl> is nonzero a TLS connection is requested
  73. and the B<bio_update_fn> parameter, described below, must be provided.
  74. If B<req_mem> is NULL it uses the HTTP GET method, else it uses HTTP POST to
  75. send a request with the contents of the memory BIO and optional B<content_type>.
  76. The optional list B<headers> may contain additional custom HTTP header lines.
  77. If B<req_mem> is NULL (i.e., the HTTP method is GET) and B<redirection_url>
  78. is not NULL the latter pointer is used to provide any new location that
  79. the server may return with HTTP code 301 (MOVED_PERMANENTLY) or 302 (FOUND).
  80. In this case the caller is responsible for deallocating this URL with
  81. L<OPENSSL_free(3)>.
  82. The above functions have the following parameters in common.
  83. Typically the OpenSSL build supports sockets
  84. and the B<bio> and B<rbio> parameters are both NULL.
  85. In this case the client creates a network BIO internally
  86. for connecting to the given B<server>
  87. at the specified B<port> (if any, defaulting to 80 for HTTP or 443 for HTTPS),
  88. optionally via a B<proxy> (respecting B<no_proxy>) as described below.
  89. Then the client uses this internal BIO for exchanging the request and response.
  90. If B<bio> is given and B<rbio> is NULL then the client uses this B<bio> instead.
  91. If both B<bio> and B<rbio> are given (which may be memory BIOs for instance)
  92. then no explicit connection is attempted,
  93. B<bio> is used for writing the request, and B<rbio> for reading the response.
  94. As soon as the client has flushed B<bio> the server must be ready to provide
  95. a response or indicate a waiting condition via B<rbio>.
  96. The optional B<proxy> parameter can be used to set the address of the an
  97. HTTP(S) proxy to use (unless overridden by "no_proxy" settings).
  98. If TLS is not used this defaults to the environment variable B<http_proxy>
  99. if set, else B<HTTP_PROXY>.
  100. If B<use_ssl> != 0 it defaults to B<https_proxy> if set, else B<HTTPS_PROXY>.
  101. An empty proxy string specifies not to use a proxy.
  102. Else the format is I<[http[s]://]address[:port][/path]>,
  103. where any path given is ignored.
  104. The default proxy port number is 80, or 443 in case "https:" is given.
  105. The HTTP client functions connect via the given proxy unless the B<server>
  106. is found in the optional list B<no_proxy> of proxy hostnames (if not NULL;
  107. default is the environment variable B<no_proxy> if set, else B<NO_PROXY>).
  108. Proxying plain HTTP is supported directly,
  109. while using a proxy for HTTPS connections requires a suitable callback function
  110. such as B<OSSL_HTTP_proxy_connect()>, described below.
  111. The B<maxline> parameter specifies the response header maximum line length,
  112. where 0 indicates the default value, which currently is 4k.
  113. The B<max_resp_len> parameter specifies the maximum response length,
  114. where 0 indicates the default value, which currently is 100k.
  115. An ASN.1-encoded response is expected by OSSL_HTTP_get_asn1() and
  116. OSSL_HTTP_post_asn1(), while for OSSL_HTTP_get() or OSSL_HTTP_transfer()
  117. this is only the case if the B<expect_asn1> parameter is nonzero.
  118. If the response header contains one or more "Content-Length" header lines and/or
  119. an ASN.1-encoded response is expected, which should include a total length,
  120. the length indications received are checked for consistency
  121. and for not exceeding the maximum response length.
  122. If the parameter B<expected_content_type> (or B<expected_ct>, respectively)
  123. is not NULL then the HTTP client checks that the given content type string
  124. is included in the HTTP header of the response and returns an error if not.
  125. If the B<timeout> parameter is > 0 this indicates the maximum number of seconds
  126. to wait until the transfer is complete.
  127. A value of 0 enables waiting indefinitely,
  128. while a value < 0 immediately leads to a timeout condition.
  129. The optional parameter B<bio_update_fn> with its optional argument B<arg> may
  130. be used to modify the connection BIO used by the HTTP client (and cannot be
  131. used when both B<bio> and B<rbio> are given).
  132. B<bio_update_fn> is a BIO connect/disconnect callback function with prototype
  133. BIO *(*OSSL_HTTP_bio_cb_t)(BIO *bio, void *arg, int connect, int detail)
  134. The callback may modify the HTTP BIO provided in the B<bio> argument,
  135. whereby it may make use of a custom defined argument B<arg>,
  136. which may for instance refer to an I<SSL_CTX> structure.
  137. During connection establishment, just after calling BIO_do_connect_retry(),
  138. the function is invoked with the B<connect> argument being 1 and the B<detail>
  139. argument being 1 if HTTPS is requested, i.e., SSL/TLS should be enabled.
  140. On disconnect B<connect> is 0 and B<detail> is 1 if no error occurred, else 0.
  141. For instance, on connect the function may prepend a TLS BIO to implement HTTPS;
  142. after disconnect it may do some diagnostic output and/or specific cleanup.
  143. The function should return NULL to indicate failure.
  144. Here is a simple example that supports TLS connections (but not via a proxy):
  145. BIO *http_tls_cb(BIO *hbio, void *arg, int connect, int detail)
  146. {
  147. SSL_CTX *ctx = (SSL_CTX *)arg;
  148. if (connect && detail) { /* connecting with TLS */
  149. BIO *sbio = BIO_new_ssl(ctx, 1);
  150. hbio = sbio != NULL ? BIO_push(sbio, hbio) : NULL;
  151. } else if (!connect && !detail) { /* disconnecting after error */
  152. /* optionally add diagnostics here */
  153. }
  154. return hbio;
  155. }
  156. After disconnect the modified BIO will be deallocated using BIO_free_all().
  157. OSSL_HTTP_proxy_connect() may be used by an above BIO connect callback function
  158. to set up an SSL/TLS connection via an HTTPS proxy.
  159. It promotes the given BIO B<bio> representing a connection
  160. pre-established with a TLS proxy using the HTTP CONNECT method,
  161. optionally using proxy client credentials B<proxyuser> and B<proxypass>,
  162. to connect with TLS protection ultimately to B<server> and B<port>.
  163. If the B<port> argument is NULL or the empty string it defaults to "443".
  164. The B<timeout> parameter is used as described above.
  165. Since this function is typically called by applications such as
  166. L<openssl-s_client(1)> it uses the B<bio_err> and B<prog> parameters (unless
  167. NULL) to print additional diagnostic information in a user-oriented way.
  168. OSSL_HTTP_parse_url() parses its input string B<url> as a URL and splits it up
  169. into host, port and path components and a flag whether it begins with 'https'.
  170. The host component may be a DNS name or an IPv4 or an IPv6 address.
  171. The port component is optional and defaults to "443" for HTTPS, else "80".
  172. The path component is also optional and defaults to "/".
  173. As far as the result pointer arguments are not NULL it assigns via
  174. them copies of the respective string components.
  175. The strings returned this way must be deallocated by the caller using
  176. L<OPENSSL_free(3)> unless they are NULL, which is their default value on error.
  177. =head1 NOTES
  178. The names of the environment variables used by this implementation:
  179. B<http_proxy>, B<HTTP_PROXY>, B<https_proxy>, B<HTTPS_PROXY>, B<no_proxy>, and
  180. B<NO_PROXY>, have been chosen for maximal compatibility with
  181. other HTTP client implementations such as wget, curl, and git.
  182. =head1 RETURN VALUES
  183. OSSL_HTTP_get(), OSSL_HTTP_get_asn1(), OSSL_HTTP_post_asn1(), and
  184. OSSL_HTTP_transfer() return on success the data received via HTTP, else NULL.
  185. Error conditions include connection/transfer timeout, parse errors, etc.
  186. OSSL_HTTP_proxy_connect() and OSSL_HTTP_parse_url()
  187. return 1 on success, 0 on error.
  188. =head1 HISTORY
  189. OSSL_HTTP_get(), OSSL_HTTP_get_asn1(), OSSL_HTTP_post_asn1(),
  190. OSSL_HTTP_proxy_connect(), and OSSL_HTTP_parse_url() were added in OpenSSL 3.0.
  191. =head1 COPYRIGHT
  192. Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  193. Licensed under the Apache License 2.0 (the "License"). You may not use
  194. this file except in compliance with the License. You can obtain a copy
  195. in the file LICENSE in the source distribution or at
  196. L<https://www.openssl.org/source/license.html>.
  197. =cut