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