servername_test.c 7.4 KB

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