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