ssl_mail_client.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * SSL client for SMTP servers
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  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. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #ifdef PRINTF_STDLIB
  31. #include <stdio.h>
  32. #endif
  33. #ifdef PRINTF_CUSTOM
  34. #include "tinystdio.h"
  35. #endif
  36. #include <unistd.h>
  37. #include "polarssl/config.h"
  38. #include "polarssl/base64.h"
  39. #include "polarssl/error.h"
  40. #include "polarssl/net.h"
  41. #include "polarssl/ssl.h"
  42. #include "polarssl/havege.h"
  43. #include "polarssl/certs.h"
  44. #include "polarssl/x509.h"
  45. #define DFL_SERVER_NAME "localhost"
  46. #define DFL_SERVER_PORT 465
  47. #define DFL_USER_NAME "user"
  48. #define DFL_USER_PWD "password"
  49. #define DFL_MAIL_FROM ""
  50. #define DFL_MAIL_TO ""
  51. #define DFL_DEBUG_LEVEL 0
  52. #define DFL_CA_FILE ""
  53. #define DFL_CRT_FILE ""
  54. #define DFL_KEY_FILE ""
  55. #define DFL_FORCE_CIPHER 0
  56. #define DFL_MODE 0
  57. #define DFL_AUTHENTICATION 0
  58. #define MODE_SSL_TLS 0
  59. #define MODE_STARTTLS 0
  60. /*
  61. * global options
  62. */
  63. struct options
  64. {
  65. char *server_name; /* hostname of the server (client only) */
  66. int server_port; /* port on which the ssl service runs */
  67. int debug_level; /* level of debugging */
  68. int authentication; /* if authentication is required */
  69. int mode; /* SSL/TLS (0) or STARTTLS (1) */
  70. char *user_name; /* username to use for authentication */
  71. char *user_pwd; /* password to use for authentication */
  72. char *mail_from; /* E-Mail address to use as sender */
  73. char *mail_to; /* E-Mail address to use as recipient */
  74. char *ca_file; /* the file with the CA certificate(s) */
  75. char *crt_file; /* the file with the client certificate */
  76. char *key_file; /* the file with the client key */
  77. int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
  78. } opt;
  79. void my_debug( void *ctx, int level, const char *str )
  80. {
  81. if( level < opt.debug_level )
  82. {
  83. fprintf( (FILE *) ctx, "%s", str );
  84. fflush( (FILE *) ctx );
  85. }
  86. }
  87. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
  88. !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \
  89. !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C)
  90. int main( void )
  91. {
  92. printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
  93. "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or "
  94. "POLARSSL_NET_C and/or POLARSSL_RSA_C not defined.\n");
  95. return( 0 );
  96. }
  97. #else
  98. int do_handshake( ssl_context *ssl, struct options *opt )
  99. {
  100. int ret;
  101. unsigned char buf[1024];
  102. memset(buf, 0, 1024);
  103. /*
  104. * 4. Handshake
  105. */
  106. printf( " . Performing the SSL/TLS handshake..." );
  107. fflush( stdout );
  108. while( ( ret = ssl_handshake( ssl ) ) != 0 )
  109. {
  110. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  111. {
  112. #if defined(POLARSSL_ERROR_C)
  113. error_strerror( ret, (char *) buf, 1024 );
  114. #endif
  115. printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf );
  116. return( -1 );
  117. }
  118. }
  119. printf( " ok\n [ Ciphersuite is %s ]\n",
  120. ssl_get_ciphersuite( ssl ) );
  121. /*
  122. * 5. Verify the server certificate
  123. */
  124. printf( " . Verifying peer X.509 certificate..." );
  125. if( ( ret = ssl_get_verify_result( ssl ) ) != 0 )
  126. {
  127. printf( " failed\n" );
  128. if( ( ret & BADCERT_EXPIRED ) != 0 )
  129. printf( " ! server certificate has expired\n" );
  130. if( ( ret & BADCERT_REVOKED ) != 0 )
  131. printf( " ! server certificate has been revoked\n" );
  132. if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
  133. printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name );
  134. if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
  135. printf( " ! self-signed or not signed by a trusted CA\n" );
  136. printf( "\n" );
  137. }
  138. else
  139. printf( " ok\n" );
  140. printf( " . Peer certificate information ...\n" );
  141. x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", ssl->peer_cert );
  142. printf( "%s\n", buf );
  143. return( 0 );
  144. }
  145. int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len )
  146. {
  147. int ret;
  148. printf("\n%s", buf);
  149. while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
  150. {
  151. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  152. {
  153. printf( " failed\n ! ssl_write returned %d\n\n", ret );
  154. return -1;
  155. }
  156. }
  157. return( 0 );
  158. }
  159. int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len )
  160. {
  161. int ret;
  162. unsigned char data[128];
  163. char code[4];
  164. size_t i, idx = 0;
  165. printf("\n%s", buf);
  166. while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 )
  167. {
  168. if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
  169. {
  170. printf( " failed\n ! ssl_write returned %d\n\n", ret );
  171. return -1;
  172. }
  173. }
  174. do
  175. {
  176. len = sizeof( data ) - 1;
  177. memset( data, 0, sizeof( data ) );
  178. ret = ssl_read( ssl, data, len );
  179. if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
  180. continue;
  181. if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )
  182. return -1;
  183. if( ret <= 0 )
  184. {
  185. printf( "failed\n ! ssl_read returned %d\n\n", ret );
  186. return -1;
  187. }
  188. printf("\n%s", data);
  189. len = ret;
  190. for( i = 0; i < len; i++ )
  191. {
  192. if( data[i] != '\n' )
  193. {
  194. if( idx < 4 )
  195. code[ idx++ ] = data[i];
  196. continue;
  197. }
  198. if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
  199. {
  200. code[3] = '\0';
  201. return atoi( code );
  202. }
  203. idx = 0;
  204. }
  205. }
  206. while( 1 );
  207. }
  208. int write_and_get_response( int sock_fd, unsigned char *buf, size_t len )
  209. {
  210. int ret;
  211. unsigned char data[128];
  212. char code[4];
  213. size_t i, idx = 0;
  214. printf("\n%s", buf);
  215. if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 )
  216. {
  217. printf( " failed\n ! ssl_write returned %d\n\n", ret );
  218. return -1;
  219. }
  220. do
  221. {
  222. len = sizeof( data ) - 1;
  223. memset( data, 0, sizeof( data ) );
  224. ret = read( sock_fd, data, len );
  225. if( ret <= 0 )
  226. {
  227. printf( "failed\n ! read returned %d\n\n", ret );
  228. return -1;
  229. }
  230. printf("\n%s", data);
  231. len = ret;
  232. for( i = 0; i < len; i++ )
  233. {
  234. if( data[i] != '\n' )
  235. {
  236. if( idx < 4 )
  237. code[ idx++ ] = data[i];
  238. continue;
  239. }
  240. if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
  241. {
  242. code[3] = '\0';
  243. return atoi( code );
  244. }
  245. idx = 0;
  246. }
  247. }
  248. while( 1 );
  249. }
  250. #if defined(POLARSSL_BASE64_C)
  251. #define USAGE_AUTH \
  252. " authentication=%%d default: 0 (disabled)\n" \
  253. " user_name=%%s default: \"user\"\n" \
  254. " user_pwd=%%s default: \"password\"\n"
  255. #else
  256. #define USAGE_AUTH \
  257. " authentication options disabled. (Require POLARSSL_BASE64_C)\n"
  258. #endif /* POLARSSL_BASE64_C */
  259. #if defined(POLARSSL_FS_IO)
  260. #define USAGE_IO \
  261. " ca_file=%%s default: \"\" (pre-loaded)\n" \
  262. " crt_file=%%s default: \"\" (pre-loaded)\n" \
  263. " key_file=%%s default: \"\" (pre-loaded)\n"
  264. #else
  265. #define USAGE_IO \
  266. " No file operations available (POLARSSL_FS_IO not defined)\n"
  267. #endif /* POLARSSL_FS_IO */
  268. #define USAGE \
  269. "\n usage: ssl_mail_client param=<>...\n" \
  270. "\n acceptable parameters:\n" \
  271. " server_name=%%s default: localhost\n" \
  272. " server_port=%%d default: 4433\n" \
  273. " debug_level=%%d default: 0 (disabled)\n" \
  274. " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
  275. USAGE_AUTH \
  276. " mail_from=%%s default: \"\"\n" \
  277. " mail_to=%%s default: \"\"\n" \
  278. USAGE_IO \
  279. " force_ciphersuite=<name> default: all enabled\n"\
  280. " acceptable ciphersuite names:\n"
  281. int main( int argc, char *argv[] )
  282. {
  283. int ret = 0, len, server_fd;
  284. unsigned char buf[1024];
  285. #if defined(POLARSSL_BASE64_C)
  286. unsigned char base[1024];
  287. #endif
  288. char hostname[32];
  289. havege_state hs;
  290. ssl_context ssl;
  291. ssl_session ssn;
  292. x509_cert cacert;
  293. x509_cert clicert;
  294. rsa_context rsa;
  295. int i;
  296. size_t j, n;
  297. char *p, *q;
  298. const int *list;
  299. /*
  300. * Make sure memory references are valid.
  301. */
  302. server_fd = 0;
  303. memset( &ssn, 0, sizeof( ssl_session ) );
  304. memset( &ssl, 0, sizeof( ssl_context ) );
  305. memset( &cacert, 0, sizeof( x509_cert ) );
  306. memset( &clicert, 0, sizeof( x509_cert ) );
  307. memset( &rsa, 0, sizeof( rsa_context ) );
  308. if( argc == 0 )
  309. {
  310. usage:
  311. printf( USAGE );
  312. list = ssl_list_ciphersuites();
  313. while( *list )
  314. {
  315. printf(" %s\n", ssl_get_ciphersuite_name( *list ) );
  316. list++;
  317. }
  318. printf("\n");
  319. goto exit;
  320. }
  321. opt.server_name = DFL_SERVER_NAME;
  322. opt.server_port = DFL_SERVER_PORT;
  323. opt.debug_level = DFL_DEBUG_LEVEL;
  324. opt.authentication = DFL_AUTHENTICATION;
  325. opt.mode = DFL_MODE;
  326. opt.user_name = DFL_USER_NAME;
  327. opt.user_pwd = DFL_USER_PWD;
  328. opt.mail_from = DFL_MAIL_FROM;
  329. opt.mail_to = DFL_MAIL_TO;
  330. opt.ca_file = DFL_CA_FILE;
  331. opt.crt_file = DFL_CRT_FILE;
  332. opt.key_file = DFL_KEY_FILE;
  333. opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
  334. for( i = 1; i < argc; i++ )
  335. {
  336. n = strlen( argv[i] );
  337. for( j = 0; j < n; j++ )
  338. {
  339. if( argv[i][j] == '=')
  340. break;
  341. if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
  342. argv[i][j] |= 0x20;
  343. }
  344. p = argv[i];
  345. if( ( q = strchr( p, '=' ) ) == NULL )
  346. goto usage;
  347. *q++ = '\0';
  348. if( strcmp( p, "server_name" ) == 0 )
  349. opt.server_name = q;
  350. else if( strcmp( p, "server_port" ) == 0 )
  351. {
  352. opt.server_port = atoi( q );
  353. if( opt.server_port < 1 || opt.server_port > 65535 )
  354. goto usage;
  355. }
  356. else if( strcmp( p, "debug_level" ) == 0 )
  357. {
  358. opt.debug_level = atoi( q );
  359. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  360. goto usage;
  361. }
  362. else if( strcmp( p, "authentication" ) == 0 )
  363. {
  364. opt.authentication = atoi( q );
  365. if( opt.authentication < 0 || opt.authentication > 1 )
  366. goto usage;
  367. }
  368. else if( strcmp( p, "mode" ) == 0 )
  369. {
  370. opt.mode = atoi( q );
  371. if( opt.mode < 0 || opt.mode > 1 )
  372. goto usage;
  373. }
  374. else if( strcmp( p, "user_name" ) == 0 )
  375. opt.user_name = q;
  376. else if( strcmp( p, "user_pwd" ) == 0 )
  377. opt.user_pwd = q;
  378. else if( strcmp( p, "mail_from" ) == 0 )
  379. opt.mail_from = q;
  380. else if( strcmp( p, "mail_to" ) == 0 )
  381. opt.mail_to = q;
  382. else if( strcmp( p, "ca_file" ) == 0 )
  383. opt.ca_file = q;
  384. else if( strcmp( p, "crt_file" ) == 0 )
  385. opt.crt_file = q;
  386. else if( strcmp( p, "key_file" ) == 0 )
  387. opt.key_file = q;
  388. else if( strcmp( p, "force_ciphersuite" ) == 0 )
  389. {
  390. opt.force_ciphersuite[0] = -1;
  391. opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q );
  392. if( opt.force_ciphersuite[0] <= 0 )
  393. goto usage;
  394. opt.force_ciphersuite[1] = 0;
  395. }
  396. else
  397. goto usage;
  398. }
  399. /*
  400. * 0. Initialize the RNG and the session data
  401. */
  402. havege_init( &hs );
  403. /*
  404. * 1.1. Load the trusted CA
  405. */
  406. printf( "\n . Loading the CA root certificate ..." );
  407. fflush( stdout );
  408. #if defined(POLARSSL_FS_IO)
  409. if( strlen( opt.ca_file ) )
  410. ret = x509parse_crtfile( &cacert, opt.ca_file );
  411. else
  412. #endif
  413. #if defined(POLARSSL_CERTS_C)
  414. ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
  415. strlen( test_ca_crt ) );
  416. #else
  417. {
  418. ret = 1;
  419. printf("POLARSSL_CERTS_C not defined.");
  420. }
  421. #endif
  422. if( ret != 0 )
  423. {
  424. printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
  425. goto exit;
  426. }
  427. printf( " ok\n" );
  428. /*
  429. * 1.2. Load own certificate and private key
  430. *
  431. * (can be skipped if client authentication is not required)
  432. */
  433. printf( " . Loading the client cert. and key..." );
  434. fflush( stdout );
  435. #if defined(POLARSSL_FS_IO)
  436. if( strlen( opt.crt_file ) )
  437. ret = x509parse_crtfile( &clicert, opt.crt_file );
  438. else
  439. #endif
  440. #if defined(POLARSSL_CERTS_C)
  441. ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
  442. strlen( test_cli_crt ) );
  443. #else
  444. {
  445. ret = 1;
  446. printf("POLARSSL_CERTS_C not defined.");
  447. }
  448. #endif
  449. if( ret != 0 )
  450. {
  451. printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
  452. goto exit;
  453. }
  454. #if defined(POLARSSL_FS_IO)
  455. if( strlen( opt.key_file ) )
  456. ret = x509parse_keyfile( &rsa, opt.key_file, "" );
  457. else
  458. #endif
  459. #if defined(POLARSSL_CERTS_C)
  460. ret = x509parse_key( &rsa, (unsigned char *) test_cli_key,
  461. strlen( test_cli_key ), NULL, 0 );
  462. #else
  463. {
  464. ret = 1;
  465. printf("POLARSSL_CERTS_C not defined.");
  466. }
  467. #endif
  468. if( ret != 0 )
  469. {
  470. printf( " failed\n ! x509parse_key returned %d\n\n", ret );
  471. goto exit;
  472. }
  473. printf( " ok\n" );
  474. /*
  475. * 2. Start the connection
  476. */
  477. printf( " . Connecting to tcp/%s/%-4d...", opt.server_name,
  478. opt.server_port );
  479. fflush( stdout );
  480. if( ( ret = net_connect( &server_fd, opt.server_name,
  481. opt.server_port ) ) != 0 )
  482. {
  483. printf( " failed\n ! net_connect returned %d\n\n", ret );
  484. goto exit;
  485. }
  486. printf( " ok\n" );
  487. /*
  488. * 3. Setup stuff
  489. */
  490. printf( " . Setting up the SSL/TLS structure..." );
  491. fflush( stdout );
  492. havege_init( &hs );
  493. if( ( ret = ssl_init( &ssl ) ) != 0 )
  494. {
  495. printf( " failed\n ! ssl_init returned %d\n\n", ret );
  496. goto exit;
  497. }
  498. printf( " ok\n" );
  499. ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
  500. ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL );
  501. ssl_set_rng( &ssl, havege_rand, &hs );
  502. ssl_set_dbg( &ssl, my_debug, stdout );
  503. ssl_set_bio( &ssl, net_recv, &server_fd,
  504. net_send, &server_fd );
  505. if( opt.force_ciphersuite[0] == DFL_FORCE_CIPHER )
  506. ssl_set_ciphersuites( &ssl, ssl_default_ciphersuites );
  507. else
  508. ssl_set_ciphersuites( &ssl, opt.force_ciphersuite );
  509. ssl_set_session( &ssl, 1, 600, &ssn );
  510. ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name );
  511. ssl_set_own_cert( &ssl, &clicert, &rsa );
  512. ssl_set_hostname( &ssl, opt.server_name );
  513. if( opt.mode == MODE_SSL_TLS )
  514. {
  515. if( do_handshake( &ssl, &opt ) != 0 )
  516. goto exit;
  517. printf( " > Get header from server:" );
  518. fflush( stdout );
  519. ret = write_ssl_and_get_response( &ssl, buf, 0 );
  520. if( ret < 200 || ret > 299 )
  521. {
  522. printf( " failed\n ! server responded with %d\n\n", ret );
  523. goto exit;
  524. }
  525. printf(" ok\n" );
  526. printf( " > Write EHLO to server:" );
  527. fflush( stdout );
  528. gethostname( hostname, 32 );
  529. len = sprintf( (char *) buf, "EHLO %s\n", hostname );
  530. ret = write_ssl_and_get_response( &ssl, buf, len );
  531. if( ret < 200 || ret > 299 )
  532. {
  533. printf( " failed\n ! server responded with %d\n\n", ret );
  534. goto exit;
  535. }
  536. }
  537. else
  538. {
  539. printf( " > Get header from server:" );
  540. fflush( stdout );
  541. ret = write_and_get_response( server_fd, buf, 0 );
  542. if( ret < 200 || ret > 299 )
  543. {
  544. printf( " failed\n ! server responded with %d\n\n", ret );
  545. goto exit;
  546. }
  547. printf(" ok\n" );
  548. printf( " > Write EHLO to server:" );
  549. fflush( stdout );
  550. gethostname( hostname, 32 );
  551. len = sprintf( (char *) buf, "EHLO %s\n", hostname );
  552. ret = write_and_get_response( server_fd, buf, len );
  553. if( ret < 200 || ret > 299 )
  554. {
  555. printf( " failed\n ! server responded with %d\n\n", ret );
  556. goto exit;
  557. }
  558. printf(" ok\n" );
  559. printf( " > Write STARTTLS to server:" );
  560. fflush( stdout );
  561. gethostname( hostname, 32 );
  562. len = sprintf( (char *) buf, "STARTTLS\n" );
  563. ret = write_and_get_response( server_fd, buf, len );
  564. if( ret < 200 || ret > 299 )
  565. {
  566. printf( " failed\n ! server responded with %d\n\n", ret );
  567. goto exit;
  568. }
  569. printf(" ok\n" );
  570. if( do_handshake( &ssl, &opt ) != 0 )
  571. goto exit;
  572. }
  573. #if defined(POLARSSL_BASE64_C)
  574. if( opt.authentication )
  575. {
  576. printf( " > Write AUTH LOGIN to server:" );
  577. fflush( stdout );
  578. len = sprintf( (char *) buf, "AUTH LOGIN\n" );
  579. ret = write_ssl_and_get_response( &ssl, buf, len );
  580. if( ret < 200 || ret > 399 )
  581. {
  582. printf( " failed\n ! server responded with %d\n\n", ret );
  583. goto exit;
  584. }
  585. printf(" ok\n" );
  586. printf( " > Write username to server: %s", opt.user_name );
  587. fflush( stdout );
  588. n = sizeof( buf );
  589. len = base64_encode( base, &n, (unsigned char *) opt.user_name, strlen( opt.user_name ) );
  590. len = sprintf( (char *) buf, "%s\n", base );
  591. ret = write_ssl_and_get_response( &ssl, buf, len );
  592. if( ret < 300 || ret > 399 )
  593. {
  594. printf( " failed\n ! server responded with %d\n\n", ret );
  595. goto exit;
  596. }
  597. printf(" ok\n" );
  598. printf( " > Write password to server: %s", opt.user_pwd );
  599. fflush( stdout );
  600. len = base64_encode( base, &n, (unsigned char *) opt.user_pwd, strlen( opt.user_pwd ) );
  601. len = sprintf( (char *) buf, "%s\n", base );
  602. ret = write_ssl_and_get_response( &ssl, buf, len );
  603. if( ret < 200 || ret > 399 )
  604. {
  605. printf( " failed\n ! server responded with %d\n\n", ret );
  606. goto exit;
  607. }
  608. printf(" ok\n" );
  609. }
  610. #endif
  611. printf( " > Write MAIL FROM to server:" );
  612. fflush( stdout );
  613. len = sprintf( (char *) buf, "MAIL FROM:<%s>\n", opt.mail_from );
  614. ret = write_ssl_and_get_response( &ssl, buf, len );
  615. if( ret < 200 || ret > 299 )
  616. {
  617. printf( " failed\n ! server responded with %d\n\n", ret );
  618. goto exit;
  619. }
  620. printf(" ok\n" );
  621. printf( " > Write RCPT TO to server:" );
  622. fflush( stdout );
  623. len = sprintf( (char *) buf, "RCPT TO:<%s>\n", opt.mail_to );
  624. ret = write_ssl_and_get_response( &ssl, buf, len );
  625. if( ret < 200 || ret > 299 )
  626. {
  627. printf( " failed\n ! server responded with %d\n\n", ret );
  628. goto exit;
  629. }
  630. printf(" ok\n" );
  631. printf( " > Write DATA to server:" );
  632. fflush( stdout );
  633. len = sprintf( (char *) buf, "DATA\n" );
  634. ret = write_ssl_and_get_response( &ssl, buf, len );
  635. if( ret < 300 || ret > 399 )
  636. {
  637. printf( " failed\n ! server responded with %d\n\n", ret );
  638. goto exit;
  639. }
  640. printf(" ok\n" );
  641. printf( " > Write content to server:" );
  642. fflush( stdout );
  643. len = sprintf( (char *) buf, "From: %s\nSubject: PolarSSL Test mail\n\n"
  644. "This is a simple test mail from the "
  645. "PolarSSL mail client example.\n"
  646. "\n"
  647. "Enjoy!", opt.mail_from );
  648. ret = write_ssl_data( &ssl, buf, len );
  649. len = sprintf( (char *) buf, "\r\n.\r\n");
  650. ret = write_ssl_and_get_response( &ssl, buf, len );
  651. if( ret < 200 || ret > 299 )
  652. {
  653. printf( " failed\n ! server responded with %d\n\n", ret );
  654. goto exit;
  655. }
  656. printf(" ok\n" );
  657. ssl_close_notify( &ssl );
  658. exit:
  659. if( server_fd )
  660. net_close( server_fd );
  661. x509_free( &clicert );
  662. x509_free( &cacert );
  663. rsa_free( &rsa );
  664. ssl_free( &ssl );
  665. memset( &ssl, 0, sizeof( ssl ) );
  666. #ifdef WIN32
  667. printf( " + Press Enter to exit this program.\n" );
  668. fflush( stdout ); getchar();
  669. #endif
  670. return( ret );
  671. }
  672. #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_SSL_TLS_C &&
  673. POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C */