• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  SSL server demonstration program
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  *
7  *  This file is provided under the Apache License 2.0, or the
8  *  GNU General Public License v2.0 or later.
9  *
10  *  **********
11  *  Apache License 2.0:
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  **********
26  *
27  *  **********
28  *  GNU General Public License v2.0 or later:
29  *
30  *  This program is free software; you can redistribute it and/or modify
31  *  it under the terms of the GNU General Public License as published by
32  *  the Free Software Foundation; either version 2 of the License, or
33  *  (at your option) any later version.
34  *
35  *  This program is distributed in the hope that it will be useful,
36  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  *  GNU General Public License for more details.
39  *
40  *  You should have received a copy of the GNU General Public License along
41  *  with this program; if not, write to the Free Software Foundation, Inc.,
42  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43  *
44  *  **********
45  */
46 
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52 
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_time       time
59 #define mbedtls_time_t     time_t
60 #define mbedtls_fprintf    fprintf
61 #define mbedtls_printf     printf
62 #define mbedtls_exit            exit
63 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
64 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
65 #endif
66 
67 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) ||    \
68     !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
69     !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) ||     \
70     !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) ||    \
71     !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
72     !defined(MBEDTLS_PEM_PARSE_C)
main(void)73 int main( void )
74 {
75     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
76            "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
77            "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
78            "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
79            "and/or MBEDTLS_PEM_PARSE_C not defined.\n");
80     mbedtls_exit( 0 );
81 }
82 #else
83 
84 #include <stdlib.h>
85 #include <string.h>
86 
87 #if defined(_WIN32)
88 #include <windows.h>
89 #endif
90 
91 #include "mbedtls/entropy.h"
92 #include "mbedtls/ctr_drbg.h"
93 #include "mbedtls/certs.h"
94 #include "mbedtls/x509.h"
95 #include "mbedtls/ssl.h"
96 #include "mbedtls/net_sockets.h"
97 #include "mbedtls/error.h"
98 #include "mbedtls/debug.h"
99 
100 #if defined(MBEDTLS_SSL_CACHE_C)
101 #include "mbedtls/ssl_cache.h"
102 #endif
103 
104 #define HTTP_RESPONSE \
105     "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
106     "<h2>mbed TLS Test Server</h2>\r\n" \
107     "<p>Successful connection using: %s</p>\r\n"
108 
109 #define DEBUG_LEVEL 0
110 
111 
my_debug(void * ctx,int level,const char * file,int line,const char * str)112 static void my_debug( void *ctx, int level,
113                       const char *file, int line,
114                       const char *str )
115 {
116     ((void) level);
117 
118     mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
119     fflush(  (FILE *) ctx  );
120 }
121 
main(void)122 int main( void )
123 {
124     int ret, len;
125     mbedtls_net_context listen_fd, client_fd;
126     unsigned char buf[1024];
127     const char *pers = "ssl_server";
128 
129     mbedtls_entropy_context entropy;
130     mbedtls_ctr_drbg_context ctr_drbg;
131     mbedtls_ssl_context ssl;
132     mbedtls_ssl_config conf;
133     mbedtls_x509_crt srvcert;
134     mbedtls_pk_context pkey;
135 #if defined(MBEDTLS_SSL_CACHE_C)
136     mbedtls_ssl_cache_context cache;
137 #endif
138 
139     mbedtls_net_init( &listen_fd );
140     mbedtls_net_init( &client_fd );
141     mbedtls_ssl_init( &ssl );
142     mbedtls_ssl_config_init( &conf );
143 #if defined(MBEDTLS_SSL_CACHE_C)
144     mbedtls_ssl_cache_init( &cache );
145 #endif
146     mbedtls_x509_crt_init( &srvcert );
147     mbedtls_pk_init( &pkey );
148     mbedtls_entropy_init( &entropy );
149     mbedtls_ctr_drbg_init( &ctr_drbg );
150 
151 #if defined(MBEDTLS_DEBUG_C)
152     mbedtls_debug_set_threshold( DEBUG_LEVEL );
153 #endif
154 
155     /*
156      * 1. Load the certificates and private RSA key
157      */
158     mbedtls_printf( "\n  . Loading the server cert. and key..." );
159     fflush( stdout );
160 
161     /*
162      * This demonstration program uses embedded test certificates.
163      * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
164      * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
165      */
166     ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
167                           mbedtls_test_srv_crt_len );
168     if( ret != 0 )
169     {
170         mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned %d\n\n", ret );
171         goto exit;
172     }
173 
174     ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
175                           mbedtls_test_cas_pem_len );
176     if( ret != 0 )
177     {
178         mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned %d\n\n", ret );
179         goto exit;
180     }
181 
182     ret =  mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
183                          mbedtls_test_srv_key_len, NULL, 0 );
184     if( ret != 0 )
185     {
186         mbedtls_printf( " failed\n  !  mbedtls_pk_parse_key returned %d\n\n", ret );
187         goto exit;
188     }
189 
190     mbedtls_printf( " ok\n" );
191 
192     /*
193      * 2. Setup the listening TCP socket
194      */
195     mbedtls_printf( "  . Bind on https://localhost:4433/ ..." );
196     fflush( stdout );
197 
198     if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
199     {
200         mbedtls_printf( " failed\n  ! mbedtls_net_bind returned %d\n\n", ret );
201         goto exit;
202     }
203 
204     mbedtls_printf( " ok\n" );
205 
206     /*
207      * 3. Seed the RNG
208      */
209     mbedtls_printf( "  . Seeding the random number generator..." );
210     fflush( stdout );
211 
212     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
213                                (const unsigned char *) pers,
214                                strlen( pers ) ) ) != 0 )
215     {
216         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
217         goto exit;
218     }
219 
220     mbedtls_printf( " ok\n" );
221 
222     /*
223      * 4. Setup stuff
224      */
225     mbedtls_printf( "  . Setting up the SSL data...." );
226     fflush( stdout );
227 
228     if( ( ret = mbedtls_ssl_config_defaults( &conf,
229                     MBEDTLS_SSL_IS_SERVER,
230                     MBEDTLS_SSL_TRANSPORT_STREAM,
231                     MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
232     {
233         mbedtls_printf( " failed\n  ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
234         goto exit;
235     }
236 
237     mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
238     mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
239 
240 #if defined(MBEDTLS_SSL_CACHE_C)
241     mbedtls_ssl_conf_session_cache( &conf, &cache,
242                                    mbedtls_ssl_cache_get,
243                                    mbedtls_ssl_cache_set );
244 #endif
245 
246     mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
247     if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
248     {
249         mbedtls_printf( " failed\n  ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
250         goto exit;
251     }
252 
253     if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
254     {
255         mbedtls_printf( " failed\n  ! mbedtls_ssl_setup returned %d\n\n", ret );
256         goto exit;
257     }
258 
259     mbedtls_printf( " ok\n" );
260 
261 reset:
262 #ifdef MBEDTLS_ERROR_C
263     if( ret != 0 )
264     {
265         char error_buf[100];
266         mbedtls_strerror( ret, error_buf, 100 );
267         mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
268     }
269 #endif
270 
271     mbedtls_net_free( &client_fd );
272 
273     mbedtls_ssl_session_reset( &ssl );
274 
275     /*
276      * 3. Wait until a client connects
277      */
278     mbedtls_printf( "  . Waiting for a remote connection ..." );
279     fflush( stdout );
280 
281     if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
282                                     NULL, 0, NULL ) ) != 0 )
283     {
284         mbedtls_printf( " failed\n  ! mbedtls_net_accept returned %d\n\n", ret );
285         goto exit;
286     }
287 
288     mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
289 
290     mbedtls_printf( " ok\n" );
291 
292     /*
293      * 5. Handshake
294      */
295     mbedtls_printf( "  . Performing the SSL/TLS handshake..." );
296     fflush( stdout );
297 
298     while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
299     {
300         if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
301         {
302             mbedtls_printf( " failed\n  ! mbedtls_ssl_handshake returned %d\n\n", ret );
303             goto reset;
304         }
305     }
306 
307     mbedtls_printf( " ok\n" );
308 
309     /*
310      * 6. Read the HTTP Request
311      */
312     mbedtls_printf( "  < Read from client:" );
313     fflush( stdout );
314 
315     do
316     {
317         len = sizeof( buf ) - 1;
318         memset( buf, 0, sizeof( buf ) );
319         ret = mbedtls_ssl_read( &ssl, buf, len );
320 
321         if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
322             continue;
323 
324         if( ret <= 0 )
325         {
326             switch( ret )
327             {
328                 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
329                     mbedtls_printf( " connection was closed gracefully\n" );
330                     break;
331 
332                 case MBEDTLS_ERR_NET_CONN_RESET:
333                     mbedtls_printf( " connection was reset by peer\n" );
334                     break;
335 
336                 default:
337                     mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
338                     break;
339             }
340 
341             break;
342         }
343 
344         len = ret;
345         mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
346 
347         if( ret > 0 )
348             break;
349     }
350     while( 1 );
351 
352     /*
353      * 7. Write the 200 Response
354      */
355     mbedtls_printf( "  > Write to client:" );
356     fflush( stdout );
357 
358     len = sprintf( (char *) buf, HTTP_RESPONSE,
359                    mbedtls_ssl_get_ciphersuite( &ssl ) );
360 
361     while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
362     {
363         if( ret == MBEDTLS_ERR_NET_CONN_RESET )
364         {
365             mbedtls_printf( " failed\n  ! peer closed the connection\n\n" );
366             goto reset;
367         }
368 
369         if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
370         {
371             mbedtls_printf( " failed\n  ! mbedtls_ssl_write returned %d\n\n", ret );
372             goto exit;
373         }
374     }
375 
376     len = ret;
377     mbedtls_printf( " %d bytes written\n\n%s\n", len, (char *) buf );
378 
379     mbedtls_printf( "  . Closing the connection..." );
380 
381     while( ( ret = mbedtls_ssl_close_notify( &ssl ) ) < 0 )
382     {
383         if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
384             ret != MBEDTLS_ERR_SSL_WANT_WRITE )
385         {
386             mbedtls_printf( " failed\n  ! mbedtls_ssl_close_notify returned %d\n\n", ret );
387             goto reset;
388         }
389     }
390 
391     mbedtls_printf( " ok\n" );
392 
393     ret = 0;
394     goto reset;
395 
396 exit:
397 
398 #ifdef MBEDTLS_ERROR_C
399     if( ret != 0 )
400     {
401         char error_buf[100];
402         mbedtls_strerror( ret, error_buf, 100 );
403         mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
404     }
405 #endif
406 
407     mbedtls_net_free( &client_fd );
408     mbedtls_net_free( &listen_fd );
409 
410     mbedtls_x509_crt_free( &srvcert );
411     mbedtls_pk_free( &pkey );
412     mbedtls_ssl_free( &ssl );
413     mbedtls_ssl_config_free( &conf );
414 #if defined(MBEDTLS_SSL_CACHE_C)
415     mbedtls_ssl_cache_free( &cache );
416 #endif
417     mbedtls_ctr_drbg_free( &ctr_drbg );
418     mbedtls_entropy_free( &entropy );
419 
420 #if defined(_WIN32)
421     mbedtls_printf( "  Press Enter to exit this program.\n" );
422     fflush( stdout ); getchar();
423 #endif
424 
425     mbedtls_exit( ret );
426 }
427 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
428           MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
429           MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C
430           && MBEDTLS_FS_IO && MBEDTLS_PEM_PARSE_C */
431