2 * Copyright 2017-2018 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 "../ssl/packet_locl.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
, pkt2
, pkt3
, pkt4
, pkt5
;
39 unsigned int servname_type
= 0, type
= 0;
42 memset(&pkt
, 0, sizeof(pkt
));
43 memset(&pkt2
, 0, sizeof(pkt2
));
44 memset(&pkt3
, 0, sizeof(pkt3
));
45 memset(&pkt4
, 0, sizeof(pkt4
));
46 memset(&pkt5
, 0, sizeof(pkt5
));
48 len
= BIO_get_mem_data(bio
, (char **)&data
);
49 if (!TEST_true(PACKET_buf_init(&pkt
, data
, len
))
50 /* Skip the record header */
51 || !PACKET_forward(&pkt
, SSL3_RT_HEADER_LENGTH
)
52 /* Skip the handshake message header */
53 || !TEST_true(PACKET_forward(&pkt
, SSL3_HM_HEADER_LENGTH
))
54 /* Skip client version and random */
55 || !TEST_true(PACKET_forward(&pkt
, CLIENT_VERSION_LEN
58 || !TEST_true(PACKET_get_length_prefixed_1(&pkt
, &pkt2
))
60 || !TEST_true(PACKET_get_length_prefixed_2(&pkt
, &pkt2
))
61 /* Skip compression */
62 || !TEST_true(PACKET_get_length_prefixed_1(&pkt
, &pkt2
))
64 || !TEST_true(PACKET_as_length_prefixed_2(&pkt
, &pkt2
)))
67 /* Loop through all extensions for SNI */
68 while (PACKET_remaining(&pkt2
)) {
69 if (!TEST_true(PACKET_get_net_2(&pkt2
, &type
))
70 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2
, &pkt3
)))
72 if (type
== TLSEXT_TYPE_server_name
) {
73 if (!TEST_true(PACKET_get_length_prefixed_2(&pkt3
, &pkt4
))
74 || !TEST_uint_ne(PACKET_remaining(&pkt4
), 0)
75 || !TEST_true(PACKET_get_1(&pkt4
, &servname_type
))
76 || !TEST_uint_eq(servname_type
, TLSEXT_NAMETYPE_host_name
)
77 || !TEST_true(PACKET_get_length_prefixed_2(&pkt4
, &pkt5
))
78 || !TEST_uint_le(PACKET_remaining(&pkt5
), TLSEXT_MAXLEN_host_name
)
79 || !TEST_false(PACKET_contains_zero_byte(&pkt5
))
80 || !TEST_true(PACKET_strndup(&pkt5
, sni
)))
90 static int client_setup_sni_before_state(void)
96 char *hostname
= NULL
;
99 /* use TLS_method to blur 'side' */
100 ctx
= SSL_CTX_new(TLS_method());
108 /* set SNI before 'client side' is set */
109 SSL_set_tlsext_host_name(con
, host
);
111 rbio
= BIO_new(BIO_s_mem());
112 wbio
= BIO_new(BIO_s_mem());
113 if (!TEST_ptr(rbio
)|| !TEST_ptr(wbio
)) {
119 SSL_set_bio(con
, rbio
, wbio
);
121 if (!TEST_int_le(SSL_connect(con
), 0))
122 /* This shouldn't succeed because we don't have a server! */
124 if (!TEST_true(get_sni_from_client_hello(wbio
, &hostname
)))
125 /* no SNI in client hello */
127 if (!TEST_str_eq(hostname
, host
))
128 /* incorrect SNI value */
132 OPENSSL_free(hostname
);
138 static int client_setup_sni_after_state(void)
144 char *hostname
= NULL
;
147 /* use TLS_method to blur 'side' */
148 ctx
= SSL_CTX_new(TLS_method());
156 rbio
= BIO_new(BIO_s_mem());
157 wbio
= BIO_new(BIO_s_mem());
158 if (!TEST_ptr(rbio
)|| !TEST_ptr(wbio
)) {
164 SSL_set_bio(con
, rbio
, wbio
);
165 SSL_set_connect_state(con
);
167 /* set SNI after 'client side' is set */
168 SSL_set_tlsext_host_name(con
, host
);
170 if (!TEST_int_le(SSL_connect(con
), 0))
171 /* This shouldn't succeed because we don't have a server! */
173 if (!TEST_true(get_sni_from_client_hello(wbio
, &hostname
)))
174 /* no SNI in client hello */
176 if (!TEST_str_eq(hostname
, host
))
177 /* incorrect SNI value */
181 OPENSSL_free(hostname
);
187 static int server_setup_sni(void)
189 SSL_CTX
*cctx
= NULL
, *sctx
= NULL
;
190 SSL
*clientssl
= NULL
, *serverssl
= NULL
;
193 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
196 &sctx
, &cctx
, cert
, privkey
))
197 || !TEST_true(create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
,
201 /* set SNI at server side */
202 SSL_set_tlsext_host_name(serverssl
, host
);
204 if (!TEST_true(create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)))
207 if (!TEST_ptr_null(SSL_get_servername(serverssl
,
208 TLSEXT_NAMETYPE_host_name
))) {
209 /* SNI should have been cleared during handshake */
223 typedef int (*sni_test_fn
)(void);
225 static sni_test_fn sni_test_fns
[3] = {
226 client_setup_sni_before_state
,
227 client_setup_sni_after_state
,
231 static int test_servername(int test
)
234 * For each test set up an SSL_CTX and SSL and see
237 return sni_test_fns
[test
]();
240 int setup_tests(void)
242 if (!TEST_ptr(cert
= test_get_argument(0))
243 || !TEST_ptr(privkey
= test_get_argument(1)))
246 ADD_ALL_TESTS(test_servername
, OSSL_NELEM(sni_test_fns
));