blob: a8411591fda81e250b13d7ebd39d0f9295c86513 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL server demonstration program
3 *
Paul Bakker3c5ef712013-06-25 16:37:45 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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é-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakkercce9d772011-11-18 14:26:47 +000032#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000033#include <windows.h>
34#endif
35
36#include <string.h>
37#include <stdlib.h>
38#include <stdio.h>
39
Paul Bakker508ad5a2011-12-04 17:09:26 +000040#include "polarssl/entropy.h"
41#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000042#include "polarssl/certs.h"
43#include "polarssl/x509.h"
44#include "polarssl/ssl.h"
45#include "polarssl/net.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000046#include "polarssl/error.h"
Paul Bakkerc73079a2014-04-25 16:34:30 +020047#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Paul Bakker0a597072012-09-25 21:55:46 +000049#if defined(POLARSSL_SSL_CACHE_C)
50#include "polarssl/ssl_cache.h"
51#endif
52
Paul Bakker5690efc2011-05-26 13:16:06 +000053#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000054 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +020055 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
56 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020057 !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000058int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000059{
Paul Bakkercce9d772011-11-18 14:26:47 +000060 ((void) argc);
61 ((void) argv);
62
Paul Bakker508ad5a2011-12-04 17:09:26 +000063 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakker5690efc2011-05-26 13:16:06 +000064 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000065 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020066 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
67 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000068 return( 0 );
69}
70#else
Paul Bakker9a97c5d2013-09-15 17:07:33 +020071
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
79static void my_debug( void *ctx, int level, const char *str )
80{
Paul Bakkerc73079a2014-04-25 16:34:30 +020081 ((void) level);
82
83 fprintf( (FILE *) ctx, "%s", str );
84 fflush( (FILE *) ctx );
Paul Bakker9a97c5d2013-09-15 17:07:33 +020085}
86
Paul Bakkercce9d772011-11-18 14:26:47 +000087int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000088{
89 int ret, len;
90 int listen_fd;
Paul Bakker7eb013f2011-10-06 12:37:39 +000091 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +000092 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020093 const char *pers = "ssl_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Paul Bakker508ad5a2011-12-04 17:09:26 +000095 entropy_context entropy;
96 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000097 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020098 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +020099 pk_context pkey;
Paul Bakkerd4324102012-09-26 08:29:38 +0000100#if defined(POLARSSL_SSL_CACHE_C)
101 ssl_cache_context cache;
102#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Paul Bakkercce9d772011-11-18 14:26:47 +0000104 ((void) argc);
105 ((void) argv);
106
Paul Bakker0c226102014-04-17 16:02:36 +0200107 memset( &ssl, 0, sizeof(ssl_context) );
Paul Bakker0a597072012-09-25 21:55:46 +0000108#if defined(POLARSSL_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +0000109 ssl_cache_init( &cache );
110#endif
Paul Bakker0c226102014-04-17 16:02:36 +0200111 x509_crt_init( &srvcert );
112 pk_init( &pkey );
113 entropy_init( &entropy );
Paul Bakkerfab5c822012-02-06 16:45:10 +0000114
Paul Bakkerc73079a2014-04-25 16:34:30 +0200115#if defined(POLARSSL_DEBUG_C)
116 debug_set_threshold( DEBUG_LEVEL );
117#endif
118
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 /*
120 * 1. Load the certificates and private RSA key
121 */
122 printf( "\n . Loading the server cert. and key..." );
123 fflush( stdout );
124
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 /*
126 * This demonstration program uses embedded test certificates.
Paul Bakkerddf26b42013-09-18 13:46:23 +0200127 * 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 Bakker5121ce52009-01-03 21:22:43 +0000129 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200130 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
131 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 if( ret != 0 )
133 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200134 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200138 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
139 strlen( test_ca_list ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 if( ret != 0 )
141 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200142 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 goto exit;
144 }
145
Paul Bakker1a7550a2013-09-15 13:01:22 +0200146 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
147 strlen( test_srv_key ), NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 if( ret != 0 )
149 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 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 Bakker508ad5a2011-12-04 17:09:26 +0000171 * 3. Seed the RNG
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000173 printf( " . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 fflush( stdout );
175
Paul Bakker508ad5a2011-12-04 17:09:26 +0000176 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200177 (const unsigned char *) pers,
178 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000179 {
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 Bakker5121ce52009-01-03 21:22:43 +0000191
192 if( ( ret = ssl_init( &ssl ) ) != 0 )
193 {
194 printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000195 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 }
197
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
199 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
200
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100201 /* SSLv3 is deprecated, set minimum to TLS 1.0 */
202 ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1 );
Manuel Pégourié-Gonnardfa065812015-01-12 14:05:33 +0100203 /* RC4 is deprecated, disable it */
204 ssl_set_arc4_support( &ssl, SSL_ARC4_DISABLED );
Manuel Pégourié-Gonnard448ea502015-01-12 11:40:14 +0100205
Paul Bakker508ad5a2011-12-04 17:09:26 +0000206 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000208
Paul Bakker0a597072012-09-25 21:55:46 +0000209#if defined(POLARSSL_SSL_CACHE_C)
210 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
211 ssl_cache_set, &cache );
212#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Paul Bakker40ea7de2009-05-03 10:18:48 +0000214 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200215 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
216 {
217 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
218 goto exit;
219 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Paul Bakker7eb013f2011-10-06 12:37:39 +0000221 printf( " ok\n" );
222
223reset:
Paul Bakkera3d195c2011-11-27 21:07:34 +0000224#ifdef POLARSSL_ERROR_C
225 if( ret != 0 )
226 {
227 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200228 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000229 printf("Last error was: %d - %s\n\n", ret, error_buf );
230 }
231#endif
232
Paul Bakker7eb013f2011-10-06 12:37:39 +0000233 if( client_fd != -1 )
234 net_close( client_fd );
235
236 ssl_session_reset( &ssl );
237
238 /*
239 * 3. Wait until a client connects
240 */
Paul Bakker7eb013f2011-10-06 12:37:39 +0000241 client_fd = -1;
242
243 printf( " . Waiting for a remote connection ..." );
244 fflush( stdout );
245
246 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
247 {
248 printf( " failed\n ! net_accept returned %d\n\n", ret );
249 goto exit;
250 }
251
252 ssl_set_bio( &ssl, net_recv, &client_fd,
253 net_send, &client_fd );
254
255 printf( " ok\n" );
256
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 /*
258 * 5. Handshake
259 */
260 printf( " . Performing the SSL/TLS handshake..." );
261 fflush( stdout );
262
263 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
264 {
Paul Bakker831a7552011-05-18 13:32:51 +0000265 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 {
267 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000268 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 }
270 }
271
272 printf( " ok\n" );
273
274 /*
275 * 6. Read the HTTP Request
276 */
277 printf( " < Read from client:" );
278 fflush( stdout );
279
280 do
281 {
282 len = sizeof( buf ) - 1;
283 memset( buf, 0, sizeof( buf ) );
284 ret = ssl_read( &ssl, buf, len );
285
Paul Bakker831a7552011-05-18 13:32:51 +0000286 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 continue;
288
289 if( ret <= 0 )
290 {
291 switch( ret )
292 {
Paul Bakker40e46942009-01-03 21:51:57 +0000293 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 printf( " connection was closed gracefully\n" );
295 break;
296
Paul Bakker40e46942009-01-03 21:51:57 +0000297 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 printf( " connection was reset by peer\n" );
299 break;
300
301 default:
Paul Bakker6f3578c2012-04-16 06:46:01 +0000302 printf( " ssl_read returned -0x%x\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 break;
304 }
305
306 break;
307 }
308
309 len = ret;
310 printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker48916f92012-09-16 19:57:18 +0000311
312 if( ret > 0 )
313 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 }
Paul Bakker48916f92012-09-16 19:57:18 +0000315 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
317 /*
318 * 7. Write the 200 Response
319 */
320 printf( " > Write to client:" );
321 fflush( stdout );
322
323 len = sprintf( (char *) buf, HTTP_RESPONSE,
Paul Bakkere3166ce2011-01-27 17:40:50 +0000324 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
327 {
Paul Bakker40e46942009-01-03 21:51:57 +0000328 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 {
330 printf( " failed\n ! peer closed the connection\n\n" );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000331 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 }
333
Paul Bakker831a7552011-05-18 13:32:51 +0000334 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 {
336 printf( " failed\n ! ssl_write returned %d\n\n", ret );
337 goto exit;
338 }
339 }
340
341 len = ret;
342 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100343
344 printf( " . Closing the connection..." );
345
346 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
347 {
348 if( ret != POLARSSL_ERR_NET_WANT_READ &&
349 ret != POLARSSL_ERR_NET_WANT_WRITE )
350 {
351 printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
352 goto reset;
353 }
354 }
355
356 printf( " ok\n" );
357
Paul Bakkera3d195c2011-11-27 21:07:34 +0000358 ret = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +0000359 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
361exit:
362
Paul Bakkera3d195c2011-11-27 21:07:34 +0000363#ifdef POLARSSL_ERROR_C
364 if( ret != 0 )
365 {
366 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200367 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000368 printf("Last error was: %d - %s\n\n", ret, error_buf );
369 }
370#endif
371
Paul Bakker0c226102014-04-17 16:02:36 +0200372 if( client_fd != -1 )
373 net_close( client_fd );
374
Paul Bakker36713e82013-09-17 13:25:29 +0200375 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200376 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 ssl_free( &ssl );
Paul Bakker0a597072012-09-25 21:55:46 +0000378#if defined(POLARSSL_SSL_CACHE_C)
379 ssl_cache_free( &cache );
380#endif
Paul Bakkera317a982014-06-18 16:44:11 +0200381 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200382 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
Paul Bakkercce9d772011-11-18 14:26:47 +0000384#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 printf( " Press Enter to exit this program.\n" );
386 fflush( stdout ); getchar();
387#endif
388
389 return( ret );
390}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000391#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000392 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000393 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */