blob: 9e325b252f50f4ede831e5e43418513a5a87ea60 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSL server demonstration program
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
Paul Bakkercce9d772011-11-18 14:26:47 +000027#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000028#include <windows.h>
29#endif
30
31#include <string.h>
32#include <stdlib.h>
33#include <stdio.h>
34
Paul Bakker5690efc2011-05-26 13:16:06 +000035#include "polarssl/config.h"
36
Paul Bakker508ad5a2011-12-04 17:09:26 +000037#include "polarssl/entropy.h"
38#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000039#include "polarssl/certs.h"
40#include "polarssl/x509.h"
41#include "polarssl/ssl.h"
42#include "polarssl/net.h"
Paul Bakkera3d195c2011-11-27 21:07:34 +000043#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Paul Bakker0a597072012-09-25 21:55:46 +000045#if defined(POLARSSL_SSL_CACHE_C)
46#include "polarssl/ssl_cache.h"
47#endif
48
Paul Bakker5121ce52009-01-03 21:22:43 +000049#define HTTP_RESPONSE \
50 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
Paul Bakkereaca51d2010-08-16 12:00:14 +000051 "<h2>PolarSSL Test Server</h2>\r\n" \
52 "<p>Successful connection using: %s</p>\r\n"
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakker835b29e2012-08-23 08:31:59 +000054#define DEBUG_LEVEL 0
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Paul Bakkerff60ee62010-03-16 21:09:09 +000056void my_debug( void *ctx, int level, const char *str )
Paul Bakker5121ce52009-01-03 21:22:43 +000057{
58 if( level < DEBUG_LEVEL )
59 {
60 fprintf( (FILE *) ctx, "%s", str );
61 fflush( (FILE *) ctx );
62 }
63}
64
Paul Bakker5690efc2011-05-26 13:16:06 +000065#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000066 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000067 !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000068 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000069int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000070{
Paul Bakkercce9d772011-11-18 14:26:47 +000071 ((void) argc);
72 ((void) argv);
73
Paul Bakker508ad5a2011-12-04 17:09:26 +000074 printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
Paul Bakker5690efc2011-05-26 13:16:06 +000075 "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000076 "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
77 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000078 return( 0 );
79}
80#else
Paul Bakkercce9d772011-11-18 14:26:47 +000081int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000082{
83 int ret, len;
84 int listen_fd;
Paul Bakker7eb013f2011-10-06 12:37:39 +000085 int client_fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +000086 unsigned char buf[1024];
Paul Bakkere0225e42013-06-06 12:52:24 +020087 const char *pers = "ssl_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Paul Bakker508ad5a2011-12-04 17:09:26 +000089 entropy_context entropy;
90 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000091 ssl_context ssl;
Paul Bakker5121ce52009-01-03 21:22:43 +000092 x509_cert srvcert;
93 rsa_context rsa;
Paul Bakkerd4324102012-09-26 08:29:38 +000094#if defined(POLARSSL_SSL_CACHE_C)
95 ssl_cache_context cache;
96#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Paul Bakkercce9d772011-11-18 14:26:47 +000098 ((void) argc);
99 ((void) argv);
100
Paul Bakker39148402014-04-17 16:02:36 +0200101 memset( &ssl, 0, sizeof(ssl_context) );
Paul Bakker0a597072012-09-25 21:55:46 +0000102#if defined(POLARSSL_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +0000103 ssl_cache_init( &cache );
104#endif
Paul Bakker39148402014-04-17 16:02:36 +0200105 memset( &srvcert, 0, sizeof( x509_cert ) );
106 rsa_init( &rsa, RSA_PKCS_V15, 0 );
107 entropy_init( &entropy );
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
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 /*
116 * This demonstration program uses embedded test certificates.
117 * Instead, you may want to use x509parse_crtfile() to read the
118 * server and CA certificates, as well as x509parse_keyfile().
119 */
Paul Bakkere0225e42013-06-06 12:52:24 +0200120 ret = x509parse_crt( &srvcert, (const unsigned char *) test_srv_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000121 strlen( test_srv_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 if( ret != 0 )
123 {
124 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
125 goto exit;
126 }
127
Paul Bakkere0225e42013-06-06 12:52:24 +0200128 ret = x509parse_crt( &srvcert, (const unsigned char *) test_ca_crt,
Paul Bakker69e095c2011-12-10 21:55:01 +0000129 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 if( ret != 0 )
131 {
132 printf( " failed\n ! x509parse_crt returned %d\n\n", ret );
133 goto exit;
134 }
135
Paul Bakkere0225e42013-06-06 12:52:24 +0200136 ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key,
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 strlen( test_srv_key ), NULL, 0 );
138 if( ret != 0 )
139 {
140 printf( " failed\n ! x509parse_key returned %d\n\n", ret );
141 goto exit;
142 }
143
144 printf( " ok\n" );
145
146 /*
147 * 2. Setup the listening TCP socket
148 */
149 printf( " . Bind on https://localhost:4433/ ..." );
150 fflush( stdout );
151
152 if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
153 {
154 printf( " failed\n ! net_bind returned %d\n\n", ret );
155 goto exit;
156 }
157
158 printf( " ok\n" );
159
160 /*
Paul Bakker508ad5a2011-12-04 17:09:26 +0000161 * 3. Seed the RNG
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 */
Paul Bakker508ad5a2011-12-04 17:09:26 +0000163 printf( " . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 fflush( stdout );
165
Paul Bakker508ad5a2011-12-04 17:09:26 +0000166 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +0200167 (const unsigned char *) pers,
168 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000169 {
170 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
171 goto exit;
172 }
173
174 printf( " ok\n" );
175
176 /*
177 * 4. Setup stuff
178 */
179 printf( " . Setting up the SSL data...." );
180 fflush( stdout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182 if( ( ret = ssl_init( &ssl ) ) != 0 )
183 {
184 printf( " failed\n ! ssl_init returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000185 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 }
187
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 ssl_set_endpoint( &ssl, SSL_IS_SERVER );
189 ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
190
Paul Bakker508ad5a2011-12-04 17:09:26 +0000191 ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 ssl_set_dbg( &ssl, my_debug, stdout );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000193
Paul Bakker0a597072012-09-25 21:55:46 +0000194#if defined(POLARSSL_SSL_CACHE_C)
195 ssl_set_session_cache( &ssl, ssl_cache_get, &cache,
196 ssl_cache_set, &cache );
197#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Paul Bakker40ea7de2009-05-03 10:18:48 +0000199 ssl_set_ca_chain( &ssl, srvcert.next, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 ssl_set_own_cert( &ssl, &srvcert, &rsa );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Paul Bakker7eb013f2011-10-06 12:37:39 +0000202 printf( " ok\n" );
203
204reset:
Paul Bakkera3d195c2011-11-27 21:07:34 +0000205#ifdef POLARSSL_ERROR_C
206 if( ret != 0 )
207 {
208 char error_buf[100];
209 error_strerror( ret, error_buf, 100 );
210 printf("Last error was: %d - %s\n\n", ret, error_buf );
211 }
212#endif
213
Paul Bakker7eb013f2011-10-06 12:37:39 +0000214 if( client_fd != -1 )
215 net_close( client_fd );
216
217 ssl_session_reset( &ssl );
218
219 /*
220 * 3. Wait until a client connects
221 */
Paul Bakker7eb013f2011-10-06 12:37:39 +0000222 client_fd = -1;
223
224 printf( " . Waiting for a remote connection ..." );
225 fflush( stdout );
226
227 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
228 {
229 printf( " failed\n ! net_accept returned %d\n\n", ret );
230 goto exit;
231 }
232
233 ssl_set_bio( &ssl, net_recv, &client_fd,
234 net_send, &client_fd );
235
236 printf( " ok\n" );
237
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 /*
239 * 5. Handshake
240 */
241 printf( " . Performing the SSL/TLS handshake..." );
242 fflush( stdout );
243
244 while( ( ret = ssl_handshake( &ssl ) ) != 0 )
245 {
Paul Bakker831a7552011-05-18 13:32:51 +0000246 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 {
248 printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000249 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 }
251 }
252
253 printf( " ok\n" );
254
255 /*
256 * 6. Read the HTTP Request
257 */
258 printf( " < Read from client:" );
259 fflush( stdout );
260
261 do
262 {
263 len = sizeof( buf ) - 1;
264 memset( buf, 0, sizeof( buf ) );
265 ret = ssl_read( &ssl, buf, len );
266
Paul Bakker831a7552011-05-18 13:32:51 +0000267 if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 continue;
269
270 if( ret <= 0 )
271 {
272 switch( ret )
273 {
Paul Bakker40e46942009-01-03 21:51:57 +0000274 case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 printf( " connection was closed gracefully\n" );
276 break;
277
Paul Bakker40e46942009-01-03 21:51:57 +0000278 case POLARSSL_ERR_NET_CONN_RESET:
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 printf( " connection was reset by peer\n" );
280 break;
281
282 default:
Paul Bakker6f3578c2012-04-16 06:46:01 +0000283 printf( " ssl_read returned -0x%x\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 break;
285 }
286
287 break;
288 }
289
290 len = ret;
291 printf( " %d bytes read\n\n%s", len, (char *) buf );
Paul Bakker48916f92012-09-16 19:57:18 +0000292
293 if( ret > 0 )
294 break;
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 }
Paul Bakker48916f92012-09-16 19:57:18 +0000296 while( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298 /*
299 * 7. Write the 200 Response
300 */
301 printf( " > Write to client:" );
302 fflush( stdout );
303
304 len = sprintf( (char *) buf, HTTP_RESPONSE,
Paul Bakkere3166ce2011-01-27 17:40:50 +0000305 ssl_get_ciphersuite( &ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307 while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
308 {
Paul Bakker40e46942009-01-03 21:51:57 +0000309 if( ret == POLARSSL_ERR_NET_CONN_RESET )
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 {
311 printf( " failed\n ! peer closed the connection\n\n" );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000312 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 }
314
Paul Bakker831a7552011-05-18 13:32:51 +0000315 if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 {
317 printf( " failed\n ! ssl_write returned %d\n\n", ret );
318 goto exit;
319 }
320 }
321
322 len = ret;
323 printf( " %d bytes written\n\n%s\n", len, (char *) buf );
Paul Bakker3cbaf1e2014-07-08 12:26:02 +0200324
325 printf( " . Closing the connection..." );
326
327 while( ( ret = ssl_close_notify( &ssl ) ) < 0 )
328 {
329 if( ret != POLARSSL_ERR_NET_WANT_READ &&
330 ret != POLARSSL_ERR_NET_WANT_WRITE )
331 {
332 printf( " failed\n ! ssl_close_notify returned %d\n\n", ret );
333 goto reset;
334 }
335 }
336
337 printf( " ok\n" );
338
Paul Bakkera3d195c2011-11-27 21:07:34 +0000339 ret = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +0000340 goto reset;
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342exit:
343
Paul Bakkera3d195c2011-11-27 21:07:34 +0000344#ifdef POLARSSL_ERROR_C
345 if( ret != 0 )
346 {
347 char error_buf[100];
348 error_strerror( ret, error_buf, 100 );
349 printf("Last error was: %d - %s\n\n", ret, error_buf );
350 }
351#endif
352
Paul Bakker39148402014-04-17 16:02:36 +0200353 if( client_fd != -1 )
354 net_close( client_fd );
355
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 x509_free( &srvcert );
357 rsa_free( &rsa );
358 ssl_free( &ssl );
Paul Bakker0a597072012-09-25 21:55:46 +0000359#if defined(POLARSSL_SSL_CACHE_C)
360 ssl_cache_free( &cache );
361#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
Paul Bakkercce9d772011-11-18 14:26:47 +0000363#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 printf( " Press Enter to exit this program.\n" );
365 fflush( stdout ); getchar();
366#endif
367
368 return( ret );
369}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000370#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000371 POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000372 POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C */