extensions_srvr.c 30.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <openssl/ocsp.h>
#include "../ssl_locl.h"
#include "statem_locl.h"

/*
 * Parse the client's renegotiation binding and abort if it's not right
 */
17
int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, X509 *x, size_t chainidx,
18
                               int *al)
19 20 21 22 23 24 25
{
    unsigned int ilen;
    const unsigned char *data;

    /* Parse the length byte */
    if (!PACKET_get_1(pkt, &ilen)
        || !PACKET_get_bytes(pkt, &data, ilen)) {
M
Matt Caswell 已提交
26
        SSLerr(SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
27 28 29 30 31 32 33
               SSL_R_RENEGOTIATION_ENCODING_ERR);
        *al = SSL_AD_ILLEGAL_PARAMETER;
        return 0;
    }

    /* Check that the extension matches */
    if (ilen != s->s3->previous_client_finished_len) {
M
Matt Caswell 已提交
34
        SSLerr(SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
35 36 37 38 39 40 41
               SSL_R_RENEGOTIATION_MISMATCH);
        *al = SSL_AD_HANDSHAKE_FAILURE;
        return 0;
    }

    if (memcmp(data, s->s3->previous_client_finished,
               s->s3->previous_client_finished_len)) {
M
Matt Caswell 已提交
42
        SSLerr(SSL_F_TLS_PARSE_CTOS_RENEGOTIATE,
43 44 45 46 47 48 49 50 51 52
               SSL_R_RENEGOTIATION_MISMATCH);
        *al = SSL_AD_HANDSHAKE_FAILURE;
        return 0;
    }

    s->s3->send_connection_binding = 1;

    return 1;
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/*-
 * The servername extension is treated as follows:
 *
 * - Only the hostname type is supported with a maximum length of 255.
 * - The servername is rejected if too long or if it contains zeros,
 *   in which case an fatal alert is generated.
 * - The servername field is maintained together with the session cache.
 * - When a session is resumed, the servername call back invoked in order
 *   to allow the application to position itself to the right context.
 * - The servername is acknowledged if it is new for a session or when
 *   it is identical to a previously used for the same session.
 *   Applications can control the behaviour.  They can at any time
 *   set a 'desirable' servername for a new SSL object. This can be the
 *   case for example with HTTPS when a Host: header field is received and
 *   a renegotiation is requested. In this case, a possible servername
 *   presented in the new client hello is only acknowledged if it matches
 *   the value of the Host: field.
 * - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
 *   if they provide for changing an explicit servername context for the
 *   session, i.e. when the session has been established with a servername
 *   extension.
 * - On session reconnect, the servername extension may be absent.
 */
76
int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, X509 *x, size_t chainidx,
77
                               int *al)
78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
    unsigned int servname_type;
    PACKET sni, hostname;

    if (!PACKET_as_length_prefixed_2(pkt, &sni)
        /* ServerNameList must be at least 1 byte long. */
        || PACKET_remaining(&sni) == 0) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    /*
     * Although the server_name extension was intended to be
     * extensible to new name types, RFC 4366 defined the
92
     * syntax inextensibly and OpenSSL 1.0.x parses it as
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
     * such.
     * RFC 6066 corrected the mistake but adding new name types
     * is nevertheless no longer feasible, so act as if no other
     * SNI types can exist, to simplify parsing.
     *
     * Also note that the RFC permits only one SNI value per type,
     * i.e., we can only have a single hostname.
     */
    if (!PACKET_get_1(&sni, &servname_type)
        || servname_type != TLSEXT_NAMETYPE_host_name
        || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    if (!s->hit) {
        if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
            *al = TLS1_AD_UNRECOGNIZED_NAME;
            return 0;
        }

        if (PACKET_contains_zero_byte(&hostname)) {
            *al = TLS1_AD_UNRECOGNIZED_NAME;
            return 0;
        }

        if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
            *al = TLS1_AD_INTERNAL_ERROR;
            return 0;
        }

        s->servername_done = 1;
    } else {
        /*
         * TODO(openssl-team): if the SNI doesn't match, we MUST
         * fall back to a full handshake.
         */
        s->servername_done = s->session->tlsext_hostname
            && PACKET_equal(&hostname, s->session->tlsext_hostname,
                            strlen(s->session->tlsext_hostname));
    }

    return 1;
}

#ifndef OPENSSL_NO_SRP
139
int tls_parse_ctos_srp(SSL *s, PACKET *pkt, X509 *x, size_t chainidx, int *al)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
{
    PACKET srp_I;

    if (!PACKET_as_length_prefixed_1(pkt, &srp_I)
            || PACKET_contains_zero_byte(&srp_I)) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    /*
     * TODO(openssl-team): currently, we re-authenticate the user
     * upon resumption. Instead, we MUST ignore the login.
     */
    if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
        *al = TLS1_AD_INTERNAL_ERROR;
        return 0;
    }

    return 1;
}
#endif

#ifndef OPENSSL_NO_EC
163
int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, X509 *x, size_t chainidx,
164
                                 int *al)
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
    PACKET ec_point_format_list;

    if (!PACKET_as_length_prefixed_1(pkt, &ec_point_format_list)
        || PACKET_remaining(&ec_point_format_list) == 0) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    if (!s->hit) {
        if (!PACKET_memdup(&ec_point_format_list,
                           &s->session->tlsext_ecpointformatlist,
                           &s->session->tlsext_ecpointformatlist_length)) {
            *al = TLS1_AD_INTERNAL_ERROR;
            return 0;
        }
    }

    return 1;
}
#endif                          /* OPENSSL_NO_EC */

187
int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, X509 *x, size_t chainidx,
188
                                  int *al)
189 190 191 192 193 194 195 196 197 198 199 200
{
    if (s->tls_session_ticket_ext_cb &&
            !s->tls_session_ticket_ext_cb(s, PACKET_data(pkt),
                                          PACKET_remaining(pkt),
                                          s->tls_session_ticket_ext_cb_arg)) {
        *al = TLS1_AD_INTERNAL_ERROR;
        return 0;
    }

    return 1;
}

201 202
int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, X509 *x, size_t chainidx,
                            int *al)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
{
    PACKET supported_sig_algs;

    if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
            || (PACKET_remaining(&supported_sig_algs) % 2) != 0
            || PACKET_remaining(&supported_sig_algs) == 0) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    if (!s->hit && !tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
                                      PACKET_remaining(&supported_sig_algs))) {
        *al = TLS1_AD_INTERNAL_ERROR;
        return 0;
    }

    return 1;
}

222
#ifndef OPENSSL_NO_OCSP
223
int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, X509 *x, size_t chainidx,
224
                                  int *al)
225
{
226 227
    PACKET responder_id_list, exts;

228 229 230 231
    /* Not defined if we get one of these in a client Certificate */
    if (x != NULL)
        return 1;

232 233 234 235
    if (!PACKET_get_1(pkt, (unsigned int *)&s->tlsext_status_type)) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }
236

237
    if (s->tlsext_status_type != TLSEXT_STATUSTYPE_ocsp) {
238
        /*
239
         * We don't know what to do with any other type so ignore it.
240
         */
241
        s->tlsext_status_type = TLSEXT_STATUSTYPE_nothing;
242 243
        return 1;
    }
244

245 246 247 248
    if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263
    /*
     * We remove any OCSP_RESPIDs from a previous handshake
     * to prevent unbounded memory growth - CVE-2016-6304
     */
    sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
    if (PACKET_remaining(&responder_id_list) > 0) {
        s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
        if (s->tlsext_ocsp_ids == NULL) {
            *al = SSL_AD_INTERNAL_ERROR;
            return 0;
        }
    } else {
        s->tlsext_ocsp_ids = NULL;
    }
264

265 266 267 268
    while (PACKET_remaining(&responder_id_list) > 0) {
        OCSP_RESPID *id;
        PACKET responder_id;
        const unsigned char *id_data;
269

270 271 272 273 274
        if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)
                || PACKET_remaining(&responder_id) == 0) {
            *al = SSL_AD_DECODE_ERROR;
            return 0;
        }
275

276 277 278 279 280 281 282
        id_data = PACKET_data(&responder_id);
        /* TODO(size_t): Convert d2i_* to size_t */
        id = d2i_OCSP_RESPID(NULL, &id_data,
                             (int)PACKET_remaining(&responder_id));
        if (id == NULL) {
            *al = SSL_AD_DECODE_ERROR;
            return 0;
283 284
        }

