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