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