]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/http_test.c
Fix typo in CONTRIBUTING.md
[thirdparty/openssl.git] / test / http_test.c
1 /*
2 * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Siemens AG 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 <openssl/http.h>
12 #include <openssl/pem.h>
13 #include <openssl/x509v3.h>
14 #include <string.h>
15
16 #include "testutil.h"
17
18 static const ASN1_ITEM *x509_it = NULL;
19 static X509 *x509 = NULL;
20 #define RPATH "/path/result.crt"
21
22 typedef struct {
23 BIO *out;
24 const char *content_type;
25 const char *txt;
26 char version;
27 int keep_alive;
28 } server_args;
29
30 /*-
31 * Pretty trivial HTTP mock server:
32 * For POST, copy request headers+body from mem BIO |in| as response to |out|.
33 * For GET, redirect to RPATH unless already there, else use |content_type| and
34 * respond with |txt| if not NULL, else with |rsp| of ASN1 type |it|.
35 * Response hdr has HTTP version 1.|version| and |keep_alive| (unless implicit).
36 */
37 static int mock_http_server(BIO *in, BIO *out, char version, int keep_alive,
38 const char *content_type, const char *txt,
39 ASN1_VALUE *rsp, const ASN1_ITEM *it)
40 {
41 const char *req, *path;
42 long count = BIO_get_mem_data(in, (unsigned char **)&req);
43 const char *hdr = (char *)req;
44 int len;
45 int is_get = count >= 4 && CHECK_AND_SKIP_PREFIX(hdr, "GET ");
46
47 /* first line should contain "(GET|POST) <path> HTTP/1.x" */
48 if (!is_get
49 && !(TEST_true(count >= 5 && CHECK_AND_SKIP_PREFIX(hdr, "POST "))))
50 return 0;
51
52 path = hdr;
53 hdr = strchr(hdr, ' ');
54 if (hdr == NULL)
55 return 0;
56 len = strlen("HTTP/1.");
57 if (!TEST_strn_eq(++hdr, "HTTP/1.", len))
58 return 0;
59 hdr += len;
60 /* check for HTTP version 1.0 .. 1.1 */
61 if (!TEST_char_le('0', *hdr) || !TEST_char_le(*hdr++, '1'))
62 return 0;
63 if (!TEST_char_eq(*hdr++, '\r') || !TEST_char_eq(*hdr++, '\n'))
64 return 0;
65 count -= (hdr - req);
66 if (count < 0 || out == NULL)
67 return 0;
68
69 if (!HAS_PREFIX(path, RPATH)) {
70 if (!is_get)
71 return 0;
72 return BIO_printf(out, "HTTP/1.%c 301 Moved Permanently\r\n"
73 "Location: %s\r\n\r\n",
74 version, RPATH) > 0; /* same server */
75 }
76 if (BIO_printf(out, "HTTP/1.%c 200 OK\r\n", version) <= 0)
77 return 0;
78 if ((version == '0') == keep_alive) /* otherwise, default */
79 if (BIO_printf(out, "Connection: %s\r\n",
80 version == '0' ? "keep-alive" : "close") <= 0)
81 return 0;
82 if (is_get) { /* construct new header and body */
83 if (txt != NULL)
84 len = strlen(txt);
85 else if ((len = ASN1_item_i2d(rsp, NULL, it)) <= 0)
86 return 0;
87 if (BIO_printf(out, "Content-Type: %s\r\n"
88 "Content-Length: %d\r\n\r\n", content_type, len) <= 0)
89 return 0;
90 if (txt != NULL)
91 return BIO_puts(out, txt);
92 return ASN1_item_i2d_bio(it, out, rsp);
93 } else {
94 if (CHECK_AND_SKIP_PREFIX(hdr, "Connection: ")) {
95 /* skip req Connection header */
96 hdr = strstr(hdr, "\r\n");
97 if (hdr == NULL)
98 return 0;
99 hdr += 2;
100 }
101 /* echo remaining request header and body */
102 return BIO_write(out, hdr, count) == count;
103 }
104 }
105
106 static long http_bio_cb_ex(BIO *bio, int oper, const char *argp, size_t len,
107 int cmd, long argl, int ret, size_t *processed)
108 {
109 server_args *args = (server_args *)BIO_get_callback_arg(bio);
110
111 if (oper == (BIO_CB_CTRL | BIO_CB_RETURN) && cmd == BIO_CTRL_FLUSH)
112 ret = mock_http_server(bio, args->out, args->version, args->keep_alive,
113 args->content_type, args->txt,
114 (ASN1_VALUE *)x509, x509_it);
115 return ret;
116 }
117
118 #define text1 "test\n"
119 #define text2 "more\n"
120 #define REAL_SERVER_URL "http://httpbin.org/"
121 #define DOCTYPE_HTML "<!DOCTYPE html>\n"
122
123 static int test_http_method(int do_get, int do_txt)
124 {
125 BIO *wbio = BIO_new(BIO_s_mem());
126 BIO *rbio = BIO_new(BIO_s_mem());
127 server_args mock_args = { NULL, NULL, NULL, '0', 0 };
128 BIO *req, *rsp;
129 STACK_OF(CONF_VALUE) *headers = NULL;
130 const char *content_type;
131 int res = 0;
132 int real_server = do_txt && 0; /* remove "&& 0" for using real server */
133
134 if (do_txt) {
135 content_type = "text/plain";
136 req = BIO_new(BIO_s_mem());
137 if (req == NULL
138 || BIO_puts(req, text1) != sizeof(text1) - 1
139 || BIO_puts(req, text2) != sizeof(text2) - 1) {
140 BIO_free(req);
141 req = NULL;
142 }
143 mock_args.txt = text1;
144 } else {
145 content_type = "application/x-x509-ca-cert";
146 req = ASN1_item_i2d_mem_bio(x509_it, (ASN1_VALUE *)x509);
147 mock_args.txt = NULL;
148 }
149 if (wbio == NULL || rbio == NULL || req == NULL)
150 goto err;
151
152 mock_args.out = rbio;
153 mock_args.content_type = content_type;
154 BIO_set_callback_ex(wbio, http_bio_cb_ex);
155 BIO_set_callback_arg(wbio, (char *)&mock_args);
156
157 rsp = do_get ?
158 OSSL_HTTP_get(real_server ? REAL_SERVER_URL :
159 do_txt ? RPATH : "/will-be-redirected",
160 NULL /* proxy */, NULL /* no_proxy */,
161 real_server ? NULL : wbio,
162 real_server ? NULL : rbio,
163 NULL /* bio_update_fn */, NULL /* arg */,
164 0 /* buf_size */, headers,
165 real_server ? "text/html; charset=utf-8": content_type,
166 !do_txt /* expect_asn1 */,
167 OSSL_HTTP_DEFAULT_MAX_RESP_LEN, 0 /* timeout */)
168 : OSSL_HTTP_transfer(NULL, NULL /* host */, NULL /* port */, RPATH,
169 0 /* use_ssl */,NULL /* proxy */, NULL /* no_pr */,
170 wbio, rbio, NULL /* bio_fn */, NULL /* arg */,
171 0 /* buf_size */, headers, content_type,
172 req, content_type, !do_txt /* expect_asn1 */,
173 OSSL_HTTP_DEFAULT_MAX_RESP_LEN, 0 /* timeout */,
174 0 /* keep_alive */);
175 if (rsp != NULL) {
176 if (do_get && real_server) {
177 char rtext[sizeof(DOCTYPE_HTML)];
178
179 res = TEST_int_eq(BIO_gets(rsp, rtext, sizeof(rtext)),
180 sizeof(DOCTYPE_HTML) - 1)
181 && TEST_str_eq(rtext, DOCTYPE_HTML);
182 } else if (do_txt) {
183 char rtext[sizeof(text1) + 1 /* more space than needed */];
184
185 res = TEST_int_eq(BIO_gets(rsp, rtext, sizeof(rtext)),
186 sizeof(text1) - 1)
187 && TEST_str_eq(rtext, text1);
188 } else {
189 X509 *rcert = d2i_X509_bio(rsp, NULL);
190
191 res = TEST_ptr(rcert) && TEST_int_eq(X509_cmp(x509, rcert), 0);
192 X509_free(rcert);
193 }
194 BIO_free(rsp);
195 }
196
197 err:
198 BIO_free(req);
199 BIO_free(wbio);
200 BIO_free(rbio);
201 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
202 return res;
203 }
204
205 static int test_http_keep_alive(char version, int keep_alive, int kept_alive)
206 {
207 BIO *wbio = BIO_new(BIO_s_mem());
208 BIO *rbio = BIO_new(BIO_s_mem());
209 BIO *rsp;
210 const char *const content_type = "application/x-x509-ca-cert";
211 server_args mock_args = { NULL, NULL, NULL, '0', 0 };
212 OSSL_HTTP_REQ_CTX *rctx = NULL;
213 int i, res = 0;
214
215 if (wbio == NULL || rbio == NULL)
216 goto err;
217 mock_args.out = rbio;
218 mock_args.content_type = content_type;
219 mock_args.version = version;
220 mock_args.keep_alive = kept_alive;
221 BIO_set_callback_ex(wbio, http_bio_cb_ex);
222 BIO_set_callback_arg(wbio, (char *)&mock_args);
223
224 for (res = 1, i = 1; res && i <= 2; i++) {
225 rsp = OSSL_HTTP_transfer(&rctx, NULL /* server */, NULL /* port */,
226 RPATH, 0 /* use_ssl */,
227 NULL /* proxy */, NULL /* no_proxy */,
228 wbio, rbio, NULL /* bio_update_fn */, NULL,
229 0 /* buf_size */, NULL /* headers */,
230 NULL /* content_type */, NULL /* req => GET */,
231 content_type, 0 /* ASN.1 not expected */,
232 0 /* max_resp_len */, 0 /* timeout */,
233 keep_alive);
234 if (keep_alive == 2 && kept_alive == 0)
235 res = res && TEST_ptr_null(rsp)
236 && TEST_int_eq(OSSL_HTTP_is_alive(rctx), 0);
237 else
238 res = res && TEST_ptr(rsp)
239 && TEST_int_eq(OSSL_HTTP_is_alive(rctx), keep_alive > 0);
240 BIO_free(rsp);
241 (void)BIO_reset(rbio); /* discard response contents */
242 keep_alive = 0;
243 }
244 OSSL_HTTP_close(rctx, res);
245
246 err:
247 BIO_free(wbio);
248 BIO_free(rbio);
249 return res;
250 }
251
252 static int test_http_url_ok(const char *url, int exp_ssl, const char *exp_host,
253 const char *exp_port, const char *exp_path)
254 {
255 char *user, *host, *port, *path, *query, *frag;
256 int exp_num, num, ssl;
257 int res;
258
259 if (!TEST_int_eq(sscanf(exp_port, "%d", &exp_num), 1))
260 return 0;
261 res = TEST_true(OSSL_HTTP_parse_url(url, &ssl, &user, &host, &port, &num,
262 &path, &query, &frag))
263 && TEST_str_eq(host, exp_host)
264 && TEST_str_eq(port, exp_port)
265 && TEST_int_eq(num, exp_num)
266 && TEST_str_eq(path, exp_path)
267 && TEST_int_eq(ssl, exp_ssl);
268 if (res && *user != '\0')
269 res = TEST_str_eq(user, "user:pass");
270 if (res && *frag != '\0')
271 res = TEST_str_eq(frag, "fr");
272 if (res && *query != '\0')
273 res = TEST_str_eq(query, "q");
274 OPENSSL_free(user);
275 OPENSSL_free(host);
276 OPENSSL_free(port);
277 OPENSSL_free(path);
278 OPENSSL_free(query);
279 OPENSSL_free(frag);
280 return res;
281 }
282
283 static int test_http_url_path_query_ok(const char *url, const char *exp_path_qu)
284 {
285 char *host, *path;
286 int res;
287
288 res = TEST_true(OSSL_HTTP_parse_url(url, NULL, NULL, &host, NULL, NULL,
289 &path, NULL, NULL))
290 && TEST_str_eq(host, "host")
291 && TEST_str_eq(path, exp_path_qu);
292 OPENSSL_free(host);
293 OPENSSL_free(path);
294 return res;
295 }
296
297 static int test_http_url_dns(void)
298 {
299 return test_http_url_ok("host:65535/path", 0, "host", "65535", "/path");
300 }
301
302 static int test_http_url_path_query(void)
303 {
304 return test_http_url_path_query_ok("http://usr@host:1/p?q=x#frag", "/p?q=x")
305 && test_http_url_path_query_ok("http://host?query#frag", "/?query")
306 && test_http_url_path_query_ok("http://host:9999#frag", "/");
307 }
308
309 static int test_http_url_userinfo_query_fragment(void)
310 {
311 return test_http_url_ok("user:pass@host/p?q#fr", 0, "host", "80", "/p");
312 }
313
314 static int test_http_url_ipv4(void)
315 {
316 return test_http_url_ok("https://1.2.3.4/p/q", 1, "1.2.3.4", "443", "/p/q");
317 }
318
319 static int test_http_url_ipv6(void)
320 {
321 return test_http_url_ok("http://[FF01::101]:6", 0, "[FF01::101]", "6", "/");
322 }
323
324 static int test_http_url_invalid(const char *url)
325 {
326 char *host = "1", *port = "1", *path = "1";
327 int num = 1, ssl = 1;
328 int res;
329
330 res = TEST_false(OSSL_HTTP_parse_url(url, &ssl, NULL, &host, &port, &num,
331 &path, NULL, NULL))
332 && TEST_ptr_null(host)
333 && TEST_ptr_null(port)
334 && TEST_ptr_null(path);
335 if (!res) {
336 OPENSSL_free(host);
337 OPENSSL_free(port);
338 OPENSSL_free(path);
339 }
340 return res;
341 }
342
343 static int test_http_url_invalid_prefix(void)
344 {
345 return test_http_url_invalid("htttps://1.2.3.4:65535/pkix");
346 }
347
348 static int test_http_url_invalid_port(void)
349 {
350 return test_http_url_invalid("https://1.2.3.4:65536/pkix")
351 && test_http_url_invalid("https://1.2.3.4:");
352 }
353
354 static int test_http_url_invalid_path(void)
355 {
356 return test_http_url_invalid("https://[FF01::101]pkix");
357 }
358
359 static int test_http_get_txt(void)
360 {
361 return test_http_method(1 /* GET */, 1);
362 }
363
364 static int test_http_post_txt(void)
365 {
366 return test_http_method(0 /* POST */, 1);
367 }
368
369 static int test_http_get_x509(void)
370 {
371 return test_http_method(1 /* GET */, 0); /* includes redirection */
372 }
373
374 static int test_http_post_x509(void)
375 {
376 return test_http_method(0 /* POST */, 0);
377 }
378
379 static int test_http_keep_alive_0_no_no(void)
380 {
381 return test_http_keep_alive('0', 0, 0);
382 }
383
384 static int test_http_keep_alive_1_no_no(void)
385 {
386 return test_http_keep_alive('1', 0, 0);
387 }
388
389 static int test_http_keep_alive_0_prefer_yes(void)
390 {
391 return test_http_keep_alive('0', 1, 1);
392 }
393
394 static int test_http_keep_alive_1_prefer_yes(void)
395 {
396 return test_http_keep_alive('1', 1, 1);
397 }
398
399 static int test_http_keep_alive_0_require_yes(void)
400 {
401 return test_http_keep_alive('0', 2, 1);
402 }
403
404 static int test_http_keep_alive_1_require_yes(void)
405 {
406 return test_http_keep_alive('1', 2, 1);
407 }
408
409 static int test_http_keep_alive_0_require_no(void)
410 {
411 return test_http_keep_alive('0', 2, 0);
412 }
413
414 static int test_http_keep_alive_1_require_no(void)
415 {
416 return test_http_keep_alive('1', 2, 0);
417 }
418
419 static int test_http_resp_hdr_limit(size_t limit)
420 {
421 BIO *wbio = BIO_new(BIO_s_mem());
422 BIO *rbio = BIO_new(BIO_s_mem());
423 BIO *mem = NULL;
424 server_args mock_args = { NULL, NULL, NULL, '0', 0 };
425 int res = 0;
426 OSSL_HTTP_REQ_CTX *rctx = NULL;
427
428 if (TEST_ptr(wbio) == 0 || TEST_ptr(rbio) == 0)
429 goto err;
430
431 mock_args.txt = text1;
432 mock_args.content_type = "text/plain";
433 mock_args.version = '1';
434 mock_args.out = rbio;
435
436 BIO_set_callback_ex(wbio, http_bio_cb_ex);
437 BIO_set_callback_arg(wbio, (char *)&mock_args);
438
439 rctx = OSSL_HTTP_REQ_CTX_new(wbio, rbio, 8192);
440 if (TEST_ptr(rctx) == 0)
441 goto err;
442
443 if (!TEST_true(OSSL_HTTP_REQ_CTX_set_request_line(rctx, 0 /* GET */,
444 NULL, NULL, RPATH)))
445 goto err;
446
447 OSSL_HTTP_REQ_CTX_set_max_response_hdr_lines(rctx, limit);
448 mem = OSSL_HTTP_REQ_CTX_exchange(rctx);
449
450 /*
451 * Note the server sends 4 http response headers, thus we expect to
452 * see failure here when we set header limit in http response to 1.
453 */
454 if (limit == 1)
455 res = TEST_ptr_null(mem);
456 else
457 res = TEST_ptr(mem);
458
459 err:
460 BIO_free(wbio);
461 BIO_free(rbio);
462 OSSL_HTTP_REQ_CTX_free(rctx);
463
464 return res;
465 }
466
467 static int test_hdr_resp_hdr_limit_none(void)
468 {
469 return test_http_resp_hdr_limit(0);
470 }
471
472 static int test_hdr_resp_hdr_limit_short(void)
473 {
474 return (test_http_resp_hdr_limit(1));
475 }
476
477 static int test_hdr_resp_hdr_limit_256(void)
478 {
479 return test_http_resp_hdr_limit(256);
480 }
481
482 void cleanup_tests(void)
483 {
484 X509_free(x509);
485 }
486
487 OPT_TEST_DECLARE_USAGE("cert.pem\n")
488
489 int setup_tests(void)
490 {
491 if (!test_skip_common_options())
492 return 0;
493
494 x509_it = ASN1_ITEM_rptr(X509);
495 if (!TEST_ptr((x509 = load_cert_pem(test_get_argument(0), NULL))))
496 return 0;
497
498 ADD_TEST(test_http_url_dns);
499 ADD_TEST(test_http_url_path_query);
500 ADD_TEST(test_http_url_userinfo_query_fragment);
501 ADD_TEST(test_http_url_ipv4);
502 ADD_TEST(test_http_url_ipv6);
503 ADD_TEST(test_http_url_invalid_prefix);
504 ADD_TEST(test_http_url_invalid_port);
505 ADD_TEST(test_http_url_invalid_path);
506 ADD_TEST(test_http_get_txt);
507 ADD_TEST(test_http_post_txt);
508 ADD_TEST(test_http_get_x509);
509 ADD_TEST(test_http_post_x509);
510 ADD_TEST(test_http_keep_alive_0_no_no);
511 ADD_TEST(test_http_keep_alive_1_no_no);
512 ADD_TEST(test_http_keep_alive_0_prefer_yes);
513 ADD_TEST(test_http_keep_alive_1_prefer_yes);
514 ADD_TEST(test_http_keep_alive_0_require_yes);
515 ADD_TEST(test_http_keep_alive_1_require_yes);
516 ADD_TEST(test_http_keep_alive_0_require_no);
517 ADD_TEST(test_http_keep_alive_1_require_no);
518 ADD_TEST(test_hdr_resp_hdr_limit_none);
519 ADD_TEST(test_hdr_resp_hdr_limit_short);
520 ADD_TEST(test_hdr_resp_hdr_limit_256);
521 return 1;
522 }