blob: a2434bbb4275f86378561289355d43be8a133bf5 [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>
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{
80 char *server_name; /* hostname of the server (client only) */
81 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) */
85 char *user_name; /* username to use for authentication */
86 char *user_pwd; /* password to use for authentication */
87 char *mail_from; /* E-Mail address to use as sender */
88 char *mail_to; /* E-Mail address to use as recipient */
Paul Bakker5690efc2011-05-26 13:16:06 +000089 char *ca_file; /* the file with the CA certificate(s) */
Paul Bakker1496d382011-05-23 12:07:29 +000090 char *crt_file; /* the file with the client certificate */
91 char *key_file; /* the file with the client key */
92 int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
93} opt;
94
95void my_debug( void *ctx, int level, const char *str )
96{
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 Bakker1496d382011-05-23 12:07:29 +0000121int do_handshake( ssl_context *ssl, struct options *opt )
122{
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 Bakker1496d382011-05-23 12:07:29 +0000138 error_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
182int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
183{
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
199int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
200{
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 }
250
251 idx = 0;
252 }
253 }
254 while( 1 );
255}
256
257int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
258{
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 Bakker508ad5a2011-12-04 17:09:26 +0000348 char *pers = "ssl_mail_client";
349
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;
355 rsa_context rsa;
356 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 ) );
367 memset( &rsa, 0, sizeof( rsa_context ) );
368
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,
468 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
469 {
470 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
471 goto exit;
472 }
473
474 printf( " ok\n" );
Paul Bakker1496d382011-05-23 12:07:29 +0000475
476 /*
477 * 1.1. Load the trusted CA
478 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000479 printf( " . Loading the CA root certificate ..." );
Paul Bakker1496d382011-05-23 12:07:29 +0000480 fflush( stdout );
481
Paul Bakker5690efc2011-05-26 13:16:06 +0000482#if defined(POLARSSL_FS_IO)
483 if( strlen( opt.ca_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000484 ret = x509parse_crtfile( &cacert, opt.ca_file );
Paul Bakker5690efc2011-05-26 13:16:06 +0000485 else
486#endif
487#if defined(POLARSSL_CERTS_C)
488 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000489 strlen( test_ca_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000490#else
491 {
492 ret = 1;
493 printf("POLARSSL_CERTS_C not defined.");
494 }
495#endif
Paul Bakker42488232012-05-16 08:21:05 +0000496 if( ret < 0 )
Paul Bakker1496d382011-05-23 12:07:29 +0000497 {
498 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
499 goto exit;
500 }
501
Paul Bakker42488232012-05-16 08:21:05 +0000502 printf( " ok (%d skipped)\n", ret );
Paul Bakker1496d382011-05-23 12:07:29 +0000503
504 /*
505 * 1.2. Load own certificate and private key
506 *
507 * (can be skipped if client authentication is not required)
508 */
509 printf( " . Loading the client cert. and key..." );
510 fflush( stdout );
511
Paul Bakker5690efc2011-05-26 13:16:06 +0000512#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000513 if( strlen( opt.crt_file ) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000514 ret = x509parse_crtfile( &clicert, opt.crt_file );
Paul Bakker1496d382011-05-23 12:07:29 +0000515 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000516#endif
517#if defined(POLARSSL_CERTS_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000518 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000519 strlen( test_cli_crt ) );
Paul Bakker5690efc2011-05-26 13:16:06 +0000520#else
521 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000522 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000523 printf("POLARSSL_CERTS_C not defined.");
524 }
525#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000526 if( ret != 0 )
527 {
528 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
529 goto exit;
530 }
531
Paul Bakker5690efc2011-05-26 13:16:06 +0000532#if defined(POLARSSL_FS_IO)
Paul Bakker1496d382011-05-23 12:07:29 +0000533 if( strlen( opt.key_file ) )
534 ret = x509parse_keyfile( &rsa, opt.key_file, "" );
535 else
Paul Bakker5690efc2011-05-26 13:16:06 +0000536#endif
537#if defined(POLARSSL_CERTS_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000538 ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
539 strlen( test_cli_key ), NULL, 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +0000540#else
541 {
Paul Bakker69e095c2011-12-10 21:55:01 +0000542 ret = -1;
Paul Bakker5690efc2011-05-26 13:16:06 +0000543 printf("POLARSSL_CERTS_C not defined.");
544 }
545#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000546 if( ret != 0 )
547 {
548 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
549 goto exit;
550 }
551
552 printf( " ok\n" );
553
554 /*
555 * 2. Start the connection
556 */
557 printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
558 opt.server_port );
559 fflush( stdout );
560
561 if( ( ret = net_connect( &server_fd, opt.server_name,
562 opt.server_port ) ) != 0 )
563 {
564 printf( " failed\n ! net_connect returned %d\n\n", ret );
565 goto exit;
566 }
567
568 printf( " ok\n" );
569
570 /*
571 * 3. Setup stuff
572 */
573 printf( " . Setting up the SSL/TLS structure..." );
574 fflush( stdout );
575
Paul Bakker1496d382011-05-23 12:07:29 +0000576 if( ( ret = ssl_init( &ssl ) ) != 0 )
577 {
578 printf( " failed\n ! ssl_init returned %d\n\n", ret );
579 goto exit;
580 }
581
582 printf( " ok\n" );
583
584 ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
585 ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
586
Paul Bakker508ad5a2011-12-04 17:09:26 +0000587 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker1496d382011-05-23 12:07:29 +0000588 ssl_set_dbg( &ssl, my_debug, stdout );
589 ssl_set_bio( &ssl, net_recv, &server_fd,
590 net_send, &server_fd );
591
Paul Bakker645ce3a2012-10-31 12:32:41 +0000592 if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
Paul Bakker1496d382011-05-23 12:07:29 +0000593 ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
594
Paul Bakker1496d382011-05-23 12:07:29 +0000595 ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
596 ssl_set_own_cert( &ssl, &clicert, &rsa );
597
598 ssl_set_hostname( &ssl, opt.server_name );
599
600 if( opt.mode == MODE_SSL_TLS )
601 {
602 if( do_handshake( &ssl, &opt ) != 0 )
603 goto exit;
604
605 printf( " > Get header from server:" );
606 fflush( stdout );
607
608 ret = write_ssl_and_get_response( &ssl, buf, 0 );
609 if( ret < 200 || ret > 299 )
610 {
611 printf( " failed\n ! server responded with %d\n\n", ret );
612 goto exit;
613 }
614
615 printf(" ok\n" );
616
617 printf( " > Write EHLO to server:" );
618 fflush( stdout );
619
620 gethostname( hostname, 32 );
621 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
622 ret = write_ssl_and_get_response( &ssl, buf, len );
623 if( ret < 200 || ret > 299 )
624 {
625 printf( " failed\n ! server responded with %d\n\n", ret );
626 goto exit;
627 }
628 }
629 else
630 {
631 printf( " > Get header from server:" );
632 fflush( stdout );
633
634 ret = write_and_get_response( server_fd, buf, 0 );
635 if( ret < 200 || ret > 299 )
636 {
637 printf( " failed\n ! server responded with %d\n\n", ret );
638 goto exit;
639 }
640
641 printf(" ok\n" );
642
643 printf( " > Write EHLO to server:" );
644 fflush( stdout );
645
646 gethostname( hostname, 32 );
647 len = sprintf( (char *) buf, "EHLO %s\n", hostname );
648 ret = write_and_get_response( server_fd, buf, len );
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 STARTTLS to server:" );
658 fflush( stdout );
659
660 gethostname( hostname, 32 );
661 len = sprintf( (char *) buf, "STARTTLS\n" );
662 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 if( do_handshake( &ssl, &opt ) != 0 )
672 goto exit;
673 }
674
Paul Bakker5690efc2011-05-26 13:16:06 +0000675#if defined(POLARSSL_BASE64_C)
Paul Bakker1496d382011-05-23 12:07:29 +0000676 if( opt.authentication )
677 {
678 printf( " > Write AUTH LOGIN to server:" );
679 fflush( stdout );
680
681 len = sprintf( (char *) buf, "AUTH LOGIN\n" );
682 ret = write_ssl_and_get_response( &ssl, buf, len );
683 if( ret < 200 || ret > 399 )
684 {
685 printf( " failed\n ! server responded with %d\n\n", ret );
686 goto exit;
687 }
688
689 printf(" ok\n" );
690
691 printf( " > Write username to server: %s", opt.user_name );
692 fflush( stdout );
693
694 n = sizeof( buf );
Paul Bakkere22d7032011-05-23 16:02:34 +0000695 len = base64_encode( base, &n, (unsigned char *) opt.user_name, strlen( opt.user_name ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000696 len = sprintf( (char *) buf, "%s\n", base );
697 ret = write_ssl_and_get_response( &ssl, buf, len );
698 if( ret < 300 || ret > 399 )
699 {
700 printf( " failed\n ! server responded with %d\n\n", ret );
701 goto exit;
702 }
703
704 printf(" ok\n" );
705
706 printf( " > Write password to server: %s", opt.user_pwd );
707 fflush( stdout );
708
Paul Bakkere22d7032011-05-23 16:02:34 +0000709 len = base64_encode( base, &n, (unsigned char *) opt.user_pwd, strlen( opt.user_pwd ) );
Paul Bakker1496d382011-05-23 12:07:29 +0000710 len = sprintf( (char *) buf, "%s\n", base );
711 ret = write_ssl_and_get_response( &ssl, buf, len );
712 if( ret < 200 || ret > 399 )
713 {
714 printf( " failed\n ! server responded with %d\n\n", ret );
715 goto exit;
716 }
717
718 printf(" ok\n" );
719 }
Paul Bakker5690efc2011-05-26 13:16:06 +0000720#endif
Paul Bakker1496d382011-05-23 12:07:29 +0000721
722 printf( " > Write MAIL FROM to server:" );
723 fflush( stdout );
724
725 len = sprintf( (char *) buf, "MAIL FROM:<%s>\n", opt.mail_from );
726 ret = write_ssl_and_get_response( &ssl, buf, len );
727 if( ret < 200 || ret > 299 )
728 {
729 printf( " failed\n ! server responded with %d\n\n", ret );
730 goto exit;
731 }
732
733 printf(" ok\n" );
734
735 printf( " > Write RCPT TO to server:" );
736 fflush( stdout );
737
738 len = sprintf( (char *) buf, "RCPT TO:<%s>\n", opt.mail_to );
739 ret = write_ssl_and_get_response( &ssl, buf, len );
740 if( ret < 200 || ret > 299 )
741 {
742 printf( " failed\n ! server responded with %d\n\n", ret );
743 goto exit;
744 }
745
746 printf(" ok\n" );
747
748 printf( " > Write DATA to server:" );
749 fflush( stdout );
750
751 len = sprintf( (char *) buf, "DATA\n" );
752 ret = write_ssl_and_get_response( &ssl, buf, len );
753 if( ret < 300 || ret > 399 )
754 {
755 printf( " failed\n ! server responded with %d\n\n", ret );
756 goto exit;
757 }
758
759 printf(" ok\n" );
760
761 printf( " > Write content to server:" );
762 fflush( stdout );
763
764 len = sprintf( (char *) buf, "From: %s\nSubject: PolarSSL Test mail\n\n"
765 "This is a simple test mail from the "
766 "PolarSSL mail client example.\n"
767 "\n"
768 "Enjoy!", opt.mail_from );
769 ret = write_ssl_data( &ssl, buf, len );
770
771 len = sprintf( (char *) buf, "\r\n.\r\n");
772 ret = write_ssl_and_get_response( &ssl, buf, len );
773 if( ret < 200 || ret > 299 )
774 {
775 printf( " failed\n ! server responded with %d\n\n", ret );
776 goto exit;
777 }
778
779 printf(" ok\n" );
780
781 ssl_close_notify( &ssl );
782
783exit:
784
785 if( server_fd )
786 net_close( server_fd );
787 x509_free( &clicert );
788 x509_free( &cacert );
789 rsa_free( &rsa );
790 ssl_free( &ssl );
791
Paul Bakkercce9d772011-11-18 14:26:47 +0000792#if defined(_WIN32)
Paul Bakker1496d382011-05-23 12:07:29 +0000793 printf( " + Press Enter to exit this program.\n" );
794 fflush( stdout ); getchar();
795#endif
796
797 return( ret );
798}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000799#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C &&
800 POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C **
801 POLARSSL_CTR_DRBG_C */