]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/servername_test.c
Remove MAC cruft
[thirdparty/openssl.git] / test / servername_test.c
CommitLineData
190b9a03 1/*
1212818e 2 * Copyright 2017-2018 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"
f01344cb 25#include "ssltestlib.h"
190b9a03
PY
26
27#define CLIENT_VERSION_LEN 2
28
29static const char *host = "dummy-host";
30
f01344cb
MC
31static char *cert = NULL;
32static char *privkey = NULL;
33
190b9a03
PY
34static 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 }
86end:
87 return ret;
88}
89
31a80694 90static 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;
131end:
132 OPENSSL_free(hostname);
133 SSL_free(con);
134 SSL_CTX_free(ctx);
135 return ret;
136}
137
31a80694 138static 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;
180end:
181 OPENSSL_free(hostname);
182 SSL_free(con);
183 SSL_CTX_free(ctx);
184 return ret;
185}
186
31a80694 187static 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
f01344cb
MC
193 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
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 214end:
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
223typedef int (*sni_test_fn)(void);
224
225static 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
231static 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
240int setup_tests(void)
241{
f01344cb
MC
242 if (!TEST_ptr(cert = test_get_argument(0))
243 || !TEST_ptr(privkey = test_get_argument(1)))
244 return 0;
245
190b9a03
PY
246 ADD_ALL_TESTS(test_servername, OSSL_NELEM(sni_test_fns));
247 return 1;
248}