blob: bd6c38407650ccd9cef739bbd7e825d6879d2c75 [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 Bakker5121ce52009-01-03 21:22:43 +000052#define HTTP_RESPONSE \
53 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Paul Bakkereaca51d2010-08-16 12:00:14 +000054 "<h2>PolarSSL Test Server</h2>\r\n" \
55 "<p>Successful connection using: %s</p>\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Paul Bakker835b29e2012-08-23 08:31:59 +000057#define DEBUG_LEVEL 0
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker3c5ef712013-06-25 16:37:45 +020059static void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000060{
61 if( level < DEBUG_LEVEL )
62 {
63 fprintf( (FILE *) ctx, "%s", str );
64 fflush( (FILE *) ctx );
65 }
66}
67
Paul Bakker5690efc2011-05-26 13:16:06 +000068#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000069 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakkered27a042013-04-18 22:46:23 +020070 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
71 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
72 !defined(POLARSSL_X509_PARSE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000073int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000074{
Paul Bakkercce9d772011-11-18 14:26:47 +000075 ((void) argc);
76 ((void) argv);
77
Paul Bakker508ad5a2011-12-04 17:09:26 +000078 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakker5690efc2011-05-26 13:16:06 +000079 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000080 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakkered27a042013-04-18 22:46:23 +020081 "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_PARSE_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000082 return( 0 );
83}
84#else
Paul Bakkercce9d772011-11-18 14:26:47 +000085int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000086{
87 int ret, len;
88 int listen_fd;
Paul Bakker7eb013f2011-10-06 12:37:39 +000089 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +000090 unsigned char buf[1024];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020091 const char *pers = "ssl_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Paul Bakker508ad5a2011-12-04 17:09:26 +000093 entropy_context entropy;
94 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000095 ssl_context ssl;
Paul Bakker5121ce52009-01-03 21:22:43 +000096 x509_cert srvcert;
97 rsa_context rsa;
Paul Bakkerd4324102012-09-26 08:29:38 +000098#if defined(POLARSSL_SSL_CACHE_C)
99 ssl_cache_context cache;
100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Paul Bakkercce9d772011-11-18 14:26:47 +0000102 ((void) argc);
103 ((void) argv);
104
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 Bakkerfab5c822012-02-06 16:45:10 +0000108
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 /*
110 * 1. Load the certificates and private RSA key
111 */
112 printf( "\n . Loading the server cert. and key..." );
113 fflush( stdout );
114
115 memset( &srvcert, 0, sizeof( x509_cert ) );
116
117 /*
118 * This demonstration program uses embedded test certificates.
119 * Instead, you may want to use x509parse_crtfile() to read the
120 * server and CA certificates, as well as x509parse_keyfile().
121 */
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200122 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000123 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 if( ret != 0 )
125 {
126 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
127 goto exit;
128 }
129
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200130 ret = x509parse_crt( &srvcert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000131 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 if( ret != 0 )
133 {
134 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
135 goto exit;
136 }
137
Paul Bakker91b41592011-05-05 12:01:31 +0000138 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200139 ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key,
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 strlen( test_srv_key ), NULL, 0 );
141 if( ret != 0 )
142 {
143 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
144 goto exit;
145 }
146
147 printf( " ok\n" );
148
149 /*
150 * 2. Setup the listening TCP socket
151 */
152 printf( " . Bind on https://localhost:4433/ ..." );
153 fflush( stdout );
154
155 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
156 {
157 printf( " failed\n ! net_bind returned %d\n\n", ret );
158 goto exit;
159 }
160
161 printf( " ok\n" );
162
163 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000164 * 3. Seed the RNG
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000166 printf( " . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 fflush( stdout );
168
Paul Bakker508ad5a2011-12-04 17:09:26 +0000169 entropy_init( &entropy );
170 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200171 (const unsigned char *) pers,
172 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000173 {
174 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
175 goto exit;
176 }
177
178 printf( " ok\n" );
179
180 /*
181 * 4. Setup stuff
182 */
183 printf( " . Setting up the SSL data...." );
184 fflush( stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186 if( ( ret = ssl_init( &ssl ) ) != 0 )
187 {
188 printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000189 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 }
191
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
193 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
194
Paul Bakker508ad5a2011-12-04 17:09:26 +0000195 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000197
Paul Bakker0a597072012-09-25 21:55:46 +0000198#if defined(POLARSSL_SSL_CACHE_C)
199 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
200 ssl_cache_set, &cache );
201#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Paul Bakker40ea7de2009-05-03 10:18:48 +0000203 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
Paul Bakker7eb013f2011-10-06 12:37:39 +0000206 printf( " ok\n" );
207
208reset:
Paul Bakkera3d195c2011-11-27 21:07:34 +0000209#ifdef POLARSSL_ERROR_C
210 if( ret != 0 )
211 {
212 char error_buf[100];
213 error_strerror( ret, error_buf, 100 );
214 printf("Last error was: %d - %s\n\n", ret, error_buf );
215 }
216#endif
217
Paul Bakker7eb013f2011-10-06 12:37:39 +0000218 if( client_fd != -1 )
219 net_close( client_fd );
220
221 ssl_session_reset( &ssl );
222
223 /*
224 * 3. Wait until a client connects
225 */
Paul Bakkercce9d772011-11-18 14:26:47 +0000226#if defined(_WIN32_WCE)
227 {
228 SHELLEXECUTEINFO sei;
229
230 ZeroMemory( &sei, sizeof( SHELLEXECUTEINFO ) );
231
232 sei.cbSize = sizeof( SHELLEXECUTEINFO );
233 sei.fMask = 0;
234 sei.hwnd = 0;
235 sei.lpVerb = _T( "open" );
236 sei.lpFile = _T( "https://localhost:4433/" );
237 sei.lpParameters = NULL;
238 sei.lpDirectory = NULL;
239 sei.nShow = SW_SHOWNORMAL;
240
241 ShellExecuteEx( &sei );
242 }
243#elif defined(_WIN32)
Paul Bakker7eb013f2011-10-06 12:37:39 +0000244 ShellExecute( NULL, "open", "https://localhost:4433/",
245 NULL, NULL, SW_SHOWNORMAL );
246#endif
247
248 client_fd = -1;
249
250 printf( " . Waiting for a remote connection ..." );
251 fflush( stdout );
252
253 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
254 {
255 printf( " failed\n ! net_accept returned %d\n\n", ret );
256 goto exit;
257 }
258
259 ssl_set_bio( &ssl, net_recv, &client_fd,
260 net_send, &client_fd );
261
262 printf( " ok\n" );
263
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 /*
265 * 5. Handshake
266 */
267 printf( " . Performing the SSL/TLS handshake..." );
268 fflush( stdout );
269
270 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
271 {
Paul Bakker831a7552011-05-18 13:32:51 +0000272 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 {
274 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000275 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 }
277 }
278
279 printf( " ok\n" );
280
281 /*
282 * 6. Read the HTTP Request
283 */
284 printf( " < Read from client:" );
285 fflush( stdout );
286
287 do
288 {
289 len = sizeof( buf ) - 1;
290 memset( buf, 0, sizeof( buf ) );
291 ret = ssl_read( &ssl, buf, len );
292
Paul Bakker831a7552011-05-18 13:32:51 +0000293 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 continue;
295
296 if( ret <= 0 )
297 {
298 switch( ret )
299 {
Paul Bakker40e46942009-01-03 21:51:57 +0000300 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 printf( " connection was closed gracefully\n" );
302 break;
303
Paul Bakker40e46942009-01-03 21:51:57 +0000304 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 printf( " connection was reset by peer\n" );
306 break;
307
308 default:
Paul Bakker6f3578c2012-04-16 06:46:01 +0000309 printf( " ssl_read returned -0x%x\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 break;
311 }
312
313 break;
314 }
315
316 len = ret;
317 printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker48916f92012-09-16 19:57:18 +0000318
319 if( ret > 0 )
320 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 }
Paul Bakker48916f92012-09-16 19:57:18 +0000322 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
324 /*
325 * 7. Write the 200 Response
326 */
327 printf( " > Write to client:" );
328 fflush( stdout );
329
330 len = sprintf( (char *) buf, HTTP_RESPONSE,
Paul Bakkere3166ce2011-01-27 17:40:50 +0000331 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
333 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
334 {
Paul Bakker40e46942009-01-03 21:51:57 +0000335 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 {
337 printf( " failed\n ! peer closed the connection\n\n" );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000338 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 }
340
Paul Bakker831a7552011-05-18 13:32:51 +0000341 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 {
343 printf( " failed\n ! ssl_write returned %d\n\n", ret );
344 goto exit;
345 }
346 }
347
348 len = ret;
349 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000350
Paul Bakkera3d195c2011-11-27 21:07:34 +0000351 ret = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +0000352 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
354exit:
355
Paul Bakkera3d195c2011-11-27 21:07:34 +0000356#ifdef POLARSSL_ERROR_C
357 if( ret != 0 )
358 {
359 char error_buf[100];
360 error_strerror( ret, error_buf, 100 );
361 printf("Last error was: %d - %s\n\n", ret, error_buf );
362 }
363#endif
364
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 net_close( client_fd );
366 x509_free( &srvcert );
367 rsa_free( &rsa );
368 ssl_free( &ssl );
Paul Bakker0a597072012-09-25 21:55:46 +0000369#if defined(POLARSSL_SSL_CACHE_C)
370 ssl_cache_free( &cache );
371#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000372
Paul Bakkercce9d772011-11-18 14:26:47 +0000373#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 printf( " Press Enter to exit this program.\n" );
375 fflush( stdout ); getchar();
376#endif
377
378 return( ret );
379}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000380#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000381 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000382 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */