blob: ed474f33300a8565b732523a8d16807554e57521 [file] [log] [blame]
Paul Bakker1496d382011-05-23 12:07:29 +00001/*
2 * SSL client for SMTP servers
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker1496d382011-05-23 12:07:29 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker1496d382011-05-23 12:07:29 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker1496d382011-05-23 12:07:29 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Simon Butcherd3138c32016-04-27 01:26:50 +010059#include <stdlib.h>
Andres Amaya Garciaea14fe72018-04-29 21:04:29 +010060#define mbedtls_time time
61#define mbedtls_time_t time_t
62#define mbedtls_fprintf fprintf
63#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020064#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010065#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaea14fe72018-04-29 21:04:29 +010066#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
67#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
70 !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
71 !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
72 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
73 !defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010074int main( void )
75{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
77 "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
78 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
79 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010080 "not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020081 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard4b3e5ef2015-03-27 11:24:27 +010082}
83#else
84
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000085#include "mbedtls/base64.h"
86#include "mbedtls/error.h"
Andres AG788aa4a2016-09-14 14:32:09 +010087#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000088#include "mbedtls/ssl.h"
89#include "mbedtls/entropy.h"
90#include "mbedtls/ctr_drbg.h"
91#include "mbedtls/certs.h"
92#include "mbedtls/x509.h"
Rich Evans18b78c72015-02-11 14:06:19 +000093
Rich Evans18b78c72015-02-11 14:06:19 +000094#include <stdlib.h>
95#include <string.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010096
97#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
Paul Bakker1496d382011-05-23 12:07:29 +000098#include <unistd.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010099#else
100#include <io.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +0100101#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000102
Paul Bakker5a835222011-10-12 09:19:31 +0000103#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5a835222011-10-12 09:19:31 +0000104#include <winsock2.h>
105#include <windows.h>
106
Paul Bakkerf0fc2a22013-12-30 15:42:43 +0100107#if defined(_MSC_VER)
Paul Bakker5a835222011-10-12 09:19:31 +0000108#if defined(_WIN32_WCE)
109#pragma comment( lib, "ws2.lib" )
110#else
111#pragma comment( lib, "ws2_32.lib" )
112#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +0100113#endif /* _MSC_VER */
Paul Bakker5a835222011-10-12 09:19:31 +0000114#endif
115
Paul Bakker1496d382011-05-23 12:07:29 +0000116#define DFL_SERVER_NAME "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200117#define DFL_SERVER_PORT "465"
Paul Bakker1496d382011-05-23 12:07:29 +0000118#define DFL_USER_NAME "user"
119#define DFL_USER_PWD "password"
120#define DFL_MAIL_FROM ""
121#define DFL_MAIL_TO ""
122#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +0000123#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +0000124#define DFL_CRT_FILE ""
125#define DFL_KEY_FILE ""
126#define DFL_FORCE_CIPHER 0
127#define DFL_MODE 0
128#define DFL_AUTHENTICATION 0
129
130#define MODE_SSL_TLS 0
131#define MODE_STARTTLS 0
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_BASE64_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000134#define USAGE_AUTH \
irwirc6f45392019-01-19 19:05:56 +0300135 " authentication=%%d default: 0 (disabled)\n" \
136 " user_name=%%s default: \"" DFL_USER_NAME "\"\n" \
137 " user_pwd=%%s default: \"" DFL_USER_PWD "\"\n"
Rich Evans85b05ec2015-02-12 11:37:29 +0000138#else
139#define USAGE_AUTH \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 " authentication options disabled. (Require MBEDTLS_BASE64_C)\n"
141#endif /* MBEDTLS_BASE64_C */
Rich Evans85b05ec2015-02-12 11:37:29 +0000142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +0000144#define USAGE_IO \
145 " ca_file=%%s default: \"\" (pre-loaded)\n" \
146 " crt_file=%%s default: \"\" (pre-loaded)\n" \
147 " key_file=%%s default: \"\" (pre-loaded)\n"
148#else
149#define USAGE_IO \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 " No file operations available (MBEDTLS_FS_IO not defined)\n"
151#endif /* MBEDTLS_FS_IO */
Rich Evans85b05ec2015-02-12 11:37:29 +0000152
153#define USAGE \
irwirc6f45392019-01-19 19:05:56 +0300154 "\n usage: ssl_mail_client param=<>...\n" \
155 "\n acceptable parameters:\n" \
156 " server_name=%%s default: " DFL_SERVER_NAME "\n" \
157 " server_port=%%d default: " DFL_SERVER_PORT "\n" \
158 " debug_level=%%d default: 0 (disabled)\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000159 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
irwirc6f45392019-01-19 19:05:56 +0300160 USAGE_AUTH \
161 " mail_from=%%s default: \"\"\n" \
162 " mail_to=%%s default: \"\"\n" \
163 USAGE_IO \
164 " force_ciphersuite=<name> default: all enabled\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000165 " acceptable ciphersuite names:\n"
166
Paul Bakker1496d382011-05-23 12:07:29 +0000167/*
168 * global options
169 */
170struct options
171{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200172 const char *server_name; /* hostname of the server (client only) */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200173 const char *server_port; /* port on which the ssl service runs */
Paul Bakker1496d382011-05-23 12:07:29 +0000174 int debug_level; /* level of debugging */
175 int authentication; /* if authentication is required */
176 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200177 const char *user_name; /* username to use for authentication */
178 const char *user_pwd; /* password to use for authentication */
179 const char *mail_from; /* E-Mail address to use as sender */
180 const char *mail_to; /* E-Mail address to use as recipient */
181 const char *ca_file; /* the file with the CA certificate(s) */
182 const char *crt_file; /* the file with the client certificate */
183 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000184 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
185} opt;
186
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200187static void my_debug( void *ctx, int level,
188 const char *file, int line,
189 const char *str )
Paul Bakker1496d382011-05-23 12:07:29 +0000190{
Manuel Pégourié-Gonnard61ee3512015-06-23 17:35:03 +0200191 ((void) level);
192
193 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
194 fflush( (FILE *) ctx );
Paul Bakker1496d382011-05-23 12:07:29 +0000195}
196
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100197static int do_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1496d382011-05-23 12:07:29 +0000198{
199 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200200 uint32_t flags;
Paul Bakker1496d382011-05-23 12:07:29 +0000201 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000202 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000203
204 /*
205 * 4. Handshake
206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000208 fflush( stdout );
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 while( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000211 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100212 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker1496d382011-05-23 12:07:29 +0000213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_ERROR_C)
215 mbedtls_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned %d: %s\n\n", ret, buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000218 return( -1 );
219 }
220 }
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_printf( " ok\n [ Ciphersuite is %s ]\n",
223 mbedtls_ssl_get_ciphersuite( ssl ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000224
225 /*
226 * 5. Verify the server certificate
227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000229
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100230 /* In real life, we probably want to bail out when ret != 0 */
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200231 if( ( flags = mbedtls_ssl_get_verify_result( ssl ) ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000232 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100233 char vrfy_buf[512];
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( " failed\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000236
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +0200237 mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
Paul Bakker1496d382011-05-23 12:07:29 +0000238
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100239 mbedtls_printf( "%s\n", vrfy_buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000240 }
241 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_printf( " . Peer certificate information ...\n" );
245 mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
246 mbedtls_ssl_get_peer_cert( ssl ) );
247 mbedtls_printf( "%s\n", buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000248
249 return( 0 );
250}
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252static int write_ssl_data( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000253{
254 int ret;
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf("\n%s", buf);
257 while( len && ( ret = mbedtls_ssl_write( ssl, buf, len ) ) <= 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000258 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100259 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker1496d382011-05-23 12:07:29 +0000260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000262 return -1;
263 }
264 }
265
266 return( 0 );
267}
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269static int write_ssl_and_get_response( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000270{
271 int ret;
272 unsigned char data[128];
273 char code[4];
274 size_t i, idx = 0;
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_printf("\n%s", buf);
277 while( len && ( ret = mbedtls_ssl_write( ssl, buf, len ) ) <= 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000278 {
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100279 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker1496d382011-05-23 12:07:29 +0000280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000282 return -1;
283 }
284 }
285
286 do
287 {
288 len = sizeof( data ) - 1;
289 memset( data, 0, sizeof( data ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 ret = mbedtls_ssl_read( ssl, data, len );
Paul Bakker1496d382011-05-23 12:07:29 +0000291
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100292 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
Paul Bakker1496d382011-05-23 12:07:29 +0000293 continue;
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
Paul Bakker1496d382011-05-23 12:07:29 +0000296 return -1;
297
298 if( ret <= 0 )
299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000301 return -1;
302 }
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000305 len = ret;
306 for( i = 0; i < len; i++ )
307 {
308 if( data[i] != '\n' )
309 {
310 if( idx < 4 )
311 code[ idx++ ] = data[i];
312 continue;
313 }
314
315 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
316 {
317 code[3] = '\0';
318 return atoi( code );
319 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200320
Paul Bakker1496d382011-05-23 12:07:29 +0000321 idx = 0;
322 }
323 }
324 while( 1 );
325}
326
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200327static int write_and_get_response( mbedtls_net_context *sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000328{
329 int ret;
330 unsigned char data[128];
331 char code[4];
332 size_t i, idx = 0;
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_printf("\n%s", buf);
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200335 if( len && ( ret = mbedtls_net_send( sock_fd, buf, len ) ) <= 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000336 {
irwirc6f45392019-01-19 19:05:56 +0300337 mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000338 return -1;
339 }
340
341 do
342 {
343 len = sizeof( data ) - 1;
344 memset( data, 0, sizeof( data ) );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200345 ret = mbedtls_net_recv( sock_fd, data, len );
Paul Bakker1496d382011-05-23 12:07:29 +0000346
347 if( ret <= 0 )
348 {
irwirc6f45392019-01-19 19:05:56 +0300349 mbedtls_printf( "failed\n ! mbedtls_net_recv returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000350 return -1;
351 }
352
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200353 data[len] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000355 len = ret;
356 for( i = 0; i < len; i++ )
357 {
358 if( data[i] != '\n' )
359 {
360 if( idx < 4 )
361 code[ idx++ ] = data[i];
362 continue;
363 }
364
365 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
366 {
367 code[3] = '\0';
368 return atoi( code );
369 }
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000370
Paul Bakker1496d382011-05-23 12:07:29 +0000371 idx = 0;
372 }
373 }
374 while( 1 );
375}
376
Paul Bakker1496d382011-05-23 12:07:29 +0000377int main( int argc, char *argv[] )
378{
Andres Amaya Garciaea14fe72018-04-29 21:04:29 +0100379 int ret = 1, len;
380 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200381 mbedtls_net_context server_fd;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000383 unsigned char base[1024];
Mohammad Azim Khan7e84aff2018-08-06 11:48:06 +0100384 /* buf is used as the destination buffer for printing base with the format:
385 * "%s\r\n". Hence, the size of buf should be at least the size of base
386 * plus 2 bytes for the \r and \n characters.
387 */
388 unsigned char buf[sizeof( base ) + 2];
389#else
390 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000391#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000392 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200393 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_entropy_context entropy;
396 mbedtls_ctr_drbg_context ctr_drbg;
397 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200398 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_x509_crt cacert;
400 mbedtls_x509_crt clicert;
401 mbedtls_pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000402 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000403 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000404 char *p, *q;
405 const int *list;
406
407 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200408 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000409 */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200410 mbedtls_net_init( &server_fd );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200411 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200412 mbedtls_ssl_config_init( &conf );
Paul Bakker333fdec2014-08-04 12:12:09 +0200413 memset( &buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_x509_crt_init( &cacert );
415 mbedtls_x509_crt_init( &clicert );
416 mbedtls_pk_init( &pkey );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200417 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000418
419 if( argc == 0 )
420 {
421 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_printf( USAGE );
Paul Bakker1496d382011-05-23 12:07:29 +0000423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 list = mbedtls_ssl_list_ciphersuites();
Paul Bakker1496d382011-05-23 12:07:29 +0000425 while( *list )
426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000428 list++;
429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_printf("\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000431 goto exit;
432 }
433
434 opt.server_name = DFL_SERVER_NAME;
435 opt.server_port = DFL_SERVER_PORT;
436 opt.debug_level = DFL_DEBUG_LEVEL;
437 opt.authentication = DFL_AUTHENTICATION;
438 opt.mode = DFL_MODE;
439 opt.user_name = DFL_USER_NAME;
440 opt.user_pwd = DFL_USER_PWD;
441 opt.mail_from = DFL_MAIL_FROM;
442 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000443 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000444 opt.crt_file = DFL_CRT_FILE;
445 opt.key_file = DFL_KEY_FILE;
446 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
447
448 for( i = 1; i < argc; i++ )
449 {
Paul Bakker1496d382011-05-23 12:07:29 +0000450 p = argv[i];
451 if( ( q = strchr( p, '=' ) ) == NULL )
452 goto usage;
453 *q++ = '\0';
454
455 if( strcmp( p, "server_name" ) == 0 )
456 opt.server_name = q;
457 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200458 opt.server_port = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000459 else if( strcmp( p, "debug_level" ) == 0 )
460 {
461 opt.debug_level = atoi( q );
462 if( opt.debug_level < 0 || opt.debug_level > 65535 )
463 goto usage;
464 }
465 else if( strcmp( p, "authentication" ) == 0 )
466 {
467 opt.authentication = atoi( q );
468 if( opt.authentication < 0 || opt.authentication > 1 )
469 goto usage;
470 }
471 else if( strcmp( p, "mode" ) == 0 )
472 {
473 opt.mode = atoi( q );
474 if( opt.mode < 0 || opt.mode > 1 )
475 goto usage;
476 }
477 else if( strcmp( p, "user_name" ) == 0 )
478 opt.user_name = q;
479 else if( strcmp( p, "user_pwd" ) == 0 )
480 opt.user_pwd = q;
481 else if( strcmp( p, "mail_from" ) == 0 )
482 opt.mail_from = q;
483 else if( strcmp( p, "mail_to" ) == 0 )
484 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000485 else if( strcmp( p, "ca_file" ) == 0 )
486 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000487 else if( strcmp( p, "crt_file" ) == 0 )
488 opt.crt_file = q;
489 else if( strcmp( p, "key_file" ) == 0 )
490 opt.key_file = q;
491 else if( strcmp( p, "force_ciphersuite" ) == 0 )
492 {
493 opt.force_ciphersuite[0] = -1;
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
Paul Bakker1496d382011-05-23 12:07:29 +0000496
497 if( opt.force_ciphersuite[0] <= 0 )
498 goto usage;
499
500 opt.force_ciphersuite[1] = 0;
501 }
502 else
503 goto usage;
504 }
505
506 /*
507 * 0. Initialize the RNG and the session data
508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000510 fflush( stdout );
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200513 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200514 (const unsigned char *) pers,
515 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000516 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200517 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000518 goto exit;
519 }
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000522
523 /*
524 * 1.1. Load the trusted CA
525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000527 fflush( stdout );
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529#if defined(MBEDTLS_FS_IO)
Paul Bakker5690efc2011-05-26 13:16:06 +0000530 if( strlen( opt.ca_file ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000532 else
533#endif
Andres AGcef21e42017-02-02 17:01:10 +0000534#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
536 mbedtls_test_cas_pem_len );
Paul Bakker5690efc2011-05-26 13:16:06 +0000537#else
538 {
Andres AGcef21e42017-02-02 17:01:10 +0000539 mbedtls_printf("MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.");
Andres Amaya Garciaea14fe72018-04-29 21:04:29 +0100540 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000541 }
542#endif
Paul Bakker42488232012-05-16 08:21:05 +0000543 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000546 goto exit;
547 }
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000550
551 /*
552 * 1.2. Load own certificate and private key
553 *
554 * (can be skipped if client authentication is not required)
555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_printf( " . Loading the client cert. and key..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000557 fflush( stdout );
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#if defined(MBEDTLS_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000560 if( strlen( opt.crt_file ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200562 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000563#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#if defined(MBEDTLS_CERTS_C)
565 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
566 mbedtls_test_cli_crt_len );
Paul Bakker5690efc2011-05-26 13:16:06 +0000567#else
568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_printf("MBEDTLS_CERTS_C not defined.");
Andres Amaya Garciaea14fe72018-04-29 21:04:29 +0100570 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000571 }
572#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000573 if( ret != 0 )
574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000576 goto exit;
577 }
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#if defined(MBEDTLS_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000580 if( strlen( opt.key_file ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker1496d382011-05-23 12:07:29 +0000582 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000583#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
585 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
586 mbedtls_test_cli_key_len, NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000587#else
588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_printf("MBEDTLS_CERTS_C or MBEDTLS_PEM_PARSE_C not defined.");
Andres Amaya Garciaea14fe72018-04-29 21:04:29 +0100590 goto exit;
Paul Bakker5690efc2011-05-26 13:16:06 +0000591 }
592#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000593 if( ret != 0 )
594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000596 goto exit;
597 }
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000600
601 /*
602 * 2. Start the connection
603 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200604 mbedtls_printf( " . Connecting to tcp/%s/%s...", opt.server_name,
Paul Bakker1496d382011-05-23 12:07:29 +0000605 opt.server_port );
606 fflush( stdout );
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_name,
609 opt.server_port, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000612 goto exit;
613 }
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000616
617 /*
618 * 3. Setup stuff
619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000621 fflush( stdout );
622
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200623 if( ( ret = mbedtls_ssl_config_defaults( &conf,
624 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200625 MBEDTLS_SSL_TRANSPORT_STREAM,
626 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200627 {
628 mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
629 goto exit;
630 }
631
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100632 /* OPTIONAL is not optimal for security,
633 * but makes interop easier in this simplified example */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200634 mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
Paul Bakker1496d382011-05-23 12:07:29 +0000635
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200636 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
637 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
Paul Bakker1496d382011-05-23 12:07:29 +0000638
Paul Bakker645ce3a2012-10-31 12:32:41 +0000639 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200640 mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
Paul Bakker1496d382011-05-23 12:07:29 +0000641
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200642 mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200643 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200644 {
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +0200645 mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200646 goto exit;
647 }
Paul Bakker1496d382011-05-23 12:07:29 +0000648
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200649 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
650 {
651 mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
652 goto exit;
653 }
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200658 goto exit;
659 }
Paul Bakker1496d382011-05-23 12:07:29 +0000660
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +0200661 mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
662
663 mbedtls_printf( " ok\n" );
664
Paul Bakker1496d382011-05-23 12:07:29 +0000665 if( opt.mode == MODE_SSL_TLS )
666 {
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100667 if( do_handshake( &ssl ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000668 goto exit;
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000671 fflush( stdout );
672
673 ret = write_ssl_and_get_response( &ssl, buf, 0 );
674 if( ret < 200 || ret > 299 )
675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000677 goto exit;
678 }
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000683 fflush( stdout );
684
685 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100686 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000687 ret = write_ssl_and_get_response( &ssl, buf, len );
688 if( ret < 200 || ret > 299 )
689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000691 goto exit;
692 }
693 }
694 else
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000697 fflush( stdout );
698
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200699 ret = write_and_get_response( &server_fd, buf, 0 );
Paul Bakker1496d382011-05-23 12:07:29 +0000700 if( ret < 200 || ret > 299 )
701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000703 goto exit;
704 }
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000709 fflush( stdout );
710
711 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100712 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200713 ret = write_and_get_response( &server_fd, buf, len );
Paul Bakker1496d382011-05-23 12:07:29 +0000714 if( ret < 200 || ret > 299 )
715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000717 goto exit;
718 }
719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_printf( " > Write STARTTLS to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000723 fflush( stdout );
724
725 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100726 len = sprintf( (char *) buf, "STARTTLS\r\n" );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200727 ret = write_and_get_response( &server_fd, buf, len );
Paul Bakker1496d382011-05-23 12:07:29 +0000728 if( ret < 200 || ret > 299 )
729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000731 goto exit;
732 }
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000735
Manuel Pégourié-Gonnard89addc42015-04-20 10:56:18 +0100736 if( do_handshake( &ssl ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000737 goto exit;
738 }
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000741 if( opt.authentication )
742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_printf( " > Write AUTH LOGIN to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000744 fflush( stdout );
745
Paul Bakkerd75ba402014-01-24 16:11:17 +0100746 len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000747 ret = write_ssl_and_get_response( &ssl, buf, len );
748 if( ret < 200 || ret > 399 )
749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000751 goto exit;
752 }
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_printf( " > Write username to server: %s", opt.user_name );
Paul Bakker1496d382011-05-23 12:07:29 +0000757 fflush( stdout );
758
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100759 ret = mbedtls_base64_encode( base, sizeof( base ), &n, (const unsigned char *) opt.user_name,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200760 strlen( opt.user_name ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200761
762 if( ret != 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_printf( " failed\n ! mbedtls_base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200764 goto exit;
765 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100766 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000767 ret = write_ssl_and_get_response( &ssl, buf, len );
768 if( ret < 300 || ret > 399 )
769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000771 goto exit;
772 }
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_printf( " > Write password to server: %s", opt.user_pwd );
Paul Bakker1496d382011-05-23 12:07:29 +0000777 fflush( stdout );
778
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100779 ret = mbedtls_base64_encode( base, sizeof( base ), &n, (const unsigned char *) opt.user_pwd,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200780 strlen( opt.user_pwd ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200781
782 if( ret != 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_printf( " failed\n ! mbedtls_base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200784 goto exit;
785 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100786 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000787 ret = write_ssl_and_get_response( &ssl, buf, len );
788 if( ret < 200 || ret > 399 )
789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000791 goto exit;
792 }
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000795 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000796#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_printf( " > Write MAIL FROM to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000799 fflush( stdout );
800
Paul Bakkerd75ba402014-01-24 16:11:17 +0100801 len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
Paul Bakker1496d382011-05-23 12:07:29 +0000802 ret = write_ssl_and_get_response( &ssl, buf, len );
803 if( ret < 200 || ret > 299 )
804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000806 goto exit;
807 }
808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_printf( " > Write RCPT TO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000812 fflush( stdout );
813
Paul Bakkerd75ba402014-01-24 16:11:17 +0100814 len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
Paul Bakker1496d382011-05-23 12:07:29 +0000815 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_printf( " > Write DATA to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000825 fflush( stdout );
826
Paul Bakkerd75ba402014-01-24 16:11:17 +0100827 len = sprintf( (char *) buf, "DATA\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000828 ret = write_ssl_and_get_response( &ssl, buf, len );
829 if( ret < 300 || ret > 399 )
830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000832 goto exit;
833 }
834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_printf( " > Write content to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000838 fflush( stdout );
839
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000840 len = sprintf( (char *) buf, "From: %s\r\nSubject: mbed TLS Test mail\r\n\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000841 "This is a simple test mail from the "
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000842 "mbed TLS mail client example.\r\n"
Paul Bakkerd75ba402014-01-24 16:11:17 +0100843 "\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000844 "Enjoy!", opt.mail_from );
845 ret = write_ssl_data( &ssl, buf, len );
846
847 len = sprintf( (char *) buf, "\r\n.\r\n");
848 ret = write_ssl_and_get_response( &ssl, buf, len );
849 if( ret < 200 || ret > 299 )
850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000852 goto exit;
853 }
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 mbedtls_ssl_close_notify( &ssl );
Paul Bakker1496d382011-05-23 12:07:29 +0000858
Andres Amaya Garciaea14fe72018-04-29 21:04:29 +0100859 exit_code = MBEDTLS_EXIT_SUCCESS;
860
Paul Bakker1496d382011-05-23 12:07:29 +0000861exit:
862
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200863 mbedtls_net_free( &server_fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_x509_crt_free( &clicert );
865 mbedtls_x509_crt_free( &cacert );
866 mbedtls_pk_free( &pkey );
867 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200868 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_ctr_drbg_free( &ctr_drbg );
870 mbedtls_entropy_free( &entropy );
Paul Bakker1496d382011-05-23 12:07:29 +0000871
Paul Bakkercce9d772011-11-18 14:26:47 +0000872#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000874 fflush( stdout ); getchar();
875#endif
876
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200877 mbedtls_exit( exit_code );
Paul Bakker1496d382011-05-23 12:07:29 +0000878}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
880 MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C **
881 MBEDTLS_CTR_DRBG_C */