VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.5/crypto/http/http_client.c@ 104078

Last change on this file since 104078 was 104078, checked in by vboxsync, 11 months ago

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette