Coverage Report

Created: 2025-06-13 06:58

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