Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL/TLS stress testing program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame^] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 967a2a5 | 2015-01-22 14:28:16 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 27 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else |
| 29 | #include POLARSSL_CONFIG_FILE |
| 30 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> |
| 35 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 36 | #include "polarssl/net.h" |
| 37 | #include "polarssl/ssl.h" |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 38 | #include "polarssl/entropy.h" |
| 39 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 40 | #include "polarssl/certs.h" |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 41 | #if defined(POLARSSL_TIMING_C) |
| 42 | #include "polarssl/timing.h" |
| 43 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 44 | |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 45 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
| 46 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \ |
| 47 | !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_NET_C) || \ |
| 48 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \ |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 49 | !defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 50 | int main( int argc, char *argv[] ) |
| 51 | { |
| 52 | ((void) argc); |
| 53 | ((void) argv); |
| 54 | |
| 55 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
| 56 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or " |
| 57 | "POLARSSL_SSL_CLI_C and/or POLARSSL_NET_C and/or " |
| 58 | "POLARSSL_RSA_C and/or POLARSSL_CTR_DRBG_C and/or " |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 59 | "POLARSSL_X509_CRT_PARSE_C not defined.\n"); |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 60 | return( 0 ); |
| 61 | } |
| 62 | #else |
| 63 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | #define OPMODE_NONE 0 |
| 65 | #define OPMODE_CLIENT 1 |
| 66 | #define OPMODE_SERVER 2 |
| 67 | |
| 68 | #define IOMODE_BLOCK 0 |
| 69 | #define IOMODE_NONBLOCK 1 |
| 70 | |
| 71 | #define COMMAND_READ 1 |
| 72 | #define COMMAND_WRITE 2 |
| 73 | #define COMMAND_BOTH 3 |
| 74 | |
| 75 | #define DFL_OPMODE OPMODE_NONE |
| 76 | #define DFL_IOMODE IOMODE_BLOCK |
| 77 | #define DFL_SERVER_NAME "localhost" |
| 78 | #define DFL_SERVER_PORT 4433 |
| 79 | #define DFL_COMMAND COMMAND_READ |
| 80 | #define DFL_BUFFER_SIZE 1024 |
| 81 | #define DFL_MAX_BYTES 0 |
| 82 | #define DFL_DEBUG_LEVEL 0 |
| 83 | #define DFL_CONN_TIMEOUT 0 |
| 84 | #define DFL_MAX_CONNECTIONS 0 |
| 85 | #define DFL_SESSION_REUSE 1 |
| 86 | #define DFL_SESSION_LIFETIME 86400 |
| 87 | #define DFL_FORCE_CIPHER 0 |
| 88 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 | int server_fd = -1; |
| 90 | |
| 91 | /* |
| 92 | * global options |
| 93 | */ |
| 94 | struct options |
| 95 | { |
| 96 | int opmode; /* operation mode (client or server) */ |
| 97 | int iomode; /* I/O mode (blocking or non-blocking) */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 98 | const char *server_name; /* hostname of the server (client only) */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | int server_port; /* port on which the ssl service runs */ |
| 100 | int command; /* what to do: read or write operation */ |
| 101 | int buffer_size; /* size of the send/receive buffer */ |
| 102 | int max_bytes; /* max. # of bytes before a reconnect */ |
| 103 | int debug_level; /* level of debugging */ |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 104 | #if defined(POLARSSL_TIMING_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | int conn_timeout; /* max. delay before a reconnect */ |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 106 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | int max_connections; /* max. number of reconnections */ |
| 108 | int session_reuse; /* flag to reuse the keying material */ |
| 109 | int session_lifetime; /* if reached, session data is expired */ |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 110 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /* |
| 114 | * Although this PRNG has good statistical properties (eg. passes |
| 115 | * DIEHARD), it is not cryptographically secure. |
| 116 | */ |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 117 | static unsigned long int lcppm5( unsigned long int *state ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | { |
| 119 | unsigned long int u, v; |
| 120 | |
| 121 | u = v = state[4] ^ 1; |
| 122 | state[u & 3] ^= u; |
| 123 | u ^= (v << 12) ^ (v >> 12); |
| 124 | u ^= v * state[0]; v >>= 8; |
| 125 | u ^= v * state[1]; v >>= 8; |
| 126 | u ^= v * state[2]; v >>= 8; |
| 127 | u ^= v * state[3]; |
| 128 | u &= 0xFFFFFFFF; |
| 129 | state[4] = u; |
| 130 | |
| 131 | return( u ); |
| 132 | } |
| 133 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 134 | static void my_debug( void *ctx, int level, const char *str ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 | { |
| 136 | if( level < ((struct options *) ctx)->debug_level ) |
| 137 | fprintf( stderr, "%s", str ); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * perform a single SSL connection |
| 142 | */ |
| 143 | static int ssl_test( struct options *opt ) |
| 144 | { |
Alfred Klomp | 5b78f21 | 2014-07-14 22:10:14 +0200 | [diff] [blame] | 145 | int ret = 1, i; |
Manuel Pégourié-Gonnard | 68821da | 2013-09-16 12:34:33 +0200 | [diff] [blame] | 146 | int client_fd = -1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 147 | int bytes_to_read; |
| 148 | int bytes_to_write; |
Paul Bakker | 026c03b | 2009-03-28 17:53:03 +0000 | [diff] [blame] | 149 | int offset_to_read = 0; |
| 150 | int offset_to_write = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 151 | |
| 152 | long int nb_read; |
| 153 | long int nb_written; |
| 154 | |
| 155 | unsigned long read_state[5]; |
| 156 | unsigned long write_state[5]; |
| 157 | |
Paul Bakker | 026c03b | 2009-03-28 17:53:03 +0000 | [diff] [blame] | 158 | unsigned char *read_buf = NULL; |
| 159 | unsigned char *write_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 161 | const char *pers = "ssl_test"; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 162 | |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 163 | #if defined(POLARSSL_TIMING_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | struct hr_time t; |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 165 | #endif |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 166 | entropy_context entropy; |
| 167 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | ssl_context ssl; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 169 | x509_crt srvcert; |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 170 | pk_context pkey; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 172 | memset( &ssl, 0, sizeof(ssl_context) ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 173 | entropy_init( &entropy ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 174 | x509_crt_init( &srvcert ); |
| 175 | pk_init( &pkey ); |
| 176 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 177 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 178 | (const unsigned char *) pers, |
| 179 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 180 | { |
| 181 | printf( " ! ctr_drbg_init returned %d\n", ret ); |
| 182 | goto exit; |
| 183 | } |
| 184 | |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 185 | #if defined(POLARSSL_TIMING_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | get_timer( &t, 1 ); |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 187 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | |
| 189 | memset( read_state, 0, sizeof( read_state ) ); |
| 190 | memset( write_state, 0, sizeof( write_state ) ); |
| 191 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | |
| 193 | if( opt->opmode == OPMODE_CLIENT ) |
| 194 | { |
| 195 | if( ( ret = net_connect( &client_fd, opt->server_name, |
| 196 | opt->server_port ) ) != 0 ) |
| 197 | { |
| 198 | printf( " ! net_connect returned %d\n\n", ret ); |
| 199 | return( ret ); |
| 200 | } |
| 201 | |
| 202 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 203 | { |
| 204 | printf( " ! ssl_init returned %d\n\n", ret ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 205 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
| 209 | } |
| 210 | |
| 211 | if( opt->opmode == OPMODE_SERVER ) |
| 212 | { |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 213 | #if !defined(POLARSSL_CERTS_C) |
| 214 | printf("POLARSSL_CERTS_C not defined.\n"); |
| 215 | goto exit; |
| 216 | #else |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 217 | ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt, |
| 218 | strlen( test_srv_crt ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 219 | if( ret != 0 ) |
| 220 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 221 | printf( " ! x509_crt_parse returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 222 | goto exit; |
| 223 | } |
| 224 | |
Manuel Pégourié-Gonnard | 641de71 | 2013-09-25 13:23:33 +0200 | [diff] [blame] | 225 | ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list, |
| 226 | strlen( test_ca_list ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 227 | if( ret != 0 ) |
| 228 | { |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 229 | printf( " ! x509_crt_parse returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 230 | goto exit; |
| 231 | } |
| 232 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 233 | ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key, |
| 234 | strlen( test_srv_key ), NULL, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | if( ret != 0 ) |
| 236 | { |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 237 | printf( " ! pk_parse_key returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | goto exit; |
| 239 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 240 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 241 | |
| 242 | if( server_fd < 0 ) |
| 243 | { |
| 244 | if( ( ret = net_bind( &server_fd, NULL, |
| 245 | opt->server_port ) ) != 0 ) |
| 246 | { |
| 247 | printf( " ! net_bind returned %d\n\n", ret ); |
| 248 | return( ret ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if( ( ret = net_accept( server_fd, &client_fd, NULL ) ) != 0 ) |
| 253 | { |
| 254 | printf( " ! net_accept returned %d\n\n", ret ); |
| 255 | return( ret ); |
| 256 | } |
| 257 | |
| 258 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 259 | { |
| 260 | printf( " ! ssl_init returned %d\n\n", ret ); |
| 261 | return( ret ); |
| 262 | } |
| 263 | |
| 264 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 265 | ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 266 | if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 ) |
| 267 | { |
| 268 | printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret ); |
| 269 | goto exit; |
| 270 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); |
| 274 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 275 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 276 | ssl_set_dbg( &ssl, my_debug, opt ); |
| 277 | ssl_set_bio( &ssl, net_recv, &client_fd, |
| 278 | net_send, &client_fd ); |
| 279 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 280 | if( opt->force_ciphersuite[0] != DFL_FORCE_CIPHER ) |
| 281 | ssl_set_ciphersuites( &ssl, opt->force_ciphersuite ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 282 | |
| 283 | if( opt->iomode == IOMODE_NONBLOCK ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 284 | { |
| 285 | if( ( ret = net_set_nonblock( client_fd ) ) != 0 ) |
| 286 | { |
| 287 | printf( " ! net_set_nonblock returned %d\n\n", ret ); |
| 288 | return( ret ); |
| 289 | } |
| 290 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 291 | |
| 292 | read_buf = (unsigned char *) malloc( opt->buffer_size ); |
| 293 | write_buf = (unsigned char *) malloc( opt->buffer_size ); |
| 294 | |
| 295 | if( read_buf == NULL || write_buf == NULL ) |
| 296 | { |
| 297 | printf( " ! malloc(%d bytes) failed\n\n", opt->buffer_size ); |
| 298 | goto exit; |
| 299 | } |
| 300 | |
| 301 | nb_read = bytes_to_read = 0; |
| 302 | nb_written = bytes_to_write = 0; |
| 303 | |
| 304 | while( 1 ) |
| 305 | { |
| 306 | if( opt->command & COMMAND_WRITE ) |
| 307 | { |
| 308 | if( bytes_to_write == 0 ) |
| 309 | { |
| 310 | while( bytes_to_write == 0 ) |
| 311 | bytes_to_write = rand() % opt->buffer_size; |
| 312 | |
| 313 | for( i = 0; i < bytes_to_write; i++ ) |
| 314 | write_buf[i] = (unsigned char) lcppm5( write_state ); |
| 315 | |
| 316 | offset_to_write = 0; |
| 317 | } |
| 318 | |
| 319 | ret = ssl_write( &ssl, write_buf + offset_to_write, |
| 320 | bytes_to_write ); |
| 321 | |
| 322 | if( ret >= 0 ) |
| 323 | { |
| 324 | nb_written += ret; |
| 325 | bytes_to_write -= ret; |
| 326 | offset_to_write += ret; |
| 327 | } |
| 328 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 329 | if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY || |
| 330 | ret == POLARSSL_ERR_NET_CONN_RESET ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 331 | { |
| 332 | ret = 0; |
| 333 | goto exit; |
| 334 | } |
| 335 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 336 | if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ && |
| 337 | ret != POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 | { |
| 339 | printf( " ! ssl_write returned %d\n\n", ret ); |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if( opt->command & COMMAND_READ ) |
| 345 | { |
Paul Bakker | 396333e | 2013-09-26 13:32:19 +0200 | [diff] [blame] | 346 | while( bytes_to_read == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 347 | { |
| 348 | bytes_to_read = rand() % opt->buffer_size; |
| 349 | offset_to_read = 0; |
| 350 | } |
| 351 | |
| 352 | ret = ssl_read( &ssl, read_buf + offset_to_read, |
| 353 | bytes_to_read ); |
| 354 | |
Paul Bakker | 396333e | 2013-09-26 13:32:19 +0200 | [diff] [blame] | 355 | if( ret > 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 356 | { |
| 357 | for( i = 0; i < ret; i++ ) |
| 358 | { |
| 359 | if( read_buf[offset_to_read + i] != |
| 360 | (unsigned char) lcppm5( read_state ) ) |
| 361 | { |
| 362 | ret = 1; |
| 363 | printf( " ! plaintext mismatch\n\n" ); |
| 364 | goto exit; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | nb_read += ret; |
| 369 | bytes_to_read -= ret; |
| 370 | offset_to_read += ret; |
| 371 | } |
| 372 | |
Paul Bakker | 396333e | 2013-09-26 13:32:19 +0200 | [diff] [blame] | 373 | if( ret == 0 || |
| 374 | ret == POLARSSL_ERR_SSL_CONN_EOF || |
| 375 | ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY || |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 376 | ret == POLARSSL_ERR_NET_CONN_RESET ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 377 | { |
| 378 | ret = 0; |
| 379 | goto exit; |
| 380 | } |
| 381 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 382 | if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ && |
| 383 | ret != POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 384 | { |
| 385 | printf( " ! ssl_read returned %d\n\n", ret ); |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | ret = 0; |
| 391 | |
| 392 | if( opt->max_bytes != 0 && |
| 393 | ( opt->max_bytes <= nb_read || |
| 394 | opt->max_bytes <= nb_written ) ) |
| 395 | break; |
| 396 | |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 397 | #if defined(POLARSSL_TIMING_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 398 | if( opt->conn_timeout != 0 && |
| 399 | opt->conn_timeout <= (int) get_timer( &t, 0 ) ) |
| 400 | break; |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 401 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | exit: |
| 405 | |
| 406 | fflush( stdout ); |
| 407 | |
| 408 | if( read_buf != NULL ) |
| 409 | free( read_buf ); |
| 410 | |
| 411 | if( write_buf != NULL ) |
| 412 | free( write_buf ); |
| 413 | |
| 414 | ssl_close_notify( &ssl ); |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 415 | x509_crt_free( &srvcert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 416 | pk_free( &pkey ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 417 | ssl_free( &ssl ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 418 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 419 | entropy_free( &entropy ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 420 | |
| 421 | if( client_fd != -1 ) |
| 422 | net_close( client_fd ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 423 | |
| 424 | return( ret ); |
| 425 | } |
| 426 | |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 427 | #if defined(POLARSSL_TIMING_C) |
| 428 | #define USAGE_TIMING \ |
| 429 | " conn_timeout=%%d (ms) default: 0 (no timeout)\n" |
| 430 | #else |
| 431 | #define USAGE_TIMING "" |
| 432 | #endif |
| 433 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 434 | #define USAGE \ |
| 435 | "\n usage: ssl_test opmode=<> command=<>...\n" \ |
| 436 | "\n acceptable parameters:\n" \ |
| 437 | " opmode=client/server default: <none>\n" \ |
| 438 | " iomode=block/nonblock default: block\n" \ |
| 439 | " server_name=%%s default: localhost\n" \ |
| 440 | " server_port=%%d default: 4433\n" \ |
| 441 | " command=read/write/both default: read\n" \ |
| 442 | " buffer_size=%%d (bytes) default: 1024\n" \ |
| 443 | " max_bytes=%%d (bytes) default: 0 (no limit)\n" \ |
| 444 | " debug_level=%%d default: 0 (disabled)\n" \ |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 445 | USAGE_TIMING \ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 446 | " max_connections=%%d default: 0 (no limit)\n" \ |
| 447 | " session_reuse=on/off default: on (enabled)\n" \ |
| 448 | " session_lifetime=%%d (s) default: 86400\n" \ |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 449 | " force_ciphersuite=<name> default: all enabled\n" \ |
| 450 | " acceptable ciphersuite names:\n" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 451 | |
| 452 | int main( int argc, char *argv[] ) |
| 453 | { |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 454 | int i; |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 455 | const int *list; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 456 | int ret = 1; |
| 457 | int nb_conn; |
| 458 | char *p, *q; |
| 459 | struct options opt; |
| 460 | |
| 461 | if( argc == 1 ) |
| 462 | { |
| 463 | usage: |
| 464 | printf( USAGE ); |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 465 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 466 | list = ssl_list_ciphersuites(); |
| 467 | while( *list ) |
| 468 | { |
| 469 | printf(" %s\n", ssl_get_ciphersuite_name( *list ) ); |
| 470 | list++; |
| 471 | } |
| 472 | printf("\n"); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 473 | goto exit; |
| 474 | } |
| 475 | |
| 476 | opt.opmode = DFL_OPMODE; |
| 477 | opt.iomode = DFL_IOMODE; |
| 478 | opt.server_name = DFL_SERVER_NAME; |
| 479 | opt.server_port = DFL_SERVER_PORT; |
| 480 | opt.command = DFL_COMMAND; |
| 481 | opt.buffer_size = DFL_BUFFER_SIZE; |
| 482 | opt.max_bytes = DFL_MAX_BYTES; |
| 483 | opt.debug_level = DFL_DEBUG_LEVEL; |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 484 | #if defined(POLARSSL_TIMING_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 485 | opt.conn_timeout = DFL_CONN_TIMEOUT; |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 486 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 487 | opt.max_connections = DFL_MAX_CONNECTIONS; |
| 488 | opt.session_reuse = DFL_SESSION_REUSE; |
| 489 | opt.session_lifetime = DFL_SESSION_LIFETIME; |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 490 | opt.force_ciphersuite[0] = DFL_FORCE_CIPHER; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 491 | |
| 492 | for( i = 1; i < argc; i++ ) |
| 493 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 494 | p = argv[i]; |
| 495 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 496 | continue; |
| 497 | *q++ = '\0'; |
| 498 | |
| 499 | if( strcmp( p, "opmode" ) == 0 ) |
| 500 | { |
| 501 | if( strcmp( q, "client" ) == 0 ) |
| 502 | opt.opmode = OPMODE_CLIENT; |
| 503 | else |
| 504 | if( strcmp( q, "server" ) == 0 ) |
| 505 | opt.opmode = OPMODE_SERVER; |
| 506 | else goto usage; |
| 507 | } |
| 508 | |
| 509 | if( strcmp( p, "iomode" ) == 0 ) |
| 510 | { |
| 511 | if( strcmp( q, "block" ) == 0 ) |
| 512 | opt.iomode = IOMODE_BLOCK; |
| 513 | else |
| 514 | if( strcmp( q, "nonblock" ) == 0 ) |
| 515 | opt.iomode = IOMODE_NONBLOCK; |
| 516 | else goto usage; |
| 517 | } |
| 518 | |
| 519 | if( strcmp( p, "server_name" ) == 0 ) |
| 520 | opt.server_name = q; |
| 521 | |
| 522 | if( strcmp( p, "server_port" ) == 0 ) |
| 523 | { |
| 524 | opt.server_port = atoi( q ); |
| 525 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
| 526 | goto usage; |
| 527 | } |
| 528 | |
| 529 | if( strcmp( p, "command" ) == 0 ) |
| 530 | { |
| 531 | if( strcmp( q, "read" ) == 0 ) |
| 532 | opt.command = COMMAND_READ; |
| 533 | else |
| 534 | if( strcmp( q, "write" ) == 0 ) |
| 535 | opt.command = COMMAND_WRITE; |
| 536 | else |
| 537 | if( strcmp( q, "both" ) == 0 ) |
| 538 | { |
| 539 | opt.iomode = IOMODE_NONBLOCK; |
| 540 | opt.command = COMMAND_BOTH; |
| 541 | } |
| 542 | else goto usage; |
| 543 | } |
| 544 | |
| 545 | if( strcmp( p, "buffer_size" ) == 0 ) |
| 546 | { |
| 547 | opt.buffer_size = atoi( q ); |
| 548 | if( opt.buffer_size < 1 || opt.buffer_size > 1048576 ) |
| 549 | goto usage; |
| 550 | } |
| 551 | |
| 552 | if( strcmp( p, "max_bytes" ) == 0 ) |
| 553 | opt.max_bytes = atoi( q ); |
| 554 | |
| 555 | if( strcmp( p, "debug_level" ) == 0 ) |
| 556 | opt.debug_level = atoi( q ); |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 557 | #if defined(POLARSSL_TIMING_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 558 | if( strcmp( p, "conn_timeout" ) == 0 ) |
| 559 | opt.conn_timeout = atoi( q ); |
Paul Bakker | 44618dd | 2013-07-04 10:34:10 +0200 | [diff] [blame] | 560 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 561 | if( strcmp( p, "max_connections" ) == 0 ) |
| 562 | opt.max_connections = atoi( q ); |
| 563 | |
| 564 | if( strcmp( p, "session_reuse" ) == 0 ) |
| 565 | { |
| 566 | if( strcmp( q, "on" ) == 0 ) |
| 567 | opt.session_reuse = 1; |
| 568 | else |
| 569 | if( strcmp( q, "off" ) == 0 ) |
| 570 | opt.session_reuse = 0; |
| 571 | else |
| 572 | goto usage; |
| 573 | } |
| 574 | |
| 575 | if( strcmp( p, "session_lifetime" ) == 0 ) |
| 576 | opt.session_lifetime = atoi( q ); |
| 577 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 578 | if( strcmp( p, "force_ciphersuite" ) == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 579 | { |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 580 | opt.force_ciphersuite[0] = -1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 581 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 582 | opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 583 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 584 | if( opt.force_ciphersuite[0] <= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 585 | goto usage; |
| 586 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 587 | opt.force_ciphersuite[1] = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | switch( opt.opmode ) |
| 592 | { |
| 593 | case OPMODE_CLIENT: |
| 594 | break; |
| 595 | |
| 596 | case OPMODE_SERVER: |
| 597 | break; |
| 598 | |
| 599 | default: |
| 600 | goto usage; |
| 601 | } |
| 602 | |
| 603 | nb_conn = 0; |
| 604 | |
| 605 | do { |
| 606 | nb_conn++; |
| 607 | ret = ssl_test( &opt ); |
| 608 | if( opt.max_connections != 0 && |
| 609 | opt.max_connections <= nb_conn ) |
| 610 | break; |
| 611 | } |
| 612 | while( ret == 0 ); |
| 613 | |
| 614 | exit: |
| 615 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 616 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 617 | printf( " Press Enter to exit this program.\n" ); |
| 618 | fflush( stdout ); getchar(); |
| 619 | #endif |
| 620 | |
| 621 | return( ret ); |
| 622 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 623 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C && |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 624 | POLARSSL_SSL_SRV_C && POLARSSL_SSL_CLI_C && POLARSSL_NET_C && |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 625 | POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */ |