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