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