blob: eb223b0b3469cc09d7f1a6c901ce803848a90092 [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>
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"
Paul Bakker508ad5a2011-12-04 17:09:26 +000053#include "polarssl/entropy.h"
54#include "polarssl/ctr_drbg.h"
Paul Bakker1496d382011-05-23 12:07:29 +000055#include "polarssl/certs.h"
56#include "polarssl/x509.h"
57
58#define DFL_SERVER_NAME "localhost"
59#define DFL_SERVER_PORT 465
60#define DFL_USER_NAME "user"
61#define DFL_USER_PWD "password"
62#define DFL_MAIL_FROM ""
63#define DFL_MAIL_TO ""
64#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000065#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000066#define DFL_CRT_FILE ""
67#define DFL_KEY_FILE ""
68#define DFL_FORCE_CIPHER 0
69#define DFL_MODE 0
70#define DFL_AUTHENTICATION 0
71
72#define MODE_SSL_TLS 0
73#define MODE_STARTTLS 0
74
75/*
76 * global options
77 */
78struct options
79{
80 char *server_name; /* hostname of the server (client only) */
81 int server_port; /* port on which the ssl service runs */
82 int debug_level; /* level of debugging */
83 int authentication; /* if authentication is required */
84 int mode; /* SSL/TLS (0) or STARTTLS (1) */
85 char *user_name; /* username to use for authentication */
86 char *user_pwd; /* password to use for authentication */
87 char *mail_from; /* E-Mail address to use as sender */
88 char *mail_to; /* E-Mail address to use as recipient */
Paul Bakker5690efc2011-05-26 13:16:06 +000089 char *ca_file; /* the file with the CA certificate(s) */
Paul Bakker1496d382011-05-23 12:07:29 +000090 char *crt_file; /* the file with the client certificate */
91 char *key_file; /* the file with the client key */
92 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
93} opt;
94
95void my_debug( void *ctx, int level, const char *str )
96{
97 if( level < opt.debug_level )
98 {
99 fprintf( (FILE *) ctx, "%s", str );
100 fflush( (FILE *) ctx );
101 }
102}
103
Paul Bakker508ad5a2011-12-04 17:09:26 +0000104#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +0000105 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +0000106 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
107 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +0000108int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000109{
Paul Bakkercce9d772011-11-18 14:26:47 +0000110 ((void) argc);
111 ((void) argv);
112
Paul Bakker508ad5a2011-12-04 17:09:26 +0000113 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +0000114 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +0000115 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
116 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +0000117 return( 0 );
118}
119#else
Paul Bakker1496d382011-05-23 12:07:29 +0000120int do_handshake( ssl_context *ssl, struct options *opt )
121{
122 int ret;
123 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000124 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000125
126 /*
127 * 4. Handshake
128 */
129 printf( " . Performing the SSL/TLS handshake..." );
130 fflush( stdout );
131
132 while( ( ret = ssl_handshake( ssl ) ) != 0 )
133 {
134 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
135 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000136#if defined(POLARSSL_ERROR_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000137 error_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000138#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000139 printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
140 return( -1 );
141 }
142 }
143
144 printf( " ok\n [ Ciphersuite is %s ]\n",
145 ssl_get_ciphersuite( ssl ) );
146
147 /*
148 * 5. Verify the server certificate
149 */
150 printf( " . Verifying peer X.509 certificate..." );
151
152 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
153 {
154 printf( " failed\n" );
155
156 if( ( ret & BADCERT_EXPIRED ) != 0 )
157 printf( " ! server certificate has expired\n" );
158
159 if( ( ret & BADCERT_REVOKED ) != 0 )
160 printf( " ! server certificate has been revoked\n" );
161
162 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
163 printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
164
165 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
166 printf( " ! self-signed or not signed by a trusted CA\n" );
167
168 printf( "\n" );
169 }
170 else
171 printf( " ok\n" );
172
173 printf( " . Peer certificate information ...\n" );
174 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl->peer_cert );
175 printf( "%s\n", buf );
176
177 return( 0 );
178}
179
180int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
181{
182 int ret;
183
184 printf("\n%s", buf);
185 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
186 {
187 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
188 {
189 printf( " failed\n ! ssl_write returned %d\n\n", ret );
190 return -1;
191 }
192 }
193
194 return( 0 );
195}
196
197int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
198{
199 int ret;
200 unsigned char data[128];
201 char code[4];
202 size_t i, idx = 0;
203
204 printf("\n%s", buf);
205 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
206 {
207 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
208 {
209 printf( " failed\n ! ssl_write returned %d\n\n", ret );
210 return -1;
211 }
212 }
213
214 do
215 {
216 len = sizeof( data ) - 1;
217 memset( data, 0, sizeof( data ) );
218 ret = ssl_read( ssl, data, len );
219
220 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
221 continue;
222
223 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
224 return -1;
225
226 if( ret <= 0 )
227 {
228 printf( "failed\n ! ssl_read returned %d\n\n", ret );
229 return -1;
230 }
231
232 printf("\n%s", data);
233 len = ret;
234 for( i = 0; i < len; i++ )
235 {
236 if( data[i] != '\n' )
237 {
238 if( idx < 4 )
239 code[ idx++ ] = data[i];
240 continue;
241 }
242
243 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
244 {
245 code[3] = '\0';
246 return atoi( code );
247 }
248
249 idx = 0;
250 }
251 }
252 while( 1 );
253}
254
255int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
256{
257 int ret;
258 unsigned char data[128];
259 char code[4];
260 size_t i, idx = 0;
261
262 printf("\n%s", buf);
263 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
264 {
265 printf( " failed\n ! ssl_write returned %d\n\n", ret );
266 return -1;
267 }
268
269 do
270 {
271 len = sizeof( data ) - 1;
272 memset( data, 0, sizeof( data ) );
273 ret = read( sock_fd, data, len );
274
275 if( ret <= 0 )
276 {
277 printf( "failed\n ! read returned %d\n\n", ret );
278 return -1;
279 }
280
281 printf("\n%s", data);
282 len = ret;
283 for( i = 0; i < len; i++ )
284 {
285 if( data[i] != '\n' )
286 {
287 if( idx < 4 )
288 code[ idx++ ] = data[i];
289 continue;
290 }
291
292 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
293 {
294 code[3] = '\0';
295 return atoi( code );
296 }
297
298 idx = 0;
299 }
300 }
301 while( 1 );
302}
303
Paul Bakker5690efc2011-05-26 13:16:06 +0000304#if defined(POLARSSL_BASE64_C)
305#define USAGE_AUTH \
306 " authentication=%%d default: 0 (disabled)\n" \
307 " user_name=%%s default: \"user\"\n" \
308 " user_pwd=%%s default: \"password\"\n"
309#else
310#define USAGE_AUTH \
311 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
312#endif /* POLARSSL_BASE64_C */
313
314#if defined(POLARSSL_FS_IO)
315#define USAGE_IO \
316 " ca_file=%%s default: \"\" (pre-loaded)\n" \
317 " crt_file=%%s default: \"\" (pre-loaded)\n" \
318 " key_file=%%s default: \"\" (pre-loaded)\n"
319#else
320#define USAGE_IO \
321 " No file operations available (POLARSSL_FS_IO not defined)\n"
322#endif /* POLARSSL_FS_IO */
323
Paul Bakker1496d382011-05-23 12:07:29 +0000324#define USAGE \
325 "\n usage: ssl_mail_client param=<>...\n" \
326 "\n acceptable parameters:\n" \
327 " server_name=%%s default: localhost\n" \
328 " server_port=%%d default: 4433\n" \
329 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000330 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000331 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000332 " mail_from=%%s default: \"\"\n" \
333 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000334 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000335 " force_ciphersuite=<name> default: all enabled\n"\
336 " acceptable ciphersuite names:\n"
337
338int main( int argc, char *argv[] )
339{
340 int ret = 0, len, server_fd;
341 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000342#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000343 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000344#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000345 char hostname[32];
Paul Bakker508ad5a2011-12-04 17:09:26 +0000346 char *pers = "ssl_mail_client";
347
348 entropy_context entropy;
349 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000350 ssl_context ssl;
351 ssl_session ssn;
352 x509_cert cacert;
353 x509_cert clicert;
354 rsa_context rsa;
355 int i;
356 size_t j, n;
357 char *p, *q;
358 const int *list;
359
360 /*
361 * Make sure memory references are valid.
362 */
363 server_fd = 0;
364 memset( &ssn, 0, sizeof( ssl_session ) );
365 memset( &ssl, 0, sizeof( ssl_context ) );
366 memset( &cacert, 0, sizeof( x509_cert ) );
367 memset( &clicert, 0, sizeof( x509_cert ) );
368 memset( &rsa, 0, sizeof( rsa_context ) );
369
370 if( argc == 0 )
371 {
372 usage:
373 printf( USAGE );
374
375 list = ssl_list_ciphersuites();
376 while( *list )
377 {
378 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
379 list++;
380 }
381 printf("\n");
382 goto exit;
383 }
384
385 opt.server_name = DFL_SERVER_NAME;
386 opt.server_port = DFL_SERVER_PORT;
387 opt.debug_level = DFL_DEBUG_LEVEL;
388 opt.authentication = DFL_AUTHENTICATION;
389 opt.mode = DFL_MODE;
390 opt.user_name = DFL_USER_NAME;
391 opt.user_pwd = DFL_USER_PWD;
392 opt.mail_from = DFL_MAIL_FROM;
393 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000394 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000395 opt.crt_file = DFL_CRT_FILE;
396 opt.key_file = DFL_KEY_FILE;
397 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
398
399 for( i = 1; i < argc; i++ )
400 {
401 n = strlen( argv[i] );
402
403 for( j = 0; j < n; j++ )
404 {
405 if( argv[i][j] == '=')
406 break;
407
408 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
409 argv[i][j] |= 0x20;
410 }
411
412 p = argv[i];
413 if( ( q = strchr( p, '=' ) ) == NULL )
414 goto usage;
415 *q++ = '\0';
416
417 if( strcmp( p, "server_name" ) == 0 )
418 opt.server_name = q;
419 else if( strcmp( p, "server_port" ) == 0 )
420 {
421 opt.server_port = atoi( q );
422 if( opt.server_port < 1 || opt.server_port > 65535 )
423 goto usage;
424 }
425 else if( strcmp( p, "debug_level" ) == 0 )
426 {
427 opt.debug_level = atoi( q );
428 if( opt.debug_level < 0 || opt.debug_level > 65535 )
429 goto usage;
430 }
431 else if( strcmp( p, "authentication" ) == 0 )
432 {
433 opt.authentication = atoi( q );
434 if( opt.authentication < 0 || opt.authentication > 1 )
435 goto usage;
436 }
437 else if( strcmp( p, "mode" ) == 0 )
438 {
439 opt.mode = atoi( q );
440 if( opt.mode < 0 || opt.mode > 1 )
441 goto usage;
442 }
443 else if( strcmp( p, "user_name" ) == 0 )
444 opt.user_name = q;
445 else if( strcmp( p, "user_pwd" ) == 0 )
446 opt.user_pwd = q;
447 else if( strcmp( p, "mail_from" ) == 0 )
448 opt.mail_from = q;
449 else if( strcmp( p, "mail_to" ) == 0 )
450 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000451 else if( strcmp( p, "ca_file" ) == 0 )
452 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000453 else if( strcmp( p, "crt_file" ) == 0 )
454 opt.crt_file = q;
455 else if( strcmp( p, "key_file" ) == 0 )
456 opt.key_file = q;
457 else if( strcmp( p, "force_ciphersuite" ) == 0 )
458 {
459 opt.force_ciphersuite[0] = -1;
460
461 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
462
463 if( opt.force_ciphersuite[0] <= 0 )
464 goto usage;
465
466 opt.force_ciphersuite[1] = 0;
467 }
468 else
469 goto usage;
470 }
471
472 /*
473 * 0. Initialize the RNG and the session data
474 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000475 printf( "\n . Seeding the random number generator..." );
476 fflush( stdout );
477
478 entropy_init( &entropy );
479 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
480 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
481 {
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 Bakker6c0ceb32011-12-04 12:24:18 +0000496 ret = x509parse_crtfile( &cacert, opt.ca_file, X509_NON_PERMISSIVE );
Paul Bakker5690efc2011-05-26 13:16:06 +0000497 else
498#endif
499#if defined(POLARSSL_CERTS_C)
500 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000501 strlen( test_ca_crt ), X509_NON_PERMISSIVE );
Paul Bakker5690efc2011-05-26 13:16:06 +0000502#else
503 {
504 ret = 1;
505 printf("POLARSSL_CERTS_C not defined.");
506 }
507#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000508 if( ret != 0 )
509 {
510 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
511 goto exit;
512 }
513
514 printf( " ok\n" );
515
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 Bakker6c0ceb32011-12-04 12:24:18 +0000526 ret = x509parse_crtfile( &clicert, opt.crt_file, X509_NON_PERMISSIVE );
Paul Bakker1496d382011-05-23 12:07:29 +0000527 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_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000531 strlen( test_cli_crt ), X509_NON_PERMISSIVE );
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_crt returned %d\n\n", ret );
541 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 ) )
546 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
547 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000548#endif
549#if defined(POLARSSL_CERTS_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000550 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
551 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000552#else
553 {
554 ret = 1;
555 printf("POLARSSL_CERTS_C not defined.");
556 }
557#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000558 if( ret != 0 )
559 {
560 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
561 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 );
597 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
598
Paul Bakker508ad5a2011-12-04 17:09:26 +0000599 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000600 ssl_set_dbg( &ssl, my_debug, stdout );
601 ssl_set_bio( &ssl, net_recv, &server_fd,
602 net_send, &server_fd );
603
604 if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
605 ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
606 else
607 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
608
609 ssl_set_session( &ssl, 1, 600, &ssn );
610
611 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
612 ssl_set_own_cert( &ssl, &clicert, &rsa );
613
614 ssl_set_hostname( &ssl, opt.server_name );
615
616 if( opt.mode == MODE_SSL_TLS )
617 {
618 if( do_handshake( &ssl, &opt ) != 0 )
619 goto exit;
620
621 printf( " > Get header from server:" );
622 fflush( stdout );
623
624 ret = write_ssl_and_get_response( &ssl, buf, 0 );
625 if( ret < 200 || ret > 299 )
626 {
627 printf( " failed\n ! server responded with %d\n\n", ret );
628 goto exit;
629 }
630
631 printf(" ok\n" );
632
633 printf( " > Write EHLO to server:" );
634 fflush( stdout );
635
636 gethostname( hostname, 32 );
637 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
638 ret = write_ssl_and_get_response( &ssl, buf, len );
639 if( ret < 200 || ret > 299 )
640 {
641 printf( " failed\n ! server responded with %d\n\n", ret );
642 goto exit;
643 }
644 }
645 else
646 {
647 printf( " > Get header from server:" );
648 fflush( stdout );
649
650 ret = write_and_get_response( server_fd, buf, 0 );
651 if( ret < 200 || ret > 299 )
652 {
653 printf( " failed\n ! server responded with %d\n\n", ret );
654 goto exit;
655 }
656
657 printf(" ok\n" );
658
659 printf( " > Write EHLO to server:" );
660 fflush( stdout );
661
662 gethostname( hostname, 32 );
663 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
664 ret = write_and_get_response( server_fd, buf, len );
665 if( ret < 200 || ret > 299 )
666 {
667 printf( " failed\n ! server responded with %d\n\n", ret );
668 goto exit;
669 }
670
671 printf(" ok\n" );
672
673 printf( " > Write STARTTLS to server:" );
674 fflush( stdout );
675
676 gethostname( hostname, 32 );
677 len = sprintf( (char *) buf, "STARTTLS\n" );
678 ret = write_and_get_response( server_fd, buf, len );
679 if( ret < 200 || ret > 299 )
680 {
681 printf( " failed\n ! server responded with %d\n\n", ret );
682 goto exit;
683 }
684
685 printf(" ok\n" );
686
687 if( do_handshake( &ssl, &opt ) != 0 )
688 goto exit;
689 }
690
Paul Bakker5690efc2011-05-26 13:16:06 +0000691#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000692 if( opt.authentication )
693 {
694 printf( " > Write AUTH LOGIN to server:" );
695 fflush( stdout );
696
697 len = sprintf( (char *) buf, "AUTH LOGIN\n" );
698 ret = write_ssl_and_get_response( &ssl, buf, len );
699 if( ret < 200 || ret > 399 )
700 {
701 printf( " failed\n ! server responded with %d\n\n", ret );
702 goto exit;
703 }
704
705 printf(" ok\n" );
706
707 printf( " > Write username to server: %s", opt.user_name );
708 fflush( stdout );
709
710 n = sizeof( buf );
Paul Bakkere22d7032011-05-23 16:02:34 +0000711 len = base64_encode( base, &n, (unsigned char *) opt.user_name, strlen( opt.user_name ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000712 len = sprintf( (char *) buf, "%s\n", base );
713 ret = write_ssl_and_get_response( &ssl, buf, len );
714 if( ret < 300 || ret > 399 )
715 {
716 printf( " failed\n ! server responded with %d\n\n", ret );
717 goto exit;
718 }
719
720 printf(" ok\n" );
721
722 printf( " > Write password to server: %s", opt.user_pwd );
723 fflush( stdout );
724
Paul Bakkere22d7032011-05-23 16:02:34 +0000725 len = base64_encode( base, &n, (unsigned char *) opt.user_pwd, strlen( opt.user_pwd ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000726 len = sprintf( (char *) buf, "%s\n", base );
727 ret = write_ssl_and_get_response( &ssl, buf, len );
728 if( ret < 200 || ret > 399 )
729 {
730 printf( " failed\n ! server responded with %d\n\n", ret );
731 goto exit;
732 }
733
734 printf(" ok\n" );
735 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000736#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000737
738 printf( " > Write MAIL FROM to server:" );
739 fflush( stdout );
740
741 len = sprintf( (char *) buf, "MAIL FROM:<%s>\n", opt.mail_from );
742 ret = write_ssl_and_get_response( &ssl, buf, len );
743 if( ret < 200 || ret > 299 )
744 {
745 printf( " failed\n ! server responded with %d\n\n", ret );
746 goto exit;
747 }
748
749 printf(" ok\n" );
750
751 printf( " > Write RCPT TO to server:" );
752 fflush( stdout );
753
754 len = sprintf( (char *) buf, "RCPT TO:<%s>\n", opt.mail_to );
755 ret = write_ssl_and_get_response( &ssl, buf, len );
756 if( ret < 200 || ret > 299 )
757 {
758 printf( " failed\n ! server responded with %d\n\n", ret );
759 goto exit;
760 }
761
762 printf(" ok\n" );
763
764 printf( " > Write DATA to server:" );
765 fflush( stdout );
766
767 len = sprintf( (char *) buf, "DATA\n" );
768 ret = write_ssl_and_get_response( &ssl, buf, len );
769 if( ret < 300 || ret > 399 )
770 {
771 printf( " failed\n ! server responded with %d\n\n", ret );
772 goto exit;
773 }
774
775 printf(" ok\n" );
776
777 printf( " > Write content to server:" );
778 fflush( stdout );
779
780 len = sprintf( (char *) buf, "From: %s\nSubject: PolarSSL Test mail\n\n"
781 "This is a simple test mail from the "
782 "PolarSSL mail client example.\n"
783 "\n"
784 "Enjoy!", opt.mail_from );
785 ret = write_ssl_data( &ssl, buf, len );
786
787 len = sprintf( (char *) buf, "\r\n.\r\n");
788 ret = write_ssl_and_get_response( &ssl, buf, len );
789 if( ret < 200 || ret > 299 )
790 {
791 printf( " failed\n ! server responded with %d\n\n", ret );
792 goto exit;
793 }
794
795 printf(" ok\n" );
796
797 ssl_close_notify( &ssl );
798
799exit:
800
801 if( server_fd )
802 net_close( server_fd );
803 x509_free( &clicert );
804 x509_free( &cacert );
805 rsa_free( &rsa );
806 ssl_free( &ssl );
807
808 memset( &ssl, 0, sizeof( ssl ) );
809
Paul Bakkercce9d772011-11-18 14:26:47 +0000810#if defined(_WIN32)
Paul Bakker1496d382011-05-23 12:07:29 +0000811 printf( " + Press Enter to exit this program.\n" );
812 fflush( stdout ); getchar();
813#endif
814
815 return( ret );
816}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000817#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
818 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
819 POLARSSL_CTR_DRBG_C */