ssl_srv.c 85.9 KB
Newer Older
1 2 3
/*
 *  SSLv3/TLSv1 server-side functions
 *
4
 *  Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakker's avatar
Paul Bakker committed
5 6
 *
 *  This file is part of PolarSSL (http://www.polarssl.org)
7
 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker's avatar
Paul Bakker committed
8
 *
9
 *  All rights reserved.
Paul Bakker's avatar
Paul Bakker committed
10
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

26
#include "polarssl/config.h"
27

28
#if defined(POLARSSL_SSL_SRV_C)
29

30 31
#include "polarssl/debug.h"
#include "polarssl/ssl.h"
32 33 34
#if defined(POLARSSL_ECP_C)
#include "polarssl/ecp.h"
#endif
35

36 37 38 39 40 41 42
#if defined(POLARSSL_MEMORY_C)
#include "polarssl/memory.h"
#else
#define polarssl_malloc     malloc
#define polarssl_free       free
#endif

43 44
#include <stdlib.h>
#include <stdio.h>
45 46

#if defined(POLARSSL_HAVE_TIME)
47
#include <time.h>
48
#endif
49

50
#if defined(POLARSSL_SSL_SESSION_TICKETS)
51 52 53 54 55 56 57 58
/*
 * Serialize a session in the following format:
 *  0   .   n-1     session structure, n = sizeof(ssl_session)
 *  n   .   n+2     peer_cert length = m (0 if no certificate)
 *  n+3 .   n+2+m   peer cert ASN.1
 *
 *  Assumes ticket is NULL (always true on server side).
 */
59 60 61
static int ssl_save_session( const ssl_session *session,
                             unsigned char *buf, size_t buf_len,
                             size_t *olen )
62 63
{
    unsigned char *p = buf;
64
    size_t left = buf_len;
65
#if defined(POLARSSL_X509_CRT_PARSE_C)
66
    size_t cert_len;
67
#endif /* POLARSSL_X509_CRT_PARSE_C */
68

69 70 71
    if( left < sizeof( ssl_session ) )
        return( -1 );

72 73
    memcpy( p, session, sizeof( ssl_session ) );
    p += sizeof( ssl_session );
74
    left -= sizeof( ssl_session );
75

76
#if defined(POLARSSL_X509_CRT_PARSE_C)
77 78 79 80 81 82 83
    ((ssl_session *) buf)->peer_cert = NULL;

    if( session->peer_cert == NULL )
        cert_len = 0;
    else
        cert_len = session->peer_cert->raw.len;

84 85 86
    if( left < 3 + cert_len )
        return( -1 );

87 88 89 90 91 92 93 94
    *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
    *p++ = (unsigned char)( cert_len >>  8 & 0xFF );
    *p++ = (unsigned char)( cert_len       & 0xFF );

    if( session->peer_cert != NULL )
        memcpy( p, session->peer_cert->raw.p, cert_len );

    p += cert_len;
95
#endif /* POLARSSL_X509_CRT_PARSE_C */
96 97

    *olen = p - buf;
98 99

    return( 0 );
100 101 102 103 104 105 106 107 108 109
}

/*
 * Unserialise session, see ssl_save_session()
 */
static int ssl_load_session( ssl_session *session,
                             const unsigned char *buf, size_t len )
{
    const unsigned char *p = buf;
    const unsigned char * const end = buf + len;
110
#if defined(POLARSSL_X509_CRT_PARSE_C)
111
    size_t cert_len;
112
#endif /* POLARSSL_X509_CRT_PARSE_C */
113 114 115 116 117 118 119

    if( p + sizeof( ssl_session ) > end )
        return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );

    memcpy( session, p, sizeof( ssl_session ) );
    p += sizeof( ssl_session );

120
#if defined(POLARSSL_X509_CRT_PARSE_C)
121 122 123 124 125 126 127 128 129 130 131 132
    if( p + 3 > end )
        return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );

    cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
    p += 3;

    if( cert_len == 0 )
    {
        session->peer_cert = NULL;
    }
    else
    {
133 134
        int ret;

135 136 137
        if( p + cert_len > end )
            return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );

138
        session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
139 140 141 142

        if( session->peer_cert == NULL )
            return( POLARSSL_ERR_SSL_MALLOC_FAILED );

143
        memset( session->peer_cert, 0, sizeof( x509_crt ) );
144

145
        if( ( ret = x509_crt_parse( session->peer_cert, p, cert_len ) ) != 0 )
146
        {
147
            x509_crt_free( session->peer_cert );
148 149 150 151 152 153 154
            polarssl_free( session->peer_cert );
            session->peer_cert = NULL;
            return( ret );
        }

        p += cert_len;
    }
155
#endif /* POLARSSL_X509_CRT_PARSE_C */
156 157 158 159 160 161 162

    if( p != end )
        return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );

    return( 0 );
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176
/*
 * Create session ticket, secured as recommended in RFC 5077 section 4:
 *
 *    struct {
 *        opaque key_name[16];
 *        opaque iv[16];
 *        opaque encrypted_state<0..2^16-1>;
 *        opaque mac[32];
 *    } ticket;
 *
 * (the internal state structure differs, however).
 */
static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
{
177
    int ret;
178 179
    unsigned char * const start = ssl->out_msg + 10;
    unsigned char *p = start;
180 181 182
    unsigned char *state;
    unsigned char iv[16];
    size_t clear_len, enc_len, pad_len, i;
183

184 185
    *tlen = 0;

186 187 188
    if( ssl->ticket_keys == NULL )
        return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );

189
    /* Write key name */
190
    memcpy( p, ssl->ticket_keys->key_name, 16 );
191 192
    p += 16;

193 194 195 196
    /* Generate and write IV (with a copy for aes_crypt) */
    if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
        return( ret );
    memcpy( iv, p, 16 );
197 198
    p += 16;

199 200 201 202 203 204
    /*
     * Dump session state
     *
     * After the session state itself, we still need room for 16 bytes of
     * padding and 32 bytes of MAC, so there's only so much room left
     */
205
    state = p + 2;
206 207 208 209 210 211
    if( ssl_save_session( ssl->session_negotiate, state,
                          SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
                          &clear_len ) != 0 )
    {
        return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
    }
212
    SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
213

214 215 216 217 218
    /* Apply PKCS padding */
    pad_len = 16 - clear_len % 16;
    enc_len = clear_len + pad_len;
    for( i = clear_len; i < enc_len; i++ )
        state[i] = (unsigned char) pad_len;
219

220 221 222 223 224 225 226 227 228 229 230
    /* Encrypt */
    if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
                               enc_len, iv, state, state ) ) != 0 )
    {
        return( ret );
    }

    /* Write length */
    *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
    *p++ = (unsigned char)( ( enc_len      ) & 0xFF );
    p = state + enc_len;
231

232 233
    /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
    sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
234 235 236 237
    p += 32;

    *tlen = p - start;

238
    SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
239 240 241 242 243 244 245 246

    return( 0 );
}

/*
 * Load session ticket (see ssl_write_ticket for structure)
 */
static int ssl_parse_ticket( ssl_context *ssl,
247
                             unsigned char *buf,
248 249 250 251
                             size_t len )
{
    int ret;
    ssl_session session;
252 253 254 255 256
    unsigned char *key_name = buf;
    unsigned char *iv = buf + 16;
    unsigned char *enc_len_p = iv + 16;
    unsigned char *ticket = enc_len_p + 2;
    unsigned char *mac;
257
    unsigned char computed_mac[16];
258 259 260 261
    size_t enc_len, clear_len, i;
    unsigned char pad_len;

    SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
262

263
    if( len < 34 || ssl->ticket_keys == NULL )
264 265 266 267 268 269 270 271
        return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );

    enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
    mac = ticket + enc_len;

    if( len != enc_len + 66 )
        return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );

272
    /* Check name */
273 274
    if( memcmp( key_name, ssl->ticket_keys->key_name, 16 ) != 0 )
        return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
275

276 277 278 279 280 281 282 283 284
    /* Check mac */
    sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
                 computed_mac, 0 );
    ret = 0;
    for( i = 0; i < 32; i++ )
        if( mac[i] != computed_mac[i] )
            ret = POLARSSL_ERR_SSL_INVALID_MAC;
    if( ret != 0 )
        return( ret );
