blob: 9f11acd35e1c8fc61235b2349f7d5035409860c2 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker4fc45522010-03-18 20:11:58 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker4fc45522010-03-18 20:11:58 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
17 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
18 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
19 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
20 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010021int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020022{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010024 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
25 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
26 "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or "
27 "MBEDTLS_CTR_DRBG_C not defined.\n");
28 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020029}
30#else
31
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/entropy.h"
33#include "mbedtls/ctr_drbg.h"
Andres AG788aa4a2016-09-14 14:32:09 +010034#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/ssl.h"
36#include "mbedtls/x509.h"
Paul Bakkerf2b92bb2016-08-11 10:45:14 +010037#include "mbedtls/debug.h"
Paul Bakker4fc45522010-03-18 20:11:58 +000038
Rich Evans18b78c72015-02-11 14:06:19 +000039#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
Paul Bakker80d44fe2013-09-09 15:59:20 +020042
Paul Bakker4fc45522010-03-18 20:11:58 +000043#define MODE_NONE 0
44#define MODE_FILE 1
45#define MODE_SSL 2
46
47#define DFL_MODE MODE_NONE
48#define DFL_FILENAME "cert.crt"
Paul Bakker777a5752013-05-21 16:20:04 +020049#define DFL_CA_FILE ""
Paul Bakker1fd32532014-05-28 11:36:16 +020050#define DFL_CRL_FILE ""
Paul Bakker777a5752013-05-21 16:20:04 +020051#define DFL_CA_PATH ""
Paul Bakker4fc45522010-03-18 20:11:58 +000052#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020053#define DFL_SERVER_PORT "4433"
Paul Bakker4fc45522010-03-18 20:11:58 +000054#define DFL_DEBUG_LEVEL 0
Paul Bakker6c0ceb32011-12-04 12:24:18 +000055#define DFL_PERMISSIVE 0
Paul Bakker4fc45522010-03-18 20:11:58 +000056
Rich Evans85b05ec2015-02-12 11:37:29 +000057#define USAGE_IO \
58 " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
59 " default: \"\" (none)\n" \
60 " crl_file=%%s The single CRL file you want to use\n" \
61 " default: \"\" (none)\n" \
62 " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
63 " default: \"\" (none) (overrides ca_file)\n"
64
65#define USAGE \
66 "\n usage: cert_app param=<>...\n" \
67 "\n acceptable parameters:\n" \
68 " mode=file|ssl default: none\n" \
69 " filename=%%s default: cert.crt\n" \
70 USAGE_IO \
71 " server_name=%%s default: localhost\n" \
72 " server_port=%%d default: 4433\n" \
73 " debug_level=%%d default: 0 (disabled)\n" \
74 " permissive=%%d default: 0 (disabled)\n" \
75 "\n"
76
Simon Butcher63cb97e2018-12-06 17:43:31 +000077
Paul Bakker4fc45522010-03-18 20:11:58 +000078/*
79 * global options
80 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081struct options {
Paul Bakker4fc45522010-03-18 20:11:58 +000082 int mode; /* the mode to run the application in */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020083 const char *filename; /* filename of the certificate file */
84 const char *ca_file; /* the file with the CA certificate(s) */
Paul Bakker1fd32532014-05-28 11:36:16 +020085 const char *crl_file; /* the file with the CRL to use */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020086 const char *ca_path; /* the path with the CA certificate(s) reside */
87 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020088 const char *server_port; /* port on which the ssl service runs */
Paul Bakker4fc45522010-03-18 20:11:58 +000089 int debug_level; /* level of debugging */
Paul Bakker6c0ceb32011-12-04 12:24:18 +000090 int permissive; /* permissive parsing */
Paul Bakker4fc45522010-03-18 20:11:58 +000091} opt;
92
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093static void my_debug(void *ctx, int level,
94 const char *file, int line,
95 const char *str)
Paul Bakker4fc45522010-03-18 20:11:58 +000096{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +020097 ((void) level);
98
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
100 fflush((FILE *) ctx);
Paul Bakker4fc45522010-03-18 20:11:58 +0000101}
102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103static int my_verify(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
Paul Bakker777a5752013-05-21 16:20:04 +0200104{
105 char buf[1024];
106 ((void) data);
107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 mbedtls_printf("\nVerify requested for (Depth %d):\n", depth);
109 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
110 mbedtls_printf("%s", buf);
Paul Bakker777a5752013-05-21 16:20:04 +0200111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if ((*flags) == 0) {
113 mbedtls_printf(" This certificate has no flags\n");
114 } else {
115 mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", *flags);
116 mbedtls_printf("%s\n", buf);
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100117 }
Paul Bakker777a5752013-05-21 16:20:04 +0200118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 return 0;
Paul Bakker777a5752013-05-21 16:20:04 +0200120}
121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122int main(int argc, char *argv[])
Paul Bakker4fc45522010-03-18 20:11:58 +0000123{
Andres Amaya Garcia7a9d01c2018-04-30 20:05:57 +0100124 int ret = 1;
125 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200126 mbedtls_net_context server_fd;
Paul Bakker4fc45522010-03-18 20:11:58 +0000127 unsigned char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_entropy_context entropy;
129 mbedtls_ctr_drbg_context ctr_drbg;
130 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200131 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_x509_crt cacert;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_x509_crl cacrl;
Paul Bakker36713e82013-09-17 13:25:29 +0200134 int i, j;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200135 uint32_t flags;
136 int verify = 0;
Paul Bakker4fc45522010-03-18 20:11:58 +0000137 char *p, *q;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200138 const char *pers = "cert_app";
Paul Bakker4fc45522010-03-18 20:11:58 +0000139
Paul Bakker1a207ec2011-02-06 13:22:40 +0000140 /*
141 * Set to sane values
142 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 mbedtls_net_init(&server_fd);
144 mbedtls_ctr_drbg_init(&ctr_drbg);
145 mbedtls_ssl_init(&ssl);
146 mbedtls_ssl_config_init(&conf);
147 mbedtls_x509_crt_init(&cacert);
Przemek Stekiel0c9d0482023-04-14 12:28:16 +0200148 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_X509_CRL_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 mbedtls_x509_crl_init(&cacrl);
Paul Bakker8880cb52014-06-12 23:22:26 +0200151#else
152 /* Zeroize structure as CRL parsing is not supported and we have to pass
153 it to the verify function */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 memset(&cacrl, 0, sizeof(mbedtls_x509_crl));
Paul Bakker8880cb52014-06-12 23:22:26 +0200155#endif
Paul Bakker1a207ec2011-02-06 13:22:40 +0000156
Przemek Stekielb00688f2023-04-17 11:10:05 +0200157#if defined(MBEDTLS_USE_PSA_CRYPTO)
158 psa_status_t status = psa_crypto_init();
159 if (status != PSA_SUCCESS) {
160 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
161 (int) status);
162 goto exit;
163 }
164#endif /* MBEDTLS_USE_PSA_CRYPTO */
165
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000166 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167usage:
168 mbedtls_printf(USAGE);
Paul Bakker4fc45522010-03-18 20:11:58 +0000169 goto exit;
170 }
171
172 opt.mode = DFL_MODE;
173 opt.filename = DFL_FILENAME;
Paul Bakker777a5752013-05-21 16:20:04 +0200174 opt.ca_file = DFL_CA_FILE;
Paul Bakker1fd32532014-05-28 11:36:16 +0200175 opt.crl_file = DFL_CRL_FILE;
Paul Bakker777a5752013-05-21 16:20:04 +0200176 opt.ca_path = DFL_CA_PATH;
Paul Bakker4fc45522010-03-18 20:11:58 +0000177 opt.server_name = DFL_SERVER_NAME;
178 opt.server_port = DFL_SERVER_PORT;
179 opt.debug_level = DFL_DEBUG_LEVEL;
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000180 opt.permissive = DFL_PERMISSIVE;
Paul Bakker4fc45522010-03-18 20:11:58 +0000181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 for (i = 1; i < argc; i++) {
Paul Bakker4fc45522010-03-18 20:11:58 +0000183 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker4fc45522010-03-18 20:11:58 +0000185 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000187 *q++ = '\0';
188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 for (j = 0; p + j < q; j++) {
190 if (argv[i][j] >= 'A' && argv[i][j] <= 'Z') {
Paul Bakkerace02862013-09-16 21:40:34 +0200191 argv[i][j] |= 0x20;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 }
Paul Bakkerace02862013-09-16 21:40:34 +0200193 }
194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 if (strcmp(p, "mode") == 0) {
196 if (strcmp(q, "file") == 0) {
Paul Bakker4fc45522010-03-18 20:11:58 +0000197 opt.mode = MODE_FILE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 } else if (strcmp(q, "ssl") == 0) {
Paul Bakker4fc45522010-03-18 20:11:58 +0000199 opt.mode = MODE_SSL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 } else {
Paul Bakker4fc45522010-03-18 20:11:58 +0000201 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 }
203 } else if (strcmp(p, "filename") == 0) {
Paul Bakker4fc45522010-03-18 20:11:58 +0000204 opt.filename = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 } else if (strcmp(p, "ca_file") == 0) {
Paul Bakker777a5752013-05-21 16:20:04 +0200206 opt.ca_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 } else if (strcmp(p, "crl_file") == 0) {
Paul Bakker1fd32532014-05-28 11:36:16 +0200208 opt.crl_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 } else if (strcmp(p, "ca_path") == 0) {
Paul Bakker777a5752013-05-21 16:20:04 +0200210 opt.ca_path = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 } else if (strcmp(p, "server_name") == 0) {
Paul Bakker4fc45522010-03-18 20:11:58 +0000212 opt.server_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 } else if (strcmp(p, "server_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200214 opt.server_port = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 } else if (strcmp(p, "debug_level") == 0) {
216 opt.debug_level = atoi(q);
217 if (opt.debug_level < 0 || opt.debug_level > 65535) {
Paul Bakker4fc45522010-03-18 20:11:58 +0000218 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 }
220 } else if (strcmp(p, "permissive") == 0) {
221 opt.permissive = atoi(q);
222 if (opt.permissive < 0 || opt.permissive > 1) {
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000223 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 }
225 } else {
Paul Bakker4fc45522010-03-18 20:11:58 +0000226 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000228 }
229
Paul Bakker777a5752013-05-21 16:20:04 +0200230 /*
231 * 1.1. Load the trusted CA
232 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 mbedtls_printf(" . Loading the CA root certificate ...");
234 fflush(stdout);
Paul Bakker777a5752013-05-21 16:20:04 +0200235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 if (strlen(opt.ca_path)) {
237 if ((ret = mbedtls_x509_crt_parse_path(&cacert, opt.ca_path)) < 0) {
238 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_path returned -0x%x\n\n",
239 (unsigned int) -ret);
Andres Amaya Garcia7a9d01c2018-04-30 20:05:57 +0100240 goto exit;
241 }
242
Paul Bakker777a5752013-05-21 16:20:04 +0200243 verify = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 } else if (strlen(opt.ca_file)) {
245 if ((ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file)) < 0) {
246 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
247 (unsigned int) -ret);
Andres Amaya Garcia7a9d01c2018-04-30 20:05:57 +0100248 goto exit;
249 }
Paul Bakker777a5752013-05-21 16:20:04 +0200250
Andres Amaya Garcia7a9d01c2018-04-30 20:05:57 +0100251 verify = 1;
Paul Bakker777a5752013-05-21 16:20:04 +0200252 }
253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakker777a5752013-05-21 16:20:04 +0200255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_X509_CRL_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 if (strlen(opt.crl_file)) {
258 if ((ret = mbedtls_x509_crl_parse_file(&cacrl, opt.crl_file)) != 0) {
259 mbedtls_printf(" failed\n ! mbedtls_x509_crl_parse returned -0x%x\n\n",
260 (unsigned int) -ret);
Paul Bakker8880cb52014-06-12 23:22:26 +0200261 goto exit;
262 }
263
Paul Bakker1fd32532014-05-28 11:36:16 +0200264 verify = 1;
265 }
Paul Bakker8880cb52014-06-12 23:22:26 +0200266#endif
Paul Bakker1fd32532014-05-28 11:36:16 +0200267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 if (opt.mode == MODE_FILE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_x509_crt crt;
270 mbedtls_x509_crt *cur = &crt;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 mbedtls_x509_crt_init(&crt);
Paul Bakker4fc45522010-03-18 20:11:58 +0000272
273 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000274 * 1.1. Load the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000275 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 mbedtls_printf("\n . Loading the certificate(s) ...");
277 fflush(stdout);
Paul Bakker4fc45522010-03-18 20:11:58 +0000278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 ret = mbedtls_x509_crt_parse_file(&crt, opt.filename);
Paul Bakker4fc45522010-03-18 20:11:58 +0000280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 if (ret < 0) {
282 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret);
283 mbedtls_x509_crt_free(&crt);
Paul Bakker4fc45522010-03-18 20:11:58 +0000284 goto exit;
285 }
286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 if (opt.permissive == 0 && ret > 0) {
288 mbedtls_printf(
289 " failed\n ! mbedtls_x509_crt_parse failed to parse %d certificates\n\n",
290 ret);
291 mbedtls_x509_crt_free(&crt);
Paul Bakker69e095c2011-12-10 21:55:01 +0000292 goto exit;
293 }
294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 mbedtls_printf(" ok\n");
Paul Bakker4fc45522010-03-18 20:11:58 +0000296
297 /*
Paul Bakker14cb63a2011-11-25 12:44:31 +0000298 * 1.2 Print the certificate(s)
Paul Bakker4fc45522010-03-18 20:11:58 +0000299 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 while (cur != NULL) {
301 mbedtls_printf(" . Peer certificate information ...\n");
302 ret = mbedtls_x509_crt_info((char *) buf, sizeof(buf) - 1, " ",
303 cur);
304 if (ret == -1) {
305 mbedtls_printf(" failed\n ! mbedtls_x509_crt_info returned %d\n\n", ret);
306 mbedtls_x509_crt_free(&crt);
Paul Bakker14cb63a2011-11-25 12:44:31 +0000307 goto exit;
308 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 mbedtls_printf("%s\n", buf);
Paul Bakker14cb63a2011-11-25 12:44:31 +0000311
312 cur = cur->next;
313 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000314
Paul Bakker777a5752013-05-21 16:20:04 +0200315 /*
316 * 1.3 Verify the certificate
317 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 if (verify) {
319 mbedtls_printf(" . Verifying X.509 certificate...");
Paul Bakker777a5752013-05-21 16:20:04 +0200320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 if ((ret = mbedtls_x509_crt_verify(&crt, &cacert, &cacrl, NULL, &flags,
322 my_verify, NULL)) != 0) {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100323 char vrfy_buf[512];
324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 mbedtls_printf(" failed\n");
Paul Bakker777a5752013-05-21 16:20:04 +0200326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
Paul Bakker777a5752013-05-21 16:20:04 +0200328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 mbedtls_printf("%s\n", vrfy_buf);
330 } else {
331 mbedtls_printf(" ok\n");
Paul Bakker777a5752013-05-21 16:20:04 +0200332 }
Paul Bakker777a5752013-05-21 16:20:04 +0200333 }
334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 mbedtls_x509_crt_free(&crt);
336 } else if (opt.mode == MODE_SSL) {
Paul Bakker4fc45522010-03-18 20:11:58 +0000337 /*
338 * 1. Initialize the RNG and the session data
339 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 mbedtls_printf("\n . Seeding the random number generator...");
341 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
344 (const unsigned char *) pers,
345 strlen(pers))) != 0) {
346 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200347 goto ssl_exit;
Paul Bakker508ad5a2011-12-04 17:09:26 +0000348 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 mbedtls_printf(" ok\n");
Paul Bakker777a5752013-05-21 16:20:04 +0200351
Paul Bakkerf2b92bb2016-08-11 10:45:14 +0100352#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 mbedtls_debug_set_threshold(opt.debug_level);
Paul Bakkerf2b92bb2016-08-11 10:45:14 +0100354#endif
355
Paul Bakker4fc45522010-03-18 20:11:58 +0000356 /*
357 * 2. Start the connection
358 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 mbedtls_printf(" . SSL connection to tcp/%s/%s...", opt.server_name,
360 opt.server_port);
361 fflush(stdout);
Paul Bakker4fc45522010-03-18 20:11:58 +0000362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 if ((ret = mbedtls_net_connect(&server_fd, opt.server_name,
364 opt.server_port, MBEDTLS_NET_PROTO_TCP)) != 0) {
365 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200366 goto ssl_exit;
Paul Bakker4fc45522010-03-18 20:11:58 +0000367 }
368
369 /*
370 * 3. Setup stuff
371 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 if ((ret = mbedtls_ssl_config_defaults(&conf,
373 MBEDTLS_SSL_IS_CLIENT,
374 MBEDTLS_SSL_TRANSPORT_STREAM,
375 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
376 mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200377 goto exit;
378 }
379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 if (verify) {
381 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
382 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
383 mbedtls_ssl_conf_verify(&conf, my_verify, NULL);
384 } else {
385 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
Paul Bakker777a5752013-05-21 16:20:04 +0200386 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
389 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Paul Bakker4fc45522010-03-18 20:11:58 +0000390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
392 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200393 goto ssl_exit;
394 }
395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 if ((ret = mbedtls_ssl_set_hostname(&ssl, opt.server_name)) != 0) {
397 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200398 goto ssl_exit;
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200399 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200402
Paul Bakker4fc45522010-03-18 20:11:58 +0000403 /*
404 * 4. Handshake
405 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
407 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
408 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200409 goto ssl_exit;
Paul Bakker4fc45522010-03-18 20:11:58 +0000410 }
411 }
412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 mbedtls_printf(" ok\n");
Paul Bakker4fc45522010-03-18 20:11:58 +0000414
415 /*
416 * 5. Print the certificate
417 */
Hanno Becker57b33c92019-02-07 13:28:57 +0000418#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 mbedtls_printf(" . Peer certificate information ... skipped\n");
Hanno Becker57b33c92019-02-07 13:28:57 +0000420#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 mbedtls_printf(" . Peer certificate information ...\n");
422 ret = mbedtls_x509_crt_info((char *) buf, sizeof(buf) - 1, " ",
423 mbedtls_ssl_get_peer_cert(&ssl));
424 if (ret == -1) {
425 mbedtls_printf(" failed\n ! mbedtls_x509_crt_info returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200426 goto ssl_exit;
Paul Bakker4fc45522010-03-18 20:11:58 +0000427 }
428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 mbedtls_printf("%s\n", buf);
Hanno Becker57b33c92019-02-07 13:28:57 +0000430#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker4fc45522010-03-18 20:11:58 +0000431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 mbedtls_ssl_close_notify(&ssl);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200433
434ssl_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 mbedtls_ssl_free(&ssl);
436 mbedtls_ssl_config_free(&conf);
437 } else {
Paul Bakker4fc45522010-03-18 20:11:58 +0000438 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 }
Paul Bakker4fc45522010-03-18 20:11:58 +0000440
Andres Amaya Garcia7a9d01c2018-04-30 20:05:57 +0100441 exit_code = MBEDTLS_EXIT_SUCCESS;
442
Paul Bakker4fc45522010-03-18 20:11:58 +0000443exit:
444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 mbedtls_net_free(&server_fd);
446 mbedtls_x509_crt_free(&cacert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_X509_CRL_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 mbedtls_x509_crl_free(&cacrl);
Paul Bakker8880cb52014-06-12 23:22:26 +0200449#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 mbedtls_ctr_drbg_free(&ctr_drbg);
451 mbedtls_entropy_free(&entropy);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200452#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200453 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200454#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker4fc45522010-03-18 20:11:58 +0000455
Paul Bakkercce9d772011-11-18 14:26:47 +0000456#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 mbedtls_printf(" + Press Enter to exit this program.\n");
458 fflush(stdout); getchar();
Paul Bakker4fc45522010-03-18 20:11:58 +0000459#endif
460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 mbedtls_exit(exit_code);
Paul Bakker4fc45522010-03-18 20:11:58 +0000462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
464 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
465 MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */