ssl_server.c 11.2 KB
Newer Older
1 2 3
/*
 *  SSL server demonstration program
 *
4
 *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5
 *  SPDX-License-Identifier: Apache-2.0
6
 *
7 8 9
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
 *  not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
10
 *
11
 *  http://www.apache.org/licenses/LICENSE-2.0
12
 *
13 14 15 16 17
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  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
Rich Evans's avatar
Rich Evans committed
31
#include <stdio.h>
32 33 34
#include <stdlib.h>
#define mbedtls_time       time
#define mbedtls_time_t     time_t 
35 36
#define mbedtls_fprintf    fprintf
#define mbedtls_printf     printf
37 38
#endif

39 40 41 42 43 44
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) ||    \
    !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
    !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) ||     \
    !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) ||    \
    !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
    !defined(MBEDTLS_PEM_PARSE_C)
45 46
int main( void )
{
47 48 49 50 51
    mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
           "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
           "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
           "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
           "and/or MBEDTLS_PEM_PARSE_C not defined.\n");
52 53 54 55 56 57 58
    return( 0 );
}
#else

#include <stdlib.h>
#include <string.h>

59
#if defined(_WIN32)
60 61 62
#include <windows.h>
#endif

63 64 65 66 67
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/certs.h"
#include "mbedtls/x509.h"
#include "mbedtls/ssl.h"
68
#include "mbedtls/net_sockets.h"
69 70
#include "mbedtls/error.h"
#include "mbedtls/debug.h"
71

72
#if defined(MBEDTLS_SSL_CACHE_C)
73
#include "mbedtls/ssl_cache.h"
74 75
#endif

Rich Evans's avatar
Rich Evans committed
76 77 78 79 80 81 82
#define HTTP_RESPONSE \
    "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
    "<h2>mbed TLS Test Server</h2>\r\n" \
    "<p>Successful connection using: %s</p>\r\n"

#define DEBUG_LEVEL 0

83 84 85
static void my_debug( void *ctx, int level,
                      const char *file, int line,
                      const char *str )
86
{
87 88
    ((void) level);

89
    mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
90
    fflush(  (FILE *) ctx  );
91 92
}

Rich Evans's avatar
Rich Evans committed
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 101 102
    mbedtls_entropy_context entropy;
    mbedtls_ctr_drbg_context ctr_drbg;
    mbedtls_ssl_context ssl;
103
    mbedtls_ssl_config conf;
104 105 106 107
    mbedtls_x509_crt srvcert;
    mbedtls_pk_context pkey;
#if defined(MBEDTLS_SSL_CACHE_C)
    mbedtls_ssl_cache_context cache;
108
#endif
109

110 111
    mbedtls_net_init( &listen_fd );
    mbedtls_net_init( &client_fd );
112
    mbedtls_ssl_init( &ssl );
113
    mbedtls_ssl_config_init( &conf );
114 115
#if defined(MBEDTLS_SSL_CACHE_C)
    mbedtls_ssl_cache_init( &cache );
116
#endif
117 118 119
    mbedtls_x509_crt_init( &srvcert );
    mbedtls_pk_init( &pkey );
    mbedtls_entropy_init( &entropy );
120
    mbedtls_ctr_drbg_init( &ctr_drbg );
121

122 123
#if defined(MBEDTLS_DEBUG_C)
    mbedtls_debug_set_threshold( DEBUG_LEVEL );
124 125
#endif

126 127 128
    /*
     * 1. Load the certificates and private RSA key
     */
129
    mbedtls_printf( "\n  . Loading the server cert. and key..." );
130 131 132 133
    fflush( stdout );

    /*
     * This demonstration program uses embedded test certificates.
134 135
     * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
     * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
136
     */
137 138
    ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_srv_crt,
                          mbedtls_test_srv_crt_len );
139 140
    if( ret != 0 )
    {
141
        mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned %d\n\n", ret );
142 143 144
        goto exit;
    }

145 146
    ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) mbedtls_test_cas_pem,
                          mbedtls_test_cas_pem_len );
147 148
    if( ret != 0 )
    {
149
        mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned %d\n\n", ret );
150 151 152
        goto exit;
    }

153 154
    ret =  mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_srv_key,
                         mbedtls_test_srv_key_len, NULL, 0 );
155 156
    if( ret != 0 )
    {
157
        mbedtls_printf( " failed\n  !  mbedtls_pk_parse_key returned %d\n\n", ret );
158 159 160
        goto exit;
    }

161
    mbedtls_printf( " ok\n" );
162 163 164 165

    /*
     * 2. Setup the listening TCP socket
     */
166
    mbedtls_printf( "  . Bind on https://localhost:4433/ ..." );
167 168
    fflush( stdout );

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 173 174
        goto exit;
    }

175
    mbedtls_printf( " ok\n" );
176 177

    /*
178
     * 3. Seed the RNG
179
     */