285 286
        if (id_data != PACKET_end(&responder_id)) {
            OCSP_RESPID_free(id);
287 288 289 290
            *al = SSL_AD_DECODE_ERROR;
            return 0;
        }

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
            OCSP_RESPID_free(id);
            *al = SSL_AD_INTERNAL_ERROR;
            return 0;
        }
    }

    /* Read in request_extensions */
    if (!PACKET_as_length_prefixed_2(pkt, &exts)) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    if (PACKET_remaining(&exts) > 0) {
        const unsigned char *ext_data = PACKET_data(&exts);

        sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
                                   X509_EXTENSION_free);
        s->tlsext_ocsp_exts =
            d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));
        if (s->tlsext_ocsp_exts == NULL || ext_data != PACKET_end(&exts)) {
            *al = SSL_AD_DECODE_ERROR;
            return 0;
314 315 316 317 318
        }
    }

    return 1;
}
319
#endif
320 321

#ifndef OPENSSL_NO_NEXTPROTONEG
322
int tls_parse_ctos_npn(SSL *s, PACKET *pkt, X509 *x, size_t chainidx, int *al)
323
{
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    /*
     * We shouldn't accept this extension on a
     * renegotiation.
     *
     * s->new_session will be set on renegotiation, but we
     * probably shouldn't rely that it couldn't be set on
     * the initial renegotiation too in certain cases (when
     * there's some other reason to disallow resuming an
     * earlier session -- the current code won't be doing
     * anything like that, but this might change).
     *
     * A valid sign that there's been a previous handshake
     * in this connection is if s->s3->tmp.finish_md_len >
     * 0.  (We are talking about a check that will happen
     * in the Hello protocol round, well before a new
     * Finished message could have been computed.)
     */
    if (s->s3->tmp.finish_md_len == 0)
342 343 344 345 346 347 348
        s->s3->next_proto_neg_seen = 1;

    return 1;
}
#endif

/*
349 350 351
 * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN
 * extension, not including type and length. |al| is a pointer to the alert
 * value to send in the event of a failure. Returns: 1 on success, 0 on error.
352
 */
353
int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, X509 *x, size_t chainidx, int *al)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
{
    PACKET protocol_list, save_protocol_list, protocol;

    if (s->s3->tmp.finish_md_len != 0)
        return 1;

    if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
        || PACKET_remaining(&protocol_list) < 2) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    save_protocol_list = protocol_list;
    do {
        /* Protocol names can't be empty. */
        if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
                || PACKET_remaining(&protocol) == 0) {
            *al = SSL_AD_DECODE_ERROR;
            return 0;
        }
    } while (PACKET_remaining(&protocol_list) != 0);

    if (!PACKET_memdup(&save_protocol_list,
                       &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
        *al = TLS1_AD_INTERNAL_ERROR;
        return 0;
    }

    return 1;
}

#ifndef OPENSSL_NO_SRTP
386 387
int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, X509 *x, size_t chainidx,
                            int *al)
388 389 390 391 392 393 394 395 396 397 398
{
    STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
    unsigned int ct, mki_len, id;
    int i, srtp_pref;
    PACKET subpkt;

    /* Ignore this if we have no SRTP profiles */
    if (SSL_get_srtp_profiles(s) == NULL)
        return 1;

    /* Pull off the length of the cipher suite list  and check it is even */
399 400
    if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0
            || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
M
Matt Caswell 已提交
401
        SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP,
402 403 404 405 406 407 408 409 410 411 412 413
               SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    srvr = SSL_get_srtp_profiles(s);
    s->srtp_profile = NULL;
    /* Search all profiles for a match initially */
    srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);

    while (PACKET_remaining(&subpkt)) {
        if (!PACKET_get_net_2(&subpkt, &id)) {
M
Matt Caswell 已提交
414
            SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP,
415 416 417 418 419 420 421 422 423 424 425 426
                   SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
            *al = SSL_AD_DECODE_ERROR;
            return 0;
        }

        /*
         * Only look for match in profiles of higher preference than
         * current match.
         * If no profiles have been have been configured then this
         * does nothing.
         */
        for (i = 0; i < srtp_pref; i++) {
427
            SRTP_PROTECTION_PROFILE *sprof =
428 429
                sk_SRTP_PROTECTION_PROFILE_value(srvr, i);

430 431 432 433 434 435 436 437
            if (sprof->id == id) {
                s->srtp_profile = sprof;
                srtp_pref = i;
                break;
            }
        }
    }

438
    /* Now extract the MKI value as a sanity check, but discard it for now */
439
    if (!PACKET_get_1(pkt, &mki_len)) {
M
Matt Caswell 已提交
440
        SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP,
441 442 443 444 445 446 447
               SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    if (!PACKET_forward(pkt, mki_len)
        || PACKET_remaining(pkt)) {
M
Matt Caswell 已提交
448
        SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP, SSL_R_BAD_SRTP_MKI_VALUE);
449 450 451 452 453 454 455 456
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    return 1;
}
#endif

457
int tls_parse_ctos_etm(SSL *s, PACKET *pkt, X509 *x, size_t chainidx, int *al)
458 459 460 461 462 463 464 465 466 467 468 469 470
{
    if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
        s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;

    return 1;
}

/*
 * Checks a list of |groups| to determine if the |group_id| is in it. If it is
 * and |checkallow| is 1 then additionally check if the group is allowed to be
 * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
 * 1) or 0 otherwise.
 */
M
Matt Caswell 已提交
471
#ifndef OPENSSL_NO_TLS1_3
472 473 474 475 476 477 478 479 480 481 482 483 484
static int check_in_list(SSL *s, unsigned int group_id,
                         const unsigned char *groups, size_t num_groups,
                         int checkallow)
{
    size_t i;

    if (groups == NULL || num_groups == 0)
        return 0;

    for (i = 0; i < num_groups; i++, groups += 2) {
        unsigned int share_id = (groups[0] << 8) | (groups[1]);

        if (group_id == share_id
485 486
                && (!checkallow
                    || tls_curve_allowed(s, groups, SSL_SECOP_CURVE_CHECK))) {
487 488 489 490 491 492 493
            break;
        }
    }

    /* If i == num_groups then not in the list */
    return i < num_groups;
}
M
Matt Caswell 已提交
494
#endif
495 496 497 498 499 500

/*
 * Process a key_share extension received in the ClientHello. |pkt| contains
 * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
 * If a failure occurs then |*al| is set to an appropriate alert value.
 */
501
int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, X509 *x, size_t chainidx,
502
                             int *al)
503
{
M
Matt Caswell 已提交
504
#ifndef OPENSSL_NO_TLS1_3
505 506 507 508 509 510 511 512 513 514 515 516 517
    unsigned int group_id;
    PACKET key_share_list, encoded_pt;
    const unsigned char *clntcurves, *srvrcurves;
    size_t clnt_num_curves, srvr_num_curves;
    int group_nid, found = 0;
    unsigned int curve_flags;

    if (s->hit)
        return 1;

    /* Sanity check */
    if (s->s3->peer_tmp != NULL) {
        *al = SSL_AD_INTERNAL_ERROR;
M
Matt Caswell 已提交
518
        SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
519 520 521 522 523
        return 0;
    }

    if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {
        *al = SSL_AD_HANDSHAKE_FAILURE;
M
Matt Caswell 已提交
524
        SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
525 526 527 528 529 530
        return 0;
    }

    /* Get our list of supported curves */
    if (!tls1_get_curvelist(s, 0, &srvrcurves, &srvr_num_curves)) {
        *al = SSL_AD_INTERNAL_ERROR;
M
Matt Caswell 已提交
531
        SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
532 533 534
        return 0;
    }

535
    /*
536
     * Get the clients list of supported curves.
537 538 539
     * TODO(TLS1.3): We should validate that we actually received
     * supported_groups!
     */
540 541
    if (!tls1_get_curvelist(s, 1, &clntcurves, &clnt_num_curves)) {
        *al = SSL_AD_INTERNAL_ERROR;
M
Matt Caswell 已提交
542
        SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
543 544 545 546 547 548 549 550
        return 0;
    }

    while (PACKET_remaining(&key_share_list) > 0) {
        if (!PACKET_get_net_2(&key_share_list, &group_id)
                || !PACKET_get_length_prefixed_2(&key_share_list, &encoded_pt)
                || PACKET_remaining(&encoded_pt) == 0) {
            *al = SSL_AD_HANDSHAKE_FAILURE;
M
Matt Caswell 已提交
551
            SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
552 553 554 555 556 557 558 559 560 561 562 563 564 565
                   SSL_R_LENGTH_MISMATCH);
            return 0;
        }

        /*
         * If we already found a suitable key_share we loop through the
         * rest to verify the structure, but don't process them.
         */
        if (found)
            continue;

        /* Check if this share is in supported_groups sent from client */
        if (!check_in_list(s, group_id, clntcurves, clnt_num_curves, 0)) {
            *al = SSL_AD_HANDSHAKE_FAILURE;
M
Matt Caswell 已提交
566
            SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
567 568 569 570 571 572 573 574 575 576 577 578 579
            return 0;
        }

        /* Check if this share is for a group we can use */
        if (!check_in_list(s, group_id, srvrcurves, srvr_num_curves, 1)) {
            /* Share not suitable */
            continue;
        }

        group_nid = tls1_ec_curve_id2nid(group_id, &curve_flags);

        if (group_nid == 0) {
            *al = SSL_AD_INTERNAL_ERROR;
M
Matt Caswell 已提交
580
            SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
581 582 583 584 585 586 587 588 589 590
                   SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
            return 0;
        }

        if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) {
            /* Can happen for some curves, e.g. X25519 */
            EVP_PKEY *key = EVP_PKEY_new();

            if (key == NULL || !EVP_PKEY_set_type(key, group_nid)) {
                *al = SSL_AD_INTERNAL_ERROR;
M
Matt Caswell 已提交
591
                SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_EVP_LIB);
592 593 594 595 596 597 598
                EVP_PKEY_free(key);
                return 0;
            }
            s->s3->peer_tmp = key;
        } else {
            /* Set up EVP_PKEY with named curve as parameters */
            EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
599

600 601 602 603 604 605
            if (pctx == NULL
                    || EVP_PKEY_paramgen_init(pctx) <= 0
                    || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
                                                              group_nid) <= 0
                    || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
                *al = SSL_AD_INTERNAL_ERROR;
M
Matt Caswell 已提交
606
                SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_EVP_LIB);
