blob: 0cf5eb695d1b8523f3d4729b1b811b39fd30b512 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
29#include <string.h>
30#include <stdio.h>
31
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/net.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020033#include "polarssl/debug.h"
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000035#include "polarssl/entropy.h"
36#include "polarssl/ctr_drbg.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000037#include "polarssl/error.h"
Paul Bakker75242c32012-11-17 00:03:46 +010038#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker508ad5a2011-12-04 17:09:26 +000040#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000041 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000042 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020043 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000044int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000045{
Paul Bakkercce9d772011-11-18 14:26:47 +000046 ((void) argc);
47 ((void) argv);
48
Paul Bakker508ad5a2011-12-04 17:09:26 +000049 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000050 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000051 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020052 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
Paul Bakkered27a042013-04-18 22:46:23 +020053 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000054 return( 0 );
55}
56#else
Paul Bakker9a97c5d2013-09-15 17:07:33 +020057
58#define SERVER_PORT 4433
59#define SERVER_NAME "localhost"
60#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
61
62#define DEBUG_LEVEL 1
63
64static void my_debug( void *ctx, int level, const char *str )
65{
Paul Bakkerc73079a2014-04-25 16:34:30 +020066 ((void) level);
67
68 fprintf( (FILE *) ctx, "%s", str );
69 fflush( (FILE *) ctx );
Paul Bakker9a97c5d2013-09-15 17:07:33 +020070}
71
Paul Bakkercce9d772011-11-18 14:26:47 +000072int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000073{
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +020074 int ret, len, server_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +000075 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020076 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +000077
78 entropy_context entropy;
79 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000080 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020081 x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Paul Bakkercce9d772011-11-18 14:26:47 +000083 ((void) argc);
84 ((void) argv);
85
Paul Bakkerc73079a2014-04-25 16:34:30 +020086#if defined(POLARSSL_DEBUG_C)
87 debug_set_threshold( DEBUG_LEVEL );
88#endif
89
Paul Bakker5121ce52009-01-03 21:22:43 +000090 /*
91 * 0. Initialize the RNG and the session data
92 */
Paul Bakker1a207ec2011-02-06 13:22:40 +000093 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +020094 x509_crt_init( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Paul Bakker508ad5a2011-12-04 17:09:26 +000096 printf( "\n . Seeding the random number generator..." );
97 fflush( stdout );
98
99 entropy_init( &entropy );
100 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200101 (const unsigned char *) pers,
102 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000103 {
104 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
105 goto exit;
106 }
107
108 printf( " ok\n" );
109
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100111 * 0. Initialize certificates
112 */
113 printf( " . Loading the CA root certificate ..." );
114 fflush( stdout );
115
116#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200117 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
118 strlen( test_ca_list ) );
Paul Bakker75242c32012-11-17 00:03:46 +0100119#else
120 ret = 1;
121 printf("POLARSSL_CERTS_C not defined.");
122#endif
123
124 if( ret < 0 )
125 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200126 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100127 goto exit;
128 }
129
130 printf( " ok (%d skipped)\n", ret );
131
132 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 * 1. Start the connection
134 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000135 printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME,
136 SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 fflush( stdout );
138
139 if( ( ret = net_connect( &server_fd, SERVER_NAME,
140 SERVER_PORT ) ) != 0 )
141 {
142 printf( " failed\n ! net_connect returned %d\n\n", ret );
143 goto exit;
144 }
145
146 printf( " ok\n" );
147
148 /*
149 * 2. Setup stuff
150 */
151 printf( " . Setting up the SSL/TLS structure..." );
152 fflush( stdout );
153
154 if( ( ret = ssl_init( &ssl ) ) != 0 )
155 {
156 printf( " failed\n ! ssl_init returned %d\n\n", ret );
157 goto exit;
158 }
159
160 printf( " ok\n" );
161
162 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100163 /* OPTIONAL is not optimal for security,
164 * but makes interop easier in this simplified example */
Paul Bakker75242c32012-11-17 00:03:46 +0100165 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
166 ssl_set_ca_chain( &ssl, &cacert, NULL, "PolarSSL Server 1" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100168 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
169 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100170 /* RC4 is deprecated, disable it */
171 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100172
Paul Bakker508ad5a2011-12-04 17:09:26 +0000173 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 ssl_set_dbg( &ssl, my_debug, stdout );
175 ssl_set_bio( &ssl, net_recv, &server_fd,
176 net_send, &server_fd );
177
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100179 * 4. Handshake
180 */
181 printf( " . Performing the SSL/TLS handshake..." );
182 fflush( stdout );
183
184 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
185 {
186 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
187 {
188 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
189 goto exit;
190 }
191 }
192
193 printf( " ok\n" );
194
195 /*
196 * 5. Verify the server certificate
197 */
198 printf( " . Verifying peer X.509 certificate..." );
199
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100200 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker75242c32012-11-17 00:03:46 +0100201 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
202 {
203 printf( " failed\n" );
204
205 if( ( ret & BADCERT_EXPIRED ) != 0 )
206 printf( " ! server certificate has expired\n" );
207
208 if( ( ret & BADCERT_REVOKED ) != 0 )
209 printf( " ! server certificate has been revoked\n" );
210
211 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
212 printf( " ! CN mismatch (expected CN=%s)\n", "PolarSSL Server 1" );
213
214 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
215 printf( " ! self-signed or not signed by a trusted CA\n" );
216
217 printf( "\n" );
218 }
219 else
220 printf( " ok\n" );
221
222 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 * 3. Write the GET request
224 */
225 printf( " > Write to server:" );
226 fflush( stdout );
227
228 len = sprintf( (char *) buf, GET_REQUEST );
229
230 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
231 {
Paul Bakker831a7552011-05-18 13:32:51 +0000232 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 {
234 printf( " failed\n ! ssl_write returned %d\n\n", ret );
235 goto exit;
236 }
237 }
238
239 len = ret;
240 printf( " %d bytes written\n\n%s", len, (char *) buf );
241
242 /*
243 * 7. Read the HTTP response
244 */
245 printf( " < Read from server:" );
246 fflush( stdout );
247
248 do
249 {
250 len = sizeof( buf ) - 1;
251 memset( buf, 0, sizeof( buf ) );
252 ret = ssl_read( &ssl, buf, len );
253
Paul Bakker831a7552011-05-18 13:32:51 +0000254 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 continue;
256
Paul Bakker40e46942009-01-03 21:51:57 +0000257 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 break;
259
Paul Bakker61da7522011-11-11 10:28:58 +0000260 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 {
262 printf( "failed\n ! ssl_read returned %d\n\n", ret );
263 break;
264 }
265
Paul Bakker61da7522011-11-11 10:28:58 +0000266 if( ret == 0 )
267 {
268 printf( "\n\nEOF\n\n" );
269 break;
270 }
271
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 len = ret;
273 printf( " %d bytes read\n\n%s", len, (char *) buf );
274 }
Paul Bakker61da7522011-11-11 10:28:58 +0000275 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
277 ssl_close_notify( &ssl );
278
279exit:
280
Paul Bakkera3d195c2011-11-27 21:07:34 +0000281#ifdef POLARSSL_ERROR_C
282 if( ret != 0 )
283 {
284 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200285 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000286 printf("Last error was: %d - %s\n\n", ret, error_buf );
287 }
288#endif
289
Paul Bakker0c226102014-04-17 16:02:36 +0200290 if( server_fd != -1 )
291 net_close( server_fd );
292
Paul Bakker36713e82013-09-17 13:25:29 +0200293 x509_crt_free( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200295 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200296 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298 memset( &ssl, 0, sizeof( ssl ) );
299
Paul Bakkercce9d772011-11-18 14:26:47 +0000300#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 printf( " + Press Enter to exit this program.\n" );
302 fflush( stdout ); getchar();
303#endif
304
305 return( ret );
306}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000307#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
308 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
309 POLARSSL_CTR_DRBG_C */