blob: 00d12ca96e256f516ef80dc3f62d04340b25702a [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
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker1496d382011-05-23 12:07:29 +000038#include <string.h>
39#include <stdlib.h>
40#include <stdio.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010041
42#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
Paul Bakker1496d382011-05-23 12:07:29 +000043#include <unistd.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010044#else
45#include <io.h>
46#define read _read
47#define write _write
48#endif
Paul Bakker1496d382011-05-23 12:07:29 +000049
Paul Bakker5a835222011-10-12 09:19:31 +000050#if defined(_WIN32) || defined(_WIN32_WCE)
51
52#include <winsock2.h>
53#include <windows.h>
54
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010055#if defined(_MSC_VER)
Paul Bakker5a835222011-10-12 09:19:31 +000056#if defined(_WIN32_WCE)
57#pragma comment( lib, "ws2.lib" )
58#else
59#pragma comment( lib, "ws2_32.lib" )
60#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010061#endif /* _MSC_VER */
Paul Bakker5a835222011-10-12 09:19:31 +000062#endif
63
Paul Bakker1496d382011-05-23 12:07:29 +000064#include "polarssl/base64.h"
65#include "polarssl/error.h"
66#include "polarssl/net.h"
67#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000068#include "polarssl/entropy.h"
69#include "polarssl/ctr_drbg.h"
Paul Bakker1496d382011-05-23 12:07:29 +000070#include "polarssl/certs.h"
71#include "polarssl/x509.h"
72
Paul Bakker9a97c5d2013-09-15 17:07:33 +020073#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
74 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
75 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020076 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020077int main( int argc, char *argv[] )
78{
79 ((void) argc);
80 ((void) argv);
81
Rich Evansf90016a2015-01-19 14:26:37 +000082 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker9a97c5d2013-09-15 17:07:33 +020083 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
84 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020085 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
Paul Bakker9a97c5d2013-09-15 17:07:33 +020086 "not defined.\n");
87 return( 0 );
88}
89#else
90
Paul Bakker1496d382011-05-23 12:07:29 +000091#define DFL_SERVER_NAME "localhost"
92#define DFL_SERVER_PORT 465
93#define DFL_USER_NAME "user"
94#define DFL_USER_PWD "password"
95#define DFL_MAIL_FROM ""
96#define DFL_MAIL_TO ""
97#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000098#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000099#define DFL_CRT_FILE ""
100#define DFL_KEY_FILE ""
101#define DFL_FORCE_CIPHER 0
102#define DFL_MODE 0
103#define DFL_AUTHENTICATION 0
104
105#define MODE_SSL_TLS 0
106#define MODE_STARTTLS 0
107
108/*
109 * global options
110 */
111struct options
112{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200113 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +0000114 int server_port; /* port on which the ssl service runs */
115 int debug_level; /* level of debugging */
116 int authentication; /* if authentication is required */
117 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200118 const char *user_name; /* username to use for authentication */
119 const char *user_pwd; /* password to use for authentication */
120 const char *mail_from; /* E-Mail address to use as sender */
121 const char *mail_to; /* E-Mail address to use as recipient */
122 const char *ca_file; /* the file with the CA certificate(s) */
123 const char *crt_file; /* the file with the client certificate */
124 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000125 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
126} opt;
127
Paul Bakker3c5ef712013-06-25 16:37:45 +0200128static void my_debug( void *ctx, int level, const char *str )
Paul Bakker1496d382011-05-23 12:07:29 +0000129{
130 if( level < opt.debug_level )
131 {
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakker1496d382011-05-23 12:07:29 +0000133 fflush( (FILE *) ctx );
134 }
135}
136
Paul Bakker3c5ef712013-06-25 16:37:45 +0200137static int do_handshake( ssl_context *ssl, struct options *opt )
Paul Bakker1496d382011-05-23 12:07:29 +0000138{
139 int ret;
140 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000141 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000142
143 /*
144 * 4. Handshake
145 */
Rich Evansf90016a2015-01-19 14:26:37 +0000146 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000147 fflush( stdout );
148
149 while( ( ret = ssl_handshake( ssl ) ) != 0 )
150 {
151 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
152 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000153#if defined(POLARSSL_ERROR_C)
Paul Bakker03a8a792013-06-30 12:18:08 +0200154 polarssl_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000155#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000156 polarssl_printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000157 return( -1 );
158 }
159 }
160
Rich Evansf90016a2015-01-19 14:26:37 +0000161 polarssl_printf( " ok\n [ Ciphersuite is %s ]\n",
Paul Bakker1496d382011-05-23 12:07:29 +0000162 ssl_get_ciphersuite( ssl ) );
163
164 /*
165 * 5. Verify the server certificate
166 */
Rich Evansf90016a2015-01-19 14:26:37 +0000167 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000168
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100169 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker1496d382011-05-23 12:07:29 +0000170 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
171 {
Rich Evansf90016a2015-01-19 14:26:37 +0000172 polarssl_printf( " failed\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000173
174 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000175 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000176
177 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000178 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000179
180 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000181 polarssl_printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
Paul Bakker1496d382011-05-23 12:07:29 +0000182
183 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000184 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000185
Rich Evansf90016a2015-01-19 14:26:37 +0000186 polarssl_printf( "\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000187 }
188 else
Rich Evansf90016a2015-01-19 14:26:37 +0000189 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000190
Rich Evansf90016a2015-01-19 14:26:37 +0000191 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200192 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
193 ssl_get_peer_cert( ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +0000194 polarssl_printf( "%s\n", buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000195
196 return( 0 );
197}
198
Paul Bakker3c5ef712013-06-25 16:37:45 +0200199static int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000200{
201 int ret;
202
Rich Evansf90016a2015-01-19 14:26:37 +0000203 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000204 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
205 {
206 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
207 {
Rich Evansf90016a2015-01-19 14:26:37 +0000208 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000209 return -1;
210 }
211 }
212
213 return( 0 );
214}
215
Paul Bakker3c5ef712013-06-25 16:37:45 +0200216static int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000217{
218 int ret;
219 unsigned char data[128];
220 char code[4];
221 size_t i, idx = 0;
222
Rich Evansf90016a2015-01-19 14:26:37 +0000223 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000224 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
225 {
226 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
227 {
Rich Evansf90016a2015-01-19 14:26:37 +0000228 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000229 return -1;
230 }
231 }
232
233 do
234 {
235 len = sizeof( data ) - 1;
236 memset( data, 0, sizeof( data ) );
237 ret = ssl_read( ssl, data, len );
238
239 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
240 continue;
241
242 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
243 return -1;
244
245 if( ret <= 0 )
246 {
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_printf( "failed\n ! ssl_read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000248 return -1;
249 }
250
Rich Evansf90016a2015-01-19 14:26:37 +0000251 polarssl_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000252 len = ret;
253 for( i = 0; i < len; i++ )
254 {
255 if( data[i] != '\n' )
256 {
257 if( idx < 4 )
258 code[ idx++ ] = data[i];
259 continue;
260 }
261
262 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
263 {
264 code[3] = '\0';
265 return atoi( code );
266 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200267
Paul Bakker1496d382011-05-23 12:07:29 +0000268 idx = 0;
269 }
270 }
271 while( 1 );
272}
273
Paul Bakker3c5ef712013-06-25 16:37:45 +0200274static int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000275{
276 int ret;
277 unsigned char data[128];
278 char code[4];
279 size_t i, idx = 0;
280
Rich Evansf90016a2015-01-19 14:26:37 +0000281 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000282 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
283 {
Rich Evansf90016a2015-01-19 14:26:37 +0000284 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000285 return -1;
286 }
287
288 do
289 {
290 len = sizeof( data ) - 1;
291 memset( data, 0, sizeof( data ) );
292 ret = read( sock_fd, data, len );
293
294 if( ret <= 0 )
295 {
Rich Evansf90016a2015-01-19 14:26:37 +0000296 polarssl_printf( "failed\n ! read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000297 return -1;
298 }
299
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200300 data[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +0000301 polarssl_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000302 len = ret;
303 for( i = 0; i < len; i++ )
304 {
305 if( data[i] != '\n' )
306 {
307 if( idx < 4 )
308 code[ idx++ ] = data[i];
309 continue;
310 }
311
312 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
313 {
314 code[3] = '\0';
315 return atoi( code );
316 }
317
318 idx = 0;
319 }
320 }
321 while( 1 );
322}
323
Paul Bakker5690efc2011-05-26 13:16:06 +0000324#if defined(POLARSSL_BASE64_C)
325#define USAGE_AUTH \
326 " authentication=%%d default: 0 (disabled)\n" \
327 " user_name=%%s default: \"user\"\n" \
328 " user_pwd=%%s default: \"password\"\n"
329#else
330#define USAGE_AUTH \
331 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
332#endif /* POLARSSL_BASE64_C */
333
334#if defined(POLARSSL_FS_IO)
335#define USAGE_IO \
336 " ca_file=%%s default: \"\" (pre-loaded)\n" \
337 " crt_file=%%s default: \"\" (pre-loaded)\n" \
338 " key_file=%%s default: \"\" (pre-loaded)\n"
339#else
340#define USAGE_IO \
341 " No file operations available (POLARSSL_FS_IO not defined)\n"
342#endif /* POLARSSL_FS_IO */
343
Paul Bakker1496d382011-05-23 12:07:29 +0000344#define USAGE \
345 "\n usage: ssl_mail_client param=<>...\n" \
346 "\n acceptable parameters:\n" \
347 " server_name=%%s default: localhost\n" \
348 " server_port=%%d default: 4433\n" \
349 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000350 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000351 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000352 " mail_from=%%s default: \"\"\n" \
353 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000354 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000355 " force_ciphersuite=<name> default: all enabled\n"\
356 " acceptable ciphersuite names:\n"
357
358int main( int argc, char *argv[] )
359{
360 int ret = 0, len, server_fd;
Paul Bakker09c9dd82014-08-18 11:06:56 +0200361 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000362#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000363 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000364#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000365 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200366 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000367
368 entropy_context entropy;
369 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000370 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200371 x509_crt cacert;
372 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200373 pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000374 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000375 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000376 char *p, *q;
377 const int *list;
378
379 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200380 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000381 */
382 server_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200383 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker333fdec2014-08-04 12:12:09 +0200384 memset( &buf, 0, sizeof( buf ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200385 x509_crt_init( &cacert );
386 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200387 pk_init( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000388
389 if( argc == 0 )
390 {
391 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000392 polarssl_printf( USAGE );
Paul Bakker1496d382011-05-23 12:07:29 +0000393
394 list = ssl_list_ciphersuites();
395 while( *list )
396 {
Rich Evansf90016a2015-01-19 14:26:37 +0000397 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000398 list++;
399 }
Rich Evansf90016a2015-01-19 14:26:37 +0000400 polarssl_printf("\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000401 goto exit;
402 }
403
404 opt.server_name = DFL_SERVER_NAME;
405 opt.server_port = DFL_SERVER_PORT;
406 opt.debug_level = DFL_DEBUG_LEVEL;
407 opt.authentication = DFL_AUTHENTICATION;
408 opt.mode = DFL_MODE;
409 opt.user_name = DFL_USER_NAME;
410 opt.user_pwd = DFL_USER_PWD;
411 opt.mail_from = DFL_MAIL_FROM;
412 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000413 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000414 opt.crt_file = DFL_CRT_FILE;
415 opt.key_file = DFL_KEY_FILE;
416 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
417
418 for( i = 1; i < argc; i++ )
419 {
Paul Bakker1496d382011-05-23 12:07:29 +0000420 p = argv[i];
421 if( ( q = strchr( p, '=' ) ) == NULL )
422 goto usage;
423 *q++ = '\0';
424
425 if( strcmp( p, "server_name" ) == 0 )
426 opt.server_name = q;
427 else if( strcmp( p, "server_port" ) == 0 )
428 {
429 opt.server_port = atoi( q );
430 if( opt.server_port < 1 || opt.server_port > 65535 )
431 goto usage;
432 }
433 else if( strcmp( p, "debug_level" ) == 0 )
434 {
435 opt.debug_level = atoi( q );
436 if( opt.debug_level < 0 || opt.debug_level > 65535 )
437 goto usage;
438 }
439 else if( strcmp( p, "authentication" ) == 0 )
440 {
441 opt.authentication = atoi( q );
442 if( opt.authentication < 0 || opt.authentication > 1 )
443 goto usage;
444 }
445 else if( strcmp( p, "mode" ) == 0 )
446 {
447 opt.mode = atoi( q );
448 if( opt.mode < 0 || opt.mode > 1 )
449 goto usage;
450 }
451 else if( strcmp( p, "user_name" ) == 0 )
452 opt.user_name = q;
453 else if( strcmp( p, "user_pwd" ) == 0 )
454 opt.user_pwd = q;
455 else if( strcmp( p, "mail_from" ) == 0 )
456 opt.mail_from = q;
457 else if( strcmp( p, "mail_to" ) == 0 )
458 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000459 else if( strcmp( p, "ca_file" ) == 0 )
460 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000461 else if( strcmp( p, "crt_file" ) == 0 )
462 opt.crt_file = q;
463 else if( strcmp( p, "key_file" ) == 0 )
464 opt.key_file = q;
465 else if( strcmp( p, "force_ciphersuite" ) == 0 )
466 {
467 opt.force_ciphersuite[0] = -1;
468
469 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
470
471 if( opt.force_ciphersuite[0] <= 0 )
472 goto usage;
473
474 opt.force_ciphersuite[1] = 0;
475 }
476 else
477 goto usage;
478 }
479
480 /*
481 * 0. Initialize the RNG and the session data
482 */
Rich Evansf90016a2015-01-19 14:26:37 +0000483 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000484 fflush( stdout );
485
486 entropy_init( &entropy );
487 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200488 (const unsigned char *) pers,
489 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000490 {
Rich Evansf90016a2015-01-19 14:26:37 +0000491 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000492 goto exit;
493 }
494
Rich Evansf90016a2015-01-19 14:26:37 +0000495 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000496
497 /*
498 * 1.1. Load the trusted CA
499 */
Rich Evansf90016a2015-01-19 14:26:37 +0000500 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000501 fflush( stdout );
502
Paul Bakker5690efc2011-05-26 13:16:06 +0000503#if defined(POLARSSL_FS_IO)
504 if( strlen( opt.ca_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200505 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000506 else
507#endif
508#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200509 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
510 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000511#else
512 {
513 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000514 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000515 }
516#endif
Paul Bakker42488232012-05-16 08:21:05 +0000517 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000518 {
Rich Evansf90016a2015-01-19 14:26:37 +0000519 polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000520 goto exit;
521 }
522
Rich Evansf90016a2015-01-19 14:26:37 +0000523 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000524
525 /*
526 * 1.2. Load own certificate and private key
527 *
528 * (can be skipped if client authentication is not required)
529 */
Rich Evansf90016a2015-01-19 14:26:37 +0000530 polarssl_printf( " . Loading the client cert. and key..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000531 fflush( stdout );
532
Paul Bakker5690efc2011-05-26 13:16:06 +0000533#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000534 if( strlen( opt.crt_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200535 ret = x509_crt_parse_file( &clicert, opt.crt_file );
536 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000537#endif
538#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200539 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
540 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000541#else
542 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000543 ret = -1;
Rich Evansf90016a2015-01-19 14:26:37 +0000544 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000545 }
546#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000547 if( ret != 0 )
548 {
Rich Evansf90016a2015-01-19 14:26:37 +0000549 polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000550 goto exit;
551 }
552
Paul Bakker5690efc2011-05-26 13:16:06 +0000553#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000554 if( strlen( opt.key_file ) )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200555 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker1496d382011-05-23 12:07:29 +0000556 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000557#endif
558#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200559 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000560 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000561#else
562 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000563 ret = -1;
Rich Evansf90016a2015-01-19 14:26:37 +0000564 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000565 }
566#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000567 if( ret != 0 )
568 {
Rich Evansf90016a2015-01-19 14:26:37 +0000569 polarssl_printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000570 goto exit;
571 }
572
Rich Evansf90016a2015-01-19 14:26:37 +0000573 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000574
575 /*
576 * 2. Start the connection
577 */
Rich Evansf90016a2015-01-19 14:26:37 +0000578 polarssl_printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
Paul Bakker1496d382011-05-23 12:07:29 +0000579 opt.server_port );
580 fflush( stdout );
581
582 if( ( ret = net_connect( &server_fd, opt.server_name,
583 opt.server_port ) ) != 0 )
584 {
Rich Evansf90016a2015-01-19 14:26:37 +0000585 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000586 goto exit;
587 }
588
Rich Evansf90016a2015-01-19 14:26:37 +0000589 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000590
591 /*
592 * 3. Setup stuff
593 */
Rich Evansf90016a2015-01-19 14:26:37 +0000594 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000595 fflush( stdout );
596
Paul Bakker1496d382011-05-23 12:07:29 +0000597 if( ( ret = ssl_init( &ssl ) ) != 0 )
598 {
Rich Evansf90016a2015-01-19 14:26:37 +0000599 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000600 goto exit;
601 }
602
Rich Evansf90016a2015-01-19 14:26:37 +0000603 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000604
605 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100606 /* OPTIONAL is not optimal for security,
607 * but makes interop easier in this simplified example */
Paul Bakker1496d382011-05-23 12:07:29 +0000608 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
609
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100610 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
611 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100612 /* RC4 is deprecated, disable it */
613 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100614
Paul Bakker508ad5a2011-12-04 17:09:26 +0000615 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000616 ssl_set_dbg( &ssl, my_debug, stdout );
617 ssl_set_bio( &ssl, net_recv, &server_fd,
618 net_send, &server_fd );
619
Paul Bakker645ce3a2012-10-31 12:32:41 +0000620 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000621 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
622
Paul Bakker1496d382011-05-23 12:07:29 +0000623 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200624 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
625 {
Rich Evansf90016a2015-01-19 14:26:37 +0000626 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200627 goto exit;
628 }
Paul Bakker1496d382011-05-23 12:07:29 +0000629
Paul Bakker0be444a2013-08-27 21:55:01 +0200630#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200631 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
632 {
Rich Evansf90016a2015-01-19 14:26:37 +0000633 polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200634 goto exit;
635 }
Paul Bakker0be444a2013-08-27 21:55:01 +0200636#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000637
638 if( opt.mode == MODE_SSL_TLS )
639 {
640 if( do_handshake( &ssl, &opt ) != 0 )
641 goto exit;
642
Rich Evansf90016a2015-01-19 14:26:37 +0000643 polarssl_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000644 fflush( stdout );
645
646 ret = write_ssl_and_get_response( &ssl, buf, 0 );
647 if( ret < 200 || ret > 299 )
648 {
Rich Evansf90016a2015-01-19 14:26:37 +0000649 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000650 goto exit;
651 }
652
Rich Evansf90016a2015-01-19 14:26:37 +0000653 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000654
Rich Evansf90016a2015-01-19 14:26:37 +0000655 polarssl_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000656 fflush( stdout );
657
658 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100659 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000660 ret = write_ssl_and_get_response( &ssl, buf, len );
661 if( ret < 200 || ret > 299 )
662 {
Rich Evansf90016a2015-01-19 14:26:37 +0000663 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000664 goto exit;
665 }
666 }
667 else
668 {
Rich Evansf90016a2015-01-19 14:26:37 +0000669 polarssl_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000670 fflush( stdout );
671
672 ret = write_and_get_response( server_fd, buf, 0 );
673 if( ret < 200 || ret > 299 )
674 {
Rich Evansf90016a2015-01-19 14:26:37 +0000675 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000676 goto exit;
677 }
678
Rich Evansf90016a2015-01-19 14:26:37 +0000679 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000680
Rich Evansf90016a2015-01-19 14:26:37 +0000681 polarssl_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000682 fflush( stdout );
683
684 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100685 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000686 ret = write_and_get_response( server_fd, buf, len );
687 if( ret < 200 || ret > 299 )
688 {
Rich Evansf90016a2015-01-19 14:26:37 +0000689 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000690 goto exit;
691 }
692
Rich Evansf90016a2015-01-19 14:26:37 +0000693 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000694
Rich Evansf90016a2015-01-19 14:26:37 +0000695 polarssl_printf( " > Write STARTTLS to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000696 fflush( stdout );
697
698 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100699 len = sprintf( (char *) buf, "STARTTLS\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000700 ret = write_and_get_response( server_fd, buf, len );
701 if( ret < 200 || ret > 299 )
702 {
Rich Evansf90016a2015-01-19 14:26:37 +0000703 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000704 goto exit;
705 }
706
Rich Evansf90016a2015-01-19 14:26:37 +0000707 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000708
709 if( do_handshake( &ssl, &opt ) != 0 )
710 goto exit;
711 }
712
Paul Bakker5690efc2011-05-26 13:16:06 +0000713#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000714 if( opt.authentication )
715 {
Rich Evansf90016a2015-01-19 14:26:37 +0000716 polarssl_printf( " > Write AUTH LOGIN to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000717 fflush( stdout );
718
Paul Bakkerd75ba402014-01-24 16:11:17 +0100719 len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000720 ret = write_ssl_and_get_response( &ssl, buf, len );
721 if( ret < 200 || ret > 399 )
722 {
Rich Evansf90016a2015-01-19 14:26:37 +0000723 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000724 goto exit;
725 }
726
Rich Evansf90016a2015-01-19 14:26:37 +0000727 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000728
Rich Evansf90016a2015-01-19 14:26:37 +0000729 polarssl_printf( " > Write username to server: %s", opt.user_name );
Paul Bakker1496d382011-05-23 12:07:29 +0000730 fflush( stdout );
731
732 n = sizeof( buf );
Alfred Klomp7c034242014-07-14 22:11:13 +0200733 ret = base64_encode( base, &n, (const unsigned char *) opt.user_name,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200734 strlen( opt.user_name ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200735
736 if( ret != 0 ) {
Rich Evansf90016a2015-01-19 14:26:37 +0000737 polarssl_printf( " failed\n ! base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200738 goto exit;
739 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100740 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000741 ret = write_ssl_and_get_response( &ssl, buf, len );
742 if( ret < 300 || ret > 399 )
743 {
Rich Evansf90016a2015-01-19 14:26:37 +0000744 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000745 goto exit;
746 }
747
Rich Evansf90016a2015-01-19 14:26:37 +0000748 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000749
Rich Evansf90016a2015-01-19 14:26:37 +0000750 polarssl_printf( " > Write password to server: %s", opt.user_pwd );
Paul Bakker1496d382011-05-23 12:07:29 +0000751 fflush( stdout );
752
Alfred Klomp7c034242014-07-14 22:11:13 +0200753 ret = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200754 strlen( opt.user_pwd ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200755
756 if( ret != 0 ) {
Rich Evansf90016a2015-01-19 14:26:37 +0000757 polarssl_printf( " failed\n ! base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200758 goto exit;
759 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100760 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000761 ret = write_ssl_and_get_response( &ssl, buf, len );
762 if( ret < 200 || ret > 399 )
763 {
Rich Evansf90016a2015-01-19 14:26:37 +0000764 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000765 goto exit;
766 }
767
Rich Evansf90016a2015-01-19 14:26:37 +0000768 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000769 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000770#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000771
Rich Evansf90016a2015-01-19 14:26:37 +0000772 polarssl_printf( " > Write MAIL FROM to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000773 fflush( stdout );
774
Paul Bakkerd75ba402014-01-24 16:11:17 +0100775 len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
Paul Bakker1496d382011-05-23 12:07:29 +0000776 ret = write_ssl_and_get_response( &ssl, buf, len );
777 if( ret < 200 || ret > 299 )
778 {
Rich Evansf90016a2015-01-19 14:26:37 +0000779 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000780 goto exit;
781 }
782
Rich Evansf90016a2015-01-19 14:26:37 +0000783 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000784
Rich Evansf90016a2015-01-19 14:26:37 +0000785 polarssl_printf( " > Write RCPT TO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000786 fflush( stdout );
787
Paul Bakkerd75ba402014-01-24 16:11:17 +0100788 len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
Paul Bakker1496d382011-05-23 12:07:29 +0000789 ret = write_ssl_and_get_response( &ssl, buf, len );
790 if( ret < 200 || ret > 299 )
791 {
Rich Evansf90016a2015-01-19 14:26:37 +0000792 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000793 goto exit;
794 }
795
Rich Evansf90016a2015-01-19 14:26:37 +0000796 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000797
Rich Evansf90016a2015-01-19 14:26:37 +0000798 polarssl_printf( " > Write DATA 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, "DATA\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000802 ret = write_ssl_and_get_response( &ssl, buf, len );
803 if( ret < 300 || ret > 399 )
804 {
Rich Evansf90016a2015-01-19 14:26:37 +0000805 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000806 goto exit;
807 }
808
Rich Evansf90016a2015-01-19 14:26:37 +0000809 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000810
Rich Evansf90016a2015-01-19 14:26:37 +0000811 polarssl_printf( " > Write content to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000812 fflush( stdout );
813
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000814 len = sprintf( (char *) buf, "From: %s\r\nSubject: mbed TLS Test mail\r\n\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000815 "This is a simple test mail from the "
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000816 "mbed TLS mail client example.\r\n"
Paul Bakkerd75ba402014-01-24 16:11:17 +0100817 "\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000818 "Enjoy!", opt.mail_from );
819 ret = write_ssl_data( &ssl, buf, len );
820
821 len = sprintf( (char *) buf, "\r\n.\r\n");
822 ret = write_ssl_and_get_response( &ssl, buf, len );
823 if( ret < 200 || ret > 299 )
824 {
Rich Evansf90016a2015-01-19 14:26:37 +0000825 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000826 goto exit;
827 }
828
Rich Evansf90016a2015-01-19 14:26:37 +0000829 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000830
831 ssl_close_notify( &ssl );
832
833exit:
834
835 if( server_fd )
836 net_close( server_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200837 x509_crt_free( &clicert );
838 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200839 pk_free( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000840 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200841 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200842 entropy_free( &entropy );
Paul Bakker1496d382011-05-23 12:07:29 +0000843
Paul Bakkercce9d772011-11-18 14:26:47 +0000844#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000845 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000846 fflush( stdout ); getchar();
847#endif
848
849 return( ret );
850}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000851#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
852 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
853 POLARSSL_CTR_DRBG_C */