blob: 86b354811b998321dd3263309834a30c319591f0 [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{
Paul Bakkere0225e42013-06-06 12:52:24 +020080 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +000081 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) */
Paul Bakkere0225e42013-06-06 12:52:24 +020085 const char *user_name; /* username to use for authentication */
86 const char *user_pwd; /* password to use for authentication */
87 const char *mail_from; /* E-Mail address to use as sender */
88 const char *mail_to; /* E-Mail address to use as recipient */
89 const char *ca_file; /* the file with the CA certificate(s) */
90 const char *crt_file; /* the file with the client certificate */
91 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +000092 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" );
Paul Bakker48916f92012-09-16 19:57:18 +0000174 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakker645ce3a2012-10-31 12:32:41 +0000175 ssl_get_peer_cert( ssl ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000176 printf( "%s\n", buf );
177
178 return( 0 );
179}
180
181int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
182{
183 int ret;
184
185 printf("\n%s", buf);
186 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
187 {
188 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
189 {
190 printf( " failed\n ! ssl_write returned %d\n\n", ret );
191 return -1;
192 }
193 }
194
195 return( 0 );
196}
197
198int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
199{
200 int ret;
201 unsigned char data[128];
202 char code[4];
203 size_t i, idx = 0;
204
205 printf("\n%s", buf);
206 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
207 {
208 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
209 {
210 printf( " failed\n ! ssl_write returned %d\n\n", ret );
211 return -1;
212 }
213 }
214
215 do
216 {
217 len = sizeof( data ) - 1;
218 memset( data, 0, sizeof( data ) );
219 ret = ssl_read( ssl, data, len );
220
221 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
222 continue;
223
224 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
225 return -1;
226
227 if( ret <= 0 )
228 {
229 printf( "failed\n ! ssl_read returned %d\n\n", ret );
230 return -1;
231 }
232
233 printf("\n%s", data);
234 len = ret;
235 for( i = 0; i < len; i++ )
236 {
237 if( data[i] != '\n' )
238 {
239 if( idx < 4 )
240 code[ idx++ ] = data[i];
241 continue;
242 }
243
244 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
245 {
246 code[3] = '\0';
247 return atoi( code );
248 }
249
250 idx = 0;
251 }
252 }
253 while( 1 );
254}
255
256int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
257{
258 int ret;
259 unsigned char data[128];
260 char code[4];
261 size_t i, idx = 0;
262
263 printf("\n%s", buf);
264 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
265 {
266 printf( " failed\n ! ssl_write returned %d\n\n", ret );
267 return -1;
268 }
269
270 do
271 {
272 len = sizeof( data ) - 1;
273 memset( data, 0, sizeof( data ) );
274 ret = read( sock_fd, data, len );
275
276 if( ret <= 0 )
277 {
278 printf( "failed\n ! read returned %d\n\n", ret );
279 return -1;
280 }
281
282 printf("\n%s", data);
283 len = ret;
284 for( i = 0; i < len; i++ )
285 {
286 if( data[i] != '\n' )
287 {
288 if( idx < 4 )
289 code[ idx++ ] = data[i];
290 continue;
291 }
292
293 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
294 {
295 code[3] = '\0';
296 return atoi( code );
297 }
298
299 idx = 0;
300 }
301 }
302 while( 1 );
303}
304
Paul Bakker5690efc2011-05-26 13:16:06 +0000305#if defined(POLARSSL_BASE64_C)
306#define USAGE_AUTH \
307 " authentication=%%d default: 0 (disabled)\n" \
308 " user_name=%%s default: \"user\"\n" \
309 " user_pwd=%%s default: \"password\"\n"
310#else
311#define USAGE_AUTH \
312 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
313#endif /* POLARSSL_BASE64_C */
314
315#if defined(POLARSSL_FS_IO)
316#define USAGE_IO \
317 " ca_file=%%s default: \"\" (pre-loaded)\n" \
318 " crt_file=%%s default: \"\" (pre-loaded)\n" \
319 " key_file=%%s default: \"\" (pre-loaded)\n"
320#else
321#define USAGE_IO \
322 " No file operations available (POLARSSL_FS_IO not defined)\n"
323#endif /* POLARSSL_FS_IO */
324
Paul Bakker1496d382011-05-23 12:07:29 +0000325#define USAGE \
326 "\n usage: ssl_mail_client param=<>...\n" \
327 "\n acceptable parameters:\n" \
328 " server_name=%%s default: localhost\n" \
329 " server_port=%%d default: 4433\n" \
330 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000331 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000332 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000333 " mail_from=%%s default: \"\"\n" \
334 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000335 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000336 " force_ciphersuite=<name> default: all enabled\n"\
337 " acceptable ciphersuite names:\n"
338
339int main( int argc, char *argv[] )
340{
341 int ret = 0, len, server_fd;
342 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000343#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000344 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000345#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000346 char hostname[32];
Paul Bakkere0225e42013-06-06 12:52:24 +0200347 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000348
349 entropy_context entropy;
350 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000351 ssl_context ssl;
Paul Bakker1496d382011-05-23 12:07:29 +0000352 x509_cert cacert;
353 x509_cert clicert;
354 rsa_context rsa;
355 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000356 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000357 char *p, *q;
358 const int *list;
359
360 /*
361 * Make sure memory references are valid.
362 */
363 server_fd = 0;
Paul Bakker1496d382011-05-23 12:07:29 +0000364 memset( &cacert, 0, sizeof( x509_cert ) );
365 memset( &clicert, 0, sizeof( x509_cert ) );
366 memset( &rsa, 0, sizeof( rsa_context ) );
367
368 if( argc == 0 )
369 {
370 usage:
371 printf( USAGE );
372
373 list = ssl_list_ciphersuites();
374 while( *list )
375 {
376 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
377 list++;
378 }
379 printf("\n");
380 goto exit;
381 }
382
383 opt.server_name = DFL_SERVER_NAME;
384 opt.server_port = DFL_SERVER_PORT;
385 opt.debug_level = DFL_DEBUG_LEVEL;
386 opt.authentication = DFL_AUTHENTICATION;
387 opt.mode = DFL_MODE;
388 opt.user_name = DFL_USER_NAME;
389 opt.user_pwd = DFL_USER_PWD;
390 opt.mail_from = DFL_MAIL_FROM;
391 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000392 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000393 opt.crt_file = DFL_CRT_FILE;
394 opt.key_file = DFL_KEY_FILE;
395 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
396
397 for( i = 1; i < argc; i++ )
398 {
Paul Bakker1496d382011-05-23 12:07:29 +0000399 p = argv[i];
400 if( ( q = strchr( p, '=' ) ) == NULL )
401 goto usage;
402 *q++ = '\0';
403
404 if( strcmp( p, "server_name" ) == 0 )
405 opt.server_name = q;
406 else if( strcmp( p, "server_port" ) == 0 )
407 {
408 opt.server_port = atoi( q );
409 if( opt.server_port < 1 || opt.server_port > 65535 )
410 goto usage;
411 }
412 else if( strcmp( p, "debug_level" ) == 0 )
413 {
414 opt.debug_level = atoi( q );
415 if( opt.debug_level < 0 || opt.debug_level > 65535 )
416 goto usage;
417 }
418 else if( strcmp( p, "authentication" ) == 0 )
419 {
420 opt.authentication = atoi( q );
421 if( opt.authentication < 0 || opt.authentication > 1 )
422 goto usage;
423 }
424 else if( strcmp( p, "mode" ) == 0 )
425 {
426 opt.mode = atoi( q );
427 if( opt.mode < 0 || opt.mode > 1 )
428 goto usage;
429 }
430 else if( strcmp( p, "user_name" ) == 0 )
431 opt.user_name = q;
432 else if( strcmp( p, "user_pwd" ) == 0 )
433 opt.user_pwd = q;
434 else if( strcmp( p, "mail_from" ) == 0 )
435 opt.mail_from = q;
436 else if( strcmp( p, "mail_to" ) == 0 )
437 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000438 else if( strcmp( p, "ca_file" ) == 0 )
439 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000440 else if( strcmp( p, "crt_file" ) == 0 )
441 opt.crt_file = q;
442 else if( strcmp( p, "key_file" ) == 0 )
443 opt.key_file = q;
444 else if( strcmp( p, "force_ciphersuite" ) == 0 )
445 {
446 opt.force_ciphersuite[0] = -1;
447
448 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
449
450 if( opt.force_ciphersuite[0] <= 0 )
451 goto usage;
452
453 opt.force_ciphersuite[1] = 0;
454 }
455 else
456 goto usage;
457 }
458
459 /*
460 * 0. Initialize the RNG and the session data
461 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000462 printf( "\n . Seeding the random number generator..." );
463 fflush( stdout );
464
465 entropy_init( &entropy );
466 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +0200467 (const unsigned char *) pers,
468 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000469 {
470 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
471 goto exit;
472 }
473
474 printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000475
476 /*
477 * 1.1. Load the trusted CA
478 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000479 printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000480 fflush( stdout );
481
Paul Bakker5690efc2011-05-26 13:16:06 +0000482#if defined(POLARSSL_FS_IO)
483 if( strlen( opt.ca_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000484 ret = x509parse_crtfile( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000485 else
486#endif
487#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200488 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000489 strlen( test_ca_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000490#else
491 {
492 ret = 1;
493 printf("POLARSSL_CERTS_C not defined.");
494 }
495#endif
Paul Bakker42488232012-05-16 08:21:05 +0000496 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000497 {
498 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
499 goto exit;
500 }
501
Paul Bakker42488232012-05-16 08:21:05 +0000502 printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000503
504 /*
505 * 1.2. Load own certificate and private key
506 *
507 * (can be skipped if client authentication is not required)
508 */
509 printf( " . Loading the client cert. and key..." );
510 fflush( stdout );
511
Paul Bakker5690efc2011-05-26 13:16:06 +0000512#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000513 if( strlen( opt.crt_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000514 ret = x509parse_crtfile( &clicert, opt.crt_file );
Paul Bakker1496d382011-05-23 12:07:29 +0000515 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000516#endif
517#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200518 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000519 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000520#else
521 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000522 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000523 printf("POLARSSL_CERTS_C not defined.");
524 }
525#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000526 if( ret != 0 )
527 {
528 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
529 goto exit;
530 }
531
Paul Bakker5690efc2011-05-26 13:16:06 +0000532#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000533 if( strlen( opt.key_file ) )
534 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
535 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000536#endif
537#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200538 ret = x509parse_key( &rsa, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000539 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000540#else
541 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000542 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000543 printf("POLARSSL_CERTS_C not defined.");
544 }
545#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000546 if( ret != 0 )
547 {
548 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
549 goto exit;
550 }
551
552 printf( " ok\n" );
553
554 /*
555 * 2. Start the connection
556 */
557 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
558 opt.server_port );
559 fflush( stdout );
560
561 if( ( ret = net_connect( &server_fd, opt.server_name,
562 opt.server_port ) ) != 0 )
563 {
564 printf( " failed\n ! net_connect returned %d\n\n", ret );
565 goto exit;
566 }
567
568 printf( " ok\n" );
569
570 /*
571 * 3. Setup stuff
572 */
573 printf( " . Setting up the SSL/TLS structure..." );
574 fflush( stdout );
575
Paul Bakker1496d382011-05-23 12:07:29 +0000576 if( ( ret = ssl_init( &ssl ) ) != 0 )
577 {
578 printf( " failed\n ! ssl_init returned %d\n\n", ret );
579 goto exit;
580 }
581
582 printf( " ok\n" );
583
584 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
585 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
586
Paul Bakker508ad5a2011-12-04 17:09:26 +0000587 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000588 ssl_set_dbg( &ssl, my_debug, stdout );
589 ssl_set_bio( &ssl, net_recv, &server_fd,
590 net_send, &server_fd );
591
Paul Bakker645ce3a2012-10-31 12:32:41 +0000592 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000593 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
594
Paul Bakker1496d382011-05-23 12:07:29 +0000595 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
596 ssl_set_own_cert( &ssl, &clicert, &rsa );
597
598 ssl_set_hostname( &ssl, opt.server_name );
599
600 if( opt.mode == MODE_SSL_TLS )
601 {
602 if( do_handshake( &ssl, &opt ) != 0 )
603 goto exit;
604
605 printf( " > Get header from server:" );
606 fflush( stdout );
607
608 ret = write_ssl_and_get_response( &ssl, buf, 0 );
609 if( ret < 200 || ret > 299 )
610 {
611 printf( " failed\n ! server responded with %d\n\n", ret );
612 goto exit;
613 }
614
615 printf(" ok\n" );
616
617 printf( " > Write EHLO to server:" );
618 fflush( stdout );
619
620 gethostname( hostname, 32 );
621 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
622 ret = write_ssl_and_get_response( &ssl, buf, len );
623 if( ret < 200 || ret > 299 )
624 {
625 printf( " failed\n ! server responded with %d\n\n", ret );
626 goto exit;
627 }
628 }
629 else
630 {
631 printf( " > Get header from server:" );
632 fflush( stdout );
633
634 ret = write_and_get_response( server_fd, buf, 0 );
635 if( ret < 200 || ret > 299 )
636 {
637 printf( " failed\n ! server responded with %d\n\n", ret );
638 goto exit;
639 }
640
641 printf(" ok\n" );
642
643 printf( " > Write EHLO to server:" );
644 fflush( stdout );
645
646 gethostname( hostname, 32 );
647 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
648 ret = write_and_get_response( server_fd, buf, len );
649 if( ret < 200 || ret > 299 )
650 {
651 printf( " failed\n ! server responded with %d\n\n", ret );
652 goto exit;
653 }
654
655 printf(" ok\n" );
656
657 printf( " > Write STARTTLS to server:" );
658 fflush( stdout );
659
660 gethostname( hostname, 32 );
661 len = sprintf( (char *) buf, "STARTTLS\n" );
662 ret = write_and_get_response( server_fd, buf, len );
663 if( ret < 200 || ret > 299 )
664 {
665 printf( " failed\n ! server responded with %d\n\n", ret );
666 goto exit;
667 }
668
669 printf(" ok\n" );
670
671 if( do_handshake( &ssl, &opt ) != 0 )
672 goto exit;
673 }
674
Paul Bakker5690efc2011-05-26 13:16:06 +0000675#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000676 if( opt.authentication )
677 {
678 printf( " > Write AUTH LOGIN to server:" );
679 fflush( stdout );
680
681 len = sprintf( (char *) buf, "AUTH LOGIN\n" );
682 ret = write_ssl_and_get_response( &ssl, buf, len );
683 if( ret < 200 || ret > 399 )
684 {
685 printf( " failed\n ! server responded with %d\n\n", ret );
686 goto exit;
687 }
688
689 printf(" ok\n" );
690
691 printf( " > Write username to server: %s", opt.user_name );
692 fflush( stdout );
693
694 n = sizeof( buf );
Paul Bakkere0225e42013-06-06 12:52:24 +0200695 len = base64_encode( base, &n, (const unsigned char *) opt.user_name,
696 strlen( opt.user_name ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000697 len = sprintf( (char *) buf, "%s\n", base );
698 ret = write_ssl_and_get_response( &ssl, buf, len );
699 if( ret < 300 || 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 password to server: %s", opt.user_pwd );
708 fflush( stdout );
709
Paul Bakkere0225e42013-06-06 12:52:24 +0200710 len = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
711 strlen( opt.user_pwd ) );
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 < 200 || ret > 399 )
715 {
716 printf( " failed\n ! server responded with %d\n\n", ret );
717 goto exit;
718 }
719
720 printf(" ok\n" );
721 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000722#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000723
724 printf( " > Write MAIL FROM to server:" );
725 fflush( stdout );
726
727 len = sprintf( (char *) buf, "MAIL FROM:<%s>\n", opt.mail_from );
728 ret = write_ssl_and_get_response( &ssl, buf, len );
729 if( ret < 200 || ret > 299 )
730 {
731 printf( " failed\n ! server responded with %d\n\n", ret );
732 goto exit;
733 }
734
735 printf(" ok\n" );
736
737 printf( " > Write RCPT TO to server:" );
738 fflush( stdout );
739
740 len = sprintf( (char *) buf, "RCPT TO:<%s>\n", opt.mail_to );
741 ret = write_ssl_and_get_response( &ssl, buf, len );
742 if( ret < 200 || ret > 299 )
743 {
744 printf( " failed\n ! server responded with %d\n\n", ret );
745 goto exit;
746 }
747
748 printf(" ok\n" );
749
750 printf( " > Write DATA to server:" );
751 fflush( stdout );
752
753 len = sprintf( (char *) buf, "DATA\n" );
754 ret = write_ssl_and_get_response( &ssl, buf, len );
755 if( ret < 300 || ret > 399 )
756 {
757 printf( " failed\n ! server responded with %d\n\n", ret );
758 goto exit;
759 }
760
761 printf(" ok\n" );
762
763 printf( " > Write content to server:" );
764 fflush( stdout );
765
766 len = sprintf( (char *) buf, "From: %s\nSubject: PolarSSL Test mail\n\n"
767 "This is a simple test mail from the "
768 "PolarSSL mail client example.\n"
769 "\n"
770 "Enjoy!", opt.mail_from );
771 ret = write_ssl_data( &ssl, buf, len );
772
773 len = sprintf( (char *) buf, "\r\n.\r\n");
774 ret = write_ssl_and_get_response( &ssl, buf, len );
775 if( ret < 200 || ret > 299 )
776 {
777 printf( " failed\n ! server responded with %d\n\n", ret );
778 goto exit;
779 }
780
781 printf(" ok\n" );
782
783 ssl_close_notify( &ssl );
784
785exit:
786
787 if( server_fd )
788 net_close( server_fd );
789 x509_free( &clicert );
790 x509_free( &cacert );
791 rsa_free( &rsa );
792 ssl_free( &ssl );
793
Paul Bakkercce9d772011-11-18 14:26:47 +0000794#if defined(_WIN32)
Paul Bakker1496d382011-05-23 12:07:29 +0000795 printf( " + Press Enter to exit this program.\n" );
796 fflush( stdout ); getchar();
797#endif
798
799 return( ret );
800}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000801#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
802 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
803 POLARSSL_CTR_DRBG_C */