blob: 38a510c3504b5feb85acd0e2c34316122ceb1464 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
38 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
39 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
40 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
41 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000042int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
45 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
46 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
47 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_entropy_context entropy;
84 mbedtls_ctr_drbg_context ctr_drbg;
85 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020086 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_DEBUG_C)
90 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Paul Bakkerc73079a2014-04-25 16:34:30 +020091#endif
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093 /*
94 * 0. Initialize the RNG and the session data
95 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +020096 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020097 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020099 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000102 fflush( stdout );
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200105 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200106 (const unsigned char *) pers,
107 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000108 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200109 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000110 goto exit;
111 }
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000114
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100116 * 0. Initialize certificates
117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100119 fflush( stdout );
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
122 mbedtls_test_cas_pem_len );
Paul Bakker75242c32012-11-17 00:03:46 +0100123 if( ret < 0 )
124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100126 goto exit;
127 }
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100130
131 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 * 1. Start the connection
133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( " . Connecting to tcp/%s/%4d...", SERVER_NAME,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000135 SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 fflush( stdout );
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
139 SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 goto exit;
143 }
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 /*
148 * 2. Setup stuff
149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 fflush( stdout );
152
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200153 if( ( ret = mbedtls_ssl_config_defaults( &conf,
154 MBEDTLS_SSL_IS_CLIENT,
155 MBEDTLS_SSL_TRANSPORT_STREAM ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200156 {
157 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
158 goto exit;
159 }
160
161 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 {
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200163 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 goto exit;
165 }
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100169 /* OPTIONAL is not optimal for security,
170 * but makes interop easier in this simplified example */
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +0200171 mbedtls_ssl_set_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_ssl_set_ca_chain( &ssl, &cacert, NULL, "mbed TLS Server 1" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_ssl_set_rng( &ssl, mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +0200175 mbedtls_ssl_set_dbg( &conf, my_debug, stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_ssl_set_bio_timeout( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100179 * 4. Handshake
180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100182 fflush( stdout );
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker75242c32012-11-17 00:03:46 +0100185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 if( ret != MBEDTLS_ERR_NET_WANT_READ && ret != MBEDTLS_ERR_NET_WANT_WRITE )
Paul Bakker75242c32012-11-17 00:03:46 +0100187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100189 goto exit;
190 }
191 }
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_printf( " ok\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100194
195 /*
196 * 5. Verify the server certificate
197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100199
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100200 /* In real life, we probably want to bail out when ret != 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 if( ( ret = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker75242c32012-11-17 00:03:46 +0100202 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100203 char vrfy_buf[512];
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_printf( " failed\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100206
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100207 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100208
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100209 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker75242c32012-11-17 00:03:46 +0100210 }
211 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_printf( " ok\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100213
214 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 * 3. Write the GET request
216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 fflush( stdout );
219
220 len = sprintf( (char *) buf, GET_REQUEST );
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( ret != MBEDTLS_ERR_NET_WANT_READ && ret != MBEDTLS_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 goto exit;
228 }
229 }
230
231 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234 /*
235 * 7. Read the HTTP response
236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 fflush( stdout );
239
240 do
241 {
242 len = sizeof( buf ) - 1;
243 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( ret == MBEDTLS_ERR_NET_WANT_READ || ret == MBEDTLS_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 continue;
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 break;
251
Paul Bakker61da7522011-11-11 10:28:58 +0000252 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 break;
256 }
257
Paul Bakker61da7522011-11-11 10:28:58 +0000258 if( ret == 0 )
259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_printf( "\n\nEOF\n\n" );
Paul Bakker61da7522011-11-11 10:28:58 +0000261 break;
262 }
263
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 }
Paul Bakker61da7522011-11-11 10:28:58 +0000267 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_ssl_close_notify( &ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
271exit:
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#ifdef MBEDTLS_ERROR_C
Paul Bakkera3d195c2011-11-27 21:07:34 +0000274 if( ret != 0 )
275 {
276 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_strerror( ret, error_buf, 100 );
278 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000279 }
280#endif
281
Paul Bakker0c226102014-04-17 16:02:36 +0200282 if( server_fd != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_net_close( server_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_x509_crt_free( &cacert );
286 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200287 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_ctr_drbg_free( &ctr_drbg );
289 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Paul Bakkercce9d772011-11-18 14:26:47 +0000291#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 fflush( stdout ); getchar();
294#endif
295
296 return( ret );
297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
299 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
300 MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C &&
301 MBEDTLS_X509_CRT_PARSE_C */