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