2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Siemens AG 2020
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
11 #include <openssl/http.h>
12 #include <openssl/pem.h>
13 #include <openssl/x509v3.h>
18 static const ASN1_ITEM *x509_it = NULL;
19 static X509 *x509 = NULL;
20 #define RPATH "/path/result.crt"
24 const char *content_type;
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).
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)
41 const char *req, *path;
42 long count = BIO_get_mem_data(in, (unsigned char **)&req);
43 const char *hdr = (char *)req;
45 int is_get = count >= 4 && CHECK_AND_SKIP_PREFIX(hdr, "GET ");
47 /* first line should contain "(GET|POST) <path> HTTP/1.x" */
49 && !(TEST_true(count >= 5 && CHECK_AND_SKIP_PREFIX(hdr, "POST "))))
53 hdr = strchr(hdr, ' ');
56 len = strlen("HTTP/1.");
57 if (!TEST_strn_eq(++hdr, "HTTP/1.", len))
60 /* check for HTTP version 1.0 .. 1.1 */
61 if (!TEST_char_le('0', *hdr) || !TEST_char_le(*hdr++, '1'))
63 if (!TEST_char_eq(*hdr++, '\r') || !TEST_char_eq(*hdr++, '\n'))
66 if (count < 0 || out == NULL)
69 if (!HAS_PREFIX(path, RPATH)) {
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 */
76 if (BIO_printf(out, "HTTP/1.%c 200 OK\r\n", version) <= 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)
82 if (is_get) { /* construct new header and body */
85 else if ((len = ASN1_item_i2d(rsp, NULL, it)) <= 0)
87 if (BIO_printf(out, "Content-Type: %s\r\n"
88 "Content-Length: %d\r\n\r\n", content_type, len) <= 0)
91 return BIO_puts(out, txt);
92 return ASN1_item_i2d_bio(it, out, rsp);
94 if (CHECK_AND_SKIP_PREFIX(hdr, "Connection: ")) {
95 /* skip req Connection header */
96 hdr = strstr(hdr, "\r\n");
101 /* echo remaining request header and body */
102 return BIO_write(out, hdr, count) == count;
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)
109 server_args *args = (server_args *)BIO_get_callback_arg(bio);
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);
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"
123 static int test_http_method(int do_get, int do_txt)
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 };
129 STACK_OF(CONF_VALUE) *headers = NULL;
130 const char *content_type;
132 int real_server = do_txt && 0; /* remove "&& 0" for using real server */
135 content_type = "text/plain";
136 req = BIO_new(BIO_s_mem());
138 || BIO_puts(req, text1) != sizeof(text1) - 1
139 || BIO_puts(req, text2) != sizeof(text2) - 1) {
143 mock_args.txt = text1;
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;
149 if (wbio == NULL || rbio == NULL || req == NULL)
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);
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 */,
176 if (do_get && real_server) {
177 char rtext[sizeof(DOCTYPE_HTML)];
179 res = TEST_int_eq(BIO_gets(rsp, rtext, sizeof(rtext)),
180 sizeof(DOCTYPE_HTML) - 1)
181 && TEST_str_eq(rtext, DOCTYPE_HTML);
183 char rtext[sizeof(text1) + 1 /* more space than needed */];
185 res = TEST_int_eq(BIO_gets(rsp, rtext, sizeof(rtext)),
187 && TEST_str_eq(rtext, text1);
189 X509 *rcert = d2i_X509_bio(rsp, NULL);
191 res = TEST_ptr(rcert) && TEST_int_eq(X509_cmp(x509, rcert), 0);
201 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
205 static int test_http_keep_alive(char version, int keep_alive, int kept_alive)
207 BIO *wbio = BIO_new(BIO_s_mem());
208 BIO *rbio = BIO_new(BIO_s_mem());
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;
215 if (wbio == NULL || rbio == NULL)
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);
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 */,
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);
238 res = res && TEST_ptr(rsp)
239 && TEST_int_eq(OSSL_HTTP_is_alive(rctx), keep_alive > 0);
241 (void)BIO_reset(rbio); /* discard response contents */
244 OSSL_HTTP_close(rctx, res);
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)
255 char *user, *host, *port, *path, *query, *frag;
256 int exp_num, num, ssl;
259 if (!TEST_int_eq(sscanf(exp_port, "%d", &exp_num), 1))
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");
283 static int test_http_url_path_query_ok(const char *url, const char *exp_path_qu)
288 res = TEST_true(OSSL_HTTP_parse_url(url, NULL, NULL, &host, NULL, NULL,
290 && TEST_str_eq(host, "host")
291 && TEST_str_eq(path, exp_path_qu);
297 static int test_http_url_dns(void)
299 return test_http_url_ok("host:65535/path", 0, "host", "65535", "/path");
302 static int test_http_url_path_query(void)
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", "/");
309 static int test_http_url_userinfo_query_fragment(void)
311 return test_http_url_ok("user:pass@host/p?q#fr", 0, "host", "80", "/p");
314 static int test_http_url_ipv4(void)
316 return test_http_url_ok("https://1.2.3.4/p/q", 1, "1.2.3.4", "443", "/p/q");
319 static int test_http_url_ipv6(void)
321 return test_http_url_ok("http://[FF01::101]:6", 0, "[FF01::101]", "6", "/");
324 static int test_http_url_invalid(const char *url)
326 char *host = "1", *port = "1", *path = "1";
327 int num = 1, ssl = 1;
330 res = TEST_false(OSSL_HTTP_parse_url(url, &ssl, NULL, &host, &port, &num,
332 && TEST_ptr_null(host)
333 && TEST_ptr_null(port)
334 && TEST_ptr_null(path);
343 static int test_http_url_invalid_prefix(void)
345 return test_http_url_invalid("htttps://1.2.3.4:65535/pkix");
348 static int test_http_url_invalid_port(void)
350 return test_http_url_invalid("https://1.2.3.4:65536/pkix")
351 && test_http_url_invalid("https://1.2.3.4:");
354 static int test_http_url_invalid_path(void)
356 return test_http_url_invalid("https://[FF01::101]pkix");
359 static int test_http_get_txt(void)
361 return test_http_method(1 /* GET */, 1);
364 static int test_http_post_txt(void)
366 return test_http_method(0 /* POST */, 1);
369 static int test_http_get_x509(void)
371 return test_http_method(1 /* GET */, 0); /* includes redirection */
374 static int test_http_post_x509(void)
376 return test_http_method(0 /* POST */, 0);
379 static int test_http_keep_alive_0_no_no(void)
381 return test_http_keep_alive('0', 0, 0);
384 static int test_http_keep_alive_1_no_no(void)
386 return test_http_keep_alive('1', 0, 0);
389 static int test_http_keep_alive_0_prefer_yes(void)
391 return test_http_keep_alive('0', 1, 1);
394 static int test_http_keep_alive_1_prefer_yes(void)
396 return test_http_keep_alive('1', 1, 1);
399 static int test_http_keep_alive_0_require_yes(void)
401 return test_http_keep_alive('0', 2, 1);
404 static int test_http_keep_alive_1_require_yes(void)
406 return test_http_keep_alive('1', 2, 1);
409 static int test_http_keep_alive_0_require_no(void)
411 return test_http_keep_alive('0', 2, 0);
414 static int test_http_keep_alive_1_require_no(void)
416 return test_http_keep_alive('1', 2, 0);
419 void cleanup_tests(void)
424 OPT_TEST_DECLARE_USAGE("cert.pem\n")
426 int setup_tests(void)
428 if (!test_skip_common_options())
431 x509_it = ASN1_ITEM_rptr(X509);
432 if (!TEST_ptr((x509 = load_cert_pem(test_get_argument(0), NULL))))
435 ADD_TEST(test_http_url_dns);
436 ADD_TEST(test_http_url_path_query);
437 ADD_TEST(test_http_url_userinfo_query_fragment);
438 ADD_TEST(test_http_url_ipv4);
439 ADD_TEST(test_http_url_ipv6);
440 ADD_TEST(test_http_url_invalid_prefix);
441 ADD_TEST(test_http_url_invalid_port);
442 ADD_TEST(test_http_url_invalid_path);
443 ADD_TEST(test_http_get_txt);
444 ADD_TEST(test_http_post_txt);
445 ADD_TEST(test_http_get_x509);
446 ADD_TEST(test_http_post_x509);
447 ADD_TEST(test_http_keep_alive_0_no_no);
448 ADD_TEST(test_http_keep_alive_1_no_no);
449 ADD_TEST(test_http_keep_alive_0_prefer_yes);
450 ADD_TEST(test_http_keep_alive_1_prefer_yes);
451 ADD_TEST(test_http_keep_alive_0_require_yes);
452 ADD_TEST(test_http_keep_alive_1_require_yes);
453 ADD_TEST(test_http_keep_alive_0_require_no);
454 ADD_TEST(test_http_keep_alive_1_require_no);