http_test.c 5.4 KB

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