285

286 287 288 289 290 291
    /* Decrypt */
    if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
                               enc_len, iv, ticket, ticket ) ) != 0 )
    {
        return( ret );
    }
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    /* Check PKCS padding */
    pad_len = ticket[enc_len - 1];

    ret = 0;
    for( i = 2; i < pad_len; i++ )
        if( ticket[enc_len - i] != pad_len )
            ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
    if( ret != 0 )
        return( ret );

    clear_len = enc_len - pad_len;

    SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );

    /* Actually load session */
308 309 310 311 312 313 314
    if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
    {
        SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
        memset( &session, 0, sizeof( ssl_session ) );
        return( ret );
    }

315 316 317 318 319 320 321 322 323 324
#if defined(POLARSSL_HAVE_TIME)
    /* Check if still valid */
    if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
    {
        SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
        memset( &session, 0, sizeof( ssl_session ) );
        return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
    }
#endif

325 326 327 328 329 330 331 332 333 334 335 336 337
    /*
     * Keep the session ID sent by the client, since we MUST send it back to
     * inform him we're accepting the ticket  (RFC 5077 section 3.4)
     */
    session.length = ssl->session_negotiate->length;
    memcpy( &session.id, ssl->session_negotiate->id, session.length );

    ssl_session_free( ssl->session_negotiate );
    memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
    memset( &session, 0, sizeof( ssl_session ) );

    return( 0 );
}
338
#endif /* POLARSSL_SSL_SESSION_TICKETS */
339

340
#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
341
static int ssl_parse_servername_ext( ssl_context *ssl,
342
                                     const unsigned char *buf,
343 344 345 346
                                     size_t len )
{
    int ret;
    size_t servername_list_size, hostname_len;
347
    const unsigned char *p;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

    servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
    if( servername_list_size + 2 != len )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    p = buf + 2;
    while( servername_list_size > 0 )
    {
        hostname_len = ( ( p[1] << 8 ) | p[2] );
        if( hostname_len + 3 > servername_list_size )
        {
            SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
            return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
        }

        if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
        {
            ret = ssl->f_sni( ssl->p_sni, ssl, p + 3, hostname_len );
            if( ret != 0 )
            {
                ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
                        SSL_ALERT_MSG_UNRECOGNIZED_NAME );
                return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
            }
375
            return( 0 );
376 377 378
        }

        servername_list_size -= hostname_len + 3;
379 380 381 382 383 384 385
        p += hostname_len + 3;
    }

    if( servername_list_size != 0 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
386 387 388 389
    }

    return( 0 );
}
390
#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
391

392
static int ssl_parse_renegotiation_info( ssl_context *ssl,
393
                                         const unsigned char *buf,
394 395
                                         size_t len )
{
396 397
    int ret;

398 399 400 401 402
    if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
    {
        if( len != 1 || buf[0] != 0x0 )
        {
            SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
403 404 405 406

            if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
                return( ret );

407 408 409 410 411 412 413 414 415 416 417 418
            return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
        }

        ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
    }
    else
    {
        if( len    != 1 + ssl->verify_data_len ||
            buf[0] !=     ssl->verify_data_len ||
            memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
        {
            SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
419 420 421 422

            if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
                return( ret );

423 424 425 426 427 428 429
            return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
        }
    }

    return( 0 );
}

430
#if defined(POLARSSL_SSL_PROTO_TLS1_2)
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
                                               const unsigned char *buf,
                                               size_t len )
{
    size_t sig_alg_list_size;
    const unsigned char *p;

    sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
    if( sig_alg_list_size + 2 != len ||
        sig_alg_list_size %2 != 0 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    p = buf + 2;
    while( sig_alg_list_size > 0 )
    {
449 450 451 452
        /*
         * For now, just ignore signature algorithm and rely on offered
         * ciphersuites only. To be fixed later.
         */
453
#if defined(POLARSSL_SHA512_C)
454 455 456 457 458 459 460 461 462 463 464
        if( p[0] == SSL_HASH_SHA512 )
        {
            ssl->handshake->sig_alg = SSL_HASH_SHA512;
            break;
        }
        if( p[0] == SSL_HASH_SHA384 )
        {
            ssl->handshake->sig_alg = SSL_HASH_SHA384;
            break;
        }
#endif
465
#if defined(POLARSSL_SHA256_C)
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
        if( p[0] == SSL_HASH_SHA256 )
        {
            ssl->handshake->sig_alg = SSL_HASH_SHA256;
            break;
        }
        if( p[0] == SSL_HASH_SHA224 )
        {
            ssl->handshake->sig_alg = SSL_HASH_SHA224;
            break;
        }
#endif
        if( p[0] == SSL_HASH_SHA1 )
        {
            ssl->handshake->sig_alg = SSL_HASH_SHA1;
            break;
        }
        if( p[0] == SSL_HASH_MD5 )
        {
            ssl->handshake->sig_alg = SSL_HASH_MD5;
            break;
        }

        sig_alg_list_size -= 2;
        p += 2;
    }

    SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
                   ssl->handshake->sig_alg ) );

    return( 0 );
}
497
#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498

