ssl_mail_client.c 23 KB

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