ssl_mail_client.c 26 KB

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