Add test for CLIENT_EARLY_TRAFFIC_SECRET key logging
[openssl.git] / test / servername_test.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2017 BaishanCloud. All rights reserved.
4  *
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
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 "../ssl/packet_locl.h"
22
23 #include "testutil.h"
24 #include "internal/nelem.h"
25
26 #define CLIENT_VERSION_LEN      2
27
28 static const char *host = "dummy-host";
29
30 static int get_sni_from_client_hello(BIO *bio, char **sni)
31 {
32     long len;
33     unsigned char *data;
34     PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0}, pkt4 = {0}, pkt5 = {0};
35     unsigned int servname_type = 0, type = 0;
36     int ret = 0;
37
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
46                                                + SSL3_RANDOM_SIZE))
47                /* Skip session id */
48             || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
49                /* Skip ciphers */
50             || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
51                /* Skip compression */
52             || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
53                /* Extensions len */
54             || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
55         goto end;
56
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)))
61             goto end;
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)))
71                 goto end;
72             ret = 1;
73             goto end;
74         }
75     }
76 end:
77     return ret;
78 }
79
80 static int client_setup_sni_before_state(void)
81 {
82     SSL_CTX *ctx;
83     SSL *con = NULL;
84     BIO *rbio;
85     BIO *wbio;
86     char *hostname = NULL;
87     int ret = 0;
88
89     /* use TLS_method to blur 'side' */
90     ctx = SSL_CTX_new(TLS_method());
91     if (!TEST_ptr(ctx))
92         goto end;
93
94     con = SSL_new(ctx);
95     if (!TEST_ptr(con))
96         goto end;
97
98     /* set SNI before 'client side' is set */
99     SSL_set_tlsext_host_name(con, host);
100
101     rbio = BIO_new(BIO_s_mem());
102     wbio = BIO_new(BIO_s_mem());
103     if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
104         BIO_free(rbio);
105         BIO_free(wbio);
106         goto end;
107     }
108
109     SSL_set_bio(con, rbio, wbio);
110
111     if (!TEST_int_le(SSL_connect(con), 0))
112         /* This shouldn't succeed because we don't have a server! */
113         goto end;
114     if (!TEST_true(get_sni_from_client_hello(wbio, &hostname)))
115         /* no SNI in client hello */
116         goto end;
117     if (!TEST_str_eq(hostname, host))
118         /* incorrect SNI value */
119         goto end;
120     ret = 1;
121 end:
122     OPENSSL_free(hostname);
123     SSL_free(con);
124     SSL_CTX_free(ctx);
125     return ret;
126 }
127
128 static int client_setup_sni_after_state(void)
129 {
130     SSL_CTX *ctx;
131     SSL *con = NULL;
132     BIO *rbio;
133     BIO *wbio;
134     char *hostname = NULL;
135     int ret = 0;
136
137     /* use TLS_method to blur 'side' */
138     ctx = SSL_CTX_new(TLS_method());
139     if (!TEST_ptr(ctx))
140         goto end;
141
142     con = SSL_new(ctx);
143     if (!TEST_ptr(con))
144         goto end;
145
146     rbio = BIO_new(BIO_s_mem());
147     wbio = BIO_new(BIO_s_mem());
148     if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
149         BIO_free(rbio);
150         BIO_free(wbio);
151         goto end;
152     }
153
154     SSL_set_bio(con, rbio, wbio);
155     SSL_set_connect_state(con);
156
157     /* set SNI after 'client side' is set */
158     SSL_set_tlsext_host_name(con, host);
159
160     if (!TEST_int_le(SSL_connect(con), 0))
161         /* This shouldn't succeed because we don't have a server! */
162         goto end;
163     if (!TEST_true(get_sni_from_client_hello(wbio, &hostname)))
164         /* no SNI in client hello */
165         goto end;
166     if (!TEST_str_eq(hostname, host))
167         /* incorrect SNI value */
168         goto end;
169     ret = 1;
170 end:
171     OPENSSL_free(hostname);
172     SSL_free(con);
173     SSL_CTX_free(ctx);
174     return ret;
175 }
176
177 static int server_setup_sni(void)
178 {
179     SSL_CTX *ctx;
180     SSL *con = NULL;
181     BIO *rbio;
182     BIO *wbio;
183     int ret = 0;
184
185     /* use TLS_server_method to choose 'server-side' */
186     ctx = SSL_CTX_new(TLS_server_method());
187     if (!TEST_ptr(ctx))
188         goto end;
189
190     con = SSL_new(ctx);
191     if (!TEST_ptr(con))
192         goto end;
193
194     rbio = BIO_new(BIO_s_mem());
195     wbio = BIO_new(BIO_s_mem());
196     if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
197         BIO_free(rbio);
198         BIO_free(wbio);
199         goto end;
200     }
201
202     SSL_set_bio(con, rbio, wbio);
203
204     /* set SNI at server side */
205     SSL_set_tlsext_host_name(con, host);
206
207     if (!TEST_int_le(SSL_accept(con), 0))
208         /* This shouldn't succeed because we have nothing to listen on */
209         goto end;
210     if (!TEST_ptr_null(SSL_get_servername(con, TLSEXT_NAMETYPE_host_name)))
211         /* SNI should be cleared by SSL_accpet */
212         goto end;
213     ret = 1;
214 end:
215     SSL_free(con);
216     SSL_CTX_free(ctx);
217     return ret;
218 }
219
220 typedef int (*sni_test_fn)(void);
221
222 static sni_test_fn sni_test_fns[3] = {
223     client_setup_sni_before_state,
224     client_setup_sni_after_state,
225     server_setup_sni
226 };
227
228 static int test_servername(int test)
229 {
230     /*
231      * For each test set up an SSL_CTX and SSL and see
232      * what SNI behaves.
233      */
234     return sni_test_fns[test]();
235 }
236
237 int setup_tests(void)
238 {
239     ADD_ALL_TESTS(test_servername, OSSL_NELEM(sni_test_fns));
240     return 1;
241 }