607 608 609 610 611 612 613 614 615 616 617 618
                EVP_PKEY_CTX_free(pctx);
                return 0;
            }
            EVP_PKEY_CTX_free(pctx);
            pctx = NULL;
        }
        s->s3->group_id = group_id;

        if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
                PACKET_data(&encoded_pt),
                PACKET_remaining(&encoded_pt))) {
            *al = SSL_AD_DECODE_ERROR;
M
Matt Caswell 已提交
619
            SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_ECPOINT);
620 621 622 623 624
            return 0;
        }

        found = 1;
    }
M
Matt Caswell 已提交
625
#endif
626 627 628 629 630

    return 1;
}

#ifndef OPENSSL_NO_EC
631 632
int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, X509 *x,
                                    size_t chainidx, int *al)
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
{
    PACKET supported_groups_list;

    /* Each group is 2 bytes and we must have at least 1. */
    if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)
            || PACKET_remaining(&supported_groups_list) == 0
            || (PACKET_remaining(&supported_groups_list) % 2) != 0) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    if (!s->hit
            && !PACKET_memdup(&supported_groups_list,
                              &s->session->tlsext_supportedgroupslist,
                              &s->session->tlsext_supportedgroupslist_length)) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    return 1;
}
#endif

656
int tls_parse_ctos_ems(SSL *s, PACKET *pkt, X509 *x, size_t chainidx, int *al)
657 658 659 660 661 662 663 664 665 666 667
{
    /* The extension must always be empty */
    if (PACKET_remaining(pkt) != 0) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;

    return 1;
}
668

669 670 671
/*
 * Add the server's renegotiation binding
 */
672 673
int tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt, X509 *x, size_t
                                   chainidx, int *al)
674 675 676 677 678 679 680 681 682 683 684 685 686
{
    if (!s->s3->send_connection_binding)
        return 1;

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
            || !WPACKET_start_sub_packet_u16(pkt)
            || !WPACKET_start_sub_packet_u8(pkt)
            || !WPACKET_memcpy(pkt, s->s3->previous_client_finished,
                               s->s3->previous_client_finished_len)
            || !WPACKET_memcpy(pkt, s->s3->previous_server_finished,
                               s->s3->previous_server_finished_len)
            || !WPACKET_close(pkt)
            || !WPACKET_close(pkt)) {
M
Matt Caswell 已提交
687
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_RENEGOTIATE, ERR_R_INTERNAL_ERROR);
688 689 690 691 692 693
        return 0;
    }

    return 1;
}

694 695
int tls_construct_stoc_server_name(SSL *s, WPACKET *pkt, X509 *x,
                                   size_t chainidx, int *al)
696 697 698 699 700 701 702
{
    if (s->hit || s->servername_done != 1
            || s->session->tlsext_hostname == NULL)
        return 1;

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
            || !WPACKET_put_bytes_u16(pkt, 0)) {
M
Matt Caswell 已提交
703
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SERVER_NAME, ERR_R_INTERNAL_ERROR);
704 705 706 707 708 709 710
        return 0;
    }

    return 1;
}

#ifndef OPENSSL_NO_EC
711
int tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt, X509 *x,
712
                                     size_t chainidx, int *al)
713 714 715
{
    unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
    unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
716 717
    int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
                    && (s->session->tlsext_ecpointformatlist != NULL);
718 719 720 721 722 723 724 725 726 727 728
    const unsigned char *plist;
    size_t plistlen;

    if (!using_ecc)
        return 1;

    tls1_get_formatlist(s, &plist, &plistlen);
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
            || !WPACKET_start_sub_packet_u16(pkt)
            || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
            || !WPACKET_close(pkt)) {
M
Matt Caswell 已提交
729
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
730 731 732 733 734 735 736
        return 0;
    }

    return 1;
}
#endif

737
int tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt, X509 *x,
738
                                      size_t chainidx, int *al)
739 740 741 742 743 744 745 746
{
    if (!s->tlsext_ticket_expected || !tls_use_ticket(s)) {
        s->tlsext_ticket_expected = 0;
        return 1;
    }

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
            || !WPACKET_put_bytes_u16(pkt, 0)) {
M
Matt Caswell 已提交
747
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
748 749 750 751 752 753
        return 0;
    }

    return 1;
}

754
#ifndef OPENSSL_NO_OCSP
755
int tls_construct_stoc_status_request(SSL *s, WPACKET *pkt, X509 *x,
756
                                     size_t chainidx, int *al)
757 758 759 760
{
    if (!s->tlsext_status_expected)
        return 1;

761
    if (SSL_IS_TLS13(s) && chainidx != 0)
762 763
        return 1;

764
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
765 766 767 768 769 770 771 772 773 774 775 776
            || !WPACKET_start_sub_packet_u16(pkt)) {
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
        return 0;
    }

    /*
     * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we
     * send back an empty extension, with the certificate status appearing as a
     * separate message
     */
    if ((SSL_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt))
            || !WPACKET_close(pkt)) {
M
Matt Caswell 已提交
777
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
778 779 780 781 782
        return 0;
    }

    return 1;
}
783
#endif
784 785

#ifndef OPENSSL_NO_NEXTPROTONEG
786
int tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt, X509 *x,
787
                                      size_t chainidx, int *al)
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
{
    const unsigned char *npa;
    unsigned int npalen;
    int ret;
    int next_proto_neg_seen = s->s3->next_proto_neg_seen;

    s->s3->next_proto_neg_seen = 0;
    if (!next_proto_neg_seen || s->ctx->next_protos_advertised_cb == NULL)
        return 1;

    ret = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
                                      s->ctx->next_protos_advertised_cb_arg);
    if (ret == SSL_TLSEXT_ERR_OK) {
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
                || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
M
Matt Caswell 已提交
803
            SSLerr(SSL_F_TLS_CONSTRUCT_STOC_NEXT_PROTO_NEG,
804 805 806 807 808 809 810 811 812 813
                   ERR_R_INTERNAL_ERROR);
            return 0;
        }
        s->s3->next_proto_neg_seen = 1;
    }

    return 1;
}
#endif

814
int tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, X509 *x, size_t chainidx,
815
                            int *al)
816 817 818 819 820 821 822 823 824 825 826 827
{
    if (s->s3->alpn_selected == NULL)
        return 1;

    if (!WPACKET_put_bytes_u16(pkt,
                TLSEXT_TYPE_application_layer_protocol_negotiation)
            || !WPACKET_start_sub_packet_u16(pkt)
            || !WPACKET_start_sub_packet_u16(pkt)
            || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected,
                                      s->s3->alpn_selected_len)
            || !WPACKET_close(pkt)
            || !WPACKET_close(pkt)) {
M
Matt Caswell 已提交
828
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_ALPN, ERR_R_INTERNAL_ERROR);
829 830 831 832 833 834 835
        return 0;
    }

    return 1;
}

#ifndef OPENSSL_NO_SRTP
836
int tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt, X509 *x, size_t chainidx,
837
                                int *al)
