]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/http/http_client.c
ef23b889aac5bd037a5b5da06e01d1971f306012
[thirdparty/openssl.git] / crypto / http / http_client.c
1 /*
2 * Copyright 2001-2020 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 "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 "internal/sockets.h"
24 #include "internal/cryptlib.h" /* for ossl_assert() */
25
26 #include "http_local.h"
27
28 #define HTTP_PREFIX "HTTP/"
29 #define HTTP_VERSION_PATT "1." /* allow 1.x */
30 #define HTTP_VERSION_STR_LEN 3
31 #define HTTP_LINE1_MINLEN ((int)strlen(HTTP_PREFIX HTTP_VERSION_PATT "x 200\n"))
32 #define HTTP_VERSION_MAX_REDIRECTIONS 50
33
34 #define HTTP_STATUS_CODE_OK 200
35 #define HTTP_STATUS_CODE_MOVED_PERMANENTLY 301
36 #define HTTP_STATUS_CODE_FOUND 302
37
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 *iobuf; /* Line buffer */
46 int iobuflen; /* Line buffer length */
47 BIO *wbio; /* BIO to send request to */
48 BIO *rbio; /* BIO to read response from */
49 BIO *mem; /* Memory BIO response is built into */
50 int method_GET; /* HTTP method "GET" or "POST" */
51 const char *expected_ct; /* expected Content-Type, or NULL */
52 int expect_asn1; /* response must be ASN.1-encoded */
53 unsigned long resp_len; /* length of response */
54 unsigned long max_resp_len; /* Maximum length of response */
55 time_t max_time; /* Maximum end time of the transfer, or 0 */
56 char *redirection_url; /* Location given with HTTP status 301/302 */
57 };
58
59 #define HTTP_DEFAULT_MAX_LINE_LENGTH (4 * 1024)
60 #define HTTP_DEFAULT_MAX_RESP_LEN (100 * 1024)
61
62 /* HTTP states */
63
64 #define OHS_NOREAD 0x1000 /* If set no reading should be performed */
65 #define OHS_ERROR (0 | OHS_NOREAD) /* Error condition */
66 #define OHS_FIRSTLINE 1 /* First line being read */
67 #define OHS_REDIRECT 0xa /* Looking for redirection location */
68 #define OHS_HEADERS 2 /* MIME headers being read */
69 #define OHS_ASN1_HEADER 3 /* HTTP initial header (tag+length) being read */
70 #define OHS_CONTENT 4 /* HTTP content octets being read */
71 #define OHS_WRITE_INIT (5 | OHS_NOREAD) /* 1st call: ready to start I/O */
72 #define OHS_WRITE (6 | OHS_NOREAD) /* Request being sent */
73 #define OHS_FLUSH (7 | OHS_NOREAD) /* Request being flushed */
74 #define OHS_DONE (8 | OHS_NOREAD) /* Completed */
75 #define OHS_HTTP_HEADER (9 | OHS_NOREAD) /* Headers set, w/o final \r\n */
76
77 OSSL_HTTP_REQ_CTX *OSSL_HTTP_REQ_CTX_new(BIO *wbio, BIO *rbio,
78 int method_GET, int maxline,
79 unsigned long max_resp_len,
80 int timeout,
81 const char *expected_content_type,
82 int expect_asn1)
83 {
84 OSSL_HTTP_REQ_CTX *rctx;
85
86 if (wbio == NULL || rbio == NULL) {
87 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
88 return NULL;
89 }
90
91 if ((rctx = OPENSSL_zalloc(sizeof(*rctx))) == NULL)
92 return NULL;
93 rctx->state = OHS_ERROR;
94 rctx->iobuflen = maxline > 0 ? maxline : HTTP_DEFAULT_MAX_LINE_LENGTH;
95 rctx->iobuf = OPENSSL_malloc(rctx->iobuflen);
96 rctx->wbio = wbio;
97 rctx->rbio = rbio;
98 rctx->mem = BIO_new(BIO_s_mem());
99 if (rctx->iobuf == NULL || rctx->mem == NULL) {
100 OSSL_HTTP_REQ_CTX_free(rctx);
101 return NULL;
102 }
103 rctx->method_GET = method_GET;
104 rctx->expected_ct = expected_content_type;
105 rctx->expect_asn1 = expect_asn1;
106 rctx->resp_len = 0;
107 OSSL_HTTP_REQ_CTX_set_max_response_length(rctx, max_resp_len);
108 rctx->max_time = timeout > 0 ? time(NULL) + timeout : 0;
109 return rctx;
110 }
111
112 void OSSL_HTTP_REQ_CTX_free(OSSL_HTTP_REQ_CTX *rctx)
113 {
114 if (rctx == NULL)
115 return;
116 BIO_free(rctx->mem); /* this may indirectly call ERR_clear_error() */
117 OPENSSL_free(rctx->iobuf);
118 OPENSSL_free(rctx);
119 }
120
121 BIO *OSSL_HTTP_REQ_CTX_get0_mem_bio(OSSL_HTTP_REQ_CTX *rctx)
122 {
123 if (rctx == NULL) {
124 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
125 return NULL;
126 }
127 return rctx->mem;
128 }
129
130 void OSSL_HTTP_REQ_CTX_set_max_response_length(OSSL_HTTP_REQ_CTX *rctx,
131 unsigned long len)
132 {
133 if (rctx == NULL) {
134 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
135 return;
136 }
137 rctx->max_resp_len = len != 0 ? len : HTTP_DEFAULT_MAX_RESP_LEN;
138 }
139
140 /*
141 * Create HTTP header using given op and path (or "/" in case path is NULL).
142 * Server name (and port) must be given if and only if plain HTTP proxy is used.
143 */
144 int OSSL_HTTP_REQ_CTX_header(OSSL_HTTP_REQ_CTX *rctx, const char *server,
145 const char *port, const char *path)
146 {
147 if (rctx == NULL) {
148 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
149 return 0;
150 }
151
152 if (BIO_printf(rctx->mem, "%s ", rctx->method_GET ? "GET" : "POST") <= 0)
153 return 0;
154
155 if (server != NULL) { /* HTTP (but not HTTPS) proxy is used */
156 /*
157 * Section 5.1.2 of RFC 1945 states that the absoluteURI form is only
158 * allowed when using a proxy
159 */
160 if (BIO_printf(rctx->mem, OSSL_HTTP_PREFIX"%s", server) <= 0)
161 return 0;
162 if (port != NULL && BIO_printf(rctx->mem, ":%s", port) <= 0)
163 return 0;
164 }
165
166 /* Make sure path includes a forward slash */
167 if (path == NULL)
168 path = "/";
169 if (path[0] != '/' && BIO_printf(rctx->mem, "/") <= 0)
170 return 0;
171
172 if (BIO_printf(rctx->mem, "%s "HTTP_PREFIX"1.0\r\n", path) <= 0)
173 return 0;
174 rctx->state = OHS_HTTP_HEADER;
175 return 1;
176 }
177
178 int OSSL_HTTP_REQ_CTX_add1_header(OSSL_HTTP_REQ_CTX *rctx,
179 const char *name, const char *value)
180 {
181 if (rctx == NULL || name == NULL) {
182 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
183 return 0;
184 }
185
186 if (BIO_puts(rctx->mem, name) <= 0)
187 return 0;
188 if (value != NULL) {
189 if (BIO_write(rctx->mem, ": ", 2) != 2)
190 return 0;
191 if (BIO_puts(rctx->mem, value) <= 0)
192 return 0;
193 }
194 if (BIO_write(rctx->mem, "\r\n", 2) != 2)
195 return 0;
196 rctx->state = OHS_HTTP_HEADER;
197 return 1;
198 }
199
200 static int OSSL_HTTP_REQ_CTX_content(OSSL_HTTP_REQ_CTX *rctx,
201 const char *content_type, BIO *req_mem)
202 {
203 const unsigned char *req;
204 long req_len;
205
206 if (rctx == NULL || req_mem == NULL) {
207 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
208 return 0;
209 }
210
211 if (content_type != NULL
212 && BIO_printf(rctx->mem, "Content-Type: %s\r\n", content_type) <= 0)
213 return 0;
214
215 if ((req_len = BIO_get_mem_data(req_mem, &req)) <= 0)
216 return 0;
217 rctx->state = OHS_WRITE_INIT;
218
219 return BIO_printf(rctx->mem, "Content-Length: %ld\r\n\r\n", req_len) > 0
220 && BIO_write(rctx->mem, req, req_len) == (int)req_len;
221 }
222
223 BIO *HTTP_asn1_item2bio(const ASN1_ITEM *it, const ASN1_VALUE *val)
224 {
225 BIO *res;
226
227 if (it == NULL || val == NULL) {
228 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
229 return NULL;
230 }
231
232 if ((res = BIO_new(BIO_s_mem())) == NULL)
233 return NULL;
234 if (ASN1_item_i2d_bio(it, res, val) <= 0) {
235 BIO_free(res);
236 res = NULL;
237 }
238 return res;
239 }
240
241 int OSSL_HTTP_REQ_CTX_i2d(OSSL_HTTP_REQ_CTX *rctx, const char *content_type,
242 const ASN1_ITEM *it, ASN1_VALUE *req)
243 {
244 BIO *mem;
245 int res;
246
247 if (rctx == NULL || it == NULL || req == NULL) {
248 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
249 return 0;
250 }
251
252 res = (mem = HTTP_asn1_item2bio(it, req)) != NULL
253 && OSSL_HTTP_REQ_CTX_content(rctx, content_type, mem);
254 BIO_free(mem);
255 return res;
256 }
257
258 static int OSSL_HTTP_REQ_CTX_add1_headers(OSSL_HTTP_REQ_CTX *rctx,
259 const STACK_OF(CONF_VALUE) *headers,
260 const char *host)
261 {
262 int i;
263 int add_host = 1;
264 CONF_VALUE *hdr;
265
266 for (i = 0; i < sk_CONF_VALUE_num(headers); i++) {
267 hdr = sk_CONF_VALUE_value(headers, i);
268 if (add_host && strcasecmp("host", hdr->name) == 0)
269 add_host = 0;
270 if (!OSSL_HTTP_REQ_CTX_add1_header(rctx, hdr->name, hdr->value))
271 return 0;
272 }
273
274 if (add_host && !OSSL_HTTP_REQ_CTX_add1_header(rctx, "Host", host))
275 return 0;
276 return 1;
277 }
278
279 /*-
280 * Create OSSL_HTTP_REQ_CTX structure using the values provided.
281 * If !use_http_proxy then the 'server' and 'port' parameters are ignored.
282 * If req_mem == NULL then use GET and ignore content_type, else POST.
283 */
284 OSSL_HTTP_REQ_CTX *HTTP_REQ_CTX_new(BIO *wbio, BIO *rbio, int use_http_proxy,
285 const char *server, const char *port,
286 const char *path,
287 const STACK_OF(CONF_VALUE) *headers,
288 const char *content_type, BIO *req_mem,
289 int maxline, unsigned long max_resp_len,
290 int timeout,
291 const char *expected_content_type,
292 int expect_asn1)
293 {
294 OSSL_HTTP_REQ_CTX *rctx;
295
296 if (use_http_proxy && (server == NULL || port == NULL)) {
297 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
298 return NULL;
299 }
300 /* remaining parameters are checked indirectly by the functions called */
301
302 if ((rctx = OSSL_HTTP_REQ_CTX_new(wbio, rbio, req_mem == NULL, maxline,
303 max_resp_len, timeout,
304 expected_content_type, expect_asn1))
305 == NULL)
306 return NULL;
307
308 if (OSSL_HTTP_REQ_CTX_header(rctx, use_http_proxy ? server : NULL,
309 port, path)
310 && OSSL_HTTP_REQ_CTX_add1_headers(rctx, headers, server)
311 && (req_mem == NULL
312 || OSSL_HTTP_REQ_CTX_content(rctx, content_type, req_mem)))
313 return rctx;
314
315 OSSL_HTTP_REQ_CTX_free(rctx);
316 return NULL;
317 }
318
319 /*
320 * Parse first HTTP response line. This should be like this: "HTTP/1.0 200 OK".
321 * We need to obtain the numeric code and (optional) informational message.
322 */
323
324 static int parse_http_line1(char *line)
325 {
326 int retcode;
327 char *code, *reason, *end;
328
329 /* Skip to first whitespace (past protocol info) */
330 for (code = line; *code != '\0' && !ossl_isspace(*code); code++)
331 continue;
332 if (*code == '\0') {
333 ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_PARSE_ERROR);
334 return 0;
335 }
336
337 /* Skip past whitespace to start of response code */
338 while (*code != '\0' && ossl_isspace(*code))
339 code++;
340
341 if (*code == '\0') {
342 ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_PARSE_ERROR);
343 return 0;
344 }
345
346 /* Find end of response code: first whitespace after start of code */
347 for (reason = code; *reason != '\0' && !ossl_isspace(*reason); reason++)
348 continue;
349
350 if (*reason == '\0') {
351 ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_PARSE_ERROR);
352 return 0;
353 }
354
355 /* Set end of response code and start of message */
356 *reason++ = '\0';
357
358 /* Attempt to parse numeric code */
359 retcode = strtoul(code, &end, 10);
360
361 if (*end != '\0')
362 return 0;
363
364 /* Skip over any leading whitespace in message */
365 while (*reason != '\0' && ossl_isspace(*reason))
366 reason++;
367
368 if (*reason != '\0') {
369 /*
370 * Finally zap any trailing whitespace in message (include CRLF)
371 */
372
373 /* chop any trailing whitespace from reason */
374 /* We know reason has a non-whitespace character so this is OK */
375 for (end = reason + strlen(reason) - 1; ossl_isspace(*end); end--)
376 *end = '\0';
377 }
378
379 switch (retcode) {
380 case HTTP_STATUS_CODE_OK:
381 case HTTP_STATUS_CODE_MOVED_PERMANENTLY:
382 case HTTP_STATUS_CODE_FOUND:
383 return retcode;
384 default:
385 if (retcode < 400)
386 ERR_raise(ERR_LIB_HTTP, HTTP_R_STATUS_CODE_UNSUPPORTED);
387 else
388 ERR_raise(ERR_LIB_HTTP, HTTP_R_RECEIVED_ERROR);
389 if (*reason == '\0')
390 ERR_add_error_data(2, "Code=", code);
391 else
392 ERR_add_error_data(4, "Code=", code, ",Reason=", reason);
393 return 0;
394 }
395 }
396
397 static int check_set_resp_len(OSSL_HTTP_REQ_CTX *rctx, unsigned long len)
398 {
399 const char *tag = NULL;
400 unsigned long val = 0;
401
402 if (len > rctx->max_resp_len) {
403 ERR_raise(ERR_LIB_HTTP, HTTP_R_MAX_RESP_LEN_EXCEEDED);
404 tag = ",max=";
405 val = rctx->max_resp_len;
406 }
407 if (rctx->resp_len != 0 && rctx->resp_len != len) {
408 ERR_raise(ERR_LIB_HTTP, HTTP_R_INCONSISTENT_CONTENT_LENGTH);
409 tag = ",before=";
410 val = rctx->resp_len;
411 }
412 if (tag != NULL) {
413 char len_str[32];
414 char str[32];
415
416 BIO_snprintf(len_str, sizeof(len_str), "%lu", len);
417 BIO_snprintf(str, sizeof(str), "%lu", val);
418 ERR_add_error_data(4, "length=", len_str, tag, str);
419 return 0;
420 }
421 rctx->resp_len = len;
422 return 1;
423 }
424
425 /*
426 * Try exchanging request and response via HTTP on (non-)blocking BIO in rctx.
427 * Returns 1 on success, 0 on error or redirection, -1 on BIO_should_retry.
428 */
429 int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx)
430 {
431 int i;
432 long n, n_to_send = 0;
433 unsigned long resp_len;
434 const unsigned char *p;
435 char *key, *value, *line_end = NULL;
436
437 if (rctx == NULL) {
438 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
439 return 0;
440 }
441
442 rctx->redirection_url = NULL;
443 next_io:
444 if ((rctx->state & OHS_NOREAD) == 0) {
445 n = BIO_read(rctx->rbio, rctx->iobuf, rctx->iobuflen);
446 if (n <= 0) {
447 if (BIO_should_retry(rctx->rbio))
448 return -1;
449 return 0;
450 }
451
452 /* Write data to memory BIO */
453 if (BIO_write(rctx->mem, rctx->iobuf, n) != n)
454 return 0;
455 }
456
457 switch (rctx->state) {
458 case OHS_HTTP_HEADER:
459 /* Last operation was adding headers: need a final \r\n */
460 if (BIO_write(rctx->mem, "\r\n", 2) != 2) {
461 rctx->state = OHS_ERROR;
462 return 0;
463 }
464 rctx->state = OHS_WRITE_INIT;
465
466 /* fall thru */
467 case OHS_WRITE_INIT:
468 n_to_send = BIO_get_mem_data(rctx->mem, NULL);
469 rctx->state = OHS_WRITE;
470
471 /* fall thru */
472 case OHS_WRITE:
473 n = BIO_get_mem_data(rctx->mem, &p);
474
475 i = BIO_write(rctx->wbio, p + (n - n_to_send), n_to_send);
476
477 if (i <= 0) {
478 if (BIO_should_retry(rctx->wbio))
479 return -1;
480 rctx->state = OHS_ERROR;
481 return 0;
482 }
483
484 n_to_send -= i;
485
486 if (n_to_send > 0)
487 goto next_io;
488
489 rctx->state = OHS_FLUSH;
490
491 (void)BIO_reset(rctx->mem);
492
493 /* fall thru */
494 case OHS_FLUSH:
495
496 i = BIO_flush(rctx->wbio);
497
498 if (i > 0) {
499 rctx->state = OHS_FIRSTLINE;
500 goto next_io;
501 }
502
503 if (BIO_should_retry(rctx->wbio))
504 return -1;
505
506 rctx->state = OHS_ERROR;
507 return 0;
508
509 case OHS_ERROR:
510 return 0;
511
512 case OHS_FIRSTLINE:
513 case OHS_HEADERS:
514 case OHS_REDIRECT:
515
516 /* Attempt to read a line in */
517 next_line:
518 /*
519 * Due to strange memory BIO behavior with BIO_gets we have to check
520 * there's a complete line in there before calling BIO_gets or we'll
521 * just get a partial read.
522 */
523 n = BIO_get_mem_data(rctx->mem, &p);
524 if (n <= 0 || memchr(p, '\n', n) == 0) {
525 if (n >= rctx->iobuflen) {
526 rctx->state = OHS_ERROR;
527 return 0;
528 }
529 goto next_io;
530 }
531 n = BIO_gets(rctx->mem, (char *)rctx->iobuf, rctx->iobuflen);
532
533 if (n <= 0) {
534 if (BIO_should_retry(rctx->mem))
535 goto next_io;
536 rctx->state = OHS_ERROR;
537 return 0;
538 }
539
540 /* Don't allow excessive lines */
541 if (n == rctx->iobuflen) {
542 ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_LINE_TOO_LONG);
543 rctx->state = OHS_ERROR;
544 return 0;
545 }
546
547 /* First line */
548 if (rctx->state == OHS_FIRSTLINE) {
549 switch (parse_http_line1((char *)rctx->iobuf)) {
550 case HTTP_STATUS_CODE_OK:
551 rctx->state = OHS_HEADERS;
552 goto next_line;
553 case HTTP_STATUS_CODE_MOVED_PERMANENTLY:
554 case HTTP_STATUS_CODE_FOUND: /* i.e., moved temporarily */
555 if (rctx->method_GET) {
556 rctx->state = OHS_REDIRECT;
557 goto next_line;
558 }
559 ERR_raise(ERR_LIB_HTTP, HTTP_R_REDIRECTION_NOT_ENABLED);
560 /* redirection is not supported/recommended for POST */
561 /* fall through */
562 default:
563 rctx->state = OHS_ERROR;
564 return 0;
565 }
566 }
567 key = (char *)rctx->iobuf;
568 value = strchr(key, ':');
569 if (value != NULL) {
570 *(value++) = '\0';
571 while (ossl_isspace(*value))
572 value++;
573 line_end = strchr(value, '\r');
574 if (line_end == NULL)
575 line_end = strchr(value, '\n');
576 if (line_end != NULL)
577 *line_end = '\0';
578 }
579 if (value != NULL && line_end != NULL) {
580 if (rctx->state == OHS_REDIRECT
581 && strcasecmp(key, "Location") == 0) {
582 rctx->redirection_url = value;
583 return 0;
584 }
585 if (rctx->expected_ct != NULL
586 && strcasecmp(key, "Content-Type") == 0) {
587 if (strcasecmp(rctx->expected_ct, value) != 0) {
588 ERR_raise(ERR_LIB_HTTP, HTTP_R_UNEXPECTED_CONTENT_TYPE);
589 ERR_add_error_data(4, "expected=", rctx->expected_ct,
590 ",actual=", value);
591 return 0;
592 }
593 rctx->expected_ct = NULL; /* content-type has been found */
594 }
595 if (strcasecmp(key, "Content-Length") == 0) {
596 resp_len = strtoul(value, &line_end, 10);
597 if (line_end == value || *line_end != '\0') {
598 ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_PARSING_CONTENT_LENGTH);
599 ERR_add_error_data(2, "input=", value);
600 return 0;
601 }
602 if (!check_set_resp_len(rctx, resp_len))
603 return 0;
604 }
605 }
606
607 /* Look for blank line: end of headers */
608 for (p = rctx->iobuf; *p != '\0'; p++) {
609 if (*p != '\r' && *p != '\n')
610 break;
611 }
612 if (*p != '\0') /* not end of headers */
613 goto next_line;
614
615 if (rctx->expected_ct != NULL) {
616 ERR_raise(ERR_LIB_HTTP, HTTP_R_MISSING_CONTENT_TYPE);
617 ERR_add_error_data(2, "expected=", rctx->expected_ct);
618 return 0;
619 }
620 if (rctx->state == OHS_REDIRECT) {
621 /* http status code indicated redirect but there was no Location */
622 ERR_raise(ERR_LIB_HTTP, HTTP_R_MISSING_REDIRECT_LOCATION);
623 return 0;
624 }
625
626 if (!rctx->expect_asn1) {
627 rctx->state = OHS_CONTENT;
628 goto content;
629 }
630
631 rctx->state = OHS_ASN1_HEADER;
632
633 /* Fall thru */
634 case OHS_ASN1_HEADER:
635 /*
636 * Now reading ASN1 header: can read at least 2 bytes which is enough
637 * for ASN1 SEQUENCE header and either length field or at least the
638 * length of the length field.
639 */
640 n = BIO_get_mem_data(rctx->mem, &p);
641 if (n < 2)
642 goto next_io;
643
644 /* Check it is an ASN1 SEQUENCE */
645 if (*p++ != (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
646 ERR_raise(ERR_LIB_HTTP, HTTP_R_MISSING_ASN1_ENCODING);
647 return 0;
648 }
649
650 /* Check out length field */
651 if ((*p & 0x80) != 0) {
652 /*
653 * If MSB set on initial length octet we can now always read 6
654 * octets: make sure we have them.
655 */
656 if (n < 6)
657 goto next_io;
658 n = *p & 0x7F;
659 /* Not NDEF or excessive length */
660 if (n == 0 || (n > 4)) {
661 ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_PARSING_ASN1_LENGTH);
662 return 0;
663 }
664 p++;
665 resp_len = 0;
666 for (i = 0; i < n; i++) {
667 resp_len <<= 8;
668 resp_len |= *p++;
669 }
670 resp_len += n + 2;
671 } else {
672 resp_len = *p + 2;
673 }
674 if (!check_set_resp_len(rctx, resp_len))
675 return 0;
676
677 content:
678 rctx->state = OHS_CONTENT;
679
680 /* Fall thru */
681 case OHS_CONTENT:
682 default:
683 n = BIO_get_mem_data(rctx->mem, NULL);
684 if (n < (long)rctx->resp_len /* may be 0 if no Content-Type or ASN.1 */)
685 goto next_io;
686
687 rctx->state = OHS_DONE;
688 return 1;
689 }
690 }
691
692 #ifndef OPENSSL_NO_SOCK
693
694 /* set up a new connection BIO, to HTTP server or to HTTP(S) proxy if given */
695 static BIO *HTTP_new_bio(const char *server /* optionally includes ":port" */,
696 const char *server_port /* explicit server port */,
697 const char *proxy /* optionally includes ":port" */)
698 {
699 const char *host = server, *host_end;
700 char host_name[100];
701 const char *port = server_port;
702 BIO *cbio;
703
704 if (!ossl_assert(server != NULL))
705 return NULL;
706
707 if (proxy != NULL) {
708 host = proxy;
709 port = NULL;
710 }
711
712 host_end = strchr(host, '/');
713 if (host_end != NULL) {
714 size_t host_len = host_end - host;
715
716 if (host_len < sizeof(host_name)) {
717 /* chop trailing string starting with '/' */
718 strncpy(host_name, host, host_len);
719 host_name[host_len] = '\0';
720 host = host_name;
721 }
722 }
723
724 cbio = BIO_new_connect(host /* optionally includes ":port" */);
725 if (cbio == NULL)
726 goto end;
727 if (port != NULL)
728 (void)BIO_set_conn_port(cbio, port);
729
730 end:
731 return cbio;
732 }
733 #endif /* OPENSSL_NO_SOCK */
734
735 static ASN1_VALUE *BIO_mem_d2i(BIO *mem, const ASN1_ITEM *it)
736 {
737 const unsigned char *p;
738 long len = BIO_get_mem_data(mem, &p);
739 ASN1_VALUE *resp = ASN1_item_d2i(NULL, &p, len, it);
740
741 if (resp == NULL)
742 ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_PARSE_ERROR);
743 return resp;
744 }
745
746 static BIO *OSSL_HTTP_REQ_CTX_transfer(OSSL_HTTP_REQ_CTX *rctx)
747 {
748 int sending = 1;
749 int rv;
750
751 if (rctx == NULL) {
752 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
753 return NULL;
754 }
755
756 for (;;) {
757 rv = OSSL_HTTP_REQ_CTX_nbio(rctx);
758 if (rv != -1)
759 break;
760 /* BIO_should_retry was true */
761 sending = 0;
762 /* will not actually wait if rctx->max_time == 0 */
763 if (BIO_wait(rctx->rbio, rctx->max_time, 100 /* milliseconds */) <= 0)
764 return NULL;
765 }
766
767 if (rv == 0) {
768 if (rctx->redirection_url == NULL) { /* an error occurred */
769 if (sending && (rctx->state & OHS_NOREAD) != 0)
770 ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_SENDING);
771 else
772 ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_RECEIVING);
773 }
774 return NULL;
775 }
776 if (!BIO_up_ref(rctx->mem))
777 return NULL;
778 return rctx->mem;
779 }
780
781 /* Exchange ASN.1-encoded request and response via HTTP on (non-)blocking BIO */
782 ASN1_VALUE *OSSL_HTTP_REQ_CTX_sendreq_d2i(OSSL_HTTP_REQ_CTX *rctx,
783 const ASN1_ITEM *it)
784 {
785 if (rctx == NULL || it == NULL) {
786 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
787 return NULL;
788 }
789 return BIO_mem_d2i(OSSL_HTTP_REQ_CTX_transfer(rctx), it);
790 }
791
792 static int update_timeout(int timeout, time_t start_time)
793 {
794 long elapsed_time;
795
796 if (timeout == 0)
797 return 0;
798 elapsed_time = (long)(time(NULL) - start_time); /* this might overflow */
799 return timeout <= elapsed_time ? -1 : timeout - elapsed_time;
800 }
801
802 /*-
803 * Exchange HTTP request and response with the given server.
804 * If req_mem == NULL then use GET and ignore content_type, else POST.
805 * The redirection_url output (freed by caller) parameter is used only for GET.
806 *
807 * Typically the bio and rbio parameters are NULL and a network BIO is created
808 * internally for connecting to the given server and port, optionally via a
809 * proxy and its port, and is then used for exchanging the request and response.
810 * If bio is given and rbio is NULL then this BIO is used instead.
811 * If both bio and rbio are given (which may be memory BIOs for instance)
812 * then no explicit connection is attempted,
813 * bio is used for writing the request, and rbio for reading the response.
814 *
815 * bio_update_fn is an optional BIO connect/disconnect callback function,
816 * which has the prototype
817 * BIO *(*OSSL_HTTP_bio_cb_t) (BIO *bio, void *arg, int conn, int detail);
818 * The callback may modify the HTTP BIO provided in the bio argument,
819 * whereby it may make use of any custom defined argument 'arg'.
820 * During connection establishment, just after BIO_do_connect_retry(),
821 * the callback function is invoked with the 'conn' argument being 1
822 * 'detail' indicating whether a HTTPS (i.e., TLS) connection is requested.
823 * On disconnect 'conn' is 0 and 'detail' indicates that no error occurred.
824 * For instance, on connect the funct may prepend a TLS BIO to implement HTTPS;
825 * after disconnect it may do some error diagnostics and/or specific cleanup.
826 * The function should return NULL to indicate failure.
827 * After disconnect the modified BIO will be deallocated using BIO_free_all().
828 */
829 BIO *OSSL_HTTP_transfer(const char *server, const char *port, const char *path,
830 int use_ssl, const char *proxy, const char *no_proxy,
831 BIO *bio, BIO *rbio,
832 OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
833 const STACK_OF(CONF_VALUE) *headers,
834 const char *content_type, BIO *req_mem,
835 int maxline, unsigned long max_resp_len, int timeout,
836 const char *expected_ct, int expect_asn1,
837 char **redirection_url)
838 {
839 time_t start_time = timeout > 0 ? time(NULL) : 0;
840 BIO *cbio; /* = bio if present, used as connection BIO if rbio is NULL */
841 OSSL_HTTP_REQ_CTX *rctx;
842 BIO *resp = NULL;
843
844 if (redirection_url != NULL)
845 *redirection_url = NULL; /* do this beforehand to prevent dbl free */
846
847 if (use_ssl && bio_update_fn == NULL) {
848 ERR_raise(ERR_LIB_HTTP, HTTP_R_TLS_NOT_ENABLED);
849 return NULL;
850 }
851 if (rbio != NULL && (bio == NULL || bio_update_fn != NULL)) {
852 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT);
853 return NULL;
854 }
855
856 if (bio != NULL) {
857 cbio = bio;
858 } else {
859 #ifndef OPENSSL_NO_SOCK
860 if (server == NULL) {
861 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
862 return NULL;
863 }
864 if (*port == '\0')
865 port = NULL;
866 if (port == NULL && strchr(server, ':') == NULL)
867 port = use_ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT;
868 proxy = http_adapt_proxy(proxy, no_proxy, server, use_ssl);
869 if ((cbio = HTTP_new_bio(server, port, proxy)) == NULL)
870 return NULL;
871 #else
872 ERR_raise(ERR_LIB_HTTP, HTTP_R_SOCK_NOT_SUPPORTED);
873 return NULL;
874 #endif
875 }
876 /* remaining parameters are checked indirectly by the functions called */
877
878 (void)ERR_set_mark(); /* prepare removing any spurious libssl errors */
879 if (rbio == NULL && BIO_do_connect_retry(cbio, timeout, -1) <= 0)
880 goto end;
881 /* now timeout is guaranteed to be >= 0 */
882
883 /* callback can be used to wrap or prepend TLS session */
884 if (bio_update_fn != NULL) {
885 BIO *orig_bio = cbio;
886 cbio = (*bio_update_fn)(cbio, arg, 1 /* connect */, use_ssl);
887 if (cbio == NULL) {
888 cbio = orig_bio;
889 goto end;
890 }
891 }
892
893 rctx = HTTP_REQ_CTX_new(cbio, rbio != NULL ? rbio : cbio,
894 !use_ssl && proxy != NULL, server, port, path,
895 headers, content_type, req_mem, maxline,
896 max_resp_len, update_timeout(timeout, start_time),
897 expected_ct, expect_asn1);
898 if (rctx == NULL)
899 goto end;
900
901 resp = OSSL_HTTP_REQ_CTX_transfer(rctx);
902 if (resp == NULL) {
903 if (rctx->redirection_url != NULL) {
904 if (redirection_url == NULL)
905 ERR_raise(ERR_LIB_HTTP, HTTP_R_REDIRECTION_NOT_ENABLED);
906 else
907 /* may be NULL if out of memory: */
908 *redirection_url = OPENSSL_strdup(rctx->redirection_url);
909 } else {
910 char buf[200];
911 unsigned long err = ERR_peek_error();
912 int lib = ERR_GET_LIB(err);
913 int reason = ERR_GET_REASON(err);
914
915 if (lib == ERR_LIB_SSL || lib == ERR_LIB_HTTP
916 || (lib == ERR_LIB_BIO && reason == BIO_R_CONNECT_TIMEOUT)
917 || (lib == ERR_LIB_BIO && reason == BIO_R_CONNECT_ERROR)
918 #ifndef OPENSSL_NO_CMP
919 || (lib == ERR_LIB_CMP
920 && reason == CMP_R_POTENTIALLY_INVALID_CERTIFICATE)
921 #endif
922 ) {
923 BIO_snprintf(buf, 200, "server=%s:%s", server, port);
924 ERR_add_error_data(1, buf);
925 if (proxy != NULL)
926 ERR_add_error_data(2, " proxy=", proxy);
927 if (err == 0) {
928 BIO_snprintf(buf, 200, " peer has disconnected%s",
929 use_ssl ? " violating the protocol" :
930 ", likely because it requires the use of TLS");
931 ERR_add_error_data(1, buf);
932 }
933 }
934 }
935 }
936 OSSL_HTTP_REQ_CTX_free(rctx);
937
938 /* callback can be used to clean up TLS session */
939 if (bio_update_fn != NULL
940 && (*bio_update_fn)(cbio, arg, 0, resp != NULL) == NULL) {
941 BIO_free(resp);
942 resp = NULL;
943 }
944
945 end:
946 /*
947 * Use BIO_free_all() because bio_update_fn may prepend or append to cbio.
948 * This also frees any (e.g., SSL/TLS) BIOs linked with bio and,
949 * like BIO_reset(bio), calls SSL_shutdown() to notify/alert the peer.
950 */
951 if (bio == NULL) /* cbio was not provided by caller */
952 BIO_free_all(cbio);
953
954 if (resp != NULL)
955 /* remove any spurious error queue entries by ssl_add_cert_chain() */
956 (void)ERR_pop_to_mark();
957 else
958 (void)ERR_clear_last_mark();
959
960 return resp;
961 }
962
963 static int redirection_ok(int n_redir, const char *old_url, const char *new_url)
964 {
965 size_t https_len = strlen(OSSL_HTTPS_NAME":");
966
967 if (n_redir >= HTTP_VERSION_MAX_REDIRECTIONS) {
968 ERR_raise(ERR_LIB_HTTP, HTTP_R_TOO_MANY_REDIRECTIONS);
969 return 0;
970 }
971 if (*new_url == '/') /* redirection to same server => same protocol */
972 return 1;
973 if (strncmp(old_url, OSSL_HTTPS_NAME":", https_len) == 0 &&
974 strncmp(new_url, OSSL_HTTPS_NAME":", https_len) != 0) {
975 ERR_raise(ERR_LIB_HTTP, HTTP_R_REDIRECTION_FROM_HTTPS_TO_HTTP);
976 return 0;
977 }
978 return 1;
979 }
980
981 /* Get data via HTTP from server at given URL, potentially with redirection */
982 BIO *OSSL_HTTP_get(const char *url, const char *proxy, const char *no_proxy,
983 BIO *bio, BIO *rbio,
984 OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
985 const STACK_OF(CONF_VALUE) *headers,
986 int maxline, unsigned long max_resp_len, int timeout,
987 const char *expected_content_type, int expect_asn1)
988 {
989 time_t start_time = timeout > 0 ? time(NULL) : 0;
990 char *current_url, *redirection_url;
991 int n_redirs = 0;
992 char *host;
993 char *port;
994 char *path;
995 int use_ssl;
996 BIO *resp = NULL;
997
998 if (url == NULL) {
999 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
1000 return NULL;
1001 }
1002 if ((current_url = OPENSSL_strdup(url)) == NULL)
1003 return NULL;
1004
1005 for (;;) {
1006 if (!OSSL_HTTP_parse_url(current_url, &host, &port, NULL /* port_num */,
1007 &path, &use_ssl))
1008 break;
1009
1010 new_rpath:
1011 resp = OSSL_HTTP_transfer(host, port, path, use_ssl, proxy, no_proxy,
1012 bio, rbio,
1013 bio_update_fn, arg, headers, NULL, NULL,
1014 maxline, max_resp_len,
1015 update_timeout(timeout, start_time),
1016 expected_content_type, expect_asn1,
1017 &redirection_url);
1018 OPENSSL_free(path);
1019 if (resp == NULL && redirection_url != NULL) {
1020 if (redirection_ok(++n_redirs, current_url, redirection_url)) {
1021 (void)BIO_reset(bio);
1022 OPENSSL_free(current_url);
1023 current_url = redirection_url;
1024 if (*redirection_url == '/') { /* redirection to same server */
1025 path = OPENSSL_strdup(redirection_url);
1026 goto new_rpath;
1027 }
1028 OPENSSL_free(host);
1029 OPENSSL_free(port);
1030 continue;
1031 }
1032 OPENSSL_free(redirection_url);
1033 }
1034 OPENSSL_free(host);
1035 OPENSSL_free(port);
1036 break;
1037 }
1038 OPENSSL_free(current_url);
1039 return resp;
1040 }
1041
1042 /* Get ASN.1-encoded data via HTTP from server at given URL */
1043 ASN1_VALUE *OSSL_HTTP_get_asn1(const char *url,
1044 const char *proxy, const char *no_proxy,
1045 BIO *bio, BIO *rbio,
1046 OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
1047 const STACK_OF(CONF_VALUE) *headers,
1048 int maxline, unsigned long max_resp_len,
1049 int timeout, const char *expected_content_type,
1050 const ASN1_ITEM *it)
1051 {
1052 BIO *mem;
1053 ASN1_VALUE *resp = NULL;
1054
1055 if (url == NULL || it == NULL) {
1056 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
1057 return NULL;
1058 }
1059 if ((mem = OSSL_HTTP_get(url, proxy, no_proxy, bio, rbio, bio_update_fn,
1060 arg, headers, maxline, max_resp_len, timeout,
1061 expected_content_type, 1 /* expect_asn1 */))
1062 != NULL)
1063 resp = BIO_mem_d2i(mem, it);
1064 BIO_free(mem);
1065 return resp;
1066 }
1067
1068 /* Post ASN.1-encoded request via HTTP to server return ASN.1 response */
1069 ASN1_VALUE *OSSL_HTTP_post_asn1(const char *server, const char *port,
1070 const char *path, int use_ssl,
1071 const char *proxy, const char *no_proxy,
1072 BIO *bio, BIO *rbio,
1073 OSSL_HTTP_bio_cb_t bio_update_fn, void *arg,
1074 const STACK_OF(CONF_VALUE) *headers,
1075 const char *content_type,
1076 const ASN1_VALUE *req, const ASN1_ITEM *req_it,
1077 int maxline, unsigned long max_resp_len,
1078 int timeout, const char *expected_ct,
1079 const ASN1_ITEM *rsp_it)
1080 {
1081 BIO *req_mem;
1082 BIO *res_mem;
1083 ASN1_VALUE *resp = NULL;
1084
1085 if (req == NULL) {
1086 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
1087 return NULL;
1088 }
1089 /* remaining parameters are checked indirectly */
1090
1091 req_mem = HTTP_asn1_item2bio(req_it, req);
1092 res_mem = OSSL_HTTP_transfer(server, port, path, use_ssl, proxy, no_proxy,
1093 bio, rbio,
1094 bio_update_fn, arg, headers, content_type,
1095 req_mem /* may be NULL */, maxline,
1096 max_resp_len, timeout,
1097 expected_ct, 1 /* expect_asn1 */, NULL);
1098 BIO_free(req_mem);
1099 if (res_mem != NULL)
1100 resp = BIO_mem_d2i(res_mem, rsp_it);
1101 BIO_free(res_mem);
1102 return resp;
1103 }
1104
1105 /* BASE64 encoder used for encoding basic proxy authentication credentials */
1106 static char *base64encode(const void *buf, size_t len)
1107 {
1108 int i;
1109 size_t outl;
1110 char *out;
1111
1112 /* Calculate size of encoded data */
1113 outl = (len / 3);
1114 if (len % 3 > 0)
1115 outl++;
1116 outl <<= 2;
1117 out = OPENSSL_malloc(outl + 1);
1118 if (out == NULL)
1119 return 0;
1120
1121 i = EVP_EncodeBlock((unsigned char *)out, buf, len);
1122 if (!ossl_assert(0 <= i && (size_t)i <= outl)) {
1123 OPENSSL_free(out);
1124 return NULL;
1125 }
1126 return out;
1127 }
1128
1129 /*
1130 * Promote the given connection BIO using the CONNECT method for a TLS proxy.
1131 * This is typically called by an app, so bio_err and prog are used unless NULL
1132 * to print additional diagnostic information in a user-oriented way.
1133 */
1134 int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port,
1135 const char *proxyuser, const char *proxypass,
1136 int timeout, BIO *bio_err, const char *prog)
1137 {
1138 #undef BUF_SIZE
1139 #define BUF_SIZE (8 * 1024)
1140 char *mbuf = OPENSSL_malloc(BUF_SIZE);
1141 char *mbufp;
1142 int read_len = 0;
1143 int ret = 0;
1144 BIO *fbio = BIO_new(BIO_f_buffer());
1145 int rv;
1146 time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
1147
1148 if (bio == NULL || server == NULL
1149 || (bio_err != NULL && prog == NULL)) {
1150 ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER);
1151 goto end;
1152 }
1153 if (port == NULL || *port == '\0')
1154 port = OSSL_HTTPS_PORT;
1155
1156 if (mbuf == NULL || fbio == NULL) {
1157 BIO_printf(bio_err /* may be NULL */, "%s: out of memory", prog);
1158 goto end;
1159 }
1160 BIO_push(fbio, bio);
1161
1162 BIO_printf(fbio, "CONNECT %s:%s "HTTP_PREFIX"1.0\r\n", server, port);
1163
1164 /*
1165 * Workaround for broken proxies which would otherwise close
1166 * the connection when entering tunnel mode (e.g., Squid 2.6)
1167 */
1168 BIO_printf(fbio, "Proxy-Connection: Keep-Alive\r\n");
1169
1170 /* Support for basic (base64) proxy authentication */
1171 if (proxyuser != NULL) {
1172 size_t len = strlen(proxyuser) + 1;
1173 char *proxyauth, *proxyauthenc = NULL;
1174
1175 if (proxypass != NULL)
1176 len += strlen(proxypass);
1177 proxyauth = OPENSSL_malloc(len + 1);
1178 if (proxyauth == NULL)
1179 goto end;
1180 if (BIO_snprintf(proxyauth, len + 1, "%s:%s", proxyuser,
1181 proxypass != NULL ? proxypass : "") != (int)len)
1182 goto proxy_end;
1183 proxyauthenc = base64encode(proxyauth, len);
1184 if (proxyauthenc != NULL) {
1185 BIO_printf(fbio, "Proxy-Authorization: Basic %s\r\n", proxyauthenc);
1186 OPENSSL_clear_free(proxyauthenc, strlen(proxyauthenc));
1187 }
1188 proxy_end:
1189 OPENSSL_clear_free(proxyauth, len);
1190 if (proxyauthenc == NULL)
1191 goto end;
1192 }
1193
1194 /* Terminate the HTTP CONNECT request */
1195 BIO_printf(fbio, "\r\n");
1196
1197 for (;;) {
1198 if (BIO_flush(fbio) != 0)
1199 break;
1200 /* potentially needs to be retried if BIO is non-blocking */
1201 if (!BIO_should_retry(fbio))
1202 break;
1203 }
1204
1205 for (;;) {
1206 /* will not actually wait if timeout == 0 */
1207 rv = BIO_wait(fbio, max_time, 100 /* milliseconds */);
1208 if (rv <= 0) {
1209 BIO_printf(bio_err, "%s: HTTP CONNECT %s\n", prog,
1210 rv == 0 ? "timed out" : "failed waiting for data");
1211 goto end;
1212 }
1213
1214 /*-
1215 * The first line is the HTTP response.
1216 * According to RFC 7230, it is formatted exactly like this:
1217 * HTTP/d.d ddd Reason text\r\n
1218 */
1219 read_len = BIO_gets(fbio, mbuf, BUF_SIZE);
1220 /* the BIO may not block, so we must wait for the 1st line to come in */
1221 if (read_len < HTTP_LINE1_MINLEN)
1222 continue;
1223
1224 /* RFC 7231 4.3.6: any 2xx status code is valid */
1225 if (strncmp(mbuf, HTTP_PREFIX, strlen(HTTP_PREFIX)) != 0) {
1226 ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_PARSE_ERROR);
1227 BIO_printf(bio_err, "%s: HTTP CONNECT failed, non-HTTP response\n",
1228 prog);
1229 /* Wrong protocol, not even HTTP, so stop reading headers */
1230 goto end;
1231 }
1232 mbufp = mbuf + strlen(HTTP_PREFIX);
1233 if (strncmp(mbufp, HTTP_VERSION_PATT, strlen(HTTP_VERSION_PATT)) != 0) {
1234 ERR_raise(ERR_LIB_HTTP, HTTP_R_RECEIVED_WRONG_HTTP_VERSION);
1235 BIO_printf(bio_err,
1236 "%s: HTTP CONNECT failed, bad HTTP version %.*s\n",
1237 prog, HTTP_VERSION_STR_LEN, mbufp);
1238 goto end;
1239 }
1240 mbufp += HTTP_VERSION_STR_LEN;
1241 if (strncmp(mbufp, " 2", strlen(" 2")) != 0) {
1242 mbufp += 1;
1243 /* chop any trailing whitespace */
1244 while (read_len > 0 && ossl_isspace(mbuf[read_len - 1]))
1245 read_len--;
1246 mbuf[read_len] = '\0';
1247 ERR_raise(ERR_LIB_HTTP, HTTP_R_CONNECT_FAILURE);
1248 ERR_add_error_data(2, "Reason=", mbufp);
1249 BIO_printf(bio_err, "%s: HTTP CONNECT failed, Reason=%s\n",
1250 prog, mbufp);
1251 goto end;
1252 }
1253 ret = 1;
1254 break;
1255 }
1256
1257 /* Read past all following headers */
1258 do {
1259 /*
1260 * TODO: This does not necessarily catch the case when the full
1261 * HTTP response came in in more than a single TCP message.
1262 */
1263 read_len = BIO_gets(fbio, mbuf, BUF_SIZE);
1264 } while (read_len > 2);
1265
1266 end:
1267 if (fbio != NULL) {
1268 (void)BIO_flush(fbio);
1269 BIO_pop(fbio);
1270 BIO_free(fbio);
1271 }
1272 OPENSSL_free(mbuf);
1273 return ret;
1274 #undef BUF_SIZE
1275 }