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