2
0

ssl_test_ctx_test.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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())) {
  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. || !testctx_eq(ctx, fixture->expected_ctx))
  117. goto err;
  118. success = 1;
  119. err:
  120. SSL_TEST_CTX_free(ctx);
  121. return success;
  122. }
  123. static void tear_down(SSL_TEST_CTX_TEST_FIXTURE *fixture)
  124. {
  125. SSL_TEST_CTX_free(fixture->expected_ctx);
  126. OPENSSL_free(fixture);
  127. }
  128. #define SETUP_SSL_TEST_CTX_TEST_FIXTURE() \
  129. SETUP_TEST_FIXTURE(SSL_TEST_CTX_TEST_FIXTURE, set_up);
  130. #define EXECUTE_SSL_TEST_CTX_TEST() \
  131. EXECUTE_TEST(execute_test, tear_down)
  132. static int test_empty_configuration(void)
  133. {
  134. SETUP_SSL_TEST_CTX_TEST_FIXTURE();
  135. if (fixture == NULL)
  136. return 0;
  137. fixture->test_section = "ssltest_default";
  138. fixture->expected_ctx->expected_result = SSL_TEST_SUCCESS;
  139. EXECUTE_SSL_TEST_CTX_TEST();
  140. return result;
  141. }
  142. static int test_good_configuration(void)
  143. {
  144. SETUP_SSL_TEST_CTX_TEST_FIXTURE();
  145. if (fixture == NULL)
  146. return 0;
  147. fixture->test_section = "ssltest_good";
  148. fixture->expected_ctx->method = SSL_TEST_METHOD_DTLS;
  149. fixture->expected_ctx->handshake_mode = SSL_TEST_HANDSHAKE_RESUME;
  150. fixture->expected_ctx->app_data_size = 1024;
  151. fixture->expected_ctx->max_fragment_size = 2048;
  152. fixture->expected_ctx->expected_result = SSL_TEST_SERVER_FAIL;
  153. fixture->expected_ctx->expected_client_alert = SSL_AD_UNKNOWN_CA;
  154. fixture->expected_ctx->expected_server_alert = 0; /* No alert. */
  155. fixture->expected_ctx->expected_protocol = TLS1_1_VERSION;
  156. fixture->expected_ctx->expected_servername = SSL_TEST_SERVERNAME_SERVER2;
  157. fixture->expected_ctx->session_ticket_expected = SSL_TEST_SESSION_TICKET_YES;
  158. fixture->expected_ctx->compression_expected = SSL_TEST_COMPRESSION_NO;
  159. fixture->expected_ctx->session_id_expected = SSL_TEST_SESSION_ID_IGNORE;
  160. fixture->expected_ctx->resumption_expected = 1;
  161. fixture->expected_ctx->extra.client.verify_callback =
  162. SSL_TEST_VERIFY_REJECT_ALL;
  163. fixture->expected_ctx->extra.client.servername = SSL_TEST_SERVERNAME_SERVER2;
  164. fixture->expected_ctx->extra.client.npn_protocols =
  165. OPENSSL_strdup("foo,bar");
  166. if (!TEST_ptr(fixture->expected_ctx->extra.client.npn_protocols))
  167. goto err;
  168. fixture->expected_ctx->extra.client.max_fragment_len_mode = 0;
  169. fixture->expected_ctx->extra.server.servername_callback =
  170. SSL_TEST_SERVERNAME_IGNORE_MISMATCH;
  171. fixture->expected_ctx->extra.server.broken_session_ticket = 1;
  172. fixture->expected_ctx->resume_extra.server2.alpn_protocols =
  173. OPENSSL_strdup("baz");
  174. if (!TEST_ptr(fixture->expected_ctx->resume_extra.server2.alpn_protocols))
  175. goto err;
  176. fixture->expected_ctx->resume_extra.client.ct_validation =
  177. SSL_TEST_CT_VALIDATION_STRICT;
  178. EXECUTE_SSL_TEST_CTX_TEST();
  179. return result;
  180. err:
  181. tear_down(fixture);
  182. return 0;
  183. }
  184. static const char *bad_configurations[] = {
  185. "ssltest_unknown_option",
  186. "ssltest_wrong_section",
  187. "ssltest_unknown_expected_result",
  188. "ssltest_unknown_alert",
  189. "ssltest_unknown_protocol",
  190. "ssltest_unknown_verify_callback",
  191. "ssltest_unknown_servername",
  192. "ssltest_unknown_servername_callback",
  193. "ssltest_unknown_session_ticket_expected",
  194. "ssltest_unknown_compression_expected",
  195. "ssltest_unknown_session_id_expected",
  196. "ssltest_unknown_method",
  197. "ssltest_unknown_handshake_mode",
  198. "ssltest_unknown_resumption_expected",
  199. "ssltest_unknown_ct_validation",
  200. "ssltest_invalid_max_fragment_len",
  201. };
  202. static int test_bad_configuration(int idx)
  203. {
  204. SSL_TEST_CTX *ctx;
  205. if (!TEST_ptr_null(ctx = SSL_TEST_CTX_create(conf,
  206. bad_configurations[idx]))) {
  207. SSL_TEST_CTX_free(ctx);
  208. return 0;
  209. }
  210. return 1;
  211. }
  212. OPT_TEST_DECLARE_USAGE("conf_file\n")
  213. int setup_tests(void)
  214. {
  215. if (!test_skip_common_options()) {
  216. TEST_error("Error parsing test options\n");
  217. return 0;
  218. }
  219. if (!TEST_ptr(conf = NCONF_new(NULL)))
  220. return 0;
  221. /* argument should point to test/ssl_test_ctx_test.cnf */
  222. if (!TEST_int_gt(NCONF_load(conf, test_get_argument(0), NULL), 0))
  223. return 0;
  224. ADD_TEST(test_empty_configuration);
  225. ADD_TEST(test_good_configuration);
  226. ADD_ALL_TESTS(test_bad_configuration, OSSL_NELEM(bad_configurations));
  227. return 1;
  228. }
  229. void cleanup_tests(void)
  230. {
  231. NCONF_free(conf);
  232. }