blob: 970d5531e4112eee58fa2e0c181bdc412c0ad925 [file] [log] [blame]
Paul Bakker1496d382011-05-23 12:07:29 +00001/*
2 * SSL client for SMTP servers
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2012, 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
Paul Bakker9a97c5d2013-09-15 17:07:33 +020058#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
59 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
60 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020061 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020062int main( int argc, char *argv[] )
63{
64 ((void) argc);
65 ((void) argv);
66
67 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
68 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
69 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020070 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
Paul Bakker9a97c5d2013-09-15 17:07:33 +020071 "not defined.\n");
72 return( 0 );
73}
74#else
75
Paul Bakker1496d382011-05-23 12:07:29 +000076#define DFL_SERVER_NAME "localhost"
77#define DFL_SERVER_PORT 465
78#define DFL_USER_NAME "user"
79#define DFL_USER_PWD "password"
80#define DFL_MAIL_FROM ""
81#define DFL_MAIL_TO ""
82#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000083#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000084#define DFL_CRT_FILE ""
85#define DFL_KEY_FILE ""
86#define DFL_FORCE_CIPHER 0
87#define DFL_MODE 0
88#define DFL_AUTHENTICATION 0
89
90#define MODE_SSL_TLS 0
91#define MODE_STARTTLS 0
92
93/*
94 * global options
95 */
96struct options
97{
Paul Bakkeref3f8c72013-06-24 13:01:08 +020098 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +000099 int server_port; /* port on which the ssl service runs */
100 int debug_level; /* level of debugging */
101 int authentication; /* if authentication is required */
102 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200103 const char *user_name; /* username to use for authentication */
104 const char *user_pwd; /* password to use for authentication */
105 const char *mail_from; /* E-Mail address to use as sender */
106 const char *mail_to; /* E-Mail address to use as recipient */
107 const char *ca_file; /* the file with the CA certificate(s) */
108 const char *crt_file; /* the file with the client certificate */
109 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000110 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
111} opt;
112
Paul Bakker3c5ef712013-06-25 16:37:45 +0200113static void my_debug( void *ctx, int level, const char *str )
Paul Bakker1496d382011-05-23 12:07:29 +0000114{
115 if( level < opt.debug_level )
116 {
117 fprintf( (FILE *) ctx, "%s", str );
118 fflush( (FILE *) ctx );
119 }
120}
121
Paul Bakker3c5ef712013-06-25 16:37:45 +0200122static int do_handshake( ssl_context *ssl, struct options *opt )
Paul Bakker1496d382011-05-23 12:07:29 +0000123{
124 int ret;
125 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000126 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000127
128 /*
129 * 4. Handshake
130 */
131 printf( " . Performing the SSL/TLS handshake..." );
132 fflush( stdout );
133
134 while( ( ret = ssl_handshake( ssl ) ) != 0 )
135 {
136 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
137 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000138#if defined(POLARSSL_ERROR_C)
Paul Bakker03a8a792013-06-30 12:18:08 +0200139 polarssl_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000140#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000141 printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
142 return( -1 );
143 }
144 }
145
146 printf( " ok\n [ Ciphersuite is %s ]\n",
147 ssl_get_ciphersuite( ssl ) );
148
149 /*
150 * 5. Verify the server certificate
151 */
152 printf( " . Verifying peer X.509 certificate..." );
153
154 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
155 {
156 printf( " failed\n" );
157
158 if( ( ret & BADCERT_EXPIRED ) != 0 )
159 printf( " ! server certificate has expired\n" );
160
161 if( ( ret & BADCERT_REVOKED ) != 0 )
162 printf( " ! server certificate has been revoked\n" );
163
164 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
165 printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
166
167 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
168 printf( " ! self-signed or not signed by a trusted CA\n" );
169
170 printf( "\n" );
171 }
172 else
173 printf( " ok\n" );
174
175 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200176 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
177 ssl_get_peer_cert( ssl ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000178 printf( "%s\n", buf );
179
180 return( 0 );
181}
182
Paul Bakker3c5ef712013-06-25 16:37:45 +0200183static int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000184{
185 int ret;
186
187 printf("\n%s", buf);
188 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
189 {
190 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
191 {
192 printf( " failed\n ! ssl_write returned %d\n\n", ret );
193 return -1;
194 }
195 }
196
197 return( 0 );
198}
199
Paul Bakker3c5ef712013-06-25 16:37:45 +0200200static int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000201{
202 int ret;
203 unsigned char data[128];
204 char code[4];
205 size_t i, idx = 0;
206
207 printf("\n%s", buf);
208 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
209 {
210 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
211 {
212 printf( " failed\n ! ssl_write returned %d\n\n", ret );
213 return -1;
214 }
215 }
216
217 do
218 {
219 len = sizeof( data ) - 1;
220 memset( data, 0, sizeof( data ) );
221 ret = ssl_read( ssl, data, len );
222
223 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
224 continue;
225
226 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
227 return -1;
228
229 if( ret <= 0 )
230 {
231 printf( "failed\n ! ssl_read returned %d\n\n", ret );
232 return -1;
233 }
234
235 printf("\n%s", data);
236 len = ret;
237 for( i = 0; i < len; i++ )
238 {
239 if( data[i] != '\n' )
240 {
241 if( idx < 4 )
242 code[ idx++ ] = data[i];
243 continue;
244 }
245
246 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
247 {
248 code[3] = '\0';
249 return atoi( code );
250 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200251
Paul Bakker1496d382011-05-23 12:07:29 +0000252 idx = 0;
253 }
254 }
255 while( 1 );
256}
257
Paul Bakker3c5ef712013-06-25 16:37:45 +0200258static int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000259{
260 int ret;
261 unsigned char data[128];
262 char code[4];
263 size_t i, idx = 0;
264
265 printf("\n%s", buf);
266 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
267 {
268 printf( " failed\n ! ssl_write returned %d\n\n", ret );
269 return -1;
270 }
271
272 do
273 {
274 len = sizeof( data ) - 1;
275 memset( data, 0, sizeof( data ) );
276 ret = read( sock_fd, data, len );
277
278 if( ret <= 0 )
279 {
280 printf( "failed\n ! read returned %d\n\n", ret );
281 return -1;
282 }
283
284 printf("\n%s", data);
285 len = ret;
286 for( i = 0; i < len; i++ )
287 {
288 if( data[i] != '\n' )
289 {
290 if( idx < 4 )
291 code[ idx++ ] = data[i];
292 continue;
293 }
294
295 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
296 {
297 code[3] = '\0';
298 return atoi( code );
299 }
300
301 idx = 0;
302 }
303 }
304 while( 1 );
305}
306
Paul Bakker5690efc2011-05-26 13:16:06 +0000307#if defined(POLARSSL_BASE64_C)
308#define USAGE_AUTH \
309 " authentication=%%d default: 0 (disabled)\n" \
310 " user_name=%%s default: \"user\"\n" \
311 " user_pwd=%%s default: \"password\"\n"
312#else
313#define USAGE_AUTH \
314 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
315#endif /* POLARSSL_BASE64_C */
316
317#if defined(POLARSSL_FS_IO)
318#define USAGE_IO \
319 " ca_file=%%s default: \"\" (pre-loaded)\n" \
320 " crt_file=%%s default: \"\" (pre-loaded)\n" \
321 " key_file=%%s default: \"\" (pre-loaded)\n"
322#else
323#define USAGE_IO \
324 " No file operations available (POLARSSL_FS_IO not defined)\n"
325#endif /* POLARSSL_FS_IO */
326
Paul Bakker1496d382011-05-23 12:07:29 +0000327#define USAGE \
328 "\n usage: ssl_mail_client param=<>...\n" \
329 "\n acceptable parameters:\n" \
330 " server_name=%%s default: localhost\n" \
331 " server_port=%%d default: 4433\n" \
332 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000333 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000334 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000335 " mail_from=%%s default: \"\"\n" \
336 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000337 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000338 " force_ciphersuite=<name> default: all enabled\n"\
339 " acceptable ciphersuite names:\n"
340
341int main( int argc, char *argv[] )
342{
343 int ret = 0, len, server_fd;
344 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000345#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000346 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000347#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000348 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200349 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000350
351 entropy_context entropy;
352 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000353 ssl_context ssl;
Paul Bakker1496d382011-05-23 12:07:29 +0000354 x509_cert cacert;
355 x509_cert clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200356 pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000357 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000358 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000359 char *p, *q;
360 const int *list;
361
362 /*
363 * Make sure memory references are valid.
364 */
365 server_fd = 0;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200366 x509_crt_init( &cacert );
367 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200368 pk_init( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000369
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 {
Paul Bakker1496d382011-05-23 12:07:29 +0000401 p = argv[i];
402 if( ( q = strchr( p, '=' ) ) == NULL )
403 goto usage;
404 *q++ = '\0';
405
406 if( strcmp( p, "server_name" ) == 0 )
407 opt.server_name = q;
408 else if( strcmp( p, "server_port" ) == 0 )
409 {
410 opt.server_port = atoi( q );
411 if( opt.server_port < 1 || opt.server_port > 65535 )
412 goto usage;
413 }
414 else if( strcmp( p, "debug_level" ) == 0 )
415 {
416 opt.debug_level = atoi( q );
417 if( opt.debug_level < 0 || opt.debug_level > 65535 )
418 goto usage;
419 }
420 else if( strcmp( p, "authentication" ) == 0 )
421 {
422 opt.authentication = atoi( q );
423 if( opt.authentication < 0 || opt.authentication > 1 )
424 goto usage;
425 }
426 else if( strcmp( p, "mode" ) == 0 )
427 {
428 opt.mode = atoi( q );
429 if( opt.mode < 0 || opt.mode > 1 )
430 goto usage;
431 }
432 else if( strcmp( p, "user_name" ) == 0 )
433 opt.user_name = q;
434 else if( strcmp( p, "user_pwd" ) == 0 )
435 opt.user_pwd = q;
436 else if( strcmp( p, "mail_from" ) == 0 )
437 opt.mail_from = q;
438 else if( strcmp( p, "mail_to" ) == 0 )
439 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000440 else if( strcmp( p, "ca_file" ) == 0 )
441 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000442 else if( strcmp( p, "crt_file" ) == 0 )
443 opt.crt_file = q;
444 else if( strcmp( p, "key_file" ) == 0 )
445 opt.key_file = q;
446 else if( strcmp( p, "force_ciphersuite" ) == 0 )
447 {
448 opt.force_ciphersuite[0] = -1;
449
450 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
451
452 if( opt.force_ciphersuite[0] <= 0 )
453 goto usage;
454
455 opt.force_ciphersuite[1] = 0;
456 }
457 else
458 goto usage;
459 }
460
461 /*
462 * 0. Initialize the RNG and the session data
463 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000464 printf( "\n . Seeding the random number generator..." );
465 fflush( stdout );
466
467 entropy_init( &entropy );
468 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200469 (const unsigned char *) pers,
470 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000471 {
472 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
473 goto exit;
474 }
475
476 printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000477
478 /*
479 * 1.1. Load the trusted CA
480 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000481 printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000482 fflush( stdout );
483
Paul Bakker5690efc2011-05-26 13:16:06 +0000484#if defined(POLARSSL_FS_IO)
485 if( strlen( opt.ca_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200486 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000487 else
488#endif
489#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200490 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
491 strlen( test_ca_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000492#else
493 {
494 ret = 1;
495 printf("POLARSSL_CERTS_C not defined.");
496 }
497#endif
Paul Bakker42488232012-05-16 08:21:05 +0000498 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000499 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200500 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000501 goto exit;
502 }
503
Paul Bakker42488232012-05-16 08:21:05 +0000504 printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000505
506 /*
507 * 1.2. Load own certificate and private key
508 *
509 * (can be skipped if client authentication is not required)
510 */
511 printf( " . Loading the client cert. and key..." );
512 fflush( stdout );
513
Paul Bakker5690efc2011-05-26 13:16:06 +0000514#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000515 if( strlen( opt.crt_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200516 ret = x509_crt_parse_file( &clicert, opt.crt_file );
517 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000518#endif
519#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200520 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
521 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000522#else
523 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000524 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000525 printf("POLARSSL_CERTS_C not defined.");
526 }
527#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000528 if( ret != 0 )
529 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200530 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000531 goto exit;
532 }
533
Paul Bakker5690efc2011-05-26 13:16:06 +0000534#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000535 if( strlen( opt.key_file ) )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200536 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker1496d382011-05-23 12:07:29 +0000537 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000538#endif
539#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200540 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000541 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000542#else
543 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000544 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000545 printf("POLARSSL_CERTS_C not defined.");
546 }
547#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000548 if( ret != 0 )
549 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200550 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000551 goto exit;
552 }
553
554 printf( " ok\n" );
555
556 /*
557 * 2. Start the connection
558 */
559 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
560 opt.server_port );
561 fflush( stdout );
562
563 if( ( ret = net_connect( &server_fd, opt.server_name,
564 opt.server_port ) ) != 0 )
565 {
566 printf( " failed\n ! net_connect returned %d\n\n", ret );
567 goto exit;
568 }
569
570 printf( " ok\n" );
571
572 /*
573 * 3. Setup stuff
574 */
575 printf( " . Setting up the SSL/TLS structure..." );
576 fflush( stdout );
577
Paul Bakker1496d382011-05-23 12:07:29 +0000578 if( ( ret = ssl_init( &ssl ) ) != 0 )
579 {
580 printf( " failed\n ! ssl_init returned %d\n\n", ret );
581 goto exit;
582 }
583
584 printf( " ok\n" );
585
586 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
587 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
588
Paul Bakker508ad5a2011-12-04 17:09:26 +0000589 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000590 ssl_set_dbg( &ssl, my_debug, stdout );
591 ssl_set_bio( &ssl, net_recv, &server_fd,
592 net_send, &server_fd );
593
Paul Bakker645ce3a2012-10-31 12:32:41 +0000594 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000595 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
596
Paul Bakker1496d382011-05-23 12:07:29 +0000597 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200598 ssl_set_own_cert( &ssl, &clicert, &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000599
Paul Bakker0be444a2013-08-27 21:55:01 +0200600#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker1496d382011-05-23 12:07:29 +0000601 ssl_set_hostname( &ssl, opt.server_name );
Paul Bakker0be444a2013-08-27 21:55:01 +0200602#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000603
604 if( opt.mode == MODE_SSL_TLS )
605 {
606 if( do_handshake( &ssl, &opt ) != 0 )
607 goto exit;
608
609 printf( " > Get header from server:" );
610 fflush( stdout );
611
612 ret = write_ssl_and_get_response( &ssl, buf, 0 );
613 if( ret < 200 || ret > 299 )
614 {
615 printf( " failed\n ! server responded with %d\n\n", ret );
616 goto exit;
617 }
618
619 printf(" ok\n" );
620
621 printf( " > Write EHLO to server:" );
622 fflush( stdout );
623
624 gethostname( hostname, 32 );
625 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
626 ret = write_ssl_and_get_response( &ssl, buf, len );
627 if( ret < 200 || ret > 299 )
628 {
629 printf( " failed\n ! server responded with %d\n\n", ret );
630 goto exit;
631 }
632 }
633 else
634 {
635 printf( " > Get header from server:" );
636 fflush( stdout );
637
638 ret = write_and_get_response( server_fd, buf, 0 );
639 if( ret < 200 || ret > 299 )
640 {
641 printf( " failed\n ! server responded with %d\n\n", ret );
642 goto exit;
643 }
644
645 printf(" ok\n" );
646
647 printf( " > Write EHLO to server:" );
648 fflush( stdout );
649
650 gethostname( hostname, 32 );
651 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
652 ret = write_and_get_response( server_fd, buf, len );
653 if( ret < 200 || ret > 299 )
654 {
655 printf( " failed\n ! server responded with %d\n\n", ret );
656 goto exit;
657 }
658
659 printf(" ok\n" );
660
661 printf( " > Write STARTTLS to server:" );
662 fflush( stdout );
663
664 gethostname( hostname, 32 );
665 len = sprintf( (char *) buf, "STARTTLS\n" );
666 ret = write_and_get_response( server_fd, buf, len );
667 if( ret < 200 || ret > 299 )
668 {
669 printf( " failed\n ! server responded with %d\n\n", ret );
670 goto exit;
671 }
672
673 printf(" ok\n" );
674
675 if( do_handshake( &ssl, &opt ) != 0 )
676 goto exit;
677 }
678
Paul Bakker5690efc2011-05-26 13:16:06 +0000679#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000680 if( opt.authentication )
681 {
682 printf( " > Write AUTH LOGIN to server:" );
683 fflush( stdout );
684
685 len = sprintf( (char *) buf, "AUTH LOGIN\n" );
686 ret = write_ssl_and_get_response( &ssl, buf, len );
687 if( ret < 200 || ret > 399 )
688 {
689 printf( " failed\n ! server responded with %d\n\n", ret );
690 goto exit;
691 }
692
693 printf(" ok\n" );
694
695 printf( " > Write username to server: %s", opt.user_name );
696 fflush( stdout );
697
698 n = sizeof( buf );
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200699 len = base64_encode( base, &n, (const unsigned char *) opt.user_name,
700 strlen( opt.user_name ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000701 len = sprintf( (char *) buf, "%s\n", base );
702 ret = write_ssl_and_get_response( &ssl, buf, len );
703 if( ret < 300 || ret > 399 )
704 {
705 printf( " failed\n ! server responded with %d\n\n", ret );
706 goto exit;
707 }
708
709 printf(" ok\n" );
710
711 printf( " > Write password to server: %s", opt.user_pwd );
712 fflush( stdout );
713
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200714 len = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
715 strlen( opt.user_pwd ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000716 len = sprintf( (char *) buf, "%s\n", base );
717 ret = write_ssl_and_get_response( &ssl, buf, len );
718 if( ret < 200 || ret > 399 )
719 {
720 printf( " failed\n ! server responded with %d\n\n", ret );
721 goto exit;
722 }
723
724 printf(" ok\n" );
725 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000726#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000727
728 printf( " > Write MAIL FROM to server:" );
729 fflush( stdout );
730
731 len = sprintf( (char *) buf, "MAIL FROM:<%s>\n", opt.mail_from );
732 ret = write_ssl_and_get_response( &ssl, buf, len );
733 if( ret < 200 || ret > 299 )
734 {
735 printf( " failed\n ! server responded with %d\n\n", ret );
736 goto exit;
737 }
738
739 printf(" ok\n" );
740
741 printf( " > Write RCPT TO to server:" );
742 fflush( stdout );
743
744 len = sprintf( (char *) buf, "RCPT TO:<%s>\n", opt.mail_to );
745 ret = write_ssl_and_get_response( &ssl, buf, len );
746 if( ret < 200 || ret > 299 )
747 {
748 printf( " failed\n ! server responded with %d\n\n", ret );
749 goto exit;
750 }
751
752 printf(" ok\n" );
753
754 printf( " > Write DATA to server:" );
755 fflush( stdout );
756
757 len = sprintf( (char *) buf, "DATA\n" );
758 ret = write_ssl_and_get_response( &ssl, buf, len );
759 if( ret < 300 || ret > 399 )
760 {
761 printf( " failed\n ! server responded with %d\n\n", ret );
762 goto exit;
763 }
764
765 printf(" ok\n" );
766
767 printf( " > Write content to server:" );
768 fflush( stdout );
769
770 len = sprintf( (char *) buf, "From: %s\nSubject: PolarSSL Test mail\n\n"
771 "This is a simple test mail from the "
772 "PolarSSL mail client example.\n"
773 "\n"
774 "Enjoy!", opt.mail_from );
775 ret = write_ssl_data( &ssl, buf, len );
776
777 len = sprintf( (char *) buf, "\r\n.\r\n");
778 ret = write_ssl_and_get_response( &ssl, buf, len );
779 if( ret < 200 || ret > 299 )
780 {
781 printf( " failed\n ! server responded with %d\n\n", ret );
782 goto exit;
783 }
784
785 printf(" ok\n" );
786
787 ssl_close_notify( &ssl );
788
789exit:
790
791 if( server_fd )
792 net_close( server_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200793 x509_crt_free( &clicert );
794 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200795 pk_free( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000796 ssl_free( &ssl );
797
Paul Bakkercce9d772011-11-18 14:26:47 +0000798#if defined(_WIN32)
Paul Bakker1496d382011-05-23 12:07:29 +0000799 printf( " + Press Enter to exit this program.\n" );
800 fflush( stdout ); getchar();
801#endif
802
803 return( ret );
804}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000805#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
806 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
807 POLARSSL_CTR_DRBG_C */