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 | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 7 | * |
| 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 | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/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) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 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) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 47 | #include "mbedtls/entropy.h" |
| 48 | #include "mbedtls/ctr_drbg.h" |
| 49 | #include "mbedtls/certs.h" |
| 50 | #include "mbedtls/x509.h" |
| 51 | #include "mbedtls/ssl.h" |
| 52 | #include "mbedtls/net.h" |
| 53 | #include "mbedtls/timing.h" |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 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, |
Manuel Pégourié-Gonnard | 75f9010 | 2015-03-27 09:56:18 +0100 | [diff] [blame^] | 157 | test_srv_crt_len ); |
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, |
Manuel Pégourié-Gonnard | 75f9010 | 2015-03-27 09:56:18 +0100 | [diff] [blame^] | 165 | test_ca_list_len ); |
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, |
Manuel Pégourié-Gonnard | 75f9010 | 2015-03-27 09:56:18 +0100 | [diff] [blame^] | 173 | test_srv_key_len, NULL, 0 ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 188 | if( ( ret = net_bind( &listen_fd, NULL, 4433, NET_PROTO_TCP ) ) != 0 ) |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 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 ); |
| 276 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 277 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 278 | ssl_set_dbg( &ssl, my_debug, stdout ); |
Manuel Pégourié-Gonnard | aeab252 | 2015-03-25 19:38:23 +0100 | [diff] [blame] | 279 | ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv, NULL, 0 ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 280 | |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 281 | ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 282 | if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 ) |
| 283 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 284 | 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] | 285 | goto exit; |
| 286 | } |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * 5. Handshake |
| 290 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 291 | polarssl_printf( " . Performing the SSL/TLS handshake..." ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 292 | fflush( stdout ); |
| 293 | |
| 294 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 295 | { |
| 296 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 297 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 298 | polarssl_printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 299 | goto exit; |
| 300 | } |
| 301 | } |
| 302 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 303 | polarssl_printf( " ok\n" ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 304 | |
| 305 | /* |
| 306 | * 6. Read the HTTP Request |
| 307 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 308 | polarssl_printf( " < Read from client:" ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 309 | fflush( stdout ); |
| 310 | |
| 311 | do |
| 312 | { |
| 313 | len = sizeof( buf ) - 1; |
| 314 | memset( buf, 0, sizeof( buf ) ); |
| 315 | ret = ssl_read( &ssl, buf, len ); |
| 316 | |
| 317 | if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) |
| 318 | continue; |
| 319 | |
| 320 | if( ret <= 0 ) |
| 321 | { |
| 322 | switch( ret ) |
| 323 | { |
| 324 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 325 | polarssl_printf( " connection was closed gracefully\n" ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 326 | break; |
| 327 | |
| 328 | case POLARSSL_ERR_NET_CONN_RESET: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 329 | polarssl_printf( " connection was reset by peer\n" ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | default: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 333 | polarssl_printf( " ssl_read returned %d\n", ret ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | len = ret; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 341 | polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf ); |
Manuel Pégourié-Gonnard | 401caad | 2015-02-14 15:53:24 +0000 | [diff] [blame] | 342 | |
| 343 | if( ret > 0 ) |
| 344 | break; |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 345 | } |
Manuel Pégourié-Gonnard | 401caad | 2015-02-14 15:53:24 +0000 | [diff] [blame] | 346 | while( 1 ); |
Paul Bakker | 896ac22 | 2011-05-20 12:33:05 +0000 | [diff] [blame] | 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 */ |