blob: 4a1961858aea82008c8d83a7b07646a8a3a1ca1c [file] [log] [blame]
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02001/*
2 * Simple DTLS client demonstration program
3 *
4 * Copyright (C) 2014, Brainspark B.V.
5 *
Manuel Pégourié-Gonnarde4d48902015-03-06 13:40:52 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
28
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000029#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000031#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#endif
35
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020036#if !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_SSL_PROTO_DTLS) || \
37 !defined(POLARSSL_NET_C) || \
38 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
39 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_RSA_C) || \
40 !defined(POLARSSL_CERTS_C)
41
42#include <stdio.h>
43int main( int argc, char *argv[] )
44{
45 ((void) argc);
46 ((void) argv);
47
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000048 polarssl_printf( "POLARSSL_SSL_CLI_C and/or POLARSSL_SSL_PROTO_DTLS and/or "
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020049 "POLARSSL_NET_C and/or "
50 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
51 "POLARSSL_X509_CRT_PARSE_C and/or POLARSSL_RSA_C and/or "
52 "POLARSSL_CERTS_C not defined.\n" );
53 return( 0 );
54}
55#else
56
57#include <string.h>
58#include <stdio.h>
59
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/net.h"
61#include "mbedtls/debug.h"
62#include "mbedtls/ssl.h"
63#include "mbedtls/entropy.h"
64#include "mbedtls/ctr_drbg.h"
65#include "mbedtls/error.h"
66#include "mbedtls/certs.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020067
68#define SERVER_PORT 4433
69#define SERVER_NAME "localhost"
70#define SERVER_ADDR "127.0.0.1" /* forces IPv4 */
71#define MESSAGE "Echo this"
72
73#define READ_TIMEOUT_MS 1000
74#define MAX_RETRY 5
75
76#define DEBUG_LEVEL 0
77
78static void my_debug( void *ctx, int level, const char *str )
79{
80 ((void) level);
81
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000082 polarssl_fprintf( (FILE *) ctx, "%s", str );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020083 fflush( (FILE *) ctx );
84}
85
86int main( int argc, char *argv[] )
87{
88 int ret, len, server_fd = -1;
89 unsigned char buf[1024];
90 const char *pers = "dtls_client";
91 int retry_left = MAX_RETRY;
92
93 entropy_context entropy;
94 ctr_drbg_context ctr_drbg;
95 ssl_context ssl;
96 x509_crt cacert;
97
98 ((void) argc);
99 ((void) argv);
100
101#if defined(POLARSSL_DEBUG_C)
102 debug_set_threshold( DEBUG_LEVEL );
103#endif
104
105 /*
106 * 0. Initialize the RNG and the session data
107 */
108 memset( &ssl, 0, sizeof( ssl_context ) );
109 x509_crt_init( &cacert );
110
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000111 polarssl_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200112 fflush( stdout );
113
114 entropy_init( &entropy );
115 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
116 (const unsigned char *) pers,
117 strlen( pers ) ) ) != 0 )
118 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000119 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200120 goto exit;
121 }
122
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000123 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200124
125 /*
126 * 0. Initialize certificates
127 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000128 polarssl_printf( " . Loading the CA root certificate ..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200129 fflush( stdout );
130
131#if defined(POLARSSL_CERTS_C)
132 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
133 strlen( test_ca_list ) );
134#else
135 ret = 1;
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000136 polarssl_printf("POLARSSL_CERTS_C not defined.");
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200137#endif
138
139 if( ret < 0 )
140 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000141 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200142 goto exit;
143 }
144
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000145 polarssl_printf( " ok (%d skipped)\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200146
147 /*
148 * 1. Start the connection
149 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000150 polarssl_printf( " . Connecting to udp/%s/%4d...", SERVER_NAME,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200151 SERVER_PORT );
152 fflush( stdout );
153
154 if( ( ret = net_connect( &server_fd, SERVER_ADDR,
155 SERVER_PORT, NET_PROTO_UDP ) ) != 0 )
156 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000157 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200158 goto exit;
159 }
160
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000161 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200162
163 /*
164 * 2. Setup stuff
165 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000166 polarssl_printf( " . Setting up the DTLS structure..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200167 fflush( stdout );
168
169 if( ( ret = ssl_init( &ssl ) ) != 0 )
170 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000171 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200172 goto exit;
173 }
174
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000175 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200176
177 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
178 ssl_set_transport( &ssl, SSL_TRANSPORT_DATAGRAM );
179
180 /* OPTIONAL is usually a bad choice for security, but makes interop easier
181 * in this simplified example, in which the ca chain is hardcoded.
182 * Production code should set a proper ca chain and use REQUIRED. */
183 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
184 ssl_set_ca_chain( &ssl, &cacert, NULL, SERVER_NAME );
185
186 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
187 ssl_set_dbg( &ssl, my_debug, stdout );
188
189 ssl_set_bio_timeout( &ssl, &server_fd,
190 net_send, net_recv, net_recv_timeout,
191 READ_TIMEOUT_MS );
192
193 /*
194 * 4. Handshake
195 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000196 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200197 fflush( stdout );
198
199 do ret = ssl_handshake( &ssl );
200 while( ret == POLARSSL_ERR_NET_WANT_READ ||
201 ret == POLARSSL_ERR_NET_WANT_WRITE );
202
203 if( ret != 0 )
204 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000205 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200206 goto exit;
207 }
208
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000209 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200210
211 /*
212 * 5. Verify the server certificate
213 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000214 polarssl_printf( " . Verifying peer X.509 certificate..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200215
216 /* In real life, we would have used SSL_VERIFY_REQUIRED so that the
217 * handshake would not succeed if the peer's cert is bad. Even if we used
218 * SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
219 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
220 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000221 polarssl_printf( " failed\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200222
223 if( ( ret & BADCERT_EXPIRED ) != 0 )
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000224 polarssl_printf( " ! server certificate has expired\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200225
226 if( ( ret & BADCERT_REVOKED ) != 0 )
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000227 polarssl_printf( " ! server certificate has been revoked\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200228
229 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000230 polarssl_printf( " ! CN mismatch (expected CN=%s)\n", SERVER_NAME );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200231
232 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000233 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200234
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000235 polarssl_printf( "\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200236 }
237 else
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000238 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200239
240 /*
241 * 6. Write the echo request
242 */
243send_request:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000244 polarssl_printf( " > Write to server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200245 fflush( stdout );
246
247 len = sizeof( MESSAGE ) - 1;
248
249 do ret = ssl_write( &ssl, (unsigned char *) MESSAGE, len );
250 while( ret == POLARSSL_ERR_NET_WANT_READ ||
251 ret == POLARSSL_ERR_NET_WANT_WRITE );
252
253 if( ret < 0 )
254 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000255 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200256 goto exit;
257 }
258
259 len = ret;
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000260 polarssl_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200261
262 /*
263 * 7. Read the echo response
264 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000265 polarssl_printf( " < Read from server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200266 fflush( stdout );
267
268 len = sizeof( buf ) - 1;
269 memset( buf, 0, sizeof( buf ) );
270
271 do ret = ssl_read( &ssl, buf, len );
272 while( ret == POLARSSL_ERR_NET_WANT_READ ||
273 ret == POLARSSL_ERR_NET_WANT_WRITE );
274
275 if( ret <= 0 )
276 {
277 switch( ret )
278 {
279 case POLARSSL_ERR_NET_TIMEOUT:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000280 polarssl_printf( " timeout\n\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200281 if( retry_left-- > 0 )
282 goto send_request;
283 goto exit;
284
285 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000286 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200287 ret = 0;
288 goto close_notify;
289
290 default:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000291 polarssl_printf( " ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200292 goto exit;
293 }
294 }
295
296 len = ret;
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000297 polarssl_printf( " %d bytes read\n\n%s\n\n", len, buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200298
299 /*
300 * 8. Done, cleanly close the connection
301 */
302close_notify:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000303 polarssl_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200304
305 /* No error checking, the connection might be closed already */
306 do ret = ssl_close_notify( &ssl );
307 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
308 ret = 0;
309
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000310 polarssl_printf( " done\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200311
312 /*
313 * 9. Final clean-ups and exit
314 */
315exit:
316
317#ifdef POLARSSL_ERROR_C
318 if( ret != 0 )
319 {
320 char error_buf[100];
321 polarssl_strerror( ret, error_buf, 100 );
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000322 polarssl_printf( "Last error was: %d - %s\n\n", ret, error_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200323 }
324#endif
325
326 if( server_fd != -1 )
327 net_close( server_fd );
328
329 x509_crt_free( &cacert );
330 ssl_free( &ssl );
331 ctr_drbg_free( &ctr_drbg );
332 entropy_free( &entropy );
333
334#if defined(_WIN32)
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000335 polarssl_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200336 fflush( stdout ); getchar();
337#endif
338
339 /* Shell can not handle large exit numbers -> 1 for errors */
340 if( ret < 0 )
341 ret = 1;
342
343 return( ret );
344}
345#endif /* POLARSSL_SSL_CLI_C && POLARSSL_SSL_PROTO_DTLS && POLARSSL_NET_C &&
346 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
347 POLARSSL_X509_CRT_PARSE_C && POLARSSL_RSA_C && POLARSSL_CERTS_C */