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