blob: 5f9766aed460d052aa0c3e19f13be2fabc0561e5 [file] [log] [blame]
Paul Bakker4fc45522010-03-18 20:11:58 +00001/*
2 * Certificate reading application
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 Bakker4fc45522010-03-18 20:11:58 +000018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker4fc45522010-03-18 20:11:58 +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 Garciaeb8bca62018-04-30 22:43:29 +010034#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000035
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020036#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_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
Chris Jonesee33c602020-12-16 11:52:37 +000040 !defined(MBEDTLS_CTR_DRBG_C) || defined(MBEDTLS_X509_REMOVE_INFO)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020041int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020042{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020043 mbedtls_printf(
44 "MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
45 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
46 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
47 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or "
48 "MBEDTLS_CTR_DRBG_C not defined and/or MBEDTLS_X509_REMOVE_INFO defined.\n");
49 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020050}
51#else
52
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053# include "mbedtls/entropy.h"
54# include "mbedtls/ctr_drbg.h"
55# include "mbedtls/net_sockets.h"
56# include "mbedtls/ssl.h"
57# include "mbedtls/x509.h"
58# include "mbedtls/debug.h"
Paul Bakker4fc45522010-03-18 20:11:58 +000059
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020060# include <stdio.h>
61# include <stdlib.h>
62# include <string.h>
Paul Bakker80d44fe2013-09-09 15:59:20 +020063
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020064# define MODE_NONE 0
65# define MODE_FILE 1
66# define MODE_SSL 2
Paul Bakker4fc45522010-03-18 20:11:58 +000067
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068# define DFL_MODE MODE_NONE
69# define DFL_FILENAME "cert.crt"
70# define DFL_CA_FILE ""
71# define DFL_CRL_FILE ""
72# define DFL_CA_PATH ""
73# define DFL_SERVER_NAME "localhost"
74# define DFL_SERVER_PORT "4433"
75# define DFL_DEBUG_LEVEL 0
76# define DFL_PERMISSIVE 0
Paul Bakker4fc45522010-03-18 20:11:58 +000077
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020078# define USAGE_IO \
79 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
80 " default: \"\" (none)\n" \
81 " crl_file=%%s The single CRL file you want to use\n" \
82 " default: \"\" (none)\n" \
83 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
84 " default: \"\" (none) (overrides ca_file)\n"
Rich Evans85b05ec2015-02-12 11:37:29 +000085
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086# define USAGE \
87 "\n usage: cert_app param=<>...\n" \
88 "\n acceptable parameters:\n" \
89 " mode=file|ssl default: none\n" \
90 " filename=%%s default: cert.crt\n" USAGE_IO \
91 " server_name=%%s default: localhost\n" \
92 " server_port=%%d default: 4433\n" \
93 " debug_level=%%d default: 0 (disabled)\n" \
94 " permissive=%%d default: 0 (disabled)\n" \
95 "\n"
Simon Butcher63cb97e2018-12-06 17:43:31 +000096
Paul Bakker4fc45522010-03-18 20:11:58 +000097/*
98 * global options
99 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200100struct options {
101 int mode; /* the mode to run the application in */
102 const char *filename; /* filename of the certificate file */
103 const char *ca_file; /* the file with the CA certificate(s) */
104 const char *crl_file; /* the file with the CRL to use */
105 const char *ca_path; /* the path with the CA certificate(s) reside */
106 const char *server_name; /* hostname of the server (client only) */
107 const char *server_port; /* port on which the ssl service runs */
108 int debug_level; /* level of debugging */
109 int permissive; /* permissive parsing */
Paul Bakker4fc45522010-03-18 20:11:58 +0000110} opt;
111
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200112static void
113my_debug(void *ctx, int level, const char *file, int line, const char *str)
Paul Bakker4fc45522010-03-18 20:11:58 +0000114{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115 ((void)level);
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200116
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117 mbedtls_fprintf((FILE *)ctx, "%s:%04d: %s", file, line, str);
118 fflush((FILE *)ctx);
Paul Bakker4fc45522010-03-18 20:11:58 +0000119}
120
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121static int
122my_verify(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
Paul Bakker777a5752013-05-21 16:20:04 +0200123{
124 char buf[1024];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200125 ((void)data);
Paul Bakker777a5752013-05-21 16:20:04 +0200126
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200127 mbedtls_printf("\nVerify requested for (Depth %d):\n", depth);
128 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
129 mbedtls_printf("%s", buf);
Paul Bakker777a5752013-05-21 16:20:04 +0200130
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200131 if ((*flags) == 0)
132 mbedtls_printf(" This certificate has no flags\n");
133 else {
134 mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", *flags);
135 mbedtls_printf("%s\n", buf);
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100136 }
Paul Bakker777a5752013-05-21 16:20:04 +0200137
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200138 return 0;
Paul Bakker777a5752013-05-21 16:20:04 +0200139}
140
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200141int main(int argc, char *argv[])
Paul Bakker4fc45522010-03-18 20:11:58 +0000142{
Andres Amaya Garcia7a9d01c2018-04-30 20:05:57 +0100143 int ret = 1;
144 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200145 mbedtls_net_context server_fd;
Paul Bakker4fc45522010-03-18 20:11:58 +0000146 unsigned char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_entropy_context entropy;
148 mbedtls_ctr_drbg_context ctr_drbg;
149 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200150 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_x509_crt cacert;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_x509_crl cacrl;
Paul Bakker36713e82013-09-17 13:25:29 +0200153 int i, j;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200154 uint32_t flags;
155 int verify = 0;
Paul Bakker4fc45522010-03-18 20:11:58 +0000156 char *p, *q;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200157 const char *pers = "cert_app";
Paul Bakker4fc45522010-03-18 20:11:58 +0000158
Paul Bakker1a207ec2011-02-06 13:22:40 +0000159 /*
160 * Set to sane values
161 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200162 mbedtls_net_init(&server_fd);
163 mbedtls_ctr_drbg_init(&ctr_drbg);
164 mbedtls_ssl_init(&ssl);
165 mbedtls_ssl_config_init(&conf);
166 mbedtls_x509_crt_init(&cacert);
167# if defined(MBEDTLS_X509_CRL_PARSE_C)
168 mbedtls_x509_crl_init(&cacrl);
169# else
Paul Bakker8880cb52014-06-12 23:22:26 +0200170 /* Zeroize structure as CRL parsing is not supported and we have to pass
171 it to the verify function */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200172 memset(&cacrl, 0, sizeof(mbedtls_x509_crl));
173# endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000174
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200175 if (argc == 0) {
176usage:
177 mbedtls_printf(USAGE);
Paul Bakker4fc45522010-03-18 20:11:58 +0000178 goto exit;
179 }
180
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200181 opt.mode = DFL_MODE;
182 opt.filename = DFL_FILENAME;
183 opt.ca_file = DFL_CA_FILE;
184 opt.crl_file = DFL_CRL_FILE;
185 opt.ca_path = DFL_CA_PATH;
186 opt.server_name = DFL_SERVER_NAME;
187 opt.server_port = DFL_SERVER_PORT;
188 opt.debug_level = DFL_DEBUG_LEVEL;
189 opt.permissive = DFL_PERMISSIVE;
Paul Bakker4fc45522010-03-18 20:11:58 +0000190
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200191 for (i = 1; i < argc; i++) {
Paul Bakker4fc45522010-03-18 20:11:58 +0000192 p = argv[i];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193 if ((q = strchr(p, '=')) == NULL)
Paul Bakker4fc45522010-03-18 20:11:58 +0000194 goto usage;
195 *q++ = '\0';
196
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200197 for (j = 0; p + j < q; j++) {
198 if (argv[i][j] >= 'A' && argv[i][j] <= 'Z')
Paul Bakkerace02862013-09-16 21:40:34 +0200199 argv[i][j] |= 0x20;
200 }
201
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200202 if (strcmp(p, "mode") == 0) {
203 if (strcmp(q, "file") == 0)
Paul Bakker4fc45522010-03-18 20:11:58 +0000204 opt.mode = MODE_FILE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200205 else if (strcmp(q, "ssl") == 0)
Paul Bakker4fc45522010-03-18 20:11:58 +0000206 opt.mode = MODE_SSL;
207 else
208 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200209 } else if (strcmp(p, "filename") == 0)
Paul Bakker4fc45522010-03-18 20:11:58 +0000210 opt.filename = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200211 else if (strcmp(p, "ca_file") == 0)
Paul Bakker777a5752013-05-21 16:20:04 +0200212 opt.ca_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200213 else if (strcmp(p, "crl_file") == 0)
Paul Bakker1fd32532014-05-28 11:36:16 +0200214 opt.crl_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200215 else if (strcmp(p, "ca_path") == 0)
Paul Bakker777a5752013-05-21 16:20:04 +0200216 opt.ca_path = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217 else if (strcmp(p, "server_name") == 0)
Paul Bakker4fc45522010-03-18 20:11:58 +0000218 opt.server_name = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200219 else if (strcmp(p, "server_port") == 0)
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200220 opt.server_port = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200221 else if (strcmp(p, "debug_level") == 0) {
222 opt.debug_level = atoi(q);
223 if (opt.debug_level < 0 || opt.debug_level > 65535)
Paul Bakker4fc45522010-03-18 20:11:58 +0000224 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200225 } else if (strcmp(p, "permissive") == 0) {
226 opt.permissive = atoi(q);
227 if (opt.permissive < 0 || opt.permissive > 1)
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000228 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200229 } else
Paul Bakker4fc45522010-03-18 20:11:58 +0000230 goto usage;
231 }
232
Paul Bakker777a5752013-05-21 16:20:04 +0200233 /*
234 * 1.1. Load the trusted CA
235 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200236 mbedtls_printf(" . Loading the CA root certificate ...");
237 fflush(stdout);
Paul Bakker777a5752013-05-21 16:20:04 +0200238
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200239 if (strlen(opt.ca_path)) {
240 if ((ret = mbedtls_x509_crt_parse_path(&cacert, opt.ca_path)) < 0) {
241 mbedtls_printf(
242 " failed\n ! mbedtls_x509_crt_parse_path returned -0x%x\n\n",
243 (unsigned int)-ret);
Andres Amaya Garcia7a9d01c2018-04-30 20:05:57 +0100244 goto exit;
245 }
246
Paul Bakker777a5752013-05-21 16:20:04 +0200247 verify = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200248 } else if (strlen(opt.ca_file)) {
249 if ((ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file)) < 0) {
250 mbedtls_printf(
251 " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
252 (unsigned int)-ret);
Andres Amaya Garcia7a9d01c2018-04-30 20:05:57 +0100253 goto exit;
254 }
Paul Bakker777a5752013-05-21 16:20:04 +0200255
Andres Amaya Garcia7a9d01c2018-04-30 20:05:57 +0100256 verify = 1;
Paul Bakker777a5752013-05-21 16:20:04 +0200257 }
258
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200259 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakker777a5752013-05-21 16:20:04 +0200260
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200261# if defined(MBEDTLS_X509_CRL_PARSE_C)
262 if (strlen(opt.crl_file)) {
263 if ((ret = mbedtls_x509_crl_parse_file(&cacrl, opt.crl_file)) != 0) {
264 mbedtls_printf(
265 " failed\n ! mbedtls_x509_crl_parse returned -0x%x\n\n",
266 (unsigned int)-ret);
Paul Bakker8880cb52014-06-12 23:22:26 +0200267 goto exit;
268 }
269
Paul Bakker1fd32532014-05-28 11:36:16 +0200270 verify = 1;
271 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200272# endif
Paul Bakker1fd32532014-05-28 11:36:16 +0200273
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200274 if (opt.mode == MODE_FILE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_x509_crt crt;
276 mbedtls_x509_crt *cur = &crt;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200277 mbedtls_x509_crt_init(&crt);
Paul Bakker4fc45522010-03-18 20:11:58 +0000278
279 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000280 * 1.1. Load the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000281 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200282 mbedtls_printf("\n . Loading the certificate(s) ...");
283 fflush(stdout);
Paul Bakker4fc45522010-03-18 20:11:58 +0000284
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200285 ret = mbedtls_x509_crt_parse_file(&crt, opt.filename);
Paul Bakker4fc45522010-03-18 20:11:58 +0000286
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200287 if (ret < 0) {
288 mbedtls_printf(
289 " failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n",
290 ret);
291 mbedtls_x509_crt_free(&crt);
Paul Bakker4fc45522010-03-18 20:11:58 +0000292 goto exit;
293 }
294
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200295 if (opt.permissive == 0 && ret > 0) {
296 mbedtls_printf(
297 " failed\n ! mbedtls_x509_crt_parse failed to parse %d certificates\n\n",
298 ret);
299 mbedtls_x509_crt_free(&crt);
Paul Bakker69e095c2011-12-10 21:55:01 +0000300 goto exit;
301 }
302
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200303 mbedtls_printf(" ok\n");
Paul Bakker4fc45522010-03-18 20:11:58 +0000304
305 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000306 * 1.2 Print the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000307 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200308 while (cur != NULL) {
309 mbedtls_printf(" . Peer certificate information ...\n");
310 ret = mbedtls_x509_crt_info((char *)buf, sizeof(buf) - 1, " ",
311 cur);
312 if (ret == -1) {
313 mbedtls_printf(
314 " failed\n ! mbedtls_x509_crt_info returned %d\n\n", ret);
315 mbedtls_x509_crt_free(&crt);
Paul Bakker14cb63a2011-11-25 12:44:31 +0000316 goto exit;
317 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000318
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200319 mbedtls_printf("%s\n", buf);
Paul Bakker14cb63a2011-11-25 12:44:31 +0000320
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200321 cur = cur->MBEDTLS_PRIVATE(next);
Paul Bakker14cb63a2011-11-25 12:44:31 +0000322 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000323
Paul Bakker777a5752013-05-21 16:20:04 +0200324 /*
325 * 1.3 Verify the certificate
326 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200327 if (verify) {
328 mbedtls_printf(" . Verifying X.509 certificate...");
Paul Bakker777a5752013-05-21 16:20:04 +0200329
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200330 if ((ret = mbedtls_x509_crt_verify(&crt, &cacert, &cacrl, NULL,
331 &flags, my_verify, NULL)) != 0) {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100332 char vrfy_buf[512];
333
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200334 mbedtls_printf(" failed\n");
Paul Bakker777a5752013-05-21 16:20:04 +0200335
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200336 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ",
337 flags);
Paul Bakker777a5752013-05-21 16:20:04 +0200338
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200339 mbedtls_printf("%s\n", vrfy_buf);
340 } else
341 mbedtls_printf(" ok\n");
Paul Bakker777a5752013-05-21 16:20:04 +0200342 }
343
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200344 mbedtls_x509_crt_free(&crt);
345 } else if (opt.mode == MODE_SSL) {
Paul Bakker4fc45522010-03-18 20:11:58 +0000346 /*
347 * 1. Initialize the RNG and the session data
348 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200349 mbedtls_printf("\n . Seeding the random number generator...");
350 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000351
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200352 mbedtls_entropy_init(&entropy);
353 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
354 &entropy, (const unsigned char *)pers,
355 strlen(pers))) != 0) {
356 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
357 ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200358 goto ssl_exit;
Paul Bakker508ad5a2011-12-04 17:09:26 +0000359 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000360
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200361 mbedtls_printf(" ok\n");
Paul Bakker777a5752013-05-21 16:20:04 +0200362
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200363# if defined(MBEDTLS_DEBUG_C)
364 mbedtls_debug_set_threshold(opt.debug_level);
365# endif
Paul Bakkerf2b92bb2016-08-11 10:45:14 +0100366
Paul Bakker4fc45522010-03-18 20:11:58 +0000367 /*
368 * 2. Start the connection
369 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200370 mbedtls_printf(" . SSL connection to tcp/%s/%s...", opt.server_name,
371 opt.server_port);
372 fflush(stdout);
Paul Bakker4fc45522010-03-18 20:11:58 +0000373
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200374 if ((ret = mbedtls_net_connect(&server_fd, opt.server_name,
375 opt.server_port,
376 MBEDTLS_NET_PROTO_TCP)) != 0) {
377 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n",
378 ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200379 goto ssl_exit;
Paul Bakker4fc45522010-03-18 20:11:58 +0000380 }
381
382 /*
383 * 3. Setup stuff
384 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200385 if ((ret = mbedtls_ssl_config_defaults(
386 &conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
387 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
388 mbedtls_printf(
389 " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n",
390 ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200391 goto exit;
392 }
393
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200394 if (verify) {
395 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
396 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
397 mbedtls_ssl_conf_verify(&conf, my_verify, NULL);
398 } else
399 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
Paul Bakker4fc45522010-03-18 20:11:58 +0000400
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200401 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
402 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Paul Bakker4fc45522010-03-18 20:11:58 +0000403
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200404 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
405 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n",
406 ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200407 goto ssl_exit;
408 }
409
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200410 if ((ret = mbedtls_ssl_set_hostname(&ssl, opt.server_name)) != 0) {
411 mbedtls_printf(
412 " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200413 goto ssl_exit;
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200414 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000415
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200416 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send,
417 mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200418
Paul Bakker4fc45522010-03-18 20:11:58 +0000419 /*
420 * 4. Handshake
421 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200422 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
423 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
424 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
425 mbedtls_printf(
426 " failed\n ! mbedtls_ssl_handshake returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200427 goto ssl_exit;
Paul Bakker4fc45522010-03-18 20:11:58 +0000428 }
429 }
430
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200431 mbedtls_printf(" ok\n");
Paul Bakker4fc45522010-03-18 20:11:58 +0000432
433 /*
434 * 5. Print the certificate
435 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200436# if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
437 mbedtls_printf(" . Peer certificate information ... skipped\n");
438# else
439 mbedtls_printf(" . Peer certificate information ...\n");
440 ret = mbedtls_x509_crt_info((char *)buf, sizeof(buf) - 1, " ",
441 mbedtls_ssl_get_peer_cert(&ssl));
442 if (ret == -1) {
443 mbedtls_printf(
444 " failed\n ! mbedtls_x509_crt_info returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200445 goto ssl_exit;
Paul Bakker4fc45522010-03-18 20:11:58 +0000446 }
447
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200448 mbedtls_printf("%s\n", buf);
449# endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker4fc45522010-03-18 20:11:58 +0000450
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200451 mbedtls_ssl_close_notify(&ssl);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200452
453ssl_exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200454 mbedtls_ssl_free(&ssl);
455 mbedtls_ssl_config_free(&conf);
456 } else
Paul Bakker4fc45522010-03-18 20:11:58 +0000457 goto usage;
458
Andres Amaya Garcia7a9d01c2018-04-30 20:05:57 +0100459 exit_code = MBEDTLS_EXIT_SUCCESS;
460
Paul Bakker4fc45522010-03-18 20:11:58 +0000461exit:
462
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200463 mbedtls_net_free(&server_fd);
464 mbedtls_x509_crt_free(&cacert);
465# if defined(MBEDTLS_X509_CRL_PARSE_C)
466 mbedtls_x509_crl_free(&cacrl);
467# endif
468 mbedtls_ctr_drbg_free(&ctr_drbg);
469 mbedtls_entropy_free(&entropy);
Paul Bakker4fc45522010-03-18 20:11:58 +0000470
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200471# if defined(_WIN32)
472 mbedtls_printf(" + Press Enter to exit this program.\n");
473 fflush(stdout);
474 getchar();
475# endif
Paul Bakker4fc45522010-03-18 20:11:58 +0000476
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200477 mbedtls_exit(exit_code);
Paul Bakker4fc45522010-03-18 20:11:58 +0000478}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200479#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && \
480 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */