blob: 933ae7555fa5cdfb9e806ecb2088446ef4379012 [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
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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
29 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
30 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
31 !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
32 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
37 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
38 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
39 "not defined.\n");
40 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000041}
42#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010043
Andres AG788aa4a2016-09-14 14:32:09 +010044#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010045#include "mbedtls/debug.h"
46#include "mbedtls/ssl.h"
47#include "mbedtls/entropy.h"
48#include "mbedtls/ctr_drbg.h"
49#include "mbedtls/error.h"
50#include "mbedtls/certs.h"
51
52#include <string.h>
53
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020054#define SERVER_PORT "4433"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010055#define SERVER_NAME "localhost"
56#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
57
58#define DEBUG_LEVEL 1
59
Simon Butcher63cb97e2018-12-06 17:43:31 +000060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061static void my_debug(void *ctx, int level,
62 const char *file, int line,
63 const char *str)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020064{
Paul Bakkerc73079a2014-04-25 16:34:30 +020065 ((void) level);
66
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
68 fflush((FILE *) ctx);
Paul Bakker9a97c5d2013-09-15 17:07:33 +020069}
70
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
Andres Amaya Garcia55172022018-04-29 20:53:53 +010073 int ret = 1, len;
74 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020075 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020076 uint32_t flags;
Paul Bakker5121ce52009-01-03 21:22:43 +000077 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020078 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +000079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_entropy_context entropy;
81 mbedtls_ctr_drbg_context ctr_drbg;
82 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020083 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 mbedtls_debug_set_threshold(DEBUG_LEVEL);
Paul Bakkerc73079a2014-04-25 16:34:30 +020088#endif
89
Przemek Stekielb00688f2023-04-17 11:10:05 +020090 /*
91 * 0. Initialize the RNG and the session data
92 */
93 mbedtls_net_init(&server_fd);
94 mbedtls_ssl_init(&ssl);
95 mbedtls_ssl_config_init(&conf);
96 mbedtls_x509_crt_init(&cacert);
97 mbedtls_ctr_drbg_init(&ctr_drbg);
98 mbedtls_entropy_init(&entropy);
99
Przemek Stekield381d2d2023-04-14 09:26:39 +0200100#if defined(MBEDTLS_USE_PSA_CRYPTO)
101 psa_status_t status = psa_crypto_init();
102 if (status != PSA_SUCCESS) {
103 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
104 (int) status);
105 goto exit;
106 }
107#endif /* MBEDTLS_USE_PSA_CRYPTO */
108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 mbedtls_printf("\n . Seeding the random number generator...");
110 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000111
Przemek Stekielb00688f2023-04-17 11:10:05 +0200112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
114 (const unsigned char *) pers,
115 strlen(pers))) != 0) {
116 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000117 goto exit;
118 }
119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000121
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100123 * 0. Initialize certificates
124 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 mbedtls_printf(" . Loading the CA root certificate ...");
126 fflush(stdout);
Paul Bakker75242c32012-11-17 00:03:46 +0100127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
129 mbedtls_test_cas_pem_len);
130 if (ret < 0) {
131 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
132 (unsigned int) -ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100133 goto exit;
134 }
135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100137
138 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 * 1. Start the connection
140 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 mbedtls_printf(" . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT);
142 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME,
145 SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
146 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 goto exit;
148 }
149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 /*
153 * 2. Setup stuff
154 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 mbedtls_printf(" . Setting up the SSL/TLS structure...");
156 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 if ((ret = mbedtls_ssl_config_defaults(&conf,
159 MBEDTLS_SSL_IS_CLIENT,
160 MBEDTLS_SSL_TRANSPORT_STREAM,
161 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
162 mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200163 goto exit;
164 }
165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100168 /* OPTIONAL is not optimal for security,
169 * but makes interop easier in this simplified example */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
171 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
172 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
173 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
176 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200177 goto exit;
178 }
179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) {
181 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100182 goto exit;
183 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100188 * 4. Handshake
189 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 mbedtls_printf(" . Performing the SSL/TLS handshake...");
191 fflush(stdout);
Paul Bakker75242c32012-11-17 00:03:46 +0100192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
194 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
195 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
196 (unsigned int) -ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100197 goto exit;
198 }
199 }
200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 mbedtls_printf(" ok\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100202
203 /*
204 * 5. Verify the server certificate
205 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 mbedtls_printf(" . Verifying peer X.509 certificate...");
Paul Bakker75242c32012-11-17 00:03:46 +0100207
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100208 /* In real life, we probably want to bail out when ret != 0 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100210 char vrfy_buf[512];
211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 mbedtls_printf(" failed\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
Paul Bakker75242c32012-11-17 00:03:46 +0100215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 mbedtls_printf("%s\n", vrfy_buf);
217 } else {
218 mbedtls_printf(" ok\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100219 }
Paul Bakker75242c32012-11-17 00:03:46 +0100220
221 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 * 3. Write the GET request
223 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 mbedtls_printf(" > Write to server:");
225 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 len = sprintf((char *) buf, GET_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
230 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
231 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 goto exit;
233 }
234 }
235
236 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 mbedtls_printf(" %d bytes written\n\n%s", len, (char *) buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 /*
240 * 7. Read the HTTP response
241 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 mbedtls_printf(" < Read from server:");
243 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 do {
246 len = sizeof(buf) - 1;
247 memset(buf, 0, sizeof(buf));
248 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 break;
256 }
257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 if (ret < 0) {
259 mbedtls_printf("failed\n ! mbedtls_ssl_read returned %d\n\n", ret);
260 break;
261 }
262
263 if (ret == 0) {
264 mbedtls_printf("\n\nEOF\n\n");
Paul Bakker61da7522011-11-11 10:28:58 +0000265 break;
266 }
267
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 mbedtls_printf(" %d bytes read\n\n%s", len, (char *) buf);
270 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 mbedtls_ssl_close_notify(&ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
Andres Amaya Garcia55172022018-04-29 20:53:53 +0100274 exit_code = MBEDTLS_EXIT_SUCCESS;
275
Paul Bakker5121ce52009-01-03 21:22:43 +0000276exit:
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#ifdef MBEDTLS_ERROR_C
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000280 char error_buf[100];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 mbedtls_strerror(ret, error_buf, 100);
282 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
Paul Bakkera3d195c2011-11-27 21:07:34 +0000283 }
284#endif
285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 mbedtls_net_free(&server_fd);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 mbedtls_x509_crt_free(&cacert);
288 mbedtls_ssl_free(&ssl);
289 mbedtls_ssl_config_free(&conf);
290 mbedtls_ctr_drbg_free(&ctr_drbg);
291 mbedtls_entropy_free(&entropy);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200292#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200293 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200294#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
Paul Bakkercce9d772011-11-18 14:26:47 +0000296#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 mbedtls_printf(" + Press Enter to exit this program.\n");
298 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000299#endif
300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
304 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
305 MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C &&
306 MBEDTLS_X509_CRT_PARSE_C */