Raise an error on syscall failure in tls_retry_write_records
[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 static const ASN1_ITEM *x509_it = NULL;
19 static X509 *x509 = NULL;
20 #define SERVER "mock.server"
21 #define PORT   "81"
22 #define RPATH  "path/any.crt"
23 static const char *rpath;
24
25 /*
26  * pretty trivial HTTP mock server:
27  * for POST, copy request headers+body from mem BIO 'in' as response to 'out'
28  * for GET, first redirect the request then respond with 'rsp' of ASN1 type 'it'
29  */
30 static int mock_http_server(BIO *in, BIO *out,
31                             ASN1_VALUE *rsp, const ASN1_ITEM *it)
32 {
33     const char *req;
34     long count = BIO_get_mem_data(in, (unsigned char **)&req);
35     const char *hdr = (char *)req;
36     int is_get = count >= 4 && strncmp(hdr, "GET ", 4) == 0;
37     int len;
38
39     /* first line should contain "<GET or POST> <rpath> HTTP/1.x" */
40     if (is_get)
41         hdr += 4;
42     else if (TEST_true(count >= 5 && strncmp(hdr, "POST ", 5) == 0))
43         hdr += 5;
44     else
45         return 0;
46
47     while (*rpath == '/')
48         rpath++;
49     while (*hdr == '/')
50         hdr++;
51     len = strlen(rpath);
52     if (!TEST_strn_eq(hdr, rpath, len) || !TEST_char_eq(hdr++[len], ' '))
53         return 0;
54     hdr += len;
55     len = strlen("HTTP/1.");
56     if (!TEST_strn_eq(hdr, "HTTP/1.", len))
57         return 0;
58     hdr += len;
59     /* check for HTTP version 1.0 .. 1.1 */
60     if (!TEST_char_le('0', *hdr) || !TEST_char_le(*hdr++, '1'))
61         return 0;
62     if (!TEST_char_eq(*hdr++, '\r') || !TEST_char_eq(*hdr++, '\n'))
63         return 0;
64     count -= (hdr - req);
65     if (count <= 0 || out == NULL)
66         return 0;
67
68     if (is_get && strcmp(rpath, RPATH) == 0) {
69         rpath = "path/new.crt";
70         return BIO_printf(out, "HTTP/1.1 301 Moved Permanently\r\n"
71                           "Location: /%s\r\n\r\n", rpath) > 0; /* same server */
72     }
73     if (BIO_printf(out, "HTTP/1.1 200 OK\r\n") <= 0)
74         return 0;
75     if (is_get) { /* construct new header and body */
76         if ((len = ASN1_item_i2d(rsp, NULL, it)) <= 0)
77             return 0;
78         if (BIO_printf(out, "Content-Type: application/x-x509-ca-cert\r\n"
79                        "Content-Length: %d\r\n\r\n", len) <= 0)
80             return 0;
81         return ASN1_item_i2d_bio(it, out, rsp);
82     } else {
83         return BIO_write(out, hdr, count) == count; /* echo header and body */
84     }
85 }
86
87 static long http_bio_cb_ex(BIO *bio, int oper, const char *argp, size_t len,
88                            int cmd, long argl, int ret, size_t *processed)
89 {
90
91     if (oper == (BIO_CB_CTRL | BIO_CB_RETURN) && cmd == BIO_CTRL_FLUSH)
92         ret = mock_http_server(bio, (BIO *)BIO_get_callback_arg(bio),
93                                (ASN1_VALUE *)x509, x509_it);
94     return ret;
95 }
96
97 static int test_http_x509(int do_get)
98 {
99     X509 *rcert = NULL;
100     BIO *wbio = BIO_new(BIO_s_mem());
101     BIO *rbio = BIO_new(BIO_s_mem());
102     STACK_OF(CONF_VALUE) *headers = NULL;
103     int res = 0;
104
105     if (wbio == NULL || rbio == NULL)
106         goto err;
107     BIO_set_callback_ex(wbio, http_bio_cb_ex);
108     BIO_set_callback_arg(wbio, (char *)rbio);
109
110     rpath = RPATH;
111     rcert = (X509 *)
112         (do_get ?
113          OSSL_HTTP_get_asn1("http://"SERVER":"PORT"/"RPATH,
114                             NULL /* proxy */, NULL /* no_proxy */,
115                             wbio, rbio, NULL /* bio_update_fn */, NULL,
116                             headers, 0 /* maxline */,
117                             0 /* max_resp_len */, 0 /* timeout */,
118                             "application/x-x509-ca-cert", x509_it)
119          :
120          OSSL_HTTP_post_asn1(SERVER, PORT, RPATH, 0 /* use_ssl */,
121                              NULL /* proxy */, NULL /* no_proxy */,
122                              wbio, rbio, NULL /* bio_update_fn */, NULL,
123                              headers, "application/x-x509-ca-cert",
124                              (ASN1_VALUE *)x509, x509_it, 0 /* maxline */,
125                              0 /* max_resp_len */, 0 /* timeout */,
126                              "application/x-x509-ca-cert", x509_it)
127          );
128     res = TEST_ptr(rcert) && TEST_int_eq(X509_cmp(x509, rcert), 0);
129
130  err:
131     X509_free(rcert);
132     BIO_free(wbio);
133     BIO_free(rbio);
134     sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
135     return res;
136 }
137
138 static int test_http_url_ok(const char *url, const char *exp_host, int exp_ssl)
139 {
140     char *host, *port, *path;
141     int num, ssl;
142     int res;
143
144     res = TEST_true(OSSL_HTTP_parse_url(url, &host, &port, &num, &path, &ssl))
145         && TEST_str_eq(host, exp_host)
146         && TEST_str_eq(port, "65535")
147         && TEST_int_eq(num, 65535)
148         && TEST_str_eq(path, "/pkix")
149         && TEST_int_eq(ssl, exp_ssl);
150     OPENSSL_free(host);
151     OPENSSL_free(port);
152     OPENSSL_free(path);
153     return res;
154 }
155
156 static int test_http_url_dns(void)
157 {
158     return test_http_url_ok("server:65535/pkix", "server", 0);
159 }
160
161 static int test_http_url_ipv4(void)
162 {
163     return test_http_url_ok("https://1.2.3.4:65535/pkix", "1.2.3.4", 1);
164 }
165
166 static int test_http_url_ipv6(void)
167 {
168     return test_http_url_ok("http://[FF01::101]:65535/pkix", "FF01::101", 0);
169 }
170
171 static int test_http_url_invalid(const char *url)
172 {
173     char *host = "1", *port = "1", *path = "1";
174     int num = 1, ssl = 1;
175     int res;
176
177     res = TEST_false(OSSL_HTTP_parse_url(url, &host, &port, &num, &path, &ssl))
178         && TEST_ptr_null(host)
179         && TEST_ptr_null(port)
180         && TEST_ptr_null(path);
181     if (!res) {
182         OPENSSL_free(host);
183         OPENSSL_free(port);
184         OPENSSL_free(path);
185     }
186     return res;
187 }
188
189 static int test_http_url_invalid_prefix(void)
190 {
191     return test_http_url_invalid("htttps://1.2.3.4:65535/pkix");
192 }
193
194 static int test_http_url_invalid_port(void)
195 {
196     return test_http_url_invalid("https://1.2.3.4:65536/pkix");
197 }
198
199 static int test_http_url_invalid_path(void)
200 {
201     return test_http_url_invalid("https://[FF01::101]pkix");
202 }
203
204 static int test_http_get_x509(void)
205 {
206     return test_http_x509(1);
207 }
208
209 static int test_http_post_x509(void)
210 {
211     return test_http_x509(0);
212 }
213
214 void cleanup_tests(void)
215 {
216     X509_free(x509);
217 }
218
219 int setup_tests(void)
220 {
221     if (!test_skip_common_options()) {
222         TEST_error("Error parsing test options\n");
223         return 0;
224     }
225
226     x509_it = ASN1_ITEM_rptr(X509);
227     if (!TEST_ptr((x509 = load_cert_pem(test_get_argument(0), NULL))))
228         return 1;
229
230     ADD_TEST(test_http_url_dns);
231     ADD_TEST(test_http_url_ipv4);
232     ADD_TEST(test_http_url_ipv6);
233     ADD_TEST(test_http_url_invalid_prefix);
234     ADD_TEST(test_http_url_invalid_port);
235     ADD_TEST(test_http_url_invalid_path);
236     ADD_TEST(test_http_get_x509);
237     ADD_TEST(test_http_post_x509);
238     return 1;
239 }