2
0

clienthellotest.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright 2015-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. #include <string.h>
  10. #include <openssl/opensslconf.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/crypto.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/ssl.h>
  15. #include <openssl/err.h>
  16. #include <time.h>
  17. #include "internal/packet.h"
  18. #include "testutil.h"
  19. #define CLIENT_VERSION_LEN 2
  20. #define TOTAL_NUM_TESTS 4
  21. /*
  22. * Test that explicitly setting ticket data results in it appearing in the
  23. * ClientHello for a negotiated SSL/TLS version
  24. */
  25. #define TEST_SET_SESSION_TICK_DATA_VER_NEG 0
  26. /* Enable padding and make sure ClientHello is long enough to require it */
  27. #define TEST_ADD_PADDING 1
  28. /* Enable padding and make sure ClientHello is short enough to not need it */
  29. #define TEST_PADDING_NOT_NEEDED 2
  30. /*
  31. * Enable padding and add a PSK to the ClientHello (this will also ensure the
  32. * ClientHello is long enough to need padding)
  33. */
  34. #define TEST_ADD_PADDING_AND_PSK 3
  35. #define F5_WORKAROUND_MIN_MSG_LEN 0x7f
  36. #define F5_WORKAROUND_MAX_MSG_LEN 0x200
  37. static const char *sessionfile = NULL;
  38. /* Dummy ALPN protocols used to pad out the size of the ClientHello */
  39. static const char alpn_prots[] =
  40. "0123456789012345678901234567890123456789012345678901234567890123456789"
  41. "0123456789012345678901234567890123456789012345678901234567890123456789"
  42. "01234567890123456789";
  43. static int test_client_hello(int currtest)
  44. {
  45. SSL_CTX *ctx;
  46. SSL *con = NULL;
  47. BIO *rbio;
  48. BIO *wbio;
  49. long len;
  50. unsigned char *data;
  51. PACKET pkt, pkt2, pkt3;
  52. char *dummytick = "Hello World!";
  53. unsigned int type = 0;
  54. int testresult = 0;
  55. size_t msglen;
  56. BIO *sessbio = NULL;
  57. SSL_SESSION *sess = NULL;
  58. #ifdef OPENSSL_NO_TLS1_3
  59. if (currtest == TEST_ADD_PADDING_AND_PSK)
  60. return 1;
  61. #endif
  62. memset(&pkt, 0, sizeof(pkt));
  63. memset(&pkt2, 0, sizeof(pkt2));
  64. memset(&pkt3, 0, sizeof(pkt3));
  65. /*
  66. * For each test set up an SSL_CTX and SSL and see what ClientHello gets
  67. * produced when we try to connect
  68. */
  69. ctx = SSL_CTX_new(TLS_method());
  70. if (!TEST_ptr(ctx))
  71. goto end;
  72. if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, 0)))
  73. goto end;
  74. switch(currtest) {
  75. case TEST_SET_SESSION_TICK_DATA_VER_NEG:
  76. #if !defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_TLS1_2)
  77. /* TLSv1.3 is enabled and TLSv1.2 is disabled so can't do this test */
  78. SSL_CTX_free(ctx);
  79. return 1;
  80. #else
  81. /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
  82. if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)))
  83. goto end;
  84. #endif
  85. break;
  86. case TEST_ADD_PADDING_AND_PSK:
  87. /*
  88. * In this case we're doing TLSv1.3 and we're sending a PSK so the
  89. * ClientHello is already going to be quite long. To avoid getting one
  90. * that is too long for this test we use a restricted ciphersuite list
  91. */
  92. if (!TEST_false(SSL_CTX_set_cipher_list(ctx, "")))
  93. goto end;
  94. ERR_clear_error();
  95. /* Fall through */
  96. case TEST_ADD_PADDING:
  97. case TEST_PADDING_NOT_NEEDED:
  98. SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
  99. /* Make sure we get a consistent size across TLS versions */
  100. SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
  101. /*
  102. * Add some dummy ALPN protocols so that the ClientHello is at least
  103. * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
  104. * needed.
  105. */
  106. if (currtest == TEST_ADD_PADDING) {
  107. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx,
  108. (unsigned char *)alpn_prots,
  109. sizeof(alpn_prots) - 1)))
  110. goto end;
  111. /*
  112. * Otherwise we need to make sure we have a small enough message to
  113. * not need padding.
  114. */
  115. } else if (!TEST_true(SSL_CTX_set_cipher_list(ctx,
  116. "AES128-SHA"))
  117. || !TEST_true(SSL_CTX_set_ciphersuites(ctx,
  118. "TLS_AES_128_GCM_SHA256"))) {
  119. goto end;
  120. }
  121. break;
  122. default:
  123. goto end;
  124. }
  125. con = SSL_new(ctx);
  126. if (!TEST_ptr(con))
  127. goto end;
  128. if (currtest == TEST_ADD_PADDING_AND_PSK) {
  129. sessbio = BIO_new_file(sessionfile, "r");
  130. if (!TEST_ptr(sessbio)) {
  131. TEST_info("Unable to open session.pem");
  132. goto end;
  133. }
  134. sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
  135. if (!TEST_ptr(sess)) {
  136. TEST_info("Unable to load SSL_SESSION");
  137. goto end;
  138. }
  139. /*
  140. * We reset the creation time so that we don't discard the session as
  141. * too old.
  142. */
  143. if (!TEST_true(SSL_SESSION_set_time(sess, (long)time(NULL)))
  144. || !TEST_true(SSL_set_session(con, sess)))
  145. goto end;
  146. }
  147. rbio = BIO_new(BIO_s_mem());
  148. wbio = BIO_new(BIO_s_mem());
  149. if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
  150. BIO_free(rbio);
  151. BIO_free(wbio);
  152. goto end;
  153. }
  154. SSL_set_bio(con, rbio, wbio);
  155. SSL_set_connect_state(con);
  156. if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
  157. if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick,
  158. strlen(dummytick))))
  159. goto end;
  160. }
  161. if (!TEST_int_le(SSL_connect(con), 0)) {
  162. /* This shouldn't succeed because we don't have a server! */
  163. goto end;
  164. }
  165. len = BIO_get_mem_data(wbio, (char **)&data);
  166. if (!TEST_true(PACKET_buf_init(&pkt, data, len))
  167. /* Skip the record header */
  168. || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
  169. goto end;
  170. msglen = PACKET_remaining(&pkt);
  171. /* Skip the handshake message header */
  172. if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
  173. /* Skip client version and random */
  174. || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
  175. + SSL3_RANDOM_SIZE))
  176. /* Skip session id */
  177. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  178. /* Skip ciphers */
  179. || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
  180. /* Skip compression */
  181. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  182. /* Extensions len */
  183. || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
  184. goto end;
  185. /* Loop through all extensions */
  186. while (PACKET_remaining(&pkt2)) {
  187. if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
  188. || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
  189. goto end;
  190. if (type == TLSEXT_TYPE_session_ticket) {
  191. if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
  192. if (TEST_true(PACKET_equal(&pkt3, dummytick,
  193. strlen(dummytick)))) {
  194. /* Ticket data is as we expected */
  195. testresult = 1;
  196. }
  197. goto end;
  198. }
  199. }
  200. if (type == TLSEXT_TYPE_padding) {
  201. if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED))
  202. goto end;
  203. else if (TEST_true(currtest == TEST_ADD_PADDING
  204. || currtest == TEST_ADD_PADDING_AND_PSK))
  205. testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN);
  206. }
  207. }
  208. if (currtest == TEST_PADDING_NOT_NEEDED)
  209. testresult = 1;
  210. end:
  211. SSL_free(con);
  212. SSL_CTX_free(ctx);
  213. SSL_SESSION_free(sess);
  214. BIO_free(sessbio);
  215. return testresult;
  216. }
  217. OPT_TEST_DECLARE_USAGE("sessionfile\n")
  218. int setup_tests(void)
  219. {
  220. if (!test_skip_common_options()) {
  221. TEST_error("Error parsing test options\n");
  222. return 0;
  223. }
  224. if (!TEST_ptr(sessionfile = test_get_argument(0)))
  225. return 0;
  226. ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
  227. return 1;
  228. }