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