blob: 8a7850e9158a6a05f8931bc654f31538894f32d3 [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>
Andres Amaya Garcia86f76ea2017-07-06 16:57:34 +010060#define read(fd, buf, len) \
61 _read( fd, (void *)( buf ), (unsigned int)( len ) )
62#define write(fd, buf, len) \
63 _write( fd, (const void *)( buf ), (unsigned int)( len ) )
Paul Bakkerfdda7852013-11-30 15:15:31 +010064#endif
Paul Bakker1496d382011-05-23 12:07:29 +000065
Paul Bakker5a835222011-10-12 09:19:31 +000066#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5a835222011-10-12 09:19:31 +000067#include <winsock2.h>
68#include <windows.h>
69
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010070#if defined(_MSC_VER)
Paul Bakker5a835222011-10-12 09:19:31 +000071#if defined(_WIN32_WCE)
72#pragma comment( lib, "ws2.lib" )
73#else
74#pragma comment( lib, "ws2_32.lib" )
75#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010076#endif /* _MSC_VER */
Paul Bakker5a835222011-10-12 09:19:31 +000077#endif
78
Paul Bakker1496d382011-05-23 12:07:29 +000079#define DFL_SERVER_NAME "localhost"
80#define DFL_SERVER_PORT 465
81#define DFL_USER_NAME "user"
82#define DFL_USER_PWD "password"
83#define DFL_MAIL_FROM ""
84#define DFL_MAIL_TO ""
85#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000086#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000087#define DFL_CRT_FILE ""
88#define DFL_KEY_FILE ""
89#define DFL_FORCE_CIPHER 0
90#define DFL_MODE 0
91#define DFL_AUTHENTICATION 0
92
93#define MODE_SSL_TLS 0
94#define MODE_STARTTLS 0
95
Rich Evans85b05ec2015-02-12 11:37:29 +000096#if defined(POLARSSL_BASE64_C)
97#define USAGE_AUTH \
98 " authentication=%%d default: 0 (disabled)\n" \
99 " user_name=%%s default: \"user\"\n" \
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000100 " user_pwd=%%s default: \"password\"\n"
Rich Evans85b05ec2015-02-12 11:37:29 +0000101#else
102#define USAGE_AUTH \
103 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
104#endif /* POLARSSL_BASE64_C */
105
106#if defined(POLARSSL_FS_IO)
107#define USAGE_IO \
108 " ca_file=%%s default: \"\" (pre-loaded)\n" \
109 " crt_file=%%s default: \"\" (pre-loaded)\n" \
110 " key_file=%%s default: \"\" (pre-loaded)\n"
111#else
112#define USAGE_IO \
113 " No file operations available (POLARSSL_FS_IO not defined)\n"
114#endif /* POLARSSL_FS_IO */
115
116#define USAGE \
117 "\n usage: ssl_mail_client param=<>...\n" \
118 "\n acceptable parameters:\n" \
119 " server_name=%%s default: localhost\n" \
120 " server_port=%%d default: 4433\n" \
121 " debug_level=%%d default: 0 (disabled)\n" \
122 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
123 USAGE_AUTH \
124 " mail_from=%%s default: \"\"\n" \
125 " mail_to=%%s default: \"\"\n" \
126 USAGE_IO \
127 " force_ciphersuite=<name> default: all enabled\n"\
128 " acceptable ciphersuite names:\n"
129
Rich Evans18b78c72015-02-11 14:06:19 +0000130#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
131 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
132 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +0000133 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
Rich Evans18b78c72015-02-11 14:06:19 +0000134 !defined(POLARSSL_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +0000135int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000136{
Rich Evans18b78c72015-02-11 14:06:19 +0000137 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
138 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
139 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
140 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
141 "not defined.\n");
142 return( 0 );
143}
144#else
Paul Bakker1496d382011-05-23 12:07:29 +0000145/*
146 * global options
147 */
148struct options
149{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200150 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +0000151 int server_port; /* port on which the ssl service runs */
152 int debug_level; /* level of debugging */
153 int authentication; /* if authentication is required */
154 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200155 const char *user_name; /* username to use for authentication */
156 const char *user_pwd; /* password to use for authentication */
157 const char *mail_from; /* E-Mail address to use as sender */
158 const char *mail_to; /* E-Mail address to use as recipient */
159 const char *ca_file; /* the file with the CA certificate(s) */
160 const char *crt_file; /* the file with the client certificate */
161 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000162 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
163} opt;
164
Paul Bakker3c5ef712013-06-25 16:37:45 +0200165static void my_debug( void *ctx, int level, const char *str )
Paul Bakker1496d382011-05-23 12:07:29 +0000166{
167 if( level < opt.debug_level )
168 {
Rich Evansf90016a2015-01-19 14:26:37 +0000169 polarssl_fprintf( (FILE *) ctx, "%s", str );
Paul Bakker1496d382011-05-23 12:07:29 +0000170 fflush( (FILE *) ctx );
171 }
172}
173
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200174static int do_handshake( ssl_context *ssl )
Paul Bakker1496d382011-05-23 12:07:29 +0000175{
176 int ret;
177 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000178 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000179
180 /*
181 * 4. Handshake
182 */
Rich Evansf90016a2015-01-19 14:26:37 +0000183 polarssl_printf( " . Performing the SSL/TLS handshake..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000184 fflush( stdout );
185
186 while( ( ret = ssl_handshake( ssl ) ) != 0 )
187 {
188 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
189 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000190#if defined(POLARSSL_ERROR_C)
Paul Bakker03a8a792013-06-30 12:18:08 +0200191 polarssl_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000192#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000193 polarssl_printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000194 return( -1 );
195 }
196 }
197
Rich Evansf90016a2015-01-19 14:26:37 +0000198 polarssl_printf( " ok\n [ Ciphersuite is %s ]\n",
Paul Bakker1496d382011-05-23 12:07:29 +0000199 ssl_get_ciphersuite( ssl ) );
200
201 /*
202 * 5. Verify the server certificate
203 */
Rich Evansf90016a2015-01-19 14:26:37 +0000204 polarssl_printf( " . Verifying peer X.509 certificate..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000205
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200206 /* In real life, we probably want to bail out when ret != 0 */
Paul Bakker1496d382011-05-23 12:07:29 +0000207 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
208 {
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200209 char vrfy_buf[512];
210
Rich Evansf90016a2015-01-19 14:26:37 +0000211 polarssl_printf( " failed\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000212
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200213 x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000214
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200215 polarssl_printf( "%s\n", vrfy_buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000216 }
217 else
Rich Evansf90016a2015-01-19 14:26:37 +0000218 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000219
Rich Evansf90016a2015-01-19 14:26:37 +0000220 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200221 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
222 ssl_get_peer_cert( ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +0000223 polarssl_printf( "%s\n", buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000224
225 return( 0 );
226}
227
Paul Bakker3c5ef712013-06-25 16:37:45 +0200228static int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000229{
230 int ret;
231
Rich Evansf90016a2015-01-19 14:26:37 +0000232 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000233 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
234 {
235 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
236 {
Rich Evansf90016a2015-01-19 14:26:37 +0000237 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000238 return -1;
239 }
240 }
241
242 return( 0 );
243}
244
Paul Bakker3c5ef712013-06-25 16:37:45 +0200245static int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000246{
247 int ret;
248 unsigned char data[128];
249 char code[4];
250 size_t i, idx = 0;
251
Rich Evansf90016a2015-01-19 14:26:37 +0000252 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000253 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
254 {
255 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
256 {
Rich Evansf90016a2015-01-19 14:26:37 +0000257 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000258 return -1;
259 }
260 }
261
262 do
263 {
264 len = sizeof( data ) - 1;
265 memset( data, 0, sizeof( data ) );
266 ret = ssl_read( ssl, data, len );
267
268 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
269 continue;
270
271 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
272 return -1;
273
274 if( ret <= 0 )
275 {
Rich Evansf90016a2015-01-19 14:26:37 +0000276 polarssl_printf( "failed\n ! ssl_read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000277 return -1;
278 }
279
Rich Evansf90016a2015-01-19 14:26:37 +0000280 polarssl_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000281 len = ret;
282 for( i = 0; i < len; i++ )
283 {
284 if( data[i] != '\n' )
285 {
286 if( idx < 4 )
287 code[ idx++ ] = data[i];
288 continue;
289 }
290
291 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
292 {
293 code[3] = '\0';
294 return atoi( code );
295 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200296
Paul Bakker1496d382011-05-23 12:07:29 +0000297 idx = 0;
298 }
299 }
300 while( 1 );
301}
302
Paul Bakker3c5ef712013-06-25 16:37:45 +0200303static int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000304{
305 int ret;
306 unsigned char data[128];
307 char code[4];
308 size_t i, idx = 0;
309
Rich Evansf90016a2015-01-19 14:26:37 +0000310 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000311 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
312 {
Rich Evansf90016a2015-01-19 14:26:37 +0000313 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000314 return -1;
315 }
316
317 do
318 {
319 len = sizeof( data ) - 1;
320 memset( data, 0, sizeof( data ) );
321 ret = read( sock_fd, data, len );
322
323 if( ret <= 0 )
324 {
Rich Evansf90016a2015-01-19 14:26:37 +0000325 polarssl_printf( "failed\n ! read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000326 return -1;
327 }
328
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200329 data[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +0000330 polarssl_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000331 len = ret;
332 for( i = 0; i < len; i++ )
333 {
334 if( data[i] != '\n' )
335 {
336 if( idx < 4 )
337 code[ idx++ ] = data[i];
338 continue;
339 }
340
341 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
342 {
343 code[3] = '\0';
344 return atoi( code );
345 }
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000346
Paul Bakker1496d382011-05-23 12:07:29 +0000347 idx = 0;
348 }
349 }
350 while( 1 );
351}
352
Paul Bakker1496d382011-05-23 12:07:29 +0000353int main( int argc, char *argv[] )
354{
355 int ret = 0, len, server_fd;
Paul Bakker09c9dd82014-08-18 11:06:56 +0200356 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000357#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000358 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000359#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000360 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200361 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000362
363 entropy_context entropy;
364 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000365 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200366 x509_crt cacert;
367 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200368 pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000369 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000370 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000371 char *p, *q;
372 const int *list;
373
374 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200375 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000376 */
377 server_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200378 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker333fdec2014-08-04 12:12:09 +0200379 memset( &buf, 0, sizeof( buf ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200380 x509_crt_init( &cacert );
381 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200382 pk_init( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000383
384 if( argc == 0 )
385 {
386 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000387 polarssl_printf( USAGE );
Paul Bakker1496d382011-05-23 12:07:29 +0000388
389 list = ssl_list_ciphersuites();
390 while( *list )
391 {
Rich Evansf90016a2015-01-19 14:26:37 +0000392 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000393 list++;
394 }
Rich Evansf90016a2015-01-19 14:26:37 +0000395 polarssl_printf("\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000396 goto exit;
397 }
398
399 opt.server_name = DFL_SERVER_NAME;
400 opt.server_port = DFL_SERVER_PORT;
401 opt.debug_level = DFL_DEBUG_LEVEL;
402 opt.authentication = DFL_AUTHENTICATION;
403 opt.mode = DFL_MODE;
404 opt.user_name = DFL_USER_NAME;
405 opt.user_pwd = DFL_USER_PWD;
406 opt.mail_from = DFL_MAIL_FROM;
407 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000408 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000409 opt.crt_file = DFL_CRT_FILE;
410 opt.key_file = DFL_KEY_FILE;
411 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
412
413 for( i = 1; i < argc; i++ )
414 {
Paul Bakker1496d382011-05-23 12:07:29 +0000415 p = argv[i];
416 if( ( q = strchr( p, '=' ) ) == NULL )
417 goto usage;
418 *q++ = '\0';
419
420 if( strcmp( p, "server_name" ) == 0 )
421 opt.server_name = q;
422 else if( strcmp( p, "server_port" ) == 0 )
423 {
424 opt.server_port = atoi( q );
425 if( opt.server_port < 1 || opt.server_port > 65535 )
426 goto usage;
427 }
428 else if( strcmp( p, "debug_level" ) == 0 )
429 {
430 opt.debug_level = atoi( q );
431 if( opt.debug_level < 0 || opt.debug_level > 65535 )
432 goto usage;
433 }
434 else if( strcmp( p, "authentication" ) == 0 )
435 {
436 opt.authentication = atoi( q );
437 if( opt.authentication < 0 || opt.authentication > 1 )
438 goto usage;
439 }
440 else if( strcmp( p, "mode" ) == 0 )
441 {
442 opt.mode = atoi( q );
443 if( opt.mode < 0 || opt.mode > 1 )
444 goto usage;
445 }
446 else if( strcmp( p, "user_name" ) == 0 )
447 opt.user_name = q;
448 else if( strcmp( p, "user_pwd" ) == 0 )
449 opt.user_pwd = q;
450 else if( strcmp( p, "mail_from" ) == 0 )
451 opt.mail_from = q;
452 else if( strcmp( p, "mail_to" ) == 0 )
453 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000454 else if( strcmp( p, "ca_file" ) == 0 )
455 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000456 else if( strcmp( p, "crt_file" ) == 0 )
457 opt.crt_file = q;
458 else if( strcmp( p, "key_file" ) == 0 )
459 opt.key_file = q;
460 else if( strcmp( p, "force_ciphersuite" ) == 0 )
461 {
462 opt.force_ciphersuite[0] = -1;
463
464 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
465
466 if( opt.force_ciphersuite[0] <= 0 )
467 goto usage;
468
469 opt.force_ciphersuite[1] = 0;
470 }
471 else
472 goto usage;
473 }
474
475 /*
476 * 0. Initialize the RNG and the session data
477 */
Rich Evansf90016a2015-01-19 14:26:37 +0000478 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000479 fflush( stdout );
480
481 entropy_init( &entropy );
482 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200483 (const unsigned char *) pers,
484 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000485 {
Rich Evansf90016a2015-01-19 14:26:37 +0000486 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000487 goto exit;
488 }
489
Rich Evansf90016a2015-01-19 14:26:37 +0000490 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000491
492 /*
493 * 1.1. Load the trusted CA
494 */
Rich Evansf90016a2015-01-19 14:26:37 +0000495 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000496 fflush( stdout );
497
Paul Bakker5690efc2011-05-26 13:16:06 +0000498#if defined(POLARSSL_FS_IO)
499 if( strlen( opt.ca_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200500 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000501 else
502#endif
503#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200504 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
505 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000506#else
507 {
508 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000509 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000510 }
511#endif
Paul Bakker42488232012-05-16 08:21:05 +0000512 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000513 {
Rich Evansf90016a2015-01-19 14:26:37 +0000514 polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000515 goto exit;
516 }
517
Rich Evansf90016a2015-01-19 14:26:37 +0000518 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000519
520 /*
521 * 1.2. Load own certificate and private key
522 *
523 * (can be skipped if client authentication is not required)
524 */
Rich Evansf90016a2015-01-19 14:26:37 +0000525 polarssl_printf( " . Loading the client cert. and key..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000526 fflush( stdout );
527
Paul Bakker5690efc2011-05-26 13:16:06 +0000528#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000529 if( strlen( opt.crt_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200530 ret = x509_crt_parse_file( &clicert, opt.crt_file );
531 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000532#endif
533#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200534 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
535 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000536#else
537 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000538 ret = -1;
Rich Evansf90016a2015-01-19 14:26:37 +0000539 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000540 }
541#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000542 if( ret != 0 )
543 {
Rich Evansf90016a2015-01-19 14:26:37 +0000544 polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000545 goto exit;
546 }
547
Paul Bakker5690efc2011-05-26 13:16:06 +0000548#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000549 if( strlen( opt.key_file ) )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200550 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker1496d382011-05-23 12:07:29 +0000551 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000552#endif
553#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200554 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000555 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000556#else
557 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000558 ret = -1;
Rich Evansf90016a2015-01-19 14:26:37 +0000559 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000560 }
561#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000562 if( ret != 0 )
563 {
Rich Evansf90016a2015-01-19 14:26:37 +0000564 polarssl_printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000565 goto exit;
566 }
567
Rich Evansf90016a2015-01-19 14:26:37 +0000568 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000569
570 /*
571 * 2. Start the connection
572 */
Rich Evansf90016a2015-01-19 14:26:37 +0000573 polarssl_printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
Paul Bakker1496d382011-05-23 12:07:29 +0000574 opt.server_port );
575 fflush( stdout );
576
577 if( ( ret = net_connect( &server_fd, opt.server_name,
578 opt.server_port ) ) != 0 )
579 {
Rich Evansf90016a2015-01-19 14:26:37 +0000580 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000581 goto exit;
582 }
583
Rich Evansf90016a2015-01-19 14:26:37 +0000584 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000585
586 /*
587 * 3. Setup stuff
588 */
Rich Evansf90016a2015-01-19 14:26:37 +0000589 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000590 fflush( stdout );
591
Paul Bakker1496d382011-05-23 12:07:29 +0000592 if( ( ret = ssl_init( &ssl ) ) != 0 )
593 {
Rich Evansf90016a2015-01-19 14:26:37 +0000594 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000595 goto exit;
596 }
597
Rich Evansf90016a2015-01-19 14:26:37 +0000598 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000599
600 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100601 /* OPTIONAL is not optimal for security,
602 * but makes interop easier in this simplified example */
Paul Bakker1496d382011-05-23 12:07:29 +0000603 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
604
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100605 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
606 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100607 /* RC4 is deprecated, disable it */
608 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100609
Paul Bakker508ad5a2011-12-04 17:09:26 +0000610 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000611 ssl_set_dbg( &ssl, my_debug, stdout );
612 ssl_set_bio( &ssl, net_recv, &server_fd,
613 net_send, &server_fd );
614
Paul Bakker645ce3a2012-10-31 12:32:41 +0000615 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000616 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
617
Paul Bakker1496d382011-05-23 12:07:29 +0000618 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200619 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
620 {
Rich Evansf90016a2015-01-19 14:26:37 +0000621 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200622 goto exit;
623 }
Paul Bakker1496d382011-05-23 12:07:29 +0000624
Paul Bakker0be444a2013-08-27 21:55:01 +0200625#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200626 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
627 {
Rich Evansf90016a2015-01-19 14:26:37 +0000628 polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200629 goto exit;
630 }
Paul Bakker0be444a2013-08-27 21:55:01 +0200631#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000632
633 if( opt.mode == MODE_SSL_TLS )
634 {
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200635 if( do_handshake( &ssl ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000636 goto exit;
637
Rich Evansf90016a2015-01-19 14:26:37 +0000638 polarssl_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000639 fflush( stdout );
640
641 ret = write_ssl_and_get_response( &ssl, buf, 0 );
642 if( ret < 200 || ret > 299 )
643 {
Rich Evansf90016a2015-01-19 14:26:37 +0000644 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000645 goto exit;
646 }
647
Rich Evansf90016a2015-01-19 14:26:37 +0000648 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000649
Rich Evansf90016a2015-01-19 14:26:37 +0000650 polarssl_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000651 fflush( stdout );
652
653 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100654 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000655 ret = write_ssl_and_get_response( &ssl, buf, len );
656 if( ret < 200 || ret > 299 )
657 {
Rich Evansf90016a2015-01-19 14:26:37 +0000658 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000659 goto exit;
660 }
661 }
662 else
663 {
Rich Evansf90016a2015-01-19 14:26:37 +0000664 polarssl_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000665 fflush( stdout );
666
667 ret = write_and_get_response( server_fd, buf, 0 );
668 if( ret < 200 || ret > 299 )
669 {
Rich Evansf90016a2015-01-19 14:26:37 +0000670 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000671 goto exit;
672 }
673
Rich Evansf90016a2015-01-19 14:26:37 +0000674 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000675
Rich Evansf90016a2015-01-19 14:26:37 +0000676 polarssl_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000677 fflush( stdout );
678
679 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100680 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000681 ret = write_and_get_response( server_fd, buf, len );
682 if( ret < 200 || ret > 299 )
683 {
Rich Evansf90016a2015-01-19 14:26:37 +0000684 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000685 goto exit;
686 }
687
Rich Evansf90016a2015-01-19 14:26:37 +0000688 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000689
Rich Evansf90016a2015-01-19 14:26:37 +0000690 polarssl_printf( " > Write STARTTLS to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000691 fflush( stdout );
692
693 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100694 len = sprintf( (char *) buf, "STARTTLS\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000695 ret = write_and_get_response( server_fd, buf, len );
696 if( ret < 200 || ret > 299 )
697 {
Rich Evansf90016a2015-01-19 14:26:37 +0000698 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000699 goto exit;
700 }
701
Rich Evansf90016a2015-01-19 14:26:37 +0000702 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000703
Manuel Pégourié-Gonnard0c6ce2f2015-04-17 16:32:21 +0200704 if( do_handshake( &ssl ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000705 goto exit;
706 }
707
Paul Bakker5690efc2011-05-26 13:16:06 +0000708#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000709 if( opt.authentication )
710 {
Rich Evansf90016a2015-01-19 14:26:37 +0000711 polarssl_printf( " > Write AUTH LOGIN to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000712 fflush( stdout );
713
Paul Bakkerd75ba402014-01-24 16:11:17 +0100714 len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000715 ret = write_ssl_and_get_response( &ssl, buf, len );
716 if( ret < 200 || ret > 399 )
717 {
Rich Evansf90016a2015-01-19 14:26:37 +0000718 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000719 goto exit;
720 }
721
Rich Evansf90016a2015-01-19 14:26:37 +0000722 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000723
Rich Evansf90016a2015-01-19 14:26:37 +0000724 polarssl_printf( " > Write username to server: %s", opt.user_name );
Paul Bakker1496d382011-05-23 12:07:29 +0000725 fflush( stdout );
726
Manuel Pégourié-Gonnardfa950c92015-04-30 12:50:22 +0200727 n = sizeof( base );
Alfred Klomp7c034242014-07-14 22:11:13 +0200728 ret = base64_encode( base, &n, (const unsigned char *) opt.user_name,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200729 strlen( opt.user_name ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200730
731 if( ret != 0 ) {
Rich Evansf90016a2015-01-19 14:26:37 +0000732 polarssl_printf( " failed\n ! base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200733 goto exit;
734 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100735 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000736 ret = write_ssl_and_get_response( &ssl, buf, len );
737 if( ret < 300 || ret > 399 )
738 {
Rich Evansf90016a2015-01-19 14:26:37 +0000739 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000740 goto exit;
741 }
742
Rich Evansf90016a2015-01-19 14:26:37 +0000743 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000744
Rich Evansf90016a2015-01-19 14:26:37 +0000745 polarssl_printf( " > Write password to server: %s", opt.user_pwd );
Paul Bakker1496d382011-05-23 12:07:29 +0000746 fflush( stdout );
747
Manuel Pégourié-Gonnardfa950c92015-04-30 12:50:22 +0200748 n = sizeof( base );
Alfred Klomp7c034242014-07-14 22:11:13 +0200749 ret = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200750 strlen( opt.user_pwd ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200751
752 if( ret != 0 ) {
Rich Evansf90016a2015-01-19 14:26:37 +0000753 polarssl_printf( " failed\n ! base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200754 goto exit;
755 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100756 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000757 ret = write_ssl_and_get_response( &ssl, buf, len );
758 if( ret < 200 || ret > 399 )
759 {
Rich Evansf90016a2015-01-19 14:26:37 +0000760 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000761 goto exit;
762 }
763
Rich Evansf90016a2015-01-19 14:26:37 +0000764 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000765 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000766#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000767
Rich Evansf90016a2015-01-19 14:26:37 +0000768 polarssl_printf( " > Write MAIL FROM to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000769 fflush( stdout );
770
Paul Bakkerd75ba402014-01-24 16:11:17 +0100771 len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
Paul Bakker1496d382011-05-23 12:07:29 +0000772 ret = write_ssl_and_get_response( &ssl, buf, len );
773 if( ret < 200 || ret > 299 )
774 {
Rich Evansf90016a2015-01-19 14:26:37 +0000775 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000776 goto exit;
777 }
778
Rich Evansf90016a2015-01-19 14:26:37 +0000779 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000780
Rich Evansf90016a2015-01-19 14:26:37 +0000781 polarssl_printf( " > Write RCPT TO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000782 fflush( stdout );
783
Paul Bakkerd75ba402014-01-24 16:11:17 +0100784 len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
Paul Bakker1496d382011-05-23 12:07:29 +0000785 ret = write_ssl_and_get_response( &ssl, buf, len );
786 if( ret < 200 || ret > 299 )
787 {
Rich Evansf90016a2015-01-19 14:26:37 +0000788 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000789 goto exit;
790 }
791
Rich Evansf90016a2015-01-19 14:26:37 +0000792 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000793
Rich Evansf90016a2015-01-19 14:26:37 +0000794 polarssl_printf( " > Write DATA to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000795 fflush( stdout );
796
Paul Bakkerd75ba402014-01-24 16:11:17 +0100797 len = sprintf( (char *) buf, "DATA\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000798 ret = write_ssl_and_get_response( &ssl, buf, len );
799 if( ret < 300 || ret > 399 )
800 {
Rich Evansf90016a2015-01-19 14:26:37 +0000801 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000802 goto exit;
803 }
804
Rich Evansf90016a2015-01-19 14:26:37 +0000805 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000806
Rich Evansf90016a2015-01-19 14:26:37 +0000807 polarssl_printf( " > Write content to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000808 fflush( stdout );
809
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000810 len = sprintf( (char *) buf, "From: %s\r\nSubject: mbed TLS Test mail\r\n\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000811 "This is a simple test mail from the "
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000812 "mbed TLS mail client example.\r\n"
Paul Bakkerd75ba402014-01-24 16:11:17 +0100813 "\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000814 "Enjoy!", opt.mail_from );
815 ret = write_ssl_data( &ssl, buf, len );
816
817 len = sprintf( (char *) buf, "\r\n.\r\n");
818 ret = write_ssl_and_get_response( &ssl, buf, len );
819 if( ret < 200 || ret > 299 )
820 {
Rich Evansf90016a2015-01-19 14:26:37 +0000821 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000822 goto exit;
823 }
824
Rich Evansf90016a2015-01-19 14:26:37 +0000825 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000826
827 ssl_close_notify( &ssl );
828
829exit:
830
831 if( server_fd )
832 net_close( server_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200833 x509_crt_free( &clicert );
834 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200835 pk_free( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000836 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200837 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200838 entropy_free( &entropy );
Paul Bakker1496d382011-05-23 12:07:29 +0000839
Paul Bakkercce9d772011-11-18 14:26:47 +0000840#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000841 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000842 fflush( stdout ); getchar();
843#endif
844
845 return( ret );
846}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000847#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
848 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
849 POLARSSL_CTR_DRBG_C */