ssl_mail_client.c 25 KB

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