blob: fb2f822d207cfb5645326425b6f5c4dc9854fc76 [file] [log] [blame]
Paul Bakker1496d382011-05-23 12:07:29 +00001/*
2 * SSL client for SMTP servers
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakker1496d382011-05-23 12:07:29 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
Paul Bakker238be3a2014-07-07 14:55:07 +020033
34#if !defined(_MSC_VER)
Paul Bakker1496d382011-05-23 12:07:29 +000035#include <unistd.h>
Paul Bakker238be3a2014-07-07 14:55:07 +020036#else
37#include <io.h>
38#define read _read
39#define write _write
40#endif
Paul Bakker1496d382011-05-23 12:07:29 +000041
Paul Bakker5a835222011-10-12 09:19:31 +000042#if defined(_WIN32) || defined(_WIN32_WCE)
43
44#include <winsock2.h>
45#include <windows.h>
46
47#if defined(_WIN32_WCE)
48#pragma comment( lib, "ws2.lib" )
49#else
50#pragma comment( lib, "ws2_32.lib" )
51#endif
52#endif
53
Paul Bakker5690efc2011-05-26 13:16:06 +000054#include "polarssl/config.h"
55
Paul Bakker1496d382011-05-23 12:07:29 +000056#include "polarssl/base64.h"
57#include "polarssl/error.h"
58#include "polarssl/net.h"
59#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000060#include "polarssl/entropy.h"
61#include "polarssl/ctr_drbg.h"
Paul Bakker1496d382011-05-23 12:07:29 +000062#include "polarssl/certs.h"
63#include "polarssl/x509.h"
64
65#define DFL_SERVER_NAME "localhost"
66#define DFL_SERVER_PORT 465
67#define DFL_USER_NAME "user"
68#define DFL_USER_PWD "password"
69#define DFL_MAIL_FROM ""
70#define DFL_MAIL_TO ""
71#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000072#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000073#define DFL_CRT_FILE ""
74#define DFL_KEY_FILE ""
75#define DFL_FORCE_CIPHER 0
76#define DFL_MODE 0
77#define DFL_AUTHENTICATION 0
78
79#define MODE_SSL_TLS 0
80#define MODE_STARTTLS 0
81
82/*
83 * global options
84 */
85struct options
86{
Paul Bakkere0225e42013-06-06 12:52:24 +020087 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +000088 int server_port; /* port on which the ssl service runs */
89 int debug_level; /* level of debugging */
90 int authentication; /* if authentication is required */
91 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkere0225e42013-06-06 12:52:24 +020092 const char *user_name; /* username to use for authentication */
93 const char *user_pwd; /* password to use for authentication */
94 const char *mail_from; /* E-Mail address to use as sender */
95 const char *mail_to; /* E-Mail address to use as recipient */
96 const char *ca_file; /* the file with the CA certificate(s) */
97 const char *crt_file; /* the file with the client certificate */
98 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +000099 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
100} opt;
101
102void my_debug( void *ctx, int level, const char *str )
103{
104 if( level < opt.debug_level )
105 {
106 fprintf( (FILE *) ctx, "%s", str );
107 fflush( (FILE *) ctx );
108 }
109}
110
Paul Bakker508ad5a2011-12-04 17:09:26 +0000111#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +0000112 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +0000113 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
114 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +0000115int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000116{
Paul Bakkercce9d772011-11-18 14:26:47 +0000117 ((void) argc);
118 ((void) argv);
119
Paul Bakker508ad5a2011-12-04 17:09:26 +0000120 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +0000121 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +0000122 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
123 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +0000124 return( 0 );
125}
126#else
Paul Bakker1496d382011-05-23 12:07:29 +0000127int do_handshake( ssl_context *ssl, struct options *opt )
128{
129 int ret;
130 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000131 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000132
133 /*
134 * 4. Handshake
135 */
136 printf( " . Performing the SSL/TLS handshake..." );
137 fflush( stdout );
138
139 while( ( ret = ssl_handshake( ssl ) ) != 0 )
140 {
141 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
142 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000143#if defined(POLARSSL_ERROR_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000144 error_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000145#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000146 printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
147 return( -1 );
148 }
149 }
150
151 printf( " ok\n [ Ciphersuite is %s ]\n",
152 ssl_get_ciphersuite( ssl ) );
153
154 /*
155 * 5. Verify the server certificate
156 */
157 printf( " . Verifying peer X.509 certificate..." );
158
159 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
160 {
161 printf( " failed\n" );
162
163 if( ( ret & BADCERT_EXPIRED ) != 0 )
164 printf( " ! server certificate has expired\n" );
165
166 if( ( ret & BADCERT_REVOKED ) != 0 )
167 printf( " ! server certificate has been revoked\n" );
168
169 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
170 printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
171
172 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
173 printf( " ! self-signed or not signed by a trusted CA\n" );
174
175 printf( "\n" );
176 }
177 else
178 printf( " ok\n" );
179
180 printf( " . Peer certificate information ...\n" );
Paul Bakker48916f92012-09-16 19:57:18 +0000181 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakker645ce3a2012-10-31 12:32:41 +0000182 ssl_get_peer_cert( ssl ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000183 printf( "%s\n", buf );
184
185 return( 0 );
186}
187
188int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
189{
190 int ret;
191
192 printf("\n%s", buf);
193 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
194 {
195 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
196 {
197 printf( " failed\n ! ssl_write returned %d\n\n", ret );
198 return -1;
199 }
200 }
201
202 return( 0 );
203}
204
205int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
206{
207 int ret;
208 unsigned char data[128];
209 char code[4];
210 size_t i, idx = 0;
211
212 printf("\n%s", buf);
213 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
214 {
215 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
216 {
217 printf( " failed\n ! ssl_write returned %d\n\n", ret );
218 return -1;
219 }
220 }
221
222 do
223 {
224 len = sizeof( data ) - 1;
225 memset( data, 0, sizeof( data ) );
226 ret = ssl_read( ssl, data, len );
227
228 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
229 continue;
230
231 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
232 return -1;
233
234 if( ret <= 0 )
235 {
236 printf( "failed\n ! ssl_read returned %d\n\n", ret );
237 return -1;
238 }
239
240 printf("\n%s", data);
241 len = ret;
242 for( i = 0; i < len; i++ )
243 {
244 if( data[i] != '\n' )
245 {
246 if( idx < 4 )
247 code[ idx++ ] = data[i];
248 continue;
249 }
250
251 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
252 {
253 code[3] = '\0';
254 return atoi( code );
255 }
256
257 idx = 0;
258 }
259 }
260 while( 1 );
261}
262
263int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
264{
265 int ret;
266 unsigned char data[128];
267 char code[4];
268 size_t i, idx = 0;
269
270 printf("\n%s", buf);
271 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
272 {
273 printf( " failed\n ! ssl_write returned %d\n\n", ret );
274 return -1;
275 }
276
277 do
278 {
279 len = sizeof( data ) - 1;
280 memset( data, 0, sizeof( data ) );
281 ret = read( sock_fd, data, len );
282
283 if( ret <= 0 )
284 {
285 printf( "failed\n ! read returned %d\n\n", ret );
286 return -1;
287 }
288
289 printf("\n%s", data);
290 len = ret;
291 for( i = 0; i < len; i++ )
292 {
293 if( data[i] != '\n' )
294 {
295 if( idx < 4 )
296 code[ idx++ ] = data[i];
297 continue;
298 }
299
300 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
301 {
302 code[3] = '\0';
303 return atoi( code );
304 }
305
306 idx = 0;
307 }
308 }
309 while( 1 );
310}
311
Paul Bakker5690efc2011-05-26 13:16:06 +0000312#if defined(POLARSSL_BASE64_C)
313#define USAGE_AUTH \
314 " authentication=%%d default: 0 (disabled)\n" \
315 " user_name=%%s default: \"user\"\n" \
316 " user_pwd=%%s default: \"password\"\n"
317#else
318#define USAGE_AUTH \
319 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
320#endif /* POLARSSL_BASE64_C */
321
322#if defined(POLARSSL_FS_IO)
323#define USAGE_IO \
324 " ca_file=%%s default: \"\" (pre-loaded)\n" \
325 " crt_file=%%s default: \"\" (pre-loaded)\n" \
326 " key_file=%%s default: \"\" (pre-loaded)\n"
327#else
328#define USAGE_IO \
329 " No file operations available (POLARSSL_FS_IO not defined)\n"
330#endif /* POLARSSL_FS_IO */
331
Paul Bakker1496d382011-05-23 12:07:29 +0000332#define USAGE \
333 "\n usage: ssl_mail_client param=<>...\n" \
334 "\n acceptable parameters:\n" \
335 " server_name=%%s default: localhost\n" \
336 " server_port=%%d default: 4433\n" \
337 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000338 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000339 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000340 " mail_from=%%s default: \"\"\n" \
341 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000342 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000343 " force_ciphersuite=<name> default: all enabled\n"\
344 " acceptable ciphersuite names:\n"
345
346int main( int argc, char *argv[] )
347{
348 int ret = 0, len, server_fd;
349 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000350#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000351 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000352#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000353 char hostname[32];
Paul Bakkere0225e42013-06-06 12:52:24 +0200354 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000355
356 entropy_context entropy;
357 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000358 ssl_context ssl;
Paul Bakker1496d382011-05-23 12:07:29 +0000359 x509_cert cacert;
360 x509_cert clicert;
361 rsa_context rsa;
362 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000363 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000364 char *p, *q;
365 const int *list;
366
367 /*
368 * Make sure memory references are valid.
369 */
370 server_fd = 0;
Paul Bakker1496d382011-05-23 12:07:29 +0000371 memset( &cacert, 0, sizeof( x509_cert ) );
372 memset( &clicert, 0, sizeof( x509_cert ) );
373 memset( &rsa, 0, sizeof( rsa_context ) );
374
375 if( argc == 0 )
376 {
377 usage:
378 printf( USAGE );
379
380 list = ssl_list_ciphersuites();
381 while( *list )
382 {
383 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
384 list++;
385 }
386 printf("\n");
387 goto exit;
388 }
389
390 opt.server_name = DFL_SERVER_NAME;
391 opt.server_port = DFL_SERVER_PORT;
392 opt.debug_level = DFL_DEBUG_LEVEL;
393 opt.authentication = DFL_AUTHENTICATION;
394 opt.mode = DFL_MODE;
395 opt.user_name = DFL_USER_NAME;
396 opt.user_pwd = DFL_USER_PWD;
397 opt.mail_from = DFL_MAIL_FROM;
398 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000399 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000400 opt.crt_file = DFL_CRT_FILE;
401 opt.key_file = DFL_KEY_FILE;
402 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
403
404 for( i = 1; i < argc; i++ )
405 {
Paul Bakker1496d382011-05-23 12:07:29 +0000406 p = argv[i];
407 if( ( q = strchr( p, '=' ) ) == NULL )
408 goto usage;
409 *q++ = '\0';
410
411 if( strcmp( p, "server_name" ) == 0 )
412 opt.server_name = q;
413 else if( strcmp( p, "server_port" ) == 0 )
414 {
415 opt.server_port = atoi( q );
416 if( opt.server_port < 1 || opt.server_port > 65535 )
417 goto usage;
418 }
419 else if( strcmp( p, "debug_level" ) == 0 )
420 {
421 opt.debug_level = atoi( q );
422 if( opt.debug_level < 0 || opt.debug_level > 65535 )
423 goto usage;
424 }
425 else if( strcmp( p, "authentication" ) == 0 )
426 {
427 opt.authentication = atoi( q );
428 if( opt.authentication < 0 || opt.authentication > 1 )
429 goto usage;
430 }
431 else if( strcmp( p, "mode" ) == 0 )
432 {
433 opt.mode = atoi( q );
434 if( opt.mode < 0 || opt.mode > 1 )
435 goto usage;
436 }
437 else if( strcmp( p, "user_name" ) == 0 )
438 opt.user_name = q;
439 else if( strcmp( p, "user_pwd" ) == 0 )
440 opt.user_pwd = q;
441 else if( strcmp( p, "mail_from" ) == 0 )
442 opt.mail_from = q;
443 else if( strcmp( p, "mail_to" ) == 0 )
444 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000445 else if( strcmp( p, "ca_file" ) == 0 )
446 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000447 else if( strcmp( p, "crt_file" ) == 0 )
448 opt.crt_file = q;
449 else if( strcmp( p, "key_file" ) == 0 )
450 opt.key_file = q;
451 else if( strcmp( p, "force_ciphersuite" ) == 0 )
452 {
453 opt.force_ciphersuite[0] = -1;
454
455 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
456
457 if( opt.force_ciphersuite[0] <= 0 )
458 goto usage;
459
460 opt.force_ciphersuite[1] = 0;
461 }
462 else
463 goto usage;
464 }
465
466 /*
467 * 0. Initialize the RNG and the session data
468 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000469 printf( "\n . Seeding the random number generator..." );
470 fflush( stdout );
471
472 entropy_init( &entropy );
473 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +0200474 (const unsigned char *) pers,
475 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000476 {
477 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
478 goto exit;
479 }
480
481 printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000482
483 /*
484 * 1.1. Load the trusted CA
485 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000486 printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000487 fflush( stdout );
488
Paul Bakker5690efc2011-05-26 13:16:06 +0000489#if defined(POLARSSL_FS_IO)
490 if( strlen( opt.ca_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000491 ret = x509parse_crtfile( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000492 else
493#endif
494#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200495 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000496 strlen( test_ca_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000497#else
498 {
499 ret = 1;
500 printf("POLARSSL_CERTS_C not defined.");
501 }
502#endif
Paul Bakker42488232012-05-16 08:21:05 +0000503 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000504 {
505 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
506 goto exit;
507 }
508
Paul Bakker42488232012-05-16 08:21:05 +0000509 printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000510
511 /*
512 * 1.2. Load own certificate and private key
513 *
514 * (can be skipped if client authentication is not required)
515 */
516 printf( " . Loading the client cert. and key..." );
517 fflush( stdout );
518
Paul Bakker5690efc2011-05-26 13:16:06 +0000519#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000520 if( strlen( opt.crt_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000521 ret = x509parse_crtfile( &clicert, opt.crt_file );
Paul Bakker1496d382011-05-23 12:07:29 +0000522 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000523#endif
524#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200525 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000526 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000527#else
528 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000529 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000530 printf("POLARSSL_CERTS_C not defined.");
531 }
532#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000533 if( ret != 0 )
534 {
535 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
536 goto exit;
537 }
538
Paul Bakker5690efc2011-05-26 13:16:06 +0000539#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000540 if( strlen( opt.key_file ) )
541 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
542 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000543#endif
544#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200545 ret = x509parse_key( &rsa, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000546 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000547#else
548 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000549 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000550 printf("POLARSSL_CERTS_C not defined.");
551 }
552#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000553 if( ret != 0 )
554 {
555 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
556 goto exit;
557 }
558
559 printf( " ok\n" );
560
561 /*
562 * 2. Start the connection
563 */
564 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
565 opt.server_port );
566 fflush( stdout );
567
568 if( ( ret = net_connect( &server_fd, opt.server_name,
569 opt.server_port ) ) != 0 )
570 {
571 printf( " failed\n ! net_connect returned %d\n\n", ret );
572 goto exit;
573 }
574
575 printf( " ok\n" );
576
577 /*
578 * 3. Setup stuff
579 */
580 printf( " . Setting up the SSL/TLS structure..." );
581 fflush( stdout );
582
Paul Bakker1496d382011-05-23 12:07:29 +0000583 if( ( ret = ssl_init( &ssl ) ) != 0 )
584 {
585 printf( " failed\n ! ssl_init returned %d\n\n", ret );
586 goto exit;
587 }
588
589 printf( " ok\n" );
590
591 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
592 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
593
Paul Bakker508ad5a2011-12-04 17:09:26 +0000594 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000595 ssl_set_dbg( &ssl, my_debug, stdout );
596 ssl_set_bio( &ssl, net_recv, &server_fd,
597 net_send, &server_fd );
598
Paul Bakker645ce3a2012-10-31 12:32:41 +0000599 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000600 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
601
Paul Bakker1496d382011-05-23 12:07:29 +0000602 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
603 ssl_set_own_cert( &ssl, &clicert, &rsa );
604
605 ssl_set_hostname( &ssl, opt.server_name );
606
607 if( opt.mode == MODE_SSL_TLS )
608 {
609 if( do_handshake( &ssl, &opt ) != 0 )
610 goto exit;
611
612 printf( " > Get header from server:" );
613 fflush( stdout );
614
615 ret = write_ssl_and_get_response( &ssl, buf, 0 );
616 if( ret < 200 || ret > 299 )
617 {
618 printf( " failed\n ! server responded with %d\n\n", ret );
619 goto exit;
620 }
621
622 printf(" ok\n" );
623
624 printf( " > Write EHLO to server:" );
625 fflush( stdout );
626
627 gethostname( hostname, 32 );
628 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
629 ret = write_ssl_and_get_response( &ssl, buf, len );
630 if( ret < 200 || ret > 299 )
631 {
632 printf( " failed\n ! server responded with %d\n\n", ret );
633 goto exit;
634 }
635 }
636 else
637 {
638 printf( " > Get header from server:" );
639 fflush( stdout );
640
641 ret = write_and_get_response( server_fd, buf, 0 );
642 if( ret < 200 || ret > 299 )
643 {
644 printf( " failed\n ! server responded with %d\n\n", ret );
645 goto exit;
646 }
647
648 printf(" ok\n" );
649
650 printf( " > Write EHLO to server:" );
651 fflush( stdout );
652
653 gethostname( hostname, 32 );
654 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
655 ret = write_and_get_response( server_fd, buf, len );
656 if( ret < 200 || ret > 299 )
657 {
658 printf( " failed\n ! server responded with %d\n\n", ret );
659 goto exit;
660 }
661
662 printf(" ok\n" );
663
664 printf( " > Write STARTTLS to server:" );
665 fflush( stdout );
666
667 gethostname( hostname, 32 );
668 len = sprintf( (char *) buf, "STARTTLS\n" );
669 ret = write_and_get_response( server_fd, buf, len );
670 if( ret < 200 || ret > 299 )
671 {
672 printf( " failed\n ! server responded with %d\n\n", ret );
673 goto exit;
674 }
675
676 printf(" ok\n" );
677
678 if( do_handshake( &ssl, &opt ) != 0 )
679 goto exit;
680 }
681
Paul Bakker5690efc2011-05-26 13:16:06 +0000682#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000683 if( opt.authentication )
684 {
685 printf( " > Write AUTH LOGIN to server:" );
686 fflush( stdout );
687
688 len = sprintf( (char *) buf, "AUTH LOGIN\n" );
689 ret = write_ssl_and_get_response( &ssl, buf, len );
690 if( ret < 200 || ret > 399 )
691 {
692 printf( " failed\n ! server responded with %d\n\n", ret );
693 goto exit;
694 }
695
696 printf(" ok\n" );
697
698 printf( " > Write username to server: %s", opt.user_name );
699 fflush( stdout );
700
701 n = sizeof( buf );
Paul Bakkere0225e42013-06-06 12:52:24 +0200702 len = base64_encode( base, &n, (const unsigned char *) opt.user_name,
703 strlen( opt.user_name ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000704 len = sprintf( (char *) buf, "%s\n", base );
705 ret = write_ssl_and_get_response( &ssl, buf, len );
706 if( ret < 300 || ret > 399 )
707 {
708 printf( " failed\n ! server responded with %d\n\n", ret );
709 goto exit;
710 }
711
712 printf(" ok\n" );
713
714 printf( " > Write password to server: %s", opt.user_pwd );
715 fflush( stdout );
716
Paul Bakkere0225e42013-06-06 12:52:24 +0200717 len = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
718 strlen( opt.user_pwd ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000719 len = sprintf( (char *) buf, "%s\n", base );
720 ret = write_ssl_and_get_response( &ssl, buf, len );
721 if( ret < 200 || ret > 399 )
722 {
723 printf( " failed\n ! server responded with %d\n\n", ret );
724 goto exit;
725 }
726
727 printf(" ok\n" );
728 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000729#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000730
731 printf( " > Write MAIL FROM to server:" );
732 fflush( stdout );
733
734 len = sprintf( (char *) buf, "MAIL FROM:<%s>\n", opt.mail_from );
735 ret = write_ssl_and_get_response( &ssl, buf, len );
736 if( ret < 200 || ret > 299 )
737 {
738 printf( " failed\n ! server responded with %d\n\n", ret );
739 goto exit;
740 }
741
742 printf(" ok\n" );
743
744 printf( " > Write RCPT TO to server:" );
745 fflush( stdout );
746
747 len = sprintf( (char *) buf, "RCPT TO:<%s>\n", opt.mail_to );
748 ret = write_ssl_and_get_response( &ssl, buf, len );
749 if( ret < 200 || ret > 299 )
750 {
751 printf( " failed\n ! server responded with %d\n\n", ret );
752 goto exit;
753 }
754
755 printf(" ok\n" );
756
757 printf( " > Write DATA to server:" );
758 fflush( stdout );
759
760 len = sprintf( (char *) buf, "DATA\n" );
761 ret = write_ssl_and_get_response( &ssl, buf, len );
762 if( ret < 300 || ret > 399 )
763 {
764 printf( " failed\n ! server responded with %d\n\n", ret );
765 goto exit;
766 }
767
768 printf(" ok\n" );
769
770 printf( " > Write content to server:" );
771 fflush( stdout );
772
773 len = sprintf( (char *) buf, "From: %s\nSubject: PolarSSL Test mail\n\n"
774 "This is a simple test mail from the "
775 "PolarSSL mail client example.\n"
776 "\n"
777 "Enjoy!", opt.mail_from );
778 ret = write_ssl_data( &ssl, buf, len );
779
780 len = sprintf( (char *) buf, "\r\n.\r\n");
781 ret = write_ssl_and_get_response( &ssl, buf, len );
782 if( ret < 200 || ret > 299 )
783 {
784 printf( " failed\n ! server responded with %d\n\n", ret );
785 goto exit;
786 }
787
788 printf(" ok\n" );
789
790 ssl_close_notify( &ssl );
791
792exit:
793
794 if( server_fd )
795 net_close( server_fd );
796 x509_free( &clicert );
797 x509_free( &cacert );
798 rsa_free( &rsa );
799 ssl_free( &ssl );
800
Paul Bakkercce9d772011-11-18 14:26:47 +0000801#if defined(_WIN32)
Paul Bakker1496d382011-05-23 12:07:29 +0000802 printf( " + Press Enter to exit this program.\n" );
803 fflush( stdout ); getchar();
804#endif
805
806 return( ret );
807}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000808#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
809 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
810 POLARSSL_CTR_DRBG_C */