2 * Simple DTLS client demonstration program
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
32 #define mbedtls_printf printf
33 #define mbedtls_fprintf fprintf
34 #define mbedtls_exit exit
35 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
36 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
39 #if !defined(MBEDTLS_SSL_CLI_C) || !defined(MBEDTLS_SSL_PROTO_DTLS) || \
40 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_TIMING_C) || \
41 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
42 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_RSA_C) || \
43 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C)
46 mbedtls_printf( "MBEDTLS_SSL_CLI_C and/or MBEDTLS_SSL_PROTO_DTLS and/or "
47 "MBEDTLS_NET_C and/or MBEDTLS_TIMING_C and/or "
48 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
49 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_RSA_C and/or "
50 "MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.\n" );
57 #include "mbedtls/net_sockets.h"
58 #include "mbedtls/debug.h"
59 #include "mbedtls/ssl.h"
60 #include "mbedtls/entropy.h"
61 #include "mbedtls/ctr_drbg.h"
62 #include "mbedtls/error.h"
63 #include "mbedtls/certs.h"
64 #include "mbedtls/timing.h"
66 /* Uncomment out the following line to default to IPv4 and disable IPv6 */
69 #define SERVER_PORT "4433"
70 #define SERVER_NAME "localhost"
73 #define SERVER_ADDR "127.0.0.1" /* Forces IPv4 */
75 #define SERVER_ADDR "::1"
78 #define MESSAGE "Echo this"
80 #define READ_TIMEOUT_MS 1000
85 #if defined(MBEDTLS_CHECK_PARAMS)
86 #include "mbedtls/platform_util.h"
87 void mbedtls_param_failed( const char *failure_condition,
91 mbedtls_printf( "%s:%i: Input param failed - %s\n",
92 file, line, failure_condition );
93 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
97 static void my_debug( void *ctx, int level,
98 const char *file, int line,
103 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
104 fflush( (FILE *) ctx );
107 int main( int argc, char *argv[] )
110 mbedtls_net_context server_fd;
112 unsigned char buf[1024];
113 const char *pers = "dtls_client";
114 int retry_left = MAX_RETRY;
116 mbedtls_entropy_context entropy;
117 mbedtls_ctr_drbg_context ctr_drbg;
118 mbedtls_ssl_context ssl;
119 mbedtls_ssl_config conf;
120 mbedtls_x509_crt cacert;
121 mbedtls_timing_delay_context timer;
126 #if defined(MBEDTLS_DEBUG_C)
127 mbedtls_debug_set_threshold( DEBUG_LEVEL );
131 * 0. Initialize the RNG and the session data
133 mbedtls_net_init( &server_fd );
134 mbedtls_ssl_init( &ssl );
135 mbedtls_ssl_config_init( &conf );
136 mbedtls_x509_crt_init( &cacert );
137 mbedtls_ctr_drbg_init( &ctr_drbg );
139 mbedtls_printf( "\n . Seeding the random number generator..." );
142 mbedtls_entropy_init( &entropy );
143 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
144 (const unsigned char *) pers,
145 strlen( pers ) ) ) != 0 )
147 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
151 mbedtls_printf( " ok\n" );
154 * 0. Load certificates
156 mbedtls_printf( " . Loading the CA root certificate ..." );
159 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
160 mbedtls_test_cas_pem_len );
163 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
167 mbedtls_printf( " ok (%d skipped)\n", ret );
170 * 1. Start the connection
172 mbedtls_printf( " . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT );
175 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_ADDR,
176 SERVER_PORT, MBEDTLS_NET_PROTO_UDP ) ) != 0 )
178 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
182 mbedtls_printf( " ok\n" );
187 mbedtls_printf( " . Setting up the DTLS structure..." );
190 if( ( ret = mbedtls_ssl_config_defaults( &conf,
191 MBEDTLS_SSL_IS_CLIENT,
192 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
193 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
195 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
199 /* OPTIONAL is usually a bad choice for security, but makes interop easier
200 * in this simplified example, in which the ca chain is hardcoded.
201 * Production code should set a proper ca chain and use REQUIRED. */
202 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
203 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
204 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
205 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
207 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
209 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
213 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
215 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
219 mbedtls_ssl_set_bio( &ssl, &server_fd,
220 mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout );
222 mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
223 mbedtls_timing_get_delay );
225 mbedtls_printf( " ok\n" );
230 mbedtls_printf( " . Performing the DTLS handshake..." );
233 do ret = mbedtls_ssl_handshake( &ssl );
234 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
235 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
239 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
243 mbedtls_printf( " ok\n" );
246 * 5. Verify the server certificate
248 mbedtls_printf( " . Verifying peer X.509 certificate..." );
250 /* In real life, we would have used MBEDTLS_SSL_VERIFY_REQUIRED so that the
251 * handshake would not succeed if the peer's cert is bad. Even if we used
252 * MBEDTLS_SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
253 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
257 mbedtls_printf( " failed\n" );
259 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
261 mbedtls_printf( "%s\n", vrfy_buf );
264 mbedtls_printf( " ok\n" );
267 * 6. Write the echo request
270 mbedtls_printf( " > Write to server:" );
273 len = sizeof( MESSAGE ) - 1;
275 do ret = mbedtls_ssl_write( &ssl, (unsigned char *) MESSAGE, len );
276 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
277 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
281 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
286 mbedtls_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
289 * 7. Read the echo response
291 mbedtls_printf( " < Read from server:" );
294 len = sizeof( buf ) - 1;
295 memset( buf, 0, sizeof( buf ) );
297 do ret = mbedtls_ssl_read( &ssl, buf, len );
298 while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
299 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
305 case MBEDTLS_ERR_SSL_TIMEOUT:
306 mbedtls_printf( " timeout\n\n" );
307 if( retry_left-- > 0 )
311 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
312 mbedtls_printf( " connection was closed gracefully\n" );
317 mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n\n", -ret );
323 mbedtls_printf( " %d bytes read\n\n%s\n\n", len, buf );
326 * 8. Done, cleanly close the connection
329 mbedtls_printf( " . Closing the connection..." );
331 /* No error checking, the connection might be closed already */
332 do ret = mbedtls_ssl_close_notify( &ssl );
333 while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
336 mbedtls_printf( " done\n" );
339 * 9. Final clean-ups and exit
343 #ifdef MBEDTLS_ERROR_C
347 mbedtls_strerror( ret, error_buf, 100 );
348 mbedtls_printf( "Last error was: %d - %s\n\n", ret, error_buf );
352 mbedtls_net_free( &server_fd );
354 mbedtls_x509_crt_free( &cacert );
355 mbedtls_ssl_free( &ssl );
356 mbedtls_ssl_config_free( &conf );
357 mbedtls_ctr_drbg_free( &ctr_drbg );
358 mbedtls_entropy_free( &entropy );
361 mbedtls_printf( " + Press Enter to exit this program.\n" );
362 fflush( stdout ); getchar();
365 /* Shell can not handle large exit numbers -> 1 for errors */
371 #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_NET_C &&
372 MBEDTLD_TIMING_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
373 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_RSA_C && MBEDTLS_CERTS_C &&
374 MBEDTLS_PEM_PARSE_C */