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