Coverage Report

Created: 2026-03-09 06:55

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