blob: 2a528cb078a55b9b1ed4ec53208e3b7e06b3808f [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é-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakkercce9d772011-11-18 14:26:47 +000028#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000029#include <windows.h>
30#endif
31
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.h>
35
Paul Bakker508ad5a2011-12-04 17:09:26 +000036#include "polarssl/entropy.h"
37#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/certs.h"
39#include "polarssl/x509.h"
40#include "polarssl/ssl.h"
41#include "polarssl/net.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000042#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Paul Bakker0a597072012-09-25 21:55:46 +000044#if defined(POLARSSL_SSL_CACHE_C)
45#include "polarssl/ssl_cache.h"
46#endif
47
Paul Bakker5690efc2011-05-26 13:16:06 +000048#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000049 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +020050 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
51 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020052 !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000053int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000054{
Paul Bakkercce9d772011-11-18 14:26:47 +000055 ((void) argc);
56 ((void) argv);
57
Paul Bakker508ad5a2011-12-04 17:09:26 +000058 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakker5690efc2011-05-26 13:16:06 +000059 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000060 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020061 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
62 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000063 return( 0 );
64}
65#else
Paul Bakker9a97c5d2013-09-15 17:07:33 +020066
67#define HTTP_RESPONSE \
68 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
69 "<h2>PolarSSL Test Server</h2>\r\n" \
70 "<p>Successful connection using: %s</p>\r\n"
71
72#define DEBUG_LEVEL 0
73
74static void my_debug( void *ctx, int level, const char *str )
75{
76 if( level < DEBUG_LEVEL )
77 {
78 fprintf( (FILE *) ctx, "%s", str );
79 fflush( (FILE *) ctx );
80 }
81}
82
Paul Bakkercce9d772011-11-18 14:26:47 +000083int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000084{
85 int ret, len;
86 int listen_fd;
Paul Bakker7eb013f2011-10-06 12:37:39 +000087 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +000088 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020089 const char *pers = "ssl_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Bakker508ad5a2011-12-04 17:09:26 +000091 entropy_context entropy;
92 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000093 ssl_context ssl;
Paul Bakkerc559c7a2013-09-18 14:13:26 +020094 x509_crt srvcert;
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +020095 pk_context pkey;
Paul Bakkerd4324102012-09-26 08:29:38 +000096#if defined(POLARSSL_SSL_CACHE_C)
97 ssl_cache_context cache;
98#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Paul Bakkercce9d772011-11-18 14:26:47 +0000100 ((void) argc);
101 ((void) argv);
102
Paul Bakker0a597072012-09-25 21:55:46 +0000103#if defined(POLARSSL_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +0000104 ssl_cache_init( &cache );
105#endif
Paul Bakkerfab5c822012-02-06 16:45:10 +0000106
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 /*
108 * 1. Load the certificates and private RSA key
109 */
110 printf( "\n . Loading the server cert. and key..." );
111 fflush( stdout );
112
Paul Bakker369d2eb2013-09-18 11:58:25 +0200113 x509_crt_init( &srvcert );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 /*
116 * This demonstration program uses embedded test certificates.
Paul Bakkerddf26b42013-09-18 13:46:23 +0200117 * Instead, you may want to use x509_crt_parse_file() to read the
118 * server and CA certificates, as well as pk_parse_keyfile().
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Paul Bakkerddf26b42013-09-18 13:46:23 +0200120 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
121 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 if( ret != 0 )
123 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200124 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 goto exit;
126 }
127
Manuel Pégourié-Gonnard641de712013-09-25 13:23:33 +0200128 ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
129 strlen( test_ca_list ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 if( ret != 0 )
131 {
Paul Bakkerddf26b42013-09-18 13:46:23 +0200132 printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 goto exit;
134 }
135
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200136 pk_init( &pkey );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
138 strlen( test_srv_key ), NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 if( ret != 0 )
140 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 goto exit;
143 }
144
145 printf( " ok\n" );
146
147 /*
148 * 2. Setup the listening TCP socket
149 */
150 printf( " . Bind on https://localhost:4433/ ..." );
151 fflush( stdout );
152
153 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
154 {
155 printf( " failed\n ! net_bind returned %d\n\n", ret );
156 goto exit;
157 }
158
159 printf( " ok\n" );
160
161 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000162 * 3. Seed the RNG
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000164 printf( " . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 fflush( stdout );
166
Paul Bakker508ad5a2011-12-04 17:09:26 +0000167 entropy_init( &entropy );
168 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200169 (const unsigned char *) pers,
170 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000171 {
172 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
173 goto exit;
174 }
175
176 printf( " ok\n" );
177
178 /*
179 * 4. Setup stuff
180 */
181 printf( " . Setting up the SSL data...." );
182 fflush( stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184 if( ( ret = ssl_init( &ssl ) ) != 0 )
185 {
186 printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000187 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 }
189
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
191 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
192
Paul Bakker508ad5a2011-12-04 17:09:26 +0000193 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000195
Paul Bakker0a597072012-09-25 21:55:46 +0000196#if defined(POLARSSL_SSL_CACHE_C)
197 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
198 ssl_cache_set, &cache );
199#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Paul Bakker40ea7de2009-05-03 10:18:48 +0000201 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200202 ssl_set_own_cert( &ssl, &srvcert, &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
Paul Bakker7eb013f2011-10-06 12:37:39 +0000204 printf( " ok\n" );
205
206reset:
Paul Bakkera3d195c2011-11-27 21:07:34 +0000207#ifdef POLARSSL_ERROR_C
208 if( ret != 0 )
209 {
210 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200211 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000212 printf("Last error was: %d - %s\n\n", ret, error_buf );
213 }
214#endif
215
Paul Bakker7eb013f2011-10-06 12:37:39 +0000216 if( client_fd != -1 )
217 net_close( client_fd );
218
219 ssl_session_reset( &ssl );
220
221 /*
222 * 3. Wait until a client connects
223 */
Paul Bakkercce9d772011-11-18 14:26:47 +0000224#if defined(_WIN32_WCE)
225 {
226 SHELLEXECUTEINFO sei;
227
228 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
229
230 sei.cbSize = sizeof( SHELLEXECUTEINFO );
231 sei.fMask = 0;
232 sei.hwnd = 0;
233 sei.lpVerb = _T( "open" );
234 sei.lpFile = _T( "https://localhost:4433/" );
235 sei.lpParameters = NULL;
236 sei.lpDirectory = NULL;
237 sei.nShow = SW_SHOWNORMAL;
238
239 ShellExecuteEx( &sei );
240 }
241#elif defined(_WIN32)
Paul Bakker7eb013f2011-10-06 12:37:39 +0000242 ShellExecute( NULL, "open", "https://localhost:4433/",
243 NULL, NULL, SW_SHOWNORMAL );
244#endif
245
246 client_fd = -1;
247
248 printf( " . Waiting for a remote connection ..." );
249 fflush( stdout );
250
251 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
252 {
253 printf( " failed\n ! net_accept returned %d\n\n", ret );
254 goto exit;
255 }
256
257 ssl_set_bio( &ssl, net_recv, &client_fd,
258 net_send, &client_fd );
259
260 printf( " ok\n" );
261
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 /*
263 * 5. Handshake
264 */
265 printf( " . Performing the SSL/TLS handshake..." );
266 fflush( stdout );
267
268 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
269 {
Paul Bakker831a7552011-05-18 13:32:51 +0000270 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 {
272 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000273 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 }
275 }
276
277 printf( " ok\n" );
278
279 /*
280 * 6. Read the HTTP Request
281 */
282 printf( " < Read from client:" );
283 fflush( stdout );
284
285 do
286 {
287 len = sizeof( buf ) - 1;
288 memset( buf, 0, sizeof( buf ) );
289 ret = ssl_read( &ssl, buf, len );
290
Paul Bakker831a7552011-05-18 13:32:51 +0000291 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 continue;
293
294 if( ret <= 0 )
295 {
296 switch( ret )
297 {
Paul Bakker40e46942009-01-03 21:51:57 +0000298 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 printf( " connection was closed gracefully\n" );
300 break;
301
Paul Bakker40e46942009-01-03 21:51:57 +0000302 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 printf( " connection was reset by peer\n" );
304 break;
305
306 default:
Paul Bakker6f3578c2012-04-16 06:46:01 +0000307 printf( " ssl_read returned -0x%x\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 break;
309 }
310
311 break;
312 }
313
314 len = ret;
315 printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker48916f92012-09-16 19:57:18 +0000316
317 if( ret > 0 )
318 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 }
Paul Bakker48916f92012-09-16 19:57:18 +0000320 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
322 /*
323 * 7. Write the 200 Response
324 */
325 printf( " > Write to client:" );
326 fflush( stdout );
327
328 len = sprintf( (char *) buf, HTTP_RESPONSE,
Paul Bakkere3166ce2011-01-27 17:40:50 +0000329 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
332 {
Paul Bakker40e46942009-01-03 21:51:57 +0000333 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 {
335 printf( " failed\n ! peer closed the connection\n\n" );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000336 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 }
338
Paul Bakker831a7552011-05-18 13:32:51 +0000339 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 {
341 printf( " failed\n ! ssl_write returned %d\n\n", ret );
342 goto exit;
343 }
344 }
345
346 len = ret;
347 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000348
Paul Bakkera3d195c2011-11-27 21:07:34 +0000349 ret = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +0000350 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
352exit:
353
Paul Bakkera3d195c2011-11-27 21:07:34 +0000354#ifdef POLARSSL_ERROR_C
355 if( ret != 0 )
356 {
357 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200358 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000359 printf("Last error was: %d - %s\n\n", ret, error_buf );
360 }
361#endif
362
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 net_close( client_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200364 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200365 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 ssl_free( &ssl );
Paul Bakker0a597072012-09-25 21:55:46 +0000367#if defined(POLARSSL_SSL_CACHE_C)
368 ssl_cache_free( &cache );
369#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
Paul Bakkercce9d772011-11-18 14:26:47 +0000371#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 printf( " Press Enter to exit this program.\n" );
373 fflush( stdout ); getchar();
374#endif
375
376 return( ret );
377}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000378#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000379 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000380 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */