blob: 73fbb2693c7ccfb3c29f70c9bec4d667c6d89db9 [file] [log] [blame]
Paul Bakker1496d382011-05-23 12:07:29 +00001/*
2 * SSL client for SMTP servers
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker1496d382011-05-23 12:07:29 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker1496d382011-05-23 12:07:29 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
28#include <stdlib.h>
29#include <stdio.h>
Paul Bakker238be3a2014-07-07 14:55:07 +020030
31#if !defined(_MSC_VER)
Paul Bakker1496d382011-05-23 12:07:29 +000032#include <unistd.h>
Paul Bakker238be3a2014-07-07 14:55:07 +020033#else
34#include <io.h>
35#define read _read
36#define write _write
37#endif
Paul Bakker1496d382011-05-23 12:07:29 +000038
Paul Bakker5a835222011-10-12 09:19:31 +000039#if defined(_WIN32) || defined(_WIN32_WCE)
40
41#include <winsock2.h>
42#include <windows.h>
43
44#if defined(_WIN32_WCE)
45#pragma comment( lib, "ws2.lib" )
46#else
47#pragma comment( lib, "ws2_32.lib" )
48#endif
49#endif
50
Paul Bakker5690efc2011-05-26 13:16:06 +000051#include "polarssl/config.h"
52
Paul Bakker1496d382011-05-23 12:07:29 +000053#include "polarssl/base64.h"
54#include "polarssl/error.h"
55#include "polarssl/net.h"
56#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000057#include "polarssl/entropy.h"
58#include "polarssl/ctr_drbg.h"
Paul Bakker1496d382011-05-23 12:07:29 +000059#include "polarssl/certs.h"
60#include "polarssl/x509.h"
61
62#define DFL_SERVER_NAME "localhost"
63#define DFL_SERVER_PORT 465
64#define DFL_USER_NAME "user"
65#define DFL_USER_PWD "password"
66#define DFL_MAIL_FROM ""
67#define DFL_MAIL_TO ""
68#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000069#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000070#define DFL_CRT_FILE ""
71#define DFL_KEY_FILE ""
72#define DFL_FORCE_CIPHER 0
73#define DFL_MODE 0
74#define DFL_AUTHENTICATION 0
75
76#define MODE_SSL_TLS 0
77#define MODE_STARTTLS 0
78
79/*
80 * global options
81 */
82struct options
83{
Paul Bakkere0225e42013-06-06 12:52:24 +020084 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +000085 int server_port; /* port on which the ssl service runs */
86 int debug_level; /* level of debugging */
87 int authentication; /* if authentication is required */
88 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkere0225e42013-06-06 12:52:24 +020089 const char *user_name; /* username to use for authentication */
90 const char *user_pwd; /* password to use for authentication */
91 const char *mail_from; /* E-Mail address to use as sender */
92 const char *mail_to; /* E-Mail address to use as recipient */
93 const char *ca_file; /* the file with the CA certificate(s) */
94 const char *crt_file; /* the file with the client certificate */
95 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +000096 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
97} opt;
98
99void my_debug( void *ctx, int level, const char *str )
100{
101 if( level < opt.debug_level )
102 {
103 fprintf( (FILE *) ctx, "%s", str );
104 fflush( (FILE *) ctx );
105 }
106}
107
Paul Bakker508ad5a2011-12-04 17:09:26 +0000108#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +0000109 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +0000110 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
111 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +0000112int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000113{
Paul Bakkercce9d772011-11-18 14:26:47 +0000114 ((void) argc);
115 ((void) argv);
116
Paul Bakker508ad5a2011-12-04 17:09:26 +0000117 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +0000118 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +0000119 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
120 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +0000121 return( 0 );
122}
123#else
Paul Bakker1496d382011-05-23 12:07:29 +0000124int do_handshake( ssl_context *ssl, struct options *opt )
125{
126 int ret;
127 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000128 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000129
130 /*
131 * 4. Handshake
132 */
133 printf( " . Performing the SSL/TLS handshake..." );
134 fflush( stdout );
135
136 while( ( ret = ssl_handshake( ssl ) ) != 0 )
137 {
138 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
139 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000140#if defined(POLARSSL_ERROR_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000141 error_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000142#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000143 printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
144 return( -1 );
145 }
146 }
147
148 printf( " ok\n [ Ciphersuite is %s ]\n",
149 ssl_get_ciphersuite( ssl ) );
150
151 /*
152 * 5. Verify the server certificate
153 */
154 printf( " . Verifying peer X.509 certificate..." );
155
Manuel Pégourié-Gonnard516eb622014-03-11 11:10:27 +0100156 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker1496d382011-05-23 12:07:29 +0000157 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
158 {
159 printf( " failed\n" );
160
161 if( ( ret & BADCERT_EXPIRED ) != 0 )
162 printf( " ! server certificate has expired\n" );
163
164 if( ( ret & BADCERT_REVOKED ) != 0 )
165 printf( " ! server certificate has been revoked\n" );
166
167 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
168 printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
169
170 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
171 printf( " ! self-signed or not signed by a trusted CA\n" );
172
173 printf( "\n" );
174 }
175 else
176 printf( " ok\n" );
177
178 printf( " . Peer certificate information ...\n" );
Paul Bakker48916f92012-09-16 19:57:18 +0000179 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakker645ce3a2012-10-31 12:32:41 +0000180 ssl_get_peer_cert( ssl ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000181 printf( "%s\n", buf );
182
183 return( 0 );
184}
185
186int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
187{
188 int ret;
189
190 printf("\n%s", buf);
191 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
192 {
193 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
194 {
195 printf( " failed\n ! ssl_write returned %d\n\n", ret );
196 return -1;
197 }
198 }
199
200 return( 0 );
201}
202
203int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
204{
205 int ret;
206 unsigned char data[128];
207 char code[4];
208 size_t i, idx = 0;
209
210 printf("\n%s", buf);
211 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
212 {
213 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
214 {
215 printf( " failed\n ! ssl_write returned %d\n\n", ret );
216 return -1;
217 }
218 }
219
220 do
221 {
222 len = sizeof( data ) - 1;
223 memset( data, 0, sizeof( data ) );
224 ret = ssl_read( ssl, data, len );
225
226 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
227 continue;
228
229 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
230 return -1;
231
232 if( ret <= 0 )
233 {
234 printf( "failed\n ! ssl_read returned %d\n\n", ret );
235 return -1;
236 }
237
238 printf("\n%s", data);
239 len = ret;
240 for( i = 0; i < len; i++ )
241 {
242 if( data[i] != '\n' )
243 {
244 if( idx < 4 )
245 code[ idx++ ] = data[i];
246 continue;
247 }
248
249 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
250 {
251 code[3] = '\0';
252 return atoi( code );
253 }
254
255 idx = 0;
256 }
257 }
258 while( 1 );
259}
260
261int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
262{
263 int ret;
264 unsigned char data[128];
265 char code[4];
266 size_t i, idx = 0;
267
268 printf("\n%s", buf);
269 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
270 {
271 printf( " failed\n ! ssl_write returned %d\n\n", ret );
272 return -1;
273 }
274
275 do
276 {
277 len = sizeof( data ) - 1;
278 memset( data, 0, sizeof( data ) );
279 ret = read( sock_fd, data, len );
280
281 if( ret <= 0 )
282 {
283 printf( "failed\n ! read returned %d\n\n", ret );
284 return -1;
285 }
286
Paul Bakkerd6d1f412014-04-17 16:03:48 +0200287 data[len] = '\0';
Paul Bakker1496d382011-05-23 12:07:29 +0000288 printf("\n%s", data);
289 len = ret;
290 for( i = 0; i < len; i++ )
291 {
292 if( data[i] != '\n' )
293 {
294 if( idx < 4 )
295 code[ idx++ ] = data[i];
296 continue;
297 }
298
299 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
300 {
301 code[3] = '\0';
302 return atoi( code );
303 }
304
305 idx = 0;
306 }
307 }
308 while( 1 );
309}
310
Paul Bakker5690efc2011-05-26 13:16:06 +0000311#if defined(POLARSSL_BASE64_C)
312#define USAGE_AUTH \
313 " authentication=%%d default: 0 (disabled)\n" \
314 " user_name=%%s default: \"user\"\n" \
315 " user_pwd=%%s default: \"password\"\n"
316#else
317#define USAGE_AUTH \
318 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
319#endif /* POLARSSL_BASE64_C */
320
321#if defined(POLARSSL_FS_IO)
322#define USAGE_IO \
323 " ca_file=%%s default: \"\" (pre-loaded)\n" \
324 " crt_file=%%s default: \"\" (pre-loaded)\n" \
325 " key_file=%%s default: \"\" (pre-loaded)\n"
326#else
327#define USAGE_IO \
328 " No file operations available (POLARSSL_FS_IO not defined)\n"
329#endif /* POLARSSL_FS_IO */
330
Paul Bakker1496d382011-05-23 12:07:29 +0000331#define USAGE \
332 "\n usage: ssl_mail_client param=<>...\n" \
333 "\n acceptable parameters:\n" \
334 " server_name=%%s default: localhost\n" \
335 " server_port=%%d default: 4433\n" \
336 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000337 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000338 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000339 " mail_from=%%s default: \"\"\n" \
340 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000341 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000342 " force_ciphersuite=<name> default: all enabled\n"\
343 " acceptable ciphersuite names:\n"
344
345int main( int argc, char *argv[] )
346{
347 int ret = 0, len, server_fd;
348 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000349#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000350 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000351#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000352 char hostname[32];
Paul Bakkere0225e42013-06-06 12:52:24 +0200353 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000354
355 entropy_context entropy;
356 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000357 ssl_context ssl;
Paul Bakker1496d382011-05-23 12:07:29 +0000358 x509_cert cacert;
359 x509_cert clicert;
360 rsa_context rsa;
361 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000362 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000363 char *p, *q;
364 const int *list;
365
366 /*
367 * Make sure memory references are valid.
368 */
369 server_fd = 0;
Paul Bakker1496d382011-05-23 12:07:29 +0000370 memset( &cacert, 0, sizeof( x509_cert ) );
371 memset( &clicert, 0, sizeof( x509_cert ) );
372 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakkera16e7f22014-07-09 14:58:11 +0200373 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000374
Manuel Pégourié-Gonnard982eda32014-10-23 15:20:19 +0200375 memset( buf, 0, sizeof( buf ) );
376
Paul Bakker1496d382011-05-23 12:07:29 +0000377 if( argc == 0 )
378 {
379 usage:
380 printf( USAGE );
381
382 list = ssl_list_ciphersuites();
383 while( *list )
384 {
385 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
386 list++;
387 }
388 printf("\n");
389 goto exit;
390 }
391
392 opt.server_name = DFL_SERVER_NAME;
393 opt.server_port = DFL_SERVER_PORT;
394 opt.debug_level = DFL_DEBUG_LEVEL;
395 opt.authentication = DFL_AUTHENTICATION;
396 opt.mode = DFL_MODE;
397 opt.user_name = DFL_USER_NAME;
398 opt.user_pwd = DFL_USER_PWD;
399 opt.mail_from = DFL_MAIL_FROM;
400 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000401 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000402 opt.crt_file = DFL_CRT_FILE;
403 opt.key_file = DFL_KEY_FILE;
404 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
405
406 for( i = 1; i < argc; i++ )
407 {
Paul Bakker1496d382011-05-23 12:07:29 +0000408 p = argv[i];
409 if( ( q = strchr( p, '=' ) ) == NULL )
410 goto usage;
411 *q++ = '\0';
412
413 if( strcmp( p, "server_name" ) == 0 )
414 opt.server_name = q;
415 else if( strcmp( p, "server_port" ) == 0 )
416 {
417 opt.server_port = atoi( q );
418 if( opt.server_port < 1 || opt.server_port > 65535 )
419 goto usage;
420 }
421 else if( strcmp( p, "debug_level" ) == 0 )
422 {
423 opt.debug_level = atoi( q );
424 if( opt.debug_level < 0 || opt.debug_level > 65535 )
425 goto usage;
426 }
427 else if( strcmp( p, "authentication" ) == 0 )
428 {
429 opt.authentication = atoi( q );
430 if( opt.authentication < 0 || opt.authentication > 1 )
431 goto usage;
432 }
433 else if( strcmp( p, "mode" ) == 0 )
434 {
435 opt.mode = atoi( q );
436 if( opt.mode < 0 || opt.mode > 1 )
437 goto usage;
438 }
439 else if( strcmp( p, "user_name" ) == 0 )
440 opt.user_name = q;
441 else if( strcmp( p, "user_pwd" ) == 0 )
442 opt.user_pwd = q;
443 else if( strcmp( p, "mail_from" ) == 0 )
444 opt.mail_from = q;
445 else if( strcmp( p, "mail_to" ) == 0 )
446 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000447 else if( strcmp( p, "ca_file" ) == 0 )
448 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000449 else if( strcmp( p, "crt_file" ) == 0 )
450 opt.crt_file = q;
451 else if( strcmp( p, "key_file" ) == 0 )
452 opt.key_file = q;
453 else if( strcmp( p, "force_ciphersuite" ) == 0 )
454 {
455 opt.force_ciphersuite[0] = -1;
456
457 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
458
459 if( opt.force_ciphersuite[0] <= 0 )
460 goto usage;
461
462 opt.force_ciphersuite[1] = 0;
463 }
464 else
465 goto usage;
466 }
467
468 /*
469 * 0. Initialize the RNG and the session data
470 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000471 printf( "\n . Seeding the random number generator..." );
472 fflush( stdout );
473
474 entropy_init( &entropy );
475 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +0200476 (const unsigned char *) pers,
477 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000478 {
479 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
480 goto exit;
481 }
482
483 printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000484
485 /*
486 * 1.1. Load the trusted CA
487 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000488 printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000489 fflush( stdout );
490
Paul Bakker5690efc2011-05-26 13:16:06 +0000491#if defined(POLARSSL_FS_IO)
492 if( strlen( opt.ca_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000493 ret = x509parse_crtfile( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000494 else
495#endif
496#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200497 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000498 strlen( test_ca_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000499#else
500 {
501 ret = 1;
502 printf("POLARSSL_CERTS_C not defined.");
503 }
504#endif
Paul Bakker42488232012-05-16 08:21:05 +0000505 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000506 {
507 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
508 goto exit;
509 }
510
Paul Bakker42488232012-05-16 08:21:05 +0000511 printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000512
513 /*
514 * 1.2. Load own certificate and private key
515 *
516 * (can be skipped if client authentication is not required)
517 */
518 printf( " . Loading the client cert. and key..." );
519 fflush( stdout );
520
Paul Bakker5690efc2011-05-26 13:16:06 +0000521#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000522 if( strlen( opt.crt_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000523 ret = x509parse_crtfile( &clicert, opt.crt_file );
Paul Bakker1496d382011-05-23 12:07:29 +0000524 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000525#endif
526#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200527 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000528 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000529#else
530 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000531 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000532 printf("POLARSSL_CERTS_C not defined.");
533 }
534#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000535 if( ret != 0 )
536 {
537 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
538 goto exit;
539 }
540
Paul Bakker5690efc2011-05-26 13:16:06 +0000541#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000542 if( strlen( opt.key_file ) )
543 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
544 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000545#endif
546#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200547 ret = x509parse_key( &rsa, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000548 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000549#else
550 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000551 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000552 printf("POLARSSL_CERTS_C not defined.");
553 }
554#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000555 if( ret != 0 )
556 {
557 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
558 goto exit;
559 }
560
561 printf( " ok\n" );
562
563 /*
564 * 2. Start the connection
565 */
566 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
567 opt.server_port );
568 fflush( stdout );
569
570 if( ( ret = net_connect( &server_fd, opt.server_name,
571 opt.server_port ) ) != 0 )
572 {
573 printf( " failed\n ! net_connect returned %d\n\n", ret );
574 goto exit;
575 }
576
577 printf( " ok\n" );
578
579 /*
580 * 3. Setup stuff
581 */
582 printf( " . Setting up the SSL/TLS structure..." );
583 fflush( stdout );
584
Paul Bakker1496d382011-05-23 12:07:29 +0000585 if( ( ret = ssl_init( &ssl ) ) != 0 )
586 {
587 printf( " failed\n ! ssl_init returned %d\n\n", ret );
588 goto exit;
589 }
590
591 printf( " ok\n" );
592
593 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnard516eb622014-03-11 11:10:27 +0100594 /* OPTIONAL is not optimal for security,
595 * but makes interop easier in this simplified example */
Paul Bakker1496d382011-05-23 12:07:29 +0000596 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
597
Paul Bakker508ad5a2011-12-04 17:09:26 +0000598 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000599 ssl_set_dbg( &ssl, my_debug, stdout );
600 ssl_set_bio( &ssl, net_recv, &server_fd,
601 net_send, &server_fd );
602
Paul Bakker645ce3a2012-10-31 12:32:41 +0000603 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000604 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
605
Paul Bakker1496d382011-05-23 12:07:29 +0000606 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
607 ssl_set_own_cert( &ssl, &clicert, &rsa );
608
609 ssl_set_hostname( &ssl, opt.server_name );
610
611 if( opt.mode == MODE_SSL_TLS )
612 {
613 if( do_handshake( &ssl, &opt ) != 0 )
614 goto exit;
615
616 printf( " > Get header from server:" );
617 fflush( stdout );
618
619 ret = write_ssl_and_get_response( &ssl, buf, 0 );
620 if( ret < 200 || ret > 299 )
621 {
622 printf( " failed\n ! server responded with %d\n\n", ret );
623 goto exit;
624 }
625
626 printf(" ok\n" );
627
628 printf( " > Write EHLO to server:" );
629 fflush( stdout );
630
631 gethostname( hostname, 32 );
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100632 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000633 ret = write_ssl_and_get_response( &ssl, buf, len );
634 if( ret < 200 || ret > 299 )
635 {
636 printf( " failed\n ! server responded with %d\n\n", ret );
637 goto exit;
638 }
639 }
640 else
641 {
642 printf( " > Get header from server:" );
643 fflush( stdout );
644
645 ret = write_and_get_response( server_fd, buf, 0 );
646 if( ret < 200 || ret > 299 )
647 {
648 printf( " failed\n ! server responded with %d\n\n", ret );
649 goto exit;
650 }
651
652 printf(" ok\n" );
653
654 printf( " > Write EHLO to server:" );
655 fflush( stdout );
656
657 gethostname( hostname, 32 );
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100658 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000659 ret = write_and_get_response( server_fd, buf, len );
660 if( ret < 200 || ret > 299 )
661 {
662 printf( " failed\n ! server responded with %d\n\n", ret );
663 goto exit;
664 }
665
666 printf(" ok\n" );
667
668 printf( " > Write STARTTLS to server:" );
669 fflush( stdout );
670
671 gethostname( hostname, 32 );
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100672 len = sprintf( (char *) buf, "STARTTLS\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000673 ret = write_and_get_response( server_fd, buf, len );
674 if( ret < 200 || ret > 299 )
675 {
676 printf( " failed\n ! server responded with %d\n\n", ret );
677 goto exit;
678 }
679
680 printf(" ok\n" );
681
682 if( do_handshake( &ssl, &opt ) != 0 )
683 goto exit;
684 }
685
Paul Bakker5690efc2011-05-26 13:16:06 +0000686#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000687 if( opt.authentication )
688 {
689 printf( " > Write AUTH LOGIN to server:" );
690 fflush( stdout );
691
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100692 len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000693 ret = write_ssl_and_get_response( &ssl, buf, len );
694 if( ret < 200 || ret > 399 )
695 {
696 printf( " failed\n ! server responded with %d\n\n", ret );
697 goto exit;
698 }
699
700 printf(" ok\n" );
701
702 printf( " > Write username to server: %s", opt.user_name );
703 fflush( stdout );
704
705 n = sizeof( buf );
Alfred Klomp9afec5f2014-07-14 22:11:13 +0200706 ret = base64_encode( base, &n, (const unsigned char *) opt.user_name,
Paul Bakkere0225e42013-06-06 12:52:24 +0200707 strlen( opt.user_name ) );
Alfred Klomp9afec5f2014-07-14 22:11:13 +0200708
709 if( ret != 0 ) {
710 printf( " failed\n ! base64_encode returned %d\n\n", ret );
711 goto exit;
712 }
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100713 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000714 ret = write_ssl_and_get_response( &ssl, buf, len );
715 if( ret < 300 || ret > 399 )
716 {
717 printf( " failed\n ! server responded with %d\n\n", ret );
718 goto exit;
719 }
720
721 printf(" ok\n" );
722
723 printf( " > Write password to server: %s", opt.user_pwd );
724 fflush( stdout );
725
Alfred Klomp9afec5f2014-07-14 22:11:13 +0200726 ret = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
Paul Bakkere0225e42013-06-06 12:52:24 +0200727 strlen( opt.user_pwd ) );
Alfred Klomp9afec5f2014-07-14 22:11:13 +0200728
729 if( ret != 0 ) {
730 printf( " failed\n ! base64_encode returned %d\n\n", ret );
731 goto exit;
732 }
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100733 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000734 ret = write_ssl_and_get_response( &ssl, buf, len );
735 if( ret < 200 || ret > 399 )
736 {
737 printf( " failed\n ! server responded with %d\n\n", ret );
738 goto exit;
739 }
740
741 printf(" ok\n" );
742 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000743#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000744
745 printf( " > Write MAIL FROM to server:" );
746 fflush( stdout );
747
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100748 len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
Paul Bakker1496d382011-05-23 12:07:29 +0000749 ret = write_ssl_and_get_response( &ssl, buf, len );
750 if( ret < 200 || ret > 299 )
751 {
752 printf( " failed\n ! server responded with %d\n\n", ret );
753 goto exit;
754 }
755
756 printf(" ok\n" );
757
758 printf( " > Write RCPT TO to server:" );
759 fflush( stdout );
760
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100761 len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
Paul Bakker1496d382011-05-23 12:07:29 +0000762 ret = write_ssl_and_get_response( &ssl, buf, len );
763 if( ret < 200 || ret > 299 )
764 {
765 printf( " failed\n ! server responded with %d\n\n", ret );
766 goto exit;
767 }
768
769 printf(" ok\n" );
770
771 printf( " > Write DATA to server:" );
772 fflush( stdout );
773
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100774 len = sprintf( (char *) buf, "DATA\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000775 ret = write_ssl_and_get_response( &ssl, buf, len );
776 if( ret < 300 || ret > 399 )
777 {
778 printf( " failed\n ! server responded with %d\n\n", ret );
779 goto exit;
780 }
781
782 printf(" ok\n" );
783
784 printf( " > Write content to server:" );
785 fflush( stdout );
786
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100787 len = sprintf( (char *) buf, "From: %s\r\nSubject: PolarSSL Test mail\r\n\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000788 "This is a simple test mail from the "
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100789 "PolarSSL mail client example.\r\n"
790 "\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000791 "Enjoy!", opt.mail_from );
792 ret = write_ssl_data( &ssl, buf, len );
793
794 len = sprintf( (char *) buf, "\r\n.\r\n");
795 ret = write_ssl_and_get_response( &ssl, buf, len );
796 if( ret < 200 || ret > 299 )
797 {
798 printf( " failed\n ! server responded with %d\n\n", ret );
799 goto exit;
800 }
801
802 printf(" ok\n" );
803
804 ssl_close_notify( &ssl );
805
806exit:
807
808 if( server_fd )
809 net_close( server_fd );
810 x509_free( &clicert );
811 x509_free( &cacert );
812 rsa_free( &rsa );
813 ssl_free( &ssl );
814
Paul Bakkercce9d772011-11-18 14:26:47 +0000815#if defined(_WIN32)
Paul Bakker1496d382011-05-23 12:07:29 +0000816 printf( " + Press Enter to exit this program.\n" );
817 fflush( stdout ); getchar();
818#endif
819
820 return( ret );
821}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000822#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
823 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
824 POLARSSL_CTR_DRBG_C */