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