blob: 90845bd4d879cfc7b63648d00dda5a58a681d726 [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>
Paul Bakker238be3a2014-07-07 14:55:07 +020033
34#if !defined(_MSC_VER)
Paul Bakker1496d382011-05-23 12:07:29 +000035#include <unistd.h>
Paul Bakker238be3a2014-07-07 14:55:07 +020036#else
37#include <io.h>
38#define read _read
39#define write _write
40#endif
Paul Bakker1496d382011-05-23 12:07:29 +000041
Paul Bakker5a835222011-10-12 09:19:31 +000042#if defined(_WIN32) || defined(_WIN32_WCE)
43
44#include <winsock2.h>
45#include <windows.h>
46
47#if defined(_WIN32_WCE)
48#pragma comment( lib, "ws2.lib" )
49#else
50#pragma comment( lib, "ws2_32.lib" )
51#endif
52#endif
53
Paul Bakker5690efc2011-05-26 13:16:06 +000054#include "polarssl/config.h"
55
Paul Bakker1496d382011-05-23 12:07:29 +000056#include "polarssl/base64.h"
57#include "polarssl/error.h"
58#include "polarssl/net.h"
59#include "polarssl/ssl.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000060#include "polarssl/entropy.h"
61#include "polarssl/ctr_drbg.h"
Paul Bakker1496d382011-05-23 12:07:29 +000062#include "polarssl/certs.h"
63#include "polarssl/x509.h"
64
65#define DFL_SERVER_NAME "localhost"
66#define DFL_SERVER_PORT 465
67#define DFL_USER_NAME "user"
68#define DFL_USER_PWD "password"
69#define DFL_MAIL_FROM ""
70#define DFL_MAIL_TO ""
71#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000072#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000073#define DFL_CRT_FILE ""
74#define DFL_KEY_FILE ""
75#define DFL_FORCE_CIPHER 0
76#define DFL_MODE 0
77#define DFL_AUTHENTICATION 0
78
79#define MODE_SSL_TLS 0
80#define MODE_STARTTLS 0
81
82/*
83 * global options
84 */
85struct options
86{
Paul Bakkere0225e42013-06-06 12:52:24 +020087 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +000088 int server_port; /* port on which the ssl service runs */
89 int debug_level; /* level of debugging */
90 int authentication; /* if authentication is required */
91 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkere0225e42013-06-06 12:52:24 +020092 const char *user_name; /* username to use for authentication */
93 const char *user_pwd; /* password to use for authentication */
94 const char *mail_from; /* E-Mail address to use as sender */
95 const char *mail_to; /* E-Mail address to use as recipient */
96 const char *ca_file; /* the file with the CA certificate(s) */
97 const char *crt_file; /* the file with the client certificate */
98 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +000099 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
100} opt;
101
102void my_debug( void *ctx, int level, const char *str )
103{
104 if( level < opt.debug_level )
105 {
106 fprintf( (FILE *) ctx, "%s", str );
107 fflush( (FILE *) ctx );
108 }
109}
110
Paul Bakker508ad5a2011-12-04 17:09:26 +0000111#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +0000112 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +0000113 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
114 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +0000115int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000116{
Paul Bakkercce9d772011-11-18 14:26:47 +0000117 ((void) argc);
118 ((void) argv);
119
Paul Bakker508ad5a2011-12-04 17:09:26 +0000120 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +0000121 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +0000122 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
123 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +0000124 return( 0 );
125}
126#else
Paul Bakker1496d382011-05-23 12:07:29 +0000127int do_handshake( ssl_context *ssl, struct options *opt )
128{
129 int ret;
130 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000131 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000132
133 /*
134 * 4. Handshake
135 */
136 printf( " . Performing the SSL/TLS handshake..." );
137 fflush( stdout );
138
139 while( ( ret = ssl_handshake( ssl ) ) != 0 )
140 {
141 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
142 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000143#if defined(POLARSSL_ERROR_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000144 error_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000145#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000146 printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
147 return( -1 );
148 }
149 }
150
151 printf( " ok\n [ Ciphersuite is %s ]\n",
152 ssl_get_ciphersuite( ssl ) );
153
154 /*
155 * 5. Verify the server certificate
156 */
157 printf( " . Verifying peer X.509 certificate..." );
158
Manuel Pégourié-Gonnard516eb622014-03-11 11:10:27 +0100159 /* In real life, we may want to bail out when ret != 0 */
Paul Bakker1496d382011-05-23 12:07:29 +0000160 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
161 {
162 printf( " failed\n" );
163
164 if( ( ret & BADCERT_EXPIRED ) != 0 )
165 printf( " ! server certificate has expired\n" );
166
167 if( ( ret & BADCERT_REVOKED ) != 0 )
168 printf( " ! server certificate has been revoked\n" );
169
170 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
171 printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
172
173 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
174 printf( " ! self-signed or not signed by a trusted CA\n" );
175
176 printf( "\n" );
177 }
178 else
179 printf( " ok\n" );
180
181 printf( " . Peer certificate information ...\n" );
Paul Bakker48916f92012-09-16 19:57:18 +0000182 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakker645ce3a2012-10-31 12:32:41 +0000183 ssl_get_peer_cert( ssl ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000184 printf( "%s\n", buf );
185
186 return( 0 );
187}
188
189int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
190{
191 int ret;
192
193 printf("\n%s", buf);
194 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
195 {
196 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
197 {
198 printf( " failed\n ! ssl_write returned %d\n\n", ret );
199 return -1;
200 }
201 }
202
203 return( 0 );
204}
205
206int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
207{
208 int ret;
209 unsigned char data[128];
210 char code[4];
211 size_t i, idx = 0;
212
213 printf("\n%s", buf);
214 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
215 {
216 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
217 {
218 printf( " failed\n ! ssl_write returned %d\n\n", ret );
219 return -1;
220 }
221 }
222
223 do
224 {
225 len = sizeof( data ) - 1;
226 memset( data, 0, sizeof( data ) );
227 ret = ssl_read( ssl, data, len );
228
229 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
230 continue;
231
232 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
233 return -1;
234
235 if( ret <= 0 )
236 {
237 printf( "failed\n ! ssl_read returned %d\n\n", ret );
238 return -1;
239 }
240
241 printf("\n%s", data);
242 len = ret;
243 for( i = 0; i < len; i++ )
244 {
245 if( data[i] != '\n' )
246 {
247 if( idx < 4 )
248 code[ idx++ ] = data[i];
249 continue;
250 }
251
252 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
253 {
254 code[3] = '\0';
255 return atoi( code );
256 }
257
258 idx = 0;
259 }
260 }
261 while( 1 );
262}
263
264int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
265{
266 int ret;
267 unsigned char data[128];
268 char code[4];
269 size_t i, idx = 0;
270
271 printf("\n%s", buf);
272 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
273 {
274 printf( " failed\n ! ssl_write returned %d\n\n", ret );
275 return -1;
276 }
277
278 do
279 {
280 len = sizeof( data ) - 1;
281 memset( data, 0, sizeof( data ) );
282 ret = read( sock_fd, data, len );
283
284 if( ret <= 0 )
285 {
286 printf( "failed\n ! read returned %d\n\n", ret );
287 return -1;
288 }
289
Paul Bakkerd6d1f412014-04-17 16:03:48 +0200290 data[len] = '\0';
Paul Bakker1496d382011-05-23 12:07:29 +0000291 printf("\n%s", data);
292 len = ret;
293 for( i = 0; i < len; i++ )
294 {
295 if( data[i] != '\n' )
296 {
297 if( idx < 4 )
298 code[ idx++ ] = data[i];
299 continue;
300 }
301
302 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
303 {
304 code[3] = '\0';
305 return atoi( code );
306 }
307
308 idx = 0;
309 }
310 }
311 while( 1 );
312}
313
Paul Bakker5690efc2011-05-26 13:16:06 +0000314#if defined(POLARSSL_BASE64_C)
315#define USAGE_AUTH \
316 " authentication=%%d default: 0 (disabled)\n" \
317 " user_name=%%s default: \"user\"\n" \
318 " user_pwd=%%s default: \"password\"\n"
319#else
320#define USAGE_AUTH \
321 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
322#endif /* POLARSSL_BASE64_C */
323
324#if defined(POLARSSL_FS_IO)
325#define USAGE_IO \
326 " ca_file=%%s default: \"\" (pre-loaded)\n" \
327 " crt_file=%%s default: \"\" (pre-loaded)\n" \
328 " key_file=%%s default: \"\" (pre-loaded)\n"
329#else
330#define USAGE_IO \
331 " No file operations available (POLARSSL_FS_IO not defined)\n"
332#endif /* POLARSSL_FS_IO */
333
Paul Bakker1496d382011-05-23 12:07:29 +0000334#define USAGE \
335 "\n usage: ssl_mail_client param=<>...\n" \
336 "\n acceptable parameters:\n" \
337 " server_name=%%s default: localhost\n" \
338 " server_port=%%d default: 4433\n" \
339 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000340 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000341 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000342 " mail_from=%%s default: \"\"\n" \
343 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000344 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000345 " force_ciphersuite=<name> default: all enabled\n"\
346 " acceptable ciphersuite names:\n"
347
348int main( int argc, char *argv[] )
349{
350 int ret = 0, len, server_fd;
351 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000352#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000353 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000354#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000355 char hostname[32];
Paul Bakkere0225e42013-06-06 12:52:24 +0200356 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000357
358 entropy_context entropy;
359 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000360 ssl_context ssl;
Paul Bakker1496d382011-05-23 12:07:29 +0000361 x509_cert cacert;
362 x509_cert clicert;
363 rsa_context rsa;
364 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000365 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000366 char *p, *q;
367 const int *list;
368
369 /*
370 * Make sure memory references are valid.
371 */
372 server_fd = 0;
Paul Bakker1496d382011-05-23 12:07:29 +0000373 memset( &cacert, 0, sizeof( x509_cert ) );
374 memset( &clicert, 0, sizeof( x509_cert ) );
375 memset( &rsa, 0, sizeof( rsa_context ) );
Paul Bakkera16e7f22014-07-09 14:58:11 +0200376 memset( &ssl, 0, sizeof( ssl_context ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000377
Manuel Pégourié-Gonnard982eda32014-10-23 15:20:19 +0200378 memset( buf, 0, sizeof( buf ) );
379
Paul Bakker1496d382011-05-23 12:07:29 +0000380 if( argc == 0 )
381 {
382 usage:
383 printf( USAGE );
384
385 list = ssl_list_ciphersuites();
386 while( *list )
387 {
388 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
389 list++;
390 }
391 printf("\n");
392 goto exit;
393 }
394
395 opt.server_name = DFL_SERVER_NAME;
396 opt.server_port = DFL_SERVER_PORT;
397 opt.debug_level = DFL_DEBUG_LEVEL;
398 opt.authentication = DFL_AUTHENTICATION;
399 opt.mode = DFL_MODE;
400 opt.user_name = DFL_USER_NAME;
401 opt.user_pwd = DFL_USER_PWD;
402 opt.mail_from = DFL_MAIL_FROM;
403 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000404 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000405 opt.crt_file = DFL_CRT_FILE;
406 opt.key_file = DFL_KEY_FILE;
407 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
408
409 for( i = 1; i < argc; i++ )
410 {
Paul Bakker1496d382011-05-23 12:07:29 +0000411 p = argv[i];
412 if( ( q = strchr( p, '=' ) ) == NULL )
413 goto usage;
414 *q++ = '\0';
415
416 if( strcmp( p, "server_name" ) == 0 )
417 opt.server_name = q;
418 else if( strcmp( p, "server_port" ) == 0 )
419 {
420 opt.server_port = atoi( q );
421 if( opt.server_port < 1 || opt.server_port > 65535 )
422 goto usage;
423 }
424 else if( strcmp( p, "debug_level" ) == 0 )
425 {
426 opt.debug_level = atoi( q );
427 if( opt.debug_level < 0 || opt.debug_level > 65535 )
428 goto usage;
429 }
430 else if( strcmp( p, "authentication" ) == 0 )
431 {
432 opt.authentication = atoi( q );
433 if( opt.authentication < 0 || opt.authentication > 1 )
434 goto usage;
435 }
436 else if( strcmp( p, "mode" ) == 0 )
437 {
438 opt.mode = atoi( q );
439 if( opt.mode < 0 || opt.mode > 1 )
440 goto usage;
441 }
442 else if( strcmp( p, "user_name" ) == 0 )
443 opt.user_name = q;
444 else if( strcmp( p, "user_pwd" ) == 0 )
445 opt.user_pwd = q;
446 else if( strcmp( p, "mail_from" ) == 0 )
447 opt.mail_from = q;
448 else if( strcmp( p, "mail_to" ) == 0 )
449 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000450 else if( strcmp( p, "ca_file" ) == 0 )
451 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000452 else if( strcmp( p, "crt_file" ) == 0 )
453 opt.crt_file = q;
454 else if( strcmp( p, "key_file" ) == 0 )
455 opt.key_file = q;
456 else if( strcmp( p, "force_ciphersuite" ) == 0 )
457 {
458 opt.force_ciphersuite[0] = -1;
459
460 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
461
462 if( opt.force_ciphersuite[0] <= 0 )
463 goto usage;
464
465 opt.force_ciphersuite[1] = 0;
466 }
467 else
468 goto usage;
469 }
470
471 /*
472 * 0. Initialize the RNG and the session data
473 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000474 printf( "\n . Seeding the random number generator..." );
475 fflush( stdout );
476
477 entropy_init( &entropy );
478 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +0200479 (const unsigned char *) pers,
480 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000481 {
482 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
483 goto exit;
484 }
485
486 printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000487
488 /*
489 * 1.1. Load the trusted CA
490 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000491 printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000492 fflush( stdout );
493
Paul Bakker5690efc2011-05-26 13:16:06 +0000494#if defined(POLARSSL_FS_IO)
495 if( strlen( opt.ca_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000496 ret = x509parse_crtfile( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000497 else
498#endif
499#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200500 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000501 strlen( test_ca_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000502#else
503 {
504 ret = 1;
505 printf("POLARSSL_CERTS_C not defined.");
506 }
507#endif
Paul Bakker42488232012-05-16 08:21:05 +0000508 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000509 {
510 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
511 goto exit;
512 }
513
Paul Bakker42488232012-05-16 08:21:05 +0000514 printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000515
516 /*
517 * 1.2. Load own certificate and private key
518 *
519 * (can be skipped if client authentication is not required)
520 */
521 printf( " . Loading the client cert. and key..." );
522 fflush( stdout );
523
Paul Bakker5690efc2011-05-26 13:16:06 +0000524#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000525 if( strlen( opt.crt_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000526 ret = x509parse_crtfile( &clicert, opt.crt_file );
Paul Bakker1496d382011-05-23 12:07:29 +0000527 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000528#endif
529#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200530 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000531 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000532#else
533 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000534 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000535 printf("POLARSSL_CERTS_C not defined.");
536 }
537#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000538 if( ret != 0 )
539 {
540 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
541 goto exit;
542 }
543
Paul Bakker5690efc2011-05-26 13:16:06 +0000544#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000545 if( strlen( opt.key_file ) )
546 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
547 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000548#endif
549#if defined(POLARSSL_CERTS_C)
Paul Bakkere0225e42013-06-06 12:52:24 +0200550 ret = x509parse_key( &rsa, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000551 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000552#else
553 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000554 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000555 printf("POLARSSL_CERTS_C not defined.");
556 }
557#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000558 if( ret != 0 )
559 {
560 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
561 goto exit;
562 }
563
564 printf( " ok\n" );
565
566 /*
567 * 2. Start the connection
568 */
569 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
570 opt.server_port );
571 fflush( stdout );
572
573 if( ( ret = net_connect( &server_fd, opt.server_name,
574 opt.server_port ) ) != 0 )
575 {
576 printf( " failed\n ! net_connect returned %d\n\n", ret );
577 goto exit;
578 }
579
580 printf( " ok\n" );
581
582 /*
583 * 3. Setup stuff
584 */
585 printf( " . Setting up the SSL/TLS structure..." );
586 fflush( stdout );
587
Paul Bakker1496d382011-05-23 12:07:29 +0000588 if( ( ret = ssl_init( &ssl ) ) != 0 )
589 {
590 printf( " failed\n ! ssl_init returned %d\n\n", ret );
591 goto exit;
592 }
593
594 printf( " ok\n" );
595
596 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
Manuel Pégourié-Gonnard516eb622014-03-11 11:10:27 +0100597 /* OPTIONAL is not optimal for security,
598 * but makes interop easier in this simplified example */
Paul Bakker1496d382011-05-23 12:07:29 +0000599 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
600
Paul Bakker508ad5a2011-12-04 17:09:26 +0000601 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000602 ssl_set_dbg( &ssl, my_debug, stdout );
603 ssl_set_bio( &ssl, net_recv, &server_fd,
604 net_send, &server_fd );
605
Paul Bakker645ce3a2012-10-31 12:32:41 +0000606 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000607 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
608
Paul Bakker1496d382011-05-23 12:07:29 +0000609 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
610 ssl_set_own_cert( &ssl, &clicert, &rsa );
611
612 ssl_set_hostname( &ssl, opt.server_name );
613
614 if( opt.mode == MODE_SSL_TLS )
615 {
616 if( do_handshake( &ssl, &opt ) != 0 )
617 goto exit;
618
619 printf( " > Get header from server:" );
620 fflush( stdout );
621
622 ret = write_ssl_and_get_response( &ssl, buf, 0 );
623 if( ret < 200 || ret > 299 )
624 {
625 printf( " failed\n ! server responded with %d\n\n", ret );
626 goto exit;
627 }
628
629 printf(" ok\n" );
630
631 printf( " > Write EHLO to server:" );
632 fflush( stdout );
633
634 gethostname( hostname, 32 );
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100635 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000636 ret = write_ssl_and_get_response( &ssl, buf, len );
637 if( ret < 200 || ret > 299 )
638 {
639 printf( " failed\n ! server responded with %d\n\n", ret );
640 goto exit;
641 }
642 }
643 else
644 {
645 printf( " > Get header from server:" );
646 fflush( stdout );
647
648 ret = write_and_get_response( server_fd, buf, 0 );
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 EHLO to server:" );
658 fflush( stdout );
659
660 gethostname( hostname, 32 );
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100661 len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
Paul Bakker1496d382011-05-23 12:07:29 +0000662 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 printf( " > Write STARTTLS to server:" );
672 fflush( stdout );
673
674 gethostname( hostname, 32 );
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100675 len = sprintf( (char *) buf, "STARTTLS\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000676 ret = write_and_get_response( server_fd, buf, len );
677 if( ret < 200 || ret > 299 )
678 {
679 printf( " failed\n ! server responded with %d\n\n", ret );
680 goto exit;
681 }
682
683 printf(" ok\n" );
684
685 if( do_handshake( &ssl, &opt ) != 0 )
686 goto exit;
687 }
688
Paul Bakker5690efc2011-05-26 13:16:06 +0000689#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000690 if( opt.authentication )
691 {
692 printf( " > Write AUTH LOGIN to server:" );
693 fflush( stdout );
694
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100695 len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000696 ret = write_ssl_and_get_response( &ssl, buf, len );
697 if( ret < 200 || ret > 399 )
698 {
699 printf( " failed\n ! server responded with %d\n\n", ret );
700 goto exit;
701 }
702
703 printf(" ok\n" );
704
705 printf( " > Write username to server: %s", opt.user_name );
706 fflush( stdout );
707
708 n = sizeof( buf );
Paul Bakkere0225e42013-06-06 12:52:24 +0200709 len = base64_encode( base, &n, (const unsigned char *) opt.user_name,
710 strlen( opt.user_name ) );
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100711 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000712 ret = write_ssl_and_get_response( &ssl, buf, len );
713 if( ret < 300 || ret > 399 )
714 {
715 printf( " failed\n ! server responded with %d\n\n", ret );
716 goto exit;
717 }
718
719 printf(" ok\n" );
720
721 printf( " > Write password to server: %s", opt.user_pwd );
722 fflush( stdout );
723
Paul Bakkere0225e42013-06-06 12:52:24 +0200724 len = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
725 strlen( opt.user_pwd ) );
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100726 len = sprintf( (char *) buf, "%s\r\n", base );
Paul Bakker1496d382011-05-23 12:07:29 +0000727 ret = write_ssl_and_get_response( &ssl, buf, len );
728 if( ret < 200 || ret > 399 )
729 {
730 printf( " failed\n ! server responded with %d\n\n", ret );
731 goto exit;
732 }
733
734 printf(" ok\n" );
735 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000736#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000737
738 printf( " > Write MAIL FROM to server:" );
739 fflush( stdout );
740
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100741 len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
Paul Bakker1496d382011-05-23 12:07:29 +0000742 ret = write_ssl_and_get_response( &ssl, buf, len );
743 if( ret < 200 || ret > 299 )
744 {
745 printf( " failed\n ! server responded with %d\n\n", ret );
746 goto exit;
747 }
748
749 printf(" ok\n" );
750
751 printf( " > Write RCPT TO to server:" );
752 fflush( stdout );
753
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100754 len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
Paul Bakker1496d382011-05-23 12:07:29 +0000755 ret = write_ssl_and_get_response( &ssl, buf, len );
756 if( ret < 200 || ret > 299 )
757 {
758 printf( " failed\n ! server responded with %d\n\n", ret );
759 goto exit;
760 }
761
762 printf(" ok\n" );
763
764 printf( " > Write DATA to server:" );
765 fflush( stdout );
766
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100767 len = sprintf( (char *) buf, "DATA\r\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000768 ret = write_ssl_and_get_response( &ssl, buf, len );
769 if( ret < 300 || ret > 399 )
770 {
771 printf( " failed\n ! server responded with %d\n\n", ret );
772 goto exit;
773 }
774
775 printf(" ok\n" );
776
777 printf( " > Write content to server:" );
778 fflush( stdout );
779
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100780 len = sprintf( (char *) buf, "From: %s\r\nSubject: PolarSSL Test mail\r\n\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000781 "This is a simple test mail from the "
Paul Bakkeraf0ccc82014-01-24 16:11:17 +0100782 "PolarSSL mail client example.\r\n"
783 "\r\n"
Paul Bakker1496d382011-05-23 12:07:29 +0000784 "Enjoy!", opt.mail_from );
785 ret = write_ssl_data( &ssl, buf, len );
786
787 len = sprintf( (char *) buf, "\r\n.\r\n");
788 ret = write_ssl_and_get_response( &ssl, buf, len );
789 if( ret < 200 || ret > 299 )
790 {
791 printf( " failed\n ! server responded with %d\n\n", ret );
792 goto exit;
793 }
794
795 printf(" ok\n" );
796
797 ssl_close_notify( &ssl );
798
799exit:
800
801 if( server_fd )
802 net_close( server_fd );
803 x509_free( &clicert );
804 x509_free( &cacert );
805 rsa_free( &rsa );
806 ssl_free( &ssl );
807
Paul Bakkercce9d772011-11-18 14:26:47 +0000808#if defined(_WIN32)
Paul Bakker1496d382011-05-23 12:07:29 +0000809 printf( " + Press Enter to exit this program.\n" );
810 fflush( stdout ); getchar();
811#endif
812
813 return( ret );
814}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000815#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
816 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
817 POLARSSL_CTR_DRBG_C */