extensions_srvr.c 41.6 KB
Newer Older
1
/*
2
 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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 18
int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
                               X509 *x, size_t chainidx, 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
               SSL_R_RENEGOTIATION_ENCODING_ERR);
28
        *al = SSL_AD_DECODE_ERROR;
29 30 31 32 33
        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 77
int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
                               X509 *x, size_t chainidx, int *al)
78 79 80 81 82 83 84 85 86 87 88 89
{
    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;
    }

    /*
F
FdaSilvaYY 已提交
90 91 92
     * Although the intent was for server_name to be extensible, RFC 4366
     * was not clear about it; and so OpenSSL among other implementations,
     * always and only allows a 'host_name' name types.
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
     * 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;
        }

118 119
        OPENSSL_free(s->session->ext.hostname);
        s->session->ext.hostname = NULL;
R
Rich Salz 已提交
120
        if (!PACKET_strndup(&hostname, &s->session->ext.hostname)) {
121 122 123 124 125 126 127 128 129 130
            *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.
         */
R
Rich Salz 已提交
131 132 133
        s->servername_done = s->session->ext.hostname
            && PACKET_equal(&hostname, s->session->ext.hostname,
                            strlen(s->session->ext.hostname));
134 135 136

        if (!s->servername_done && s->session->ext.hostname != NULL)
            s->ext.early_data_ok = 0;
137 138 139 140 141 142
    }

    return 1;
}

#ifndef OPENSSL_NO_SRP
143 144
int tls_parse_ctos_srp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al)
145 146 147 148 149 150 151 152 153 154 155 156 157 158
{
    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)) {
159
        *al = SSL_AD_INTERNAL_ERROR;
160 161 162 163 164 165 166 167
        return 0;
    }

    return 1;
}
#endif

#ifndef OPENSSL_NO_EC
168 169
int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
                                 X509 *x, size_t chainidx, int *al)
170 171 172 173 174 175 176 177 178 179 180
{
    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,
R
Rich Salz 已提交
181 182
                           &s->session->ext.ecpointformats,
                           &s->session->ext.ecpointformats_len)) {
183
            *al = SSL_AD_INTERNAL_ERROR;
184 185 186 187 188 189 190 191
            return 0;
        }
    }

    return 1;
}
#endif                          /* OPENSSL_NO_EC */

192 193
int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al)
194
{
R
Rich Salz 已提交
195 196 197 198
    if (s->ext.session_ticket_cb &&
            !s->ext.session_ticket_cb(s, PACKET_data(pkt),
                                  PACKET_remaining(pkt),
                                  s->ext.session_ticket_cb_arg)) {
199
        *al = SSL_AD_INTERNAL_ERROR;
200 201 202 203 204 205
        return 0;
    }

    return 1;
}

206 207
int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                            size_t chainidx, int *al)
208 209 210 211 212 213 214 215 216
{
    PACKET supported_sig_algs;

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

217
    if (!s->hit && !tls1_save_sigalgs(s, &supported_sig_algs)) {
218
        *al = SSL_AD_DECODE_ERROR;
219 220 221 222 223 224
        return 0;
    }

    return 1;
}

225
#ifndef OPENSSL_NO_OCSP
226 227
int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al)
228
{
229 230
    PACKET responder_id_list, exts;

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

R
Rich Salz 已提交
235
    if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) {
236 237 238
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }
239

R
Rich Salz 已提交
240
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
241
        /*
242
         * We don't know what to do with any other type so ignore it.
243
         */
R
Rich Salz 已提交
244
        s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
245 246
        return 1;
    }
247

248 249 250 251
    if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }
252

253 254 255 256
    /*
     * We remove any OCSP_RESPIDs from a previous handshake
     * to prevent unbounded memory growth - CVE-2016-6304
     */
R
Rich Salz 已提交
257
    sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
258
    if (PACKET_remaining(&responder_id_list) > 0) {
R
Rich Salz 已提交
259 260
        s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();
        if (s->ext.ocsp.ids == NULL) {
261 262 263 264
            *al = SSL_AD_INTERNAL_ERROR;
            return 0;
        }
    } else {
R
Rich Salz 已提交
265
        s->ext.ocsp.ids = NULL;
266
    }
267

268 269 270 271
    while (PACKET_remaining(&responder_id_list) > 0) {
        OCSP_RESPID *id;
        PACKET responder_id;
        const unsigned char *id_data;
272

273 274 275 276 277
        if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)
                || PACKET_remaining(&responder_id) == 0) {
            *al = SSL_AD_DECODE_ERROR;
            return 0;
        }
278

279 280 281 282 283 284 285
        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;
286 287
        }

288 289
        if (id_data != PACKET_end(&responder_id)) {
            OCSP_RESPID_free(id);
290 291 292 293
            *al = SSL_AD_DECODE_ERROR;
            return 0;
        }

R
Rich Salz 已提交
294
        if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) {
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
            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);

R
Rich Salz 已提交
310
        sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,
311
                                   X509_EXTENSION_free);
R
Rich Salz 已提交
312
        s->ext.ocsp.exts =
313
            d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));
R
Rich Salz 已提交
314
        if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) {
315 316
            *al = SSL_AD_DECODE_ERROR;
            return 0;
317 318 319 320 321
        }
    }

    return 1;
}
322
#endif
323 324

#ifndef OPENSSL_NO_NEXTPROTONEG
325 326
int tls_parse_ctos_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al)
327
{
328 329 330 331
    /*
     * We shouldn't accept this extension on a
     * renegotiation.
     */
332
    if (SSL_IS_FIRST_HANDSHAKE(s))
R
Rich Salz 已提交
333
        s->s3->npn_seen = 1;
334 335 336 337 338 339

    return 1;
}
#endif

/*
340 341 342
 * 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.
343
 */
344 345
int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                        size_t chainidx, int *al)
346 347 348
{
    PACKET protocol_list, save_protocol_list, protocol;

349
    if (!SSL_IS_FIRST_HANDSHAKE(s))
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        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);

368 369 370
    OPENSSL_free(s->s3->alpn_proposed);
    s->s3->alpn_proposed = NULL;
    s->s3->alpn_proposed_len = 0;
371 372
    if (!PACKET_memdup(&save_protocol_list,
                       &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
373
        *al = SSL_AD_INTERNAL_ERROR;
374 375 376 377 378 379 380
        return 0;
    }

    return 1;
}

#ifndef OPENSSL_NO_SRTP
381 382
int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                            size_t chainidx, int *al)
383 384 385 386 387 388 389 390 391 392 393
{
    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 */
394 395
    if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0
            || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
M
Matt Caswell 已提交
396
        SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP,
397 398 399 400 401 402 403 404 405 406 407 408
               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 已提交
409
            SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP,
410 411 412 413 414 415 416 417 418 419 420 421
                   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++) {
422
            SRTP_PROTECTION_PROFILE *sprof =
423 424
                sk_SRTP_PROTECTION_PROFILE_value(srvr, i);

425 426 427 428 429 430 431 432
            if (sprof->id == id) {
                s->srtp_profile = sprof;
                srtp_pref = i;
                break;
            }
        }
    }

433
    /* Now extract the MKI value as a sanity check, but discard it for now */
434
    if (!PACKET_get_1(pkt, &mki_len)) {
M
Matt Caswell 已提交
435
        SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP,
436 437 438 439 440 441 442
               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 已提交
443
        SSLerr(SSL_F_TLS_PARSE_CTOS_USE_SRTP, SSL_R_BAD_SRTP_MKI_VALUE);
444 445 446 447 448 449 450 451
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    return 1;
}
#endif

452 453
int tls_parse_ctos_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al)
454 455
{
    if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
456
        s->ext.use_etm = 1;
457 458 459 460

    return 1;
}

461 462 463 464 465
/*
 * Process a psk_kex_modes 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.
 */
466 467
int tls_parse_ctos_psk_kex_modes(SSL *s, PACKET *pkt, unsigned int context,
                                 X509 *x, size_t chainidx, int *al)
468 469 470 471 472 473 474 475 476 477 478 479 480 481
{
#ifndef OPENSSL_NO_TLS1_3
    PACKET psk_kex_modes;
    unsigned int mode;

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

    while (PACKET_get_1(&psk_kex_modes, &mode)) {
        if (mode == TLSEXT_KEX_MODE_KE_DHE)
            s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;
482 483
        else if (mode == TLSEXT_KEX_MODE_KE
                && (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)
484 485 486 487 488 489 490
            s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
    }
#endif

    return 1;
}

491 492 493 494 495
/*
 * 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.
 */
496 497
int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                             size_t chainidx, int *al)
498
{
M
Matt Caswell 已提交
499
#ifndef OPENSSL_NO_TLS1_3
500 501
    unsigned int group_id;
    PACKET key_share_list, encoded_pt;
D
Dr. Stephen Henson 已提交
502
    const uint16_t *clntcurves, *srvrcurves;
503
    size_t clnt_num_curves, srvr_num_curves;
504 505
    int found = 0;
    const TLS_GROUP_INFO *ginf;
506

507
    if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)
508 509 510 511 512
        return 1;

    /* Sanity check */
    if (s->s3->peer_tmp != NULL) {
        *al = SSL_AD_INTERNAL_ERROR;
M
Matt Caswell 已提交
513
        SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
514 515 516 517
        return 0;
    }

    if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {
M
Matt Caswell 已提交
518
        *al = SSL_AD_DECODE_ERROR;
M
Matt Caswell 已提交
519
        SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_LENGTH_MISMATCH);
520 521 522 523 524 525
        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 已提交
526
        SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
527 528 529
        return 0;
    }

530
    /* Get the clients list of supported curves. */
531 532
    if (!tls1_get_curvelist(s, 1, &clntcurves, &clnt_num_curves)) {
        *al = SSL_AD_INTERNAL_ERROR;
M
Matt Caswell 已提交
533
        SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);
534 535
        return 0;
    }
536 537 538 539 540 541 542 543 544 545 546
    if (clnt_num_curves == 0) {
        /*
         * This can only happen if the supported_groups extension was not sent,
         * because we verify that the length is non-zero when we process that
         * extension.
         */
        *al = SSL_AD_MISSING_EXTENSION;
        SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
               SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);
        return 0;
    }
547 548 549 550 551

    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) {
M
Matt Caswell 已提交
552
            *al = SSL_AD_DECODE_ERROR;
M
Matt Caswell 已提交
553
            SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE,
554 555 556 557 558 559 560 561 562 563 564 565 566
                   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)) {
M
Matt Caswell 已提交
567
            *al = SSL_AD_ILLEGAL_PARAMETER;
M
Matt Caswell 已提交
568
            SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
569 570 571 572 573 574 575 576 577
            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;
        }

578
        ginf = tls1_group_id_lookup(group_id);
579

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

587
        if ((ginf->flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) {
588 589 590
            /* Can happen for some curves, e.g. X25519 */
            EVP_PKEY *key = EVP_PKEY_new();

591
            if (key == NULL || !EVP_PKEY_set_type(key, ginf->nid)) {
592
                *al = SSL_AD_INTERNAL_ERROR;
M
Matt Caswell 已提交
593
                SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_EVP_LIB);
594 595 596 597 598 599 600
                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);
601

602 603 604
            if (pctx == NULL
                    || EVP_PKEY_paramgen_init(pctx) <= 0
                    || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
605
                                                              ginf->nid) <= 0
606 607
                    || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
                *al = SSL_AD_INTERNAL_ERROR;
M
Matt Caswell 已提交
608
                SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, ERR_R_EVP_LIB);
609 610 611 612 613 614 615 616 617 618 619
                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))) {
620
            *al = SSL_AD_ILLEGAL_PARAMETER;
M
Matt Caswell 已提交
621
            SSLerr(SSL_F_TLS_PARSE_CTOS_KEY_SHARE, SSL_R_BAD_ECPOINT);
622 623 624 625 626
            return 0;
        }

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

    return 1;
}

#ifndef OPENSSL_NO_EC
633 634
int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, unsigned int context,
                                    X509 *x, size_t chainidx, int *al)
635 636 637 638 639 640 641 642 643 644 645
{
    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;
    }

646 647 648 649
    if (!s->hit || SSL_IS_TLS13(s)) {
        OPENSSL_free(s->session->ext.supportedgroups);
        s->session->ext.supportedgroups = NULL;
        s->session->ext.supportedgroups_len = 0;
D
Dr. Stephen Henson 已提交
650
        if (!tls1_save_u16(&supported_groups_list,
651 652 653 654 655
                           &s->session->ext.supportedgroups,
                           &s->session->ext.supportedgroups_len)) {
            *al = SSL_AD_INTERNAL_ERROR;
            return 0;
        }
656 657 658 659 660 661
    }

    return 1;
}
#endif

662 663
int tls_parse_ctos_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al)
664 665 666 667 668 669 670 671 672 673 674
{
    /* 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;
}
675

676 677 678 679 680 681 682 683 684

int tls_parse_ctos_early_data(SSL *s, PACKET *pkt, unsigned int context,
                              X509 *x, size_t chainidx, int *al)
{
    if (PACKET_remaining(pkt) != 0) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

M
Matt Caswell 已提交
685 686 687 688 689
    if (s->hello_retry_request) {
        *al = SSL_AD_ILLEGAL_PARAMETER;
        return 0;
    }

690 691 692
    return 1;
}

693 694
int tls_parse_ctos_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al)
695 696 697 698
{
    PACKET identities, binders, binder;
    size_t binderoffset, hashsize;
    SSL_SESSION *sess = NULL;
699
    unsigned int id, i, ext = 0;
700 701
    const EVP_MD *md = NULL;

702 703 704 705 706 707 708 709
    /*
     * If we have no PSK kex mode that we recognise then we can't resume so
     * ignore this extension
     */
    if ((s->ext.psk_kex_mode
            & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE)) == 0)
        return 1;

710 711 712 713 714 715 716
    if (!PACKET_get_length_prefixed_2(pkt, &identities)) {
        *al = SSL_AD_DECODE_ERROR;
        return 0;
    }

    for (id = 0; PACKET_remaining(&identities) != 0; id++) {
        PACKET identity;
717
        unsigned long ticket_agel;
718 719

        if (!PACKET_get_length_prefixed_2(&identities, &identity)
720
                || !PACKET_get_net_4(&identities, &ticket_agel)) {
721 722 723 724
            *al = SSL_AD_DECODE_ERROR;
            return 0;
        }

725
        if (s->psk_find_session_cb != NULL
726 727 728 729 730 731 732 733 734
                && !s->psk_find_session_cb(s, PACKET_data(&identity),
                                           PACKET_remaining(&identity),
                                           &sess)) {
            *al = SSL_AD_INTERNAL_ERROR;
            return 0;
        }

        if (sess != NULL) {
            /* We found a PSK */
735
            SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);
736

737 738 739 740 741 742 743 744 745 746 747 748 749 750
            if (sesstmp == NULL) {
                *al = SSL_AD_INTERNAL_ERROR;
                return 0;
            }
            SSL_SESSION_free(sess);
            sess = sesstmp;

            /*
             * We've just been told to use this session for this context so
             * make sure the sid_ctx matches up.
             */
            memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);
            sess->sid_ctx_length = s->sid_ctx_length;
            ext = 1;
751 752
            if (id == 0)
                s->ext.early_data_ok = 1;
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
        } else {
            uint32_t ticket_age = 0, now, agesec, agems;
            int ret = tls_decrypt_ticket(s, PACKET_data(&identity),
                                         PACKET_remaining(&identity), NULL, 0,
                                         &sess);

            if (ret == TICKET_FATAL_ERR_MALLOC
                    || ret == TICKET_FATAL_ERR_OTHER) {
                *al = SSL_AD_INTERNAL_ERROR;
                return 0;
            }
            if (ret == TICKET_NO_DECRYPT)
                continue;

            ticket_age = (uint32_t)ticket_agel;
            now = (uint32_t)time(NULL);
            agesec = now - (uint32_t)sess->time;
            agems = agesec * (uint32_t)1000;
            ticket_age -= sess->ext.tick_age_add;

            /*
             * For simplicity we do our age calculations in seconds. If the
             * client does it in ms then it could appear that their ticket age
             * is longer than ours (our ticket age calculation should always be
             * slightly longer than the client's due to the network latency).
             * Therefore we add 1000ms to our age calculation to adjust for
             * rounding errors.
             */
781 782
            if (id == 0
                    && sess->timeout >= (long)agesec
783 784 785 786 787 788 789 790 791
                    && agems / (uint32_t)1000 == agesec
                    && ticket_age <= agems + 1000
                    && ticket_age + TICKET_AGE_ALLOWANCE >= agems + 1000) {
                /*
                 * Ticket age is within tolerance and not expired. We allow it
                 * for early data
                 */
                s->ext.early_data_ok = 1;
            }
792 793
        }

