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