xref: /freebsd/crypto/openssl/crypto/http/http_client.c (revision 10a428653ee7216475f1ddce3fb4cbf1200319f8)
1 /*
2  * Copyright 2001-2026 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright Siemens AG 2018-2020
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10 
11 #include "internal/e_os.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include "crypto/ctype.h"
15 #include <string.h>
16 #include <openssl/asn1.h>
17 #include <openssl/evp.h>
18 #include <openssl/err.h>
19 #include <openssl/httperr.h>
20 #include <openssl/cmperr.h>
21 #include <openssl/buffer.h>
22 #include <openssl/http.h>
23 #include <openssl/trace.h>
24 #include "internal/sockets.h"
25 #include "internal/common.h" /* for ossl_assert() */
26 
27 #define HTTP_PREFIX "HTTP/"
28 #define HTTP_VERSION_PATT "1." /* allow 1.x */
29 #define HTTP_VERSION_STR_LEN sizeof(HTTP_VERSION_PATT) /* == strlen("1.0") */
30 #define HTTP_PREFIX_VERSION HTTP_PREFIX "" HTTP_VERSION_PATT
31 #define HTTP_1_0 HTTP_PREFIX_VERSION "0" /* "HTTP/1.0" */
32 #define HTTP_LINE1_MINLEN (sizeof(HTTP_PREFIX_VERSION "x 200\n") - 1)
33 #define HTTP_VERSION_MAX_REDIRECTIONS 50
34 
35 #define HTTP_STATUS_CODE_OK 200
36 #define HTTP_STATUS_CODE_MOVED_PERMANENTLY 301
37 #define HTTP_STATUS_CODE_FOUND 302
38 #define HTTP_STATUS_CODES_NONFATAL_ERROR 400
39 #define HTTP_STATUS_CODE_NOT_FOUND 404
40 
41 /* Stateful HTTP request code, supporting blocking and non-blocking I/O */
42 
43 /* Opaque HTTP request status structure */
44 
45 struct ossl_http_req_ctx_st {
46     int state; /* Current I/O state */
47     unsigned char *buf; /* Buffer to write request or read response */
48     int buf_size; /* Buffer size */
49     int free_wbio; /* wbio allocated internally, free with ctx */
50     BIO *wbio; /* BIO to write/send request to */
51     BIO *rbio; /* BIO to read/receive response from */
52     OSSL_HTTP_bio_cb_t upd_fn; /* Optional BIO update callback used for TLS */
53     void *upd_arg; /* Optional arg for update callback function */
54     int use_ssl; /* Use HTTPS */
55     char *proxy; /* Optional proxy name or URI */
56     char *server; /* Optional server hostname */
57     char *port; /* Optional server port */
58     BIO *mem; /* Mem BIO holding request header or response */
59     BIO *req; /* BIO holding the request provided by caller */
60     int method_POST; /* HTTP method is POST (else GET) */
61     int text; /* Request content type is (likely) text */
62     char *expected_ct; /* Optional expected Content-Type */
63     int expect_asn1; /* Response content must be ASN.1-encoded */
64     unsigned char *pos; /* Current position sending data */
65     long len_to_send; /* Number of bytes still to send */
66     size_t resp_len; /* Length of response */
67     size_t max_resp_len; /* Maximum length of response, or 0 */
68     int keep_alive; /* Persistent conn. 0=no, 1=prefer, 2=require */
69     time_t max_time; /* Maximum end time of current transfer, or 0 */
70     time_t max_total_time; /* Maximum end time of total transfer, or 0 */
71     char *redirection_url; /* Location obtained from HTTP status 301/302 */
72     size_t max_hdr_lines; /* Max. number of response header lines, or 0 */
73 };
74 
75 /* HTTP client OSSL_HTTP_REQ_CTX_nbio() internal states, in typical order */
76 
77 #define OHS_NOREAD 0x1000 /* If set no reading should be performed */
78 #define OHS_ERROR (0 | OHS_NOREAD) /* Error condition */
79 #define OHS_ADD_HEADERS (1 | OHS_NOREAD) /* Adding header lines to request */
80 #define OHS_WRITE_INIT (2 | OHS_NOREAD) /* 1st call: ready to start send */
81 #define OHS_WRITE_HDR1 (3 | OHS_NOREAD) /* Request header to be sent */
82 #define OHS_WRITE_HDR (4 | OHS_NOREAD) /* Request header being sent */
83 #define OHS_WRITE_REQ (5 | OHS_NOREAD) /* Request content (body) being sent */
84 #define OHS_FLUSH (6 | OHS_NOREAD) /* Request being flushed */
85 
86 #define OHS_FIRSTLINE 1 /* First line of response being read */
87 #define OHS_HEADERS 2 /* MIME headers of response being read */
88 #define OHS_HEADERS_ERROR 3 /* MIME headers of response being read after fatal error */
89 #define OHS_REDIRECT 4 /* MIME headers being read, expecting Location */
90 #define OHS_ASN1_HEADER 5 /* ASN1 sequence header (tag+length) being read */
91 #define OHS_ASN1_CONTENT 6 /* ASN1 content octets being read */
92 #define OHS_ASN1_DONE 7 /* ASN1 content read completed */
93 #define OHS_STREAM 8 /* HTTP content stream to be read by caller */
94 #define OHS_ERROR_CONTENT 9 /* response content (body) being read after fatal error */
95 
96 /* Low-level HTTP API implementation */
97 
OSSL_HTTP_REQ_CTX_new(BIO * wbio,BIO * rbio,int buf_size)98 OSSL_HTTP_REQ_CTX *OSSL_HTTP_REQ_CTX_new(BIO *wbio, BIO *rbio, int buf_size)
99 {
100     OSSL_HTTP_REQ_CTX *rctx;
101 
102     if (wbio == NULL || rbio == NULL) {
103         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
104         return NULL;
105     }
106 
107     if ((rctx = OPENSSL_zalloc(sizeof(*rctx))) == NULL)
108         return NULL;
109     rctx->state = OHS_ERROR;
110     rctx->buf_size = buf_size > 0 ? buf_size : OSSL_HTTP_DEFAULT_MAX_LINE_LEN;
111     rctx->buf = OPENSSL_malloc(rctx->buf_size);
112     rctx->wbio = wbio;
113     rctx->rbio = rbio;
114     rctx->max_hdr_lines = OSSL_HTTP_DEFAULT_MAX_RESP_HDR_LINES;
115     if (rctx->buf == NULL) {
116         OPENSSL_free(rctx);
117         return NULL;
118     }
119     rctx->max_resp_len = OSSL_HTTP_DEFAULT_MAX_RESP_LEN;
120     /* everything else is 0, e.g. rctx->len_to_send, or NULL, e.g. rctx->mem  */
121     return rctx;
122 }
123 
OSSL_HTTP_REQ_CTX_free(OSSL_HTTP_REQ_CTX * rctx)124 void OSSL_HTTP_REQ_CTX_free(OSSL_HTTP_REQ_CTX *rctx)
125 {
126     if (rctx == NULL)
127         return;
128     /*
129      * Use BIO_free_all() because bio_update_fn may prepend or append to cbio.
130      * This also frees any (e.g., SSL/TLS) BIOs linked with bio and,
131      * like BIO_reset(bio), calls SSL_shutdown() to notify/alert the peer.
132      */
133     if (rctx->free_wbio)
134         BIO_free_all(rctx->wbio);
135     /* do not free rctx->rbio */
136     BIO_free(rctx->mem);
137     BIO_free(rctx->req);
138     OPENSSL_free(rctx->buf);
139     OPENSSL_free(rctx->proxy);
140     OPENSSL_free(rctx->server);
141     OPENSSL_free(rctx->port);
142     OPENSSL_free(rctx->expected_ct);
143     OPENSSL_free(rctx);
144 }
145 
OSSL_HTTP_REQ_CTX_get0_mem_bio(const OSSL_HTTP_REQ_CTX * rctx)146 BIO *OSSL_HTTP_REQ_CTX_get0_mem_bio(const OSSL_HTTP_REQ_CTX *rctx)
147 {
148     if (rctx == NULL) {
149         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
150         return NULL;
151     }
152     return rctx->mem;
153 }
154 
OSSL_HTTP_REQ_CTX_get_resp_len(const OSSL_HTTP_REQ_CTX * rctx)155 size_t OSSL_HTTP_REQ_CTX_get_resp_len(const OSSL_HTTP_REQ_CTX *rctx)
156 {
157     if (rctx == NULL) {
158         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
159         return 0;
160     }
161     return rctx->resp_len;
162 }
163 
OSSL_HTTP_REQ_CTX_set_max_response_length(OSSL_HTTP_REQ_CTX * rctx,unsigned long len)164 void OSSL_HTTP_REQ_CTX_set_max_response_length(OSSL_HTTP_REQ_CTX *rctx,
165     unsigned long len)
166 {
167     if (rctx == NULL) {
168         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
169         return;
170     }
171     rctx->max_resp_len = len != 0 ? (size_t)len : OSSL_HTTP_DEFAULT_MAX_RESP_LEN;
172 }
173 
174 /*
175  * Create request line using |rctx| and |path| (or "/" in case |path| is NULL).
176  * Server name (and optional port) must be given if and only if
177  * a plain HTTP proxy is used and |path| does not begin with 'http://'.
178  */
OSSL_HTTP_REQ_CTX_set_request_line(OSSL_HTTP_REQ_CTX * rctx,int method_POST,const char * server,const char * port,const char * path)179 int OSSL_HTTP_REQ_CTX_set_request_line(OSSL_HTTP_REQ_CTX *rctx, int method_POST,
180     const char *server, const char *port,
181     const char *path)
182 {
183     if (rctx == NULL) {
184         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
185         return 0;
186     }
187     BIO_free(rctx->mem);
188     if ((rctx->mem = BIO_new(BIO_s_mem())) == NULL)
189         return 0;
190 
191     rctx->method_POST = method_POST != 0;
192     if (BIO_printf(rctx->mem, "%s ", rctx->method_POST ? "POST" : "GET") <= 0)
193         return 0;
194 
195     if (server != NULL) { /* HTTP (but not HTTPS) proxy is used */
196         /*
197          * Section 5.1.2 of RFC 1945 states that the absoluteURI form is only
198          * allowed when using a proxy
199          */
200         if (BIO_printf(rctx->mem, OSSL_HTTP_PREFIX "%s", server) <= 0)
201             return 0;
202         if (port != NULL && BIO_printf(rctx->mem, ":%s", port) <= 0)
203             return 0;
204     }
205 
206     /* Make sure path includes a forward slash (abs_path) */
207     if (path == NULL) {
208         path = "/";
209     } else if (HAS_PREFIX(path, "http://")) { /* absoluteURI for proxy use */
210         if (server != NULL) {
211             ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT);
212             return 0;
213         }
214     } else if (path[0] != '/' && BIO_printf(rctx->mem, "/") <= 0) {
215         return 0;
216     }
217     /*
218      * Add (the rest of) the path and the HTTP version,
219      * which is fixed to 1.0 for straightforward implementation of keep-alive
220      */
221     if (BIO_printf(rctx->mem, "%s " HTTP_1_0 "\r\n", path) <= 0)
222         return 0;
223 
224     rctx->resp_len = 0;
225     rctx->state = OHS_ADD_HEADERS;
226     return 1;
227 }
228 
OSSL_HTTP_REQ_CTX_add1_header(OSSL_HTTP_REQ_CTX * rctx,const char * name,const char * value)229 int OSSL_HTTP_REQ_CTX_add1_header(OSSL_HTTP_REQ_CTX *rctx,
230     const char *name, const char *value)
231 {
232     if (rctx == NULL || name == NULL) {
233         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
234         return 0;
235     }
236     if (rctx->mem == NULL) {
237         ERR_raise(ERR_LIB_HTTP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
238         return 0;
239     }
240 
241     if (BIO_puts(rctx->mem, name) <= 0)
242         return 0;
243     if (value != NULL) {
244         if (BIO_write(rctx->mem, ": ", 2) != 2)
245             return 0;
246         if (BIO_puts(rctx->mem, value) <= 0)
247             return 0;
248     }
249     return BIO_write(rctx->mem, "\r\n", 2) == 2;
250 }
251 
OSSL_HTTP_REQ_CTX_set_expected(OSSL_HTTP_REQ_CTX * rctx,const char * content_type,int asn1,int timeout,int keep_alive)252 int OSSL_HTTP_REQ_CTX_set_expected(OSSL_HTTP_REQ_CTX *rctx,
253     const char *content_type, int asn1,
254     int timeout, int keep_alive)
255 {
256     if (rctx == NULL) {
257         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
258         return 0;
259     }
260     if (keep_alive != 0
261         && rctx->state != OHS_ERROR && rctx->state != OHS_ADD_HEADERS) {
262         /* Cannot anymore set keep-alive in request header */
263         ERR_raise(ERR_LIB_HTTP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
264         return 0;
265     }
266 
267     OPENSSL_free(rctx->expected_ct);
268     rctx->expected_ct = NULL;
269     if (content_type != NULL
270         && (rctx->expected_ct = OPENSSL_strdup(content_type)) == NULL)
271         return 0;
272 
273     rctx->expect_asn1 = asn1;
274     if (timeout >= 0)
275         rctx->max_time = timeout > 0 ? time(NULL) + timeout : 0;
276     else /* take over any |overall_timeout| arg of OSSL_HTTP_open(), else 0 */
277         rctx->max_time = rctx->max_total_time;
278     rctx->keep_alive = keep_alive;
279     return 1;
280 }
281 
set1_content(OSSL_HTTP_REQ_CTX * rctx,const char * content_type,BIO * req)282 static int set1_content(OSSL_HTTP_REQ_CTX *rctx,
283     const char *content_type, BIO *req)
284 {
285     long req_len = 0;
286 #ifndef OPENSSL_NO_STDIO
287     FILE *fp = NULL;
288 #endif
289 
290     if (rctx == NULL || (req == NULL && content_type != NULL)) {
291         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
292         return 0;
293     }
294 
295     if (rctx->keep_alive != 0
296         && !OSSL_HTTP_REQ_CTX_add1_header(rctx, "Connection", "keep-alive"))
297         return 0;
298 
299     BIO_free(rctx->req);
300     rctx->req = NULL;
301     if (req == NULL)
302         return 1;
303     if (!rctx->method_POST) {
304         ERR_raise(ERR_LIB_HTTP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
305         return 0;
306     }
307 
308     if (content_type == NULL) {
309         rctx->text = 1; /* assuming request to be text by default, used just for tracing */
310     } else {
311         if (HAS_CASE_PREFIX(content_type, "text/"))
312             rctx->text = 1;
313         if (BIO_printf(rctx->mem, "Content-Type: %s\r\n", content_type) <= 0)
314             return 0;
315     }
316 
317     /*
318      * BIO_CTRL_INFO yields the data length at least for memory BIOs, but for
319      * file-based BIOs it gives the current position, which is not what we need.
320      */
321     if (BIO_method_type(req) == BIO_TYPE_FILE) {
322 #ifndef OPENSSL_NO_STDIO
323         if (BIO_get_fp(req, &fp) == 1 && fseek(fp, 0, SEEK_END) == 0) {
324             req_len = ftell(fp);
325             (void)fseek(fp, 0, SEEK_SET);
326         } else {
327             fp = NULL;
328         }
329 #endif
330     } else {
331         req_len = BIO_ctrl(req, BIO_CTRL_INFO, 0, NULL);
332         /*
333          * Streaming BIOs likely will not support querying the size at all,
334          * and we assume we got a correct value if req_len > 0.
335          */
336     }
337     if ((
338 #ifndef OPENSSL_NO_STDIO
339             fp != NULL /* definitely correct req_len */ ||
340 #endif
341             req_len > 0)
342         && BIO_printf(rctx->mem, "Content-Length: %ld\r\n", req_len) < 0)
343         return 0;
344 
345     if (!BIO_up_ref(req))
346         return 0;
347     rctx->req = req;
348     return 1;
349 }
350 
OSSL_HTTP_REQ_CTX_set1_req(OSSL_HTTP_REQ_CTX * rctx,const char * content_type,const ASN1_ITEM * it,const ASN1_VALUE * req)351 int OSSL_HTTP_REQ_CTX_set1_req(OSSL_HTTP_REQ_CTX *rctx, const char *content_type,
352     const ASN1_ITEM *it, const ASN1_VALUE *req)
353 {
354     BIO *mem = NULL;
355     int res = 1;
356 
357     if (req != NULL)
358         res = (mem = ASN1_item_i2d_mem_bio(it, req)) != NULL;
359     res = res && set1_content(rctx, content_type, mem);
360     BIO_free(mem);
361     return res;
362 }
363 
OSSL_HTTP_REQ_CTX_set_max_response_hdr_lines(OSSL_HTTP_REQ_CTX * rctx,size_t count)364 void OSSL_HTTP_REQ_CTX_set_max_response_hdr_lines(OSSL_HTTP_REQ_CTX *rctx,
365     size_t count)
366 {
367     if (rctx == NULL) {
368         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
369         return;
370     }
371     rctx->max_hdr_lines = count;
372 }
373 
add1_headers(OSSL_HTTP_REQ_CTX * rctx,const STACK_OF (CONF_VALUE)* headers,const char * host)374 static int add1_headers(OSSL_HTTP_REQ_CTX *rctx,
375     const STACK_OF(CONF_VALUE) *headers, const char *host)
376 {
377     int i;
378     int add_host = host != NULL && *host != '\0';
379     CONF_VALUE *hdr;
380 
381     for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
382         hdr = sk_CONF_VALUE_value(headers, i);
383         if (add_host && OPENSSL_strcasecmp("host", hdr->name) == 0)
384             add_host = 0;
385         if (!OSSL_HTTP_REQ_CTX_add1_header(rctx, hdr->name, hdr->value))
386             return 0;
387     }
388 
389     if (add_host && !OSSL_HTTP_REQ_CTX_add1_header(rctx, "Host", host))
390         return 0;
391     return 1;
392 }
393 
394 /* Create OSSL_HTTP_REQ_CTX structure using the values provided. */
http_req_ctx_new(int free_wbio,BIO * wbio,BIO * rbio,OSSL_HTTP_bio_cb_t bio_update_fn,void * arg,int use_ssl,const char * proxy,const char * server,const char * port,int buf_size,int overall_timeout)395 static OSSL_HTTP_REQ_CTX *http_req_ctx_new(int free_wbio, BIO *wbio, BIO *rbio,
396     OSSL_HTTP_bio_cb_t bio_update_fn,
397     void *arg, int use_ssl,
398     const char *proxy,
399     const char *server, const char *port,
400     int buf_size, int overall_timeout)
401 {
402     OSSL_HTTP_REQ_CTX *rctx = OSSL_HTTP_REQ_CTX_new(wbio, rbio, buf_size);
403 
404     if (rctx == NULL)
405         return NULL;
406     rctx->free_wbio = free_wbio;
407     rctx->upd_fn = bio_update_fn;
408     rctx->upd_arg = arg;
409     rctx->use_ssl = use_ssl;
410     if (proxy != NULL
411         && (rctx->proxy = OPENSSL_strdup(proxy)) == NULL)
412         goto err;
413     if (server != NULL
414         && (rctx->server = OPENSSL_strdup(server)) == NULL)
415         goto err;
416     if (port != NULL
417         && (rctx->port = OPENSSL_strdup(port)) == NULL)
418         goto err;
419     rctx->max_total_time = overall_timeout > 0 ? time(NULL) + overall_timeout : 0;
420     return rctx;
421 
422 err:
423     OSSL_HTTP_REQ_CTX_free(rctx);
424     return NULL;
425 }
426 
427 /*
428  * Parse first HTTP response line. This should be like this: "HTTP/1.0 200 OK".
429  * We need to obtain the status code and (optional) informational message.
430  * Return any received HTTP response status code, or 0 on fatal error.
431  */
432 
parse_http_line1(char * line,int * found_keep_alive)433 static int parse_http_line1(char *line, int *found_keep_alive)
434 {
435     int i, retcode;
436     char *code, *reason, *end;
437 
438     if (!CHECK_AND_SKIP_PREFIX(line, HTTP_PREFIX_VERSION))
439         goto err;
440     /* above HTTP 1.0, connection persistence is the default */
441     *found_keep_alive = *line > '0';
442 
443     /* Skip to first whitespace (past protocol info) */
444     for (code = line; *code != '\0' && !ossl_isspace(*code); code++)
445         continue;
446     if (*code == '\0')
447         goto err;
448 
449     /* Skip past whitespace to start of response code */
450     while (*code != '\0' && ossl_isspace(*code))
451         code++;
452     if (*code == '\0')
453         goto err;
454 
455     /* Find end of response code: first whitespace after start of code */
456     for (reason = code; *reason != '\0' && !ossl_isspace(*reason); reason++)
457         continue;
458 
459     if (*reason == '\0')
460         goto err;
461 
462     /* Set end of response code and start of message */
463     *reason++ = '\0';
464 
465     /* Attempt to parse numeric code */
466     retcode = strtoul(code, &end, 10);
467     if (*end != '\0')
468         goto err;
469 
470     /* Skip over any leading whitespace in message */
471     while (*reason != '\0' && ossl_isspace(*reason))
472         reason++;
473 
474     if (*reason != '\0') {
475         /*
476          * Finally zap any trailing whitespace in message (include CRLF)
477          */
478 
479         /* chop any trailing whitespace from reason */
480         /* We know reason has a non-whitespace character so this is OK */
481         for (end = reason + strlen(reason) - 1; ossl_isspace(*end); end--)
482             *end = '\0';
483     }
484 
485     switch (retcode) {
486     case HTTP_STATUS_CODE_OK:
487     case HTTP_STATUS_CODE_MOVED_PERMANENTLY:
488     case HTTP_STATUS_CODE_FOUND:
489         return retcode;
490     default:
491         if (retcode == HTTP_STATUS_CODE_NOT_FOUND
492             || retcode < HTTP_STATUS_CODES_NONFATAL_ERROR) {
493             ERR_raise_data(ERR_LIB_HTTP, HTTP_R_STATUS_CODE_UNSUPPORTED, "code=%s", code);
494             if (*reason != '\0')
495                 ERR_add_error_data(2, ", reason=", reason);
496         } /* must return content normally if status >= 400, still tentatively raised error on 404 */
497         return retcode;
498     }
499 
500 err:
501     for (i = 0; i < 60 && line[i] != '\0'; i++)
502         if (!ossl_isprint(line[i]))
503             line[i] = ' ';
504     line[i] = '\0';
505     ERR_raise_data(ERR_LIB_HTTP, HTTP_R_HEADER_PARSE_ERROR, "content=%s", line);
506     return 0;
507 }
508 
check_max_len(const char * desc,size_t max_len,size_t len)509 static int check_max_len(const char *desc, size_t max_len, size_t len)
510 {
511     if (max_len != 0 && len > max_len) {
512         ERR_raise_data(ERR_LIB_HTTP, HTTP_R_MAX_RESP_LEN_EXCEEDED,
513             "%s length=%zu, max=%zu", desc, len, max_len);
514         return 0;
515     }
516     return 1;
517 }
518 
check_set_resp_len(const char * desc,OSSL_HTTP_REQ_CTX * rctx,size_t len)519 static int check_set_resp_len(const char *desc, OSSL_HTTP_REQ_CTX *rctx, size_t len)
520 {
521     if (!check_max_len(desc, rctx->max_resp_len, len))
522         return 0;
523     if (rctx->resp_len != 0 && rctx->resp_len != len) {
524         ERR_raise_data(ERR_LIB_HTTP, HTTP_R_INCONSISTENT_CONTENT_LENGTH,
525             "%s length=%zu, Content-Length=%zu", desc, len, rctx->resp_len);
526         return 0;
527     }
528     rctx->resp_len = len;
529     return 1;
530 }
531 
may_still_retry(time_t max_time,int * ptimeout)532 static int may_still_retry(time_t max_time, int *ptimeout)
533 {
534     time_t time_diff, now = time(NULL);
535 
536     if (max_time != 0) {
537         if (max_time < now) {
538             ERR_raise(ERR_LIB_HTTP, HTTP_R_RETRY_TIMEOUT);
539             return 0;
540         }
541         time_diff = max_time - now;
542         *ptimeout = time_diff > INT_MAX ? INT_MAX : (int)time_diff;
543     }
544     return 1;
545 }
546 
547 /*
548  * Try exchanging request and response via HTTP on (non-)blocking BIO in rctx.
549  * Returns 1 on success, 0 on error or redirection, -1 on BIO_should_retry.
550  */
OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX * rctx)551 int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx)
552 {
553     int i, found_expected_ct = 0, found_keep_alive = 0;
554     int status_code = 0;
555     int got_text = 1;
556     long n;
557     size_t resp_len = 0;
558     const unsigned char *p;
559     char *buf, *key, *value, *line_end = NULL;
560     size_t resp_hdr_lines = 0;
561 
562     if (rctx == NULL) {
563         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
564         return 0;
565     }
566     if (rctx->mem == NULL || rctx->wbio == NULL || rctx->rbio == NULL) {
567         ERR_raise(ERR_LIB_HTTP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
568         return 0;
569     }
570 
571     rctx->redirection_url = NULL;
572 next_io:
573     buf = (char *)rctx->buf;
574     if ((rctx->state & OHS_NOREAD) == 0) {
575         if (rctx->expect_asn1 && (rctx->state == OHS_ASN1_HEADER || rctx->state == OHS_ASN1_CONTENT)) {
576             n = BIO_read(rctx->rbio, buf, rctx->buf_size);
577         } else { /* read one text line */
578             (void)ERR_set_mark();
579             n = BIO_gets(rctx->rbio, buf, rctx->buf_size);
580             if (n == -2) { /* some BIOs, such as SSL, do not support "gets" */
581                 (void)ERR_pop_to_mark();
582                 n = BIO_get_line(rctx->rbio, buf, rctx->buf_size);
583             } else {
584                 (void)ERR_clear_last_mark();
585             }
586         }
587         if (n <= 0) {
588             if (rctx->state == OHS_ERROR_CONTENT) {
589                 if (OSSL_TRACE_ENABLED(HTTP))
590                     OSSL_TRACE(HTTP, "]\n"); /* end of error response content */
591                 /* in addition, throw error on inconsistent length: */
592                 (void)check_set_resp_len("error response content", rctx, resp_len);
593                 return 0;
594             }
595             if (BIO_should_retry(rctx->rbio))
596                 return -1;
597             ERR_raise(ERR_LIB_HTTP, HTTP_R_FAILED_READING_DATA);
598             return 0;
599         }
600 
601         /* Write data to memory BIO */
602         if (BIO_write(rctx->mem, buf, n) != n)
603             return 0;
604     }
605 
606     switch (rctx->state) {
607     case OHS_ERROR:
608     default:
609         return 0;
610 
611     case OHS_ADD_HEADERS:
612         /* Last operation was adding headers: need a final \r\n */
613         if (BIO_write(rctx->mem, "\r\n", 2) != 2) {
614             rctx->state = OHS_ERROR;
615             return 0;
616         }
617         rctx->state = OHS_WRITE_INIT;
618 
619         /* fall through */
620     case OHS_WRITE_INIT:
621         rctx->len_to_send = BIO_get_mem_data(rctx->mem, &rctx->pos);
622         rctx->state = OHS_WRITE_HDR1;
623 
624         /* fall through */
625     case OHS_WRITE_HDR1:
626     case OHS_WRITE_HDR:
627         /* Copy some chunk of data from rctx->mem to rctx->wbio */
628     case OHS_WRITE_REQ:
629         /* Copy some chunk of data from rctx->req to rctx->wbio */
630 
631         if (rctx->len_to_send > 0) {
632             size_t sz;
633 
634             if (!BIO_write_ex(rctx->wbio, rctx->pos, rctx->len_to_send, &sz)) {
635                 if (BIO_should_retry(rctx->wbio))
636                     return -1;
637                 rctx->state = OHS_ERROR;
638                 return 0;
639             }
640             if (OSSL_TRACE_ENABLED(HTTP)) {
641                 if (rctx->state == OHS_WRITE_HDR1)
642                     OSSL_TRACE(HTTP, "Sending request header: [\n");
643                 /* for request headers, this usually traces several lines at once: */
644                 OSSL_TRACE_STRING(HTTP, rctx->state != OHS_WRITE_REQ || rctx->text,
645                     rctx->state != OHS_WRITE_REQ, rctx->pos, sz);
646                 OSSL_TRACE(HTTP, "]\n"); /* end of request header or content */
647             }
648             if (rctx->state == OHS_WRITE_HDR1)
649                 rctx->state = OHS_WRITE_HDR;
650             rctx->pos += sz;
651             rctx->len_to_send -= sz;
652             goto next_io;
653         }
654         if (rctx->state == OHS_WRITE_HDR) {
655             (void)BIO_reset(rctx->mem);
656             rctx->state = OHS_WRITE_REQ;
657         }
658         if (rctx->req != NULL && !BIO_eof(rctx->req)) {
659             if (OSSL_TRACE_ENABLED(HTTP))
660                 OSSL_TRACE1(HTTP, "Sending request content (likely %s)\n",
661                     rctx->text ? "text" : "ASN.1");
662             n = BIO_read(rctx->req, rctx->buf, rctx->buf_size);
663             if (n <= 0) {
664                 if (BIO_should_retry(rctx->req))
665                     return -1;
666                 ERR_raise(ERR_LIB_HTTP, HTTP_R_FAILED_READING_DATA);
667                 return 0;
668             }
669             rctx->pos = rctx->buf;
670             rctx->len_to_send = n;
671             goto next_io;
672         }
673         rctx->state = OHS_FLUSH;
674 
675         /* fall through */
676     case OHS_FLUSH:
677 
678         i = BIO_flush(rctx->wbio);
679 
680         if (i > 0) {
681             rctx->state = OHS_FIRSTLINE;
682             goto next_io;
683         }
684 
685         if (BIO_should_retry(rctx->wbio))
686             return -1;
687 
688         rctx->state = OHS_ERROR;
689         return 0;
690 
691         /* State machine could be broken up at this point and bulky code sections factorized out. */
692 
693     case OHS_FIRSTLINE:
694     case OHS_HEADERS:
695     case OHS_HEADERS_ERROR:
696     case OHS_REDIRECT:
697     case OHS_ERROR_CONTENT:
698 
699         /* Attempt to read a line in */
700     next_line:
701         /*
702          * Due to strange memory BIO behavior with BIO_gets we have to check
703          * there's a complete line in there before calling BIO_gets or we'll
704          * just get a partial read.
705          */
706         n = BIO_get_mem_data(rctx->mem, &p);
707         if (n <= 0 || memchr(p, '\n', n) == 0) {
708             if (n >= rctx->buf_size) {
709                 rctx->state = OHS_ERROR;
710                 return 0;
711             }
712             goto next_io;
713         }
714         n = BIO_gets(rctx->mem, buf, rctx->buf_size);
715 
716         if (n <= 0) {
717             if (BIO_should_retry(rctx->mem))
718                 goto next_io;
719             rctx->state = OHS_ERROR;
720             return 0;
721         }
722 
723         if (rctx->state == OHS_ERROR_CONTENT) {
724             resp_len += n;
725             if (!check_max_len("error response content", rctx->max_resp_len, resp_len))
726                 return 0;
727             if (OSSL_TRACE_ENABLED(HTTP)) /* dump response content line */
728                 OSSL_TRACE_STRING(HTTP, got_text, 1, (unsigned char *)buf, n);
729             goto next_line;
730         }
731 
732         resp_hdr_lines++;
733         if (rctx->max_hdr_lines != 0 && rctx->max_hdr_lines < resp_hdr_lines) {
734             ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_TOO_MANY_HDRLINES);
735             rctx->state = OHS_ERROR;
736             return 0;
737         }
738 
739         /* Don't allow excessive lines */
740         if (n == rctx->buf_size) {
741             ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_LINE_TOO_LONG);
742             rctx->state = OHS_ERROR;
743             return 0;
744         }
745 
746         if (OSSL_TRACE_ENABLED(HTTP)) {
747             /* dump all response header line */
748             if (rctx->state == OHS_FIRSTLINE)
749                 OSSL_TRACE(HTTP, "Receiving response header: [\n");
750             OSSL_TRACE_STRING(HTTP, 1, 1, (unsigned char *)buf, n);
751         }
752 
753         /* First line in response header */
754         if (rctx->state == OHS_FIRSTLINE) {
755             status_code = parse_http_line1(buf, &found_keep_alive);
756             switch (status_code) {
757             case HTTP_STATUS_CODE_OK:
758                 rctx->state = OHS_HEADERS;
759                 goto next_line;
760             case HTTP_STATUS_CODE_MOVED_PERMANENTLY:
761             case HTTP_STATUS_CODE_FOUND: /* i.e., moved temporarily */
762                 if (!rctx->method_POST) { /* method is GET */
763                     rctx->state = OHS_REDIRECT;
764                     goto next_line;
765                 }
766                 ERR_raise(ERR_LIB_HTTP, HTTP_R_REDIRECTION_NOT_ENABLED);
767                 /* redirection is not supported/recommended for POST */
768                 /* fall through */
769             default:
770                 /* must return content if status >= 400 */
771                 rctx->state = status_code < HTTP_STATUS_CODES_NONFATAL_ERROR
772                     ? OHS_HEADERS_ERROR
773                     : OHS_HEADERS;
774                 goto next_line; /* continue parsing, also on HTTP error */
775             }
776         }
777         key = buf;
778         value = strchr(key, ':');
779         if (value != NULL) {
780             *(value++) = '\0';
781             while (ossl_isspace(*value))
782                 value++;
783             line_end = strchr(value, '\r');
784             if (line_end == NULL)
785                 line_end = strchr(value, '\n');
786             if (line_end != NULL)
787                 *line_end = '\0';
788         }
789         if (value != NULL && line_end != NULL) {
790             if (rctx->state == OHS_REDIRECT
791                 && OPENSSL_strcasecmp(key, "Location") == 0) {
792                 rctx->redirection_url = value;
793                 if (OSSL_TRACE_ENABLED(HTTP))
794                     OSSL_TRACE(HTTP, "]\n");
795                 /* stop reading due to redirect */
796                 (void)BIO_reset(rctx->rbio);
797                 return 0;
798             }
799             if (OPENSSL_strcasecmp(key, "Content-Type") == 0) {
800                 got_text = HAS_CASE_PREFIX(value, "text/");
801                 if (got_text
802                     && rctx->state == OHS_HEADERS
803                     && rctx->expect_asn1
804                     && (status_code >= HTTP_STATUS_CODES_NONFATAL_ERROR
805                         || status_code == HTTP_STATUS_CODE_OK)) {
806                     ERR_raise_data(ERR_LIB_HTTP, HTTP_R_CONTENT_TYPE_MISMATCH,
807                         "expected ASN.1 content but got http code %d with Content-Type: %s",
808                         status_code, value);
809                     rctx->state = OHS_HEADERS_ERROR;
810                     goto next_line;
811                 }
812                 if (rctx->state == OHS_HEADERS
813                     && rctx->expected_ct != NULL) {
814                     const char *semicolon;
815 
816                     if (OPENSSL_strcasecmp(rctx->expected_ct, value) != 0
817                         /* ignore past ';' unless expected_ct contains ';' */
818                         && (strchr(rctx->expected_ct, ';') != NULL
819                             || (semicolon = strchr(value, ';')) == NULL
820                             || (size_t)(semicolon - value) != strlen(rctx->expected_ct)
821                             || OPENSSL_strncasecmp(rctx->expected_ct, value,
822                                    semicolon - value)
823                                 != 0)) {
824                         ERR_raise_data(ERR_LIB_HTTP,
825                             HTTP_R_UNEXPECTED_CONTENT_TYPE,
826                             "expected=%s, actual=%s",
827                             rctx->expected_ct, value);
828                         return 0;
829                     }
830                     found_expected_ct = 1;
831                 }
832             }
833 
834             /* https://tools.ietf.org/html/rfc7230#section-6.3 Persistence */
835             if (OPENSSL_strcasecmp(key, "Connection") == 0) {
836                 if (OPENSSL_strcasecmp(value, "keep-alive") == 0)
837                     found_keep_alive = 1;
838                 else if (OPENSSL_strcasecmp(value, "close") == 0)
839                     found_keep_alive = 0;
840             } else if (OPENSSL_strcasecmp(key, "Content-Length") == 0) {
841                 size_t content_len = (size_t)strtoul(value, &line_end, 10);
842 
843                 if (line_end == value || *line_end != '\0') {
844                     ERR_raise_data(ERR_LIB_HTTP,
845                         HTTP_R_ERROR_PARSING_CONTENT_LENGTH,
846                         "input=%s", value);
847                     return 0;
848                 }
849                 if (!check_set_resp_len("response content-length", rctx, content_len))
850                     return 0;
851             }
852         }
853 
854         /* Look for blank line indicating end of headers */
855         for (p = rctx->buf; *p != '\0'; p++) {
856             if (*p != '\r' && *p != '\n')
857                 break;
858         }
859         if (*p != '\0') /* not end of headers or not end of error response content */
860             goto next_line;
861 
862         /* Found blank line(s) indicating end of headers */
863         if (OSSL_TRACE_ENABLED(HTTP))
864             OSSL_TRACE(HTTP, "]\n"); /* end of response header */
865 
866         if (rctx->keep_alive != 0 /* do not let server initiate keep_alive */
867             && !found_keep_alive /* otherwise there is no change */) {
868             if (rctx->keep_alive == 2) {
869                 rctx->keep_alive = 0;
870                 ERR_raise(ERR_LIB_HTTP, HTTP_R_SERVER_CANCELED_CONNECTION);
871                 return 0;
872             }
873             rctx->keep_alive = 0;
874         }
875 
876         if (rctx->state == OHS_HEADERS_ERROR) {
877             rctx->state = OHS_ERROR_CONTENT;
878             if (OSSL_TRACE_ENABLED(HTTP)) {
879                 OSSL_TRACE1(HTTP, "Receiving error response content (likely %s): [\n",
880                     got_text ? "text" : "ASN.1");
881                 goto next_line;
882             }
883             /* discard response content when trace not enabled */
884             (void)BIO_reset(rctx->rbio);
885             return 0;
886         }
887 
888         if (rctx->expected_ct != NULL && !found_expected_ct) {
889             ERR_raise_data(ERR_LIB_HTTP, HTTP_R_MISSING_CONTENT_TYPE,
890                 "expected=%s", rctx->expected_ct);
891             return 0;
892         }
893         if (rctx->state == OHS_REDIRECT) {
894             /* http status code indicated redirect but there was no Location */
895             ERR_raise(ERR_LIB_HTTP, HTTP_R_MISSING_REDIRECT_LOCATION);
896             return 0;
897         }
898 
899         /* Note: in non-error situations cannot trace response content */
900         if (!rctx->expect_asn1) {
901             if (OSSL_TRACE_ENABLED(HTTP))
902                 OSSL_TRACE(HTTP, "Receiving response text content\n");
903             rctx->state = OHS_STREAM;
904             return 1;
905         }
906 
907         if (OSSL_TRACE_ENABLED(HTTP))
908             OSSL_TRACE(HTTP, "Receiving response ASN.1 content\n");
909         rctx->state = OHS_ASN1_HEADER;
910 
911         /* Fall thru */
912     case OHS_ASN1_HEADER:
913         /*
914          * Now reading ASN1 header: can read at least 2 bytes which is enough
915          * for ASN1 SEQUENCE header and either length field or at least the
916          * length of the length field.
917          */
918         n = BIO_get_mem_data(rctx->mem, &p);
919         if (n < 2)
920             goto next_io;
921 
922         /* Check it is an ASN1 SEQUENCE */
923         if (*p++ != (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
924             ERR_raise(ERR_LIB_HTTP, HTTP_R_MISSING_ASN1_ENCODING);
925             return 0;
926         }
927 
928         /* Check out length field */
929         if ((*p & 0x80) != 0) {
930             /*
931              * If MSB set on initial length octet we can now always read 6
932              * octets: make sure we have them.
933              */
934             if (n < 6)
935                 goto next_io;
936             n = *p & 0x7F;
937             /* Not NDEF or excessive length */
938             if (n == 0 || (n > 4)) {
939                 ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_PARSING_ASN1_LENGTH);
940                 return 0;
941             }
942             p++;
943             resp_len = 0;
944             for (i = 0; i < n; i++) {
945                 resp_len <<= 8;
946                 resp_len |= *p++;
947             }
948             resp_len += n + 2;
949         } else {
950             resp_len = *p + 2;
951         }
952         if (!check_set_resp_len("ASN.1 DER content", rctx, resp_len))
953             return 0;
954 
955         if (OSSL_TRACE_ENABLED(HTTP))
956             OSSL_TRACE1(HTTP, "Expected response ASN.1 DER content length: %zd\n", resp_len);
957         rctx->state = OHS_ASN1_CONTENT;
958 
959         /* Fall thru */
960     case OHS_ASN1_CONTENT:
961         n = BIO_get_mem_data(rctx->mem, NULL);
962         if (n < 0 || (size_t)n < rctx->resp_len)
963             goto next_io;
964 
965         if (OSSL_TRACE_ENABLED(HTTP))
966             OSSL_TRACE(HTTP, "Finished receiving response ASN.1 content\n");
967         rctx->state = OHS_ASN1_DONE;
968         return 1;
969     }
970 }
971 
OSSL_HTTP_REQ_CTX_nbio_d2i(OSSL_HTTP_REQ_CTX * rctx,ASN1_VALUE ** pval,const ASN1_ITEM * it)972 int OSSL_HTTP_REQ_CTX_nbio_d2i(OSSL_HTTP_REQ_CTX *rctx,
973     ASN1_VALUE **pval, const ASN1_ITEM *it)
974 {
975     const unsigned char *p;
976     int rv;
977 
978     *pval = NULL;
979     if ((rv = OSSL_HTTP_REQ_CTX_nbio(rctx)) != 1)
980         return rv;
981     *pval = ASN1_item_d2i(NULL, &p, BIO_get_mem_data(rctx->mem, &p), it);
982     return *pval != NULL;
983 }
984 
985 #ifndef OPENSSL_NO_SOCK
986 
explict_or_default_port(const char * hostserv,const char * port,int use_ssl)987 static const char *explict_or_default_port(const char *hostserv, const char *port, int use_ssl)
988 {
989     if (port == NULL) {
990         char *service = NULL;
991 
992         if (!BIO_parse_hostserv(hostserv, NULL, &service, BIO_PARSE_PRIO_HOST))
993             return NULL;
994         if (service == NULL) /* implicit port */
995             port = use_ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT;
996         OPENSSL_free(service);
997     } /* otherwise take the explicitly given port */
998     return port;
999 }
1000 
1001 /* set up a new connection BIO, to HTTP server or to HTTP(S) proxy if given */
http_new_bio(const char * server,const char * server_port,int use_ssl,const char * proxy,const char * proxy_port)1002 static BIO *http_new_bio(const char *server /* optionally includes ":port" */,
1003     const char *server_port /* explicit server port */,
1004     int use_ssl,
1005     const char *proxy /* optionally includes ":port" */,
1006     const char *proxy_port /* explicit proxy port */)
1007 {
1008     const char *host = server;
1009     const char *port = server_port;
1010     BIO *cbio;
1011 
1012     if (!ossl_assert(server != NULL))
1013         return NULL;
1014 
1015     if (proxy != NULL) {
1016         host = proxy;
1017         port = proxy_port;
1018     }
1019 
1020     port = explict_or_default_port(host, port, use_ssl);
1021 
1022     cbio = BIO_new_connect(host /* optionally includes ":port" */);
1023     if (cbio == NULL)
1024         goto end;
1025     if (port != NULL)
1026         (void)BIO_set_conn_port(cbio, port);
1027 
1028 end:
1029     return cbio;
1030 }
1031 #endif /* OPENSSL_NO_SOCK */
1032 
1033 /* Exchange request and response via HTTP on (non-)blocking BIO */
OSSL_HTTP_REQ_CTX_exchange(OSSL_HTTP_REQ_CTX * rctx)1034 BIO *OSSL_HTTP_REQ_CTX_exchange(OSSL_HTTP_REQ_CTX *rctx)
1035 {
1036     int rv;
1037 
1038     if (rctx == NULL) {
1039         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
1040         return NULL;
1041     }
1042 
1043     for (;;) {
1044         rv = OSSL_HTTP_REQ_CTX_nbio(rctx);
1045         if (rv != -1)
1046             break;
1047         /* BIO_should_retry was true */
1048         /* will not actually wait if rctx->max_time == 0 */
1049         if (BIO_wait(rctx->rbio, rctx->max_time, 100 /* milliseconds */) <= 0)
1050             return NULL;
1051     }
1052 
1053     if (rv == 0) {
1054         if (rctx->redirection_url == NULL) { /* an error occurred */
1055             if (rctx->len_to_send > 0)
1056                 ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_SENDING);
1057             else
1058                 ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_RECEIVING);
1059         }
1060         return NULL;
1061     }
1062     return rctx->state == OHS_STREAM ? rctx->rbio : rctx->mem;
1063 }
1064 
OSSL_HTTP_is_alive(const OSSL_HTTP_REQ_CTX * rctx)1065 int OSSL_HTTP_is_alive(const OSSL_HTTP_REQ_CTX *rctx)
1066 {
1067     return rctx != NULL && rctx->keep_alive != 0;
1068 }
1069 
1070 /* High-level HTTP API implementation */
1071 
1072 /* Initiate an HTTP session using bio, else use given server, proxy, etc. */
OSSL_HTTP_open(const char * server,const char * port,const char * proxy,const char * no_proxy,int use_ssl,BIO * bio,BIO * rbio,OSSL_HTTP_bio_cb_t bio_update_fn,void * arg,int buf_size,int overall_timeout)1073 OSSL_HTTP_REQ_CTX *OSSL_HTTP_open(const char *server, const char *port,
1074     const char *proxy, const char *no_proxy,
1075     int use_ssl, BIO *bio, BIO *rbio,
1076     OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
1077     int buf_size, int overall_timeout)
1078 {
1079     BIO *cbio; /* == bio if supplied, used as connection BIO if rbio is NULL */
1080     OSSL_HTTP_REQ_CTX *rctx = NULL;
1081 
1082     if (use_ssl && bio_update_fn == NULL) {
1083         ERR_raise(ERR_LIB_HTTP, HTTP_R_TLS_NOT_ENABLED);
1084         return NULL;
1085     }
1086     if (rbio != NULL && (bio == NULL || bio_update_fn != NULL)) {
1087         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT);
1088         return NULL;
1089     }
1090 
1091     if (bio != NULL) {
1092         cbio = bio;
1093         if (proxy != NULL || no_proxy != NULL) {
1094             ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT);
1095             return NULL;
1096         }
1097     } else {
1098 #ifndef OPENSSL_NO_SOCK
1099         char *proxy_host = NULL, *proxy_port = NULL;
1100 
1101         if (server == NULL) {
1102             ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
1103             return NULL;
1104         }
1105         if (port != NULL && *port == '\0')
1106             port = NULL;
1107         proxy = OSSL_HTTP_adapt_proxy(proxy, no_proxy, server, use_ssl);
1108         if (proxy != NULL
1109             && !OSSL_HTTP_parse_url(proxy, NULL /* use_ssl */, NULL /* user */,
1110                 &proxy_host, &proxy_port, NULL /* num */,
1111                 NULL /* path */, NULL, NULL))
1112             return NULL;
1113         cbio = http_new_bio(server, port, use_ssl, proxy_host, proxy_port);
1114         OPENSSL_free(proxy_host);
1115         OPENSSL_free(proxy_port);
1116         if (cbio == NULL)
1117             return NULL;
1118 #else
1119         ERR_raise(ERR_LIB_HTTP, HTTP_R_SOCK_NOT_SUPPORTED);
1120         return NULL;
1121 #endif
1122     }
1123 
1124     (void)ERR_set_mark(); /* prepare removing any spurious libssl errors */
1125     if (rbio == NULL && BIO_do_connect_retry(cbio, overall_timeout, -1) <= 0) {
1126         if (bio == NULL) /* cbio was not provided by caller */
1127             BIO_free_all(cbio);
1128         goto end;
1129     }
1130     /* now overall_timeout is guaranteed to be >= 0 */
1131 
1132     /* adapt in order to fix callback design flaw, see #17088 */
1133     /* callback can be used to wrap or prepend TLS session */
1134     if (bio_update_fn != NULL) {
1135         BIO *orig_bio = cbio;
1136 
1137         cbio = (*bio_update_fn)(cbio, arg, 1 /* connect */, use_ssl != 0);
1138         if (cbio == NULL) {
1139             if (bio == NULL) /* cbio was not provided by caller */
1140                 BIO_free_all(orig_bio);
1141             goto end;
1142         }
1143     }
1144 
1145     rctx = http_req_ctx_new(bio == NULL, cbio, rbio != NULL ? rbio : cbio,
1146         bio_update_fn, arg, use_ssl, proxy, server, port,
1147         buf_size, overall_timeout);
1148 
1149 end:
1150     if (rctx != NULL)
1151         /* remove any spurious error queue entries by ssl_add_cert_chain() */
1152         (void)ERR_pop_to_mark();
1153     else
1154         (void)ERR_clear_last_mark();
1155 
1156     return rctx;
1157 }
1158 
OSSL_HTTP_set1_request(OSSL_HTTP_REQ_CTX * rctx,const char * path,const STACK_OF (CONF_VALUE)* headers,const char * content_type,BIO * req,const char * expected_content_type,int expect_asn1,size_t max_resp_len,int timeout,int keep_alive)1159 int OSSL_HTTP_set1_request(OSSL_HTTP_REQ_CTX *rctx, const char *path,
1160     const STACK_OF(CONF_VALUE) *headers,
1161     const char *content_type, BIO *req,
1162     const char *expected_content_type, int expect_asn1,
1163     size_t max_resp_len, int timeout, int keep_alive)
1164 {
1165     int use_http_proxy;
1166 
1167     if (rctx == NULL) {
1168         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
1169         return 0;
1170     }
1171     use_http_proxy = rctx->proxy != NULL && !rctx->use_ssl;
1172     if (use_http_proxy && rctx->server == NULL) {
1173         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT);
1174         return 0;
1175     }
1176     rctx->max_resp_len = max_resp_len; /* allows for 0: indefinite */
1177 
1178     return OSSL_HTTP_REQ_CTX_set_request_line(rctx, req != NULL,
1179                use_http_proxy ? rctx->server
1180                               : NULL,
1181                rctx->port, path)
1182         && add1_headers(rctx, headers, rctx->server)
1183         && OSSL_HTTP_REQ_CTX_set_expected(rctx, expected_content_type,
1184             expect_asn1, timeout, keep_alive)
1185         && set1_content(rctx, content_type, req);
1186 }
1187 
1188 /*-
1189  * Exchange single HTTP request and response according to rctx.
1190  * If rctx->method_POST then use POST, else use GET and ignore content_type.
1191  * The redirection_url output (freed by caller) parameter is used only for GET.
1192  */
OSSL_HTTP_exchange(OSSL_HTTP_REQ_CTX * rctx,char ** redirection_url)1193 BIO *OSSL_HTTP_exchange(OSSL_HTTP_REQ_CTX *rctx, char **redirection_url)
1194 {
1195     BIO *resp;
1196 
1197     if (rctx == NULL) {
1198         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
1199         return NULL;
1200     }
1201 
1202     if (redirection_url != NULL)
1203         *redirection_url = NULL; /* do this beforehand to prevent dbl free */
1204 
1205     resp = OSSL_HTTP_REQ_CTX_exchange(rctx);
1206     if (resp == NULL) {
1207         if (rctx->redirection_url != NULL) {
1208             if (redirection_url == NULL)
1209                 ERR_raise(ERR_LIB_HTTP, HTTP_R_REDIRECTION_NOT_ENABLED);
1210             else
1211                 /* may be NULL if out of memory: */
1212                 *redirection_url = OPENSSL_strdup(rctx->redirection_url);
1213         } else {
1214             char buf[200];
1215             unsigned long err = ERR_peek_error();
1216             int lib = ERR_GET_LIB(err);
1217             int reason = ERR_GET_REASON(err);
1218 
1219             if (lib == ERR_LIB_SSL || lib == ERR_LIB_HTTP
1220                 || (lib == ERR_LIB_BIO && reason == BIO_R_CONNECT_TIMEOUT)
1221                 || (lib == ERR_LIB_BIO && reason == BIO_R_CONNECT_ERROR)
1222 #ifndef OPENSSL_NO_CMP
1223                 || (lib == ERR_LIB_CMP
1224                     && reason == CMP_R_POTENTIALLY_INVALID_CERTIFICATE)
1225 #endif
1226             ) {
1227                 if (rctx->server != NULL && *rctx->server != '\0') {
1228                     BIO_snprintf(buf, sizeof(buf), "server=http%s://%s%s%s",
1229                         rctx->use_ssl ? "s" : "", rctx->server,
1230                         rctx->port != NULL ? ":" : "",
1231                         rctx->port != NULL ? rctx->port : "");
1232                     ERR_add_error_data(1, buf);
1233                 }
1234                 if (rctx->proxy != NULL)
1235                     ERR_add_error_data(2, " proxy=", rctx->proxy);
1236                 if (err == 0) {
1237                     BIO_snprintf(buf, sizeof(buf), " peer has disconnected%s",
1238                         rctx->use_ssl ? " violating the protocol" : ", likely because it requires the use of TLS");
1239                     ERR_add_error_data(1, buf);
1240                 }
1241             }
1242         }
1243     }
1244 
1245     if (resp != NULL && !BIO_up_ref(resp))
1246         resp = NULL;
1247     return resp;
1248 }
1249 
redirection_ok(int n_redir,const char * old_url,const char * new_url)1250 static int redirection_ok(int n_redir, const char *old_url, const char *new_url)
1251 {
1252     if (n_redir >= HTTP_VERSION_MAX_REDIRECTIONS) {
1253         ERR_raise(ERR_LIB_HTTP, HTTP_R_TOO_MANY_REDIRECTIONS);
1254         return 0;
1255     }
1256     if (*new_url == '/') /* redirection to same server => same protocol */
1257         return 1;
1258     if (HAS_PREFIX(old_url, OSSL_HTTPS_NAME ":") && !HAS_PREFIX(new_url, OSSL_HTTPS_NAME ":")) {
1259         ERR_raise(ERR_LIB_HTTP, HTTP_R_REDIRECTION_FROM_HTTPS_TO_HTTP);
1260         return 0;
1261     }
1262     return 1;
1263 }
1264 
1265 /* Get data via HTTP from server at given URL, potentially with redirection */
OSSL_HTTP_get(const char * url,const char * proxy,const char * no_proxy,BIO * bio,BIO * rbio,OSSL_HTTP_bio_cb_t bio_update_fn,void * arg,int buf_size,const STACK_OF (CONF_VALUE)* headers,const char * expected_ct,int expect_asn1,size_t max_resp_len,int timeout)1266 BIO *OSSL_HTTP_get(const char *url, const char *proxy, const char *no_proxy,
1267     BIO *bio, BIO *rbio,
1268     OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
1269     int buf_size, const STACK_OF(CONF_VALUE) *headers,
1270     const char *expected_ct, int expect_asn1,
1271     size_t max_resp_len, int timeout)
1272 {
1273     char *current_url;
1274     int n_redirs = 0;
1275     char *host;
1276     char *port;
1277     char *path;
1278     int use_ssl;
1279     BIO *resp = NULL;
1280     time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
1281 
1282     if (url == NULL) {
1283         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
1284         return NULL;
1285     }
1286     if ((current_url = OPENSSL_strdup(url)) == NULL)
1287         return NULL;
1288 
1289     for (;;) {
1290         OSSL_HTTP_REQ_CTX *rctx;
1291         char *redirection_url;
1292 
1293         if (!OSSL_HTTP_parse_url(current_url, &use_ssl, NULL /* user */, &host,
1294                 &port, NULL /* port_num */, &path, NULL, NULL))
1295             break;
1296 
1297         rctx = OSSL_HTTP_open(host, port, proxy, no_proxy,
1298             use_ssl, bio, rbio, bio_update_fn, arg,
1299             buf_size, timeout);
1300     new_rpath:
1301         redirection_url = NULL;
1302         if (rctx != NULL) {
1303             if (!OSSL_HTTP_set1_request(rctx, path, headers,
1304                     NULL /* content_type */,
1305                     NULL /* req */,
1306                     expected_ct, expect_asn1, max_resp_len,
1307                     -1 /* use same max time (timeout) */,
1308                     0 /* no keep_alive */)) {
1309                 OSSL_HTTP_REQ_CTX_free(rctx);
1310                 rctx = NULL;
1311             } else {
1312                 resp = OSSL_HTTP_exchange(rctx, &redirection_url);
1313             }
1314         }
1315         OPENSSL_free(path);
1316         if (resp == NULL && redirection_url != NULL) {
1317             if (redirection_ok(++n_redirs, current_url, redirection_url)
1318                 && may_still_retry(max_time, &timeout)) {
1319                 (void)BIO_reset(bio);
1320                 OPENSSL_free(current_url);
1321                 current_url = redirection_url;
1322                 if (*redirection_url == '/') { /* redirection to same server */
1323                     path = OPENSSL_strdup(redirection_url);
1324                     if (path == NULL) {
1325                         OPENSSL_free(host);
1326                         OPENSSL_free(port);
1327                         (void)OSSL_HTTP_close(rctx, 1);
1328                         BIO_free(resp);
1329                         OPENSSL_free(current_url);
1330                         return NULL;
1331                     }
1332                     goto new_rpath;
1333                 }
1334                 OPENSSL_free(host);
1335                 OPENSSL_free(port);
1336                 (void)OSSL_HTTP_close(rctx, 1);
1337                 continue;
1338             }
1339             /* if redirection not allowed, ignore it */
1340             OPENSSL_free(redirection_url);
1341         }
1342         OPENSSL_free(host);
1343         OPENSSL_free(port);
1344         if (!OSSL_HTTP_close(rctx, resp != NULL)) {
1345             BIO_free(resp);
1346             resp = NULL;
1347         }
1348         break;
1349     }
1350     OPENSSL_free(current_url);
1351     return resp;
1352 }
1353 
1354 /* Exchange request and response over a connection managed via |prctx| */
OSSL_HTTP_transfer(OSSL_HTTP_REQ_CTX ** prctx,const char * server,const char * port,const char * path,int use_ssl,const char * proxy,const char * no_proxy,BIO * bio,BIO * rbio,OSSL_HTTP_bio_cb_t bio_update_fn,void * arg,int buf_size,const STACK_OF (CONF_VALUE)* headers,const char * content_type,BIO * req,const char * expected_ct,int expect_asn1,size_t max_resp_len,int timeout,int keep_alive)1355 BIO *OSSL_HTTP_transfer(OSSL_HTTP_REQ_CTX **prctx,
1356     const char *server, const char *port,
1357     const char *path, int use_ssl,
1358     const char *proxy, const char *no_proxy,
1359     BIO *bio, BIO *rbio,
1360     OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
1361     int buf_size, const STACK_OF(CONF_VALUE) *headers,
1362     const char *content_type, BIO *req,
1363     const char *expected_ct, int expect_asn1,
1364     size_t max_resp_len, int timeout, int keep_alive)
1365 {
1366     OSSL_HTTP_REQ_CTX *rctx = prctx == NULL ? NULL : *prctx;
1367     BIO *resp = NULL;
1368 
1369     if (rctx == NULL) {
1370         rctx = OSSL_HTTP_open(server, port, proxy, no_proxy,
1371             use_ssl, bio, rbio, bio_update_fn, arg,
1372             buf_size, timeout);
1373         timeout = -1; /* Already set during opening the connection */
1374     }
1375     if (rctx != NULL) {
1376         if (OSSL_HTTP_set1_request(rctx, path, headers, content_type, req,
1377                 expected_ct, expect_asn1,
1378                 max_resp_len, timeout, keep_alive))
1379             resp = OSSL_HTTP_exchange(rctx, NULL);
1380         if (resp == NULL || !OSSL_HTTP_is_alive(rctx)) {
1381             if (!OSSL_HTTP_close(rctx, resp != NULL)) {
1382                 BIO_free(resp);
1383                 resp = NULL;
1384             }
1385             rctx = NULL;
1386         }
1387     }
1388     if (prctx != NULL)
1389         *prctx = rctx;
1390     return resp;
1391 }
1392 
OSSL_HTTP_close(OSSL_HTTP_REQ_CTX * rctx,int ok)1393 int OSSL_HTTP_close(OSSL_HTTP_REQ_CTX *rctx, int ok)
1394 {
1395     BIO *wbio;
1396     int ret = 1;
1397 
1398     /* callback can be used to finish TLS session and free its BIO */
1399     if (rctx != NULL && rctx->upd_fn != NULL) {
1400         wbio = (*rctx->upd_fn)(rctx->wbio, rctx->upd_arg,
1401             0 /* disconnect */, ok);
1402         ret = wbio != NULL;
1403         if (ret)
1404             rctx->wbio = wbio;
1405     }
1406     OSSL_HTTP_REQ_CTX_free(rctx);
1407     return ret;
1408 }
1409 
1410 /* BASE64 encoder used for encoding basic proxy authentication credentials */
base64encode(const void * buf,size_t len)1411 static char *base64encode(const void *buf, size_t len)
1412 {
1413     int i;
1414     size_t outl;
1415     char *out;
1416 
1417     /* Calculate size of encoded data */
1418     outl = (len / 3);
1419     if (len % 3 > 0)
1420         outl++;
1421     outl <<= 2;
1422     out = OPENSSL_malloc(outl + 1);
1423     if (out == NULL)
1424         return 0;
1425 
1426     i = EVP_EncodeBlock((unsigned char *)out, buf, len);
1427     if (!ossl_assert(0 <= i && (size_t)i <= outl)) {
1428         OPENSSL_free(out);
1429         return NULL;
1430     }
1431     return out;
1432 }
1433 
1434 /*
1435  * Promote the given connection BIO using the CONNECT method for a TLS proxy.
1436  * This is typically called by an app, so bio_err and prog are used unless NULL
1437  * to print additional diagnostic information in a user-oriented way.
1438  */
OSSL_HTTP_proxy_connect(BIO * bio,const char * server,const char * port,const char * proxyuser,const char * proxypass,int timeout,BIO * bio_err,const char * prog)1439 int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
1440     const char *proxyuser, const char *proxypass,
1441     int timeout, BIO *bio_err, const char *prog)
1442 {
1443 #undef BUF_SIZE
1444 #define BUF_SIZE (8 * 1024)
1445     char *mbuf = OPENSSL_malloc(BUF_SIZE);
1446     char *mbufp;
1447     int read_len = 0;
1448     int ret = 0;
1449     BIO *fbio = BIO_new(BIO_f_buffer());
1450     int rv;
1451     time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
1452 
1453     if (bio == NULL || server == NULL
1454         || (bio_err != NULL && prog == NULL)) {
1455         ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
1456         goto end;
1457     }
1458     if (port == NULL || *port == '\0')
1459         port = OSSL_HTTPS_PORT;
1460 
1461     if (mbuf == NULL || fbio == NULL) {
1462         BIO_printf(bio_err /* may be NULL */, "%s: out of memory", prog);
1463         goto end;
1464     }
1465     BIO_push(fbio, bio);
1466 
1467     /* Add square brackets around a naked IPv6 address */
1468     if (server[0] != '[' && strchr(server, ':') != NULL)
1469         BIO_printf(fbio, "CONNECT [%s]:%s " HTTP_1_0 "\r\n", server, port);
1470     else
1471         BIO_printf(fbio, "CONNECT %s:%s " HTTP_1_0 "\r\n", server, port);
1472 
1473     /*
1474      * Workaround for broken proxies which would otherwise close
1475      * the connection when entering tunnel mode (e.g., Squid 2.6)
1476      */
1477     BIO_printf(fbio, "Proxy-Connection: Keep-Alive\r\n");
1478 
1479     /* Support for basic (base64) proxy authentication */
1480     if (proxyuser != NULL) {
1481         size_t len = strlen(proxyuser) + 1;
1482         char *proxyauth, *proxyauthenc = NULL;
1483 
1484         if (proxypass != NULL)
1485             len += strlen(proxypass);
1486         proxyauth = OPENSSL_malloc(len + 1);
1487         if (proxyauth == NULL)
1488             goto end;
1489         if (BIO_snprintf(proxyauth, len + 1, "%s:%s", proxyuser,
1490                 proxypass != NULL ? proxypass : "")
1491             != (int)len)
1492             goto proxy_end;
1493         proxyauthenc = base64encode(proxyauth, len);
1494         if (proxyauthenc != NULL) {
1495             BIO_printf(fbio, "Proxy-Authorization: Basic %s\r\n", proxyauthenc);
1496             OPENSSL_clear_free(proxyauthenc, strlen(proxyauthenc));
1497         }
1498     proxy_end:
1499         OPENSSL_clear_free(proxyauth, len);
1500         if (proxyauthenc == NULL)
1501             goto end;
1502     }
1503 
1504     /* Terminate the HTTP CONNECT request */
1505     BIO_printf(fbio, "\r\n");
1506 
1507     for (;;) {
1508         if (BIO_flush(fbio) != 0)
1509             break;
1510         /* potentially needs to be retried if BIO is non-blocking */
1511         if (!BIO_should_retry(fbio))
1512             break;
1513     }
1514 
1515     for (;;) {
1516         /* will not actually wait if timeout == 0 */
1517         rv = BIO_wait(fbio, max_time, 100 /* milliseconds */);
1518         if (rv <= 0) {
1519             BIO_printf(bio_err, "%s: HTTP CONNECT %s\n", prog,
1520                 rv == 0 ? "timed out" : "failed waiting for data");
1521             goto end;
1522         }
1523 
1524         /*-
1525          * The first line is the HTTP response.
1526          * According to RFC 7230, it is formatted exactly like this:
1527          * HTTP/d.d ddd reason text\r\n
1528          */
1529         read_len = BIO_gets(fbio, mbuf, BUF_SIZE);
1530         /* the BIO may not block, so we must wait for the 1st line to come in */
1531         if (read_len < (int)HTTP_LINE1_MINLEN)
1532             continue;
1533 
1534         /* Check for HTTP/1.x */
1535         mbufp = mbuf;
1536         if (!CHECK_AND_SKIP_PREFIX(mbufp, HTTP_PREFIX)) {
1537             ERR_raise(ERR_LIB_HTTP, HTTP_R_HEADER_PARSE_ERROR);
1538             BIO_printf(bio_err, "%s: HTTP CONNECT failed, non-HTTP response\n",
1539                 prog);
1540             /* Wrong protocol, not even HTTP, so stop reading headers */
1541             goto end;
1542         }
1543         if (!HAS_PREFIX(mbufp, HTTP_VERSION_PATT)) {
1544             ERR_raise(ERR_LIB_HTTP, HTTP_R_RECEIVED_WRONG_HTTP_VERSION);
1545             BIO_printf(bio_err,
1546                 "%s: HTTP CONNECT failed, bad HTTP version %.*s\n",
1547                 prog, (int)HTTP_VERSION_STR_LEN, mbufp);
1548             goto end;
1549         }
1550         mbufp += HTTP_VERSION_STR_LEN;
1551 
1552         /* RFC 7231 4.3.6: any 2xx status code is valid */
1553         if (!HAS_PREFIX(mbufp, " 2")) {
1554             if (ossl_isspace(*mbufp))
1555                 mbufp++;
1556             /* chop any trailing whitespace */
1557             while (read_len > 0 && ossl_isspace(mbuf[read_len - 1]))
1558                 read_len--;
1559             mbuf[read_len] = '\0';
1560             ERR_raise_data(ERR_LIB_HTTP, HTTP_R_CONNECT_FAILURE,
1561                 "reason=%s", mbufp);
1562             BIO_printf(bio_err, "%s: HTTP CONNECT failed, reason=%s\n",
1563                 prog, mbufp);
1564             goto end;
1565         }
1566         ret = 1;
1567         break;
1568     }
1569 
1570     /* Read past all following headers */
1571     do {
1572         /*
1573          * This does not necessarily catch the case when the full
1574          * HTTP response came in more than a single TCP message.
1575          */
1576         read_len = BIO_gets(fbio, mbuf, BUF_SIZE);
1577     } while (read_len > 2);
1578 
1579 end:
1580     if (fbio != NULL) {
1581         (void)BIO_flush(fbio);
1582         BIO_pop(fbio);
1583         BIO_free(fbio);
1584     }
1585     OPENSSL_free(mbuf);
1586     return ret;
1587 #undef BUF_SIZE
1588 }
1589