blob: 10a21d18f3f5dede5e7f2de094b144afca9667ad [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client demonstration program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Paul Bakker5121ce52009-01-03 21:22:43 +000036#include <string.h>
37#include <stdio.h>
38
Paul Bakker40e46942009-01-03 21:51:57 +000039#include "polarssl/net.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020040#include "polarssl/debug.h"
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000042#include "polarssl/entropy.h"
43#include "polarssl/ctr_drbg.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000044#include "polarssl/error.h"
Paul Bakker75242c32012-11-17 00:03:46 +010045#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Paul Bakker508ad5a2011-12-04 17:09:26 +000047#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000048 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000049 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020050 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000051int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000052{
Paul Bakkercce9d772011-11-18 14:26:47 +000053 ((void) argc);
54 ((void) argv);
55
Rich Evansf90016a2015-01-19 14:26:37 +000056 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000057 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000058 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020059 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
Paul Bakkered27a042013-04-18 22:46:23 +020060 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000061 return( 0 );
62}
63#else
Paul Bakker9a97c5d2013-09-15 17:07:33 +020064
65#define SERVER_PORT 4433
66#define SERVER_NAME "localhost"
67#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
68
69#define DEBUG_LEVEL 1
70
71static void my_debug( void *ctx, int level, const char *str )
72{
Paul Bakkerc73079a2014-04-25 16:34:30 +020073 ((void) level);
74
Rich Evansf90016a2015-01-19 14:26:37 +000075 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakkerc73079a2014-04-25 16:34:30 +020076 fflush( (FILE *) ctx );
Paul Bakker9a97c5d2013-09-15 17:07:33 +020077}
78
Paul Bakkercce9d772011-11-18 14:26:47 +000079int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000080{
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +020081 int ret, len, server_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +000082 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020083 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +000084
85 entropy_context entropy;
86 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000087 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020088 x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Paul Bakkercce9d772011-11-18 14:26:47 +000090 ((void) argc);
91 ((void) argv);
92
Paul Bakkerc73079a2014-04-25 16:34:30 +020093#if defined(POLARSSL_DEBUG_C)
94 debug_set_threshold( DEBUG_LEVEL );
95#endif
96
Paul Bakker5121ce52009-01-03 21:22:43 +000097 /*
98 * 0. Initialize the RNG and the session data
99 */
Paul Bakker1a207ec2011-02-06 13:22:40 +0000100 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200101 x509_crt_init( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Rich Evansf90016a2015-01-19 14:26:37 +0000103 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000104 fflush( stdout );
105
106 entropy_init( &entropy );
107 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200108 (const unsigned char *) pers,
109 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000110 {
Rich Evansf90016a2015-01-19 14:26:37 +0000111 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000112 goto exit;
113 }
114
Rich Evansf90016a2015-01-19 14:26:37 +0000115 polarssl_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000116
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100118 * 0. Initialize certificates
119 */
Rich Evansf90016a2015-01-19 14:26:37 +0000120 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100121 fflush( stdout );
122
123#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200124 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
125 strlen( test_ca_list ) );
Paul Bakker75242c32012-11-17 00:03:46 +0100126#else
127 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000128 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker75242c32012-11-17 00:03:46 +0100129#endif
130
131 if( ret < 0 )
132 {
Rich Evansf90016a2015-01-19 14:26:37 +0000133 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100134 goto exit;
135 }
136
Rich Evansf90016a2015-01-19 14:26:37 +0000137 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100138
139 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 * 1. Start the connection
141 */
Rich Evansf90016a2015-01-19 14:26:37 +0000142 polarssl_printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000143 SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 fflush( stdout );
145
146 if( ( ret = net_connect( &server_fd, SERVER_NAME,
147 SERVER_PORT ) ) != 0 )
148 {
Rich Evansf90016a2015-01-19 14:26:37 +0000149 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 goto exit;
151 }
152
Rich Evansf90016a2015-01-19 14:26:37 +0000153 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155 /*
156 * 2. Setup stuff
157 */
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 fflush( stdout );
160
161 if( ( ret = ssl_init( &ssl ) ) != 0 )
162 {
Rich Evansf90016a2015-01-19 14:26:37 +0000163 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 goto exit;
165 }
166
Rich Evansf90016a2015-01-19 14:26:37 +0000167 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100170 /* OPTIONAL is not optimal for security,
171 * but makes interop easier in this simplified example */
Paul Bakker75242c32012-11-17 00:03:46 +0100172 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
173 ssl_set_ca_chain( &ssl, &cacert, NULL, "PolarSSL Server 1" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100175 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
176 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100177 /* RC4 is deprecated, disable it */
178 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100179
Paul Bakker508ad5a2011-12-04 17:09:26 +0000180 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 ssl_set_dbg( &ssl, my_debug, stdout );
182 ssl_set_bio( &ssl, net_recv, &server_fd,
183 net_send, &server_fd );
184
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100186 * 4. Handshake
187 */
Rich Evansf90016a2015-01-19 14:26:37 +0000188 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100189 fflush( stdout );
190
191 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
192 {
193 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
194 {
Rich Evansf90016a2015-01-19 14:26:37 +0000195 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100196 goto exit;
197 }
198 }
199
Rich Evansf90016a2015-01-19 14:26:37 +0000200 polarssl_printf( " ok\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100201
202 /*
203 * 5. Verify the server certificate
204 */
Rich Evansf90016a2015-01-19 14:26:37 +0000205 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100206
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100207 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker75242c32012-11-17 00:03:46 +0100208 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
209 {
Rich Evansf90016a2015-01-19 14:26:37 +0000210 polarssl_printf( " failed\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100211
212 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000213 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100214
215 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000216 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100217
218 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000219 polarssl_printf( " ! CN mismatch (expected CN=%s)\n", "PolarSSL Server 1" );
Paul Bakker75242c32012-11-17 00:03:46 +0100220
221 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000222 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100223
Rich Evansf90016a2015-01-19 14:26:37 +0000224 polarssl_printf( "\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100225 }
226 else
Rich Evansf90016a2015-01-19 14:26:37 +0000227 polarssl_printf( " ok\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100228
229 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 * 3. Write the GET request
231 */
Rich Evansf90016a2015-01-19 14:26:37 +0000232 polarssl_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 fflush( stdout );
234
235 len = sprintf( (char *) buf, GET_REQUEST );
236
237 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
238 {
Paul Bakker831a7552011-05-18 13:32:51 +0000239 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 {
Rich Evansf90016a2015-01-19 14:26:37 +0000241 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 goto exit;
243 }
244 }
245
246 len = ret;
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_printf( " %d bytes written\n\n%s", len, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
249 /*
250 * 7. Read the HTTP response
251 */
Rich Evansf90016a2015-01-19 14:26:37 +0000252 polarssl_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 fflush( stdout );
254
255 do
256 {
257 len = sizeof( buf ) - 1;
258 memset( buf, 0, sizeof( buf ) );
259 ret = ssl_read( &ssl, buf, len );
260
Paul Bakker831a7552011-05-18 13:32:51 +0000261 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 continue;
263
Paul Bakker40e46942009-01-03 21:51:57 +0000264 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 break;
266
Paul Bakker61da7522011-11-11 10:28:58 +0000267 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 {
Rich Evansf90016a2015-01-19 14:26:37 +0000269 polarssl_printf( "failed\n ! ssl_read returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 break;
271 }
272
Paul Bakker61da7522011-11-11 10:28:58 +0000273 if( ret == 0 )
274 {
Rich Evansf90016a2015-01-19 14:26:37 +0000275 polarssl_printf( "\n\nEOF\n\n" );
Paul Bakker61da7522011-11-11 10:28:58 +0000276 break;
277 }
278
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 len = ret;
Rich Evansf90016a2015-01-19 14:26:37 +0000280 polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 }
Paul Bakker61da7522011-11-11 10:28:58 +0000282 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284 ssl_close_notify( &ssl );
285
286exit:
287
Paul Bakkera3d195c2011-11-27 21:07:34 +0000288#ifdef POLARSSL_ERROR_C
289 if( ret != 0 )
290 {
291 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200292 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +0000293 polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000294 }
295#endif
296
Paul Bakker0c226102014-04-17 16:02:36 +0200297 if( server_fd != -1 )
298 net_close( server_fd );
299
Paul Bakker36713e82013-09-17 13:25:29 +0200300 x509_crt_free( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200302 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200303 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305 memset( &ssl, 0, sizeof( ssl ) );
306
Paul Bakkercce9d772011-11-18 14:26:47 +0000307#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000308 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 fflush( stdout ); getchar();
310#endif
311
312 return( ret );
313}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000314#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
315 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
316 POLARSSL_CTR_DRBG_C */