blob: 38fa2f2635861213cd321d6458866f6e43c406ec [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
Paul Bakkercce9d772011-11-18 14:26:47 +000030#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000031#include <windows.h>
32#endif
33
34#include <string.h>
35#include <stdlib.h>
36#include <stdio.h>
37
Paul Bakker5690efc2011-05-26 13:16:06 +000038#include "polarssl/config.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 Bakker5121ce52009-01-03 21:22:43 +000047
Paul Bakker0a597072012-09-25 21:55:46 +000048#if defined(POLARSSL_SSL_CACHE_C)
49#include "polarssl/ssl_cache.h"
50#endif
51
Paul Bakker5690efc2011-05-26 13:16:06 +000052#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000053 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +020054 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
55 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
Paul Bakker36713e82013-09-17 13:25:29 +020056 !defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000057int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000058{
Paul Bakkercce9d772011-11-18 14:26:47 +000059 ((void) argc);
60 ((void) argv);
61
Paul Bakker508ad5a2011-12-04 17:09:26 +000062 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakker5690efc2011-05-26 13:16:06 +000063 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000064 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker36713e82013-09-17 13:25:29 +020065 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C "
66 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000067 return( 0 );
68}
69#else
Paul Bakker9a97c5d2013-09-15 17:07:33 +020070
71#define HTTP_RESPONSE \
72 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
73 "<h2>PolarSSL Test Server</h2>\r\n" \
74 "<p>Successful connection using: %s</p>\r\n"
75
76#define DEBUG_LEVEL 0
77
78static void my_debug( void *ctx, int level, const char *str )
79{
80 if( level < DEBUG_LEVEL )
81 {
82 fprintf( (FILE *) ctx, "%s", str );
83 fflush( (FILE *) ctx );
84 }
85}
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 Bakker5121ce52009-01-03 21:22:43 +000098 x509_cert 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 Bakker0a597072012-09-25 21:55:46 +0000107#if defined(POLARSSL_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +0000108 ssl_cache_init( &cache );
109#endif
Paul Bakkerfab5c822012-02-06 16:45:10 +0000110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 /*
112 * 1. Load the certificates and private RSA key
113 */
114 printf( "\n . Loading the server cert. and key..." );
115 fflush( stdout );
116
117 memset( &srvcert, 0, sizeof( x509_cert ) );
118
119 /*
120 * This demonstration program uses embedded test certificates.
121 * Instead, you may want to use x509parse_crtfile() to read the
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200122 * server and CA certificates, as well as x509parse_keyfile().
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200124 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000125 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 if( ret != 0 )
127 {
128 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
129 goto exit;
130 }
131
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200132 ret = x509parse_crt( &srvcert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000133 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 if( ret != 0 )
135 {
136 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
137 goto exit;
138 }
139
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200140 pk_init( &pkey );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141 ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
142 strlen( test_srv_key ), NULL, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 if( ret != 0 )
144 {
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145 printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 goto exit;
147 }
148
149 printf( " ok\n" );
150
151 /*
152 * 2. Setup the listening TCP socket
153 */
154 printf( " . Bind on https://localhost:4433/ ..." );
155 fflush( stdout );
156
157 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
158 {
159 printf( " failed\n ! net_bind returned %d\n\n", ret );
160 goto exit;
161 }
162
163 printf( " ok\n" );
164
165 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000166 * 3. Seed the RNG
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000168 printf( " . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 fflush( stdout );
170
Paul Bakker508ad5a2011-12-04 17:09:26 +0000171 entropy_init( &entropy );
172 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200173 (const unsigned char *) pers,
174 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000175 {
176 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
177 goto exit;
178 }
179
180 printf( " ok\n" );
181
182 /*
183 * 4. Setup stuff
184 */
185 printf( " . Setting up the SSL data...." );
186 fflush( stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
188 if( ( ret = ssl_init( &ssl ) ) != 0 )
189 {
190 printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000191 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 }
193
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
195 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
196
Paul Bakker508ad5a2011-12-04 17:09:26 +0000197 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000199
Paul Bakker0a597072012-09-25 21:55:46 +0000200#if defined(POLARSSL_SSL_CACHE_C)
201 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
202 ssl_cache_set, &cache );
203#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Paul Bakker40ea7de2009-05-03 10:18:48 +0000205 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200206 ssl_set_own_cert( &ssl, &srvcert, &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Paul Bakker7eb013f2011-10-06 12:37:39 +0000208 printf( " ok\n" );
209
210reset:
Paul Bakkera3d195c2011-11-27 21:07:34 +0000211#ifdef POLARSSL_ERROR_C
212 if( ret != 0 )
213 {
214 char error_buf[100];
Paul Bakker03a8a792013-06-30 12:18:08 +0200215 polarssl_strerror( ret, error_buf, 100 );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000216 printf("Last error was: %d - %s\n\n", ret, error_buf );
217 }
218#endif
219
Paul Bakker7eb013f2011-10-06 12:37:39 +0000220 if( client_fd != -1 )
221 net_close( client_fd );
222
223 ssl_session_reset( &ssl );
224
225 /*
226 * 3. Wait until a client connects
227 */
Paul Bakkercce9d772011-11-18 14:26:47 +0000228#if defined(_WIN32_WCE)
229 {
230 SHELLEXECUTEINFO sei;
231
232 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
233
234 sei.cbSize = sizeof( SHELLEXECUTEINFO );
235 sei.fMask = 0;
236 sei.hwnd = 0;
237 sei.lpVerb = _T( "open" );
238 sei.lpFile = _T( "https://localhost:4433/" );
239 sei.lpParameters = NULL;
240 sei.lpDirectory = NULL;
241 sei.nShow = SW_SHOWNORMAL;
242
243 ShellExecuteEx( &sei );
244 }
245#elif defined(_WIN32)
Paul Bakker7eb013f2011-10-06 12:37:39 +0000246 ShellExecute( NULL, "open", "https://localhost:4433/",
247 NULL, NULL, SW_SHOWNORMAL );
248#endif
249
250 client_fd = -1;
251
252 printf( " . Waiting for a remote connection ..." );
253 fflush( stdout );
254
255 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
256 {
257 printf( " failed\n ! net_accept returned %d\n\n", ret );
258 goto exit;
259 }
260
261 ssl_set_bio( &ssl, net_recv, &client_fd,
262 net_send, &client_fd );
263
264 printf( " ok\n" );
265
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 /*
267 * 5. Handshake
268 */
269 printf( " . Performing the SSL/TLS handshake..." );
270 fflush( stdout );
271
272 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
273 {
Paul Bakker831a7552011-05-18 13:32:51 +0000274 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 {
276 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000277 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 }
279 }
280
281 printf( " ok\n" );
282
283 /*
284 * 6. Read the HTTP Request
285 */
286 printf( " < Read from client:" );
287 fflush( stdout );
288
289 do
290 {
291 len = sizeof( buf ) - 1;
292 memset( buf, 0, sizeof( buf ) );
293 ret = ssl_read( &ssl, buf, len );
294
Paul Bakker831a7552011-05-18 13:32:51 +0000295 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 continue;
297
298 if( ret <= 0 )
299 {
300 switch( ret )
301 {
Paul Bakker40e46942009-01-03 21:51:57 +0000302 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 printf( " connection was closed gracefully\n" );
304 break;
305
Paul Bakker40e46942009-01-03 21:51:57 +0000306 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 printf( " connection was reset by peer\n" );
308 break;
309
310 default:
Paul Bakker6f3578c2012-04-16 06:46:01 +0000311 printf( " ssl_read returned -0x%x\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 break;
313 }
314
315 break;
316 }
317
318 len = ret;
319 printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker48916f92012-09-16 19:57:18 +0000320
321 if( ret > 0 )
322 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 }
Paul Bakker48916f92012-09-16 19:57:18 +0000324 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 /*
327 * 7. Write the 200 Response
328 */
329 printf( " > Write to client:" );
330 fflush( stdout );
331
332 len = sprintf( (char *) buf, HTTP_RESPONSE,
Paul Bakkere3166ce2011-01-27 17:40:50 +0000333 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
335 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
336 {
Paul Bakker40e46942009-01-03 21:51:57 +0000337 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 {
339 printf( " failed\n ! peer closed the connection\n\n" );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000340 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 }
342
Paul Bakker831a7552011-05-18 13:32:51 +0000343 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 {
345 printf( " failed\n ! ssl_write returned %d\n\n", ret );
346 goto exit;
347 }
348 }
349
350 len = ret;
351 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000352
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 Bakker5121ce52009-01-03 21:22:43 +0000367 net_close( client_fd );
Paul Bakker36713e82013-09-17 13:25:29 +0200368 x509_crt_free( &srvcert );
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +0200369 pk_free( &pkey );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 ssl_free( &ssl );
Paul Bakker0a597072012-09-25 21:55:46 +0000371#if defined(POLARSSL_SSL_CACHE_C)
372 ssl_cache_free( &cache );
373#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
Paul Bakkercce9d772011-11-18 14:26:47 +0000375#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 printf( " Press Enter to exit this program.\n" );
377 fflush( stdout ); getchar();
378#endif
379
380 return( ret );
381}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000382#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000383 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000384 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */