| 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 | * | 
| Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2013, ARM Limited, All Rights Reserved | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 5 | * | 
| Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 6 | *  This file is part of mbed TLS (https://polarssl.org) | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 7 | * | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 8 | *  This program is free software; you can redistribute it and/or modify | 
|  | 9 | *  it under the terms of the GNU General Public License as published by | 
|  | 10 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 11 | *  (at your option) any later version. | 
|  | 12 | * | 
|  | 13 | *  This program is distributed in the hope that it will be useful, | 
|  | 14 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 15 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 16 | *  GNU General Public License for more details. | 
|  | 17 | * | 
|  | 18 | *  You should have received a copy of the GNU General Public License along | 
|  | 19 | *  with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 20 | *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 21 | */ | 
|  | 22 |  | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else | 
|  | 26 | #include POLARSSL_CONFIG_FILE | 
|  | 27 | #endif | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 28 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) | 
|  | 30 | #include "polarssl/platform.h" | 
|  | 31 | #else | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_fprintf    fprintf | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 34 | #define polarssl_printf     printf | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 35 | #endif | 
|  | 36 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 37 | #if defined(_WIN32) | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 38 | #include <windows.h> | 
|  | 39 | #endif | 
|  | 40 |  | 
| Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 41 | #if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_CERTS_C) && \ | 
|  | 42 | defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_SSL_TLS_C) && \ | 
|  | 43 | defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_NET_C) && \ | 
|  | 44 | defined(POLARSSL_RSA_C) && defined(POLARSSL_CTR_DRBG_C) && \ | 
|  | 45 | defined(POLARSSL_X509_CRT_PARSE_C) && defined(POLARSSL_TIMING_C) && \ | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 46 | defined(POLARSSL_FS_IO) | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 47 | #include "polarssl/entropy.h" | 
|  | 48 | #include "polarssl/ctr_drbg.h" | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 49 | #include "polarssl/certs.h" | 
|  | 50 | #include "polarssl/x509.h" | 
|  | 51 | #include "polarssl/ssl.h" | 
|  | 52 | #include "polarssl/net.h" | 
|  | 53 | #include "polarssl/timing.h" | 
|  | 54 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 55 | #include <string.h> | 
|  | 56 | #include <stdio.h> | 
|  | 57 | #include <signal.h> | 
|  | 58 | #endif | 
|  | 59 |  | 
|  | 60 | #if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32) | 
|  | 61 | #include <unistd.h> | 
|  | 62 | #endif | 
|  | 63 |  | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 64 | #define HTTP_RESPONSE \ | 
|  | 65 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ | 
| Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 66 | "<h2>mbed TLS Test Server</h2>\r\n" \ | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 67 | "<p>Successful connection using: %s</p>\r\n" | 
|  | 68 |  | 
| Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 69 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) ||    \ | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 70 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \ | 
| Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 71 | !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) ||     \ | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 72 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) ||    \ | 
| Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 73 | !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_TIMING_C) || \ | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 74 | !defined(POLARSSL_FS_IO) | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 75 | int main( int argc, char *argv[] ) | 
| Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 76 | { | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 77 | ((void) argc); | 
|  | 78 | ((void) argv); | 
|  | 79 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 80 | polarssl_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] | 81 | "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] | 82 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " | 
| Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 83 | "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C and/or " | 
| Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 84 | "POLARSSL_TIMING_C not defined.\n"); | 
| Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 85 | return( 0 ); | 
|  | 86 | } | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 87 | #elif defined(_WIN32) | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 88 | int main( void ) | 
| Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 89 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 90 | polarssl_printf("_WIN32 defined. This application requires fork() and signals " | 
| Paul Bakker | b892b13 | 2011-10-12 09:19:43 +0000 | [diff] [blame] | 91 | "to work correctly.\n"); | 
|  | 92 | return( 0 ); | 
|  | 93 | } | 
|  | 94 | #else | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 95 |  | 
|  | 96 | #define DEBUG_LEVEL 0 | 
|  | 97 |  | 
| Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 98 | static void my_debug( void *ctx, int level, const char *str ) | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 99 | { | 
|  | 100 | if( level < DEBUG_LEVEL ) | 
|  | 101 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 102 | polarssl_fprintf( (FILE *) ctx, "%s", str ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 103 | fflush(  (FILE *) ctx  ); | 
|  | 104 | } | 
|  | 105 | } | 
|  | 106 |  | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 107 | int main( void ) | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 108 | { | 
|  | 109 | int ret, len, cnt = 0, pid; | 
|  | 110 | int listen_fd; | 
| Manuel Pégourié-Gonnard | 68821da | 2013-09-16 12:34:33 +0200 | [diff] [blame] | 111 | int client_fd = -1; | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 112 | unsigned char buf[1024]; | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 113 | const char *pers = "ssl_fork_server"; | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 114 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 115 | entropy_context entropy; | 
|  | 116 | ctr_drbg_context ctr_drbg; | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 117 | ssl_context ssl; | 
| Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 118 | x509_crt srvcert; | 
| Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 119 | pk_context pkey; | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 120 |  | 
| Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 121 | memset( &ssl, 0, sizeof(ssl_context) ); | 
|  | 122 |  | 
|  | 123 | entropy_init( &entropy ); | 
|  | 124 | pk_init( &pkey ); | 
|  | 125 | x509_crt_init( &srvcert ); | 
|  | 126 |  | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 127 | signal( SIGCHLD, SIG_IGN ); | 
|  | 128 |  | 
|  | 129 | /* | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 130 | * 0. Initial seeding of the RNG | 
|  | 131 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 132 | polarssl_printf( "\n  . Initial seeding of the random generator..." ); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 133 | fflush( stdout ); | 
|  | 134 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 135 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 136 | (const unsigned char *) pers, | 
|  | 137 | strlen( pers ) ) ) != 0 ) | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 138 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 139 | polarssl_printf( " failed\n  ! ctr_drbg_init returned %d\n", ret ); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 140 | goto exit; | 
|  | 141 | } | 
|  | 142 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 143 | polarssl_printf( " ok\n" ); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 144 |  | 
|  | 145 | /* | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 146 | * 1. Load the certificates and private RSA key | 
|  | 147 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 148 | polarssl_printf( "  . Loading the server cert. and key..." ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 149 | fflush( stdout ); | 
|  | 150 |  | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 151 | /* | 
|  | 152 | * This demonstration program uses embedded test certificates. | 
| Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 153 | * Instead, you may want to use x509_crt_parse_file() to read the | 
|  | 154 | * server and CA certificates, as well as pk_parse_keyfile(). | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 155 | */ | 
| Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 156 | ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt, | 
|  | 157 | strlen( test_srv_crt ) ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 158 | if( ret != 0 ) | 
|  | 159 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 160 | polarssl_printf( " failed\n  !  x509_crt_parse returned %d\n\n", ret ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 161 | goto exit; | 
|  | 162 | } | 
|  | 163 |  | 
| Manuel Pégourié-Gonnard | 641de71 | 2013-09-25 13:23:33 +0200 | [diff] [blame] | 164 | ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list, | 
|  | 165 | strlen( test_ca_list ) ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 166 | if( ret != 0 ) | 
|  | 167 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 168 | polarssl_printf( " failed\n  !  x509_crt_parse returned %d\n\n", ret ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 169 | goto exit; | 
|  | 170 | } | 
|  | 171 |  | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 172 | ret =  pk_parse_key( &pkey, (const unsigned char *) test_srv_key, | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 173 | strlen( test_srv_key ), NULL, 0 ); | 
|  | 174 | if( ret != 0 ) | 
|  | 175 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 176 | polarssl_printf( " failed\n  !  pk_parse_key returned %d\n\n", ret ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 177 | goto exit; | 
|  | 178 | } | 
|  | 179 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 180 | polarssl_printf( " ok\n" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 181 |  | 
|  | 182 | /* | 
|  | 183 | * 2. Setup the listening TCP socket | 
|  | 184 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 185 | polarssl_printf( "  . Bind on https://localhost:4433/ ..." ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 186 | fflush( stdout ); | 
|  | 187 |  | 
|  | 188 | if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 ) | 
|  | 189 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 190 | polarssl_printf( " failed\n  ! net_bind returned %d\n\n", ret ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 191 | goto exit; | 
|  | 192 | } | 
|  | 193 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 194 | polarssl_printf( " ok\n" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 195 |  | 
|  | 196 | while( 1 ) | 
|  | 197 | { | 
|  | 198 | /* | 
|  | 199 | * 3. Wait until a client connects | 
|  | 200 | */ | 
|  | 201 | client_fd = -1; | 
|  | 202 | memset( &ssl, 0, sizeof( ssl ) ); | 
|  | 203 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 204 | polarssl_printf( "  . Waiting for a remote connection ..." ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 205 | fflush( stdout ); | 
|  | 206 |  | 
|  | 207 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) | 
|  | 208 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 209 | polarssl_printf( " failed\n  ! net_accept returned %d\n\n", ret ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 210 | goto exit; | 
|  | 211 | } | 
|  | 212 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 213 | polarssl_printf( " ok\n" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 214 |  | 
|  | 215 | /* | 
|  | 216 | * 3.5. Forking server thread | 
|  | 217 | */ | 
|  | 218 |  | 
|  | 219 | pid = fork(); | 
|  | 220 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 221 | polarssl_printf( "  . Forking to handle connection ..." ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 222 | fflush( stdout ); | 
|  | 223 |  | 
|  | 224 | if( pid < 0 ) | 
|  | 225 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 226 | polarssl_printf(" failed\n  ! fork returned %d\n\n", pid ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 227 | goto exit; | 
|  | 228 | } | 
|  | 229 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 230 | polarssl_printf( " ok\n" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 231 |  | 
|  | 232 | if( pid != 0 ) | 
|  | 233 | { | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 234 | if( ( ret = ctr_drbg_reseed( &ctr_drbg, | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 235 | (const unsigned char *) "parent", | 
|  | 236 | 6 ) ) != 0 ) | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 237 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 238 | polarssl_printf( " failed\n  ! ctr_drbg_reseed returned %d\n", ret ); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 239 | goto exit; | 
|  | 240 | } | 
|  | 241 |  | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 242 | close( client_fd ); | 
|  | 243 | continue; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | close( listen_fd ); | 
|  | 247 |  | 
|  | 248 | /* | 
|  | 249 | * 4. Setup stuff | 
|  | 250 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 251 | polarssl_printf( "  . Setting up the SSL data...." ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 252 | fflush( stdout ); | 
|  | 253 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 254 | if( ( ret = ctr_drbg_reseed( &ctr_drbg, | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 255 | (const unsigned char *) "child", | 
|  | 256 | 5 ) ) != 0 ) | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 257 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 258 | polarssl_printf( " failed\n  ! ctr_drbg_reseed returned %d\n", ret ); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 259 | goto exit; | 
|  | 260 | } | 
| Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 261 |  | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 262 | if( ( ret = ssl_init( &ssl ) ) != 0 ) | 
|  | 263 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 264 | polarssl_printf( " failed\n  ! ssl_init returned %d\n\n", ret ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 265 | goto exit; | 
|  | 266 | } | 
|  | 267 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 268 | polarssl_printf( " ok\n" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 269 |  | 
|  | 270 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); | 
|  | 271 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); | 
|  | 272 |  | 
| Manuel Pégourié-Gonnard | 448ea50 | 2015-01-12 11:40:14 +0100 | [diff] [blame] | 273 | /* SSLv3 is deprecated, set minimum to TLS 1.0 */ | 
|  | 274 | ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, | 
|  | 275 | SSL_MINOR_VERSION_1 ); | 
| Manuel Pégourié-Gonnard | fa06581 | 2015-01-12 14:05:33 +0100 | [diff] [blame] | 276 | /* RC4 is deprecated, disable it */ | 
|  | 277 | ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED ); | 
| Manuel Pégourié-Gonnard | 448ea50 | 2015-01-12 11:40:14 +0100 | [diff] [blame] | 278 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 279 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 280 | ssl_set_dbg( &ssl, my_debug, stdout ); | 
|  | 281 | ssl_set_bio( &ssl, net_recv, &client_fd, | 
|  | 282 | net_send, &client_fd ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 283 |  | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 284 | ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL ); | 
| Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 285 | if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 ) | 
|  | 286 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 287 | polarssl_printf( " failed\n  ! ssl_set_own_cert returned %d\n\n", ret ); | 
| Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 288 | goto exit; | 
|  | 289 | } | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 290 |  | 
|  | 291 | /* | 
|  | 292 | * 5. Handshake | 
|  | 293 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 294 | polarssl_printf( "  . Performing the SSL/TLS handshake..." ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 295 | fflush( stdout ); | 
|  | 296 |  | 
|  | 297 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) | 
|  | 298 | { | 
|  | 299 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) | 
|  | 300 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 301 | polarssl_printf( " failed\n  ! ssl_handshake returned %d\n\n", ret ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 302 | goto exit; | 
|  | 303 | } | 
|  | 304 | } | 
|  | 305 |  | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 306 | polarssl_printf( " ok\n" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 307 |  | 
|  | 308 | /* | 
|  | 309 | * 6. Read the HTTP Request | 
|  | 310 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 311 | polarssl_printf( "  < Read from client:" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 312 | fflush( stdout ); | 
|  | 313 |  | 
|  | 314 | do | 
|  | 315 | { | 
|  | 316 | len = sizeof( buf ) - 1; | 
|  | 317 | memset( buf, 0, sizeof( buf ) ); | 
|  | 318 | ret = ssl_read( &ssl, buf, len ); | 
|  | 319 |  | 
|  | 320 | if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) | 
|  | 321 | continue; | 
|  | 322 |  | 
|  | 323 | if( ret <= 0 ) | 
|  | 324 | { | 
|  | 325 | switch( ret ) | 
|  | 326 | { | 
|  | 327 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 328 | polarssl_printf( " connection was closed gracefully\n" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 329 | break; | 
|  | 330 |  | 
|  | 331 | case POLARSSL_ERR_NET_CONN_RESET: | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 332 | polarssl_printf( " connection was reset by peer\n" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 333 | break; | 
|  | 334 |  | 
|  | 335 | default: | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 336 | polarssl_printf( " ssl_read returned %d\n", ret ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 337 | break; | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | break; | 
|  | 341 | } | 
|  | 342 |  | 
|  | 343 | len = ret; | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 344 | polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 345 | } | 
|  | 346 | while( 0 ); | 
|  | 347 |  | 
|  | 348 | /* | 
|  | 349 | * 7. Write the 200 Response | 
|  | 350 | */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 351 | polarssl_printf( "  > Write to client:" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 352 | fflush( stdout ); | 
|  | 353 |  | 
|  | 354 | len = sprintf( (char *) buf, HTTP_RESPONSE, | 
|  | 355 | ssl_get_ciphersuite( &ssl ) ); | 
|  | 356 |  | 
| Paul Bakker | 030decd | 2014-04-17 16:03:23 +0200 | [diff] [blame] | 357 | while( cnt++ < 100 ) | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 358 | { | 
|  | 359 | while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) | 
|  | 360 | { | 
|  | 361 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) | 
|  | 362 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 363 | polarssl_printf( " failed\n  ! peer closed the connection\n\n" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 364 | goto exit; | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) | 
|  | 368 | { | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 369 | polarssl_printf( " failed\n  ! ssl_write returned %d\n\n", ret ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 370 | goto exit; | 
|  | 371 | } | 
|  | 372 | } | 
|  | 373 | len = ret; | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 374 | polarssl_printf( " %d bytes written\n\n%s\n", len, (char *) buf ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 375 |  | 
|  | 376 | m_sleep( 1000 ); | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | ssl_close_notify( &ssl ); | 
|  | 380 | goto exit; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | exit: | 
|  | 384 |  | 
| Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 385 | if( client_fd != -1 ) | 
|  | 386 | net_close( client_fd ); | 
|  | 387 |  | 
| Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 388 | x509_crt_free( &srvcert ); | 
| Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 389 | pk_free( &pkey ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 390 | ssl_free( &ssl ); | 
| Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 391 | ctr_drbg_free( &ctr_drbg ); | 
| Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 392 | entropy_free( &entropy ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 393 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 394 | #if defined(_WIN32) | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 395 | polarssl_printf( "  Press Enter to exit this program.\n" ); | 
| Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 396 | fflush( stdout ); getchar(); | 
|  | 397 | #endif | 
|  | 398 |  | 
|  | 399 | return( ret ); | 
|  | 400 | } | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 401 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C && | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 402 | POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C && | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 403 | POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */ |