blob: a497c60f56911b2b49ae91fa0b68f968b7188c56 [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é-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010024#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
25 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
26 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
27 !defined(MBEDTLS_PEM_PARSE_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
28 !defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010029int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010032 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
33 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
34 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
35 "not defined.\n");
36 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000037}
38#else
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010039
Andres AG788aa4a2016-09-14 14:32:09 +010040#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010041#include "mbedtls/debug.h"
42#include "mbedtls/ssl.h"
43#include "mbedtls/entropy.h"
44#include "mbedtls/ctr_drbg.h"
45#include "mbedtls/error.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010046#include "test/certs.h"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010047
48#include <string.h>
49
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020050#define SERVER_PORT "4433"
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010051#define SERVER_NAME "localhost"
52#define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
53
54#define DEBUG_LEVEL 1
55
Simon Butcher63cb97e2018-12-06 17:43:31 +000056
Gilles Peskine449bd832023-01-11 14:50:10 +010057static void my_debug(void *ctx, int level,
58 const char *file, int line,
59 const char *str)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020060{
Paul Bakkerc73079a2014-04-25 16:34:30 +020061 ((void) level);
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
64 fflush((FILE *) ctx);
Paul Bakker9a97c5d2013-09-15 17:07:33 +020065}
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000068{
Andres Amaya Garcia55172022018-04-29 20:53:53 +010069 int ret = 1, len;
70 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020071 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020072 uint32_t flags;
Paul Bakker5121ce52009-01-03 21:22:43 +000073 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020074 const char *pers = "ssl_client1";
Paul Bakker508ad5a2011-12-04 17:09:26 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_entropy_context entropy;
77 mbedtls_ctr_drbg_context ctr_drbg;
78 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020079 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_x509_crt cacert;
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010083 mbedtls_debug_set_threshold(DEBUG_LEVEL);
Paul Bakkerc73079a2014-04-25 16:34:30 +020084#endif
85
Przemek Stekiel89c636e2023-04-14 09:26:39 +020086#if defined(MBEDTLS_USE_PSA_CRYPTO)
87 psa_status_t status = psa_crypto_init();
88 if (status != PSA_SUCCESS) {
89 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
90 (int) status);
91 goto exit;
92 }
93#endif /* MBEDTLS_USE_PSA_CRYPTO */
94
Paul Bakker5121ce52009-01-03 21:22:43 +000095 /*
96 * 0. Initialize the RNG and the session data
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098 mbedtls_net_init(&server_fd);
99 mbedtls_ssl_init(&ssl);
100 mbedtls_ssl_config_init(&conf);
101 mbedtls_x509_crt_init(&cacert);
102 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_printf("\n . Seeding the random number generator...");
105 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_entropy_init(&entropy);
108 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
109 (const unsigned char *) pers,
110 strlen(pers))) != 0) {
111 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000112 goto exit;
113 }
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_printf(" ok\n");
Paul Bakker508ad5a2011-12-04 17:09:26 +0000116
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100118 * 0. Initialize certificates
119 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_printf(" . Loading the CA root certificate ...");
121 fflush(stdout);
Paul Bakker75242c32012-11-17 00:03:46 +0100122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
124 mbedtls_test_cas_pem_len);
125 if (ret < 0) {
126 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
127 (unsigned int) -ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100128 goto exit;
129 }
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100132
133 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 * 1. Start the connection
135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_printf(" . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT);
137 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME,
140 SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
141 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 goto exit;
143 }
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 /*
148 * 2. Setup stuff
149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 mbedtls_printf(" . Setting up the SSL/TLS structure...");
151 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if ((ret = mbedtls_ssl_config_defaults(&conf,
154 MBEDTLS_SSL_IS_CLIENT,
155 MBEDTLS_SSL_TRANSPORT_STREAM,
156 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
157 mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200158 goto exit;
159 }
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_printf(" ok\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100163 /* OPTIONAL is not optimal for security,
164 * but makes interop easier in this simplified example */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
166 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
167 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
168 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
171 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200172 goto exit;
173 }
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) {
176 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +0100177 goto exit;
178 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 /*
Paul Bakker75242c32012-11-17 00:03:46 +0100183 * 4. Handshake
184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_printf(" . Performing the SSL/TLS handshake...");
186 fflush(stdout);
Paul Bakker75242c32012-11-17 00:03:46 +0100187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
189 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
190 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
191 (unsigned int) -ret);
Paul Bakker75242c32012-11-17 00:03:46 +0100192 goto exit;
193 }
194 }
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 mbedtls_printf(" ok\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100197
198 /*
199 * 5. Verify the server certificate
200 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 mbedtls_printf(" . Verifying peer X.509 certificate...");
Paul Bakker75242c32012-11-17 00:03:46 +0100202
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100203 /* In real life, we probably want to bail out when ret != 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if ((flags = mbedtls_ssl_get_verify_result(&ssl)) != 0) {
Hanno Becker612a2f12020-10-09 09:19:39 +0100205#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100206 char vrfy_buf[512];
Peter Kolbus9a969b62018-12-11 13:55:56 -0600207#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_printf(" failed\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100210
Hanno Becker612a2f12020-10-09 09:19:39 +0100211#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
Paul Bakker75242c32012-11-17 00:03:46 +0100213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 mbedtls_printf("%s\n", vrfy_buf);
Peter Kolbus9a969b62018-12-11 13:55:56 -0600215#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 } else {
217 mbedtls_printf(" ok\n");
Paul Bakker75242c32012-11-17 00:03:46 +0100218 }
Paul Bakker75242c32012-11-17 00:03:46 +0100219
220 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 * 3. Write the GET request
222 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 mbedtls_printf(" > Write to server:");
224 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 len = sprintf((char *) buf, GET_REQUEST);
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
229 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
230 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 goto exit;
232 }
233 }
234
235 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 mbedtls_printf(" %d bytes written\n\n%s", len, (char *) buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
238 /*
239 * 7. Read the HTTP response
240 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_printf(" < Read from server:");
242 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 do {
245 len = sizeof(buf) - 1;
246 memset(buf, 0, sizeof(buf));
247 ret = mbedtls_ssl_read(&ssl, buf, len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 break;
255 }
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (ret < 0) {
258 mbedtls_printf("failed\n ! mbedtls_ssl_read returned %d\n\n", ret);
259 break;
260 }
261
262 if (ret == 0) {
263 mbedtls_printf("\n\nEOF\n\n");
Paul Bakker61da7522011-11-11 10:28:58 +0000264 break;
265 }
266
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_printf(" %d bytes read\n\n%s", len, (char *) buf);
269 } while (1);
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_ssl_close_notify(&ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
Andres Amaya Garcia55172022018-04-29 20:53:53 +0100273 exit_code = MBEDTLS_EXIT_SUCCESS;
274
Paul Bakker5121ce52009-01-03 21:22:43 +0000275exit:
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000279 char error_buf[100];
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 mbedtls_strerror(ret, error_buf, 100);
281 mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf);
Paul Bakkera3d195c2011-11-27 21:07:34 +0000282 }
283#endif
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 mbedtls_net_free(&server_fd);
Paul Bakker0c226102014-04-17 16:02:36 +0200286
Gilles Peskine449bd832023-01-11 14:50:10 +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);
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
296 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100297 MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C */