blob: f755158dce8cd8b7fd8802255a8d74bc127a5e07 [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 Rodgman7ff79652023-11-03 12:04:52 +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
9 * be set before config.h, which pulls in glibc's features.h indirectly.
Nicholas Wilson2682edf2017-12-05 12:08:15 +000010 * Harmless on other platforms. */
11#define _POSIX_C_SOURCE 200112L
nia7eb0e622020-06-11 12:30:12 +010012#define _XOPEN_SOURCE 600
Nicholas Wilson2682edf2017-12-05 12:08:15 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020016#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020018#endif
Paul Bakker1496d382011-05-23 12:07:29 +000019
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000020#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
23 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
24 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
25 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
26 !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010027int main(void)
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010030 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
31 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
32 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
33 "not defined.\n");
34 mbedtls_exit(0);
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010035}
36#else
37
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/base64.h"
39#include "mbedtls/error.h"
Andres AG788aa4a2016-09-14 14:32:09 +010040#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ssl.h"
42#include "mbedtls/entropy.h"
43#include "mbedtls/ctr_drbg.h"
44#include "mbedtls/certs.h"
45#include "mbedtls/x509.h"
Rich Evans18b78c72015-02-11 14:06:19 +000046
Rich Evans18b78c72015-02-11 14:06:19 +000047#include <stdlib.h>
48#include <string.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010049
50#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
Paul Bakker1496d382011-05-23 12:07:29 +000051#include <unistd.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010052#else
53#include <io.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010054#endif
Paul Bakker1496d382011-05-23 12:07:29 +000055
Paul Bakker5a835222011-10-12 09:19:31 +000056#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5a835222011-10-12 09:19:31 +000057#include <winsock2.h>
58#include <windows.h>
59
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010060#if defined(_MSC_VER)
Paul Bakker5a835222011-10-12 09:19:31 +000061#if defined(_WIN32_WCE)
62#pragma comment( lib, "ws2.lib" )
63#else
64#pragma comment( lib, "ws2_32.lib" )
65#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010066#endif /* _MSC_VER */
Paul Bakker5a835222011-10-12 09:19:31 +000067#endif
68
Paul Bakker1496d382011-05-23 12:07:29 +000069#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020070#define DFL_SERVER_PORT "465"
Paul Bakker1496d382011-05-23 12:07:29 +000071#define DFL_USER_NAME "user"
72#define DFL_USER_PWD "password"
73#define DFL_MAIL_FROM ""
74#define DFL_MAIL_TO ""
75#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000076#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000077#define DFL_CRT_FILE ""
78#define DFL_KEY_FILE ""
79#define DFL_FORCE_CIPHER 0
80#define DFL_MODE 0
81#define DFL_AUTHENTICATION 0
82
83#define MODE_SSL_TLS 0
84#define MODE_STARTTLS 0
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_BASE64_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000087#define USAGE_AUTH \
irwirf5ce5d52019-01-19 19:05:56 +030088 " authentication=%%d default: 0 (disabled)\n" \
89 " user_name=%%s default: \"" DFL_USER_NAME "\"\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 " user_pwd=%%s default: \"" \
91 DFL_USER_PWD "\"\n"
Rich Evans85b05ec2015-02-12 11:37:29 +000092#else
93#define USAGE_AUTH \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 " authentication options disabled. (Require MBEDTLS_BASE64_C)\n"
95#endif /* MBEDTLS_BASE64_C */
Rich Evans85b05ec2015-02-12 11:37:29 +000096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000098#define USAGE_IO \
99 " ca_file=%%s default: \"\" (pre-loaded)\n" \
100 " crt_file=%%s default: \"\" (pre-loaded)\n" \
101 " key_file=%%s default: \"\" (pre-loaded)\n"
102#else
103#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 " No file operations available (MBEDTLS_FS_IO not defined)\n"
105#endif /* MBEDTLS_FS_IO */
Rich Evans85b05ec2015-02-12 11:37:29 +0000106
107#define USAGE \
irwirf5ce5d52019-01-19 19:05:56 +0300108 "\n usage: ssl_mail_client param=<>...\n" \
109 "\n acceptable parameters:\n" \
110 " server_name=%%s default: " DFL_SERVER_NAME "\n" \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 " server_port=%%d default: " \
112 DFL_SERVER_PORT "\n" \
113 " debug_level=%%d default: 0 (disabled)\n" \
114 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
irwirf5ce5d52019-01-19 19:05:56 +0300115 USAGE_AUTH \
116 " mail_from=%%s default: \"\"\n" \
117 " mail_to=%%s default: \"\"\n" \
118 USAGE_IO \
119 " force_ciphersuite=<name> default: all enabled\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000120 " acceptable ciphersuite names:\n"
121
Simon Butcher63cb97e2018-12-06 17:43:31 +0000122
Paul Bakker1496d382011-05-23 12:07:29 +0000123/*
124 * global options
125 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126struct options {
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200127 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200128 const char *server_port; /* port on which the ssl service runs */
Paul Bakker1496d382011-05-23 12:07:29 +0000129 int debug_level; /* level of debugging */
130 int authentication; /* if authentication is required */
131 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200132 const char *user_name; /* username to use for authentication */
133 const char *user_pwd; /* password to use for authentication */
134 const char *mail_from; /* E-Mail address to use as sender */
135 const char *mail_to; /* E-Mail address to use as recipient */
136 const char *ca_file; /* the file with the CA certificate(s) */
137 const char *crt_file; /* the file with the client certificate */
138 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000139 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
140} opt;
141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142static void my_debug(void *ctx, int level,
143 const char *file, int line,
144 const char *str)
Paul Bakker1496d382011-05-23 12:07:29 +0000145{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200146 ((void) level);
147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
149 fflush((FILE *) ctx);
Paul Bakker1496d382011-05-23 12:07:29 +0000150}
151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152static int do_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1496d382011-05-23 12:07:29 +0000153{
154 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200155 uint32_t flags;
Paul Bakker1496d382011-05-23 12:07:29 +0000156 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000157 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000158
159 /*
160 * 4. Handshake
161 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 mbedtls_printf(" . Performing the SSL/TLS handshake...");
163 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 while ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
166 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_ERROR_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 mbedtls_strerror(ret, (char *) buf, 1024);
Paul Bakker5690efc2011-05-26 13:16:06 +0000169#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned %d: %s\n\n", ret, buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000171 return -1;
172 }
173 }
174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 mbedtls_printf(" ok\n [ Ciphersuite is %s ]\n",
176 mbedtls_ssl_get_ciphersuite(ssl));
177
178 /*
179 * 5. Verify the server certificate
180 */
181 mbedtls_printf(" . Verifying peer X.509 certificate...");
182
183 /* In real life, we probably want to bail out when ret != 0 */
184 if ((flags = mbedtls_ssl_get_verify_result(ssl)) != 0) {
185 char vrfy_buf[512];
186
187 mbedtls_printf(" failed\n");
188
189 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
190
191 mbedtls_printf("%s\n", vrfy_buf);
192 } else {
193 mbedtls_printf(" ok\n");
194 }
195
196 mbedtls_printf(" . Peer certificate information ...\n");
197 mbedtls_x509_crt_info((char *) buf, sizeof(buf) - 1, " ",
198 mbedtls_ssl_get_peer_cert(ssl));
199 mbedtls_printf("%s\n", buf);
200
201 return 0;
Paul Bakker1496d382011-05-23 12:07:29 +0000202}
203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204static int write_ssl_data(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
205{
206 int ret;
207
208 mbedtls_printf("\n%s", buf);
209 while (len && (ret = mbedtls_ssl_write(ssl, buf, len)) <= 0) {
210 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
211 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
212 return -1;
213 }
214 }
215
216 return 0;
217}
218
219static int write_ssl_and_get_response(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
Paul Bakker1496d382011-05-23 12:07:29 +0000220{
221 int ret;
222 unsigned char data[128];
223 char code[4];
224 size_t i, idx = 0;
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_printf("\n%s", buf);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 while (len && (ret = mbedtls_ssl_write(ssl, buf, len)) <= 0) {
228 if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
229 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000230 return -1;
231 }
232 }
233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 do {
235 len = sizeof(data) - 1;
236 memset(data, 0, sizeof(data));
237 ret = mbedtls_ssl_read(ssl, data, len);
Paul Bakker1496d382011-05-23 12:07:29 +0000238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
Paul Bakker1496d382011-05-23 12:07:29 +0000240 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 }
Paul Bakker1496d382011-05-23 12:07:29 +0000242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
Paul Bakker1496d382011-05-23 12:07:29 +0000244 return -1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 }
Paul Bakker1496d382011-05-23 12:07:29 +0000246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 if (ret <= 0) {
248 mbedtls_printf("failed\n ! mbedtls_ssl_read returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000249 return -1;
250 }
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000253 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 for (i = 0; i < len; i++) {
255 if (data[i] != '\n') {
256 if (idx < 4) {
257 code[idx++] = data[i];
258 }
Paul Bakker1496d382011-05-23 12:07:29 +0000259 continue;
260 }
261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 if (idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ') {
Paul Bakker1496d382011-05-23 12:07:29 +0000263 code[3] = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return atoi(code);
Paul Bakker1496d382011-05-23 12:07:29 +0000265 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200266
Paul Bakker1496d382011-05-23 12:07:29 +0000267 idx = 0;
268 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 } while (1);
Paul Bakker1496d382011-05-23 12:07:29 +0000270}
271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272static int write_and_get_response(mbedtls_net_context *sock_fd, unsigned char *buf, size_t len)
Paul Bakker1496d382011-05-23 12:07:29 +0000273{
274 int ret;
275 unsigned char data[128];
276 char code[4];
277 size_t i, idx = 0;
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_printf("\n%s", buf);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 if (len && (ret = mbedtls_net_send(sock_fd, buf, len)) <= 0) {
281 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
282 return -1;
Paul Bakker1496d382011-05-23 12:07:29 +0000283 }
284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 do {
286 len = sizeof(data) - 1;
287 memset(data, 0, sizeof(data));
288 ret = mbedtls_net_recv(sock_fd, data, len);
Paul Bakker1496d382011-05-23 12:07:29 +0000289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 if (ret <= 0) {
291 mbedtls_printf("failed\n ! mbedtls_net_recv returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000292 return -1;
293 }
294
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200295 data[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000297 len = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 for (i = 0; i < len; i++) {
299 if (data[i] != '\n') {
300 if (idx < 4) {
301 code[idx++] = data[i];
302 }
Paul Bakker1496d382011-05-23 12:07:29 +0000303 continue;
304 }
305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 if (idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ') {
Paul Bakker1496d382011-05-23 12:07:29 +0000307 code[3] = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 return atoi(code);
Paul Bakker1496d382011-05-23 12:07:29 +0000309 }
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000310
Paul Bakker1496d382011-05-23 12:07:29 +0000311 idx = 0;
312 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 } while (1);
Paul Bakker1496d382011-05-23 12:07:29 +0000314}
315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316int main(int argc, char *argv[])
Paul Bakker1496d382011-05-23 12:07:29 +0000317{
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100318 int ret = 1, len;
319 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200320 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000322 unsigned char base[1024];
Mohammad Azim Khan9ebdcff2018-08-06 11:48:06 +0100323 /* buf is used as the destination buffer for printing base with the format:
324 * "%s\r\n". Hence, the size of buf should be at least the size of base
325 * plus 2 bytes for the \r and \n characters.
326 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 unsigned char buf[sizeof(base) + 2];
Mohammad Azim Khan9ebdcff2018-08-06 11:48:06 +0100328#else
329 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000330#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000331 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200332 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_entropy_context entropy;
335 mbedtls_ctr_drbg_context ctr_drbg;
336 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200337 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_x509_crt cacert;
339 mbedtls_x509_crt clicert;
340 mbedtls_pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000341 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000342 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000343 char *p, *q;
344 const int *list;
345
346 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200347 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 mbedtls_net_init(&server_fd);
350 mbedtls_ssl_init(&ssl);
351 mbedtls_ssl_config_init(&conf);
352 memset(&buf, 0, sizeof(buf));
353 mbedtls_x509_crt_init(&cacert);
354 mbedtls_x509_crt_init(&clicert);
355 mbedtls_pk_init(&pkey);
356 mbedtls_ctr_drbg_init(&ctr_drbg);
Przemek Stekielb00688f2023-04-17 11:10:05 +0200357 mbedtls_entropy_init(&entropy);
358
359#if defined(MBEDTLS_USE_PSA_CRYPTO)
360 psa_status_t status = psa_crypto_init();
361 if (status != PSA_SUCCESS) {
362 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
363 (int) status);
364 goto exit;
365 }
366#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1496d382011-05-23 12:07:29 +0000367
Aditya Deshpande0504ac22023-01-30 15:58:50 +0000368 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369usage:
370 mbedtls_printf(USAGE);
Paul Bakker1496d382011-05-23 12:07:29 +0000371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 list = mbedtls_ssl_list_ciphersuites();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 while (*list) {
374 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name(*list));
Paul Bakker1496d382011-05-23 12:07:29 +0000375 list++;
376 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_printf("\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000378 goto exit;
379 }
380
381 opt.server_name = DFL_SERVER_NAME;
382 opt.server_port = DFL_SERVER_PORT;
383 opt.debug_level = DFL_DEBUG_LEVEL;
384 opt.authentication = DFL_AUTHENTICATION;
385 opt.mode = DFL_MODE;
386 opt.user_name = DFL_USER_NAME;
387 opt.user_pwd = DFL_USER_PWD;
388 opt.mail_from = DFL_MAIL_FROM;
389 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000390 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000391 opt.crt_file = DFL_CRT_FILE;
392 opt.key_file = DFL_KEY_FILE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
Paul Bakker1496d382011-05-23 12:07:29 +0000394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 for (i = 1; i < argc; i++) {
Paul Bakker1496d382011-05-23 12:07:29 +0000396 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker1496d382011-05-23 12:07:29 +0000398 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 }
Paul Bakker1496d382011-05-23 12:07:29 +0000400 *q++ = '\0';
401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 if (strcmp(p, "server_name") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000403 opt.server_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 } else if (strcmp(p, "server_port") == 0) {
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200405 opt.server_port = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 } else if (strcmp(p, "debug_level") == 0) {
407 opt.debug_level = atoi(q);
408 if (opt.debug_level < 0 || opt.debug_level > 65535) {
Paul Bakker1496d382011-05-23 12:07:29 +0000409 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 }
411 } else if (strcmp(p, "authentication") == 0) {
412 opt.authentication = atoi(q);
413 if (opt.authentication < 0 || opt.authentication > 1) {
Paul Bakker1496d382011-05-23 12:07:29 +0000414 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 }
416 } else if (strcmp(p, "mode") == 0) {
417 opt.mode = atoi(q);
418 if (opt.mode < 0 || opt.mode > 1) {
Paul Bakker1496d382011-05-23 12:07:29 +0000419 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 }
421 } else if (strcmp(p, "user_name") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000422 opt.user_name = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 } else if (strcmp(p, "user_pwd") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000424 opt.user_pwd = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 } else if (strcmp(p, "mail_from") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000426 opt.mail_from = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 } else if (strcmp(p, "mail_to") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000428 opt.mail_to = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 } else if (strcmp(p, "ca_file") == 0) {
Paul Bakker5690efc2011-05-26 13:16:06 +0000430 opt.ca_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 } else if (strcmp(p, "crt_file") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000432 opt.crt_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 } else if (strcmp(p, "key_file") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000434 opt.key_file = q;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 } else if (strcmp(p, "force_ciphersuite") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000436 opt.force_ciphersuite[0] = -1;
437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(q);
Paul Bakker1496d382011-05-23 12:07:29 +0000439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 if (opt.force_ciphersuite[0] <= 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000441 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 }
Paul Bakker1496d382011-05-23 12:07:29 +0000443
444 opt.force_ciphersuite[1] = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 } else {
Paul Bakker1496d382011-05-23 12:07:29 +0000446 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 }
Paul Bakker1496d382011-05-23 12:07:29 +0000448 }
449
450 /*
451 * 0. Initialize the RNG and the session data
452 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 mbedtls_printf("\n . Seeding the random number generator...");
454 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
457 (const unsigned char *) pers,
458 strlen(pers))) != 0) {
459 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000460 goto exit;
461 }
462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000464
465 /*
466 * 1.1. Load the trusted CA
467 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 mbedtls_printf(" . Loading the CA root certificate ...");
469 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 if (strlen(opt.ca_file)) {
473 ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file);
474 } else
Paul Bakker5690efc2011-05-26 13:16:06 +0000475#endif
Andres AGcef21e42017-02-02 17:01:10 +0000476#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_cas_pem,
478 mbedtls_test_cas_pem_len);
Paul Bakker5690efc2011-05-26 13:16:06 +0000479#else
480 {
Andres AGcef21e42017-02-02 17:01:10 +0000481 mbedtls_printf("MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.");
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100482 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000483 }
484#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 if (ret < 0) {
486 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000487 goto exit;
488 }
489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000491
492 /*
493 * 1.2. Load own certificate and private key
494 *
495 * (can be skipped if client authentication is not required)
496 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 mbedtls_printf(" . Loading the client cert. and key...");
498 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 if (strlen(opt.crt_file)) {
502 ret = mbedtls_x509_crt_parse_file(&clicert, opt.crt_file);
503 } else
Paul Bakker5690efc2011-05-26 13:16:06 +0000504#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#if defined(MBEDTLS_CERTS_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *) mbedtls_test_cli_crt,
507 mbedtls_test_cli_crt_len);
Paul Bakker5690efc2011-05-26 13:16:06 +0000508#else
509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100511 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000512 }
513#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 if (ret != 0) {
515 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000516 goto exit;
517 }
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 if (strlen(opt.key_file)) {
521 ret = mbedtls_pk_parse_keyfile(&pkey, opt.key_file, "");
522 } else
Paul Bakker5690efc2011-05-26 13:16:06 +0000523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100525 ret = mbedtls_pk_parse_key(&pkey, (const unsigned char *) mbedtls_test_cli_key,
526 mbedtls_test_cli_key_len, NULL, 0);
Paul Bakker5690efc2011-05-26 13:16:06 +0000527#else
528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_printf("MBEDTLS_CERTS_C or MBEDTLS_PEM_PARSE_C not defined.");
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100530 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000531 }
532#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 if (ret != 0) {
534 mbedtls_printf(" failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000535 goto exit;
536 }
537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000539
540 /*
541 * 2. Start the connection
542 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100543 mbedtls_printf(" . Connecting to tcp/%s/%s...", opt.server_name,
544 opt.server_port);
545 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547 if ((ret = mbedtls_net_connect(&server_fd, opt.server_name,
548 opt.server_port, MBEDTLS_NET_PROTO_TCP)) != 0) {
549 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000550 goto exit;
551 }
552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100553 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000554
555 /*
556 * 3. Setup stuff
557 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 mbedtls_printf(" . Setting up the SSL/TLS structure...");
559 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561 if ((ret = mbedtls_ssl_config_defaults(&conf,
562 MBEDTLS_SSL_IS_CLIENT,
563 MBEDTLS_SSL_TRANSPORT_STREAM,
564 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
565 mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200566 goto exit;
567 }
568
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100569 /* OPTIONAL is not optimal for security,
570 * but makes interop easier in this simplified example */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
Paul Bakker1496d382011-05-23 12:07:29 +0000572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100573 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
574 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 if (opt.force_ciphersuite[0] != DFL_FORCE_CIPHER) {
577 mbedtls_ssl_conf_ciphersuites(&conf, opt.force_ciphersuite);
578 }
Paul Bakker1496d382011-05-23 12:07:29 +0000579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
581 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &clicert, &pkey)) != 0) {
582 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200583 goto exit;
584 }
Paul Bakker1496d382011-05-23 12:07:29 +0000585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
587 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200588 goto exit;
589 }
590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100591 if ((ret = mbedtls_ssl_set_hostname(&ssl, opt.server_name)) != 0) {
592 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200593 goto exit;
594 }
Paul Bakker1496d382011-05-23 12:07:29 +0000595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100598 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 if (opt.mode == MODE_SSL_TLS) {
601 if (do_handshake(&ssl) != 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000602 goto exit;
603 }
604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 mbedtls_printf(" > Get header from server:");
606 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100608 ret = write_ssl_and_get_response(&ssl, buf, 0);
609 if (ret < 200 || ret > 299) {
610 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000611 goto exit;
612 }
613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100614 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 mbedtls_printf(" > Write EHLO to server:");
617 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100619 gethostname(hostname, 32);
620 len = sprintf((char *) buf, "EHLO %s\r\n", hostname);
621 ret = write_ssl_and_get_response(&ssl, buf, len);
622 if (ret < 200 || ret > 299) {
623 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
624 goto exit;
625 }
626 } else {
627 mbedtls_printf(" > Get header from server:");
628 fflush(stdout);
629
630 ret = write_and_get_response(&server_fd, buf, 0);
631 if (ret < 200 || ret > 299) {
632 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000633 goto exit;
634 }
635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 mbedtls_printf(" > Write EHLO to server:");
639 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100641 gethostname(hostname, 32);
642 len = sprintf((char *) buf, "EHLO %s\r\n", hostname);
643 ret = write_and_get_response(&server_fd, buf, len);
644 if (ret < 200 || ret > 299) {
645 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000646 goto exit;
647 }
648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 mbedtls_printf(" > Write STARTTLS to server:");
652 fflush(stdout);
653
654 gethostname(hostname, 32);
655 len = sprintf((char *) buf, "STARTTLS\r\n");
656 ret = write_and_get_response(&server_fd, buf, len);
657 if (ret < 200 || ret > 299) {
658 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000659 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 }
661
662 mbedtls_printf(" ok\n");
663
664 if (do_handshake(&ssl) != 0) {
665 goto exit;
666 }
Paul Bakker1496d382011-05-23 12:07:29 +0000667 }
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_BASE64_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100670 if (opt.authentication) {
671 mbedtls_printf(" > Write AUTH LOGIN to server:");
672 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100674 len = sprintf((char *) buf, "AUTH LOGIN\r\n");
675 ret = write_ssl_and_get_response(&ssl, buf, len);
676 if (ret < 200 || ret > 399) {
677 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000678 goto exit;
679 }
680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683 mbedtls_printf(" > Write username to server: %s", opt.user_name);
684 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100686 ret = mbedtls_base64_encode(base, sizeof(base), &n, (const unsigned char *) opt.user_name,
687 strlen(opt.user_name));
Alfred Klomp7c034242014-07-14 22:11:13 +0200688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 if (ret != 0) {
690 mbedtls_printf(" failed\n ! mbedtls_base64_encode returned %d\n\n", ret);
Alfred Klomp7c034242014-07-14 22:11:13 +0200691 goto exit;
692 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 len = sprintf((char *) buf, "%s\r\n", base);
694 ret = write_ssl_and_get_response(&ssl, buf, len);
695 if (ret < 300 || ret > 399) {
696 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000697 goto exit;
698 }
699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100700 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 mbedtls_printf(" > Write password to server: %s", opt.user_pwd);
703 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705 ret = mbedtls_base64_encode(base, sizeof(base), &n, (const unsigned char *) opt.user_pwd,
706 strlen(opt.user_pwd));
Alfred Klomp7c034242014-07-14 22:11:13 +0200707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100708 if (ret != 0) {
709 mbedtls_printf(" failed\n ! mbedtls_base64_encode returned %d\n\n", ret);
Alfred Klomp7c034242014-07-14 22:11:13 +0200710 goto exit;
711 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100712 len = sprintf((char *) buf, "%s\r\n", base);
713 ret = write_ssl_and_get_response(&ssl, buf, len);
714 if (ret < 200 || ret > 399) {
715 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000716 goto exit;
717 }
718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100719 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000720 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000721#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 mbedtls_printf(" > Write MAIL FROM to server:");
724 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000725
Mingjie Shencfe1be32024-03-05 18:13:28 -0500726 len = snprintf((char *) buf, sizeof(buf), "MAIL FROM:<%s>\r\n", opt.mail_from);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727 ret = write_ssl_and_get_response(&ssl, buf, len);
728 if (ret < 200 || ret > 299) {
729 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000730 goto exit;
731 }
732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100733 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 mbedtls_printf(" > Write RCPT TO to server:");
736 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000737
Mingjie Shencfe1be32024-03-05 18:13:28 -0500738 len = snprintf((char *) buf, sizeof(buf), "RCPT TO:<%s>\r\n", opt.mail_to);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100739 ret = write_ssl_and_get_response(&ssl, buf, len);
740 if (ret < 200 || ret > 299) {
741 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000742 goto exit;
743 }
744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100745 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 mbedtls_printf(" > Write DATA to server:");
748 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100750 len = sprintf((char *) buf, "DATA\r\n");
751 ret = write_ssl_and_get_response(&ssl, buf, len);
752 if (ret < 300 || ret > 399) {
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 Peskine1b6c09a2023-01-11 14:52:35 +0100757 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100759 mbedtls_printf(" > Write content to server:");
760 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000761
Mingjie Shencfe1be32024-03-05 18:13:28 -0500762 len = snprintf((char *) buf, sizeof(buf),
763 "From: %s\r\nSubject: Mbed TLS Test mail\r\n\r\n"
764 "This is a simple test mail from the "
765 "Mbed TLS mail client example.\r\n"
766 "\r\n"
767 "Enjoy!", opt.mail_from);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 ret = write_ssl_data(&ssl, buf, len);
Paul Bakker1496d382011-05-23 12:07:29 +0000769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100770 len = sprintf((char *) buf, "\r\n.\r\n");
771 ret = write_ssl_and_get_response(&ssl, buf, len);
772 if (ret < 200 || ret > 299) {
773 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000774 goto exit;
775 }
776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100779 mbedtls_ssl_close_notify(&ssl);
Paul Bakker1496d382011-05-23 12:07:29 +0000780
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100781 exit_code = MBEDTLS_EXIT_SUCCESS;
782
Paul Bakker1496d382011-05-23 12:07:29 +0000783exit:
784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100785 mbedtls_net_free(&server_fd);
786 mbedtls_x509_crt_free(&clicert);
787 mbedtls_x509_crt_free(&cacert);
788 mbedtls_pk_free(&pkey);
789 mbedtls_ssl_free(&ssl);
790 mbedtls_ssl_config_free(&conf);
791 mbedtls_ctr_drbg_free(&ctr_drbg);
792 mbedtls_entropy_free(&entropy);
Przemek Stekield4d049b2023-04-19 13:47:43 +0200793#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielc4ddf922023-04-19 10:15:26 +0200794 mbedtls_psa_crypto_free();
Przemek Stekield4d049b2023-04-19 13:47:43 +0200795#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1496d382011-05-23 12:07:29 +0000796
Paul Bakkercce9d772011-11-18 14:26:47 +0000797#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 mbedtls_printf(" + Press Enter to exit this program.\n");
799 fflush(stdout); getchar();
Paul Bakker1496d382011-05-23 12:07:29 +0000800#endif
801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802 mbedtls_exit(exit_code);
Paul Bakker1496d382011-05-23 12:07:29 +0000803}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
805 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C **
806 MBEDTLS_CTR_DRBG_C */