blob: 14a604e4cccfb7547cd61c4cf3e49014c6eeb41f [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1496d382011-05-23 12:07:29 +000018 */
19
Nicholas Wilson61fa4362018-06-25 12:10:00 +010020/* Enable definition of gethostname() even when compiling with -std=c99. Must
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020021 * be set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
Nicholas Wilson2682edf2017-12-05 12:08:15 +000022 * Harmless on other platforms. */
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +020023
Nicholas Wilson2682edf2017-12-05 12:08:15 +000024#define _POSIX_C_SOURCE 200112L
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025#define _XOPEN_SOURCE 600
Nicholas Wilson2682edf2017-12-05 12:08:15 +000026
Bence Szépkútic662b362021-05-27 11:25:03 +020027#include "mbedtls/build_info.h"
Paul Bakker1496d382011-05-23 12:07:29 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020030# include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020032# include <stdio.h>
33# include <stdlib.h>
34# define mbedtls_time time
35# define mbedtls_time_t time_t
36# define mbedtls_fprintf fprintf
37# define mbedtls_printf printf
38# define mbedtls_exit exit
39# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
40# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +010041#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000042
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020043#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
44 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
45 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
47 !defined(MBEDTLS_FS_IO)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048int main(void)
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
52 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
53 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
54 "not defined.\n");
55 mbedtls_exit(0);
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010056}
57#else
58
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059# include "mbedtls/base64.h"
60# include "mbedtls/error.h"
61# include "mbedtls/net_sockets.h"
62# include "mbedtls/ssl.h"
63# include "mbedtls/entropy.h"
64# include "mbedtls/ctr_drbg.h"
65# include "test/certs.h"
66# include "mbedtls/x509.h"
Rich Evans18b78c72015-02-11 14:06:19 +000067
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068# include <stdlib.h>
69# include <string.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010070
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071# if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
72# include <unistd.h>
73# else
74# include <io.h>
75# endif
Paul Bakker1496d382011-05-23 12:07:29 +000076
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077# if defined(_WIN32) || defined(_WIN32_WCE)
78# include <winsock2.h>
79# include <windows.h>
Paul Bakker5a835222011-10-12 09:19:31 +000080
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020081# if defined(_MSC_VER)
82# if defined(_WIN32_WCE)
83# pragma comment(lib, "ws2.lib")
84# else
85# pragma comment(lib, "ws2_32.lib")
86# endif
87# endif /* _MSC_VER */
88# endif
Paul Bakker5a835222011-10-12 09:19:31 +000089
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020090# define DFL_SERVER_NAME "localhost"
91# define DFL_SERVER_PORT "465"
92# define DFL_USER_NAME "user"
93# define DFL_USER_PWD "password"
94# define DFL_MAIL_FROM ""
95# define DFL_MAIL_TO ""
96# define DFL_DEBUG_LEVEL 0
97# define DFL_CA_FILE ""
98# define DFL_CRT_FILE ""
99# define DFL_KEY_FILE ""
100# define DFL_FORCE_CIPHER 0
101# define DFL_MODE 0
102# define DFL_AUTHENTICATION 0
Paul Bakker1496d382011-05-23 12:07:29 +0000103
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104# define MODE_SSL_TLS 0
105# define MODE_STARTTLS 0
Paul Bakker1496d382011-05-23 12:07:29 +0000106
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200107# if defined(MBEDTLS_BASE64_C)
108# define USAGE_AUTH \
109 " authentication=%%d default: 0 (disabled)\n" \
110 " user_name=%%s default: \"" DFL_USER_NAME "\"\n" \
111 " user_pwd=%%s default: \"" DFL_USER_PWD "\"\n"
112# else
113# define USAGE_AUTH \
114 " authentication options disabled. (Require MBEDTLS_BASE64_C)\n"
115# endif /* MBEDTLS_BASE64_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000116
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117# if defined(MBEDTLS_FS_IO)
118# define USAGE_IO \
119 " ca_file=%%s default: \"\" (pre-loaded)\n" \
120 " crt_file=%%s default: \"\" (pre-loaded)\n" \
121 " key_file=%%s default: \"\" (pre-loaded)\n"
122# else
123# define USAGE_IO \
124 " No file operations available (MBEDTLS_FS_IO not defined)\n"
125# endif /* MBEDTLS_FS_IO */
Rich Evans85b05ec2015-02-12 11:37:29 +0000126
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200127# define USAGE \
128 "\n usage: ssl_mail_client param=<>...\n" \
129 "\n acceptable parameters:\n" \
130 " server_name=%%s default: " DFL_SERVER_NAME "\n" \
131 " server_port=%%d default: " DFL_SERVER_PORT "\n" \
132 " debug_level=%%d default: 0 (disabled)\n" \
133 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" USAGE_AUTH \
134 " mail_from=%%s default: \"\"\n" \
135 " mail_to=%%s default: \"\"\n" USAGE_IO \
136 " force_ciphersuite=<name> default: all enabled\n" \
137 " acceptable ciphersuite names:\n"
Simon Butcher63cb97e2018-12-06 17:43:31 +0000138
Paul Bakker1496d382011-05-23 12:07:29 +0000139/*
140 * global options
141 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200142struct options {
143 const char *server_name; /* hostname of the server (client only) */
144 const char *server_port; /* port on which the ssl service runs */
145 int debug_level; /* level of debugging */
146 int authentication; /* if authentication is required */
147 int mode; /* SSL/TLS (0) or STARTTLS (1) */
148 const char *user_name; /* username to use for authentication */
149 const char *user_pwd; /* password to use for authentication */
150 const char *mail_from; /* E-Mail address to use as sender */
151 const char *mail_to; /* E-Mail address to use as recipient */
152 const char *ca_file; /* the file with the CA certificate(s) */
153 const char *crt_file; /* the file with the client certificate */
154 const char *key_file; /* the file with the client key */
155 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
Paul Bakker1496d382011-05-23 12:07:29 +0000156} opt;
157
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200158static void
159my_debug(void *ctx, int level, const char *file, int line, const char *str)
Paul Bakker1496d382011-05-23 12:07:29 +0000160{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200161 ((void)level);
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200162
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200163 mbedtls_fprintf((FILE *)ctx, "%s:%04d: %s", file, line, str);
164 fflush((FILE *)ctx);
Paul Bakker1496d382011-05-23 12:07:29 +0000165}
166
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200167static int do_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1496d382011-05-23 12:07:29 +0000168{
169 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200170 uint32_t flags;
Paul Bakker1496d382011-05-23 12:07:29 +0000171 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000172 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000173
174 /*
175 * 4. Handshake
176 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177 mbedtls_printf(" . Performing the SSL/TLS handshake...");
178 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000179
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200180 while ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
181 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
182 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
183# if defined(MBEDTLS_ERROR_C)
184 mbedtls_strerror(ret, (char *)buf, 1024);
185# endif
186 mbedtls_printf(
187 " failed\n ! mbedtls_ssl_handshake returned %d: %s\n\n", ret,
188 buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000189 return -1;
190 }
191 }
192
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193 mbedtls_printf(" ok\n [ Ciphersuite is %s ]\n",
194 mbedtls_ssl_get_ciphersuite(ssl));
195
196 /*
197 * 5. Verify the server certificate
198 */
199 mbedtls_printf(" . Verifying peer X.509 certificate...");
200
201 /* In real life, we probably want to bail out when ret != 0 */
202 if ((flags = mbedtls_ssl_get_verify_result(ssl)) != 0) {
203# if !defined(MBEDTLS_X509_REMOVE_INFO)
204 char vrfy_buf[512];
205# endif
206
207 mbedtls_printf(" failed\n");
208
209# if !defined(MBEDTLS_X509_REMOVE_INFO)
210 mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
211
212 mbedtls_printf("%s\n", vrfy_buf);
213# endif
214 } else
215 mbedtls_printf(" ok\n");
216
217# if !defined(MBEDTLS_X509_REMOVE_INFO)
218 mbedtls_printf(" . Peer certificate information ...\n");
219 mbedtls_x509_crt_info((char *)buf, sizeof(buf) - 1, " ",
220 mbedtls_ssl_get_peer_cert(ssl));
221 mbedtls_printf("%s\n", buf);
222# endif
223
224 return 0;
Paul Bakker1496d382011-05-23 12:07:29 +0000225}
226
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200227static int
228write_ssl_data(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
229{
230 int ret;
231
232 mbedtls_printf("\n%s", buf);
233 while (len && (ret = mbedtls_ssl_write(ssl, buf, len)) <= 0) {
234 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
235 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
236 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n",
237 ret);
238 return -1;
239 }
240 }
241
242 return 0;
243}
244
245static int write_ssl_and_get_response(mbedtls_ssl_context *ssl,
246 unsigned char *buf,
247 size_t len)
Paul Bakker1496d382011-05-23 12:07:29 +0000248{
249 int ret;
250 unsigned char data[128];
251 char code[4];
252 size_t i, idx = 0;
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_printf("\n%s", buf);
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200255 while (len && (ret = mbedtls_ssl_write(ssl, buf, len)) <= 0) {
256 if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
257 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
258 mbedtls_printf(" failed\n ! mbedtls_ssl_write returned %d\n\n",
259 ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000260 return -1;
261 }
262 }
263
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200264 do {
265 len = sizeof(data) - 1;
266 memset(data, 0, sizeof(data));
267 ret = mbedtls_ssl_read(ssl, data, len);
Paul Bakker1496d382011-05-23 12:07:29 +0000268
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200269 if (ret == MBEDTLS_ERR_SSL_WANT_READ ||
270 ret == MBEDTLS_ERR_SSL_WANT_WRITE)
Paul Bakker1496d382011-05-23 12:07:29 +0000271 continue;
272
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200273 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
Paul Bakker1496d382011-05-23 12:07:29 +0000274 return -1;
275
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200276 if (ret <= 0) {
277 mbedtls_printf("failed\n ! mbedtls_ssl_read returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000278 return -1;
279 }
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000282 len = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200283 for (i = 0; i < len; i++) {
284 if (data[i] != '\n') {
285 if (idx < 4)
286 code[idx++] = data[i];
Paul Bakker1496d382011-05-23 12:07:29 +0000287 continue;
288 }
289
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200290 if (idx == 4 && code[0] >= '0' && code[0] <= '9' &&
291 code[3] == ' ') {
Paul Bakker1496d382011-05-23 12:07:29 +0000292 code[3] = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200293 return atoi(code);
Paul Bakker1496d382011-05-23 12:07:29 +0000294 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200295
Paul Bakker1496d382011-05-23 12:07:29 +0000296 idx = 0;
297 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200298 } while (1);
Paul Bakker1496d382011-05-23 12:07:29 +0000299}
300
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200301static int write_and_get_response(mbedtls_net_context *sock_fd,
302 unsigned char *buf,
303 size_t len)
Paul Bakker1496d382011-05-23 12:07:29 +0000304{
305 int ret;
306 unsigned char data[128];
307 char code[4];
308 size_t i, idx = 0;
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_printf("\n%s", buf);
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200311 if (len && (ret = mbedtls_net_send(sock_fd, buf, len)) <= 0) {
312 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
313 return -1;
Paul Bakker1496d382011-05-23 12:07:29 +0000314 }
315
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200316 do {
317 len = sizeof(data) - 1;
318 memset(data, 0, sizeof(data));
319 ret = mbedtls_net_recv(sock_fd, data, len);
Paul Bakker1496d382011-05-23 12:07:29 +0000320
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200321 if (ret <= 0) {
322 mbedtls_printf("failed\n ! mbedtls_net_recv returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000323 return -1;
324 }
325
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200326 data[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000328 len = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200329 for (i = 0; i < len; i++) {
330 if (data[i] != '\n') {
331 if (idx < 4)
332 code[idx++] = data[i];
Paul Bakker1496d382011-05-23 12:07:29 +0000333 continue;
334 }
335
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200336 if (idx == 4 && code[0] >= '0' && code[0] <= '9' &&
337 code[3] == ' ') {
Paul Bakker1496d382011-05-23 12:07:29 +0000338 code[3] = '\0';
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200339 return atoi(code);
Paul Bakker1496d382011-05-23 12:07:29 +0000340 }
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000341
Paul Bakker1496d382011-05-23 12:07:29 +0000342 idx = 0;
343 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200344 } while (1);
Paul Bakker1496d382011-05-23 12:07:29 +0000345}
346
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200347int main(int argc, char *argv[])
Paul Bakker1496d382011-05-23 12:07:29 +0000348{
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100349 int ret = 1, len;
350 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200351 mbedtls_net_context server_fd;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200352# if defined(MBEDTLS_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000353 unsigned char base[1024];
Mohammad Azim Khan9ebdcff2018-08-06 11:48:06 +0100354 /* buf is used as the destination buffer for printing base with the format:
355 * "%s\r\n". Hence, the size of buf should be at least the size of base
356 * plus 2 bytes for the \r and \n characters.
357 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200358 unsigned char buf[sizeof(base) + 2];
359# else
Mohammad Azim Khan9ebdcff2018-08-06 11:48:06 +0100360 unsigned char buf[1024];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200361# endif
Paul Bakker1496d382011-05-23 12:07:29 +0000362 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200363 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_entropy_context entropy;
366 mbedtls_ctr_drbg_context ctr_drbg;
367 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200368 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_x509_crt cacert;
370 mbedtls_x509_crt clicert;
371 mbedtls_pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000372 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000373 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000374 char *p, *q;
375 const int *list;
376
377 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200378 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000379 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200380 mbedtls_net_init(&server_fd);
381 mbedtls_ssl_init(&ssl);
382 mbedtls_ssl_config_init(&conf);
383 memset(&buf, 0, sizeof(buf));
384 mbedtls_x509_crt_init(&cacert);
385 mbedtls_x509_crt_init(&clicert);
386 mbedtls_pk_init(&pkey);
387 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker1496d382011-05-23 12:07:29 +0000388
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200389 if (argc == 0) {
390usage:
391 mbedtls_printf(USAGE);
Paul Bakker1496d382011-05-23 12:07:29 +0000392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 list = mbedtls_ssl_list_ciphersuites();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200394 while (*list) {
395 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name(*list));
Paul Bakker1496d382011-05-23 12:07:29 +0000396 list++;
397 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_printf("\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000399 goto exit;
400 }
401
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200402 opt.server_name = DFL_SERVER_NAME;
403 opt.server_port = DFL_SERVER_PORT;
404 opt.debug_level = DFL_DEBUG_LEVEL;
405 opt.authentication = DFL_AUTHENTICATION;
406 opt.mode = DFL_MODE;
407 opt.user_name = DFL_USER_NAME;
408 opt.user_pwd = DFL_USER_PWD;
409 opt.mail_from = DFL_MAIL_FROM;
410 opt.mail_to = DFL_MAIL_TO;
411 opt.ca_file = DFL_CA_FILE;
412 opt.crt_file = DFL_CRT_FILE;
413 opt.key_file = DFL_KEY_FILE;
414 opt.force_ciphersuite[0] = DFL_FORCE_CIPHER;
Paul Bakker1496d382011-05-23 12:07:29 +0000415
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200416 for (i = 1; i < argc; i++) {
Paul Bakker1496d382011-05-23 12:07:29 +0000417 p = argv[i];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200418 if ((q = strchr(p, '=')) == NULL)
Paul Bakker1496d382011-05-23 12:07:29 +0000419 goto usage;
420 *q++ = '\0';
421
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200422 if (strcmp(p, "server_name") == 0)
Paul Bakker1496d382011-05-23 12:07:29 +0000423 opt.server_name = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200424 else if (strcmp(p, "server_port") == 0)
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200425 opt.server_port = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200426 else if (strcmp(p, "debug_level") == 0) {
427 opt.debug_level = atoi(q);
428 if (opt.debug_level < 0 || opt.debug_level > 65535)
Paul Bakker1496d382011-05-23 12:07:29 +0000429 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200430 } else if (strcmp(p, "authentication") == 0) {
431 opt.authentication = atoi(q);
432 if (opt.authentication < 0 || opt.authentication > 1)
Paul Bakker1496d382011-05-23 12:07:29 +0000433 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200434 } else if (strcmp(p, "mode") == 0) {
435 opt.mode = atoi(q);
436 if (opt.mode < 0 || opt.mode > 1)
Paul Bakker1496d382011-05-23 12:07:29 +0000437 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200438 } else if (strcmp(p, "user_name") == 0)
Paul Bakker1496d382011-05-23 12:07:29 +0000439 opt.user_name = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200440 else if (strcmp(p, "user_pwd") == 0)
Paul Bakker1496d382011-05-23 12:07:29 +0000441 opt.user_pwd = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200442 else if (strcmp(p, "mail_from") == 0)
Paul Bakker1496d382011-05-23 12:07:29 +0000443 opt.mail_from = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200444 else if (strcmp(p, "mail_to") == 0)
Paul Bakker1496d382011-05-23 12:07:29 +0000445 opt.mail_to = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200446 else if (strcmp(p, "ca_file") == 0)
Paul Bakker5690efc2011-05-26 13:16:06 +0000447 opt.ca_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200448 else if (strcmp(p, "crt_file") == 0)
Paul Bakker1496d382011-05-23 12:07:29 +0000449 opt.crt_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200450 else if (strcmp(p, "key_file") == 0)
Paul Bakker1496d382011-05-23 12:07:29 +0000451 opt.key_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200452 else if (strcmp(p, "force_ciphersuite") == 0) {
Paul Bakker1496d382011-05-23 12:07:29 +0000453 opt.force_ciphersuite[0] = -1;
454
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200455 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(q);
Paul Bakker1496d382011-05-23 12:07:29 +0000456
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200457 if (opt.force_ciphersuite[0] <= 0)
Paul Bakker1496d382011-05-23 12:07:29 +0000458 goto usage;
459
460 opt.force_ciphersuite[1] = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200461 } else
Paul Bakker1496d382011-05-23 12:07:29 +0000462 goto usage;
463 }
464
465 /*
466 * 0. Initialize the RNG and the session data
467 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200468 mbedtls_printf("\n . Seeding the random number generator...");
469 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000470
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200471 mbedtls_entropy_init(&entropy);
472 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
473 (const unsigned char *)pers,
474 strlen(pers))) != 0) {
475 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000476 goto exit;
477 }
478
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200479 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000480
481 /*
482 * 1.1. Load the trusted CA
483 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200484 mbedtls_printf(" . Loading the CA root certificate ...");
485 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000486
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200487# if defined(MBEDTLS_FS_IO)
488 if (strlen(opt.ca_file))
489 ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file);
Paul Bakker5690efc2011-05-26 13:16:06 +0000490 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200491# endif
492# if defined(MBEDTLS_PEM_PARSE_C)
493 ret = mbedtls_x509_crt_parse(
494 &cacert, (const unsigned char *)mbedtls_test_cas_pem,
495 mbedtls_test_cas_pem_len);
496# else
Paul Bakker5690efc2011-05-26 13:16:06 +0000497 {
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100498 mbedtls_printf("MBEDTLS_PEM_PARSE_C not defined.");
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100499 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000500 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200501# endif
502 if (ret < 0) {
503 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n",
504 ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000505 goto exit;
506 }
507
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200508 mbedtls_printf(" ok (%d skipped)\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000509
510 /*
511 * 1.2. Load own certificate and private key
512 *
513 * (can be skipped if client authentication is not required)
514 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200515 mbedtls_printf(" . Loading the client cert. and key...");
516 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000517
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200518# if defined(MBEDTLS_FS_IO)
519 if (strlen(opt.crt_file))
520 ret = mbedtls_x509_crt_parse_file(&clicert, opt.crt_file);
Paul Bakkerddf26b42013-09-18 13:46:23 +0200521 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200522# endif
523 ret = mbedtls_x509_crt_parse(
524 &clicert, (const unsigned char *)mbedtls_test_cli_crt,
525 mbedtls_test_cli_crt_len);
526 if (ret != 0) {
527 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse returned %d\n\n",
528 ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000529 goto exit;
530 }
531
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200532# if defined(MBEDTLS_FS_IO)
533 if (strlen(opt.key_file)) {
534 ret = mbedtls_pk_parse_keyfile(&pkey, opt.key_file, "",
535 mbedtls_ctr_drbg_random, &ctr_drbg);
536 } else
537# endif
538# if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200539 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200540 ret = mbedtls_pk_parse_key(&pkey,
541 (const unsigned char *)mbedtls_test_cli_key,
542 mbedtls_test_cli_key_len, NULL, 0,
543 mbedtls_ctr_drbg_random, &ctr_drbg);
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200544 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200545# else
Paul Bakker5690efc2011-05-26 13:16:06 +0000546 {
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100547 mbedtls_printf("MBEDTLS_PEM_PARSE_C not defined.");
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100548 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000549 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200550# endif
551 if (ret != 0) {
552 mbedtls_printf(" failed\n ! mbedtls_pk_parse_key returned %d\n\n",
553 ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000554 goto exit;
555 }
556
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200557 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000558
559 /*
560 * 2. Start the connection
561 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200562 mbedtls_printf(" . Connecting to tcp/%s/%s...", opt.server_name,
563 opt.server_port);
564 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000565
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200566 if ((ret = mbedtls_net_connect(&server_fd, opt.server_name, opt.server_port,
567 MBEDTLS_NET_PROTO_TCP)) != 0) {
568 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000569 goto exit;
570 }
571
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200572 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000573
574 /*
575 * 3. Setup stuff
576 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200577 mbedtls_printf(" . Setting up the SSL/TLS structure...");
578 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000579
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200580 if ((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT,
581 MBEDTLS_SSL_TRANSPORT_STREAM,
582 MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
583 mbedtls_printf(
584 " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200585 goto exit;
586 }
587
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100588 /* OPTIONAL is not optimal for security,
589 * but makes interop easier in this simplified example */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200590 mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
Paul Bakker1496d382011-05-23 12:07:29 +0000591
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200592 mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
593 mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000594
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200595 if (opt.force_ciphersuite[0] != DFL_FORCE_CIPHER)
596 mbedtls_ssl_conf_ciphersuites(&conf, opt.force_ciphersuite);
Paul Bakker1496d382011-05-23 12:07:29 +0000597
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200598 mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
599 if ((ret = mbedtls_ssl_conf_own_cert(&conf, &clicert, &pkey)) != 0) {
600 mbedtls_printf(" failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
601 ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200602 goto exit;
603 }
Paul Bakker1496d382011-05-23 12:07:29 +0000604
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200605 if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
606 mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200607 goto exit;
608 }
609
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200610 if ((ret = mbedtls_ssl_set_hostname(&ssl, opt.server_name)) != 0) {
611 mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
612 ret);
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200613 goto exit;
614 }
Paul Bakker1496d382011-05-23 12:07:29 +0000615
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200616 mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
617 NULL);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200618
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200619 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200620
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200621 if (opt.mode == MODE_SSL_TLS) {
622 if (do_handshake(&ssl) != 0)
Paul Bakker1496d382011-05-23 12:07:29 +0000623 goto exit;
624
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200625 mbedtls_printf(" > Get header from server:");
626 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000627
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200628 ret = write_ssl_and_get_response(&ssl, buf, 0);
629 if (ret < 200 || ret > 299) {
630 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000631 goto exit;
632 }
633
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200634 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000635
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200636 mbedtls_printf(" > Write EHLO to server:");
637 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000638
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200639 gethostname(hostname, 32);
640 len = sprintf((char *)buf, "EHLO %s\r\n", hostname);
641 ret = write_ssl_and_get_response(&ssl, buf, len);
642 if (ret < 200 || ret > 299) {
643 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000644 goto exit;
645 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200646 } else {
647 mbedtls_printf(" > Get header from server:");
648 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000649
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200650 ret = write_and_get_response(&server_fd, buf, 0);
651 if (ret < 200 || ret > 299) {
652 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000653 goto exit;
654 }
655
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200656 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000657
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200658 mbedtls_printf(" > Write EHLO to server:");
659 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000660
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200661 gethostname(hostname, 32);
662 len = sprintf((char *)buf, "EHLO %s\r\n", hostname);
663 ret = write_and_get_response(&server_fd, buf, len);
664 if (ret < 200 || ret > 299) {
665 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000666 goto exit;
667 }
668
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200669 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000670
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200671 mbedtls_printf(" > Write STARTTLS to server:");
672 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000673
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200674 gethostname(hostname, 32);
675 len = sprintf((char *)buf, "STARTTLS\r\n");
676 ret = write_and_get_response(&server_fd, buf, len);
677 if (ret < 200 || ret > 299) {
678 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000679 goto exit;
680 }
681
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200682 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000683
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200684 if (do_handshake(&ssl) != 0)
Paul Bakker1496d382011-05-23 12:07:29 +0000685 goto exit;
686 }
687
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200688# if defined(MBEDTLS_BASE64_C)
689 if (opt.authentication) {
690 mbedtls_printf(" > Write AUTH LOGIN to server:");
691 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000692
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200693 len = sprintf((char *)buf, "AUTH LOGIN\r\n");
694 ret = write_ssl_and_get_response(&ssl, buf, len);
695 if (ret < 200 || 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200700 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000701
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200702 mbedtls_printf(" > Write username to server: %s", opt.user_name);
703 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000704
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200705 ret = mbedtls_base64_encode(base, sizeof(base), &n,
706 (const unsigned char *)opt.user_name,
707 strlen(opt.user_name));
Alfred Klomp7c034242014-07-14 22:11:13 +0200708
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200709 if (ret != 0) {
710 mbedtls_printf(" failed\n ! mbedtls_base64_encode returned %d\n\n",
711 ret);
Alfred Klomp7c034242014-07-14 22:11:13 +0200712 goto exit;
713 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200714 len = sprintf((char *)buf, "%s\r\n", base);
715 ret = write_ssl_and_get_response(&ssl, buf, len);
716 if (ret < 300 || ret > 399) {
717 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000718 goto exit;
719 }
720
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200721 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000722
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200723 mbedtls_printf(" > Write password to server: %s", opt.user_pwd);
724 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000725
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200726 ret = mbedtls_base64_encode(base, sizeof(base), &n,
727 (const unsigned char *)opt.user_pwd,
728 strlen(opt.user_pwd));
Alfred Klomp7c034242014-07-14 22:11:13 +0200729
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200730 if (ret != 0) {
731 mbedtls_printf(" failed\n ! mbedtls_base64_encode returned %d\n\n",
732 ret);
Alfred Klomp7c034242014-07-14 22:11:13 +0200733 goto exit;
734 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200735 len = sprintf((char *)buf, "%s\r\n", base);
736 ret = write_ssl_and_get_response(&ssl, buf, len);
737 if (ret < 200 || ret > 399) {
738 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000739 goto exit;
740 }
741
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200742 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000743 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200744# endif
Paul Bakker1496d382011-05-23 12:07:29 +0000745
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200746 mbedtls_printf(" > Write MAIL FROM to server:");
747 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000748
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200749 len = sprintf((char *)buf, "MAIL FROM:<%s>\r\n", opt.mail_from);
750 ret = write_ssl_and_get_response(&ssl, buf, len);
751 if (ret < 200 || ret > 299) {
752 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000753 goto exit;
754 }
755
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200756 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000757
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200758 mbedtls_printf(" > Write RCPT TO to server:");
759 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000760
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200761 len = sprintf((char *)buf, "RCPT TO:<%s>\r\n", opt.mail_to);
762 ret = write_ssl_and_get_response(&ssl, buf, len);
763 if (ret < 200 || ret > 299) {
764 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000765 goto exit;
766 }
767
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200768 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000769
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200770 mbedtls_printf(" > Write DATA to server:");
771 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000772
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200773 len = sprintf((char *)buf, "DATA\r\n");
774 ret = write_ssl_and_get_response(&ssl, buf, len);
775 if (ret < 300 || ret > 399) {
776 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000777 goto exit;
778 }
779
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200780 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000781
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200782 mbedtls_printf(" > Write content to server:");
783 fflush(stdout);
Paul Bakker1496d382011-05-23 12:07:29 +0000784
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200785 len = sprintf((char *)buf,
786 "From: %s\r\nSubject: mbed TLS Test mail\r\n\r\n"
787 "This is a simple test mail from the "
788 "mbed TLS mail client example.\r\n"
789 "\r\n"
790 "Enjoy!",
791 opt.mail_from);
792 ret = write_ssl_data(&ssl, buf, len);
Paul Bakker1496d382011-05-23 12:07:29 +0000793
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200794 len = sprintf((char *)buf, "\r\n.\r\n");
795 ret = write_ssl_and_get_response(&ssl, buf, len);
796 if (ret < 200 || ret > 299) {
797 mbedtls_printf(" failed\n ! server responded with %d\n\n", ret);
Paul Bakker1496d382011-05-23 12:07:29 +0000798 goto exit;
799 }
800
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200801 mbedtls_printf(" ok\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000802
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200803 mbedtls_ssl_close_notify(&ssl);
Paul Bakker1496d382011-05-23 12:07:29 +0000804
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100805 exit_code = MBEDTLS_EXIT_SUCCESS;
806
Paul Bakker1496d382011-05-23 12:07:29 +0000807exit:
808
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200809 mbedtls_net_free(&server_fd);
810 mbedtls_x509_crt_free(&clicert);
811 mbedtls_x509_crt_free(&cacert);
812 mbedtls_pk_free(&pkey);
813 mbedtls_ssl_free(&ssl);
814 mbedtls_ssl_config_free(&conf);
815 mbedtls_ctr_drbg_free(&ctr_drbg);
816 mbedtls_entropy_free(&entropy);
Paul Bakker1496d382011-05-23 12:07:29 +0000817
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200818# if defined(_WIN32)
819 mbedtls_printf(" + Press Enter to exit this program.\n");
820 fflush(stdout);
821 getchar();
822# endif
Paul Bakker1496d382011-05-23 12:07:29 +0000823
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200824 mbedtls_exit(exit_code);
Paul Bakker1496d382011-05-23 12:07:29 +0000825}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200826#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && \
827 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C ** \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 MBEDTLS_CTR_DRBG_C */