blob: 45a690290a52a9f364cd61e614e36d47f3e0d917 [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +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é-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Paul Bakker508ad5a2011-12-04 17:09:26 +000037#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000038 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000039 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010040 !defined(POLARSSL_CERTS_C) || !defined(POLARSSL_PEM_PARSE_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020041 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000042int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000043{
Rich Evansf90016a2015-01-19 14:26:37 +000044 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000045 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000046 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020047 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
Paul Bakkered27a042013-04-18 22:46:23 +020048 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000049 return( 0 );
50}
51#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010052
53#include "mbedtls/net.h"
54#include "mbedtls/debug.h"
55#include "mbedtls/ssl.h"
56#include "mbedtls/entropy.h"
57#include "mbedtls/ctr_drbg.h"
58#include "mbedtls/error.h"
59#include "mbedtls/certs.h"
60
61#include <string.h>
62
63#define SERVER_PORT 4433
64#define SERVER_NAME "localhost"
65#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
66
67#define DEBUG_LEVEL 1
68
Paul Bakker9a97c5d2013-09-15 17:07:33 +020069static void my_debug( void *ctx, int level, const char *str )
70{
Paul Bakkerc73079a2014-04-25 16:34:30 +020071 ((void) level);
72
Rich Evansf90016a2015-01-19 14:26:37 +000073 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakkerc73079a2014-04-25 16:34:30 +020074 fflush( (FILE *) ctx );
Paul Bakker9a97c5d2013-09-15 17:07:33 +020075}
76
Rich Evans85b05ec2015-02-12 11:37:29 +000077int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000078{
Manuel Pégourié-Gonnard68821da2013-09-16 12:34:33 +020079 int ret, len, server_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +000080 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020081 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +000082
83 entropy_context entropy;
84 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000085 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020086 x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Paul Bakkerc73079a2014-04-25 16:34:30 +020088#if defined(POLARSSL_DEBUG_C)
89 debug_set_threshold( DEBUG_LEVEL );
90#endif
91
Paul Bakker5121ce52009-01-03 21:22:43 +000092 /*
93 * 0. Initialize the RNG and the session data
94 */
Paul Bakker1a207ec2011-02-06 13:22:40 +000095 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +020096 x509_crt_init( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Rich Evansf90016a2015-01-19 14:26:37 +000098 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +000099 fflush( stdout );
100
101 entropy_init( &entropy );
102 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200103 (const unsigned char *) pers,
104 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000105 {
Rich Evansf90016a2015-01-19 14:26:37 +0000106 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000107 goto exit;
108 }
109
Rich Evansf90016a2015-01-19 14:26:37 +0000110 polarssl_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000111
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100113 * 0. Initialize certificates
114 */
Rich Evansf90016a2015-01-19 14:26:37 +0000115 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100116 fflush( stdout );
117
Manuel Pégourié-Gonnarda958d692015-03-27 10:23:53 +0100118 ret = x509_crt_parse( &cacert, (const unsigned char *) test_cas_pem,
119 test_cas_pem_len );
Paul Bakker75242c32012-11-17 00:03:46 +0100120 if( ret < 0 )
121 {
Rich Evansf90016a2015-01-19 14:26:37 +0000122 polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100123 goto exit;
124 }
125
Rich Evansf90016a2015-01-19 14:26:37 +0000126 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100127
128 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 * 1. Start the connection
130 */
Rich Evansf90016a2015-01-19 14:26:37 +0000131 polarssl_printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000132 SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 fflush( stdout );
134
135 if( ( ret = net_connect( &server_fd, SERVER_NAME,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100136 SERVER_PORT, NET_PROTO_TCP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 {
Rich Evansf90016a2015-01-19 14:26:37 +0000138 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 goto exit;
140 }
141
Rich Evansf90016a2015-01-19 14:26:37 +0000142 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144 /*
145 * 2. Setup stuff
146 */
Rich Evansf90016a2015-01-19 14:26:37 +0000147 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 fflush( stdout );
149
150 if( ( ret = ssl_init( &ssl ) ) != 0 )
151 {
Rich Evansf90016a2015-01-19 14:26:37 +0000152 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 goto exit;
154 }
155
Rich Evansf90016a2015-01-19 14:26:37 +0000156 polarssl_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100159 /* OPTIONAL is not optimal for security,
160 * but makes interop easier in this simplified example */
Paul Bakker75242c32012-11-17 00:03:46 +0100161 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
Manuel Pégourié-Gonnard83b04de2015-03-09 17:33:07 +0000162 ssl_set_ca_chain( &ssl, &cacert, NULL, "mbed TLS Server 1" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Paul Bakker508ad5a2011-12-04 17:09:26 +0000164 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 ssl_set_dbg( &ssl, my_debug, stdout );
Manuel Pégourié-Gonnardaeab2522015-03-25 19:38:23 +0100166 ssl_set_bio_timeout( &ssl, &server_fd, net_send, net_recv, NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100169 * 4. Handshake
170 */
Rich Evansf90016a2015-01-19 14:26:37 +0000171 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100172 fflush( stdout );
173
174 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
175 {
176 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
177 {
Rich Evansf90016a2015-01-19 14:26:37 +0000178 polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100179 goto exit;
180 }
181 }
182
Rich Evansf90016a2015-01-19 14:26:37 +0000183 polarssl_printf( " ok\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100184
185 /*
186 * 5. Verify the server certificate
187 */
Rich Evansf90016a2015-01-19 14:26:37 +0000188 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100189
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100190 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker75242c32012-11-17 00:03:46 +0100191 if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
192 {
Rich Evansf90016a2015-01-19 14:26:37 +0000193 polarssl_printf( " failed\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100194
195 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000196 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100197
198 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000199 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100200
201 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000202 polarssl_printf( " ! CN mismatch (expected CN=%s)\n", "PolarSSL Server 1" );
Paul Bakker75242c32012-11-17 00:03:46 +0100203
204 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000205 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100206
Rich Evansf90016a2015-01-19 14:26:37 +0000207 polarssl_printf( "\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100208 }
209 else
Rich Evansf90016a2015-01-19 14:26:37 +0000210 polarssl_printf( " ok\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100211
212 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 * 3. Write the GET request
214 */
Rich Evansf90016a2015-01-19 14:26:37 +0000215 polarssl_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 fflush( stdout );
217
218 len = sprintf( (char *) buf, GET_REQUEST );
219
220 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
221 {
Paul Bakker831a7552011-05-18 13:32:51 +0000222 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 {
Rich Evansf90016a2015-01-19 14:26:37 +0000224 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 goto exit;
226 }
227 }
228
229 len = ret;
Rich Evansf90016a2015-01-19 14:26:37 +0000230 polarssl_printf( " %d bytes written\n\n%s", len, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232 /*
233 * 7. Read the HTTP response
234 */
Rich Evansf90016a2015-01-19 14:26:37 +0000235 polarssl_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 fflush( stdout );
237
238 do
239 {
240 len = sizeof( buf ) - 1;
241 memset( buf, 0, sizeof( buf ) );
242 ret = ssl_read( &ssl, buf, len );
243
Paul Bakker831a7552011-05-18 13:32:51 +0000244 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 continue;
246
Paul Bakker40e46942009-01-03 21:51:57 +0000247 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 break;
249
Paul Bakker61da7522011-11-11 10:28:58 +0000250 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 {
Rich Evansf90016a2015-01-19 14:26:37 +0000252 polarssl_printf( "failed\n ! ssl_read returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 break;
254 }
255
Paul Bakker61da7522011-11-11 10:28:58 +0000256 if( ret == 0 )
257 {
Rich Evansf90016a2015-01-19 14:26:37 +0000258 polarssl_printf( "\n\nEOF\n\n" );
Paul Bakker61da7522011-11-11 10:28:58 +0000259 break;
260 }
261
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 len = ret;
Rich Evansf90016a2015-01-19 14:26:37 +0000263 polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 }
Paul Bakker61da7522011-11-11 10:28:58 +0000265 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267 ssl_close_notify( &ssl );
268
269exit:
270
Paul Bakkera3d195c2011-11-27 21:07:34 +0000271#ifdef POLARSSL_ERROR_C
272 if( ret != 0 )
273 {
274 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200275 polarssl_strerror( ret, error_buf, 100 );
Rich Evansf90016a2015-01-19 14:26:37 +0000276 polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000277 }
278#endif
279
Paul Bakker0c226102014-04-17 16:02:36 +0200280 if( server_fd != -1 )
281 net_close( server_fd );
282
Paul Bakker36713e82013-09-17 13:25:29 +0200283 x509_crt_free( &cacert );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200285 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200286 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288 memset( &ssl, 0, sizeof( ssl ) );
289
Paul Bakkercce9d772011-11-18 14:26:47 +0000290#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000291 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 fflush( stdout ); getchar();
293#endif
294
295 return( ret );
296}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000297#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
298 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C &&
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +0100299 POLARSSL_CERTS_C && POLARSSL_PEM_PARSE_C && POLARSSL_CTR_DRBG_C &&
300 POLARSSL_X509_CRT_PARSE_C */