blob: f9bf800857bf6ad34818e69d4a692cd28f3b82ee [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
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010032#include <stdio.h>
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000033#define polarssl_printf printf
34#define polarssl_fprintf fprintf
35#endif
36
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020037#if !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_SSL_PROTO_DTLS) || \
38 !defined(POLARSSL_NET_C) || \
39 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
40 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_RSA_C) || \
41 !defined(POLARSSL_CERTS_C)
42
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020043int 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 "
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010052 "POLARSSL_CERTS_C and/or POLARSSL_PEM_PARSE_C not defined.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020053 return( 0 );
54}
55#else
56
57#include <string.h>
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020058
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/net.h"
60#include "mbedtls/debug.h"
61#include "mbedtls/ssl.h"
62#include "mbedtls/entropy.h"
63#include "mbedtls/ctr_drbg.h"
64#include "mbedtls/error.h"
65#include "mbedtls/certs.h"
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020066
67#define SERVER_PORT 4433
68#define SERVER_NAME "localhost"
69#define SERVER_ADDR "127.0.0.1" /* forces IPv4 */
70#define MESSAGE "Echo this"
71
72#define READ_TIMEOUT_MS 1000
73#define MAX_RETRY 5
74
75#define DEBUG_LEVEL 0
76
77static void my_debug( void *ctx, int level, const char *str )
78{
79 ((void) level);
80
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000081 polarssl_fprintf( (FILE *) ctx, "%s", str );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +020082 fflush( (FILE *) ctx );
83}
84
85int main( int argc, char *argv[] )
86{
87 int ret, len, server_fd = -1;
88 unsigned char buf[1024];
89 const char *pers = "dtls_client";
90 int retry_left = MAX_RETRY;
91
92 entropy_context entropy;
93 ctr_drbg_context ctr_drbg;
94 ssl_context ssl;
95 x509_crt cacert;
96
97 ((void) argc);
98 ((void) argv);
99
100#if defined(POLARSSL_DEBUG_C)
101 debug_set_threshold( DEBUG_LEVEL );
102#endif
103
104 /*
105 * 0. Initialize the RNG and the session data
106 */
107 memset( &ssl, 0, sizeof( ssl_context ) );
108 x509_crt_init( &cacert );
109
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000110 polarssl_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200111 fflush( stdout );
112
113 entropy_init( &entropy );
114 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
115 (const unsigned char *) pers,
116 strlen( pers ) ) ) != 0 )
117 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000118 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200119 goto exit;
120 }
121
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000122 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200123
124 /*
125 * 0. Initialize certificates
126 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000127 polarssl_printf( " . Loading the CA root certificate ..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200128 fflush( stdout );
129
Manuel Pégourié-Gonnarda958d692015-03-27 10:23:53 +0100130 ret = x509_crt_parse( &cacert, (const unsigned char *) test_cas_pem,
131 test_cas_pem_len );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200132 if( ret < 0 )
133 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000134 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200135 goto exit;
136 }
137
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000138 polarssl_printf( " ok (%d skipped)\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200139
140 /*
141 * 1. Start the connection
142 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000143 polarssl_printf( " . Connecting to udp/%s/%4d...", SERVER_NAME,
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200144 SERVER_PORT );
145 fflush( stdout );
146
147 if( ( ret = net_connect( &server_fd, SERVER_ADDR,
148 SERVER_PORT, NET_PROTO_UDP ) ) != 0 )
149 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000150 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200151 goto exit;
152 }
153
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000154 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200155
156 /*
157 * 2. Setup stuff
158 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000159 polarssl_printf( " . Setting up the DTLS structure..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200160 fflush( stdout );
161
162 if( ( ret = ssl_init( &ssl ) ) != 0 )
163 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000164 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200165 goto exit;
166 }
167
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000168 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200169
170 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
171 ssl_set_transport( &ssl, SSL_TRANSPORT_DATAGRAM );
172
173 /* OPTIONAL is usually a bad choice for security, but makes interop easier
174 * in this simplified example, in which the ca chain is hardcoded.
175 * Production code should set a proper ca chain and use REQUIRED. */
176 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
177 ssl_set_ca_chain( &ssl, &cacert, NULL, SERVER_NAME );
178
179 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
180 ssl_set_dbg( &ssl, my_debug, stdout );
181
182 ssl_set_bio_timeout( &ssl, &server_fd,
183 net_send, net_recv, net_recv_timeout,
184 READ_TIMEOUT_MS );
185
186 /*
187 * 4. Handshake
188 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000189 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200190 fflush( stdout );
191
192 do ret = ssl_handshake( &ssl );
193 while( ret == POLARSSL_ERR_NET_WANT_READ ||
194 ret == POLARSSL_ERR_NET_WANT_WRITE );
195
196 if( ret != 0 )
197 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000198 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200199 goto exit;
200 }
201
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000202 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200203
204 /*
205 * 5. Verify the server certificate
206 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000207 polarssl_printf( " . Verifying peer X.509 certificate..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200208
209 /* In real life, we would have used SSL_VERIFY_REQUIRED so that the
210 * handshake would not succeed if the peer's cert is bad. Even if we used
211 * SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
212 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
213 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000214 polarssl_printf( " failed\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200215
216 if( ( ret & BADCERT_EXPIRED ) != 0 )
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000217 polarssl_printf( " ! server certificate has expired\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200218
219 if( ( ret & BADCERT_REVOKED ) != 0 )
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000220 polarssl_printf( " ! server certificate has been revoked\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200221
222 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000223 polarssl_printf( " ! CN mismatch (expected CN=%s)\n", SERVER_NAME );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200224
225 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000226 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200227
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000228 polarssl_printf( "\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200229 }
230 else
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000231 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200232
233 /*
234 * 6. Write the echo request
235 */
236send_request:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000237 polarssl_printf( " > Write to server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200238 fflush( stdout );
239
240 len = sizeof( MESSAGE ) - 1;
241
242 do ret = ssl_write( &ssl, (unsigned char *) MESSAGE, len );
243 while( ret == POLARSSL_ERR_NET_WANT_READ ||
244 ret == POLARSSL_ERR_NET_WANT_WRITE );
245
246 if( ret < 0 )
247 {
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000248 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200249 goto exit;
250 }
251
252 len = ret;
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000253 polarssl_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200254
255 /*
256 * 7. Read the echo response
257 */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000258 polarssl_printf( " < Read from server:" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200259 fflush( stdout );
260
261 len = sizeof( buf ) - 1;
262 memset( buf, 0, sizeof( buf ) );
263
264 do ret = ssl_read( &ssl, buf, len );
265 while( ret == POLARSSL_ERR_NET_WANT_READ ||
266 ret == POLARSSL_ERR_NET_WANT_WRITE );
267
268 if( ret <= 0 )
269 {
270 switch( ret )
271 {
272 case POLARSSL_ERR_NET_TIMEOUT:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000273 polarssl_printf( " timeout\n\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200274 if( retry_left-- > 0 )
275 goto send_request;
276 goto exit;
277
278 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000279 polarssl_printf( " connection was closed gracefully\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200280 ret = 0;
281 goto close_notify;
282
283 default:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000284 polarssl_printf( " ssl_read returned -0x%x\n\n", -ret );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200285 goto exit;
286 }
287 }
288
289 len = ret;
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000290 polarssl_printf( " %d bytes read\n\n%s\n\n", len, buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200291
292 /*
293 * 8. Done, cleanly close the connection
294 */
295close_notify:
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000296 polarssl_printf( " . Closing the connection..." );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200297
298 /* No error checking, the connection might be closed already */
299 do ret = ssl_close_notify( &ssl );
300 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
301 ret = 0;
302
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000303 polarssl_printf( " done\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200304
305 /*
306 * 9. Final clean-ups and exit
307 */
308exit:
309
310#ifdef POLARSSL_ERROR_C
311 if( ret != 0 )
312 {
313 char error_buf[100];
314 polarssl_strerror( ret, error_buf, 100 );
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000315 polarssl_printf( "Last error was: %d - %s\n\n", ret, error_buf );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200316 }
317#endif
318
319 if( server_fd != -1 )
320 net_close( server_fd );
321
322 x509_crt_free( &cacert );
323 ssl_free( &ssl );
324 ctr_drbg_free( &ctr_drbg );
325 entropy_free( &entropy );
326
327#if defined(_WIN32)
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +0000328 polarssl_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnarde63582a2014-10-14 11:47:21 +0200329 fflush( stdout ); getchar();
330#endif
331
332 /* Shell can not handle large exit numbers -> 1 for errors */
333 if( ret < 0 )
334 ret = 1;
335
336 return( ret );
337}
338#endif /* POLARSSL_SSL_CLI_C && POLARSSL_SSL_PROTO_DTLS && POLARSSL_NET_C &&
339 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100340 POLARSSL_X509_CRT_PARSE_C && POLARSSL_RSA_C && POLARSSL_CERTS_C &&
341 POLARSSL_PEM_PARSE_C */