Coverage Report

Created: 2025-12-31 06:58

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