Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL server demonstration program |
| 3 | * |
Paul Bakker | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, 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 | 3c5ef71 | 2013-06-25 16:37:45 +0200 | [diff] [blame] | 59 | static 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 | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 70 | !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \ |
| 71 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \ |
| 72 | !defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 73 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 74 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 75 | ((void) argc); |
| 76 | ((void) argv); |
| 77 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 78 | 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] | 79 | "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] | 80 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 81 | "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_PARSE_C not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 82 | return( 0 ); |
| 83 | } |
| 84 | #else |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 85 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | { |
| 87 | int ret, len; |
| 88 | int listen_fd; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 89 | int client_fd = -1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | unsigned char buf[1024]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 91 | const char *pers = "ssl_server"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 93 | entropy_context entropy; |
| 94 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | ssl_context ssl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | x509_cert srvcert; |
| 97 | rsa_context rsa; |
Paul Bakker | d432410 | 2012-09-26 08:29:38 +0000 | [diff] [blame] | 98 | #if defined(POLARSSL_SSL_CACHE_C) |
| 99 | ssl_cache_context cache; |
| 100 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 102 | ((void) argc); |
| 103 | ((void) argv); |
| 104 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 105 | #if defined(POLARSSL_SSL_CACHE_C) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 106 | ssl_cache_init( &cache ); |
| 107 | #endif |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 108 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | /* |
| 110 | * 1. Load the certificates and private RSA key |
| 111 | */ |
| 112 | printf( "\n . Loading the server cert. and key..." ); |
| 113 | fflush( stdout ); |
| 114 | |
| 115 | memset( &srvcert, 0, sizeof( x509_cert ) ); |
| 116 | |
| 117 | /* |
| 118 | * This demonstration program uses embedded test certificates. |
| 119 | * Instead, you may want to use x509parse_crtfile() to read the |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 120 | * server and CA certificates, as well as x509parse_keyfile_rsa(). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 121 | */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 122 | ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 123 | strlen( test_srv_crt ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | if( ret != 0 ) |
| 125 | { |
| 126 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 127 | goto exit; |
| 128 | } |
| 129 | |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 130 | ret = x509parse_crt( &srvcert, (const unsigned char *) test_ca_crt, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 131 | strlen( test_ca_crt ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | if( ret != 0 ) |
| 133 | { |
| 134 | printf( " failed\n ! x509parse_crt returned %d\n\n", ret ); |
| 135 | goto exit; |
| 136 | } |
| 137 | |
Paul Bakker | 91b4159 | 2011-05-05 12:01:31 +0000 | [diff] [blame] | 138 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 139 | ret = x509parse_key_rsa( &rsa, (const unsigned char *) test_srv_key, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 140 | strlen( test_srv_key ), NULL, 0 ); |
| 141 | if( ret != 0 ) |
| 142 | { |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 143 | printf( " failed\n ! x509parse_key_rsa returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | goto exit; |
| 145 | } |
| 146 | |
| 147 | printf( " ok\n" ); |
| 148 | |
| 149 | /* |
| 150 | * 2. Setup the listening TCP socket |
| 151 | */ |
| 152 | printf( " . Bind on https://localhost:4433/ ..." ); |
| 153 | fflush( stdout ); |
| 154 | |
| 155 | if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 ) |
| 156 | { |
| 157 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 158 | goto exit; |
| 159 | } |
| 160 | |
| 161 | printf( " ok\n" ); |
| 162 | |
| 163 | /* |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 164 | * 3. Seed the RNG |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | */ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 166 | printf( " . Seeding the random number generator..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 167 | fflush( stdout ); |
| 168 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 169 | entropy_init( &entropy ); |
| 170 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 171 | (const unsigned char *) pers, |
| 172 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 173 | { |
| 174 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 175 | goto exit; |
| 176 | } |
| 177 | |
| 178 | printf( " ok\n" ); |
| 179 | |
| 180 | /* |
| 181 | * 4. Setup stuff |
| 182 | */ |
| 183 | printf( " . Setting up the SSL data...." ); |
| 184 | fflush( stdout ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 185 | |
| 186 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 187 | { |
| 188 | printf( " failed\n ! ssl_init returned %d\n\n", ret ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 189 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | ssl_set_endpoint( &ssl, SSL_IS_SERVER ); |
| 193 | ssl_set_authmode( &ssl, SSL_VERIFY_NONE ); |
| 194 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 195 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 196 | ssl_set_dbg( &ssl, my_debug, stdout ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 197 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 198 | #if defined(POLARSSL_SSL_CACHE_C) |
| 199 | ssl_set_session_cache( &ssl, ssl_cache_get, &cache, |
| 200 | ssl_cache_set, &cache ); |
| 201 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 202 | |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 203 | ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 204 | ssl_set_own_cert( &ssl, &srvcert, &rsa ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 205 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 206 | printf( " ok\n" ); |
| 207 | |
| 208 | reset: |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 209 | #ifdef POLARSSL_ERROR_C |
| 210 | if( ret != 0 ) |
| 211 | { |
| 212 | char error_buf[100]; |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 213 | polarssl_strerror( ret, error_buf, 100 ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 214 | printf("Last error was: %d - %s\n\n", ret, error_buf ); |
| 215 | } |
| 216 | #endif |
| 217 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 218 | if( client_fd != -1 ) |
| 219 | net_close( client_fd ); |
| 220 | |
| 221 | ssl_session_reset( &ssl ); |
| 222 | |
| 223 | /* |
| 224 | * 3. Wait until a client connects |
| 225 | */ |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 226 | #if defined(_WIN32_WCE) |
| 227 | { |
| 228 | SHELLEXECUTEINFO sei; |
| 229 | |
| 230 | ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) ); |
| 231 | |
| 232 | sei.cbSize = sizeof( SHELLEXECUTEINFO ); |
| 233 | sei.fMask = 0; |
| 234 | sei.hwnd = 0; |
| 235 | sei.lpVerb = _T( "open" ); |
| 236 | sei.lpFile = _T( "https://localhost:4433/" ); |
| 237 | sei.lpParameters = NULL; |
| 238 | sei.lpDirectory = NULL; |
| 239 | sei.nShow = SW_SHOWNORMAL; |
| 240 | |
| 241 | ShellExecuteEx( &sei ); |
| 242 | } |
| 243 | #elif defined(_WIN32) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 244 | ShellExecute( NULL, "open", "https://localhost:4433/", |
| 245 | NULL, NULL, SW_SHOWNORMAL ); |
| 246 | #endif |
| 247 | |
| 248 | client_fd = -1; |
| 249 | |
| 250 | printf( " . Waiting for a remote connection ..." ); |
| 251 | fflush( stdout ); |
| 252 | |
| 253 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 254 | { |
| 255 | printf( " failed\n ! net_accept returned %d\n\n", ret ); |
| 256 | goto exit; |
| 257 | } |
| 258 | |
| 259 | ssl_set_bio( &ssl, net_recv, &client_fd, |
| 260 | net_send, &client_fd ); |
| 261 | |
| 262 | printf( " ok\n" ); |
| 263 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 264 | /* |
| 265 | * 5. Handshake |
| 266 | */ |
| 267 | printf( " . Performing the SSL/TLS handshake..." ); |
| 268 | fflush( stdout ); |
| 269 | |
| 270 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 271 | { |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 272 | 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] | 273 | { |
| 274 | printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 275 | goto reset; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | printf( " ok\n" ); |
| 280 | |
| 281 | /* |
| 282 | * 6. Read the HTTP Request |
| 283 | */ |
| 284 | printf( " < Read from client:" ); |
| 285 | fflush( stdout ); |
| 286 | |
| 287 | do |
| 288 | { |
| 289 | len = sizeof( buf ) - 1; |
| 290 | memset( buf, 0, sizeof( buf ) ); |
| 291 | ret = ssl_read( &ssl, buf, len ); |
| 292 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 293 | 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] | 294 | continue; |
| 295 | |
| 296 | if( ret <= 0 ) |
| 297 | { |
| 298 | switch( ret ) |
| 299 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 300 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 301 | printf( " connection was closed gracefully\n" ); |
| 302 | break; |
| 303 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 304 | case POLARSSL_ERR_NET_CONN_RESET: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 305 | printf( " connection was reset by peer\n" ); |
| 306 | break; |
| 307 | |
| 308 | default: |
Paul Bakker | 6f3578c | 2012-04-16 06:46:01 +0000 | [diff] [blame] | 309 | printf( " ssl_read returned -0x%x\n", -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | len = ret; |
| 317 | printf( " %d bytes read\n\n%s", len, (char *) buf ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 318 | |
| 319 | if( ret > 0 ) |
| 320 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 321 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 322 | while( 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * 7. Write the 200 Response |
| 326 | */ |
| 327 | printf( " > Write to client:" ); |
| 328 | fflush( stdout ); |
| 329 | |
| 330 | len = sprintf( (char *) buf, HTTP_RESPONSE, |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 331 | ssl_get_ciphersuite( &ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 332 | |
| 333 | while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) |
| 334 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 335 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | { |
| 337 | printf( " failed\n ! peer closed the connection\n\n" ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 338 | goto reset; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 341 | 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] | 342 | { |
| 343 | printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
| 344 | goto exit; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | len = ret; |
| 349 | printf( " %d bytes written\n\n%s\n", len, (char *) buf ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 350 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 351 | ret = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 352 | goto reset; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 353 | |
| 354 | exit: |
| 355 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 356 | #ifdef POLARSSL_ERROR_C |
| 357 | if( ret != 0 ) |
| 358 | { |
| 359 | char error_buf[100]; |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 360 | polarssl_strerror( ret, error_buf, 100 ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 361 | printf("Last error was: %d - %s\n\n", ret, error_buf ); |
| 362 | } |
| 363 | #endif |
| 364 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 365 | net_close( client_fd ); |
| 366 | x509_free( &srvcert ); |
| 367 | rsa_free( &rsa ); |
| 368 | ssl_free( &ssl ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 369 | #if defined(POLARSSL_SSL_CACHE_C) |
| 370 | ssl_cache_free( &cache ); |
| 371 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 372 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 373 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 374 | printf( " Press Enter to exit this program.\n" ); |
| 375 | fflush( stdout ); getchar(); |
| 376 | #endif |
| 377 | |
| 378 | return( ret ); |
| 379 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 380 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C && |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 381 | POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C && |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 382 | POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */ |