ssl_test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2016 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 <openssl/conf.h>
  11. #include <openssl/err.h>
  12. #include <openssl/ssl.h>
  13. #include "handshake_helper.h"
  14. #include "ssl_test_ctx.h"
  15. #include "testutil.h"
  16. static CONF *conf = NULL;
  17. /* Currently the section names are of the form test-<number>, e.g. test-15. */
  18. #define MAX_TESTCASE_NAME_LENGTH 100
  19. typedef struct ssl_test_ctx_test_fixture {
  20. const char *test_case_name;
  21. char test_app[MAX_TESTCASE_NAME_LENGTH];
  22. } SSL_TEST_FIXTURE;
  23. static SSL_TEST_FIXTURE set_up(const char *const test_case_name)
  24. {
  25. SSL_TEST_FIXTURE fixture;
  26. fixture.test_case_name = test_case_name;
  27. return fixture;
  28. }
  29. static const char *print_alert(int alert)
  30. {
  31. return alert ? SSL_alert_desc_string_long(alert) : "no alert";
  32. }
  33. static int check_result(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
  34. {
  35. if (result.result != test_ctx->expected_result) {
  36. fprintf(stderr, "ExpectedResult mismatch: expected %s, got %s.\n",
  37. ssl_test_result_name(test_ctx->expected_result),
  38. ssl_test_result_name(result.result));
  39. return 0;
  40. }
  41. return 1;
  42. }
  43. static int check_alerts(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
  44. {
  45. if (result.client_alert_sent != result.client_alert_received) {
  46. fprintf(stderr, "Client sent alert %s but server received %s\n.",
  47. print_alert(result.client_alert_sent),
  48. print_alert(result.client_alert_received));
  49. /*
  50. * We can't bail here because the peer doesn't always get far enough
  51. * to process a received alert. Specifically, in protocol version
  52. * negotiation tests, we have the following scenario.
  53. * Client supports TLS v1.2 only; Server supports TLS v1.1.
  54. * Client proposes TLS v1.2; server responds with 1.1;
  55. * Client now sends a protocol alert, using TLS v1.2 in the header.
  56. * The server, however, rejects the alert because of version mismatch
  57. * in the record layer; therefore, the server appears to never
  58. * receive the alert.
  59. */
  60. /* return 0; */
  61. }
  62. if (result.server_alert_sent != result.server_alert_received) {
  63. fprintf(stderr, "Server sent alert %s but client received %s\n.",
  64. print_alert(result.server_alert_sent),
  65. print_alert(result.server_alert_received));
  66. /* return 0; */
  67. }
  68. /* Tolerate an alert if one wasn't explicitly specified in the test. */
  69. if (test_ctx->client_alert
  70. /*
  71. * The info callback alert value is computed as
  72. * (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]
  73. * where the low byte is the alert code and the high byte is other stuff.
  74. */
  75. && (result.client_alert_sent & 0xff) != test_ctx->client_alert) {
  76. fprintf(stderr, "ClientAlert mismatch: expected %s, got %s.\n",
  77. print_alert(test_ctx->client_alert),
  78. print_alert(result.client_alert_sent));
  79. return 0;
  80. }
  81. if (test_ctx->server_alert
  82. && (result.server_alert_sent & 0xff) != test_ctx->server_alert) {
  83. fprintf(stderr, "ServerAlert mismatch: expected %s, got %s.\n",
  84. print_alert(test_ctx->server_alert),
  85. print_alert(result.server_alert_sent));
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. static int check_protocol(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
  91. {
  92. if (result.client_protocol != result.server_protocol) {
  93. fprintf(stderr, "Client has protocol %s but server has %s\n.",
  94. ssl_protocol_name(result.client_protocol),
  95. ssl_protocol_name(result.server_protocol));
  96. return 0;
  97. }
  98. if (test_ctx->protocol) {
  99. if (result.client_protocol != test_ctx->protocol) {
  100. fprintf(stderr, "Protocol mismatch: expected %s, got %s.\n",
  101. ssl_protocol_name(test_ctx->protocol),
  102. ssl_protocol_name(result.client_protocol));
  103. return 0;
  104. }
  105. }
  106. return 1;
  107. }
  108. /*
  109. * This could be further simplified by constructing an expected
  110. * HANDSHAKE_RESULT, and implementing comparison methods for
  111. * its fields.
  112. */
  113. static int check_test(HANDSHAKE_RESULT result, SSL_TEST_CTX *test_ctx)
  114. {
  115. int ret = 1;
  116. ret &= check_result(result, test_ctx);
  117. ret &= check_alerts(result, test_ctx);
  118. if (result.result == SSL_TEST_SUCCESS)
  119. ret &= check_protocol(result, test_ctx);
  120. return ret;
  121. }
  122. static int execute_test(SSL_TEST_FIXTURE fixture)
  123. {
  124. int ret = 0;
  125. SSL_CTX *server_ctx = NULL, *client_ctx = NULL;
  126. SSL_TEST_CTX *test_ctx = NULL;
  127. HANDSHAKE_RESULT result;
  128. server_ctx = SSL_CTX_new(TLS_server_method());
  129. client_ctx = SSL_CTX_new(TLS_client_method());
  130. OPENSSL_assert(server_ctx != NULL && client_ctx != NULL);
  131. OPENSSL_assert(CONF_modules_load(conf, fixture.test_app, 0) > 0);
  132. if (!SSL_CTX_config(server_ctx, "server")
  133. || !SSL_CTX_config(client_ctx, "client")) {
  134. goto err;
  135. }
  136. test_ctx = SSL_TEST_CTX_create(conf, fixture.test_app);
  137. if (test_ctx == NULL)
  138. goto err;
  139. result = do_handshake(server_ctx, client_ctx, test_ctx);
  140. ret = check_test(result, test_ctx);
  141. err:
  142. CONF_modules_unload(0);
  143. SSL_CTX_free(server_ctx);
  144. SSL_CTX_free(client_ctx);
  145. SSL_TEST_CTX_free(test_ctx);
  146. if (ret != 1)
  147. ERR_print_errors_fp(stderr);
  148. return ret;
  149. }
  150. static void tear_down(SSL_TEST_FIXTURE fixture)
  151. {
  152. }
  153. #define SETUP_SSL_TEST_FIXTURE() \
  154. SETUP_TEST_FIXTURE(SSL_TEST_FIXTURE, set_up)
  155. #define EXECUTE_SSL_TEST() \
  156. EXECUTE_TEST(execute_test, tear_down)
  157. static int test_handshake(int idx)
  158. {
  159. SETUP_SSL_TEST_FIXTURE();
  160. BIO_snprintf(fixture.test_app, sizeof(fixture.test_app),
  161. "test-%d", idx);
  162. EXECUTE_SSL_TEST();
  163. }
  164. int main(int argc, char **argv)
  165. {
  166. int result = 0;
  167. long num_tests;
  168. if (argc != 2)
  169. return 1;
  170. conf = NCONF_new(NULL);
  171. OPENSSL_assert(conf != NULL);
  172. /* argv[1] should point to the test conf file */
  173. OPENSSL_assert(NCONF_load(conf, argv[1], NULL) > 0);
  174. OPENSSL_assert(NCONF_get_number_e(conf, NULL, "num_tests", &num_tests));
  175. ADD_ALL_TESTS(test_handshake, (int)(num_tests));
  176. result = run_tests(argv[0]);
  177. return result;
  178. }