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