794
        md = ssl_md(sess->cipher->algorithm2);
795 796
        if (md != ssl_md(s->s3->tmp.new_cipher->algorithm2)) {
            /* The ciphersuite is not compatible with this session. */
797 798
            SSL_SESSION_free(sess);
            sess = NULL;
799
            s->ext.early_data_ok = 0;
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
            continue;
        }
        break;
    }

    if (sess == NULL)
        return 1;

    binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data;
    hashsize = EVP_MD_size(md);

    if (!PACKET_get_length_prefixed_2(pkt, &binders)) {
        *al = SSL_AD_DECODE_ERROR;
        goto err;
    }

    for (i = 0; i <= id; i++) {
        if (!PACKET_get_length_prefixed_1(&binders, &binder)) {
            *al = SSL_AD_DECODE_ERROR;
            goto err;
        }
    }

    if (PACKET_remaining(&binder) != hashsize
            || tls_psk_do_binder(s, md,
                                 (const unsigned char *)s->init_buf->data,
                                 binderoffset, PACKET_data(&binder), NULL,
827
                                 sess, 0, ext) != 1) {
828 829 830 831 832 833
        *al = SSL_AD_DECODE_ERROR;
        SSLerr(SSL_F_TLS_PARSE_CTOS_PSK, ERR_R_INTERNAL_ERROR);
        goto err;
    }

    sess->ext.tick_identity = id;
834

835 836 837 838
    SSL_SESSION_free(s->session);
    s->session = sess;
    return 1;
err:
839
    SSL_SESSION_free(sess);
840 841 842
    return 0;
}

843 844 845
/*
 * Add the server's renegotiation binding
 */
846 847 848
EXT_RETURN tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt,
                                          unsigned int context, X509 *x,
                                          size_t chainidx, int *al)
849 850
{
    if (!s->s3->send_connection_binding)
851
        return EXT_RETURN_NOT_SENT;
852

T
Todd Short 已提交
853
    /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */
854 855 856 857 858 859 860 861 862
    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 已提交
863
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_RENEGOTIATE, ERR_R_INTERNAL_ERROR);
864
        return EXT_RETURN_FAIL;
865 866
    }

867
    return EXT_RETURN_SENT;
868 869
}

870 871 872
EXT_RETURN tls_construct_stoc_server_name(SSL *s, WPACKET *pkt,
                                          unsigned int context, X509 *x,
                                          size_t chainidx, int *al)
873 874
{
    if (s->hit || s->servername_done != 1
R
Rich Salz 已提交
875
            || s->session->ext.hostname == NULL)
876
        return EXT_RETURN_NOT_SENT;
877 878 879

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
            || !WPACKET_put_bytes_u16(pkt, 0)) {
M
Matt Caswell 已提交
880
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SERVER_NAME, ERR_R_INTERNAL_ERROR);
881
        return EXT_RETURN_FAIL;
882 883
    }

884
    return EXT_RETURN_SENT;
885 886 887
}

#ifndef OPENSSL_NO_EC
888 889 890
EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt,
                                            unsigned int context, X509 *x,
                                            size_t chainidx, int *al)
891 892 893
{
    unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
    unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
894
    int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
R
Rich Salz 已提交
895
                    && (s->session->ext.ecpointformats != NULL);
896 897 898 899
    const unsigned char *plist;
    size_t plistlen;

    if (!using_ecc)
900
        return EXT_RETURN_NOT_SENT;
901 902 903 904 905 906

    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 已提交
907
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
908
        return EXT_RETURN_FAIL;
909 910
    }

911
    return EXT_RETURN_SENT;
912 913 914
}
#endif

