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