]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/servername_test.c
Fix SSL_check_chain()
[thirdparty/openssl.git] / test / servername_test.c
1 /*
2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2017 BaishanCloud. All rights reserved.
4 *
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
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
21 #include "internal/packet.h"
22
23 #include "testutil.h"
24 #include "internal/nelem.h"
25 #include "ssltestlib.h"
26
27 #define CLIENT_VERSION_LEN 2
28
29 static const char *host = "dummy-host";
30
31 static char *cert = NULL;
32 static char *privkey = NULL;
33
34 static int get_sni_from_client_hello(BIO *bio, char **sni)
35 {
36 long len;
37 unsigned char *data;
38 PACKET pkt, pkt2, pkt3, pkt4, pkt5;
39 unsigned int servname_type = 0, type = 0;
40 int ret = 0;
41
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
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
90 static int client_setup_sni_before_state(void)
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
138 static int client_setup_sni_after_state(void)
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
187 static int server_setup_sni(void)
188 {
189 SSL_CTX *cctx = NULL, *sctx = NULL;
190 SSL *clientssl = NULL, *serverssl = NULL;
191 int testresult = 0;
192
193 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
194 TLS_client_method(),
195 TLS1_VERSION, 0,
196 &sctx, &cctx, cert, privkey))
197 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
198 NULL, NULL)))
199 goto end;
200
201 /* set SNI at server side */
202 SSL_set_tlsext_host_name(serverssl, host);
203
204 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
205 goto end;
206
207 if (!TEST_ptr_null(SSL_get_servername(serverssl,
208 TLSEXT_NAMETYPE_host_name))) {
209 /* SNI should have been cleared during handshake */
210 goto end;
211 }
212
213 testresult = 1;
214 end:
215 SSL_free(serverssl);
216 SSL_free(clientssl);
217 SSL_CTX_free(sctx);
218 SSL_CTX_free(cctx);
219
220 return testresult;
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 {
242 if (!TEST_ptr(cert = test_get_argument(0))
243 || !TEST_ptr(privkey = test_get_argument(1)))
244 return 0;
245
246 ADD_ALL_TESTS(test_servername, OSSL_NELEM(sni_test_fns));
247 return 1;
248 }