R
Richard Levitte 已提交
915
#ifndef OPENSSL_NO_EC
916 917 918
EXT_RETURN tls_construct_stoc_supported_groups(SSL *s, WPACKET *pkt,
                                               unsigned int context, X509 *x,
                                               size_t chainidx, int *al)
919
{
D
Dr. Stephen Henson 已提交
920
    const uint16_t *groups;
921 922 923 924
    size_t numgroups, i, first = 1;

    /* s->s3->group_id is non zero if we accepted a key_share */
    if (s->s3->group_id == 0)
925
        return EXT_RETURN_NOT_SENT;
926 927 928 929

    /* Get our list of supported groups */
    if (!tls1_get_curvelist(s, 0, &groups, &numgroups) || numgroups == 0) {
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS, ERR_R_INTERNAL_ERROR);
930
        return EXT_RETURN_FAIL;
931 932 933
    }

    /* Copy group ID if supported */
D
Dr. Stephen Henson 已提交
934 935 936 937
    for (i = 0; i < numgroups; i++) {
        uint16_t group = groups[i];

        if (tls_curve_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {
938 939 940 941 942
            if (first) {
                /*
                 * Check if the client is already using our preferred group. If
                 * so we don't need to add this extension
                 */
D
Dr. Stephen Henson 已提交
943
                if (s->s3->group_id == group)
944
                    return EXT_RETURN_NOT_SENT;
945 946 947 948 949 950 951 952

                /* Add extension header */
                if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
                           /* Sub-packet for supported_groups extension */
                        || !WPACKET_start_sub_packet_u16(pkt)
                        || !WPACKET_start_sub_packet_u16(pkt)) {
                    SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
                           ERR_R_INTERNAL_ERROR);
953
                    return EXT_RETURN_FAIL;
954 955 956 957
                }

                first = 0;
            }
D
Dr. Stephen Henson 已提交
958
            if (!WPACKET_put_bytes_u16(pkt, group)) {
959 960
                    SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS,
                           ERR_R_INTERNAL_ERROR);
961
                    return EXT_RETURN_FAIL;
962 963 964 965 966 967
                }
        }
    }

    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SUPPORTED_GROUPS, ERR_R_INTERNAL_ERROR);
968
        return EXT_RETURN_FAIL;
969 970
    }

971
    return EXT_RETURN_SENT;
972
}
R
Richard Levitte 已提交
973
#endif
974

975 976 977
EXT_RETURN tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt,
                                             unsigned int context, X509 *x,
                                             size_t chainidx, int *al)
978
{
R
Rich Salz 已提交
979 980
    if (!s->ext.ticket_expected || !tls_use_ticket(s)) {
        s->ext.ticket_expected = 0;
981
        return EXT_RETURN_NOT_SENT;
982 983 984 985
    }

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
            || !WPACKET_put_bytes_u16(pkt, 0)) {
M
Matt Caswell 已提交
986
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
987
        return EXT_RETURN_FAIL;
988 989
    }

990
    return EXT_RETURN_SENT;
991 992
}

993
#ifndef OPENSSL_NO_OCSP
994 995 996
EXT_RETURN tls_construct_stoc_status_request(SSL *s, WPACKET *pkt,
                                             unsigned int context, X509 *x,
                                             size_t chainidx, int *al)
