2
0

servername_test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2017 BaishanCloud. All rights reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <string.h>
  11. #include <openssl/opensslconf.h>
  12. #include <openssl/bio.h>
  13. #include <openssl/crypto.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/ssl.h>
  16. #include <openssl/err.h>
  17. #include <time.h>
  18. #include "../ssl/packet_locl.h"
  19. #include "testutil.h"
  20. #include "internal/nelem.h"
  21. #define CLIENT_VERSION_LEN 2
  22. static const char *host = "dummy-host";
  23. static int get_sni_from_client_hello(BIO *bio, char **sni)
  24. {
  25. long len;
  26. unsigned char *data;
  27. PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0}, pkt4 = {0}, pkt5 = {0};
  28. unsigned int servname_type = 0, type = 0;
  29. int ret = 0;
  30. len = BIO_get_mem_data(bio, (char **)&data);
  31. if (!TEST_true(PACKET_buf_init(&pkt, data, len))
  32. /* Skip the record header */
  33. || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
  34. /* Skip the handshake message header */
  35. || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
  36. /* Skip client version and random */
  37. || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
  38. + SSL3_RANDOM_SIZE))
  39. /* Skip session id */
  40. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  41. /* Skip ciphers */
  42. || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
  43. /* Skip compression */
  44. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  45. /* Extensions len */
  46. || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
  47. goto end;
  48. /* Loop through all extensions for SNI */
  49. while (PACKET_remaining(&pkt2)) {
  50. if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
  51. || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
  52. goto end;
  53. if (type == TLSEXT_TYPE_server_name) {
  54. if (!TEST_true(PACKET_get_length_prefixed_2(&pkt3, &pkt4))
  55. || !TEST_uint_ne(PACKET_remaining(&pkt4), 0)
  56. || !TEST_true(PACKET_get_1(&pkt4, &servname_type))
  57. || !TEST_uint_eq(servname_type, TLSEXT_NAMETYPE_host_name)
  58. || !TEST_true(PACKET_get_length_prefixed_2(&pkt4, &pkt5))
  59. || !TEST_uint_le(PACKET_remaining(&pkt5), TLSEXT_MAXLEN_host_name)
  60. || !TEST_false(PACKET_contains_zero_byte(&pkt5))
  61. || !TEST_true(PACKET_strndup(&pkt5, sni)))
  62. goto end;
  63. ret = 1;
  64. goto end;
  65. }
  66. }
  67. end:
  68. return ret;
  69. }
  70. static int client_setup_sni_before_state(void)
  71. {
  72. SSL_CTX *ctx;
  73. SSL *con = NULL;
  74. BIO *rbio;
  75. BIO *wbio;
  76. char *hostname = NULL;
  77. int ret = 0;
  78. /* use TLS_method to blur 'side' */
  79. ctx = SSL_CTX_new(TLS_method());
  80. if (!TEST_ptr(ctx))
  81. goto end;
  82. con = SSL_new(ctx);
  83. if (!TEST_ptr(con))
  84. goto end;
  85. /* set SNI before 'client side' is set */
  86. SSL_set_tlsext_host_name(con, host);
  87. rbio = BIO_new(BIO_s_mem());
  88. wbio = BIO_new(BIO_s_mem());
  89. if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
  90. BIO_free(rbio);
  91. BIO_free(wbio);
  92. goto end;
  93. }
  94. SSL_set_bio(con, rbio, wbio);
  95. if (!TEST_int_le(SSL_connect(con), 0))
  96. /* This shouldn't succeed because we don't have a server! */
  97. goto end;
  98. if (!TEST_true(get_sni_from_client_hello(wbio, &hostname)))
  99. /* no SNI in client hello */
  100. goto end;
  101. if (!TEST_str_eq(hostname, host))
  102. /* incorrect SNI value */
  103. goto end;
  104. ret = 1;
  105. end:
  106. OPENSSL_free(hostname);
  107. SSL_free(con);
  108. SSL_CTX_free(ctx);
  109. return ret;
  110. }
  111. static int client_setup_sni_after_state(void)
  112. {
  113. SSL_CTX *ctx;
  114. SSL *con = NULL;
  115. BIO *rbio;
  116. BIO *wbio;
  117. char *hostname = NULL;
  118. int ret = 0;
  119. /* use TLS_method to blur 'side' */
  120. ctx = SSL_CTX_new(TLS_method());
  121. if (!TEST_ptr(ctx))
  122. goto end;
  123. con = SSL_new(ctx);
  124. if (!TEST_ptr(con))
  125. goto end;
  126. rbio = BIO_new(BIO_s_mem());
  127. wbio = BIO_new(BIO_s_mem());
  128. if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
  129. BIO_free(rbio);
  130. BIO_free(wbio);
  131. goto end;
  132. }
  133. SSL_set_bio(con, rbio, wbio);
  134. SSL_set_connect_state(con);
  135. /* set SNI after 'client side' is set */
  136. SSL_set_tlsext_host_name(con, host);
  137. if (!TEST_int_le(SSL_connect(con), 0))
  138. /* This shouldn't succeed because we don't have a server! */
  139. goto end;
  140. if (!TEST_true(get_sni_from_client_hello(wbio, &hostname)))
  141. /* no SNI in client hello */
  142. goto end;
  143. if (!TEST_str_eq(hostname, host))
  144. /* incorrect SNI value */
  145. goto end;
  146. ret = 1;
  147. end:
  148. OPENSSL_free(hostname);
  149. SSL_free(con);
  150. SSL_CTX_free(ctx);
  151. return ret;
  152. }
  153. static int server_setup_sni(void)
  154. {
  155. SSL_CTX *ctx;
  156. SSL *con = NULL;
  157. BIO *rbio;
  158. BIO *wbio;
  159. int ret = 0;
  160. /* use TLS_server_method to choose 'server-side' */
  161. ctx = SSL_CTX_new(TLS_server_method());
  162. if (!TEST_ptr(ctx))
  163. goto end;
  164. con = SSL_new(ctx);
  165. if (!TEST_ptr(con))
  166. goto end;
  167. rbio = BIO_new(BIO_s_mem());
  168. wbio = BIO_new(BIO_s_mem());
  169. if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
  170. BIO_free(rbio);
  171. BIO_free(wbio);
  172. goto end;
  173. }
  174. SSL_set_bio(con, rbio, wbio);
  175. /* set SNI at server side */
  176. SSL_set_tlsext_host_name(con, host);
  177. if (!TEST_int_le(SSL_accept(con), 0))
  178. /* This shouldn't succeed because we have nothing to listen on */
  179. goto end;
  180. if (!TEST_ptr_null(SSL_get_servername(con, TLSEXT_NAMETYPE_host_name)))
  181. /* SNI should be cleared by SSL_accpet */
  182. goto end;
  183. ret = 1;
  184. end:
  185. SSL_free(con);
  186. SSL_CTX_free(ctx);
  187. return ret;
  188. }
  189. typedef int (*sni_test_fn)(void);
  190. static sni_test_fn sni_test_fns[3] = {
  191. client_setup_sni_before_state,
  192. client_setup_sni_after_state,
  193. server_setup_sni
  194. };
  195. static int test_servername(int test)
  196. {
  197. /*
  198. * For each test set up an SSL_CTX and SSL and see
  199. * what SNI behaves.
  200. */
  201. return sni_test_fns[test]();
  202. }
  203. int setup_tests(void)
  204. {
  205. ADD_ALL_TESTS(test_servername, OSSL_NELEM(sni_test_fns));
  206. return 1;
  207. }