Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL client with options |
| 3 | * |
| 4 | * Copyright (C) 2006-2012, Brainspark B.V. |
| 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 | #if defined(_WIN32) |
| 31 | #include <windows.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <stdio.h> |
| 37 | |
| 38 | #include "polarssl/config.h" |
| 39 | |
| 40 | #include "polarssl/net.h" |
| 41 | #include "polarssl/ssl.h" |
| 42 | #include "polarssl/entropy.h" |
| 43 | #include "polarssl/ctr_drbg.h" |
| 44 | #include "polarssl/certs.h" |
| 45 | #include "polarssl/x509.h" |
| 46 | #include "polarssl/error.h" |
| 47 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 48 | #if defined(POLARSSL_SSL_CACHE_C) |
| 49 | #include "polarssl/ssl_cache.h" |
| 50 | #endif |
| 51 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 52 | #define DFL_SERVER_PORT 4433 |
| 53 | #define DFL_REQUEST_PAGE "/" |
| 54 | #define DFL_DEBUG_LEVEL 0 |
| 55 | #define DFL_CA_FILE "" |
| 56 | #define DFL_CA_PATH "" |
| 57 | #define DFL_CRT_FILE "" |
| 58 | #define DFL_KEY_FILE "" |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame^] | 59 | #define DFL_PSK "" |
| 60 | #define DFL_PSK_IDENTITY "Client_identity" |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 61 | #define DFL_FORCE_CIPHER 0 |
| 62 | #define DFL_RENEGOTIATION SSL_RENEGOTIATION_ENABLED |
| 63 | #define DFL_ALLOW_LEGACY SSL_LEGACY_NO_RENEGOTIATION |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 64 | #define DFL_MIN_VERSION -1 |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 65 | #define DFL_AUTH_MODE SSL_VERIFY_OPTIONAL |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 66 | |
| 67 | #define HTTP_RESPONSE \ |
| 68 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ |
| 69 | "<h2>PolarSSL Test Server</h2>\r\n" \ |
| 70 | "<p>Successful connection using: %s</p>\r\n" |
| 71 | |
| 72 | /* |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 73 | * global options |
| 74 | */ |
| 75 | struct options |
| 76 | { |
| 77 | int server_port; /* port on which the ssl service runs */ |
| 78 | int debug_level; /* level of debugging */ |
| 79 | char *ca_file; /* the file with the CA certificate(s) */ |
| 80 | char *ca_path; /* the path with the CA certificate(s) reside */ |
| 81 | char *crt_file; /* the file with the client certificate */ |
| 82 | char *key_file; /* the file with the client key */ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame^] | 83 | char *psk; /* the pre-shared key */ |
| 84 | char *psk_identity; /* the pre-shared key identity */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 85 | int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */ |
| 86 | int renegotiation; /* enable / disable renegotiation */ |
| 87 | int allow_legacy; /* allow legacy renegotiation */ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 88 | int min_version; /* minimum protocol version accepted */ |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 89 | int auth_mode; /* verify mode for connection */ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 90 | } opt; |
| 91 | |
| 92 | void my_debug( void *ctx, int level, const char *str ) |
| 93 | { |
| 94 | if( level < opt.debug_level ) |
| 95 | { |
| 96 | fprintf( (FILE *) ctx, "%s", str ); |
| 97 | fflush( (FILE *) ctx ); |
| 98 | } |
| 99 | } |
| 100 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 101 | #if defined(POLARSSL_FS_IO) |
| 102 | #define USAGE_IO \ |
Paul Bakker | 1f9d02d | 2012-11-20 10:30:55 +0100 | [diff] [blame] | 103 | " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \ |
| 104 | " default: \"\" (pre-loaded)\n" \ |
| 105 | " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \ |
| 106 | " default: \"\" (pre-loaded) (overrides ca_file)\n" \ |
| 107 | " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \ |
| 108 | " default: \"\" (pre-loaded)\n" \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 109 | " key_file=%%s default: \"\" (pre-loaded)\n" |
| 110 | #else |
| 111 | #define USAGE_IO \ |
| 112 | " No file operations available (POLARSSL_FS_IO not defined)\n" |
| 113 | #endif /* POLARSSL_FS_IO */ |
| 114 | |
| 115 | #define USAGE \ |
| 116 | "\n usage: ssl_server2 param=<>...\n" \ |
| 117 | "\n acceptable parameters:\n" \ |
| 118 | " server_port=%%d default: 4433\n" \ |
| 119 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 120 | USAGE_IO \ |
| 121 | " request_page=%%s default: \".\"\n" \ |
| 122 | " renegotiation=%%d default: 1 (enabled)\n" \ |
| 123 | " allow_legacy=%%d default: 0 (disabled)\n" \ |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 124 | " min_version=%%s default: \"ssl3\"\n" \ |
| 125 | " options: ssl3, tls1, tls1_1, tls1_2\n" \ |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 126 | " auth_mode=%%s default: \"optional\"\n" \ |
| 127 | " options: none, optional, required\n" \ |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame^] | 128 | " psk=%%s default: \"\" (in hex, without 0x)\n" \ |
| 129 | " psk_identity=%%s default: \"Client_identity\"\n" \ |
| 130 | "\n" \ |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 131 | " force_ciphersuite=<name> default: all enabled\n"\ |
| 132 | " acceptable ciphersuite names:\n" |
| 133 | |
| 134 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
| 135 | !defined(POLARSSL_SSL_TLS_C) || !defined(POLARSSL_SSL_SRV_C) || \ |
| 136 | !defined(POLARSSL_NET_C) || !defined(POLARSSL_RSA_C) || \ |
| 137 | !defined(POLARSSL_CTR_DRBG_C) |
| 138 | int main( int argc, char *argv[] ) |
| 139 | { |
| 140 | ((void) argc); |
| 141 | ((void) argv); |
| 142 | |
| 143 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
| 144 | "POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or " |
| 145 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
| 146 | "POLARSSL_CTR_DRBG_C not defined.\n"); |
| 147 | return( 0 ); |
| 148 | } |
| 149 | #else |
| 150 | int main( int argc, char *argv[] ) |
| 151 | { |
| 152 | int ret = 0, len; |
| 153 | int listen_fd; |
| 154 | int client_fd = -1; |
| 155 | unsigned char buf[1024]; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame^] | 156 | unsigned char psk[256]; |
| 157 | size_t psk_len = 0; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 158 | char *pers = "ssl_server2"; |
| 159 | |
| 160 | entropy_context entropy; |
| 161 | ctr_drbg_context ctr_drbg; |
| 162 | ssl_context ssl; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 163 | x509_cert cacert; |
| 164 | x509_cert srvcert; |
| 165 | rsa_context rsa; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 166 | #if defined(POLARSSL_SSL_CACHE_C) |
| 167 | ssl_cache_context cache; |
| 168 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 169 | |
| 170 | int i; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 171 | char *p, *q; |
| 172 | const int *list; |
| 173 | |
| 174 | /* |
| 175 | * Make sure memory references are valid. |
| 176 | */ |
| 177 | listen_fd = 0; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 178 | memset( &cacert, 0, sizeof( x509_cert ) ); |
| 179 | memset( &srvcert, 0, sizeof( x509_cert ) ); |
| 180 | memset( &rsa, 0, sizeof( rsa_context ) ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 181 | #if defined(POLARSSL_SSL_CACHE_C) |
| 182 | ssl_cache_init( &cache ); |
| 183 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 184 | |
| 185 | if( argc == 0 ) |
| 186 | { |
| 187 | usage: |
| 188 | if( ret == 0 ) |
| 189 | ret = 1; |
| 190 | |
| 191 | printf( USAGE ); |
| 192 | |
| 193 | list = ssl_list_ciphersuites(); |
| 194 | while( *list ) |
| 195 | { |
| 196 | printf(" %s\n", ssl_get_ciphersuite_name( *list ) ); |
| 197 | list++; |
| 198 | } |
| 199 | printf("\n"); |
| 200 | goto exit; |
| 201 | } |
| 202 | |
| 203 | opt.server_port = DFL_SERVER_PORT; |
| 204 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 205 | opt.ca_file = DFL_CA_FILE; |
| 206 | opt.ca_path = DFL_CA_PATH; |
| 207 | opt.crt_file = DFL_CRT_FILE; |
| 208 | opt.key_file = DFL_KEY_FILE; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame^] | 209 | opt.psk = DFL_PSK; |
| 210 | opt.psk_identity = DFL_PSK_IDENTITY; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 211 | opt.force_ciphersuite[0]= DFL_FORCE_CIPHER; |
| 212 | opt.renegotiation = DFL_RENEGOTIATION; |
| 213 | opt.allow_legacy = DFL_ALLOW_LEGACY; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 214 | opt.min_version = DFL_MIN_VERSION; |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 215 | opt.auth_mode = DFL_AUTH_MODE; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 216 | |
| 217 | for( i = 1; i < argc; i++ ) |
| 218 | { |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 219 | p = argv[i]; |
| 220 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 221 | goto usage; |
| 222 | *q++ = '\0'; |
| 223 | |
| 224 | if( strcmp( p, "server_port" ) == 0 ) |
| 225 | { |
| 226 | opt.server_port = atoi( q ); |
| 227 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
| 228 | goto usage; |
| 229 | } |
| 230 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 231 | { |
| 232 | opt.debug_level = atoi( q ); |
| 233 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 234 | goto usage; |
| 235 | } |
| 236 | else if( strcmp( p, "ca_file" ) == 0 ) |
| 237 | opt.ca_file = q; |
| 238 | else if( strcmp( p, "ca_path" ) == 0 ) |
| 239 | opt.ca_path = q; |
| 240 | else if( strcmp( p, "crt_file" ) == 0 ) |
| 241 | opt.crt_file = q; |
| 242 | else if( strcmp( p, "key_file" ) == 0 ) |
| 243 | opt.key_file = q; |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame^] | 244 | else if( strcmp( p, "psk" ) == 0 ) |
| 245 | opt.psk = q; |
| 246 | else if( strcmp( p, "psk_identity" ) == 0 ) |
| 247 | opt.psk_identity = q; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 248 | else if( strcmp( p, "force_ciphersuite" ) == 0 ) |
| 249 | { |
| 250 | opt.force_ciphersuite[0] = -1; |
| 251 | |
| 252 | opt.force_ciphersuite[0] = ssl_get_ciphersuite_id( q ); |
| 253 | |
| 254 | if( opt.force_ciphersuite[0] <= 0 ) |
| 255 | { |
| 256 | ret = 2; |
| 257 | goto usage; |
| 258 | } |
| 259 | opt.force_ciphersuite[1] = 0; |
| 260 | } |
| 261 | else if( strcmp( p, "renegotiation" ) == 0 ) |
| 262 | { |
| 263 | opt.renegotiation = (atoi( q )) ? SSL_RENEGOTIATION_ENABLED : |
| 264 | SSL_RENEGOTIATION_DISABLED; |
| 265 | } |
| 266 | else if( strcmp( p, "allow_legacy" ) == 0 ) |
| 267 | { |
| 268 | opt.allow_legacy = atoi( q ); |
| 269 | if( opt.allow_legacy < 0 || opt.allow_legacy > 1 ) |
| 270 | goto usage; |
| 271 | } |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 272 | else if( strcmp( p, "min_version" ) == 0 ) |
| 273 | { |
| 274 | if( strcmp( q, "ssl3" ) == 0 ) |
| 275 | opt.min_version = SSL_MINOR_VERSION_0; |
| 276 | else if( strcmp( q, "tls1" ) == 0 ) |
| 277 | opt.min_version = SSL_MINOR_VERSION_1; |
| 278 | else if( strcmp( q, "tls1_1" ) == 0 ) |
| 279 | opt.min_version = SSL_MINOR_VERSION_2; |
| 280 | else if( strcmp( q, "tls1_2" ) == 0 ) |
| 281 | opt.min_version = SSL_MINOR_VERSION_3; |
| 282 | else |
| 283 | goto usage; |
| 284 | } |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 285 | else if( strcmp( p, "auth_mode" ) == 0 ) |
| 286 | { |
| 287 | if( strcmp( q, "none" ) == 0 ) |
| 288 | opt.auth_mode = SSL_VERIFY_NONE; |
| 289 | else if( strcmp( q, "optional" ) == 0 ) |
| 290 | opt.auth_mode = SSL_VERIFY_OPTIONAL; |
| 291 | else if( strcmp( q, "required" ) == 0 ) |
| 292 | opt.auth_mode = SSL_VERIFY_REQUIRED; |
| 293 | else |
| 294 | goto usage; |
| 295 | } |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 296 | else |
| 297 | goto usage; |
| 298 | } |
| 299 | |
| 300 | /* |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame^] | 301 | * Unhexify the pre-shared key if any is given |
| 302 | */ |
| 303 | if( strlen( opt.psk ) ) |
| 304 | { |
| 305 | unsigned char c; |
| 306 | size_t j; |
| 307 | |
| 308 | if( strlen( opt.psk ) % 2 != 0 ) |
| 309 | { |
| 310 | printf("pre-shared key not valid hex\n"); |
| 311 | goto exit; |
| 312 | } |
| 313 | |
| 314 | psk_len = strlen( opt.psk ) / 2; |
| 315 | |
| 316 | for( j = 0; j < strlen( opt.psk ); j += 2 ) |
| 317 | { |
| 318 | c = opt.psk[j]; |
| 319 | if( c >= '0' && c <= '9' ) |
| 320 | c -= '0'; |
| 321 | else if( c >= 'a' && c <= 'f' ) |
| 322 | c -= 'a' - 10; |
| 323 | else if( c >= 'A' && c <= 'F' ) |
| 324 | c -= 'A' - 10; |
| 325 | else |
| 326 | { |
| 327 | printf("pre-shared key not valid hex\n"); |
| 328 | goto exit; |
| 329 | } |
| 330 | psk[ j / 2 ] = c << 4; |
| 331 | |
| 332 | c = opt.psk[j + 1]; |
| 333 | if( c >= '0' && c <= '9' ) |
| 334 | c -= '0'; |
| 335 | else if( c >= 'a' && c <= 'f' ) |
| 336 | c -= 'a' - 10; |
| 337 | else if( c >= 'A' && c <= 'F' ) |
| 338 | c -= 'A' - 10; |
| 339 | else |
| 340 | { |
| 341 | printf("pre-shared key not valid hex\n"); |
| 342 | goto exit; |
| 343 | } |
| 344 | psk[ j / 2 ] |= c; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /* |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 349 | * 0. Initialize the RNG and the session data |
| 350 | */ |
| 351 | printf( "\n . Seeding the random number generator..." ); |
| 352 | fflush( stdout ); |
| 353 | |
| 354 | entropy_init( &entropy ); |
| 355 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 356 | (unsigned char *) pers, strlen( pers ) ) ) != 0 ) |
| 357 | { |
| 358 | printf( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret ); |
| 359 | goto exit; |
| 360 | } |
| 361 | |
| 362 | printf( " ok\n" ); |
| 363 | |
| 364 | /* |
| 365 | * 1.1. Load the trusted CA |
| 366 | */ |
| 367 | printf( " . Loading the CA root certificate ..." ); |
| 368 | fflush( stdout ); |
| 369 | |
| 370 | #if defined(POLARSSL_FS_IO) |
| 371 | if( strlen( opt.ca_path ) ) |
| 372 | ret = x509parse_crtpath( &cacert, opt.ca_path ); |
| 373 | else if( strlen( opt.ca_file ) ) |
| 374 | ret = x509parse_crtfile( &cacert, opt.ca_file ); |
| 375 | else |
| 376 | #endif |
| 377 | #if defined(POLARSSL_CERTS_C) |
| 378 | ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt, |
| 379 | strlen( test_ca_crt ) ); |
| 380 | #else |
| 381 | { |
| 382 | ret = 1; |
| 383 | printf("POLARSSL_CERTS_C not defined."); |
| 384 | } |
| 385 | #endif |
| 386 | if( ret < 0 ) |
| 387 | { |
| 388 | printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret ); |
| 389 | goto exit; |
| 390 | } |
| 391 | |
| 392 | printf( " ok (%d skipped)\n", ret ); |
| 393 | |
| 394 | /* |
| 395 | * 1.2. Load own certificate and private key |
| 396 | */ |
| 397 | printf( " . Loading the server cert. and key..." ); |
| 398 | fflush( stdout ); |
| 399 | |
| 400 | #if defined(POLARSSL_FS_IO) |
| 401 | if( strlen( opt.crt_file ) ) |
| 402 | ret = x509parse_crtfile( &srvcert, opt.crt_file ); |
| 403 | else |
| 404 | #endif |
| 405 | #if defined(POLARSSL_CERTS_C) |
| 406 | ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt, |
| 407 | strlen( test_srv_crt ) ); |
| 408 | #else |
| 409 | { |
| 410 | ret = 1; |
| 411 | printf("POLARSSL_CERTS_C not defined."); |
| 412 | } |
| 413 | #endif |
| 414 | if( ret != 0 ) |
| 415 | { |
| 416 | printf( " failed\n ! x509parse_crt returned -0x%x\n\n", -ret ); |
| 417 | goto exit; |
| 418 | } |
| 419 | |
| 420 | #if defined(POLARSSL_FS_IO) |
| 421 | if( strlen( opt.key_file ) ) |
| 422 | ret = x509parse_keyfile( &rsa, opt.key_file, "" ); |
| 423 | else |
| 424 | #endif |
| 425 | #if defined(POLARSSL_CERTS_C) |
| 426 | ret = x509parse_key( &rsa, (unsigned char *) test_srv_key, |
| 427 | strlen( test_srv_key ), NULL, 0 ); |
| 428 | #else |
| 429 | { |
| 430 | ret = 1; |
| 431 | printf("POLARSSL_CERTS_C not defined."); |
| 432 | } |
| 433 | #endif |
| 434 | if( ret != 0 ) |
| 435 | { |
| 436 | printf( " failed\n ! x509parse_key returned -0x%x\n\n", -ret ); |
| 437 | goto exit; |
| 438 | } |
| 439 | |
| 440 | printf( " ok\n" ); |
| 441 | |
| 442 | /* |
| 443 | * 2. Setup the listening TCP socket |
| 444 | */ |
| 445 | printf( " . Bind on tcp://localhost:%-4d/ ...", opt.server_port ); |
| 446 | fflush( stdout ); |
| 447 | |
| 448 | if( ( ret = net_bind( &listen_fd, NULL, opt.server_port ) ) != 0 ) |
| 449 | { |
| 450 | printf( " failed\n ! net_bind returned -0x%x\n\n", -ret ); |
| 451 | goto exit; |
| 452 | } |
| 453 | |
| 454 | printf( " ok\n" ); |
| 455 | |
| 456 | /* |
| 457 | * 3. Setup stuff |
| 458 | */ |
| 459 | printf( " . Setting up the SSL/TLS structure..." ); |
| 460 | fflush( stdout ); |
| 461 | |
| 462 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 463 | { |
| 464 | printf( " failed\n ! ssl_init returned -0x%x\n\n", -ret ); |
| 465 | goto exit; |
| 466 | } |
| 467 | |
| 468 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); |
Paul Bakker | 91ebfb5 | 2012-11-23 14:04:08 +0100 | [diff] [blame] | 469 | ssl_set_authmode( &ssl, opt.auth_mode ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 470 | |
| 471 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
| 472 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 473 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 474 | #if defined(POLARSSL_SSL_CACHE_C) |
| 475 | ssl_set_session_cache( &ssl, ssl_cache_get, &cache, |
| 476 | ssl_cache_set, &cache ); |
| 477 | #endif |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 478 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 479 | if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 480 | ssl_set_ciphersuites( &ssl, opt.force_ciphersuite ); |
| 481 | |
| 482 | ssl_set_renegotiation( &ssl, opt.renegotiation ); |
| 483 | ssl_legacy_renegotiation( &ssl, opt.allow_legacy ); |
| 484 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 485 | ssl_set_ca_chain( &ssl, &cacert, NULL, NULL ); |
| 486 | ssl_set_own_cert( &ssl, &srvcert, &rsa ); |
Paul Bakker | fbb1780 | 2013-04-17 19:10:21 +0200 | [diff] [blame^] | 487 | ssl_set_psk( &ssl, psk, psk_len, (unsigned char *) opt.psk_identity, |
| 488 | strlen( opt.psk_identity ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 489 | |
| 490 | #if defined(POLARSSL_DHM_C) |
Paul Bakker | 5d19f86 | 2012-09-28 07:33:00 +0000 | [diff] [blame] | 491 | /* |
| 492 | * Use different group than default DHM group |
| 493 | */ |
Paul Bakker | d432410 | 2012-09-26 08:29:38 +0000 | [diff] [blame] | 494 | ssl_set_dh_param( &ssl, POLARSSL_DHM_RFC5114_MODP_2048_P, |
| 495 | POLARSSL_DHM_RFC5114_MODP_2048_G ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 496 | #endif |
| 497 | |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 498 | if( opt.min_version != -1 ) |
| 499 | ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version ); |
| 500 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 501 | printf( " ok\n" ); |
| 502 | |
| 503 | reset: |
| 504 | #ifdef POLARSSL_ERROR_C |
| 505 | if( ret != 0 ) |
| 506 | { |
| 507 | char error_buf[100]; |
| 508 | error_strerror( ret, error_buf, 100 ); |
| 509 | printf("Last error was: %d - %s\n\n", ret, error_buf ); |
| 510 | } |
| 511 | #endif |
| 512 | |
| 513 | if( client_fd != -1 ) |
| 514 | net_close( client_fd ); |
| 515 | |
| 516 | ssl_session_reset( &ssl ); |
| 517 | |
| 518 | /* |
| 519 | * 3. Wait until a client connects |
| 520 | */ |
| 521 | #if defined(_WIN32_WCE) |
| 522 | { |
| 523 | SHELLEXECUTEINFO sei; |
| 524 | |
| 525 | ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) ); |
| 526 | |
| 527 | sei.cbSize = sizeof( SHELLEXECUTEINFO ); |
| 528 | sei.fMask = 0; |
| 529 | sei.hwnd = 0; |
| 530 | sei.lpVerb = _T( "open" ); |
| 531 | sei.lpFile = _T( "https://localhost:4433/" ); |
| 532 | sei.lpParameters = NULL; |
| 533 | sei.lpDirectory = NULL; |
| 534 | sei.nShow = SW_SHOWNORMAL; |
| 535 | |
| 536 | ShellExecuteEx( &sei ); |
| 537 | } |
| 538 | #elif defined(_WIN32) |
| 539 | ShellExecute( NULL, "open", "https://localhost:4433/", |
| 540 | NULL, NULL, SW_SHOWNORMAL ); |
| 541 | #endif |
| 542 | |
| 543 | client_fd = -1; |
| 544 | |
| 545 | printf( " . Waiting for a remote connection ..." ); |
| 546 | fflush( stdout ); |
| 547 | |
| 548 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 549 | { |
| 550 | printf( " failed\n ! net_accept returned -0x%x\n\n", -ret ); |
| 551 | goto exit; |
| 552 | } |
| 553 | |
| 554 | ssl_set_bio( &ssl, net_recv, &client_fd, |
| 555 | net_send, &client_fd ); |
| 556 | |
| 557 | printf( " ok\n" ); |
| 558 | |
| 559 | /* |
| 560 | * 4. Handshake |
| 561 | */ |
| 562 | printf( " . Performing the SSL/TLS handshake..." ); |
| 563 | fflush( stdout ); |
| 564 | |
| 565 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 566 | { |
| 567 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 568 | { |
| 569 | printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret ); |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 570 | goto reset; |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | printf( " ok\n [ Ciphersuite is %s ]\n", |
| 575 | ssl_get_ciphersuite( &ssl ) ); |
| 576 | |
| 577 | /* |
| 578 | * 5. Verify the server certificate |
| 579 | */ |
| 580 | printf( " . Verifying peer X.509 certificate..." ); |
| 581 | |
| 582 | if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 ) |
| 583 | { |
| 584 | printf( " failed\n" ); |
| 585 | |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 586 | if( !ssl_get_peer_cert( &ssl ) ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 587 | printf( " ! no client certificate sent\n" ); |
| 588 | |
| 589 | if( ( ret & BADCERT_EXPIRED ) != 0 ) |
| 590 | printf( " ! client certificate has expired\n" ); |
| 591 | |
| 592 | if( ( ret & BADCERT_REVOKED ) != 0 ) |
| 593 | printf( " ! client certificate has been revoked\n" ); |
| 594 | |
| 595 | if( ( ret & BADCERT_NOT_TRUSTED ) != 0 ) |
| 596 | printf( " ! self-signed or not signed by a trusted CA\n" ); |
| 597 | |
| 598 | printf( "\n" ); |
| 599 | } |
| 600 | else |
| 601 | printf( " ok\n" ); |
| 602 | |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 603 | if( ssl_get_peer_cert( &ssl ) ) |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 604 | { |
| 605 | printf( " . Peer certificate information ...\n" ); |
| 606 | x509parse_cert_info( (char *) buf, sizeof( buf ) - 1, " ", |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 607 | ssl_get_peer_cert( &ssl ) ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 608 | printf( "%s\n", buf ); |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * 6. Read the HTTP Request |
| 613 | */ |
| 614 | printf( " < Read from client:" ); |
| 615 | fflush( stdout ); |
| 616 | |
| 617 | do |
| 618 | { |
| 619 | len = sizeof( buf ) - 1; |
| 620 | memset( buf, 0, sizeof( buf ) ); |
| 621 | ret = ssl_read( &ssl, buf, len ); |
| 622 | |
| 623 | if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) |
| 624 | continue; |
| 625 | |
| 626 | if( ret <= 0 ) |
| 627 | { |
| 628 | switch( ret ) |
| 629 | { |
| 630 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: |
| 631 | printf( " connection was closed gracefully\n" ); |
| 632 | break; |
| 633 | |
| 634 | case POLARSSL_ERR_NET_CONN_RESET: |
| 635 | printf( " connection was reset by peer\n" ); |
| 636 | break; |
| 637 | |
| 638 | default: |
| 639 | printf( " ssl_read returned -0x%x\n", -ret ); |
| 640 | break; |
| 641 | } |
| 642 | |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | len = ret; |
| 647 | printf( " %d bytes read\n\n%s", len, (char *) buf ); |
| 648 | |
| 649 | if( ret > 0 ) |
| 650 | break; |
| 651 | } |
| 652 | while( 1 ); |
| 653 | |
| 654 | /* |
| 655 | * 7. Write the 200 Response |
| 656 | */ |
| 657 | printf( " > Write to client:" ); |
| 658 | fflush( stdout ); |
| 659 | |
| 660 | len = sprintf( (char *) buf, HTTP_RESPONSE, |
| 661 | ssl_get_ciphersuite( &ssl ) ); |
| 662 | |
| 663 | while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) |
| 664 | { |
| 665 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) |
| 666 | { |
| 667 | printf( " failed\n ! peer closed the connection\n\n" ); |
| 668 | goto reset; |
| 669 | } |
| 670 | |
| 671 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 672 | { |
| 673 | printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
| 674 | goto exit; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | len = ret; |
| 679 | printf( " %d bytes written\n\n%s\n", len, (char *) buf ); |
| 680 | |
| 681 | ret = 0; |
| 682 | goto reset; |
| 683 | |
| 684 | exit: |
| 685 | |
| 686 | #ifdef POLARSSL_ERROR_C |
| 687 | if( ret != 0 ) |
| 688 | { |
| 689 | char error_buf[100]; |
| 690 | error_strerror( ret, error_buf, 100 ); |
| 691 | printf("Last error was: -0x%X - %s\n\n", -ret, error_buf ); |
| 692 | } |
| 693 | #endif |
| 694 | |
| 695 | net_close( client_fd ); |
| 696 | x509_free( &srvcert ); |
| 697 | x509_free( &cacert ); |
| 698 | rsa_free( &rsa ); |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 699 | ssl_free( &ssl ); |
| 700 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 701 | #if defined(POLARSSL_SSL_CACHE_C) |
| 702 | ssl_cache_free( &cache ); |
| 703 | #endif |
| 704 | |
Paul Bakker | b60b95f | 2012-09-25 09:05:17 +0000 | [diff] [blame] | 705 | #if defined(_WIN32) |
| 706 | printf( " + Press Enter to exit this program.\n" ); |
| 707 | fflush( stdout ); getchar(); |
| 708 | #endif |
| 709 | |
| 710 | return( ret ); |
| 711 | } |
| 712 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_SSL_TLS_C && |
| 713 | POLARSSL_SSL_SRV_C && POLARSSL_NET_C && POLARSSL_RSA_C && |
| 714 | POLARSSL_CTR_DRBG_C */ |