• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   /* 10 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     mbedtls_ssl_conf_read_timeout( &conf, READ_TIMEOUT_MS );
254 
255 #if defined(MBEDTLS_SSL_CACHE_C)
256     mbedtls_ssl_conf_session_cache( &conf, &cache,
257                                    mbedtls_ssl_cache_get,
258                                    mbedtls_ssl_cache_set );
259 #endif
260 
261     mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
262    if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
263     {
264         printf( " failed\n  ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
265         goto exit;
266     }
267 
268     if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
269                                   mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
270     {
271         printf( " failed\n  ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
272         goto exit;
273     }
274 
275     mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
276                                &cookie_ctx );
277 
278     if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
279     {
280         printf( " failed\n  ! mbedtls_ssl_setup returned %d\n\n", ret );
281         goto exit;
282     }
283 
284     mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
285                                             mbedtls_timing_get_delay );
286 
287     printf( " ok\n" );
288 
289 reset:
290 #ifdef MBEDTLS_ERROR_C
291     if( ret != 0 )
292     {
293         char error_buf[100];
294         mbedtls_strerror( ret, error_buf, 100 );
295         printf("Last error was: %d - %s\n\n", ret, error_buf );
296     }
297 #endif
298 
299     mbedtls_net_free( &client_fd );
300 
301     mbedtls_ssl_session_reset( &ssl );
302 
303     /*
304      * 3. Wait until a client connects
305      */
306     printf( "  . Waiting for a remote connection ..." );
307     fflush( stdout );
308 
309     if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
310                     client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
311     {
312         printf( " failed\n  ! mbedtls_net_accept returned %d\n\n", ret );
313         goto exit;
314     }
315 
316     /* For HelloVerifyRequest cookies */
317     if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
318                     client_ip, cliip_len ) ) != 0 )
319     {
320         printf( " failed\n  ! "
321                 "mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n", -ret );
322         goto exit;
323     }
324 
325     mbedtls_ssl_set_bio( &ssl, &client_fd,
326                          mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
327 
328     printf( " ok\n" );
329 
330     /*
331      * 5. Handshake
332      */
333     printf( "  . Performing the DTLS handshake..." );
334     fflush( stdout );
335 
336     do ret = mbedtls_ssl_handshake( &ssl );
337     while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
338            ret == MBEDTLS_ERR_SSL_WANT_WRITE );
339 
340     if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
341     {
342         printf( " hello verification requested\n" );
343         ret = 0;
344         goto reset;
345     }
346     else if( ret != 0 )
347     {
348         printf( " failed\n  ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
349         goto reset;
350     }
351 
352     printf( " ok\n" );
353 
354     /*
355      * 6. Read the echo Request
356      */
357     printf( "  < Read from client:" );
358     fflush( stdout );
359 
360     len = sizeof( buf ) - 1;
361     memset( buf, 0, sizeof( buf ) );
362 
363     do ret = mbedtls_ssl_read( &ssl, buf, len );
364     while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
365            ret == MBEDTLS_ERR_SSL_WANT_WRITE );
366 
367     if( ret <= 0 )
368     {
369         switch( ret )
370         {
371             case MBEDTLS_ERR_SSL_TIMEOUT:
372                 printf( " timeout\n\n" );
373                 goto reset;
374 
375             case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
376                 printf( " connection was closed gracefully\n" );
377                 ret = 0;
378                 goto close_notify;
379 
380             default:
381                 printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
382                 goto reset;
383         }
384     }
385 
386     len = ret;
387     printf( " %d bytes read\n\n%s\n\n", len, buf );
388 
389     /*
390      * 7. Write the 200 Response
391      */
392     printf( "  > Write to client:" );
393     fflush( stdout );
394 
395     do ret = mbedtls_ssl_write( &ssl, buf, len );
396     while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
397            ret == MBEDTLS_ERR_SSL_WANT_WRITE );
398 
399     if( ret < 0 )
400     {
401         printf( " failed\n  ! mbedtls_ssl_write returned %d\n\n", ret );
402         goto exit;
403     }
404 
405     len = ret;
406     printf( " %d bytes written\n\n%s\n\n", len, buf );
407 
408     /*
409      * 8. Done, cleanly close the connection
410      */
411 close_notify:
412     printf( "  . Closing the connection..." );
413 
414     /* No error checking, the connection might be closed already */
415     do ret = mbedtls_ssl_close_notify( &ssl );
416     while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
417     ret = 0;
418 
419     printf( " done\n" );
420 
421     goto reset;
422 
423     /*
424      * Final clean-ups and exit
425      */
426 exit:
427 
428 #ifdef MBEDTLS_ERROR_C
429     if( ret != 0 )
430     {
431         char error_buf[100];
432         mbedtls_strerror( ret, error_buf, 100 );
433         printf( "Last error was: %d - %s\n\n", ret, error_buf );
434     }
435 #endif
436 
437     mbedtls_net_free( &client_fd );
438     mbedtls_net_free( &listen_fd );
439 
440     mbedtls_x509_crt_free( &srvcert );
441     mbedtls_pk_free( &pkey );
442     mbedtls_ssl_free( &ssl );
443     mbedtls_ssl_config_free( &conf );
444     mbedtls_ssl_cookie_free( &cookie_ctx );
445 #if defined(MBEDTLS_SSL_CACHE_C)
446     mbedtls_ssl_cache_free( &cache );
447 #endif
448     mbedtls_ctr_drbg_free( &ctr_drbg );
449     mbedtls_entropy_free( &entropy );
450 
451 #if defined(_WIN32)
452     printf( "  Press Enter to exit this program.\n" );
453     fflush( stdout ); getchar();
454 #endif
455 
456     /* Shell can not handle large exit numbers -> 1 for errors */
457     if( ret < 0 )
458         ret = 1;
459 
460     mbedtls_exit( ret );
461 }
462 #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_DTLS &&
463           MBEDTLS_SSL_COOKIE_C && MBEDTLS_NET_C && MBEDTLS_ENTROPY_C &&
464           MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C
465           && MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_TIMING_C */
466