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