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