blob: 1b369a6580c5d3e8a8c8bd4c431343b5358584af [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client demonstration program
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
32#include <string.h>
33#include <stdio.h>
34
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/net.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020036#include "polarssl/debug.h"
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000038#include "polarssl/entropy.h"
39#include "polarssl/ctr_drbg.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000040#include "polarssl/error.h"
Paul Bakker75242c32012-11-17 00:03:46 +010041#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Paul Bakker508ad5a2011-12-04 17:09:26 +000043#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000044 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000045 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020046 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000047int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000048{
Paul Bakkercce9d772011-11-18 14:26:47 +000049 ((void) argc);
50 ((void) argv);
51
Paul Bakker508ad5a2011-12-04 17:09:26 +000052 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000053 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000054 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020055 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
Paul Bakkered27a042013-04-18 22:46:23 +020056 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000057 return( 0 );
58}
59#else
Paul Bakker9a97c5d2013-09-15 17:07:33 +020060
61#define SERVER_PORT 4433
62#define SERVER_NAME "localhost"
63#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
64
65#define DEBUG_LEVEL 1
66
67static void my_debug( void *ctx, int level, const char *str )
68{
Paul Bakkerc73079a2014-04-25 16:34:30 +020069 ((void) level);
70
71 fprintf( (FILE *) ctx, "%s", str );
72 fflush( (FILE *) ctx );
Paul Bakker9a97c5d2013-09-15 17:07:33 +020073}
74
Paul Bakkercce9d772011-11-18 14:26:47 +000075int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000076{
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +020077 int ret, len, server_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +000078 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +000080
81 entropy_context entropy;
82 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000083 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020084 x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Paul Bakkercce9d772011-11-18 14:26:47 +000086 ((void) argc);
87 ((void) argv);
88
Paul Bakkerc73079a2014-04-25 16:34:30 +020089#if defined(POLARSSL_DEBUG_C)
90 debug_set_threshold( DEBUG_LEVEL );
91#endif
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093 /*
94 * 0. Initialize the RNG and the session data
95 */
Paul Bakker1a207ec2011-02-06 13:22:40 +000096 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +020097 x509_crt_init( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Paul Bakker508ad5a2011-12-04 17:09:26 +000099 printf( "\n . Seeding the random number generator..." );
100 fflush( stdout );
101
102 entropy_init( &entropy );
103 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200104 (const unsigned char *) pers,
105 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000106 {
107 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
108 goto exit;
109 }
110
111 printf( " ok\n" );
112
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100114 * 0. Initialize certificates
115 */
116 printf( " . Loading the CA root certificate ..." );
117 fflush( stdout );
118
119#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200120 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
121 strlen( test_ca_list ) );
Paul Bakker75242c32012-11-17 00:03:46 +0100122#else
123 ret = 1;
124 printf("POLARSSL_CERTS_C not defined.");
125#endif
126
127 if( ret < 0 )
128 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200129 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100130 goto exit;
131 }
132
133 printf( " ok (%d skipped)\n", ret );
134
135 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 * 1. Start the connection
137 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000138 printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME,
139 SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 fflush( stdout );
141
142 if( ( ret = net_connect( &server_fd, SERVER_NAME,
143 SERVER_PORT ) ) != 0 )
144 {
145 printf( " failed\n ! net_connect returned %d\n\n", ret );
146 goto exit;
147 }
148
149 printf( " ok\n" );
150
151 /*
152 * 2. Setup stuff
153 */
154 printf( " . Setting up the SSL/TLS structure..." );
155 fflush( stdout );
156
157 if( ( ret = ssl_init( &ssl ) ) != 0 )
158 {
159 printf( " failed\n ! ssl_init returned %d\n\n", ret );
160 goto exit;
161 }
162
163 printf( " ok\n" );
164
165 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100166 /* OPTIONAL is not optimal for security,
167 * but makes interop easier in this simplified example */
Paul Bakker75242c32012-11-17 00:03:46 +0100168 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
169 ssl_set_ca_chain( &ssl, &cacert, NULL, "PolarSSL Server 1" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
Paul Bakker508ad5a2011-12-04 17:09:26 +0000171 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 ssl_set_dbg( &ssl, my_debug, stdout );
173 ssl_set_bio( &ssl, net_recv, &server_fd,
174 net_send, &server_fd );
175
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100177 * 4. Handshake
178 */
179 printf( " . Performing the SSL/TLS handshake..." );
180 fflush( stdout );
181
182 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
183 {
184 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
185 {
186 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
187 goto exit;
188 }
189 }
190
191 printf( " ok\n" );
192
193 /*
194 * 5. Verify the server certificate
195 */
196 printf( " . Verifying peer X.509 certificate..." );
197
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100198 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker75242c32012-11-17 00:03:46 +0100199 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
200 {
201 printf( " failed\n" );
202
203 if( ( ret & BADCERT_EXPIRED ) != 0 )
204 printf( " ! server certificate has expired\n" );
205
206 if( ( ret & BADCERT_REVOKED ) != 0 )
207 printf( " ! server certificate has been revoked\n" );
208
209 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
210 printf( " ! CN mismatch (expected CN=%s)\n", "PolarSSL Server 1" );
211
212 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
213 printf( " ! self-signed or not signed by a trusted CA\n" );
214
215 printf( "\n" );
216 }
217 else
218 printf( " ok\n" );
219
220 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 * 3. Write the GET request
222 */
223 printf( " > Write to server:" );
224 fflush( stdout );
225
226 len = sprintf( (char *) buf, GET_REQUEST );
227
228 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
229 {
Paul Bakker831a7552011-05-18 13:32:51 +0000230 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 {
232 printf( " failed\n ! ssl_write returned %d\n\n", ret );
233 goto exit;
234 }
235 }
236
237 len = ret;
238 printf( " %d bytes written\n\n%s", len, (char *) buf );
239
240 /*
241 * 7. Read the HTTP response
242 */
243 printf( " < Read from server:" );
244 fflush( stdout );
245
246 do
247 {
248 len = sizeof( buf ) - 1;
249 memset( buf, 0, sizeof( buf ) );
250 ret = ssl_read( &ssl, buf, len );
251
Paul Bakker831a7552011-05-18 13:32:51 +0000252 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 continue;
254
Paul Bakker40e46942009-01-03 21:51:57 +0000255 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 break;
257
Paul Bakker61da7522011-11-11 10:28:58 +0000258 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 {
260 printf( "failed\n ! ssl_read returned %d\n\n", ret );
261 break;
262 }
263
Paul Bakker61da7522011-11-11 10:28:58 +0000264 if( ret == 0 )
265 {
266 printf( "\n\nEOF\n\n" );
267 break;
268 }
269
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 len = ret;
271 printf( " %d bytes read\n\n%s", len, (char *) buf );
272 }
Paul Bakker61da7522011-11-11 10:28:58 +0000273 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275 ssl_close_notify( &ssl );
276
277exit:
278
Paul Bakkera3d195c2011-11-27 21:07:34 +0000279#ifdef POLARSSL_ERROR_C
280 if( ret != 0 )
281 {
282 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200283 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000284 printf("Last error was: %d - %s\n\n", ret, error_buf );
285 }
286#endif
287
Paul Bakker0c226102014-04-17 16:02:36 +0200288 if( server_fd != -1 )
289 net_close( server_fd );
290
Paul Bakker36713e82013-09-17 13:25:29 +0200291 x509_crt_free( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200293 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200294 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296 memset( &ssl, 0, sizeof( ssl ) );
297
Paul Bakkercce9d772011-11-18 14:26:47 +0000298#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 printf( " + Press Enter to exit this program.\n" );
300 fflush( stdout ); getchar();
301#endif
302
303 return( ret );
304}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000305#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
306 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
307 POLARSSL_CTR_DRBG_C */