2 * SSL server demonstration program using fork() for handling multiple clients
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
33 #define mbedtls_fprintf fprintf
34 #define mbedtls_printf printf
35 #define mbedtls_time_t time_t
36 #define mbedtls_exit exit
37 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
38 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
39 #endif /* MBEDTLS_PLATFORM_C */
41 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \
42 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
43 !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) || \
44 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
45 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_TIMING_C) || \
46 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C)
47 int main( int argc, char *argv[] )
52 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
53 "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
54 "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
55 "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
56 "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
62 mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
63 "to work correctly.\n");
68 #include "mbedtls/entropy.h"
69 #include "mbedtls/ctr_drbg.h"
70 #include "mbedtls/certs.h"
71 #include "mbedtls/x509.h"
72 #include "mbedtls/ssl.h"
73 #include "mbedtls/net_sockets.h"
74 #include "mbedtls/timing.h"
79 #if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
83 #define HTTP_RESPONSE \
84 "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
85 "<h2>mbed TLS Test Server</h2>\r\n" \
86 "<p>Successful connection using: %s</p>\r\n"
90 #if defined(MBEDTLS_CHECK_PARAMS)
91 #include "mbedtls/platform_util.h"
92 void mbedtls_param_failed( const char *failure_condition,
96 mbedtls_printf( "%s:%i: Input param failed - %s\n",
97 file, line, failure_condition );
98 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
102 static void my_debug( void *ctx, int level,
103 const char *file, int line,
108 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
109 fflush( (FILE *) ctx );
114 int ret = 1, len, cnt = 0, pid;
115 int exit_code = MBEDTLS_EXIT_FAILURE;
116 mbedtls_net_context listen_fd, client_fd;
117 unsigned char buf[1024];
118 const char *pers = "ssl_fork_server";
120 mbedtls_entropy_context entropy;
121 mbedtls_ctr_drbg_context ctr_drbg;
122 mbedtls_ssl_context ssl;
123 mbedtls_ssl_config conf;
124 mbedtls_x509_crt srvcert;
125 mbedtls_pk_context pkey;
127 mbedtls_net_init( &listen_fd );
128 mbedtls_net_init( &client_fd );
129 mbedtls_ssl_init( &ssl );
130 mbedtls_ssl_config_init( &conf );
131 mbedtls_entropy_init( &entropy );
132 mbedtls_pk_init( &pkey );
133 mbedtls_x509_crt_init( &srvcert );
134 mbedtls_ctr_drbg_init( &ctr_drbg );
136 signal( SIGCHLD, SIG_IGN );
139 * 0. Initial seeding of the RNG
141 mbedtls_printf( "\n . Initial seeding of the random generator..." );
144 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
145 (const unsigned char *) pers,
146 strlen( pers ) ) ) != 0 )
148 mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret );
152 mbedtls_printf( " ok\n" );
155 * 1. Load the certificates and private RSA key
157 mbedtls_printf( " . Loading the server cert. and key..." );
161 * This demonstration program uses embedded test certificates.
162 * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
163 * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
165 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
166 mbedtls_test_srv_crt_len );
169 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
173 ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
174 mbedtls_test_cas_pem_len );
177 mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret );
181 ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
182 mbedtls_test_srv_key_len, NULL, 0 );
185 mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret );
189 mbedtls_printf( " ok\n" );
192 * 1b. Prepare SSL configuration
194 mbedtls_printf( " . Configuring SSL..." );
197 if( ( ret = mbedtls_ssl_config_defaults( &conf,
198 MBEDTLS_SSL_IS_SERVER,
199 MBEDTLS_SSL_TRANSPORT_STREAM,
200 MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
202 mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret );
206 mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
207 mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
209 mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
210 if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
212 mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
216 mbedtls_printf( " ok\n" );
219 * 2. Setup the listening TCP socket
221 mbedtls_printf( " . Bind on https://localhost:4433/ ..." );
224 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
226 mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret );
230 mbedtls_printf( " ok\n" );
235 * 3. Wait until a client connects
237 mbedtls_net_init( &client_fd );
238 mbedtls_ssl_init( &ssl );
240 mbedtls_printf( " . Waiting for a remote connection ...\n" );
243 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
244 NULL, 0, NULL ) ) != 0 )
246 mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret );
251 * 3.5. Forking server thread
254 mbedtls_printf( " . Forking to handle connection ..." );
261 mbedtls_printf(" failed! fork returned %d\n\n", pid );
267 mbedtls_printf( " ok\n" );
269 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
270 (const unsigned char *) "parent",
273 mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
280 mbedtls_net_init( &listen_fd );
287 mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
290 if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
291 (const unsigned char *) "child",
295 "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n",
300 if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
303 "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n",
308 mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
310 mbedtls_printf( "pid %d: SSL setup ok\n", pid );
315 mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
318 while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
320 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
323 "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n",
329 mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
332 * 6. Read the HTTP Request
334 mbedtls_printf( "pid %d: Start reading from client.\n", pid );
339 len = sizeof( buf ) - 1;
340 memset( buf, 0, sizeof( buf ) );
341 ret = mbedtls_ssl_read( &ssl, buf, len );
343 if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
350 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
351 mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
354 case MBEDTLS_ERR_NET_CONN_RESET:
355 mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
359 mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
367 mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
375 * 7. Write the 200 Response
377 mbedtls_printf( "pid %d: Start writing to client.\n", pid );
380 len = sprintf( (char *) buf, HTTP_RESPONSE,
381 mbedtls_ssl_get_ciphersuite( &ssl ) );
385 while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
387 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
390 "pid %d: Write failed! peer closed the connection\n\n", pid );
394 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
397 "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n",
403 mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
405 mbedtls_net_usleep( 1000000 );
408 mbedtls_ssl_close_notify( &ssl );
412 exit_code = MBEDTLS_EXIT_SUCCESS;
415 mbedtls_net_free( &client_fd );
416 mbedtls_net_free( &listen_fd );
418 mbedtls_x509_crt_free( &srvcert );
419 mbedtls_pk_free( &pkey );
420 mbedtls_ssl_free( &ssl );
421 mbedtls_ssl_config_free( &conf );
422 mbedtls_ctr_drbg_free( &ctr_drbg );
423 mbedtls_entropy_free( &entropy );
426 mbedtls_printf( " Press Enter to exit this program.\n" );
427 fflush( stdout ); getchar();
432 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
433 MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
434 MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&