blob: 3d6e67c6a9273538422fb4b0bed8324e1f10cc83 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL client demonstration program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Gilles Peskinee6b82502024-09-04 16:06:10 +020012#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
13 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_SSL_CLI_C) || \
14 !defined(MBEDTLS_PEM_PARSE_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010015int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000016{
Gilles Peskinee6b82502024-09-04 16:06:10 +020017 mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
18 "MBEDTLS_NET_C and/or MBEDTLS_SSL_CLI_C and/or "
19 "MBEDTLS_PEM_PARSE_C and/or MBEDTLS_X509_CRT_PARSE_C "
Gilles Peskine449bd832023-01-11 14:50:10 +010020 "not defined.\n");
21 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000022}
23#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010024
Andres AG788aa4a2016-09-14 14:32:09 +010025#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010026#include "mbedtls/debug.h"
27#include "mbedtls/ssl.h"
28#include "mbedtls/entropy.h"
29#include "mbedtls/ctr_drbg.h"
30#include "mbedtls/error.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010031#include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010032
33#include <string.h>
34
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020035#define SERVER_PORT "4433"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010036#define SERVER_NAME "localhost"
37#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
38
39#define DEBUG_LEVEL 1
40
Simon Butcher63cb97e2018-12-06 17:43:31 +000041
Gilles Peskine449bd832023-01-11 14:50:10 +010042static void my_debug(void *ctx, int level,
43 const char *file, int line,
44 const char *str)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020045{
Paul Bakkerc73079a2014-04-25 16:34:30 +020046 ((void) level);
47
Gilles Peskine449bd832023-01-11 14:50:10 +010048 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
49 fflush((FILE *) ctx);
Paul Bakker9a97c5d2013-09-15 17:07:33 +020050}
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000053{
Andres Amaya Garcia55172022018-04-29 20:53:53 +010054 int ret = 1, len;
55 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020056 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020057 uint32_t flags;
Paul Bakker5121ce52009-01-03 21:22:43 +000058 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020059 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_entropy_context entropy;
62 mbedtls_ctr_drbg_context ctr_drbg;
63 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020064 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_debug_set_threshold(DEBUG_LEVEL);
Paul Bakkerc73079a2014-04-25 16:34:30 +020069#endif
70
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +020071 /*
72 * 0. Initialize the RNG and the session data
73 */
74 mbedtls_net_init(&server_fd);
75 mbedtls_ssl_init(&ssl);
76 mbedtls_ssl_config_init(&conf);
77 mbedtls_x509_crt_init(&cacert);
78 mbedtls_ctr_drbg_init(&ctr_drbg);
79 mbedtls_entropy_init(&entropy);
80
Przemek Stekiel89c636e2023-04-14 09:26:39 +020081#if defined(MBEDTLS_USE_PSA_CRYPTO)
82 psa_status_t status = psa_crypto_init();
83 if (status != PSA_SUCCESS) {
84 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
85 (int) status);
86 goto exit;
87 }
88#endif /* MBEDTLS_USE_PSA_CRYPTO */
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_printf("\n . Seeding the random number generator...");
91 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +000092
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +020093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
95 (const unsigned char *) pers,
96 strlen(pers))) != 0) {
97 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +000098 goto exit;
99 }
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000102
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100104 * 0. Initialize certificates
105 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_printf(" . Loading the CA root certificate ...");
107 fflush(stdout);
Paul Bakker75242c32012-11-17 00:03:46 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
110 mbedtls_test_cas_pem_len);
111 if (ret < 0) {
112 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
113 (unsigned int) -ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100114 goto exit;
115 }
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100118
119 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 * 1. Start the connection
121 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_printf(" . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT);
123 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME,
126 SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
127 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 goto exit;
129 }
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
133 /*
134 * 2. Setup stuff
135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_printf(" . Setting up the SSL/TLS structure...");
137 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if ((ret = mbedtls_ssl_config_defaults(&conf,
140 MBEDTLS_SSL_IS_CLIENT,
141 MBEDTLS_SSL_TRANSPORT_STREAM,
142 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
143 mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200144 goto exit;
145 }
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100149 /* OPTIONAL is not optimal for security,
150 * but makes interop easier in this simplified example */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
152 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
153 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
154 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
157 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200158 goto exit;
159 }
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) {
162 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100163 goto exit;
164 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 mbedtls_printf(" . Performing the SSL/TLS handshake...");
172 fflush(stdout);
Paul Bakker75242c32012-11-17 00:03:46 +0100173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
175 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
176 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
177 (unsigned int) -ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100178 goto exit;
179 }
180 }
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_printf(" ok\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100183
184 /*
185 * 5. Verify the server certificate
186 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 mbedtls_printf(" . Verifying peer X.509 certificate...");
Paul Bakker75242c32012-11-17 00:03:46 +0100188
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100189 /* In real life, we probably want to bail out when ret != 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
Hanno Becker612a2f12020-10-09 09:19:39 +0100191#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100192 char vrfy_buf[512];
Peter Kolbus9a969b62018-12-11 13:55:56 -0600193#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 mbedtls_printf(" failed\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100196
Hanno Becker612a2f12020-10-09 09:19:39 +0100197#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
Paul Bakker75242c32012-11-17 00:03:46 +0100199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 mbedtls_printf("%s\n", vrfy_buf);
Peter Kolbus9a969b62018-12-11 13:55:56 -0600201#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 } else {
203 mbedtls_printf(" ok\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100204 }
Paul Bakker75242c32012-11-17 00:03:46 +0100205
206 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 * 3. Write the GET request
208 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_printf(" > Write to server:");
210 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 len = sprintf((char *) buf, GET_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
215 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
216 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 goto exit;
218 }
219 }
220
221 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 mbedtls_printf(" %d bytes written\n\n%s", len, (char *) buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224 /*
225 * 7. Read the HTTP response
226 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_printf(" < Read from server:");
228 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 do {
231 len = sizeof(buf) - 1;
232 memset(buf, 0, sizeof(buf));
233 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
Gilles Peskinedd518262024-08-27 12:17:22 +0200240 mbedtls_printf("The return value %d from mbedtls_ssl_read() means that the server\n"
241 "closed the connection first. We're ok with that.\n",
242 MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY);
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 break;
244 }
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 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 Bakker61da7522011-11-11 10:28:58 +0000253 break;
254 }
255
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 mbedtls_printf(" %d bytes read\n\n%s", len, (char *) buf);
258 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 mbedtls_ssl_close_notify(&ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
Gilles Peskinedd518262024-08-27 12:17:22 +0200262 if (ret == 0 || ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
263 exit_code = MBEDTLS_EXIT_SUCCESS;
264 }
Andres Amaya Garcia55172022018-04-29 20:53:53 +0100265
Paul Bakker5121ce52009-01-03 21:22:43 +0000266exit:
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000270 char error_buf[100];
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_strerror(ret, error_buf, 100);
272 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
Paul Bakkera3d195c2011-11-27 21:07:34 +0000273 }
274#endif
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 mbedtls_net_free(&server_fd);
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 mbedtls_x509_crt_free(&cacert);
278 mbedtls_ssl_free(&ssl);
279 mbedtls_ssl_config_free(&conf);
280 mbedtls_ctr_drbg_free(&ctr_drbg);
281 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200282#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200283 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200284#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000287}
Gilles Peskinee6b82502024-09-04 16:06:10 +0200288
289#endif /* configuration allows running this program */