2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2017 BaishanCloud. All rights reserved.
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
13 #include <openssl/opensslconf.h>
14 #include <openssl/bio.h>
15 #include <openssl/crypto.h>
16 #include <openssl/evp.h>
17 #include <openssl/ssl.h>
18 #include <openssl/err.h>
21 #include "internal/packet.h"
24 #include "internal/nelem.h"
25 #include "helpers/ssltestlib.h"
27 #define CLIENT_VERSION_LEN 2
29 static const char *host = "dummy-host";
31 static char *cert = NULL;
32 static char *privkey = NULL;
34 #if defined(OPENSSL_NO_TLS1_3) || \
35 (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH))
36 static int maxversion = TLS1_2_VERSION;
38 static int maxversion = 0;
41 static int get_sni_from_client_hello(BIO *bio, char **sni)
45 PACKET pkt, pkt2, pkt3, pkt4, pkt5;
46 unsigned int servname_type = 0, type = 0;
49 memset(&pkt, 0, sizeof(pkt));
50 memset(&pkt2, 0, sizeof(pkt2));
51 memset(&pkt3, 0, sizeof(pkt3));
52 memset(&pkt4, 0, sizeof(pkt4));
53 memset(&pkt5, 0, sizeof(pkt5));
55 if (!TEST_long_ge(len = BIO_get_mem_data(bio, (char **)&data), 0)
56 || !TEST_true(PACKET_buf_init(&pkt, data, len))
57 /* Skip the record header */
58 || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
59 /* Skip the handshake message header */
60 || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
61 /* Skip client version and random */
62 || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
65 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
67 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
68 /* Skip compression */
69 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
71 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
74 /* Loop through all extensions for SNI */
75 while (PACKET_remaining(&pkt2)) {
76 if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
77 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
79 if (type == TLSEXT_TYPE_server_name) {
80 if (!TEST_true(PACKET_get_length_prefixed_2(&pkt3, &pkt4))
81 || !TEST_uint_ne(PACKET_remaining(&pkt4), 0)
82 || !TEST_true(PACKET_get_1(&pkt4, &servname_type))
83 || !TEST_uint_eq(servname_type, TLSEXT_NAMETYPE_host_name)
84 || !TEST_true(PACKET_get_length_prefixed_2(&pkt4, &pkt5))
85 || !TEST_uint_le(PACKET_remaining(&pkt5), TLSEXT_MAXLEN_host_name)
86 || !TEST_false(PACKET_contains_zero_byte(&pkt5))
87 || !TEST_true(PACKET_strndup(&pkt5, sni)))
97 static int client_setup_sni_before_state(void)
103 char *hostname = NULL;
106 /* use TLS_method to blur 'side' */
107 ctx = SSL_CTX_new(TLS_method());
112 && !TEST_true(SSL_CTX_set_max_proto_version(ctx, maxversion)))
119 /* set SNI before 'client side' is set */
120 SSL_set_tlsext_host_name(con, host);
122 rbio = BIO_new(BIO_s_mem());
123 wbio = BIO_new(BIO_s_mem());
124 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
130 SSL_set_bio(con, rbio, wbio);
132 if (!TEST_int_le(SSL_connect(con), 0))
133 /* This shouldn't succeed because we don't have a server! */
135 if (!TEST_true(get_sni_from_client_hello(wbio, &hostname)))
136 /* no SNI in client hello */
138 if (!TEST_str_eq(hostname, host))
139 /* incorrect SNI value */
143 OPENSSL_free(hostname);
149 static int client_setup_sni_after_state(void)
155 char *hostname = NULL;
158 /* use TLS_method to blur 'side' */
159 ctx = SSL_CTX_new(TLS_method());
164 && !TEST_true(SSL_CTX_set_max_proto_version(ctx, maxversion)))
171 rbio = BIO_new(BIO_s_mem());
172 wbio = BIO_new(BIO_s_mem());
173 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
179 SSL_set_bio(con, rbio, wbio);
180 SSL_set_connect_state(con);
182 /* set SNI after 'client side' is set */
183 SSL_set_tlsext_host_name(con, host);
185 if (!TEST_int_le(SSL_connect(con), 0))
186 /* This shouldn't succeed because we don't have a server! */
188 if (!TEST_true(get_sni_from_client_hello(wbio, &hostname)))
189 /* no SNI in client hello */
191 if (!TEST_str_eq(hostname, host))
192 /* incorrect SNI value */
196 OPENSSL_free(hostname);
202 static int server_setup_sni(void)
204 SSL_CTX *cctx = NULL, *sctx = NULL;
205 SSL *clientssl = NULL, *serverssl = NULL;
208 if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_server_method(),
211 &sctx, &cctx, cert, privkey))
212 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
216 /* set SNI at server side */
217 SSL_set_tlsext_host_name(serverssl, host);
219 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
222 if (!TEST_ptr_null(SSL_get_servername(serverssl,
223 TLSEXT_NAMETYPE_host_name))) {
224 /* SNI should have been cleared during handshake */
238 typedef int (*sni_test_fn)(void);
240 static sni_test_fn sni_test_fns[3] = {
241 client_setup_sni_before_state,
242 client_setup_sni_after_state,
246 static int test_servername(int test)
249 * For each test set up an SSL_CTX and SSL and see
252 return sni_test_fns[test]();
255 int setup_tests(void)
257 if (!test_skip_common_options()) {
258 TEST_error("Error parsing test options\n");
262 if (!TEST_ptr(cert = test_get_argument(0))
263 || !TEST_ptr(privkey = test_get_argument(1)))
266 ADD_ALL_TESTS(test_servername, OSSL_NELEM(sni_test_fns));