blob: 41e0777c644d5515d0dc42a2f7b7623a5572c607 [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 *
8 * 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é-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/base64.h"
43#include "mbedtls/error.h"
44#include "mbedtls/net.h"
45#include "mbedtls/ssl.h"
46#include "mbedtls/entropy.h"
47#include "mbedtls/ctr_drbg.h"
48#include "mbedtls/certs.h"
49#include "mbedtls/x509.h"
Rich Evans18b78c72015-02-11 14:06:19 +000050
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
Paul Bakker3c5ef712013-06-25 16:37:45 +0200172static int do_handshake( ssl_context *ssl, struct options *opt )
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é-Gonnardfcf2fc22014-03-11 11:10:27 +0100204 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker1496d382011-05-23 12:07:29 +0000205 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
206 {
Rich Evansf90016a2015-01-19 14:26:37 +0000207 polarssl_printf( " failed\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000208
209 if( ( ret & BADCERT_EXPIRED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000210 polarssl_printf( " ! server certificate has expired\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000211
212 if( ( ret & BADCERT_REVOKED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000213 polarssl_printf( " ! server certificate has been revoked\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000214
215 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000216 polarssl_printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
Paul Bakker1496d382011-05-23 12:07:29 +0000217
218 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
Rich Evansf90016a2015-01-19 14:26:37 +0000219 polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000220
Rich Evansf90016a2015-01-19 14:26:37 +0000221 polarssl_printf( "\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000222 }
223 else
Rich Evansf90016a2015-01-19 14:26:37 +0000224 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000225
Rich Evansf90016a2015-01-19 14:26:37 +0000226 polarssl_printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200227 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
228 ssl_get_peer_cert( ssl ) );
Rich Evansf90016a2015-01-19 14:26:37 +0000229 polarssl_printf( "%s\n", buf );
Paul Bakker1496d382011-05-23 12:07:29 +0000230
231 return( 0 );
232}
233
Paul Bakker3c5ef712013-06-25 16:37:45 +0200234static int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000235{
236 int ret;
237
Rich Evansf90016a2015-01-19 14:26:37 +0000238 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000239 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
240 {
241 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
242 {
Rich Evansf90016a2015-01-19 14:26:37 +0000243 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000244 return -1;
245 }
246 }
247
248 return( 0 );
249}
250
Paul Bakker3c5ef712013-06-25 16:37:45 +0200251static int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000252{
253 int ret;
254 unsigned char data[128];
255 char code[4];
256 size_t i, idx = 0;
257
Rich Evansf90016a2015-01-19 14:26:37 +0000258 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000259 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
260 {
261 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
262 {
Rich Evansf90016a2015-01-19 14:26:37 +0000263 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000264 return -1;
265 }
266 }
267
268 do
269 {
270 len = sizeof( data ) - 1;
271 memset( data, 0, sizeof( data ) );
272 ret = ssl_read( ssl, data, len );
273
274 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
275 continue;
276
277 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
278 return -1;
279
280 if( ret <= 0 )
281 {
Rich Evansf90016a2015-01-19 14:26:37 +0000282 polarssl_printf( "failed\n ! ssl_read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000283 return -1;
284 }
285
Rich Evansf90016a2015-01-19 14:26:37 +0000286 polarssl_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000287 len = ret;
288 for( i = 0; i < len; i++ )
289 {
290 if( data[i] != '\n' )
291 {
292 if( idx < 4 )
293 code[ idx++ ] = data[i];
294 continue;
295 }
296
297 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
298 {
299 code[3] = '\0';
300 return atoi( code );
301 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200302
Paul Bakker1496d382011-05-23 12:07:29 +0000303 idx = 0;
304 }
305 }
306 while( 1 );
307}
308
Paul Bakker3c5ef712013-06-25 16:37:45 +0200309static int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000310{
311 int ret;
312 unsigned char data[128];
313 char code[4];
314 size_t i, idx = 0;
315
Rich Evansf90016a2015-01-19 14:26:37 +0000316 polarssl_printf("\n%s", buf);
Paul Bakker1496d382011-05-23 12:07:29 +0000317 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
318 {
Rich Evansf90016a2015-01-19 14:26:37 +0000319 polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000320 return -1;
321 }
322
323 do
324 {
325 len = sizeof( data ) - 1;
326 memset( data, 0, sizeof( data ) );
327 ret = read( sock_fd, data, len );
328
329 if( ret <= 0 )
330 {
Rich Evansf90016a2015-01-19 14:26:37 +0000331 polarssl_printf( "failed\n ! read returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000332 return -1;
333 }
334
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200335 data[len] = '\0';
Rich Evansf90016a2015-01-19 14:26:37 +0000336 polarssl_printf("\n%s", data);
Paul Bakker1496d382011-05-23 12:07:29 +0000337 len = ret;
338 for( i = 0; i < len; i++ )
339 {
340 if( data[i] != '\n' )
341 {
342 if( idx < 4 )
343 code[ idx++ ] = data[i];
344 continue;
345 }
346
347 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
348 {
349 code[3] = '\0';
350 return atoi( code );
351 }
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000352
Paul Bakker1496d382011-05-23 12:07:29 +0000353 idx = 0;
354 }
355 }
356 while( 1 );
357}
358
Paul Bakker1496d382011-05-23 12:07:29 +0000359int main( int argc, char *argv[] )
360{
361 int ret = 0, len, server_fd;
Paul Bakker09c9dd82014-08-18 11:06:56 +0200362 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000363#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000364 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000365#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000366 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200367 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000368
369 entropy_context entropy;
370 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000371 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200372 x509_crt cacert;
373 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200374 pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000375 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000376 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000377 char *p, *q;
378 const int *list;
379
380 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200381 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000382 */
383 server_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200384 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker333fdec2014-08-04 12:12:09 +0200385 memset( &buf, 0, sizeof( buf ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200386 x509_crt_init( &cacert );
387 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200388 pk_init( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000389
390 if( argc == 0 )
391 {
392 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000393 polarssl_printf( USAGE );
Paul Bakker1496d382011-05-23 12:07:29 +0000394
395 list = ssl_list_ciphersuites();
396 while( *list )
397 {
Rich Evansf90016a2015-01-19 14:26:37 +0000398 polarssl_printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000399 list++;
400 }
Rich Evansf90016a2015-01-19 14:26:37 +0000401 polarssl_printf("\n");
Paul Bakker1496d382011-05-23 12:07:29 +0000402 goto exit;
403 }
404
405 opt.server_name = DFL_SERVER_NAME;
406 opt.server_port = DFL_SERVER_PORT;
407 opt.debug_level = DFL_DEBUG_LEVEL;
408 opt.authentication = DFL_AUTHENTICATION;
409 opt.mode = DFL_MODE;
410 opt.user_name = DFL_USER_NAME;
411 opt.user_pwd = DFL_USER_PWD;
412 opt.mail_from = DFL_MAIL_FROM;
413 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000414 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000415 opt.crt_file = DFL_CRT_FILE;
416 opt.key_file = DFL_KEY_FILE;
417 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
418
419 for( i = 1; i < argc; i++ )
420 {
Paul Bakker1496d382011-05-23 12:07:29 +0000421 p = argv[i];
422 if( ( q = strchr( p, '=' ) ) == NULL )
423 goto usage;
424 *q++ = '\0';
425
426 if( strcmp( p, "server_name" ) == 0 )
427 opt.server_name = q;
428 else if( strcmp( p, "server_port" ) == 0 )
429 {
430 opt.server_port = atoi( q );
431 if( opt.server_port < 1 || opt.server_port > 65535 )
432 goto usage;
433 }
434 else if( strcmp( p, "debug_level" ) == 0 )
435 {
436 opt.debug_level = atoi( q );
437 if( opt.debug_level < 0 || opt.debug_level > 65535 )
438 goto usage;
439 }
440 else if( strcmp( p, "authentication" ) == 0 )
441 {
442 opt.authentication = atoi( q );
443 if( opt.authentication < 0 || opt.authentication > 1 )
444 goto usage;
445 }
446 else if( strcmp( p, "mode" ) == 0 )
447 {
448 opt.mode = atoi( q );
449 if( opt.mode < 0 || opt.mode > 1 )
450 goto usage;
451 }
452 else if( strcmp( p, "user_name" ) == 0 )
453 opt.user_name = q;
454 else if( strcmp( p, "user_pwd" ) == 0 )
455 opt.user_pwd = q;
456 else if( strcmp( p, "mail_from" ) == 0 )
457 opt.mail_from = q;
458 else if( strcmp( p, "mail_to" ) == 0 )
459 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000460 else if( strcmp( p, "ca_file" ) == 0 )
461 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000462 else if( strcmp( p, "crt_file" ) == 0 )
463 opt.crt_file = q;
464 else if( strcmp( p, "key_file" ) == 0 )
465 opt.key_file = q;
466 else if( strcmp( p, "force_ciphersuite" ) == 0 )
467 {
468 opt.force_ciphersuite[0] = -1;
469
470 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
471
472 if( opt.force_ciphersuite[0] <= 0 )
473 goto usage;
474
475 opt.force_ciphersuite[1] = 0;
476 }
477 else
478 goto usage;
479 }
480
481 /*
482 * 0. Initialize the RNG and the session data
483 */
Rich Evansf90016a2015-01-19 14:26:37 +0000484 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000485 fflush( stdout );
486
487 entropy_init( &entropy );
488 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200489 (const unsigned char *) pers,
490 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000491 {
Rich Evansf90016a2015-01-19 14:26:37 +0000492 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000493 goto exit;
494 }
495
Rich Evansf90016a2015-01-19 14:26:37 +0000496 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000497
498 /*
499 * 1.1. Load the trusted CA
500 */
Rich Evansf90016a2015-01-19 14:26:37 +0000501 polarssl_printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000502 fflush( stdout );
503
Paul Bakker5690efc2011-05-26 13:16:06 +0000504#if defined(POLARSSL_FS_IO)
505 if( strlen( opt.ca_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200506 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000507 else
508#endif
509#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200510 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
511 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000512#else
513 {
514 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000515 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000516 }
517#endif
Paul Bakker42488232012-05-16 08:21:05 +0000518 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000519 {
Rich Evansf90016a2015-01-19 14:26:37 +0000520 polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000521 goto exit;
522 }
523
Rich Evansf90016a2015-01-19 14:26:37 +0000524 polarssl_printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000525
526 /*
527 * 1.2. Load own certificate and private key
528 *
529 * (can be skipped if client authentication is not required)
530 */
Rich Evansf90016a2015-01-19 14:26:37 +0000531 polarssl_printf( " . Loading the client cert. and key..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000532 fflush( stdout );
533
Paul Bakker5690efc2011-05-26 13:16:06 +0000534#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000535 if( strlen( opt.crt_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200536 ret = x509_crt_parse_file( &clicert, opt.crt_file );
537 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000538#endif
539#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200540 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
541 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000542#else
543 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000544 ret = -1;
Rich Evansf90016a2015-01-19 14:26:37 +0000545 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000546 }
547#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000548 if( ret != 0 )
549 {
Rich Evansf90016a2015-01-19 14:26:37 +0000550 polarssl_printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000551 goto exit;
552 }
553
Paul Bakker5690efc2011-05-26 13:16:06 +0000554#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000555 if( strlen( opt.key_file ) )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200556 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker1496d382011-05-23 12:07:29 +0000557 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000558#endif
559#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200560 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000561 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000562#else
563 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000564 ret = -1;
Rich Evansf90016a2015-01-19 14:26:37 +0000565 polarssl_printf("POLARSSL_CERTS_C not defined.");
Paul Bakker5690efc2011-05-26 13:16:06 +0000566 }
567#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000568 if( ret != 0 )
569 {
Rich Evansf90016a2015-01-19 14:26:37 +0000570 polarssl_printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000571 goto exit;
572 }
573
Rich Evansf90016a2015-01-19 14:26:37 +0000574 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000575
576 /*
577 * 2. Start the connection
578 */
Rich Evansf90016a2015-01-19 14:26:37 +0000579 polarssl_printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
Paul Bakker1496d382011-05-23 12:07:29 +0000580 opt.server_port );
581 fflush( stdout );
582
583 if( ( ret = net_connect( &server_fd, opt.server_name,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100584 opt.server_port, NET_PROTO_TCP ) ) != 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000585 {
Rich Evansf90016a2015-01-19 14:26:37 +0000586 polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000587 goto exit;
588 }
589
Rich Evansf90016a2015-01-19 14:26:37 +0000590 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000591
592 /*
593 * 3. Setup stuff
594 */
Rich Evansf90016a2015-01-19 14:26:37 +0000595 polarssl_printf( " . Setting up the SSL/TLS structure..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000596 fflush( stdout );
597
Paul Bakker1496d382011-05-23 12:07:29 +0000598 if( ( ret = ssl_init( &ssl ) ) != 0 )
599 {
Rich Evansf90016a2015-01-19 14:26:37 +0000600 polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000601 goto exit;
602 }
603
Rich Evansf90016a2015-01-19 14:26:37 +0000604 polarssl_printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000605
606 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100607 /* OPTIONAL is not optimal for security,
608 * but makes interop easier in this simplified example */
Paul Bakker1496d382011-05-23 12:07:29 +0000609 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
610
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100611 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
612 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100613 /* RC4 is deprecated, disable it */
614 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100615
Paul Bakker508ad5a2011-12-04 17:09:26 +0000616 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000617 ssl_set_dbg( &ssl, my_debug, stdout );
618 ssl_set_bio( &ssl, net_recv, &server_fd,
619 net_send, &server_fd );
620
Paul Bakker645ce3a2012-10-31 12:32:41 +0000621 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000622 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
623
Paul Bakker1496d382011-05-23 12:07:29 +0000624 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200625 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
626 {
Rich Evansf90016a2015-01-19 14:26:37 +0000627 polarssl_printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200628 goto exit;
629 }
Paul Bakker1496d382011-05-23 12:07:29 +0000630
Paul Bakker0be444a2013-08-27 21:55:01 +0200631#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200632 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
633 {
Rich Evansf90016a2015-01-19 14:26:37 +0000634 polarssl_printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200635 goto exit;
636 }
Paul Bakker0be444a2013-08-27 21:55:01 +0200637#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000638
639 if( opt.mode == MODE_SSL_TLS )
640 {
641 if( do_handshake( &ssl, &opt ) != 0 )
642 goto exit;
643
Rich Evansf90016a2015-01-19 14:26:37 +0000644 polarssl_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000645 fflush( stdout );
646
647 ret = write_ssl_and_get_response( &ssl, buf, 0 );
648 if( ret < 200 || ret > 299 )
649 {
Rich Evansf90016a2015-01-19 14:26:37 +0000650 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000651 goto exit;
652 }
653
Rich Evansf90016a2015-01-19 14:26:37 +0000654 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000655
Rich Evansf90016a2015-01-19 14:26:37 +0000656 polarssl_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000657 fflush( stdout );
658
659 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100660 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000661 ret = write_ssl_and_get_response( &ssl, buf, len );
662 if( ret < 200 || ret > 299 )
663 {
Rich Evansf90016a2015-01-19 14:26:37 +0000664 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000665 goto exit;
666 }
667 }
668 else
669 {
Rich Evansf90016a2015-01-19 14:26:37 +0000670 polarssl_printf( " > Get header from server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000671 fflush( stdout );
672
673 ret = write_and_get_response( server_fd, buf, 0 );
674 if( ret < 200 || ret > 299 )
675 {
Rich Evansf90016a2015-01-19 14:26:37 +0000676 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000677 goto exit;
678 }
679
Rich Evansf90016a2015-01-19 14:26:37 +0000680 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000681
Rich Evansf90016a2015-01-19 14:26:37 +0000682 polarssl_printf( " > Write EHLO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000683 fflush( stdout );
684
685 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100686 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000687 ret = write_and_get_response( server_fd, buf, len );
688 if( ret < 200 || ret > 299 )
689 {
Rich Evansf90016a2015-01-19 14:26:37 +0000690 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000691 goto exit;
692 }
693
Rich Evansf90016a2015-01-19 14:26:37 +0000694 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000695
Rich Evansf90016a2015-01-19 14:26:37 +0000696 polarssl_printf( " > Write STARTTLS to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000697 fflush( stdout );
698
699 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100700 len = sprintf( (char *) buf, "STARTTLS\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000701 ret = write_and_get_response( server_fd, buf, len );
702 if( ret < 200 || ret > 299 )
703 {
Rich Evansf90016a2015-01-19 14:26:37 +0000704 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000705 goto exit;
706 }
707
Rich Evansf90016a2015-01-19 14:26:37 +0000708 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000709
710 if( do_handshake( &ssl, &opt ) != 0 )
711 goto exit;
712 }
713
Paul Bakker5690efc2011-05-26 13:16:06 +0000714#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000715 if( opt.authentication )
716 {
Rich Evansf90016a2015-01-19 14:26:37 +0000717 polarssl_printf( " > Write AUTH LOGIN to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000718 fflush( stdout );
719
Paul Bakkerd75ba402014-01-24 16:11:17 +0100720 len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000721 ret = write_ssl_and_get_response( &ssl, buf, len );
722 if( ret < 200 || ret > 399 )
723 {
Rich Evansf90016a2015-01-19 14:26:37 +0000724 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000725 goto exit;
726 }
727
Rich Evansf90016a2015-01-19 14:26:37 +0000728 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000729
Rich Evansf90016a2015-01-19 14:26:37 +0000730 polarssl_printf( " > Write username to server: %s", opt.user_name );
Paul Bakker1496d382011-05-23 12:07:29 +0000731 fflush( stdout );
732
733 n = sizeof( buf );
Alfred Klomp7c034242014-07-14 22:11:13 +0200734 ret = base64_encode( base, &n, (const unsigned char *) opt.user_name,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200735 strlen( opt.user_name ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200736
737 if( ret != 0 ) {
Rich Evansf90016a2015-01-19 14:26:37 +0000738 polarssl_printf( " failed\n ! base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200739 goto exit;
740 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100741 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000742 ret = write_ssl_and_get_response( &ssl, buf, len );
743 if( ret < 300 || ret > 399 )
744 {
Rich Evansf90016a2015-01-19 14:26:37 +0000745 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000746 goto exit;
747 }
748
Rich Evansf90016a2015-01-19 14:26:37 +0000749 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000750
Rich Evansf90016a2015-01-19 14:26:37 +0000751 polarssl_printf( " > Write password to server: %s", opt.user_pwd );
Paul Bakker1496d382011-05-23 12:07:29 +0000752 fflush( stdout );
753
Alfred Klomp7c034242014-07-14 22:11:13 +0200754 ret = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200755 strlen( opt.user_pwd ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200756
757 if( ret != 0 ) {
Rich Evansf90016a2015-01-19 14:26:37 +0000758 polarssl_printf( " failed\n ! base64_encode returned %d\n\n", ret );
Alfred Klomp7c034242014-07-14 22:11:13 +0200759 goto exit;
760 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100761 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000762 ret = write_ssl_and_get_response( &ssl, buf, len );
763 if( ret < 200 || ret > 399 )
764 {
Rich Evansf90016a2015-01-19 14:26:37 +0000765 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000766 goto exit;
767 }
768
Rich Evansf90016a2015-01-19 14:26:37 +0000769 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000770 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000771#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000772
Rich Evansf90016a2015-01-19 14:26:37 +0000773 polarssl_printf( " > Write MAIL FROM to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000774 fflush( stdout );
775
Paul Bakkerd75ba402014-01-24 16:11:17 +0100776 len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
Paul Bakker1496d382011-05-23 12:07:29 +0000777 ret = write_ssl_and_get_response( &ssl, buf, len );
778 if( ret < 200 || ret > 299 )
779 {
Rich Evansf90016a2015-01-19 14:26:37 +0000780 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000781 goto exit;
782 }
783
Rich Evansf90016a2015-01-19 14:26:37 +0000784 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000785
Rich Evansf90016a2015-01-19 14:26:37 +0000786 polarssl_printf( " > Write RCPT TO to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000787 fflush( stdout );
788
Paul Bakkerd75ba402014-01-24 16:11:17 +0100789 len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
Paul Bakker1496d382011-05-23 12:07:29 +0000790 ret = write_ssl_and_get_response( &ssl, buf, len );
791 if( ret < 200 || ret > 299 )
792 {
Rich Evansf90016a2015-01-19 14:26:37 +0000793 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000794 goto exit;
795 }
796
Rich Evansf90016a2015-01-19 14:26:37 +0000797 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000798
Rich Evansf90016a2015-01-19 14:26:37 +0000799 polarssl_printf( " > Write DATA to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000800 fflush( stdout );
801
Paul Bakkerd75ba402014-01-24 16:11:17 +0100802 len = sprintf( (char *) buf, "DATA\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000803 ret = write_ssl_and_get_response( &ssl, buf, len );
804 if( ret < 300 || ret > 399 )
805 {
Rich Evansf90016a2015-01-19 14:26:37 +0000806 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000807 goto exit;
808 }
809
Rich Evansf90016a2015-01-19 14:26:37 +0000810 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000811
Rich Evansf90016a2015-01-19 14:26:37 +0000812 polarssl_printf( " > Write content to server:" );
Paul Bakker1496d382011-05-23 12:07:29 +0000813 fflush( stdout );
814
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000815 len = sprintf( (char *) buf, "From: %s\r\nSubject: mbed TLS Test mail\r\n\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000816 "This is a simple test mail from the "
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000817 "mbed TLS mail client example.\r\n"
Paul Bakkerd75ba402014-01-24 16:11:17 +0100818 "\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000819 "Enjoy!", opt.mail_from );
820 ret = write_ssl_data( &ssl, buf, len );
821
822 len = sprintf( (char *) buf, "\r\n.\r\n");
823 ret = write_ssl_and_get_response( &ssl, buf, len );
824 if( ret < 200 || ret > 299 )
825 {
Rich Evansf90016a2015-01-19 14:26:37 +0000826 polarssl_printf( " failed\n ! server responded with %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000827 goto exit;
828 }
829
Rich Evansf90016a2015-01-19 14:26:37 +0000830 polarssl_printf(" ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000831
832 ssl_close_notify( &ssl );
833
834exit:
835
836 if( server_fd )
837 net_close( server_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200838 x509_crt_free( &clicert );
839 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200840 pk_free( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000841 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200842 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200843 entropy_free( &entropy );
Paul Bakker1496d382011-05-23 12:07:29 +0000844
Paul Bakkercce9d772011-11-18 14:26:47 +0000845#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000846 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000847 fflush( stdout ); getchar();
848#endif
849
850 return( ret );
851}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000852#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
853 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
854 POLARSSL_CTR_DRBG_C */