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