Add an Apple privacy info file for OpenSSL
[openssl.git] / test / http_test.c
1 /*
2  * Copyright 2020 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 DEFINE_STACK_OF(CONF_VALUE)
19
20 static const ASN1_ITEM *x509_it = NULL;
21 static X509 *x509 = NULL;
22 #define SERVER "mock.server"
23 #define PORT   "81"
24 #define RPATH  "path/any.crt"
25 static const char *rpath;
26
27 static X509 *load_pem_cert(const char *file)
28 {
29     X509 *cert = NULL;
30     BIO *bio = NULL;
31
32     if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
33         return NULL;
34     if (TEST_int_gt(BIO_read_filename(bio, file), 0))
35         (void)TEST_ptr(cert = PEM_read_bio_X509(bio, NULL, NULL, NULL));
36
37     BIO_free(bio);
38     return cert;
39 }
40
41 /*
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'
45  */
46 static int mock_http_server(BIO *in, BIO *out,
47                             ASN1_VALUE *rsp, const ASN1_ITEM *it)
48 {
49     const char *req;
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;
53     int len;
54
55     /* first line should contain "<GET or POST> <rpath> HTTP/1.x" */
56     if (is_get)
57         hdr += 4;
58     else if (TEST_true(count >= 5 && strncmp(hdr, "POST ", 5) == 0))
59         hdr += 5;
60     else
61         return 0;
62
63     while (*rpath == '/')
64         rpath++;
65     while (*hdr == '/')
66         hdr++;
67     len = strlen(rpath);
68     if (!TEST_strn_eq(hdr, rpath, len) || !TEST_char_eq(hdr++[len], ' '))
69         return 0;
70     hdr += len;
71     len = strlen("HTTP/1.");
72     if (!TEST_strn_eq(hdr, "HTTP/1.", len))
73         return 0;
74     hdr += len;
75     /* check for HTTP version 1.0 .. 1.1 */
76     if (!TEST_char_le('0', *hdr) || !TEST_char_le(*hdr++, '1'))
77         return 0;
78     if (!TEST_char_eq(*hdr++, '\r') || !TEST_char_eq(*hdr++, '\n'))
79         return 0;
80     count -= (hdr - req);
81     if (count <= 0 || out == NULL)
82         return 0;
83
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 */
88     }
89     if (BIO_printf(out, "HTTP/1.1 200 OK\r\n") <= 0)
90         return 0;
91     if (is_get) { /* construct new header and body */
92         if ((len = ASN1_item_i2d(rsp, NULL, it)) <= 0)
93             return 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)
96             return 0;
97         return ASN1_item_i2d_bio(it, out, rsp);
98     } else {
99         return BIO_write(out, hdr, count) == count; /* echo header and body */
100     }
101 }
102
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)
105 {
106
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);
110     return ret;
111 }
112
113 static int test_http_x509(int do_get)
114 {
115     X509 *rcert = NULL;
116     BIO *wbio = BIO_new(BIO_s_mem());
117     BIO *rbio = BIO_new(BIO_s_mem());
118     STACK_OF(CONF_VALUE) *headers = NULL;
119     int res = 0;
120
121     if (wbio == NULL || rbio == NULL)
122         goto err;
123     BIO_set_callback_ex(wbio, http_bio_cb_ex);
124     BIO_set_callback_arg(wbio, (char *)rbio);
125
126     rpath = RPATH;
127     rcert = (X509 *)
128         (do_get ?
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)
135          :
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)
143          );
144     res = TEST_ptr(rcert) && TEST_int_eq(X509_cmp(x509, rcert), 0);
145
146  err:
147     X509_free(rcert);
148     BIO_free(wbio);
149     BIO_free(rbio);
150     sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
151     return res;
152 }
153
154 static int test_http_get_x509(void)
155 {
156     return test_http_x509(1);
157 }
158
159 static int test_http_post_x509(void)
160 {
161     return test_http_x509(0);
162 }
163
164 void cleanup_tests(void)
165 {
166     X509_free(x509);
167 }
168
169 int setup_tests(void)
170 {
171     if (!test_skip_common_options()) {
172         TEST_error("Error parsing test options\n");
173         return 0;
174     }
175
176     x509_it = ASN1_ITEM_rptr(X509);
177     if (!TEST_ptr((x509 = load_pem_cert(test_get_argument(0)))))
178         return 1;
179
180     ADD_TEST(test_http_get_x509);
181     ADD_TEST(test_http_post_x509);
182     return 1;
183 }