blob: d0fed72b80efff99ea3b5f407f84bf49005f160d [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
58#define DFL_SERVER_NAME "localhost"
59#define DFL_SERVER_PORT 465
60#define DFL_USER_NAME "user"
61#define DFL_USER_PWD "password"
62#define DFL_MAIL_FROM ""
63#define DFL_MAIL_TO ""
64#define DFL_DEBUG_LEVEL 0
Paul Bakker5690efc2011-05-26 13:16:06 +000065#define DFL_CA_FILE ""
Paul Bakker1496d382011-05-23 12:07:29 +000066#define DFL_CRT_FILE ""
67#define DFL_KEY_FILE ""
68#define DFL_FORCE_CIPHER 0
69#define DFL_MODE 0
70#define DFL_AUTHENTICATION 0
71
72#define MODE_SSL_TLS 0
73#define MODE_STARTTLS 0
74
75/*
76 * global options
77 */
78struct options
79{
Paul Bakkeref3f8c72013-06-24 13:01:08 +020080 const char *server_name; /* hostname of the server (client only) */
Paul Bakker1496d382011-05-23 12:07:29 +000081 int server_port; /* port on which the ssl service runs */
82 int debug_level; /* level of debugging */
83 int authentication; /* if authentication is required */
84 int mode; /* SSL/TLS (0) or STARTTLS (1) */
Paul Bakkeref3f8c72013-06-24 13:01:08 +020085 const char *user_name; /* username to use for authentication */
86 const char *user_pwd; /* password to use for authentication */
87 const char *mail_from; /* E-Mail address to use as sender */
88 const char *mail_to; /* E-Mail address to use as recipient */
89 const char *ca_file; /* the file with the CA certificate(s) */
90 const char *crt_file; /* the file with the client certificate */
91 const char *key_file; /* the file with the client key */
Paul Bakker1496d382011-05-23 12:07:29 +000092 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
93} opt;
94
Paul Bakker3c5ef712013-06-25 16:37:45 +020095static void my_debug( void *ctx, int level, const char *str )
Paul Bakker1496d382011-05-23 12:07:29 +000096{
97 if( level < opt.debug_level )
98 {
99 fprintf( (FILE *) ctx, "%s", str );
100 fflush( (FILE *) ctx );
101 }
102}
103
Paul Bakker508ad5a2011-12-04 17:09:26 +0000104#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +0000105 !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +0000106 !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +0200107 !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +0000108int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000109{
Paul Bakkercce9d772011-11-18 14:26:47 +0000110 ((void) argc);
111 ((void) argv);
112
Paul Bakker508ad5a2011-12-04 17:09:26 +0000113 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +0000114 "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +0000115 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakkered27a042013-04-18 22:46:23 +0200116 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_PARSE_C "
117 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +0000118 return( 0 );
119}
120#else
Paul Bakker3c5ef712013-06-25 16:37:45 +0200121static int do_handshake( ssl_context *ssl, struct options *opt )
Paul Bakker1496d382011-05-23 12:07:29 +0000122{
123 int ret;
124 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000125 memset(buf, 0, 1024);
Paul Bakker1496d382011-05-23 12:07:29 +0000126
127 /*
128 * 4. Handshake
129 */
130 printf( " . Performing the SSL/TLS handshake..." );
131 fflush( stdout );
132
133 while( ( ret = ssl_handshake( ssl ) ) != 0 )
134 {
135 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
136 {
Paul Bakker5690efc2011-05-26 13:16:06 +0000137#if defined(POLARSSL_ERROR_C)
Paul Bakker03a8a792013-06-30 12:18:08 +0200138 polarssl_strerror( ret, (char *) buf, 1024 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000139#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000140 printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
141 return( -1 );
142 }
143 }
144
145 printf( " ok\n [ Ciphersuite is %s ]\n",
146 ssl_get_ciphersuite( ssl ) );
147
148 /*
149 * 5. Verify the server certificate
150 */
151 printf( " . Verifying peer X.509 certificate..." );
152
153 if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
154 {
155 printf( " failed\n" );
156
157 if( ( ret & BADCERT_EXPIRED ) != 0 )
158 printf( " ! server certificate has expired\n" );
159
160 if( ( ret & BADCERT_REVOKED ) != 0 )
161 printf( " ! server certificate has been revoked\n" );
162
163 if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
164 printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
165
166 if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
167 printf( " ! self-signed or not signed by a trusted CA\n" );
168
169 printf( "\n" );
170 }
171 else
172 printf( " ok\n" );
173
174 printf( " . Peer certificate information ...\n" );
Paul Bakker48916f92012-09-16 19:57:18 +0000175 x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ",
Paul Bakker645ce3a2012-10-31 12:32:41 +0000176 ssl_get_peer_cert( ssl ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000177 printf( "%s\n", buf );
178
179 return( 0 );
180}
181
Paul Bakker3c5ef712013-06-25 16:37:45 +0200182static int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000183{
184 int ret;
185
186 printf("\n%s", buf);
187 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
188 {
189 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
190 {
191 printf( " failed\n ! ssl_write returned %d\n\n", ret );
192 return -1;
193 }
194 }
195
196 return( 0 );
197}
198
Paul Bakker3c5ef712013-06-25 16:37:45 +0200199static int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000200{
201 int ret;
202 unsigned char data[128];
203 char code[4];
204 size_t i, idx = 0;
205
206 printf("\n%s", buf);
207 while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
208 {
209 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
210 {
211 printf( " failed\n ! ssl_write returned %d\n\n", ret );
212 return -1;
213 }
214 }
215
216 do
217 {
218 len = sizeof( data ) - 1;
219 memset( data, 0, sizeof( data ) );
220 ret = ssl_read( ssl, data, len );
221
222 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
223 continue;
224
225 if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
226 return -1;
227
228 if( ret <= 0 )
229 {
230 printf( "failed\n ! ssl_read returned %d\n\n", ret );
231 return -1;
232 }
233
234 printf("\n%s", data);
235 len = ret;
236 for( i = 0; i < len; i++ )
237 {
238 if( data[i] != '\n' )
239 {
240 if( idx < 4 )
241 code[ idx++ ] = data[i];
242 continue;
243 }
244
245 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
246 {
247 code[3] = '\0';
248 return atoi( code );
249 }
Paul Bakker3c5ef712013-06-25 16:37:45 +0200250
Paul Bakker1496d382011-05-23 12:07:29 +0000251 idx = 0;
252 }
253 }
254 while( 1 );
255}
256
Paul Bakker3c5ef712013-06-25 16:37:45 +0200257static int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
Paul Bakker1496d382011-05-23 12:07:29 +0000258{
259 int ret;
260 unsigned char data[128];
261 char code[4];
262 size_t i, idx = 0;
263
264 printf("\n%s", buf);
265 if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
266 {
267 printf( " failed\n ! ssl_write returned %d\n\n", ret );
268 return -1;
269 }
270
271 do
272 {
273 len = sizeof( data ) - 1;
274 memset( data, 0, sizeof( data ) );
275 ret = read( sock_fd, data, len );
276
277 if( ret <= 0 )
278 {
279 printf( "failed\n ! read returned %d\n\n", ret );
280 return -1;
281 }
282
283 printf("\n%s", data);
284 len = ret;
285 for( i = 0; i < len; i++ )
286 {
287 if( data[i] != '\n' )
288 {
289 if( idx < 4 )
290 code[ idx++ ] = data[i];
291 continue;
292 }
293
294 if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
295 {
296 code[3] = '\0';
297 return atoi( code );
298 }
299
300 idx = 0;
301 }
302 }
303 while( 1 );
304}
305
Paul Bakker5690efc2011-05-26 13:16:06 +0000306#if defined(POLARSSL_BASE64_C)
307#define USAGE_AUTH \
308 " authentication=%%d default: 0 (disabled)\n" \
309 " user_name=%%s default: \"user\"\n" \
310 " user_pwd=%%s default: \"password\"\n"
311#else
312#define USAGE_AUTH \
313 " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
314#endif /* POLARSSL_BASE64_C */
315
316#if defined(POLARSSL_FS_IO)
317#define USAGE_IO \
318 " ca_file=%%s default: \"\" (pre-loaded)\n" \
319 " crt_file=%%s default: \"\" (pre-loaded)\n" \
320 " key_file=%%s default: \"\" (pre-loaded)\n"
321#else
322#define USAGE_IO \
323 " No file operations available (POLARSSL_FS_IO not defined)\n"
324#endif /* POLARSSL_FS_IO */
325
Paul Bakker1496d382011-05-23 12:07:29 +0000326#define USAGE \
327 "\n usage: ssl_mail_client param=<>...\n" \
328 "\n acceptable parameters:\n" \
329 " server_name=%%s default: localhost\n" \
330 " server_port=%%d default: 4433\n" \
331 " debug_level=%%d default: 0 (disabled)\n" \
Paul Bakker1496d382011-05-23 12:07:29 +0000332 " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000333 USAGE_AUTH \
Paul Bakker1496d382011-05-23 12:07:29 +0000334 " mail_from=%%s default: \"\"\n" \
335 " mail_to=%%s default: \"\"\n" \
Paul Bakker5690efc2011-05-26 13:16:06 +0000336 USAGE_IO \
Paul Bakker1496d382011-05-23 12:07:29 +0000337 " force_ciphersuite=<name> default: all enabled\n"\
338 " acceptable ciphersuite names:\n"
339
340int main( int argc, char *argv[] )
341{
342 int ret = 0, len, server_fd;
343 unsigned char buf[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000344#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000345 unsigned char base[1024];
Paul Bakker5690efc2011-05-26 13:16:06 +0000346#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000347 char hostname[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200348 const char *pers = "ssl_mail_client";
Paul Bakker508ad5a2011-12-04 17:09:26 +0000349
350 entropy_context entropy;
351 ctr_drbg_context ctr_drbg;
Paul Bakker1496d382011-05-23 12:07:29 +0000352 ssl_context ssl;
Paul Bakker1496d382011-05-23 12:07:29 +0000353 x509_cert cacert;
354 x509_cert clicert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200355 pk_context pkey;
Paul Bakker1496d382011-05-23 12:07:29 +0000356 int i;
Paul Bakker819370c2012-09-28 07:04:41 +0000357 size_t n;
Paul Bakker1496d382011-05-23 12:07:29 +0000358 char *p, *q;
359 const int *list;
360
361 /*
362 * Make sure memory references are valid.
363 */
364 server_fd = 0;
Paul Bakker1496d382011-05-23 12:07:29 +0000365 memset( &cacert, 0, sizeof( x509_cert ) );
366 memset( &clicert, 0, sizeof( x509_cert ) );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200367 pk_init( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000368
369 if( argc == 0 )
370 {
371 usage:
372 printf( USAGE );
373
374 list = ssl_list_ciphersuites();
375 while( *list )
376 {
377 printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
378 list++;
379 }
380 printf("\n");
381 goto exit;
382 }
383
384 opt.server_name = DFL_SERVER_NAME;
385 opt.server_port = DFL_SERVER_PORT;
386 opt.debug_level = DFL_DEBUG_LEVEL;
387 opt.authentication = DFL_AUTHENTICATION;
388 opt.mode = DFL_MODE;
389 opt.user_name = DFL_USER_NAME;
390 opt.user_pwd = DFL_USER_PWD;
391 opt.mail_from = DFL_MAIL_FROM;
392 opt.mail_to = DFL_MAIL_TO;
Paul Bakker5690efc2011-05-26 13:16:06 +0000393 opt.ca_file = DFL_CA_FILE;
Paul Bakker1496d382011-05-23 12:07:29 +0000394 opt.crt_file = DFL_CRT_FILE;
395 opt.key_file = DFL_KEY_FILE;
396 opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
397
398 for( i = 1; i < argc; i++ )
399 {
Paul Bakker1496d382011-05-23 12:07:29 +0000400 p = argv[i];
401 if( ( q = strchr( p, '=' ) ) == NULL )
402 goto usage;
403 *q++ = '\0';
404
405 if( strcmp( p, "server_name" ) == 0 )
406 opt.server_name = q;
407 else if( strcmp( p, "server_port" ) == 0 )
408 {
409 opt.server_port = atoi( q );
410 if( opt.server_port < 1 || opt.server_port > 65535 )
411 goto usage;
412 }
413 else if( strcmp( p, "debug_level" ) == 0 )
414 {
415 opt.debug_level = atoi( q );
416 if( opt.debug_level < 0 || opt.debug_level > 65535 )
417 goto usage;
418 }
419 else if( strcmp( p, "authentication" ) == 0 )
420 {
421 opt.authentication = atoi( q );
422 if( opt.authentication < 0 || opt.authentication > 1 )
423 goto usage;
424 }
425 else if( strcmp( p, "mode" ) == 0 )
426 {
427 opt.mode = atoi( q );
428 if( opt.mode < 0 || opt.mode > 1 )
429 goto usage;
430 }
431 else if( strcmp( p, "user_name" ) == 0 )
432 opt.user_name = q;
433 else if( strcmp( p, "user_pwd" ) == 0 )
434 opt.user_pwd = q;
435 else if( strcmp( p, "mail_from" ) == 0 )
436 opt.mail_from = q;
437 else if( strcmp( p, "mail_to" ) == 0 )
438 opt.mail_to = q;
Paul Bakker5690efc2011-05-26 13:16:06 +0000439 else if( strcmp( p, "ca_file" ) == 0 )
440 opt.ca_file = q;
Paul Bakker1496d382011-05-23 12:07:29 +0000441 else if( strcmp( p, "crt_file" ) == 0 )
442 opt.crt_file = q;
443 else if( strcmp( p, "key_file" ) == 0 )
444 opt.key_file = q;
445 else if( strcmp( p, "force_ciphersuite" ) == 0 )
446 {
447 opt.force_ciphersuite[0] = -1;
448
449 opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
450
451 if( opt.force_ciphersuite[0] <= 0 )
452 goto usage;
453
454 opt.force_ciphersuite[1] = 0;
455 }
456 else
457 goto usage;
458 }
459
460 /*
461 * 0. Initialize the RNG and the session data
462 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000463 printf( "\n . Seeding the random number generator..." );
464 fflush( stdout );
465
466 entropy_init( &entropy );
467 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200468 (const unsigned char *) pers,
469 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000470 {
471 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
472 goto exit;
473 }
474
475 printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000476
477 /*
478 * 1.1. Load the trusted CA
479 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000480 printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000481 fflush( stdout );
482
Paul Bakker5690efc2011-05-26 13:16:06 +0000483#if defined(POLARSSL_FS_IO)
484 if( strlen( opt.ca_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000485 ret = x509parse_crtfile( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000486 else
487#endif
488#if defined(POLARSSL_CERTS_C)
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200489 ret = x509parse_crt( &cacert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000490 strlen( test_ca_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000491#else
492 {
493 ret = 1;
494 printf("POLARSSL_CERTS_C not defined.");
495 }
496#endif
Paul Bakker42488232012-05-16 08:21:05 +0000497 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000498 {
499 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
500 goto exit;
501 }
502
Paul Bakker42488232012-05-16 08:21:05 +0000503 printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000504
505 /*
506 * 1.2. Load own certificate and private key
507 *
508 * (can be skipped if client authentication is not required)
509 */
510 printf( " . Loading the client cert. and key..." );
511 fflush( stdout );
512
Paul Bakker5690efc2011-05-26 13:16:06 +0000513#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000514 if( strlen( opt.crt_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000515 ret = x509parse_crtfile( &clicert, opt.crt_file );
Paul Bakker1496d382011-05-23 12:07:29 +0000516 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000517#endif
518#if defined(POLARSSL_CERTS_C)
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200519 ret = x509parse_crt( &clicert, (const unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000520 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000521#else
522 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000523 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000524 printf("POLARSSL_CERTS_C not defined.");
525 }
526#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000527 if( ret != 0 )
528 {
529 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
530 goto exit;
531 }
532
Paul Bakker5690efc2011-05-26 13:16:06 +0000533#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000534 if( strlen( opt.key_file ) )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200535 ret = pk_parse_keyfile( &pkey, opt.key_file, "" );
Paul Bakker1496d382011-05-23 12:07:29 +0000536 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000537#endif
538#if defined(POLARSSL_CERTS_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200539 ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key,
Paul Bakker1496d382011-05-23 12:07:29 +0000540 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000541#else
542 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000543 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000544 printf("POLARSSL_CERTS_C not defined.");
545 }
546#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000547 if( ret != 0 )
548 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200549 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000550 goto exit;
551 }
552
553 printf( " ok\n" );
554
555 /*
556 * 2. Start the connection
557 */
558 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
559 opt.server_port );
560 fflush( stdout );
561
562 if( ( ret = net_connect( &server_fd, opt.server_name,
563 opt.server_port ) ) != 0 )
564 {
565 printf( " failed\n ! net_connect returned %d\n\n", ret );
566 goto exit;
567 }
568
569 printf( " ok\n" );
570
571 /*
572 * 3. Setup stuff
573 */
574 printf( " . Setting up the SSL/TLS structure..." );
575 fflush( stdout );
576
Paul Bakker1496d382011-05-23 12:07:29 +0000577 if( ( ret = ssl_init( &ssl ) ) != 0 )
578 {
579 printf( " failed\n ! ssl_init returned %d\n\n", ret );
580 goto exit;
581 }
582
583 printf( " ok\n" );
584
585 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
586 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
587
Paul Bakker508ad5a2011-12-04 17:09:26 +0000588 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000589 ssl_set_dbg( &ssl, my_debug, stdout );
590 ssl_set_bio( &ssl, net_recv, &server_fd,
591 net_send, &server_fd );
592
Paul Bakker645ce3a2012-10-31 12:32:41 +0000593 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000594 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
595
Paul Bakker1496d382011-05-23 12:07:29 +0000596 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200597 ssl_set_own_cert( &ssl, &clicert, &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000598
Paul Bakker0be444a2013-08-27 21:55:01 +0200599#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker1496d382011-05-23 12:07:29 +0000600 ssl_set_hostname( &ssl, opt.server_name );
Paul Bakker0be444a2013-08-27 21:55:01 +0200601#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000602
603 if( opt.mode == MODE_SSL_TLS )
604 {
605 if( do_handshake( &ssl, &opt ) != 0 )
606 goto exit;
607
608 printf( " > Get header from server:" );
609 fflush( stdout );
610
611 ret = write_ssl_and_get_response( &ssl, buf, 0 );
612 if( ret < 200 || ret > 299 )
613 {
614 printf( " failed\n ! server responded with %d\n\n", ret );
615 goto exit;
616 }
617
618 printf(" ok\n" );
619
620 printf( " > Write EHLO to server:" );
621 fflush( stdout );
622
623 gethostname( hostname, 32 );
624 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
625 ret = write_ssl_and_get_response( &ssl, buf, len );
626 if( ret < 200 || ret > 299 )
627 {
628 printf( " failed\n ! server responded with %d\n\n", ret );
629 goto exit;
630 }
631 }
632 else
633 {
634 printf( " > Get header from server:" );
635 fflush( stdout );
636
637 ret = write_and_get_response( server_fd, buf, 0 );
638 if( ret < 200 || ret > 299 )
639 {
640 printf( " failed\n ! server responded with %d\n\n", ret );
641 goto exit;
642 }
643
644 printf(" ok\n" );
645
646 printf( " > Write EHLO to server:" );
647 fflush( stdout );
648
649 gethostname( hostname, 32 );
650 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
651 ret = write_and_get_response( server_fd, buf, len );
652 if( ret < 200 || ret > 299 )
653 {
654 printf( " failed\n ! server responded with %d\n\n", ret );
655 goto exit;
656 }
657
658 printf(" ok\n" );
659
660 printf( " > Write STARTTLS to server:" );
661 fflush( stdout );
662
663 gethostname( hostname, 32 );
664 len = sprintf( (char *) buf, "STARTTLS\n" );
665 ret = write_and_get_response( server_fd, buf, len );
666 if( ret < 200 || ret > 299 )
667 {
668 printf( " failed\n ! server responded with %d\n\n", ret );
669 goto exit;
670 }
671
672 printf(" ok\n" );
673
674 if( do_handshake( &ssl, &opt ) != 0 )
675 goto exit;
676 }
677
Paul Bakker5690efc2011-05-26 13:16:06 +0000678#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000679 if( opt.authentication )
680 {
681 printf( " > Write AUTH LOGIN to server:" );
682 fflush( stdout );
683
684 len = sprintf( (char *) buf, "AUTH LOGIN\n" );
685 ret = write_ssl_and_get_response( &ssl, buf, len );
686 if( ret < 200 || ret > 399 )
687 {
688 printf( " failed\n ! server responded with %d\n\n", ret );
689 goto exit;
690 }
691
692 printf(" ok\n" );
693
694 printf( " > Write username to server: %s", opt.user_name );
695 fflush( stdout );
696
697 n = sizeof( buf );
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200698 len = base64_encode( base, &n, (const unsigned char *) opt.user_name,
699 strlen( opt.user_name ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000700 len = sprintf( (char *) buf, "%s\n", base );
701 ret = write_ssl_and_get_response( &ssl, buf, len );
702 if( ret < 300 || ret > 399 )
703 {
704 printf( " failed\n ! server responded with %d\n\n", ret );
705 goto exit;
706 }
707
708 printf(" ok\n" );
709
710 printf( " > Write password to server: %s", opt.user_pwd );
711 fflush( stdout );
712
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200713 len = base64_encode( base, &n, (const unsigned char *) opt.user_pwd,
714 strlen( opt.user_pwd ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000715 len = sprintf( (char *) buf, "%s\n", base );
716 ret = write_ssl_and_get_response( &ssl, buf, len );
717 if( ret < 200 || ret > 399 )
718 {
719 printf( " failed\n ! server responded with %d\n\n", ret );
720 goto exit;
721 }
722
723 printf(" ok\n" );
724 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000725#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000726
727 printf( " > Write MAIL FROM to server:" );
728 fflush( stdout );
729
730 len = sprintf( (char *) buf, "MAIL FROM:<%s>\n", opt.mail_from );
731 ret = write_ssl_and_get_response( &ssl, buf, len );
732 if( ret < 200 || ret > 299 )
733 {
734 printf( " failed\n ! server responded with %d\n\n", ret );
735 goto exit;
736 }
737
738 printf(" ok\n" );
739
740 printf( " > Write RCPT TO to server:" );
741 fflush( stdout );
742
743 len = sprintf( (char *) buf, "RCPT TO:<%s>\n", opt.mail_to );
744 ret = write_ssl_and_get_response( &ssl, buf, len );
745 if( ret < 200 || ret > 299 )
746 {
747 printf( " failed\n ! server responded with %d\n\n", ret );
748 goto exit;
749 }
750
751 printf(" ok\n" );
752
753 printf( " > Write DATA to server:" );
754 fflush( stdout );
755
756 len = sprintf( (char *) buf, "DATA\n" );
757 ret = write_ssl_and_get_response( &ssl, buf, len );
758 if( ret < 300 || ret > 399 )
759 {
760 printf( " failed\n ! server responded with %d\n\n", ret );
761 goto exit;
762 }
763
764 printf(" ok\n" );
765
766 printf( " > Write content to server:" );
767 fflush( stdout );
768
769 len = sprintf( (char *) buf, "From: %s\nSubject: PolarSSL Test mail\n\n"
770 "This is a simple test mail from the "
771 "PolarSSL mail client example.\n"
772 "\n"
773 "Enjoy!", opt.mail_from );
774 ret = write_ssl_data( &ssl, buf, len );
775
776 len = sprintf( (char *) buf, "\r\n.\r\n");
777 ret = write_ssl_and_get_response( &ssl, buf, len );
778 if( ret < 200 || ret > 299 )
779 {
780 printf( " failed\n ! server responded with %d\n\n", ret );
781 goto exit;
782 }
783
784 printf(" ok\n" );
785
786 ssl_close_notify( &ssl );
787
788exit:
789
790 if( server_fd )
791 net_close( server_fd );
792 x509_free( &clicert );
793 x509_free( &cacert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200794 pk_free( &pkey );
Paul Bakker1496d382011-05-23 12:07:29 +0000795 ssl_free( &ssl );
796
Paul Bakkercce9d772011-11-18 14:26:47 +0000797#if defined(_WIN32)
Paul Bakker1496d382011-05-23 12:07:29 +0000798 printf( " + Press Enter to exit this program.\n" );
799 fflush( stdout ); getchar();
800#endif
801
802 return( ret );
803}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000804#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
805 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
806 POLARSSL_CTR_DRBG_C */