blob: 7c95c4417ffa9e73e7fd64398affd70c4b76cb9f [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 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#if !defined(POLARSSL_CONFIG_FILE)
27#include "polarssl/config.h"
28#else
29#include POLARSSL_CONFIG_FILE
30#endif
31
32#if !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_SSL_PROTO_DTLS) || \
33 !defined(POLARSSL_NET_C) || \
34 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
35 !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_RSA_C) || \
36 !defined(POLARSSL_CERTS_C)
37
38#include <stdio.h>
39int main( int argc, char *argv[] )
40{
41 ((void) argc);
42 ((void) argv);
43
44 printf( "POLARSSL_SSL_CLI_C and/or POLARSSL_SSL_PROTO_DTLS and/or "
45 "POLARSSL_NET_C and/or "
46 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
47 "POLARSSL_X509_CRT_PARSE_C and/or POLARSSL_RSA_C and/or "
48 "POLARSSL_CERTS_C not defined.\n" );
49 return( 0 );
50}
51#else
52
53#include <string.h>
54#include <stdio.h>
55
56#include "polarssl/net.h"
57#include "polarssl/debug.h"
58#include "polarssl/ssl.h"
59#include "polarssl/entropy.h"
60#include "polarssl/ctr_drbg.h"
61#include "polarssl/error.h"
62#include "polarssl/certs.h"
63
64#define SERVER_PORT 4433
65#define SERVER_NAME "localhost"
66#define SERVER_ADDR "127.0.0.1" /* forces IPv4 */
67#define MESSAGE "Echo this"
68
69#define READ_TIMEOUT_MS 1000
70#define MAX_RETRY 5
71
72#define DEBUG_LEVEL 0
73
74static void my_debug( void *ctx, int level, const char *str )
75{
76 ((void) level);
77
78 fprintf( (FILE *) ctx, "%s", str );
79 fflush( (FILE *) ctx );
80}
81
82int main( int argc, char *argv[] )
83{
84 int ret, len, server_fd = -1;
85 unsigned char buf[1024];
86 const char *pers = "dtls_client";
87 int retry_left = MAX_RETRY;
88
89 entropy_context entropy;
90 ctr_drbg_context ctr_drbg;
91 ssl_context ssl;
92 x509_crt cacert;
93
94 ((void) argc);
95 ((void) argv);
96
97#if defined(POLARSSL_DEBUG_C)
98 debug_set_threshold( DEBUG_LEVEL );
99#endif
100
101 /*
102 * 0. Initialize the RNG and the session data
103 */
104 memset( &ssl, 0, sizeof( ssl_context ) );
105 x509_crt_init( &cacert );
106
107 printf( "\n . Seeding the random number generator..." );
108 fflush( stdout );
109
110 entropy_init( &entropy );
111 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
112 (const unsigned char *) pers,
113 strlen( pers ) ) ) != 0 )
114 {
115 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
116 goto exit;
117 }
118
119 printf( " ok\n" );
120
121 /*
122 * 0. Initialize certificates
123 */
124 printf( " . Loading the CA root certificate ..." );
125 fflush( stdout );
126
127#if defined(POLARSSL_CERTS_C)
128 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
129 strlen( test_ca_list ) );
130#else
131 ret = 1;
132 printf("POLARSSL_CERTS_C not defined.");
133#endif
134
135 if( ret < 0 )
136 {
137 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
138 goto exit;
139 }
140
141 printf( " ok (%d skipped)\n", ret );
142
143 /*
144 * 1. Start the connection
145 */
146 printf( " . Connecting to udp/%s/%4d...", SERVER_NAME,
147 SERVER_PORT );
148 fflush( stdout );
149
150 if( ( ret = net_connect( &server_fd, SERVER_ADDR,
151 SERVER_PORT, NET_PROTO_UDP ) ) != 0 )
152 {
153 printf( " failed\n ! net_connect returned %d\n\n", ret );
154 goto exit;
155 }
156
157 printf( " ok\n" );
158
159 /*
160 * 2. Setup stuff
161 */
162 printf( " . Setting up the DTLS structure..." );
163 fflush( stdout );
164
165 if( ( ret = ssl_init( &ssl ) ) != 0 )
166 {
167 printf( " failed\n ! ssl_init returned %d\n\n", ret );
168 goto exit;
169 }
170
171 printf( " ok\n" );
172
173 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
174 ssl_set_transport( &ssl, SSL_TRANSPORT_DATAGRAM );
175
176 /* OPTIONAL is usually a bad choice for security, but makes interop easier
177 * in this simplified example, in which the ca chain is hardcoded.
178 * Production code should set a proper ca chain and use REQUIRED. */
179 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
180 ssl_set_ca_chain( &ssl, &cacert, NULL, SERVER_NAME );
181
182 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
183 ssl_set_dbg( &ssl, my_debug, stdout );
184
185 ssl_set_bio_timeout( &ssl, &server_fd,
186 net_send, net_recv, net_recv_timeout,
187 READ_TIMEOUT_MS );
188
189 /*
190 * 4. Handshake
191 */
192 printf( " . Performing the SSL/TLS handshake..." );
193 fflush( stdout );
194
195 do ret = ssl_handshake( &ssl );
196 while( ret == POLARSSL_ERR_NET_WANT_READ ||
197 ret == POLARSSL_ERR_NET_WANT_WRITE );
198
199 if( ret != 0 )
200 {
201 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
202 goto exit;
203 }
204
205 printf( " ok\n" );
206
207 /*
208 * 5. Verify the server certificate
209 */
210 printf( " . Verifying peer X.509 certificate..." );
211
212 /* In real life, we would have used SSL_VERIFY_REQUIRED so that the
213 * handshake would not succeed if the peer's cert is bad. Even if we used
214 * SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
215 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
216 {
217 printf( " failed\n" );
218
219 if( ( ret & BADCERT_EXPIRED ) != 0 )
220 printf( " ! server certificate has expired\n" );
221
222 if( ( ret & BADCERT_REVOKED ) != 0 )
223 printf( " ! server certificate has been revoked\n" );
224
225 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
226 printf( " ! CN mismatch (expected CN=%s)\n", SERVER_NAME );
227
228 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
229 printf( " ! self-signed or not signed by a trusted CA\n" );
230
231 printf( "\n" );
232 }
233 else
234 printf( " ok\n" );
235
236 /*
237 * 6. Write the echo request
238 */
239send_request:
240 printf( " > Write to server:" );
241 fflush( stdout );
242
243 len = sizeof( MESSAGE ) - 1;
244
245 do ret = ssl_write( &ssl, (unsigned char *) MESSAGE, len );
246 while( ret == POLARSSL_ERR_NET_WANT_READ ||
247 ret == POLARSSL_ERR_NET_WANT_WRITE );
248
249 if( ret < 0 )
250 {
251 printf( " failed\n ! ssl_write returned %d\n\n", ret );
252 goto exit;
253 }
254
255 len = ret;
256 printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
257
258 /*
259 * 7. Read the echo response
260 */
261 printf( " < Read from server:" );
262 fflush( stdout );
263
264 len = sizeof( buf ) - 1;
265 memset( buf, 0, sizeof( buf ) );
266
267 do ret = ssl_read( &ssl, buf, len );
268 while( ret == POLARSSL_ERR_NET_WANT_READ ||
269 ret == POLARSSL_ERR_NET_WANT_WRITE );
270
271 if( ret <= 0 )
272 {
273 switch( ret )
274 {
275 case POLARSSL_ERR_NET_TIMEOUT:
276 printf( " timeout\n\n" );
277 if( retry_left-- > 0 )
278 goto send_request;
279 goto exit;
280
281 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
282 printf( " connection was closed gracefully\n" );
283 ret = 0;
284 goto close_notify;
285
286 default:
287 printf( " ssl_read returned -0x%x\n\n", -ret );
288 goto exit;
289 }
290 }
291
292 len = ret;
293 printf( " %d bytes read\n\n%s\n\n", len, buf );
294
295 /*
296 * 8. Done, cleanly close the connection
297 */
298close_notify:
299 printf( " . Closing the connection..." );
300
301 /* No error checking, the connection might be closed already */
302 do ret = ssl_close_notify( &ssl );
303 while( ret == POLARSSL_ERR_NET_WANT_WRITE );
304 ret = 0;
305
306 printf( " done\n" );
307
308 /*
309 * 9. Final clean-ups and exit
310 */
311exit:
312
313#ifdef POLARSSL_ERROR_C
314 if( ret != 0 )
315 {
316 char error_buf[100];
317 polarssl_strerror( ret, error_buf, 100 );
318 printf( "Last error was: %d - %s\n\n", ret, error_buf );
319 }
320#endif
321
322 if( server_fd != -1 )
323 net_close( server_fd );
324
325 x509_crt_free( &cacert );
326 ssl_free( &ssl );
327 ctr_drbg_free( &ctr_drbg );
328 entropy_free( &entropy );
329
330#if defined(_WIN32)
331 printf( " + Press Enter to exit this program.\n" );
332 fflush( stdout ); getchar();
333#endif
334
335 /* Shell can not handle large exit numbers -> 1 for errors */
336 if( ret < 0 )
337 ret = 1;
338
339 return( ret );
340}
341#endif /* POLARSSL_SSL_CLI_C && POLARSSL_SSL_PROTO_DTLS && POLARSSL_NET_C &&
342 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
343 POLARSSL_X509_CRT_PARSE_C && POLARSSL_RSA_C && POLARSSL_CERTS_C */