| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  SSL client demonstration program | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6 | */ | 
|  | 7 |  | 
| Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 8 | #include "mbedtls/build_info.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 10 | #include "mbedtls/platform.h" | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 11 |  | 
| Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 12 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||     \ | 
|  | 13 | !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) ||    \ | 
|  | 14 | !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) ||            \ | 
|  | 15 | !defined(MBEDTLS_PEM_PARSE_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ | 
|  | 16 | !defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 17 | int main(void) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 18 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 19 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 20 | "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or " | 
|  | 21 | "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or " | 
|  | 22 | "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C " | 
|  | 23 | "not defined.\n"); | 
|  | 24 | mbedtls_exit(0); | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 25 | } | 
|  | 26 | #else | 
| Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 27 |  | 
| Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 28 | #include "mbedtls/net_sockets.h" | 
| Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 29 | #include "mbedtls/debug.h" | 
|  | 30 | #include "mbedtls/ssl.h" | 
|  | 31 | #include "mbedtls/entropy.h" | 
|  | 32 | #include "mbedtls/ctr_drbg.h" | 
|  | 33 | #include "mbedtls/error.h" | 
| Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 34 | #include "test/certs.h" | 
| Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 35 |  | 
|  | 36 | #include <string.h> | 
|  | 37 |  | 
| Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 38 | #define SERVER_PORT "4433" | 
| Manuel Pégourié-Gonnard | 4b3e5ef | 2015-03-27 11:24:27 +0100 | [diff] [blame] | 39 | #define SERVER_NAME "localhost" | 
|  | 40 | #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n" | 
|  | 41 |  | 
|  | 42 | #define DEBUG_LEVEL 1 | 
|  | 43 |  | 
| Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 44 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | static void my_debug(void *ctx, int level, | 
|  | 46 | const char *file, int line, | 
|  | 47 | const char *str) | 
| Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 48 | { | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 49 | ((void) level); | 
|  | 50 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str); | 
|  | 52 | fflush((FILE *) ctx); | 
| Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | int main(void) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | { | 
| Andres Amaya Garcia | 5517202 | 2018-04-29 20:53:53 +0100 | [diff] [blame] | 57 | int ret = 1, len; | 
|  | 58 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 59 | mbedtls_net_context server_fd; | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 60 | uint32_t flags; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | unsigned char buf[1024]; | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 62 | const char *pers = "ssl_client1"; | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 63 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | mbedtls_entropy_context entropy; | 
|  | 65 | mbedtls_ctr_drbg_context ctr_drbg; | 
|  | 66 | mbedtls_ssl_context ssl; | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 67 | mbedtls_ssl_config conf; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | mbedtls_x509_crt cacert; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | #if defined(MBEDTLS_DEBUG_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | mbedtls_debug_set_threshold(DEBUG_LEVEL); | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 72 | #endif | 
|  | 73 |  | 
| Przemek Stekiel | a0a1c1e | 2023-04-17 11:10:05 +0200 | [diff] [blame] | 74 | /* | 
|  | 75 | * 0. Initialize the RNG and the session data | 
|  | 76 | */ | 
|  | 77 | mbedtls_net_init(&server_fd); | 
|  | 78 | mbedtls_ssl_init(&ssl); | 
|  | 79 | mbedtls_ssl_config_init(&conf); | 
|  | 80 | mbedtls_x509_crt_init(&cacert); | 
|  | 81 | mbedtls_ctr_drbg_init(&ctr_drbg); | 
|  | 82 | mbedtls_entropy_init(&entropy); | 
|  | 83 |  | 
| Przemek Stekiel | 89c636e | 2023-04-14 09:26:39 +0200 | [diff] [blame] | 84 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 85 | psa_status_t status = psa_crypto_init(); | 
|  | 86 | if (status != PSA_SUCCESS) { | 
|  | 87 | mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", | 
|  | 88 | (int) status); | 
|  | 89 | goto exit; | 
|  | 90 | } | 
|  | 91 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 92 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | mbedtls_printf("\n  . Seeding the random number generator..."); | 
|  | 94 | fflush(stdout); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 95 |  | 
| Przemek Stekiel | a0a1c1e | 2023-04-17 11:10:05 +0200 | [diff] [blame] | 96 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, | 
|  | 98 | (const unsigned char *) pers, | 
|  | 99 | strlen(pers))) != 0) { | 
|  | 100 | mbedtls_printf(" failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 101 | goto exit; | 
|  | 102 | } | 
|  | 103 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | mbedtls_printf(" ok\n"); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 105 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | /* | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 107 | * 0. Initialize certificates | 
|  | 108 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | mbedtls_printf("  . Loading the CA root certificate ..."); | 
|  | 110 | fflush(stdout); | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 111 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem, | 
|  | 113 | mbedtls_test_cas_pem_len); | 
|  | 114 | if (ret < 0) { | 
|  | 115 | mbedtls_printf(" failed\n  !  mbedtls_x509_crt_parse returned -0x%x\n\n", | 
|  | 116 | (unsigned int) -ret); | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 117 | goto exit; | 
|  | 118 | } | 
|  | 119 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | mbedtls_printf(" ok (%d skipped)\n", ret); | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 121 |  | 
|  | 122 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | * 1. Start the connection | 
|  | 124 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | mbedtls_printf("  . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT); | 
|  | 126 | fflush(stdout); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME, | 
|  | 129 | SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) { | 
|  | 130 | mbedtls_printf(" failed\n  ! mbedtls_net_connect returned %d\n\n", ret); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | goto exit; | 
|  | 132 | } | 
|  | 133 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | mbedtls_printf(" ok\n"); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 |  | 
|  | 136 | /* | 
|  | 137 | * 2. Setup stuff | 
|  | 138 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | mbedtls_printf("  . Setting up the SSL/TLS structure..."); | 
|  | 140 | fflush(stdout); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | if ((ret = mbedtls_ssl_config_defaults(&conf, | 
|  | 143 | MBEDTLS_SSL_IS_CLIENT, | 
|  | 144 | MBEDTLS_SSL_TRANSPORT_STREAM, | 
|  | 145 | MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { | 
|  | 146 | mbedtls_printf(" failed\n  ! mbedtls_ssl_config_defaults returned %d\n\n", ret); | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 147 | goto exit; | 
|  | 148 | } | 
|  | 149 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | mbedtls_printf(" ok\n"); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 151 |  | 
| Manuel Pégourié-Gonnard | fcf2fc2 | 2014-03-11 11:10:27 +0100 | [diff] [blame] | 152 | /* OPTIONAL is not optimal for security, | 
|  | 153 | * but makes interop easier in this simplified example */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL); | 
|  | 155 | mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL); | 
|  | 156 | mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); | 
|  | 157 | mbedtls_ssl_conf_dbg(&conf, my_debug, stdout); | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 158 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) { | 
|  | 160 | mbedtls_printf(" failed\n  ! mbedtls_ssl_setup returned %d\n\n", ret); | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 161 | goto exit; | 
|  | 162 | } | 
|  | 163 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) { | 
|  | 165 | mbedtls_printf(" failed\n  ! mbedtls_ssl_set_hostname returned %d\n\n", ret); | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 166 | goto exit; | 
|  | 167 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 170 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | /* | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 172 | * 4. Handshake | 
|  | 173 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | mbedtls_printf("  . Performing the SSL/TLS handshake..."); | 
|  | 175 | fflush(stdout); | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 176 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) { | 
|  | 178 | if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { | 
|  | 179 | mbedtls_printf(" failed\n  ! mbedtls_ssl_handshake returned -0x%x\n\n", | 
|  | 180 | (unsigned int) -ret); | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 181 | goto exit; | 
|  | 182 | } | 
|  | 183 | } | 
|  | 184 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | mbedtls_printf(" ok\n"); | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 186 |  | 
|  | 187 | /* | 
|  | 188 | * 5. Verify the server certificate | 
|  | 189 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | mbedtls_printf("  . Verifying peer X.509 certificate..."); | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 191 |  | 
| Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 192 | /* In real life, we probably want to bail out when ret != 0 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) { | 
| Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 194 | #if !defined(MBEDTLS_X509_REMOVE_INFO) | 
| Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 195 | char vrfy_buf[512]; | 
| Peter Kolbus | 9a969b6 | 2018-12-11 13:55:56 -0600 | [diff] [blame] | 196 | #endif | 
| Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 197 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | mbedtls_printf(" failed\n"); | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 199 |  | 
| Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 200 | #if !defined(MBEDTLS_X509_REMOVE_INFO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "  ! ", flags); | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 202 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | mbedtls_printf("%s\n", vrfy_buf); | 
| Peter Kolbus | 9a969b6 | 2018-12-11 13:55:56 -0600 | [diff] [blame] | 204 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | } else { | 
|  | 206 | mbedtls_printf(" ok\n"); | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 207 | } | 
| Paul Bakker | 75242c3 | 2012-11-17 00:03:46 +0100 | [diff] [blame] | 208 |  | 
|  | 209 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | * 3. Write the GET request | 
|  | 211 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 212 | mbedtls_printf("  > Write to server:"); | 
|  | 213 | fflush(stdout); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | len = sprintf((char *) buf, GET_REQUEST); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 216 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) { | 
|  | 218 | if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { | 
|  | 219 | mbedtls_printf(" failed\n  ! mbedtls_ssl_write returned %d\n\n", ret); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 220 | goto exit; | 
|  | 221 | } | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | len = ret; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | mbedtls_printf(" %d bytes written\n\n%s", len, (char *) buf); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 |  | 
|  | 227 | /* | 
|  | 228 | * 7. Read the HTTP response | 
|  | 229 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | mbedtls_printf("  < Read from server:"); | 
|  | 231 | fflush(stdout); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 232 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | do { | 
|  | 234 | len = sizeof(buf) - 1; | 
|  | 235 | memset(buf, 0, sizeof(buf)); | 
|  | 236 | ret = mbedtls_ssl_read(&ssl, buf, len); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 237 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 239 | continue; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 241 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 243 | break; | 
|  | 244 | } | 
|  | 245 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | if (ret < 0) { | 
|  | 247 | mbedtls_printf("failed\n  ! mbedtls_ssl_read returned %d\n\n", ret); | 
|  | 248 | break; | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | if (ret == 0) { | 
|  | 252 | mbedtls_printf("\n\nEOF\n\n"); | 
| Paul Bakker | 61da752 | 2011-11-11 10:28:58 +0000 | [diff] [blame] | 253 | break; | 
|  | 254 | } | 
|  | 255 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 256 | len = ret; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 257 | mbedtls_printf(" %d bytes read\n\n%s", len, (char *) buf); | 
|  | 258 | } while (1); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 259 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | mbedtls_ssl_close_notify(&ssl); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 261 |  | 
| Andres Amaya Garcia | 5517202 | 2018-04-29 20:53:53 +0100 | [diff] [blame] | 262 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
|  | 263 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 264 | exit: | 
|  | 265 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 266 | #ifdef MBEDTLS_ERROR_C | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | if (exit_code != MBEDTLS_EXIT_SUCCESS) { | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 268 | char error_buf[100]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | mbedtls_strerror(ret, error_buf, 100); | 
|  | 270 | mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf); | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 271 | } | 
|  | 272 | #endif | 
|  | 273 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | mbedtls_net_free(&server_fd); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | mbedtls_x509_crt_free(&cacert); | 
|  | 276 | mbedtls_ssl_free(&ssl); | 
|  | 277 | mbedtls_ssl_config_free(&conf); | 
|  | 278 | mbedtls_ctr_drbg_free(&ctr_drbg); | 
|  | 279 | mbedtls_entropy_free(&entropy); | 
| Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 280 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Przemek Stekiel | a8c560a | 2023-04-19 10:15:26 +0200 | [diff] [blame] | 281 | mbedtls_psa_crypto_free(); | 
| Przemek Stekiel | 758aef6 | 2023-04-19 13:47:43 +0200 | [diff] [blame] | 282 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 283 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | mbedtls_exit(exit_code); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 285 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && | 
|  | 287 | MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && | 
| Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 288 | MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C */ |