xref: /freebsd-src/crypto/openssl/test/dtlsv1listentest.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <string.h>
11*e0c4386eSCy Schubert #include <openssl/ssl.h>
12*e0c4386eSCy Schubert #include <openssl/bio.h>
13*e0c4386eSCy Schubert #include <openssl/err.h>
14*e0c4386eSCy Schubert #include <openssl/conf.h>
15*e0c4386eSCy Schubert #include "internal/nelem.h"
16*e0c4386eSCy Schubert #include "testutil.h"
17*e0c4386eSCy Schubert 
18*e0c4386eSCy Schubert #ifndef OPENSSL_NO_SOCK
19*e0c4386eSCy Schubert 
20*e0c4386eSCy Schubert /* Just a ClientHello without a cookie */
21*e0c4386eSCy Schubert static const unsigned char clienthello_nocookie[] = {
22*e0c4386eSCy Schubert     0x16, /* Handshake */
23*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
24*e0c4386eSCy Schubert     0x00, 0x00, /* Epoch */
25*e0c4386eSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */
26*e0c4386eSCy Schubert     0x00, 0x3A, /* Record Length */
27*e0c4386eSCy Schubert     0x01, /* ClientHello */
28*e0c4386eSCy Schubert     0x00, 0x00, 0x2E, /* Message length */
29*e0c4386eSCy Schubert     0x00, 0x00, /* Message sequence */
30*e0c4386eSCy Schubert     0x00, 0x00, 0x00, /* Fragment offset */
31*e0c4386eSCy Schubert     0x00, 0x00, 0x2E, /* Fragment length */
32*e0c4386eSCy Schubert     0xFE, 0xFD, /* DTLSv1.2 */
33*e0c4386eSCy Schubert     0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90,
34*e0c4386eSCy Schubert     0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56,
35*e0c4386eSCy Schubert     0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */
36*e0c4386eSCy Schubert     0x00, /* Session id len */
37*e0c4386eSCy Schubert     0x00, /* Cookie len */
38*e0c4386eSCy Schubert     0x00, 0x04, /* Ciphersuites len */
39*e0c4386eSCy Schubert     0x00, 0x2f, /* AES128-SHA */
40*e0c4386eSCy Schubert     0x00, 0xff, /* Empty reneg info SCSV */
41*e0c4386eSCy Schubert     0x01, /* Compression methods len */
42*e0c4386eSCy Schubert     0x00, /* Null compression */
43*e0c4386eSCy Schubert     0x00, 0x00 /* Extensions len */
44*e0c4386eSCy Schubert };
45*e0c4386eSCy Schubert 
46*e0c4386eSCy Schubert /* First fragment of a ClientHello without a cookie */
47*e0c4386eSCy Schubert static const unsigned char clienthello_nocookie_frag[] = {
48*e0c4386eSCy Schubert     0x16, /* Handshake */
49*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
50*e0c4386eSCy Schubert     0x00, 0x00, /* Epoch */
51*e0c4386eSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */
52*e0c4386eSCy Schubert     0x00, 0x30, /* Record Length */
53*e0c4386eSCy Schubert     0x01, /* ClientHello */
54*e0c4386eSCy Schubert     0x00, 0x00, 0x2E, /* Message length */
55*e0c4386eSCy Schubert     0x00, 0x00, /* Message sequence */
56*e0c4386eSCy Schubert     0x00, 0x00, 0x00, /* Fragment offset */
57*e0c4386eSCy Schubert     0x00, 0x00, 0x24, /* Fragment length */
58*e0c4386eSCy Schubert     0xFE, 0xFD, /* DTLSv1.2 */
59*e0c4386eSCy Schubert     0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90,
60*e0c4386eSCy Schubert     0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56,
61*e0c4386eSCy Schubert     0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */
62*e0c4386eSCy Schubert     0x00, /* Session id len */
63*e0c4386eSCy Schubert     0x00 /* Cookie len */
64*e0c4386eSCy Schubert };
65*e0c4386eSCy Schubert 
66*e0c4386eSCy Schubert /* First fragment of a ClientHello which is too short */
67*e0c4386eSCy Schubert static const unsigned char clienthello_nocookie_short[] = {
68*e0c4386eSCy Schubert     0x16, /* Handshake */
69*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
70*e0c4386eSCy Schubert     0x00, 0x00, /* Epoch */
71*e0c4386eSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */
72*e0c4386eSCy Schubert     0x00, 0x2F, /* Record Length */
73*e0c4386eSCy Schubert     0x01, /* ClientHello */
74*e0c4386eSCy Schubert     0x00, 0x00, 0x2E, /* Message length */
75*e0c4386eSCy Schubert     0x00, 0x00, /* Message sequence */
76*e0c4386eSCy Schubert     0x00, 0x00, 0x00, /* Fragment offset */
77*e0c4386eSCy Schubert     0x00, 0x00, 0x23, /* Fragment length */
78*e0c4386eSCy Schubert     0xFE, 0xFD, /* DTLSv1.2 */
79*e0c4386eSCy Schubert     0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90,
80*e0c4386eSCy Schubert     0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56,
81*e0c4386eSCy Schubert     0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */
82*e0c4386eSCy Schubert     0x00 /* Session id len */
83*e0c4386eSCy Schubert };
84*e0c4386eSCy Schubert 
85*e0c4386eSCy Schubert /* Second fragment of a ClientHello */
86*e0c4386eSCy Schubert static const unsigned char clienthello_2ndfrag[] = {
87*e0c4386eSCy Schubert     0x16, /* Handshake */
88*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
89*e0c4386eSCy Schubert     0x00, 0x00, /* Epoch */
90*e0c4386eSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */
91*e0c4386eSCy Schubert     0x00, 0x38, /* Record Length */
92*e0c4386eSCy Schubert     0x01, /* ClientHello */
93*e0c4386eSCy Schubert     0x00, 0x00, 0x2E, /* Message length */
94*e0c4386eSCy Schubert     0x00, 0x00, /* Message sequence */
95*e0c4386eSCy Schubert     0x00, 0x00, 0x02, /* Fragment offset */
96*e0c4386eSCy Schubert     0x00, 0x00, 0x2C, /* Fragment length */
97*e0c4386eSCy Schubert     /* Version skipped - sent in first fragment */
98*e0c4386eSCy Schubert     0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90,
99*e0c4386eSCy Schubert     0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56,
100*e0c4386eSCy Schubert     0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */
101*e0c4386eSCy Schubert     0x00, /* Session id len */
102*e0c4386eSCy Schubert     0x00, /* Cookie len */
103*e0c4386eSCy Schubert     0x00, 0x04, /* Ciphersuites len */
104*e0c4386eSCy Schubert     0x00, 0x2f, /* AES128-SHA */
105*e0c4386eSCy Schubert     0x00, 0xff, /* Empty reneg info SCSV */
106*e0c4386eSCy Schubert     0x01, /* Compression methods len */
107*e0c4386eSCy Schubert     0x00, /* Null compression */
108*e0c4386eSCy Schubert     0x00, 0x00 /* Extensions len */
109*e0c4386eSCy Schubert };
110*e0c4386eSCy Schubert 
111*e0c4386eSCy Schubert /* A ClientHello with a good cookie */
112*e0c4386eSCy Schubert static const unsigned char clienthello_cookie[] = {
113*e0c4386eSCy Schubert     0x16, /* Handshake */
114*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
115*e0c4386eSCy Schubert     0x00, 0x00, /* Epoch */
116*e0c4386eSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */
117*e0c4386eSCy Schubert     0x00, 0x4E, /* Record Length */
118*e0c4386eSCy Schubert     0x01, /* ClientHello */
119*e0c4386eSCy Schubert     0x00, 0x00, 0x42, /* Message length */
120*e0c4386eSCy Schubert     0x00, 0x00, /* Message sequence */
121*e0c4386eSCy Schubert     0x00, 0x00, 0x00, /* Fragment offset */
122*e0c4386eSCy Schubert     0x00, 0x00, 0x42, /* Fragment length */
123*e0c4386eSCy Schubert     0xFE, 0xFD, /* DTLSv1.2 */
124*e0c4386eSCy Schubert     0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90,
125*e0c4386eSCy Schubert     0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56,
126*e0c4386eSCy Schubert     0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */
127*e0c4386eSCy Schubert     0x00, /* Session id len */
128*e0c4386eSCy Schubert     0x14, /* Cookie len */
129*e0c4386eSCy Schubert     0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
130*e0c4386eSCy Schubert     0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, /* Cookie */
131*e0c4386eSCy Schubert     0x00, 0x04, /* Ciphersuites len */
132*e0c4386eSCy Schubert     0x00, 0x2f, /* AES128-SHA */
133*e0c4386eSCy Schubert     0x00, 0xff, /* Empty reneg info SCSV */
134*e0c4386eSCy Schubert     0x01, /* Compression methods len */
135*e0c4386eSCy Schubert     0x00, /* Null compression */
136*e0c4386eSCy Schubert     0x00, 0x00 /* Extensions len */
137*e0c4386eSCy Schubert };
138*e0c4386eSCy Schubert 
139*e0c4386eSCy Schubert /* A fragmented ClientHello with a good cookie */
140*e0c4386eSCy Schubert static const unsigned char clienthello_cookie_frag[] = {
141*e0c4386eSCy Schubert     0x16, /* Handshake */
142*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
143*e0c4386eSCy Schubert     0x00, 0x00, /* Epoch */
144*e0c4386eSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */
145*e0c4386eSCy Schubert     0x00, 0x44, /* Record Length */
146*e0c4386eSCy Schubert     0x01, /* ClientHello */
147*e0c4386eSCy Schubert     0x00, 0x00, 0x42, /* Message length */
148*e0c4386eSCy Schubert     0x00, 0x00, /* Message sequence */
149*e0c4386eSCy Schubert     0x00, 0x00, 0x00, /* Fragment offset */
150*e0c4386eSCy Schubert     0x00, 0x00, 0x38, /* Fragment length */
151*e0c4386eSCy Schubert     0xFE, 0xFD, /* DTLSv1.2 */
152*e0c4386eSCy Schubert     0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90,
153*e0c4386eSCy Schubert     0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56,
154*e0c4386eSCy Schubert     0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */
155*e0c4386eSCy Schubert     0x00, /* Session id len */
156*e0c4386eSCy Schubert     0x14, /* Cookie len */
157*e0c4386eSCy Schubert     0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
158*e0c4386eSCy Schubert     0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13 /* Cookie */
159*e0c4386eSCy Schubert };
160*e0c4386eSCy Schubert 
161*e0c4386eSCy Schubert 
162*e0c4386eSCy Schubert /* A ClientHello with a bad cookie */
163*e0c4386eSCy Schubert static const unsigned char clienthello_badcookie[] = {
164*e0c4386eSCy Schubert     0x16, /* Handshake */
165*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
166*e0c4386eSCy Schubert     0x00, 0x00, /* Epoch */
167*e0c4386eSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */
168*e0c4386eSCy Schubert     0x00, 0x4E, /* Record Length */
169*e0c4386eSCy Schubert     0x01, /* ClientHello */
170*e0c4386eSCy Schubert     0x00, 0x00, 0x42, /* Message length */
171*e0c4386eSCy Schubert     0x00, 0x00, /* Message sequence */
172*e0c4386eSCy Schubert     0x00, 0x00, 0x00, /* Fragment offset */
173*e0c4386eSCy Schubert     0x00, 0x00, 0x42, /* Fragment length */
174*e0c4386eSCy Schubert     0xFE, 0xFD, /* DTLSv1.2 */
175*e0c4386eSCy Schubert     0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90,
176*e0c4386eSCy Schubert     0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56,
177*e0c4386eSCy Schubert     0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */
178*e0c4386eSCy Schubert     0x00, /* Session id len */
179*e0c4386eSCy Schubert     0x14, /* Cookie len */
180*e0c4386eSCy Schubert     0x01, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
181*e0c4386eSCy Schubert     0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, /* Cookie */
182*e0c4386eSCy Schubert     0x00, 0x04, /* Ciphersuites len */
183*e0c4386eSCy Schubert     0x00, 0x2f, /* AES128-SHA */
184*e0c4386eSCy Schubert     0x00, 0xff, /* Empty reneg info SCSV */
185*e0c4386eSCy Schubert     0x01, /* Compression methods len */
186*e0c4386eSCy Schubert     0x00, /* Null compression */
187*e0c4386eSCy Schubert     0x00, 0x00 /* Extensions len */
188*e0c4386eSCy Schubert };
189*e0c4386eSCy Schubert 
190*e0c4386eSCy Schubert /* A fragmented ClientHello with the fragment boundary mid cookie */
191*e0c4386eSCy Schubert static const unsigned char clienthello_cookie_short[] = {
192*e0c4386eSCy Schubert     0x16, /* Handshake */
193*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
194*e0c4386eSCy Schubert     0x00, 0x00, /* Epoch */
195*e0c4386eSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */
196*e0c4386eSCy Schubert     0x00, 0x43, /* Record Length */
197*e0c4386eSCy Schubert     0x01, /* ClientHello */
198*e0c4386eSCy Schubert     0x00, 0x00, 0x42, /* Message length */
199*e0c4386eSCy Schubert     0x00, 0x00, /* Message sequence */
200*e0c4386eSCy Schubert     0x00, 0x00, 0x00, /* Fragment offset */
201*e0c4386eSCy Schubert     0x00, 0x00, 0x37, /* Fragment length */
202*e0c4386eSCy Schubert     0xFE, 0xFD, /* DTLSv1.2 */
203*e0c4386eSCy Schubert     0xCA, 0x18, 0x9F, 0x76, 0xEC, 0x57, 0xCE, 0xE5, 0xB3, 0xAB, 0x79, 0x90,
204*e0c4386eSCy Schubert     0xAD, 0xAC, 0x6E, 0xD1, 0x58, 0x35, 0x03, 0x97, 0x16, 0x10, 0x82, 0x56,
205*e0c4386eSCy Schubert     0xD8, 0x55, 0xFF, 0xE1, 0x8A, 0xA3, 0x2E, 0xF6, /* Random */
206*e0c4386eSCy Schubert     0x00, /* Session id len */
207*e0c4386eSCy Schubert     0x14, /* Cookie len */
208*e0c4386eSCy Schubert     0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
209*e0c4386eSCy Schubert     0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12 /* Cookie */
210*e0c4386eSCy Schubert };
211*e0c4386eSCy Schubert 
212*e0c4386eSCy Schubert /* Bad record - too short */
213*e0c4386eSCy Schubert static const unsigned char record_short[] = {
214*e0c4386eSCy Schubert     0x16, /* Handshake */
215*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
216*e0c4386eSCy Schubert     0x00, 0x00, /* Epoch */
217*e0c4386eSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* Record sequence number */
218*e0c4386eSCy Schubert };
219*e0c4386eSCy Schubert 
220*e0c4386eSCy Schubert static const unsigned char verify[] = {
221*e0c4386eSCy Schubert     0x16, /* Handshake */
222*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
223*e0c4386eSCy Schubert     0x00, 0x00, /* Epoch */
224*e0c4386eSCy Schubert     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Record sequence number */
225*e0c4386eSCy Schubert     0x00, 0x23, /* Record Length */
226*e0c4386eSCy Schubert     0x03, /* HelloVerifyRequest */
227*e0c4386eSCy Schubert     0x00, 0x00, 0x17, /* Message length */
228*e0c4386eSCy Schubert     0x00, 0x00, /* Message sequence */
229*e0c4386eSCy Schubert     0x00, 0x00, 0x00, /* Fragment offset */
230*e0c4386eSCy Schubert     0x00, 0x00, 0x17, /* Fragment length */
231*e0c4386eSCy Schubert     0xFE, 0xFF, /* DTLSv1.0 */
232*e0c4386eSCy Schubert     0x14, /* Cookie len */
233*e0c4386eSCy Schubert     0x00, 0x01, 0x02, 0x03, 0x04, 005, 0x06, 007, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
234*e0c4386eSCy Schubert     0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13 /* Cookie */
235*e0c4386eSCy Schubert };
236*e0c4386eSCy Schubert 
237*e0c4386eSCy Schubert typedef struct {
238*e0c4386eSCy Schubert     const unsigned char *in;
239*e0c4386eSCy Schubert     unsigned int inlen;
240*e0c4386eSCy Schubert     /*
241*e0c4386eSCy Schubert      * GOOD == positive return value from DTLSv1_listen, no output yet
242*e0c4386eSCy Schubert      * VERIFY == 0 return value, HelloVerifyRequest sent
243*e0c4386eSCy Schubert      * DROP == 0 return value, no output
244*e0c4386eSCy Schubert      */
245*e0c4386eSCy Schubert     enum {GOOD, VERIFY, DROP} outtype;
246*e0c4386eSCy Schubert } tests;
247*e0c4386eSCy Schubert 
248*e0c4386eSCy Schubert static tests testpackets[9] = {
249*e0c4386eSCy Schubert     { clienthello_nocookie, sizeof(clienthello_nocookie), VERIFY },
250*e0c4386eSCy Schubert     { clienthello_nocookie_frag, sizeof(clienthello_nocookie_frag), VERIFY },
251*e0c4386eSCy Schubert     { clienthello_nocookie_short, sizeof(clienthello_nocookie_short), DROP },
252*e0c4386eSCy Schubert     { clienthello_2ndfrag, sizeof(clienthello_2ndfrag), DROP },
253*e0c4386eSCy Schubert     { clienthello_cookie, sizeof(clienthello_cookie), GOOD },
254*e0c4386eSCy Schubert     { clienthello_cookie_frag, sizeof(clienthello_cookie_frag), GOOD },
255*e0c4386eSCy Schubert     { clienthello_badcookie, sizeof(clienthello_badcookie), VERIFY },
256*e0c4386eSCy Schubert     { clienthello_cookie_short, sizeof(clienthello_cookie_short), DROP },
257*e0c4386eSCy Schubert     { record_short, sizeof(record_short), DROP }
258*e0c4386eSCy Schubert };
259*e0c4386eSCy Schubert 
260*e0c4386eSCy Schubert # define COOKIE_LEN  20
261*e0c4386eSCy Schubert 
cookie_gen(SSL * ssl,unsigned char * cookie,unsigned int * cookie_len)262*e0c4386eSCy Schubert static int cookie_gen(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len)
263*e0c4386eSCy Schubert {
264*e0c4386eSCy Schubert     unsigned int i;
265*e0c4386eSCy Schubert 
266*e0c4386eSCy Schubert     for (i = 0; i < COOKIE_LEN; i++, cookie++)
267*e0c4386eSCy Schubert         *cookie = i;
268*e0c4386eSCy Schubert     *cookie_len = COOKIE_LEN;
269*e0c4386eSCy Schubert 
270*e0c4386eSCy Schubert     return 1;
271*e0c4386eSCy Schubert }
272*e0c4386eSCy Schubert 
cookie_verify(SSL * ssl,const unsigned char * cookie,unsigned int cookie_len)273*e0c4386eSCy Schubert static int cookie_verify(SSL *ssl, const unsigned char *cookie,
274*e0c4386eSCy Schubert                          unsigned int cookie_len)
275*e0c4386eSCy Schubert {
276*e0c4386eSCy Schubert     unsigned int i;
277*e0c4386eSCy Schubert 
278*e0c4386eSCy Schubert     if (cookie_len != COOKIE_LEN)
279*e0c4386eSCy Schubert         return 0;
280*e0c4386eSCy Schubert 
281*e0c4386eSCy Schubert     for (i = 0; i < COOKIE_LEN; i++, cookie++) {
282*e0c4386eSCy Schubert         if (*cookie != i)
283*e0c4386eSCy Schubert             return 0;
284*e0c4386eSCy Schubert     }
285*e0c4386eSCy Schubert 
286*e0c4386eSCy Schubert     return 1;
287*e0c4386eSCy Schubert }
288*e0c4386eSCy Schubert 
dtls_listen_test(int i)289*e0c4386eSCy Schubert static int dtls_listen_test(int i)
290*e0c4386eSCy Schubert {
291*e0c4386eSCy Schubert     SSL_CTX *ctx = NULL;
292*e0c4386eSCy Schubert     SSL *ssl = NULL;
293*e0c4386eSCy Schubert     BIO *outbio = NULL;
294*e0c4386eSCy Schubert     BIO *inbio = NULL;
295*e0c4386eSCy Schubert     BIO_ADDR *peer = NULL;
296*e0c4386eSCy Schubert     tests *tp = &testpackets[i];
297*e0c4386eSCy Schubert     char *data;
298*e0c4386eSCy Schubert     long datalen;
299*e0c4386eSCy Schubert     int ret, success = 0;
300*e0c4386eSCy Schubert 
301*e0c4386eSCy Schubert     if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_server_method()))
302*e0c4386eSCy Schubert             || !TEST_ptr(peer = BIO_ADDR_new()))
303*e0c4386eSCy Schubert         goto err;
304*e0c4386eSCy Schubert     SSL_CTX_set_cookie_generate_cb(ctx, cookie_gen);
305*e0c4386eSCy Schubert     SSL_CTX_set_cookie_verify_cb(ctx, cookie_verify);
306*e0c4386eSCy Schubert 
307*e0c4386eSCy Schubert     /* Create an SSL object and set the BIO */
308*e0c4386eSCy Schubert     if (!TEST_ptr(ssl = SSL_new(ctx))
309*e0c4386eSCy Schubert             || !TEST_ptr(outbio = BIO_new(BIO_s_mem())))
310*e0c4386eSCy Schubert         goto err;
311*e0c4386eSCy Schubert     SSL_set0_wbio(ssl, outbio);
312*e0c4386eSCy Schubert 
313*e0c4386eSCy Schubert     /* Set Non-blocking IO behaviour */
314*e0c4386eSCy Schubert     if (!TEST_ptr(inbio = BIO_new_mem_buf((char *)tp->in, tp->inlen)))
315*e0c4386eSCy Schubert         goto err;
316*e0c4386eSCy Schubert     BIO_set_mem_eof_return(inbio, -1);
317*e0c4386eSCy Schubert     SSL_set0_rbio(ssl, inbio);
318*e0c4386eSCy Schubert 
319*e0c4386eSCy Schubert     /* Process the incoming packet */
320*e0c4386eSCy Schubert     if (!TEST_int_ge(ret = DTLSv1_listen(ssl, peer), 0))
321*e0c4386eSCy Schubert         goto err;
322*e0c4386eSCy Schubert     datalen = BIO_get_mem_data(outbio, &data);
323*e0c4386eSCy Schubert 
324*e0c4386eSCy Schubert     if (tp->outtype == VERIFY) {
325*e0c4386eSCy Schubert         if (!TEST_int_eq(ret, 0)
326*e0c4386eSCy Schubert                 || !TEST_mem_eq(data, datalen, verify, sizeof(verify)))
327*e0c4386eSCy Schubert             goto err;
328*e0c4386eSCy Schubert     } else if (datalen == 0) {
329*e0c4386eSCy Schubert         if (!TEST_true((ret == 0 && tp->outtype == DROP)
330*e0c4386eSCy Schubert                 || (ret == 1 && tp->outtype == GOOD)))
331*e0c4386eSCy Schubert             goto err;
332*e0c4386eSCy Schubert     } else {
333*e0c4386eSCy Schubert         TEST_info("Test %d: unexpected data output", i);
334*e0c4386eSCy Schubert         goto err;
335*e0c4386eSCy Schubert     }
336*e0c4386eSCy Schubert     (void)BIO_reset(outbio);
337*e0c4386eSCy Schubert     inbio = NULL;
338*e0c4386eSCy Schubert     SSL_set0_rbio(ssl, NULL);
339*e0c4386eSCy Schubert     success = 1;
340*e0c4386eSCy Schubert 
341*e0c4386eSCy Schubert  err:
342*e0c4386eSCy Schubert     /* Also frees up outbio */
343*e0c4386eSCy Schubert     SSL_free(ssl);
344*e0c4386eSCy Schubert     SSL_CTX_free(ctx);
345*e0c4386eSCy Schubert     BIO_free(inbio);
346*e0c4386eSCy Schubert     OPENSSL_free(peer);
347*e0c4386eSCy Schubert     return success;
348*e0c4386eSCy Schubert }
349*e0c4386eSCy Schubert #endif
350*e0c4386eSCy Schubert 
setup_tests(void)351*e0c4386eSCy Schubert int setup_tests(void)
352*e0c4386eSCy Schubert {
353*e0c4386eSCy Schubert #ifndef OPENSSL_NO_SOCK
354*e0c4386eSCy Schubert     ADD_ALL_TESTS(dtls_listen_test, (int)OSSL_NELEM(testpackets));
355*e0c4386eSCy Schubert #endif
356*e0c4386eSCy Schubert     return 1;
357*e0c4386eSCy Schubert }
358