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