2
0

servername_test.c 6.9 KB

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