]>
Commit | Line | Data |
---|---|---|
190b9a03 | 1 | /* |
33388b44 | 2 | * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. |
190b9a03 PY |
3 | * Copyright 2017 BaishanCloud. All rights reserved. |
4 | * | |
909f1a2e | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
190b9a03 PY |
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 | ||
11 | #include <string.h> | |
12 | ||
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> | |
19 | #include <time.h> | |
20 | ||
0d345f0e | 21 | #include "internal/packet.h" |
190b9a03 PY |
22 | |
23 | #include "testutil.h" | |
176db6dc | 24 | #include "internal/nelem.h" |
20f8bc72 | 25 | #include "helpers/ssltestlib.h" |
190b9a03 PY |
26 | |
27 | #define CLIENT_VERSION_LEN 2 | |
28 | ||
29 | static const char *host = "dummy-host"; | |
30 | ||
f01344cb MC |
31 | static char *cert = NULL; |
32 | static char *privkey = NULL; | |
33 | ||
190b9a03 PY |
34 | static int get_sni_from_client_hello(BIO *bio, char **sni) |
35 | { | |
36 | long len; | |
37 | unsigned char *data; | |
72962d02 | 38 | PACKET pkt, pkt2, pkt3, pkt4, pkt5; |
190b9a03 PY |
39 | unsigned int servname_type = 0, type = 0; |
40 | int ret = 0; | |
41 | ||
72962d02 P |
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)); | |
47 | ||
190b9a03 PY |
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 | |
56 | + SSL3_RANDOM_SIZE)) | |
57 | /* Skip session id */ | |
58 | || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) | |
59 | /* Skip ciphers */ | |
60 | || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2)) | |
61 | /* Skip compression */ | |
62 | || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) | |
63 | /* Extensions len */ | |
64 | || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2))) | |
65 | goto end; | |
66 | ||
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))) | |
71 | goto end; | |
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))) | |
81 | goto end; | |
82 | ret = 1; | |
83 | goto end; | |
84 | } | |
85 | } | |
86 | end: | |
87 | return ret; | |
88 | } | |
89 | ||
31a80694 | 90 | static int client_setup_sni_before_state(void) |
190b9a03 PY |
91 | { |
92 | SSL_CTX *ctx; | |
93 | SSL *con = NULL; | |
94 | BIO *rbio; | |
95 | BIO *wbio; | |
96 | char *hostname = NULL; | |
97 | int ret = 0; | |
98 | ||
99 | /* use TLS_method to blur 'side' */ | |
100 | ctx = SSL_CTX_new(TLS_method()); | |
101 | if (!TEST_ptr(ctx)) | |
102 | goto end; | |
103 | ||
104 | con = SSL_new(ctx); | |
105 | if (!TEST_ptr(con)) | |
106 | goto end; | |
107 | ||
108 | /* set SNI before 'client side' is set */ | |
109 | SSL_set_tlsext_host_name(con, host); | |
110 | ||
111 | rbio = BIO_new(BIO_s_mem()); | |
112 | wbio = BIO_new(BIO_s_mem()); | |
113 | if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) { | |
114 | BIO_free(rbio); | |
115 | BIO_free(wbio); | |
116 | goto end; | |
117 | } | |
118 | ||
119 | SSL_set_bio(con, rbio, wbio); | |
120 | ||
121 | if (!TEST_int_le(SSL_connect(con), 0)) | |
122 | /* This shouldn't succeed because we don't have a server! */ | |
123 | goto end; | |
124 | if (!TEST_true(get_sni_from_client_hello(wbio, &hostname))) | |
125 | /* no SNI in client hello */ | |
126 | goto end; | |
127 | if (!TEST_str_eq(hostname, host)) | |
128 | /* incorrect SNI value */ | |
129 | goto end; | |
130 | ret = 1; | |
131 | end: | |
132 | OPENSSL_free(hostname); | |
133 | SSL_free(con); | |
134 | SSL_CTX_free(ctx); | |
135 | return ret; | |
136 | } | |
137 | ||
31a80694 | 138 | static int client_setup_sni_after_state(void) |
190b9a03 PY |
139 | { |
140 | SSL_CTX *ctx; | |
141 | SSL *con = NULL; | |
142 | BIO *rbio; | |
143 | BIO *wbio; | |
144 | char *hostname = NULL; | |
145 | int ret = 0; | |
146 | ||
147 | /* use TLS_method to blur 'side' */ | |
148 | ctx = SSL_CTX_new(TLS_method()); | |
149 | if (!TEST_ptr(ctx)) | |
150 | goto end; | |
151 | ||
152 | con = SSL_new(ctx); | |
153 | if (!TEST_ptr(con)) | |
154 | goto end; | |
155 | ||
156 | rbio = BIO_new(BIO_s_mem()); | |
157 | wbio = BIO_new(BIO_s_mem()); | |
158 | if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) { | |
159 | BIO_free(rbio); | |
160 | BIO_free(wbio); | |
161 | goto end; | |
162 | } | |
163 | ||
164 | SSL_set_bio(con, rbio, wbio); | |
165 | SSL_set_connect_state(con); | |
166 | ||
167 | /* set SNI after 'client side' is set */ | |
168 | SSL_set_tlsext_host_name(con, host); | |
169 | ||
170 | if (!TEST_int_le(SSL_connect(con), 0)) | |
171 | /* This shouldn't succeed because we don't have a server! */ | |
172 | goto end; | |
173 | if (!TEST_true(get_sni_from_client_hello(wbio, &hostname))) | |
174 | /* no SNI in client hello */ | |
175 | goto end; | |
176 | if (!TEST_str_eq(hostname, host)) | |
177 | /* incorrect SNI value */ | |
178 | goto end; | |
179 | ret = 1; | |
180 | end: | |
181 | OPENSSL_free(hostname); | |
182 | SSL_free(con); | |
183 | SSL_CTX_free(ctx); | |
184 | return ret; | |
185 | } | |
186 | ||
31a80694 | 187 | static int server_setup_sni(void) |
190b9a03 | 188 | { |
f01344cb MC |
189 | SSL_CTX *cctx = NULL, *sctx = NULL; |
190 | SSL *clientssl = NULL, *serverssl = NULL; | |
191 | int testresult = 0; | |
190b9a03 | 192 | |
5e30f2fd | 193 | if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_server_method(), |
f01344cb | 194 | TLS_client_method(), |
5c587fb6 | 195 | TLS1_VERSION, 0, |
f01344cb MC |
196 | &sctx, &cctx, cert, privkey)) |
197 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
198 | NULL, NULL))) | |
190b9a03 | 199 | goto end; |
190b9a03 PY |
200 | |
201 | /* set SNI at server side */ | |
f01344cb | 202 | SSL_set_tlsext_host_name(serverssl, host); |
190b9a03 | 203 | |
f01344cb | 204 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) |
190b9a03 | 205 | goto end; |
f01344cb MC |
206 | |
207 | if (!TEST_ptr_null(SSL_get_servername(serverssl, | |
208 | TLSEXT_NAMETYPE_host_name))) { | |
209 | /* SNI should have been cleared during handshake */ | |
190b9a03 | 210 | goto end; |
f01344cb | 211 | } |
df443918 | 212 | |
f01344cb | 213 | testresult = 1; |
190b9a03 | 214 | end: |
f01344cb MC |
215 | SSL_free(serverssl); |
216 | SSL_free(clientssl); | |
217 | SSL_CTX_free(sctx); | |
218 | SSL_CTX_free(cctx); | |
219 | ||
220 | return testresult; | |
190b9a03 PY |
221 | } |
222 | ||
223 | typedef int (*sni_test_fn)(void); | |
224 | ||
225 | static sni_test_fn sni_test_fns[3] = { | |
226 | client_setup_sni_before_state, | |
227 | client_setup_sni_after_state, | |
228 | server_setup_sni | |
229 | }; | |
230 | ||
231 | static int test_servername(int test) | |
232 | { | |
233 | /* | |
234 | * For each test set up an SSL_CTX and SSL and see | |
235 | * what SNI behaves. | |
236 | */ | |
237 | return sni_test_fns[test](); | |
238 | } | |
239 | ||
240 | int setup_tests(void) | |
241 | { | |
8d242823 MC |
242 | if (!test_skip_common_options()) { |
243 | TEST_error("Error parsing test options\n"); | |
244 | return 0; | |
245 | } | |
246 | ||
f01344cb MC |
247 | if (!TEST_ptr(cert = test_get_argument(0)) |
248 | || !TEST_ptr(privkey = test_get_argument(1))) | |
249 | return 0; | |
250 | ||
190b9a03 PY |
251 | ADD_ALL_TESTS(test_servername, OSSL_NELEM(sni_test_fns)); |
252 | return 1; | |
253 | } |