997
{
R
Rich Salz 已提交
998
    if (!s->ext.status_expected)
999
        return EXT_RETURN_NOT_SENT;
1000

1001
    if (SSL_IS_TLS13(s) && chainidx != 0)
1002
        return EXT_RETURN_NOT_SENT;
1003

1004
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1005 1006
            || !WPACKET_start_sub_packet_u16(pkt)) {
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
1007
        return EXT_RETURN_FAIL;
1008 1009 1010 1011 1012 1013 1014 1015 1016
    }

    /*
     * 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 已提交
1017
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
1018
        return EXT_RETURN_FAIL;
1019 1020
    }

1021
    return EXT_RETURN_SENT;
1022
}
1023
#endif
1024 1025

#ifndef OPENSSL_NO_NEXTPROTONEG
1026 1027 1028
EXT_RETURN tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt,
                                             unsigned int context, X509 *x,
                                             size_t chainidx, int *al)
1029 1030 1031 1032
{
    const unsigned char *npa;
    unsigned int npalen;
    int ret;
R
Rich Salz 已提交
1033
    int npn_seen = s->s3->npn_seen;
1034

R
Rich Salz 已提交
1035 1036
    s->s3->npn_seen = 0;
    if (!npn_seen || s->ctx->ext.npn_advertised_cb == NULL)
1037
        return EXT_RETURN_NOT_SENT;
1038

R
Rich Salz 已提交
1039 1040
    ret = s->ctx->ext.npn_advertised_cb(s, &npa, &npalen,
                                        s->ctx->ext.npn_advertised_cb_arg);
1041 1042 1043
    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 已提交
1044
            SSLerr(SSL_F_TLS_CONSTRUCT_STOC_NEXT_PROTO_NEG,
1045
                   ERR_R_INTERNAL_ERROR);
1046
            return EXT_RETURN_FAIL;
1047
        }
R
Rich Salz 已提交
1048
        s->s3->npn_seen = 1;
1049 1050
    }

1051
    return EXT_RETURN_SENT;
1052 1053 1054
}
#endif

1055 1056
EXT_RETURN tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context,
                                   X509 *x, size_t chainidx, int *al)
1057 1058
{
    if (s->s3->alpn_selected == NULL)
1059
        return EXT_RETURN_NOT_SENT;
1060 1061 1062 1063 1064 1065 1066 1067 1068

    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 已提交
1069
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_ALPN, ERR_R_INTERNAL_ERROR);
1070
        return EXT_RETURN_FAIL;
1071 1072
    }

1073
    return EXT_RETURN_SENT;
1074 1075 1076
}

#ifndef OPENSSL_NO_SRTP
1077 1078 1079
EXT_RETURN tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt,
                                       unsigned int context, X509 *x,
                                       size_t chainidx, int *al)
1080 1081
{
    if (s->srtp_profile == NULL)
1082
        return EXT_RETURN_NOT_SENT;
M
Matt Caswell 已提交
1083

1084 1085 1086 1087 1088 1089
    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 已提交
1090
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_USE_SRTP, ERR_R_INTERNAL_ERROR);
1091
        return EXT_RETURN_FAIL;
1092 1093
    }

1094
    return EXT_RETURN_SENT;
1095 1096 1097
}
#endif

1098 1099
EXT_RETURN tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al)
1100
{
1101
    if (!s->ext.use_etm)
1102
        return EXT_RETURN_NOT_SENT;
1103 1104 1105 1106 1107 1108 1109 1110 1111

    /*
     * 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) {
1112
        s->ext.use_etm = 0;
1113
        return EXT_RETURN_NOT_SENT;
1114 1115 1116 1117
    }

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
            || !WPACKET_put_bytes_u16(pkt, 0)) {
M
Matt Caswell 已提交
1118
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_ETM, ERR_R_INTERNAL_ERROR);
1119
        return EXT_RETURN_FAIL;
1120 1121
    }

1122
    return EXT_RETURN_SENT;
1123 1124
}

1125 1126
EXT_RETURN tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al)
1127 1128
{
    if ((s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
1129
        return EXT_RETURN_NOT_SENT;
1130 1131 1132

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
            || !WPACKET_put_bytes_u16(pkt, 0)) {
M
Matt Caswell 已提交
1133
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EMS, ERR_R_INTERNAL_ERROR);
1134
        return EXT_RETURN_FAIL;
1135 1136
    }

1137
    return EXT_RETURN_SENT;
1138 1139
}

1140 1141 1142
EXT_RETURN tls_construct_stoc_key_share(SSL *s, WPACKET *pkt,
                                        unsigned int context, X509 *x,
                                        size_t chainidx, int *al)
1143
{
M
Matt Caswell 已提交
1144
#ifndef OPENSSL_NO_TLS1_3
1145 1146 1147 1148 1149
    unsigned char *encodedPoint;
    size_t encoded_pt_len = 0;
    EVP_PKEY *ckey = s->s3->peer_tmp, *skey = NULL;

    if (ckey == NULL) {
1150 1151 1152
        /* No key_share received from client */
        if (s->hello_retry_request) {
            if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
1153 1154 1155
                    || !WPACKET_start_sub_packet_u16(pkt)
                    || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)
                    || !WPACKET_close(pkt)) {
1156 1157
                SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE,
                       ERR_R_INTERNAL_ERROR);
