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 | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +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 | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/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) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_fprintf fprintf |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 34 | #define polarssl_printf printf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 37 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | #include <windows.h> |
| 39 | #endif |
| 40 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 41 | #if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_CERTS_C) && \ |
| 42 | defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_SSL_TLS_C) && \ |
| 43 | defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_NET_C) && \ |
| 44 | defined(POLARSSL_RSA_C) && defined(POLARSSL_CTR_DRBG_C) && \ |
| 45 | defined(POLARSSL_X509_CRT_PARSE_C) && defined(POLARSSL_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/entropy.h" |
| 47 | #include "mbedtls/ctr_drbg.h" |
| 48 | #include "mbedtls/certs.h" |
| 49 | #include "mbedtls/x509.h" |
| 50 | #include "mbedtls/ssl.h" |
| 51 | #include "mbedtls/net.h" |
| 52 | #include "mbedtls/error.h" |
| 53 | #include "mbedtls/debug.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 54 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 55 | #include <stdio.h> |
| 56 | #include <stdlib.h> |
| 57 | #include <string.h> |
| 58 | #endif |
| 59 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 60 | #if defined(POLARSSL_SSL_CACHE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 61 | #include "mbedtls/ssl_cache.h" |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 64 | #define HTTP_RESPONSE \ |
| 65 | "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] | 66 | "<h2>mbed TLS Test Server</h2>\r\n" \ |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 67 | "<p>Successful connection using: %s</p>\r\n" |
| 68 | |
| 69 | #define DEBUG_LEVEL 0 |
| 70 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 72 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 73 | !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 74 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 75 | !defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 76 | int main( void ) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 77 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 78 | polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C " |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 79 | "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or " |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 80 | "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 81 | "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C " |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 82 | "not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 83 | return( 0 ); |
| 84 | } |
| 85 | #else |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 86 | static void my_debug( void *ctx, int level, const char *str ) |
| 87 | { |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 88 | ((void) level); |
| 89 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 90 | polarssl_fprintf( (FILE *) ctx, "%s", str ); |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 91 | fflush( (FILE *) ctx ); |
Paul Bakker | 9a97c5d | 2013-09-15 17:07:33 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 94 | int main( void ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | { |
| 96 | int ret, len; |
| 97 | int listen_fd; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 98 | int client_fd = -1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | unsigned char buf[1024]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 100 | const char *pers = "ssl_server"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 102 | entropy_context entropy; |
| 103 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 104 | ssl_context ssl; |
Paul Bakker | c559c7a | 2013-09-18 14:13:26 +0200 | [diff] [blame] | 105 | x509_crt srvcert; |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 106 | pk_context pkey; |
Paul Bakker | d432410 | 2012-09-26 08:29:38 +0000 | [diff] [blame] | 107 | #if defined(POLARSSL_SSL_CACHE_C) |
| 108 | ssl_cache_context cache; |
| 109 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | f5a1312 | 2014-03-23 17:38:16 +0100 | [diff] [blame] | 166 | if( ( ret = net_bind( &listen_fd, NULL, 4433, NET_PROTO_TCP ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 ); |
| 207 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 208 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | ssl_set_dbg( &ssl, my_debug, stdout ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 210 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 211 | #if defined(POLARSSL_SSL_CACHE_C) |
| 212 | ssl_set_session_cache( &ssl, ssl_cache_get, &cache, |
| 213 | ssl_cache_set, &cache ); |
| 214 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 215 | |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 216 | ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL ); |
Manuel Pégourié-Gonnard | c5fd391 | 2014-07-08 14:05:52 +0200 | [diff] [blame] | 217 | if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 ) |
| 218 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 219 | 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] | 220 | goto exit; |
| 221 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 222 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 223 | polarssl_printf( " ok\n" ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 224 | |
| 225 | reset: |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 226 | #ifdef POLARSSL_ERROR_C |
| 227 | if( ret != 0 ) |
| 228 | { |
| 229 | char error_buf[100]; |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 230 | polarssl_strerror( ret, error_buf, 100 ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 231 | polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 232 | } |
| 233 | #endif |
| 234 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 235 | if( client_fd != -1 ) |
| 236 | net_close( client_fd ); |
| 237 | |
| 238 | ssl_session_reset( &ssl ); |
| 239 | |
| 240 | /* |
| 241 | * 3. Wait until a client connects |
| 242 | */ |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 243 | client_fd = -1; |
| 244 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 245 | polarssl_printf( " . Waiting for a remote connection ..." ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 246 | fflush( stdout ); |
| 247 | |
| 248 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 249 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 250 | polarssl_printf( " failed\n ! net_accept returned %d\n\n", ret ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 251 | goto exit; |
| 252 | } |
| 253 | |
Manuel Pégourié-Gonnard | aeab252 | 2015-03-25 19:38:23 +0100 | [diff] [blame] | 254 | ssl_set_bio_timeout( &ssl, &client_fd, net_send, net_recv, NULL, 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 255 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 256 | polarssl_printf( " ok\n" ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 257 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 258 | /* |
| 259 | * 5. Handshake |
| 260 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 261 | polarssl_printf( " . Performing the SSL/TLS handshake..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 262 | fflush( stdout ); |
| 263 | |
| 264 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 265 | { |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 266 | 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] | 267 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 268 | polarssl_printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 269 | goto reset; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 273 | polarssl_printf( " ok\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * 6. Read the HTTP Request |
| 277 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 278 | polarssl_printf( " < Read from client:" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 279 | fflush( stdout ); |
| 280 | |
| 281 | do |
| 282 | { |
| 283 | len = sizeof( buf ) - 1; |
| 284 | memset( buf, 0, sizeof( buf ) ); |
| 285 | ret = ssl_read( &ssl, buf, len ); |
| 286 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 287 | 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] | 288 | continue; |
| 289 | |
| 290 | if( ret <= 0 ) |
| 291 | { |
| 292 | switch( ret ) |
| 293 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 294 | case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 295 | polarssl_printf( " connection was closed gracefully\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 296 | break; |
| 297 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 298 | case POLARSSL_ERR_NET_CONN_RESET: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 299 | polarssl_printf( " connection was reset by peer\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 300 | break; |
| 301 | |
| 302 | default: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 303 | polarssl_printf( " ssl_read returned -0x%x\n", -ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | len = ret; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 311 | polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 312 | |
| 313 | if( ret > 0 ) |
| 314 | break; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 315 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 316 | while( 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 317 | |
| 318 | /* |
| 319 | * 7. Write the 200 Response |
| 320 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 321 | polarssl_printf( " > Write to client:" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 322 | fflush( stdout ); |
| 323 | |
| 324 | len = sprintf( (char *) buf, HTTP_RESPONSE, |
Paul Bakker | e3166ce | 2011-01-27 17:40:50 +0000 | [diff] [blame] | 325 | ssl_get_ciphersuite( &ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 326 | |
| 327 | while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) |
| 328 | { |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 329 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 331 | polarssl_printf( " failed\n ! peer closed the connection\n\n" ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 332 | goto reset; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 335 | 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] | 336 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 337 | polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 | goto exit; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | len = ret; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 343 | 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] | 344 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 345 | polarssl_printf( " . Closing the connection..." ); |
Manuel Pégourié-Gonnard | 6b0d268 | 2014-03-25 11:24:43 +0100 | [diff] [blame] | 346 | |
| 347 | while( ( ret = ssl_close_notify( &ssl ) ) < 0 ) |
| 348 | { |
| 349 | if( ret != POLARSSL_ERR_NET_WANT_READ && |
| 350 | ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 351 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 352 | 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] | 353 | goto reset; |
| 354 | } |
| 355 | } |
| 356 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 357 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 6b0d268 | 2014-03-25 11:24:43 +0100 | [diff] [blame] | 358 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 359 | ret = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 360 | goto reset; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 361 | |
| 362 | exit: |
| 363 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 364 | #ifdef POLARSSL_ERROR_C |
| 365 | if( ret != 0 ) |
| 366 | { |
| 367 | char error_buf[100]; |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 368 | polarssl_strerror( ret, error_buf, 100 ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 369 | polarssl_printf("Last error was: %d - %s\n\n", ret, error_buf ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 370 | } |
| 371 | #endif |
| 372 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 373 | if( client_fd != -1 ) |
| 374 | net_close( client_fd ); |
| 375 | |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 376 | x509_crt_free( &srvcert ); |
Manuel Pégourié-Gonnard | ac75523 | 2013-08-19 14:10:16 +0200 | [diff] [blame] | 377 | pk_free( &pkey ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 378 | ssl_free( &ssl ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 379 | #if defined(POLARSSL_SSL_CACHE_C) |
| 380 | ssl_cache_free( &cache ); |
| 381 | #endif |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 382 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 383 | entropy_free( &entropy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 384 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 385 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 386 | polarssl_printf( " Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 387 | fflush( stdout ); getchar(); |
| 388 | #endif |
| 389 | |
| 390 | return( ret ); |
| 391 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 392 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C && |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 393 | POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C && |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 394 | POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */ |