http_test.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <openssl/http.h>
  11. #include <openssl/pem.h>
  12. #include <openssl/x509v3.h>
  13. #include <string.h>
  14. #include "testutil.h"
  15. DEFINE_STACK_OF(CONF_VALUE)
  16. static const ASN1_ITEM *x509_it = NULL;
  17. static X509 *x509 = NULL;
  18. #define SERVER "mock.server"
  19. #define PORT "81"
  20. #define RPATH "path/any.crt"
  21. static const char *rpath;
  22. static X509 *load_pem_cert(const char *file)
  23. {
  24. X509 *cert = NULL;
  25. BIO *bio = NULL;
  26. if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
  27. return NULL;
  28. if (TEST_int_gt(BIO_read_filename(bio, file), 0))
  29. (void)TEST_ptr(cert = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  30. BIO_free(bio);
  31. return cert;
  32. }
  33. /*
  34. * pretty trivial HTTP mock server:
  35. * for POST, copy request headers+body from mem BIO 'in' as response to 'out'
  36. * for GET, first redirect the request then respond with 'rsp' of ASN1 type 'it'
  37. */
  38. static int mock_http_server(BIO *in, BIO *out,
  39. ASN1_VALUE *rsp, const ASN1_ITEM *it)
  40. {
  41. const char *req;
  42. long count = BIO_get_mem_data(in, (unsigned char **)&req);
  43. const char *hdr = (char *)req;
  44. int is_get = count >= 4 && strncmp(hdr, "GET ", 4) == 0;
  45. int len;
  46. /* first line should contain "<GET or POST> <rpath> HTTP/1.x" */
  47. if (is_get)
  48. hdr += 4;
  49. else if (TEST_true(count >= 5 && strncmp(hdr, "POST ", 5) == 0))
  50. hdr += 5;
  51. else
  52. return 0;
  53. while (*rpath == '/')
  54. rpath++;
  55. while (*hdr == '/')
  56. hdr++;
  57. len = strlen(rpath);
  58. if (!TEST_strn_eq(hdr, rpath, len) || !TEST_char_eq(hdr++[len], ' '))
  59. return 0;
  60. hdr += len;
  61. len = strlen("HTTP/1.");
  62. if (!TEST_strn_eq(hdr, "HTTP/1.", len))
  63. return 0;
  64. hdr += len;
  65. /* check for HTTP version 1.0 .. 1.1 */
  66. if (!TEST_char_le('0', *hdr) || !TEST_char_le(*hdr++, '1'))
  67. return 0;
  68. if (!TEST_char_eq(*hdr++, '\r') || !TEST_char_eq(*hdr++, '\n'))
  69. return 0;
  70. count -= (hdr - req);
  71. if (count <= 0 || out == NULL)
  72. return 0;
  73. if (is_get && strcmp(rpath, RPATH) == 0) {
  74. rpath = "path/new.crt";
  75. return BIO_printf(out, "HTTP/1.1 301 Moved Permanently\r\n"
  76. "Location: /%s\r\n\r\n", rpath) > 0; /* same server */
  77. }
  78. if (BIO_printf(out, "HTTP/1.1 200 OK\r\n") <= 0)
  79. return 0;
  80. if (is_get) { /* construct new header and body */
  81. if ((len = ASN1_item_i2d(rsp, NULL, it)) <= 0)
  82. return 0;
  83. if (BIO_printf(out, "Content-Type: application/x-x509-ca-cert\r\n"
  84. "Content-Length: %d\r\n\r\n", len) <= 0)
  85. return 0;
  86. return ASN1_item_i2d_bio(it, out, rsp);
  87. } else {
  88. return BIO_write(out, hdr, count) == count; /* echo header and body */
  89. }
  90. }
  91. static long http_bio_cb_ex(BIO *bio, int oper, const char *argp, size_t len,
  92. int cmd, long argl, int ret, size_t *processed)
  93. {
  94. if (oper == (BIO_CB_CTRL | BIO_CB_RETURN) && cmd == BIO_CTRL_FLUSH)
  95. ret = mock_http_server(bio, (BIO *)BIO_get_callback_arg(bio),
  96. (ASN1_VALUE *)x509, x509_it);
  97. return ret;
  98. }
  99. static int test_http_x509(int do_get)
  100. {
  101. X509 *rcert = NULL;
  102. BIO *wbio = BIO_new(BIO_s_mem());
  103. BIO *rbio = BIO_new(BIO_s_mem());
  104. STACK_OF(CONF_VALUE) *headers = NULL;
  105. int res = 0;
  106. if (wbio == NULL || rbio == NULL)
  107. goto err;
  108. BIO_set_callback_ex(wbio, http_bio_cb_ex);
  109. BIO_set_callback_arg(wbio, (char *)rbio);
  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. err:
  130. X509_free(rcert);
  131. BIO_free(wbio);
  132. BIO_free(rbio);
  133. sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
  134. return res;
  135. }
  136. static int test_http_get_x509(void)
  137. {
  138. return test_http_x509(1);
  139. }
  140. static int test_http_post_x509(void)
  141. {
  142. return test_http_x509(0);
  143. }
  144. void cleanup_tests(void)
  145. {
  146. X509_free(x509);
  147. }
  148. int setup_tests(void)
  149. {
  150. if (!test_skip_common_options()) {
  151. TEST_error("Error parsing test options\n");
  152. return 0;
  153. }
  154. x509_it = ASN1_ITEM_rptr(X509);
  155. if (!TEST_ptr((x509 = load_pem_cert(test_get_argument(0)))))
  156. return 1;
  157. ADD_TEST(test_http_get_x509);
  158. ADD_TEST(test_http_post_x509);
  159. return 1;
  160. }