499
#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
500 501 502
static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
                                                const unsigned char *buf,
                                                size_t len )
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
{
    size_t list_size;
    const unsigned char *p;

    list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
    if( list_size + 2 != len ||
        list_size % 2 != 0 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    p = buf + 2;
    while( list_size > 0 )
    {
518 519
#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
        if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP192R1 )
520 521 522 523
        {
            ssl->handshake->ec_curve = p[1];
            return( 0 );
        }
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
#endif
#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
        if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP224R1 )
        {
            ssl->handshake->ec_curve = p[1];
            return( 0 );
        }
#endif
#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
        if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP256R1 )
        {
            ssl->handshake->ec_curve = p[1];
            return( 0 );
        }
#endif
#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
        if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP384R1 )
        {
            ssl->handshake->ec_curve = p[1];
            return( 0 );
        }
#endif
#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
        if( p[0] == 0x00 && p[1] == POLARSSL_ECP_DP_SECP521R1 )
        {
            ssl->handshake->ec_curve = p[1];
            return( 0 );
        }
#endif
553 554 555 556 557 558 559 560

        list_size -= 2;
        p += 2;
    }

    return( 0 );
}

561 562 563
static int ssl_parse_supported_point_formats( ssl_context *ssl,
                                              const unsigned char *buf,
                                              size_t len )
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
{
    size_t list_size;
    const unsigned char *p;

    list_size = buf[0];
    if( list_size + 1 != len )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    p = buf + 2;
    while( list_size > 0 )
    {
        if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
            p[0] == POLARSSL_ECP_PF_COMPRESSED )
        {
581
            ssl->handshake->ecdh_ctx.point_format = p[0];
582
            SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
583 584 585 586 587 588 589 590 591
            return( 0 );
        }

        list_size--;
        p++;
    }

    return( 0 );
}
592
#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
593

594
#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
595 596 597 598
static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
                                              const unsigned char *buf,
                                              size_t len )
{
599
    if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
600 601 602 603 604
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

605 606
    ssl->session_negotiate->mfl_code = buf[0];

607 608
    return( 0 );
}
609
#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
610

611
#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
                                         const unsigned char *buf,
                                         size_t len )
{
    if( len != 0 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    ((void) buf);

    ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;

    return( 0 );
}
628
#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
629

630
#if defined(POLARSSL_SSL_SESSION_TICKETS)
631
static int ssl_parse_session_ticket_ext( ssl_context *ssl,
632
                                         unsigned char *buf,
633 634
                                         size_t len )
{
635 636
    int ret;

637 638 639
    if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
        return( 0 );

640
    /* Remember the client asked us to send a new ticket */
641 642
    ssl->handshake->new_session_ticket = 1;

643 644
    SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );

645 646 647
    if( len == 0 )
        return( 0 );

648 649 650 651 652
    if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
    {
        SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
        return( 0 );
    }
653 654 655 656

    /*
     * Failures are ok: just ignore the ticket and proceed.
     */
657 658 659
    if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
    {
        SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
660
        return( 0 );
661
    }
662 663 664 665

    SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );

    ssl->handshake->resume = 1;
