blob: e8a49dbe6b07f39bb05067906232d044f2e57f96 [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
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker1496d382011-05-23 12:07:29 +000027
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <unistd.h>
32
Paul Bakker5a835222011-10-12 09:19:31 +000033#if defined(_WIN32) || defined(_WIN32_WCE)
34
35#include <winsock2.h>
36#include <windows.h>
37
38#if defined(_WIN32_WCE)
39#pragma comment( lib, "ws2.lib" )
40#else
41#pragma comment( lib, "ws2_32.lib" )
42#endif
43#endif
44
Paul Bakker1496d382011-05-23 12:07:29 +000045#include "polarssl/base64.h"
46#include "polarssl/error.h"
47#include "polarssl/net.h"
48#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000049#include "polarssl/entropy.h"
50#include "polarssl/ctr_drbg.h"
Paul Bakker1496d382011-05-23 12:07:29 +000051#include "polarssl/certs.h"
52#include "polarssl/x509.h"
53
Paul Bakker9a97c5d2013-09-15 17:07:33 +020054#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
55 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
56 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020057 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker9a97c5d2013-09-15 17:07:33 +020058int main( int argc, char *argv[] )
59{
60 ((void) argc);
61 ((void) argv);
62
63 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
64 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
65 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020066 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
Paul Bakker9a97c5d2013-09-15 17:07:33 +020067 "not defined.\n");
68 return( 0 );
69}
70#else
71
Paul Bakker1496d382011-05-23 12:07:29 +000072#define DFL_SERVER_NAME "localhost"
73#define DFL_SERVER_PORT 465
74#define DFL_USER_NAME "user"
75#define DFL_USER_PWD "password"
76#define DFL_MAIL_FROM ""
77#define DFL_MAIL_TO ""
78#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000079#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000080#define DFL_CRT_FILE ""
81#define DFL_KEY_FILE ""
82#define DFL_FORCE_CIPHER 0
83#define DFL_MODE 0
84#define DFL_AUTHENTICATION 0
85
86#define MODE_SSL_TLS 0
87#define MODE_STARTTLS 0
88
89/*
90 * global options
91 */
92struct options
93{
Paul Bakkeref3f8c72013-06-24 13:01:08 +020094 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +000095 int server_port; /* port on which the ssl service runs */
96 int debug_level; /* level of debugging */
97 int authentication; /* if authentication is required */
98 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020099 const char *user_name; /* username to use for authentication */
100 const char *user_pwd; /* password to use for authentication */
101 const char *mail_from; /* E-Mail address to use as sender */
102 const char *mail_to; /* E-Mail address to use as recipient */
103 const char *ca_file; /* the file with the CA certificate(s) */
104 const char *crt_file; /* the file with the client certificate */
105 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +0000106 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
107} opt;
108
Paul Bakker3c5ef712013-06-25 16:37:45 +0200109static void my_debug( void *ctx, int level, const char *str )
Paul Bakker1496d382011-05-23 12:07:29 +0000110{
111 if( level < opt.debug_level )
112 {
113 fprintf( (FILE *) ctx, "%s", str );
114 fflush( (FILE *) ctx );
115 }
116}
117
Paul Bakker3c5ef712013-06-25 16:37:45 +0200118static int do_handshake( ssl_context *ssl, struct options *opt )
Paul Bakker1496d382011-05-23 12:07:29 +0000119{
120 int ret;
121 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000122 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000123
124 /*
125 * 4. Handshake
126 */
127 printf( " . Performing the SSL/TLS handshake..." );
128 fflush( stdout );
129
130 while( ( ret = ssl_handshake( ssl ) ) != 0 )
131 {
132 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
133 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000134#if defined(POLARSSL_ERROR_C)
Paul Bakker03a8a792013-06-30 12:18:08 +0200135 polarssl_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000136#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000137 printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
138 return( -1 );
139 }
140 }
141
142 printf( " ok\n [ Ciphersuite is %s ]\n",
143 ssl_get_ciphersuite( ssl ) );
144
145 /*
146 * 5. Verify the server certificate
147 */
148 printf( " . Verifying peer X.509 certificate..." );
149
150 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
151 {
152 printf( " failed\n" );
153
154 if( ( ret & BADCERT_EXPIRED ) != 0 )
155 printf( " ! server certificate has expired\n" );
156
157 if( ( ret & BADCERT_REVOKED ) != 0 )
158 printf( " ! server certificate has been revoked\n" );
159
160 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
161 printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
162
163 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
164 printf( " ! self-signed or not signed by a trusted CA\n" );
165
166 printf( "\n" );
167 }
168 else
169 printf( " ok\n" );
170
171 printf( " . Peer certificate information ...\n" );
Paul Bakkerddf26b42013-09-18 13:46:23 +0200172 x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
173 ssl_get_peer_cert( ssl ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000174 printf( "%s\n", buf );
175
176 return( 0 );
177}
178
Paul Bakker3c5ef712013-06-25 16:37:45 +0200179static int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000180{
181 int ret;
182
183 printf("\n%s", buf);
184 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
185 {
186 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
187 {
188 printf( " failed\n ! ssl_write returned %d\n\n", ret );
189 return -1;
190 }
191 }
192
193 return( 0 );
194}
195
Paul Bakker3c5ef712013-06-25 16:37:45 +0200196static int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000197{
198 int ret;
199 unsigned char data[128];
200 char code[4];
201 size_t i, idx = 0;
202
203 printf("\n%s", buf);
204 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
205 {
206 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
207 {
208 printf( " failed\n ! ssl_write returned %d\n\n", ret );
209 return -1;
210 }
211 }
212
213 do
214 {
215 len = sizeof( data ) - 1;
216 memset( data, 0, sizeof( data ) );
217 ret = ssl_read( ssl, data, len );
218
219 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
220 continue;
221
222 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
223 return -1;
224
225 if( ret <= 0 )
226 {
227 printf( "failed\n ! ssl_read returned %d\n\n", ret );
228 return -1;
229 }
230
231 printf("\n%s", data);
232 len = ret;
233 for( i = 0; i < len; i++ )
234 {
235 if( data[i] != '\n' )
236 {
237 if( idx < 4 )
238 code[ idx++ ] = data[i];
239 continue;
240 }
241
242 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
243 {
244 code[3] = '\0';
245 return atoi( code );
246 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200247
Paul Bakker1496d382011-05-23 12:07:29 +0000248 idx = 0;
249 }
250 }
251 while( 1 );
252}
253
Paul Bakker3c5ef712013-06-25 16:37:45 +0200254static int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000255{
256 int ret;
257 unsigned char data[128];
258 char code[4];
259 size_t i, idx = 0;
260
261 printf("\n%s", buf);
262 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
263 {
264 printf( " failed\n ! ssl_write returned %d\n\n", ret );
265 return -1;
266 }
267
268 do
269 {
270 len = sizeof( data ) - 1;
271 memset( data, 0, sizeof( data ) );
272 ret = read( sock_fd, data, len );
273
274 if( ret <= 0 )
275 {
276 printf( "failed\n ! read returned %d\n\n", ret );
277 return -1;
278 }
279
280 printf("\n%s", data);
281 len = ret;
282 for( i = 0; i < len; i++ )
283 {
284 if( data[i] != '\n' )
285 {
286 if( idx < 4 )
287 code[ idx++ ] = data[i];
288 continue;
289 }
290
291 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
292 {
293 code[3] = '\0';
294 return atoi( code );
295 }
296
297 idx = 0;
298 }
299 }
300 while( 1 );
301}
302
Paul Bakker5690efc2011-05-26 13:16:06 +0000303#if defined(POLARSSL_BASE64_C)
304#define USAGE_AUTH \
305 " authentication=%%d default: 0 (disabled)\n" \
306 " user_name=%%s default: \"user\"\n" \
307 " user_pwd=%%s default: \"password\"\n"
308#else
309#define USAGE_AUTH \
310 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
311#endif /* POLARSSL_BASE64_C */
312
313#if defined(POLARSSL_FS_IO)
314#define USAGE_IO \
315 " ca_file=%%s default: \"\" (pre-loaded)\n" \
316 " crt_file=%%s default: \"\" (pre-loaded)\n" \
317 " key_file=%%s default: \"\" (pre-loaded)\n"
318#else
319#define USAGE_IO \
320 " No file operations available (POLARSSL_FS_IO not defined)\n"
321#endif /* POLARSSL_FS_IO */
322
Paul Bakker1496d382011-05-23 12:07:29 +0000323#define USAGE \
324 "\n usage: ssl_mail_client param=<>...\n" \
325 "\n acceptable parameters:\n" \
326 " server_name=%%s default: localhost\n" \
327 " server_port=%%d default: 4433\n" \
328 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000329 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000330 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000331 " mail_from=%%s default: \"\"\n" \
332 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000333 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000334 " force_ciphersuite=<name> default: all enabled\n"\
335 " acceptable ciphersuite names:\n"
336
337int main( int argc, char *argv[] )
338{
339 int ret = 0, len, server_fd;
340 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000341#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000342 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000343#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000344 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200345 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000346
347 entropy_context entropy;
348 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000349 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200350 x509_crt cacert;
351 x509_crt clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200352 pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000353 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000354 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000355 char *p, *q;
356 const int *list;
357
358 /*
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200359 * Make sure memory references are valid in case we exit early.
Paul Bakker1496d382011-05-23 12:07:29 +0000360 */
361 server_fd = 0;
Manuel Pégourié-Gonnard3bd2aae2013-09-20 13:10:13 +0200362 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker369d2eb2013-09-18 11:58:25 +0200363 x509_crt_init( &cacert );
364 x509_crt_init( &clicert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200365 pk_init( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000366
367 if( argc == 0 )
368 {
369 usage:
370 printf( USAGE );
371
372 list = ssl_list_ciphersuites();
373 while( *list )
374 {
375 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
376 list++;
377 }
378 printf("\n");
379 goto exit;
380 }
381
382 opt.server_name = DFL_SERVER_NAME;
383 opt.server_port = DFL_SERVER_PORT;
384 opt.debug_level = DFL_DEBUG_LEVEL;
385 opt.authentication = DFL_AUTHENTICATION;
386 opt.mode = DFL_MODE;
387 opt.user_name = DFL_USER_NAME;
388 opt.user_pwd = DFL_USER_PWD;
389 opt.mail_from = DFL_MAIL_FROM;
390 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000391 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000392 opt.crt_file = DFL_CRT_FILE;
393 opt.key_file = DFL_KEY_FILE;
394 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
395
396 for( i = 1; i < argc; i++ )
397 {
Paul Bakker1496d382011-05-23 12:07:29 +0000398 p = argv[i];
399 if( ( q = strchr( p, '=' ) ) == NULL )
400 goto usage;
401 *q++ = '\0';
402
403 if( strcmp( p, "server_name" ) == 0 )
404 opt.server_name = q;
405 else if( strcmp( p, "server_port" ) == 0 )
406 {
407 opt.server_port = atoi( q );
408 if( opt.server_port < 1 || opt.server_port > 65535 )
409 goto usage;
410 }
411 else if( strcmp( p, "debug_level" ) == 0 )
412 {
413 opt.debug_level = atoi( q );
414 if( opt.debug_level < 0 || opt.debug_level > 65535 )
415 goto usage;
416 }
417 else if( strcmp( p, "authentication" ) == 0 )
418 {
419 opt.authentication = atoi( q );
420 if( opt.authentication < 0 || opt.authentication > 1 )
421 goto usage;
422 }
423 else if( strcmp( p, "mode" ) == 0 )
424 {
425 opt.mode = atoi( q );
426 if( opt.mode < 0 || opt.mode > 1 )
427 goto usage;
428 }
429 else if( strcmp( p, "user_name" ) == 0 )
430 opt.user_name = q;
431 else if( strcmp( p, "user_pwd" ) == 0 )
432 opt.user_pwd = q;
433 else if( strcmp( p, "mail_from" ) == 0 )
434 opt.mail_from = q;
435 else if( strcmp( p, "mail_to" ) == 0 )
436 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000437 else if( strcmp( p, "ca_file" ) == 0 )
438 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000439 else if( strcmp( p, "crt_file" ) == 0 )
440 opt.crt_file = q;
441 else if( strcmp( p, "key_file" ) == 0 )
442 opt.key_file = q;
443 else if( strcmp( p, "force_ciphersuite" ) == 0 )
444 {
445 opt.force_ciphersuite[0] = -1;
446
447 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
448
449 if( opt.force_ciphersuite[0] <= 0 )
450 goto usage;
451
452 opt.force_ciphersuite[1] = 0;
453 }
454 else
455 goto usage;
456 }
457
458 /*
459 * 0. Initialize the RNG and the session data
460 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000461 printf( "\n . Seeding the random number generator..." );
462 fflush( stdout );
463
464 entropy_init( &entropy );
465 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200466 (const unsigned char *) pers,
467 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000468 {
469 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
470 goto exit;
471 }
472
473 printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000474
475 /*
476 * 1.1. Load the trusted CA
477 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000478 printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000479 fflush( stdout );
480
Paul Bakker5690efc2011-05-26 13:16:06 +0000481#if defined(POLARSSL_FS_IO)
482 if( strlen( opt.ca_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200483 ret = x509_crt_parse_file( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000484 else
485#endif
486#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200487 ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_crt,
488 strlen( test_ca_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000489#else
490 {
491 ret = 1;
492 printf("POLARSSL_CERTS_C not defined.");
493 }
494#endif
Paul Bakker42488232012-05-16 08:21:05 +0000495 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000496 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200497 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000498 goto exit;
499 }
500
Paul Bakker42488232012-05-16 08:21:05 +0000501 printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000502
503 /*
504 * 1.2. Load own certificate and private key
505 *
506 * (can be skipped if client authentication is not required)
507 */
508 printf( " . Loading the client cert. and key..." );
509 fflush( stdout );
510
Paul Bakker5690efc2011-05-26 13:16:06 +0000511#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000512 if( strlen( opt.crt_file ) )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200513 ret = x509_crt_parse_file( &clicert, opt.crt_file );
514 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000515#endif
516#if defined(POLARSSL_CERTS_C)
Paul Bakkerddf26b42013-09-18 13:46:23 +0200517 ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt,
518 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000519#else
520 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000521 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000522 printf("POLARSSL_CERTS_C not defined.");
523 }
524#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000525 if( ret != 0 )
526 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200527 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000528 goto exit;
529 }
530
Paul Bakker5690efc2011-05-26 13:16:06 +0000531#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000532 if( strlen( opt.key_file ) )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200533 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker1496d382011-05-23 12:07:29 +0000534 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000535#endif
536#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200537 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000538 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000539#else
540 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000541 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000542 printf("POLARSSL_CERTS_C not defined.");
543 }
544#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000545 if( ret != 0 )
546 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200547 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000548 goto exit;
549 }
550
551 printf( " ok\n" );
552
553 /*
554 * 2. Start the connection
555 */
556 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
557 opt.server_port );
558 fflush( stdout );
559
560 if( ( ret = net_connect( &server_fd, opt.server_name,
561 opt.server_port ) ) != 0 )
562 {
563 printf( " failed\n ! net_connect returned %d\n\n", ret );
564 goto exit;
565 }
566
567 printf( " ok\n" );
568
569 /*
570 * 3. Setup stuff
571 */
572 printf( " . Setting up the SSL/TLS structure..." );
573 fflush( stdout );
574
Paul Bakker1496d382011-05-23 12:07:29 +0000575 if( ( ret = ssl_init( &ssl ) ) != 0 )
576 {
577 printf( " failed\n ! ssl_init returned %d\n\n", ret );
578 goto exit;
579 }
580
581 printf( " ok\n" );
582
583 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
584 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
585
Paul Bakker508ad5a2011-12-04 17:09:26 +0000586 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000587 ssl_set_dbg( &ssl, my_debug, stdout );
588 ssl_set_bio( &ssl, net_recv, &server_fd,
589 net_send, &server_fd );
590
Paul Bakker645ce3a2012-10-31 12:32:41 +0000591 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000592 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
593
Paul Bakker1496d382011-05-23 12:07:29 +0000594 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200595 ssl_set_own_cert( &ssl, &clicert, &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000596
Paul Bakker0be444a2013-08-27 21:55:01 +0200597#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker1496d382011-05-23 12:07:29 +0000598 ssl_set_hostname( &ssl, opt.server_name );
Paul Bakker0be444a2013-08-27 21:55:01 +0200599#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000600
601 if( opt.mode == MODE_SSL_TLS )
602 {
603 if( do_handshake( &ssl, &opt ) != 0 )
604 goto exit;
605
606 printf( " > Get header from server:" );
607 fflush( stdout );
608
609 ret = write_ssl_and_get_response( &ssl, buf, 0 );
610 if( ret < 200 || ret > 299 )
611 {
612 printf( " failed\n ! server responded with %d\n\n", ret );
613 goto exit;
614 }
615
616 printf(" ok\n" );
617
618 printf( " > Write EHLO to server:" );
619 fflush( stdout );
620
621 gethostname( hostname, 32 );
622 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
623 ret = write_ssl_and_get_response( &ssl, buf, len );
624 if( ret < 200 || ret > 299 )
625 {
626 printf( " failed\n ! server responded with %d\n\n", ret );
627 goto exit;
628 }
629 }
630 else
631 {
632 printf( " > Get header from server:" );
633 fflush( stdout );
634
635 ret = write_and_get_response( server_fd, buf, 0 );
636 if( ret < 200 || ret > 299 )
637 {
638 printf( " failed\n ! server responded with %d\n\n", ret );
639 goto exit;
640 }
641
642 printf(" ok\n" );
643
644 printf( " > Write EHLO to server:" );
645 fflush( stdout );
646
647 gethostname( hostname, 32 );
648 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
649 ret = write_and_get_response( server_fd, buf, len );
650 if( ret < 200 || ret > 299 )
651 {
652 printf( " failed\n ! server responded with %d\n\n", ret );
653 goto exit;
654 }
655
656 printf(" ok\n" );
657
658 printf( " > Write STARTTLS to server:" );
659 fflush( stdout );
660
661 gethostname( hostname, 32 );
662 len = sprintf( (char *) buf, "STARTTLS\n" );
663 ret = write_and_get_response( server_fd, buf, len );
664 if( ret < 200 || ret > 299 )
665 {
666 printf( " failed\n ! server responded with %d\n\n", ret );
667 goto exit;
668 }
669
670 printf(" ok\n" );
671
672 if( do_handshake( &ssl, &opt ) != 0 )
673 goto exit;
674 }
675
Paul Bakker5690efc2011-05-26 13:16:06 +0000676#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000677 if( opt.authentication )
678 {
679 printf( " > Write AUTH LOGIN to server:" );
680 fflush( stdout );
681
682 len = sprintf( (char *) buf, "AUTH LOGIN\n" );
683 ret = write_ssl_and_get_response( &ssl, buf, len );
684 if( ret < 200 || ret > 399 )
685 {
686 printf( " failed\n ! server responded with %d\n\n", ret );
687 goto exit;
688 }
689
690 printf(" ok\n" );
691
692 printf( " > Write username to server: %s", opt.user_name );
693 fflush( stdout );
694
695 n = sizeof( buf );
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200696 len = base64_encode( base, &n, (const unsigned char *) opt.user_name,
697 strlen( opt.user_name ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000698 len = sprintf( (char *) buf, "%s\n", base );
699 ret = write_ssl_and_get_response( &ssl, buf, len );
700 if( ret < 300 || ret > 399 )
701 {
702 printf( " failed\n ! server responded with %d\n\n", ret );
703 goto exit;
704 }
705
706 printf(" ok\n" );
707
708 printf( " > Write password to server: %s", opt.user_pwd );
709 fflush( stdout );
710
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200711 len = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
712 strlen( opt.user_pwd ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000713 len = sprintf( (char *) buf, "%s\n", base );
714 ret = write_ssl_and_get_response( &ssl, buf, len );
715 if( ret < 200 || ret > 399 )
716 {
717 printf( " failed\n ! server responded with %d\n\n", ret );
718 goto exit;
719 }
720
721 printf(" ok\n" );
722 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000723#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000724
725 printf( " > Write MAIL FROM to server:" );
726 fflush( stdout );
727
728 len = sprintf( (char *) buf, "MAIL FROM:<%s>\n", opt.mail_from );
729 ret = write_ssl_and_get_response( &ssl, buf, len );
730 if( ret < 200 || ret > 299 )
731 {
732 printf( " failed\n ! server responded with %d\n\n", ret );
733 goto exit;
734 }
735
736 printf(" ok\n" );
737
738 printf( " > Write RCPT TO to server:" );
739 fflush( stdout );
740
741 len = sprintf( (char *) buf, "RCPT TO:<%s>\n", opt.mail_to );
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 DATA to server:" );
752 fflush( stdout );
753
754 len = sprintf( (char *) buf, "DATA\n" );
755 ret = write_ssl_and_get_response( &ssl, buf, len );
756 if( ret < 300 || ret > 399 )
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 content to server:" );
765 fflush( stdout );
766
767 len = sprintf( (char *) buf, "From: %s\nSubject: PolarSSL Test mail\n\n"
768 "This is a simple test mail from the "
769 "PolarSSL mail client example.\n"
770 "\n"
771 "Enjoy!", opt.mail_from );
772 ret = write_ssl_data( &ssl, buf, len );
773
774 len = sprintf( (char *) buf, "\r\n.\r\n");
775 ret = write_ssl_and_get_response( &ssl, buf, len );
776 if( ret < 200 || ret > 299 )
777 {
778 printf( " failed\n ! server responded with %d\n\n", ret );
779 goto exit;
780 }
781
782 printf(" ok\n" );
783
784 ssl_close_notify( &ssl );
785
786exit:
787
788 if( server_fd )
789 net_close( server_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200790 x509_crt_free( &clicert );
791 x509_crt_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200792 pk_free( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000793 ssl_free( &ssl );
794
Paul Bakkercce9d772011-11-18 14:26:47 +0000795#if defined(_WIN32)
Paul Bakker1496d382011-05-23 12:07:29 +0000796 printf( " + Press Enter to exit this program.\n" );
797 fflush( stdout ); getchar();
798#endif
799
800 return( ret );
801}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000802#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
803 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
804 POLARSSL_CTR_DRBG_C */