1*4724848cSchristos /*
2*4724848cSchristos * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8*4724848cSchristos */
9*4724848cSchristos
10*4724848cSchristos #include <stdio.h>
11*4724848cSchristos #include <string.h>
12*4724848cSchristos
13*4724848cSchristos #include <openssl/dtls1.h>
14*4724848cSchristos #include <openssl/ssl.h>
15*4724848cSchristos #include <openssl/err.h>
16*4724848cSchristos
17*4724848cSchristos #include "ssltestlib.h"
18*4724848cSchristos #include "testutil.h"
19*4724848cSchristos
20*4724848cSchristos /* for SSL_READ_ETM() */
21*4724848cSchristos #include "../ssl/ssl_local.h"
22*4724848cSchristos
23*4724848cSchristos static int debug = 0;
24*4724848cSchristos
clnt_psk_callback(SSL * ssl,const char * hint,char * ident,unsigned int max_ident_len,unsigned char * psk,unsigned int max_psk_len)25*4724848cSchristos static unsigned int clnt_psk_callback(SSL *ssl, const char *hint,
26*4724848cSchristos char *ident, unsigned int max_ident_len,
27*4724848cSchristos unsigned char *psk,
28*4724848cSchristos unsigned int max_psk_len)
29*4724848cSchristos {
30*4724848cSchristos BIO_snprintf(ident, max_ident_len, "psk");
31*4724848cSchristos
32*4724848cSchristos if (max_psk_len > 20)
33*4724848cSchristos max_psk_len = 20;
34*4724848cSchristos memset(psk, 0x5a, max_psk_len);
35*4724848cSchristos
36*4724848cSchristos return max_psk_len;
37*4724848cSchristos }
38*4724848cSchristos
srvr_psk_callback(SSL * ssl,const char * identity,unsigned char * psk,unsigned int max_psk_len)39*4724848cSchristos static unsigned int srvr_psk_callback(SSL *ssl, const char *identity,
40*4724848cSchristos unsigned char *psk,
41*4724848cSchristos unsigned int max_psk_len)
42*4724848cSchristos {
43*4724848cSchristos if (max_psk_len > 20)
44*4724848cSchristos max_psk_len = 20;
45*4724848cSchristos memset(psk, 0x5a, max_psk_len);
46*4724848cSchristos return max_psk_len;
47*4724848cSchristos }
48*4724848cSchristos
mtu_test(SSL_CTX * ctx,const char * cs,int no_etm)49*4724848cSchristos static int mtu_test(SSL_CTX *ctx, const char *cs, int no_etm)
50*4724848cSchristos {
51*4724848cSchristos SSL *srvr_ssl = NULL, *clnt_ssl = NULL;
52*4724848cSchristos BIO *sc_bio = NULL;
53*4724848cSchristos int i;
54*4724848cSchristos size_t s;
55*4724848cSchristos size_t mtus[30];
56*4724848cSchristos unsigned char buf[600];
57*4724848cSchristos int rv = 0;
58*4724848cSchristos
59*4724848cSchristos memset(buf, 0x5a, sizeof(buf));
60*4724848cSchristos
61*4724848cSchristos if (!TEST_true(create_ssl_objects(ctx, ctx, &srvr_ssl, &clnt_ssl,
62*4724848cSchristos NULL, NULL)))
63*4724848cSchristos goto end;
64*4724848cSchristos
65*4724848cSchristos if (no_etm)
66*4724848cSchristos SSL_set_options(srvr_ssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
67*4724848cSchristos
68*4724848cSchristos if (!TEST_true(SSL_set_cipher_list(srvr_ssl, cs))
69*4724848cSchristos || !TEST_true(SSL_set_cipher_list(clnt_ssl, cs))
70*4724848cSchristos || !TEST_ptr(sc_bio = SSL_get_rbio(srvr_ssl))
71*4724848cSchristos || !TEST_true(create_ssl_connection(clnt_ssl, srvr_ssl,
72*4724848cSchristos SSL_ERROR_NONE)))
73*4724848cSchristos goto end;
74*4724848cSchristos
75*4724848cSchristos if (debug)
76*4724848cSchristos TEST_info("Channel established");
77*4724848cSchristos
78*4724848cSchristos /* For record MTU values between 500 and 539, call DTLS_get_data_mtu()
79*4724848cSchristos * to query the payload MTU which will fit. */
80*4724848cSchristos for (i = 0; i < 30; i++) {
81*4724848cSchristos SSL_set_mtu(clnt_ssl, 500 + i);
82*4724848cSchristos mtus[i] = DTLS_get_data_mtu(clnt_ssl);
83*4724848cSchristos if (debug)
84*4724848cSchristos TEST_info("%s%s MTU for record mtu %d = %lu",
85*4724848cSchristos cs, no_etm ? "-noEtM" : "",
86*4724848cSchristos 500 + i, (unsigned long)mtus[i]);
87*4724848cSchristos if (!TEST_size_t_ne(mtus[i], 0)) {
88*4724848cSchristos TEST_info("Cipher %s MTU %d", cs, 500 + i);
89*4724848cSchristos goto end;
90*4724848cSchristos }
91*4724848cSchristos }
92*4724848cSchristos
93*4724848cSchristos /* Now get out of the way */
94*4724848cSchristos SSL_set_mtu(clnt_ssl, 1000);
95*4724848cSchristos
96*4724848cSchristos /*
97*4724848cSchristos * Now for all values in the range of payload MTUs, send a payload of
98*4724848cSchristos * that size and see what actual record size we end up with.
99*4724848cSchristos */
100*4724848cSchristos for (s = mtus[0]; s <= mtus[29]; s++) {
101*4724848cSchristos size_t reclen;
102*4724848cSchristos
103*4724848cSchristos if (!TEST_int_eq(SSL_write(clnt_ssl, buf, s), (int)s))
104*4724848cSchristos goto end;
105*4724848cSchristos reclen = BIO_read(sc_bio, buf, sizeof(buf));
106*4724848cSchristos if (debug)
107*4724848cSchristos TEST_info("record %zu for payload %zu", reclen, s);
108*4724848cSchristos
109*4724848cSchristos for (i = 0; i < 30; i++) {
110*4724848cSchristos /* DTLS_get_data_mtu() with record MTU 500+i returned mtus[i] ... */
111*4724848cSchristos
112*4724848cSchristos if (!TEST_false(s <= mtus[i] && reclen > (size_t)(500 + i))) {
113*4724848cSchristos /*
114*4724848cSchristos * We sent a packet smaller than or equal to mtus[j] and
115*4724848cSchristos * that made a record *larger* than the record MTU 500+j!
116*4724848cSchristos */
117*4724848cSchristos TEST_error("%s: s=%lu, mtus[i]=%lu, reclen=%lu, i=%d",
118*4724848cSchristos cs, (unsigned long)s, (unsigned long)mtus[i],
119*4724848cSchristos (unsigned long)reclen, 500 + i);
120*4724848cSchristos goto end;
121*4724848cSchristos }
122*4724848cSchristos if (!TEST_false(s > mtus[i] && reclen <= (size_t)(500 + i))) {
123*4724848cSchristos /*
124*4724848cSchristos * We sent a *larger* packet than mtus[i] and that *still*
125*4724848cSchristos * fits within the record MTU 500+i, so DTLS_get_data_mtu()
126*4724848cSchristos * was overly pessimistic.
127*4724848cSchristos */
128*4724848cSchristos TEST_error("%s: s=%lu, mtus[i]=%lu, reclen=%lu, i=%d",
129*4724848cSchristos cs, (unsigned long)s, (unsigned long)mtus[i],
130*4724848cSchristos (unsigned long)reclen, 500 + i);
131*4724848cSchristos goto end;
132*4724848cSchristos }
133*4724848cSchristos }
134*4724848cSchristos }
135*4724848cSchristos rv = 1;
136*4724848cSchristos if (SSL_READ_ETM(clnt_ssl))
137*4724848cSchristos rv = 2;
138*4724848cSchristos end:
139*4724848cSchristos SSL_free(clnt_ssl);
140*4724848cSchristos SSL_free(srvr_ssl);
141*4724848cSchristos return rv;
142*4724848cSchristos }
143*4724848cSchristos
run_mtu_tests(void)144*4724848cSchristos static int run_mtu_tests(void)
145*4724848cSchristos {
146*4724848cSchristos SSL_CTX *ctx = NULL;
147*4724848cSchristos STACK_OF(SSL_CIPHER) *ciphers;
148*4724848cSchristos int i, ret = 0;
149*4724848cSchristos
150*4724848cSchristos if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_method())))
151*4724848cSchristos goto end;
152*4724848cSchristos
153*4724848cSchristos SSL_CTX_set_psk_server_callback(ctx, srvr_psk_callback);
154*4724848cSchristos SSL_CTX_set_psk_client_callback(ctx, clnt_psk_callback);
155*4724848cSchristos SSL_CTX_set_security_level(ctx, 0);
156*4724848cSchristos
157*4724848cSchristos /*
158*4724848cSchristos * We only care about iterating over each enc/mac; we don't want to
159*4724848cSchristos * repeat the test for each auth/kx variant. So keep life simple and
160*4724848cSchristos * only do (non-DH) PSK.
161*4724848cSchristos */
162*4724848cSchristos if (!TEST_true(SSL_CTX_set_cipher_list(ctx, "PSK")))
163*4724848cSchristos goto end;
164*4724848cSchristos
165*4724848cSchristos ciphers = SSL_CTX_get_ciphers(ctx);
166*4724848cSchristos for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
167*4724848cSchristos const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
168*4724848cSchristos const char *cipher_name = SSL_CIPHER_get_name(cipher);
169*4724848cSchristos
170*4724848cSchristos /* As noted above, only one test for each enc/mac variant. */
171*4724848cSchristos if (strncmp(cipher_name, "PSK-", 4) != 0)
172*4724848cSchristos continue;
173*4724848cSchristos
174*4724848cSchristos if (!TEST_int_gt(ret = mtu_test(ctx, cipher_name, 0), 0))
175*4724848cSchristos break;
176*4724848cSchristos TEST_info("%s OK", cipher_name);
177*4724848cSchristos if (ret == 1)
178*4724848cSchristos continue;
179*4724848cSchristos
180*4724848cSchristos /* mtu_test() returns 2 if it used Encrypt-then-MAC */
181*4724848cSchristos if (!TEST_int_gt(ret = mtu_test(ctx, cipher_name, 1), 0))
182*4724848cSchristos break;
183*4724848cSchristos TEST_info("%s without EtM OK", cipher_name);
184*4724848cSchristos }
185*4724848cSchristos
186*4724848cSchristos end:
187*4724848cSchristos SSL_CTX_free(ctx);
188*4724848cSchristos return ret;
189*4724848cSchristos }
190*4724848cSchristos
test_server_mtu_larger_than_max_fragment_length(void)191*4724848cSchristos static int test_server_mtu_larger_than_max_fragment_length(void)
192*4724848cSchristos {
193*4724848cSchristos SSL_CTX *ctx = NULL;
194*4724848cSchristos SSL *srvr_ssl = NULL, *clnt_ssl = NULL;
195*4724848cSchristos int rv = 0;
196*4724848cSchristos
197*4724848cSchristos if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_method())))
198*4724848cSchristos goto end;
199*4724848cSchristos
200*4724848cSchristos SSL_CTX_set_psk_server_callback(ctx, srvr_psk_callback);
201*4724848cSchristos SSL_CTX_set_psk_client_callback(ctx, clnt_psk_callback);
202*4724848cSchristos
203*4724848cSchristos #ifndef OPENSSL_NO_DH
204*4724848cSchristos if (!TEST_true(SSL_CTX_set_dh_auto(ctx, 1)))
205*4724848cSchristos goto end;
206*4724848cSchristos #endif
207*4724848cSchristos
208*4724848cSchristos if (!TEST_true(create_ssl_objects(ctx, ctx, &srvr_ssl, &clnt_ssl,
209*4724848cSchristos NULL, NULL)))
210*4724848cSchristos goto end;
211*4724848cSchristos
212*4724848cSchristos SSL_set_options(srvr_ssl, SSL_OP_NO_QUERY_MTU);
213*4724848cSchristos if (!TEST_true(DTLS_set_link_mtu(srvr_ssl, 1500)))
214*4724848cSchristos goto end;
215*4724848cSchristos
216*4724848cSchristos SSL_set_tlsext_max_fragment_length(clnt_ssl,
217*4724848cSchristos TLSEXT_max_fragment_length_512);
218*4724848cSchristos
219*4724848cSchristos if (!TEST_true(create_ssl_connection(srvr_ssl, clnt_ssl,
220*4724848cSchristos SSL_ERROR_NONE)))
221*4724848cSchristos goto end;
222*4724848cSchristos
223*4724848cSchristos rv = 1;
224*4724848cSchristos
225*4724848cSchristos end:
226*4724848cSchristos SSL_free(clnt_ssl);
227*4724848cSchristos SSL_free(srvr_ssl);
228*4724848cSchristos SSL_CTX_free(ctx);
229*4724848cSchristos return rv;
230*4724848cSchristos }
231*4724848cSchristos
setup_tests(void)232*4724848cSchristos int setup_tests(void)
233*4724848cSchristos {
234*4724848cSchristos ADD_TEST(run_mtu_tests);
235*4724848cSchristos ADD_TEST(test_server_mtu_larger_than_max_fragment_length);
236*4724848cSchristos return 1;
237*4724848cSchristos }
238*4724848cSchristos
cleanup_tests(void)239*4724848cSchristos void cleanup_tests(void)
240*4724848cSchristos {
241*4724848cSchristos bio_s_mempacket_test_free();
242*4724848cSchristos }
243