666

667 668 669
    /* Don't send a new ticket after all, this one is OK */
    ssl->handshake->new_session_ticket = 0;

670 671
    return( 0 );
}
672
#endif /* POLARSSL_SSL_SESSION_TICKETS */
673

674 675 676 677 678 679 680 681
#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
static int ssl_parse_client_hello_v2( ssl_context *ssl )
{
    int ret;
    unsigned int i, j;
    size_t n;
    unsigned int ciph_len, sess_len, chal_len;
    unsigned char *buf, *p;
682
    const int *ciphersuites;
683
    const ssl_ciphersuite_t *ciphersuite_info;
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733

    SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );

    if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
    {
        SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );

        if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
            return( ret );

        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    buf = ssl->in_hdr;

    SSL_DEBUG_BUF( 4, "record header", buf, 5 );

    SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
                   buf[2] ) );
    SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
                   ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
    SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
                   buf[3], buf[4] ) );

    /*
     * SSLv2 Client Hello
     *
     * Record layer:
     *     0  .   1   message length
     *
     * SSL layer:
     *     2  .   2   message type
     *     3  .   4   protocol version
     */
    if( buf[2] != SSL_HS_CLIENT_HELLO ||
        buf[3] != SSL_MAJOR_VERSION_3 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;

    if( n < 17 || n > 512 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    ssl->major_ver = SSL_MAJOR_VERSION_3;
734 735
    ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
                     ? buf[4]  : ssl->max_minor_ver;
736 737 738 739 740 741 742 743 744 745 746 747

    if( ssl->minor_ver < ssl->min_minor_ver )
    {
        SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
                            " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
                            ssl->min_major_ver, ssl->min_minor_ver ) );

        ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
                                     SSL_ALERT_MSG_PROTOCOL_VERSION );
        return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
    }

748 749
    ssl->handshake->max_major_ver = buf[3];
    ssl->handshake->max_minor_ver = buf[4];
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843

    if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
    {
        SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
        return( ret );
    }

    ssl->handshake->update_checksum( ssl, buf + 2, n );

    buf = ssl->in_msg;
    n = ssl->in_left - 5;

    /*
     *    0  .   1   ciphersuitelist length
     *    2  .   3   session id length
     *    4  .   5   challenge length
     *    6  .  ..   ciphersuitelist
     *   ..  .  ..   session id
     *   ..  .  ..   challenge
     */
    SSL_DEBUG_BUF( 4, "record contents", buf, n );

    ciph_len = ( buf[0] << 8 ) | buf[1];
    sess_len = ( buf[2] << 8 ) | buf[3];
    chal_len = ( buf[4] << 8 ) | buf[5];

    SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
                   ciph_len, sess_len, chal_len ) );

    /*
     * Make sure each parameter length is valid
     */
    if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    if( sess_len > 32 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    if( chal_len < 8 || chal_len > 32 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    if( n != 6 + ciph_len + sess_len + chal_len )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
                   buf + 6, ciph_len );
    SSL_DEBUG_BUF( 3, "client hello, session id",
                   buf + 6 + ciph_len, sess_len );
    SSL_DEBUG_BUF( 3, "client hello, challenge",
                   buf + 6 + ciph_len + sess_len, chal_len );

    p = buf + 6 + ciph_len;
    ssl->session_negotiate->length = sess_len;
    memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
    memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );

    p += sess_len;
    memset( ssl->handshake->randbytes, 0, 64 );
    memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );

    /*
     * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
     */
    for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
    {
        if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
        {
            SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
            if( ssl->renegotiation == SSL_RENEGOTIATION )
            {
                SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );

                if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
                    return( ret );

                return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
            }
            ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
            break;
        }
    }

844 845
    ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
    for( i = 0; ciphersuites[i] != 0; i++ )
846 847 848
    {
        for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
        {
849 850
            // Only allow non-ECC ciphersuites as we do not have extensions
            //
851
            if( p[0] == 0 && p[1] == 0 &&
852 853
                ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
                p[2] == ( ciphersuites[i] & 0xFF ) )
854 855 856 857 858 859 860 861 862 863
            {
                ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );

                if( ciphersuite_info == NULL )
                {
                    SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
                                   ciphersuites[i] ) );
                    return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
                }

