blob: e6d27eb7c4e4966b9f793b5e342320ba43c3987b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client demonstration program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010059#include <stdlib.h>
Andres Amaya Garciabc1af792018-04-29 20:53:53 +010060#define mbedtls_time time
61#define mbedtls_time_t time_t
62#define mbedtls_fprintf fprintf
63#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020064#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010065#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciabc1af792018-04-29 20:53:53 +010066#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
67#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
70 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
71 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
72 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
73 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000074int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
77 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
78 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
79 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
Paul Bakkered27a042013-04-18 22:46:23 +020080 "not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020081 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000082}
83#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010084
Andres AG788aa4a2016-09-14 14:32:09 +010085#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010086#include "mbedtls/debug.h"
87#include "mbedtls/ssl.h"
88#include "mbedtls/entropy.h"
89#include "mbedtls/ctr_drbg.h"
90#include "mbedtls/error.h"
91#include "mbedtls/certs.h"
92
93#include <string.h>
94
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020095#define SERVER_PORT "4433"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010096#define SERVER_NAME "localhost"
97#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
98
99#define DEBUG_LEVEL 1
100
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200101static void my_debug( void *ctx, int level,
102 const char *file, int line,
103 const char *str )
Paul Bakker9a97c5d2013-09-15 17:07:33 +0200104{
Paul Bakkerc73079a2014-04-25 16:34:30 +0200105 ((void) level);
106
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200107 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200108 fflush( (FILE *) ctx );
Paul Bakker9a97c5d2013-09-15 17:07:33 +0200109}
110
Rich Evans85b05ec2015-02-12 11:37:29 +0000111int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000112{
Andres Amaya Garciabc1af792018-04-29 20:53:53 +0100113 int ret = 1, len;
114 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200115 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200116 uint32_t flags;
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200118 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_entropy_context entropy;
121 mbedtls_ctr_drbg_context ctr_drbg;
122 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200123 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_DEBUG_C)
127 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200128#endif
129
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 /*
131 * 0. Initialize the RNG and the session data
132 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200133 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200134 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200135 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200137 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000140 fflush( stdout );
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200143 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200144 (const unsigned char *) pers,
145 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000146 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200147 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000148 goto exit;
149 }
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000152
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100154 * 0. Initialize certificates
155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100157 fflush( stdout );
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
160 mbedtls_test_cas_pem_len );
Paul Bakker75242c32012-11-17 00:03:46 +0100161 if( ret < 0 )
162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100164 goto exit;
165 }
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100168
169 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 * 1. Start the connection
171 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200172 mbedtls_printf( " . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 fflush( stdout );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
176 SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 goto exit;
180 }
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184 /*
185 * 2. Setup stuff
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 fflush( stdout );
189
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200190 if( ( ret = mbedtls_ssl_config_defaults( &conf,
191 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200192 MBEDTLS_SSL_TRANSPORT_STREAM,
193 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200194 {
195 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
196 goto exit;
197 }
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100201 /* OPTIONAL is not optimal for security,
202 * but makes interop easier in this simplified example */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200203 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
204 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200205 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
206 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
207
208 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
209 {
210 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
211 goto exit;
212 }
213
Paul Bakker23828162016-07-22 11:54:30 +0100214 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100215 {
216 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
217 goto exit;
218 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +0100220 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100223 * 4. Handshake
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100226 fflush( stdout );
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker75242c32012-11-17 00:03:46 +0100229 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100230 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker75242c32012-11-17 00:03:46 +0100231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100233 goto exit;
234 }
235 }
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_printf( " ok\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100238
239 /*
240 * 5. Verify the server certificate
241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100243
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100244 /* In real life, we probably want to bail out when ret != 0 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200245 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker75242c32012-11-17 00:03:46 +0100246 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100247 char vrfy_buf[512];
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_printf( " failed\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100250
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200251 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker75242c32012-11-17 00:03:46 +0100252
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100253 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker75242c32012-11-17 00:03:46 +0100254 }
255 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf( " ok\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100257
258 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 * 3. Write the GET request
260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 fflush( stdout );
263
264 len = sprintf( (char *) buf, GET_REQUEST );
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100268 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 goto exit;
272 }
273 }
274
275 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278 /*
279 * 7. Read the HTTP response
280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 fflush( stdout );
283
284 do
285 {
286 len = sizeof( buf ) - 1;
287 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100290 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 continue;
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 break;
295
Paul Bakker61da7522011-11-11 10:28:58 +0000296 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 break;
300 }
301
Paul Bakker61da7522011-11-11 10:28:58 +0000302 if( ret == 0 )
303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_printf( "\n\nEOF\n\n" );
Paul Bakker61da7522011-11-11 10:28:58 +0000305 break;
306 }
307
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 }
Paul Bakker61da7522011-11-11 10:28:58 +0000311 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_ssl_close_notify( &ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Andres Amaya Garciabc1af792018-04-29 20:53:53 +0100315 exit_code = MBEDTLS_EXIT_SUCCESS;
316
Paul Bakker5121ce52009-01-03 21:22:43 +0000317exit:
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#ifdef MBEDTLS_ERROR_C
Andres Amaya Garciabc1af792018-04-29 20:53:53 +0100320 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000321 {
322 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_strerror( ret, error_buf, 100 );
324 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000325 }
326#endif
327
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200328 mbedtls_net_free( &server_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_x509_crt_free( &cacert );
331 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200332 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_ctr_drbg_free( &ctr_drbg );
334 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
Paul Bakkercce9d772011-11-18 14:26:47 +0000336#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 fflush( stdout ); getchar();
339#endif
340
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200341 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
344 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
345 MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C &&
346 MBEDTLS_X509_CRT_PARSE_C */