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