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