1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1 christos OSSL_HTTP_open, 6 1.1 christos OSSL_HTTP_bio_cb_t, 7 1.1 christos OSSL_HTTP_proxy_connect, 8 1.1 christos OSSL_HTTP_set1_request, 9 1.1 christos OSSL_HTTP_exchange, 10 1.1 christos OSSL_HTTP_get, 11 1.1 christos OSSL_HTTP_transfer, 12 1.1 christos OSSL_HTTP_close 13 1.1 christos - HTTP client high-level functions 14 1.1 christos 15 1.1 christos =head1 SYNOPSIS 16 1.1 christos 17 1.1 christos #include <openssl/http.h> 18 1.1 christos 19 1.1 christos typedef BIO *(*OSSL_HTTP_bio_cb_t)(BIO *bio, void *arg, 20 1.1 christos int connect, int detail); 21 1.1 christos OSSL_HTTP_REQ_CTX *OSSL_HTTP_open(const char *server, const char *port, 22 1.1 christos const char *proxy, const char *no_proxy, 23 1.1 christos int use_ssl, BIO *bio, BIO *rbio, 24 1.1 christos OSSL_HTTP_bio_cb_t bio_update_fn, void *arg, 25 1.1 christos int buf_size, int overall_timeout); 26 1.1 christos int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port, 27 1.1 christos const char *proxyuser, const char *proxypass, 28 1.1 christos int timeout, BIO *bio_err, const char *prog); 29 1.1 christos int OSSL_HTTP_set1_request(OSSL_HTTP_REQ_CTX *rctx, const char *path, 30 1.1 christos const STACK_OF(CONF_VALUE) *headers, 31 1.1 christos const char *content_type, BIO *req, 32 1.1 christos const char *expected_content_type, int expect_asn1, 33 1.1 christos size_t max_resp_len, int timeout, int keep_alive); 34 1.1 christos BIO *OSSL_HTTP_exchange(OSSL_HTTP_REQ_CTX *rctx, char **redirection_url); 35 1.1 christos BIO *OSSL_HTTP_get(const char *url, const char *proxy, const char *no_proxy, 36 1.1 christos BIO *bio, BIO *rbio, 37 1.1 christos OSSL_HTTP_bio_cb_t bio_update_fn, void *arg, 38 1.1 christos int buf_size, const STACK_OF(CONF_VALUE) *headers, 39 1.1 christos const char *expected_content_type, int expect_asn1, 40 1.1 christos size_t max_resp_len, int timeout); 41 1.1 christos BIO *OSSL_HTTP_transfer(OSSL_HTTP_REQ_CTX **prctx, 42 1.1 christos const char *server, const char *port, 43 1.1 christos const char *path, int use_ssl, 44 1.1 christos const char *proxy, const char *no_proxy, 45 1.1 christos BIO *bio, BIO *rbio, 46 1.1 christos OSSL_HTTP_bio_cb_t bio_update_fn, void *arg, 47 1.1 christos int buf_size, const STACK_OF(CONF_VALUE) *headers, 48 1.1 christos const char *content_type, BIO *req, 49 1.1 christos const char *expected_content_type, int expect_asn1, 50 1.1 christos size_t max_resp_len, int timeout, int keep_alive); 51 1.1 christos int OSSL_HTTP_close(OSSL_HTTP_REQ_CTX *rctx, int ok); 52 1.1 christos 53 1.1 christos =head1 DESCRIPTION 54 1.1 christos 55 1.1 christos OSSL_HTTP_open() initiates an HTTP session using the I<bio> argument if not 56 1.1 christos NULL, else by connecting to a given I<server> optionally via a I<proxy>. 57 1.1 christos 58 1.1 christos Typically the OpenSSL build supports sockets and the I<bio> parameter is NULL. 59 1.1 christos In this case I<rbio> must be NULL as well and the I<server> must be non-NULL. 60 1.1 christos The function creates a network BIO internally using L<BIO_new_connect(3)> 61 1.1 christos for connecting to the given server and the optionally given I<port>, 62 1.1 christos defaulting to 80 for HTTP or 443 for HTTPS. 63 1.1 christos Then this internal BIO is used for setting up a connection 64 1.1 christos and for exchanging one or more request and response. 65 1.1 christos If I<bio> is given and I<rbio> is NULL then this I<bio> is used instead. 66 1.1 christos If both I<bio> and I<rbio> are given (which may be memory BIOs for instance) 67 1.1 christos then no explicit connection is set up, but 68 1.1 christos I<bio> is used for writing requests and I<rbio> for reading responses. 69 1.1 christos As soon as the client has flushed I<bio> the server must be ready to provide 70 1.1 christos a response or indicate a waiting condition via I<rbio>. 71 1.1 christos 72 1.1 christos If I<bio> is given, it is an error to provide I<proxy> or I<no_proxy> arguments, 73 1.1 christos while I<server> and I<port> arguments may be given to support diagnostic output. 74 1.1 christos If I<bio> is NULL the optional I<proxy> parameter can be used to set an 75 1.1 christos HTTP(S) proxy to use (unless overridden by "no_proxy" settings). 76 1.1 christos If TLS is not used this defaults to the environment variable C<http_proxy> 77 1.1 christos if set, else C<HTTP_PROXY>. 78 1.1 christos If I<use_ssl> != 0 it defaults to C<https_proxy> if set, else C<HTTPS_PROXY>. 79 1.1 christos An empty proxy string C<""> forbids using a proxy. 80 1.3 christos Otherwise, the format is 81 1.1 christos C<[http[s]://][userinfo@]host[:port][/path][?query][#fragment]>, 82 1.1 christos where any userinfo, path, query, and fragment given is ignored. 83 1.3 christos If the host string is an IPv6 address, it must be enclosed in C<[> and C<]>. 84 1.1 christos The default proxy port number is 80, or 443 in case "https:" is given. 85 1.1 christos The HTTP client functions connect via the given proxy unless the I<server> 86 1.3 christos is found in the optional list I<no_proxy> of proxy hostnames or IP addresses 87 1.3 christos separated by C<,> and/or whitespace (if not NULL; 88 1.1 christos default is the environment variable C<no_proxy> if set, else C<NO_PROXY>). 89 1.1 christos Proxying plain HTTP is supported directly, 90 1.1 christos while using a proxy for HTTPS connections requires a suitable callback function 91 1.1 christos such as OSSL_HTTP_proxy_connect(), described below. 92 1.1 christos 93 1.1 christos If I<use_ssl> is nonzero a TLS connection is requested 94 1.1 christos and the I<bio_update_fn> parameter must be provided. 95 1.1 christos 96 1.1 christos The parameter I<bio_update_fn>, which is optional if I<use_ssl> is 0, 97 1.1 christos may be used to modify the connection BIO used by the HTTP client, 98 1.1 christos but cannot be used when both I<bio> and I<rbio> are given. 99 1.1 christos I<bio_update_fn> is a BIO connect/disconnect callback function with prototype 100 1.1 christos 101 1.1 christos BIO *(*OSSL_HTTP_bio_cb_t)(BIO *bio, void *arg, int connect, int detail) 102 1.1 christos 103 1.1 christos The callback function may modify the BIO provided in the I<bio> argument, 104 1.1 christos whereby it may make use of a custom defined argument I<arg>, 105 1.1 christos which may for instance point to an B<SSL_CTX> structure. 106 1.1 christos During connection establishment, just after calling BIO_do_connect_retry(), the 107 1.1 christos callback function is invoked with the I<connect> argument being 1 and 108 1.1 christos I<detail> being 1 if I<use_ssl> is nonzero (i.e., HTTPS is requested), else 0. 109 1.1 christos On disconnect I<connect> is 0 and I<detail> is 1 if no error occurred, else 0. 110 1.1 christos For instance, on connect the callback may push an SSL BIO to implement HTTPS; 111 1.1 christos after disconnect it may do some diagnostic output and pop and free the SSL BIO. 112 1.1 christos 113 1.1 christos The callback function must return either the potentially modified BIO I<bio>. 114 1.1 christos or NULL to indicate failure, in which case it should not modify the BIO. 115 1.1 christos 116 1.1 christos Here is a simple example that supports TLS connections (but not via a proxy): 117 1.1 christos 118 1.1 christos BIO *http_tls_cb(BIO *bio, void *arg, int connect, int detail) 119 1.1 christos { 120 1.1 christos if (connect && detail) { /* connecting with TLS */ 121 1.1 christos SSL_CTX *ctx = (SSL_CTX *)arg; 122 1.1 christos BIO *sbio = BIO_new_ssl(ctx, 1); 123 1.1 christos 124 1.1 christos bio = sbio != NULL ? BIO_push(sbio, bio) : NULL; 125 1.1 christos } else if (!connect) { /* disconnecting */ 126 1.1 christos BIO *hbio; 127 1.1 christos 128 1.1 christos if (!detail) { /* an error has occurred */ 129 1.1 christos /* optionally add diagnostics here */ 130 1.1 christos } 131 1.1 christos BIO_ssl_shutdown(bio); 132 1.1 christos hbio = BIO_pop(bio); 133 1.1 christos BIO_free(bio); /* SSL BIO */ 134 1.1 christos bio = hbio; 135 1.1 christos } 136 1.1 christos return bio; 137 1.1 christos } 138 1.1 christos 139 1.1 christos After disconnect the modified BIO will be deallocated using BIO_free_all(). 140 1.1 christos 141 1.1 christos The I<buf_size> parameter specifies the response header maximum line length. 142 1.1 christos A value <= 0 means that the B<OSSL_HTTP_DEFAULT_MAX_LINE_LEN> (4KiB) is used. 143 1.1 christos I<buf_size> is also used as the number of content bytes that are read at a time. 144 1.1 christos 145 1.1 christos If the I<overall_timeout> parameter is > 0 this indicates the maximum number of 146 1.1 christos seconds the overall HTTP transfer (i.e., connection setup if needed, 147 1.1 christos sending requests, and receiving responses) is allowed to take until completion. 148 1.1 christos A value <= 0 enables waiting indefinitely, i.e., no timeout. 149 1.1 christos 150 1.1 christos OSSL_HTTP_proxy_connect() may be used by an above BIO connect callback function 151 1.1 christos to set up an SSL/TLS connection via an HTTPS proxy. 152 1.1 christos It promotes the given BIO I<bio> representing a connection 153 1.1 christos pre-established with a TLS proxy using the HTTP CONNECT method, 154 1.1 christos optionally using proxy client credentials I<proxyuser> and I<proxypass>, 155 1.1 christos to connect with TLS protection ultimately to I<server> and I<port>. 156 1.1 christos If the I<port> argument is NULL or the empty string it defaults to "443". 157 1.1 christos If the I<timeout> parameter is > 0 this indicates the maximum number of 158 1.1 christos seconds the connection setup is allowed to take. 159 1.1 christos A value <= 0 enables waiting indefinitely, i.e., no timeout. 160 1.1 christos Since this function is typically called by applications such as 161 1.1 christos L<openssl-s_client(1)> it uses the I<bio_err> and I<prog> parameters (unless 162 1.1 christos NULL) to print additional diagnostic information in a user-oriented way. 163 1.1 christos 164 1.1 christos OSSL_HTTP_set1_request() sets up in I<rctx> the request header and content data 165 1.1 christos and expectations on the response using the following parameters. 166 1.2 christos If <rctx> indicates using a proxy for HTTP (but not HTTPS), the server host 167 1.2 christos (and optionally port) needs to be placed in the header; thus it must be present 168 1.2 christos in I<rctx>. 169 1.2 christos For backward compatibility, the server (and optional port) may also be given in 170 1.2 christos the I<path> argument beginning with C<http://> (thus giving an absoluteURI). 171 1.1 christos If I<path> is NULL it defaults to "/". 172 1.1 christos If I<req> is NULL the HTTP GET method will be used to send the request 173 1.1 christos else HTTP POST with the contents of I<req> and optional I<content_type>, where 174 1.1 christos the length of the data in I<req> does not need to be determined in advance: the 175 1.1 christos BIO will be read on-the-fly while sending the request, which supports streaming. 176 1.1 christos The optional list I<headers> may contain additional custom HTTP header lines. 177 1.1 christos If the parameter I<expected_content_type> 178 1.1 christos is not NULL then the client will check that the given content type string 179 1.1 christos is included in the HTTP header of the response and return an error if not. 180 1.1 christos If the I<expect_asn1> parameter is nonzero, 181 1.1 christos a structure in ASN.1 encoding will be expected as response content. 182 1.1 christos The I<max_resp_len> parameter specifies the maximum allowed 183 1.1 christos response content length, where the value 0 indicates no limit. 184 1.1 christos If the I<timeout> parameter is > 0 this indicates the maximum number of seconds 185 1.1 christos the subsequent HTTP transfer (sending the request and receiving a response) 186 1.1 christos is allowed to take. 187 1.1 christos A value of 0 enables waiting indefinitely, i.e., no timeout. 188 1.1 christos A value < 0 indicates that the I<overall_timeout> parameter value given 189 1.1 christos when opening the HTTP transfer will be used instead. 190 1.1 christos If I<keep_alive> is 0 the connection is not kept open 191 1.1 christos after receiving a response, which is the default behavior for HTTP 1.0. 192 1.1 christos If the value is 1 or 2 then a persistent connection is requested. 193 1.1 christos If the value is 2 then a persistent connection is required, 194 1.1 christos i.e., an error occurs in case the server does not grant it. 195 1.1 christos 196 1.1 christos OSSL_HTTP_exchange() exchanges any form of HTTP request and response 197 1.1 christos as specified by I<rctx>, which must include both connection and request data, 198 1.1 christos typically set up using OSSL_HTTP_open() and OSSL_HTTP_set1_request(). 199 1.1 christos It implements the core of the functions described below. 200 1.1 christos If the HTTP method is GET and I<redirection_url> 201 1.1 christos is not NULL the latter pointer is used to provide any new location that 202 1.1 christos the server may return with HTTP code 301 (MOVED_PERMANENTLY) or 302 (FOUND). 203 1.1 christos In this case the function returns NULL and the caller is 204 1.1 christos responsible for deallocating the URL with L<OPENSSL_free(3)>. 205 1.1 christos If the response header contains one or more "Content-Length" header lines and/or 206 1.1 christos an ASN.1-encoded response is expected, which should include a total length, 207 1.1 christos the length indications received are checked for consistency 208 1.1 christos and for not exceeding any given maximum response length. 209 1.1 christos If an ASN.1-encoded response is expected, the function returns on success 210 1.1 christos the contents buffered in a memory BIO, which does not support streaming. 211 1.1 christos Otherwise it returns directly the read BIO that holds the response contents, 212 1.1 christos which allows a response of indefinite length and may support streaming. 213 1.1 christos The caller is responsible for freeing the BIO pointer obtained. 214 1.1 christos 215 1.1 christos OSSL_HTTP_get() uses HTTP GET to obtain data from I<bio> if non-NULL, 216 1.1 christos else from the server contained in the I<url>, and returns it as a BIO. 217 1.1 christos It supports redirection via HTTP status code 301 or 302. It is meant for 218 1.1 christos transfers with a single round trip, so does not support persistent connections. 219 1.1 christos If I<bio> is non-NULL, any host and port components in the I<url> are not used 220 1.1 christos for connecting but the hostname is used, as usual, for the C<Host> header. 221 1.1 christos Any userinfo and fragment components in the I<url> are ignored. 222 1.1 christos Any query component is handled as part of the path component. 223 1.1 christos If the scheme component of the I<url> is C<https> a TLS connection is requested 224 1.1 christos and the I<bio_update_fn>, as described for OSSL_HTTP_open(), must be provided. 225 1.1 christos Also the remaining parameters are interpreted as described for OSSL_HTTP_open() 226 1.1 christos and OSSL_HTTP_set1_request(), respectively. 227 1.1 christos The caller is responsible for freeing the BIO pointer obtained. 228 1.1 christos 229 1.1 christos OSSL_HTTP_transfer() exchanges an HTTP request and response 230 1.1 christos over a connection managed via I<prctx> without supporting redirection. 231 1.1 christos It combines OSSL_HTTP_open(), OSSL_HTTP_set1_request(), OSSL_HTTP_exchange(), 232 1.1 christos and OSSL_HTTP_close(). 233 1.1 christos If I<prctx> is not NULL it reuses any open connection represented by a non-NULL 234 1.1 christos I<*prctx>. It keeps the connection open if a persistent connection is requested 235 1.1 christos or required and this was granted by the server, else it closes the connection 236 1.1 christos and assigns NULL to I<*prctx>. 237 1.1 christos The remaining parameters are interpreted as described for OSSL_HTTP_open() 238 1.1 christos and OSSL_HTTP_set1_request(), respectively. 239 1.1 christos The caller is responsible for freeing the BIO pointer obtained. 240 1.1 christos 241 1.1 christos OSSL_HTTP_close() closes the connection and releases I<rctx>. 242 1.1 christos The I<ok> parameter is passed to any BIO update function 243 1.1 christos given during setup as described above for OSSL_HTTP_open(). 244 1.1 christos It must be 1 if no error occurred during the HTTP transfer and 0 otherwise. 245 1.1 christos 246 1.1 christos =head1 NOTES 247 1.1 christos 248 1.1 christos The names of the environment variables used by this implementation: 249 1.1 christos C<http_proxy>, C<HTTP_PROXY>, C<https_proxy>, C<HTTPS_PROXY>, C<no_proxy>, and 250 1.1 christos C<NO_PROXY>, have been chosen for maximal compatibility with 251 1.1 christos other HTTP client implementations such as wget, curl, and git. 252 1.1 christos 253 1.1 christos =head1 RETURN VALUES 254 1.1 christos 255 1.1 christos OSSL_HTTP_open() returns on success a B<OSSL_HTTP_REQ_CTX>, else NULL. 256 1.1 christos 257 1.1 christos OSSL_HTTP_proxy_connect() and OSSL_HTTP_set1_request() 258 1.1 christos return 1 on success, 0 on error. 259 1.1 christos 260 1.1 christos On success, OSSL_HTTP_exchange(), OSSL_HTTP_get(), and OSSL_HTTP_transfer() 261 1.1 christos return a memory BIO that buffers all the data received if an ASN.1-encoded 262 1.1 christos response is expected, otherwise a BIO that may support streaming. 263 1.1 christos The BIO must be freed by the caller. 264 1.1 christos On failure, they return NULL. 265 1.1 christos Failure conditions include connection/transfer timeout, parse errors, etc. 266 1.1 christos The caller is responsible for freeing the BIO pointer obtained. 267 1.1 christos 268 1.1 christos OSSL_HTTP_close() returns 0 if anything went wrong while disconnecting, else 1. 269 1.1 christos 270 1.1 christos =head1 SEE ALSO 271 1.1 christos 272 1.1 christos L<OSSL_HTTP_parse_url(3)>, L<BIO_new_connect(3)>, 273 1.1 christos L<ASN1_item_i2d_mem_bio(3)>, L<ASN1_item_d2i_bio(3)>, 274 1.1 christos L<OSSL_HTTP_is_alive(3)> 275 1.1 christos 276 1.1 christos =head1 HISTORY 277 1.1 christos 278 1.1 christos All the functions described here were added in OpenSSL 3.0. 279 1.1 christos 280 1.1 christos =head1 COPYRIGHT 281 1.1 christos 282 1.2 christos Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. 283 1.1 christos 284 1.1 christos Licensed under the Apache License 2.0 (the "License"). You may not use 285 1.1 christos this file except in compliance with the License. You can obtain a copy 286 1.1 christos in the file LICENSE in the source distribution or at 287 1.1 christos L<https://www.openssl.org/source/license.html>. 288 1.1 christos 289 1.1 christos =cut 290