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