180
    mbedtls_printf( "  . Seeding the random number generator..." );
181 182
    fflush( stdout );

183
    if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
184 185
                               (const unsigned char *) pers,
                               strlen( pers ) ) ) != 0 )
186
    {
187
        mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
188 189 190
        goto exit;
    }

191
    mbedtls_printf( " ok\n" );
192 193 194 195

    /*
     * 4. Setup stuff
     */
196
    mbedtls_printf( "  . Setting up the SSL data...." );
197
    fflush( stdout );
198

199 200
    if( ( ret = mbedtls_ssl_config_defaults( &conf,
                    MBEDTLS_SSL_IS_SERVER,
201 202
                    MBEDTLS_SSL_TRANSPORT_STREAM,
                    MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
203 204 205 206 207
    {
        mbedtls_printf( " failed\n  ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
        goto exit;
    }

208 209
    mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
    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 214
                                   mbedtls_ssl_cache_get,
                                   mbedtls_ssl_cache_set );
215
#endif
216

217 218
    mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
    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 222
        goto exit;
    }
223

224 225 226 227 228 229
    if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
    {
        mbedtls_printf( " failed\n  ! mbedtls_ssl_setup returned %d\n\n", ret );
        goto exit;
    }

230
    mbedtls_printf( " ok\n" );
231 232

reset:
233
#ifdef MBEDTLS_ERROR_C
234 235 236
    if( ret != 0 )
    {
        char error_buf[100];
237 238
        mbedtls_strerror( ret, error_buf, 100 );
        mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
239 240 241
    }
#endif

242
    mbedtls_net_free( &client_fd );
243

244
    mbedtls_ssl_session_reset( &ssl );
245 246 247 248

    /*
     * 3. Wait until a client connects
     */
249
    mbedtls_printf( "  . Waiting for a remote connection ..." );
250 251
    fflush( stdout );

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 257 258
        goto exit;
    }

259
    mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
260

261
    mbedtls_printf( " ok\n" );
262

263 264 265
    /*
     * 5. Handshake
     */
266
    mbedtls_printf( "  . Performing the SSL/TLS handshake..." );
267 268
    fflush( stdout );

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 282

    /*
     * 6. Read the HTTP Request
     */
283
    mbedtls_printf( "  < Read from client:" );
284 285 286 287 288 289
    fflush( stdout );

    do
    {
        len = sizeof( buf ) - 1;
        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 294 295 296 297 298
            continue;

        if( ret <= 0 )
        {
            switch( ret )
            {
299 300
                case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
                    mbedtls_printf( " connection was closed gracefully\n" );
301 302
                    break;

303 304
                case MBEDTLS_ERR_NET_CONN_RESET:
                    mbedtls_printf( " connection was reset by peer\n" );
305 306 307
                    break;

                default:
308
                    mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
309 310 311 312 313 314 315
                    break;
            }

            break;
        }

        len = ret;
316
        mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
317 318 319

        if( ret > 0 )
            break;
320
    }
321
    while( 1 );
322 323 324 325

    /*
     * 7. Write the 200 Response
     */
326
    mbedtls_printf( "  > Write to client:" );
327 328 329
    fflush( stdout );

    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 344 345 346 347
            goto exit;
        }
    }

    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 355
        if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
            ret != MBEDTLS_ERR_SSL_WANT_WRITE )
356
        {
357
            mbedtls_printf( " failed\n  ! mbedtls_ssl_close_notify returned %d\n\n", ret );
358 359 360 361
            goto reset;
        }
    }

362
    mbedtls_printf( " ok\n" );
363

364
    ret = 0;
365
    goto reset;
366 367 368

exit:

369
#ifdef MBEDTLS_ERROR_C
370 371 372
    if( ret != 0 )
    {
        char error_buf[100];
373 374
        mbedtls_strerror( ret, error_buf, 100 );
        mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
375 376 377
    }
#endif

378 379
    mbedtls_net_free( &client_fd );
    mbedtls_net_free( &listen_fd );
380

381 382 383
    mbedtls_x509_crt_free( &srvcert );
    mbedtls_pk_free( &pkey );
    mbedtls_ssl_free( &ssl );
384
    mbedtls_ssl_config_free( &conf );
385 386
#if defined(MBEDTLS_SSL_CACHE_C)
    mbedtls_ssl_cache_free( &cache );
387
#endif
388 389
    mbedtls_ctr_drbg_free( &ctr_drbg );
    mbedtls_entropy_free( &entropy );
390

391
#if defined(_WIN32)
392
    mbedtls_printf( "  Press Enter to exit this program.\n" );
393 394 395 396 397
    fflush( stdout ); getchar();
#endif

    return( ret );
}
398 399 400 401
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
          MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
          MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C
          && MBEDTLS_FS_IO && MBEDTLS_PEM_PARSE_C */