2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2017 BaishanCloud. All rights reserved.
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
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 "../ssl/packet_locl.h"
26 #define CLIENT_VERSION_LEN 2
28 static const char *host
= "dummy-host";
30 static int get_sni_from_client_hello(BIO
*bio
, char **sni
)
34 PACKET pkt
= {0}, pkt2
= {0}, pkt3
= {0}, pkt4
= {0}, pkt5
= {0};
35 unsigned int servname_type
= 0, type
= 0;
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
48 || !TEST_true(PACKET_get_length_prefixed_1(&pkt
, &pkt2
))
50 || !TEST_true(PACKET_get_length_prefixed_2(&pkt
, &pkt2
))
51 /* Skip compression */
52 || !TEST_true(PACKET_get_length_prefixed_1(&pkt
, &pkt2
))
54 || !TEST_true(PACKET_as_length_prefixed_2(&pkt
, &pkt2
)))
57 /* Loop through all extensions for SNI */
58 while (PACKET_remaining(&pkt2
)) {
59 if (!TEST_true(PACKET_get_net_2(&pkt2
, &type
))
60 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2
, &pkt3
)))
62 if (type
== TLSEXT_TYPE_server_name
) {
63 if (!TEST_true(PACKET_get_length_prefixed_2(&pkt3
, &pkt4
))
64 || !TEST_uint_ne(PACKET_remaining(&pkt4
), 0)
65 || !TEST_true(PACKET_get_1(&pkt4
, &servname_type
))
66 || !TEST_uint_eq(servname_type
, TLSEXT_NAMETYPE_host_name
)
67 || !TEST_true(PACKET_get_length_prefixed_2(&pkt4
, &pkt5
))
68 || !TEST_uint_le(PACKET_remaining(&pkt5
), TLSEXT_MAXLEN_host_name
)
69 || !TEST_false(PACKET_contains_zero_byte(&pkt5
))
70 || !TEST_true(PACKET_strndup(&pkt5
, sni
)))
80 static int client_setup_sni_before_state()
86 char *hostname
= NULL
;
89 /* use TLS_method to blur 'side' */
90 ctx
= SSL_CTX_new(TLS_method());
98 /* set SNI before 'client side' is set */
99 SSL_set_tlsext_host_name(con
, host
);
101 rbio
= BIO_new(BIO_s_mem());
102 wbio
= BIO_new(BIO_s_mem());
103 if (!TEST_ptr(rbio
)|| !TEST_ptr(wbio
)) {
109 SSL_set_bio(con
, rbio
, wbio
);
111 if (!TEST_int_le(SSL_connect(con
), 0))
112 /* This shouldn't succeed because we don't have a server! */
114 if (!TEST_true(get_sni_from_client_hello(wbio
, &hostname
)))
115 /* no SNI in client hello */
117 if (!TEST_str_eq(hostname
, host
))
118 /* incorrect SNI value */
122 OPENSSL_free(hostname
);
128 static int client_setup_sni_after_state()
134 char *hostname
= NULL
;
137 /* use TLS_method to blur 'side' */
138 ctx
= SSL_CTX_new(TLS_method());
146 rbio
= BIO_new(BIO_s_mem());
147 wbio
= BIO_new(BIO_s_mem());
148 if (!TEST_ptr(rbio
)|| !TEST_ptr(wbio
)) {
154 SSL_set_bio(con
, rbio
, wbio
);
155 SSL_set_connect_state(con
);
157 /* set SNI after 'client side' is set */
158 SSL_set_tlsext_host_name(con
, host
);
160 if (!TEST_int_le(SSL_connect(con
), 0))
161 /* This shouldn't succeed because we don't have a server! */
163 if (!TEST_true(get_sni_from_client_hello(wbio
, &hostname
)))
164 /* no SNI in client hello */
166 if (!TEST_str_eq(hostname
, host
))
167 /* incorrect SNI value */
171 OPENSSL_free(hostname
);
177 static int server_setup_sni()
185 /* use TLS_server_method to choose 'server-side' */
186 ctx
= SSL_CTX_new(TLS_server_method());
194 rbio
= BIO_new(BIO_s_mem());
195 wbio
= BIO_new(BIO_s_mem());
196 if (!TEST_ptr(rbio
)|| !TEST_ptr(wbio
)) {
202 SSL_set_bio(con
, rbio
, wbio
);
204 /* set SNI at server side */
205 SSL_set_tlsext_host_name(con
, host
);
207 if (!TEST_int_le(SSL_accept(con
), 0))
208 /* This shouldn't succeed because we have nothing to listen on */
210 if (!TEST_ptr_null(SSL_get_servername(con
, TLSEXT_NAMETYPE_host_name
)))
211 /* SNI should be cleared by SSL_accpet */
220 typedef int (*sni_test_fn
)(void);
222 static sni_test_fn sni_test_fns
[3] = {
223 client_setup_sni_before_state
,
224 client_setup_sni_after_state
,
228 static int test_servername(int test
)
231 * For each test set up an SSL_CTX and SSL and see
234 return sni_test_fns
[test
]();
237 int setup_tests(void)
239 ADD_ALL_TESTS(test_servername
, OSSL_NELEM(sni_test_fns
));