blob: 8a43537f9d8324e2f1274a7098fed5a73b1e641e [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (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 Bakker5121ce52009-01-03 21:22:43 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000029
30#include <string.h>
31#include <stdio.h>
32
Paul Bakker40e46942009-01-03 21:51:57 +000033#include "polarssl/net.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020034#include "polarssl/debug.h"
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000036#include "polarssl/entropy.h"
37#include "polarssl/ctr_drbg.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000038#include "polarssl/error.h"
Paul Bakker75242c32012-11-17 00:03:46 +010039#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker508ad5a2011-12-04 17:09:26 +000041#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000042 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000043 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020044 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000045int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000046{
Paul Bakkercce9d772011-11-18 14:26:47 +000047 ((void) argc);
48 ((void) argv);
49
Paul Bakker508ad5a2011-12-04 17:09:26 +000050 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000051 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000052 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020053 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
Paul Bakkered27a042013-04-18 22:46:23 +020054 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000055 return( 0 );
56}
57#else
Paul Bakker9a97c5d2013-09-15 17:07:33 +020058
59#define SERVER_PORT 4433
60#define SERVER_NAME "localhost"
61#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
62
63#define DEBUG_LEVEL 1
64
65static void my_debug( void *ctx, int level, const char *str )
66{
Paul Bakkerc73079a2014-04-25 16:34:30 +020067 ((void) level);
68
69 fprintf( (FILE *) ctx, "%s", str );
70 fflush( (FILE *) ctx );
Paul Bakker9a97c5d2013-09-15 17:07:33 +020071}
72
Paul Bakkercce9d772011-11-18 14:26:47 +000073int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000074{
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +020075 int ret, len, server_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +000076 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020077 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +000078
79 entropy_context entropy;
80 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000081 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020082 x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Paul Bakkercce9d772011-11-18 14:26:47 +000084 ((void) argc);
85 ((void) argv);
86
Paul Bakkerc73079a2014-04-25 16:34:30 +020087#if defined(POLARSSL_DEBUG_C)
88 debug_set_threshold( DEBUG_LEVEL );
89#endif
90
Paul Bakker5121ce52009-01-03 21:22:43 +000091 /*
92 * 0. Initialize the RNG and the session data
93 */
Paul Bakker1a207ec2011-02-06 13:22:40 +000094 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +020095 x509_crt_init( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Paul Bakker508ad5a2011-12-04 17:09:26 +000097 printf( "\n . Seeding the random number generator..." );
98 fflush( stdout );
99
100 entropy_init( &entropy );
101 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200102 (const unsigned char *) pers,
103 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000104 {
105 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
106 goto exit;
107 }
108
109 printf( " ok\n" );
110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100112 * 0. Initialize certificates
113 */
114 printf( " . Loading the CA root certificate ..." );
115 fflush( stdout );
116
117#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200118 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
119 strlen( test_ca_list ) );
Paul Bakker75242c32012-11-17 00:03:46 +0100120#else
121 ret = 1;
122 printf("POLARSSL_CERTS_C not defined.");
123#endif
124
125 if( ret < 0 )
126 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200127 printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100128 goto exit;
129 }
130
131 printf( " ok (%d skipped)\n", ret );
132
133 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 * 1. Start the connection
135 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000136 printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME,
137 SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 fflush( stdout );
139
140 if( ( ret = net_connect( &server_fd, SERVER_NAME,
141 SERVER_PORT ) ) != 0 )
142 {
143 printf( " failed\n ! net_connect returned %d\n\n", ret );
144 goto exit;
145 }
146
147 printf( " ok\n" );
148
149 /*
150 * 2. Setup stuff
151 */
152 printf( " . Setting up the SSL/TLS structure..." );
153 fflush( stdout );
154
155 if( ( ret = ssl_init( &ssl ) ) != 0 )
156 {
157 printf( " failed\n ! ssl_init returned %d\n\n", ret );
158 goto exit;
159 }
160
161 printf( " ok\n" );
162
163 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100164 /* OPTIONAL is not optimal for security,
165 * but makes interop easier in this simplified example */
Paul Bakker75242c32012-11-17 00:03:46 +0100166 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
167 ssl_set_ca_chain( &ssl, &cacert, NULL, "PolarSSL Server 1" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100169 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
170 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100171 /* RC4 is deprecated, disable it */
172 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100173
Paul Bakker508ad5a2011-12-04 17:09:26 +0000174 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 ssl_set_dbg( &ssl, my_debug, stdout );
176 ssl_set_bio( &ssl, net_recv, &server_fd,
177 net_send, &server_fd );
178
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100180 * 4. Handshake
181 */
182 printf( " . Performing the SSL/TLS handshake..." );
183 fflush( stdout );
184
185 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
186 {
187 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
188 {
189 printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
190 goto exit;
191 }
192 }
193
194 printf( " ok\n" );
195
196 /*
197 * 5. Verify the server certificate
198 */
199 printf( " . Verifying peer X.509 certificate..." );
200
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100201 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker75242c32012-11-17 00:03:46 +0100202 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
203 {
204 printf( " failed\n" );
205
206 if( ( ret & BADCERT_EXPIRED ) != 0 )
207 printf( " ! server certificate has expired\n" );
208
209 if( ( ret & BADCERT_REVOKED ) != 0 )
210 printf( " ! server certificate has been revoked\n" );
211
212 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
213 printf( " ! CN mismatch (expected CN=%s)\n", "PolarSSL Server 1" );
214
215 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
216 printf( " ! self-signed or not signed by a trusted CA\n" );
217
218 printf( "\n" );
219 }
220 else
221 printf( " ok\n" );
222
223 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 * 3. Write the GET request
225 */
226 printf( " > Write to server:" );
227 fflush( stdout );
228
229 len = sprintf( (char *) buf, GET_REQUEST );
230
231 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
232 {
Paul Bakker831a7552011-05-18 13:32:51 +0000233 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 {
235 printf( " failed\n ! ssl_write returned %d\n\n", ret );
236 goto exit;
237 }
238 }
239
240 len = ret;
241 printf( " %d bytes written\n\n%s", len, (char *) buf );
242
243 /*
244 * 7. Read the HTTP response
245 */
246 printf( " < Read from server:" );
247 fflush( stdout );
248
249 do
250 {
251 len = sizeof( buf ) - 1;
252 memset( buf, 0, sizeof( buf ) );
253 ret = ssl_read( &ssl, buf, len );
254
Paul Bakker831a7552011-05-18 13:32:51 +0000255 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 continue;
257
Paul Bakker40e46942009-01-03 21:51:57 +0000258 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 break;
260
Paul Bakker61da7522011-11-11 10:28:58 +0000261 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 {
263 printf( "failed\n ! ssl_read returned %d\n\n", ret );
264 break;
265 }
266
Paul Bakker61da7522011-11-11 10:28:58 +0000267 if( ret == 0 )
268 {
269 printf( "\n\nEOF\n\n" );
270 break;
271 }
272
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 len = ret;
274 printf( " %d bytes read\n\n%s", len, (char *) buf );
275 }
Paul Bakker61da7522011-11-11 10:28:58 +0000276 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278 ssl_close_notify( &ssl );
279
280exit:
281
Paul Bakkera3d195c2011-11-27 21:07:34 +0000282#ifdef POLARSSL_ERROR_C
283 if( ret != 0 )
284 {
285 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200286 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000287 printf("Last error was: %d - %s\n\n", ret, error_buf );
288 }
289#endif
290
Paul Bakker0c226102014-04-17 16:02:36 +0200291 if( server_fd != -1 )
292 net_close( server_fd );
293
Paul Bakker36713e82013-09-17 13:25:29 +0200294 x509_crt_free( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200296 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200297 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
299 memset( &ssl, 0, sizeof( ssl ) );
300
Paul Bakkercce9d772011-11-18 14:26:47 +0000301#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 printf( " + Press Enter to exit this program.\n" );
303 fflush( stdout ); getchar();
304#endif
305
306 return( ret );
307}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000308#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
309 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
310 POLARSSL_CTR_DRBG_C */