clienthellotest.c 7.3 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/opensslconf.h>
  57. #include <openssl/bio.h>
  58. #include <openssl/crypto.h>
  59. #include <openssl/evp.h>
  60. #include <openssl/ssl.h>
  61. #include <openssl/err.h>
  62. #define CLIENT_VERSION_LEN 2
  63. #define SESSION_ID_LEN_LEN 1
  64. #define CIPHERS_LEN_LEN 2
  65. #define COMPRESSION_LEN_LEN 1
  66. #define EXTENSIONS_LEN_LEN 2
  67. #define EXTENSION_TYPE_LEN 2
  68. #define EXTENSION_SIZE_LEN 2
  69. #define TOTAL_NUM_TESTS 2
  70. /*
  71. * Test that explicitly setting ticket data results in it appearing in the
  72. * ClientHello for TLS1.2
  73. */
  74. #define TEST_SET_SESSION_TICK_DATA_TLS_1_2 0
  75. /*
  76. * Test that explicitly setting ticket data results in it appearing in the
  77. * ClientHello for a negotiated SSL/TLS version
  78. */
  79. #define TEST_SET_SESSION_TICK_DATA_VER_NEG 1
  80. int main(int argc, char *argv[])
  81. {
  82. SSL_CTX *ctx;
  83. SSL *con;
  84. BIO *rbio;
  85. BIO *wbio;
  86. BIO *err;
  87. long len;
  88. unsigned char *data;
  89. unsigned char *dataend;
  90. char *dummytick = "Hello World!";
  91. unsigned int tmplen;
  92. unsigned int type;
  93. unsigned int size;
  94. int testresult = 0;
  95. int currtest = 0;
  96. err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  97. CRYPTO_set_mem_debug(1);
  98. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  99. /*
  100. * For each test set up an SSL_CTX and SSL and see what ClientHello gets
  101. * produced when we try to connect
  102. */
  103. for (; currtest < TOTAL_NUM_TESTS; currtest++) {
  104. testresult = 0;
  105. if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2) {
  106. #ifndef OPENSSL_NO_TLS1_2
  107. ctx = SSL_CTX_new(TLSv1_2_method());
  108. #else
  109. continue;
  110. #endif
  111. } else {
  112. ctx = SSL_CTX_new(TLS_method());
  113. }
  114. con = SSL_new(ctx);
  115. rbio = BIO_new(BIO_s_mem());
  116. wbio = BIO_new(BIO_s_mem());
  117. SSL_set_bio(con, rbio, wbio);
  118. SSL_set_connect_state(con);
  119. if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2
  120. || currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
  121. if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
  122. goto end;
  123. }
  124. if (SSL_connect(con) > 0) {
  125. /* This shouldn't succeed because we don't have a server! */
  126. goto end;
  127. }
  128. len = BIO_get_mem_data(wbio, (char **)&data);
  129. dataend = data + len;
  130. /* Skip the record header */
  131. data += SSL3_RT_HEADER_LENGTH;
  132. /* Skip the handshake message header */
  133. data += SSL3_HM_HEADER_LENGTH;
  134. /* Skip client version and random */
  135. data += CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE;
  136. if (data + SESSION_ID_LEN_LEN > dataend)
  137. goto end;
  138. /* Skip session id */
  139. tmplen = *data;
  140. data += SESSION_ID_LEN_LEN + tmplen;
  141. if (data + CIPHERS_LEN_LEN > dataend)
  142. goto end;
  143. /* Skip ciphers */
  144. tmplen = ((*data) << 8) | *(data + 1);
  145. data += CIPHERS_LEN_LEN + tmplen;
  146. if (data + COMPRESSION_LEN_LEN > dataend)
  147. goto end;
  148. /* Skip compression */
  149. tmplen = *data;
  150. data += COMPRESSION_LEN_LEN + tmplen;
  151. if (data + EXTENSIONS_LEN_LEN > dataend)
  152. goto end;
  153. /* Extensions len */
  154. tmplen = ((*data) << 8) | *(data + 1);
  155. data += EXTENSIONS_LEN_LEN;
  156. if (data + tmplen > dataend)
  157. goto end;
  158. /* Loop through all extensions */
  159. while (tmplen > EXTENSION_TYPE_LEN + EXTENSION_SIZE_LEN) {
  160. type = ((*data) << 8) | *(data + 1);
  161. data += EXTENSION_TYPE_LEN;
  162. size = ((*data) << 8) | *(data + 1);
  163. data += EXTENSION_SIZE_LEN;
  164. if (data + size > dataend)
  165. goto end;
  166. if (type == TLSEXT_TYPE_session_ticket) {
  167. if (currtest == TEST_SET_SESSION_TICK_DATA_TLS_1_2
  168. || currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
  169. if (size == strlen(dummytick)
  170. && memcmp(data, dummytick, size) == 0) {
  171. /* Ticket data is as we expected */
  172. testresult = 1;
  173. } else {
  174. printf("Received session ticket is not as expected\n");
  175. }
  176. break;
  177. }
  178. }
  179. tmplen -= EXTENSION_TYPE_LEN + EXTENSION_SIZE_LEN + size;
  180. data += size;
  181. }
  182. end:
  183. SSL_free(con);
  184. SSL_CTX_free(ctx);
  185. if (!testresult) {
  186. printf("ClientHello test: FAILED (Test %d)\n", currtest);
  187. break;
  188. }
  189. }
  190. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  191. if (CRYPTO_mem_leaks(err) <= 0)
  192. testresult = 0;
  193. #endif
  194. BIO_free(err);
  195. return testresult?0:1;
  196. }