blob: 254ecb15bbc1aa0b70294c39c48d103c6f70918b [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010032#include <stdlib.h>
Andres Amaya Garcia55172022018-04-29 20:53:53 +010033#define mbedtls_time time
34#define mbedtls_time_t time_t
35#define mbedtls_fprintf fprintf
36#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010037#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010038#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia55172022018-04-29 20:53:53 +010039#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
40#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
43 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
44 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
45 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010046 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
Manuel Pégourié-Gonnardba8b1eb2019-06-17 15:21:07 +020047 defined(MBEDTLS_SSL_PROTO_NO_TLS)
Rich Evans85b05ec2015-02-12 11:37:29 +000048int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
51 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
52 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010053 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or"
Manuel Pégourié-Gonnardba8b1eb2019-06-17 15:21:07 +020054 "not defined, and/or MBEDTLS_SSL_PROTO_NO_TLS defined.\n");
Andrzej Kurek825ebd42020-05-18 11:47:25 -040055 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000056}
57#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010058
Andres AG788aa4a2016-09-14 14:32:09 +010059#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010060#include "mbedtls/debug.h"
61#include "mbedtls/ssl.h"
62#include "mbedtls/entropy.h"
63#include "mbedtls/ctr_drbg.h"
64#include "mbedtls/error.h"
65#include "mbedtls/certs.h"
66
67#include <string.h>
68
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020069#define SERVER_PORT "4433"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010070#define SERVER_NAME "localhost"
71#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
72
73#define DEBUG_LEVEL 1
74
Simon Butcher63cb97e2018-12-06 17:43:31 +000075
Hanno Becker14a4a442019-07-02 17:00:34 +010076#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020077static void my_debug( void *ctx, int level,
78 const char *file, int line,
79 const char *str )
Paul Bakker9a97c5d2013-09-15 17:07:33 +020080{
Paul Bakkerc73079a2014-04-25 16:34:30 +020081 ((void) level);
82
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020083 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
Paul Bakkerc73079a2014-04-25 16:34:30 +020084 fflush( (FILE *) ctx );
Paul Bakker9a97c5d2013-09-15 17:07:33 +020085}
Hanno Becker14a4a442019-07-02 17:00:34 +010086#endif /* MBEDTLS_DEBUG_C */
Paul Bakker9a97c5d2013-09-15 17:07:33 +020087
Hanno Becker572d4482019-07-23 13:47:53 +010088#if defined(MBEDTLS_SSL_CONF_RNG)
89int rng_wrap( void *ctx, unsigned char *dst, size_t len );
90
91mbedtls_ctr_drbg_context *rng_ctx_global = NULL;
92int rng_wrap( void *ctx, unsigned char *dst, size_t len )
93{
94 /* We expect the NULL parameter here. */
95 if( ctx != NULL )
96 return( -1 );
97
98 return( mbedtls_ctr_drbg_random( rng_ctx_global, dst, len ) );
99}
100#endif /* MBEDTLS_SSL_CONF_RNG */
101
Jarno Lamsa642596e2019-10-04 12:52:42 +0300102#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
103int mbedtls_hardware_poll( void *data, unsigned char *output,
104 size_t len, size_t *olen )
105{
106 size_t i;
107 (void) data;
108 for( i = 0; i < len; ++i )
109 output[i] = rand();
110 *olen = len;
111 return( 0 );
112}
113#endif
114
Rich Evans85b05ec2015-02-12 11:37:29 +0000115int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
Andres Amaya Garcia55172022018-04-29 20:53:53 +0100117 int ret = 1, len;
118 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200119 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200120 uint32_t flags;
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200122 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_entropy_context entropy;
125 mbedtls_ctr_drbg_context ctr_drbg;
126 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200127 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_DEBUG_C)
131 mbedtls_debug_set_threshold( DEBUG_LEVEL );
Paul Bakkerc73079a2014-04-25 16:34:30 +0200132#endif
133
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 /*
135 * 0. Initialize the RNG and the session data
136 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200137 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200138 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200139 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_x509_crt_init( &cacert );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200141 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000144 fflush( stdout );
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200147 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200148 (const unsigned char *) pers,
149 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000150 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200151 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000152 goto exit;
153 }
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_printf( " ok\n" );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000156
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100158 * 0. Initialize certificates
159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100161 fflush( stdout );
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
164 mbedtls_test_cas_pem_len );
Paul Bakker75242c32012-11-17 00:03:46 +0100165 if( ret < 0 )
166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100168 goto exit;
169 }
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100172
173 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 * 1. Start the connection
175 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200176 mbedtls_printf( " . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 fflush( stdout );
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
180 SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 goto exit;
184 }
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
188 /*
189 * 2. Setup stuff
190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 fflush( stdout );
193
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200194 if( ( ret = mbedtls_ssl_config_defaults( &conf,
195 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200196 MBEDTLS_SSL_TRANSPORT_STREAM,
197 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200198 {
199 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
200 goto exit;
201 }
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_printf( " ok\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100205 /* OPTIONAL is not optimal for security,
206 * but makes interop easier in this simplified example */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200207 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
208 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Hanno Becker572d4482019-07-23 13:47:53 +0100209
210#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200211 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
Hanno Becker572d4482019-07-23 13:47:53 +0100212#else
213 rng_ctx_global = &ctr_drbg;
214#endif
215
Hanno Becker14a4a442019-07-02 17:00:34 +0100216#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200217 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Hanno Becker14a4a442019-07-02 17:00:34 +0100218#endif
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200219
220 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
221 {
222 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
223 goto exit;
224 }
225
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300226#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker23828162016-07-22 11:54:30 +0100227 if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100228 {
229 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
230 goto exit;
231 }
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300232#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
Hanno Becker41e5a682019-07-19 17:07:30 +0100234#if !defined(MBEDTLS_SSL_CONF_RECV) && \
235 !defined(MBEDTLS_SSL_CONF_SEND) && \
236 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Hanno Becker28d2a882019-07-24 16:01:38 +0100237 mbedtls_ssl_set_bio( &ssl, &server_fd,
238 mbedtls_net_send, mbedtls_net_recv, NULL );
Hanno Becker41e5a682019-07-19 17:07:30 +0100239#else
Hanno Becker28d2a882019-07-24 16:01:38 +0100240 mbedtls_ssl_set_bio_ctx( &ssl, &server_fd );
Hanno Becker41e5a682019-07-19 17:07:30 +0100241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100244 * 4. Handshake
245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100247 fflush( stdout );
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
Paul Bakker75242c32012-11-17 00:03:46 +0100250 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100251 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker75242c32012-11-17 00:03:46 +0100252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
Paul Bakker75242c32012-11-17 00:03:46 +0100254 goto exit;
255 }
256 }
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_printf( " ok\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100259
260 /*
261 * 5. Verify the server certificate
262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker75242c32012-11-17 00:03:46 +0100264
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100265 /* In real life, we probably want to bail out when ret != 0 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200266 if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
Paul Bakker75242c32012-11-17 00:03:46 +0100267 {
Hanno Becker02a21932019-06-10 15:08:43 +0100268#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100269 char vrfy_buf[512];
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600270#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_printf( " failed\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100273
Hanno Becker02a21932019-06-10 15:08:43 +0100274#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200275 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker75242c32012-11-17 00:03:46 +0100276
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100277 mbedtls_printf( "%s\n", vrfy_buf );
Peter Kolbusdc470ae2018-12-11 13:55:56 -0600278#endif
Paul Bakker75242c32012-11-17 00:03:46 +0100279 }
280 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( " ok\n" );
Paul Bakker75242c32012-11-17 00:03:46 +0100282
283 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 * 3. Write the GET request
285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_printf( " > Write to server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 fflush( stdout );
288
289 len = sprintf( (char *) buf, GET_REQUEST );
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100293 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 goto exit;
297 }
298 }
299
300 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
303 /*
304 * 7. Read the HTTP response
305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_printf( " < Read from server:" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 fflush( stdout );
308
309 do
310 {
311 len = sizeof( buf ) - 1;
312 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 ret = mbedtls_ssl_read( &ssl, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100315 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 continue;
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 break;
320
Paul Bakker61da7522011-11-11 10:28:58 +0000321 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 break;
325 }
326
Paul Bakker61da7522011-11-11 10:28:58 +0000327 if( ret == 0 )
328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_printf( "\n\nEOF\n\n" );
Paul Bakker61da7522011-11-11 10:28:58 +0000330 break;
331 }
332
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 len = ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 }
Paul Bakker61da7522011-11-11 10:28:58 +0000336 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_ssl_close_notify( &ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Andres Amaya Garcia55172022018-04-29 20:53:53 +0100340 exit_code = MBEDTLS_EXIT_SUCCESS;
341
Paul Bakker5121ce52009-01-03 21:22:43 +0000342exit:
343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia55172022018-04-29 20:53:53 +0100345 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000346 {
347 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_strerror( ret, error_buf, 100 );
349 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000350 }
351#endif
352
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200353 mbedtls_net_free( &server_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_x509_crt_free( &cacert );
356 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200357 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_ctr_drbg_free( &ctr_drbg );
359 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
Paul Bakkercce9d772011-11-18 14:26:47 +0000361#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 fflush( stdout ); getchar();
364#endif
365
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400366 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
369 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
370 MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C &&
371 MBEDTLS_X509_CRT_PARSE_C */