http_test.c 8.4 KB

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