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