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