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 | * |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 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 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 30 | #if defined(_WIN32) |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 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 | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 42 | #include "polarssl/entropy.h" |
| 43 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 44 | #include "polarssl/certs.h" |
| 45 | #include "polarssl/x509.h" |
| 46 | #include "polarssl/ssl.h" |
| 47 | #include "polarssl/net.h" |
| 48 | #include "polarssl/timing.h" |
| 49 | |
| 50 | #define HTTP_RESPONSE \ |
| 51 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ |
| 52 | "<h2>PolarSSL Test Server</h2>\r\n" \ |
| 53 | "<p>Successful connection using: %s</p>\r\n" |
| 54 | |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 55 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 56 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \ |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 57 | !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 58 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \ |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 59 | !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_TIMING_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 60 | int main( int argc, char *argv[] ) |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 61 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 62 | ((void) argc); |
| 63 | ((void) argv); |
| 64 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 65 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C " |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 66 | "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] | 67 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 68 | "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_PARSE_C and/or " |
| 69 | "POLARSSL_TIMING_C not defined.\n"); |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 70 | return( 0 ); |
| 71 | } |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 72 | #elif defined(_WIN32) |
| 73 | int main( int argc, char *argv[] ) |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 74 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 75 | ((void) argc); |
| 76 | ((void) argv); |
| 77 | |
| 78 | printf("_WIN32 defined. This application requires fork() and signals " |
Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 79 | "to work correctly.\n"); |
| 80 | return( 0 ); |
| 81 | } |
| 82 | #else |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 83 | |
| 84 | #define DEBUG_LEVEL 0 |
| 85 | |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 86 | static void my_debug( void *ctx, int level, const char *str ) |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 87 | { |
| 88 | if( level < DEBUG_LEVEL ) |
| 89 | { |
| 90 | fprintf( (FILE *) ctx, "%s", str ); |
| 91 | fflush( (FILE *) ctx ); |
| 92 | } |
| 93 | } |
| 94 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 95 | int main( int argc, char *argv[] ) |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 96 | { |
| 97 | int ret, len, cnt = 0, pid; |
| 98 | int listen_fd; |
| 99 | int client_fd; |
| 100 | unsigned char buf[1024]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 101 | const char *pers = "ssl_fork_server"; |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 102 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 103 | entropy_context entropy; |
| 104 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 105 | ssl_context ssl; |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 106 | x509_cert srvcert; |
| 107 | rsa_context rsa; |
| 108 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 109 | ((void) argc); |
| 110 | ((void) argv); |
| 111 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 112 | signal( SIGCHLD, SIG_IGN ); |
| 113 | |
| 114 | /* |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 115 | * 0. Initial seeding of the RNG |
| 116 | */ |
| 117 | printf( "\n . Initial seeding of the random generator..." ); |
| 118 | fflush( stdout ); |
| 119 | |
| 120 | entropy_init( &entropy ); |
| 121 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 122 | (const unsigned char *) pers, |
| 123 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 124 | { |
| 125 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 126 | goto exit; |
| 127 | } |
| 128 | |
| 129 | printf( " ok\n" ); |
| 130 | |
| 131 | /* |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 132 | * 1. Load the certificates and private RSA key |
| 133 | */ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 134 | printf( " . Loading the server cert. and key..." ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 135 | fflush( stdout ); |
| 136 | |
| 137 | memset( &srvcert, 0, sizeof( x509_cert ) ); |
| 138 | |
| 139 | /* |
| 140 | * This demonstration program uses embedded test certificates. |
| 141 | * Instead, you may want to use x509parse_crtfile() to read the |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 142 | * server and CA certificates, as well as x509parse_keyfile_rsa(). |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 143 | */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 144 | ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 145 | strlen( test_srv_crt ) ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 146 | if( ret != 0 ) |
| 147 | { |
| 148 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 149 | goto exit; |
| 150 | } |
| 151 | |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 152 | ret = x509parse_crt( &srvcert, (const unsigned char *) test_ca_crt, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 153 | strlen( test_ca_crt ) ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 154 | if( ret != 0 ) |
| 155 | { |
| 156 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 157 | goto exit; |
| 158 | } |
| 159 | |
| 160 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 161 | ret = x509parse_key_rsa( &rsa, (const unsigned char *) test_srv_key, |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 162 | strlen( test_srv_key ), NULL, 0 ); |
| 163 | if( ret != 0 ) |
| 164 | { |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 165 | printf( " failed\n ! x509parse_key_rsa returned %d\n\n", ret ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 166 | goto exit; |
| 167 | } |
| 168 | |
| 169 | printf( " ok\n" ); |
| 170 | |
| 171 | /* |
| 172 | * 2. Setup the listening TCP socket |
| 173 | */ |
| 174 | printf( " . Bind on https://localhost:4433/ ..." ); |
| 175 | fflush( stdout ); |
| 176 | |
| 177 | if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 ) |
| 178 | { |
| 179 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 180 | goto exit; |
| 181 | } |
| 182 | |
| 183 | printf( " ok\n" ); |
| 184 | |
| 185 | while( 1 ) |
| 186 | { |
| 187 | /* |
| 188 | * 3. Wait until a client connects |
| 189 | */ |
| 190 | client_fd = -1; |
| 191 | memset( &ssl, 0, sizeof( ssl ) ); |
| 192 | |
| 193 | printf( " . Waiting for a remote connection ..." ); |
| 194 | fflush( stdout ); |
| 195 | |
| 196 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 197 | { |
| 198 | printf( " failed\n ! net_accept returned %d\n\n", ret ); |
| 199 | goto exit; |
| 200 | } |
| 201 | |
| 202 | printf( " ok\n" ); |
| 203 | |
| 204 | /* |
| 205 | * 3.5. Forking server thread |
| 206 | */ |
| 207 | |
| 208 | pid = fork(); |
| 209 | |
| 210 | printf( " . Forking to handle connection ..." ); |
| 211 | fflush( stdout ); |
| 212 | |
| 213 | if( pid < 0 ) |
| 214 | { |
| 215 | printf(" failed\n ! fork returned %d\n\n", pid ); |
| 216 | goto exit; |
| 217 | } |
| 218 | |
| 219 | printf( " ok\n" ); |
| 220 | |
| 221 | if( pid != 0 ) |
| 222 | { |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 223 | if( ( ret = ctr_drbg_reseed( &ctr_drbg, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 224 | (const unsigned char *) "parent", |
| 225 | 6 ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 226 | { |
| 227 | printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret ); |
| 228 | goto exit; |
| 229 | } |
| 230 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 231 | close( client_fd ); |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | close( listen_fd ); |
| 236 | |
| 237 | /* |
| 238 | * 4. Setup stuff |
| 239 | */ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 240 | printf( " . Setting up the SSL data...." ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 241 | fflush( stdout ); |
| 242 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 243 | if( ( ret = ctr_drbg_reseed( &ctr_drbg, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 244 | (const unsigned char *) "child", |
| 245 | 5 ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 246 | { |
| 247 | printf( " failed\n ! ctr_drbg_reseed returned %d\n", ret ); |
| 248 | goto exit; |
| 249 | } |
| 250 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 251 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 252 | { |
| 253 | printf( " failed\n ! ssl_init returned %d\n\n", ret ); |
| 254 | goto exit; |
| 255 | } |
| 256 | |
| 257 | printf( " ok\n" ); |
| 258 | |
| 259 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); |
| 260 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); |
| 261 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 262 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 263 | ssl_set_dbg( &ssl, my_debug, stdout ); |
| 264 | ssl_set_bio( &ssl, net_recv, &client_fd, |
| 265 | net_send, &client_fd ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 266 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 267 | ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL ); |
| 268 | ssl_set_own_cert( &ssl, &srvcert, &rsa ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 269 | |
| 270 | /* |
| 271 | * 5. Handshake |
| 272 | */ |
| 273 | printf( " . Performing the SSL/TLS handshake..." ); |
| 274 | fflush( stdout ); |
| 275 | |
| 276 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 277 | { |
| 278 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 279 | { |
| 280 | printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); |
| 281 | goto exit; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | printf( " ok\n" ); |
| 286 | |
| 287 | /* |
| 288 | * 6. Read the HTTP Request |
| 289 | */ |
| 290 | printf( " < Read from client:" ); |
| 291 | fflush( stdout ); |
| 292 | |
| 293 | do |
| 294 | { |
| 295 | len = sizeof( buf ) - 1; |
| 296 | memset( buf, 0, sizeof( buf ) ); |
| 297 | ret = ssl_read( &ssl, buf, len ); |
| 298 | |
| 299 | if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) |
| 300 | continue; |
| 301 | |
| 302 | if( ret <= 0 ) |
| 303 | { |
| 304 | switch( ret ) |
| 305 | { |
| 306 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: |
| 307 | printf( " connection was closed gracefully\n" ); |
| 308 | break; |
| 309 | |
| 310 | case POLARSSL_ERR_NET_CONN_RESET: |
| 311 | printf( " connection was reset by peer\n" ); |
| 312 | break; |
| 313 | |
| 314 | default: |
| 315 | printf( " ssl_read returned %d\n", ret ); |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | len = ret; |
| 323 | printf( " %d bytes read\n\n%s", len, (char *) buf ); |
| 324 | } |
| 325 | while( 0 ); |
| 326 | |
| 327 | /* |
| 328 | * 7. Write the 200 Response |
| 329 | */ |
| 330 | printf( " > Write to client:" ); |
| 331 | fflush( stdout ); |
| 332 | |
| 333 | len = sprintf( (char *) buf, HTTP_RESPONSE, |
| 334 | ssl_get_ciphersuite( &ssl ) ); |
| 335 | |
| 336 | while( cnt < 100 ) |
| 337 | { |
| 338 | while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) |
| 339 | { |
| 340 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) |
| 341 | { |
| 342 | printf( " failed\n ! peer closed the connection\n\n" ); |
| 343 | goto exit; |
| 344 | } |
| 345 | |
| 346 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 347 | { |
| 348 | printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
| 349 | goto exit; |
| 350 | } |
| 351 | } |
| 352 | len = ret; |
| 353 | printf( " %d bytes written\n\n%s\n", len, (char *) buf ); |
| 354 | |
| 355 | m_sleep( 1000 ); |
| 356 | } |
| 357 | |
| 358 | ssl_close_notify( &ssl ); |
| 359 | goto exit; |
| 360 | } |
| 361 | |
| 362 | exit: |
| 363 | |
| 364 | net_close( client_fd ); |
| 365 | x509_free( &srvcert ); |
| 366 | rsa_free( &rsa ); |
| 367 | ssl_free( &ssl ); |
| 368 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 369 | #if defined(_WIN32) |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 370 | printf( " Press Enter to exit this program.\n" ); |
| 371 | fflush( stdout ); getchar(); |
| 372 | #endif |
| 373 | |
| 374 | return( ret ); |
| 375 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 376 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C && |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 377 | POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C && |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 378 | POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */ |