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 #define mbedtls_fprintf    fprintf
33 #define mbedtls_printf     printf
34 #define mbedtls_time_t     time_t
35 #endif
36 
37 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) ||    \
38     !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
39     !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) ||     \
40     !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) ||    \
41     !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_TIMING_C) || \
42     !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_PEM_PARSE_C)
main(int argc,char * argv[])43 int main( int argc, char *argv[] )
44 {
45     ((void) argc);
46     ((void) argv);
47 
48     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
49            "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
50            "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
51            "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
52            "MBEDTLS_TIMING_C and/or MBEDTLS_PEM_PARSE_C not defined.\n");
53     return( 0 );
54 }
55 #elif defined(_WIN32)
main(void)56 int main( void )
57 {
58     mbedtls_printf("_WIN32 defined. This application requires fork() and signals "
59            "to work correctly.\n");
60     return( 0 );
61 }
62 #else
63 
64 #include "mbedtls/entropy.h"
65 #include "mbedtls/ctr_drbg.h"
66 #include "mbedtls/certs.h"
67 #include "mbedtls/x509.h"
68 #include "mbedtls/ssl.h"
69 #include "mbedtls/net_sockets.h"
70 #include "mbedtls/timing.h"
71 
72 #include <string.h>
73 #include <signal.h>
74 
75 #if !defined(_MSC_VER) || defined(EFIX64) || defined(EFI32)
76 #include <unistd.h>
77 #endif
78 
79 #define HTTP_RESPONSE \
80     "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
81     "<h2>mbed TLS Test Server</h2>\r\n" \
82     "<p>Successful connection using: %s</p>\r\n"
83 
84 #define DEBUG_LEVEL 0
85 
my_debug(void * ctx,int level,const char * file,int line,const char * str)86 static void my_debug( void *ctx, int level,
87                       const char *file, int line,
88                       const char *str )
89 {
90     ((void) level);
91 
92     mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
93     fflush(  (FILE *) ctx  );
94 }
95 
main(void)96 int main( void )
97 {
98     int ret, len, cnt = 0, pid;
99     mbedtls_net_context listen_fd, client_fd;
100     unsigned char buf[1024];
101     const char *pers = "ssl_fork_server";
102 
103     mbedtls_entropy_context entropy;
104     mbedtls_ctr_drbg_context ctr_drbg;
105     mbedtls_ssl_context ssl;
106     mbedtls_ssl_config conf;
107     mbedtls_x509_crt srvcert;
108     mbedtls_pk_context pkey;
109 
110     mbedtls_net_init( &listen_fd );
111     mbedtls_net_init( &client_fd );
112     mbedtls_ssl_init( &ssl );
113     mbedtls_ssl_config_init( &conf );
114     mbedtls_entropy_init( &entropy );
115     mbedtls_pk_init( &pkey );
116     mbedtls_x509_crt_init( &srvcert );
117     mbedtls_ctr_drbg_init( &ctr_drbg );
118 
119     signal( SIGCHLD, SIG_IGN );
120 
121     /*
122      * 0. Initial seeding of the RNG
123      */
124     mbedtls_printf( "\n  . Initial seeding of the random generator..." );
125     fflush( stdout );
126 
127     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
128                                (const unsigned char *) pers,
129                                strlen( pers ) ) ) != 0 )
130     {
131         mbedtls_printf( " failed!  mbedtls_ctr_drbg_seed returned %d\n\n", ret );
132         goto exit;
133     }
134 
135     mbedtls_printf( " ok\n" );
136 
137     /*
138      * 1. Load the certificates and private RSA key
139      */
140     mbedtls_printf( "  . Loading the server cert. and key..." );
141     fflush( stdout );
142 
143     /*
144      * This demonstration program uses embedded test certificates.
145      * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
146      * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
147      */
148     ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
149                           mbedtls_test_srv_crt_len );
150     if( ret != 0 )
151     {
152         mbedtls_printf( " failed!  mbedtls_x509_crt_parse returned %d\n\n", ret );
153         goto exit;
154     }
155 
156     ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
157                           mbedtls_test_cas_pem_len );
158     if( ret != 0 )
159     {
160         mbedtls_printf( " failed!  mbedtls_x509_crt_parse returned %d\n\n", ret );
161         goto exit;
162     }
163 
164     ret =  mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
165                           mbedtls_test_srv_key_len, NULL, 0 );
166     if( ret != 0 )
167     {
168         mbedtls_printf( " failed!  mbedtls_pk_parse_key returned %d\n\n", ret );
169         goto exit;
170     }
171 
172     mbedtls_printf( " ok\n" );
173 
174     /*
175      * 1b. Prepare SSL configuration
176      */
177     mbedtls_printf( "  . Configuring SSL..." );
178     fflush( stdout );
179 
180     if( ( ret = mbedtls_ssl_config_defaults( &conf,
181                     MBEDTLS_SSL_IS_SERVER,
182                     MBEDTLS_SSL_TRANSPORT_STREAM,
183                     MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
184     {
185         mbedtls_printf( " failed!  mbedtls_ssl_config_defaults returned %d\n\n", ret );
186         goto exit;
187     }
188 
189     mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
190     mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
191 
192     mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
193     if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
194     {
195         mbedtls_printf( " failed!  mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
196         goto exit;
197     }
198 
199     mbedtls_printf( " ok\n" );
200 
201     /*
202      * 2. Setup the listening TCP socket
203      */
204     mbedtls_printf( "  . Bind on https://localhost:4433/ ..." );
205     fflush( stdout );
206 
207     if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
208     {
209         mbedtls_printf( " failed!  mbedtls_net_bind returned %d\n\n", ret );
210         goto exit;
211     }
212 
213     mbedtls_printf( " ok\n" );
214 
215     while( 1 )
216     {
217         /*
218          * 3. Wait until a client connects
219          */
220         mbedtls_net_init( &client_fd );
221         mbedtls_ssl_init( &ssl );
222 
223         mbedtls_printf( "  . Waiting for a remote connection ...\n" );
224         fflush( stdout );
225 
226         if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
227                                         NULL, 0, NULL ) ) != 0 )
228         {
229             mbedtls_printf( " failed!  mbedtls_net_accept returned %d\n\n", ret );
230             goto exit;
231         }
232 
233         /*
234          * 3.5. Forking server thread
235          */
236 
237         mbedtls_printf( "  . Forking to handle connection ..." );
238         fflush( stdout );
239 
240         pid = fork();
241 
242         if( pid < 0 )
243         {
244             mbedtls_printf(" failed!  fork returned %d\n\n", pid );
245             goto exit;
246         }
247 
248         if( pid != 0 )
249         {
250             mbedtls_printf( " ok\n" );
251 
252             if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
253                                          (const unsigned char *) "parent",
254                                          6 ) ) != 0 )
255             {
256                 mbedtls_printf( " failed!  mbedtls_ctr_drbg_reseed returned %d\n\n", ret );
257                 goto exit;
258             }
259 
260             continue;
261         }
262 
263         mbedtls_net_init( &listen_fd );
264 
265         pid = getpid();
266 
267         /*
268          * 4. Setup stuff
269          */
270         mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
271         fflush( stdout );
272 
273         if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
274                                      (const unsigned char *) "child",
275                                      5 ) ) != 0 )
276         {
277             mbedtls_printf(
278                     "pid %d: SSL setup failed!  mbedtls_ctr_drbg_reseed returned %d\n\n",
279                     pid, ret );
280             goto exit;
281         }
282 
283         if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
284         {
285             mbedtls_printf(
286                     "pid %d: SSL setup failed!  mbedtls_ssl_setup returned %d\n\n",
287                     pid, ret );
288             goto exit;
289         }
290 
291         mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
292 
293         mbedtls_printf( "pid %d: SSL setup ok\n", pid );
294 
295         /*
296          * 5. Handshake
297          */
298         mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
299         fflush( stdout );
300 
301         while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
302         {
303             if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
304             {
305                 mbedtls_printf(
306                         "pid %d: SSL handshake failed!  mbedtls_ssl_handshake returned %d\n\n",
307                         pid, ret );
308                 goto exit;
309             }
310         }
311 
312         mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
313 
314         /*
315          * 6. Read the HTTP Request
316          */
317         mbedtls_printf( "pid %d: Start reading from client.\n", pid );
318         fflush( stdout );
319 
320         do
321         {
322             len = sizeof( buf ) - 1;
323             memset( buf, 0, sizeof( buf ) );
324             ret = mbedtls_ssl_read( &ssl, buf, len );
325 
326             if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
327                 continue;
328 
329             if( ret <= 0 )
330             {
331                 switch( ret )
332                 {
333                     case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
334                         mbedtls_printf( "pid %d: connection was closed gracefully\n", pid );
335                         break;
336 
337                     case MBEDTLS_ERR_NET_CONN_RESET:
338                         mbedtls_printf( "pid %d: connection was reset by peer\n", pid );
339                         break;
340 
341                     default:
342                         mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
343                         break;
344                 }
345 
346                 break;
347             }
348 
349             len = ret;
350             mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf );
351 
352             if( ret > 0 )
353                 break;
354         }
355         while( 1 );
356 
357         /*
358          * 7. Write the 200 Response
359          */
360         mbedtls_printf( "pid %d: Start writing to client.\n", pid );
361         fflush( stdout );
362 
363         len = sprintf( (char *) buf, HTTP_RESPONSE,
364                 mbedtls_ssl_get_ciphersuite( &ssl ) );
365 
366         while( cnt++ < 100 )
367         {
368             while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
369             {
370                 if( ret == MBEDTLS_ERR_NET_CONN_RESET )
371                 {
372                     mbedtls_printf(
373                             "pid %d: Write failed!  peer closed the connection\n\n", pid );
374                     goto exit;
375                 }
376 
377                 if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
378                 {
379                     mbedtls_printf(
380                             "pid %d: Write failed!  mbedtls_ssl_write returned %d\n\n",
381                             pid, ret );
382                     goto exit;
383                 }
384             }
385             len = ret;
386             mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf );
387 
388             mbedtls_net_usleep( 1000000 );
389         }
390 
391         mbedtls_ssl_close_notify( &ssl );
392         goto exit;
393     }
394 
395 exit:
396     mbedtls_net_free( &client_fd );
397     mbedtls_net_free( &listen_fd );
398 
399     mbedtls_x509_crt_free( &srvcert );
400     mbedtls_pk_free( &pkey );
401     mbedtls_ssl_free( &ssl );
402     mbedtls_ssl_config_free( &conf );
403     mbedtls_ctr_drbg_free( &ctr_drbg );
404     mbedtls_entropy_free( &entropy );
405 
406 #if defined(_WIN32)
407     mbedtls_printf( "  Press Enter to exit this program.\n" );
408     fflush( stdout ); getchar();
409 #endif
410 
411     return( ret );
412 }
413 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
414           MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
415           MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_PARSE_C &&
416           ! _WIN32 */
417