blob: d3354caf7312bdb33e446fddc3d19f5c441c317e [file] [log] [blame]
Paul Bakker1496d382011-05-23 12:07:29 +00001/*
2 * SSL client for SMTP servers
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker1496d382011-05-23 12:07:29 +00006 */
7
Nicholas Wilson61fa4362018-06-25 12:10:00 +01008/* Enable definition of gethostname() even when compiling with -std=c99. Must
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +02009 * be set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
Nicholas Wilson2682edf2017-12-05 12:08:15 +000010 * Harmless on other platforms. */
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +020011
Nicholas Wilson2682edf2017-12-05 12:08:15 +000012#define _POSIX_C_SOURCE 200112L
nia7eb0e622020-06-11 12:30:12 +010013#define _XOPEN_SOURCE 600
Felix Conway998760a2025-03-24 11:37:33 +000014#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
15
Nicholas Wilson2682edf2017-12-05 12:08:15 +000016
Bence Szépkútic662b362021-05-27 11:25:03 +020017#include "mbedtls/build_info.h"
Paul Bakker1496d382011-05-23 12:07:29 +000018
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
22 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
23 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
24 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
25 !defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +010026int main(void)
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010027{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010029 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
30 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
31 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
32 "not defined.\n");
33 mbedtls_exit(0);
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010034}
35#else
36
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/base64.h"
38#include "mbedtls/error.h"
Andres AG788aa4a2016-09-14 14:32:09 +010039#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ssl.h"
41#include "mbedtls/entropy.h"
42#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010043#include "test/certs.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/x509.h"
Rich Evans18b78c72015-02-11 14:06:19 +000045
Rich Evans18b78c72015-02-11 14:06:19 +000046#include <stdlib.h>
47#include <string.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010048
49#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
Paul Bakker1496d382011-05-23 12:07:29 +000050#include <unistd.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010051#else
52#include <io.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010053#endif
Paul Bakker1496d382011-05-23 12:07:29 +000054
Paul Bakker5a835222011-10-12 09:19:31 +000055#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5a835222011-10-12 09:19:31 +000056#include <winsock2.h>
57#include <windows.h>
58
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010059#if defined(_MSC_VER)
Paul Bakker5a835222011-10-12 09:19:31 +000060#if defined(_WIN32_WCE)
61#pragma comment( lib, "ws2.lib" )
62#else
63#pragma comment( lib, "ws2_32.lib" )
64#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010065#endif /* _MSC_VER */
Paul Bakker5a835222011-10-12 09:19:31 +000066#endif
67
Paul Bakker1496d382011-05-23 12:07:29 +000068#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020069#define DFL_SERVER_PORT "465"
Paul Bakker1496d382011-05-23 12:07:29 +000070#define DFL_USER_NAME "user"
71#define DFL_USER_PWD "password"
72#define DFL_MAIL_FROM ""
73#define DFL_MAIL_TO ""
74#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000075#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000076#define DFL_CRT_FILE ""
77#define DFL_KEY_FILE ""
78#define DFL_FORCE_CIPHER 0
79#define DFL_MODE 0
80#define DFL_AUTHENTICATION 0
81
82#define MODE_SSL_TLS 0
83#define MODE_STARTTLS 0
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_BASE64_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000086#define USAGE_AUTH \
irwirf5ce5d52019-01-19 19:05:56 +030087 " authentication=%%d default: 0 (disabled)\n" \
88 " user_name=%%s default: \"" DFL_USER_NAME "\"\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +010089 " user_pwd=%%s default: \"" \
90 DFL_USER_PWD "\"\n"
Rich Evans85b05ec2015-02-12 11:37:29 +000091#else
92#define USAGE_AUTH \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 " authentication options disabled. (Require MBEDTLS_BASE64_C)\n"
94#endif /* MBEDTLS_BASE64_C */
Rich Evans85b05ec2015-02-12 11:37:29 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000097#define USAGE_IO \
98 " ca_file=%%s default: \"\" (pre-loaded)\n" \
99 " crt_file=%%s default: \"\" (pre-loaded)\n" \
100 " key_file=%%s default: \"\" (pre-loaded)\n"
101#else
102#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 " No file operations available (MBEDTLS_FS_IO not defined)\n"
104#endif /* MBEDTLS_FS_IO */
Rich Evans85b05ec2015-02-12 11:37:29 +0000105
106#define USAGE \
irwirf5ce5d52019-01-19 19:05:56 +0300107 "\n usage: ssl_mail_client param=<>...\n" \
108 "\n acceptable parameters:\n" \
109 " server_name=%%s default: " DFL_SERVER_NAME "\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 " server_port=%%d default: " \
111 DFL_SERVER_PORT "\n" \
112 " debug_level=%%d default: 0 (disabled)\n" \
113 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
irwirf5ce5d52019-01-19 19:05:56 +0300114 USAGE_AUTH \
115 " mail_from=%%s default: \"\"\n" \
116 " mail_to=%%s default: \"\"\n" \
117 USAGE_IO \
118 " force_ciphersuite=<name> default: all enabled\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000119 " acceptable ciphersuite names:\n"
120
Simon Butcher63cb97e2018-12-06 17:43:31 +0000121
Paul Bakker1496d382011-05-23 12:07:29 +0000122/*
123 * global options
124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125struct options {
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200126 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200127 const char *server_port; /* port on which the ssl service runs */
Paul Bakker1496d382011-05-23 12:07:29 +0000128 int debug_level; /* level of debugging */
129 int authentication; /* if authentication is required */
130 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200131 const char *user_name; /* username to use for authentication */
132 const char *user_pwd; /* password to use for authentication */
133 const char *mail_from; /* E-Mail address to use as sender */
134 const char *mail_to; /* E-Mail address to use as recipient */
135 const char *ca_file; /* the file with the CA certificate(s) */
136 const char *crt_file; /* the file with the client certificate */
137 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000138 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
139} opt;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141static void my_debug(void *ctx, int level,
142 const char *file, int line,
143 const char *str)
Paul Bakker1496d382011-05-23 12:07:29 +0000144{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200145 ((void) level);
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
148 fflush((FILE *) ctx);
Paul Bakker1496d382011-05-23 12:07:29 +0000149}
150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151static int do_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1496d382011-05-23 12:07:29 +0000152{
153 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200154 uint32_t flags;
Paul Bakker1496d382011-05-23 12:07:29 +0000155 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000156 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000157
158 /*
159 * 4. Handshake
160 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_printf(" . Performing the SSL/TLS handshake...");
162 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 while ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
165 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_strerror(ret, (char *) buf, 1024);
Paul Bakker5690efc2011-05-26 13:16:06 +0000168#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned %d: %s\n\n", ret, buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000170 return -1;
171 }
172 }
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_printf(" ok\n [ Ciphersuite is %s ]\n",
175 mbedtls_ssl_get_ciphersuite(ssl));
176
177 /*
178 * 5. Verify the server certificate
179 */
180 mbedtls_printf(" . Verifying peer X.509 certificate...");
181
182 /* In real life, we probably want to bail out when ret != 0 */
183 if ((flags = mbedtls_ssl_get_verify_result(ssl)) != 0) {
184#if !defined(MBEDTLS_X509_REMOVE_INFO)
185 char vrfy_buf[512];
186#endif
187
188 mbedtls_printf(" failed\n");
189
190#if !defined(MBEDTLS_X509_REMOVE_INFO)
191 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
192
193 mbedtls_printf("%s\n", vrfy_buf);
194#endif
195 } else {
196 mbedtls_printf(" ok\n");
197 }
198
199#if !defined(MBEDTLS_X509_REMOVE_INFO)
200 mbedtls_printf(" . Peer certificate information ...\n");
201 mbedtls_x509_crt_info((char *) buf, sizeof(buf) - 1, " ",
202 mbedtls_ssl_get_peer_cert(ssl));
203 mbedtls_printf("%s\n", buf);
204#endif
205
206 return 0;
Paul Bakker1496d382011-05-23 12:07:29 +0000207}
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209static int write_ssl_data(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
210{
211 int ret;
212
213 mbedtls_printf("\n%s", buf);
214 while (len && (ret = mbedtls_ssl_write(ssl, buf, len)) <= 0) {
215 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
216 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
217 return -1;
218 }
219 }
220
221 return 0;
222}
223
224static int write_ssl_and_get_response(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
Paul Bakker1496d382011-05-23 12:07:29 +0000225{
226 int ret;
227 unsigned char data[128];
228 char code[4];
229 size_t i, idx = 0;
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_printf("\n%s", buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 while (len && (ret = mbedtls_ssl_write(ssl, buf, len)) <= 0) {
233 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
234 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000235 return -1;
236 }
237 }
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 do {
240 len = sizeof(data) - 1;
241 memset(data, 0, sizeof(data));
242 ret = mbedtls_ssl_read(ssl, data, len);
Paul Bakker1496d382011-05-23 12:07:29 +0000243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker1496d382011-05-23 12:07:29 +0000245 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 }
Paul Bakker1496d382011-05-23 12:07:29 +0000247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
Paul Bakker1496d382011-05-23 12:07:29 +0000249 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 }
Paul Bakker1496d382011-05-23 12:07:29 +0000251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if (ret <= 0) {
253 mbedtls_printf("failed\n ! mbedtls_ssl_read returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000254 return -1;
255 }
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000258 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 for (i = 0; i < len; i++) {
260 if (data[i] != '\n') {
261 if (idx < 4) {
262 code[idx++] = data[i];
263 }
Paul Bakker1496d382011-05-23 12:07:29 +0000264 continue;
265 }
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ') {
Paul Bakker1496d382011-05-23 12:07:29 +0000268 code[3] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return atoi(code);
Paul Bakker1496d382011-05-23 12:07:29 +0000270 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200271
Paul Bakker1496d382011-05-23 12:07:29 +0000272 idx = 0;
273 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 } while (1);
Paul Bakker1496d382011-05-23 12:07:29 +0000275}
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277static int write_and_get_response(mbedtls_net_context *sock_fd, unsigned char *buf, size_t len)
Paul Bakker1496d382011-05-23 12:07:29 +0000278{
279 int ret;
280 unsigned char data[128];
281 char code[4];
282 size_t i, idx = 0;
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_printf("\n%s", buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (len && (ret = mbedtls_net_send(sock_fd, buf, len)) <= 0) {
286 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
287 return -1;
Paul Bakker1496d382011-05-23 12:07:29 +0000288 }
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 do {
291 len = sizeof(data) - 1;
292 memset(data, 0, sizeof(data));
293 ret = mbedtls_net_recv(sock_fd, data, len);
Paul Bakker1496d382011-05-23 12:07:29 +0000294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (ret <= 0) {
296 mbedtls_printf("failed\n ! mbedtls_net_recv returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000297 return -1;
298 }
299
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200300 data[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000302 len = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 for (i = 0; i < len; i++) {
304 if (data[i] != '\n') {
305 if (idx < 4) {
306 code[idx++] = data[i];
307 }
Paul Bakker1496d382011-05-23 12:07:29 +0000308 continue;
309 }
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 if (idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ') {
Paul Bakker1496d382011-05-23 12:07:29 +0000312 code[3] = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return atoi(code);
Paul Bakker1496d382011-05-23 12:07:29 +0000314 }
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000315
Paul Bakker1496d382011-05-23 12:07:29 +0000316 idx = 0;
317 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 } while (1);
Paul Bakker1496d382011-05-23 12:07:29 +0000319}
320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321int main(int argc, char *argv[])
Paul Bakker1496d382011-05-23 12:07:29 +0000322{
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100323 int ret = 1, len;
324 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200325 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#if defined(MBEDTLS_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000327 unsigned char base[1024];
Mohammad Azim Khan9ebdcff2018-08-06 11:48:06 +0100328 /* buf is used as the destination buffer for printing base with the format:
329 * "%s\r\n". Hence, the size of buf should be at least the size of base
330 * plus 2 bytes for the \r and \n characters.
331 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 unsigned char buf[sizeof(base) + 2];
Mohammad Azim Khan9ebdcff2018-08-06 11:48:06 +0100333#else
334 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000335#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000336 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200337 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_entropy_context entropy;
340 mbedtls_ctr_drbg_context ctr_drbg;
341 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200342 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_x509_crt cacert;
344 mbedtls_x509_crt clicert;
345 mbedtls_pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000346 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000347 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000348 char *p, *q;
349 const int *list;
350
351 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200352 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000353 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 mbedtls_net_init(&server_fd);
355 mbedtls_ssl_init(&ssl);
356 mbedtls_ssl_config_init(&conf);
357 memset(&buf, 0, sizeof(buf));
358 mbedtls_x509_crt_init(&cacert);
359 mbedtls_x509_crt_init(&clicert);
360 mbedtls_pk_init(&pkey);
361 mbedtls_ctr_drbg_init(&ctr_drbg);
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200362 mbedtls_entropy_init(&entropy);
363
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200364 psa_status_t status = psa_crypto_init();
365 if (status != PSA_SUCCESS) {
366 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
367 (int) status);
368 goto exit;
369 }
Paul Bakker1496d382011-05-23 12:07:29 +0000370
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000371 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100372usage:
373 mbedtls_printf(USAGE);
Paul Bakker1496d382011-05-23 12:07:29 +0000374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 list = mbedtls_ssl_list_ciphersuites();
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 while (*list) {
377 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name(*list));
Paul Bakker1496d382011-05-23 12:07:29 +0000378 list++;
379 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_printf("\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000381 goto exit;
382 }
383
384 opt.server_name = DFL_SERVER_NAME;
385 opt.server_port = DFL_SERVER_PORT;
386 opt.debug_level = DFL_DEBUG_LEVEL;
387 opt.authentication = DFL_AUTHENTICATION;
388 opt.mode = DFL_MODE;
389 opt.user_name = DFL_USER_NAME;
390 opt.user_pwd = DFL_USER_PWD;
391 opt.mail_from = DFL_MAIL_FROM;
392 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000393 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000394 opt.crt_file = DFL_CRT_FILE;
395 opt.key_file = DFL_KEY_FILE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
Paul Bakker1496d382011-05-23 12:07:29 +0000397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 for (i = 1; i < argc; i++) {
Paul Bakker1496d382011-05-23 12:07:29 +0000399 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker1496d382011-05-23 12:07:29 +0000401 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 }
Paul Bakker1496d382011-05-23 12:07:29 +0000403 *q++ = '\0';
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (strcmp(p, "server_name") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000406 opt.server_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 } else if (strcmp(p, "server_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200408 opt.server_port = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 } else if (strcmp(p, "debug_level") == 0) {
410 opt.debug_level = atoi(q);
411 if (opt.debug_level < 0 || opt.debug_level > 65535) {
Paul Bakker1496d382011-05-23 12:07:29 +0000412 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 }
414 } else if (strcmp(p, "authentication") == 0) {
415 opt.authentication = atoi(q);
416 if (opt.authentication < 0 || opt.authentication > 1) {
Paul Bakker1496d382011-05-23 12:07:29 +0000417 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 }
419 } else if (strcmp(p, "mode") == 0) {
420 opt.mode = atoi(q);
421 if (opt.mode < 0 || opt.mode > 1) {
Paul Bakker1496d382011-05-23 12:07:29 +0000422 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 }
424 } else if (strcmp(p, "user_name") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000425 opt.user_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 } else if (strcmp(p, "user_pwd") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000427 opt.user_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 } else if (strcmp(p, "mail_from") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000429 opt.mail_from = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 } else if (strcmp(p, "mail_to") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000431 opt.mail_to = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 } else if (strcmp(p, "ca_file") == 0) {
Paul Bakker5690efc2011-05-26 13:16:06 +0000433 opt.ca_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 } else if (strcmp(p, "crt_file") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000435 opt.crt_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 } else if (strcmp(p, "key_file") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000437 opt.key_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 } else if (strcmp(p, "force_ciphersuite") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000439 opt.force_ciphersuite[0] = -1;
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(q);
Paul Bakker1496d382011-05-23 12:07:29 +0000442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (opt.force_ciphersuite[0] <= 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000444 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 }
Paul Bakker1496d382011-05-23 12:07:29 +0000446
447 opt.force_ciphersuite[1] = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 } else {
Paul Bakker1496d382011-05-23 12:07:29 +0000449 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 }
Paul Bakker1496d382011-05-23 12:07:29 +0000451 }
452
453 /*
454 * 0. Initialize the RNG and the session data
455 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 mbedtls_printf("\n . Seeding the random number generator...");
457 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
460 (const unsigned char *) pers,
461 strlen(pers))) != 0) {
462 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000463 goto exit;
464 }
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000467
468 /*
469 * 1.1. Load the trusted CA
470 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 mbedtls_printf(" . Loading the CA root certificate ...");
472 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#if defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (strlen(opt.ca_file)) {
476 ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file);
477 } else
Paul Bakker5690efc2011-05-26 13:16:06 +0000478#endif
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100479#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
481 mbedtls_test_cas_pem_len);
Paul Bakker5690efc2011-05-26 13:16:06 +0000482#else
483 {
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100484 mbedtls_printf("MBEDTLS_PEM_PARSE_C not defined.");
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100485 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000486 }
487#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (ret < 0) {
489 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000490 goto exit;
491 }
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000494
495 /*
496 * 1.2. Load own certificate and private key
497 *
498 * (can be skipped if client authentication is not required)
499 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 mbedtls_printf(" . Loading the client cert. and key...");
501 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (strlen(opt.crt_file)) {
505 ret = mbedtls_x509_crt_parse_file(&clicert, opt.crt_file);
506 } else
Paul Bakker5690efc2011-05-26 13:16:06 +0000507#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *) mbedtls_test_cli_crt,
509 mbedtls_test_cli_crt_len);
510 if (ret != 0) {
511 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000512 goto exit;
513 }
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if (strlen(opt.key_file)) {
517 ret = mbedtls_pk_parse_keyfile(&pkey, opt.key_file, "",
518 mbedtls_ctr_drbg_random, &ctr_drbg);
519 } else
Paul Bakker5690efc2011-05-26 13:16:06 +0000520#endif
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100521#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200522 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 ret = mbedtls_pk_parse_key(&pkey,
524 (const unsigned char *) mbedtls_test_cli_key,
525 mbedtls_test_cli_key_len,
526 NULL,
527 0,
528 mbedtls_ctr_drbg_random,
529 &ctr_drbg);
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200530 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000531#else
532 {
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100533 mbedtls_printf("MBEDTLS_PEM_PARSE_C not defined.");
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100534 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000535 }
536#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (ret != 0) {
538 mbedtls_printf(" failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000539 goto exit;
540 }
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000543
544 /*
545 * 2. Start the connection
546 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 mbedtls_printf(" . Connecting to tcp/%s/%s...", opt.server_name,
548 opt.server_port);
549 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if ((ret = mbedtls_net_connect(&server_fd, opt.server_name,
552 opt.server_port, MBEDTLS_NET_PROTO_TCP)) != 0) {
553 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000554 goto exit;
555 }
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000558
559 /*
560 * 3. Setup stuff
561 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_printf(" . Setting up the SSL/TLS structure...");
563 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if ((ret = mbedtls_ssl_config_defaults(&conf,
566 MBEDTLS_SSL_IS_CLIENT,
567 MBEDTLS_SSL_TRANSPORT_STREAM,
568 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
569 mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200570 goto exit;
571 }
572
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100573 /* OPTIONAL is not optimal for security,
574 * but makes interop easier in this simplified example */
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
Paul Bakker1496d382011-05-23 12:07:29 +0000576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
578 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (opt.force_ciphersuite[0] != DFL_FORCE_CIPHER) {
581 mbedtls_ssl_conf_ciphersuites(&conf, opt.force_ciphersuite);
582 }
Paul Bakker1496d382011-05-23 12:07:29 +0000583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
585 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &clicert, &pkey)) != 0) {
586 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200587 goto exit;
588 }
Paul Bakker1496d382011-05-23 12:07:29 +0000589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
591 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200592 goto exit;
593 }
594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if ((ret = mbedtls_ssl_set_hostname(&ssl, opt.server_name)) != 0) {
596 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200597 goto exit;
598 }
Paul Bakker1496d382011-05-23 12:07:29 +0000599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (opt.mode == MODE_SSL_TLS) {
605 if (do_handshake(&ssl) != 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000606 goto exit;
607 }
608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 mbedtls_printf(" > Get header from server:");
610 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 ret = write_ssl_and_get_response(&ssl, buf, 0);
613 if (ret < 200 || ret > 299) {
614 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000615 goto exit;
616 }
617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 mbedtls_printf(" > Write EHLO to server:");
621 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 gethostname(hostname, 32);
624 len = sprintf((char *) buf, "EHLO %s\r\n", hostname);
625 ret = write_ssl_and_get_response(&ssl, buf, len);
626 if (ret < 200 || ret > 299) {
627 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
628 goto exit;
629 }
630 } else {
631 mbedtls_printf(" > Get header from server:");
632 fflush(stdout);
633
634 ret = write_and_get_response(&server_fd, buf, 0);
635 if (ret < 200 || ret > 299) {
636 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000637 goto exit;
638 }
639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 mbedtls_printf(" > Write EHLO to server:");
643 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 gethostname(hostname, 32);
646 len = sprintf((char *) buf, "EHLO %s\r\n", hostname);
647 ret = write_and_get_response(&server_fd, buf, len);
648 if (ret < 200 || ret > 299) {
649 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000650 goto exit;
651 }
652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 mbedtls_printf(" > Write STARTTLS to server:");
656 fflush(stdout);
657
658 gethostname(hostname, 32);
659 len = sprintf((char *) buf, "STARTTLS\r\n");
660 ret = write_and_get_response(&server_fd, buf, len);
661 if (ret < 200 || ret > 299) {
662 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000663 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 }
665
666 mbedtls_printf(" ok\n");
667
668 if (do_handshake(&ssl) != 0) {
669 goto exit;
670 }
Paul Bakker1496d382011-05-23 12:07:29 +0000671 }
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_BASE64_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (opt.authentication) {
675 mbedtls_printf(" > Write AUTH LOGIN to server:");
676 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 len = sprintf((char *) buf, "AUTH LOGIN\r\n");
679 ret = write_ssl_and_get_response(&ssl, buf, len);
680 if (ret < 200 || ret > 399) {
681 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000682 goto exit;
683 }
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 mbedtls_printf(" > Write username to server: %s", opt.user_name);
688 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 ret = mbedtls_base64_encode(base, sizeof(base), &n, (const unsigned char *) opt.user_name,
691 strlen(opt.user_name));
Alfred Klomp7c034242014-07-14 22:11:13 +0200692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (ret != 0) {
694 mbedtls_printf(" failed\n ! mbedtls_base64_encode returned %d\n\n", ret);
Alfred Klomp7c034242014-07-14 22:11:13 +0200695 goto exit;
696 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 len = sprintf((char *) buf, "%s\r\n", base);
698 ret = write_ssl_and_get_response(&ssl, buf, len);
699 if (ret < 300 || ret > 399) {
700 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000701 goto exit;
702 }
703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 mbedtls_printf(" > Write password to server: %s", opt.user_pwd);
707 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 ret = mbedtls_base64_encode(base, sizeof(base), &n, (const unsigned char *) opt.user_pwd,
710 strlen(opt.user_pwd));
Alfred Klomp7c034242014-07-14 22:11:13 +0200711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (ret != 0) {
713 mbedtls_printf(" failed\n ! mbedtls_base64_encode returned %d\n\n", ret);
Alfred Klomp7c034242014-07-14 22:11:13 +0200714 goto exit;
715 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 len = sprintf((char *) buf, "%s\r\n", base);
717 ret = write_ssl_and_get_response(&ssl, buf, len);
718 if (ret < 200 || ret > 399) {
719 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000720 goto exit;
721 }
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000724 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000725#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 mbedtls_printf(" > Write MAIL FROM to server:");
728 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000729
Mingjie Shen0fc20cd2024-03-12 16:00:28 -0400730 len = mbedtls_snprintf((char *) buf, sizeof(buf), "MAIL FROM:<%s>\r\n", opt.mail_from);
Mingjie Shend97b96f2024-03-18 14:30:06 -0400731 if (len < 0 || (size_t) len >= sizeof(buf)) {
Mingjie Shen8e35d962024-03-12 16:23:41 -0400732 mbedtls_printf(" failed\n ! mbedtls_snprintf encountered error or truncated output\n\n");
733 goto exit;
734 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 ret = write_ssl_and_get_response(&ssl, buf, len);
736 if (ret < 200 || ret > 299) {
737 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000738 goto exit;
739 }
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 mbedtls_printf(" > Write RCPT TO to server:");
744 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000745
Mingjie Shen0fc20cd2024-03-12 16:00:28 -0400746 len = mbedtls_snprintf((char *) buf, sizeof(buf), "RCPT TO:<%s>\r\n", opt.mail_to);
Mingjie Shend97b96f2024-03-18 14:30:06 -0400747 if (len < 0 || (size_t) len >= sizeof(buf)) {
Mingjie Shen8e35d962024-03-12 16:23:41 -0400748 mbedtls_printf(" failed\n ! mbedtls_snprintf encountered error or truncated output\n\n");
749 goto exit;
750 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 ret = write_ssl_and_get_response(&ssl, buf, len);
752 if (ret < 200 || ret > 299) {
753 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000754 goto exit;
755 }
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 mbedtls_printf(" > Write DATA to server:");
760 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 len = sprintf((char *) buf, "DATA\r\n");
763 ret = write_ssl_and_get_response(&ssl, buf, len);
764 if (ret < 300 || ret > 399) {
765 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000766 goto exit;
767 }
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 mbedtls_printf(" > Write content to server:");
772 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000773
Mingjie Shen0fc20cd2024-03-12 16:00:28 -0400774 len = mbedtls_snprintf((char *) buf, sizeof(buf),
Mingjie Shend97b96f2024-03-18 14:30:06 -0400775 "From: %s\r\nSubject: Mbed TLS Test mail\r\n\r\n"
776 "This is a simple test mail from the "
777 "Mbed TLS mail client example.\r\n"
778 "\r\n"
779 "Enjoy!", opt.mail_from);
780 if (len < 0 || (size_t) len >= sizeof(buf)) {
Mingjie Shen8e35d962024-03-12 16:23:41 -0400781 mbedtls_printf(" failed\n ! mbedtls_snprintf encountered error or truncated output\n\n");
782 goto exit;
783 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 ret = write_ssl_data(&ssl, buf, len);
Paul Bakker1496d382011-05-23 12:07:29 +0000785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 len = sprintf((char *) buf, "\r\n.\r\n");
787 ret = write_ssl_and_get_response(&ssl, buf, len);
788 if (ret < 200 || ret > 299) {
789 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000790 goto exit;
791 }
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 mbedtls_ssl_close_notify(&ssl);
Paul Bakker1496d382011-05-23 12:07:29 +0000796
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100797 exit_code = MBEDTLS_EXIT_SUCCESS;
798
Paul Bakker1496d382011-05-23 12:07:29 +0000799exit:
800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 mbedtls_net_free(&server_fd);
802 mbedtls_x509_crt_free(&clicert);
803 mbedtls_x509_crt_free(&cacert);
804 mbedtls_pk_free(&pkey);
805 mbedtls_ssl_free(&ssl);
806 mbedtls_ssl_config_free(&conf);
807 mbedtls_ctr_drbg_free(&ctr_drbg);
808 mbedtls_entropy_free(&entropy);
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200809 mbedtls_psa_crypto_free();
Paul Bakker1496d382011-05-23 12:07:29 +0000810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 mbedtls_exit(exit_code);
Paul Bakker1496d382011-05-23 12:07:29 +0000812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
814 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C **
815 MBEDTLS_CTR_DRBG_C */