| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  SSL server demonstration program | 
|  | 3 | * | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2011, Brainspark B.V. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * | 
|  | 6 | *  This file is part of PolarSSL (http://www.polarssl.org) | 
| Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * | 
| Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | *  All rights reserved. | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | *  This program is free software; you can redistribute it and/or modify | 
|  | 12 | *  it under the terms of the GNU General Public License as published by | 
|  | 13 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 14 | *  (at your option) any later version. | 
|  | 15 | * | 
|  | 16 | *  This program is distributed in the hope that it will be useful, | 
|  | 17 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 18 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 19 | *  GNU General Public License for more details. | 
|  | 20 | * | 
|  | 21 | *  You should have received a copy of the GNU General Public License along | 
|  | 22 | *  with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 23 | *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 24 | */ | 
|  | 25 |  | 
|  | 26 | #ifndef _CRT_SECURE_NO_DEPRECATE | 
|  | 27 | #define _CRT_SECURE_NO_DEPRECATE 1 | 
|  | 28 | #endif | 
|  | 29 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 30 | #if defined(_WIN32) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | #include <windows.h> | 
|  | 32 | #endif | 
|  | 33 |  | 
|  | 34 | #include <string.h> | 
|  | 35 | #include <stdlib.h> | 
|  | 36 | #include <stdio.h> | 
|  | 37 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 38 | #include "polarssl/config.h" | 
|  | 39 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 40 | #include "polarssl/entropy.h" | 
|  | 41 | #include "polarssl/ctr_drbg.h" | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 42 | #include "polarssl/certs.h" | 
|  | 43 | #include "polarssl/x509.h" | 
|  | 44 | #include "polarssl/ssl.h" | 
|  | 45 | #include "polarssl/net.h" | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 46 | #include "polarssl/error.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 48 | #if defined(POLARSSL_SSL_CACHE_C) | 
|  | 49 | #include "polarssl/ssl_cache.h" | 
|  | 50 | #endif | 
|  | 51 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | #define HTTP_RESPONSE \ | 
|  | 53 | "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \ | 
| Paul Bakker | eaca51d | 2010-08-16 12:00:14 +0000 | [diff] [blame] | 54 | "<h2>PolarSSL Test Server</h2>\r\n" \ | 
|  | 55 | "<p>Successful connection using: %s</p>\r\n" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 |  | 
| Paul Bakker | 835b29e | 2012-08-23 08:31:59 +0000 | [diff] [blame] | 57 | #define DEBUG_LEVEL 0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 |  | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 59 | void my_debug( void *ctx, int level, const char *str ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | { | 
|  | 61 | if( level < DEBUG_LEVEL ) | 
|  | 62 | { | 
|  | 63 | fprintf( (FILE *) ctx, "%s", str ); | 
|  | 64 | fflush(  (FILE *) ctx  ); | 
|  | 65 | } | 
|  | 66 | } | 
|  | 67 |  | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 68 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) ||    \ | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 69 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \ | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 70 | !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) ||   \ | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 71 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 72 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 73 | { | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 74 | ((void) argc); | 
|  | 75 | ((void) argv); | 
|  | 76 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 77 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C " | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 78 | "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] | 79 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " | 
|  | 80 | "POLARSSL_CTR_DRBG_C not defined.\n"); | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 81 | return( 0 ); | 
|  | 82 | } | 
|  | 83 | #else | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 84 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | { | 
|  | 86 | int ret, len; | 
|  | 87 | int listen_fd; | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 88 | int client_fd = -1; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 | unsigned char buf[1024]; | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 90 | char *pers = "ssl_server"; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 92 | entropy_context entropy; | 
|  | 93 | ctr_drbg_context ctr_drbg; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | ssl_context ssl; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | x509_cert srvcert; | 
|  | 96 | rsa_context rsa; | 
| Paul Bakker | d432410 | 2012-09-26 08:29:38 +0000 | [diff] [blame] | 97 | #if defined(POLARSSL_SSL_CACHE_C) | 
|  | 98 | ssl_cache_context cache; | 
|  | 99 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 101 | ((void) argc); | 
|  | 102 | ((void) argv); | 
|  | 103 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 104 | #if defined(POLARSSL_SSL_CACHE_C) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 105 | ssl_cache_init( &cache ); | 
|  | 106 | #endif | 
| Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 107 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | /* | 
|  | 109 | * 1. Load the certificates and private RSA key | 
|  | 110 | */ | 
|  | 111 | printf( "\n  . Loading the server cert. and key..." ); | 
|  | 112 | fflush( stdout ); | 
|  | 113 |  | 
|  | 114 | memset( &srvcert, 0, sizeof( x509_cert ) ); | 
|  | 115 |  | 
|  | 116 | /* | 
|  | 117 | * This demonstration program uses embedded test certificates. | 
|  | 118 | * Instead, you may want to use x509parse_crtfile() to read the | 
|  | 119 | * server and CA certificates, as well as x509parse_keyfile(). | 
|  | 120 | */ | 
|  | 121 | ret = x509parse_crt( &srvcert, (unsigned char *) test_srv_crt, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 122 | strlen( test_srv_crt ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | if( ret != 0 ) | 
|  | 124 | { | 
|  | 125 | printf( " failed\n  !  x509parse_crt returned %d\n\n", ret ); | 
|  | 126 | goto exit; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | ret = x509parse_crt( &srvcert, (unsigned char *) test_ca_crt, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 130 | strlen( test_ca_crt ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | if( ret != 0 ) | 
|  | 132 | { | 
|  | 133 | printf( " failed\n  !  x509parse_crt returned %d\n\n", ret ); | 
|  | 134 | goto exit; | 
|  | 135 | } | 
|  | 136 |  | 
| Paul Bakker | 91b4159 | 2011-05-05 12:01:31 +0000 | [diff] [blame] | 137 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | ret =  x509parse_key( &rsa, (unsigned char *) test_srv_key, | 
|  | 139 | strlen( test_srv_key ), NULL, 0 ); | 
|  | 140 | if( ret != 0 ) | 
|  | 141 | { | 
|  | 142 | printf( " failed\n  !  x509parse_key returned %d\n\n", ret ); | 
|  | 143 | goto exit; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | printf( " ok\n" ); | 
|  | 147 |  | 
|  | 148 | /* | 
|  | 149 | * 2. Setup the listening TCP socket | 
|  | 150 | */ | 
|  | 151 | printf( "  . Bind on https://localhost:4433/ ..." ); | 
|  | 152 | fflush( stdout ); | 
|  | 153 |  | 
|  | 154 | if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 ) | 
|  | 155 | { | 
|  | 156 | printf( " failed\n  ! net_bind returned %d\n\n", ret ); | 
|  | 157 | goto exit; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | printf( " ok\n" ); | 
|  | 161 |  | 
|  | 162 | /* | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 163 | * 3. Seed the RNG | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | */ | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 165 | printf( "  . Seeding the random number generator..." ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | fflush( stdout ); | 
|  | 167 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 168 | entropy_init( &entropy ); | 
|  | 169 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, | 
|  | 170 | (unsigned char *) pers, strlen( pers ) ) ) != 0 ) | 
|  | 171 | { | 
|  | 172 | printf( " failed\n  ! ctr_drbg_init returned %d\n", ret ); | 
|  | 173 | goto exit; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | printf( " ok\n" ); | 
|  | 177 |  | 
|  | 178 | /* | 
|  | 179 | * 4. Setup stuff | 
|  | 180 | */ | 
|  | 181 | printf( "  . Setting up the SSL data...." ); | 
|  | 182 | fflush( stdout ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 |  | 
|  | 184 | if( ( ret = ssl_init( &ssl ) ) != 0 ) | 
|  | 185 | { | 
|  | 186 | printf( " failed\n  ! ssl_init returned %d\n\n", ret ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 187 | goto exit; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); | 
|  | 191 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); | 
|  | 192 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 193 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 194 | ssl_set_dbg( &ssl, my_debug, stdout ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 195 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 196 | #if defined(POLARSSL_SSL_CACHE_C) | 
|  | 197 | ssl_set_session_cache( &ssl, ssl_cache_get, &cache, | 
|  | 198 | ssl_cache_set, &cache ); | 
|  | 199 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 |  | 
| Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 201 | ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 202 | ssl_set_own_cert( &ssl, &srvcert, &rsa ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 |  | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 204 | printf( " ok\n" ); | 
|  | 205 |  | 
|  | 206 | reset: | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 207 | #ifdef POLARSSL_ERROR_C | 
|  | 208 | if( ret != 0 ) | 
|  | 209 | { | 
|  | 210 | char error_buf[100]; | 
|  | 211 | error_strerror( ret, error_buf, 100 ); | 
|  | 212 | printf("Last error was: %d - %s\n\n", ret, error_buf ); | 
|  | 213 | } | 
|  | 214 | #endif | 
|  | 215 |  | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 216 | if( client_fd != -1 ) | 
|  | 217 | net_close( client_fd ); | 
|  | 218 |  | 
|  | 219 | ssl_session_reset( &ssl ); | 
|  | 220 |  | 
|  | 221 | /* | 
|  | 222 | * 3. Wait until a client connects | 
|  | 223 | */ | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 224 | #if defined(_WIN32_WCE) | 
|  | 225 | { | 
|  | 226 | SHELLEXECUTEINFO sei; | 
|  | 227 |  | 
|  | 228 | ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) ); | 
|  | 229 |  | 
|  | 230 | sei.cbSize = sizeof( SHELLEXECUTEINFO ); | 
|  | 231 | sei.fMask = 0; | 
|  | 232 | sei.hwnd = 0; | 
|  | 233 | sei.lpVerb = _T( "open" ); | 
|  | 234 | sei.lpFile = _T( "https://localhost:4433/" ); | 
|  | 235 | sei.lpParameters = NULL; | 
|  | 236 | sei.lpDirectory = NULL; | 
|  | 237 | sei.nShow = SW_SHOWNORMAL; | 
|  | 238 |  | 
|  | 239 | ShellExecuteEx( &sei ); | 
|  | 240 | } | 
|  | 241 | #elif defined(_WIN32) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 242 | ShellExecute( NULL, "open", "https://localhost:4433/", | 
|  | 243 | NULL, NULL, SW_SHOWNORMAL ); | 
|  | 244 | #endif | 
|  | 245 |  | 
|  | 246 | client_fd = -1; | 
|  | 247 |  | 
|  | 248 | printf( "  . Waiting for a remote connection ..." ); | 
|  | 249 | fflush( stdout ); | 
|  | 250 |  | 
|  | 251 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) | 
|  | 252 | { | 
|  | 253 | printf( " failed\n  ! net_accept returned %d\n\n", ret ); | 
|  | 254 | goto exit; | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | ssl_set_bio( &ssl, net_recv, &client_fd, | 
|  | 258 | net_send, &client_fd ); | 
|  | 259 |  | 
|  | 260 | printf( " ok\n" ); | 
|  | 261 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 262 | /* | 
|  | 263 | * 5. Handshake | 
|  | 264 | */ | 
|  | 265 | printf( "  . Performing the SSL/TLS handshake..." ); | 
|  | 266 | fflush( stdout ); | 
|  | 267 |  | 
|  | 268 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) | 
|  | 269 | { | 
| Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 270 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 271 | { | 
|  | 272 | printf( " failed\n  ! ssl_handshake returned %d\n\n", ret ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 273 | goto reset; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 274 | } | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | printf( " ok\n" ); | 
|  | 278 |  | 
|  | 279 | /* | 
|  | 280 | * 6. Read the HTTP Request | 
|  | 281 | */ | 
|  | 282 | printf( "  < Read from client:" ); | 
|  | 283 | fflush( stdout ); | 
|  | 284 |  | 
|  | 285 | do | 
|  | 286 | { | 
|  | 287 | len = sizeof( buf ) - 1; | 
|  | 288 | memset( buf, 0, sizeof( buf ) ); | 
|  | 289 | ret = ssl_read( &ssl, buf, len ); | 
|  | 290 |  | 
| Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 291 | if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 | continue; | 
|  | 293 |  | 
|  | 294 | if( ret <= 0 ) | 
|  | 295 | { | 
|  | 296 | switch( ret ) | 
|  | 297 | { | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 298 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 299 | printf( " connection was closed gracefully\n" ); | 
|  | 300 | break; | 
|  | 301 |  | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 302 | case POLARSSL_ERR_NET_CONN_RESET: | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 303 | printf( " connection was reset by peer\n" ); | 
|  | 304 | break; | 
|  | 305 |  | 
|  | 306 | default: | 
| Paul Bakker | 6f3578c | 2012-04-16 06:46:01 +0000 | [diff] [blame] | 307 | printf( " ssl_read returned -0x%x\n", -ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 308 | break; | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | break; | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | len = ret; | 
|  | 315 | printf( " %d bytes read\n\n%s", len, (char *) buf ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 316 |  | 
|  | 317 | if( ret > 0 ) | 
|  | 318 | break; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 319 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 320 | while( 1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 321 |  | 
|  | 322 | /* | 
|  | 323 | * 7. Write the 200 Response | 
|  | 324 | */ | 
|  | 325 | printf( "  > Write to client:" ); | 
|  | 326 | fflush( stdout ); | 
|  | 327 |  | 
|  | 328 | len = sprintf( (char *) buf, HTTP_RESPONSE, | 
| Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 329 | ssl_get_ciphersuite( &ssl ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 |  | 
|  | 331 | while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) | 
|  | 332 | { | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 333 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 334 | { | 
|  | 335 | printf( " failed\n  ! peer closed the connection\n\n" ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 336 | goto reset; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
| Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 339 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 340 | { | 
|  | 341 | printf( " failed\n  ! ssl_write returned %d\n\n", ret ); | 
|  | 342 | goto exit; | 
|  | 343 | } | 
|  | 344 | } | 
|  | 345 |  | 
|  | 346 | len = ret; | 
|  | 347 | printf( " %d bytes written\n\n%s\n", len, (char *) buf ); | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 348 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 349 | ret = 0; | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 350 | goto reset; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 351 |  | 
|  | 352 | exit: | 
|  | 353 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 354 | #ifdef POLARSSL_ERROR_C | 
|  | 355 | if( ret != 0 ) | 
|  | 356 | { | 
|  | 357 | char error_buf[100]; | 
|  | 358 | error_strerror( ret, error_buf, 100 ); | 
|  | 359 | printf("Last error was: %d - %s\n\n", ret, error_buf ); | 
|  | 360 | } | 
|  | 361 | #endif | 
|  | 362 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 363 | net_close( client_fd ); | 
|  | 364 | x509_free( &srvcert ); | 
|  | 365 | rsa_free( &rsa ); | 
|  | 366 | ssl_free( &ssl ); | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 367 | #if defined(POLARSSL_SSL_CACHE_C) | 
|  | 368 | ssl_cache_free( &cache ); | 
|  | 369 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 370 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 371 | #if defined(_WIN32) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 372 | printf( "  Press Enter to exit this program.\n" ); | 
|  | 373 | fflush( stdout ); getchar(); | 
|  | 374 | #endif | 
|  | 375 |  | 
|  | 376 | return( ret ); | 
|  | 377 | } | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 378 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C && | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 379 | POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C && | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 380 | POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */ |