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