1 /*
2 * Simple DTLS server demonstration program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 */
46
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_printf printf
59 #define mbedtls_fprintf fprintf
60 #define mbedtls_time_t time_t
61 #define mbedtls_exit exit
62 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
63 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
64 #endif
65
66 /* Uncomment out the following line to default to IPv4 and disable IPv6 */
67 //#define FORCE_IPV4
68
69 #ifdef FORCE_IPV4
70 #define BIND_IP "0.0.0.0" /* Forces IPv4 */
71 #else
72 #define BIND_IP "::"
73 #endif
74
75 #if !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
76 !defined(MBEDTLS_SSL_COOKIE_C) || !defined(MBEDTLS_NET_C) || \
77 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
78 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
79 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
80 !defined(MBEDTLS_TIMING_C)
81
main(void)82 int main( void )
83 {
84 printf( "MBEDTLS_SSL_SRV_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
85 "MBEDTLS_SSL_COOKIE_C and/or MBEDTLS_NET_C and/or "
86 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
87 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
88 "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C and/or "
89 "MBEDTLS_TIMING_C not defined.\n" );
90 mbedtls_exit( 0 );
91 }
92 #else
93
94 #if defined(_WIN32)
95 #include <windows.h>
96 #endif
97
98 #include <string.h>
99 #include <stdlib.h>
100 #include <stdio.h>
101
102 #include "mbedtls/entropy.h"
103 #include "mbedtls/ctr_drbg.h"
104 #include "mbedtls/certs.h"
105 #include "mbedtls/x509.h"
106 #include "mbedtls/ssl.h"
107 #include "mbedtls/ssl_cookie.h"
108 #include "mbedtls/net_sockets.h"
109 #include "mbedtls/error.h"
110 #include "mbedtls/debug.h"
111 #include "mbedtls/timing.h"
112
113 #if defined(MBEDTLS_SSL_CACHE_C)
114 #include "mbedtls/ssl_cache.h"
115 #endif
116
117 #define READ_TIMEOUT_MS 10000 /* 5 seconds */
118 #define DEBUG_LEVEL 0
119
120
my_debug(void * ctx,int level,const char * file,int line,const char * str)121 static void my_debug( void *ctx, int level,
122 const char *file, int line,
123 const char *str )
124 {
125 ((void) level);
126
127 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
128 fflush( (FILE *) ctx );
129 }
130
main(void)131 int main( void )
132 {
133 int ret, len;
134 mbedtls_net_context listen_fd, client_fd;
135 unsigned char buf[1024];
136 const char *pers = "dtls_server";
137 unsigned char client_ip[16] = { 0 };
138 size_t cliip_len;
139 mbedtls_ssl_cookie_ctx cookie_ctx;
140
141 mbedtls_entropy_context entropy;
142 mbedtls_ctr_drbg_context ctr_drbg;
143 mbedtls_ssl_context ssl;
144 mbedtls_ssl_config conf;
145 mbedtls_x509_crt srvcert;
146 mbedtls_pk_context pkey;
147 mbedtls_timing_delay_context timer;
148 #if defined(MBEDTLS_SSL_CACHE_C)
149 mbedtls_ssl_cache_context cache;
150 #endif
151
152 mbedtls_net_init( &listen_fd );
153 mbedtls_net_init( &client_fd );
154 mbedtls_ssl_init( &ssl );
155 mbedtls_ssl_config_init( &conf );
156 mbedtls_ssl_cookie_init( &cookie_ctx );
157 #if defined(MBEDTLS_SSL_CACHE_C)
158 mbedtls_ssl_cache_init( &cache );
159 #endif
160 mbedtls_x509_crt_init( &srvcert );
161 mbedtls_pk_init( &pkey );
162 mbedtls_entropy_init( &entropy );
163 mbedtls_ctr_drbg_init( &ctr_drbg );
164
165 #if defined(MBEDTLS_DEBUG_C)
166 mbedtls_debug_set_threshold( DEBUG_LEVEL );
167 #endif
168
169 /*
170 * 1. Load the certificates and private RSA key
171 */
172 printf( "\n . Loading the server cert. and key..." );
173 fflush( stdout );
174
175 /*
176 * This demonstration program uses embedded test certificates.
177 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
178 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
179 */
180 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
181 mbedtls_test_srv_crt_len );
182 if( ret != 0 )
183 {
184 printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
185 goto exit;
186 }
187
188 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
189 mbedtls_test_cas_pem_len );
190 if( ret != 0 )
191 {
192 printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
193 goto exit;
194 }
195
196 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
197 mbedtls_test_srv_key_len, NULL, 0 );
198 if( ret != 0 )
199 {
200 printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret );
201 goto exit;
202 }
203
204 printf( " ok\n" );
205
206 /*
207 * 2. Setup the "listening" UDP socket
208 */
209 printf( " . Bind on udp/*/4433 ..." );
210 fflush( stdout );
211
212 if( ( ret = mbedtls_net_bind( &listen_fd, BIND_IP, "4433", MBEDTLS_NET_PROTO_UDP ) ) != 0 )
213 {
214 printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
215 goto exit;
216 }
217
218 printf( " ok\n" );
219
220 /*
221 * 3. Seed the RNG
222 */
223 printf( " . Seeding the random number generator..." );
224 fflush( stdout );
225
226 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
227 (const unsigned char *) pers,
228 strlen( pers ) ) ) != 0 )
229 {
230 printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
231 goto exit;
232 }
233
234 printf( " ok\n" );
235
236 /*
237 * 4. Setup stuff
238 */
239 printf( " . Setting up the DTLS data..." );
240 fflush( stdout );
241
242 if( ( ret = mbedtls_ssl_config_defaults( &conf,
243 MBEDTLS_SSL_IS_SERVER,
244 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
245 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
246 {
247 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
248 goto exit;
249 }
250
251 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
252 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
253
254 #if defined(MBEDTLS_SSL_CACHE_C)
255 mbedtls_ssl_conf_session_cache( &conf, &cache,
256 mbedtls_ssl_cache_get,
257 mbedtls_ssl_cache_set );
258 #endif
259
260 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
261 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
262 {
263 printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
264 goto exit;
265 }
266
267 if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
268 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
269 {
270 printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
271 goto exit;
272 }
273
274 mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
275 &cookie_ctx );
276
277 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
278 {
279 printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
280 goto exit;
281 }
282
283 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
284 mbedtls_timing_get_delay );
285
286 printf( " ok\n" );
287
288 reset:
289 #ifdef MBEDTLS_ERROR_C
290 if( ret != 0 )
291 {
292 char error_buf[100];
293 mbedtls_strerror( ret, error_buf, 100 );
294 printf("Last error was: %d - %s\n\n", ret, error_buf );
295 }
296 #endif
297
298 mbedtls_net_free( &client_fd );
299
300 mbedtls_ssl_session_reset( &ssl );
301
302 /*
303 * 3. Wait until a client connects
304 */
305 printf( " . Waiting for a remote connection ..." );
306 fflush( stdout );
307
308 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
309 client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
310 {
311 printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
312 goto exit;
313 }
314
315 /* For HelloVerifyRequest cookies */
316 if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
317 client_ip, cliip_len ) ) != 0 )
318 {
319 printf( " failed\n ! "
320 "mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n", -ret );
321 goto exit;
322 }
323
324 mbedtls_ssl_set_bio( &ssl, &client_fd,
325 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
326
327 printf( " ok\n" );
328
329 /*
330 * 5. Handshake
331 */
332 printf( " . Performing the DTLS handshake..." );
333 fflush( stdout );
334
335 do ret = mbedtls_ssl_handshake( &ssl );
336 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
337 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
338
339 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
340 {
341 printf( " hello verification requested\n" );
342 ret = 0;
343 goto reset;
344 }
345 else if( ret != 0 )
346 {
347 printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
348 goto reset;
349 }
350
351 printf( " ok\n" );
352
353 /*
354 * 6. Read the echo Request
355 */
356 printf( " < Read from client:" );
357 fflush( stdout );
358
359 len = sizeof( buf ) - 1;
360 memset( buf, 0, sizeof( buf ) );
361
362 do ret = mbedtls_ssl_read( &ssl, buf, len );
363 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
364 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
365
366 if( ret <= 0 )
367 {
368 switch( ret )
369 {
370 case MBEDTLS_ERR_SSL_TIMEOUT:
371 printf( " timeout\n\n" );
372 goto reset;
373
374 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
375 printf( " connection was closed gracefully\n" );
376 ret = 0;
377 goto close_notify;
378
379 default:
380 printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
381 goto reset;
382 }
383 }
384
385 len = ret;
386 printf( " %d bytes read\n\n%s\n\n", len, buf );
387
388 /*
389 * 7. Write the 200 Response
390 */
391 printf( " > Write to client:" );
392 fflush( stdout );
393
394 do ret = mbedtls_ssl_write( &ssl, buf, len );
395 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
396 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
397
398 if( ret < 0 )
399 {
400 printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
401 goto exit;
402 }
403
404 len = ret;
405 printf( " %d bytes written\n\n%s\n\n", len, buf );
406
407 /*
408 * 8. Done, cleanly close the connection
409 */
410 close_notify:
411 printf( " . Closing the connection..." );
412
413 /* No error checking, the connection might be closed already */
414 do ret = mbedtls_ssl_close_notify( &ssl );
415 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
416 ret = 0;
417
418 printf( " done\n" );
419
420 goto reset;
421
422 /*
423 * Final clean-ups and exit
424 */
425 exit:
426
427 #ifdef MBEDTLS_ERROR_C
428 if( ret != 0 )
429 {
430 char error_buf[100];
431 mbedtls_strerror( ret, error_buf, 100 );
432 printf( "Last error was: %d - %s\n\n", ret, error_buf );
433 }
434 #endif
435
436 mbedtls_net_free( &client_fd );
437 mbedtls_net_free( &listen_fd );
438
439 mbedtls_x509_crt_free( &srvcert );
440 mbedtls_pk_free( &pkey );
441 mbedtls_ssl_free( &ssl );
442 mbedtls_ssl_config_free( &conf );
443 mbedtls_ssl_cookie_free( &cookie_ctx );
444 #if defined(MBEDTLS_SSL_CACHE_C)
445 mbedtls_ssl_cache_free( &cache );
446 #endif
447 mbedtls_ctr_drbg_free( &ctr_drbg );
448 mbedtls_entropy_free( &entropy );
449
450 #if defined(_WIN32)
451 printf( " Press Enter to exit this program.\n" );
452 fflush( stdout ); getchar();
453 #endif
454
455 /* Shell can not handle large exit numbers -> 1 for errors */
456 if( ret < 0 )
457 ret = 1;
458
459 mbedtls_exit( ret );
460 }
461 #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS &&
462 MBEDTLS_SSL_COOKIE_C && MBEDTLS_NET_C && MBEDTLS_ENTROPY_C &&
463 MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C
464 && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_TIMING_C */
465