clienthellotest.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Written by Matt Caswell for the OpenSSL Project */
  2. /* ====================================================================
  3. * Copyright (c) 1998-2015 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. #include <string.h>
  56. #include <openssl/bio.h>
  57. #include <openssl/crypto.h>
  58. #include <openssl/evp.h>
  59. #include <openssl/ssl.h>
  60. #include <openssl/err.h>
  61. #define CLIENT_VERSION_LEN 2
  62. #define SESSION_ID_LEN_LEN 1
  63. #define CIPHERS_LEN_LEN 2
  64. #define COMPRESSION_LEN_LEN 1
  65. #define EXTENSIONS_LEN_LEN 2
  66. #define EXTENSION_TYPE_LEN 2
  67. #define EXTENSION_SIZE_LEN 2
  68. #define TOTAL_NUM_TESTS 2
  69. /*
  70. * Test that explicitly setting ticket data results in it appearing in the
  71. * ClientHello for TLS1.2
  72. */
  73. #define TEST_SET_SESSION_TICK_DATA_TLS_1_2 0
  74. /*
  75. * Test that explicitly setting ticket data results in it appearing in the
  76. * ClientHello for a negotiated SSL/TLS version
  77. */
  78. #define TEST_SET_SESSION_TICK_DATA_VER_NEG 1
  79. int main(int argc, char *argv[])
  80. {
  81. SSL_CTX *ctx;
  82. SSL *con;
  83. BIO *rbio;
  84. BIO *wbio;
  85. BIO *err;
  86. long len;
  87. unsigned char *data;
  88. unsigned char *dataend;
  89. char *dummytick = "Hello World!";
  90. unsigned int tmplen;
  91. unsigned int type;
  92. unsigned int size;
  93. int testresult = 0;
  94. int currtest = 0;
  95. SSL_library_init();
  96. SSL_load_error_strings();
  97. err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  98. CRYPTO_malloc_debug_init();
  99. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  100. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  101. /*
  102. * For each test set up an SSL_CTX and SSL and see what ClientHello gets
  103. * produced when we try to connect
  104. */
  105. for (; currtest < TOTAL_NUM_TESTS; currtest++) {
  106. testresult = 0;
  107. if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2) {
  108. ctx = SSL_CTX_new(TLSv1_2_method());
  109. } else {
  110. ctx = SSL_CTX_new(SSLv23_method());
  111. }
  112. con = SSL_new(ctx);
  113. rbio = BIO_new(BIO_s_mem());
  114. wbio = BIO_new(BIO_s_mem());
  115. SSL_set_bio(con, rbio, wbio);
  116. SSL_set_connect_state(con);
  117. if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2
  118. || currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
  119. if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
  120. goto end;
  121. }
  122. if (SSL_connect(con) > 0) {
  123. /* This shouldn't succeed because we don't have a server! */
  124. goto end;
  125. }
  126. len = BIO_get_mem_data(wbio, (char **)&data);
  127. dataend = data + len;
  128. /* Skip the record header */
  129. data += SSL3_RT_HEADER_LENGTH;
  130. /* Skip the handshake message header */
  131. data += SSL3_HM_HEADER_LENGTH;
  132. /* Skip client version and random */
  133. data += CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE;
  134. if (data + SESSION_ID_LEN_LEN > dataend)
  135. goto end;
  136. /* Skip session id */
  137. tmplen = *data;
  138. data += SESSION_ID_LEN_LEN + tmplen;
  139. if (data + CIPHERS_LEN_LEN > dataend)
  140. goto end;
  141. /* Skip ciphers */
  142. tmplen = ((*data) << 8) | *(data + 1);
  143. data += CIPHERS_LEN_LEN + tmplen;
  144. if (data + COMPRESSION_LEN_LEN > dataend)
  145. goto end;
  146. /* Skip compression */
  147. tmplen = *data;
  148. data += COMPRESSION_LEN_LEN + tmplen;
  149. if (data + EXTENSIONS_LEN_LEN > dataend)
  150. goto end;
  151. /* Extensions len */
  152. tmplen = ((*data) << 8) | *(data + 1);
  153. data += EXTENSIONS_LEN_LEN;
  154. if (data + tmplen > dataend)
  155. goto end;
  156. /* Loop through all extensions */
  157. while (tmplen > EXTENSION_TYPE_LEN + EXTENSION_SIZE_LEN) {
  158. type = ((*data) << 8) | *(data + 1);
  159. data += EXTENSION_TYPE_LEN;
  160. size = ((*data) << 8) | *(data + 1);
  161. data += EXTENSION_SIZE_LEN;
  162. if (data + size > dataend)
  163. goto end;
  164. if (type == TLSEXT_TYPE_session_ticket) {
  165. if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2
  166. || currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
  167. if (size == strlen(dummytick)
  168. && memcmp(data, dummytick, size) == 0) {
  169. /* Ticket data is as we expected */
  170. testresult = 1;
  171. } else {
  172. printf("Received session ticket is not as expected\n");
  173. }
  174. break;
  175. }
  176. }
  177. tmplen -= EXTENSION_TYPE_LEN + EXTENSION_SIZE_LEN + size;
  178. data += size;
  179. }
  180. end:
  181. SSL_free(con);
  182. SSL_CTX_free(ctx);
  183. if (!testresult) {
  184. printf("ClientHello test: FAILED (Test %d)\n", currtest);
  185. break;
  186. }
  187. }
  188. ERR_free_strings();
  189. ERR_remove_thread_state(NULL);
  190. EVP_cleanup();
  191. CRYPTO_cleanup_all_ex_data();
  192. CRYPTO_mem_leaks(err);
  193. BIO_free(err);
  194. return testresult?0:1;
  195. }