http_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright 2020-2021 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. #include <openssl/http.h>
  11. #include <openssl/pem.h>
  12. #include <openssl/x509v3.h>
  13. #include <string.h>
  14. #include "testutil.h"
  15. static const ASN1_ITEM *x509_it = NULL;
  16. static X509 *x509 = NULL;
  17. #define RPATH "/path/result.crt"
  18. typedef struct {
  19. BIO *out;
  20. char version;
  21. int keep_alive;
  22. } server_args;
  23. /*-
  24. * Pretty trivial HTTP mock server:
  25. * For POST, copy request headers+body from mem BIO 'in' as response to 'out'.
  26. * For GET, redirect to RPATH, else respond with 'rsp' of ASN1 type 'it'.
  27. * Respond with HTTP version 1.'version' and 'keep_alive' (unless implicit).
  28. */
  29. static int mock_http_server(BIO *in, BIO *out, char version, int keep_alive,
  30. ASN1_VALUE *rsp, const ASN1_ITEM *it)
  31. {
  32. const char *req, *path;
  33. long count = BIO_get_mem_data(in, (unsigned char **)&req);
  34. const char *hdr = (char *)req;
  35. int is_get = count >= 4 && strncmp(hdr, "GET ", 4) == 0;
  36. int len;
  37. /* first line should contain "<GET or POST> <path> HTTP/1.x" */
  38. if (is_get)
  39. hdr += 4;
  40. else if (TEST_true(count >= 5 && strncmp(hdr, "POST ", 5) == 0))
  41. hdr += 5;
  42. else
  43. return 0;
  44. path = hdr;
  45. hdr = strchr(hdr, ' ');
  46. if (hdr == NULL)
  47. return 0;
  48. len = strlen("HTTP/1.");
  49. if (!TEST_strn_eq(++hdr, "HTTP/1.", len))
  50. return 0;
  51. hdr += len;
  52. /* check for HTTP version 1.0 .. 1.1 */
  53. if (!TEST_char_le('0', *hdr) || !TEST_char_le(*hdr++, '1'))
  54. return 0;
  55. if (!TEST_char_eq(*hdr++, '\r') || !TEST_char_eq(*hdr++, '\n'))
  56. return 0;
  57. count -= (hdr - req);
  58. if (count < 0 || out == NULL)
  59. return 0;
  60. if (strncmp(path, RPATH, strlen(RPATH)) != 0) {
  61. if (!is_get)
  62. return 0;
  63. return BIO_printf(out, "HTTP/1.%c 301 Moved Permanently\r\n"
  64. "Location: %s\r\n\r\n",
  65. version, RPATH) > 0; /* same server */
  66. }
  67. if (BIO_printf(out, "HTTP/1.%c 200 OK\r\n", version) <= 0)
  68. return 0;
  69. if ((version == '0') == keep_alive) /* otherwise, default */
  70. if (BIO_printf(out, "Connection: %s\r\n",
  71. version == '0' ? "keep-alive" : "close") <= 0)
  72. return 0;
  73. if (is_get) { /* construct new header and body */
  74. if ((len = ASN1_item_i2d(rsp, NULL, it)) <= 0)
  75. return 0;
  76. if (BIO_printf(out, "Content-Type: application/x-x509-ca-cert\r\n"
  77. "Content-Length: %d\r\n\r\n", len) <= 0)
  78. return 0;
  79. return ASN1_item_i2d_bio(it, out, rsp);
  80. } else {
  81. len = strlen("Connection: ");
  82. if (strncmp(hdr, "Connection: ", len) == 0) {
  83. /* skip req Connection header */
  84. hdr = strstr(hdr + len, "\r\n");
  85. if (hdr == NULL)
  86. return 0;
  87. hdr += 2;
  88. }
  89. /* echo remaining request header and body */
  90. return BIO_write(out, hdr, count) == count;
  91. }
  92. }
  93. static long http_bio_cb_ex(BIO *bio, int oper, const char *argp, size_t len,
  94. int cmd, long argl, int ret, size_t *processed)
  95. {
  96. server_args *args = (server_args *)BIO_get_callback_arg(bio);
  97. if (oper == (BIO_CB_CTRL | BIO_CB_RETURN) && cmd == BIO_CTRL_FLUSH)
  98. ret = mock_http_server(bio, args->out, args->version, args->keep_alive,
  99. (ASN1_VALUE *)x509, x509_it);
  100. return ret;
  101. }
  102. static int test_http_x509(int do_get)
  103. {
  104. X509 *rcert = NULL;
  105. BIO *wbio = BIO_new(BIO_s_mem());
  106. BIO *rbio = BIO_new(BIO_s_mem());
  107. server_args mock_args = { NULL, '0', 0 };
  108. BIO *rsp, *req = ASN1_item_i2d_mem_bio(x509_it, (ASN1_VALUE *)x509);
  109. STACK_OF(CONF_VALUE) *headers = NULL;
  110. const char content_type[] = "application/x-x509-ca-cert";
  111. int res = 0;
  112. if (wbio == NULL || rbio == NULL || req == NULL)
  113. goto err;
  114. mock_args.out = rbio;
  115. BIO_set_callback_ex(wbio, http_bio_cb_ex);
  116. BIO_set_callback_arg(wbio, (char *)&mock_args);
  117. rsp = do_get ?
  118. OSSL_HTTP_get("/will-be-redirected",
  119. NULL /* proxy */, NULL /* no_proxy */,
  120. wbio, rbio, NULL /* bio_update_fn */, NULL /* arg */,
  121. 0 /* buf_size */, headers, content_type,
  122. 1 /* expect_asn1 */,
  123. OSSL_HTTP_DEFAULT_MAX_RESP_LEN, 0 /* timeout */)
  124. : OSSL_HTTP_transfer(NULL, NULL /* host */, NULL /* port */, RPATH,
  125. 0 /* use_ssl */,NULL /* proxy */, NULL /* no_pr */,
  126. wbio, rbio, NULL /* bio_fn */, NULL /* arg */,
  127. 0 /* buf_size */, headers, content_type,
  128. req, content_type, 1 /* expect_asn1 */,
  129. OSSL_HTTP_DEFAULT_MAX_RESP_LEN, 0 /* timeout */,
  130. 0 /* keep_alive */);
  131. rcert = d2i_X509_bio(rsp, NULL);
  132. BIO_free(rsp);
  133. res = TEST_ptr(rcert) && TEST_int_eq(X509_cmp(x509, rcert), 0);
  134. err:
  135. X509_free(rcert);
  136. BIO_free(req);
  137. BIO_free(wbio);
  138. BIO_free(rbio);
  139. sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
  140. return res;
  141. }
  142. static int test_http_keep_alive(char version, int keep_alive, int kept_alive)
  143. {
  144. BIO *wbio = BIO_new(BIO_s_mem());
  145. BIO *rbio = BIO_new(BIO_s_mem());
  146. BIO *rsp;
  147. server_args mock_args = { NULL, '0', 0 };
  148. const char *const content_type = "application/x-x509-ca-cert";
  149. OSSL_HTTP_REQ_CTX *rctx = NULL;
  150. int i, res = 0;
  151. if (wbio == NULL || rbio == NULL)
  152. goto err;
  153. mock_args.out = rbio;
  154. mock_args.version = version;
  155. mock_args.keep_alive = kept_alive;
  156. BIO_set_callback_ex(wbio, http_bio_cb_ex);
  157. BIO_set_callback_arg(wbio, (char *)&mock_args);
  158. for (res = 1, i = 1; res && i <= 2; i++) {
  159. rsp = OSSL_HTTP_transfer(&rctx, NULL /* server */, NULL /* port */,
  160. RPATH, 0 /* use_ssl */,
  161. NULL /* proxy */, NULL /* no_proxy */,
  162. wbio, rbio, NULL /* bio_update_fn */, NULL,
  163. 0 /* buf_size */, NULL /* headers */,
  164. NULL /* content_type */, NULL /* req => GET */,
  165. content_type, 0 /* ASN.1 not expected */,
  166. 0 /* max_resp_len */, 0 /* timeout */,
  167. keep_alive);
  168. if (keep_alive == 2 && kept_alive == 0)
  169. res = res && TEST_ptr_null(rsp)
  170. && TEST_int_eq(OSSL_HTTP_is_alive(rctx), 0);
  171. else
  172. res = res && TEST_ptr(rsp)
  173. && TEST_int_eq(OSSL_HTTP_is_alive(rctx), keep_alive > 0);
  174. BIO_free(rsp);
  175. (void)BIO_reset(rbio); /* discard response contents */
  176. keep_alive = 0;
  177. }
  178. OSSL_HTTP_close(rctx, res);
  179. err:
  180. BIO_free(wbio);
  181. BIO_free(rbio);
  182. return res;
  183. }
  184. static int test_http_url_ok(const char *url, int exp_ssl, const char *exp_host,
  185. const char *exp_port, const char *exp_path)
  186. {
  187. char *user, *host, *port, *path, *query, *frag;
  188. int exp_num, num, ssl;
  189. int res;
  190. if (!TEST_int_eq(sscanf(exp_port, "%d", &exp_num), 1))
  191. return 0;
  192. res = TEST_true(OSSL_HTTP_parse_url(url, &ssl, &user, &host, &port, &num,
  193. &path, &query, &frag))
  194. && TEST_str_eq(host, exp_host)
  195. && TEST_str_eq(port, exp_port)
  196. && TEST_int_eq(num, exp_num)
  197. && TEST_str_eq(path, exp_path)
  198. && TEST_int_eq(ssl, exp_ssl);
  199. if (res && *user != '\0')
  200. res = TEST_str_eq(user, "user:pass");
  201. if (res && *frag != '\0')
  202. res = TEST_str_eq(frag, "fr");
  203. if (res && *query != '\0')
  204. res = TEST_str_eq(query, "q");
  205. OPENSSL_free(user);
  206. OPENSSL_free(host);
  207. OPENSSL_free(port);
  208. OPENSSL_free(path);
  209. OPENSSL_free(query);
  210. OPENSSL_free(frag);
  211. return res;
  212. }
  213. static int test_http_url_path_query_ok(const char *url, const char *exp_path_qu)
  214. {
  215. char *host, *path;
  216. int res;
  217. res = TEST_true(OSSL_HTTP_parse_url(url, NULL, NULL, &host, NULL, NULL,
  218. &path, NULL, NULL))
  219. && TEST_str_eq(host, "host")
  220. && TEST_str_eq(path, exp_path_qu);
  221. OPENSSL_free(host);
  222. OPENSSL_free(path);
  223. return res;
  224. }
  225. static int test_http_url_dns(void)
  226. {
  227. return test_http_url_ok("host:65535/path", 0, "host", "65535", "/path");
  228. }
  229. static int test_http_url_path_query(void)
  230. {
  231. return test_http_url_path_query_ok("http://usr@host:1/p?q=x#frag", "/p?q=x")
  232. && test_http_url_path_query_ok("http://host?query#frag", "/?query")
  233. && test_http_url_path_query_ok("http://host:9999#frag", "/");
  234. }
  235. static int test_http_url_userinfo_query_fragment(void)
  236. {
  237. return test_http_url_ok("user:pass@host/p?q#fr", 0, "host", "80", "/p");
  238. }
  239. static int test_http_url_ipv4(void)
  240. {
  241. return test_http_url_ok("https://1.2.3.4/p/q", 1, "1.2.3.4", "443", "/p/q");
  242. }
  243. static int test_http_url_ipv6(void)
  244. {
  245. return test_http_url_ok("http://[FF01::101]:6", 0, "[FF01::101]", "6", "/");
  246. }
  247. static int test_http_url_invalid(const char *url)
  248. {
  249. char *host = "1", *port = "1", *path = "1";
  250. int num = 1, ssl = 1;
  251. int res;
  252. res = TEST_false(OSSL_HTTP_parse_url(url, &ssl, NULL, &host, &port, &num,
  253. &path, NULL, NULL))
  254. && TEST_ptr_null(host)
  255. && TEST_ptr_null(port)
  256. && TEST_ptr_null(path);
  257. if (!res) {
  258. OPENSSL_free(host);
  259. OPENSSL_free(port);
  260. OPENSSL_free(path);
  261. }
  262. return res;
  263. }
  264. static int test_http_url_invalid_prefix(void)
  265. {
  266. return test_http_url_invalid("htttps://1.2.3.4:65535/pkix");
  267. }
  268. static int test_http_url_invalid_port(void)
  269. {
  270. return test_http_url_invalid("https://1.2.3.4:65536/pkix");
  271. }
  272. static int test_http_url_invalid_path(void)
  273. {
  274. return test_http_url_invalid("https://[FF01::101]pkix");
  275. }
  276. static int test_http_get_x509(void)
  277. {
  278. return test_http_x509(1);
  279. }
  280. static int test_http_post_x509(void)
  281. {
  282. return test_http_x509(0);
  283. }
  284. static int test_http_keep_alive_0_no_no(void)
  285. {
  286. return test_http_keep_alive('0', 0, 0);
  287. }
  288. static int test_http_keep_alive_1_no_no(void)
  289. {
  290. return test_http_keep_alive('1', 0, 0);
  291. }
  292. static int test_http_keep_alive_0_prefer_yes(void)
  293. {
  294. return test_http_keep_alive('0', 1, 1);
  295. }
  296. static int test_http_keep_alive_1_prefer_yes(void)
  297. {
  298. return test_http_keep_alive('1', 1, 1);
  299. }
  300. static int test_http_keep_alive_0_require_yes(void)
  301. {
  302. return test_http_keep_alive('0', 2, 1);
  303. }
  304. static int test_http_keep_alive_1_require_yes(void)
  305. {
  306. return test_http_keep_alive('1', 2, 1);
  307. }
  308. static int test_http_keep_alive_0_require_no(void)
  309. {
  310. return test_http_keep_alive('0', 2, 0);
  311. }
  312. static int test_http_keep_alive_1_require_no(void)
  313. {
  314. return test_http_keep_alive('1', 2, 0);
  315. }
  316. void cleanup_tests(void)
  317. {
  318. X509_free(x509);
  319. }
  320. OPT_TEST_DECLARE_USAGE("cert.pem\n")
  321. int setup_tests(void)
  322. {
  323. if (!test_skip_common_options())
  324. return 0;
  325. x509_it = ASN1_ITEM_rptr(X509);
  326. if (!TEST_ptr((x509 = load_cert_pem(test_get_argument(0), NULL))))
  327. return 0;
  328. ADD_TEST(test_http_url_dns);
  329. ADD_TEST(test_http_url_path_query);
  330. ADD_TEST(test_http_url_userinfo_query_fragment);
  331. ADD_TEST(test_http_url_ipv4);
  332. ADD_TEST(test_http_url_ipv6);
  333. ADD_TEST(test_http_url_invalid_prefix);
  334. ADD_TEST(test_http_url_invalid_port);
  335. ADD_TEST(test_http_url_invalid_path);
  336. ADD_TEST(test_http_get_x509);
  337. ADD_TEST(test_http_post_x509);
  338. ADD_TEST(test_http_keep_alive_0_no_no);
  339. ADD_TEST(test_http_keep_alive_1_no_no);
  340. ADD_TEST(test_http_keep_alive_0_prefer_yes);
  341. ADD_TEST(test_http_keep_alive_1_prefer_yes);
  342. ADD_TEST(test_http_keep_alive_0_require_yes);
  343. ADD_TEST(test_http_keep_alive_1_require_yes);
  344. ADD_TEST(test_http_keep_alive_0_require_no);
  345. ADD_TEST(test_http_keep_alive_1_require_no);
  346. return 1;
  347. }