blob: 962b0985cc8e8d42a114e2a95cd70ce755c21630 [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
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100162 if( ( ret = net_bind( &listen_fd, NULL, 4433, NET_PROTO_TCP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 {
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
Paul Bakker508ad5a2011-12-04 17:09:26 +0000201 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000203
Paul Bakker0a597072012-09-25 21:55:46 +0000204#if defined(POLARSSL_SSL_CACHE_C)
205 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
206 ssl_cache_set, &cache );
207#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
Paul Bakker40ea7de2009-05-03 10:18:48 +0000209 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardc5fd3912014-07-08 14:05:52 +0200210 if( ( ret = ssl_set_own_cert( &ssl, &srvcert, &pkey ) ) != 0 )
211 {
212 printf( " failed\n ! ssl_set_own_cert returned %d\n\n", ret );
213 goto exit;
214 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Paul Bakker7eb013f2011-10-06 12:37:39 +0000216 printf( " ok\n" );
217
218reset:
Paul Bakkera3d195c2011-11-27 21:07:34 +0000219#ifdef POLARSSL_ERROR_C
220 if( ret != 0 )
221 {
222 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200223 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000224 printf("Last error was: %d - %s\n\n", ret, error_buf );
225 }
226#endif
227
Paul Bakker7eb013f2011-10-06 12:37:39 +0000228 if( client_fd != -1 )
229 net_close( client_fd );
230
231 ssl_session_reset( &ssl );
232
233 /*
234 * 3. Wait until a client connects
235 */
Paul Bakker7eb013f2011-10-06 12:37:39 +0000236 client_fd = -1;
237
238 printf( " . Waiting for a remote connection ..." );
239 fflush( stdout );
240
241 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
242 {
243 printf( " failed\n ! net_accept returned %d\n\n", ret );
244 goto exit;
245 }
246
247 ssl_set_bio( &ssl, net_recv, &client_fd,
248 net_send, &client_fd );
249
250 printf( " ok\n" );
251
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 /*
253 * 5. Handshake
254 */
255 printf( " . Performing the SSL/TLS handshake..." );
256 fflush( stdout );
257
258 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
259 {
Paul Bakker831a7552011-05-18 13:32:51 +0000260 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 {
262 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000263 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 }
265 }
266
267 printf( " ok\n" );
268
269 /*
270 * 6. Read the HTTP Request
271 */
272 printf( " < Read from client:" );
273 fflush( stdout );
274
275 do
276 {
277 len = sizeof( buf ) - 1;
278 memset( buf, 0, sizeof( buf ) );
279 ret = ssl_read( &ssl, buf, len );
280
Paul Bakker831a7552011-05-18 13:32:51 +0000281 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 continue;
283
284 if( ret <= 0 )
285 {
286 switch( ret )
287 {
Paul Bakker40e46942009-01-03 21:51:57 +0000288 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 printf( " connection was closed gracefully\n" );
290 break;
291
Paul Bakker40e46942009-01-03 21:51:57 +0000292 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 printf( " connection was reset by peer\n" );
294 break;
295
296 default:
Paul Bakker6f3578c2012-04-16 06:46:01 +0000297 printf( " ssl_read returned -0x%x\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 break;
299 }
300
301 break;
302 }
303
304 len = ret;
305 printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker48916f92012-09-16 19:57:18 +0000306
307 if( ret > 0 )
308 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 }
Paul Bakker48916f92012-09-16 19:57:18 +0000310 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312 /*
313 * 7. Write the 200 Response
314 */
315 printf( " > Write to client:" );
316 fflush( stdout );
317
318 len = sprintf( (char *) buf, HTTP_RESPONSE,
Paul Bakkere3166ce2011-01-27 17:40:50 +0000319 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
322 {
Paul Bakker40e46942009-01-03 21:51:57 +0000323 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 {
325 printf( " failed\n ! peer closed the connection\n\n" );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000326 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 }
328
Paul Bakker831a7552011-05-18 13:32:51 +0000329 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 {
331 printf( " failed\n ! ssl_write returned %d\n\n", ret );
332 goto exit;
333 }
334 }
335
336 len = ret;
337 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
Manuel Pégourié-Gonnard6b0d2682014-03-25 11:24:43 +0100338
339 printf( " . Closing the connection..." );
340
341 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
342 {
343 if( ret != POLARSSL_ERR_NET_WANT_READ &&
344 ret != POLARSSL_ERR_NET_WANT_WRITE )
345 {
346 printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
347 goto reset;
348 }
349 }
350
351 printf( " ok\n" );
352
Paul Bakkera3d195c2011-11-27 21:07:34 +0000353 ret = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +0000354 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
356exit:
357
Paul Bakkera3d195c2011-11-27 21:07:34 +0000358#ifdef POLARSSL_ERROR_C
359 if( ret != 0 )
360 {
361 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200362 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000363 printf("Last error was: %d - %s\n\n", ret, error_buf );
364 }
365#endif
366
Paul Bakker0c226102014-04-17 16:02:36 +0200367 if( client_fd != -1 )
368 net_close( client_fd );
369
Paul Bakker36713e82013-09-17 13:25:29 +0200370 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200371 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 ssl_free( &ssl );
Paul Bakker0a597072012-09-25 21:55:46 +0000373#if defined(POLARSSL_SSL_CACHE_C)
374 ssl_cache_free( &cache );
375#endif
Paul Bakkera317a982014-06-18 16:44:11 +0200376 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200377 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
Paul Bakkercce9d772011-11-18 14:26:47 +0000379#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 printf( " Press Enter to exit this program.\n" );
381 fflush( stdout ); getchar();
382#endif
383
384 return( ret );
385}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000386#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000387 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000388 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */