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