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 | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 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 | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdio.h> |
Paul Bakker | fdda785 | 2013-11-30 15:15:31 +0100 | [diff] [blame] | 32 | |
| 33 | #if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
Paul Bakker | fdda785 | 2013-11-30 15:15:31 +0100 | [diff] [blame] | 35 | #else |
| 36 | #include <io.h> |
| 37 | #define read _read |
| 38 | #define write _write |
| 39 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 40 | |
Paul Bakker | 5a83522 | 2011-10-12 09:19:31 +0000 | [diff] [blame] | 41 | #if defined(_WIN32) || defined(_WIN32_WCE) |
| 42 | |
| 43 | #include <winsock2.h> |
| 44 | #include <windows.h> |
| 45 | |
Paul Bakker | f0fc2a2 | 2013-12-30 15:42:43 +0100 | [diff] [blame] | 46 | #if defined(_MSC_VER) |
Paul Bakker | 5a83522 | 2011-10-12 09:19:31 +0000 | [diff] [blame] | 47 | #if defined(_WIN32_WCE) |
| 48 | #pragma comment( lib, "ws2.lib" ) |
| 49 | #else |
| 50 | #pragma comment( lib, "ws2_32.lib" ) |
| 51 | #endif |
Paul Bakker | f0fc2a2 | 2013-12-30 15:42:43 +0100 | [diff] [blame] | 52 | #endif /* _MSC_VER */ |
Paul Bakker | 5a83522 | 2011-10-12 09:19:31 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 55 | #include "polarssl/base64.h" |
| 56 | #include "polarssl/error.h" |
| 57 | #include "polarssl/net.h" |
| 58 | #include "polarssl/ssl.h" |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 59 | #include "polarssl/entropy.h" |
| 60 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 61 | #include "polarssl/certs.h" |
| 62 | #include "polarssl/x509.h" |
| 63 | |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 64 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
| 65 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_CLI_C) || \ |
| 66 | !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 67 | !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 68 | int main( int argc, char *argv[] ) |
| 69 | { |
| 70 | ((void) argc); |
| 71 | ((void) argv); |
| 72 | |
| 73 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
| 74 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_CLI_C and/or " |
| 75 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 76 | "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C " |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 77 | "not defined.\n"); |
| 78 | return( 0 ); |
| 79 | } |
| 80 | #else |
| 81 | |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 82 | #define DFL_SERVER_NAME "localhost" |
| 83 | #define DFL_SERVER_PORT 465 |
| 84 | #define DFL_USER_NAME "user" |
| 85 | #define DFL_USER_PWD "password" |
| 86 | #define DFL_MAIL_FROM "" |
| 87 | #define DFL_MAIL_TO "" |
| 88 | #define DFL_DEBUG_LEVEL 0 |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 89 | #define DFL_CA_FILE "" |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 90 | #define DFL_CRT_FILE "" |
| 91 | #define DFL_KEY_FILE "" |
| 92 | #define DFL_FORCE_CIPHER 0 |
| 93 | #define DFL_MODE 0 |
| 94 | #define DFL_AUTHENTICATION 0 |
| 95 | |
| 96 | #define MODE_SSL_TLS 0 |
| 97 | #define MODE_STARTTLS 0 |
| 98 | |
| 99 | /* |
| 100 | * global options |
| 101 | */ |
| 102 | struct options |
| 103 | { |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 104 | const char *server_name; /* hostname of the server (client only) */ |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 105 | int server_port; /* port on which the ssl service runs */ |
| 106 | int debug_level; /* level of debugging */ |
| 107 | int authentication; /* if authentication is required */ |
| 108 | int mode; /* SSL/TLS (0) or STARTTLS (1) */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 109 | const char *user_name; /* username to use for authentication */ |
| 110 | const char *user_pwd; /* password to use for authentication */ |
| 111 | const char *mail_from; /* E-Mail address to use as sender */ |
| 112 | const char *mail_to; /* E-Mail address to use as recipient */ |
| 113 | const char *ca_file; /* the file with the CA certificate(s) */ |
| 114 | const char *crt_file; /* the file with the client certificate */ |
| 115 | const char *key_file; /* the file with the client key */ |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 116 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
| 117 | } opt; |
| 118 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 119 | static void my_debug( void *ctx, int level, const char *str ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 120 | { |
| 121 | if( level < opt.debug_level ) |
| 122 | { |
| 123 | fprintf( (FILE *) ctx, "%s", str ); |
| 124 | fflush( (FILE *) ctx ); |
| 125 | } |
| 126 | } |
| 127 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 128 | static int do_handshake( ssl_context *ssl, struct options *opt ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 129 | { |
| 130 | int ret; |
| 131 | unsigned char buf[1024]; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 132 | memset(buf, 0, 1024); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * 4. Handshake |
| 136 | */ |
| 137 | printf( " . Performing the SSL/TLS handshake..." ); |
| 138 | fflush( stdout ); |
| 139 | |
| 140 | while( ( ret = ssl_handshake( ssl ) ) != 0 ) |
| 141 | { |
| 142 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 143 | { |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 144 | #if defined(POLARSSL_ERROR_C) |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 145 | polarssl_strerror( ret, (char *) buf, 1024 ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 146 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 147 | printf( " failed\n ! ssl_handshake returned %d: %s\n\n", ret, buf ); |
| 148 | return( -1 ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | printf( " ok\n [ Ciphersuite is %s ]\n", |
| 153 | ssl_get_ciphersuite( ssl ) ); |
| 154 | |
| 155 | /* |
| 156 | * 5. Verify the server certificate |
| 157 | */ |
| 158 | printf( " . Verifying peer X.509 certificate..." ); |
| 159 | |
Manuel Pégourié-Gonnard | fcf2fc2 | 2014-03-11 11:10:27 +0100 | [diff] [blame] | 160 | /* In real life, we may want to bail out when ret != 0 */ |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 161 | if( ( ret = ssl_get_verify_result( ssl ) ) != 0 ) |
| 162 | { |
| 163 | printf( " failed\n" ); |
| 164 | |
| 165 | if( ( ret & BADCERT_EXPIRED ) != 0 ) |
| 166 | printf( " ! server certificate has expired\n" ); |
| 167 | |
| 168 | if( ( ret & BADCERT_REVOKED ) != 0 ) |
| 169 | printf( " ! server certificate has been revoked\n" ); |
| 170 | |
| 171 | if( ( ret & BADCERT_CN_MISMATCH ) != 0 ) |
| 172 | printf( " ! CN mismatch (expected CN=%s)\n", opt->server_name ); |
| 173 | |
| 174 | if( ( ret & BADCERT_NOT_TRUSTED ) != 0 ) |
| 175 | printf( " ! self-signed or not signed by a trusted CA\n" ); |
| 176 | |
| 177 | printf( "\n" ); |
| 178 | } |
| 179 | else |
| 180 | printf( " ok\n" ); |
| 181 | |
| 182 | printf( " . Peer certificate information ...\n" ); |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 183 | x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ", |
| 184 | ssl_get_peer_cert( ssl ) ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 185 | printf( "%s\n", buf ); |
| 186 | |
| 187 | return( 0 ); |
| 188 | } |
| 189 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 190 | static int write_ssl_data( ssl_context *ssl, unsigned char *buf, size_t len ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 191 | { |
| 192 | int ret; |
| 193 | |
| 194 | printf("\n%s", buf); |
| 195 | while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 ) |
| 196 | { |
| 197 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 198 | { |
| 199 | printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
| 200 | return -1; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return( 0 ); |
| 205 | } |
| 206 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 207 | static int write_ssl_and_get_response( ssl_context *ssl, unsigned char *buf, size_t len ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 208 | { |
| 209 | int ret; |
| 210 | unsigned char data[128]; |
| 211 | char code[4]; |
| 212 | size_t i, idx = 0; |
| 213 | |
| 214 | printf("\n%s", buf); |
| 215 | while( len && ( ret = ssl_write( ssl, buf, len ) ) <= 0 ) |
| 216 | { |
| 217 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 218 | { |
| 219 | printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
| 220 | return -1; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | do |
| 225 | { |
| 226 | len = sizeof( data ) - 1; |
| 227 | memset( data, 0, sizeof( data ) ); |
| 228 | ret = ssl_read( ssl, data, len ); |
| 229 | |
| 230 | if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) |
| 231 | continue; |
| 232 | |
| 233 | if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ) |
| 234 | return -1; |
| 235 | |
| 236 | if( ret <= 0 ) |
| 237 | { |
| 238 | printf( "failed\n ! ssl_read returned %d\n\n", ret ); |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | printf("\n%s", data); |
| 243 | len = ret; |
| 244 | for( i = 0; i < len; i++ ) |
| 245 | { |
| 246 | if( data[i] != '\n' ) |
| 247 | { |
| 248 | if( idx < 4 ) |
| 249 | code[ idx++ ] = data[i]; |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' ) |
| 254 | { |
| 255 | code[3] = '\0'; |
| 256 | return atoi( code ); |
| 257 | } |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 258 | |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 259 | idx = 0; |
| 260 | } |
| 261 | } |
| 262 | while( 1 ); |
| 263 | } |
| 264 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 265 | 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] | 266 | { |
| 267 | int ret; |
| 268 | unsigned char data[128]; |
| 269 | char code[4]; |
| 270 | size_t i, idx = 0; |
| 271 | |
| 272 | printf("\n%s", buf); |
| 273 | if( len && ( ret = write( sock_fd, buf, len ) ) <= 0 ) |
| 274 | { |
| 275 | printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | do |
| 280 | { |
| 281 | len = sizeof( data ) - 1; |
| 282 | memset( data, 0, sizeof( data ) ); |
| 283 | ret = read( sock_fd, data, len ); |
| 284 | |
| 285 | if( ret <= 0 ) |
| 286 | { |
| 287 | printf( "failed\n ! read returned %d\n\n", ret ); |
| 288 | return -1; |
| 289 | } |
| 290 | |
Paul Bakker | df71dd1 | 2014-04-17 16:03:48 +0200 | [diff] [blame] | 291 | data[len] = '\0'; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 292 | printf("\n%s", data); |
| 293 | len = ret; |
| 294 | for( i = 0; i < len; i++ ) |
| 295 | { |
| 296 | if( data[i] != '\n' ) |
| 297 | { |
| 298 | if( idx < 4 ) |
| 299 | code[ idx++ ] = data[i]; |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | if( idx == 4 && code[0] >= '0' && code[0] <= '9' && code[3] == ' ' ) |
| 304 | { |
| 305 | code[3] = '\0'; |
| 306 | return atoi( code ); |
| 307 | } |
| 308 | |
| 309 | idx = 0; |
| 310 | } |
| 311 | } |
| 312 | while( 1 ); |
| 313 | } |
| 314 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 315 | #if defined(POLARSSL_BASE64_C) |
| 316 | #define USAGE_AUTH \ |
| 317 | " authentication=%%d default: 0 (disabled)\n" \ |
| 318 | " user_name=%%s default: \"user\"\n" \ |
| 319 | " user_pwd=%%s default: \"password\"\n" |
| 320 | #else |
| 321 | #define USAGE_AUTH \ |
| 322 | " authentication options disabled. (Require POLARSSL_BASE64_C)\n" |
| 323 | #endif /* POLARSSL_BASE64_C */ |
| 324 | |
| 325 | #if defined(POLARSSL_FS_IO) |
| 326 | #define USAGE_IO \ |
| 327 | " ca_file=%%s default: \"\" (pre-loaded)\n" \ |
| 328 | " crt_file=%%s default: \"\" (pre-loaded)\n" \ |
| 329 | " key_file=%%s default: \"\" (pre-loaded)\n" |
| 330 | #else |
| 331 | #define USAGE_IO \ |
| 332 | " No file operations available (POLARSSL_FS_IO not defined)\n" |
| 333 | #endif /* POLARSSL_FS_IO */ |
| 334 | |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 335 | #define USAGE \ |
| 336 | "\n usage: ssl_mail_client param=<>...\n" \ |
| 337 | "\n acceptable parameters:\n" \ |
| 338 | " server_name=%%s default: localhost\n" \ |
| 339 | " server_port=%%d default: 4433\n" \ |
| 340 | " debug_level=%%d default: 0 (disabled)\n" \ |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 341 | " mode=%%d default: 0 (SSL/TLS) (1 for STARTTLS)\n" \ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 342 | USAGE_AUTH \ |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 343 | " mail_from=%%s default: \"\"\n" \ |
| 344 | " mail_to=%%s default: \"\"\n" \ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 345 | USAGE_IO \ |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 346 | " force_ciphersuite=<name> default: all enabled\n"\ |
| 347 | " acceptable ciphersuite names:\n" |
| 348 | |
| 349 | int main( int argc, char *argv[] ) |
| 350 | { |
| 351 | int ret = 0, len, server_fd; |
Paul Bakker | 09c9dd8 | 2014-08-18 11:06:56 +0200 | [diff] [blame] | 352 | unsigned char buf[1024]; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 353 | #if defined(POLARSSL_BASE64_C) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 354 | unsigned char base[1024]; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 355 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 356 | char hostname[32]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 357 | const char *pers = "ssl_mail_client"; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 358 | |
| 359 | entropy_context entropy; |
| 360 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 361 | ssl_context ssl; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 362 | x509_crt cacert; |
| 363 | x509_crt clicert; |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 364 | pk_context pkey; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 365 | int i; |
Paul Bakker | 819370c | 2012-09-28 07:04:41 +0000 | [diff] [blame] | 366 | size_t n; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 367 | char *p, *q; |
| 368 | const int *list; |
| 369 | |
| 370 | /* |
Manuel Pégourié-Gonnard | 3bd2aae | 2013-09-20 13:10:13 +0200 | [diff] [blame] | 371 | * Make sure memory references are valid in case we exit early. |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 372 | */ |
| 373 | server_fd = 0; |
Manuel Pégourié-Gonnard | 3bd2aae | 2013-09-20 13:10:13 +0200 | [diff] [blame] | 374 | memset( &ssl, 0, sizeof( ssl_context ) ); |
Paul Bakker | 333fdec | 2014-08-04 12:12:09 +0200 | [diff] [blame] | 375 | memset( &buf, 0, sizeof( buf ) ); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 376 | x509_crt_init( &cacert ); |
| 377 | x509_crt_init( &clicert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 378 | pk_init( &pkey ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 379 | |
| 380 | if( argc == 0 ) |
| 381 | { |
| 382 | usage: |
| 383 | printf( USAGE ); |
| 384 | |
| 385 | list = ssl_list_ciphersuites(); |
| 386 | while( *list ) |
| 387 | { |
| 388 | printf(" %s\n", ssl_get_ciphersuite_name( *list ) ); |
| 389 | list++; |
| 390 | } |
| 391 | printf("\n"); |
| 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 ) |
| 419 | { |
| 420 | opt.server_port = atoi( q ); |
| 421 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
| 422 | goto usage; |
| 423 | } |
| 424 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 425 | { |
| 426 | opt.debug_level = atoi( q ); |
| 427 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 428 | goto usage; |
| 429 | } |
| 430 | else if( strcmp( p, "authentication" ) == 0 ) |
| 431 | { |
| 432 | opt.authentication = atoi( q ); |
| 433 | if( opt.authentication < 0 || opt.authentication > 1 ) |
| 434 | goto usage; |
| 435 | } |
| 436 | else if( strcmp( p, "mode" ) == 0 ) |
| 437 | { |
| 438 | opt.mode = atoi( q ); |
| 439 | if( opt.mode < 0 || opt.mode > 1 ) |
| 440 | goto usage; |
| 441 | } |
| 442 | else if( strcmp( p, "user_name" ) == 0 ) |
| 443 | opt.user_name = q; |
| 444 | else if( strcmp( p, "user_pwd" ) == 0 ) |
| 445 | opt.user_pwd = q; |
| 446 | else if( strcmp( p, "mail_from" ) == 0 ) |
| 447 | opt.mail_from = q; |
| 448 | else if( strcmp( p, "mail_to" ) == 0 ) |
| 449 | opt.mail_to = q; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 450 | else if( strcmp( p, "ca_file" ) == 0 ) |
| 451 | opt.ca_file = q; |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 452 | else if( strcmp( p, "crt_file" ) == 0 ) |
| 453 | opt.crt_file = q; |
| 454 | else if( strcmp( p, "key_file" ) == 0 ) |
| 455 | opt.key_file = q; |
| 456 | else if( strcmp( p, "force_ciphersuite" ) == 0 ) |
| 457 | { |
| 458 | opt.force_ciphersuite[0] = -1; |
| 459 | |
| 460 | opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q ); |
| 461 | |
| 462 | if( opt.force_ciphersuite[0] <= 0 ) |
| 463 | goto usage; |
| 464 | |
| 465 | opt.force_ciphersuite[1] = 0; |
| 466 | } |
| 467 | else |
| 468 | goto usage; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * 0. Initialize the RNG and the session data |
| 473 | */ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 474 | printf( "\n . Seeding the random number generator..." ); |
| 475 | fflush( stdout ); |
| 476 | |
| 477 | entropy_init( &entropy ); |
| 478 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 479 | (const unsigned char *) pers, |
| 480 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 481 | { |
| 482 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 483 | goto exit; |
| 484 | } |
| 485 | |
| 486 | printf( " ok\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 487 | |
| 488 | /* |
| 489 | * 1.1. Load the trusted CA |
| 490 | */ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 491 | printf( " . Loading the CA root certificate ..." ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 492 | fflush( stdout ); |
| 493 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 494 | #if defined(POLARSSL_FS_IO) |
| 495 | if( strlen( opt.ca_file ) ) |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 496 | ret = x509_crt_parse_file( &cacert, opt.ca_file ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 497 | else |
| 498 | #endif |
| 499 | #if defined(POLARSSL_CERTS_C) |
Manuel Pégourié-Gonnard | 641de71 | 2013-09-25 13:23:33 +0200 | [diff] [blame] | 500 | ret = x509_crt_parse( &cacert, (const unsigned char *) test_ca_list, |
| 501 | strlen( test_ca_list ) ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 502 | #else |
| 503 | { |
| 504 | ret = 1; |
| 505 | printf("POLARSSL_CERTS_C not defined."); |
| 506 | } |
| 507 | #endif |
Paul Bakker | 4248823 | 2012-05-16 08:21:05 +0000 | [diff] [blame] | 508 | if( ret < 0 ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 509 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 510 | printf( " failed\n ! x509_crt_parse returned %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 511 | goto exit; |
| 512 | } |
| 513 | |
Paul Bakker | 4248823 | 2012-05-16 08:21:05 +0000 | [diff] [blame] | 514 | printf( " ok (%d skipped)\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 515 | |
| 516 | /* |
| 517 | * 1.2. Load own certificate and private key |
| 518 | * |
| 519 | * (can be skipped if client authentication is not required) |
| 520 | */ |
| 521 | printf( " . Loading the client cert. and key..." ); |
| 522 | fflush( stdout ); |
| 523 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 524 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 525 | if( strlen( opt.crt_file ) ) |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 526 | ret = x509_crt_parse_file( &clicert, opt.crt_file ); |
| 527 | else |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 528 | #endif |
| 529 | #if defined(POLARSSL_CERTS_C) |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 530 | ret = x509_crt_parse( &clicert, (const unsigned char *) test_cli_crt, |
| 531 | strlen( test_cli_crt ) ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 532 | #else |
| 533 | { |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 534 | ret = -1; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 535 | printf("POLARSSL_CERTS_C not defined."); |
| 536 | } |
| 537 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 538 | if( ret != 0 ) |
| 539 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 540 | printf( " failed\n ! x509_crt_parse returned %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 541 | goto exit; |
| 542 | } |
| 543 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 544 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 545 | if( strlen( opt.key_file ) ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 546 | ret = pk_parse_keyfile( &pkey, opt.key_file, "" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 547 | else |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 548 | #endif |
| 549 | #if defined(POLARSSL_CERTS_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 550 | ret = pk_parse_key( &pkey, (const unsigned char *) test_cli_key, |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 551 | strlen( test_cli_key ), NULL, 0 ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 552 | #else |
| 553 | { |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 554 | ret = -1; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 555 | printf("POLARSSL_CERTS_C not defined."); |
| 556 | } |
| 557 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 558 | if( ret != 0 ) |
| 559 | { |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 560 | printf( " failed\n ! pk_parse_key returned %d\n\n", ret ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 561 | goto exit; |
| 562 | } |
| 563 | |
| 564 | printf( " ok\n" ); |
| 565 | |
| 566 | /* |
| 567 | * 2. Start the connection |
| 568 | */ |
| 569 | printf( " . Connecting to tcp/%s/%-4d...", opt.server_name, |
| 570 | opt.server_port ); |
| 571 | fflush( stdout ); |
| 572 | |
| 573 | if( ( ret = net_connect( &server_fd, opt.server_name, |
| 574 | opt.server_port ) ) != 0 ) |
| 575 | { |
| 576 | printf( " failed\n ! net_connect returned %d\n\n", ret ); |
| 577 | goto exit; |
| 578 | } |
| 579 | |
| 580 | printf( " ok\n" ); |
| 581 | |
| 582 | /* |
| 583 | * 3. Setup stuff |
| 584 | */ |
| 585 | printf( " . Setting up the SSL/TLS structure..." ); |
| 586 | fflush( stdout ); |
| 587 | |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 588 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 589 | { |
| 590 | printf( " failed\n ! ssl_init returned %d\n\n", ret ); |
| 591 | goto exit; |
| 592 | } |
| 593 | |
| 594 | printf( " ok\n" ); |
| 595 | |
| 596 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
Manuel Pégourié-Gonnard | fcf2fc2 | 2014-03-11 11:10:27 +0100 | [diff] [blame] | 597 | /* OPTIONAL is not optimal for security, |
| 598 | * but makes interop easier in this simplified example */ |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 599 | ssl_set_authmode( &ssl, SSL_VERIFY_OPTIONAL ); |
| 600 | |
Manuel Pégourié-Gonnard | 448ea50 | 2015-01-12 11:40:14 +0100 | [diff] [blame] | 601 | /* SSLv3 is deprecated, set minimum to TLS 1.0 */ |
| 602 | ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 ); |
Manuel Pégourié-Gonnard | fa06581 | 2015-01-12 14:05:33 +0100 | [diff] [blame] | 603 | /* RC4 is deprecated, disable it */ |
| 604 | ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED ); |
Manuel Pégourié-Gonnard | 448ea50 | 2015-01-12 11:40:14 +0100 | [diff] [blame] | 605 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 606 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 607 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 608 | ssl_set_bio( &ssl, net_recv, &server_fd, |
| 609 | net_send, &server_fd ); |
| 610 | |
Paul Bakker | 645ce3a | 2012-10-31 12:32:41 +0000 | [diff] [blame] | 611 | if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 612 | ssl_set_ciphersuites( &ssl, opt.force_ciphersuite ); |
| 613 | |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 614 | ssl_set_ca_chain( &ssl, &cacert, NULL, opt.server_name ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 615 | if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 ) |
| 616 | { |
| 617 | printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret ); |
| 618 | goto exit; |
| 619 | } |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 620 | |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 621 | #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 622 | if( ( ret = ssl_set_hostname( &ssl, opt.server_name ) ) != 0 ) |
| 623 | { |
| 624 | printf( " failed\n ! ssl_set_hostname returned %d\n\n", ret ); |
| 625 | goto exit; |
| 626 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 627 | #endif |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 628 | |
| 629 | if( opt.mode == MODE_SSL_TLS ) |
| 630 | { |
| 631 | if( do_handshake( &ssl, &opt ) != 0 ) |
| 632 | goto exit; |
| 633 | |
| 634 | printf( " > Get header from server:" ); |
| 635 | fflush( stdout ); |
| 636 | |
| 637 | ret = write_ssl_and_get_response( &ssl, buf, 0 ); |
| 638 | if( ret < 200 || ret > 299 ) |
| 639 | { |
| 640 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 641 | goto exit; |
| 642 | } |
| 643 | |
| 644 | printf(" ok\n" ); |
| 645 | |
| 646 | printf( " > Write EHLO to server:" ); |
| 647 | fflush( stdout ); |
| 648 | |
| 649 | gethostname( hostname, 32 ); |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 650 | len = sprintf( (char *) buf, "EHLO %s\r\n", hostname ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 651 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 652 | if( ret < 200 || ret > 299 ) |
| 653 | { |
| 654 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 655 | goto exit; |
| 656 | } |
| 657 | } |
| 658 | else |
| 659 | { |
| 660 | printf( " > Get header from server:" ); |
| 661 | fflush( stdout ); |
| 662 | |
| 663 | ret = write_and_get_response( server_fd, buf, 0 ); |
| 664 | if( ret < 200 || ret > 299 ) |
| 665 | { |
| 666 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 667 | goto exit; |
| 668 | } |
| 669 | |
| 670 | printf(" ok\n" ); |
| 671 | |
| 672 | printf( " > Write EHLO to server:" ); |
| 673 | fflush( stdout ); |
| 674 | |
| 675 | gethostname( hostname, 32 ); |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 676 | len = sprintf( (char *) buf, "EHLO %s\r\n", hostname ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 677 | ret = write_and_get_response( server_fd, buf, len ); |
| 678 | if( ret < 200 || ret > 299 ) |
| 679 | { |
| 680 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 681 | goto exit; |
| 682 | } |
| 683 | |
| 684 | printf(" ok\n" ); |
| 685 | |
| 686 | printf( " > Write STARTTLS to server:" ); |
| 687 | fflush( stdout ); |
| 688 | |
| 689 | gethostname( hostname, 32 ); |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 690 | len = sprintf( (char *) buf, "STARTTLS\r\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 691 | ret = write_and_get_response( server_fd, buf, len ); |
| 692 | if( ret < 200 || ret > 299 ) |
| 693 | { |
| 694 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 695 | goto exit; |
| 696 | } |
| 697 | |
| 698 | printf(" ok\n" ); |
| 699 | |
| 700 | if( do_handshake( &ssl, &opt ) != 0 ) |
| 701 | goto exit; |
| 702 | } |
| 703 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 704 | #if defined(POLARSSL_BASE64_C) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 705 | if( opt.authentication ) |
| 706 | { |
| 707 | printf( " > Write AUTH LOGIN to server:" ); |
| 708 | fflush( stdout ); |
| 709 | |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 710 | len = sprintf( (char *) buf, "AUTH LOGIN\r\n" ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 711 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 712 | if( ret < 200 || ret > 399 ) |
| 713 | { |
| 714 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 715 | goto exit; |
| 716 | } |
| 717 | |
| 718 | printf(" ok\n" ); |
| 719 | |
| 720 | printf( " > Write username to server: %s", opt.user_name ); |
| 721 | fflush( stdout ); |
| 722 | |
| 723 | n = sizeof( buf ); |
Alfred Klomp | 7c03424 | 2014-07-14 22:11:13 +0200 | [diff] [blame] | 724 | ret = base64_encode( base, &n, (const unsigned char *) opt.user_name, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 725 | strlen( opt.user_name ) ); |
Alfred Klomp | 7c03424 | 2014-07-14 22:11:13 +0200 | [diff] [blame] | 726 | |
| 727 | if( ret != 0 ) { |
| 728 | printf( " failed\n ! base64_encode returned %d\n\n", ret ); |
| 729 | goto exit; |
| 730 | } |
Paul Bakker | d75ba40 | 2014-01-24 16:11:17 +0100 | [diff] [blame] | 731 | len = sprintf( (char *) buf, "%s\r\n", base ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 732 | ret = write_ssl_and_get_response( &ssl, buf, len ); |
| 733 | if( ret < 300 || ret > 399 ) |
| 734 | { |
| 735 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 736 | goto exit; |
| 737 | } |
| 738 | |
| 739 | printf(" ok\n" ); |
| 740 | |
| 741 | printf( " > Write password to server: %s", opt.user_pwd ); |
| 742 | fflush( stdout ); |
| 743 | |
Alfred Klomp | 7c03424 | 2014-07-14 22:11:13 +0200 | [diff] [blame] | 744 | ret = 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 ) { |
| 748 | printf( " failed\n ! base64_encode returned %d\n\n", ret ); |
| 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 | { |
| 755 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 756 | goto exit; |
| 757 | } |
| 758 | |
| 759 | printf(" ok\n" ); |
| 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 | |
| 763 | printf( " > Write MAIL FROM to server:" ); |
| 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 | { |
| 770 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 771 | goto exit; |
| 772 | } |
| 773 | |
| 774 | printf(" ok\n" ); |
| 775 | |
| 776 | printf( " > Write RCPT TO to server:" ); |
| 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 | { |
| 783 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 784 | goto exit; |
| 785 | } |
| 786 | |
| 787 | printf(" ok\n" ); |
| 788 | |
| 789 | printf( " > Write DATA to server:" ); |
| 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 | { |
| 796 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 797 | goto exit; |
| 798 | } |
| 799 | |
| 800 | printf(" ok\n" ); |
| 801 | |
| 802 | printf( " > Write content to server:" ); |
| 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 | { |
| 816 | printf( " failed\n ! server responded with %d\n\n", ret ); |
| 817 | goto exit; |
| 818 | } |
| 819 | |
| 820 | printf(" ok\n" ); |
| 821 | |
| 822 | ssl_close_notify( &ssl ); |
| 823 | |
| 824 | exit: |
| 825 | |
| 826 | if( server_fd ) |
| 827 | net_close( server_fd ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 828 | x509_crt_free( &clicert ); |
| 829 | x509_crt_free( &cacert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 830 | pk_free( &pkey ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 831 | ssl_free( &ssl ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 832 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 833 | entropy_free( &entropy ); |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 834 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 835 | #if defined(_WIN32) |
Paul Bakker | 1496d38 | 2011-05-23 12:07:29 +0000 | [diff] [blame] | 836 | printf( " + Press Enter to exit this program.\n" ); |
| 837 | fflush( stdout ); getchar(); |
| 838 | #endif |
| 839 | |
| 840 | return( ret ); |
| 841 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 842 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C && |
| 843 | POLARSSL_SSL_CLI_C && POLARSSL_NET_C && POLARSSL_RSA_C ** |
| 844 | POLARSSL_CTR_DRBG_C */ |