1 /*
2  *  SSL server demonstration program using fork() for handling multiple clients
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
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
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #include <stdlib.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 */
40 
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)
main(int argc,char * argv[])47 int main( int argc, char *argv[] )
48 {
49     ((void) argc);
50     ((void) argv);
51 
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");
57     return( 0 );
58 }
59 #elif defined(_WIN32)
main(void)60 int main( void )
61 {
62     mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
63            "to work correctly.\n");
64     return( 0 );
65 }
66 #else
67 
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"
75 
76 #include <string.h>
77 #include <signal.h>
78 
79 #if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
80 #include <unistd.h>
81 #endif
82 
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"
87 
88 #define DEBUG_LEVEL 0
89 
90 #if defined(MBEDTLS_CHECK_PARAMS)
91 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)92 void mbedtls_param_failed( const char *failure_condition,
93                            const char *file,
94                            int line )
95 {
96     mbedtls_printf( "%s:%i: Input param failed - %s\n",
97                     file, line, failure_condition );
98     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
99 }
100 #endif
101 
my_debug(void * ctx,int level,const char * file,int line,const char * str)102 static void my_debug( void *ctx, int level,
103                       const char *file, int line,
104                       const char *str )
105 {
106     ((void) level);
107 
108     mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
109     fflush(  (FILE *) ctx  );
110 }
111 
main(void)112 int main( void )
113 {
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";
119 
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;
126 
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 );
135 
136     signal( SIGCHLD, SIG_IGN );
137 
138     /*
139      * 0. Initial seeding of the RNG
140      */
141     mbedtls_printf( "\n  . Initial seeding of the random generator..." );
142     fflush( stdout );
143 
144     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
145                                (const unsigned char *) pers,
146                                strlen( pers ) ) ) != 0 )
147     {
148         mbedtls_printf( " failed!  mbedtls_ctr_drbg_seed returned %d\n\n", ret );
149         goto exit;
150     }
151 
152     mbedtls_printf( " ok\n" );
153 
154     /*
155      * 1. Load the certificates and private RSA key
156      */
157     mbedtls_printf( "  . Loading the server cert. and key..." );
158     fflush( stdout );
159 
160     /*
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().
164      */
165     ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
166                           mbedtls_test_srv_crt_len );
167     if( ret != 0 )
168     {
169         mbedtls_printf( " failed!  mbedtls_x509_crt_parse returned %d\n\n", ret );
170         goto exit;
171     }
172 
173     ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
174                           mbedtls_test_cas_pem_len );
175     if( ret != 0 )
176     {
177         mbedtls_printf( " failed!  mbedtls_x509_crt_parse returned %d\n\n", ret );
178         goto exit;
179     }
180 
181     ret =  mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
182                           mbedtls_test_srv_key_len, NULL, 0 );
183     if( ret != 0 )
184     {
185         mbedtls_printf( " failed!  mbedtls_pk_parse_key returned %d\n\n", ret );
186         goto exit;
187     }
188 
189     mbedtls_printf( " ok\n" );
190 
191     /*
192      * 1b. Prepare SSL configuration
193      */
194     mbedtls_printf( "  . Configuring SSL..." );
195     fflush( stdout );
196 
197     if( ( ret = mbedtls_ssl_config_defaults( &conf,
198                     MBEDTLS_SSL_IS_SERVER,
199                     MBEDTLS_SSL_TRANSPORT_STREAM,
200                     MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
201     {
202         mbedtls_printf( " failed!  mbedtls_ssl_config_defaults returned %d\n\n", ret );
203         goto exit;
204     }
205 
206     mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
207     mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
208 
209     mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
210     if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
211     {
212         mbedtls_printf( " failed!  mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
213         goto exit;
214     }
215 
216     mbedtls_printf( " ok\n" );
217 
218     /*
219      * 2. Setup the listening TCP socket
220      */
221     mbedtls_printf( "  . Bind on https://localhost:4433/ ..." );
222     fflush( stdout );
223 
224     if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
225     {
226         mbedtls_printf( " failed!  mbedtls_net_bind returned %d\n\n", ret );
227         goto exit;
228     }
229 
230     mbedtls_printf( " ok\n" );
231 
232     while( 1 )
233     {
234         /*
235          * 3. Wait until a client connects
236          */
237         mbedtls_net_init( &client_fd );
238         mbedtls_ssl_init( &ssl );
239 
240         mbedtls_printf( "  . Waiting for a remote connection ...\n" );
241         fflush( stdout );
242 
243         if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
244                                         NULL, 0, NULL ) ) != 0 )
245         {
246             mbedtls_printf( " failed!  mbedtls_net_accept returned %d\n\n", ret );
247             goto exit;
248         }
249 
250         /*
251          * 3.5. Forking server thread
252          */
253 
254         mbedtls_printf( "  . Forking to handle connection ..." );
255         fflush( stdout );
256 
257         pid = fork();
258 
259         if( pid < 0 )
260         {
261             mbedtls_printf(" failed!  fork returned %d\n\n", pid );
262             goto exit;
263         }
264 
265         if( pid != 0 )
266         {
267             mbedtls_printf( " ok\n" );
268 
269             if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
270                                          (const unsigned char *) "parent",
271                                          6 ) ) != 0 )
272             {
273                 mbedtls_printf( " failed!  mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
274                 goto exit;
275             }
276 
277             continue;
278         }
279 
280         mbedtls_net_init( &listen_fd );
281 
282         pid = getpid();
283 
284         /*
285          * 4. Setup stuff
286          */
287         mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
288         fflush( stdout );
289 
290         if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
291                                      (const unsigned char *) "child",
292                                      5 ) ) != 0 )
293         {
294             mbedtls_printf(
295                     "pid %d: SSL setup failed!  mbedtls_ctr_drbg_reseed returned %d\n\n",
296                     pid, ret );
297             goto exit;
298         }
299 
300         if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
301         {
302             mbedtls_printf(
303                     "pid %d: SSL setup failed!  mbedtls_ssl_setup returned %d\n\n",
304                     pid, ret );
305             goto exit;
306         }
307 
308         mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
309 
310         mbedtls_printf( "pid %d: SSL setup ok\n", pid );
311 
312         /*
313          * 5. Handshake
314          */
315         mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
316         fflush( stdout );
317 
318         while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
319         {
320             if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
321             {
322                 mbedtls_printf(
323                         "pid %d: SSL handshake failed!  mbedtls_ssl_handshake returned %d\n\n",
324                         pid, ret );
325                 goto exit;
326             }
327         }
328 
329         mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
330 
331         /*
332          * 6. Read the HTTP Request
333          */
334         mbedtls_printf( "pid %d: Start reading from client.\n", pid );
335         fflush( stdout );
336 
337         do
338         {
339             len = sizeof( buf ) - 1;
340             memset( buf, 0, sizeof( buf ) );
341             ret = mbedtls_ssl_read( &ssl, buf, len );
342 
343             if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
344                 continue;
345 
346             if( ret <= 0 )
347             {
348                 switch( ret )
349                 {
350                     case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
351                         mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
352                         break;
353 
354                     case MBEDTLS_ERR_NET_CONN_RESET:
355                         mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
356                         break;
357 
358                     default:
359                         mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
360                         break;
361                 }
362 
363                 break;
364             }
365 
366             len = ret;
367             mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
368 
369             if( ret > 0 )
370                 break;
371         }
372         while( 1 );
373 
374         /*
375          * 7. Write the 200 Response
376          */
377         mbedtls_printf( "pid %d: Start writing to client.\n", pid );
378         fflush( stdout );
379 
380         len = sprintf( (char *) buf, HTTP_RESPONSE,
381                 mbedtls_ssl_get_ciphersuite( &ssl ) );
382 
383         while( cnt++ < 100 )
384         {
385             while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
386             {
387                 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
388                 {
389                     mbedtls_printf(
390                             "pid %d: Write failed!  peer closed the connection\n\n", pid );
391                     goto exit;
392                 }
393 
394                 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
395                 {
396                     mbedtls_printf(
397                             "pid %d: Write failed!  mbedtls_ssl_write returned %d\n\n",
398                             pid, ret );
399                     goto exit;
400                 }
401             }
402             len = ret;
403             mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
404 
405             mbedtls_net_usleep( 1000000 );
406         }
407 
408         mbedtls_ssl_close_notify( &ssl );
409         goto exit;
410     }
411 
412     exit_code = MBEDTLS_EXIT_SUCCESS;
413 
414 exit:
415     mbedtls_net_free( &client_fd );
416     mbedtls_net_free( &listen_fd );
417 
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 );
424 
425 #if defined(_WIN32)
426     mbedtls_printf( "  Press Enter to exit this program.\n" );
427     fflush( stdout ); getchar();
428 #endif
429 
430     return( exit_code );
431 }
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 &&
435           ! _WIN32 */
436