servername_test.c 6.7 KB

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