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