blob: 03d66a65a09c2e30d7d2f3d4ce5127dad3440ab8 [file] [log] [blame]
Paul Bakker1496d382011-05-23 12:07:29 +00001/*
2 * SSL client for SMTP servers
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved
Paul Bakker1496d382011-05-23 12:07:29 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker1496d382011-05-23 12:07:29 +00007 *
Paul Bakker1496d382011-05-23 12:07:29 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker1496d382011-05-23 12:07:29 +000028
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010032
33#if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
Paul Bakker1496d382011-05-23 12:07:29 +000034#include <unistd.h>
Paul Bakkerfdda7852013-11-30 15:15:31 +010035#else
36#include <io.h>
37#define read _read
38#define write _write
39#endif
Paul Bakker1496d382011-05-23 12:07:29 +000040
Paul Bakker5a835222011-10-12 09:19:31 +000041#if defined(_WIN32) || defined(_WIN32_WCE)
42
43#include <winsock2.h>
44#include <windows.h>
45
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010046#if defined(_MSC_VER)
Paul Bakker5a835222011-10-12 09:19:31 +000047#if defined(_WIN32_WCE)
48#pragma comment( lib, "ws2.lib" )
49#else
50#pragma comment( lib, "ws2_32.lib" )
51#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010052#endif /* _MSC_VER */
Paul Bakker5a835222011-10-12 09:19:31 +000053#endif
54
Paul Bakker1496d382011-05-23 12:07:29 +000055#include "polarssl/base64.h"
56#include "polarssl/error.h"
57#include "polarssl/net.h"
58#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000059#include "polarssl/entropy.h"
60#include "polarssl/ctr_drbg.h"
Paul Bakker1496d382011-05-23 12:07:29 +000061#include "polarssl/certs.h"
62#include "polarssl/x509.h"
63
Paul Bakker9a97c5d2013-09-15 17:07:33 +020064#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
65 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
66 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020067 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020068int main( int argc, char *argv[] )
69{
70 ((void) argc);
71 ((void) argv);
72
73 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
74 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
75 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020076 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
Paul Bakker9a97c5d2013-09-15 17:07:33 +020077 "not defined.\n");
78 return( 0 );
79}
80#else
81
Paul Bakker1496d382011-05-23 12:07:29 +000082#define DFL_SERVER_NAME "localhost"
83#define DFL_SERVER_PORT 465
84#define DFL_USER_NAME "user"
85#define DFL_USER_PWD "password"
86#define DFL_MAIL_FROM ""
87#define DFL_MAIL_TO ""
88#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000089#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000090#define DFL_CRT_FILE ""
91#define DFL_KEY_FILE ""
92#define DFL_FORCE_CIPHER 0
93#define DFL_MODE 0
94#define DFL_AUTHENTICATION 0
95
96#define MODE_SSL_TLS 0
97#define MODE_STARTTLS 0
98
99/*
100 * global options
101 */
102struct options
103{
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200104 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +0000105 int server_port; /* port on which the ssl service runs */
106 int debug_level; /* level of debugging */
107 int authentication; /* if authentication is required */
108 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200109 const char *user_name; /* username to use for authentication */
110 const char *user_pwd; /* password to use for authentication */
111 const char *mail_from; /* E-Mail address to use as sender */
112 const char *mail_to; /* E-Mail address to use as recipient */
113 const char *ca_file; /* the file with the CA certificate(s) */
114 const char *crt_file; /* the file with the client certificate */
115 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000116 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
117} opt;
118
Paul Bakker3c5ef712013-06-25 16:37:45 +0200119static void my_debug( void *ctx, int level, const char *str )
Paul Bakker1496d382011-05-23 12:07:29 +0000120{
121 if( level < opt.debug_level )
122 {
123 fprintf( (FILE *) ctx, "%s", str );
124 fflush( (FILE *) ctx );
125 }
126}
127
Paul Bakker3c5ef712013-06-25 16:37:45 +0200128static int do_handshake( ssl_context *ssl, struct options *opt )
Paul Bakker1496d382011-05-23 12:07:29 +0000129{
130 int ret;
131 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000132 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000133
134 /*
135 * 4. Handshake
136 */
137 printf( " . Performing the SSL/TLS handshake..." );
138 fflush( stdout );
139
140 while( ( ret = ssl_handshake( ssl ) ) != 0 )
141 {
142 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
143 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000144#if defined(POLARSSL_ERROR_C)
Paul Bakker03a8a792013-06-30 12:18:08 +0200145 polarssl_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000146#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000147 printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
148 return( -1 );
149 }
150 }
151
152 printf( " ok\n [ Ciphersuite is %s ]\n",
153 ssl_get_ciphersuite( ssl ) );
154
155 /*
156 * 5. Verify the server certificate
157 */
158 printf( " . Verifying peer X.509 certificate..." );
159
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100160 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker1496d382011-05-23 12:07:29 +0000161 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
162 {
163 printf( " failed\n" );
164
165 if( ( ret & BADCERT_EXPIRED ) != 0 )
166 printf( " ! server certificate has expired\n" );
167
168 if( ( ret & BADCERT_REVOKED ) != 0 )
169 printf( " ! server certificate has been revoked\n" );
170
171 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
172 printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
173
174 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
175 printf( " ! self-signed or not signed by a trusted CA\n" );
176
177 printf( "\n" );
178 }
179 else
180 printf( " ok\n" );
181
182 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200183 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
184 ssl_get_peer_cert( ssl ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000185 printf( "%s\n", buf );
186
187 return( 0 );
188}
189
Paul Bakker3c5ef712013-06-25 16:37:45 +0200190static int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000191{
192 int ret;
193
194 printf("\n%s", buf);
195 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
196 {
197 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
198 {
199 printf( " failed\n ! ssl_write returned %d\n\n", ret );
200 return -1;
201 }
202 }
203
204 return( 0 );
205}
206
Paul Bakker3c5ef712013-06-25 16:37:45 +0200207static int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000208{
209 int ret;
210 unsigned char data[128];
211 char code[4];
212 size_t i, idx = 0;
213
214 printf("\n%s", buf);
215 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
216 {
217 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
218 {
219 printf( " failed\n ! ssl_write returned %d\n\n", ret );
220 return -1;
221 }
222 }
223
224 do
225 {
226 len = sizeof( data ) - 1;
227 memset( data, 0, sizeof( data ) );
228 ret = ssl_read( ssl, data, len );
229
230 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
231 continue;
232
233 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
234 return -1;
235
236 if( ret <= 0 )
237 {
238 printf( "failed\n ! ssl_read returned %d\n\n", ret );
239 return -1;
240 }
241
242 printf("\n%s", data);
243 len = ret;
244 for( i = 0; i < len; i++ )
245 {
246 if( data[i] != '\n' )
247 {
248 if( idx < 4 )
249 code[ idx++ ] = data[i];
250 continue;
251 }
252
253 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
254 {
255 code[3] = '\0';
256 return atoi( code );
257 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200258
Paul Bakker1496d382011-05-23 12:07:29 +0000259 idx = 0;
260 }
261 }
262 while( 1 );
263}
264
Paul Bakker3c5ef712013-06-25 16:37:45 +0200265static int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000266{
267 int ret;
268 unsigned char data[128];
269 char code[4];
270 size_t i, idx = 0;
271
272 printf("\n%s", buf);
273 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
274 {
275 printf( " failed\n ! ssl_write returned %d\n\n", ret );
276 return -1;
277 }
278
279 do
280 {
281 len = sizeof( data ) - 1;
282 memset( data, 0, sizeof( data ) );
283 ret = read( sock_fd, data, len );
284
285 if( ret <= 0 )
286 {
287 printf( "failed\n ! read returned %d\n\n", ret );
288 return -1;
289 }
290
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200291 data[len] = '\0';
Paul Bakker1496d382011-05-23 12:07:29 +0000292 printf("\n%s", data);
293 len = ret;
294 for( i = 0; i < len; i++ )
295 {
296 if( data[i] != '\n' )
297 {
298 if( idx < 4 )
299 code[ idx++ ] = data[i];
300 continue;
301 }
302
303 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
304 {
305 code[3] = '\0';
306 return atoi( code );
307 }
308
309 idx = 0;
310 }
311 }
312 while( 1 );
313}
314
Paul Bakker5690efc2011-05-26 13:16:06 +0000315#if defined(POLARSSL_BASE64_C)
316#define USAGE_AUTH \
317 " authentication=%%d default: 0 (disabled)\n" \
318 " user_name=%%s default: \"user\"\n" \
319 " user_pwd=%%s default: \"password\"\n"
320#else
321#define USAGE_AUTH \
322 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
323#endif /* POLARSSL_BASE64_C */
324
325#if defined(POLARSSL_FS_IO)
326#define USAGE_IO \
327 " ca_file=%%s default: \"\" (pre-loaded)\n" \
328 " crt_file=%%s default: \"\" (pre-loaded)\n" \
329 " key_file=%%s default: \"\" (pre-loaded)\n"
330#else
331#define USAGE_IO \
332 " No file operations available (POLARSSL_FS_IO not defined)\n"
333#endif /* POLARSSL_FS_IO */
334
Paul Bakker1496d382011-05-23 12:07:29 +0000335#define USAGE \
336 "\n usage: ssl_mail_client param=<>...\n" \
337 "\n acceptable parameters:\n" \
338 " server_name=%%s default: localhost\n" \
339 " server_port=%%d default: 4433\n" \
340 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000341 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000342 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000343 " mail_from=%%s default: \"\"\n" \
344 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000345 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000346 " force_ciphersuite=<name> default: all enabled\n"\
347 " acceptable ciphersuite names:\n"
348
349int main( int argc, char *argv[] )
350{
351 int ret = 0, len, server_fd;
Paul Bakker09c9dd82014-08-18 11:06:56 +0200352 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000353#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000354 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000355#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000356 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200357 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000358
359 entropy_context entropy;
360 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000361 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200362 x509_crt cacert;
363 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200364 pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000365 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000366 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000367 char *p, *q;
368 const int *list;
369
370 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200371 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000372 */
373 server_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200374 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker333fdec2014-08-04 12:12:09 +0200375 memset( &buf, 0, sizeof( buf ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200376 x509_crt_init( &cacert );
377 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200378 pk_init( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000379
380 if( argc == 0 )
381 {
382 usage:
383 printf( USAGE );
384
385 list = ssl_list_ciphersuites();
386 while( *list )
387 {
388 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
389 list++;
390 }
391 printf("\n");
392 goto exit;
393 }
394
395 opt.server_name = DFL_SERVER_NAME;
396 opt.server_port = DFL_SERVER_PORT;
397 opt.debug_level = DFL_DEBUG_LEVEL;
398 opt.authentication = DFL_AUTHENTICATION;
399 opt.mode = DFL_MODE;
400 opt.user_name = DFL_USER_NAME;
401 opt.user_pwd = DFL_USER_PWD;
402 opt.mail_from = DFL_MAIL_FROM;
403 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000404 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000405 opt.crt_file = DFL_CRT_FILE;
406 opt.key_file = DFL_KEY_FILE;
407 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
408
409 for( i = 1; i < argc; i++ )
410 {
Paul Bakker1496d382011-05-23 12:07:29 +0000411 p = argv[i];
412 if( ( q = strchr( p, '=' ) ) == NULL )
413 goto usage;
414 *q++ = '\0';
415
416 if( strcmp( p, "server_name" ) == 0 )
417 opt.server_name = q;
418 else if( strcmp( p, "server_port" ) == 0 )
419 {
420 opt.server_port = atoi( q );
421 if( opt.server_port < 1 || opt.server_port > 65535 )
422 goto usage;
423 }
424 else if( strcmp( p, "debug_level" ) == 0 )
425 {
426 opt.debug_level = atoi( q );
427 if( opt.debug_level < 0 || opt.debug_level > 65535 )
428 goto usage;
429 }
430 else if( strcmp( p, "authentication" ) == 0 )
431 {
432 opt.authentication = atoi( q );
433 if( opt.authentication < 0 || opt.authentication > 1 )
434 goto usage;
435 }
436 else if( strcmp( p, "mode" ) == 0 )
437 {
438 opt.mode = atoi( q );
439 if( opt.mode < 0 || opt.mode > 1 )
440 goto usage;
441 }
442 else if( strcmp( p, "user_name" ) == 0 )
443 opt.user_name = q;
444 else if( strcmp( p, "user_pwd" ) == 0 )
445 opt.user_pwd = q;
446 else if( strcmp( p, "mail_from" ) == 0 )
447 opt.mail_from = q;
448 else if( strcmp( p, "mail_to" ) == 0 )
449 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000450 else if( strcmp( p, "ca_file" ) == 0 )
451 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000452 else if( strcmp( p, "crt_file" ) == 0 )
453 opt.crt_file = q;
454 else if( strcmp( p, "key_file" ) == 0 )
455 opt.key_file = q;
456 else if( strcmp( p, "force_ciphersuite" ) == 0 )
457 {
458 opt.force_ciphersuite[0] = -1;
459
460 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
461
462 if( opt.force_ciphersuite[0] <= 0 )
463 goto usage;
464
465 opt.force_ciphersuite[1] = 0;
466 }
467 else
468 goto usage;
469 }
470
471 /*
472 * 0. Initialize the RNG and the session data
473 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000474 printf( "\n . Seeding the random number generator..." );
475 fflush( stdout );
476
477 entropy_init( &entropy );
478 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200479 (const unsigned char *) pers,
480 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000481 {
482 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
483 goto exit;
484 }
485
486 printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000487
488 /*
489 * 1.1. Load the trusted CA
490 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000491 printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000492 fflush( stdout );
493
Paul Bakker5690efc2011-05-26 13:16:06 +0000494#if defined(POLARSSL_FS_IO)
495 if( strlen( opt.ca_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200496 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000497 else
498#endif
499#if defined(POLARSSL_CERTS_C)
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200500 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list,
501 strlen( test_ca_list ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000502#else
503 {
504 ret = 1;
505 printf("POLARSSL_CERTS_C not defined.");
506 }
507#endif
Paul Bakker42488232012-05-16 08:21:05 +0000508 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000509 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200510 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000511 goto exit;
512 }
513
Paul Bakker42488232012-05-16 08:21:05 +0000514 printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000515
516 /*
517 * 1.2. Load own certificate and private key
518 *
519 * (can be skipped if client authentication is not required)
520 */
521 printf( " . Loading the client cert. and key..." );
522 fflush( stdout );
523
Paul Bakker5690efc2011-05-26 13:16:06 +0000524#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000525 if( strlen( opt.crt_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200526 ret = x509_crt_parse_file( &clicert, opt.crt_file );
527 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000528#endif
529#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200530 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
531 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000532#else
533 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000534 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000535 printf("POLARSSL_CERTS_C not defined.");
536 }
537#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000538 if( ret != 0 )
539 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200540 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000541 goto exit;
542 }
543
Paul Bakker5690efc2011-05-26 13:16:06 +0000544#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000545 if( strlen( opt.key_file ) )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200546 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker1496d382011-05-23 12:07:29 +0000547 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000548#endif
549#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200550 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000551 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000552#else
553 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000554 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000555 printf("POLARSSL_CERTS_C not defined.");
556 }
557#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000558 if( ret != 0 )
559 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200560 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000561 goto exit;
562 }
563
564 printf( " ok\n" );
565
566 /*
567 * 2. Start the connection
568 */
569 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
570 opt.server_port );
571 fflush( stdout );
572
573 if( ( ret = net_connect( &server_fd, opt.server_name,
574 opt.server_port ) ) != 0 )
575 {
576 printf( " failed\n ! net_connect returned %d\n\n", ret );
577 goto exit;
578 }
579
580 printf( " ok\n" );
581
582 /*
583 * 3. Setup stuff
584 */
585 printf( " . Setting up the SSL/TLS structure..." );
586 fflush( stdout );
587
Paul Bakker1496d382011-05-23 12:07:29 +0000588 if( ( ret = ssl_init( &ssl ) ) != 0 )
589 {
590 printf( " failed\n ! ssl_init returned %d\n\n", ret );
591 goto exit;
592 }
593
594 printf( " ok\n" );
595
596 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnardfcf2fc22014-03-11 11:10:27 +0100597 /* OPTIONAL is not optimal for security,
598 * but makes interop easier in this simplified example */
Paul Bakker1496d382011-05-23 12:07:29 +0000599 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
600
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100601 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
602 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100603 /* RC4 is deprecated, disable it */
604 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100605
Paul Bakker508ad5a2011-12-04 17:09:26 +0000606 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000607 ssl_set_dbg( &ssl, my_debug, stdout );
608 ssl_set_bio( &ssl, net_recv, &server_fd,
609 net_send, &server_fd );
610
Paul Bakker645ce3a2012-10-31 12:32:41 +0000611 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000612 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
613
Paul Bakker1496d382011-05-23 12:07:29 +0000614 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200615 if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 )
616 {
617 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
618 goto exit;
619 }
Paul Bakker1496d382011-05-23 12:07:29 +0000620
Paul Bakker0be444a2013-08-27 21:55:01 +0200621#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200622 if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
623 {
624 printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret );
625 goto exit;
626 }
Paul Bakker0be444a2013-08-27 21:55:01 +0200627#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000628
629 if( opt.mode == MODE_SSL_TLS )
630 {
631 if( do_handshake( &ssl, &opt ) != 0 )
632 goto exit;
633
634 printf( " > Get header from server:" );
635 fflush( stdout );
636
637 ret = write_ssl_and_get_response( &ssl, buf, 0 );
638 if( ret < 200 || ret > 299 )
639 {
640 printf( " failed\n ! server responded with %d\n\n", ret );
641 goto exit;
642 }
643
644 printf(" ok\n" );
645
646 printf( " > Write EHLO to server:" );
647 fflush( stdout );
648
649 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100650 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000651 ret = write_ssl_and_get_response( &ssl, buf, len );
652 if( ret < 200 || ret > 299 )
653 {
654 printf( " failed\n ! server responded with %d\n\n", ret );
655 goto exit;
656 }
657 }
658 else
659 {
660 printf( " > Get header from server:" );
661 fflush( stdout );
662
663 ret = write_and_get_response( server_fd, buf, 0 );
664 if( ret < 200 || ret > 299 )
665 {
666 printf( " failed\n ! server responded with %d\n\n", ret );
667 goto exit;
668 }
669
670 printf(" ok\n" );
671
672 printf( " > Write EHLO to server:" );
673 fflush( stdout );
674
675 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100676 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000677 ret = write_and_get_response( server_fd, buf, len );
678 if( ret < 200 || ret > 299 )
679 {
680 printf( " failed\n ! server responded with %d\n\n", ret );
681 goto exit;
682 }
683
684 printf(" ok\n" );
685
686 printf( " > Write STARTTLS to server:" );
687 fflush( stdout );
688
689 gethostname( hostname, 32 );
Paul Bakkerd75ba402014-01-24 16:11:17 +0100690 len = sprintf( (char *) buf, "STARTTLS\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000691 ret = write_and_get_response( server_fd, buf, len );
692 if( ret < 200 || ret > 299 )
693 {
694 printf( " failed\n ! server responded with %d\n\n", ret );
695 goto exit;
696 }
697
698 printf(" ok\n" );
699
700 if( do_handshake( &ssl, &opt ) != 0 )
701 goto exit;
702 }
703
Paul Bakker5690efc2011-05-26 13:16:06 +0000704#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000705 if( opt.authentication )
706 {
707 printf( " > Write AUTH LOGIN to server:" );
708 fflush( stdout );
709
Paul Bakkerd75ba402014-01-24 16:11:17 +0100710 len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000711 ret = write_ssl_and_get_response( &ssl, buf, len );
712 if( ret < 200 || ret > 399 )
713 {
714 printf( " failed\n ! server responded with %d\n\n", ret );
715 goto exit;
716 }
717
718 printf(" ok\n" );
719
720 printf( " > Write username to server: %s", opt.user_name );
721 fflush( stdout );
722
723 n = sizeof( buf );
Alfred Klomp7c034242014-07-14 22:11:13 +0200724 ret = base64_encode( base, &n, (const unsigned char *) opt.user_name,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200725 strlen( opt.user_name ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200726
727 if( ret != 0 ) {
728 printf( " failed\n ! base64_encode returned %d\n\n", ret );
729 goto exit;
730 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100731 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000732 ret = write_ssl_and_get_response( &ssl, buf, len );
733 if( ret < 300 || ret > 399 )
734 {
735 printf( " failed\n ! server responded with %d\n\n", ret );
736 goto exit;
737 }
738
739 printf(" ok\n" );
740
741 printf( " > Write password to server: %s", opt.user_pwd );
742 fflush( stdout );
743
Alfred Klomp7c034242014-07-14 22:11:13 +0200744 ret = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200745 strlen( opt.user_pwd ) );
Alfred Klomp7c034242014-07-14 22:11:13 +0200746
747 if( ret != 0 ) {
748 printf( " failed\n ! base64_encode returned %d\n\n", ret );
749 goto exit;
750 }
Paul Bakkerd75ba402014-01-24 16:11:17 +0100751 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000752 ret = write_ssl_and_get_response( &ssl, buf, len );
753 if( ret < 200 || ret > 399 )
754 {
755 printf( " failed\n ! server responded with %d\n\n", ret );
756 goto exit;
757 }
758
759 printf(" ok\n" );
760 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000761#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000762
763 printf( " > Write MAIL FROM to server:" );
764 fflush( stdout );
765
Paul Bakkerd75ba402014-01-24 16:11:17 +0100766 len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
Paul Bakker1496d382011-05-23 12:07:29 +0000767 ret = write_ssl_and_get_response( &ssl, buf, len );
768 if( ret < 200 || ret > 299 )
769 {
770 printf( " failed\n ! server responded with %d\n\n", ret );
771 goto exit;
772 }
773
774 printf(" ok\n" );
775
776 printf( " > Write RCPT TO to server:" );
777 fflush( stdout );
778
Paul Bakkerd75ba402014-01-24 16:11:17 +0100779 len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
Paul Bakker1496d382011-05-23 12:07:29 +0000780 ret = write_ssl_and_get_response( &ssl, buf, len );
781 if( ret < 200 || ret > 299 )
782 {
783 printf( " failed\n ! server responded with %d\n\n", ret );
784 goto exit;
785 }
786
787 printf(" ok\n" );
788
789 printf( " > Write DATA to server:" );
790 fflush( stdout );
791
Paul Bakkerd75ba402014-01-24 16:11:17 +0100792 len = sprintf( (char *) buf, "DATA\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000793 ret = write_ssl_and_get_response( &ssl, buf, len );
794 if( ret < 300 || ret > 399 )
795 {
796 printf( " failed\n ! server responded with %d\n\n", ret );
797 goto exit;
798 }
799
800 printf(" ok\n" );
801
802 printf( " > Write content to server:" );
803 fflush( stdout );
804
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000805 len = sprintf( (char *) buf, "From: %s\r\nSubject: mbed TLS Test mail\r\n\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000806 "This is a simple test mail from the "
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +0000807 "mbed TLS mail client example.\r\n"
Paul Bakkerd75ba402014-01-24 16:11:17 +0100808 "\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000809 "Enjoy!", opt.mail_from );
810 ret = write_ssl_data( &ssl, buf, len );
811
812 len = sprintf( (char *) buf, "\r\n.\r\n");
813 ret = write_ssl_and_get_response( &ssl, buf, len );
814 if( ret < 200 || ret > 299 )
815 {
816 printf( " failed\n ! server responded with %d\n\n", ret );
817 goto exit;
818 }
819
820 printf(" ok\n" );
821
822 ssl_close_notify( &ssl );
823
824exit:
825
826 if( server_fd )
827 net_close( server_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200828 x509_crt_free( &clicert );
829 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200830 pk_free( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000831 ssl_free( &ssl );
Paul Bakkera317a982014-06-18 16:44:11 +0200832 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200833 entropy_free( &entropy );
Paul Bakker1496d382011-05-23 12:07:29 +0000834
Paul Bakkercce9d772011-11-18 14:26:47 +0000835#if defined(_WIN32)
Paul Bakker1496d382011-05-23 12:07:29 +0000836 printf( " + Press Enter to exit this program.\n" );
837 fflush( stdout ); getchar();
838#endif
839
840 return( ret );
841}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000842#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
843 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
844 POLARSSL_CTR_DRBG_C */