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