blob: 2adff52c93e7109f75f24ab41cacdc2e2fa03415 [file] [log] [blame]
Paul Bakker1496d382011-05-23 12:07:29 +00001/*
2 * SSL client for SMTP servers
3 *
4 * Copyright (C) 2006-2010, Brainspark B.V.
5 *
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>
33#include <unistd.h>
34
Paul Bakker5a835222011-10-12 09:19:31 +000035#if defined(_WIN32) || defined(_WIN32_WCE)
36
37#include <winsock2.h>
38#include <windows.h>
39
40#if defined(_WIN32_WCE)
41#pragma comment( lib, "ws2.lib" )
42#else
43#pragma comment( lib, "ws2_32.lib" )
44#endif
45#endif
46
Paul Bakker5690efc2011-05-26 13:16:06 +000047#include "polarssl/config.h"
48
Paul Bakker1496d382011-05-23 12:07:29 +000049#include "polarssl/base64.h"
50#include "polarssl/error.h"
51#include "polarssl/net.h"
52#include "polarssl/ssl.h"
53#include "polarssl/havege.h"
54#include "polarssl/certs.h"
55#include "polarssl/x509.h"
56
57#define DFL_SERVER_NAME "localhost"
58#define DFL_SERVER_PORT 465
59#define DFL_USER_NAME "user"
60#define DFL_USER_PWD "password"
61#define DFL_MAIL_FROM ""
62#define DFL_MAIL_TO ""
63#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000064#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000065#define DFL_CRT_FILE ""
66#define DFL_KEY_FILE ""
67#define DFL_FORCE_CIPHER 0
68#define DFL_MODE 0
69#define DFL_AUTHENTICATION 0
70
71#define MODE_SSL_TLS 0
72#define MODE_STARTTLS 0
73
74/*
75 * global options
76 */
77struct options
78{
79 char *server_name; /* hostname of the server (client only) */
80 int server_port; /* port on which the ssl service runs */
81 int debug_level; /* level of debugging */
82 int authentication; /* if authentication is required */
83 int mode; /* SSL/TLS (0) or STARTTLS (1) */
84 char *user_name; /* username to use for authentication */
85 char *user_pwd; /* password to use for authentication */
86 char *mail_from; /* E-Mail address to use as sender */
87 char *mail_to; /* E-Mail address to use as recipient */
Paul Bakker5690efc2011-05-26 13:16:06 +000088 char *ca_file; /* the file with the CA certificate(s) */
Paul Bakker1496d382011-05-23 12:07:29 +000089 char *crt_file; /* the file with the client certificate */
90 char *key_file; /* the file with the client key */
91 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
92} opt;
93
94void my_debug( void *ctx, int level, const char *str )
95{
96 if( level < opt.debug_level )
97 {
98 fprintf( (FILE *) ctx, "%s", str );
99 fflush( (FILE *) ctx );
100 }
101}
102
Paul Bakker5690efc2011-05-26 13:16:06 +0000103#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
104 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
105 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C)
106int main( void )
107{
108 printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
109 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
110 "POLARSSL_NET_C and/or POLARSSL_RSA_C not defined.\n");
111 return( 0 );
112}
113#else
Paul Bakker1496d382011-05-23 12:07:29 +0000114int do_handshake( ssl_context *ssl, struct options *opt )
115{
116 int ret;
117 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000118 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000119
120 /*
121 * 4. Handshake
122 */
123 printf( " . Performing the SSL/TLS handshake..." );
124 fflush( stdout );
125
126 while( ( ret = ssl_handshake( ssl ) ) != 0 )
127 {
128 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
129 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000130#if defined(POLARSSL_ERROR_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000131 error_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000132#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000133 printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
134 return( -1 );
135 }
136 }
137
138 printf( " ok\n [ Ciphersuite is %s ]\n",
139 ssl_get_ciphersuite( ssl ) );
140
141 /*
142 * 5. Verify the server certificate
143 */
144 printf( " . Verifying peer X.509 certificate..." );
145
146 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
147 {
148 printf( " failed\n" );
149
150 if( ( ret & BADCERT_EXPIRED ) != 0 )
151 printf( " ! server certificate has expired\n" );
152
153 if( ( ret & BADCERT_REVOKED ) != 0 )
154 printf( " ! server certificate has been revoked\n" );
155
156 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
157 printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
158
159 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
160 printf( " ! self-signed or not signed by a trusted CA\n" );
161
162 printf( "\n" );
163 }
164 else
165 printf( " ok\n" );
166
167 printf( " . Peer certificate information ...\n" );
168 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl->peer_cert );
169 printf( "%s\n", buf );
170
171 return( 0 );
172}
173
174int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
175{
176 int ret;
177
178 printf("\n%s", buf);
179 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
180 {
181 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
182 {
183 printf( " failed\n ! ssl_write returned %d\n\n", ret );
184 return -1;
185 }
186 }
187
188 return( 0 );
189}
190
191int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
192{
193 int ret;
194 unsigned char data[128];
195 char code[4];
196 size_t i, idx = 0;
197
198 printf("\n%s", buf);
199 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
200 {
201 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
202 {
203 printf( " failed\n ! ssl_write returned %d\n\n", ret );
204 return -1;
205 }
206 }
207
208 do
209 {
210 len = sizeof( data ) - 1;
211 memset( data, 0, sizeof( data ) );
212 ret = ssl_read( ssl, data, len );
213
214 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
215 continue;
216
217 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
218 return -1;
219
220 if( ret <= 0 )
221 {
222 printf( "failed\n ! ssl_read returned %d\n\n", ret );
223 return -1;
224 }
225
226 printf("\n%s", data);
227 len = ret;
228 for( i = 0; i < len; i++ )
229 {
230 if( data[i] != '\n' )
231 {
232 if( idx < 4 )
233 code[ idx++ ] = data[i];
234 continue;
235 }
236
237 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
238 {
239 code[3] = '\0';
240 return atoi( code );
241 }
242
243 idx = 0;
244 }
245 }
246 while( 1 );
247}
248
249int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
250{
251 int ret;
252 unsigned char data[128];
253 char code[4];
254 size_t i, idx = 0;
255
256 printf("\n%s", buf);
257 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
258 {
259 printf( " failed\n ! ssl_write returned %d\n\n", ret );
260 return -1;
261 }
262
263 do
264 {
265 len = sizeof( data ) - 1;
266 memset( data, 0, sizeof( data ) );
267 ret = read( sock_fd, data, len );
268
269 if( ret <= 0 )
270 {
271 printf( "failed\n ! read returned %d\n\n", ret );
272 return -1;
273 }
274
275 printf("\n%s", data);
276 len = ret;
277 for( i = 0; i < len; i++ )
278 {
279 if( data[i] != '\n' )
280 {
281 if( idx < 4 )
282 code[ idx++ ] = data[i];
283 continue;
284 }
285
286 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
287 {
288 code[3] = '\0';
289 return atoi( code );
290 }
291
292 idx = 0;
293 }
294 }
295 while( 1 );
296}
297
Paul Bakker5690efc2011-05-26 13:16:06 +0000298#if defined(POLARSSL_BASE64_C)
299#define USAGE_AUTH \
300 " authentication=%%d default: 0 (disabled)\n" \
301 " user_name=%%s default: \"user\"\n" \
302 " user_pwd=%%s default: \"password\"\n"
303#else
304#define USAGE_AUTH \
305 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
306#endif /* POLARSSL_BASE64_C */
307
308#if defined(POLARSSL_FS_IO)
309#define USAGE_IO \
310 " ca_file=%%s default: \"\" (pre-loaded)\n" \
311 " crt_file=%%s default: \"\" (pre-loaded)\n" \
312 " key_file=%%s default: \"\" (pre-loaded)\n"
313#else
314#define USAGE_IO \
315 " No file operations available (POLARSSL_FS_IO not defined)\n"
316#endif /* POLARSSL_FS_IO */
317
Paul Bakker1496d382011-05-23 12:07:29 +0000318#define USAGE \
319 "\n usage: ssl_mail_client param=<>...\n" \
320 "\n acceptable parameters:\n" \
321 " server_name=%%s default: localhost\n" \
322 " server_port=%%d default: 4433\n" \
323 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000324 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000325 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000326 " mail_from=%%s default: \"\"\n" \
327 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000328 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000329 " force_ciphersuite=<name> default: all enabled\n"\
330 " acceptable ciphersuite names:\n"
331
332int main( int argc, char *argv[] )
333{
334 int ret = 0, len, server_fd;
335 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000336#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000337 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000338#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000339 char hostname[32];
340 havege_state hs;
341 ssl_context ssl;
342 ssl_session ssn;
343 x509_cert cacert;
344 x509_cert clicert;
345 rsa_context rsa;
346 int i;
347 size_t j, n;
348 char *p, *q;
349 const int *list;
350
351 /*
352 * Make sure memory references are valid.
353 */
354 server_fd = 0;
355 memset( &ssn, 0, sizeof( ssl_session ) );
356 memset( &ssl, 0, sizeof( ssl_context ) );
357 memset( &cacert, 0, sizeof( x509_cert ) );
358 memset( &clicert, 0, sizeof( x509_cert ) );
359 memset( &rsa, 0, sizeof( rsa_context ) );
360
361 if( argc == 0 )
362 {
363 usage:
364 printf( USAGE );
365
366 list = ssl_list_ciphersuites();
367 while( *list )
368 {
369 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
370 list++;
371 }
372 printf("\n");
373 goto exit;
374 }
375
376 opt.server_name = DFL_SERVER_NAME;
377 opt.server_port = DFL_SERVER_PORT;
378 opt.debug_level = DFL_DEBUG_LEVEL;
379 opt.authentication = DFL_AUTHENTICATION;
380 opt.mode = DFL_MODE;
381 opt.user_name = DFL_USER_NAME;
382 opt.user_pwd = DFL_USER_PWD;
383 opt.mail_from = DFL_MAIL_FROM;
384 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000385 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000386 opt.crt_file = DFL_CRT_FILE;
387 opt.key_file = DFL_KEY_FILE;
388 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
389
390 for( i = 1; i < argc; i++ )
391 {
392 n = strlen( argv[i] );
393
394 for( j = 0; j < n; j++ )
395 {
396 if( argv[i][j] == '=')
397 break;
398
399 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
400 argv[i][j] |= 0x20;
401 }
402
403 p = argv[i];
404 if( ( q = strchr( p, '=' ) ) == NULL )
405 goto usage;
406 *q++ = '\0';
407
408 if( strcmp( p, "server_name" ) == 0 )
409 opt.server_name = q;
410 else if( strcmp( p, "server_port" ) == 0 )
411 {
412 opt.server_port = atoi( q );
413 if( opt.server_port < 1 || opt.server_port > 65535 )
414 goto usage;
415 }
416 else if( strcmp( p, "debug_level" ) == 0 )
417 {
418 opt.debug_level = atoi( q );
419 if( opt.debug_level < 0 || opt.debug_level > 65535 )
420 goto usage;
421 }
422 else if( strcmp( p, "authentication" ) == 0 )
423 {
424 opt.authentication = atoi( q );
425 if( opt.authentication < 0 || opt.authentication > 1 )
426 goto usage;
427 }
428 else if( strcmp( p, "mode" ) == 0 )
429 {
430 opt.mode = atoi( q );
431 if( opt.mode < 0 || opt.mode > 1 )
432 goto usage;
433 }
434 else if( strcmp( p, "user_name" ) == 0 )
435 opt.user_name = q;
436 else if( strcmp( p, "user_pwd" ) == 0 )
437 opt.user_pwd = q;
438 else if( strcmp( p, "mail_from" ) == 0 )
439 opt.mail_from = q;
440 else if( strcmp( p, "mail_to" ) == 0 )
441 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000442 else if( strcmp( p, "ca_file" ) == 0 )
443 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000444 else if( strcmp( p, "crt_file" ) == 0 )
445 opt.crt_file = q;
446 else if( strcmp( p, "key_file" ) == 0 )
447 opt.key_file = q;
448 else if( strcmp( p, "force_ciphersuite" ) == 0 )
449 {
450 opt.force_ciphersuite[0] = -1;
451
452 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
453
454 if( opt.force_ciphersuite[0] <= 0 )
455 goto usage;
456
457 opt.force_ciphersuite[1] = 0;
458 }
459 else
460 goto usage;
461 }
462
463 /*
464 * 0. Initialize the RNG and the session data
465 */
466 havege_init( &hs );
467
468 /*
469 * 1.1. Load the trusted CA
470 */
471 printf( "\n . Loading the CA root certificate ..." );
472 fflush( stdout );
473
Paul Bakker5690efc2011-05-26 13:16:06 +0000474#if defined(POLARSSL_FS_IO)
475 if( strlen( opt.ca_file ) )
476 ret = x509parse_crtfile( &cacert, opt.ca_file );
477 else
478#endif
479#if defined(POLARSSL_CERTS_C)
480 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
481 strlen( test_ca_crt ) );
482#else
483 {
484 ret = 1;
485 printf("POLARSSL_CERTS_C not defined.");
486 }
487#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000488 if( ret != 0 )
489 {
490 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
491 goto exit;
492 }
493
494 printf( " ok\n" );
495
496 /*
497 * 1.2. Load own certificate and private key
498 *
499 * (can be skipped if client authentication is not required)
500 */
501 printf( " . Loading the client cert. and key..." );
502 fflush( stdout );
503
Paul Bakker5690efc2011-05-26 13:16:06 +0000504#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000505 if( strlen( opt.crt_file ) )
506 ret = x509parse_crtfile( &clicert, opt.crt_file );
507 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000508#endif
509#if defined(POLARSSL_CERTS_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000510 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
511 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000512#else
513 {
514 ret = 1;
515 printf("POLARSSL_CERTS_C not defined.");
516 }
517#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000518 if( ret != 0 )
519 {
520 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
521 goto exit;
522 }
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.key_file ) )
526 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
527 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000528#endif
529#if defined(POLARSSL_CERTS_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000530 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
531 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000532#else
533 {
534 ret = 1;
535 printf("POLARSSL_CERTS_C not defined.");
536 }
537#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000538 if( ret != 0 )
539 {
540 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
541 goto exit;
542 }
543
544 printf( " ok\n" );
545
546 /*
547 * 2. Start the connection
548 */
549 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
550 opt.server_port );
551 fflush( stdout );
552
553 if( ( ret = net_connect( &server_fd, opt.server_name,
554 opt.server_port ) ) != 0 )
555 {
556 printf( " failed\n ! net_connect returned %d\n\n", ret );
557 goto exit;
558 }
559
560 printf( " ok\n" );
561
562 /*
563 * 3. Setup stuff
564 */
565 printf( " . Setting up the SSL/TLS structure..." );
566 fflush( stdout );
567
568 havege_init( &hs );
569
570 if( ( ret = ssl_init( &ssl ) ) != 0 )
571 {
572 printf( " failed\n ! ssl_init returned %d\n\n", ret );
573 goto exit;
574 }
575
576 printf( " ok\n" );
577
578 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
579 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
580
581 ssl_set_rng( &ssl, havege_rand, &hs );
582 ssl_set_dbg( &ssl, my_debug, stdout );
583 ssl_set_bio( &ssl, net_recv, &server_fd,
584 net_send, &server_fd );
585
586 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
587 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
588 else
589 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
590
591 ssl_set_session( &ssl, 1, 600, &ssn );
592
593 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
594 ssl_set_own_cert( &ssl, &clicert, &rsa );
595
596 ssl_set_hostname( &ssl, opt.server_name );
597
598 if( opt.mode == MODE_SSL_TLS )
599 {
600 if( do_handshake( &ssl, &opt ) != 0 )
601 goto exit;
602
603 printf( " > Get header from server:" );
604 fflush( stdout );
605
606 ret = write_ssl_and_get_response( &ssl, buf, 0 );
607 if( ret < 200 || ret > 299 )
608 {
609 printf( " failed\n ! server responded with %d\n\n", ret );
610 goto exit;
611 }
612
613 printf(" ok\n" );
614
615 printf( " > Write EHLO to server:" );
616 fflush( stdout );
617
618 gethostname( hostname, 32 );
619 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
620 ret = write_ssl_and_get_response( &ssl, buf, len );
621 if( ret < 200 || ret > 299 )
622 {
623 printf( " failed\n ! server responded with %d\n\n", ret );
624 goto exit;
625 }
626 }
627 else
628 {
629 printf( " > Get header from server:" );
630 fflush( stdout );
631
632 ret = write_and_get_response( server_fd, buf, 0 );
633 if( ret < 200 || ret > 299 )
634 {
635 printf( " failed\n ! server responded with %d\n\n", ret );
636 goto exit;
637 }
638
639 printf(" ok\n" );
640
641 printf( " > Write EHLO to server:" );
642 fflush( stdout );
643
644 gethostname( hostname, 32 );
645 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
646 ret = write_and_get_response( server_fd, buf, len );
647 if( ret < 200 || ret > 299 )
648 {
649 printf( " failed\n ! server responded with %d\n\n", ret );
650 goto exit;
651 }
652
653 printf(" ok\n" );
654
655 printf( " > Write STARTTLS to server:" );
656 fflush( stdout );
657
658 gethostname( hostname, 32 );
659 len = sprintf( (char *) buf, "STARTTLS\n" );
660 ret = write_and_get_response( server_fd, buf, len );
661 if( ret < 200 || ret > 299 )
662 {
663 printf( " failed\n ! server responded with %d\n\n", ret );
664 goto exit;
665 }
666
667 printf(" ok\n" );
668
669 if( do_handshake( &ssl, &opt ) != 0 )
670 goto exit;
671 }
672
Paul Bakker5690efc2011-05-26 13:16:06 +0000673#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000674 if( opt.authentication )
675 {
676 printf( " > Write AUTH LOGIN to server:" );
677 fflush( stdout );
678
679 len = sprintf( (char *) buf, "AUTH LOGIN\n" );
680 ret = write_ssl_and_get_response( &ssl, buf, len );
681 if( ret < 200 || ret > 399 )
682 {
683 printf( " failed\n ! server responded with %d\n\n", ret );
684 goto exit;
685 }
686
687 printf(" ok\n" );
688
689 printf( " > Write username to server: %s", opt.user_name );
690 fflush( stdout );
691
692 n = sizeof( buf );
Paul Bakkere22d7032011-05-23 16:02:34 +0000693 len = base64_encode( base, &n, (unsigned char *) opt.user_name, strlen( opt.user_name ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000694 len = sprintf( (char *) buf, "%s\n", base );
695 ret = write_ssl_and_get_response( &ssl, buf, len );
696 if( ret < 300 || ret > 399 )
697 {
698 printf( " failed\n ! server responded with %d\n\n", ret );
699 goto exit;
700 }
701
702 printf(" ok\n" );
703
704 printf( " > Write password to server: %s", opt.user_pwd );
705 fflush( stdout );
706
Paul Bakkere22d7032011-05-23 16:02:34 +0000707 len = base64_encode( base, &n, (unsigned char *) opt.user_pwd, strlen( opt.user_pwd ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000708 len = sprintf( (char *) buf, "%s\n", base );
709 ret = write_ssl_and_get_response( &ssl, buf, len );
710 if( ret < 200 || ret > 399 )
711 {
712 printf( " failed\n ! server responded with %d\n\n", ret );
713 goto exit;
714 }
715
716 printf(" ok\n" );
717 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000718#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000719
720 printf( " > Write MAIL FROM to server:" );
721 fflush( stdout );
722
723 len = sprintf( (char *) buf, "MAIL FROM:<%s>\n", opt.mail_from );
724 ret = write_ssl_and_get_response( &ssl, buf, len );
725 if( ret < 200 || ret > 299 )
726 {
727 printf( " failed\n ! server responded with %d\n\n", ret );
728 goto exit;
729 }
730
731 printf(" ok\n" );
732
733 printf( " > Write RCPT TO to server:" );
734 fflush( stdout );
735
736 len = sprintf( (char *) buf, "RCPT TO:<%s>\n", opt.mail_to );
737 ret = write_ssl_and_get_response( &ssl, buf, len );
738 if( ret < 200 || ret > 299 )
739 {
740 printf( " failed\n ! server responded with %d\n\n", ret );
741 goto exit;
742 }
743
744 printf(" ok\n" );
745
746 printf( " > Write DATA to server:" );
747 fflush( stdout );
748
749 len = sprintf( (char *) buf, "DATA\n" );
750 ret = write_ssl_and_get_response( &ssl, buf, len );
751 if( ret < 300 || ret > 399 )
752 {
753 printf( " failed\n ! server responded with %d\n\n", ret );
754 goto exit;
755 }
756
757 printf(" ok\n" );
758
759 printf( " > Write content to server:" );
760 fflush( stdout );
761
762 len = sprintf( (char *) buf, "From: %s\nSubject: PolarSSL Test mail\n\n"
763 "This is a simple test mail from the "
764 "PolarSSL mail client example.\n"
765 "\n"
766 "Enjoy!", opt.mail_from );
767 ret = write_ssl_data( &ssl, buf, len );
768
769 len = sprintf( (char *) buf, "\r\n.\r\n");
770 ret = write_ssl_and_get_response( &ssl, buf, len );
771 if( ret < 200 || ret > 299 )
772 {
773 printf( " failed\n ! server responded with %d\n\n", ret );
774 goto exit;
775 }
776
777 printf(" ok\n" );
778
779 ssl_close_notify( &ssl );
780
781exit:
782
783 if( server_fd )
784 net_close( server_fd );
785 x509_free( &clicert );
786 x509_free( &cacert );
787 rsa_free( &rsa );
788 ssl_free( &ssl );
789
790 memset( &ssl, 0, sizeof( ssl ) );
791
792#ifdef WIN32
793 printf( " + Press Enter to exit this program.\n" );
794 fflush( stdout ); getchar();
795#endif
796
797 return( ret );
798}
Paul Bakker5690efc2011-05-26 13:16:06 +0000799#endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C &&
800 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C */