864 865 866
                if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
                    ciphersuite_info->max_minor_ver < ssl->minor_ver )
                    continue;
867

868
                goto have_ciphersuite_v2;
869
            }
870 871 872 873 874 875 876 877
        }
    }

    SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );

    return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );

have_ciphersuite_v2:
878
    ssl->session_negotiate->ciphersuite = ciphersuites[i];
879
    ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
880
    ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904

    /*
     * SSLv2 Client Hello relevant renegotiation security checks
     */
    if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
        ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
    {
        SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );

        if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
            return( ret );

        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }

    ssl->in_left = 0;
    ssl->state++;

    SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );

    return( 0 );
}
#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */

905 906
static int ssl_parse_client_hello( ssl_context *ssl )
{
907 908 909 910
    int ret;
    unsigned int i, j;
    size_t n;
    unsigned int ciph_len, sess_len;
911
    unsigned int comp_len;
912 913
    unsigned int ext_len = 0;
    unsigned char *buf, *p, *ext;
914 915
    int renegotiation_info_seen = 0;
    int handshake_failure = 0;
916
    const int *ciphersuites;
917
    const ssl_ciphersuite_t *ciphersuite_info;
918
    pk_type_t pk_alg;
919 920 921

    SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );

922 923
    if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
        ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
924 925 926 927 928 929 930
    {
        SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
        return( ret );
    }

    buf = ssl->in_hdr;

931 932 933 934 935
#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
    if( ( buf[0] & 0x80 ) != 0 )
        return ssl_parse_client_hello_v2( ssl );
#endif

936
    SSL_DEBUG_BUF( 4, "record header", buf, 5 );
937

938 939 940 941 942 943
    SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
                   buf[0] ) );
    SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
                   ( buf[3] << 8 ) | buf[4] ) );
    SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
                   buf[1], buf[2] ) );
944

945 946 947 948 949 950 951 952 953 954 955 956 957 958
    /*
     * SSLv3 Client Hello
     *
     * Record layer:
     *     0  .   0   message type
     *     1  .   2   protocol version
     *     3  .   4   message length
     */
    if( buf[0] != SSL_MSG_HANDSHAKE ||
        buf[1] != SSL_MAJOR_VERSION_3 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }
959

960
    n = ( buf[3] << 8 ) | buf[4];
961

962
    if( n < 45 || n > 2048 )
963 964 965
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
966
    }
967

968 969
    if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
        ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
970
    {
971 972 973
        SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
        return( ret );
    }
974

975
    buf = ssl->in_msg;
976 977 978 979
    if( !ssl->renegotiation )
        n = ssl->in_left - 5;
    else
        n = ssl->in_msglen;
980

981
    ssl->handshake->update_checksum( ssl, buf, n );
982

983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    /*
     * SSL layer:
     *     0  .   0   handshake type
     *     1  .   3   handshake length
     *     4  .   5   protocol version
     *     6  .   9   UNIX time()
     *    10  .  37   random bytes
     *    38  .  38   session id length
     *    39  . 38+x  session id
     *   39+x . 40+x  ciphersuitelist length
     *   41+x .  ..   ciphersuitelist
     *    ..  .  ..   compression alg.
     *    ..  .  ..   extensions
     */
    SSL_DEBUG_BUF( 4, "record contents", buf, n );

    SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
                   buf[0] ) );
    SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
                   ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
    SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
                   buf[4], buf[5] ) );
1005

1006 1007 1008 1009 1010 1011 1012 1013 1014
    /*
     * Check the handshake type and protocol version
     */
    if( buf[0] != SSL_HS_CLIENT_HELLO ||
        buf[4] != SSL_MAJOR_VERSION_3 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }
1015

1016
    ssl->major_ver = SSL_MAJOR_VERSION_3;
1017 1018
    ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
                     ? buf[5]  : ssl->max_minor_ver;
1019

