Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL server demonstration program |
| 3 | * |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (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 | |
| 26 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 27 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 28 | #endif |
| 29 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 30 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | #include <windows.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <stdio.h> |
| 37 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 38 | #include "polarssl/config.h" |
| 39 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 40 | #include "polarssl/entropy.h" |
| 41 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 42 | #include "polarssl/certs.h" |
| 43 | #include "polarssl/x509.h" |
| 44 | #include "polarssl/ssl.h" |
| 45 | #include "polarssl/net.h" |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 46 | #include "polarssl/error.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | |
| 48 | #define HTTP_RESPONSE \ |
| 49 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ |
Paul Bakker | eaca51d | 2010-08-16 12:00:14 +0000 | [diff] [blame] | 50 | "<h2>PolarSSL Test Server</h2>\r\n" \ |
| 51 | "<p>Successful connection using: %s</p>\r\n" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * Computing a "safe" DH-1024 prime can take a very |
| 55 | * long time, so a precomputed value is provided below. |
| 56 | * You may run dh_genprime to generate a new value. |
| 57 | */ |
| 58 | char *my_dhm_P = |
| 59 | "E4004C1F94182000103D883A448B3F80" \ |
| 60 | "2CE4B44A83301270002C20D0321CFD00" \ |
| 61 | "11CCEF784C26A400F43DFB901BCA7538" \ |
| 62 | "F2C6B176001CF5A0FD16D2C48B1D0C1C" \ |
| 63 | "F6AC8E1DA6BCC3B4E1F96B0564965300" \ |
| 64 | "FFA1D0B601EB2800F489AA512C4B248C" \ |
| 65 | "01F76949A60BB7F00A40B1EAB64BDD48" \ |
| 66 | "E8A700D60B7F1200FA8E77B0A979DABF"; |
| 67 | |
| 68 | char *my_dhm_G = "4"; |
| 69 | |
| 70 | /* |
| 71 | * Sorted by order of preference |
| 72 | */ |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 73 | int my_ciphersuites[] = |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | { |
| 75 | SSL_EDH_RSA_AES_256_SHA, |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 76 | SSL_EDH_RSA_CAMELLIA_256_SHA, |
Paul Bakker | 77a4358 | 2010-06-15 21:32:46 +0000 | [diff] [blame] | 77 | SSL_EDH_RSA_AES_128_SHA, |
| 78 | SSL_EDH_RSA_CAMELLIA_128_SHA, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | SSL_EDH_RSA_DES_168_SHA, |
| 80 | SSL_RSA_AES_256_SHA, |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 81 | SSL_RSA_CAMELLIA_256_SHA, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | SSL_RSA_AES_128_SHA, |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 83 | SSL_RSA_CAMELLIA_128_SHA, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 84 | SSL_RSA_DES_168_SHA, |
| 85 | SSL_RSA_RC4_128_SHA, |
| 86 | SSL_RSA_RC4_128_MD5, |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame^] | 87 | #if defined(POLARSSL_ENABLE_WEAK_CIPHERSUITES) |
| 88 | SSL_EDH_RSA_DES_SHA, |
| 89 | SSL_RSA_DES_SHA, |
| 90 | #if defined(POLARSSL_CIPHER_NULL_CIPHER) |
| 91 | SSL_RSA_NULL_MD5, |
| 92 | SSL_RSA_NULL_SHA, |
| 93 | SSL_RSA_NULL_SHA256, |
| 94 | #endif |
| 95 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | 0 |
| 97 | }; |
| 98 | |
| 99 | #define DEBUG_LEVEL 0 |
| 100 | |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 101 | void my_debug( void *ctx, int level, const char *str ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | { |
| 103 | if( level < DEBUG_LEVEL ) |
| 104 | { |
| 105 | fprintf( (FILE *) ctx, "%s", str ); |
| 106 | fflush( (FILE *) ctx ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * These session callbacks use a simple chained list |
| 112 | * to store and retrieve the session information. |
| 113 | */ |
| 114 | ssl_session *s_list_1st = NULL; |
| 115 | ssl_session *cur, *prv; |
| 116 | |
| 117 | static int my_get_session( ssl_context *ssl ) |
| 118 | { |
| 119 | time_t t = time( NULL ); |
| 120 | |
| 121 | if( ssl->resume == 0 ) |
| 122 | return( 1 ); |
| 123 | |
| 124 | cur = s_list_1st; |
| 125 | prv = NULL; |
| 126 | |
| 127 | while( cur != NULL ) |
| 128 | { |
| 129 | prv = cur; |
| 130 | cur = cur->next; |
| 131 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 132 | if( ssl->timeout != 0 && (int) ( t - prv->start ) > ssl->timeout ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 | continue; |
| 134 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 135 | if( ssl->session->ciphersuite != prv->ciphersuite || |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 | ssl->session->length != prv->length ) |
| 137 | continue; |
| 138 | |
| 139 | if( memcmp( ssl->session->id, prv->id, prv->length ) != 0 ) |
| 140 | continue; |
| 141 | |
| 142 | memcpy( ssl->session->master, prv->master, 48 ); |
| 143 | return( 0 ); |
| 144 | } |
| 145 | |
| 146 | return( 1 ); |
| 147 | } |
| 148 | |
| 149 | static int my_set_session( ssl_context *ssl ) |
| 150 | { |
| 151 | time_t t = time( NULL ); |
| 152 | |
| 153 | cur = s_list_1st; |
| 154 | prv = NULL; |
| 155 | |
| 156 | while( cur != NULL ) |
| 157 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 158 | if( ssl->timeout != 0 && (int) ( t - cur->start ) > ssl->timeout ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | break; /* expired, reuse this slot */ |
| 160 | |
| 161 | if( memcmp( ssl->session->id, cur->id, cur->length ) == 0 ) |
| 162 | break; /* client reconnected */ |
| 163 | |
| 164 | prv = cur; |
| 165 | cur = cur->next; |
| 166 | } |
| 167 | |
| 168 | if( cur == NULL ) |
| 169 | { |
| 170 | cur = (ssl_session *) malloc( sizeof( ssl_session ) ); |
| 171 | if( cur == NULL ) |
| 172 | return( 1 ); |
| 173 | |
| 174 | if( prv == NULL ) |
| 175 | s_list_1st = cur; |
| 176 | else prv->next = cur; |
| 177 | } |
| 178 | |
| 179 | memcpy( cur, ssl->session, sizeof( ssl_session ) ); |
| 180 | |
| 181 | return( 0 ); |
| 182 | } |
| 183 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 184 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 185 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 186 | !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 187 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 188 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 189 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 190 | ((void) argc); |
| 191 | ((void) argv); |
| 192 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 193 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C " |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 194 | "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or " |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 195 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
| 196 | "POLARSSL_CTR_DRBG_C not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 197 | return( 0 ); |
| 198 | } |
| 199 | #else |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 200 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | { |
| 202 | int ret, len; |
| 203 | int listen_fd; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 204 | int client_fd = -1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 205 | unsigned char buf[1024]; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 206 | char *pers = "ssl_server"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 207 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 208 | entropy_context entropy; |
| 209 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | ssl_context ssl; |
| 211 | ssl_session ssn; |
| 212 | x509_cert srvcert; |
| 213 | rsa_context rsa; |
| 214 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 215 | ((void) argc); |
| 216 | ((void) argv); |
| 217 | |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame^] | 218 | memset( &ssl, 0, sizeof( ssl_context ) ); |
| 219 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 220 | /* |
| 221 | * 1. Load the certificates and private RSA key |
| 222 | */ |
| 223 | printf( "\n . Loading the server cert. and key..." ); |
| 224 | fflush( stdout ); |
| 225 | |
| 226 | memset( &srvcert, 0, sizeof( x509_cert ) ); |
| 227 | |
| 228 | /* |
| 229 | * This demonstration program uses embedded test certificates. |
| 230 | * Instead, you may want to use x509parse_crtfile() to read the |
| 231 | * server and CA certificates, as well as x509parse_keyfile(). |
| 232 | */ |
| 233 | ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 234 | strlen( test_srv_crt ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | if( ret != 0 ) |
| 236 | { |
| 237 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 238 | goto exit; |
| 239 | } |
| 240 | |
| 241 | ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 242 | strlen( test_ca_crt ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 243 | if( ret != 0 ) |
| 244 | { |
| 245 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 246 | goto exit; |
| 247 | } |
| 248 | |
Paul Bakker | 91b4159 | 2011-05-05 12:01:31 +0000 | [diff] [blame] | 249 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 250 | ret = x509parse_key( &rsa, (unsigned char *) test_srv_key, |
| 251 | strlen( test_srv_key ), NULL, 0 ); |
| 252 | if( ret != 0 ) |
| 253 | { |
| 254 | printf( " failed\n ! x509parse_key returned %d\n\n", ret ); |
| 255 | goto exit; |
| 256 | } |
| 257 | |
| 258 | printf( " ok\n" ); |
| 259 | |
| 260 | /* |
| 261 | * 2. Setup the listening TCP socket |
| 262 | */ |
| 263 | printf( " . Bind on https://localhost:4433/ ..." ); |
| 264 | fflush( stdout ); |
| 265 | |
| 266 | if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 ) |
| 267 | { |
| 268 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 269 | goto exit; |
| 270 | } |
| 271 | |
| 272 | printf( " ok\n" ); |
| 273 | |
| 274 | /* |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 275 | * 3. Seed the RNG |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 276 | */ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 277 | printf( " . Seeding the random number generator..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 278 | fflush( stdout ); |
| 279 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 280 | entropy_init( &entropy ); |
| 281 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 282 | (unsigned char *) pers, strlen( pers ) ) ) != 0 ) |
| 283 | { |
| 284 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 285 | goto exit; |
| 286 | } |
| 287 | |
| 288 | printf( " ok\n" ); |
| 289 | |
| 290 | /* |
| 291 | * 4. Setup stuff |
| 292 | */ |
| 293 | printf( " . Setting up the SSL data...." ); |
| 294 | fflush( stdout ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 295 | |
| 296 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 297 | { |
| 298 | printf( " failed\n ! ssl_init returned %d\n\n", ret ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 299 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 302 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); |
| 303 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); |
| 304 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 305 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 306 | ssl_set_dbg( &ssl, my_debug, stdout ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 307 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 308 | ssl_set_scb( &ssl, my_get_session, |
| 309 | my_set_session ); |
| 310 | |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 311 | ssl_set_ciphersuites( &ssl, my_ciphersuites ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 312 | ssl_set_session( &ssl, 1, 0, &ssn ); |
| 313 | |
| 314 | memset( &ssn, 0, sizeof( ssl_session ) ); |
| 315 | |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 316 | ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 317 | ssl_set_own_cert( &ssl, &srvcert, &rsa ); |
| 318 | ssl_set_dh_param( &ssl, my_dhm_P, my_dhm_G ); |
| 319 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 320 | printf( " ok\n" ); |
| 321 | |
| 322 | reset: |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 323 | #ifdef POLARSSL_ERROR_C |
| 324 | if( ret != 0 ) |
| 325 | { |
| 326 | char error_buf[100]; |
| 327 | error_strerror( ret, error_buf, 100 ); |
| 328 | printf("Last error was: %d - %s\n\n", ret, error_buf ); |
| 329 | } |
| 330 | #endif |
| 331 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 332 | if( client_fd != -1 ) |
| 333 | net_close( client_fd ); |
| 334 | |
| 335 | ssl_session_reset( &ssl ); |
| 336 | |
| 337 | /* |
| 338 | * 3. Wait until a client connects |
| 339 | */ |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 340 | #if defined(_WIN32_WCE) |
| 341 | { |
| 342 | SHELLEXECUTEINFO sei; |
| 343 | |
| 344 | ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) ); |
| 345 | |
| 346 | sei.cbSize = sizeof( SHELLEXECUTEINFO ); |
| 347 | sei.fMask = 0; |
| 348 | sei.hwnd = 0; |
| 349 | sei.lpVerb = _T( "open" ); |
| 350 | sei.lpFile = _T( "https://localhost:4433/" ); |
| 351 | sei.lpParameters = NULL; |
| 352 | sei.lpDirectory = NULL; |
| 353 | sei.nShow = SW_SHOWNORMAL; |
| 354 | |
| 355 | ShellExecuteEx( &sei ); |
| 356 | } |
| 357 | #elif defined(_WIN32) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 358 | ShellExecute( NULL, "open", "https://localhost:4433/", |
| 359 | NULL, NULL, SW_SHOWNORMAL ); |
| 360 | #endif |
| 361 | |
| 362 | client_fd = -1; |
| 363 | |
| 364 | printf( " . Waiting for a remote connection ..." ); |
| 365 | fflush( stdout ); |
| 366 | |
| 367 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 368 | { |
| 369 | printf( " failed\n ! net_accept returned %d\n\n", ret ); |
| 370 | goto exit; |
| 371 | } |
| 372 | |
| 373 | ssl_set_bio( &ssl, net_recv, &client_fd, |
| 374 | net_send, &client_fd ); |
| 375 | |
| 376 | printf( " ok\n" ); |
| 377 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 378 | /* |
| 379 | * 5. Handshake |
| 380 | */ |
| 381 | printf( " . Performing the SSL/TLS handshake..." ); |
| 382 | fflush( stdout ); |
| 383 | |
| 384 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 385 | { |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 386 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 387 | { |
| 388 | printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 389 | goto reset; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | printf( " ok\n" ); |
| 394 | |
| 395 | /* |
| 396 | * 6. Read the HTTP Request |
| 397 | */ |
| 398 | printf( " < Read from client:" ); |
| 399 | fflush( stdout ); |
| 400 | |
| 401 | do |
| 402 | { |
| 403 | len = sizeof( buf ) - 1; |
| 404 | memset( buf, 0, sizeof( buf ) ); |
| 405 | ret = ssl_read( &ssl, buf, len ); |
| 406 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 407 | if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 408 | continue; |
| 409 | |
| 410 | if( ret <= 0 ) |
| 411 | { |
| 412 | switch( ret ) |
| 413 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 414 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 415 | printf( " connection was closed gracefully\n" ); |
| 416 | break; |
| 417 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 418 | case POLARSSL_ERR_NET_CONN_RESET: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 419 | printf( " connection was reset by peer\n" ); |
| 420 | break; |
| 421 | |
| 422 | default: |
| 423 | printf( " ssl_read returned %d\n", ret ); |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | len = ret; |
| 431 | printf( " %d bytes read\n\n%s", len, (char *) buf ); |
| 432 | } |
| 433 | while( 0 ); |
| 434 | |
| 435 | /* |
| 436 | * 7. Write the 200 Response |
| 437 | */ |
| 438 | printf( " > Write to client:" ); |
| 439 | fflush( stdout ); |
| 440 | |
| 441 | len = sprintf( (char *) buf, HTTP_RESPONSE, |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 442 | ssl_get_ciphersuite( &ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 443 | |
| 444 | while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) |
| 445 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 446 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 447 | { |
| 448 | printf( " failed\n ! peer closed the connection\n\n" ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 449 | goto reset; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 452 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 453 | { |
| 454 | printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
| 455 | goto exit; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | len = ret; |
| 460 | printf( " %d bytes written\n\n%s\n", len, (char *) buf ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 461 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 462 | ret = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 463 | goto reset; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 464 | |
| 465 | exit: |
| 466 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 467 | #ifdef POLARSSL_ERROR_C |
| 468 | if( ret != 0 ) |
| 469 | { |
| 470 | char error_buf[100]; |
| 471 | error_strerror( ret, error_buf, 100 ); |
| 472 | printf("Last error was: %d - %s\n\n", ret, error_buf ); |
| 473 | } |
| 474 | #endif |
| 475 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 476 | net_close( client_fd ); |
| 477 | x509_free( &srvcert ); |
| 478 | rsa_free( &rsa ); |
| 479 | ssl_free( &ssl ); |
| 480 | |
| 481 | cur = s_list_1st; |
| 482 | while( cur != NULL ) |
| 483 | { |
| 484 | prv = cur; |
| 485 | cur = cur->next; |
| 486 | memset( prv, 0, sizeof( ssl_session ) ); |
| 487 | free( prv ); |
| 488 | } |
| 489 | |
| 490 | memset( &ssl, 0, sizeof( ssl_context ) ); |
| 491 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 492 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 493 | printf( " Press Enter to exit this program.\n" ); |
| 494 | fflush( stdout ); getchar(); |
| 495 | #endif |
| 496 | |
| 497 | return( ret ); |
| 498 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 499 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C && |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 500 | POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C && |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 501 | POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */ |