ssl_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/conf.h>
  12. #include <openssl/err.h>
  13. #include <openssl/ssl.h>
  14. #include "handshake_helper.h"
  15. #include "ssl_test_ctx.h"
  16. #include "testutil.h"
  17. static CONF *conf = NULL;
  18. /* Currently the section names are of the form test-<number>, e.g. test-15. */
  19. #define MAX_TESTCASE_NAME_LENGTH 100
  20. typedef struct ssl_test_ctx_test_fixture {
  21. const char *test_case_name;
  22. char test_app[MAX_TESTCASE_NAME_LENGTH];
  23. } SSL_TEST_FIXTURE;
  24. static SSL_TEST_FIXTURE set_up(const char *const test_case_name)
  25. {
  26. SSL_TEST_FIXTURE fixture;
  27. fixture.test_case_name = test_case_name;
  28. return fixture;
  29. }
  30. static const char *print_alert(int alert)
  31. {
  32. return alert ? SSL_alert_desc_string_long(alert) : "no alert";
  33. }
  34. static int check_result(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
  35. {
  36. if (result->result != test_ctx->expected_result) {
  37. fprintf(stderr, "ExpectedResult mismatch: expected %s, got %s.\n",
  38. ssl_test_result_name(test_ctx->expected_result),
  39. ssl_test_result_name(result->result));
  40. return 0;
  41. }
  42. return 1;
  43. }
  44. static int check_alerts(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
  45. {
  46. if (result->client_alert_sent != result->client_alert_received) {
  47. fprintf(stderr, "Client sent alert %s but server received %s\n.",
  48. print_alert(result->client_alert_sent),
  49. print_alert(result->client_alert_received));
  50. /*
  51. * We can't bail here because the peer doesn't always get far enough
  52. * to process a received alert. Specifically, in protocol version
  53. * negotiation tests, we have the following scenario.
  54. * Client supports TLS v1.2 only; Server supports TLS v1.1.
  55. * Client proposes TLS v1.2; server responds with 1.1;
  56. * Client now sends a protocol alert, using TLS v1.2 in the header.
  57. * The server, however, rejects the alert because of version mismatch
  58. * in the record layer; therefore, the server appears to never
  59. * receive the alert.
  60. */
  61. /* return 0; */
  62. }
  63. if (result->server_alert_sent != result->server_alert_received) {
  64. fprintf(stderr, "Server sent alert %s but client received %s\n.",
  65. print_alert(result->server_alert_sent),
  66. print_alert(result->server_alert_received));
  67. /* return 0; */
  68. }
  69. /* Tolerate an alert if one wasn't explicitly specified in the test. */
  70. if (test_ctx->expected_client_alert
  71. /*
  72. * The info callback alert value is computed as
  73. * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
  74. * where the low byte is the alert code and the high byte is other stuff.
  75. */
  76. && (result->client_alert_sent & 0xff) != test_ctx->expected_client_alert) {
  77. fprintf(stderr, "ClientAlert mismatch: expected %s, got %s.\n",
  78. print_alert(test_ctx->expected_client_alert),
  79. print_alert(result->client_alert_sent));
  80. return 0;
  81. }
  82. if (test_ctx->expected_server_alert
  83. && (result->server_alert_sent & 0xff) != test_ctx->expected_server_alert) {
  84. fprintf(stderr, "ServerAlert mismatch: expected %s, got %s.\n",
  85. print_alert(test_ctx->expected_server_alert),
  86. print_alert(result->server_alert_sent));
  87. return 0;
  88. }
  89. if (result->client_num_fatal_alerts_sent > 1) {
  90. fprintf(stderr, "Client sent %d fatal alerts.\n",
  91. result->client_num_fatal_alerts_sent);
  92. return 0;
  93. }
  94. if (result->server_num_fatal_alerts_sent > 1) {
  95. fprintf(stderr, "Server sent %d alerts.\n",
  96. result->server_num_fatal_alerts_sent);
  97. return 0;
  98. }
  99. return 1;
  100. }
  101. static int check_protocol(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
  102. {
  103. if (result->client_protocol != result->server_protocol) {
  104. fprintf(stderr, "Client has protocol %s but server has %s\n.",
  105. ssl_protocol_name(result->client_protocol),
  106. ssl_protocol_name(result->server_protocol));
  107. return 0;
  108. }
  109. if (test_ctx->expected_protocol) {
  110. if (result->client_protocol != test_ctx->expected_protocol) {
  111. fprintf(stderr, "Protocol mismatch: expected %s, got %s.\n",
  112. ssl_protocol_name(test_ctx->expected_protocol),
  113. ssl_protocol_name(result->client_protocol));
  114. return 0;
  115. }
  116. }
  117. return 1;
  118. }
  119. static int check_servername(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
  120. {
  121. if (result->servername != test_ctx->expected_servername) {
  122. fprintf(stderr, "Client ServerName mismatch, expected %s, got %s\n.",
  123. ssl_servername_name(test_ctx->expected_servername),
  124. ssl_servername_name(result->servername));
  125. return 0;
  126. }
  127. return 1;
  128. }
  129. static int check_session_ticket(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
  130. {
  131. if (test_ctx->session_ticket_expected == SSL_TEST_SESSION_TICKET_IGNORE)
  132. return 1;
  133. if (result->session_ticket != test_ctx->session_ticket_expected) {
  134. fprintf(stderr, "Client SessionTicketExpected mismatch, expected %s, got %s\n.",
  135. ssl_session_ticket_name(test_ctx->session_ticket_expected),
  136. ssl_session_ticket_name(result->session_ticket));
  137. return 0;
  138. }
  139. return 1;
  140. }
  141. #ifndef OPENSSL_NO_NEXTPROTONEG
  142. static int check_npn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
  143. {
  144. int ret = 1;
  145. ret &= strings_equal("NPN Negotiated (client vs server)",
  146. result->client_npn_negotiated,
  147. result->server_npn_negotiated);
  148. ret &= strings_equal("ExpectedNPNProtocol",
  149. test_ctx->expected_npn_protocol,
  150. result->client_npn_negotiated);
  151. return ret;
  152. }
  153. #endif
  154. static int check_alpn(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
  155. {
  156. int ret = 1;
  157. ret &= strings_equal("ALPN Negotiated (client vs server)",
  158. result->client_alpn_negotiated,
  159. result->server_alpn_negotiated);
  160. ret &= strings_equal("ExpectedALPNProtocol",
  161. test_ctx->expected_alpn_protocol,
  162. result->client_alpn_negotiated);
  163. return ret;
  164. }
  165. static int check_resumption(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
  166. {
  167. if (result->client_resumed != result->server_resumed) {
  168. fprintf(stderr, "Resumption mismatch (client vs server): %d vs %d\n",
  169. result->client_resumed, result->server_resumed);
  170. return 0;
  171. }
  172. if (result->client_resumed != test_ctx->resumption_expected) {
  173. fprintf(stderr, "ResumptionExpected mismatch: %d vs %d\n",
  174. test_ctx->resumption_expected, result->client_resumed);
  175. return 0;
  176. }
  177. return 1;
  178. }
  179. static int check_tmp_key(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
  180. {
  181. if (test_ctx->expected_tmp_key_type == 0
  182. || test_ctx->expected_tmp_key_type == result->tmp_key_type)
  183. return 1;
  184. fprintf(stderr, "Tmp key type mismatch, %s vs %s\n",
  185. OBJ_nid2ln(test_ctx->expected_tmp_key_type),
  186. OBJ_nid2ln(result->tmp_key_type));
  187. return 0;
  188. }
  189. /*
  190. * This could be further simplified by constructing an expected
  191. * HANDSHAKE_RESULT, and implementing comparison methods for
  192. * its fields.
  193. */
  194. static int check_test(HANDSHAKE_RESULT *result, SSL_TEST_CTX *test_ctx)
  195. {
  196. int ret = 1;
  197. ret &= check_result(result, test_ctx);
  198. ret &= check_alerts(result, test_ctx);
  199. if (result->result == SSL_TEST_SUCCESS) {
  200. ret &= check_protocol(result, test_ctx);
  201. ret &= check_servername(result, test_ctx);
  202. ret &= check_session_ticket(result, test_ctx);
  203. ret &= (result->session_ticket_do_not_call == 0);
  204. #ifndef OPENSSL_NO_NEXTPROTONEG
  205. ret &= check_npn(result, test_ctx);
  206. #endif
  207. ret &= check_alpn(result, test_ctx);
  208. ret &= check_resumption(result, test_ctx);
  209. ret &= check_tmp_key(result, test_ctx);
  210. }
  211. return ret;
  212. }
  213. static int execute_test(SSL_TEST_FIXTURE fixture)
  214. {
  215. int ret = 0;
  216. SSL_CTX *server_ctx = NULL, *server2_ctx = NULL, *client_ctx = NULL,
  217. *resume_server_ctx = NULL, *resume_client_ctx = NULL;
  218. SSL_TEST_CTX *test_ctx = NULL;
  219. HANDSHAKE_RESULT *result = NULL;
  220. test_ctx = SSL_TEST_CTX_create(conf, fixture.test_app);
  221. if (test_ctx == NULL)
  222. goto err;
  223. #ifndef OPENSSL_NO_DTLS
  224. if (test_ctx->method == SSL_TEST_METHOD_DTLS) {
  225. server_ctx = SSL_CTX_new(DTLS_server_method());
  226. TEST_check(SSL_CTX_set_max_proto_version(server_ctx, DTLS_MAX_VERSION));
  227. if (test_ctx->extra.server.servername_callback !=
  228. SSL_TEST_SERVERNAME_CB_NONE) {
  229. server2_ctx = SSL_CTX_new(DTLS_server_method());
  230. TEST_check(server2_ctx != NULL);
  231. }
  232. client_ctx = SSL_CTX_new(DTLS_client_method());
  233. TEST_check(SSL_CTX_set_max_proto_version(client_ctx, DTLS_MAX_VERSION));
  234. if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
  235. resume_server_ctx = SSL_CTX_new(DTLS_server_method());
  236. TEST_check(SSL_CTX_set_max_proto_version(resume_server_ctx,
  237. DTLS_MAX_VERSION));
  238. resume_client_ctx = SSL_CTX_new(DTLS_client_method());
  239. TEST_check(SSL_CTX_set_max_proto_version(resume_client_ctx,
  240. DTLS_MAX_VERSION));
  241. TEST_check(resume_server_ctx != NULL);
  242. TEST_check(resume_client_ctx != NULL);
  243. }
  244. }
  245. #endif
  246. if (test_ctx->method == SSL_TEST_METHOD_TLS) {
  247. server_ctx = SSL_CTX_new(TLS_server_method());
  248. TEST_check(SSL_CTX_set_max_proto_version(server_ctx, TLS_MAX_VERSION));
  249. /* SNI on resumption isn't supported/tested yet. */
  250. if (test_ctx->extra.server.servername_callback !=
  251. SSL_TEST_SERVERNAME_CB_NONE) {
  252. server2_ctx = SSL_CTX_new(TLS_server_method());
  253. TEST_check(server2_ctx != NULL);
  254. }
  255. client_ctx = SSL_CTX_new(TLS_client_method());
  256. TEST_check(SSL_CTX_set_max_proto_version(client_ctx, TLS_MAX_VERSION));
  257. if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME) {
  258. resume_server_ctx = SSL_CTX_new(TLS_server_method());
  259. TEST_check(SSL_CTX_set_max_proto_version(resume_server_ctx,
  260. TLS_MAX_VERSION));
  261. resume_client_ctx = SSL_CTX_new(TLS_client_method());
  262. TEST_check(SSL_CTX_set_max_proto_version(resume_client_ctx,
  263. TLS_MAX_VERSION));
  264. TEST_check(resume_server_ctx != NULL);
  265. TEST_check(resume_client_ctx != NULL);
  266. }
  267. }
  268. TEST_check(server_ctx != NULL);
  269. TEST_check(client_ctx != NULL);
  270. TEST_check(CONF_modules_load(conf, fixture.test_app, 0) > 0);
  271. if (!SSL_CTX_config(server_ctx, "server")
  272. || !SSL_CTX_config(client_ctx, "client")) {
  273. goto err;
  274. }
  275. if (server2_ctx != NULL && !SSL_CTX_config(server2_ctx, "server2"))
  276. goto err;
  277. if (resume_server_ctx != NULL
  278. && !SSL_CTX_config(resume_server_ctx, "resume-server"))
  279. goto err;
  280. if (resume_client_ctx != NULL
  281. && !SSL_CTX_config(resume_client_ctx, "resume-client"))
  282. goto err;
  283. result = do_handshake(server_ctx, server2_ctx, client_ctx,
  284. resume_server_ctx, resume_client_ctx, test_ctx);
  285. ret = check_test(result, test_ctx);
  286. err:
  287. CONF_modules_unload(0);
  288. SSL_CTX_free(server_ctx);
  289. SSL_CTX_free(server2_ctx);
  290. SSL_CTX_free(client_ctx);
  291. SSL_CTX_free(resume_server_ctx);
  292. SSL_CTX_free(resume_client_ctx);
  293. SSL_TEST_CTX_free(test_ctx);
  294. if (ret != 1)
  295. ERR_print_errors_fp(stderr);
  296. HANDSHAKE_RESULT_free(result);
  297. return ret;
  298. }
  299. static void tear_down(SSL_TEST_FIXTURE fixture)
  300. {
  301. }
  302. #define SETUP_SSL_TEST_FIXTURE() \
  303. SETUP_TEST_FIXTURE(SSL_TEST_FIXTURE, set_up)
  304. #define EXECUTE_SSL_TEST() \
  305. EXECUTE_TEST(execute_test, tear_down)
  306. static int test_handshake(int idx)
  307. {
  308. SETUP_SSL_TEST_FIXTURE();
  309. BIO_snprintf(fixture.test_app, sizeof(fixture.test_app),
  310. "test-%d", idx);
  311. EXECUTE_SSL_TEST();
  312. }
  313. int main(int argc, char **argv)
  314. {
  315. int result = 0;
  316. long num_tests;
  317. if (argc != 2)
  318. return 1;
  319. conf = NCONF_new(NULL);
  320. TEST_check(conf != NULL);
  321. /* argv[1] should point to the test conf file */
  322. TEST_check(NCONF_load(conf, argv[1], NULL) > 0);
  323. TEST_check(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
  324. ADD_ALL_TESTS(test_handshake, (int)(num_tests));
  325. result = run_tests(argv[0]);
  326. return result;
  327. }