blob: 27c57a17f16ffedcb666f7d0ede0c283f42879a2 [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Rich Evans18b78c72015-02-11 14:06:19 +000037#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_ENTROPY_C) && \
38 defined(POLARSSL_SSL_TLS_C) && defined(POLARSSL_SSL_CLI_C) && \
39 defined(POLARSSL_NET_C) && defined(POLARSSL_RSA_C) && \
40 defined(POLARSSL_CTR_DRBG_C) && defined(POLARSSL_X509_CRT_PARSE_C) && \
41 defined(POLARSSL_FS_IO)
42#include "polarssl/base64.h"
43#include "polarssl/error.h"
44#include "polarssl/net.h"
45#include "polarssl/ssl.h"
46#include "polarssl/entropy.h"
47#include "polarssl/ctr_drbg.h"
48#include "polarssl/certs.h"
49#include "polarssl/x509.h"
50
Paul Bakker1496d382011-05-23 12:07:29 +000051#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000052#include <stdlib.h>
53#include <string.h>
54#endif
Paul Bakkerfdda7852013-11-30 15:15:31 +010055
56#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
Paul Bakker1496d382011-05-23 12:07:29 +000057#include <unistd.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010058#else
59#include <io.h>
60#define read _read
61#define write _write
62#endif
Paul Bakker1496d382011-05-23 12:07:29 +000063
Paul Bakker5a835222011-10-12 09:19:31 +000064#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5a835222011-10-12 09:19:31 +000065#include <winsock2.h>
66#include <windows.h>
67
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010068#if defined(_MSC_VER)
Paul Bakker5a835222011-10-12 09:19:31 +000069#if defined(_WIN32_WCE)
70#pragma comment( lib, "ws2.lib" )
71#else
72#pragma comment( lib, "ws2_32.lib" )
73#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010074#endif /* _MSC_VER */
Paul Bakker5a835222011-10-12 09:19:31 +000075#endif
76
Paul Bakker1496d382011-05-23 12:07:29 +000077#define DFL_SERVER_NAME "localhost"
78#define DFL_SERVER_PORT 465
79#define DFL_USER_NAME "user"
80#define DFL_USER_PWD "password"
81#define DFL_MAIL_FROM ""
82#define DFL_MAIL_TO ""
83#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000084#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000085#define DFL_CRT_FILE ""
86#define DFL_KEY_FILE ""
87#define DFL_FORCE_CIPHER 0
88#define DFL_MODE 0
89#define DFL_AUTHENTICATION 0
90
91#define MODE_SSL_TLS 0
92#define MODE_STARTTLS 0
93
Rich Evans85b05ec2015-02-12 11:37:29 +000094#if defined(POLARSSL_BASE64_C)
95#define USAGE_AUTH \
96 " authentication=%%d default: 0 (disabled)\n" \
97 " user_name=%%s default: \"user\"\n" \
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000098 " user_pwd=%%s default: \"password\"\n"
Rich Evans85b05ec2015-02-12 11:37:29 +000099#else
100#define USAGE_AUTH \
101 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
102#endif /* POLARSSL_BASE64_C */
103
104#if defined(POLARSSL_FS_IO)
105#define USAGE_IO \
106 " ca_file=%%s default: \"\" (pre-loaded)\n" \
107 " crt_file=%%s default: \"\" (pre-loaded)\n" \
108 " key_file=%%s default: \"\" (pre-loaded)\n"
109#else
110#define USAGE_IO \
111 " No file operations available (POLARSSL_FS_IO not defined)\n"
112#endif /* POLARSSL_FS_IO */
113
114#define USAGE \
115 "\n usage: ssl_mail_client param=<>...\n" \
116 "\n acceptable parameters:\n" \
117 " server_name=%%s default: localhost\n" \
118 " server_port=%%d default: 4433\n" \
119 " debug_level=%%d default: 0 (disabled)\n" \
120 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
121 USAGE_AUTH \
122 " mail_from=%%s default: \"\"\n" \
123 " mail_to=%%s default: \"\"\n" \
124 USAGE_IO \
125 " force_ciphersuite=<name> default: all enabled\n"\
126 " acceptable ciphersuite names:\n"
127
Rich Evans18b78c72015-02-11 14:06:19 +0000128#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
129 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
130 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +0000131 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
Rich Evans18b78c72015-02-11 14:06:19 +0000132 !defined(POLARSSL_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +0000133int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000134{
Rich Evans18b78c72015-02-11 14:06:19 +0000135 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
136 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
137 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
138 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
139 "not defined.\n");
140 return( 0 );
141}
142#else
Paul Bakker1496d382011-05-23 12:07:29 +0000143/*
144 * global options
145 */
146struct options
147{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200148 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +0000149 int server_port; /* port on which the ssl service runs */
150 int debug_level; /* level of debugging */
151 int authentication; /* if authentication is required */
152 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200153 const char *user_name; /* username to use for authentication */
154 const char *user_pwd; /* password to use for authentication */
155 const char *mail_from; /* E-Mail address to use as sender */
156 const char *mail_to; /* E-Mail address to use as recipient */
157 const char *ca_file; /* the file with the CA certificate(s) */
158 const char *crt_file; /* the file with the client certificate */
159 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000160 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
161} opt;
162
Paul Bakker3c5ef712013-06-25 16:37:45 +0200163static void my_debug( void *ctx, int level, const char *str )
Paul Bakker1496d382011-05-23 12:07:29 +0000164{
165 if( level < opt.debug_level )
166 {
Rich Evansf90016a2015-01-19 14:26:37 +0000167 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakker1496d382011-05-23 12:07:29 +0000168 fflush( (FILE *) ctx );
169 }
170}
171
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200172static int do_handshake( ssl_context *ssl )
Paul Bakker1496d382011-05-23 12:07:29 +0000173{
174 int ret;
175 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000176 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000177
178 /*
179 * 4. Handshake
180 */
Rich Evansf90016a2015-01-19 14:26:37 +0000181 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000182 fflush( stdout );
183
184 while( ( ret = ssl_handshake( ssl ) ) != 0 )
185 {
186 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
187 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000188#if defined(POLARSSL_ERROR_C)
Paul Bakker03a8a792013-06-30 12:18:08 +0200189 polarssl_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000190#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000191 polarssl_printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000192 return( -1 );
193 }
194 }
195
Rich Evansf90016a2015-01-19 14:26:37 +0000196 polarssl_printf( " ok\n [ Ciphersuite is %s ]\n",
Paul Bakker1496d382011-05-23 12:07:29 +0000197 ssl_get_ciphersuite( ssl ) );
198
199 /*
200 * 5. Verify the server certificate
201 */
Rich Evansf90016a2015-01-19 14:26:37 +0000202 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000203
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200204 /* In real life, we probably want to bail out when ret != 0 */
Paul Bakker1496d382011-05-23 12:07:29 +0000205 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
206 {
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200207 char vrfy_buf[512];
208
Rich Evansf90016a2015-01-19 14:26:37 +0000209 polarssl_printf( " failed\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000210
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200211 x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000212
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200213 polarssl_printf( "%s\n", vrfy_buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000214 }
215 else
Rich Evansf90016a2015-01-19 14:26:37 +0000216 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000217
Rich Evansf90016a2015-01-19 14:26:37 +0000218 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200219 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
220 ssl_get_peer_cert( ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +0000221 polarssl_printf( "%s\n", buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000222
223 return( 0 );
224}
225
Paul Bakker3c5ef712013-06-25 16:37:45 +0200226static int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000227{
228 int ret;
229
Rich Evansf90016a2015-01-19 14:26:37 +0000230 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000231 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
232 {
233 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
234 {
Rich Evansf90016a2015-01-19 14:26:37 +0000235 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000236 return -1;
237 }
238 }
239
240 return( 0 );
241}
242
Paul Bakker3c5ef712013-06-25 16:37:45 +0200243static int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000244{
245 int ret;
246 unsigned char data[128];
247 char code[4];
248 size_t i, idx = 0;
249
Rich Evansf90016a2015-01-19 14:26:37 +0000250 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000251 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
252 {
253 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
254 {
Rich Evansf90016a2015-01-19 14:26:37 +0000255 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000256 return -1;
257 }
258 }
259
260 do
261 {
262 len = sizeof( data ) - 1;
263 memset( data, 0, sizeof( data ) );
264 ret = ssl_read( ssl, data, len );
265
266 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
267 continue;
268
269 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
270 return -1;
271
272 if( ret <= 0 )
273 {
Rich Evansf90016a2015-01-19 14:26:37 +0000274 polarssl_printf( "failed\n ! ssl_read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000275 return -1;
276 }
277
Rich Evansf90016a2015-01-19 14:26:37 +0000278 polarssl_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000279 len = ret;
280 for( i = 0; i < len; i++ )
281 {
282 if( data[i] != '\n' )
283 {
284 if( idx < 4 )
285 code[ idx++ ] = data[i];
286 continue;
287 }
288
289 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
290 {
291 code[3] = '\0';
292 return atoi( code );
293 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200294
Paul Bakker1496d382011-05-23 12:07:29 +0000295 idx = 0;
296 }
297 }
298 while( 1 );
299}
300
Paul Bakker3c5ef712013-06-25 16:37:45 +0200301static int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000302{
303 int ret;
304 unsigned char data[128];
305 char code[4];
306 size_t i, idx = 0;
307
Rich Evansf90016a2015-01-19 14:26:37 +0000308 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000309 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
310 {
Rich Evansf90016a2015-01-19 14:26:37 +0000311 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000312 return -1;
313 }
314
315 do
316 {
317 len = sizeof( data ) - 1;
318 memset( data, 0, sizeof( data ) );
319 ret = read( sock_fd, data, len );
320
321 if( ret <= 0 )
322 {
Rich Evansf90016a2015-01-19 14:26:37 +0000323 polarssl_printf( "failed\n ! read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000324 return -1;
325 }
326
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200327 data[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +0000328 polarssl_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000329 len = ret;
330 for( i = 0; i < len; i++ )
331 {
332 if( data[i] != '\n' )
333 {
334 if( idx < 4 )
335 code[ idx++ ] = data[i];
336 continue;
337 }
338
339 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
340 {
341 code[3] = '\0';
342 return atoi( code );
343 }
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000344
Paul Bakker1496d382011-05-23 12:07:29 +0000345 idx = 0;
346 }
347 }
348 while( 1 );
349}
350
Paul Bakker1496d382011-05-23 12:07:29 +0000351int main( int argc, char *argv[] )
352{
353 int ret = 0, len, server_fd;
Paul Bakker09c9dd82014-08-18 11:06:56 +0200354 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000355#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000356 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000357#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000358 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200359 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000360
361 entropy_context entropy;
362 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000363 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200364 x509_crt cacert;
365 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200366 pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000367 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000368 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000369 char *p, *q;
370 const int *list;
371
372 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200373 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000374 */
375 server_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200376 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker333fdec2014-08-04 12:12:09 +0200377 memset( &buf, 0, sizeof( buf ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200378 x509_crt_init( &cacert );
379 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200380 pk_init( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000381
382 if( argc == 0 )
383 {
384 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000385 polarssl_printf( USAGE );
Paul Bakker1496d382011-05-23 12:07:29 +0000386
387 list = ssl_list_ciphersuites();
388 while( *list )
389 {
Rich Evansf90016a2015-01-19 14:26:37 +0000390 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000391 list++;
392 }
Rich Evansf90016a2015-01-19 14:26:37 +0000393 polarssl_printf("\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000394 goto exit;
395 }
396
397 opt.server_name = DFL_SERVER_NAME;
398 opt.server_port = DFL_SERVER_PORT;
399 opt.debug_level = DFL_DEBUG_LEVEL;
400 opt.authentication = DFL_AUTHENTICATION;
401 opt.mode = DFL_MODE;
402 opt.user_name = DFL_USER_NAME;
403 opt.user_pwd = DFL_USER_PWD;
404 opt.mail_from = DFL_MAIL_FROM;
405 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000406 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000407 opt.crt_file = DFL_CRT_FILE;
408 opt.key_file = DFL_KEY_FILE;
409 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
410
411 for( i = 1; i < argc; i++ )
412 {
Paul Bakker1496d382011-05-23 12:07:29 +0000413 p = argv[i];
414 if( ( q = strchr( p, '=' ) ) == NULL )
415 goto usage;
416 *q++ = '\0';
417
418 if( strcmp( p, "server_name" ) == 0 )
419 opt.server_name = q;
420 else if( strcmp( p, "server_port" ) == 0 )
421 {
422 opt.server_port = atoi( q );
423 if( opt.server_port < 1 || opt.server_port > 65535 )
424 goto usage;
425 }
426 else if( strcmp( p, "debug_level" ) == 0 )
427 {
428 opt.debug_level = atoi( q );
429 if( opt.debug_level < 0 || opt.debug_level > 65535 )
430 goto usage;
431 }
432 else if( strcmp( p, "authentication" ) == 0 )
433 {
434 opt.authentication = atoi( q );
435 if( opt.authentication < 0 || opt.authentication > 1 )
436 goto usage;
437 }
438 else if( strcmp( p, "mode" ) == 0 )
439 {
440 opt.mode = atoi( q );
441 if( opt.mode < 0 || opt.mode > 1 )
442 goto usage;
443 }
444 else if( strcmp( p, "user_name" ) == 0 )
445 opt.user_name = q;
446 else if( strcmp( p, "user_pwd" ) == 0 )
447 opt.user_pwd = q;
448 else if( strcmp( p, "mail_from" ) == 0 )
449 opt.mail_from = q;
450 else if( strcmp( p, "mail_to" ) == 0 )
451 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000452 else if( strcmp( p, "ca_file" ) == 0 )
453 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000454 else if( strcmp( p, "crt_file" ) == 0 )
455 opt.crt_file = q;
456 else if( strcmp( p, "key_file" ) == 0 )
457 opt.key_file = q;
458 else if( strcmp( p, "force_ciphersuite" ) == 0 )
459 {
460 opt.force_ciphersuite[0] = -1;
461
462 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
463
464 if( opt.force_ciphersuite[0] <= 0 )
465 goto usage;
466
467 opt.force_ciphersuite[1] = 0;
468 }
469 else
470 goto usage;
471 }
472
473 /*
474 * 0. Initialize the RNG and the session data
475 */
Rich Evansf90016a2015-01-19 14:26:37 +0000476 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000477 fflush( stdout );
478
479 entropy_init( &entropy );
480 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200481 (const unsigned char *) pers,
482 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000483 {
Rich Evansf90016a2015-01-19 14:26:37 +0000484 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000485 goto exit;
486 }
487
Rich Evansf90016a2015-01-19 14:26:37 +0000488 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000489
490 /*
491 * 1.1. Load the trusted CA
492 */
Rich Evansf90016a2015-01-19 14:26:37 +0000493 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000494 fflush( stdout );
495
Paul Bakker5690efc2011-05-26 13:16:06 +0000496#if defined(POLARSSL_FS_IO)
497 if( strlen( opt.ca_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200498 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000499 else
500#endif
501#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200502 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
503 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000504#else
505 {
506 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000507 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000508 }
509#endif
Paul Bakker42488232012-05-16 08:21:05 +0000510 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000511 {
Rich Evansf90016a2015-01-19 14:26:37 +0000512 polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000513 goto exit;
514 }
515
Rich Evansf90016a2015-01-19 14:26:37 +0000516 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000517
518 /*
519 * 1.2. Load own certificate and private key
520 *
521 * (can be skipped if client authentication is not required)
522 */
Rich Evansf90016a2015-01-19 14:26:37 +0000523 polarssl_printf( " . Loading the client cert. and key..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000524 fflush( stdout );
525
Paul Bakker5690efc2011-05-26 13:16:06 +0000526#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000527 if( strlen( opt.crt_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200528 ret = x509_crt_parse_file( &clicert, opt.crt_file );
529 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000530#endif
531#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200532 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
533 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000534#else
535 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000536 ret = -1;
Rich Evansf90016a2015-01-19 14:26:37 +0000537 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000538 }
539#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000540 if( ret != 0 )
541 {
Rich Evansf90016a2015-01-19 14:26:37 +0000542 polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000543 goto exit;
544 }
545
Paul Bakker5690efc2011-05-26 13:16:06 +0000546#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000547 if( strlen( opt.key_file ) )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200548 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker1496d382011-05-23 12:07:29 +0000549 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000550#endif
551#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200552 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000553 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000554#else
555 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000556 ret = -1;
Rich Evansf90016a2015-01-19 14:26:37 +0000557 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000558 }
559#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000560 if( ret != 0 )
561 {
Rich Evansf90016a2015-01-19 14:26:37 +0000562 polarssl_printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000563 goto exit;
564 }
565
Rich Evansf90016a2015-01-19 14:26:37 +0000566 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000567
568 /*
569 * 2. Start the connection
570 */
Rich Evansf90016a2015-01-19 14:26:37 +0000571 polarssl_printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
Paul Bakker1496d382011-05-23 12:07:29 +0000572 opt.server_port );
573 fflush( stdout );
574
575 if( ( ret = net_connect( &server_fd, opt.server_name,
576 opt.server_port ) ) != 0 )
577 {
Rich Evansf90016a2015-01-19 14:26:37 +0000578 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000579 goto exit;
580 }
581
Rich Evansf90016a2015-01-19 14:26:37 +0000582 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000583
584 /*
585 * 3. Setup stuff
586 */
Rich Evansf90016a2015-01-19 14:26:37 +0000587 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000588 fflush( stdout );
589
Paul Bakker1496d382011-05-23 12:07:29 +0000590 if( ( ret = ssl_init( &ssl ) ) != 0 )
591 {
Rich Evansf90016a2015-01-19 14:26:37 +0000592 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000593 goto exit;
594 }
595
Rich Evansf90016a2015-01-19 14:26:37 +0000596 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000597
598 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100599 /* OPTIONAL is not optimal for security,
600 * but makes interop easier in this simplified example */
Paul Bakker1496d382011-05-23 12:07:29 +0000601 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
602
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100603 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
604 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100605 /* RC4 is deprecated, disable it */
606 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100607
Paul Bakker508ad5a2011-12-04 17:09:26 +0000608 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000609 ssl_set_dbg( &ssl, my_debug, stdout );
610 ssl_set_bio( &ssl, net_recv, &server_fd,
611 net_send, &server_fd );
612
Paul Bakker645ce3a2012-10-31 12:32:41 +0000613 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000614 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
615
Paul Bakker1496d382011-05-23 12:07:29 +0000616 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200617 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
618 {
Rich Evansf90016a2015-01-19 14:26:37 +0000619 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200620 goto exit;
621 }
Paul Bakker1496d382011-05-23 12:07:29 +0000622
Paul Bakker0be444a2013-08-27 21:55:01 +0200623#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200624 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
625 {
Rich Evansf90016a2015-01-19 14:26:37 +0000626 polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200627 goto exit;
628 }
Paul Bakker0be444a2013-08-27 21:55:01 +0200629#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000630
631 if( opt.mode == MODE_SSL_TLS )
632 {
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200633 if( do_handshake( &ssl ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000634 goto exit;
635
Rich Evansf90016a2015-01-19 14:26:37 +0000636 polarssl_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000637 fflush( stdout );
638
639 ret = write_ssl_and_get_response( &ssl, buf, 0 );
640 if( ret < 200 || ret > 299 )
641 {
Rich Evansf90016a2015-01-19 14:26:37 +0000642 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000643 goto exit;
644 }
645
Rich Evansf90016a2015-01-19 14:26:37 +0000646 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000647
Rich Evansf90016a2015-01-19 14:26:37 +0000648 polarssl_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000649 fflush( stdout );
650
651 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100652 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000653 ret = write_ssl_and_get_response( &ssl, buf, len );
654 if( ret < 200 || ret > 299 )
655 {
Rich Evansf90016a2015-01-19 14:26:37 +0000656 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000657 goto exit;
658 }
659 }
660 else
661 {
Rich Evansf90016a2015-01-19 14:26:37 +0000662 polarssl_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000663 fflush( stdout );
664
665 ret = write_and_get_response( server_fd, buf, 0 );
666 if( ret < 200 || ret > 299 )
667 {
Rich Evansf90016a2015-01-19 14:26:37 +0000668 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000669 goto exit;
670 }
671
Rich Evansf90016a2015-01-19 14:26:37 +0000672 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000673
Rich Evansf90016a2015-01-19 14:26:37 +0000674 polarssl_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000675 fflush( stdout );
676
677 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100678 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000679 ret = write_and_get_response( server_fd, buf, len );
680 if( ret < 200 || ret > 299 )
681 {
Rich Evansf90016a2015-01-19 14:26:37 +0000682 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000683 goto exit;
684 }
685
Rich Evansf90016a2015-01-19 14:26:37 +0000686 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000687
Rich Evansf90016a2015-01-19 14:26:37 +0000688 polarssl_printf( " > Write STARTTLS to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000689 fflush( stdout );
690
691 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100692 len = sprintf( (char *) buf, "STARTTLS\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000693 ret = write_and_get_response( server_fd, buf, len );
694 if( ret < 200 || ret > 299 )
695 {
Rich Evansf90016a2015-01-19 14:26:37 +0000696 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000697 goto exit;
698 }
699
Rich Evansf90016a2015-01-19 14:26:37 +0000700 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000701
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200702 if( do_handshake( &ssl ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000703 goto exit;
704 }
705
Paul Bakker5690efc2011-05-26 13:16:06 +0000706#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000707 if( opt.authentication )
708 {
Rich Evansf90016a2015-01-19 14:26:37 +0000709 polarssl_printf( " > Write AUTH LOGIN to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000710 fflush( stdout );
711
Paul Bakkerd75ba402014-01-24 16:11:17 +0100712 len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000713 ret = write_ssl_and_get_response( &ssl, buf, len );
714 if( ret < 200 || ret > 399 )
715 {
Rich Evansf90016a2015-01-19 14:26:37 +0000716 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000717 goto exit;
718 }
719
Rich Evansf90016a2015-01-19 14:26:37 +0000720 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000721
Rich Evansf90016a2015-01-19 14:26:37 +0000722 polarssl_printf( " > Write username to server: %s", opt.user_name );
Paul Bakker1496d382011-05-23 12:07:29 +0000723 fflush( stdout );
724
Manuel Pégourié-Gonnardfa950c92015-04-30 12:50:22 +0200725 n = sizeof( base );
Alfred Klomp7c034242014-07-14 22:11:13 +0200726 ret = base64_encode( base, &n, (const unsigned char *) opt.user_name,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200727 strlen( opt.user_name ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200728
729 if( ret != 0 ) {
Rich Evansf90016a2015-01-19 14:26:37 +0000730 polarssl_printf( " failed\n ! base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200731 goto exit;
732 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100733 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000734 ret = write_ssl_and_get_response( &ssl, buf, len );
735 if( ret < 300 || ret > 399 )
736 {
Rich Evansf90016a2015-01-19 14:26:37 +0000737 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000738 goto exit;
739 }
740
Rich Evansf90016a2015-01-19 14:26:37 +0000741 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000742
Rich Evansf90016a2015-01-19 14:26:37 +0000743 polarssl_printf( " > Write password to server: %s", opt.user_pwd );
Paul Bakker1496d382011-05-23 12:07:29 +0000744 fflush( stdout );
745
Manuel Pégourié-Gonnardfa950c92015-04-30 12:50:22 +0200746 n = sizeof( base );
Alfred Klomp7c034242014-07-14 22:11:13 +0200747 ret = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200748 strlen( opt.user_pwd ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200749
750 if( ret != 0 ) {
Rich Evansf90016a2015-01-19 14:26:37 +0000751 polarssl_printf( " failed\n ! base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200752 goto exit;
753 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100754 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000755 ret = write_ssl_and_get_response( &ssl, buf, len );
756 if( ret < 200 || ret > 399 )
757 {
Rich Evansf90016a2015-01-19 14:26:37 +0000758 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000759 goto exit;
760 }
761
Rich Evansf90016a2015-01-19 14:26:37 +0000762 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000763 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000764#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000765
Rich Evansf90016a2015-01-19 14:26:37 +0000766 polarssl_printf( " > Write MAIL FROM to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000767 fflush( stdout );
768
Paul Bakkerd75ba402014-01-24 16:11:17 +0100769 len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
Paul Bakker1496d382011-05-23 12:07:29 +0000770 ret = write_ssl_and_get_response( &ssl, buf, len );
771 if( ret < 200 || ret > 299 )
772 {
Rich Evansf90016a2015-01-19 14:26:37 +0000773 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000774 goto exit;
775 }
776
Rich Evansf90016a2015-01-19 14:26:37 +0000777 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000778
Rich Evansf90016a2015-01-19 14:26:37 +0000779 polarssl_printf( " > Write RCPT TO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000780 fflush( stdout );
781
Paul Bakkerd75ba402014-01-24 16:11:17 +0100782 len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
Paul Bakker1496d382011-05-23 12:07:29 +0000783 ret = write_ssl_and_get_response( &ssl, buf, len );
784 if( ret < 200 || ret > 299 )
785 {
Rich Evansf90016a2015-01-19 14:26:37 +0000786 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000787 goto exit;
788 }
789
Rich Evansf90016a2015-01-19 14:26:37 +0000790 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000791
Rich Evansf90016a2015-01-19 14:26:37 +0000792 polarssl_printf( " > Write DATA to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000793 fflush( stdout );
794
Paul Bakkerd75ba402014-01-24 16:11:17 +0100795 len = sprintf( (char *) buf, "DATA\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000796 ret = write_ssl_and_get_response( &ssl, buf, len );
797 if( ret < 300 || ret > 399 )
798 {
Rich Evansf90016a2015-01-19 14:26:37 +0000799 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000800 goto exit;
801 }
802
Rich Evansf90016a2015-01-19 14:26:37 +0000803 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000804
Rich Evansf90016a2015-01-19 14:26:37 +0000805 polarssl_printf( " > Write content to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000806 fflush( stdout );
807
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000808 len = sprintf( (char *) buf, "From: %s\r\nSubject: mbed TLS Test mail\r\n\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000809 "This is a simple test mail from the "
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000810 "mbed TLS mail client example.\r\n"
Paul Bakkerd75ba402014-01-24 16:11:17 +0100811 "\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000812 "Enjoy!", opt.mail_from );
813 ret = write_ssl_data( &ssl, buf, len );
814
815 len = sprintf( (char *) buf, "\r\n.\r\n");
816 ret = write_ssl_and_get_response( &ssl, buf, len );
817 if( ret < 200 || ret > 299 )
818 {
Rich Evansf90016a2015-01-19 14:26:37 +0000819 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000820 goto exit;
821 }
822
Rich Evansf90016a2015-01-19 14:26:37 +0000823 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000824
825 ssl_close_notify( &ssl );
826
827exit:
828
829 if( server_fd )
830 net_close( server_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200831 x509_crt_free( &clicert );
832 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200833 pk_free( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000834 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200835 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200836 entropy_free( &entropy );
Paul Bakker1496d382011-05-23 12:07:29 +0000837
Paul Bakkercce9d772011-11-18 14:26:47 +0000838#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000839 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000840 fflush( stdout ); getchar();
841#endif
842
843 return( ret );
844}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000845#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
846 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
847 POLARSSL_CTR_DRBG_C */