ssl_test_ctx_test.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Ideally, CONF should offer standard parsing methods and cover them
  11. * in tests. But since we have no CONF tests, we use a custom test for now.
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "internal/nelem.h"
  16. #include "ssl_test_ctx.h"
  17. #include "testutil.h"
  18. #include <openssl/e_os2.h>
  19. #include <openssl/err.h>
  20. #include <openssl/conf.h>
  21. #include <openssl/ssl.h>
  22. static CONF *conf = NULL;
  23. typedef struct ssl_test_ctx_test_fixture {
  24. const char *test_case_name;
  25. const char *test_section;
  26. /* Expected parsed configuration. */
  27. SSL_TEST_CTX *expected_ctx;
  28. } SSL_TEST_CTX_TEST_FIXTURE;
  29. static int clientconf_eq(SSL_TEST_CLIENT_CONF *conf1,
  30. SSL_TEST_CLIENT_CONF *conf2)
  31. {
  32. if (!TEST_int_eq(conf1->verify_callback, conf2->verify_callback)
  33. || !TEST_int_eq(conf1->servername, conf2->servername)
  34. || !TEST_str_eq(conf1->npn_protocols, conf2->npn_protocols)
  35. || !TEST_str_eq(conf1->alpn_protocols, conf2->alpn_protocols)
  36. || !TEST_int_eq(conf1->ct_validation, conf2->ct_validation)
  37. || !TEST_int_eq(conf1->max_fragment_len_mode,
  38. conf2->max_fragment_len_mode))
  39. return 0;
  40. return 1;
  41. }
  42. static int serverconf_eq(SSL_TEST_SERVER_CONF *serv,
  43. SSL_TEST_SERVER_CONF *serv2)
  44. {
  45. if (!TEST_int_eq(serv->servername_callback, serv2->servername_callback)
  46. || !TEST_str_eq(serv->npn_protocols, serv2->npn_protocols)
  47. || !TEST_str_eq(serv->alpn_protocols, serv2->alpn_protocols)
  48. || !TEST_int_eq(serv->broken_session_ticket,
  49. serv2->broken_session_ticket)
  50. || !TEST_str_eq(serv->session_ticket_app_data,
  51. serv2->session_ticket_app_data)
  52. || !TEST_int_eq(serv->cert_status, serv2->cert_status))
  53. return 0;
  54. return 1;
  55. }
  56. static int extraconf_eq(SSL_TEST_EXTRA_CONF *extra,
  57. SSL_TEST_EXTRA_CONF *extra2)
  58. {
  59. if (!TEST_true(clientconf_eq(&extra->client, &extra2->client))
  60. || !TEST_true(serverconf_eq(&extra->server, &extra2->server))
  61. || !TEST_true(serverconf_eq(&extra->server2, &extra2->server2)))
  62. return 0;
  63. return 1;
  64. }
  65. static int testctx_eq(SSL_TEST_CTX *ctx, SSL_TEST_CTX *ctx2)
  66. {
  67. if (!TEST_int_eq(ctx->method, ctx2->method)
  68. || !TEST_int_eq(ctx->handshake_mode, ctx2->handshake_mode)
  69. || !TEST_int_eq(ctx->app_data_size, ctx2->app_data_size)
  70. || !TEST_int_eq(ctx->max_fragment_size, ctx2->max_fragment_size)
  71. || !extraconf_eq(&ctx->extra, &ctx2->extra)
  72. || !extraconf_eq(&ctx->resume_extra, &ctx2->resume_extra)
  73. || !TEST_int_eq(ctx->expected_result, ctx2->expected_result)
  74. || !TEST_int_eq(ctx->expected_client_alert,
  75. ctx2->expected_client_alert)
  76. || !TEST_int_eq(ctx->expected_server_alert,
  77. ctx2->expected_server_alert)
  78. || !TEST_int_eq(ctx->expected_protocol, ctx2->expected_protocol)
  79. || !TEST_int_eq(ctx->expected_servername, ctx2->expected_servername)
  80. || !TEST_int_eq(ctx->session_ticket_expected,
  81. ctx2->session_ticket_expected)
  82. || !TEST_int_eq(ctx->compression_expected,
  83. ctx2->compression_expected)
  84. || !TEST_str_eq(ctx->expected_npn_protocol,
  85. ctx2->expected_npn_protocol)
  86. || !TEST_str_eq(ctx->expected_alpn_protocol,
  87. ctx2->expected_alpn_protocol)
  88. || !TEST_str_eq(ctx->expected_cipher,
  89. ctx2->expected_cipher)
  90. || !TEST_str_eq(ctx->expected_session_ticket_app_data,
  91. ctx2->expected_session_ticket_app_data)
  92. || !TEST_int_eq(ctx->resumption_expected,
  93. ctx2->resumption_expected)
  94. || !TEST_int_eq(ctx->session_id_expected,
  95. ctx2->session_id_expected))
  96. return 0;
  97. return 1;
  98. }
  99. static SSL_TEST_CTX_TEST_FIXTURE *set_up(const char *const test_case_name)
  100. {
  101. SSL_TEST_CTX_TEST_FIXTURE *fixture;
  102. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
  103. return NULL;
  104. fixture->test_case_name = test_case_name;
  105. if (!TEST_ptr(fixture->expected_ctx = SSL_TEST_CTX_new(NULL))) {
  106. OPENSSL_free(fixture);
  107. return NULL;
  108. }
  109. return fixture;
  110. }
  111. static int execute_test(SSL_TEST_CTX_TEST_FIXTURE *fixture)
  112. {
  113. int success = 0;
  114. SSL_TEST_CTX *ctx;
  115. if (!TEST_ptr(ctx = SSL_TEST_CTX_create(conf, fixture->test_section,
  116. fixture->expected_ctx->libctx))
  117. || !testctx_eq(ctx, fixture->expected_ctx))
  118. goto err;
  119. success = 1;
  120. err:
  121. SSL_TEST_CTX_free(ctx);
  122. return success;
  123. }
  124. static void tear_down(SSL_TEST_CTX_TEST_FIXTURE *fixture)
  125. {
  126. SSL_TEST_CTX_free(fixture->expected_ctx);
  127. OPENSSL_free(fixture);
  128. }
  129. #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
  130. SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up);
  131. #define EXECUTE_SSL_TEST_CTX_TEST() \
  132. EXECUTE_TEST(execute_test, tear_down)
  133. static int test_empty_configuration(void)
  134. {
  135. SETUP_SSL_TEST_CTX_TEST_FIXTURE();
  136. fixture->test_section = "ssltest_default";
  137. fixture->expected_ctx->expected_result = SSL_TEST_SUCCESS;
  138. EXECUTE_SSL_TEST_CTX_TEST();
  139. return result;
  140. }
  141. static int test_good_configuration(void)
  142. {
  143. SETUP_SSL_TEST_CTX_TEST_FIXTURE();
  144. fixture->test_section = "ssltest_good";
  145. fixture->expected_ctx->method = SSL_TEST_METHOD_DTLS;
  146. fixture->expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
  147. fixture->expected_ctx->app_data_size = 1024;
  148. fixture->expected_ctx->max_fragment_size = 2048;
  149. fixture->expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
  150. fixture->expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
  151. fixture->expected_ctx->expected_server_alert = 0; /* No alert. */
  152. fixture->expected_ctx->expected_protocol = TLS1_1_VERSION;
  153. fixture->expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
  154. fixture->expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
  155. fixture->expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO;
  156. fixture->expected_ctx->session_id_expected = SSL_TEST_SESSION_ID_IGNORE;
  157. fixture->expected_ctx->resumption_expected = 1;
  158. fixture->expected_ctx->extra.client.verify_callback =
  159. SSL_TEST_VERIFY_REJECT_ALL;
  160. fixture->expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
  161. fixture->expected_ctx->extra.client.npn_protocols =
  162. OPENSSL_strdup("foo,bar");
  163. if (!TEST_ptr(fixture->expected_ctx->extra.client.npn_protocols))
  164. goto err;
  165. fixture->expected_ctx->extra.client.max_fragment_len_mode = 0;
  166. fixture->expected_ctx->extra.server.servername_callback =
  167. SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
  168. fixture->expected_ctx->extra.server.broken_session_ticket = 1;
  169. fixture->expected_ctx->resume_extra.server2.alpn_protocols =
  170. OPENSSL_strdup("baz");
  171. if (!TEST_ptr(fixture->expected_ctx->resume_extra.server2.alpn_protocols))
  172. goto err;
  173. fixture->expected_ctx->resume_extra.client.ct_validation =
  174. SSL_TEST_CT_VALIDATION_STRICT;
  175. EXECUTE_SSL_TEST_CTX_TEST();
  176. return result;
  177. err:
  178. tear_down(fixture);
  179. return 0;
  180. }
  181. static const char *bad_configurations[] = {
  182. "ssltest_unknown_option",
  183. "ssltest_wrong_section",
  184. "ssltest_unknown_expected_result",
  185. "ssltest_unknown_alert",
  186. "ssltest_unknown_protocol",
  187. "ssltest_unknown_verify_callback",
  188. "ssltest_unknown_servername",
  189. "ssltest_unknown_servername_callback",
  190. "ssltest_unknown_session_ticket_expected",
  191. "ssltest_unknown_compression_expected",
  192. "ssltest_unknown_session_id_expected",
  193. "ssltest_unknown_method",
  194. "ssltest_unknown_handshake_mode",
  195. "ssltest_unknown_resumption_expected",
  196. "ssltest_unknown_ct_validation",
  197. "ssltest_invalid_max_fragment_len",
  198. };
  199. static int test_bad_configuration(int idx)
  200. {
  201. SSL_TEST_CTX *ctx;
  202. if (!TEST_ptr_null(ctx = SSL_TEST_CTX_create(conf,
  203. bad_configurations[idx], NULL))) {
  204. SSL_TEST_CTX_free(ctx);
  205. return 0;
  206. }
  207. return 1;
  208. }
  209. OPT_TEST_DECLARE_USAGE("conf_file\n")
  210. int setup_tests(void)
  211. {
  212. if (!test_skip_common_options()) {
  213. TEST_error("Error parsing test options\n");
  214. return 0;
  215. }
  216. if (!TEST_ptr(conf = NCONF_new(NULL)))
  217. return 0;
  218. /* argument should point to test/ssl_test_ctx_test.cnf */
  219. if (!TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0))
  220. return 0;
  221. ADD_TEST(test_empty_configuration);
  222. ADD_TEST(test_good_configuration);
  223. ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
  224. return 1;
  225. }
  226. void cleanup_tests(void)
  227. {
  228. NCONF_free(conf);
  229. }