1158
                return EXT_RETURN_FAIL;
1159 1160
            }

1161
            return EXT_RETURN_SENT;
1162 1163 1164
        }

        /* Must be resuming. */
1165 1166 1167
        if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {
            *al = SSL_AD_INTERNAL_ERROR;
            SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1168
            return EXT_RETURN_FAIL;
1169
        }
1170
        return EXT_RETURN_NOT_SENT;
1171 1172 1173 1174 1175
    }

    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 已提交
1176
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1177
        return EXT_RETURN_FAIL;
1178 1179 1180 1181
    }

    skey = ssl_generate_pkey(ckey);
    if (skey == NULL) {
M
Matt Caswell 已提交
1182
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_MALLOC_FAILURE);
1183
        return EXT_RETURN_FAIL;
1184 1185 1186 1187 1188
    }

    /* Generate encoding of server key */
    encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(skey, &encodedPoint);
    if (encoded_pt_len == 0) {
M
Matt Caswell 已提交
1189
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_EC_LIB);
1190
        EVP_PKEY_free(skey);
1191
        return EXT_RETURN_FAIL;
1192 1193 1194 1195
    }

    if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len)
            || !WPACKET_close(pkt)) {
M
Matt Caswell 已提交
1196
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1197 1198
        EVP_PKEY_free(skey);
        OPENSSL_free(encodedPoint);
1199
        return EXT_RETURN_FAIL;
1200 1201 1202 1203 1204 1205
    }
    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 已提交
1206
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1207
        return EXT_RETURN_FAIL;
1208
    }
M
Matt Caswell 已提交
1209
#endif
1210

1211
    return EXT_RETURN_SENT;
1212 1213
}

1214 1215 1216
EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt,
                                            unsigned int context, X509 *x,
                                            size_t chainidx, int *al)
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
{
    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)
1230
        return EXT_RETURN_NOT_SENT;
1231 1232

    if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
M
Matt Caswell 已提交
1233
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_CRYPTOPRO_BUG, ERR_R_INTERNAL_ERROR);
1234
        return EXT_RETURN_FAIL;
1235 1236
    }

1237
    return EXT_RETURN_SENT;
1238
}
1239

1240 1241 1242
EXT_RETURN tls_construct_stoc_early_data(SSL *s, WPACKET *pkt,
                                         unsigned int context, X509 *x,
                                         size_t chainidx, int *al)
1243
{
1244
    if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
1245
        if (s->max_early_data == 0)
1246
            return EXT_RETURN_NOT_SENT;
1247 1248 1249 1250 1251 1252

        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
                || !WPACKET_start_sub_packet_u16(pkt)
                || !WPACKET_put_bytes_u32(pkt, s->max_early_data)
                || !WPACKET_close(pkt)) {
            SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA, ERR_R_INTERNAL_ERROR);
1253
            return EXT_RETURN_FAIL;
1254 1255
        }

1256
        return EXT_RETURN_SENT;
1257 1258
    }

1259
    if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
1260
        return EXT_RETURN_NOT_SENT;
1261 1262 1263 1264 1265

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
            || !WPACKET_start_sub_packet_u16(pkt)
            || !WPACKET_close(pkt)) {
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EARLY_DATA, ERR_R_INTERNAL_ERROR);
1266
        return EXT_RETURN_FAIL;
1267 1268
    }

1269
    return EXT_RETURN_SENT;
1270 1271
}

1272 1273
EXT_RETURN tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al)
1274 1275
{
    if (!s->hit)
1276
        return EXT_RETURN_NOT_SENT;
1277 1278 1279 1280 1281 1282

    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
            || !WPACKET_start_sub_packet_u16(pkt)
            || !WPACKET_put_bytes_u16(pkt, s->session->ext.tick_identity)
            || !WPACKET_close(pkt)) {
        SSLerr(SSL_F_TLS_CONSTRUCT_STOC_PSK, ERR_R_INTERNAL_ERROR);
1283
        return EXT_RETURN_FAIL;
1284 1285
    }

1286
    return EXT_RETURN_SENT;
1287
}