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> |
Paul Bakker | fdda785 | 2013-11-30 15:15:31 +0100 | [diff] [blame] | 69 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 70 | |
Paul Bakker | 5a83522 | 2011-10-12 09:19:31 +0000 | [diff] [blame] | 71 | #if defined(_WIN32) || defined(_WIN32_WCE) |
Paul Bakker | 5a83522 | 2011-10-12 09:19:31 +0000 | [diff] [blame] | 72 | #include <winsock2.h> |
| 73 | #include <windows.h> |
| 74 | |
Paul Bakker | f0fc2a2 | 2013-12-30 15:42:43 +0100 | [diff] [blame] | 75 | #if defined(_MSC_VER) |
Paul Bakker | 5a83522 | 2011-10-12 09:19:31 +0000 | [diff] [blame] | 76 | #if defined(_WIN32_WCE) |
| 77 | #pragma comment( lib, "ws2.lib" ) |
| 78 | #else |
| 79 | #pragma comment( lib, "ws2_32.lib" ) |
| 80 | #endif |
Paul Bakker | f0fc2a2 | 2013-12-30 15:42:43 +0100 | [diff] [blame] | 81 | #endif /* _MSC_VER */ |
Paul Bakker | 5a83522 | 2011-10-12 09:19:31 +0000 | [diff] [blame] | 82 | #endif |
| 83 | |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 84 | #define DFL_SERVER_NAME "localhost" |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 85 | #define DFL_SERVER_PORT "465" |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 86 | #define DFL_USER_NAME "user" |
| 87 | #define DFL_USER_PWD "password" |
| 88 | #define DFL_MAIL_FROM "" |
| 89 | #define DFL_MAIL_TO "" |
| 90 | #define DFL_DEBUG_LEVEL 0 |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 91 | #define DFL_CA_FILE "" |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 92 | #define DFL_CRT_FILE "" |
| 93 | #define DFL_KEY_FILE "" |
| 94 | #define DFL_FORCE_CIPHER 0 |
| 95 | #define DFL_MODE 0 |
| 96 | #define DFL_AUTHENTICATION 0 |
| 97 | |
| 98 | #define MODE_SSL_TLS 0 |
| 99 | #define MODE_STARTTLS 0 |
| 100 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | #if defined(MBEDTLS_BASE64_C) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 102 | #define USAGE_AUTH \ |
| 103 | " authentication=%%d default: 0 (disabled)\n" \ |
| 104 | " user_name=%%s default: \"user\"\n" \ |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 105 | " user_pwd=%%s default: \"password\"\n" |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 106 | #else |
| 107 | #define USAGE_AUTH \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | " authentication options disabled. (Require MBEDTLS_BASE64_C)\n" |
| 109 | #endif /* MBEDTLS_BASE64_C */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | #if defined(MBEDTLS_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 112 | #define USAGE_IO \ |
| 113 | " ca_file=%%s default: \"\" (pre-loaded)\n" \ |
| 114 | " crt_file=%%s default: \"\" (pre-loaded)\n" \ |
| 115 | " key_file=%%s default: \"\" (pre-loaded)\n" |
| 116 | #else |
| 117 | #define USAGE_IO \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | " No file operations available (MBEDTLS_FS_IO not defined)\n" |
| 119 | #endif /* MBEDTLS_FS_IO */ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 120 | |
| 121 | #define USAGE \ |
| 122 | "\n usage: ssl_mail_client param=<>...\n" \ |
| 123 | "\n acceptable parameters:\n" \ |
| 124 | " server_name=%%s default: localhost\n" \ |
| 125 | " server_port=%%d default: 4433\n" \ |
| 126 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 127 | " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \ |
| 128 | USAGE_AUTH \ |
| 129 | " mail_from=%%s default: \"\"\n" \ |
| 130 | " mail_to=%%s default: \"\"\n" \ |
| 131 | USAGE_IO \ |
| 132 | " force_ciphersuite=<name> default: all enabled\n"\ |
| 133 | " acceptable ciphersuite names:\n" |
| 134 | |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 135 | /* |
| 136 | * global options |
| 137 | */ |
| 138 | struct options |
| 139 | { |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 140 | const char *server_name; /* hostname of the server (client only) */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 141 | const char *server_port; /* port on which the ssl service runs */ |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 142 | int debug_level; /* level of debugging */ |
| 143 | int authentication; /* if authentication is required */ |
| 144 | int mode; /* SSL/TLS (0) or STARTTLS (1) */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 145 | const char *user_name; /* username to use for authentication */ |
| 146 | const char *user_pwd; /* password to use for authentication */ |
| 147 | const char *mail_from; /* E-Mail address to use as sender */ |
| 148 | const char *mail_to; /* E-Mail address to use as recipient */ |
| 149 | const char *ca_file; /* the file with the CA certificate(s) */ |
| 150 | const char *crt_file; /* the file with the client certificate */ |
| 151 | const char *key_file; /* the file with the client key */ |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 152 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
| 153 | } opt; |
| 154 | |
Manuel Pégourié-Gonnard | 61ee351 | 2015-06-23 17:35:03 +0200 | [diff] [blame] | 155 | static void my_debug( void *ctx, int level, |
| 156 | const char *file, int line, |
| 157 | const char *str ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 158 | { |
Manuel Pégourié-Gonnard | 61ee351 | 2015-06-23 17:35:03 +0200 | [diff] [blame] | 159 | ((void) level); |
| 160 | |
| 161 | mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str ); |
| 162 | fflush( (FILE *) ctx ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 165 | static int do_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 166 | { |
| 167 | int ret; |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 168 | uint32_t flags; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 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 | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 199 | if( ( flags = 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 | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 205 | mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags ); |
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 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 295 | static int write_and_get_response( mbedtls_net_context *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); |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 303 | if( len && ( ret = mbedtls_net_send( sock_fd, buf, len ) ) <= 0 ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 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 ) ); |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 313 | ret = mbedtls_net_recv( sock_fd, data, len ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 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 | { |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 347 | int ret = 0, len; |
| 348 | mbedtls_net_context server_fd; |
Paul Bakker | 09c9dd8 | 2014-08-18 11:06:56 +0200 | [diff] [blame] | 349 | unsigned char buf[1024]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | #if defined(MBEDTLS_BASE64_C) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 351 | unsigned char base[1024]; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 352 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 353 | char hostname[32]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 354 | const char *pers = "ssl_mail_client"; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 355 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 356 | mbedtls_entropy_context entropy; |
| 357 | mbedtls_ctr_drbg_context ctr_drbg; |
| 358 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 359 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | mbedtls_x509_crt cacert; |
| 361 | mbedtls_x509_crt clicert; |
| 362 | mbedtls_pk_context pkey; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 363 | int i; |
Paul Bakker | 819370c | 2012-09-28 07:04:41 +0000 | [diff] [blame] | 364 | size_t n; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 365 | char *p, *q; |
| 366 | const int *list; |
| 367 | |
| 368 | /* |
Manuel Pégourié-Gonnard | 3bd2aae | 2013-09-20 13:10:13 +0200 | [diff] [blame] | 369 | * Make sure memory references are valid in case we exit early. |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 370 | */ |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 371 | mbedtls_net_init( &server_fd ); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 372 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 373 | mbedtls_ssl_config_init( &conf ); |
Paul Bakker | 333fdec | 2014-08-04 12:12:09 +0200 | [diff] [blame] | 374 | memset( &buf, 0, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | mbedtls_x509_crt_init( &cacert ); |
| 376 | mbedtls_x509_crt_init( &clicert ); |
| 377 | mbedtls_pk_init( &pkey ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 378 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 379 | |
| 380 | if( argc == 0 ) |
| 381 | { |
| 382 | usage: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 383 | mbedtls_printf( USAGE ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 384 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 385 | list = mbedtls_ssl_list_ciphersuites(); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 386 | while( *list ) |
| 387 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 389 | list++; |
| 390 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | mbedtls_printf("\n"); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 392 | goto exit; |
| 393 | } |
| 394 | |
| 395 | opt.server_name = DFL_SERVER_NAME; |
| 396 | opt.server_port = DFL_SERVER_PORT; |
| 397 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 398 | opt.authentication = DFL_AUTHENTICATION; |
| 399 | opt.mode = DFL_MODE; |
| 400 | opt.user_name = DFL_USER_NAME; |
| 401 | opt.user_pwd = DFL_USER_PWD; |
| 402 | opt.mail_from = DFL_MAIL_FROM; |
| 403 | opt.mail_to = DFL_MAIL_TO; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 404 | opt.ca_file = DFL_CA_FILE; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 405 | opt.crt_file = DFL_CRT_FILE; |
| 406 | opt.key_file = DFL_KEY_FILE; |
| 407 | opt.force_ciphersuite[0]= DFL_FORCE_CIPHER; |
| 408 | |
| 409 | for( i = 1; i < argc; i++ ) |
| 410 | { |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 411 | p = argv[i]; |
| 412 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 413 | goto usage; |
| 414 | *q++ = '\0'; |
| 415 | |
| 416 | if( strcmp( p, "server_name" ) == 0 ) |
| 417 | opt.server_name = q; |
| 418 | else if( strcmp( p, "server_port" ) == 0 ) |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 419 | opt.server_port = q; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 420 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 421 | { |
| 422 | opt.debug_level = atoi( q ); |
| 423 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 424 | goto usage; |
| 425 | } |
| 426 | else if( strcmp( p, "authentication" ) == 0 ) |
| 427 | { |
| 428 | opt.authentication = atoi( q ); |
| 429 | if( opt.authentication < 0 || opt.authentication > 1 ) |
| 430 | goto usage; |
| 431 | } |
| 432 | else if( strcmp( p, "mode" ) == 0 ) |
| 433 | { |
| 434 | opt.mode = atoi( q ); |
| 435 | if( opt.mode < 0 || opt.mode > 1 ) |
| 436 | goto usage; |
| 437 | } |
| 438 | else if( strcmp( p, "user_name" ) == 0 ) |
| 439 | opt.user_name = q; |
| 440 | else if( strcmp( p, "user_pwd" ) == 0 ) |
| 441 | opt.user_pwd = q; |
| 442 | else if( strcmp( p, "mail_from" ) == 0 ) |
| 443 | opt.mail_from = q; |
| 444 | else if( strcmp( p, "mail_to" ) == 0 ) |
| 445 | opt.mail_to = q; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 446 | else if( strcmp( p, "ca_file" ) == 0 ) |
| 447 | opt.ca_file = q; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 448 | else if( strcmp( p, "crt_file" ) == 0 ) |
| 449 | opt.crt_file = q; |
| 450 | else if( strcmp( p, "key_file" ) == 0 ) |
| 451 | opt.key_file = q; |
| 452 | else if( strcmp( p, "force_ciphersuite" ) == 0 ) |
| 453 | { |
| 454 | opt.force_ciphersuite[0] = -1; |
| 455 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 457 | |
| 458 | if( opt.force_ciphersuite[0] <= 0 ) |
| 459 | goto usage; |
| 460 | |
| 461 | opt.force_ciphersuite[1] = 0; |
| 462 | } |
| 463 | else |
| 464 | goto usage; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * 0. Initialize the RNG and the session data |
| 469 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 470 | mbedtls_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 471 | fflush( stdout ); |
| 472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 473 | mbedtls_entropy_init( &entropy ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 474 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 475 | (const unsigned char *) pers, |
| 476 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 477 | { |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 478 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 479 | goto exit; |
| 480 | } |
| 481 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 482 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 483 | |
| 484 | /* |
| 485 | * 1.1. Load the trusted CA |
| 486 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 487 | mbedtls_printf( " . Loading the CA root certificate ..." ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 488 | fflush( stdout ); |
| 489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 491 | if( strlen( opt.ca_file ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 493 | else |
| 494 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 495 | #if defined(MBEDTLS_CERTS_C) |
| 496 | ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem, |
| 497 | mbedtls_test_cas_pem_len ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 498 | #else |
| 499 | { |
| 500 | ret = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 501 | mbedtls_printf("MBEDTLS_CERTS_C not defined."); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 502 | } |
| 503 | #endif |
Paul Bakker | 4248823 | 2012-05-16 08:21:05 +0000 | [diff] [blame] | 504 | if( ret < 0 ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 505 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 506 | 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] | 507 | goto exit; |
| 508 | } |
| 509 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 510 | mbedtls_printf( " ok (%d skipped)\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 511 | |
| 512 | /* |
| 513 | * 1.2. Load own certificate and private key |
| 514 | * |
| 515 | * (can be skipped if client authentication is not required) |
| 516 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 517 | mbedtls_printf( " . Loading the client cert. and key..." ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 518 | fflush( stdout ); |
| 519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 520 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 521 | if( strlen( opt.crt_file ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 522 | ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 523 | else |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 524 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 525 | #if defined(MBEDTLS_CERTS_C) |
| 526 | ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt, |
| 527 | mbedtls_test_cli_crt_len ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 528 | #else |
| 529 | { |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 530 | ret = -1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | mbedtls_printf("MBEDTLS_CERTS_C not defined."); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 532 | } |
| 533 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 534 | if( ret != 0 ) |
| 535 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 536 | 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] | 537 | goto exit; |
| 538 | } |
| 539 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 540 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 541 | if( strlen( opt.key_file ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 543 | else |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 544 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 545 | #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C) |
| 546 | ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key, |
| 547 | mbedtls_test_cli_key_len, NULL, 0 ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 548 | #else |
| 549 | { |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 550 | ret = -1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 551 | mbedtls_printf("MBEDTLS_CERTS_C or MBEDTLS_PEM_PARSE_C not defined."); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 552 | } |
| 553 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 554 | if( ret != 0 ) |
| 555 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | 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] | 557 | goto exit; |
| 558 | } |
| 559 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 560 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 561 | |
| 562 | /* |
| 563 | * 2. Start the connection |
| 564 | */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 565 | mbedtls_printf( " . Connecting to tcp/%s/%s...", opt.server_name, |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 566 | opt.server_port ); |
| 567 | fflush( stdout ); |
| 568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | if( ( ret = mbedtls_net_connect( &server_fd, opt.server_name, |
| 570 | opt.server_port, MBEDTLS_NET_PROTO_TCP ) ) != 0 ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 571 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 572 | mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 573 | goto exit; |
| 574 | } |
| 575 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 576 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * 3. Setup stuff |
| 580 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 581 | mbedtls_printf( " . Setting up the SSL/TLS structure..." ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 582 | fflush( stdout ); |
| 583 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 584 | if( ( ret = mbedtls_ssl_config_defaults( &conf, |
| 585 | MBEDTLS_SSL_IS_CLIENT, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 586 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 587 | MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 588 | { |
| 589 | mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret ); |
| 590 | goto exit; |
| 591 | } |
| 592 | |
Manuel Pégourié-Gonnard | fcf2fc2 | 2014-03-11 11:10:27 +0100 | [diff] [blame] | 593 | /* OPTIONAL is not optimal for security, |
| 594 | * but makes interop easier in this simplified example */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 595 | mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 596 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 597 | mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 598 | mbedtls_ssl_conf_dbg( &conf, my_debug, stdout ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 599 | |
Paul Bakker | 645ce3a | 2012-10-31 12:32:41 +0000 | [diff] [blame] | 600 | if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 601 | mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 602 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 603 | mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 604 | 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] | 605 | { |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 606 | 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] | 607 | goto exit; |
| 608 | } |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 609 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 610 | if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) |
| 611 | { |
| 612 | mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret ); |
| 613 | goto exit; |
| 614 | } |
| 615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 616 | 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] | 617 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | 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] | 619 | goto exit; |
| 620 | } |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 621 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 622 | mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL ); |
| 623 | |
| 624 | mbedtls_printf( " ok\n" ); |
| 625 | |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 626 | if( opt.mode == MODE_SSL_TLS ) |
| 627 | { |
Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 628 | if( do_handshake( &ssl ) != 0 ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 629 | goto exit; |
| 630 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 631 | mbedtls_printf( " > Get header from server:" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 632 | fflush( stdout ); |
| 633 | |
| 634 | ret = write_ssl_and_get_response( &ssl, buf, 0 ); |
| 635 | if( ret < 200 || ret > 299 ) |
| 636 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 637 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 638 | goto exit; |
| 639 | } |
| 640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 641 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 642 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 643 | mbedtls_printf( " > Write EHLO to server:" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 644 | fflush( stdout ); |
| 645 | |
| 646 | gethostname( hostname, 32 ); |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 647 | len = sprintf( (char *) buf, "EHLO %s\r\n", hostname ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 648 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 649 | if( ret < 200 || ret > 299 ) |
| 650 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 651 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 652 | goto exit; |
| 653 | } |
| 654 | } |
| 655 | else |
| 656 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 657 | mbedtls_printf( " > Get header from server:" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 658 | fflush( stdout ); |
| 659 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 660 | ret = write_and_get_response( &server_fd, buf, 0 ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 661 | if( ret < 200 || ret > 299 ) |
| 662 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 664 | goto exit; |
| 665 | } |
| 666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 667 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 669 | mbedtls_printf( " > Write EHLO to server:" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 670 | fflush( stdout ); |
| 671 | |
| 672 | gethostname( hostname, 32 ); |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 673 | len = sprintf( (char *) buf, "EHLO %s\r\n", hostname ); |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 674 | ret = write_and_get_response( &server_fd, buf, len ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 675 | if( ret < 200 || ret > 299 ) |
| 676 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 678 | goto exit; |
| 679 | } |
| 680 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 681 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 683 | mbedtls_printf( " > Write STARTTLS to server:" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 684 | fflush( stdout ); |
| 685 | |
| 686 | gethostname( hostname, 32 ); |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 687 | len = sprintf( (char *) buf, "STARTTLS\r\n" ); |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 688 | ret = write_and_get_response( &server_fd, buf, len ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 689 | if( ret < 200 || ret > 299 ) |
| 690 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 691 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 692 | goto exit; |
| 693 | } |
| 694 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 695 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 696 | |
Manuel Pégourié-Gonnard | 89addc4 | 2015-04-20 10:56:18 +0100 | [diff] [blame] | 697 | if( do_handshake( &ssl ) != 0 ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 698 | goto exit; |
| 699 | } |
| 700 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | #if defined(MBEDTLS_BASE64_C) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 702 | if( opt.authentication ) |
| 703 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 704 | mbedtls_printf( " > Write AUTH LOGIN to server:" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 705 | fflush( stdout ); |
| 706 | |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 707 | len = sprintf( (char *) buf, "AUTH LOGIN\r\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 708 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 709 | if( ret < 200 || ret > 399 ) |
| 710 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 711 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 712 | goto exit; |
| 713 | } |
| 714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | mbedtls_printf( " > Write username to server: %s", opt.user_name ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 718 | fflush( stdout ); |
| 719 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 720 | ret = mbedtls_base64_encode( base, sizeof( base ), &n, (const unsigned char *) opt.user_name, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 721 | strlen( opt.user_name ) ); |
Alfred Klomp | 7c03424 | 2014-07-14 22:11:13 +0200 | [diff] [blame] | 722 | |
| 723 | if( ret != 0 ) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | mbedtls_printf( " failed\n ! mbedtls_base64_encode returned %d\n\n", ret ); |
Alfred Klomp | 7c03424 | 2014-07-14 22:11:13 +0200 | [diff] [blame] | 725 | goto exit; |
| 726 | } |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 727 | len = sprintf( (char *) buf, "%s\r\n", base ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 728 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 729 | if( ret < 300 || ret > 399 ) |
| 730 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 731 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 732 | goto exit; |
| 733 | } |
| 734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 735 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 737 | mbedtls_printf( " > Write password to server: %s", opt.user_pwd ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 738 | fflush( stdout ); |
| 739 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 740 | ret = mbedtls_base64_encode( base, sizeof( base ), &n, (const unsigned char *) opt.user_pwd, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 741 | strlen( opt.user_pwd ) ); |
Alfred Klomp | 7c03424 | 2014-07-14 22:11:13 +0200 | [diff] [blame] | 742 | |
| 743 | if( ret != 0 ) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | mbedtls_printf( " failed\n ! mbedtls_base64_encode returned %d\n\n", ret ); |
Alfred Klomp | 7c03424 | 2014-07-14 22:11:13 +0200 | [diff] [blame] | 745 | goto exit; |
| 746 | } |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 747 | len = sprintf( (char *) buf, "%s\r\n", base ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 748 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 749 | if( ret < 200 || ret > 399 ) |
| 750 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 751 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 752 | goto exit; |
| 753 | } |
| 754 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 755 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 756 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 757 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 758 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 759 | mbedtls_printf( " > Write MAIL FROM to server:" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 760 | fflush( stdout ); |
| 761 | |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 762 | len = sprintf( (char *) buf, "MAIL FROM:<%s>\r\n", opt.mail_from ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 763 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 764 | if( ret < 200 || ret > 299 ) |
| 765 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 766 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 767 | goto exit; |
| 768 | } |
| 769 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 770 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 772 | mbedtls_printf( " > Write RCPT TO to server:" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 773 | fflush( stdout ); |
| 774 | |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 775 | len = sprintf( (char *) buf, "RCPT TO:<%s>\r\n", opt.mail_to ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 776 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 777 | if( ret < 200 || ret > 299 ) |
| 778 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 780 | goto exit; |
| 781 | } |
| 782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 783 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 785 | mbedtls_printf( " > Write DATA to server:" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 786 | fflush( stdout ); |
| 787 | |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 788 | len = sprintf( (char *) buf, "DATA\r\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 789 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 790 | if( ret < 300 || ret > 399 ) |
| 791 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 792 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 793 | goto exit; |
| 794 | } |
| 795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 796 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 797 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 798 | mbedtls_printf( " > Write content to server:" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 799 | fflush( stdout ); |
| 800 | |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 801 | 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] | 802 | "This is a simple test mail from the " |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 803 | "mbed TLS mail client example.\r\n" |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 804 | "\r\n" |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 805 | "Enjoy!", opt.mail_from ); |
| 806 | ret = write_ssl_data( &ssl, buf, len ); |
| 807 | |
| 808 | len = sprintf( (char *) buf, "\r\n.\r\n"); |
| 809 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 810 | if( ret < 200 || ret > 299 ) |
| 811 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 812 | mbedtls_printf( " failed\n ! server responded with %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 813 | goto exit; |
| 814 | } |
| 815 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | mbedtls_printf(" ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 817 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 818 | mbedtls_ssl_close_notify( &ssl ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 819 | |
| 820 | exit: |
| 821 | |
Manuel Pégourié-Gonnard | 3d7d00a | 2015-06-30 15:55:03 +0200 | [diff] [blame^] | 822 | mbedtls_net_free( &server_fd ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 823 | mbedtls_x509_crt_free( &clicert ); |
| 824 | mbedtls_x509_crt_free( &cacert ); |
| 825 | mbedtls_pk_free( &pkey ); |
| 826 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 827 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 828 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 829 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 830 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 831 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 832 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 833 | fflush( stdout ); getchar(); |
| 834 | #endif |
| 835 | |
| 836 | return( ret ); |
| 837 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 838 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && |
| 839 | MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C ** |
| 840 | MBEDTLS_CTR_DRBG_C */ |