blob: 8c7370f3fcd068c35290b85d41c1f3bf78e67a01 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020023# include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025# include <stdio.h>
26# include <stdlib.h>
27# define mbedtls_time time
28# define mbedtls_time_t time_t
29# define mbedtls_fprintf fprintf
30# define mbedtls_printf printf
31# define mbedtls_exit exit
32# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
33# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Andres Amaya Garcia55172022018-04-29 20:53:53 +010034#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000035
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010036#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
37 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
38 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
39 !defined(MBEDTLS_PEM_PARSE_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
40 !defined(MBEDTLS_X509_CRT_PARSE_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020041int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
45 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
46 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
47 "not defined.\n");
48 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000049}
50#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010051
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020052# include "mbedtls/net_sockets.h"
53# include "mbedtls/debug.h"
54# include "mbedtls/ssl.h"
55# include "mbedtls/entropy.h"
56# include "mbedtls/ctr_drbg.h"
57# include "mbedtls/error.h"
58# include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010059
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020060# include <string.h>
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010061
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062# define SERVER_PORT "4433"
63# define SERVER_NAME "localhost"
64# define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010065
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020066# define DEBUG_LEVEL 1
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010067
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068static void
69my_debug(void *ctx, int level, const char *file, int line, const char *str)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020070{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071 ((void)level);
Paul Bakkerc73079a2014-04-25 16:34:30 +020072
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020073 mbedtls_fprintf((FILE *)ctx, "%s:%04d: %s", file, line, str);
74 fflush((FILE *)ctx);
Paul Bakker9a97c5d2013-09-15 17:07:33 +020075}
76
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000078{
Andres Amaya Garcia55172022018-04-29 20:53:53 +010079 int ret = 1, len;
80 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020081 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020082 uint32_t flags;
Paul Bakker5121ce52009-01-03 21:22:43 +000083 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020084 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_entropy_context entropy;
87 mbedtls_ctr_drbg_context ctr_drbg;
88 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020089 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020092# if defined(MBEDTLS_DEBUG_C)
93 mbedtls_debug_set_threshold(DEBUG_LEVEL);
94# endif
Paul Bakkerc73079a2014-04-25 16:34:30 +020095
Paul Bakker5121ce52009-01-03 21:22:43 +000096 /*
97 * 0. Initialize the RNG and the session data
98 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099 mbedtls_net_init(&server_fd);
100 mbedtls_ssl_init(&ssl);
101 mbedtls_ssl_config_init(&conf);
102 mbedtls_x509_crt_init(&cacert);
103 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105 mbedtls_printf("\n . Seeding the random number generator...");
106 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000107
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108 mbedtls_entropy_init(&entropy);
109 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
110 (const unsigned char *)pers,
111 strlen(pers))) != 0) {
112 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000113 goto exit;
114 }
115
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200116 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000117
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100119 * 0. Initialize certificates
120 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121 mbedtls_printf(" . Loading the CA root certificate ...");
122 fflush(stdout);
Paul Bakker75242c32012-11-17 00:03:46 +0100123
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200124 ret = mbedtls_x509_crt_parse(&cacert,
125 (const unsigned char *)mbedtls_test_cas_pem,
126 mbedtls_test_cas_pem_len);
127 if (ret < 0) {
128 mbedtls_printf(
129 " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
130 (unsigned int)-ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100131 goto exit;
132 }
133
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200134 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100135
136 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 * 1. Start the connection
138 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200139 mbedtls_printf(" . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT);
140 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200142 if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME, SERVER_PORT,
143 MBEDTLS_NET_PROTO_TCP)) != 0) {
144 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 goto exit;
146 }
147
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200148 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150 /*
151 * 2. Setup stuff
152 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200153 mbedtls_printf(" . Setting up the SSL/TLS structure...");
154 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200156 if ((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT,
157 MBEDTLS_SSL_TRANSPORT_STREAM,
158 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
159 mbedtls_printf(
160 " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200161 goto exit;
162 }
163
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100166 /* OPTIONAL is not optimal for security,
167 * but makes interop easier in this simplified example */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200168 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
169 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
170 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
171 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200172
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200173 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
174 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200175 goto exit;
176 }
177
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200178 if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) {
179 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
180 ret);
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100181 goto exit;
182 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200184 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
185 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190 mbedtls_printf(" . Performing the SSL/TLS handshake...");
191 fflush(stdout);
Paul Bakker75242c32012-11-17 00:03:46 +0100192
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
194 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
195 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
196 mbedtls_printf(
197 " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
198 (unsigned int)-ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100199 goto exit;
200 }
201 }
202
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200203 mbedtls_printf(" ok\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100204
205 /*
206 * 5. Verify the server certificate
207 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200208 mbedtls_printf(" . Verifying peer X.509 certificate...");
Paul Bakker75242c32012-11-17 00:03:46 +0100209
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100210 /* In real life, we probably want to bail out when ret != 0 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200211 if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
212# if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100213 char vrfy_buf[512];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200214# endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100215
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200216 mbedtls_printf(" failed\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100217
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200218# if !defined(MBEDTLS_X509_REMOVE_INFO)
219 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
Paul Bakker75242c32012-11-17 00:03:46 +0100220
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200221 mbedtls_printf("%s\n", vrfy_buf);
222# endif
223 } else
224 mbedtls_printf(" ok\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100225
226 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 * 3. Write the GET request
228 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200229 mbedtls_printf(" > Write to server:");
230 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200232 len = sprintf((char *)buf, GET_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200234 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
235 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
236 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
237 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n",
238 ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 goto exit;
240 }
241 }
242
243 len = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200244 mbedtls_printf(" %d bytes written\n\n%s", len, (char *)buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
246 /*
247 * 7. Read the HTTP response
248 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200249 mbedtls_printf(" < Read from server:");
250 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200252 do {
253 len = sizeof(buf) - 1;
254 memset(buf, 0, sizeof(buf));
255 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200257 if (ret == MBEDTLS_ERR_SSL_WANT_READ ||
258 ret == MBEDTLS_ERR_SSL_WANT_WRITE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 continue;
260
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200261 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 break;
263
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200264 if (ret < 0) {
265 mbedtls_printf("failed\n ! mbedtls_ssl_read returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 break;
267 }
268
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200269 if (ret == 0) {
270 mbedtls_printf("\n\nEOF\n\n");
Paul Bakker61da7522011-11-11 10:28:58 +0000271 break;
272 }
273
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 len = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200275 mbedtls_printf(" %d bytes read\n\n%s", len, (char *)buf);
276 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200278 mbedtls_ssl_close_notify(&ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +0000279
Andres Amaya Garcia55172022018-04-29 20:53:53 +0100280 exit_code = MBEDTLS_EXIT_SUCCESS;
281
Paul Bakker5121ce52009-01-03 21:22:43 +0000282exit:
283
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200284# ifdef MBEDTLS_ERROR_C
285 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000286 char error_buf[100];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200287 mbedtls_strerror(ret, error_buf, 100);
288 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
Paul Bakkera3d195c2011-11-27 21:07:34 +0000289 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200290# endif
Paul Bakkera3d195c2011-11-27 21:07:34 +0000291
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200292 mbedtls_net_free(&server_fd);
Paul Bakker0c226102014-04-17 16:02:36 +0200293
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200294 mbedtls_x509_crt_free(&cacert);
295 mbedtls_ssl_free(&ssl);
296 mbedtls_ssl_config_free(&conf);
297 mbedtls_ctr_drbg_free(&ctr_drbg);
298 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200300# if defined(_WIN32)
301 mbedtls_printf(" + Press Enter to exit this program.\n");
302 fflush(stdout);
303 getchar();
304# endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200306 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000307}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200308#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && \
309 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && \
310 MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C && \
311 MBEDTLS_X509_CRT_PARSE_C */