blob: b5bcfecf7a77d3492b53e4e5b26a9627bb9c09fb [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL server demonstration program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * 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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakkercce9d772011-11-18 14:26:47 +000029#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000030#include <windows.h>
31#endif
32
33#include <string.h>
34#include <stdlib.h>
35#include <stdio.h>
36
Paul Bakker508ad5a2011-12-04 17:09:26 +000037#include "polarssl/entropy.h"
38#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000039#include "polarssl/certs.h"
40#include "polarssl/x509.h"
41#include "polarssl/ssl.h"
42#include "polarssl/net.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000043#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020044#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Paul Bakker0a597072012-09-25 21:55:46 +000046#if defined(POLARSSL_SSL_CACHE_C)
47#include "polarssl/ssl_cache.h"
48#endif
49
Paul Bakker5690efc2011-05-26 13:16:06 +000050#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000051 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +020052 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
53 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020054 !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000055int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000056{
Paul Bakkercce9d772011-11-18 14:26:47 +000057 ((void) argc);
58 ((void) argv);
59
Paul Bakker508ad5a2011-12-04 17:09:26 +000060 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakker5690efc2011-05-26 13:16:06 +000061 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000062 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020063 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
64 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000065 return( 0 );
66}
67#else
Paul Bakker9a97c5d2013-09-15 17:07:33 +020068
69#define HTTP_RESPONSE \
70 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000071 "<h2>mbed TLS Test Server</h2>\r\n" \
Paul Bakker9a97c5d2013-09-15 17:07:33 +020072 "<p>Successful connection using: %s</p>\r\n"
73
74#define DEBUG_LEVEL 0
75
76static void my_debug( void *ctx, int level, const char *str )
77{
Paul Bakkerc73079a2014-04-25 16:34:30 +020078 ((void) level);
79
80 fprintf( (FILE *) ctx, "%s", str );
81 fflush( (FILE *) ctx );
Paul Bakker9a97c5d2013-09-15 17:07:33 +020082}
83
Paul Bakkercce9d772011-11-18 14:26:47 +000084int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
86 int ret, len;
87 int listen_fd;
Paul Bakker7eb013f2011-10-06 12:37:39 +000088 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +000089 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020090 const char *pers = "ssl_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Paul Bakker508ad5a2011-12-04 17:09:26 +000092 entropy_context entropy;
93 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000094 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020095 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +020096 pk_context pkey;
Paul Bakkerd4324102012-09-26 08:29:38 +000097#if defined(POLARSSL_SSL_CACHE_C)
98 ssl_cache_context cache;
99#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Paul Bakkercce9d772011-11-18 14:26:47 +0000101 ((void) argc);
102 ((void) argv);
103
Paul Bakker0c226102014-04-17 16:02:36 +0200104 memset( &ssl, 0, sizeof(ssl_context) );
Paul Bakker0a597072012-09-25 21:55:46 +0000105#if defined(POLARSSL_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +0000106 ssl_cache_init( &cache );
107#endif
Paul Bakker0c226102014-04-17 16:02:36 +0200108 x509_crt_init( &srvcert );
109 pk_init( &pkey );
110 entropy_init( &entropy );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000111
Paul Bakkerc73079a2014-04-25 16:34:30 +0200112#if defined(POLARSSL_DEBUG_C)
113 debug_set_threshold( DEBUG_LEVEL );
114#endif
115
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 /*
117 * 1. Load the certificates and private RSA key
118 */
119 printf( "\n . Loading the server cert. and key..." );
120 fflush( stdout );
121
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 /*
123 * This demonstration program uses embedded test certificates.
Paul Bakkerddf26b42013-09-18 13:46:23 +0200124 * 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 Bakker5121ce52009-01-03 21:22:43 +0000126 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200127 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
128 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 if( ret != 0 )
130 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200131 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 goto exit;
133 }
134
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200135 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
136 strlen( test_ca_list ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 if( ret != 0 )
138 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200139 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 goto exit;
141 }
142
Paul Bakker1a7550a2013-09-15 13:01:22 +0200143 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
144 strlen( test_srv_key ), NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 if( ret != 0 )
146 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 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 Bakker508ad5a2011-12-04 17:09:26 +0000168 * 3. Seed the RNG
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000170 printf( " . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 fflush( stdout );
172
Paul Bakker508ad5a2011-12-04 17:09:26 +0000173 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200174 (const unsigned char *) pers,
175 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000176 {
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 Bakker5121ce52009-01-03 21:22:43 +0000188
189 if( ( ret = ssl_init( &ssl ) ) != 0 )
190 {
191 printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000192 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 }
194
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
196 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
197
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100198 /* 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é-Gonnardfa065812015-01-12 14:05:33 +0100200 /* RC4 is deprecated, disable it */
201 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100202
Paul Bakker508ad5a2011-12-04 17:09:26 +0000203 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000205
Paul Bakker0a597072012-09-25 21:55:46 +0000206#if defined(POLARSSL_SSL_CACHE_C)
207 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
208 ssl_cache_set, &cache );
209#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Paul Bakker40ea7de2009-05-03 10:18:48 +0000211 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200212 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 Bakker5121ce52009-01-03 21:22:43 +0000217
Paul Bakker7eb013f2011-10-06 12:37:39 +0000218 printf( " ok\n" );
219
220reset:
Paul Bakkera3d195c2011-11-27 21:07:34 +0000221#ifdef POLARSSL_ERROR_C
222 if( ret != 0 )
223 {
224 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200225 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000226 printf("Last error was: %d - %s\n\n", ret, error_buf );
227 }
228#endif
229
Paul Bakker7eb013f2011-10-06 12:37:39 +0000230 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 Bakker7eb013f2011-10-06 12:37:39 +0000238 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 Bakker5121ce52009-01-03 21:22:43 +0000254 /*
255 * 5. Handshake
256 */
257 printf( " . Performing the SSL/TLS handshake..." );
258 fflush( stdout );
259
260 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
261 {
Paul Bakker831a7552011-05-18 13:32:51 +0000262 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 {
264 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000265 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 }
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 Bakker831a7552011-05-18 13:32:51 +0000283 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 continue;
285
286 if( ret <= 0 )
287 {
288 switch( ret )
289 {
Paul Bakker40e46942009-01-03 21:51:57 +0000290 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 printf( " connection was closed gracefully\n" );
292 break;
293
Paul Bakker40e46942009-01-03 21:51:57 +0000294 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 printf( " connection was reset by peer\n" );
296 break;
297
298 default:
Paul Bakker6f3578c2012-04-16 06:46:01 +0000299 printf( " ssl_read returned -0x%x\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 break;
301 }
302
303 break;
304 }
305
306 len = ret;
307 printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker48916f92012-09-16 19:57:18 +0000308
309 if( ret > 0 )
310 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 }
Paul Bakker48916f92012-09-16 19:57:18 +0000312 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
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 Bakkere3166ce2011-01-27 17:40:50 +0000321 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
324 {
Paul Bakker40e46942009-01-03 21:51:57 +0000325 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 {
327 printf( " failed\n ! peer closed the connection\n\n" );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000328 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 }
330
Paul Bakker831a7552011-05-18 13:32:51 +0000331 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 {
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é-Gonnard6b0d2682014-03-25 11:24:43 +0100340
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 Bakkera3d195c2011-11-27 21:07:34 +0000355 ret = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +0000356 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358exit:
359
Paul Bakkera3d195c2011-11-27 21:07:34 +0000360#ifdef POLARSSL_ERROR_C
361 if( ret != 0 )
362 {
363 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200364 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000365 printf("Last error was: %d - %s\n\n", ret, error_buf );
366 }
367#endif
368
Paul Bakker0c226102014-04-17 16:02:36 +0200369 if( client_fd != -1 )
370 net_close( client_fd );
371
Paul Bakker36713e82013-09-17 13:25:29 +0200372 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200373 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 ssl_free( &ssl );
Paul Bakker0a597072012-09-25 21:55:46 +0000375#if defined(POLARSSL_SSL_CACHE_C)
376 ssl_cache_free( &cache );
377#endif
Paul Bakkera317a982014-06-18 16:44:11 +0200378 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200379 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
Paul Bakkercce9d772011-11-18 14:26:47 +0000381#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 printf( " Press Enter to exit this program.\n" );
383 fflush( stdout ); getchar();
384#endif
385
386 return( ret );
387}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000388#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000389 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000390 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */