ssl_mail_client.c 25 KB

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