1020 1021 1022 1023
    if( ssl->minor_ver < ssl->min_minor_ver )
    {
        SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
                            " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
1024
                            ssl->min_major_ver, ssl->min_minor_ver ) );
1025 1026 1027 1028 1029 1030 1031

        ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
                                     SSL_ALERT_MSG_PROTOCOL_VERSION );

        return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
    }

1032 1033
    ssl->handshake->max_major_ver = buf[4];
    ssl->handshake->max_minor_ver = buf[5];
1034

1035
    memcpy( ssl->handshake->randbytes, buf + 6, 32 );
1036

1037 1038 1039 1040 1041 1042 1043 1044
    /*
     * Check the handshake message length
     */
    if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }
1045

1046 1047 1048 1049
    /*
     * Check the session length
     */
    sess_len = buf[38];
1050

1051 1052 1053 1054 1055
    if( sess_len > 32 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }
1056

1057 1058 1059 1060 1061
    ssl->session_negotiate->length = sess_len;
    memset( ssl->session_negotiate->id, 0,
            sizeof( ssl->session_negotiate->id ) );
    memcpy( ssl->session_negotiate->id, buf + 39,
            ssl->session_negotiate->length );
1062

1063 1064 1065 1066 1067
    /*
     * Check the ciphersuitelist length
     */
    ciph_len = ( buf[39 + sess_len] << 8 )
             | ( buf[40 + sess_len]      );
1068

1069 1070 1071 1072 1073
    if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }
1074

1075 1076 1077 1078
    /*
     * Check the compression algorithms length
     */
    comp_len = buf[41 + sess_len + ciph_len];
1079

1080 1081 1082 1083 1084
    if( comp_len < 1 || comp_len > 16 )
    {
        SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
        return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
    }
1085

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    /*
     * Check the extension length
     */
    if( n > 42 + sess_len + ciph_len + comp_len )
    {
        ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
                | ( buf[43 + sess_len + ciph_len + comp_len]      );

        if( ( ext_len > 0 && ext_len < 4 ) ||
            n != 44 + sess_len + ciph_len + comp_len + ext_len )
        {
            SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
            SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
            return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
        }
    }

    ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1104
#if defined(POLARSSL_ZLIB_SUPPORT)
1105 1106
    for( i = 0; i < comp_len; ++i )
    {
1107
        if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
1108
        {
1109
            ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
1110
            break;
1111
        }
1112
    }
1113 1114
#endif

1115 1116 1117 1118 1119 1120 1121 1122 1123
    SSL_DEBUG_BUF( 3, "client hello, random bytes",
                   buf +  6,  32 );
    SSL_DEBUG_BUF( 3, "client hello, session id",
                   buf + 38,  sess_len );
    SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
                   buf + 41 + sess_len,  ciph_len );
    SSL_DEBUG_BUF( 3, "client hello, compression",
                   buf + 42 + sess_len + ciph_len, comp_len );

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
    /*
     * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
     */
    for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
    {
        if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
        {
            SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
            if( ssl->renegotiation == SSL_RENEGOTIATION )
            {
                SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1135 1136 1137 1138

                if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
                    return( ret );

1139 1140 1141 1142 1143 1144 1145 1146
                return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
            }
            ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
            break;
        }
    }

    ext = buf + 44 + sess_len + ciph_len + comp_len;
1147

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
    while( ext_len )
    {
        unsigned int ext_id   = ( ( ext[0] <<  8 )
                                | ( ext[1]       ) );
        unsigned int ext_size = ( ( ext[2] <<  8 )
                                | ( ext[3]       ) );

        if( ext_size + 4 > ext_len )
        {
            SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
            return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
        }
        switch( ext_id )
        {
1162
#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1163 1164 1165 1166 1167 1168 1169 1170 1171
        case TLS_EXT_SERVERNAME:
            SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
            if( ssl->f_sni == NULL )
                break;

            ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
            if( ret != 0 )
                return( ret );
            break;
1172
#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
1173

1174 1175 1176 1177
        case TLS_EXT_RENEGOTIATION_INFO:
            SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
            renegotiation_info_seen = 1;

1178 1179
            ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
            if( ret != 0 )
1180
                return( ret );
1181 1182
            break;