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 DEFINE_STACK_OF(CONF_VALUE)
20 static const ASN1_ITEM *x509_it = NULL;
21 static X509 *x509 = NULL;
22 #define SERVER "mock.server"
24 #define RPATH "path/any.crt"
25 static const char *rpath;
27 static X509 *load_pem_cert(const char *file)
32 if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
34 if (TEST_int_gt(BIO_read_filename(bio, file), 0))
35 (void)TEST_ptr(cert = PEM_read_bio_X509(bio, NULL, NULL, NULL));
42 * pretty trivial HTTP mock server:
43 * for POST, copy request headers+body from mem BIO 'in' as response to 'out'
44 * for GET, first redirect the request then respond with 'rsp' of ASN1 type 'it'
46 static int mock_http_server(BIO *in, BIO *out,
47 ASN1_VALUE *rsp, const ASN1_ITEM *it)
50 long count = BIO_get_mem_data(in, (unsigned char **)&req);
51 const char *hdr = (char *)req;
52 int is_get = count >= 4 && strncmp(hdr, "GET ", 4) == 0;
55 /* first line should contain "<GET or POST> <rpath> HTTP/1.x" */
58 else if (TEST_true(count >= 5 && strncmp(hdr, "POST ", 5) == 0))
68 if (!TEST_strn_eq(hdr, rpath, len) || !TEST_char_eq(hdr++[len], ' '))
71 len = strlen("HTTP/1.");
72 if (!TEST_strn_eq(hdr, "HTTP/1.", len))
75 /* check for HTTP version 1.0 .. 1.1 */
76 if (!TEST_char_le('0', *hdr) || !TEST_char_le(*hdr++, '1'))
78 if (!TEST_char_eq(*hdr++, '\r') || !TEST_char_eq(*hdr++, '\n'))
81 if (count <= 0 || out == NULL)
84 if (is_get && strcmp(rpath, RPATH) == 0) {
85 rpath = "path/new.crt";
86 return BIO_printf(out, "HTTP/1.1 301 Moved Permanently\r\n"
87 "Location: /%s\r\n\r\n", rpath) > 0; /* same server */
89 if (BIO_printf(out, "HTTP/1.1 200 OK\r\n") <= 0)
91 if (is_get) { /* construct new header and body */
92 if ((len = ASN1_item_i2d(rsp, NULL, it)) <= 0)
94 if (BIO_printf(out, "Content-Type: application/x-x509-ca-cert\r\n"
95 "Content-Length: %d\r\n\r\n", len) <= 0)
97 return ASN1_item_i2d_bio(it, out, rsp);
99 return BIO_write(out, hdr, count) == count; /* echo header and body */
103 static long http_bio_cb_ex(BIO *bio, int oper, const char *argp, size_t len,
104 int cmd, long argl, int ret, size_t *processed)
107 if (oper == (BIO_CB_CTRL | BIO_CB_RETURN) && cmd == BIO_CTRL_FLUSH)
108 ret = mock_http_server(bio, (BIO *)BIO_get_callback_arg(bio),
109 (ASN1_VALUE *)x509, x509_it);
113 static int test_http_x509(int do_get)
116 BIO *wbio = BIO_new(BIO_s_mem());
117 BIO *rbio = BIO_new(BIO_s_mem());
118 STACK_OF(CONF_VALUE) *headers = NULL;
121 if (wbio == NULL || rbio == NULL)
123 BIO_set_callback_ex(wbio, http_bio_cb_ex);
124 BIO_set_callback_arg(wbio, (char *)rbio);
129 OSSL_HTTP_get_asn1("http://"SERVER":"PORT"/"RPATH,
130 NULL /* proxy */, NULL /* no_proxy */,
131 wbio, rbio, NULL /* bio_update_fn */, NULL,
132 headers, 0 /* maxline */,
133 0 /* max_resp_len */, 0 /* timeout */,
134 "application/x-x509-ca-cert", x509_it)
136 OSSL_HTTP_post_asn1(SERVER, PORT, RPATH, 0 /* use_ssl */,
137 NULL /* proxy */, NULL /* no_proxy */,
138 wbio, rbio, NULL /* bio_update_fn */, NULL,
139 headers, "application/x-x509-ca-cert",
140 (ASN1_VALUE *)x509, x509_it, 0 /* maxline */,
141 0 /* max_resp_len */, 0 /* timeout */,
142 "application/x-x509-ca-cert", x509_it)
144 res = TEST_ptr(rcert) && TEST_int_eq(X509_cmp(x509, rcert), 0);
150 sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
154 static int test_http_get_x509(void)
156 return test_http_x509(1);
159 static int test_http_post_x509(void)
161 return test_http_x509(0);
164 void cleanup_tests(void)
169 int setup_tests(void)
171 if (!test_skip_common_options()) {
172 TEST_error("Error parsing test options\n");
176 x509_it = ASN1_ITEM_rptr(X509);
177 if (!TEST_ptr((x509 = load_pem_cert(test_get_argument(0)))))
180 ADD_TEST(test_http_get_x509);
181 ADD_TEST(test_http_post_x509);