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