2 * Copyright 2017-2019 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_local.h"
24 #include "internal/nelem.h"
25 #include "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 static int get_sni_from_client_hello(BIO
*bio
, char **sni
)
38 PACKET pkt
= {0}, pkt2
= {0}, pkt3
= {0}, pkt4
= {0}, pkt5
= {0};
39 unsigned int servname_type
= 0, type
= 0;
42 len
= BIO_get_mem_data(bio
, (char **)&data
);
43 if (!TEST_true(PACKET_buf_init(&pkt
, data
, len
))
44 /* Skip the record header */
45 || !PACKET_forward(&pkt
, SSL3_RT_HEADER_LENGTH
)
46 /* Skip the handshake message header */
47 || !TEST_true(PACKET_forward(&pkt
, SSL3_HM_HEADER_LENGTH
))
48 /* Skip client version and random */
49 || !TEST_true(PACKET_forward(&pkt
, CLIENT_VERSION_LEN
52 || !TEST_true(PACKET_get_length_prefixed_1(&pkt
, &pkt2
))
54 || !TEST_true(PACKET_get_length_prefixed_2(&pkt
, &pkt2
))
55 /* Skip compression */
56 || !TEST_true(PACKET_get_length_prefixed_1(&pkt
, &pkt2
))
58 || !TEST_true(PACKET_as_length_prefixed_2(&pkt
, &pkt2
)))
61 /* Loop through all extensions for SNI */
62 while (PACKET_remaining(&pkt2
)) {
63 if (!TEST_true(PACKET_get_net_2(&pkt2
, &type
))
64 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2
, &pkt3
)))
66 if (type
== TLSEXT_TYPE_server_name
) {
67 if (!TEST_true(PACKET_get_length_prefixed_2(&pkt3
, &pkt4
))
68 || !TEST_uint_ne(PACKET_remaining(&pkt4
), 0)
69 || !TEST_true(PACKET_get_1(&pkt4
, &servname_type
))
70 || !TEST_uint_eq(servname_type
, TLSEXT_NAMETYPE_host_name
)
71 || !TEST_true(PACKET_get_length_prefixed_2(&pkt4
, &pkt5
))
72 || !TEST_uint_le(PACKET_remaining(&pkt5
), TLSEXT_MAXLEN_host_name
)
73 || !TEST_false(PACKET_contains_zero_byte(&pkt5
))
74 || !TEST_true(PACKET_strndup(&pkt5
, sni
)))
84 static int client_setup_sni_before_state(void)
90 char *hostname
= NULL
;
93 /* use TLS_method to blur 'side' */
94 ctx
= SSL_CTX_new(TLS_method());
102 /* set SNI before 'client side' is set */
103 SSL_set_tlsext_host_name(con
, host
);
105 rbio
= BIO_new(BIO_s_mem());
106 wbio
= BIO_new(BIO_s_mem());
107 if (!TEST_ptr(rbio
)|| !TEST_ptr(wbio
)) {
113 SSL_set_bio(con
, rbio
, wbio
);
115 if (!TEST_int_le(SSL_connect(con
), 0))
116 /* This shouldn't succeed because we don't have a server! */
118 if (!TEST_true(get_sni_from_client_hello(wbio
, &hostname
)))
119 /* no SNI in client hello */
121 if (!TEST_str_eq(hostname
, host
))
122 /* incorrect SNI value */
126 OPENSSL_free(hostname
);
132 static int client_setup_sni_after_state(void)
138 char *hostname
= NULL
;
141 /* use TLS_method to blur 'side' */
142 ctx
= SSL_CTX_new(TLS_method());
150 rbio
= BIO_new(BIO_s_mem());
151 wbio
= BIO_new(BIO_s_mem());
152 if (!TEST_ptr(rbio
)|| !TEST_ptr(wbio
)) {
158 SSL_set_bio(con
, rbio
, wbio
);
159 SSL_set_connect_state(con
);
161 /* set SNI after 'client side' is set */
162 SSL_set_tlsext_host_name(con
, host
);
164 if (!TEST_int_le(SSL_connect(con
), 0))
165 /* This shouldn't succeed because we don't have a server! */
167 if (!TEST_true(get_sni_from_client_hello(wbio
, &hostname
)))
168 /* no SNI in client hello */
170 if (!TEST_str_eq(hostname
, host
))
171 /* incorrect SNI value */
175 OPENSSL_free(hostname
);
181 static int server_setup_sni(void)
183 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
184 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
187 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
189 TLS1_VERSION
, TLS_MAX_VERSION
,
190 &sctx
, &cctx
, cert
, privkey
))
191 || !TEST_true(create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
,
195 /* set SNI at server side */
196 SSL_set_tlsext_host_name(serverssl
, host
);
198 if (!TEST_true(create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)))
201 if (!TEST_ptr_null(SSL_get_servername(serverssl
,
202 TLSEXT_NAMETYPE_host_name
))) {
203 /* SNI should have been cleared during handshake */
217 typedef int (*sni_test_fn
)(void);
219 static sni_test_fn sni_test_fns
[3] = {
220 client_setup_sni_before_state
,
221 client_setup_sni_after_state
,
225 static int test_servername(int test
)
228 * For each test set up an SSL_CTX and SSL and see
231 return sni_test_fns
[test
]();
234 int setup_tests(void)
236 if (!TEST_ptr(cert
= test_get_argument(0))
237 || !TEST_ptr(privkey
= test_get_argument(1)))
240 ADD_ALL_TESTS(test_servername
, OSSL_NELEM(sni_test_fns
));