2 * Copyright 2020 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 SERVER "mock.server"
22 #define RPATH "path/any.crt"
23 static const char *rpath;
25 static X509 *load_pem_cert(const char *file)
30 if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
32 if (TEST_int_gt(BIO_read_filename(bio, file), 0))
33 (void)TEST_ptr(cert = PEM_read_bio_X509(bio, NULL, NULL, NULL));
40 * pretty trivial HTTP mock server:
41 * for POST, copy request headers+body from mem BIO 'in' as response to 'out'
42 * for GET, first redirect the request then respond with 'rsp' of ASN1 type 'it'
44 static int mock_http_server(BIO *in, BIO *out,
45 ASN1_VALUE *rsp, const ASN1_ITEM *it)
48 long count = BIO_get_mem_data(in, (unsigned char **)&req);
49 const char *hdr = (char *)req;
50 int is_get = count >= 4 && strncmp(hdr, "GET ", 4) == 0;
53 /* first line should contain "<GET or POST> <rpath> HTTP/1.x" */
56 else if (TEST_true(count >= 5 && strncmp(hdr, "POST ", 5) == 0))
66 if (!TEST_strn_eq(hdr, rpath, len) || !TEST_char_eq(hdr++[len], ' '))
69 len = strlen("HTTP/1.");
70 if (!TEST_strn_eq(hdr, "HTTP/1.", len))
73 /* check for HTTP version 1.0 .. 1.1 */
74 if (!TEST_char_le('0', *hdr) || !TEST_char_le(*hdr++, '1'))
76 if (!TEST_char_eq(*hdr++, '\r') || !TEST_char_eq(*hdr++, '\n'))
79 if (count <= 0 || out == NULL)
82 if (is_get && strcmp(rpath, RPATH) == 0) {
83 rpath = "path/new.crt";
84 return BIO_printf(out, "HTTP/1.1 301 Moved Permanently\r\n"
85 "Location: /%s\r\n\r\n", rpath) > 0; /* same server */
87 if (BIO_printf(out, "HTTP/1.1 200 OK\r\n") <= 0)
89 if (is_get) { /* construct new header and body */
90 if ((len = ASN1_item_i2d(rsp, NULL, it)) <= 0)
92 if (BIO_printf(out, "Content-Type: application/x-x509-ca-cert\r\n"
93 "Content-Length: %d\r\n\r\n", len) <= 0)
95 return ASN1_item_i2d_bio(it, out, rsp);
97 return BIO_write(out, hdr, count) == count; /* echo header and body */
101 static long http_bio_cb_ex(BIO *bio, int oper, const char *argp, size_t len,
102 int cmd, long argl, int ret, size_t *processed)
105 if (oper == (BIO_CB_CTRL | BIO_CB_RETURN) && cmd == BIO_CTRL_FLUSH)
106 ret = mock_http_server(bio, (BIO *)BIO_get_callback_arg(bio),
107 (ASN1_VALUE *)x509, x509_it);
111 static int test_http_x509(int do_get)
114 BIO *wbio = BIO_new(BIO_s_mem());
115 BIO *rbio = BIO_new(BIO_s_mem());
116 STACK_OF(CONF_VALUE) *headers = NULL;
119 if (wbio == NULL || rbio == NULL)
121 BIO_set_callback_ex(wbio, http_bio_cb_ex);
122 BIO_set_callback_arg(wbio, (char *)rbio);
127 OSSL_HTTP_get_asn1("http://"SERVER":"PORT"/"RPATH,
128 NULL /* proxy */, NULL /* no_proxy */,
129 wbio, rbio, NULL /* bio_update_fn */, NULL,
130 headers, 0 /* maxline */,
131 0 /* max_resp_len */, 0 /* timeout */,
132 "application/x-x509-ca-cert", x509_it)
134 OSSL_HTTP_post_asn1(SERVER, PORT, RPATH, 0 /* use_ssl */,
135 NULL /* proxy */, NULL /* no_proxy */,
136 wbio, rbio, NULL /* bio_update_fn */, NULL,
137 headers, "application/x-x509-ca-cert",
138 (ASN1_VALUE *)x509, x509_it, 0 /* maxline */,
139 0 /* max_resp_len */, 0 /* timeout */,
140 "application/x-x509-ca-cert", x509_it)
142 res = TEST_ptr(rcert) && TEST_int_eq(X509_cmp(x509, rcert), 0);
148 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
152 static int test_http_url_ok(const char *url, const char *exp_host, int exp_ssl)
154 char *host, *port, *path;
158 res = TEST_true(OSSL_HTTP_parse_url(url, &host, &port, &num, &path, &ssl))
159 && TEST_str_eq(host, exp_host)
160 && TEST_str_eq(port, "65535")
161 && TEST_int_eq(num, 65535)
162 && TEST_str_eq(path, "/pkix")
163 && TEST_int_eq(ssl, exp_ssl);
170 static int test_http_url_dns(void)
172 return test_http_url_ok("server:65535/pkix", "server", 0);
175 static int test_http_url_ipv4(void)
177 return test_http_url_ok("https://1.2.3.4:65535/pkix", "1.2.3.4", 1);
180 static int test_http_url_ipv6(void)
182 return test_http_url_ok("http://[FF01::101]:65535/pkix", "FF01::101", 0);
185 static int test_http_url_invalid(const char *url)
187 char *host = "1", *port = "1", *path = "1";
188 int num = 1, ssl = 1;
191 res = TEST_false(OSSL_HTTP_parse_url(url, &host, &port, &num, &path, &ssl))
192 && TEST_ptr_null(host)
193 && TEST_ptr_null(port)
194 && TEST_ptr_null(path);
203 static int test_http_url_invalid_prefix(void)
205 return test_http_url_invalid("htttps://1.2.3.4:65535/pkix");
208 static int test_http_url_invalid_port(void)
210 return test_http_url_invalid("https://1.2.3.4:65536/pkix");
213 static int test_http_url_invalid_path(void)
215 return test_http_url_invalid("https://[FF01::101]pkix");
218 static int test_http_get_x509(void)
220 return test_http_x509(1);
223 static int test_http_post_x509(void)
225 return test_http_x509(0);
228 void cleanup_tests(void)
233 int setup_tests(void)
235 if (!test_skip_common_options()) {
236 TEST_error("Error parsing test options\n");
240 x509_it = ASN1_ITEM_rptr(X509);
241 if (!TEST_ptr((x509 = load_pem_cert(test_get_argument(0)))))
244 ADD_TEST(test_http_url_dns);
245 ADD_TEST(test_http_url_ipv4);
246 ADD_TEST(test_http_url_ipv6);
247 ADD_TEST(test_http_url_invalid_prefix);
248 ADD_TEST(test_http_url_invalid_port);
249 ADD_TEST(test_http_url_invalid_path);
250 ADD_TEST(test_http_get_x509);
251 ADD_TEST(test_http_post_x509);