ssl_mail_client.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*
  2. * SSL client for SMTP servers
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /* Enable definition of gethostname() even when compiling with -std=c99. Must
  22. * be set before config.h, which pulls in glibc's features.h indirectly.
  23. * Harmless on other platforms. */
  24. #define _POSIX_C_SOURCE 200112L
  25. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "mbedtls/config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #if defined(MBEDTLS_PLATFORM_C)
  31. #include "mbedtls/platform.h"
  32. #else
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #define mbedtls_time time
  36. #define mbedtls_time_t time_t
  37. #define mbedtls_fprintf fprintf
  38. #define mbedtls_printf printf
  39. #define mbedtls_exit exit
  40. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  41. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  42. #endif /* MBEDTLS_PLATFORM_C */
  43. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  44. !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
  45. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
  46. !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
  47. !defined(MBEDTLS_FS_IO)
  48. int main( void )
  49. {
  50. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  51. "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
  52. "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  53. "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
  54. "not defined.\n");
  55. return( 0 );
  56. }
  57. #else
  58. #include "mbedtls/base64.h"
  59. #include "mbedtls/error.h"
  60. #include "mbedtls/net_sockets.h"
  61. #include "mbedtls/ssl.h"
  62. #include "mbedtls/entropy.h"
  63. #include "mbedtls/ctr_drbg.h"
  64. #include "mbedtls/certs.h"
  65. #include "mbedtls/x509.h"
  66. #include <stdlib.h>
  67. #include <string.h>
  68. #if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
  69. #include <unistd.h>
  70. #else
  71. #include <io.h>
  72. #endif
  73. #if defined(_WIN32) || defined(_WIN32_WCE)
  74. #include <winsock2.h>
  75. #include <windows.h>
  76. #if defined(_MSC_VER)
  77. #if defined(_WIN32_WCE)
  78. #pragma comment( lib, "ws2.lib" )
  79. #else
  80. #pragma comment( lib, "ws2_32.lib" )
  81. #endif
  82. #endif /* _MSC_VER */
  83. #endif
  84. #define DFL_SERVER_NAME "localhost"
  85. #define DFL_SERVER_PORT "465"
  86. #define DFL_USER_NAME "user"
  87. #define DFL_USER_PWD "password"
  88. #define DFL_MAIL_FROM ""
  89. #define DFL_MAIL_TO ""
  90. #define DFL_DEBUG_LEVEL 0
  91. #define DFL_CA_FILE ""
  92. #define DFL_CRT_FILE ""
  93. #define DFL_KEY_FILE ""
  94. #define DFL_FORCE_CIPHER 0
  95. #define DFL_MODE 0
  96. #define DFL_AUTHENTICATION 0
  97. #define MODE_SSL_TLS 0
  98. #define MODE_STARTTLS 0
  99. #if defined(MBEDTLS_BASE64_C)
  100. #define USAGE_AUTH \
  101. " authentication=%%d default: 0 (disabled)\n" \
  102. " user_name=%%s default: \"" DFL_USER_NAME "\"\n" \
  103. " user_pwd=%%s default: \"" DFL_USER_PWD "\"\n"
  104. #else
  105. #define USAGE_AUTH \
  106. " authentication options disabled. (Require MBEDTLS_BASE64_C)\n"
  107. #endif /* MBEDTLS_BASE64_C */
  108. #if defined(MBEDTLS_FS_IO)
  109. #define USAGE_IO \
  110. " ca_file=%%s default: \"\" (pre-loaded)\n" \
  111. " crt_file=%%s default: \"\" (pre-loaded)\n" \
  112. " key_file=%%s default: \"\" (pre-loaded)\n"
  113. #else
  114. #define USAGE_IO \
  115. " No file operations available (MBEDTLS_FS_IO not defined)\n"
  116. #endif /* MBEDTLS_FS_IO */
  117. #define USAGE \
  118. "\n usage: ssl_mail_client param=<>...\n" \
  119. "\n acceptable parameters:\n" \
  120. " server_name=%%s default: " DFL_SERVER_NAME "\n" \
  121. " server_port=%%d default: " DFL_SERVER_PORT "\n" \
  122. " debug_level=%%d default: 0 (disabled)\n" \
  123. " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \
  124. USAGE_AUTH \
  125. " mail_from=%%s default: \"\"\n" \
  126. " mail_to=%%s default: \"\"\n" \
  127. USAGE_IO \
  128. " force_ciphersuite=<name> default: all enabled\n" \
  129. " acceptable ciphersuite names:\n"
  130. /*
  131. * global options
  132. */
  133. struct options
  134. {
  135. const char *server_name; /* hostname of the server (client only) */
  136. const char *server_port; /* port on which the ssl service runs */
  137. int debug_level; /* level of debugging */
  138. int authentication; /* if authentication is required */
  139. int mode; /* SSL/TLS (0) or STARTTLS (1) */
  140. const char *user_name; /* username to use for authentication */
  141. const char *user_pwd; /* password to use for authentication */
  142. const char *mail_from; /* E-Mail address to use as sender */
  143. const char *mail_to; /* E-Mail address to use as recipient */
  144. const char *ca_file; /* the file with the CA certificate(s) */
  145. const char *crt_file; /* the file with the client certificate */
  146. const char *key_file; /* the file with the client key */
  147. int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
  148. } opt;
  149. static void my_debug( void *ctx, int level,
  150. const char *file, int line,
  151. const char *str )
  152. {
  153. ((void) level);
  154. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
  155. fflush( (FILE *) ctx );
  156. }
  157. static int do_handshake( mbedtls_ssl_context *ssl )
  158. {
  159. int ret;
  160. uint32_t flags;
  161. unsigned char buf[1024];
  162. memset(buf, 0, 1024);
  163. /*
  164. * 4. Handshake
  165. */
  166. mbedtls_printf( " . Performing the SSL/TLS handshake..." );
  167. fflush( stdout );
  168. while( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
  169. {
  170. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  171. {
  172. #if defined(MBEDTLS_ERROR_C)
  173. mbedtls_strerror( ret, (char *) buf, 1024 );
  174. #endif
  175. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned %d: %s\n\n", ret, buf );
  176. return( -1 );
  177. }
  178. }
  179. mbedtls_printf( " ok\n [ Ciphersuite is %s ]\n",
  180. mbedtls_ssl_get_ciphersuite( ssl ) );
  181. /*
  182. * 5. Verify the server certificate
  183. */
  184. mbedtls_printf( " . Verifying peer X.509 certificate..." );
  185. /* In real life, we probably want to bail out when ret != 0 */
  186. if( ( flags = mbedtls_ssl_get_verify_result( ssl ) ) != 0 )
  187. {
  188. char vrfy_buf[512];
  189. mbedtls_printf( " failed\n" );
  190. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  191. mbedtls_printf( "%s\n", vrfy_buf );
  192. }
  193. else
  194. mbedtls_printf( " ok\n" );
  195. mbedtls_printf( " . Peer certificate information ...\n" );
  196. mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
  197. mbedtls_ssl_get_peer_cert( ssl ) );
  198. mbedtls_printf( "%s\n", buf );
  199. return( 0 );
  200. }
  201. static int write_ssl_data( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
  202. {
  203. int ret;
  204. mbedtls_printf("\n%s", buf);
  205. while( len && ( ret = mbedtls_ssl_write( ssl, buf, len ) ) <= 0 )
  206. {
  207. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  208. {
  209. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  210. return -1;
  211. }
  212. }
  213. return( 0 );
  214. }
  215. static int write_ssl_and_get_response( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
  216. {
  217. int ret;
  218. unsigned char data[128];
  219. char code[4];
  220. size_t i, idx = 0;
  221. mbedtls_printf("\n%s", buf);
  222. while( len && ( ret = mbedtls_ssl_write( ssl, buf, len ) ) <= 0 )
  223. {
  224. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  225. {
  226. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  227. return -1;
  228. }
  229. }
  230. do
  231. {
  232. len = sizeof( data ) - 1;
  233. memset( data, 0, sizeof( data ) );
  234. ret = mbedtls_ssl_read( ssl, data, len );
  235. if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  236. continue;
  237. if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
  238. return -1;
  239. if( ret <= 0 )
  240. {
  241. mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
  242. return -1;
  243. }
  244. mbedtls_printf("\n%s", data);
  245. len = ret;
  246. for( i = 0; i < len; i++ )
  247. {
  248. if( data[i] != '\n' )
  249. {
  250. if( idx < 4 )
  251. code[ idx++ ] = data[i];
  252. continue;
  253. }
  254. if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
  255. {
  256. code[3] = '\0';
  257. return atoi( code );
  258. }
  259. idx = 0;
  260. }
  261. }
  262. while( 1 );
  263. }
  264. static int write_and_get_response( mbedtls_net_context *sock_fd, unsigned char *buf, size_t len )
  265. {
  266. int ret;
  267. unsigned char data[128];
  268. char code[4];
  269. size_t i, idx = 0;
  270. mbedtls_printf("\n%s", buf);
  271. if( len && ( ret = mbedtls_net_send( sock_fd, buf, len ) ) <= 0 )
  272. {
  273. mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
  274. return -1;
  275. }
  276. do
  277. {
  278. len = sizeof( data ) - 1;
  279. memset( data, 0, sizeof( data ) );
  280. ret = mbedtls_net_recv( sock_fd, data, len );
  281. if( ret <= 0 )
  282. {
  283. mbedtls_printf( "failed\n ! mbedtls_net_recv returned %d\n\n", ret );
  284. return -1;
  285. }
  286. data[len] = '\0';
  287. mbedtls_printf("\n%s", data);
  288. len = ret;
  289. for( i = 0; i < len; i++ )
  290. {
  291. if( data[i] != '\n' )
  292. {
  293. if( idx < 4 )
  294. code[ idx++ ] = data[i];
  295. continue;
  296. }
  297. if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' )
  298. {
  299. code[3] = '\0';
  300. return atoi( code );
  301. }
  302. idx = 0;
  303. }
  304. }
  305. while( 1 );
  306. }
  307. int main( int argc, char *argv[] )
  308. {
  309. int ret = 1, len;
  310. int exit_code = MBEDTLS_EXIT_FAILURE;
  311. mbedtls_net_context server_fd;
  312. #if defined(MBEDTLS_BASE64_C)
  313. unsigned char base[1024];
  314. /* buf is used as the destination buffer for printing base with the format:
  315. * "%s\r\n". Hence, the size of buf should be at least the size of base
  316. * plus 2 bytes for the \r and \n characters.
  317. */
  318. unsigned char buf[sizeof( base ) + 2];
  319. #else
  320. unsigned char buf[1024];
  321. #endif
  322. char hostname[32];
  323. const char *pers = "ssl_mail_client";
  324. mbedtls_entropy_context entropy;
  325. mbedtls_ctr_drbg_context ctr_drbg;
  326. mbedtls_ssl_context ssl;
  327. mbedtls_ssl_config conf;
  328. mbedtls_x509_crt cacert;
  329. mbedtls_x509_crt clicert;
  330. mbedtls_pk_context pkey;
  331. int i;
  332. size_t n;
  333. char *p, *q;
  334. const int *list;
  335. /*
  336. * Make sure memory references are valid in case we exit early.
  337. */
  338. mbedtls_net_init( &server_fd );
  339. mbedtls_ssl_init( &ssl );
  340. mbedtls_ssl_config_init( &conf );
  341. memset( &buf, 0, sizeof( buf ) );
  342. mbedtls_x509_crt_init( &cacert );
  343. mbedtls_x509_crt_init( &clicert );
  344. mbedtls_pk_init( &pkey );
  345. mbedtls_ctr_drbg_init( &ctr_drbg );
  346. if( argc == 0 )
  347. {
  348. usage:
  349. mbedtls_printf( USAGE );
  350. list = mbedtls_ssl_list_ciphersuites();
  351. while( *list )
  352. {
  353. mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
  354. list++;
  355. }
  356. mbedtls_printf("\n");
  357. goto exit;
  358. }
  359. opt.server_name = DFL_SERVER_NAME;
  360. opt.server_port = DFL_SERVER_PORT;
  361. opt.debug_level = DFL_DEBUG_LEVEL;
  362. opt.authentication = DFL_AUTHENTICATION;
  363. opt.mode = DFL_MODE;
  364. opt.user_name = DFL_USER_NAME;
  365. opt.user_pwd = DFL_USER_PWD;
  366. opt.mail_from = DFL_MAIL_FROM;
  367. opt.mail_to = DFL_MAIL_TO;
  368. opt.ca_file = DFL_CA_FILE;
  369. opt.crt_file = DFL_CRT_FILE;
  370. opt.key_file = DFL_KEY_FILE;
  371. opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
  372. for( i = 1; i < argc; i++ )
  373. {
  374. p = argv[i];
  375. if( ( q = strchr( p, '=' ) ) == NULL )
  376. goto usage;
  377. *q++ = '\0';
  378. if( strcmp( p, "server_name" ) == 0 )
  379. opt.server_name = q;
  380. else if( strcmp( p, "server_port" ) == 0 )
  381. opt.server_port = q;
  382. else if( strcmp( p, "debug_level" ) == 0 )
  383. {
  384. opt.debug_level = atoi( q );
  385. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  386. goto usage;
  387. }
  388. else if( strcmp( p, "authentication" ) == 0 )
  389. {
  390. opt.authentication = atoi( q );
  391. if( opt.authentication < 0 || opt.authentication > 1 )
  392. goto usage;
  393. }
  394. else if( strcmp( p, "mode" ) == 0 )
  395. {
  396. opt.mode = atoi( q );
  397. if( opt.mode < 0 || opt.mode > 1 )
  398. goto usage;
  399. }
  400. else if( strcmp( p, "user_name" ) == 0 )
  401. opt.user_name = q;
  402. else if( strcmp( p, "user_pwd" ) == 0 )
  403. opt.user_pwd = q;
  404. else if( strcmp( p, "mail_from" ) == 0 )
  405. opt.mail_from = q;
  406. else if( strcmp( p, "mail_to" ) == 0 )
  407. opt.mail_to = q;
  408. else if( strcmp( p, "ca_file" ) == 0 )
  409. opt.ca_file = q;
  410. else if( strcmp( p, "crt_file" ) == 0 )
  411. opt.crt_file = q;
  412. else if( strcmp( p, "key_file" ) == 0 )
  413. opt.key_file = q;
  414. else if( strcmp( p, "force_ciphersuite" ) == 0 )
  415. {
  416. opt.force_ciphersuite[0] = -1;
  417. opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
  418. if( opt.force_ciphersuite[0] <= 0 )
  419. goto usage;
  420. opt.force_ciphersuite[1] = 0;
  421. }
  422. else
  423. goto usage;
  424. }
  425. /*
  426. * 0. Initialize the RNG and the session data
  427. */
  428. mbedtls_printf( "\n . Seeding the random number generator..." );
  429. fflush( stdout );
  430. mbedtls_entropy_init( &entropy );
  431. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  432. (const unsigned char *) pers,
  433. strlen( pers ) ) ) != 0 )
  434. {
  435. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  436. goto exit;
  437. }
  438. mbedtls_printf( " ok\n" );
  439. /*
  440. * 1.1. Load the trusted CA
  441. */
  442. mbedtls_printf( " . Loading the CA root certificate ..." );
  443. fflush( stdout );
  444. #if defined(MBEDTLS_FS_IO)
  445. if( strlen( opt.ca_file ) )
  446. ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
  447. else
  448. #endif
  449. #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
  450. ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
  451. mbedtls_test_cas_pem_len );
  452. #else
  453. {
  454. mbedtls_printf("MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined.");
  455. goto exit;
  456. }
  457. #endif
  458. if( ret < 0 )
  459. {
  460. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
  461. goto exit;
  462. }
  463. mbedtls_printf( " ok (%d skipped)\n", ret );
  464. /*
  465. * 1.2. Load own certificate and private key
  466. *
  467. * (can be skipped if client authentication is not required)
  468. */
  469. mbedtls_printf( " . Loading the client cert. and key..." );
  470. fflush( stdout );
  471. #if defined(MBEDTLS_FS_IO)
  472. if( strlen( opt.crt_file ) )
  473. ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
  474. else
  475. #endif
  476. #if defined(MBEDTLS_CERTS_C)
  477. ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
  478. mbedtls_test_cli_crt_len );
  479. #else
  480. {
  481. mbedtls_printf("MBEDTLS_CERTS_C not defined.");
  482. goto exit;
  483. }
  484. #endif
  485. if( ret != 0 )
  486. {
  487. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret );
  488. goto exit;
  489. }
  490. #if defined(MBEDTLS_FS_IO)
  491. if( strlen( opt.key_file ) )
  492. ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
  493. else
  494. #endif
  495. #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C)
  496. ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key,
  497. mbedtls_test_cli_key_len, NULL, 0 );
  498. #else
  499. {
  500. mbedtls_printf("MBEDTLS_CERTS_C or MBEDTLS_PEM_PARSE_C not defined.");
  501. goto exit;
  502. }
  503. #endif
  504. if( ret != 0 )
  505. {
  506. mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret );
  507. goto exit;
  508. }
  509. mbedtls_printf( " ok\n" );
  510. /*
  511. * 2. Start the connection
  512. */
  513. mbedtls_printf( " . Connecting to tcp/%s/%s...", opt.server_name,
  514. opt.server_port );
  515. fflush( stdout );
  516. if( ( ret = mbedtls_net_connect( &server_fd, opt.server_name,
  517. opt.server_port, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
  518. {
  519. mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
  520. goto exit;
  521. }
  522. mbedtls_printf( " ok\n" );
  523. /*
  524. * 3. Setup stuff
  525. */
  526. mbedtls_printf( " . Setting up the SSL/TLS structure..." );
  527. fflush( stdout );
  528. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  529. MBEDTLS_SSL_IS_CLIENT,
  530. MBEDTLS_SSL_TRANSPORT_STREAM,
  531. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  532. {
  533. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
  534. goto exit;
  535. }
  536. /* OPTIONAL is not optimal for security,
  537. * but makes interop easier in this simplified example */
  538. mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
  539. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  540. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  541. if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
  542. mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
  543. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  544. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
  545. {
  546. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  547. goto exit;
  548. }
  549. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  550. {
  551. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
  552. goto exit;
  553. }
  554. if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
  555. {
  556. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
  557. goto exit;
  558. }
  559. mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
  560. mbedtls_printf( " ok\n" );
  561. if( opt.mode == MODE_SSL_TLS )
  562. {
  563. if( do_handshake( &ssl ) != 0 )
  564. goto exit;
  565. mbedtls_printf( " > Get header from server:" );
  566. fflush( stdout );
  567. ret = write_ssl_and_get_response( &ssl, buf, 0 );
  568. if( ret < 200 || ret > 299 )
  569. {
  570. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  571. goto exit;
  572. }
  573. mbedtls_printf(" ok\n" );
  574. mbedtls_printf( " > Write EHLO to server:" );
  575. fflush( stdout );
  576. gethostname( hostname, 32 );
  577. len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
  578. ret = write_ssl_and_get_response( &ssl, buf, len );
  579. if( ret < 200 || ret > 299 )
  580. {
  581. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  582. goto exit;
  583. }
  584. }
  585. else
  586. {
  587. mbedtls_printf( " > Get header from server:" );
  588. fflush( stdout );
  589. ret = write_and_get_response( &server_fd, buf, 0 );
  590. if( ret < 200 || ret > 299 )
  591. {
  592. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  593. goto exit;
  594. }
  595. mbedtls_printf(" ok\n" );
  596. mbedtls_printf( " > Write EHLO to server:" );
  597. fflush( stdout );
  598. gethostname( hostname, 32 );
  599. len = sprintf( (char *) buf, "EHLO %s\r\n", hostname );
  600. ret = write_and_get_response( &server_fd, buf, len );
  601. if( ret < 200 || ret > 299 )
  602. {
  603. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  604. goto exit;
  605. }
  606. mbedtls_printf(" ok\n" );
  607. mbedtls_printf( " > Write STARTTLS to server:" );
  608. fflush( stdout );
  609. gethostname( hostname, 32 );
  610. len = sprintf( (char *) buf, "STARTTLS\r\n" );
  611. ret = write_and_get_response( &server_fd, buf, len );
  612. if( ret < 200 || ret > 299 )
  613. {
  614. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  615. goto exit;
  616. }
  617. mbedtls_printf(" ok\n" );
  618. if( do_handshake( &ssl ) != 0 )
  619. goto exit;
  620. }
  621. #if defined(MBEDTLS_BASE64_C)
  622. if( opt.authentication )
  623. {
  624. mbedtls_printf( " > Write AUTH LOGIN to server:" );
  625. fflush( stdout );
  626. len = sprintf( (char *) buf, "AUTH LOGIN\r\n" );
  627. ret = write_ssl_and_get_response( &ssl, buf, len );
  628. if( ret < 200 || ret > 399 )
  629. {
  630. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  631. goto exit;
  632. }
  633. mbedtls_printf(" ok\n" );
  634. mbedtls_printf( " > Write username to server: %s", opt.user_name );
  635. fflush( stdout );
  636. ret = mbedtls_base64_encode( base, sizeof( base ), &n, (const unsigned char *) opt.user_name,
  637. strlen( opt.user_name ) );
  638. if( ret != 0 ) {
  639. mbedtls_printf( " failed\n ! mbedtls_base64_encode returned %d\n\n", ret );
  640. goto exit;
  641. }
  642. len = sprintf( (char *) buf, "%s\r\n", base );
  643. ret = write_ssl_and_get_response( &ssl, buf, len );
  644. if( ret < 300 || ret > 399 )
  645. {
  646. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  647. goto exit;
  648. }
  649. mbedtls_printf(" ok\n" );
  650. mbedtls_printf( " > Write password to server: %s", opt.user_pwd );
  651. fflush( stdout );
  652. ret = mbedtls_base64_encode( base, sizeof( base ), &n, (const unsigned char *) opt.user_pwd,
  653. strlen( opt.user_pwd ) );
  654. if( ret != 0 ) {
  655. mbedtls_printf( " failed\n ! mbedtls_base64_encode returned %d\n\n", ret );
  656. goto exit;
  657. }
  658. len = sprintf( (char *) buf, "%s\r\n", base );
  659. ret = write_ssl_and_get_response( &ssl, buf, len );
  660. if( ret < 200 || ret > 399 )
  661. {
  662. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  663. goto exit;
  664. }
  665. mbedtls_printf(" ok\n" );
  666. }
  667. #endif
  668. mbedtls_printf( " > Write MAIL FROM to server:" );
  669. fflush( stdout );
  670. len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from );
  671. ret = write_ssl_and_get_response( &ssl, buf, len );
  672. if( ret < 200 || ret > 299 )
  673. {
  674. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  675. goto exit;
  676. }
  677. mbedtls_printf(" ok\n" );
  678. mbedtls_printf( " > Write RCPT TO to server:" );
  679. fflush( stdout );
  680. len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to );
  681. ret = write_ssl_and_get_response( &ssl, buf, len );
  682. if( ret < 200 || ret > 299 )
  683. {
  684. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  685. goto exit;
  686. }
  687. mbedtls_printf(" ok\n" );
  688. mbedtls_printf( " > Write DATA to server:" );
  689. fflush( stdout );
  690. len = sprintf( (char *) buf, "DATA\r\n" );
  691. ret = write_ssl_and_get_response( &ssl, buf, len );
  692. if( ret < 300 || ret > 399 )
  693. {
  694. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  695. goto exit;
  696. }
  697. mbedtls_printf(" ok\n" );
  698. mbedtls_printf( " > Write content to server:" );
  699. fflush( stdout );
  700. len = sprintf( (char *) buf, "From: %s\r\nSubject: mbed TLS Test mail\r\n\r\n"
  701. "This is a simple test mail from the "
  702. "mbed TLS mail client example.\r\n"
  703. "\r\n"
  704. "Enjoy!", opt.mail_from );
  705. ret = write_ssl_data( &ssl, buf, len );
  706. len = sprintf( (char *) buf, "\r\n.\r\n");
  707. ret = write_ssl_and_get_response( &ssl, buf, len );
  708. if( ret < 200 || ret > 299 )
  709. {
  710. mbedtls_printf( " failed\n ! server responded with %d\n\n", ret );
  711. goto exit;
  712. }
  713. mbedtls_printf(" ok\n" );
  714. mbedtls_ssl_close_notify( &ssl );
  715. exit_code = MBEDTLS_EXIT_SUCCESS;
  716. exit:
  717. mbedtls_net_free( &server_fd );
  718. mbedtls_x509_crt_free( &clicert );
  719. mbedtls_x509_crt_free( &cacert );
  720. mbedtls_pk_free( &pkey );
  721. mbedtls_ssl_free( &ssl );
  722. mbedtls_ssl_config_free( &conf );
  723. mbedtls_ctr_drbg_free( &ctr_drbg );
  724. mbedtls_entropy_free( &entropy );
  725. #if defined(_WIN32)
  726. mbedtls_printf( " + Press Enter to exit this program.\n" );
  727. fflush( stdout ); getchar();
  728. #endif
  729. return( exit_code );
  730. }
  731. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
  732. MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C **
  733. MBEDTLS_CTR_DRBG_C */