blob: e5bc4f7c563eddfa8f6200fbf51d77b12e858554 [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é-Gonnard3d2c4b72015-01-29 11:33:59 +00006 * This file is part of mbed TLS (https://polarssl.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)
24#include "polarssl/config.h"
25#else
26#include POLARSSL_CONFIG_FILE
27#endif
28
29#if !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_SSL_PROTO_DTLS) || \
30 !defined(POLARSSL_NET_C) || \
31 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
32 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_RSA_C) || \
33 !defined(POLARSSL_CERTS_C)
34
35#include <stdio.h>
36int main( int argc, char *argv[] )
37{
38 ((void) argc);
39 ((void) argv);
40
41 printf( "POLARSSL_SSL_CLI_C and/or POLARSSL_SSL_PROTO_DTLS and/or "
42 "POLARSSL_NET_C and/or "
43 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
44 "POLARSSL_X509_CRT_PARSE_C and/or POLARSSL_RSA_C and/or "
45 "POLARSSL_CERTS_C not defined.\n" );
46 return( 0 );
47}
48#else
49
50#include <string.h>
51#include <stdio.h>
52
53#include "polarssl/net.h"
54#include "polarssl/debug.h"
55#include "polarssl/ssl.h"
56#include "polarssl/entropy.h"
57#include "polarssl/ctr_drbg.h"
58#include "polarssl/error.h"
59#include "polarssl/certs.h"
60
61#define SERVER_PORT 4433
62#define SERVER_NAME "localhost"
63#define SERVER_ADDR "127.0.0.1" /* forces IPv4 */
64#define MESSAGE "Echo this"
65
66#define READ_TIMEOUT_MS 1000
67#define MAX_RETRY 5
68
69#define DEBUG_LEVEL 0
70
71static void my_debug( void *ctx, int level, const char *str )
72{
73 ((void) level);
74
75 fprintf( (FILE *) ctx, "%s", str );
76 fflush( (FILE *) ctx );
77}
78
79int main( int argc, char *argv[] )
80{
81 int ret, len, server_fd = -1;
82 unsigned char buf[1024];
83 const char *pers = "dtls_client";
84 int retry_left = MAX_RETRY;
85
86 entropy_context entropy;
87 ctr_drbg_context ctr_drbg;
88 ssl_context ssl;
89 x509_crt cacert;
90
91 ((void) argc);
92 ((void) argv);
93
94#if defined(POLARSSL_DEBUG_C)
95 debug_set_threshold( DEBUG_LEVEL );
96#endif
97
98 /*
99 * 0. Initialize the RNG and the session data
100 */
101 memset( &ssl, 0, sizeof( ssl_context ) );
102 x509_crt_init( &cacert );
103
104 printf( "\n . Seeding the random number generator..." );
105 fflush( stdout );
106
107 entropy_init( &entropy );
108 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
109 (const unsigned char *) pers,
110 strlen( pers ) ) ) != 0 )
111 {
112 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
113 goto exit;
114 }
115
116 printf( " ok\n" );
117
118 /*
119 * 0. Initialize certificates
120 */
121 printf( " . Loading the CA root certificate ..." );
122 fflush( stdout );
123
124#if defined(POLARSSL_CERTS_C)
125 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
126 strlen( test_ca_list ) );
127#else
128 ret = 1;
129 printf("POLARSSL_CERTS_C not defined.");
130#endif
131
132 if( ret < 0 )
133 {
134 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
135 goto exit;
136 }
137
138 printf( " ok (%d skipped)\n", ret );
139
140 /*
141 * 1. Start the connection
142 */
143 printf( " . Connecting to udp/%s/%4d...", SERVER_NAME,
144 SERVER_PORT );
145 fflush( stdout );
146
147 if( ( ret = net_connect( &server_fd, SERVER_ADDR,
148 SERVER_PORT, NET_PROTO_UDP ) ) != 0 )
149 {
150 printf( " failed\n ! net_connect returned %d\n\n", ret );
151 goto exit;
152 }
153
154 printf( " ok\n" );
155
156 /*
157 * 2. Setup stuff
158 */
159 printf( " . Setting up the DTLS structure..." );
160 fflush( stdout );
161
162 if( ( ret = ssl_init( &ssl ) ) != 0 )
163 {
164 printf( " failed\n ! ssl_init returned %d\n\n", ret );
165 goto exit;
166 }
167
168 printf( " ok\n" );
169
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 */
189 printf( " . Performing the SSL/TLS handshake..." );
190 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 {
198 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
199 goto exit;
200 }
201
202 printf( " ok\n" );
203
204 /*
205 * 5. Verify the server certificate
206 */
207 printf( " . Verifying peer X.509 certificate..." );
208
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 {
214 printf( " failed\n" );
215
216 if( ( ret & BADCERT_EXPIRED ) != 0 )
217 printf( " ! server certificate has expired\n" );
218
219 if( ( ret & BADCERT_REVOKED ) != 0 )
220 printf( " ! server certificate has been revoked\n" );
221
222 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
223 printf( " ! CN mismatch (expected CN=%s)\n", SERVER_NAME );
224
225 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
226 printf( " ! self-signed or not signed by a trusted CA\n" );
227
228 printf( "\n" );
229 }
230 else
231 printf( " ok\n" );
232
233 /*
234 * 6. Write the echo request
235 */
236send_request:
237 printf( " > Write to server:" );
238 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 {
248 printf( " failed\n ! ssl_write returned %d\n\n", ret );
249 goto exit;
250 }
251
252 len = ret;
253 printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
254
255 /*
256 * 7. Read the echo response
257 */
258 printf( " < Read from server:" );
259 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:
273 printf( " timeout\n\n" );
274 if( retry_left-- > 0 )
275 goto send_request;
276 goto exit;
277
278 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
279 printf( " connection was closed gracefully\n" );
280 ret = 0;
281 goto close_notify;
282
283 default:
284 printf( " ssl_read returned -0x%x\n\n", -ret );
285 goto exit;
286 }
287 }
288
289 len = ret;
290 printf( " %d bytes read\n\n%s\n\n", len, buf );
291
292 /*
293 * 8. Done, cleanly close the connection
294 */
295close_notify:
296 printf( " . Closing the connection..." );
297
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
303 printf( " done\n" );
304
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 );
315 printf( "Last error was: %d - %s\n\n", ret, error_buf );
316 }
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)
328 printf( " + Press Enter to exit this program.\n" );
329 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 &&
340 POLARSSL_X509_CRT_PARSE_C && POLARSSL_RSA_C && POLARSSL_CERTS_C */