838 839 840
{
    if (s->srtp_profile == NULL)
        return 1;
M
Matt Caswell 已提交
841

842 843 844 845 846 847
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
            || !WPACKET_start_sub_packet_u16(pkt)
            || !WPACKET_put_bytes_u16(pkt, 2)
            || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
            || !WPACKET_put_bytes_u8(pkt, 0)
            || !WPACKET_close(pkt)) {
M
Matt Caswell 已提交
848
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_USE_SRTP, ERR_R_INTERNAL_ERROR);
849 850 851 852 853 854 855
        return 0;
    }

    return 1;
}
#endif

856 857
int tls_construct_stoc_etm(SSL *s, WPACKET *pkt, X509 *x, size_t chainidx,
                           int *al)
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
{
    if ((s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) == 0)
        return 1;

    /*
     * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
     * for other cases too.
     */
    if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
        || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
        || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
        || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12) {
        s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
        return 1;
    }

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
            || !WPACKET_put_bytes_u16(pkt, 0)) {
M
Matt Caswell 已提交
876
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_ETM, ERR_R_INTERNAL_ERROR);
877 878 879 880 881 882
        return 0;
    }

    return 1;
}

883 884
int tls_construct_stoc_ems(SSL *s, WPACKET *pkt, X509 *x, size_t chainidx,
                           int *al)
885 886 887 888 889 890
{
    if ((s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
        return 1;

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
            || !WPACKET_put_bytes_u16(pkt, 0)) {
M
Matt Caswell 已提交
891
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EMS, ERR_R_INTERNAL_ERROR);
892 893 894 895 896 897
        return 0;
    }

    return 1;
}

898
int tls_construct_stoc_key_share(SSL *s, WPACKET *pkt, X509 *x, size_t chainidx,
899
                                 int *al)
900
{
M
Matt Caswell 已提交
901
#ifndef OPENSSL_NO_TLS1_3
902 903 904 905 906 907 908 909
    unsigned char *encodedPoint;
    size_t encoded_pt_len = 0;
    EVP_PKEY *ckey = s->s3->peer_tmp, *skey = NULL;

    if (s->hit)
        return 1;

    if (ckey == NULL) {
M
Matt Caswell 已提交
910
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
911 912 913 914 915 916
        return 0;
    }

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
            || !WPACKET_start_sub_packet_u16(pkt)
            || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)) {
M
Matt Caswell 已提交
917
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
918 919 920 921 922
        return 0;
    }

    skey = ssl_generate_pkey(ckey);
    if (skey == NULL) {
M
Matt Caswell 已提交
923
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_MALLOC_FAILURE);
924 925 926 927 928 929
        return 0;
    }

    /* Generate encoding of server key */
    encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(skey, &encodedPoint);
    if (encoded_pt_len == 0) {
M
Matt Caswell 已提交
930
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_EC_LIB);
931 932 933 934 935 936
        EVP_PKEY_free(skey);
        return 0;
    }

    if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len)
            || !WPACKET_close(pkt)) {
M
Matt Caswell 已提交
937
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
938 939 940 941 942 943 944 945 946
        EVP_PKEY_free(skey);
        OPENSSL_free(encodedPoint);
        return 0;
    }
    OPENSSL_free(encodedPoint);

    /* This causes the crypto state to be updated based on the derived keys */
    s->s3->tmp.pkey = skey;
    if (ssl_derive(s, skey, ckey, 1) == 0) {
M
Matt Caswell 已提交
947
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
948 949
        return 0;
    }
M
Matt Caswell 已提交
950
#endif
951 952 953 954

    return 1;
}

955
int tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt, X509 *x,
956
                                     size_t chainidx, int *al)
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
{
    const unsigned char cryptopro_ext[36] = {
        0xfd, 0xe8,         /* 65000 */
        0x00, 0x20,         /* 32 bytes length */
        0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
        0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
        0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
        0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
    };

    if (((s->s3->tmp.new_cipher->id & 0xFFFF) != 0x80
         && (s->s3->tmp.new_cipher->id & 0xFFFF) != 0x81)
            || (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG) == 0)
        return 1;

    if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
M
Matt Caswell 已提交
973
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_CRYPTOPRO_BUG, ERR_R_INTERNAL_ERROR);
974 975 976 977 978
        return 0;
    }

    return 1;
}