blob: 9fb65079e57fce44586595344205cc95862a29bf [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
nia7eb0e622020-06-11 12:30:12 +010025#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é-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
32 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
33 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
34 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
35 !defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010036int main( void )
37{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
39 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
40 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
41 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010042 "not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020043 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010044}
45#else
46
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/base64.h"
48#include "mbedtls/error.h"
Andres AG788aa4a2016-09-14 14:32:09 +010049#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/ssl.h"
51#include "mbedtls/entropy.h"
52#include "mbedtls/ctr_drbg.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010053#include "test/certs.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/x509.h"
Rich Evans18b78c72015-02-11 14:06:19 +000055
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdlib.h>
57#include <string.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010058
59#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
Paul Bakker1496d382011-05-23 12:07:29 +000060#include <unistd.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010061#else
62#include <io.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010063#endif
Paul Bakker1496d382011-05-23 12:07:29 +000064
Paul Bakker5a835222011-10-12 09:19:31 +000065#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5a835222011-10-12 09:19:31 +000066#include <winsock2.h>
67#include <windows.h>
68
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010069#if defined(_MSC_VER)
Paul Bakker5a835222011-10-12 09:19:31 +000070#if defined(_WIN32_WCE)
71#pragma comment( lib, "ws2.lib" )
72#else
73#pragma comment( lib, "ws2_32.lib" )
74#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010075#endif /* _MSC_VER */
Paul Bakker5a835222011-10-12 09:19:31 +000076#endif
77
Paul Bakker1496d382011-05-23 12:07:29 +000078#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020079#define DFL_SERVER_PORT "465"
Paul Bakker1496d382011-05-23 12:07:29 +000080#define DFL_USER_NAME "user"
81#define DFL_USER_PWD "password"
82#define DFL_MAIL_FROM ""
83#define DFL_MAIL_TO ""
84#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000085#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000086#define DFL_CRT_FILE ""
87#define DFL_KEY_FILE ""
88#define DFL_FORCE_CIPHER 0
89#define DFL_MODE 0
90#define DFL_AUTHENTICATION 0
91
92#define MODE_SSL_TLS 0
93#define MODE_STARTTLS 0
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if defined(MBEDTLS_BASE64_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000096#define USAGE_AUTH \
irwirf5ce5d52019-01-19 19:05:56 +030097 " authentication=%%d default: 0 (disabled)\n" \
98 " user_name=%%s default: \"" DFL_USER_NAME "\"\n" \
99 " user_pwd=%%s default: \"" DFL_USER_PWD "\"\n"
Rich Evans85b05ec2015-02-12 11:37:29 +0000100#else
101#define USAGE_AUTH \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 " authentication options disabled. (Require MBEDTLS_BASE64_C)\n"
103#endif /* MBEDTLS_BASE64_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#if defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +0000106#define USAGE_IO \
107 " ca_file=%%s default: \"\" (pre-loaded)\n" \
108 " crt_file=%%s default: \"\" (pre-loaded)\n" \
109 " key_file=%%s default: \"\" (pre-loaded)\n"
110#else
111#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 " No file operations available (MBEDTLS_FS_IO not defined)\n"
113#endif /* MBEDTLS_FS_IO */
Rich Evans85b05ec2015-02-12 11:37:29 +0000114
115#define USAGE \
irwirf5ce5d52019-01-19 19:05:56 +0300116 "\n usage: ssl_mail_client param=<>...\n" \
117 "\n acceptable parameters:\n" \
118 " server_name=%%s default: " DFL_SERVER_NAME "\n" \
119 " server_port=%%d default: " DFL_SERVER_PORT "\n" \
120 " debug_level=%%d default: 0 (disabled)\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000121 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
irwirf5ce5d52019-01-19 19:05:56 +0300122 USAGE_AUTH \
123 " mail_from=%%s default: \"\"\n" \
124 " mail_to=%%s default: \"\"\n" \
125 USAGE_IO \
126 " force_ciphersuite=<name> default: all enabled\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000127 " acceptable ciphersuite names:\n"
128
Simon Butcher63cb97e2018-12-06 17:43:31 +0000129
Paul Bakker1496d382011-05-23 12:07:29 +0000130/*
131 * global options
132 */
133struct options
134{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200135 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200136 const char *server_port; /* port on which the ssl service runs */
Paul Bakker1496d382011-05-23 12:07:29 +0000137 int debug_level; /* level of debugging */
138 int authentication; /* if authentication is required */
139 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200140 const char *user_name; /* username to use for authentication */
141 const char *user_pwd; /* password to use for authentication */
142 const char *mail_from; /* E-Mail address to use as sender */
143 const char *mail_to; /* E-Mail address to use as recipient */
144 const char *ca_file; /* the file with the CA certificate(s) */
145 const char *crt_file; /* the file with the client certificate */
146 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000147 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
148} opt;
149
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200150static void my_debug( void *ctx, int level,
151 const char *file, int line,
152 const char *str )
Paul Bakker1496d382011-05-23 12:07:29 +0000153{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200154 ((void) level);
155
156 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
157 fflush( (FILE *) ctx );
Paul Bakker1496d382011-05-23 12:07:29 +0000158}
159
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100160static int do_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1496d382011-05-23 12:07:29 +0000161{
162 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200163 uint32_t flags;
Paul Bakker1496d382011-05-23 12:07:29 +0000164 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000165 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000166
167 /*
168 * 4. Handshake
169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000171 fflush( stdout );
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 while( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000174 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100175 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker1496d382011-05-23 12:07:29 +0000176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_ERROR_C)
178 mbedtls_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned %d: %s\n\n", ret, buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000181 return( -1 );
182 }
183 }
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_printf( " ok\n [ Ciphersuite is %s ]\n",
186 mbedtls_ssl_get_ciphersuite( ssl ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000187
188 /*
189 * 5. Verify the server certificate
190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000192
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100193 /* In real life, we probably want to bail out when ret != 0 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200194 if( ( flags = mbedtls_ssl_get_verify_result( ssl ) ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000195 {
Hanno Becker612a2f12020-10-09 09:19:39 +0100196#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100197 char vrfy_buf[512];
Peter Kolbus9a969b62018-12-11 13:55:56 -0600198#endif
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_printf( " failed\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000201
Hanno Becker612a2f12020-10-09 09:19:39 +0100202#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200203 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker1496d382011-05-23 12:07:29 +0000204
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100205 mbedtls_printf( "%s\n", vrfy_buf );
Peter Kolbus9a969b62018-12-11 13:55:56 -0600206#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000207 }
208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000210
Hanno Becker612a2f12020-10-09 09:19:39 +0100211#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_printf( " . Peer certificate information ...\n" );
213 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
214 mbedtls_ssl_get_peer_cert( ssl ) );
215 mbedtls_printf( "%s\n", buf );
Peter Kolbus9a969b62018-12-11 13:55:56 -0600216#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000217
218 return( 0 );
219}
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221static int write_ssl_data( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000222{
223 int ret;
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_printf("\n%s", buf);
226 while( len && ( ret = mbedtls_ssl_write( ssl, buf, len ) ) <= 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000227 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100228 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker1496d382011-05-23 12:07:29 +0000229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000231 return -1;
232 }
233 }
234
235 return( 0 );
236}
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238static int write_ssl_and_get_response( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000239{
240 int ret;
241 unsigned char data[128];
242 char code[4];
243 size_t i, idx = 0;
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_printf("\n%s", buf);
246 while( len && ( ret = mbedtls_ssl_write( ssl, buf, len ) ) <= 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000247 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100248 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker1496d382011-05-23 12:07:29 +0000249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000251 return -1;
252 }
253 }
254
255 do
256 {
257 len = sizeof( data ) - 1;
258 memset( data, 0, sizeof( data ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 ret = mbedtls_ssl_read( ssl, data, len );
Paul Bakker1496d382011-05-23 12:07:29 +0000260
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100261 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker1496d382011-05-23 12:07:29 +0000262 continue;
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker1496d382011-05-23 12:07:29 +0000265 return -1;
266
267 if( ret <= 0 )
268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000270 return -1;
271 }
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000274 len = ret;
275 for( i = 0; i < len; i++ )
276 {
277 if( data[i] != '\n' )
278 {
279 if( idx < 4 )
280 code[ idx++ ] = data[i];
281 continue;
282 }
283
284 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
285 {
286 code[3] = '\0';
287 return atoi( code );
288 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200289
Paul Bakker1496d382011-05-23 12:07:29 +0000290 idx = 0;
291 }
292 }
293 while( 1 );
294}
295
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200296static int write_and_get_response( mbedtls_net_context *sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000297{
298 int ret;
299 unsigned char data[128];
300 char code[4];
301 size_t i, idx = 0;
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_printf("\n%s", buf);
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200304 if( len && ( ret = mbedtls_net_send( sock_fd, buf, len ) ) <= 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000305 {
irwirf5ce5d52019-01-19 19:05:56 +0300306 mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000307 return -1;
308 }
309
310 do
311 {
312 len = sizeof( data ) - 1;
313 memset( data, 0, sizeof( data ) );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200314 ret = mbedtls_net_recv( sock_fd, data, len );
Paul Bakker1496d382011-05-23 12:07:29 +0000315
316 if( ret <= 0 )
317 {
irwirf5ce5d52019-01-19 19:05:56 +0300318 mbedtls_printf( "failed\n ! mbedtls_net_recv returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000319 return -1;
320 }
321
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200322 data[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000324 len = ret;
325 for( i = 0; i < len; i++ )
326 {
327 if( data[i] != '\n' )
328 {
329 if( idx < 4 )
330 code[ idx++ ] = data[i];
331 continue;
332 }
333
334 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
335 {
336 code[3] = '\0';
337 return atoi( code );
338 }
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000339
Paul Bakker1496d382011-05-23 12:07:29 +0000340 idx = 0;
341 }
342 }
343 while( 1 );
344}
345
Paul Bakker1496d382011-05-23 12:07:29 +0000346int main( int argc, char *argv[] )
347{
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100348 int ret = 1, len;
349 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200350 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#if defined(MBEDTLS_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000352 unsigned char base[1024];
Mohammad Azim Khan9ebdcff2018-08-06 11:48:06 +0100353 /* buf is used as the destination buffer for printing base with the format:
354 * "%s\r\n". Hence, the size of buf should be at least the size of base
355 * plus 2 bytes for the \r and \n characters.
356 */
357 unsigned char buf[sizeof( base ) + 2];
358#else
359 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000360#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000361 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200362 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_entropy_context entropy;
365 mbedtls_ctr_drbg_context ctr_drbg;
366 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200367 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_x509_crt cacert;
369 mbedtls_x509_crt clicert;
370 mbedtls_pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000371 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000372 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000373 char *p, *q;
374 const int *list;
375
376 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200377 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000378 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200379 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200380 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200381 mbedtls_ssl_config_init( &conf );
Paul Bakker333fdec2014-08-04 12:12:09 +0200382 memset( &buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_x509_crt_init( &cacert );
384 mbedtls_x509_crt_init( &clicert );
385 mbedtls_pk_init( &pkey );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200386 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000387
388 if( argc == 0 )
389 {
390 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 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();
Paul Bakker1496d382011-05-23 12:07:29 +0000394 while( *list )
395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000397 list++;
398 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_printf("\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000400 goto exit;
401 }
402
403 opt.server_name = DFL_SERVER_NAME;
404 opt.server_port = DFL_SERVER_PORT;
405 opt.debug_level = DFL_DEBUG_LEVEL;
406 opt.authentication = DFL_AUTHENTICATION;
407 opt.mode = DFL_MODE;
408 opt.user_name = DFL_USER_NAME;
409 opt.user_pwd = DFL_USER_PWD;
410 opt.mail_from = DFL_MAIL_FROM;
411 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000412 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000413 opt.crt_file = DFL_CRT_FILE;
414 opt.key_file = DFL_KEY_FILE;
415 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
416
417 for( i = 1; i < argc; i++ )
418 {
Paul Bakker1496d382011-05-23 12:07:29 +0000419 p = argv[i];
420 if( ( q = strchr( p, '=' ) ) == NULL )
421 goto usage;
422 *q++ = '\0';
423
424 if( strcmp( p, "server_name" ) == 0 )
425 opt.server_name = q;
426 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200427 opt.server_port = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000428 else if( strcmp( p, "debug_level" ) == 0 )
429 {
430 opt.debug_level = atoi( q );
431 if( opt.debug_level < 0 || opt.debug_level > 65535 )
432 goto usage;
433 }
434 else if( strcmp( p, "authentication" ) == 0 )
435 {
436 opt.authentication = atoi( q );
437 if( opt.authentication < 0 || opt.authentication > 1 )
438 goto usage;
439 }
440 else if( strcmp( p, "mode" ) == 0 )
441 {
442 opt.mode = atoi( q );
443 if( opt.mode < 0 || opt.mode > 1 )
444 goto usage;
445 }
446 else if( strcmp( p, "user_name" ) == 0 )
447 opt.user_name = q;
448 else if( strcmp( p, "user_pwd" ) == 0 )
449 opt.user_pwd = q;
450 else if( strcmp( p, "mail_from" ) == 0 )
451 opt.mail_from = q;
452 else if( strcmp( p, "mail_to" ) == 0 )
453 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000454 else if( strcmp( p, "ca_file" ) == 0 )
455 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000456 else if( strcmp( p, "crt_file" ) == 0 )
457 opt.crt_file = q;
458 else if( strcmp( p, "key_file" ) == 0 )
459 opt.key_file = q;
460 else if( strcmp( p, "force_ciphersuite" ) == 0 )
461 {
462 opt.force_ciphersuite[0] = -1;
463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker1496d382011-05-23 12:07:29 +0000465
466 if( opt.force_ciphersuite[0] <= 0 )
467 goto usage;
468
469 opt.force_ciphersuite[1] = 0;
470 }
471 else
472 goto usage;
473 }
474
475 /*
476 * 0. Initialize the RNG and the session data
477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000479 fflush( stdout );
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200482 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200483 (const unsigned char *) pers,
484 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000485 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200486 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000487 goto exit;
488 }
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000491
492 /*
493 * 1.1. Load the trusted CA
494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000496 fflush( stdout );
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000499 if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000501 else
502#endif
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100503#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
505 mbedtls_test_cas_pem_len );
Paul Bakker5690efc2011-05-26 13:16:06 +0000506#else
507 {
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100508 mbedtls_printf("MBEDTLS_PEM_PARSE_C not defined.");
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100509 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000510 }
511#endif
Paul Bakker42488232012-05-16 08:21:05 +0000512 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000515 goto exit;
516 }
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000519
520 /*
521 * 1.2. Load own certificate and private key
522 *
523 * (can be skipped if client authentication is not required)
524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000526 fflush( stdout );
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000529 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200531 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
534 mbedtls_test_cli_crt_len );
Paul Bakker1496d382011-05-23 12:07:29 +0000535 if( ret != 0 )
536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000538 goto exit;
539 }
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541#if defined(MBEDTLS_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000542 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200543 {
544 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "",
545 mbedtls_ctr_drbg_random, &ctr_drbg );
546 }
Paul Bakker1496d382011-05-23 12:07:29 +0000547 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000548#endif
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100549#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200552 mbedtls_test_cli_key_len, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg );
553 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000554#else
555 {
Mateusz Starzyk1aec6462021-02-08 15:34:42 +0100556 mbedtls_printf("MBEDTLS_PEM_PARSE_C not defined.");
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100557 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000558 }
559#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000560 if( ret != 0 )
561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000563 goto exit;
564 }
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000567
568 /*
569 * 2. Start the connection
570 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200571 mbedtls_printf( " . Connecting to tcp/%s/%s...", opt.server_name,
Paul Bakker1496d382011-05-23 12:07:29 +0000572 opt.server_port );
573 fflush( stdout );
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_name,
576 opt.server_port, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000579 goto exit;
580 }
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000583
584 /*
585 * 3. Setup stuff
586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000588 fflush( stdout );
589
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200590 if( ( ret = mbedtls_ssl_config_defaults( &conf,
591 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200592 MBEDTLS_SSL_TRANSPORT_STREAM,
593 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200594 {
595 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
596 goto exit;
597 }
598
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100599 /* OPTIONAL is not optimal for security,
600 * but makes interop easier in this simplified example */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200601 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
Paul Bakker1496d382011-05-23 12:07:29 +0000602
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200603 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
604 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakker1496d382011-05-23 12:07:29 +0000605
Paul Bakker645ce3a2012-10-31 12:32:41 +0000606 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200607 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Paul Bakker1496d382011-05-23 12:07:29 +0000608
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200609 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200610 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200611 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200612 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200613 goto exit;
614 }
Paul Bakker1496d382011-05-23 12:07:29 +0000615
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200616 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
617 {
618 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
619 goto exit;
620 }
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200625 goto exit;
626 }
Paul Bakker1496d382011-05-23 12:07:29 +0000627
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200628 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
629
630 mbedtls_printf( " ok\n" );
631
Paul Bakker1496d382011-05-23 12:07:29 +0000632 if( opt.mode == MODE_SSL_TLS )
633 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100634 if( do_handshake( &ssl ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000635 goto exit;
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000638 fflush( stdout );
639
640 ret = write_ssl_and_get_response( &ssl, buf, 0 );
641 if( ret < 200 || ret > 299 )
642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000644 goto exit;
645 }
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000650 fflush( stdout );
651
652 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100653 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000654 ret = write_ssl_and_get_response( &ssl, buf, len );
655 if( ret < 200 || ret > 299 )
656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000658 goto exit;
659 }
660 }
661 else
662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000664 fflush( stdout );
665
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200666 ret = write_and_get_response( &server_fd, buf, 0 );
Paul Bakker1496d382011-05-23 12:07:29 +0000667 if( ret < 200 || ret > 299 )
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000670 goto exit;
671 }
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000676 fflush( stdout );
677
678 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100679 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200680 ret = write_and_get_response( &server_fd, buf, len );
Paul Bakker1496d382011-05-23 12:07:29 +0000681 if( ret < 200 || ret > 299 )
682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000684 goto exit;
685 }
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_printf( " > Write STARTTLS to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000690 fflush( stdout );
691
692 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100693 len = sprintf( (char *) buf, "STARTTLS\r\n" );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200694 ret = write_and_get_response( &server_fd, buf, len );
Paul Bakker1496d382011-05-23 12:07:29 +0000695 if( ret < 200 || ret > 299 )
696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000698 goto exit;
699 }
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000702
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100703 if( do_handshake( &ssl ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000704 goto exit;
705 }
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#if defined(MBEDTLS_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000708 if( opt.authentication )
709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_printf( " > Write AUTH LOGIN to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000711 fflush( stdout );
712
Paul Bakkerd75ba402014-01-24 16:11:17 +0100713 len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000714 ret = write_ssl_and_get_response( &ssl, buf, len );
715 if( ret < 200 || ret > 399 )
716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000718 goto exit;
719 }
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_printf( " > Write username to server: %s", opt.user_name );
Paul Bakker1496d382011-05-23 12:07:29 +0000724 fflush( stdout );
725
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100726 ret = mbedtls_base64_encode( base, sizeof( base ), &n, (const unsigned char *) opt.user_name,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200727 strlen( opt.user_name ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200728
729 if( ret != 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_printf( " failed\n ! mbedtls_base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200731 goto exit;
732 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100733 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000734 ret = write_ssl_and_get_response( &ssl, buf, len );
735 if( ret < 300 || ret > 399 )
736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000738 goto exit;
739 }
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_printf( " > Write password to server: %s", opt.user_pwd );
Paul Bakker1496d382011-05-23 12:07:29 +0000744 fflush( stdout );
745
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100746 ret = mbedtls_base64_encode( base, sizeof( base ), &n, (const unsigned char *) opt.user_pwd,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200747 strlen( opt.user_pwd ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200748
749 if( ret != 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_printf( " failed\n ! mbedtls_base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200751 goto exit;
752 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100753 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000754 ret = write_ssl_and_get_response( &ssl, buf, len );
755 if( ret < 200 || ret > 399 )
756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000758 goto exit;
759 }
760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000762 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000763#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_printf( " > Write MAIL FROM to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000766 fflush( stdout );
767
Paul Bakkerd75ba402014-01-24 16:11:17 +0100768 len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
Paul Bakker1496d382011-05-23 12:07:29 +0000769 ret = write_ssl_and_get_response( &ssl, buf, len );
770 if( ret < 200 || ret > 299 )
771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000773 goto exit;
774 }
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_printf( " > Write RCPT TO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000779 fflush( stdout );
780
Paul Bakkerd75ba402014-01-24 16:11:17 +0100781 len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
Paul Bakker1496d382011-05-23 12:07:29 +0000782 ret = write_ssl_and_get_response( &ssl, buf, len );
783 if( ret < 200 || ret > 299 )
784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000786 goto exit;
787 }
788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_printf( " > Write DATA to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000792 fflush( stdout );
793
Paul Bakkerd75ba402014-01-24 16:11:17 +0100794 len = sprintf( (char *) buf, "DATA\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000795 ret = write_ssl_and_get_response( &ssl, buf, len );
796 if( ret < 300 || ret > 399 )
797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000799 goto exit;
800 }
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_printf( " > Write content to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000805 fflush( stdout );
806
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000807 len = sprintf( (char *) buf, "From: %s\r\nSubject: mbed TLS Test mail\r\n\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000808 "This is a simple test mail from the "
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000809 "mbed TLS mail client example.\r\n"
Paul Bakkerd75ba402014-01-24 16:11:17 +0100810 "\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000811 "Enjoy!", opt.mail_from );
812 ret = write_ssl_data( &ssl, buf, len );
813
814 len = sprintf( (char *) buf, "\r\n.\r\n");
815 ret = write_ssl_and_get_response( &ssl, buf, len );
816 if( ret < 200 || ret > 299 )
817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000819 goto exit;
820 }
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_ssl_close_notify( &ssl );
Paul Bakker1496d382011-05-23 12:07:29 +0000825
Andres Amaya Garcia67a42ac2018-04-29 21:04:29 +0100826 exit_code = MBEDTLS_EXIT_SUCCESS;
827
Paul Bakker1496d382011-05-23 12:07:29 +0000828exit:
829
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200830 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_x509_crt_free( &clicert );
832 mbedtls_x509_crt_free( &cacert );
833 mbedtls_pk_free( &pkey );
834 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200835 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_ctr_drbg_free( &ctr_drbg );
837 mbedtls_entropy_free( &entropy );
Paul Bakker1496d382011-05-23 12:07:29 +0000838
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200839 mbedtls_exit( exit_code );
Paul Bakker1496d382011-05-23 12:07:29 +0000840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
842 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C **
843 MBEDTLS_CTR_DRBG_C */