clienthellotest.c 8.5 KB

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