blob: 31c0b6ee8b75168c81f44c0a172e6fc083650938 [file] [log] [blame]
Paul Bakker1496d382011-05-23 12:07:29 +00001/*
2 * SSL client for SMTP servers
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved
Paul Bakker1496d382011-05-23 12:07:29 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker1496d382011-05-23 12:07:29 +00007 *
Paul Bakker1496d382011-05-23 12:07:29 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker1496d382011-05-23 12:07:29 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Paul Bakker1496d382011-05-23 12:07:29 +000036#include <string.h>
37#include <stdlib.h>
38#include <stdio.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010039
40#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
Paul Bakker1496d382011-05-23 12:07:29 +000041#include <unistd.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010042#else
43#include <io.h>
44#define read _read
45#define write _write
46#endif
Paul Bakker1496d382011-05-23 12:07:29 +000047
Paul Bakker5a835222011-10-12 09:19:31 +000048#if defined(_WIN32) || defined(_WIN32_WCE)
49
50#include <winsock2.h>
51#include <windows.h>
52
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010053#if defined(_MSC_VER)
Paul Bakker5a835222011-10-12 09:19:31 +000054#if defined(_WIN32_WCE)
55#pragma comment( lib, "ws2.lib" )
56#else
57#pragma comment( lib, "ws2_32.lib" )
58#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010059#endif /* _MSC_VER */
Paul Bakker5a835222011-10-12 09:19:31 +000060#endif
61
Paul Bakker1496d382011-05-23 12:07:29 +000062#include "polarssl/base64.h"
63#include "polarssl/error.h"
64#include "polarssl/net.h"
65#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000066#include "polarssl/entropy.h"
67#include "polarssl/ctr_drbg.h"
Paul Bakker1496d382011-05-23 12:07:29 +000068#include "polarssl/certs.h"
69#include "polarssl/x509.h"
70
Paul Bakker9a97c5d2013-09-15 17:07:33 +020071#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
72 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
73 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020074 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020075int main( int argc, char *argv[] )
76{
77 ((void) argc);
78 ((void) argv);
79
Rich Evansf90016a2015-01-19 14:26:37 +000080 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker9a97c5d2013-09-15 17:07:33 +020081 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
82 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020083 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
Paul Bakker9a97c5d2013-09-15 17:07:33 +020084 "not defined.\n");
85 return( 0 );
86}
87#else
88
Paul Bakker1496d382011-05-23 12:07:29 +000089#define DFL_SERVER_NAME "localhost"
90#define DFL_SERVER_PORT 465
91#define DFL_USER_NAME "user"
92#define DFL_USER_PWD "password"
93#define DFL_MAIL_FROM ""
94#define DFL_MAIL_TO ""
95#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000096#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000097#define DFL_CRT_FILE ""
98#define DFL_KEY_FILE ""
99#define DFL_FORCE_CIPHER 0
100#define DFL_MODE 0
101#define DFL_AUTHENTICATION 0
102
103#define MODE_SSL_TLS 0
104#define MODE_STARTTLS 0
105
106/*
107 * global options
108 */
109struct options
110{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200111 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +0000112 int server_port; /* port on which the ssl service runs */
113 int debug_level; /* level of debugging */
114 int authentication; /* if authentication is required */
115 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200116 const char *user_name; /* username to use for authentication */
117 const char *user_pwd; /* password to use for authentication */
118 const char *mail_from; /* E-Mail address to use as sender */
119 const char *mail_to; /* E-Mail address to use as recipient */
120 const char *ca_file; /* the file with the CA certificate(s) */
121 const char *crt_file; /* the file with the client certificate */
122 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000123 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
124} opt;
125
Paul Bakker3c5ef712013-06-25 16:37:45 +0200126static void my_debug( void *ctx, int level, const char *str )
Paul Bakker1496d382011-05-23 12:07:29 +0000127{
128 if( level < opt.debug_level )
129 {
Rich Evansf90016a2015-01-19 14:26:37 +0000130 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakker1496d382011-05-23 12:07:29 +0000131 fflush( (FILE *) ctx );
132 }
133}
134
Paul Bakker3c5ef712013-06-25 16:37:45 +0200135static int do_handshake( ssl_context *ssl, struct options *opt )
Paul Bakker1496d382011-05-23 12:07:29 +0000136{
137 int ret;
138 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000139 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000140
141 /*
142 * 4. Handshake
143 */
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000145 fflush( stdout );
146
147 while( ( ret = ssl_handshake( ssl ) ) != 0 )
148 {
149 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
150 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000151#if defined(POLARSSL_ERROR_C)
Paul Bakker03a8a792013-06-30 12:18:08 +0200152 polarssl_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000153#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000154 polarssl_printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000155 return( -1 );
156 }
157 }
158
Rich Evansf90016a2015-01-19 14:26:37 +0000159 polarssl_printf( " ok\n [ Ciphersuite is %s ]\n",
Paul Bakker1496d382011-05-23 12:07:29 +0000160 ssl_get_ciphersuite( ssl ) );
161
162 /*
163 * 5. Verify the server certificate
164 */
Rich Evansf90016a2015-01-19 14:26:37 +0000165 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000166
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100167 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker1496d382011-05-23 12:07:29 +0000168 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
169 {
Rich Evansf90016a2015-01-19 14:26:37 +0000170 polarssl_printf( " failed\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000171
172 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000173 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000174
175 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000176 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000177
178 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000179 polarssl_printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
Paul Bakker1496d382011-05-23 12:07:29 +0000180
181 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000182 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000183
Rich Evansf90016a2015-01-19 14:26:37 +0000184 polarssl_printf( "\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000185 }
186 else
Rich Evansf90016a2015-01-19 14:26:37 +0000187 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000188
Rich Evansf90016a2015-01-19 14:26:37 +0000189 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200190 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
191 ssl_get_peer_cert( ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +0000192 polarssl_printf( "%s\n", buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000193
194 return( 0 );
195}
196
Paul Bakker3c5ef712013-06-25 16:37:45 +0200197static int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000198{
199 int ret;
200
Rich Evansf90016a2015-01-19 14:26:37 +0000201 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000202 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
203 {
204 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
205 {
Rich Evansf90016a2015-01-19 14:26:37 +0000206 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000207 return -1;
208 }
209 }
210
211 return( 0 );
212}
213
Paul Bakker3c5ef712013-06-25 16:37:45 +0200214static int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000215{
216 int ret;
217 unsigned char data[128];
218 char code[4];
219 size_t i, idx = 0;
220
Rich Evansf90016a2015-01-19 14:26:37 +0000221 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000222 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
223 {
224 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
225 {
Rich Evansf90016a2015-01-19 14:26:37 +0000226 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000227 return -1;
228 }
229 }
230
231 do
232 {
233 len = sizeof( data ) - 1;
234 memset( data, 0, sizeof( data ) );
235 ret = ssl_read( ssl, data, len );
236
237 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
238 continue;
239
240 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
241 return -1;
242
243 if( ret <= 0 )
244 {
Rich Evansf90016a2015-01-19 14:26:37 +0000245 polarssl_printf( "failed\n ! ssl_read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000246 return -1;
247 }
248
Rich Evansf90016a2015-01-19 14:26:37 +0000249 polarssl_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000250 len = ret;
251 for( i = 0; i < len; i++ )
252 {
253 if( data[i] != '\n' )
254 {
255 if( idx < 4 )
256 code[ idx++ ] = data[i];
257 continue;
258 }
259
260 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
261 {
262 code[3] = '\0';
263 return atoi( code );
264 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200265
Paul Bakker1496d382011-05-23 12:07:29 +0000266 idx = 0;
267 }
268 }
269 while( 1 );
270}
271
Paul Bakker3c5ef712013-06-25 16:37:45 +0200272static int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000273{
274 int ret;
275 unsigned char data[128];
276 char code[4];
277 size_t i, idx = 0;
278
Rich Evansf90016a2015-01-19 14:26:37 +0000279 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000280 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
281 {
Rich Evansf90016a2015-01-19 14:26:37 +0000282 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000283 return -1;
284 }
285
286 do
287 {
288 len = sizeof( data ) - 1;
289 memset( data, 0, sizeof( data ) );
290 ret = read( sock_fd, data, len );
291
292 if( ret <= 0 )
293 {
Rich Evansf90016a2015-01-19 14:26:37 +0000294 polarssl_printf( "failed\n ! read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000295 return -1;
296 }
297
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200298 data[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +0000299 polarssl_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000300 len = ret;
301 for( i = 0; i < len; i++ )
302 {
303 if( data[i] != '\n' )
304 {
305 if( idx < 4 )
306 code[ idx++ ] = data[i];
307 continue;
308 }
309
310 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
311 {
312 code[3] = '\0';
313 return atoi( code );
314 }
315
316 idx = 0;
317 }
318 }
319 while( 1 );
320}
321
Paul Bakker5690efc2011-05-26 13:16:06 +0000322#if defined(POLARSSL_BASE64_C)
323#define USAGE_AUTH \
324 " authentication=%%d default: 0 (disabled)\n" \
325 " user_name=%%s default: \"user\"\n" \
326 " user_pwd=%%s default: \"password\"\n"
327#else
328#define USAGE_AUTH \
329 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
330#endif /* POLARSSL_BASE64_C */
331
332#if defined(POLARSSL_FS_IO)
333#define USAGE_IO \
334 " ca_file=%%s default: \"\" (pre-loaded)\n" \
335 " crt_file=%%s default: \"\" (pre-loaded)\n" \
336 " key_file=%%s default: \"\" (pre-loaded)\n"
337#else
338#define USAGE_IO \
339 " No file operations available (POLARSSL_FS_IO not defined)\n"
340#endif /* POLARSSL_FS_IO */
341
Paul Bakker1496d382011-05-23 12:07:29 +0000342#define USAGE \
343 "\n usage: ssl_mail_client param=<>...\n" \
344 "\n acceptable parameters:\n" \
345 " server_name=%%s default: localhost\n" \
346 " server_port=%%d default: 4433\n" \
347 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000348 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000349 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000350 " mail_from=%%s default: \"\"\n" \
351 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000352 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000353 " force_ciphersuite=<name> default: all enabled\n"\
354 " acceptable ciphersuite names:\n"
355
356int main( int argc, char *argv[] )
357{
358 int ret = 0, len, server_fd;
Paul Bakker09c9dd82014-08-18 11:06:56 +0200359 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000360#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000361 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000362#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000363 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200364 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000365
366 entropy_context entropy;
367 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000368 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200369 x509_crt cacert;
370 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200371 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 */
380 server_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200381 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker333fdec2014-08-04 12:12:09 +0200382 memset( &buf, 0, sizeof( buf ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200383 x509_crt_init( &cacert );
384 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200385 pk_init( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000386
387 if( argc == 0 )
388 {
389 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000390 polarssl_printf( USAGE );
Paul Bakker1496d382011-05-23 12:07:29 +0000391
392 list = ssl_list_ciphersuites();
393 while( *list )
394 {
Rich Evansf90016a2015-01-19 14:26:37 +0000395 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000396 list++;
397 }
Rich Evansf90016a2015-01-19 14:26:37 +0000398 polarssl_printf("\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000399 goto exit;
400 }
401
402 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;
Paul Bakker5690efc2011-05-26 13:16:06 +0000411 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000412 opt.crt_file = DFL_CRT_FILE;
413 opt.key_file = DFL_KEY_FILE;
414 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
415
416 for( i = 1; i < argc; i++ )
417 {
Paul Bakker1496d382011-05-23 12:07:29 +0000418 p = argv[i];
419 if( ( q = strchr( p, '=' ) ) == NULL )
420 goto usage;
421 *q++ = '\0';
422
423 if( strcmp( p, "server_name" ) == 0 )
424 opt.server_name = q;
425 else if( strcmp( p, "server_port" ) == 0 )
426 {
427 opt.server_port = atoi( q );
428 if( opt.server_port < 1 || opt.server_port > 65535 )
429 goto usage;
430 }
431 else if( strcmp( p, "debug_level" ) == 0 )
432 {
433 opt.debug_level = atoi( q );
434 if( opt.debug_level < 0 || opt.debug_level > 65535 )
435 goto usage;
436 }
437 else if( strcmp( p, "authentication" ) == 0 )
438 {
439 opt.authentication = atoi( q );
440 if( opt.authentication < 0 || opt.authentication > 1 )
441 goto usage;
442 }
443 else if( strcmp( p, "mode" ) == 0 )
444 {
445 opt.mode = atoi( q );
446 if( opt.mode < 0 || opt.mode > 1 )
447 goto usage;
448 }
449 else if( strcmp( p, "user_name" ) == 0 )
450 opt.user_name = q;
451 else if( strcmp( p, "user_pwd" ) == 0 )
452 opt.user_pwd = q;
453 else if( strcmp( p, "mail_from" ) == 0 )
454 opt.mail_from = q;
455 else if( strcmp( p, "mail_to" ) == 0 )
456 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000457 else if( strcmp( p, "ca_file" ) == 0 )
458 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000459 else if( strcmp( p, "crt_file" ) == 0 )
460 opt.crt_file = q;
461 else if( strcmp( p, "key_file" ) == 0 )
462 opt.key_file = q;
463 else if( strcmp( p, "force_ciphersuite" ) == 0 )
464 {
465 opt.force_ciphersuite[0] = -1;
466
467 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
468
469 if( opt.force_ciphersuite[0] <= 0 )
470 goto usage;
471
472 opt.force_ciphersuite[1] = 0;
473 }
474 else
475 goto usage;
476 }
477
478 /*
479 * 0. Initialize the RNG and the session data
480 */
Rich Evansf90016a2015-01-19 14:26:37 +0000481 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000482 fflush( stdout );
483
484 entropy_init( &entropy );
485 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200486 (const unsigned char *) pers,
487 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000488 {
Rich Evansf90016a2015-01-19 14:26:37 +0000489 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000490 goto exit;
491 }
492
Rich Evansf90016a2015-01-19 14:26:37 +0000493 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000494
495 /*
496 * 1.1. Load the trusted CA
497 */
Rich Evansf90016a2015-01-19 14:26:37 +0000498 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000499 fflush( stdout );
500
Paul Bakker5690efc2011-05-26 13:16:06 +0000501#if defined(POLARSSL_FS_IO)
502 if( strlen( opt.ca_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200503 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000504 else
505#endif
506#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200507 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
508 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000509#else
510 {
511 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000512 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000513 }
514#endif
Paul Bakker42488232012-05-16 08:21:05 +0000515 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000516 {
Rich Evansf90016a2015-01-19 14:26:37 +0000517 polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000518 goto exit;
519 }
520
Rich Evansf90016a2015-01-19 14:26:37 +0000521 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000522
523 /*
524 * 1.2. Load own certificate and private key
525 *
526 * (can be skipped if client authentication is not required)
527 */
Rich Evansf90016a2015-01-19 14:26:37 +0000528 polarssl_printf( " . Loading the client cert. and key..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000529 fflush( stdout );
530
Paul Bakker5690efc2011-05-26 13:16:06 +0000531#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000532 if( strlen( opt.crt_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200533 ret = x509_crt_parse_file( &clicert, opt.crt_file );
534 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000535#endif
536#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200537 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
538 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000539#else
540 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000541 ret = -1;
Rich Evansf90016a2015-01-19 14:26:37 +0000542 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000543 }
544#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000545 if( ret != 0 )
546 {
Rich Evansf90016a2015-01-19 14:26:37 +0000547 polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000548 goto exit;
549 }
550
Paul Bakker5690efc2011-05-26 13:16:06 +0000551#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000552 if( strlen( opt.key_file ) )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200553 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker1496d382011-05-23 12:07:29 +0000554 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000555#endif
556#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200557 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000558 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000559#else
560 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000561 ret = -1;
Rich Evansf90016a2015-01-19 14:26:37 +0000562 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000563 }
564#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000565 if( ret != 0 )
566 {
Rich Evansf90016a2015-01-19 14:26:37 +0000567 polarssl_printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000568 goto exit;
569 }
570
Rich Evansf90016a2015-01-19 14:26:37 +0000571 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000572
573 /*
574 * 2. Start the connection
575 */
Rich Evansf90016a2015-01-19 14:26:37 +0000576 polarssl_printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
Paul Bakker1496d382011-05-23 12:07:29 +0000577 opt.server_port );
578 fflush( stdout );
579
580 if( ( ret = net_connect( &server_fd, opt.server_name,
581 opt.server_port ) ) != 0 )
582 {
Rich Evansf90016a2015-01-19 14:26:37 +0000583 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000584 goto exit;
585 }
586
Rich Evansf90016a2015-01-19 14:26:37 +0000587 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000588
589 /*
590 * 3. Setup stuff
591 */
Rich Evansf90016a2015-01-19 14:26:37 +0000592 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000593 fflush( stdout );
594
Paul Bakker1496d382011-05-23 12:07:29 +0000595 if( ( ret = ssl_init( &ssl ) ) != 0 )
596 {
Rich Evansf90016a2015-01-19 14:26:37 +0000597 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000598 goto exit;
599 }
600
Rich Evansf90016a2015-01-19 14:26:37 +0000601 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000602
603 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100604 /* OPTIONAL is not optimal for security,
605 * but makes interop easier in this simplified example */
Paul Bakker1496d382011-05-23 12:07:29 +0000606 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
607
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100608 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
609 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100610 /* RC4 is deprecated, disable it */
611 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100612
Paul Bakker508ad5a2011-12-04 17:09:26 +0000613 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000614 ssl_set_dbg( &ssl, my_debug, stdout );
615 ssl_set_bio( &ssl, net_recv, &server_fd,
616 net_send, &server_fd );
617
Paul Bakker645ce3a2012-10-31 12:32:41 +0000618 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000619 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
620
Paul Bakker1496d382011-05-23 12:07:29 +0000621 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200622 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
623 {
Rich Evansf90016a2015-01-19 14:26:37 +0000624 polarssl_printf( " failed\n ! ssl_set_own_cert 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
Paul Bakker0be444a2013-08-27 21:55:01 +0200628#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200629 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
630 {
Rich Evansf90016a2015-01-19 14:26:37 +0000631 polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200632 goto exit;
633 }
Paul Bakker0be444a2013-08-27 21:55:01 +0200634#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000635
636 if( opt.mode == MODE_SSL_TLS )
637 {
638 if( do_handshake( &ssl, &opt ) != 0 )
639 goto exit;
640
Rich Evansf90016a2015-01-19 14:26:37 +0000641 polarssl_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000642 fflush( stdout );
643
644 ret = write_ssl_and_get_response( &ssl, buf, 0 );
645 if( ret < 200 || ret > 299 )
646 {
Rich Evansf90016a2015-01-19 14:26:37 +0000647 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000648 goto exit;
649 }
650
Rich Evansf90016a2015-01-19 14:26:37 +0000651 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000652
Rich Evansf90016a2015-01-19 14:26:37 +0000653 polarssl_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000654 fflush( stdout );
655
656 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100657 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000658 ret = write_ssl_and_get_response( &ssl, buf, len );
659 if( ret < 200 || ret > 299 )
660 {
Rich Evansf90016a2015-01-19 14:26:37 +0000661 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000662 goto exit;
663 }
664 }
665 else
666 {
Rich Evansf90016a2015-01-19 14:26:37 +0000667 polarssl_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000668 fflush( stdout );
669
670 ret = write_and_get_response( server_fd, buf, 0 );
671 if( ret < 200 || ret > 299 )
672 {
Rich Evansf90016a2015-01-19 14:26:37 +0000673 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000674 goto exit;
675 }
676
Rich Evansf90016a2015-01-19 14:26:37 +0000677 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000678
Rich Evansf90016a2015-01-19 14:26:37 +0000679 polarssl_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000680 fflush( stdout );
681
682 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100683 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000684 ret = write_and_get_response( server_fd, buf, len );
685 if( ret < 200 || ret > 299 )
686 {
Rich Evansf90016a2015-01-19 14:26:37 +0000687 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000688 goto exit;
689 }
690
Rich Evansf90016a2015-01-19 14:26:37 +0000691 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000692
Rich Evansf90016a2015-01-19 14:26:37 +0000693 polarssl_printf( " > Write STARTTLS to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000694 fflush( stdout );
695
696 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100697 len = sprintf( (char *) buf, "STARTTLS\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000698 ret = write_and_get_response( server_fd, buf, len );
699 if( ret < 200 || ret > 299 )
700 {
Rich Evansf90016a2015-01-19 14:26:37 +0000701 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000702 goto exit;
703 }
704
Rich Evansf90016a2015-01-19 14:26:37 +0000705 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000706
707 if( do_handshake( &ssl, &opt ) != 0 )
708 goto exit;
709 }
710
Paul Bakker5690efc2011-05-26 13:16:06 +0000711#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000712 if( opt.authentication )
713 {
Rich Evansf90016a2015-01-19 14:26:37 +0000714 polarssl_printf( " > Write AUTH LOGIN to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000715 fflush( stdout );
716
Paul Bakkerd75ba402014-01-24 16:11:17 +0100717 len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000718 ret = write_ssl_and_get_response( &ssl, buf, len );
719 if( ret < 200 || ret > 399 )
720 {
Rich Evansf90016a2015-01-19 14:26:37 +0000721 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000722 goto exit;
723 }
724
Rich Evansf90016a2015-01-19 14:26:37 +0000725 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000726
Rich Evansf90016a2015-01-19 14:26:37 +0000727 polarssl_printf( " > Write username to server: %s", opt.user_name );
Paul Bakker1496d382011-05-23 12:07:29 +0000728 fflush( stdout );
729
730 n = sizeof( buf );
Alfred Klomp7c034242014-07-14 22:11:13 +0200731 ret = base64_encode( base, &n, (const unsigned char *) opt.user_name,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200732 strlen( opt.user_name ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200733
734 if( ret != 0 ) {
Rich Evansf90016a2015-01-19 14:26:37 +0000735 polarssl_printf( " failed\n ! base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200736 goto exit;
737 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100738 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000739 ret = write_ssl_and_get_response( &ssl, buf, len );
740 if( ret < 300 || ret > 399 )
741 {
Rich Evansf90016a2015-01-19 14:26:37 +0000742 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000743 goto exit;
744 }
745
Rich Evansf90016a2015-01-19 14:26:37 +0000746 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000747
Rich Evansf90016a2015-01-19 14:26:37 +0000748 polarssl_printf( " > Write password to server: %s", opt.user_pwd );
Paul Bakker1496d382011-05-23 12:07:29 +0000749 fflush( stdout );
750
Alfred Klomp7c034242014-07-14 22:11:13 +0200751 ret = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200752 strlen( opt.user_pwd ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200753
754 if( ret != 0 ) {
Rich Evansf90016a2015-01-19 14:26:37 +0000755 polarssl_printf( " failed\n ! base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200756 goto exit;
757 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100758 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000759 ret = write_ssl_and_get_response( &ssl, buf, len );
760 if( ret < 200 || ret > 399 )
761 {
Rich Evansf90016a2015-01-19 14:26:37 +0000762 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000763 goto exit;
764 }
765
Rich Evansf90016a2015-01-19 14:26:37 +0000766 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000767 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000768#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000769
Rich Evansf90016a2015-01-19 14:26:37 +0000770 polarssl_printf( " > Write MAIL FROM to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000771 fflush( stdout );
772
Paul Bakkerd75ba402014-01-24 16:11:17 +0100773 len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
Paul Bakker1496d382011-05-23 12:07:29 +0000774 ret = write_ssl_and_get_response( &ssl, buf, len );
775 if( ret < 200 || ret > 299 )
776 {
Rich Evansf90016a2015-01-19 14:26:37 +0000777 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000778 goto exit;
779 }
780
Rich Evansf90016a2015-01-19 14:26:37 +0000781 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000782
Rich Evansf90016a2015-01-19 14:26:37 +0000783 polarssl_printf( " > Write RCPT TO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000784 fflush( stdout );
785
Paul Bakkerd75ba402014-01-24 16:11:17 +0100786 len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
Paul Bakker1496d382011-05-23 12:07:29 +0000787 ret = write_ssl_and_get_response( &ssl, buf, len );
788 if( ret < 200 || ret > 299 )
789 {
Rich Evansf90016a2015-01-19 14:26:37 +0000790 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000791 goto exit;
792 }
793
Rich Evansf90016a2015-01-19 14:26:37 +0000794 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000795
Rich Evansf90016a2015-01-19 14:26:37 +0000796 polarssl_printf( " > Write DATA to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000797 fflush( stdout );
798
Paul Bakkerd75ba402014-01-24 16:11:17 +0100799 len = sprintf( (char *) buf, "DATA\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000800 ret = write_ssl_and_get_response( &ssl, buf, len );
801 if( ret < 300 || ret > 399 )
802 {
Rich Evansf90016a2015-01-19 14:26:37 +0000803 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000804 goto exit;
805 }
806
Rich Evansf90016a2015-01-19 14:26:37 +0000807 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000808
Rich Evansf90016a2015-01-19 14:26:37 +0000809 polarssl_printf( " > Write content to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000810 fflush( stdout );
811
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000812 len = sprintf( (char *) buf, "From: %s\r\nSubject: mbed TLS Test mail\r\n\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000813 "This is a simple test mail from the "
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000814 "mbed TLS mail client example.\r\n"
Paul Bakkerd75ba402014-01-24 16:11:17 +0100815 "\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000816 "Enjoy!", opt.mail_from );
817 ret = write_ssl_data( &ssl, buf, len );
818
819 len = sprintf( (char *) buf, "\r\n.\r\n");
820 ret = write_ssl_and_get_response( &ssl, buf, len );
821 if( ret < 200 || ret > 299 )
822 {
Rich Evansf90016a2015-01-19 14:26:37 +0000823 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000824 goto exit;
825 }
826
Rich Evansf90016a2015-01-19 14:26:37 +0000827 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000828
829 ssl_close_notify( &ssl );
830
831exit:
832
833 if( server_fd )
834 net_close( server_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200835 x509_crt_free( &clicert );
836 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200837 pk_free( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000838 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200839 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200840 entropy_free( &entropy );
Paul Bakker1496d382011-05-23 12:07:29 +0000841
Paul Bakkercce9d772011-11-18 14:26:47 +0000842#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000843 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000844 fflush( stdout ); getchar();
845#endif
846
847 return( ret );
848}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000849#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
850 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
851 POLARSSL_CTR_DRBG_C */