11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
31cb0ef41Sopenharmony_ci * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
41cb0ef41Sopenharmony_ci * Copyright 2005 Nokia. All rights reserved.
51cb0ef41Sopenharmony_ci *
61cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
71cb0ef41Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
81cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at
91cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html
101cb0ef41Sopenharmony_ci */
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include <stdio.h>
131cb0ef41Sopenharmony_ci#include "../ssl_local.h"
141cb0ef41Sopenharmony_ci#include "statem_local.h"
151cb0ef41Sopenharmony_ci#include "internal/constant_time.h"
161cb0ef41Sopenharmony_ci#include "internal/cryptlib.h"
171cb0ef41Sopenharmony_ci#include <openssl/buffer.h>
181cb0ef41Sopenharmony_ci#include <openssl/rand.h>
191cb0ef41Sopenharmony_ci#include <openssl/objects.h>
201cb0ef41Sopenharmony_ci#include <openssl/evp.h>
211cb0ef41Sopenharmony_ci#include <openssl/x509.h>
221cb0ef41Sopenharmony_ci#include <openssl/dh.h>
231cb0ef41Sopenharmony_ci#include <openssl/rsa.h>
241cb0ef41Sopenharmony_ci#include <openssl/bn.h>
251cb0ef41Sopenharmony_ci#include <openssl/md5.h>
261cb0ef41Sopenharmony_ci#include <openssl/trace.h>
271cb0ef41Sopenharmony_ci#include <openssl/core_names.h>
281cb0ef41Sopenharmony_ci#include <openssl/asn1t.h>
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci#define TICKET_NONCE_SIZE       8
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_citypedef struct {
331cb0ef41Sopenharmony_ci  ASN1_TYPE *kxBlob;
341cb0ef41Sopenharmony_ci  ASN1_TYPE *opaqueBlob;
351cb0ef41Sopenharmony_ci} GOST_KX_MESSAGE;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciDECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciASN1_SEQUENCE(GOST_KX_MESSAGE) = {
401cb0ef41Sopenharmony_ci  ASN1_SIMPLE(GOST_KX_MESSAGE,  kxBlob, ASN1_ANY),
411cb0ef41Sopenharmony_ci  ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY),
421cb0ef41Sopenharmony_ci} ASN1_SEQUENCE_END(GOST_KX_MESSAGE)
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciIMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cistatic int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci/*
491cb0ef41Sopenharmony_ci * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
501cb0ef41Sopenharmony_ci * handshake state transitions when a TLSv1.3 server is reading messages from
511cb0ef41Sopenharmony_ci * the client. The message type that the client has sent is provided in |mt|.
521cb0ef41Sopenharmony_ci * The current state is in |s->statem.hand_state|.
531cb0ef41Sopenharmony_ci *
541cb0ef41Sopenharmony_ci * Return values are 1 for success (transition allowed) and  0 on error
551cb0ef41Sopenharmony_ci * (transition not allowed)
561cb0ef41Sopenharmony_ci */
571cb0ef41Sopenharmony_cistatic int ossl_statem_server13_read_transition(SSL *s, int mt)
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci    OSSL_STATEM *st = &s->statem;
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci    /*
621cb0ef41Sopenharmony_ci     * Note: There is no case for TLS_ST_BEFORE because at that stage we have
631cb0ef41Sopenharmony_ci     * not negotiated TLSv1.3 yet, so that case is handled by
641cb0ef41Sopenharmony_ci     * ossl_statem_server_read_transition()
651cb0ef41Sopenharmony_ci     */
661cb0ef41Sopenharmony_ci    switch (st->hand_state) {
671cb0ef41Sopenharmony_ci    default:
681cb0ef41Sopenharmony_ci        break;
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci    case TLS_ST_EARLY_DATA:
711cb0ef41Sopenharmony_ci        if (s->hello_retry_request == SSL_HRR_PENDING) {
721cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_CLIENT_HELLO) {
731cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_CLNT_HELLO;
741cb0ef41Sopenharmony_ci                return 1;
751cb0ef41Sopenharmony_ci            }
761cb0ef41Sopenharmony_ci            break;
771cb0ef41Sopenharmony_ci        } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED
781cb0ef41Sopenharmony_ci                   && !SSL_IS_QUIC(s)) {
791cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_END_OF_EARLY_DATA) {
801cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
811cb0ef41Sopenharmony_ci                return 1;
821cb0ef41Sopenharmony_ci            }
831cb0ef41Sopenharmony_ci            break;
841cb0ef41Sopenharmony_ci        }
851cb0ef41Sopenharmony_ci        /* Fall through */
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci    case TLS_ST_SR_END_OF_EARLY_DATA:
881cb0ef41Sopenharmony_ci    case TLS_ST_SW_FINISHED:
891cb0ef41Sopenharmony_ci        if (s->s3.tmp.cert_request) {
901cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_CERTIFICATE) {
911cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_CERT;
921cb0ef41Sopenharmony_ci                return 1;
931cb0ef41Sopenharmony_ci            }
941cb0ef41Sopenharmony_ci        } else {
951cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_FINISHED) {
961cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_FINISHED;
971cb0ef41Sopenharmony_ci                return 1;
981cb0ef41Sopenharmony_ci            }
991cb0ef41Sopenharmony_ci        }
1001cb0ef41Sopenharmony_ci        break;
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci    case TLS_ST_SR_CERT:
1031cb0ef41Sopenharmony_ci        if (s->session->peer == NULL) {
1041cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_FINISHED) {
1051cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_FINISHED;
1061cb0ef41Sopenharmony_ci                return 1;
1071cb0ef41Sopenharmony_ci            }
1081cb0ef41Sopenharmony_ci        } else {
1091cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
1101cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_CERT_VRFY;
1111cb0ef41Sopenharmony_ci                return 1;
1121cb0ef41Sopenharmony_ci            }
1131cb0ef41Sopenharmony_ci        }
1141cb0ef41Sopenharmony_ci        break;
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci    case TLS_ST_SR_CERT_VRFY:
1171cb0ef41Sopenharmony_ci        if (mt == SSL3_MT_FINISHED) {
1181cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SR_FINISHED;
1191cb0ef41Sopenharmony_ci            return 1;
1201cb0ef41Sopenharmony_ci        }
1211cb0ef41Sopenharmony_ci        break;
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci    case TLS_ST_OK:
1241cb0ef41Sopenharmony_ci        /*
1251cb0ef41Sopenharmony_ci         * Its never ok to start processing handshake messages in the middle of
1261cb0ef41Sopenharmony_ci         * early data (i.e. before we've received the end of early data alert)
1271cb0ef41Sopenharmony_ci         */
1281cb0ef41Sopenharmony_ci        if (s->early_data_state == SSL_EARLY_DATA_READING)
1291cb0ef41Sopenharmony_ci            break;
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci        if (mt == SSL3_MT_CERTIFICATE
1321cb0ef41Sopenharmony_ci                && s->post_handshake_auth == SSL_PHA_REQUESTED) {
1331cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SR_CERT;
1341cb0ef41Sopenharmony_ci            return 1;
1351cb0ef41Sopenharmony_ci        }
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci        if (mt == SSL3_MT_KEY_UPDATE) {
1381cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SR_KEY_UPDATE;
1391cb0ef41Sopenharmony_ci            return 1;
1401cb0ef41Sopenharmony_ci        }
1411cb0ef41Sopenharmony_ci        break;
1421cb0ef41Sopenharmony_ci    }
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci    /* No valid transition found */
1451cb0ef41Sopenharmony_ci    return 0;
1461cb0ef41Sopenharmony_ci}
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci/*
1491cb0ef41Sopenharmony_ci * ossl_statem_server_read_transition() encapsulates the logic for the allowed
1501cb0ef41Sopenharmony_ci * handshake state transitions when the server is reading messages from the
1511cb0ef41Sopenharmony_ci * client. The message type that the client has sent is provided in |mt|. The
1521cb0ef41Sopenharmony_ci * current state is in |s->statem.hand_state|.
1531cb0ef41Sopenharmony_ci *
1541cb0ef41Sopenharmony_ci * Return values are 1 for success (transition allowed) and  0 on error
1551cb0ef41Sopenharmony_ci * (transition not allowed)
1561cb0ef41Sopenharmony_ci */
1571cb0ef41Sopenharmony_ciint ossl_statem_server_read_transition(SSL *s, int mt)
1581cb0ef41Sopenharmony_ci{
1591cb0ef41Sopenharmony_ci    OSSL_STATEM *st = &s->statem;
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
1621cb0ef41Sopenharmony_ci        if (!ossl_statem_server13_read_transition(s, mt))
1631cb0ef41Sopenharmony_ci            goto err;
1641cb0ef41Sopenharmony_ci        return 1;
1651cb0ef41Sopenharmony_ci    }
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci    switch (st->hand_state) {
1681cb0ef41Sopenharmony_ci    default:
1691cb0ef41Sopenharmony_ci        break;
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci    case TLS_ST_BEFORE:
1721cb0ef41Sopenharmony_ci    case TLS_ST_OK:
1731cb0ef41Sopenharmony_ci    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
1741cb0ef41Sopenharmony_ci        if (mt == SSL3_MT_CLIENT_HELLO) {
1751cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SR_CLNT_HELLO;
1761cb0ef41Sopenharmony_ci            return 1;
1771cb0ef41Sopenharmony_ci        }
1781cb0ef41Sopenharmony_ci        break;
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci    case TLS_ST_SW_SRVR_DONE:
1811cb0ef41Sopenharmony_ci        /*
1821cb0ef41Sopenharmony_ci         * If we get a CKE message after a ServerDone then either
1831cb0ef41Sopenharmony_ci         * 1) We didn't request a Certificate
1841cb0ef41Sopenharmony_ci         * OR
1851cb0ef41Sopenharmony_ci         * 2) If we did request one then
1861cb0ef41Sopenharmony_ci         *      a) We allow no Certificate to be returned
1871cb0ef41Sopenharmony_ci         *      AND
1881cb0ef41Sopenharmony_ci         *      b) We are running SSL3 (in TLS1.0+ the client must return a 0
1891cb0ef41Sopenharmony_ci         *         list if we requested a certificate)
1901cb0ef41Sopenharmony_ci         */
1911cb0ef41Sopenharmony_ci        if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
1921cb0ef41Sopenharmony_ci            if (s->s3.tmp.cert_request) {
1931cb0ef41Sopenharmony_ci                if (s->version == SSL3_VERSION) {
1941cb0ef41Sopenharmony_ci                    if ((s->verify_mode & SSL_VERIFY_PEER)
1951cb0ef41Sopenharmony_ci                        && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
1961cb0ef41Sopenharmony_ci                        /*
1971cb0ef41Sopenharmony_ci                         * This isn't an unexpected message as such - we're just
1981cb0ef41Sopenharmony_ci                         * not going to accept it because we require a client
1991cb0ef41Sopenharmony_ci                         * cert.
2001cb0ef41Sopenharmony_ci                         */
2011cb0ef41Sopenharmony_ci                        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2021cb0ef41Sopenharmony_ci                                 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2031cb0ef41Sopenharmony_ci                        return 0;
2041cb0ef41Sopenharmony_ci                    }
2051cb0ef41Sopenharmony_ci                    st->hand_state = TLS_ST_SR_KEY_EXCH;
2061cb0ef41Sopenharmony_ci                    return 1;
2071cb0ef41Sopenharmony_ci                }
2081cb0ef41Sopenharmony_ci            } else {
2091cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_KEY_EXCH;
2101cb0ef41Sopenharmony_ci                return 1;
2111cb0ef41Sopenharmony_ci            }
2121cb0ef41Sopenharmony_ci        } else if (s->s3.tmp.cert_request) {
2131cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_CERTIFICATE) {
2141cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_CERT;
2151cb0ef41Sopenharmony_ci                return 1;
2161cb0ef41Sopenharmony_ci            }
2171cb0ef41Sopenharmony_ci        }
2181cb0ef41Sopenharmony_ci        break;
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci    case TLS_ST_SR_CERT:
2211cb0ef41Sopenharmony_ci        if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2221cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SR_KEY_EXCH;
2231cb0ef41Sopenharmony_ci            return 1;
2241cb0ef41Sopenharmony_ci        }
2251cb0ef41Sopenharmony_ci        break;
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci    case TLS_ST_SR_KEY_EXCH:
2281cb0ef41Sopenharmony_ci        /*
2291cb0ef41Sopenharmony_ci         * We should only process a CertificateVerify message if we have
2301cb0ef41Sopenharmony_ci         * received a Certificate from the client. If so then |s->session->peer|
2311cb0ef41Sopenharmony_ci         * will be non NULL. In some instances a CertificateVerify message is
2321cb0ef41Sopenharmony_ci         * not required even if the peer has sent a Certificate (e.g. such as in
2331cb0ef41Sopenharmony_ci         * the case of static DH). In that case |st->no_cert_verify| should be
2341cb0ef41Sopenharmony_ci         * set.
2351cb0ef41Sopenharmony_ci         */
2361cb0ef41Sopenharmony_ci        if (s->session->peer == NULL || st->no_cert_verify) {
2371cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
2381cb0ef41Sopenharmony_ci                /*
2391cb0ef41Sopenharmony_ci                 * For the ECDH ciphersuites when the client sends its ECDH
2401cb0ef41Sopenharmony_ci                 * pub key in a certificate, the CertificateVerify message is
2411cb0ef41Sopenharmony_ci                 * not sent. Also for GOST ciphersuites when the client uses
2421cb0ef41Sopenharmony_ci                 * its key from the certificate for key exchange.
2431cb0ef41Sopenharmony_ci                 */
2441cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_CHANGE;
2451cb0ef41Sopenharmony_ci                return 1;
2461cb0ef41Sopenharmony_ci            }
2471cb0ef41Sopenharmony_ci        } else {
2481cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
2491cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_CERT_VRFY;
2501cb0ef41Sopenharmony_ci                return 1;
2511cb0ef41Sopenharmony_ci            }
2521cb0ef41Sopenharmony_ci        }
2531cb0ef41Sopenharmony_ci        break;
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ci    case TLS_ST_SR_CERT_VRFY:
2561cb0ef41Sopenharmony_ci        if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
2571cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SR_CHANGE;
2581cb0ef41Sopenharmony_ci            return 1;
2591cb0ef41Sopenharmony_ci        }
2601cb0ef41Sopenharmony_ci        break;
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_ci    case TLS_ST_SR_CHANGE:
2631cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_NEXTPROTONEG
2641cb0ef41Sopenharmony_ci        if (s->s3.npn_seen) {
2651cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_NEXT_PROTO) {
2661cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_NEXT_PROTO;
2671cb0ef41Sopenharmony_ci                return 1;
2681cb0ef41Sopenharmony_ci            }
2691cb0ef41Sopenharmony_ci        } else {
2701cb0ef41Sopenharmony_ci#endif
2711cb0ef41Sopenharmony_ci            if (mt == SSL3_MT_FINISHED) {
2721cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SR_FINISHED;
2731cb0ef41Sopenharmony_ci                return 1;
2741cb0ef41Sopenharmony_ci            }
2751cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_NEXTPROTONEG
2761cb0ef41Sopenharmony_ci        }
2771cb0ef41Sopenharmony_ci#endif
2781cb0ef41Sopenharmony_ci        break;
2791cb0ef41Sopenharmony_ci
2801cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_NEXTPROTONEG
2811cb0ef41Sopenharmony_ci    case TLS_ST_SR_NEXT_PROTO:
2821cb0ef41Sopenharmony_ci        if (mt == SSL3_MT_FINISHED) {
2831cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SR_FINISHED;
2841cb0ef41Sopenharmony_ci            return 1;
2851cb0ef41Sopenharmony_ci        }
2861cb0ef41Sopenharmony_ci        break;
2871cb0ef41Sopenharmony_ci#endif
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci    case TLS_ST_SW_FINISHED:
2901cb0ef41Sopenharmony_ci        if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
2911cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SR_CHANGE;
2921cb0ef41Sopenharmony_ci            return 1;
2931cb0ef41Sopenharmony_ci        }
2941cb0ef41Sopenharmony_ci        break;
2951cb0ef41Sopenharmony_ci    }
2961cb0ef41Sopenharmony_ci
2971cb0ef41Sopenharmony_ci err:
2981cb0ef41Sopenharmony_ci    /* No valid transition found */
2991cb0ef41Sopenharmony_ci    if (SSL_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
3001cb0ef41Sopenharmony_ci        BIO *rbio;
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_ci        /*
3031cb0ef41Sopenharmony_ci         * CCS messages don't have a message sequence number so this is probably
3041cb0ef41Sopenharmony_ci         * because of an out-of-order CCS. We'll just drop it.
3051cb0ef41Sopenharmony_ci         */
3061cb0ef41Sopenharmony_ci        s->init_num = 0;
3071cb0ef41Sopenharmony_ci        s->rwstate = SSL_READING;
3081cb0ef41Sopenharmony_ci        rbio = SSL_get_rbio(s);
3091cb0ef41Sopenharmony_ci        BIO_clear_retry_flags(rbio);
3101cb0ef41Sopenharmony_ci        BIO_set_retry_read(rbio);
3111cb0ef41Sopenharmony_ci        return 0;
3121cb0ef41Sopenharmony_ci    }
3131cb0ef41Sopenharmony_ci    SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
3141cb0ef41Sopenharmony_ci    return 0;
3151cb0ef41Sopenharmony_ci}
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci/*
3181cb0ef41Sopenharmony_ci * Should we send a ServerKeyExchange message?
3191cb0ef41Sopenharmony_ci *
3201cb0ef41Sopenharmony_ci * Valid return values are:
3211cb0ef41Sopenharmony_ci *   1: Yes
3221cb0ef41Sopenharmony_ci *   0: No
3231cb0ef41Sopenharmony_ci */
3241cb0ef41Sopenharmony_cistatic int send_server_key_exchange(SSL *s)
3251cb0ef41Sopenharmony_ci{
3261cb0ef41Sopenharmony_ci    unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ci    /*
3291cb0ef41Sopenharmony_ci     * only send a ServerKeyExchange if DH or fortezza but we have a
3301cb0ef41Sopenharmony_ci     * sign only certificate PSK: may send PSK identity hints For
3311cb0ef41Sopenharmony_ci     * ECC ciphersuites, we send a serverKeyExchange message only if
3321cb0ef41Sopenharmony_ci     * the cipher suite is either ECDH-anon or ECDHE. In other cases,
3331cb0ef41Sopenharmony_ci     * the server certificate contains the server's public key for
3341cb0ef41Sopenharmony_ci     * key exchange.
3351cb0ef41Sopenharmony_ci     */
3361cb0ef41Sopenharmony_ci    if (alg_k & (SSL_kDHE | SSL_kECDHE)
3371cb0ef41Sopenharmony_ci        /*
3381cb0ef41Sopenharmony_ci         * PSK: send ServerKeyExchange if PSK identity hint if
3391cb0ef41Sopenharmony_ci         * provided
3401cb0ef41Sopenharmony_ci         */
3411cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_PSK
3421cb0ef41Sopenharmony_ci        /* Only send SKE if we have identity hint for plain PSK */
3431cb0ef41Sopenharmony_ci        || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
3441cb0ef41Sopenharmony_ci            && s->cert->psk_identity_hint)
3451cb0ef41Sopenharmony_ci        /* For other PSK always send SKE */
3461cb0ef41Sopenharmony_ci        || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
3471cb0ef41Sopenharmony_ci#endif
3481cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SRP
3491cb0ef41Sopenharmony_ci        /* SRP: send ServerKeyExchange */
3501cb0ef41Sopenharmony_ci        || (alg_k & SSL_kSRP)
3511cb0ef41Sopenharmony_ci#endif
3521cb0ef41Sopenharmony_ci        ) {
3531cb0ef41Sopenharmony_ci        return 1;
3541cb0ef41Sopenharmony_ci    }
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ci    return 0;
3571cb0ef41Sopenharmony_ci}
3581cb0ef41Sopenharmony_ci
3591cb0ef41Sopenharmony_ci/*
3601cb0ef41Sopenharmony_ci * Should we send a CertificateRequest message?
3611cb0ef41Sopenharmony_ci *
3621cb0ef41Sopenharmony_ci * Valid return values are:
3631cb0ef41Sopenharmony_ci *   1: Yes
3641cb0ef41Sopenharmony_ci *   0: No
3651cb0ef41Sopenharmony_ci */
3661cb0ef41Sopenharmony_ciint send_certificate_request(SSL *s)
3671cb0ef41Sopenharmony_ci{
3681cb0ef41Sopenharmony_ci    if (
3691cb0ef41Sopenharmony_ci           /* don't request cert unless asked for it: */
3701cb0ef41Sopenharmony_ci           s->verify_mode & SSL_VERIFY_PEER
3711cb0ef41Sopenharmony_ci           /*
3721cb0ef41Sopenharmony_ci            * don't request if post-handshake-only unless doing
3731cb0ef41Sopenharmony_ci            * post-handshake in TLSv1.3:
3741cb0ef41Sopenharmony_ci            */
3751cb0ef41Sopenharmony_ci           && (!SSL_IS_TLS13(s) || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)
3761cb0ef41Sopenharmony_ci               || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)
3771cb0ef41Sopenharmony_ci           /*
3781cb0ef41Sopenharmony_ci            * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
3791cb0ef41Sopenharmony_ci            * a second time:
3801cb0ef41Sopenharmony_ci            */
3811cb0ef41Sopenharmony_ci           && (s->certreqs_sent < 1 ||
3821cb0ef41Sopenharmony_ci               !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
3831cb0ef41Sopenharmony_ci           /*
3841cb0ef41Sopenharmony_ci            * never request cert in anonymous ciphersuites (see
3851cb0ef41Sopenharmony_ci            * section "Certificate request" in SSL 3 drafts and in
3861cb0ef41Sopenharmony_ci            * RFC 2246):
3871cb0ef41Sopenharmony_ci            */
3881cb0ef41Sopenharmony_ci           && (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL)
3891cb0ef41Sopenharmony_ci               /*
3901cb0ef41Sopenharmony_ci                * ... except when the application insists on
3911cb0ef41Sopenharmony_ci                * verification (against the specs, but statem_clnt.c accepts
3921cb0ef41Sopenharmony_ci                * this for SSL 3)
3931cb0ef41Sopenharmony_ci                */
3941cb0ef41Sopenharmony_ci               || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
3951cb0ef41Sopenharmony_ci           /* don't request certificate for SRP auth */
3961cb0ef41Sopenharmony_ci           && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP)
3971cb0ef41Sopenharmony_ci           /*
3981cb0ef41Sopenharmony_ci            * With normal PSK Certificates and Certificate Requests
3991cb0ef41Sopenharmony_ci            * are omitted
4001cb0ef41Sopenharmony_ci            */
4011cb0ef41Sopenharmony_ci           && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
4021cb0ef41Sopenharmony_ci        return 1;
4031cb0ef41Sopenharmony_ci    }
4041cb0ef41Sopenharmony_ci
4051cb0ef41Sopenharmony_ci    return 0;
4061cb0ef41Sopenharmony_ci}
4071cb0ef41Sopenharmony_ci
4081cb0ef41Sopenharmony_ci/*
4091cb0ef41Sopenharmony_ci * ossl_statem_server13_write_transition() works out what handshake state to
4101cb0ef41Sopenharmony_ci * move to next when a TLSv1.3 server is writing messages to be sent to the
4111cb0ef41Sopenharmony_ci * client.
4121cb0ef41Sopenharmony_ci */
4131cb0ef41Sopenharmony_cistatic WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
4141cb0ef41Sopenharmony_ci{
4151cb0ef41Sopenharmony_ci    OSSL_STATEM *st = &s->statem;
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci    /*
4181cb0ef41Sopenharmony_ci     * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
4191cb0ef41Sopenharmony_ci     * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
4201cb0ef41Sopenharmony_ci     */
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ci    switch (st->hand_state) {
4231cb0ef41Sopenharmony_ci    default:
4241cb0ef41Sopenharmony_ci        /* Shouldn't happen */
4251cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4261cb0ef41Sopenharmony_ci        return WRITE_TRAN_ERROR;
4271cb0ef41Sopenharmony_ci
4281cb0ef41Sopenharmony_ci    case TLS_ST_OK:
4291cb0ef41Sopenharmony_ci        if (s->key_update != SSL_KEY_UPDATE_NONE) {
4301cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_KEY_UPDATE;
4311cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
4321cb0ef41Sopenharmony_ci        }
4331cb0ef41Sopenharmony_ci        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
4341cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_CERT_REQ;
4351cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
4361cb0ef41Sopenharmony_ci        }
4371cb0ef41Sopenharmony_ci        if (s->ext.extra_tickets_expected > 0) {
4381cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_SESSION_TICKET;
4391cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
4401cb0ef41Sopenharmony_ci        }
4411cb0ef41Sopenharmony_ci        /* Try to read from the client instead */
4421cb0ef41Sopenharmony_ci        return WRITE_TRAN_FINISHED;
4431cb0ef41Sopenharmony_ci
4441cb0ef41Sopenharmony_ci    case TLS_ST_SR_CLNT_HELLO:
4451cb0ef41Sopenharmony_ci        st->hand_state = TLS_ST_SW_SRVR_HELLO;
4461cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
4471cb0ef41Sopenharmony_ci
4481cb0ef41Sopenharmony_ci    case TLS_ST_SW_SRVR_HELLO:
4491cb0ef41Sopenharmony_ci        if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
4501cb0ef41Sopenharmony_ci                && s->hello_retry_request != SSL_HRR_COMPLETE)
4511cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_CHANGE;
4521cb0ef41Sopenharmony_ci        else if (s->hello_retry_request == SSL_HRR_PENDING)
4531cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_EARLY_DATA;
4541cb0ef41Sopenharmony_ci        else
4551cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
4561cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
4571cb0ef41Sopenharmony_ci
4581cb0ef41Sopenharmony_ci    case TLS_ST_SW_CHANGE:
4591cb0ef41Sopenharmony_ci        if (s->hello_retry_request == SSL_HRR_PENDING)
4601cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_EARLY_DATA;
4611cb0ef41Sopenharmony_ci        else
4621cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
4631cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
4641cb0ef41Sopenharmony_ci
4651cb0ef41Sopenharmony_ci    case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
4661cb0ef41Sopenharmony_ci        if (s->hit)
4671cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_FINISHED;
4681cb0ef41Sopenharmony_ci        else if (send_certificate_request(s))
4691cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_CERT_REQ;
4701cb0ef41Sopenharmony_ci        else
4711cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_CERT;
4721cb0ef41Sopenharmony_ci
4731cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT_REQ:
4761cb0ef41Sopenharmony_ci        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
4771cb0ef41Sopenharmony_ci            s->post_handshake_auth = SSL_PHA_REQUESTED;
4781cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_OK;
4791cb0ef41Sopenharmony_ci        } else {
4801cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_CERT;
4811cb0ef41Sopenharmony_ci        }
4821cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
4831cb0ef41Sopenharmony_ci
4841cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT:
4851cb0ef41Sopenharmony_ci        st->hand_state = TLS_ST_SW_CERT_VRFY;
4861cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
4871cb0ef41Sopenharmony_ci
4881cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT_VRFY:
4891cb0ef41Sopenharmony_ci        st->hand_state = TLS_ST_SW_FINISHED;
4901cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
4911cb0ef41Sopenharmony_ci
4921cb0ef41Sopenharmony_ci    case TLS_ST_SW_FINISHED:
4931cb0ef41Sopenharmony_ci        st->hand_state = TLS_ST_EARLY_DATA;
4941cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
4951cb0ef41Sopenharmony_ci
4961cb0ef41Sopenharmony_ci    case TLS_ST_EARLY_DATA:
4971cb0ef41Sopenharmony_ci        return WRITE_TRAN_FINISHED;
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_ci    case TLS_ST_SR_FINISHED:
5001cb0ef41Sopenharmony_ci        /*
5011cb0ef41Sopenharmony_ci         * Technically we have finished the handshake at this point, but we're
5021cb0ef41Sopenharmony_ci         * going to remain "in_init" for now and write out any session tickets
5031cb0ef41Sopenharmony_ci         * immediately.
5041cb0ef41Sopenharmony_ci         */
5051cb0ef41Sopenharmony_ci        if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
5061cb0ef41Sopenharmony_ci            s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
5071cb0ef41Sopenharmony_ci        } else if (!s->ext.ticket_expected) {
5081cb0ef41Sopenharmony_ci            /*
5091cb0ef41Sopenharmony_ci             * If we're not going to renew the ticket then we just finish the
5101cb0ef41Sopenharmony_ci             * handshake at this point.
5111cb0ef41Sopenharmony_ci             */
5121cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_OK;
5131cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
5141cb0ef41Sopenharmony_ci        }
5151cb0ef41Sopenharmony_ci        if (s->num_tickets > s->sent_tickets)
5161cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_SESSION_TICKET;
5171cb0ef41Sopenharmony_ci        else
5181cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_OK;
5191cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
5201cb0ef41Sopenharmony_ci
5211cb0ef41Sopenharmony_ci    case TLS_ST_SR_KEY_UPDATE:
5221cb0ef41Sopenharmony_ci    case TLS_ST_SW_KEY_UPDATE:
5231cb0ef41Sopenharmony_ci        st->hand_state = TLS_ST_OK;
5241cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
5251cb0ef41Sopenharmony_ci
5261cb0ef41Sopenharmony_ci    case TLS_ST_SW_SESSION_TICKET:
5271cb0ef41Sopenharmony_ci        /* In a resumption we only ever send a maximum of one new ticket.
5281cb0ef41Sopenharmony_ci         * Following an initial handshake we send the number of tickets we have
5291cb0ef41Sopenharmony_ci         * been configured for.
5301cb0ef41Sopenharmony_ci         */
5311cb0ef41Sopenharmony_ci        if (!SSL_IS_FIRST_HANDSHAKE(s) && s->ext.extra_tickets_expected > 0) {
5321cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
5331cb0ef41Sopenharmony_ci        } else if (s->hit || s->num_tickets <= s->sent_tickets) {
5341cb0ef41Sopenharmony_ci            /* We've written enough tickets out. */
5351cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_OK;
5361cb0ef41Sopenharmony_ci        }
5371cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
5381cb0ef41Sopenharmony_ci    }
5391cb0ef41Sopenharmony_ci}
5401cb0ef41Sopenharmony_ci
5411cb0ef41Sopenharmony_ci/*
5421cb0ef41Sopenharmony_ci * ossl_statem_server_write_transition() works out what handshake state to move
5431cb0ef41Sopenharmony_ci * to next when the server is writing messages to be sent to the client.
5441cb0ef41Sopenharmony_ci */
5451cb0ef41Sopenharmony_ciWRITE_TRAN ossl_statem_server_write_transition(SSL *s)
5461cb0ef41Sopenharmony_ci{
5471cb0ef41Sopenharmony_ci    OSSL_STATEM *st = &s->statem;
5481cb0ef41Sopenharmony_ci
5491cb0ef41Sopenharmony_ci    /*
5501cb0ef41Sopenharmony_ci     * Note that before the ClientHello we don't know what version we are going
5511cb0ef41Sopenharmony_ci     * to negotiate yet, so we don't take this branch until later
5521cb0ef41Sopenharmony_ci     */
5531cb0ef41Sopenharmony_ci
5541cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s))
5551cb0ef41Sopenharmony_ci        return ossl_statem_server13_write_transition(s);
5561cb0ef41Sopenharmony_ci
5571cb0ef41Sopenharmony_ci    switch (st->hand_state) {
5581cb0ef41Sopenharmony_ci    default:
5591cb0ef41Sopenharmony_ci        /* Shouldn't happen */
5601cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
5611cb0ef41Sopenharmony_ci        return WRITE_TRAN_ERROR;
5621cb0ef41Sopenharmony_ci
5631cb0ef41Sopenharmony_ci    case TLS_ST_OK:
5641cb0ef41Sopenharmony_ci        if (st->request_state == TLS_ST_SW_HELLO_REQ) {
5651cb0ef41Sopenharmony_ci            /* We must be trying to renegotiate */
5661cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_HELLO_REQ;
5671cb0ef41Sopenharmony_ci            st->request_state = TLS_ST_BEFORE;
5681cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
5691cb0ef41Sopenharmony_ci        }
5701cb0ef41Sopenharmony_ci        /* Must be an incoming ClientHello */
5711cb0ef41Sopenharmony_ci        if (!tls_setup_handshake(s)) {
5721cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
5731cb0ef41Sopenharmony_ci            return WRITE_TRAN_ERROR;
5741cb0ef41Sopenharmony_ci        }
5751cb0ef41Sopenharmony_ci        /* Fall through */
5761cb0ef41Sopenharmony_ci
5771cb0ef41Sopenharmony_ci    case TLS_ST_BEFORE:
5781cb0ef41Sopenharmony_ci        /* Just go straight to trying to read from the client */
5791cb0ef41Sopenharmony_ci        return WRITE_TRAN_FINISHED;
5801cb0ef41Sopenharmony_ci
5811cb0ef41Sopenharmony_ci    case TLS_ST_SW_HELLO_REQ:
5821cb0ef41Sopenharmony_ci        st->hand_state = TLS_ST_OK;
5831cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
5841cb0ef41Sopenharmony_ci
5851cb0ef41Sopenharmony_ci    case TLS_ST_SR_CLNT_HELLO:
5861cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
5871cb0ef41Sopenharmony_ci            && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) {
5881cb0ef41Sopenharmony_ci            st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
5891cb0ef41Sopenharmony_ci        } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
5901cb0ef41Sopenharmony_ci            /* We must have rejected the renegotiation */
5911cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_OK;
5921cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
5931cb0ef41Sopenharmony_ci        } else {
5941cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_SRVR_HELLO;
5951cb0ef41Sopenharmony_ci        }
5961cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
5971cb0ef41Sopenharmony_ci
5981cb0ef41Sopenharmony_ci    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
5991cb0ef41Sopenharmony_ci        return WRITE_TRAN_FINISHED;
6001cb0ef41Sopenharmony_ci
6011cb0ef41Sopenharmony_ci    case TLS_ST_SW_SRVR_HELLO:
6021cb0ef41Sopenharmony_ci        if (s->hit) {
6031cb0ef41Sopenharmony_ci            if (s->ext.ticket_expected)
6041cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SW_SESSION_TICKET;
6051cb0ef41Sopenharmony_ci            else
6061cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SW_CHANGE;
6071cb0ef41Sopenharmony_ci        } else {
6081cb0ef41Sopenharmony_ci            /* Check if it is anon DH or anon ECDH, */
6091cb0ef41Sopenharmony_ci            /* normal PSK or SRP */
6101cb0ef41Sopenharmony_ci            if (!(s->s3.tmp.new_cipher->algorithm_auth &
6111cb0ef41Sopenharmony_ci                  (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
6121cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SW_CERT;
6131cb0ef41Sopenharmony_ci            } else if (send_server_key_exchange(s)) {
6141cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SW_KEY_EXCH;
6151cb0ef41Sopenharmony_ci            } else if (send_certificate_request(s)) {
6161cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SW_CERT_REQ;
6171cb0ef41Sopenharmony_ci            } else {
6181cb0ef41Sopenharmony_ci                st->hand_state = TLS_ST_SW_SRVR_DONE;
6191cb0ef41Sopenharmony_ci            }
6201cb0ef41Sopenharmony_ci        }
6211cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
6221cb0ef41Sopenharmony_ci
6231cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT:
6241cb0ef41Sopenharmony_ci        if (s->ext.status_expected) {
6251cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_CERT_STATUS;
6261cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
6271cb0ef41Sopenharmony_ci        }
6281cb0ef41Sopenharmony_ci        /* Fall through */
6291cb0ef41Sopenharmony_ci
6301cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT_STATUS:
6311cb0ef41Sopenharmony_ci        if (send_server_key_exchange(s)) {
6321cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_KEY_EXCH;
6331cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
6341cb0ef41Sopenharmony_ci        }
6351cb0ef41Sopenharmony_ci        /* Fall through */
6361cb0ef41Sopenharmony_ci
6371cb0ef41Sopenharmony_ci    case TLS_ST_SW_KEY_EXCH:
6381cb0ef41Sopenharmony_ci        if (send_certificate_request(s)) {
6391cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_CERT_REQ;
6401cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
6411cb0ef41Sopenharmony_ci        }
6421cb0ef41Sopenharmony_ci        /* Fall through */
6431cb0ef41Sopenharmony_ci
6441cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT_REQ:
6451cb0ef41Sopenharmony_ci        st->hand_state = TLS_ST_SW_SRVR_DONE;
6461cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
6471cb0ef41Sopenharmony_ci
6481cb0ef41Sopenharmony_ci    case TLS_ST_SW_SRVR_DONE:
6491cb0ef41Sopenharmony_ci        return WRITE_TRAN_FINISHED;
6501cb0ef41Sopenharmony_ci
6511cb0ef41Sopenharmony_ci    case TLS_ST_SR_FINISHED:
6521cb0ef41Sopenharmony_ci        if (s->hit) {
6531cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_OK;
6541cb0ef41Sopenharmony_ci            return WRITE_TRAN_CONTINUE;
6551cb0ef41Sopenharmony_ci        } else if (s->ext.ticket_expected) {
6561cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_SESSION_TICKET;
6571cb0ef41Sopenharmony_ci        } else {
6581cb0ef41Sopenharmony_ci            st->hand_state = TLS_ST_SW_CHANGE;
6591cb0ef41Sopenharmony_ci        }
6601cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
6611cb0ef41Sopenharmony_ci
6621cb0ef41Sopenharmony_ci    case TLS_ST_SW_SESSION_TICKET:
6631cb0ef41Sopenharmony_ci        st->hand_state = TLS_ST_SW_CHANGE;
6641cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
6651cb0ef41Sopenharmony_ci
6661cb0ef41Sopenharmony_ci    case TLS_ST_SW_CHANGE:
6671cb0ef41Sopenharmony_ci        st->hand_state = TLS_ST_SW_FINISHED;
6681cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
6691cb0ef41Sopenharmony_ci
6701cb0ef41Sopenharmony_ci    case TLS_ST_SW_FINISHED:
6711cb0ef41Sopenharmony_ci        if (s->hit) {
6721cb0ef41Sopenharmony_ci            return WRITE_TRAN_FINISHED;
6731cb0ef41Sopenharmony_ci        }
6741cb0ef41Sopenharmony_ci        st->hand_state = TLS_ST_OK;
6751cb0ef41Sopenharmony_ci        return WRITE_TRAN_CONTINUE;
6761cb0ef41Sopenharmony_ci    }
6771cb0ef41Sopenharmony_ci}
6781cb0ef41Sopenharmony_ci
6791cb0ef41Sopenharmony_ci/*
6801cb0ef41Sopenharmony_ci * Perform any pre work that needs to be done prior to sending a message from
6811cb0ef41Sopenharmony_ci * the server to the client.
6821cb0ef41Sopenharmony_ci */
6831cb0ef41Sopenharmony_ciWORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
6841cb0ef41Sopenharmony_ci{
6851cb0ef41Sopenharmony_ci    OSSL_STATEM *st = &s->statem;
6861cb0ef41Sopenharmony_ci
6871cb0ef41Sopenharmony_ci    switch (st->hand_state) {
6881cb0ef41Sopenharmony_ci    default:
6891cb0ef41Sopenharmony_ci        /* No pre work to be done */
6901cb0ef41Sopenharmony_ci        break;
6911cb0ef41Sopenharmony_ci
6921cb0ef41Sopenharmony_ci    case TLS_ST_SW_HELLO_REQ:
6931cb0ef41Sopenharmony_ci        s->shutdown = 0;
6941cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s))
6951cb0ef41Sopenharmony_ci            dtls1_clear_sent_buffer(s);
6961cb0ef41Sopenharmony_ci        break;
6971cb0ef41Sopenharmony_ci
6981cb0ef41Sopenharmony_ci    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
6991cb0ef41Sopenharmony_ci        s->shutdown = 0;
7001cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s)) {
7011cb0ef41Sopenharmony_ci            dtls1_clear_sent_buffer(s);
7021cb0ef41Sopenharmony_ci            /* We don't buffer this message so don't use the timer */
7031cb0ef41Sopenharmony_ci            st->use_timer = 0;
7041cb0ef41Sopenharmony_ci        }
7051cb0ef41Sopenharmony_ci        break;
7061cb0ef41Sopenharmony_ci
7071cb0ef41Sopenharmony_ci    case TLS_ST_SW_SRVR_HELLO:
7081cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s)) {
7091cb0ef41Sopenharmony_ci            /*
7101cb0ef41Sopenharmony_ci             * Messages we write from now on should be buffered and
7111cb0ef41Sopenharmony_ci             * retransmitted if necessary, so we need to use the timer now
7121cb0ef41Sopenharmony_ci             */
7131cb0ef41Sopenharmony_ci            st->use_timer = 1;
7141cb0ef41Sopenharmony_ci        }
7151cb0ef41Sopenharmony_ci        break;
7161cb0ef41Sopenharmony_ci
7171cb0ef41Sopenharmony_ci    case TLS_ST_SW_SRVR_DONE:
7181cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SCTP
7191cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
7201cb0ef41Sopenharmony_ci            /* Calls SSLfatal() as required */
7211cb0ef41Sopenharmony_ci            return dtls_wait_for_dry(s);
7221cb0ef41Sopenharmony_ci        }
7231cb0ef41Sopenharmony_ci#endif
7241cb0ef41Sopenharmony_ci        return WORK_FINISHED_CONTINUE;
7251cb0ef41Sopenharmony_ci
7261cb0ef41Sopenharmony_ci    case TLS_ST_SW_SESSION_TICKET:
7271cb0ef41Sopenharmony_ci        if (SSL_IS_TLS13(s) && s->sent_tickets == 0
7281cb0ef41Sopenharmony_ci                && s->ext.extra_tickets_expected == 0) {
7291cb0ef41Sopenharmony_ci            /*
7301cb0ef41Sopenharmony_ci             * Actually this is the end of the handshake, but we're going
7311cb0ef41Sopenharmony_ci             * straight into writing the session ticket out. So we finish off
7321cb0ef41Sopenharmony_ci             * the handshake, but keep the various buffers active.
7331cb0ef41Sopenharmony_ci             *
7341cb0ef41Sopenharmony_ci             * Calls SSLfatal as required.
7351cb0ef41Sopenharmony_ci             */
7361cb0ef41Sopenharmony_ci            return tls_finish_handshake(s, wst, 0, 0);
7371cb0ef41Sopenharmony_ci        }
7381cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s)) {
7391cb0ef41Sopenharmony_ci            /*
7401cb0ef41Sopenharmony_ci             * We're into the last flight. We don't retransmit the last flight
7411cb0ef41Sopenharmony_ci             * unless we need to, so we don't use the timer
7421cb0ef41Sopenharmony_ci             */
7431cb0ef41Sopenharmony_ci            st->use_timer = 0;
7441cb0ef41Sopenharmony_ci        }
7451cb0ef41Sopenharmony_ci        break;
7461cb0ef41Sopenharmony_ci
7471cb0ef41Sopenharmony_ci    case TLS_ST_SW_CHANGE:
7481cb0ef41Sopenharmony_ci        if (SSL_IS_TLS13(s))
7491cb0ef41Sopenharmony_ci            break;
7501cb0ef41Sopenharmony_ci        /* Writes to s->session are only safe for initial handshakes */
7511cb0ef41Sopenharmony_ci        if (s->session->cipher == NULL) {
7521cb0ef41Sopenharmony_ci            s->session->cipher = s->s3.tmp.new_cipher;
7531cb0ef41Sopenharmony_ci        } else if (s->session->cipher != s->s3.tmp.new_cipher) {
7541cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
7551cb0ef41Sopenharmony_ci            return WORK_ERROR;
7561cb0ef41Sopenharmony_ci        }
7571cb0ef41Sopenharmony_ci        if (!s->method->ssl3_enc->setup_key_block(s)) {
7581cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
7591cb0ef41Sopenharmony_ci            return WORK_ERROR;
7601cb0ef41Sopenharmony_ci        }
7611cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s)) {
7621cb0ef41Sopenharmony_ci            /*
7631cb0ef41Sopenharmony_ci             * We're into the last flight. We don't retransmit the last flight
7641cb0ef41Sopenharmony_ci             * unless we need to, so we don't use the timer. This might have
7651cb0ef41Sopenharmony_ci             * already been set to 0 if we sent a NewSessionTicket message,
7661cb0ef41Sopenharmony_ci             * but we'll set it again here in case we didn't.
7671cb0ef41Sopenharmony_ci             */
7681cb0ef41Sopenharmony_ci            st->use_timer = 0;
7691cb0ef41Sopenharmony_ci        }
7701cb0ef41Sopenharmony_ci        return WORK_FINISHED_CONTINUE;
7711cb0ef41Sopenharmony_ci
7721cb0ef41Sopenharmony_ci    case TLS_ST_EARLY_DATA:
7731cb0ef41Sopenharmony_ci        if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING
7741cb0ef41Sopenharmony_ci                && (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
7751cb0ef41Sopenharmony_ci            return WORK_FINISHED_CONTINUE;
7761cb0ef41Sopenharmony_ci        /* Fall through */
7771cb0ef41Sopenharmony_ci
7781cb0ef41Sopenharmony_ci    case TLS_ST_OK:
7791cb0ef41Sopenharmony_ci        /* Calls SSLfatal() as required */
7801cb0ef41Sopenharmony_ci        return tls_finish_handshake(s, wst, 1, 1);
7811cb0ef41Sopenharmony_ci    }
7821cb0ef41Sopenharmony_ci
7831cb0ef41Sopenharmony_ci    return WORK_FINISHED_CONTINUE;
7841cb0ef41Sopenharmony_ci}
7851cb0ef41Sopenharmony_ci
7861cb0ef41Sopenharmony_cistatic ossl_inline int conn_is_closed(void)
7871cb0ef41Sopenharmony_ci{
7881cb0ef41Sopenharmony_ci    switch (get_last_sys_error()) {
7891cb0ef41Sopenharmony_ci#if defined(EPIPE)
7901cb0ef41Sopenharmony_ci    case EPIPE:
7911cb0ef41Sopenharmony_ci        return 1;
7921cb0ef41Sopenharmony_ci#endif
7931cb0ef41Sopenharmony_ci#if defined(ECONNRESET)
7941cb0ef41Sopenharmony_ci    case ECONNRESET:
7951cb0ef41Sopenharmony_ci        return 1;
7961cb0ef41Sopenharmony_ci#endif
7971cb0ef41Sopenharmony_ci#if defined(WSAECONNRESET)
7981cb0ef41Sopenharmony_ci    case WSAECONNRESET:
7991cb0ef41Sopenharmony_ci        return 1;
8001cb0ef41Sopenharmony_ci#endif
8011cb0ef41Sopenharmony_ci    default:
8021cb0ef41Sopenharmony_ci        return 0;
8031cb0ef41Sopenharmony_ci    }
8041cb0ef41Sopenharmony_ci}
8051cb0ef41Sopenharmony_ci
8061cb0ef41Sopenharmony_ci/*
8071cb0ef41Sopenharmony_ci * Perform any work that needs to be done after sending a message from the
8081cb0ef41Sopenharmony_ci * server to the client.
8091cb0ef41Sopenharmony_ci */
8101cb0ef41Sopenharmony_ciWORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
8111cb0ef41Sopenharmony_ci{
8121cb0ef41Sopenharmony_ci    OSSL_STATEM *st = &s->statem;
8131cb0ef41Sopenharmony_ci
8141cb0ef41Sopenharmony_ci    s->init_num = 0;
8151cb0ef41Sopenharmony_ci
8161cb0ef41Sopenharmony_ci    switch (st->hand_state) {
8171cb0ef41Sopenharmony_ci    default:
8181cb0ef41Sopenharmony_ci        /* No post work to be done */
8191cb0ef41Sopenharmony_ci        break;
8201cb0ef41Sopenharmony_ci
8211cb0ef41Sopenharmony_ci    case TLS_ST_SW_HELLO_REQ:
8221cb0ef41Sopenharmony_ci        if (statem_flush(s) != 1)
8231cb0ef41Sopenharmony_ci            return WORK_MORE_A;
8241cb0ef41Sopenharmony_ci        if (!ssl3_init_finished_mac(s)) {
8251cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
8261cb0ef41Sopenharmony_ci            return WORK_ERROR;
8271cb0ef41Sopenharmony_ci        }
8281cb0ef41Sopenharmony_ci        break;
8291cb0ef41Sopenharmony_ci
8301cb0ef41Sopenharmony_ci    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
8311cb0ef41Sopenharmony_ci        if (statem_flush(s) != 1)
8321cb0ef41Sopenharmony_ci            return WORK_MORE_A;
8331cb0ef41Sopenharmony_ci        /* HelloVerifyRequest resets Finished MAC */
8341cb0ef41Sopenharmony_ci        if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
8351cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
8361cb0ef41Sopenharmony_ci            return WORK_ERROR;
8371cb0ef41Sopenharmony_ci        }
8381cb0ef41Sopenharmony_ci        /*
8391cb0ef41Sopenharmony_ci         * The next message should be another ClientHello which we need to
8401cb0ef41Sopenharmony_ci         * treat like it was the first packet
8411cb0ef41Sopenharmony_ci         */
8421cb0ef41Sopenharmony_ci        s->first_packet = 1;
8431cb0ef41Sopenharmony_ci        break;
8441cb0ef41Sopenharmony_ci
8451cb0ef41Sopenharmony_ci    case TLS_ST_SW_SRVR_HELLO:
8461cb0ef41Sopenharmony_ci        if (SSL_IS_TLS13(s) && s->hello_retry_request == SSL_HRR_PENDING) {
8471cb0ef41Sopenharmony_ci            if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
8481cb0ef41Sopenharmony_ci                    && statem_flush(s) != 1)
8491cb0ef41Sopenharmony_ci                return WORK_MORE_A;
8501cb0ef41Sopenharmony_ci            break;
8511cb0ef41Sopenharmony_ci        }
8521cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SCTP
8531cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s) && s->hit) {
8541cb0ef41Sopenharmony_ci            unsigned char sctpauthkey[64];
8551cb0ef41Sopenharmony_ci            char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
8561cb0ef41Sopenharmony_ci            size_t labellen;
8571cb0ef41Sopenharmony_ci
8581cb0ef41Sopenharmony_ci            /*
8591cb0ef41Sopenharmony_ci             * Add new shared key for SCTP-Auth, will be ignored if no
8601cb0ef41Sopenharmony_ci             * SCTP used.
8611cb0ef41Sopenharmony_ci             */
8621cb0ef41Sopenharmony_ci            memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
8631cb0ef41Sopenharmony_ci                   sizeof(DTLS1_SCTP_AUTH_LABEL));
8641cb0ef41Sopenharmony_ci
8651cb0ef41Sopenharmony_ci            /* Don't include the terminating zero. */
8661cb0ef41Sopenharmony_ci            labellen = sizeof(labelbuffer) - 1;
8671cb0ef41Sopenharmony_ci            if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
8681cb0ef41Sopenharmony_ci                labellen += 1;
8691cb0ef41Sopenharmony_ci
8701cb0ef41Sopenharmony_ci            if (SSL_export_keying_material(s, sctpauthkey,
8711cb0ef41Sopenharmony_ci                                           sizeof(sctpauthkey), labelbuffer,
8721cb0ef41Sopenharmony_ci                                           labellen, NULL, 0,
8731cb0ef41Sopenharmony_ci                                           0) <= 0) {
8741cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
8751cb0ef41Sopenharmony_ci                return WORK_ERROR;
8761cb0ef41Sopenharmony_ci            }
8771cb0ef41Sopenharmony_ci
8781cb0ef41Sopenharmony_ci            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
8791cb0ef41Sopenharmony_ci                     sizeof(sctpauthkey), sctpauthkey);
8801cb0ef41Sopenharmony_ci        }
8811cb0ef41Sopenharmony_ci#endif
8821cb0ef41Sopenharmony_ci        if (!SSL_IS_TLS13(s)
8831cb0ef41Sopenharmony_ci                || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
8841cb0ef41Sopenharmony_ci                    && s->hello_retry_request != SSL_HRR_COMPLETE))
8851cb0ef41Sopenharmony_ci            break;
8861cb0ef41Sopenharmony_ci        /* Fall through */
8871cb0ef41Sopenharmony_ci
8881cb0ef41Sopenharmony_ci    case TLS_ST_SW_CHANGE:
8891cb0ef41Sopenharmony_ci        if (s->hello_retry_request == SSL_HRR_PENDING) {
8901cb0ef41Sopenharmony_ci            if (!statem_flush(s))
8911cb0ef41Sopenharmony_ci                return WORK_MORE_A;
8921cb0ef41Sopenharmony_ci            break;
8931cb0ef41Sopenharmony_ci        }
8941cb0ef41Sopenharmony_ci
8951cb0ef41Sopenharmony_ci        if (SSL_IS_TLS13(s)) {
8961cb0ef41Sopenharmony_ci            if (!s->method->ssl3_enc->setup_key_block(s)
8971cb0ef41Sopenharmony_ci                || !s->method->ssl3_enc->change_cipher_state(s,
8981cb0ef41Sopenharmony_ci                        SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
8991cb0ef41Sopenharmony_ci                /* SSLfatal() already called */
9001cb0ef41Sopenharmony_ci                return WORK_ERROR;
9011cb0ef41Sopenharmony_ci            }
9021cb0ef41Sopenharmony_ci
9031cb0ef41Sopenharmony_ci            if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
9041cb0ef41Sopenharmony_ci                && !s->method->ssl3_enc->change_cipher_state(s,
9051cb0ef41Sopenharmony_ci                        SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) {
9061cb0ef41Sopenharmony_ci                /* SSLfatal() already called */
9071cb0ef41Sopenharmony_ci                return WORK_ERROR;
9081cb0ef41Sopenharmony_ci            }
9091cb0ef41Sopenharmony_ci            /*
9101cb0ef41Sopenharmony_ci             * We don't yet know whether the next record we are going to receive
9111cb0ef41Sopenharmony_ci             * is an unencrypted alert, an encrypted alert, or an encrypted
9121cb0ef41Sopenharmony_ci             * handshake message. We temporarily tolerate unencrypted alerts.
9131cb0ef41Sopenharmony_ci             */
9141cb0ef41Sopenharmony_ci            s->statem.enc_read_state = ENC_READ_STATE_ALLOW_PLAIN_ALERTS;
9151cb0ef41Sopenharmony_ci            break;
9161cb0ef41Sopenharmony_ci        }
9171cb0ef41Sopenharmony_ci
9181cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SCTP
9191cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s) && !s->hit) {
9201cb0ef41Sopenharmony_ci            /*
9211cb0ef41Sopenharmony_ci             * Change to new shared key of SCTP-Auth, will be ignored if
9221cb0ef41Sopenharmony_ci             * no SCTP used.
9231cb0ef41Sopenharmony_ci             */
9241cb0ef41Sopenharmony_ci            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
9251cb0ef41Sopenharmony_ci                     0, NULL);
9261cb0ef41Sopenharmony_ci        }
9271cb0ef41Sopenharmony_ci#endif
9281cb0ef41Sopenharmony_ci        if (!s->method->ssl3_enc->change_cipher_state(s,
9291cb0ef41Sopenharmony_ci                                                      SSL3_CHANGE_CIPHER_SERVER_WRITE))
9301cb0ef41Sopenharmony_ci        {
9311cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
9321cb0ef41Sopenharmony_ci            return WORK_ERROR;
9331cb0ef41Sopenharmony_ci        }
9341cb0ef41Sopenharmony_ci
9351cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s))
9361cb0ef41Sopenharmony_ci            dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
9371cb0ef41Sopenharmony_ci        break;
9381cb0ef41Sopenharmony_ci
9391cb0ef41Sopenharmony_ci    case TLS_ST_SW_SRVR_DONE:
9401cb0ef41Sopenharmony_ci        if (statem_flush(s) != 1)
9411cb0ef41Sopenharmony_ci            return WORK_MORE_A;
9421cb0ef41Sopenharmony_ci        break;
9431cb0ef41Sopenharmony_ci
9441cb0ef41Sopenharmony_ci    case TLS_ST_SW_FINISHED:
9451cb0ef41Sopenharmony_ci        if (statem_flush(s) != 1)
9461cb0ef41Sopenharmony_ci            return WORK_MORE_A;
9471cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SCTP
9481cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s) && s->hit) {
9491cb0ef41Sopenharmony_ci            /*
9501cb0ef41Sopenharmony_ci             * Change to new shared key of SCTP-Auth, will be ignored if
9511cb0ef41Sopenharmony_ci             * no SCTP used.
9521cb0ef41Sopenharmony_ci             */
9531cb0ef41Sopenharmony_ci            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
9541cb0ef41Sopenharmony_ci                     0, NULL);
9551cb0ef41Sopenharmony_ci        }
9561cb0ef41Sopenharmony_ci#endif
9571cb0ef41Sopenharmony_ci        if (SSL_IS_TLS13(s)) {
9581cb0ef41Sopenharmony_ci            /* TLS 1.3 gets the secret size from the handshake md */
9591cb0ef41Sopenharmony_ci            size_t dummy;
9601cb0ef41Sopenharmony_ci            if (!s->method->ssl3_enc->generate_master_secret(s,
9611cb0ef41Sopenharmony_ci                        s->master_secret, s->handshake_secret, 0,
9621cb0ef41Sopenharmony_ci                        &dummy)
9631cb0ef41Sopenharmony_ci                || !s->method->ssl3_enc->change_cipher_state(s,
9641cb0ef41Sopenharmony_ci                        SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
9651cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
9661cb0ef41Sopenharmony_ci            return WORK_ERROR;
9671cb0ef41Sopenharmony_ci
9681cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_QUIC
9691cb0ef41Sopenharmony_ci            if (SSL_IS_QUIC(s) && s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
9701cb0ef41Sopenharmony_ci                s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
9711cb0ef41Sopenharmony_ci                if (!s->method->ssl3_enc->change_cipher_state(
9721cb0ef41Sopenharmony_ci                        s, SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ))
9731cb0ef41Sopenharmony_ci                    /* SSLfatal() already called */
9741cb0ef41Sopenharmony_ci                    return WORK_ERROR;
9751cb0ef41Sopenharmony_ci            }
9761cb0ef41Sopenharmony_ci#endif
9771cb0ef41Sopenharmony_ci        }
9781cb0ef41Sopenharmony_ci        break;
9791cb0ef41Sopenharmony_ci
9801cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT_REQ:
9811cb0ef41Sopenharmony_ci        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
9821cb0ef41Sopenharmony_ci            if (statem_flush(s) != 1)
9831cb0ef41Sopenharmony_ci                return WORK_MORE_A;
9841cb0ef41Sopenharmony_ci        }
9851cb0ef41Sopenharmony_ci        break;
9861cb0ef41Sopenharmony_ci
9871cb0ef41Sopenharmony_ci    case TLS_ST_SW_KEY_UPDATE:
9881cb0ef41Sopenharmony_ci        if (statem_flush(s) != 1)
9891cb0ef41Sopenharmony_ci            return WORK_MORE_A;
9901cb0ef41Sopenharmony_ci        if (!tls13_update_key(s, 1)) {
9911cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
9921cb0ef41Sopenharmony_ci            return WORK_ERROR;
9931cb0ef41Sopenharmony_ci        }
9941cb0ef41Sopenharmony_ci        break;
9951cb0ef41Sopenharmony_ci
9961cb0ef41Sopenharmony_ci    case TLS_ST_SW_SESSION_TICKET:
9971cb0ef41Sopenharmony_ci        clear_sys_error();
9981cb0ef41Sopenharmony_ci        if (SSL_IS_TLS13(s) && statem_flush(s) != 1) {
9991cb0ef41Sopenharmony_ci            if (SSL_get_error(s, 0) == SSL_ERROR_SYSCALL
10001cb0ef41Sopenharmony_ci                    && conn_is_closed()) {
10011cb0ef41Sopenharmony_ci                /*
10021cb0ef41Sopenharmony_ci                 * We ignore connection closed errors in TLSv1.3 when sending a
10031cb0ef41Sopenharmony_ci                 * NewSessionTicket and behave as if we were successful. This is
10041cb0ef41Sopenharmony_ci                 * so that we are still able to read data sent to us by a client
10051cb0ef41Sopenharmony_ci                 * that closes soon after the end of the handshake without
10061cb0ef41Sopenharmony_ci                 * waiting to read our post-handshake NewSessionTickets.
10071cb0ef41Sopenharmony_ci                 */
10081cb0ef41Sopenharmony_ci                s->rwstate = SSL_NOTHING;
10091cb0ef41Sopenharmony_ci                break;
10101cb0ef41Sopenharmony_ci            }
10111cb0ef41Sopenharmony_ci
10121cb0ef41Sopenharmony_ci            return WORK_MORE_A;
10131cb0ef41Sopenharmony_ci        }
10141cb0ef41Sopenharmony_ci        break;
10151cb0ef41Sopenharmony_ci    }
10161cb0ef41Sopenharmony_ci
10171cb0ef41Sopenharmony_ci    return WORK_FINISHED_CONTINUE;
10181cb0ef41Sopenharmony_ci}
10191cb0ef41Sopenharmony_ci
10201cb0ef41Sopenharmony_ci/*
10211cb0ef41Sopenharmony_ci * Get the message construction function and message type for sending from the
10221cb0ef41Sopenharmony_ci * server
10231cb0ef41Sopenharmony_ci *
10241cb0ef41Sopenharmony_ci * Valid return values are:
10251cb0ef41Sopenharmony_ci *   1: Success
10261cb0ef41Sopenharmony_ci *   0: Error
10271cb0ef41Sopenharmony_ci */
10281cb0ef41Sopenharmony_ciint ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
10291cb0ef41Sopenharmony_ci                                         confunc_f *confunc, int *mt)
10301cb0ef41Sopenharmony_ci{
10311cb0ef41Sopenharmony_ci    OSSL_STATEM *st = &s->statem;
10321cb0ef41Sopenharmony_ci
10331cb0ef41Sopenharmony_ci    switch (st->hand_state) {
10341cb0ef41Sopenharmony_ci    default:
10351cb0ef41Sopenharmony_ci        /* Shouldn't happen */
10361cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
10371cb0ef41Sopenharmony_ci        return 0;
10381cb0ef41Sopenharmony_ci
10391cb0ef41Sopenharmony_ci    case TLS_ST_SW_CHANGE:
10401cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s))
10411cb0ef41Sopenharmony_ci            *confunc = dtls_construct_change_cipher_spec;
10421cb0ef41Sopenharmony_ci        else
10431cb0ef41Sopenharmony_ci            *confunc = tls_construct_change_cipher_spec;
10441cb0ef41Sopenharmony_ci        *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
10451cb0ef41Sopenharmony_ci        break;
10461cb0ef41Sopenharmony_ci
10471cb0ef41Sopenharmony_ci    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
10481cb0ef41Sopenharmony_ci        *confunc = dtls_construct_hello_verify_request;
10491cb0ef41Sopenharmony_ci        *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
10501cb0ef41Sopenharmony_ci        break;
10511cb0ef41Sopenharmony_ci
10521cb0ef41Sopenharmony_ci    case TLS_ST_SW_HELLO_REQ:
10531cb0ef41Sopenharmony_ci        /* No construction function needed */
10541cb0ef41Sopenharmony_ci        *confunc = NULL;
10551cb0ef41Sopenharmony_ci        *mt = SSL3_MT_HELLO_REQUEST;
10561cb0ef41Sopenharmony_ci        break;
10571cb0ef41Sopenharmony_ci
10581cb0ef41Sopenharmony_ci    case TLS_ST_SW_SRVR_HELLO:
10591cb0ef41Sopenharmony_ci        *confunc = tls_construct_server_hello;
10601cb0ef41Sopenharmony_ci        *mt = SSL3_MT_SERVER_HELLO;
10611cb0ef41Sopenharmony_ci        break;
10621cb0ef41Sopenharmony_ci
10631cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT:
10641cb0ef41Sopenharmony_ci        *confunc = tls_construct_server_certificate;
10651cb0ef41Sopenharmony_ci        *mt = SSL3_MT_CERTIFICATE;
10661cb0ef41Sopenharmony_ci        break;
10671cb0ef41Sopenharmony_ci
10681cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT_VRFY:
10691cb0ef41Sopenharmony_ci        *confunc = tls_construct_cert_verify;
10701cb0ef41Sopenharmony_ci        *mt = SSL3_MT_CERTIFICATE_VERIFY;
10711cb0ef41Sopenharmony_ci        break;
10721cb0ef41Sopenharmony_ci
10731cb0ef41Sopenharmony_ci
10741cb0ef41Sopenharmony_ci    case TLS_ST_SW_KEY_EXCH:
10751cb0ef41Sopenharmony_ci        *confunc = tls_construct_server_key_exchange;
10761cb0ef41Sopenharmony_ci        *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
10771cb0ef41Sopenharmony_ci        break;
10781cb0ef41Sopenharmony_ci
10791cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT_REQ:
10801cb0ef41Sopenharmony_ci        *confunc = tls_construct_certificate_request;
10811cb0ef41Sopenharmony_ci        *mt = SSL3_MT_CERTIFICATE_REQUEST;
10821cb0ef41Sopenharmony_ci        break;
10831cb0ef41Sopenharmony_ci
10841cb0ef41Sopenharmony_ci    case TLS_ST_SW_SRVR_DONE:
10851cb0ef41Sopenharmony_ci        *confunc = tls_construct_server_done;
10861cb0ef41Sopenharmony_ci        *mt = SSL3_MT_SERVER_DONE;
10871cb0ef41Sopenharmony_ci        break;
10881cb0ef41Sopenharmony_ci
10891cb0ef41Sopenharmony_ci    case TLS_ST_SW_SESSION_TICKET:
10901cb0ef41Sopenharmony_ci        *confunc = tls_construct_new_session_ticket;
10911cb0ef41Sopenharmony_ci        *mt = SSL3_MT_NEWSESSION_TICKET;
10921cb0ef41Sopenharmony_ci        break;
10931cb0ef41Sopenharmony_ci
10941cb0ef41Sopenharmony_ci    case TLS_ST_SW_CERT_STATUS:
10951cb0ef41Sopenharmony_ci        *confunc = tls_construct_cert_status;
10961cb0ef41Sopenharmony_ci        *mt = SSL3_MT_CERTIFICATE_STATUS;
10971cb0ef41Sopenharmony_ci        break;
10981cb0ef41Sopenharmony_ci
10991cb0ef41Sopenharmony_ci    case TLS_ST_SW_FINISHED:
11001cb0ef41Sopenharmony_ci        *confunc = tls_construct_finished;
11011cb0ef41Sopenharmony_ci        *mt = SSL3_MT_FINISHED;
11021cb0ef41Sopenharmony_ci        break;
11031cb0ef41Sopenharmony_ci
11041cb0ef41Sopenharmony_ci    case TLS_ST_EARLY_DATA:
11051cb0ef41Sopenharmony_ci        *confunc = NULL;
11061cb0ef41Sopenharmony_ci        *mt = SSL3_MT_DUMMY;
11071cb0ef41Sopenharmony_ci        break;
11081cb0ef41Sopenharmony_ci
11091cb0ef41Sopenharmony_ci    case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
11101cb0ef41Sopenharmony_ci        *confunc = tls_construct_encrypted_extensions;
11111cb0ef41Sopenharmony_ci        *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
11121cb0ef41Sopenharmony_ci        break;
11131cb0ef41Sopenharmony_ci
11141cb0ef41Sopenharmony_ci    case TLS_ST_SW_KEY_UPDATE:
11151cb0ef41Sopenharmony_ci        *confunc = tls_construct_key_update;
11161cb0ef41Sopenharmony_ci        *mt = SSL3_MT_KEY_UPDATE;
11171cb0ef41Sopenharmony_ci        break;
11181cb0ef41Sopenharmony_ci    }
11191cb0ef41Sopenharmony_ci
11201cb0ef41Sopenharmony_ci    return 1;
11211cb0ef41Sopenharmony_ci}
11221cb0ef41Sopenharmony_ci
11231cb0ef41Sopenharmony_ci/*
11241cb0ef41Sopenharmony_ci * Maximum size (excluding the Handshake header) of a ClientHello message,
11251cb0ef41Sopenharmony_ci * calculated as follows:
11261cb0ef41Sopenharmony_ci *
11271cb0ef41Sopenharmony_ci *  2 + # client_version
11281cb0ef41Sopenharmony_ci *  32 + # only valid length for random
11291cb0ef41Sopenharmony_ci *  1 + # length of session_id
11301cb0ef41Sopenharmony_ci *  32 + # maximum size for session_id
11311cb0ef41Sopenharmony_ci *  2 + # length of cipher suites
11321cb0ef41Sopenharmony_ci *  2^16-2 + # maximum length of cipher suites array
11331cb0ef41Sopenharmony_ci *  1 + # length of compression_methods
11341cb0ef41Sopenharmony_ci *  2^8-1 + # maximum length of compression methods
11351cb0ef41Sopenharmony_ci *  2 + # length of extensions
11361cb0ef41Sopenharmony_ci *  2^16-1 # maximum length of extensions
11371cb0ef41Sopenharmony_ci */
11381cb0ef41Sopenharmony_ci#define CLIENT_HELLO_MAX_LENGTH         131396
11391cb0ef41Sopenharmony_ci
11401cb0ef41Sopenharmony_ci#define CLIENT_KEY_EXCH_MAX_LENGTH      2048
11411cb0ef41Sopenharmony_ci#define NEXT_PROTO_MAX_LENGTH           514
11421cb0ef41Sopenharmony_ci
11431cb0ef41Sopenharmony_ci/*
11441cb0ef41Sopenharmony_ci * Returns the maximum allowed length for the current message that we are
11451cb0ef41Sopenharmony_ci * reading. Excludes the message header.
11461cb0ef41Sopenharmony_ci */
11471cb0ef41Sopenharmony_cisize_t ossl_statem_server_max_message_size(SSL *s)
11481cb0ef41Sopenharmony_ci{
11491cb0ef41Sopenharmony_ci    OSSL_STATEM *st = &s->statem;
11501cb0ef41Sopenharmony_ci
11511cb0ef41Sopenharmony_ci    switch (st->hand_state) {
11521cb0ef41Sopenharmony_ci    default:
11531cb0ef41Sopenharmony_ci        /* Shouldn't happen */
11541cb0ef41Sopenharmony_ci        return 0;
11551cb0ef41Sopenharmony_ci
11561cb0ef41Sopenharmony_ci    case TLS_ST_SR_CLNT_HELLO:
11571cb0ef41Sopenharmony_ci        return CLIENT_HELLO_MAX_LENGTH;
11581cb0ef41Sopenharmony_ci
11591cb0ef41Sopenharmony_ci    case TLS_ST_SR_END_OF_EARLY_DATA:
11601cb0ef41Sopenharmony_ci        return END_OF_EARLY_DATA_MAX_LENGTH;
11611cb0ef41Sopenharmony_ci
11621cb0ef41Sopenharmony_ci    case TLS_ST_SR_CERT:
11631cb0ef41Sopenharmony_ci        return s->max_cert_list;
11641cb0ef41Sopenharmony_ci
11651cb0ef41Sopenharmony_ci    case TLS_ST_SR_KEY_EXCH:
11661cb0ef41Sopenharmony_ci        return CLIENT_KEY_EXCH_MAX_LENGTH;
11671cb0ef41Sopenharmony_ci
11681cb0ef41Sopenharmony_ci    case TLS_ST_SR_CERT_VRFY:
11691cb0ef41Sopenharmony_ci        return SSL3_RT_MAX_PLAIN_LENGTH;
11701cb0ef41Sopenharmony_ci
11711cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_NEXTPROTONEG
11721cb0ef41Sopenharmony_ci    case TLS_ST_SR_NEXT_PROTO:
11731cb0ef41Sopenharmony_ci        return NEXT_PROTO_MAX_LENGTH;
11741cb0ef41Sopenharmony_ci#endif
11751cb0ef41Sopenharmony_ci
11761cb0ef41Sopenharmony_ci    case TLS_ST_SR_CHANGE:
11771cb0ef41Sopenharmony_ci        return CCS_MAX_LENGTH;
11781cb0ef41Sopenharmony_ci
11791cb0ef41Sopenharmony_ci    case TLS_ST_SR_FINISHED:
11801cb0ef41Sopenharmony_ci        return FINISHED_MAX_LENGTH;
11811cb0ef41Sopenharmony_ci
11821cb0ef41Sopenharmony_ci    case TLS_ST_SR_KEY_UPDATE:
11831cb0ef41Sopenharmony_ci        return KEY_UPDATE_MAX_LENGTH;
11841cb0ef41Sopenharmony_ci    }
11851cb0ef41Sopenharmony_ci}
11861cb0ef41Sopenharmony_ci
11871cb0ef41Sopenharmony_ci/*
11881cb0ef41Sopenharmony_ci * Process a message that the server has received from the client.
11891cb0ef41Sopenharmony_ci */
11901cb0ef41Sopenharmony_ciMSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
11911cb0ef41Sopenharmony_ci{
11921cb0ef41Sopenharmony_ci    OSSL_STATEM *st = &s->statem;
11931cb0ef41Sopenharmony_ci
11941cb0ef41Sopenharmony_ci    switch (st->hand_state) {
11951cb0ef41Sopenharmony_ci    default:
11961cb0ef41Sopenharmony_ci        /* Shouldn't happen */
11971cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
11981cb0ef41Sopenharmony_ci        return MSG_PROCESS_ERROR;
11991cb0ef41Sopenharmony_ci
12001cb0ef41Sopenharmony_ci    case TLS_ST_SR_CLNT_HELLO:
12011cb0ef41Sopenharmony_ci        return tls_process_client_hello(s, pkt);
12021cb0ef41Sopenharmony_ci
12031cb0ef41Sopenharmony_ci    case TLS_ST_SR_END_OF_EARLY_DATA:
12041cb0ef41Sopenharmony_ci        return tls_process_end_of_early_data(s, pkt);
12051cb0ef41Sopenharmony_ci
12061cb0ef41Sopenharmony_ci    case TLS_ST_SR_CERT:
12071cb0ef41Sopenharmony_ci        return tls_process_client_certificate(s, pkt);
12081cb0ef41Sopenharmony_ci
12091cb0ef41Sopenharmony_ci    case TLS_ST_SR_KEY_EXCH:
12101cb0ef41Sopenharmony_ci        return tls_process_client_key_exchange(s, pkt);
12111cb0ef41Sopenharmony_ci
12121cb0ef41Sopenharmony_ci    case TLS_ST_SR_CERT_VRFY:
12131cb0ef41Sopenharmony_ci        return tls_process_cert_verify(s, pkt);
12141cb0ef41Sopenharmony_ci
12151cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_NEXTPROTONEG
12161cb0ef41Sopenharmony_ci    case TLS_ST_SR_NEXT_PROTO:
12171cb0ef41Sopenharmony_ci        return tls_process_next_proto(s, pkt);
12181cb0ef41Sopenharmony_ci#endif
12191cb0ef41Sopenharmony_ci
12201cb0ef41Sopenharmony_ci    case TLS_ST_SR_CHANGE:
12211cb0ef41Sopenharmony_ci        return tls_process_change_cipher_spec(s, pkt);
12221cb0ef41Sopenharmony_ci
12231cb0ef41Sopenharmony_ci    case TLS_ST_SR_FINISHED:
12241cb0ef41Sopenharmony_ci        return tls_process_finished(s, pkt);
12251cb0ef41Sopenharmony_ci
12261cb0ef41Sopenharmony_ci    case TLS_ST_SR_KEY_UPDATE:
12271cb0ef41Sopenharmony_ci        return tls_process_key_update(s, pkt);
12281cb0ef41Sopenharmony_ci
12291cb0ef41Sopenharmony_ci    }
12301cb0ef41Sopenharmony_ci}
12311cb0ef41Sopenharmony_ci
12321cb0ef41Sopenharmony_ci/*
12331cb0ef41Sopenharmony_ci * Perform any further processing required following the receipt of a message
12341cb0ef41Sopenharmony_ci * from the client
12351cb0ef41Sopenharmony_ci */
12361cb0ef41Sopenharmony_ciWORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
12371cb0ef41Sopenharmony_ci{
12381cb0ef41Sopenharmony_ci    OSSL_STATEM *st = &s->statem;
12391cb0ef41Sopenharmony_ci
12401cb0ef41Sopenharmony_ci    switch (st->hand_state) {
12411cb0ef41Sopenharmony_ci    default:
12421cb0ef41Sopenharmony_ci        /* Shouldn't happen */
12431cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
12441cb0ef41Sopenharmony_ci        return WORK_ERROR;
12451cb0ef41Sopenharmony_ci
12461cb0ef41Sopenharmony_ci    case TLS_ST_SR_CLNT_HELLO:
12471cb0ef41Sopenharmony_ci        return tls_post_process_client_hello(s, wst);
12481cb0ef41Sopenharmony_ci
12491cb0ef41Sopenharmony_ci    case TLS_ST_SR_KEY_EXCH:
12501cb0ef41Sopenharmony_ci        return tls_post_process_client_key_exchange(s, wst);
12511cb0ef41Sopenharmony_ci    }
12521cb0ef41Sopenharmony_ci}
12531cb0ef41Sopenharmony_ci
12541cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SRP
12551cb0ef41Sopenharmony_ci/* Returns 1 on success, 0 for retryable error, -1 for fatal error */
12561cb0ef41Sopenharmony_cistatic int ssl_check_srp_ext_ClientHello(SSL *s)
12571cb0ef41Sopenharmony_ci{
12581cb0ef41Sopenharmony_ci    int ret;
12591cb0ef41Sopenharmony_ci    int al = SSL_AD_UNRECOGNIZED_NAME;
12601cb0ef41Sopenharmony_ci
12611cb0ef41Sopenharmony_ci    if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
12621cb0ef41Sopenharmony_ci        (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
12631cb0ef41Sopenharmony_ci        if (s->srp_ctx.login == NULL) {
12641cb0ef41Sopenharmony_ci            /*
12651cb0ef41Sopenharmony_ci             * RFC 5054 says SHOULD reject, we do so if There is no srp
12661cb0ef41Sopenharmony_ci             * login name
12671cb0ef41Sopenharmony_ci             */
12681cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
12691cb0ef41Sopenharmony_ci                     SSL_R_PSK_IDENTITY_NOT_FOUND);
12701cb0ef41Sopenharmony_ci            return -1;
12711cb0ef41Sopenharmony_ci        } else {
12721cb0ef41Sopenharmony_ci            ret = ssl_srp_server_param_with_username_intern(s, &al);
12731cb0ef41Sopenharmony_ci            if (ret < 0)
12741cb0ef41Sopenharmony_ci                return 0;
12751cb0ef41Sopenharmony_ci            if (ret == SSL3_AL_FATAL) {
12761cb0ef41Sopenharmony_ci                SSLfatal(s, al,
12771cb0ef41Sopenharmony_ci                         al == SSL_AD_UNKNOWN_PSK_IDENTITY
12781cb0ef41Sopenharmony_ci                         ? SSL_R_PSK_IDENTITY_NOT_FOUND
12791cb0ef41Sopenharmony_ci                         : SSL_R_CLIENTHELLO_TLSEXT);
12801cb0ef41Sopenharmony_ci                return -1;
12811cb0ef41Sopenharmony_ci            }
12821cb0ef41Sopenharmony_ci        }
12831cb0ef41Sopenharmony_ci    }
12841cb0ef41Sopenharmony_ci    return 1;
12851cb0ef41Sopenharmony_ci}
12861cb0ef41Sopenharmony_ci#endif
12871cb0ef41Sopenharmony_ci
12881cb0ef41Sopenharmony_ciint dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
12891cb0ef41Sopenharmony_ci                                  size_t cookie_len)
12901cb0ef41Sopenharmony_ci{
12911cb0ef41Sopenharmony_ci    /* Always use DTLS 1.0 version: see RFC 6347 */
12921cb0ef41Sopenharmony_ci    if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
12931cb0ef41Sopenharmony_ci            || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
12941cb0ef41Sopenharmony_ci        return 0;
12951cb0ef41Sopenharmony_ci
12961cb0ef41Sopenharmony_ci    return 1;
12971cb0ef41Sopenharmony_ci}
12981cb0ef41Sopenharmony_ci
12991cb0ef41Sopenharmony_ciint dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
13001cb0ef41Sopenharmony_ci{
13011cb0ef41Sopenharmony_ci    unsigned int cookie_leni;
13021cb0ef41Sopenharmony_ci    if (s->ctx->app_gen_cookie_cb == NULL ||
13031cb0ef41Sopenharmony_ci        s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
13041cb0ef41Sopenharmony_ci                                  &cookie_leni) == 0 ||
13051cb0ef41Sopenharmony_ci        cookie_leni > DTLS1_COOKIE_LENGTH) {
13061cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
13071cb0ef41Sopenharmony_ci        return 0;
13081cb0ef41Sopenharmony_ci    }
13091cb0ef41Sopenharmony_ci    s->d1->cookie_len = cookie_leni;
13101cb0ef41Sopenharmony_ci
13111cb0ef41Sopenharmony_ci    if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
13121cb0ef41Sopenharmony_ci                                              s->d1->cookie_len)) {
13131cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
13141cb0ef41Sopenharmony_ci        return 0;
13151cb0ef41Sopenharmony_ci    }
13161cb0ef41Sopenharmony_ci
13171cb0ef41Sopenharmony_ci    return 1;
13181cb0ef41Sopenharmony_ci}
13191cb0ef41Sopenharmony_ci
13201cb0ef41Sopenharmony_ci/*-
13211cb0ef41Sopenharmony_ci * ssl_check_for_safari attempts to fingerprint Safari using OS X
13221cb0ef41Sopenharmony_ci * SecureTransport using the TLS extension block in |hello|.
13231cb0ef41Sopenharmony_ci * Safari, since 10.6, sends exactly these extensions, in this order:
13241cb0ef41Sopenharmony_ci *   SNI,
13251cb0ef41Sopenharmony_ci *   elliptic_curves
13261cb0ef41Sopenharmony_ci *   ec_point_formats
13271cb0ef41Sopenharmony_ci *   signature_algorithms (for TLSv1.2 only)
13281cb0ef41Sopenharmony_ci *
13291cb0ef41Sopenharmony_ci * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
13301cb0ef41Sopenharmony_ci * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
13311cb0ef41Sopenharmony_ci * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
13321cb0ef41Sopenharmony_ci * 10.8..10.8.3 (which don't work).
13331cb0ef41Sopenharmony_ci */
13341cb0ef41Sopenharmony_cistatic void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
13351cb0ef41Sopenharmony_ci{
13361cb0ef41Sopenharmony_ci    static const unsigned char kSafariExtensionsBlock[] = {
13371cb0ef41Sopenharmony_ci        0x00, 0x0a,             /* elliptic_curves extension */
13381cb0ef41Sopenharmony_ci        0x00, 0x08,             /* 8 bytes */
13391cb0ef41Sopenharmony_ci        0x00, 0x06,             /* 6 bytes of curve ids */
13401cb0ef41Sopenharmony_ci        0x00, 0x17,             /* P-256 */
13411cb0ef41Sopenharmony_ci        0x00, 0x18,             /* P-384 */
13421cb0ef41Sopenharmony_ci        0x00, 0x19,             /* P-521 */
13431cb0ef41Sopenharmony_ci
13441cb0ef41Sopenharmony_ci        0x00, 0x0b,             /* ec_point_formats */
13451cb0ef41Sopenharmony_ci        0x00, 0x02,             /* 2 bytes */
13461cb0ef41Sopenharmony_ci        0x01,                   /* 1 point format */
13471cb0ef41Sopenharmony_ci        0x00,                   /* uncompressed */
13481cb0ef41Sopenharmony_ci        /* The following is only present in TLS 1.2 */
13491cb0ef41Sopenharmony_ci        0x00, 0x0d,             /* signature_algorithms */
13501cb0ef41Sopenharmony_ci        0x00, 0x0c,             /* 12 bytes */
13511cb0ef41Sopenharmony_ci        0x00, 0x0a,             /* 10 bytes */
13521cb0ef41Sopenharmony_ci        0x05, 0x01,             /* SHA-384/RSA */
13531cb0ef41Sopenharmony_ci        0x04, 0x01,             /* SHA-256/RSA */
13541cb0ef41Sopenharmony_ci        0x02, 0x01,             /* SHA-1/RSA */
13551cb0ef41Sopenharmony_ci        0x04, 0x03,             /* SHA-256/ECDSA */
13561cb0ef41Sopenharmony_ci        0x02, 0x03,             /* SHA-1/ECDSA */
13571cb0ef41Sopenharmony_ci    };
13581cb0ef41Sopenharmony_ci    /* Length of the common prefix (first two extensions). */
13591cb0ef41Sopenharmony_ci    static const size_t kSafariCommonExtensionsLength = 18;
13601cb0ef41Sopenharmony_ci    unsigned int type;
13611cb0ef41Sopenharmony_ci    PACKET sni, tmppkt;
13621cb0ef41Sopenharmony_ci    size_t ext_len;
13631cb0ef41Sopenharmony_ci
13641cb0ef41Sopenharmony_ci    tmppkt = hello->extensions;
13651cb0ef41Sopenharmony_ci
13661cb0ef41Sopenharmony_ci    if (!PACKET_forward(&tmppkt, 2)
13671cb0ef41Sopenharmony_ci        || !PACKET_get_net_2(&tmppkt, &type)
13681cb0ef41Sopenharmony_ci        || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
13691cb0ef41Sopenharmony_ci        return;
13701cb0ef41Sopenharmony_ci    }
13711cb0ef41Sopenharmony_ci
13721cb0ef41Sopenharmony_ci    if (type != TLSEXT_TYPE_server_name)
13731cb0ef41Sopenharmony_ci        return;
13741cb0ef41Sopenharmony_ci
13751cb0ef41Sopenharmony_ci    ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
13761cb0ef41Sopenharmony_ci        sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
13771cb0ef41Sopenharmony_ci
13781cb0ef41Sopenharmony_ci    s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
13791cb0ef41Sopenharmony_ci                                             ext_len);
13801cb0ef41Sopenharmony_ci}
13811cb0ef41Sopenharmony_ci
13821cb0ef41Sopenharmony_ci#define RENEG_OPTIONS_OK(options) \
13831cb0ef41Sopenharmony_ci    ((options & SSL_OP_NO_RENEGOTIATION) == 0 \
13841cb0ef41Sopenharmony_ci     && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0)
13851cb0ef41Sopenharmony_ci
13861cb0ef41Sopenharmony_ciMSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
13871cb0ef41Sopenharmony_ci{
13881cb0ef41Sopenharmony_ci    /* |cookie| will only be initialized for DTLS. */
13891cb0ef41Sopenharmony_ci    PACKET session_id, compression, extensions, cookie;
13901cb0ef41Sopenharmony_ci    static const unsigned char null_compression = 0;
13911cb0ef41Sopenharmony_ci    CLIENTHELLO_MSG *clienthello = NULL;
13921cb0ef41Sopenharmony_ci
13931cb0ef41Sopenharmony_ci    /* Check if this is actually an unexpected renegotiation ClientHello */
13941cb0ef41Sopenharmony_ci    if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
13951cb0ef41Sopenharmony_ci        if (!ossl_assert(!SSL_IS_TLS13(s))) {
13961cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
13971cb0ef41Sopenharmony_ci            goto err;
13981cb0ef41Sopenharmony_ci        }
13991cb0ef41Sopenharmony_ci        if (!RENEG_OPTIONS_OK(s->options)
14001cb0ef41Sopenharmony_ci                || (!s->s3.send_connection_binding
14011cb0ef41Sopenharmony_ci                    && (s->options
14021cb0ef41Sopenharmony_ci                        & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) {
14031cb0ef41Sopenharmony_ci            ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
14041cb0ef41Sopenharmony_ci            return MSG_PROCESS_FINISHED_READING;
14051cb0ef41Sopenharmony_ci        }
14061cb0ef41Sopenharmony_ci        s->renegotiate = 1;
14071cb0ef41Sopenharmony_ci        s->new_session = 1;
14081cb0ef41Sopenharmony_ci    }
14091cb0ef41Sopenharmony_ci
14101cb0ef41Sopenharmony_ci    clienthello = OPENSSL_zalloc(sizeof(*clienthello));
14111cb0ef41Sopenharmony_ci    if (clienthello == NULL) {
14121cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
14131cb0ef41Sopenharmony_ci        goto err;
14141cb0ef41Sopenharmony_ci    }
14151cb0ef41Sopenharmony_ci
14161cb0ef41Sopenharmony_ci    /*
14171cb0ef41Sopenharmony_ci     * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
14181cb0ef41Sopenharmony_ci     */
14191cb0ef41Sopenharmony_ci    clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
14201cb0ef41Sopenharmony_ci    PACKET_null_init(&cookie);
14211cb0ef41Sopenharmony_ci
14221cb0ef41Sopenharmony_ci    if (clienthello->isv2) {
14231cb0ef41Sopenharmony_ci        unsigned int mt;
14241cb0ef41Sopenharmony_ci
14251cb0ef41Sopenharmony_ci        if (!SSL_IS_FIRST_HANDSHAKE(s)
14261cb0ef41Sopenharmony_ci                || s->hello_retry_request != SSL_HRR_NONE) {
14271cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
14281cb0ef41Sopenharmony_ci            goto err;
14291cb0ef41Sopenharmony_ci        }
14301cb0ef41Sopenharmony_ci
14311cb0ef41Sopenharmony_ci        /*-
14321cb0ef41Sopenharmony_ci         * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
14331cb0ef41Sopenharmony_ci         * header is sent directly on the wire, not wrapped as a TLS
14341cb0ef41Sopenharmony_ci         * record. Our record layer just processes the message length and passes
14351cb0ef41Sopenharmony_ci         * the rest right through. Its format is:
14361cb0ef41Sopenharmony_ci         * Byte  Content
14371cb0ef41Sopenharmony_ci         * 0-1   msg_length - decoded by the record layer
14381cb0ef41Sopenharmony_ci         * 2     msg_type - s->init_msg points here
14391cb0ef41Sopenharmony_ci         * 3-4   version
14401cb0ef41Sopenharmony_ci         * 5-6   cipher_spec_length
14411cb0ef41Sopenharmony_ci         * 7-8   session_id_length
14421cb0ef41Sopenharmony_ci         * 9-10  challenge_length
14431cb0ef41Sopenharmony_ci         * ...   ...
14441cb0ef41Sopenharmony_ci         */
14451cb0ef41Sopenharmony_ci
14461cb0ef41Sopenharmony_ci        if (!PACKET_get_1(pkt, &mt)
14471cb0ef41Sopenharmony_ci            || mt != SSL2_MT_CLIENT_HELLO) {
14481cb0ef41Sopenharmony_ci            /*
14491cb0ef41Sopenharmony_ci             * Should never happen. We should have tested this in the record
14501cb0ef41Sopenharmony_ci             * layer in order to have determined that this is a SSLv2 record
14511cb0ef41Sopenharmony_ci             * in the first place
14521cb0ef41Sopenharmony_ci             */
14531cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
14541cb0ef41Sopenharmony_ci            goto err;
14551cb0ef41Sopenharmony_ci        }
14561cb0ef41Sopenharmony_ci    }
14571cb0ef41Sopenharmony_ci
14581cb0ef41Sopenharmony_ci    if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
14591cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
14601cb0ef41Sopenharmony_ci        goto err;
14611cb0ef41Sopenharmony_ci    }
14621cb0ef41Sopenharmony_ci
14631cb0ef41Sopenharmony_ci    /* Parse the message and load client random. */
14641cb0ef41Sopenharmony_ci    if (clienthello->isv2) {
14651cb0ef41Sopenharmony_ci        /*
14661cb0ef41Sopenharmony_ci         * Handle an SSLv2 backwards compatible ClientHello
14671cb0ef41Sopenharmony_ci         * Note, this is only for SSLv3+ using the backward compatible format.
14681cb0ef41Sopenharmony_ci         * Real SSLv2 is not supported, and is rejected below.
14691cb0ef41Sopenharmony_ci         */
14701cb0ef41Sopenharmony_ci        unsigned int ciphersuite_len, session_id_len, challenge_len;
14711cb0ef41Sopenharmony_ci        PACKET challenge;
14721cb0ef41Sopenharmony_ci
14731cb0ef41Sopenharmony_ci        if (!PACKET_get_net_2(pkt, &ciphersuite_len)
14741cb0ef41Sopenharmony_ci            || !PACKET_get_net_2(pkt, &session_id_len)
14751cb0ef41Sopenharmony_ci            || !PACKET_get_net_2(pkt, &challenge_len)) {
14761cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);
14771cb0ef41Sopenharmony_ci            goto err;
14781cb0ef41Sopenharmony_ci        }
14791cb0ef41Sopenharmony_ci
14801cb0ef41Sopenharmony_ci        if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
14811cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_MISMATCH);
14821cb0ef41Sopenharmony_ci            goto err;
14831cb0ef41Sopenharmony_ci        }
14841cb0ef41Sopenharmony_ci
14851cb0ef41Sopenharmony_ci        if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
14861cb0ef41Sopenharmony_ci                                   ciphersuite_len)
14871cb0ef41Sopenharmony_ci            || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
14881cb0ef41Sopenharmony_ci            || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
14891cb0ef41Sopenharmony_ci            /* No extensions. */
14901cb0ef41Sopenharmony_ci            || PACKET_remaining(pkt) != 0) {
14911cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);
14921cb0ef41Sopenharmony_ci            goto err;
14931cb0ef41Sopenharmony_ci        }
14941cb0ef41Sopenharmony_ci        clienthello->session_id_len = session_id_len;
14951cb0ef41Sopenharmony_ci
14961cb0ef41Sopenharmony_ci        /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
14971cb0ef41Sopenharmony_ci         * here rather than sizeof(clienthello->random) because that is the limit
14981cb0ef41Sopenharmony_ci         * for SSLv3 and it is fixed. It won't change even if
14991cb0ef41Sopenharmony_ci         * sizeof(clienthello->random) does.
15001cb0ef41Sopenharmony_ci         */
15011cb0ef41Sopenharmony_ci        challenge_len = challenge_len > SSL3_RANDOM_SIZE
15021cb0ef41Sopenharmony_ci                        ? SSL3_RANDOM_SIZE : challenge_len;
15031cb0ef41Sopenharmony_ci        memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
15041cb0ef41Sopenharmony_ci        if (!PACKET_copy_bytes(&challenge,
15051cb0ef41Sopenharmony_ci                               clienthello->random + SSL3_RANDOM_SIZE -
15061cb0ef41Sopenharmony_ci                               challenge_len, challenge_len)
15071cb0ef41Sopenharmony_ci            /* Advertise only null compression. */
15081cb0ef41Sopenharmony_ci            || !PACKET_buf_init(&compression, &null_compression, 1)) {
15091cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
15101cb0ef41Sopenharmony_ci            goto err;
15111cb0ef41Sopenharmony_ci        }
15121cb0ef41Sopenharmony_ci
15131cb0ef41Sopenharmony_ci        PACKET_null_init(&clienthello->extensions);
15141cb0ef41Sopenharmony_ci    } else {
15151cb0ef41Sopenharmony_ci        /* Regular ClientHello. */
15161cb0ef41Sopenharmony_ci        if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
15171cb0ef41Sopenharmony_ci            || !PACKET_get_length_prefixed_1(pkt, &session_id)
15181cb0ef41Sopenharmony_ci            || !PACKET_copy_all(&session_id, clienthello->session_id,
15191cb0ef41Sopenharmony_ci                    SSL_MAX_SSL_SESSION_ID_LENGTH,
15201cb0ef41Sopenharmony_ci                    &clienthello->session_id_len)) {
15211cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
15221cb0ef41Sopenharmony_ci            goto err;
15231cb0ef41Sopenharmony_ci        }
15241cb0ef41Sopenharmony_ci
15251cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s)) {
15261cb0ef41Sopenharmony_ci            if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
15271cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
15281cb0ef41Sopenharmony_ci                goto err;
15291cb0ef41Sopenharmony_ci            }
15301cb0ef41Sopenharmony_ci            if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
15311cb0ef41Sopenharmony_ci                                 DTLS1_COOKIE_LENGTH,
15321cb0ef41Sopenharmony_ci                                 &clienthello->dtls_cookie_len)) {
15331cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
15341cb0ef41Sopenharmony_ci                goto err;
15351cb0ef41Sopenharmony_ci            }
15361cb0ef41Sopenharmony_ci            /*
15371cb0ef41Sopenharmony_ci             * If we require cookies and this ClientHello doesn't contain one,
15381cb0ef41Sopenharmony_ci             * just return since we do not want to allocate any memory yet.
15391cb0ef41Sopenharmony_ci             * So check cookie length...
15401cb0ef41Sopenharmony_ci             */
15411cb0ef41Sopenharmony_ci            if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
15421cb0ef41Sopenharmony_ci                if (clienthello->dtls_cookie_len == 0) {
15431cb0ef41Sopenharmony_ci                    OPENSSL_free(clienthello);
15441cb0ef41Sopenharmony_ci                    return MSG_PROCESS_FINISHED_READING;
15451cb0ef41Sopenharmony_ci                }
15461cb0ef41Sopenharmony_ci            }
15471cb0ef41Sopenharmony_ci        }
15481cb0ef41Sopenharmony_ci
15491cb0ef41Sopenharmony_ci        if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
15501cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
15511cb0ef41Sopenharmony_ci            goto err;
15521cb0ef41Sopenharmony_ci        }
15531cb0ef41Sopenharmony_ci
15541cb0ef41Sopenharmony_ci        if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
15551cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
15561cb0ef41Sopenharmony_ci            goto err;
15571cb0ef41Sopenharmony_ci        }
15581cb0ef41Sopenharmony_ci
15591cb0ef41Sopenharmony_ci        /* Could be empty. */
15601cb0ef41Sopenharmony_ci        if (PACKET_remaining(pkt) == 0) {
15611cb0ef41Sopenharmony_ci            PACKET_null_init(&clienthello->extensions);
15621cb0ef41Sopenharmony_ci        } else {
15631cb0ef41Sopenharmony_ci            if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
15641cb0ef41Sopenharmony_ci                    || PACKET_remaining(pkt) != 0) {
15651cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
15661cb0ef41Sopenharmony_ci                goto err;
15671cb0ef41Sopenharmony_ci            }
15681cb0ef41Sopenharmony_ci        }
15691cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_QUIC
15701cb0ef41Sopenharmony_ci        if (SSL_IS_QUIC(s)) {
15711cb0ef41Sopenharmony_ci            /* Any other QUIC checks on ClientHello here */
15721cb0ef41Sopenharmony_ci            if (clienthello->session_id_len > 0) {
15731cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_MISMATCH);
15741cb0ef41Sopenharmony_ci                goto err;
15751cb0ef41Sopenharmony_ci            }
15761cb0ef41Sopenharmony_ci        }
15771cb0ef41Sopenharmony_ci#endif
15781cb0ef41Sopenharmony_ci    }
15791cb0ef41Sopenharmony_ci
15801cb0ef41Sopenharmony_ci    if (!PACKET_copy_all(&compression, clienthello->compressions,
15811cb0ef41Sopenharmony_ci                         MAX_COMPRESSIONS_SIZE,
15821cb0ef41Sopenharmony_ci                         &clienthello->compressions_len)) {
15831cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
15841cb0ef41Sopenharmony_ci        goto err;
15851cb0ef41Sopenharmony_ci    }
15861cb0ef41Sopenharmony_ci
15871cb0ef41Sopenharmony_ci    /* Preserve the raw extensions PACKET for later use */
15881cb0ef41Sopenharmony_ci    extensions = clienthello->extensions;
15891cb0ef41Sopenharmony_ci    if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
15901cb0ef41Sopenharmony_ci                                &clienthello->pre_proc_exts,
15911cb0ef41Sopenharmony_ci                                &clienthello->pre_proc_exts_len, 1)) {
15921cb0ef41Sopenharmony_ci        /* SSLfatal already been called */
15931cb0ef41Sopenharmony_ci        goto err;
15941cb0ef41Sopenharmony_ci    }
15951cb0ef41Sopenharmony_ci    s->clienthello = clienthello;
15961cb0ef41Sopenharmony_ci
15971cb0ef41Sopenharmony_ci    return MSG_PROCESS_CONTINUE_PROCESSING;
15981cb0ef41Sopenharmony_ci
15991cb0ef41Sopenharmony_ci err:
16001cb0ef41Sopenharmony_ci    if (clienthello != NULL)
16011cb0ef41Sopenharmony_ci        OPENSSL_free(clienthello->pre_proc_exts);
16021cb0ef41Sopenharmony_ci    OPENSSL_free(clienthello);
16031cb0ef41Sopenharmony_ci
16041cb0ef41Sopenharmony_ci    return MSG_PROCESS_ERROR;
16051cb0ef41Sopenharmony_ci}
16061cb0ef41Sopenharmony_ci
16071cb0ef41Sopenharmony_cistatic int tls_early_post_process_client_hello(SSL *s)
16081cb0ef41Sopenharmony_ci{
16091cb0ef41Sopenharmony_ci    unsigned int j;
16101cb0ef41Sopenharmony_ci    int i, al = SSL_AD_INTERNAL_ERROR;
16111cb0ef41Sopenharmony_ci    int protverr;
16121cb0ef41Sopenharmony_ci    size_t loop;
16131cb0ef41Sopenharmony_ci    unsigned long id;
16141cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_COMP
16151cb0ef41Sopenharmony_ci    SSL_COMP *comp = NULL;
16161cb0ef41Sopenharmony_ci#endif
16171cb0ef41Sopenharmony_ci    const SSL_CIPHER *c;
16181cb0ef41Sopenharmony_ci    STACK_OF(SSL_CIPHER) *ciphers = NULL;
16191cb0ef41Sopenharmony_ci    STACK_OF(SSL_CIPHER) *scsvs = NULL;
16201cb0ef41Sopenharmony_ci    CLIENTHELLO_MSG *clienthello = s->clienthello;
16211cb0ef41Sopenharmony_ci    DOWNGRADE dgrd = DOWNGRADE_NONE;
16221cb0ef41Sopenharmony_ci
16231cb0ef41Sopenharmony_ci    /* Finished parsing the ClientHello, now we can start processing it */
16241cb0ef41Sopenharmony_ci    /* Give the ClientHello callback a crack at things */
16251cb0ef41Sopenharmony_ci    if (s->ctx->client_hello_cb != NULL) {
16261cb0ef41Sopenharmony_ci        /* A failure in the ClientHello callback terminates the connection. */
16271cb0ef41Sopenharmony_ci        switch (s->ctx->client_hello_cb(s, &al, s->ctx->client_hello_cb_arg)) {
16281cb0ef41Sopenharmony_ci        case SSL_CLIENT_HELLO_SUCCESS:
16291cb0ef41Sopenharmony_ci            break;
16301cb0ef41Sopenharmony_ci        case SSL_CLIENT_HELLO_RETRY:
16311cb0ef41Sopenharmony_ci            s->rwstate = SSL_CLIENT_HELLO_CB;
16321cb0ef41Sopenharmony_ci            return -1;
16331cb0ef41Sopenharmony_ci        case SSL_CLIENT_HELLO_ERROR:
16341cb0ef41Sopenharmony_ci        default:
16351cb0ef41Sopenharmony_ci            SSLfatal(s, al, SSL_R_CALLBACK_FAILED);
16361cb0ef41Sopenharmony_ci            goto err;
16371cb0ef41Sopenharmony_ci        }
16381cb0ef41Sopenharmony_ci    }
16391cb0ef41Sopenharmony_ci
16401cb0ef41Sopenharmony_ci    /* Set up the client_random */
16411cb0ef41Sopenharmony_ci    memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE);
16421cb0ef41Sopenharmony_ci
16431cb0ef41Sopenharmony_ci    /* Choose the version */
16441cb0ef41Sopenharmony_ci
16451cb0ef41Sopenharmony_ci    if (clienthello->isv2) {
16461cb0ef41Sopenharmony_ci        if (clienthello->legacy_version == SSL2_VERSION
16471cb0ef41Sopenharmony_ci                || (clienthello->legacy_version & 0xff00)
16481cb0ef41Sopenharmony_ci                   != (SSL3_VERSION_MAJOR << 8)) {
16491cb0ef41Sopenharmony_ci            /*
16501cb0ef41Sopenharmony_ci             * This is real SSLv2 or something completely unknown. We don't
16511cb0ef41Sopenharmony_ci             * support it.
16521cb0ef41Sopenharmony_ci             */
16531cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNKNOWN_PROTOCOL);
16541cb0ef41Sopenharmony_ci            goto err;
16551cb0ef41Sopenharmony_ci        }
16561cb0ef41Sopenharmony_ci        /* SSLv3/TLS */
16571cb0ef41Sopenharmony_ci        s->client_version = clienthello->legacy_version;
16581cb0ef41Sopenharmony_ci    }
16591cb0ef41Sopenharmony_ci    /*
16601cb0ef41Sopenharmony_ci     * Do SSL/TLS version negotiation if applicable. For DTLS we just check
16611cb0ef41Sopenharmony_ci     * versions are potentially compatible. Version negotiation comes later.
16621cb0ef41Sopenharmony_ci     */
16631cb0ef41Sopenharmony_ci    if (!SSL_IS_DTLS(s)) {
16641cb0ef41Sopenharmony_ci        protverr = ssl_choose_server_version(s, clienthello, &dgrd);
16651cb0ef41Sopenharmony_ci    } else if (s->method->version != DTLS_ANY_VERSION &&
16661cb0ef41Sopenharmony_ci               DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
16671cb0ef41Sopenharmony_ci        protverr = SSL_R_VERSION_TOO_LOW;
16681cb0ef41Sopenharmony_ci    } else {
16691cb0ef41Sopenharmony_ci        protverr = 0;
16701cb0ef41Sopenharmony_ci    }
16711cb0ef41Sopenharmony_ci
16721cb0ef41Sopenharmony_ci    if (protverr) {
16731cb0ef41Sopenharmony_ci        if (SSL_IS_FIRST_HANDSHAKE(s)) {
16741cb0ef41Sopenharmony_ci            /* like ssl3_get_record, send alert using remote version number */
16751cb0ef41Sopenharmony_ci            s->version = s->client_version = clienthello->legacy_version;
16761cb0ef41Sopenharmony_ci        }
16771cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);
16781cb0ef41Sopenharmony_ci        goto err;
16791cb0ef41Sopenharmony_ci    }
16801cb0ef41Sopenharmony_ci
16811cb0ef41Sopenharmony_ci    /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
16821cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
16831cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
16841cb0ef41Sopenharmony_ci        goto err;
16851cb0ef41Sopenharmony_ci    }
16861cb0ef41Sopenharmony_ci
16871cb0ef41Sopenharmony_ci    if (SSL_IS_DTLS(s)) {
16881cb0ef41Sopenharmony_ci        /* Empty cookie was already handled above by returning early. */
16891cb0ef41Sopenharmony_ci        if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
16901cb0ef41Sopenharmony_ci            if (s->ctx->app_verify_cookie_cb != NULL) {
16911cb0ef41Sopenharmony_ci                if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
16921cb0ef41Sopenharmony_ci                        clienthello->dtls_cookie_len) == 0) {
16931cb0ef41Sopenharmony_ci                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
16941cb0ef41Sopenharmony_ci                             SSL_R_COOKIE_MISMATCH);
16951cb0ef41Sopenharmony_ci                    goto err;
16961cb0ef41Sopenharmony_ci                    /* else cookie verification succeeded */
16971cb0ef41Sopenharmony_ci                }
16981cb0ef41Sopenharmony_ci                /* default verification */
16991cb0ef41Sopenharmony_ci            } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
17001cb0ef41Sopenharmony_ci                    || memcmp(clienthello->dtls_cookie, s->d1->cookie,
17011cb0ef41Sopenharmony_ci                              s->d1->cookie_len) != 0) {
17021cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_COOKIE_MISMATCH);
17031cb0ef41Sopenharmony_ci                goto err;
17041cb0ef41Sopenharmony_ci            }
17051cb0ef41Sopenharmony_ci            s->d1->cookie_verified = 1;
17061cb0ef41Sopenharmony_ci        }
17071cb0ef41Sopenharmony_ci        if (s->method->version == DTLS_ANY_VERSION) {
17081cb0ef41Sopenharmony_ci            protverr = ssl_choose_server_version(s, clienthello, &dgrd);
17091cb0ef41Sopenharmony_ci            if (protverr != 0) {
17101cb0ef41Sopenharmony_ci                s->version = s->client_version;
17111cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);
17121cb0ef41Sopenharmony_ci                goto err;
17131cb0ef41Sopenharmony_ci            }
17141cb0ef41Sopenharmony_ci        }
17151cb0ef41Sopenharmony_ci    }
17161cb0ef41Sopenharmony_ci
17171cb0ef41Sopenharmony_ci    s->hit = 0;
17181cb0ef41Sopenharmony_ci
17191cb0ef41Sopenharmony_ci    if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
17201cb0ef41Sopenharmony_ci                              clienthello->isv2) ||
17211cb0ef41Sopenharmony_ci        !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
17221cb0ef41Sopenharmony_ci                              clienthello->isv2, 1)) {
17231cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
17241cb0ef41Sopenharmony_ci        goto err;
17251cb0ef41Sopenharmony_ci    }
17261cb0ef41Sopenharmony_ci
17271cb0ef41Sopenharmony_ci    s->s3.send_connection_binding = 0;
17281cb0ef41Sopenharmony_ci    /* Check what signalling cipher-suite values were received. */
17291cb0ef41Sopenharmony_ci    if (scsvs != NULL) {
17301cb0ef41Sopenharmony_ci        for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
17311cb0ef41Sopenharmony_ci            c = sk_SSL_CIPHER_value(scsvs, i);
17321cb0ef41Sopenharmony_ci            if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
17331cb0ef41Sopenharmony_ci                if (s->renegotiate) {
17341cb0ef41Sopenharmony_ci                    /* SCSV is fatal if renegotiating */
17351cb0ef41Sopenharmony_ci                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
17361cb0ef41Sopenharmony_ci                             SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
17371cb0ef41Sopenharmony_ci                    goto err;
17381cb0ef41Sopenharmony_ci                }
17391cb0ef41Sopenharmony_ci                s->s3.send_connection_binding = 1;
17401cb0ef41Sopenharmony_ci            } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
17411cb0ef41Sopenharmony_ci                       !ssl_check_version_downgrade(s)) {
17421cb0ef41Sopenharmony_ci                /*
17431cb0ef41Sopenharmony_ci                 * This SCSV indicates that the client previously tried
17441cb0ef41Sopenharmony_ci                 * a higher version.  We should fail if the current version
17451cb0ef41Sopenharmony_ci                 * is an unexpected downgrade, as that indicates that the first
17461cb0ef41Sopenharmony_ci                 * connection may have been tampered with in order to trigger
17471cb0ef41Sopenharmony_ci                 * an insecure downgrade.
17481cb0ef41Sopenharmony_ci                 */
17491cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
17501cb0ef41Sopenharmony_ci                         SSL_R_INAPPROPRIATE_FALLBACK);
17511cb0ef41Sopenharmony_ci                goto err;
17521cb0ef41Sopenharmony_ci            }
17531cb0ef41Sopenharmony_ci        }
17541cb0ef41Sopenharmony_ci    }
17551cb0ef41Sopenharmony_ci
17561cb0ef41Sopenharmony_ci    /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
17571cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
17581cb0ef41Sopenharmony_ci        const SSL_CIPHER *cipher =
17591cb0ef41Sopenharmony_ci            ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s));
17601cb0ef41Sopenharmony_ci
17611cb0ef41Sopenharmony_ci        if (cipher == NULL) {
17621cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
17631cb0ef41Sopenharmony_ci            goto err;
17641cb0ef41Sopenharmony_ci        }
17651cb0ef41Sopenharmony_ci        if (s->hello_retry_request == SSL_HRR_PENDING
17661cb0ef41Sopenharmony_ci                && (s->s3.tmp.new_cipher == NULL
17671cb0ef41Sopenharmony_ci                    || s->s3.tmp.new_cipher->id != cipher->id)) {
17681cb0ef41Sopenharmony_ci            /*
17691cb0ef41Sopenharmony_ci             * A previous HRR picked a different ciphersuite to the one we
17701cb0ef41Sopenharmony_ci             * just selected. Something must have changed.
17711cb0ef41Sopenharmony_ci             */
17721cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);
17731cb0ef41Sopenharmony_ci            goto err;
17741cb0ef41Sopenharmony_ci        }
17751cb0ef41Sopenharmony_ci        s->s3.tmp.new_cipher = cipher;
17761cb0ef41Sopenharmony_ci    }
17771cb0ef41Sopenharmony_ci
17781cb0ef41Sopenharmony_ci    /* We need to do this before getting the session */
17791cb0ef41Sopenharmony_ci    if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
17801cb0ef41Sopenharmony_ci                             SSL_EXT_CLIENT_HELLO,
17811cb0ef41Sopenharmony_ci                             clienthello->pre_proc_exts, NULL, 0)) {
17821cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
17831cb0ef41Sopenharmony_ci        goto err;
17841cb0ef41Sopenharmony_ci    }
17851cb0ef41Sopenharmony_ci
17861cb0ef41Sopenharmony_ci    /*
17871cb0ef41Sopenharmony_ci     * We don't allow resumption in a backwards compatible ClientHello.
17881cb0ef41Sopenharmony_ci     * In TLS1.1+, session_id MUST be empty.
17891cb0ef41Sopenharmony_ci     *
17901cb0ef41Sopenharmony_ci     * Versions before 0.9.7 always allow clients to resume sessions in
17911cb0ef41Sopenharmony_ci     * renegotiation. 0.9.7 and later allow this by default, but optionally
17921cb0ef41Sopenharmony_ci     * ignore resumption requests with flag
17931cb0ef41Sopenharmony_ci     * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
17941cb0ef41Sopenharmony_ci     * than a change to default behavior so that applications relying on
17951cb0ef41Sopenharmony_ci     * this for security won't even compile against older library versions).
17961cb0ef41Sopenharmony_ci     * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
17971cb0ef41Sopenharmony_ci     * request renegotiation but not a new session (s->new_session remains
17981cb0ef41Sopenharmony_ci     * unset): for servers, this essentially just means that the
17991cb0ef41Sopenharmony_ci     * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
18001cb0ef41Sopenharmony_ci     * ignored.
18011cb0ef41Sopenharmony_ci     */
18021cb0ef41Sopenharmony_ci    if (clienthello->isv2 ||
18031cb0ef41Sopenharmony_ci        (s->new_session &&
18041cb0ef41Sopenharmony_ci         (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
18051cb0ef41Sopenharmony_ci        if (!ssl_get_new_session(s, 1)) {
18061cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
18071cb0ef41Sopenharmony_ci            goto err;
18081cb0ef41Sopenharmony_ci        }
18091cb0ef41Sopenharmony_ci    } else {
18101cb0ef41Sopenharmony_ci        i = ssl_get_prev_session(s, clienthello);
18111cb0ef41Sopenharmony_ci        if (i == 1) {
18121cb0ef41Sopenharmony_ci            /* previous session */
18131cb0ef41Sopenharmony_ci            s->hit = 1;
18141cb0ef41Sopenharmony_ci        } else if (i == -1) {
18151cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
18161cb0ef41Sopenharmony_ci            goto err;
18171cb0ef41Sopenharmony_ci        } else {
18181cb0ef41Sopenharmony_ci            /* i == 0 */
18191cb0ef41Sopenharmony_ci            if (!ssl_get_new_session(s, 1)) {
18201cb0ef41Sopenharmony_ci                /* SSLfatal() already called */
18211cb0ef41Sopenharmony_ci                goto err;
18221cb0ef41Sopenharmony_ci            }
18231cb0ef41Sopenharmony_ci        }
18241cb0ef41Sopenharmony_ci    }
18251cb0ef41Sopenharmony_ci
18261cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
18271cb0ef41Sopenharmony_ci        memcpy(s->tmp_session_id, s->clienthello->session_id,
18281cb0ef41Sopenharmony_ci               s->clienthello->session_id_len);
18291cb0ef41Sopenharmony_ci        s->tmp_session_id_len = s->clienthello->session_id_len;
18301cb0ef41Sopenharmony_ci    }
18311cb0ef41Sopenharmony_ci
18321cb0ef41Sopenharmony_ci    /*
18331cb0ef41Sopenharmony_ci     * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
18341cb0ef41Sopenharmony_ci     * ciphersuite compatibility with the session as part of resumption.
18351cb0ef41Sopenharmony_ci     */
18361cb0ef41Sopenharmony_ci    if (!SSL_IS_TLS13(s) && s->hit) {
18371cb0ef41Sopenharmony_ci        j = 0;
18381cb0ef41Sopenharmony_ci        id = s->session->cipher->id;
18391cb0ef41Sopenharmony_ci
18401cb0ef41Sopenharmony_ci        OSSL_TRACE_BEGIN(TLS_CIPHER) {
18411cb0ef41Sopenharmony_ci            BIO_printf(trc_out, "client sent %d ciphers\n",
18421cb0ef41Sopenharmony_ci                       sk_SSL_CIPHER_num(ciphers));
18431cb0ef41Sopenharmony_ci        }
18441cb0ef41Sopenharmony_ci        for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
18451cb0ef41Sopenharmony_ci            c = sk_SSL_CIPHER_value(ciphers, i);
18461cb0ef41Sopenharmony_ci            if (trc_out != NULL)
18471cb0ef41Sopenharmony_ci                BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i,
18481cb0ef41Sopenharmony_ci                           sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
18491cb0ef41Sopenharmony_ci            if (c->id == id) {
18501cb0ef41Sopenharmony_ci                j = 1;
18511cb0ef41Sopenharmony_ci                break;
18521cb0ef41Sopenharmony_ci            }
18531cb0ef41Sopenharmony_ci        }
18541cb0ef41Sopenharmony_ci        if (j == 0) {
18551cb0ef41Sopenharmony_ci            /*
18561cb0ef41Sopenharmony_ci             * we need to have the cipher in the cipher list if we are asked
18571cb0ef41Sopenharmony_ci             * to reuse it
18581cb0ef41Sopenharmony_ci             */
18591cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
18601cb0ef41Sopenharmony_ci                     SSL_R_REQUIRED_CIPHER_MISSING);
18611cb0ef41Sopenharmony_ci            OSSL_TRACE_CANCEL(TLS_CIPHER);
18621cb0ef41Sopenharmony_ci            goto err;
18631cb0ef41Sopenharmony_ci        }
18641cb0ef41Sopenharmony_ci        OSSL_TRACE_END(TLS_CIPHER);
18651cb0ef41Sopenharmony_ci    }
18661cb0ef41Sopenharmony_ci
18671cb0ef41Sopenharmony_ci    for (loop = 0; loop < clienthello->compressions_len; loop++) {
18681cb0ef41Sopenharmony_ci        if (clienthello->compressions[loop] == 0)
18691cb0ef41Sopenharmony_ci            break;
18701cb0ef41Sopenharmony_ci    }
18711cb0ef41Sopenharmony_ci
18721cb0ef41Sopenharmony_ci    if (loop >= clienthello->compressions_len) {
18731cb0ef41Sopenharmony_ci        /* no compress */
18741cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_COMPRESSION_SPECIFIED);
18751cb0ef41Sopenharmony_ci        goto err;
18761cb0ef41Sopenharmony_ci    }
18771cb0ef41Sopenharmony_ci
18781cb0ef41Sopenharmony_ci    if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
18791cb0ef41Sopenharmony_ci        ssl_check_for_safari(s, clienthello);
18801cb0ef41Sopenharmony_ci
18811cb0ef41Sopenharmony_ci    /* TLS extensions */
18821cb0ef41Sopenharmony_ci    if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
18831cb0ef41Sopenharmony_ci                                  clienthello->pre_proc_exts, NULL, 0, 1)) {
18841cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
18851cb0ef41Sopenharmony_ci        goto err;
18861cb0ef41Sopenharmony_ci    }
18871cb0ef41Sopenharmony_ci
18881cb0ef41Sopenharmony_ci    /*
18891cb0ef41Sopenharmony_ci     * Check if we want to use external pre-shared secret for this handshake
18901cb0ef41Sopenharmony_ci     * for not reused session only. We need to generate server_random before
18911cb0ef41Sopenharmony_ci     * calling tls_session_secret_cb in order to allow SessionTicket
18921cb0ef41Sopenharmony_ci     * processing to use it in key derivation.
18931cb0ef41Sopenharmony_ci     */
18941cb0ef41Sopenharmony_ci    {
18951cb0ef41Sopenharmony_ci        unsigned char *pos;
18961cb0ef41Sopenharmony_ci        pos = s->s3.server_random;
18971cb0ef41Sopenharmony_ci        if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
18981cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
18991cb0ef41Sopenharmony_ci            goto err;
19001cb0ef41Sopenharmony_ci        }
19011cb0ef41Sopenharmony_ci    }
19021cb0ef41Sopenharmony_ci
19031cb0ef41Sopenharmony_ci    if (!s->hit
19041cb0ef41Sopenharmony_ci            && s->version >= TLS1_VERSION
19051cb0ef41Sopenharmony_ci            && !SSL_IS_TLS13(s)
19061cb0ef41Sopenharmony_ci            && !SSL_IS_DTLS(s)
19071cb0ef41Sopenharmony_ci            && s->ext.session_secret_cb) {
19081cb0ef41Sopenharmony_ci        const SSL_CIPHER *pref_cipher = NULL;
19091cb0ef41Sopenharmony_ci        /*
19101cb0ef41Sopenharmony_ci         * s->session->master_key_length is a size_t, but this is an int for
19111cb0ef41Sopenharmony_ci         * backwards compat reasons
19121cb0ef41Sopenharmony_ci         */
19131cb0ef41Sopenharmony_ci        int master_key_length;
19141cb0ef41Sopenharmony_ci
19151cb0ef41Sopenharmony_ci        master_key_length = sizeof(s->session->master_key);
19161cb0ef41Sopenharmony_ci        if (s->ext.session_secret_cb(s, s->session->master_key,
19171cb0ef41Sopenharmony_ci                                     &master_key_length, ciphers,
19181cb0ef41Sopenharmony_ci                                     &pref_cipher,
19191cb0ef41Sopenharmony_ci                                     s->ext.session_secret_cb_arg)
19201cb0ef41Sopenharmony_ci                && master_key_length > 0) {
19211cb0ef41Sopenharmony_ci            s->session->master_key_length = master_key_length;
19221cb0ef41Sopenharmony_ci            s->hit = 1;
19231cb0ef41Sopenharmony_ci            s->peer_ciphers = ciphers;
19241cb0ef41Sopenharmony_ci            s->session->verify_result = X509_V_OK;
19251cb0ef41Sopenharmony_ci
19261cb0ef41Sopenharmony_ci            ciphers = NULL;
19271cb0ef41Sopenharmony_ci
19281cb0ef41Sopenharmony_ci            /* check if some cipher was preferred by call back */
19291cb0ef41Sopenharmony_ci            if (pref_cipher == NULL)
19301cb0ef41Sopenharmony_ci                pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers,
19311cb0ef41Sopenharmony_ci                                                 SSL_get_ciphers(s));
19321cb0ef41Sopenharmony_ci            if (pref_cipher == NULL) {
19331cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
19341cb0ef41Sopenharmony_ci                goto err;
19351cb0ef41Sopenharmony_ci            }
19361cb0ef41Sopenharmony_ci
19371cb0ef41Sopenharmony_ci            s->session->cipher = pref_cipher;
19381cb0ef41Sopenharmony_ci            sk_SSL_CIPHER_free(s->cipher_list);
19391cb0ef41Sopenharmony_ci            s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers);
19401cb0ef41Sopenharmony_ci            sk_SSL_CIPHER_free(s->cipher_list_by_id);
19411cb0ef41Sopenharmony_ci            s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers);
19421cb0ef41Sopenharmony_ci        }
19431cb0ef41Sopenharmony_ci    }
19441cb0ef41Sopenharmony_ci
19451cb0ef41Sopenharmony_ci    /*
19461cb0ef41Sopenharmony_ci     * Worst case, we will use the NULL compression, but if we have other
19471cb0ef41Sopenharmony_ci     * options, we will now look for them.  We have complen-1 compression
19481cb0ef41Sopenharmony_ci     * algorithms from the client, starting at q.
19491cb0ef41Sopenharmony_ci     */
19501cb0ef41Sopenharmony_ci    s->s3.tmp.new_compression = NULL;
19511cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
19521cb0ef41Sopenharmony_ci        /*
19531cb0ef41Sopenharmony_ci         * We already checked above that the NULL compression method appears in
19541cb0ef41Sopenharmony_ci         * the list. Now we check there aren't any others (which is illegal in
19551cb0ef41Sopenharmony_ci         * a TLSv1.3 ClientHello.
19561cb0ef41Sopenharmony_ci         */
19571cb0ef41Sopenharmony_ci        if (clienthello->compressions_len != 1) {
19581cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
19591cb0ef41Sopenharmony_ci                     SSL_R_INVALID_COMPRESSION_ALGORITHM);
19601cb0ef41Sopenharmony_ci            goto err;
19611cb0ef41Sopenharmony_ci        }
19621cb0ef41Sopenharmony_ci    }
19631cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_COMP
19641cb0ef41Sopenharmony_ci    /* This only happens if we have a cache hit */
19651cb0ef41Sopenharmony_ci    else if (s->session->compress_meth != 0) {
19661cb0ef41Sopenharmony_ci        int m, comp_id = s->session->compress_meth;
19671cb0ef41Sopenharmony_ci        unsigned int k;
19681cb0ef41Sopenharmony_ci        /* Perform sanity checks on resumed compression algorithm */
19691cb0ef41Sopenharmony_ci        /* Can't disable compression */
19701cb0ef41Sopenharmony_ci        if (!ssl_allow_compression(s)) {
19711cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
19721cb0ef41Sopenharmony_ci                     SSL_R_INCONSISTENT_COMPRESSION);
19731cb0ef41Sopenharmony_ci            goto err;
19741cb0ef41Sopenharmony_ci        }
19751cb0ef41Sopenharmony_ci        /* Look for resumed compression method */
19761cb0ef41Sopenharmony_ci        for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
19771cb0ef41Sopenharmony_ci            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
19781cb0ef41Sopenharmony_ci            if (comp_id == comp->id) {
19791cb0ef41Sopenharmony_ci                s->s3.tmp.new_compression = comp;
19801cb0ef41Sopenharmony_ci                break;
19811cb0ef41Sopenharmony_ci            }
19821cb0ef41Sopenharmony_ci        }
19831cb0ef41Sopenharmony_ci        if (s->s3.tmp.new_compression == NULL) {
19841cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
19851cb0ef41Sopenharmony_ci                     SSL_R_INVALID_COMPRESSION_ALGORITHM);
19861cb0ef41Sopenharmony_ci            goto err;
19871cb0ef41Sopenharmony_ci        }
19881cb0ef41Sopenharmony_ci        /* Look for resumed method in compression list */
19891cb0ef41Sopenharmony_ci        for (k = 0; k < clienthello->compressions_len; k++) {
19901cb0ef41Sopenharmony_ci            if (clienthello->compressions[k] == comp_id)
19911cb0ef41Sopenharmony_ci                break;
19921cb0ef41Sopenharmony_ci        }
19931cb0ef41Sopenharmony_ci        if (k >= clienthello->compressions_len) {
19941cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
19951cb0ef41Sopenharmony_ci                     SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
19961cb0ef41Sopenharmony_ci            goto err;
19971cb0ef41Sopenharmony_ci        }
19981cb0ef41Sopenharmony_ci    } else if (s->hit) {
19991cb0ef41Sopenharmony_ci        comp = NULL;
20001cb0ef41Sopenharmony_ci    } else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
20011cb0ef41Sopenharmony_ci        /* See if we have a match */
20021cb0ef41Sopenharmony_ci        int m, nn, v, done = 0;
20031cb0ef41Sopenharmony_ci        unsigned int o;
20041cb0ef41Sopenharmony_ci
20051cb0ef41Sopenharmony_ci        nn = sk_SSL_COMP_num(s->ctx->comp_methods);
20061cb0ef41Sopenharmony_ci        for (m = 0; m < nn; m++) {
20071cb0ef41Sopenharmony_ci            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
20081cb0ef41Sopenharmony_ci            v = comp->id;
20091cb0ef41Sopenharmony_ci            for (o = 0; o < clienthello->compressions_len; o++) {
20101cb0ef41Sopenharmony_ci                if (v == clienthello->compressions[o]) {
20111cb0ef41Sopenharmony_ci                    done = 1;
20121cb0ef41Sopenharmony_ci                    break;
20131cb0ef41Sopenharmony_ci                }
20141cb0ef41Sopenharmony_ci            }
20151cb0ef41Sopenharmony_ci            if (done)
20161cb0ef41Sopenharmony_ci                break;
20171cb0ef41Sopenharmony_ci        }
20181cb0ef41Sopenharmony_ci        if (done)
20191cb0ef41Sopenharmony_ci            s->s3.tmp.new_compression = comp;
20201cb0ef41Sopenharmony_ci        else
20211cb0ef41Sopenharmony_ci            comp = NULL;
20221cb0ef41Sopenharmony_ci    }
20231cb0ef41Sopenharmony_ci#else
20241cb0ef41Sopenharmony_ci    /*
20251cb0ef41Sopenharmony_ci     * If compression is disabled we'd better not try to resume a session
20261cb0ef41Sopenharmony_ci     * using compression.
20271cb0ef41Sopenharmony_ci     */
20281cb0ef41Sopenharmony_ci    if (s->session->compress_meth != 0) {
20291cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
20301cb0ef41Sopenharmony_ci        goto err;
20311cb0ef41Sopenharmony_ci    }
20321cb0ef41Sopenharmony_ci#endif
20331cb0ef41Sopenharmony_ci
20341cb0ef41Sopenharmony_ci    /*
20351cb0ef41Sopenharmony_ci     * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
20361cb0ef41Sopenharmony_ci     */
20371cb0ef41Sopenharmony_ci
20381cb0ef41Sopenharmony_ci    if (!s->hit || SSL_IS_TLS13(s)) {
20391cb0ef41Sopenharmony_ci        sk_SSL_CIPHER_free(s->peer_ciphers);
20401cb0ef41Sopenharmony_ci        s->peer_ciphers = ciphers;
20411cb0ef41Sopenharmony_ci        if (ciphers == NULL) {
20421cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
20431cb0ef41Sopenharmony_ci            goto err;
20441cb0ef41Sopenharmony_ci        }
20451cb0ef41Sopenharmony_ci        ciphers = NULL;
20461cb0ef41Sopenharmony_ci    }
20471cb0ef41Sopenharmony_ci
20481cb0ef41Sopenharmony_ci    if (!s->hit) {
20491cb0ef41Sopenharmony_ci#ifdef OPENSSL_NO_COMP
20501cb0ef41Sopenharmony_ci        s->session->compress_meth = 0;
20511cb0ef41Sopenharmony_ci#else
20521cb0ef41Sopenharmony_ci        s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
20531cb0ef41Sopenharmony_ci#endif
20541cb0ef41Sopenharmony_ci        if (!tls1_set_server_sigalgs(s)) {
20551cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
20561cb0ef41Sopenharmony_ci            goto err;
20571cb0ef41Sopenharmony_ci        }
20581cb0ef41Sopenharmony_ci    }
20591cb0ef41Sopenharmony_ci
20601cb0ef41Sopenharmony_ci    sk_SSL_CIPHER_free(ciphers);
20611cb0ef41Sopenharmony_ci    sk_SSL_CIPHER_free(scsvs);
20621cb0ef41Sopenharmony_ci    OPENSSL_free(clienthello->pre_proc_exts);
20631cb0ef41Sopenharmony_ci    OPENSSL_free(s->clienthello);
20641cb0ef41Sopenharmony_ci    s->clienthello = NULL;
20651cb0ef41Sopenharmony_ci    return 1;
20661cb0ef41Sopenharmony_ci err:
20671cb0ef41Sopenharmony_ci    sk_SSL_CIPHER_free(ciphers);
20681cb0ef41Sopenharmony_ci    sk_SSL_CIPHER_free(scsvs);
20691cb0ef41Sopenharmony_ci    OPENSSL_free(clienthello->pre_proc_exts);
20701cb0ef41Sopenharmony_ci    OPENSSL_free(s->clienthello);
20711cb0ef41Sopenharmony_ci    s->clienthello = NULL;
20721cb0ef41Sopenharmony_ci
20731cb0ef41Sopenharmony_ci    return 0;
20741cb0ef41Sopenharmony_ci}
20751cb0ef41Sopenharmony_ci
20761cb0ef41Sopenharmony_ci/*
20771cb0ef41Sopenharmony_ci * Call the status request callback if needed. Upon success, returns 1.
20781cb0ef41Sopenharmony_ci * Upon failure, returns 0.
20791cb0ef41Sopenharmony_ci */
20801cb0ef41Sopenharmony_cistatic int tls_handle_status_request(SSL *s)
20811cb0ef41Sopenharmony_ci{
20821cb0ef41Sopenharmony_ci    s->ext.status_expected = 0;
20831cb0ef41Sopenharmony_ci
20841cb0ef41Sopenharmony_ci    /*
20851cb0ef41Sopenharmony_ci     * If status request then ask callback what to do. Note: this must be
20861cb0ef41Sopenharmony_ci     * called after servername callbacks in case the certificate has changed,
20871cb0ef41Sopenharmony_ci     * and must be called after the cipher has been chosen because this may
20881cb0ef41Sopenharmony_ci     * influence which certificate is sent
20891cb0ef41Sopenharmony_ci     */
20901cb0ef41Sopenharmony_ci    if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
20911cb0ef41Sopenharmony_ci            && s->ctx->ext.status_cb != NULL) {
20921cb0ef41Sopenharmony_ci        int ret;
20931cb0ef41Sopenharmony_ci
20941cb0ef41Sopenharmony_ci        /* If no certificate can't return certificate status */
20951cb0ef41Sopenharmony_ci        if (s->s3.tmp.cert != NULL) {
20961cb0ef41Sopenharmony_ci            /*
20971cb0ef41Sopenharmony_ci             * Set current certificate to one we will use so SSL_get_certificate
20981cb0ef41Sopenharmony_ci             * et al can pick it up.
20991cb0ef41Sopenharmony_ci             */
21001cb0ef41Sopenharmony_ci            s->cert->key = s->s3.tmp.cert;
21011cb0ef41Sopenharmony_ci            ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
21021cb0ef41Sopenharmony_ci            switch (ret) {
21031cb0ef41Sopenharmony_ci                /* We don't want to send a status request response */
21041cb0ef41Sopenharmony_ci            case SSL_TLSEXT_ERR_NOACK:
21051cb0ef41Sopenharmony_ci                s->ext.status_expected = 0;
21061cb0ef41Sopenharmony_ci                break;
21071cb0ef41Sopenharmony_ci                /* status request response should be sent */
21081cb0ef41Sopenharmony_ci            case SSL_TLSEXT_ERR_OK:
21091cb0ef41Sopenharmony_ci                if (s->ext.ocsp.resp)
21101cb0ef41Sopenharmony_ci                    s->ext.status_expected = 1;
21111cb0ef41Sopenharmony_ci                break;
21121cb0ef41Sopenharmony_ci                /* something bad happened */
21131cb0ef41Sopenharmony_ci            case SSL_TLSEXT_ERR_ALERT_FATAL:
21141cb0ef41Sopenharmony_ci            default:
21151cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CLIENTHELLO_TLSEXT);
21161cb0ef41Sopenharmony_ci                return 0;
21171cb0ef41Sopenharmony_ci            }
21181cb0ef41Sopenharmony_ci        }
21191cb0ef41Sopenharmony_ci    }
21201cb0ef41Sopenharmony_ci
21211cb0ef41Sopenharmony_ci    return 1;
21221cb0ef41Sopenharmony_ci}
21231cb0ef41Sopenharmony_ci
21241cb0ef41Sopenharmony_ci/*
21251cb0ef41Sopenharmony_ci * Call the alpn_select callback if needed. Upon success, returns 1.
21261cb0ef41Sopenharmony_ci * Upon failure, returns 0.
21271cb0ef41Sopenharmony_ci */
21281cb0ef41Sopenharmony_ciint tls_handle_alpn(SSL *s)
21291cb0ef41Sopenharmony_ci{
21301cb0ef41Sopenharmony_ci    const unsigned char *selected = NULL;
21311cb0ef41Sopenharmony_ci    unsigned char selected_len = 0;
21321cb0ef41Sopenharmony_ci
21331cb0ef41Sopenharmony_ci    if (s->ctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) {
21341cb0ef41Sopenharmony_ci        int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
21351cb0ef41Sopenharmony_ci                                           s->s3.alpn_proposed,
21361cb0ef41Sopenharmony_ci                                           (unsigned int)s->s3.alpn_proposed_len,
21371cb0ef41Sopenharmony_ci                                           s->ctx->ext.alpn_select_cb_arg);
21381cb0ef41Sopenharmony_ci
21391cb0ef41Sopenharmony_ci        if (r == SSL_TLSEXT_ERR_OK) {
21401cb0ef41Sopenharmony_ci            OPENSSL_free(s->s3.alpn_selected);
21411cb0ef41Sopenharmony_ci            s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len);
21421cb0ef41Sopenharmony_ci            if (s->s3.alpn_selected == NULL) {
21431cb0ef41Sopenharmony_ci                s->s3.alpn_selected_len = 0;
21441cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
21451cb0ef41Sopenharmony_ci                return 0;
21461cb0ef41Sopenharmony_ci            }
21471cb0ef41Sopenharmony_ci            s->s3.alpn_selected_len = selected_len;
21481cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_NEXTPROTONEG
21491cb0ef41Sopenharmony_ci            /* ALPN takes precedence over NPN. */
21501cb0ef41Sopenharmony_ci            s->s3.npn_seen = 0;
21511cb0ef41Sopenharmony_ci#endif
21521cb0ef41Sopenharmony_ci
21531cb0ef41Sopenharmony_ci            /* Check ALPN is consistent with session */
21541cb0ef41Sopenharmony_ci            if (s->session->ext.alpn_selected == NULL
21551cb0ef41Sopenharmony_ci                        || selected_len != s->session->ext.alpn_selected_len
21561cb0ef41Sopenharmony_ci                        || memcmp(selected, s->session->ext.alpn_selected,
21571cb0ef41Sopenharmony_ci                                  selected_len) != 0) {
21581cb0ef41Sopenharmony_ci                /* Not consistent so can't be used for early_data */
21591cb0ef41Sopenharmony_ci                s->ext.early_data_ok = 0;
21601cb0ef41Sopenharmony_ci
21611cb0ef41Sopenharmony_ci                if (!s->hit) {
21621cb0ef41Sopenharmony_ci                    /*
21631cb0ef41Sopenharmony_ci                     * This is a new session and so alpn_selected should have
21641cb0ef41Sopenharmony_ci                     * been initialised to NULL. We should update it with the
21651cb0ef41Sopenharmony_ci                     * selected ALPN.
21661cb0ef41Sopenharmony_ci                     */
21671cb0ef41Sopenharmony_ci                    if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
21681cb0ef41Sopenharmony_ci                        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
21691cb0ef41Sopenharmony_ci                                 ERR_R_INTERNAL_ERROR);
21701cb0ef41Sopenharmony_ci                        return 0;
21711cb0ef41Sopenharmony_ci                    }
21721cb0ef41Sopenharmony_ci                    s->session->ext.alpn_selected = OPENSSL_memdup(selected,
21731cb0ef41Sopenharmony_ci                                                                   selected_len);
21741cb0ef41Sopenharmony_ci                    if (s->session->ext.alpn_selected == NULL) {
21751cb0ef41Sopenharmony_ci                        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
21761cb0ef41Sopenharmony_ci                                 ERR_R_INTERNAL_ERROR);
21771cb0ef41Sopenharmony_ci                        return 0;
21781cb0ef41Sopenharmony_ci                    }
21791cb0ef41Sopenharmony_ci                    s->session->ext.alpn_selected_len = selected_len;
21801cb0ef41Sopenharmony_ci                }
21811cb0ef41Sopenharmony_ci            }
21821cb0ef41Sopenharmony_ci
21831cb0ef41Sopenharmony_ci            return 1;
21841cb0ef41Sopenharmony_ci        } else if (r != SSL_TLSEXT_ERR_NOACK) {
21851cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL,
21861cb0ef41Sopenharmony_ci                     SSL_R_NO_APPLICATION_PROTOCOL);
21871cb0ef41Sopenharmony_ci            return 0;
21881cb0ef41Sopenharmony_ci        }
21891cb0ef41Sopenharmony_ci        /*
21901cb0ef41Sopenharmony_ci         * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
21911cb0ef41Sopenharmony_ci         * present.
21921cb0ef41Sopenharmony_ci         */
21931cb0ef41Sopenharmony_ci    }
21941cb0ef41Sopenharmony_ci
21951cb0ef41Sopenharmony_ci    /* Check ALPN is consistent with session */
21961cb0ef41Sopenharmony_ci    if (s->session->ext.alpn_selected != NULL) {
21971cb0ef41Sopenharmony_ci        /* Not consistent so can't be used for early_data */
21981cb0ef41Sopenharmony_ci        s->ext.early_data_ok = 0;
21991cb0ef41Sopenharmony_ci    }
22001cb0ef41Sopenharmony_ci
22011cb0ef41Sopenharmony_ci    return 1;
22021cb0ef41Sopenharmony_ci}
22031cb0ef41Sopenharmony_ci
22041cb0ef41Sopenharmony_ciWORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
22051cb0ef41Sopenharmony_ci{
22061cb0ef41Sopenharmony_ci    const SSL_CIPHER *cipher;
22071cb0ef41Sopenharmony_ci
22081cb0ef41Sopenharmony_ci    if (wst == WORK_MORE_A) {
22091cb0ef41Sopenharmony_ci        int rv = tls_early_post_process_client_hello(s);
22101cb0ef41Sopenharmony_ci        if (rv == 0) {
22111cb0ef41Sopenharmony_ci            /* SSLfatal() was already called */
22121cb0ef41Sopenharmony_ci            goto err;
22131cb0ef41Sopenharmony_ci        }
22141cb0ef41Sopenharmony_ci        if (rv < 0)
22151cb0ef41Sopenharmony_ci            return WORK_MORE_A;
22161cb0ef41Sopenharmony_ci        wst = WORK_MORE_B;
22171cb0ef41Sopenharmony_ci    }
22181cb0ef41Sopenharmony_ci    if (wst == WORK_MORE_B) {
22191cb0ef41Sopenharmony_ci        if (!s->hit || SSL_IS_TLS13(s)) {
22201cb0ef41Sopenharmony_ci            /* Let cert callback update server certificates if required */
22211cb0ef41Sopenharmony_ci            if (!s->hit && s->cert->cert_cb != NULL) {
22221cb0ef41Sopenharmony_ci                int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
22231cb0ef41Sopenharmony_ci                if (rv == 0) {
22241cb0ef41Sopenharmony_ci                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CERT_CB_ERROR);
22251cb0ef41Sopenharmony_ci                    goto err;
22261cb0ef41Sopenharmony_ci                }
22271cb0ef41Sopenharmony_ci                if (rv < 0) {
22281cb0ef41Sopenharmony_ci                    s->rwstate = SSL_X509_LOOKUP;
22291cb0ef41Sopenharmony_ci                    return WORK_MORE_B;
22301cb0ef41Sopenharmony_ci                }
22311cb0ef41Sopenharmony_ci                s->rwstate = SSL_NOTHING;
22321cb0ef41Sopenharmony_ci            }
22331cb0ef41Sopenharmony_ci
22341cb0ef41Sopenharmony_ci            /* In TLSv1.3 we selected the ciphersuite before resumption */
22351cb0ef41Sopenharmony_ci            if (!SSL_IS_TLS13(s)) {
22361cb0ef41Sopenharmony_ci                cipher =
22371cb0ef41Sopenharmony_ci                    ssl3_choose_cipher(s, s->peer_ciphers, SSL_get_ciphers(s));
22381cb0ef41Sopenharmony_ci
22391cb0ef41Sopenharmony_ci                if (cipher == NULL) {
22401cb0ef41Sopenharmony_ci                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
22411cb0ef41Sopenharmony_ci                             SSL_R_NO_SHARED_CIPHER);
22421cb0ef41Sopenharmony_ci                    goto err;
22431cb0ef41Sopenharmony_ci                }
22441cb0ef41Sopenharmony_ci                s->s3.tmp.new_cipher = cipher;
22451cb0ef41Sopenharmony_ci            }
22461cb0ef41Sopenharmony_ci            if (!s->hit) {
22471cb0ef41Sopenharmony_ci                if (!tls_choose_sigalg(s, 1)) {
22481cb0ef41Sopenharmony_ci                    /* SSLfatal already called */
22491cb0ef41Sopenharmony_ci                    goto err;
22501cb0ef41Sopenharmony_ci                }
22511cb0ef41Sopenharmony_ci                /* check whether we should disable session resumption */
22521cb0ef41Sopenharmony_ci                if (s->not_resumable_session_cb != NULL)
22531cb0ef41Sopenharmony_ci                    s->session->not_resumable =
22541cb0ef41Sopenharmony_ci                        s->not_resumable_session_cb(s,
22551cb0ef41Sopenharmony_ci                            ((s->s3.tmp.new_cipher->algorithm_mkey
22561cb0ef41Sopenharmony_ci                              & (SSL_kDHE | SSL_kECDHE)) != 0));
22571cb0ef41Sopenharmony_ci                if (s->session->not_resumable)
22581cb0ef41Sopenharmony_ci                    /* do not send a session ticket */
22591cb0ef41Sopenharmony_ci                    s->ext.ticket_expected = 0;
22601cb0ef41Sopenharmony_ci            }
22611cb0ef41Sopenharmony_ci        } else {
22621cb0ef41Sopenharmony_ci            /* Session-id reuse */
22631cb0ef41Sopenharmony_ci            s->s3.tmp.new_cipher = s->session->cipher;
22641cb0ef41Sopenharmony_ci        }
22651cb0ef41Sopenharmony_ci
22661cb0ef41Sopenharmony_ci        /*-
22671cb0ef41Sopenharmony_ci         * we now have the following setup.
22681cb0ef41Sopenharmony_ci         * client_random
22691cb0ef41Sopenharmony_ci         * cipher_list          - our preferred list of ciphers
22701cb0ef41Sopenharmony_ci         * ciphers              - the clients preferred list of ciphers
22711cb0ef41Sopenharmony_ci         * compression          - basically ignored right now
22721cb0ef41Sopenharmony_ci         * ssl version is set   - sslv3
22731cb0ef41Sopenharmony_ci         * s->session           - The ssl session has been setup.
22741cb0ef41Sopenharmony_ci         * s->hit               - session reuse flag
22751cb0ef41Sopenharmony_ci         * s->s3.tmp.new_cipher - the new cipher to use.
22761cb0ef41Sopenharmony_ci         */
22771cb0ef41Sopenharmony_ci
22781cb0ef41Sopenharmony_ci        /*
22791cb0ef41Sopenharmony_ci         * Call status_request callback if needed. Has to be done after the
22801cb0ef41Sopenharmony_ci         * certificate callbacks etc above.
22811cb0ef41Sopenharmony_ci         */
22821cb0ef41Sopenharmony_ci        if (!tls_handle_status_request(s)) {
22831cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
22841cb0ef41Sopenharmony_ci            goto err;
22851cb0ef41Sopenharmony_ci        }
22861cb0ef41Sopenharmony_ci        /*
22871cb0ef41Sopenharmony_ci         * Call alpn_select callback if needed.  Has to be done after SNI and
22881cb0ef41Sopenharmony_ci         * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
22891cb0ef41Sopenharmony_ci         * we already did this because cipher negotiation happens earlier, and
22901cb0ef41Sopenharmony_ci         * we must handle ALPN before we decide whether to accept early_data.
22911cb0ef41Sopenharmony_ci         */
22921cb0ef41Sopenharmony_ci        if (!SSL_IS_TLS13(s) && !tls_handle_alpn(s)) {
22931cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
22941cb0ef41Sopenharmony_ci            goto err;
22951cb0ef41Sopenharmony_ci        }
22961cb0ef41Sopenharmony_ci
22971cb0ef41Sopenharmony_ci        wst = WORK_MORE_C;
22981cb0ef41Sopenharmony_ci    }
22991cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SRP
23001cb0ef41Sopenharmony_ci    if (wst == WORK_MORE_C) {
23011cb0ef41Sopenharmony_ci        int ret;
23021cb0ef41Sopenharmony_ci        if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
23031cb0ef41Sopenharmony_ci            /*
23041cb0ef41Sopenharmony_ci             * callback indicates further work to be done
23051cb0ef41Sopenharmony_ci             */
23061cb0ef41Sopenharmony_ci            s->rwstate = SSL_X509_LOOKUP;
23071cb0ef41Sopenharmony_ci            return WORK_MORE_C;
23081cb0ef41Sopenharmony_ci        }
23091cb0ef41Sopenharmony_ci        if (ret < 0) {
23101cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
23111cb0ef41Sopenharmony_ci            goto err;
23121cb0ef41Sopenharmony_ci        }
23131cb0ef41Sopenharmony_ci    }
23141cb0ef41Sopenharmony_ci#endif
23151cb0ef41Sopenharmony_ci
23161cb0ef41Sopenharmony_ci    return WORK_FINISHED_STOP;
23171cb0ef41Sopenharmony_ci err:
23181cb0ef41Sopenharmony_ci    return WORK_ERROR;
23191cb0ef41Sopenharmony_ci}
23201cb0ef41Sopenharmony_ci
23211cb0ef41Sopenharmony_ciint tls_construct_server_hello(SSL *s, WPACKET *pkt)
23221cb0ef41Sopenharmony_ci{
23231cb0ef41Sopenharmony_ci    int compm;
23241cb0ef41Sopenharmony_ci    size_t sl, len;
23251cb0ef41Sopenharmony_ci    int version;
23261cb0ef41Sopenharmony_ci    unsigned char *session_id;
23271cb0ef41Sopenharmony_ci    int usetls13 = SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING;
23281cb0ef41Sopenharmony_ci
23291cb0ef41Sopenharmony_ci    version = usetls13 ? TLS1_2_VERSION : s->version;
23301cb0ef41Sopenharmony_ci    if (!WPACKET_put_bytes_u16(pkt, version)
23311cb0ef41Sopenharmony_ci               /*
23321cb0ef41Sopenharmony_ci                * Random stuff. Filling of the server_random takes place in
23331cb0ef41Sopenharmony_ci                * tls_process_client_hello()
23341cb0ef41Sopenharmony_ci                */
23351cb0ef41Sopenharmony_ci            || !WPACKET_memcpy(pkt,
23361cb0ef41Sopenharmony_ci                               s->hello_retry_request == SSL_HRR_PENDING
23371cb0ef41Sopenharmony_ci                                   ? hrrrandom : s->s3.server_random,
23381cb0ef41Sopenharmony_ci                               SSL3_RANDOM_SIZE)) {
23391cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
23401cb0ef41Sopenharmony_ci        return 0;
23411cb0ef41Sopenharmony_ci    }
23421cb0ef41Sopenharmony_ci
23431cb0ef41Sopenharmony_ci    /*-
23441cb0ef41Sopenharmony_ci     * There are several cases for the session ID to send
23451cb0ef41Sopenharmony_ci     * back in the server hello:
23461cb0ef41Sopenharmony_ci     * - For session reuse from the session cache,
23471cb0ef41Sopenharmony_ci     *   we send back the old session ID.
23481cb0ef41Sopenharmony_ci     * - If stateless session reuse (using a session ticket)
23491cb0ef41Sopenharmony_ci     *   is successful, we send back the client's "session ID"
23501cb0ef41Sopenharmony_ci     *   (which doesn't actually identify the session).
23511cb0ef41Sopenharmony_ci     * - If it is a new session, we send back the new
23521cb0ef41Sopenharmony_ci     *   session ID.
23531cb0ef41Sopenharmony_ci     * - However, if we want the new session to be single-use,
23541cb0ef41Sopenharmony_ci     *   we send back a 0-length session ID.
23551cb0ef41Sopenharmony_ci     * - In TLSv1.3 we echo back the session id sent to us by the client
23561cb0ef41Sopenharmony_ci     *   regardless
23571cb0ef41Sopenharmony_ci     * s->hit is non-zero in either case of session reuse,
23581cb0ef41Sopenharmony_ci     * so the following won't overwrite an ID that we're supposed
23591cb0ef41Sopenharmony_ci     * to send back.
23601cb0ef41Sopenharmony_ci     */
23611cb0ef41Sopenharmony_ci    if (s->session->not_resumable ||
23621cb0ef41Sopenharmony_ci        (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
23631cb0ef41Sopenharmony_ci         && !s->hit))
23641cb0ef41Sopenharmony_ci        s->session->session_id_length = 0;
23651cb0ef41Sopenharmony_ci
23661cb0ef41Sopenharmony_ci    if (usetls13) {
23671cb0ef41Sopenharmony_ci        sl = s->tmp_session_id_len;
23681cb0ef41Sopenharmony_ci        session_id = s->tmp_session_id;
23691cb0ef41Sopenharmony_ci    } else {
23701cb0ef41Sopenharmony_ci        sl = s->session->session_id_length;
23711cb0ef41Sopenharmony_ci        session_id = s->session->session_id;
23721cb0ef41Sopenharmony_ci    }
23731cb0ef41Sopenharmony_ci
23741cb0ef41Sopenharmony_ci    if (sl > sizeof(s->session->session_id)) {
23751cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
23761cb0ef41Sopenharmony_ci        return 0;
23771cb0ef41Sopenharmony_ci    }
23781cb0ef41Sopenharmony_ci
23791cb0ef41Sopenharmony_ci    /* set up the compression method */
23801cb0ef41Sopenharmony_ci#ifdef OPENSSL_NO_COMP
23811cb0ef41Sopenharmony_ci    compm = 0;
23821cb0ef41Sopenharmony_ci#else
23831cb0ef41Sopenharmony_ci    if (usetls13 || s->s3.tmp.new_compression == NULL)
23841cb0ef41Sopenharmony_ci        compm = 0;
23851cb0ef41Sopenharmony_ci    else
23861cb0ef41Sopenharmony_ci        compm = s->s3.tmp.new_compression->id;
23871cb0ef41Sopenharmony_ci#endif
23881cb0ef41Sopenharmony_ci
23891cb0ef41Sopenharmony_ci    if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
23901cb0ef41Sopenharmony_ci            || !s->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt, &len)
23911cb0ef41Sopenharmony_ci            || !WPACKET_put_bytes_u8(pkt, compm)) {
23921cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
23931cb0ef41Sopenharmony_ci        return 0;
23941cb0ef41Sopenharmony_ci    }
23951cb0ef41Sopenharmony_ci
23961cb0ef41Sopenharmony_ci    if (!tls_construct_extensions(s, pkt,
23971cb0ef41Sopenharmony_ci                                  s->hello_retry_request == SSL_HRR_PENDING
23981cb0ef41Sopenharmony_ci                                      ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
23991cb0ef41Sopenharmony_ci                                      : (SSL_IS_TLS13(s)
24001cb0ef41Sopenharmony_ci                                          ? SSL_EXT_TLS1_3_SERVER_HELLO
24011cb0ef41Sopenharmony_ci                                          : SSL_EXT_TLS1_2_SERVER_HELLO),
24021cb0ef41Sopenharmony_ci                                  NULL, 0)) {
24031cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
24041cb0ef41Sopenharmony_ci        return 0;
24051cb0ef41Sopenharmony_ci    }
24061cb0ef41Sopenharmony_ci
24071cb0ef41Sopenharmony_ci    if (s->hello_retry_request == SSL_HRR_PENDING) {
24081cb0ef41Sopenharmony_ci        /* Ditch the session. We'll create a new one next time around */
24091cb0ef41Sopenharmony_ci        SSL_SESSION_free(s->session);
24101cb0ef41Sopenharmony_ci        s->session = NULL;
24111cb0ef41Sopenharmony_ci        s->hit = 0;
24121cb0ef41Sopenharmony_ci
24131cb0ef41Sopenharmony_ci        /*
24141cb0ef41Sopenharmony_ci         * Re-initialise the Transcript Hash. We're going to prepopulate it with
24151cb0ef41Sopenharmony_ci         * a synthetic message_hash in place of ClientHello1.
24161cb0ef41Sopenharmony_ci         */
24171cb0ef41Sopenharmony_ci        if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
24181cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
24191cb0ef41Sopenharmony_ci            return 0;
24201cb0ef41Sopenharmony_ci        }
24211cb0ef41Sopenharmony_ci    } else if (!(s->verify_mode & SSL_VERIFY_PEER)
24221cb0ef41Sopenharmony_ci                && !ssl3_digest_cached_records(s, 0)) {
24231cb0ef41Sopenharmony_ci        /* SSLfatal() already called */;
24241cb0ef41Sopenharmony_ci        return 0;
24251cb0ef41Sopenharmony_ci    }
24261cb0ef41Sopenharmony_ci
24271cb0ef41Sopenharmony_ci    return 1;
24281cb0ef41Sopenharmony_ci}
24291cb0ef41Sopenharmony_ci
24301cb0ef41Sopenharmony_ciint tls_construct_server_done(SSL *s, WPACKET *pkt)
24311cb0ef41Sopenharmony_ci{
24321cb0ef41Sopenharmony_ci    if (!s->s3.tmp.cert_request) {
24331cb0ef41Sopenharmony_ci        if (!ssl3_digest_cached_records(s, 0)) {
24341cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
24351cb0ef41Sopenharmony_ci            return 0;
24361cb0ef41Sopenharmony_ci        }
24371cb0ef41Sopenharmony_ci    }
24381cb0ef41Sopenharmony_ci    return 1;
24391cb0ef41Sopenharmony_ci}
24401cb0ef41Sopenharmony_ci
24411cb0ef41Sopenharmony_ciint tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
24421cb0ef41Sopenharmony_ci{
24431cb0ef41Sopenharmony_ci    EVP_PKEY *pkdh = NULL;
24441cb0ef41Sopenharmony_ci    unsigned char *encodedPoint = NULL;
24451cb0ef41Sopenharmony_ci    size_t encodedlen = 0;
24461cb0ef41Sopenharmony_ci    int curve_id = 0;
24471cb0ef41Sopenharmony_ci    const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;
24481cb0ef41Sopenharmony_ci    int i;
24491cb0ef41Sopenharmony_ci    unsigned long type;
24501cb0ef41Sopenharmony_ci    BIGNUM *r[4];
24511cb0ef41Sopenharmony_ci    EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
24521cb0ef41Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL;
24531cb0ef41Sopenharmony_ci    size_t paramlen, paramoffset;
24541cb0ef41Sopenharmony_ci    int freer = 0, ret = 0;
24551cb0ef41Sopenharmony_ci
24561cb0ef41Sopenharmony_ci    if (!WPACKET_get_total_written(pkt, &paramoffset)) {
24571cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
24581cb0ef41Sopenharmony_ci        goto err;
24591cb0ef41Sopenharmony_ci    }
24601cb0ef41Sopenharmony_ci
24611cb0ef41Sopenharmony_ci    if (md_ctx == NULL) {
24621cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
24631cb0ef41Sopenharmony_ci        goto err;
24641cb0ef41Sopenharmony_ci    }
24651cb0ef41Sopenharmony_ci
24661cb0ef41Sopenharmony_ci    type = s->s3.tmp.new_cipher->algorithm_mkey;
24671cb0ef41Sopenharmony_ci
24681cb0ef41Sopenharmony_ci    r[0] = r[1] = r[2] = r[3] = NULL;
24691cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_PSK
24701cb0ef41Sopenharmony_ci    /* Plain PSK or RSAPSK nothing to do */
24711cb0ef41Sopenharmony_ci    if (type & (SSL_kPSK | SSL_kRSAPSK)) {
24721cb0ef41Sopenharmony_ci    } else
24731cb0ef41Sopenharmony_ci#endif                          /* !OPENSSL_NO_PSK */
24741cb0ef41Sopenharmony_ci    if (type & (SSL_kDHE | SSL_kDHEPSK)) {
24751cb0ef41Sopenharmony_ci        CERT *cert = s->cert;
24761cb0ef41Sopenharmony_ci        EVP_PKEY *pkdhp = NULL;
24771cb0ef41Sopenharmony_ci
24781cb0ef41Sopenharmony_ci        if (s->cert->dh_tmp_auto) {
24791cb0ef41Sopenharmony_ci            pkdh = ssl_get_auto_dh(s);
24801cb0ef41Sopenharmony_ci            if (pkdh == NULL) {
24811cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
24821cb0ef41Sopenharmony_ci                goto err;
24831cb0ef41Sopenharmony_ci            }
24841cb0ef41Sopenharmony_ci            pkdhp = pkdh;
24851cb0ef41Sopenharmony_ci        } else {
24861cb0ef41Sopenharmony_ci            pkdhp = cert->dh_tmp;
24871cb0ef41Sopenharmony_ci        }
24881cb0ef41Sopenharmony_ci#if !defined(OPENSSL_NO_DEPRECATED_3_0)
24891cb0ef41Sopenharmony_ci        if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
24901cb0ef41Sopenharmony_ci            pkdh = ssl_dh_to_pkey(s->cert->dh_tmp_cb(s, 0, 1024));
24911cb0ef41Sopenharmony_ci            if (pkdh == NULL) {
24921cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
24931cb0ef41Sopenharmony_ci                goto err;
24941cb0ef41Sopenharmony_ci            }
24951cb0ef41Sopenharmony_ci            pkdhp = pkdh;
24961cb0ef41Sopenharmony_ci        }
24971cb0ef41Sopenharmony_ci#endif
24981cb0ef41Sopenharmony_ci        if (pkdhp == NULL) {
24991cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
25001cb0ef41Sopenharmony_ci            goto err;
25011cb0ef41Sopenharmony_ci        }
25021cb0ef41Sopenharmony_ci        if (!ssl_security(s, SSL_SECOP_TMP_DH,
25031cb0ef41Sopenharmony_ci                          EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) {
25041cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
25051cb0ef41Sopenharmony_ci            goto err;
25061cb0ef41Sopenharmony_ci        }
25071cb0ef41Sopenharmony_ci        if (s->s3.tmp.pkey != NULL) {
25081cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
25091cb0ef41Sopenharmony_ci            goto err;
25101cb0ef41Sopenharmony_ci        }
25111cb0ef41Sopenharmony_ci
25121cb0ef41Sopenharmony_ci        s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp);
25131cb0ef41Sopenharmony_ci        if (s->s3.tmp.pkey == NULL) {
25141cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
25151cb0ef41Sopenharmony_ci            goto err;
25161cb0ef41Sopenharmony_ci        }
25171cb0ef41Sopenharmony_ci
25181cb0ef41Sopenharmony_ci        EVP_PKEY_free(pkdh);
25191cb0ef41Sopenharmony_ci        pkdh = NULL;
25201cb0ef41Sopenharmony_ci
25211cb0ef41Sopenharmony_ci        /* These BIGNUMs need to be freed when we're finished */
25221cb0ef41Sopenharmony_ci        freer = 1;
25231cb0ef41Sopenharmony_ci        if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P,
25241cb0ef41Sopenharmony_ci                                   &r[0])
25251cb0ef41Sopenharmony_ci                || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G,
25261cb0ef41Sopenharmony_ci                                          &r[1])
25271cb0ef41Sopenharmony_ci                || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey,
25281cb0ef41Sopenharmony_ci                                          OSSL_PKEY_PARAM_PUB_KEY, &r[2])) {
25291cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
25301cb0ef41Sopenharmony_ci            goto err;
25311cb0ef41Sopenharmony_ci        }
25321cb0ef41Sopenharmony_ci    } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
25331cb0ef41Sopenharmony_ci
25341cb0ef41Sopenharmony_ci        if (s->s3.tmp.pkey != NULL) {
25351cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
25361cb0ef41Sopenharmony_ci            goto err;
25371cb0ef41Sopenharmony_ci        }
25381cb0ef41Sopenharmony_ci
25391cb0ef41Sopenharmony_ci        /* Get NID of appropriate shared curve */
25401cb0ef41Sopenharmony_ci        curve_id = tls1_shared_group(s, -2);
25411cb0ef41Sopenharmony_ci        if (curve_id == 0) {
25421cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
25431cb0ef41Sopenharmony_ci                     SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
25441cb0ef41Sopenharmony_ci            goto err;
25451cb0ef41Sopenharmony_ci        }
25461cb0ef41Sopenharmony_ci        /* Cache the group used in the SSL_SESSION */
25471cb0ef41Sopenharmony_ci        s->session->kex_group = curve_id;
25481cb0ef41Sopenharmony_ci        /* Generate a new key for this curve */
25491cb0ef41Sopenharmony_ci        s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id);
25501cb0ef41Sopenharmony_ci        if (s->s3.tmp.pkey == NULL) {
25511cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
25521cb0ef41Sopenharmony_ci            goto err;
25531cb0ef41Sopenharmony_ci        }
25541cb0ef41Sopenharmony_ci
25551cb0ef41Sopenharmony_ci        /* Encode the public key. */
25561cb0ef41Sopenharmony_ci        encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey,
25571cb0ef41Sopenharmony_ci                                                      &encodedPoint);
25581cb0ef41Sopenharmony_ci        if (encodedlen == 0) {
25591cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
25601cb0ef41Sopenharmony_ci            goto err;
25611cb0ef41Sopenharmony_ci        }
25621cb0ef41Sopenharmony_ci
25631cb0ef41Sopenharmony_ci        /*
25641cb0ef41Sopenharmony_ci         * We'll generate the serverKeyExchange message explicitly so we
25651cb0ef41Sopenharmony_ci         * can set these to NULLs
25661cb0ef41Sopenharmony_ci         */
25671cb0ef41Sopenharmony_ci        r[0] = NULL;
25681cb0ef41Sopenharmony_ci        r[1] = NULL;
25691cb0ef41Sopenharmony_ci        r[2] = NULL;
25701cb0ef41Sopenharmony_ci        r[3] = NULL;
25711cb0ef41Sopenharmony_ci    } else
25721cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SRP
25731cb0ef41Sopenharmony_ci    if (type & SSL_kSRP) {
25741cb0ef41Sopenharmony_ci        if ((s->srp_ctx.N == NULL) ||
25751cb0ef41Sopenharmony_ci            (s->srp_ctx.g == NULL) ||
25761cb0ef41Sopenharmony_ci            (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
25771cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM);
25781cb0ef41Sopenharmony_ci            goto err;
25791cb0ef41Sopenharmony_ci        }
25801cb0ef41Sopenharmony_ci        r[0] = s->srp_ctx.N;
25811cb0ef41Sopenharmony_ci        r[1] = s->srp_ctx.g;
25821cb0ef41Sopenharmony_ci        r[2] = s->srp_ctx.s;
25831cb0ef41Sopenharmony_ci        r[3] = s->srp_ctx.B;
25841cb0ef41Sopenharmony_ci    } else
25851cb0ef41Sopenharmony_ci#endif
25861cb0ef41Sopenharmony_ci    {
25871cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
25881cb0ef41Sopenharmony_ci        goto err;
25891cb0ef41Sopenharmony_ci    }
25901cb0ef41Sopenharmony_ci
25911cb0ef41Sopenharmony_ci    if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
25921cb0ef41Sopenharmony_ci        || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
25931cb0ef41Sopenharmony_ci        lu = NULL;
25941cb0ef41Sopenharmony_ci    } else if (lu == NULL) {
25951cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
25961cb0ef41Sopenharmony_ci        goto err;
25971cb0ef41Sopenharmony_ci    }
25981cb0ef41Sopenharmony_ci
25991cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_PSK
26001cb0ef41Sopenharmony_ci    if (type & SSL_PSK) {
26011cb0ef41Sopenharmony_ci        size_t len = (s->cert->psk_identity_hint == NULL)
26021cb0ef41Sopenharmony_ci                        ? 0 : strlen(s->cert->psk_identity_hint);
26031cb0ef41Sopenharmony_ci
26041cb0ef41Sopenharmony_ci        /*
26051cb0ef41Sopenharmony_ci         * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
26061cb0ef41Sopenharmony_ci         * checked this when we set the identity hint - but just in case
26071cb0ef41Sopenharmony_ci         */
26081cb0ef41Sopenharmony_ci        if (len > PSK_MAX_IDENTITY_LEN
26091cb0ef41Sopenharmony_ci                || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
26101cb0ef41Sopenharmony_ci                                           len)) {
26111cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
26121cb0ef41Sopenharmony_ci            goto err;
26131cb0ef41Sopenharmony_ci        }
26141cb0ef41Sopenharmony_ci    }
26151cb0ef41Sopenharmony_ci#endif
26161cb0ef41Sopenharmony_ci
26171cb0ef41Sopenharmony_ci    for (i = 0; i < 4 && r[i] != NULL; i++) {
26181cb0ef41Sopenharmony_ci        unsigned char *binval;
26191cb0ef41Sopenharmony_ci        int res;
26201cb0ef41Sopenharmony_ci
26211cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SRP
26221cb0ef41Sopenharmony_ci        if ((i == 2) && (type & SSL_kSRP)) {
26231cb0ef41Sopenharmony_ci            res = WPACKET_start_sub_packet_u8(pkt);
26241cb0ef41Sopenharmony_ci        } else
26251cb0ef41Sopenharmony_ci#endif
26261cb0ef41Sopenharmony_ci            res = WPACKET_start_sub_packet_u16(pkt);
26271cb0ef41Sopenharmony_ci
26281cb0ef41Sopenharmony_ci        if (!res) {
26291cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
26301cb0ef41Sopenharmony_ci            goto err;
26311cb0ef41Sopenharmony_ci        }
26321cb0ef41Sopenharmony_ci
26331cb0ef41Sopenharmony_ci        /*-
26341cb0ef41Sopenharmony_ci         * for interoperability with some versions of the Microsoft TLS
26351cb0ef41Sopenharmony_ci         * stack, we need to zero pad the DHE pub key to the same length
26361cb0ef41Sopenharmony_ci         * as the prime
26371cb0ef41Sopenharmony_ci         */
26381cb0ef41Sopenharmony_ci        if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
26391cb0ef41Sopenharmony_ci            size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
26401cb0ef41Sopenharmony_ci
26411cb0ef41Sopenharmony_ci            if (len > 0) {
26421cb0ef41Sopenharmony_ci                if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
26431cb0ef41Sopenharmony_ci                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
26441cb0ef41Sopenharmony_ci                    goto err;
26451cb0ef41Sopenharmony_ci                }
26461cb0ef41Sopenharmony_ci                memset(binval, 0, len);
26471cb0ef41Sopenharmony_ci            }
26481cb0ef41Sopenharmony_ci        }
26491cb0ef41Sopenharmony_ci
26501cb0ef41Sopenharmony_ci        if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
26511cb0ef41Sopenharmony_ci                || !WPACKET_close(pkt)) {
26521cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
26531cb0ef41Sopenharmony_ci            goto err;
26541cb0ef41Sopenharmony_ci        }
26551cb0ef41Sopenharmony_ci
26561cb0ef41Sopenharmony_ci        BN_bn2bin(r[i], binval);
26571cb0ef41Sopenharmony_ci    }
26581cb0ef41Sopenharmony_ci
26591cb0ef41Sopenharmony_ci    if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
26601cb0ef41Sopenharmony_ci        /*
26611cb0ef41Sopenharmony_ci         * We only support named (not generic) curves. In this situation, the
26621cb0ef41Sopenharmony_ci         * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
26631cb0ef41Sopenharmony_ci         * [1 byte length of encoded point], followed by the actual encoded
26641cb0ef41Sopenharmony_ci         * point itself
26651cb0ef41Sopenharmony_ci         */
26661cb0ef41Sopenharmony_ci        if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
26671cb0ef41Sopenharmony_ci                || !WPACKET_put_bytes_u8(pkt, 0)
26681cb0ef41Sopenharmony_ci                || !WPACKET_put_bytes_u8(pkt, curve_id)
26691cb0ef41Sopenharmony_ci                || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
26701cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
26711cb0ef41Sopenharmony_ci            goto err;
26721cb0ef41Sopenharmony_ci        }
26731cb0ef41Sopenharmony_ci        OPENSSL_free(encodedPoint);
26741cb0ef41Sopenharmony_ci        encodedPoint = NULL;
26751cb0ef41Sopenharmony_ci    }
26761cb0ef41Sopenharmony_ci
26771cb0ef41Sopenharmony_ci    /* not anonymous */
26781cb0ef41Sopenharmony_ci    if (lu != NULL) {
26791cb0ef41Sopenharmony_ci        EVP_PKEY *pkey = s->s3.tmp.cert->privatekey;
26801cb0ef41Sopenharmony_ci        const EVP_MD *md;
26811cb0ef41Sopenharmony_ci        unsigned char *sigbytes1, *sigbytes2, *tbs;
26821cb0ef41Sopenharmony_ci        size_t siglen = 0, tbslen;
26831cb0ef41Sopenharmony_ci
26841cb0ef41Sopenharmony_ci        if (pkey == NULL || !tls1_lookup_md(s->ctx, lu, &md)) {
26851cb0ef41Sopenharmony_ci            /* Should never happen */
26861cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
26871cb0ef41Sopenharmony_ci            goto err;
26881cb0ef41Sopenharmony_ci        }
26891cb0ef41Sopenharmony_ci        /* Get length of the parameters we have written above */
26901cb0ef41Sopenharmony_ci        if (!WPACKET_get_length(pkt, &paramlen)) {
26911cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
26921cb0ef41Sopenharmony_ci            goto err;
26931cb0ef41Sopenharmony_ci        }
26941cb0ef41Sopenharmony_ci        /* send signature algorithm */
26951cb0ef41Sopenharmony_ci        if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
26961cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
26971cb0ef41Sopenharmony_ci            goto err;
26981cb0ef41Sopenharmony_ci        }
26991cb0ef41Sopenharmony_ci
27001cb0ef41Sopenharmony_ci        if (EVP_DigestSignInit_ex(md_ctx, &pctx,
27011cb0ef41Sopenharmony_ci                                  md == NULL ? NULL : EVP_MD_get0_name(md),
27021cb0ef41Sopenharmony_ci                                  s->ctx->libctx, s->ctx->propq, pkey,
27031cb0ef41Sopenharmony_ci                                  NULL) <= 0) {
27041cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
27051cb0ef41Sopenharmony_ci            goto err;
27061cb0ef41Sopenharmony_ci        }
27071cb0ef41Sopenharmony_ci        if (lu->sig == EVP_PKEY_RSA_PSS) {
27081cb0ef41Sopenharmony_ci            if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
27091cb0ef41Sopenharmony_ci                || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
27101cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
27111cb0ef41Sopenharmony_ci                goto err;
27121cb0ef41Sopenharmony_ci            }
27131cb0ef41Sopenharmony_ci        }
27141cb0ef41Sopenharmony_ci        tbslen = construct_key_exchange_tbs(s, &tbs,
27151cb0ef41Sopenharmony_ci                                            s->init_buf->data + paramoffset,
27161cb0ef41Sopenharmony_ci                                            paramlen);
27171cb0ef41Sopenharmony_ci        if (tbslen == 0) {
27181cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
27191cb0ef41Sopenharmony_ci            goto err;
27201cb0ef41Sopenharmony_ci        }
27211cb0ef41Sopenharmony_ci
27221cb0ef41Sopenharmony_ci        if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <=0
27231cb0ef41Sopenharmony_ci                || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
27241cb0ef41Sopenharmony_ci                || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0
27251cb0ef41Sopenharmony_ci                || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
27261cb0ef41Sopenharmony_ci                || sigbytes1 != sigbytes2) {
27271cb0ef41Sopenharmony_ci            OPENSSL_free(tbs);
27281cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
27291cb0ef41Sopenharmony_ci            goto err;
27301cb0ef41Sopenharmony_ci        }
27311cb0ef41Sopenharmony_ci        OPENSSL_free(tbs);
27321cb0ef41Sopenharmony_ci    }
27331cb0ef41Sopenharmony_ci
27341cb0ef41Sopenharmony_ci    ret = 1;
27351cb0ef41Sopenharmony_ci err:
27361cb0ef41Sopenharmony_ci    EVP_PKEY_free(pkdh);
27371cb0ef41Sopenharmony_ci    OPENSSL_free(encodedPoint);
27381cb0ef41Sopenharmony_ci    EVP_MD_CTX_free(md_ctx);
27391cb0ef41Sopenharmony_ci    if (freer) {
27401cb0ef41Sopenharmony_ci        BN_free(r[0]);
27411cb0ef41Sopenharmony_ci        BN_free(r[1]);
27421cb0ef41Sopenharmony_ci        BN_free(r[2]);
27431cb0ef41Sopenharmony_ci        BN_free(r[3]);
27441cb0ef41Sopenharmony_ci    }
27451cb0ef41Sopenharmony_ci    return ret;
27461cb0ef41Sopenharmony_ci}
27471cb0ef41Sopenharmony_ci
27481cb0ef41Sopenharmony_ciint tls_construct_certificate_request(SSL *s, WPACKET *pkt)
27491cb0ef41Sopenharmony_ci{
27501cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
27511cb0ef41Sopenharmony_ci        /* Send random context when doing post-handshake auth */
27521cb0ef41Sopenharmony_ci        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
27531cb0ef41Sopenharmony_ci            OPENSSL_free(s->pha_context);
27541cb0ef41Sopenharmony_ci            s->pha_context_len = 32;
27551cb0ef41Sopenharmony_ci            if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) {
27561cb0ef41Sopenharmony_ci                s->pha_context_len = 0;
27571cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
27581cb0ef41Sopenharmony_ci                return 0;
27591cb0ef41Sopenharmony_ci            }
27601cb0ef41Sopenharmony_ci            if (RAND_bytes_ex(s->ctx->libctx, s->pha_context,
27611cb0ef41Sopenharmony_ci                                     s->pha_context_len, 0) <= 0
27621cb0ef41Sopenharmony_ci                    || !WPACKET_sub_memcpy_u8(pkt, s->pha_context,
27631cb0ef41Sopenharmony_ci                                              s->pha_context_len)) {
27641cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
27651cb0ef41Sopenharmony_ci                return 0;
27661cb0ef41Sopenharmony_ci            }
27671cb0ef41Sopenharmony_ci            /* reset the handshake hash back to just after the ClientFinished */
27681cb0ef41Sopenharmony_ci            if (!tls13_restore_handshake_digest_for_pha(s)) {
27691cb0ef41Sopenharmony_ci                /* SSLfatal() already called */
27701cb0ef41Sopenharmony_ci                return 0;
27711cb0ef41Sopenharmony_ci            }
27721cb0ef41Sopenharmony_ci        } else {
27731cb0ef41Sopenharmony_ci            if (!WPACKET_put_bytes_u8(pkt, 0)) {
27741cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
27751cb0ef41Sopenharmony_ci                return 0;
27761cb0ef41Sopenharmony_ci            }
27771cb0ef41Sopenharmony_ci        }
27781cb0ef41Sopenharmony_ci
27791cb0ef41Sopenharmony_ci        if (!tls_construct_extensions(s, pkt,
27801cb0ef41Sopenharmony_ci                                      SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
27811cb0ef41Sopenharmony_ci                                      0)) {
27821cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
27831cb0ef41Sopenharmony_ci            return 0;
27841cb0ef41Sopenharmony_ci        }
27851cb0ef41Sopenharmony_ci        goto done;
27861cb0ef41Sopenharmony_ci    }
27871cb0ef41Sopenharmony_ci
27881cb0ef41Sopenharmony_ci    /* get the list of acceptable cert types */
27891cb0ef41Sopenharmony_ci    if (!WPACKET_start_sub_packet_u8(pkt)
27901cb0ef41Sopenharmony_ci        || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
27911cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
27921cb0ef41Sopenharmony_ci        return 0;
27931cb0ef41Sopenharmony_ci    }
27941cb0ef41Sopenharmony_ci
27951cb0ef41Sopenharmony_ci    if (SSL_USE_SIGALGS(s)) {
27961cb0ef41Sopenharmony_ci        const uint16_t *psigs;
27971cb0ef41Sopenharmony_ci        size_t nl = tls12_get_psigalgs(s, 1, &psigs);
27981cb0ef41Sopenharmony_ci
27991cb0ef41Sopenharmony_ci        if (!WPACKET_start_sub_packet_u16(pkt)
28001cb0ef41Sopenharmony_ci                || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
28011cb0ef41Sopenharmony_ci                || !tls12_copy_sigalgs(s, pkt, psigs, nl)
28021cb0ef41Sopenharmony_ci                || !WPACKET_close(pkt)) {
28031cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
28041cb0ef41Sopenharmony_ci            return 0;
28051cb0ef41Sopenharmony_ci        }
28061cb0ef41Sopenharmony_ci    }
28071cb0ef41Sopenharmony_ci
28081cb0ef41Sopenharmony_ci    if (!construct_ca_names(s, get_ca_names(s), pkt)) {
28091cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
28101cb0ef41Sopenharmony_ci        return 0;
28111cb0ef41Sopenharmony_ci    }
28121cb0ef41Sopenharmony_ci
28131cb0ef41Sopenharmony_ci done:
28141cb0ef41Sopenharmony_ci    s->certreqs_sent++;
28151cb0ef41Sopenharmony_ci    s->s3.tmp.cert_request = 1;
28161cb0ef41Sopenharmony_ci    return 1;
28171cb0ef41Sopenharmony_ci}
28181cb0ef41Sopenharmony_ci
28191cb0ef41Sopenharmony_cistatic int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt)
28201cb0ef41Sopenharmony_ci{
28211cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_PSK
28221cb0ef41Sopenharmony_ci    unsigned char psk[PSK_MAX_PSK_LEN];
28231cb0ef41Sopenharmony_ci    size_t psklen;
28241cb0ef41Sopenharmony_ci    PACKET psk_identity;
28251cb0ef41Sopenharmony_ci
28261cb0ef41Sopenharmony_ci    if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
28271cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
28281cb0ef41Sopenharmony_ci        return 0;
28291cb0ef41Sopenharmony_ci    }
28301cb0ef41Sopenharmony_ci    if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
28311cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG);
28321cb0ef41Sopenharmony_ci        return 0;
28331cb0ef41Sopenharmony_ci    }
28341cb0ef41Sopenharmony_ci    if (s->psk_server_callback == NULL) {
28351cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB);
28361cb0ef41Sopenharmony_ci        return 0;
28371cb0ef41Sopenharmony_ci    }
28381cb0ef41Sopenharmony_ci
28391cb0ef41Sopenharmony_ci    if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
28401cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
28411cb0ef41Sopenharmony_ci        return 0;
28421cb0ef41Sopenharmony_ci    }
28431cb0ef41Sopenharmony_ci
28441cb0ef41Sopenharmony_ci    psklen = s->psk_server_callback(s, s->session->psk_identity,
28451cb0ef41Sopenharmony_ci                                    psk, sizeof(psk));
28461cb0ef41Sopenharmony_ci
28471cb0ef41Sopenharmony_ci    if (psklen > PSK_MAX_PSK_LEN) {
28481cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
28491cb0ef41Sopenharmony_ci        return 0;
28501cb0ef41Sopenharmony_ci    } else if (psklen == 0) {
28511cb0ef41Sopenharmony_ci        /*
28521cb0ef41Sopenharmony_ci         * PSK related to the given identity not found
28531cb0ef41Sopenharmony_ci         */
28541cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND);
28551cb0ef41Sopenharmony_ci        return 0;
28561cb0ef41Sopenharmony_ci    }
28571cb0ef41Sopenharmony_ci
28581cb0ef41Sopenharmony_ci    OPENSSL_free(s->s3.tmp.psk);
28591cb0ef41Sopenharmony_ci    s->s3.tmp.psk = OPENSSL_memdup(psk, psklen);
28601cb0ef41Sopenharmony_ci    OPENSSL_cleanse(psk, psklen);
28611cb0ef41Sopenharmony_ci
28621cb0ef41Sopenharmony_ci    if (s->s3.tmp.psk == NULL) {
28631cb0ef41Sopenharmony_ci        s->s3.tmp.psklen = 0;
28641cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
28651cb0ef41Sopenharmony_ci        return 0;
28661cb0ef41Sopenharmony_ci    }
28671cb0ef41Sopenharmony_ci
28681cb0ef41Sopenharmony_ci    s->s3.tmp.psklen = psklen;
28691cb0ef41Sopenharmony_ci
28701cb0ef41Sopenharmony_ci    return 1;
28711cb0ef41Sopenharmony_ci#else
28721cb0ef41Sopenharmony_ci    /* Should never happen */
28731cb0ef41Sopenharmony_ci    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
28741cb0ef41Sopenharmony_ci    return 0;
28751cb0ef41Sopenharmony_ci#endif
28761cb0ef41Sopenharmony_ci}
28771cb0ef41Sopenharmony_ci
28781cb0ef41Sopenharmony_cistatic int tls_process_cke_rsa(SSL *s, PACKET *pkt)
28791cb0ef41Sopenharmony_ci{
28801cb0ef41Sopenharmony_ci    size_t outlen;
28811cb0ef41Sopenharmony_ci    PACKET enc_premaster;
28821cb0ef41Sopenharmony_ci    EVP_PKEY *rsa = NULL;
28831cb0ef41Sopenharmony_ci    unsigned char *rsa_decrypt = NULL;
28841cb0ef41Sopenharmony_ci    int ret = 0;
28851cb0ef41Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
28861cb0ef41Sopenharmony_ci    OSSL_PARAM params[3], *p = params;
28871cb0ef41Sopenharmony_ci
28881cb0ef41Sopenharmony_ci    rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey;
28891cb0ef41Sopenharmony_ci    if (rsa == NULL) {
28901cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE);
28911cb0ef41Sopenharmony_ci        return 0;
28921cb0ef41Sopenharmony_ci    }
28931cb0ef41Sopenharmony_ci
28941cb0ef41Sopenharmony_ci    /* SSLv3 and pre-standard DTLS omit the length bytes. */
28951cb0ef41Sopenharmony_ci    if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
28961cb0ef41Sopenharmony_ci        enc_premaster = *pkt;
28971cb0ef41Sopenharmony_ci    } else {
28981cb0ef41Sopenharmony_ci        if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
28991cb0ef41Sopenharmony_ci            || PACKET_remaining(pkt) != 0) {
29001cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
29011cb0ef41Sopenharmony_ci            return 0;
29021cb0ef41Sopenharmony_ci        }
29031cb0ef41Sopenharmony_ci    }
29041cb0ef41Sopenharmony_ci
29051cb0ef41Sopenharmony_ci    outlen = SSL_MAX_MASTER_KEY_LENGTH;
29061cb0ef41Sopenharmony_ci    rsa_decrypt = OPENSSL_malloc(outlen);
29071cb0ef41Sopenharmony_ci    if (rsa_decrypt == NULL) {
29081cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
29091cb0ef41Sopenharmony_ci        return 0;
29101cb0ef41Sopenharmony_ci    }
29111cb0ef41Sopenharmony_ci
29121cb0ef41Sopenharmony_ci    ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, rsa, s->ctx->propq);
29131cb0ef41Sopenharmony_ci    if (ctx == NULL) {
29141cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
29151cb0ef41Sopenharmony_ci        goto err;
29161cb0ef41Sopenharmony_ci    }
29171cb0ef41Sopenharmony_ci
29181cb0ef41Sopenharmony_ci    /*
29191cb0ef41Sopenharmony_ci     * We must not leak whether a decryption failure occurs because of
29201cb0ef41Sopenharmony_ci     * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
29211cb0ef41Sopenharmony_ci     * section 7.4.7.1). We use the special padding type
29221cb0ef41Sopenharmony_ci     * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automaticaly decrypt the
29231cb0ef41Sopenharmony_ci     * RSA, check the padding and check that the client version is as expected
29241cb0ef41Sopenharmony_ci     * in the premaster secret. If any of that fails then the function appears
29251cb0ef41Sopenharmony_ci     * to return successfully but with a random result. The call below could
29261cb0ef41Sopenharmony_ci     * still fail if the input is publicly invalid.
29271cb0ef41Sopenharmony_ci     * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
29281cb0ef41Sopenharmony_ci     */
29291cb0ef41Sopenharmony_ci    if (EVP_PKEY_decrypt_init(ctx) <= 0
29301cb0ef41Sopenharmony_ci            || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) {
29311cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
29321cb0ef41Sopenharmony_ci        goto err;
29331cb0ef41Sopenharmony_ci    }
29341cb0ef41Sopenharmony_ci
29351cb0ef41Sopenharmony_ci    *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION,
29361cb0ef41Sopenharmony_ci                                     (unsigned int *)&s->client_version);
29371cb0ef41Sopenharmony_ci   if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0)
29381cb0ef41Sopenharmony_ci        *p++ = OSSL_PARAM_construct_uint(
29391cb0ef41Sopenharmony_ci            OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION,
29401cb0ef41Sopenharmony_ci            (unsigned int *)&s->version);
29411cb0ef41Sopenharmony_ci    *p++ = OSSL_PARAM_construct_end();
29421cb0ef41Sopenharmony_ci
29431cb0ef41Sopenharmony_ci    if (!EVP_PKEY_CTX_set_params(ctx, params)
29441cb0ef41Sopenharmony_ci            || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen,
29451cb0ef41Sopenharmony_ci                                PACKET_data(&enc_premaster),
29461cb0ef41Sopenharmony_ci                                PACKET_remaining(&enc_premaster)) <= 0) {
29471cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
29481cb0ef41Sopenharmony_ci        goto err;
29491cb0ef41Sopenharmony_ci    }
29501cb0ef41Sopenharmony_ci
29511cb0ef41Sopenharmony_ci    /*
29521cb0ef41Sopenharmony_ci     * This test should never fail (otherwise we should have failed above) but
29531cb0ef41Sopenharmony_ci     * we double check anyway.
29541cb0ef41Sopenharmony_ci     */
29551cb0ef41Sopenharmony_ci    if (outlen != SSL_MAX_MASTER_KEY_LENGTH) {
29561cb0ef41Sopenharmony_ci        OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH);
29571cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
29581cb0ef41Sopenharmony_ci        goto err;
29591cb0ef41Sopenharmony_ci    }
29601cb0ef41Sopenharmony_ci
29611cb0ef41Sopenharmony_ci    /* Also cleanses rsa_decrypt (on success or failure) */
29621cb0ef41Sopenharmony_ci    if (!ssl_generate_master_secret(s, rsa_decrypt,
29631cb0ef41Sopenharmony_ci                                    SSL_MAX_MASTER_KEY_LENGTH, 0)) {
29641cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
29651cb0ef41Sopenharmony_ci        goto err;
29661cb0ef41Sopenharmony_ci    }
29671cb0ef41Sopenharmony_ci
29681cb0ef41Sopenharmony_ci    ret = 1;
29691cb0ef41Sopenharmony_ci err:
29701cb0ef41Sopenharmony_ci    OPENSSL_free(rsa_decrypt);
29711cb0ef41Sopenharmony_ci    EVP_PKEY_CTX_free(ctx);
29721cb0ef41Sopenharmony_ci    return ret;
29731cb0ef41Sopenharmony_ci}
29741cb0ef41Sopenharmony_ci
29751cb0ef41Sopenharmony_cistatic int tls_process_cke_dhe(SSL *s, PACKET *pkt)
29761cb0ef41Sopenharmony_ci{
29771cb0ef41Sopenharmony_ci    EVP_PKEY *skey = NULL;
29781cb0ef41Sopenharmony_ci    unsigned int i;
29791cb0ef41Sopenharmony_ci    const unsigned char *data;
29801cb0ef41Sopenharmony_ci    EVP_PKEY *ckey = NULL;
29811cb0ef41Sopenharmony_ci    int ret = 0;
29821cb0ef41Sopenharmony_ci
29831cb0ef41Sopenharmony_ci    if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
29841cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
29851cb0ef41Sopenharmony_ci        goto err;
29861cb0ef41Sopenharmony_ci    }
29871cb0ef41Sopenharmony_ci    skey = s->s3.tmp.pkey;
29881cb0ef41Sopenharmony_ci    if (skey == NULL) {
29891cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
29901cb0ef41Sopenharmony_ci        goto err;
29911cb0ef41Sopenharmony_ci    }
29921cb0ef41Sopenharmony_ci
29931cb0ef41Sopenharmony_ci    if (PACKET_remaining(pkt) == 0L) {
29941cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY);
29951cb0ef41Sopenharmony_ci        goto err;
29961cb0ef41Sopenharmony_ci    }
29971cb0ef41Sopenharmony_ci    if (!PACKET_get_bytes(pkt, &data, i)) {
29981cb0ef41Sopenharmony_ci        /* We already checked we have enough data */
29991cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
30001cb0ef41Sopenharmony_ci        goto err;
30011cb0ef41Sopenharmony_ci    }
30021cb0ef41Sopenharmony_ci    ckey = EVP_PKEY_new();
30031cb0ef41Sopenharmony_ci    if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
30041cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
30051cb0ef41Sopenharmony_ci        goto err;
30061cb0ef41Sopenharmony_ci    }
30071cb0ef41Sopenharmony_ci
30081cb0ef41Sopenharmony_ci    if (!EVP_PKEY_set1_encoded_public_key(ckey, data, i)) {
30091cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
30101cb0ef41Sopenharmony_ci        goto err;
30111cb0ef41Sopenharmony_ci    }
30121cb0ef41Sopenharmony_ci
30131cb0ef41Sopenharmony_ci    if (ssl_derive(s, skey, ckey, 1) == 0) {
30141cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
30151cb0ef41Sopenharmony_ci        goto err;
30161cb0ef41Sopenharmony_ci    }
30171cb0ef41Sopenharmony_ci
30181cb0ef41Sopenharmony_ci    ret = 1;
30191cb0ef41Sopenharmony_ci    EVP_PKEY_free(s->s3.tmp.pkey);
30201cb0ef41Sopenharmony_ci    s->s3.tmp.pkey = NULL;
30211cb0ef41Sopenharmony_ci err:
30221cb0ef41Sopenharmony_ci    EVP_PKEY_free(ckey);
30231cb0ef41Sopenharmony_ci    return ret;
30241cb0ef41Sopenharmony_ci}
30251cb0ef41Sopenharmony_ci
30261cb0ef41Sopenharmony_cistatic int tls_process_cke_ecdhe(SSL *s, PACKET *pkt)
30271cb0ef41Sopenharmony_ci{
30281cb0ef41Sopenharmony_ci    EVP_PKEY *skey = s->s3.tmp.pkey;
30291cb0ef41Sopenharmony_ci    EVP_PKEY *ckey = NULL;
30301cb0ef41Sopenharmony_ci    int ret = 0;
30311cb0ef41Sopenharmony_ci
30321cb0ef41Sopenharmony_ci    if (PACKET_remaining(pkt) == 0L) {
30331cb0ef41Sopenharmony_ci        /* We don't support ECDH client auth */
30341cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY);
30351cb0ef41Sopenharmony_ci        goto err;
30361cb0ef41Sopenharmony_ci    } else {
30371cb0ef41Sopenharmony_ci        unsigned int i;
30381cb0ef41Sopenharmony_ci        const unsigned char *data;
30391cb0ef41Sopenharmony_ci
30401cb0ef41Sopenharmony_ci        /*
30411cb0ef41Sopenharmony_ci         * Get client's public key from encoded point in the
30421cb0ef41Sopenharmony_ci         * ClientKeyExchange message.
30431cb0ef41Sopenharmony_ci         */
30441cb0ef41Sopenharmony_ci
30451cb0ef41Sopenharmony_ci        /* Get encoded point length */
30461cb0ef41Sopenharmony_ci        if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
30471cb0ef41Sopenharmony_ci            || PACKET_remaining(pkt) != 0) {
30481cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
30491cb0ef41Sopenharmony_ci            goto err;
30501cb0ef41Sopenharmony_ci        }
30511cb0ef41Sopenharmony_ci        if (skey == NULL) {
30521cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY);
30531cb0ef41Sopenharmony_ci            goto err;
30541cb0ef41Sopenharmony_ci        }
30551cb0ef41Sopenharmony_ci
30561cb0ef41Sopenharmony_ci        ckey = EVP_PKEY_new();
30571cb0ef41Sopenharmony_ci        if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
30581cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
30591cb0ef41Sopenharmony_ci            goto err;
30601cb0ef41Sopenharmony_ci        }
30611cb0ef41Sopenharmony_ci
30621cb0ef41Sopenharmony_ci        if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) {
30631cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
30641cb0ef41Sopenharmony_ci            goto err;
30651cb0ef41Sopenharmony_ci        }
30661cb0ef41Sopenharmony_ci    }
30671cb0ef41Sopenharmony_ci
30681cb0ef41Sopenharmony_ci    if (ssl_derive(s, skey, ckey, 1) == 0) {
30691cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
30701cb0ef41Sopenharmony_ci        goto err;
30711cb0ef41Sopenharmony_ci    }
30721cb0ef41Sopenharmony_ci
30731cb0ef41Sopenharmony_ci    ret = 1;
30741cb0ef41Sopenharmony_ci    EVP_PKEY_free(s->s3.tmp.pkey);
30751cb0ef41Sopenharmony_ci    s->s3.tmp.pkey = NULL;
30761cb0ef41Sopenharmony_ci err:
30771cb0ef41Sopenharmony_ci    EVP_PKEY_free(ckey);
30781cb0ef41Sopenharmony_ci
30791cb0ef41Sopenharmony_ci    return ret;
30801cb0ef41Sopenharmony_ci}
30811cb0ef41Sopenharmony_ci
30821cb0ef41Sopenharmony_cistatic int tls_process_cke_srp(SSL *s, PACKET *pkt)
30831cb0ef41Sopenharmony_ci{
30841cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SRP
30851cb0ef41Sopenharmony_ci    unsigned int i;
30861cb0ef41Sopenharmony_ci    const unsigned char *data;
30871cb0ef41Sopenharmony_ci
30881cb0ef41Sopenharmony_ci    if (!PACKET_get_net_2(pkt, &i)
30891cb0ef41Sopenharmony_ci        || !PACKET_get_bytes(pkt, &data, i)) {
30901cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH);
30911cb0ef41Sopenharmony_ci        return 0;
30921cb0ef41Sopenharmony_ci    }
30931cb0ef41Sopenharmony_ci    if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
30941cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
30951cb0ef41Sopenharmony_ci        return 0;
30961cb0ef41Sopenharmony_ci    }
30971cb0ef41Sopenharmony_ci    if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
30981cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS);
30991cb0ef41Sopenharmony_ci        return 0;
31001cb0ef41Sopenharmony_ci    }
31011cb0ef41Sopenharmony_ci    OPENSSL_free(s->session->srp_username);
31021cb0ef41Sopenharmony_ci    s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
31031cb0ef41Sopenharmony_ci    if (s->session->srp_username == NULL) {
31041cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
31051cb0ef41Sopenharmony_ci        return 0;
31061cb0ef41Sopenharmony_ci    }
31071cb0ef41Sopenharmony_ci
31081cb0ef41Sopenharmony_ci    if (!srp_generate_server_master_secret(s)) {
31091cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
31101cb0ef41Sopenharmony_ci        return 0;
31111cb0ef41Sopenharmony_ci    }
31121cb0ef41Sopenharmony_ci
31131cb0ef41Sopenharmony_ci    return 1;
31141cb0ef41Sopenharmony_ci#else
31151cb0ef41Sopenharmony_ci    /* Should never happen */
31161cb0ef41Sopenharmony_ci    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
31171cb0ef41Sopenharmony_ci    return 0;
31181cb0ef41Sopenharmony_ci#endif
31191cb0ef41Sopenharmony_ci}
31201cb0ef41Sopenharmony_ci
31211cb0ef41Sopenharmony_cistatic int tls_process_cke_gost(SSL *s, PACKET *pkt)
31221cb0ef41Sopenharmony_ci{
31231cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_GOST
31241cb0ef41Sopenharmony_ci    EVP_PKEY_CTX *pkey_ctx;
31251cb0ef41Sopenharmony_ci    EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
31261cb0ef41Sopenharmony_ci    unsigned char premaster_secret[32];
31271cb0ef41Sopenharmony_ci    const unsigned char *start;
31281cb0ef41Sopenharmony_ci    size_t outlen = 32, inlen;
31291cb0ef41Sopenharmony_ci    unsigned long alg_a;
31301cb0ef41Sopenharmony_ci    GOST_KX_MESSAGE *pKX = NULL;
31311cb0ef41Sopenharmony_ci    const unsigned char *ptr;
31321cb0ef41Sopenharmony_ci    int ret = 0;
31331cb0ef41Sopenharmony_ci
31341cb0ef41Sopenharmony_ci    /* Get our certificate private key */
31351cb0ef41Sopenharmony_ci    alg_a = s->s3.tmp.new_cipher->algorithm_auth;
31361cb0ef41Sopenharmony_ci    if (alg_a & SSL_aGOST12) {
31371cb0ef41Sopenharmony_ci        /*
31381cb0ef41Sopenharmony_ci         * New GOST ciphersuites have SSL_aGOST01 bit too
31391cb0ef41Sopenharmony_ci         */
31401cb0ef41Sopenharmony_ci        pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
31411cb0ef41Sopenharmony_ci        if (pk == NULL) {
31421cb0ef41Sopenharmony_ci            pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
31431cb0ef41Sopenharmony_ci        }
31441cb0ef41Sopenharmony_ci        if (pk == NULL) {
31451cb0ef41Sopenharmony_ci            pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
31461cb0ef41Sopenharmony_ci        }
31471cb0ef41Sopenharmony_ci    } else if (alg_a & SSL_aGOST01) {
31481cb0ef41Sopenharmony_ci        pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
31491cb0ef41Sopenharmony_ci    }
31501cb0ef41Sopenharmony_ci
31511cb0ef41Sopenharmony_ci    pkey_ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, pk, s->ctx->propq);
31521cb0ef41Sopenharmony_ci    if (pkey_ctx == NULL) {
31531cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
31541cb0ef41Sopenharmony_ci        return 0;
31551cb0ef41Sopenharmony_ci    }
31561cb0ef41Sopenharmony_ci    if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
31571cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
31581cb0ef41Sopenharmony_ci        return 0;
31591cb0ef41Sopenharmony_ci    }
31601cb0ef41Sopenharmony_ci    /*
31611cb0ef41Sopenharmony_ci     * If client certificate is present and is of the same type, maybe
31621cb0ef41Sopenharmony_ci     * use it for key exchange.  Don't mind errors from
31631cb0ef41Sopenharmony_ci     * EVP_PKEY_derive_set_peer, because it is completely valid to use a
31641cb0ef41Sopenharmony_ci     * client certificate for authorization only.
31651cb0ef41Sopenharmony_ci     */
31661cb0ef41Sopenharmony_ci    client_pub_pkey = X509_get0_pubkey(s->session->peer);
31671cb0ef41Sopenharmony_ci    if (client_pub_pkey) {
31681cb0ef41Sopenharmony_ci        if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
31691cb0ef41Sopenharmony_ci            ERR_clear_error();
31701cb0ef41Sopenharmony_ci    }
31711cb0ef41Sopenharmony_ci
31721cb0ef41Sopenharmony_ci    ptr = PACKET_data(pkt);
31731cb0ef41Sopenharmony_ci    /* Some implementations provide extra data in the opaqueBlob
31741cb0ef41Sopenharmony_ci     * We have nothing to do with this blob so we just skip it */
31751cb0ef41Sopenharmony_ci    pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt));
31761cb0ef41Sopenharmony_ci    if (pKX == NULL
31771cb0ef41Sopenharmony_ci       || pKX->kxBlob == NULL
31781cb0ef41Sopenharmony_ci       || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) {
31791cb0ef41Sopenharmony_ci         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
31801cb0ef41Sopenharmony_ci         goto err;
31811cb0ef41Sopenharmony_ci    }
31821cb0ef41Sopenharmony_ci
31831cb0ef41Sopenharmony_ci    if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) {
31841cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
31851cb0ef41Sopenharmony_ci        goto err;
31861cb0ef41Sopenharmony_ci    }
31871cb0ef41Sopenharmony_ci
31881cb0ef41Sopenharmony_ci    if (PACKET_remaining(pkt) != 0) {
31891cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
31901cb0ef41Sopenharmony_ci        goto err;
31911cb0ef41Sopenharmony_ci    }
31921cb0ef41Sopenharmony_ci
31931cb0ef41Sopenharmony_ci    inlen = pKX->kxBlob->value.sequence->length;
31941cb0ef41Sopenharmony_ci    start = pKX->kxBlob->value.sequence->data;
31951cb0ef41Sopenharmony_ci
31961cb0ef41Sopenharmony_ci    if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
31971cb0ef41Sopenharmony_ci                         inlen) <= 0) {
31981cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
31991cb0ef41Sopenharmony_ci        goto err;
32001cb0ef41Sopenharmony_ci    }
32011cb0ef41Sopenharmony_ci    /* Generate master secret */
32021cb0ef41Sopenharmony_ci    if (!ssl_generate_master_secret(s, premaster_secret,
32031cb0ef41Sopenharmony_ci                                    sizeof(premaster_secret), 0)) {
32041cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
32051cb0ef41Sopenharmony_ci        goto err;
32061cb0ef41Sopenharmony_ci    }
32071cb0ef41Sopenharmony_ci    /* Check if pubkey from client certificate was used */
32081cb0ef41Sopenharmony_ci    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
32091cb0ef41Sopenharmony_ci                          NULL) > 0)
32101cb0ef41Sopenharmony_ci        s->statem.no_cert_verify = 1;
32111cb0ef41Sopenharmony_ci
32121cb0ef41Sopenharmony_ci    ret = 1;
32131cb0ef41Sopenharmony_ci err:
32141cb0ef41Sopenharmony_ci    EVP_PKEY_CTX_free(pkey_ctx);
32151cb0ef41Sopenharmony_ci    GOST_KX_MESSAGE_free(pKX);
32161cb0ef41Sopenharmony_ci    return ret;
32171cb0ef41Sopenharmony_ci#else
32181cb0ef41Sopenharmony_ci    /* Should never happen */
32191cb0ef41Sopenharmony_ci    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
32201cb0ef41Sopenharmony_ci    return 0;
32211cb0ef41Sopenharmony_ci#endif
32221cb0ef41Sopenharmony_ci}
32231cb0ef41Sopenharmony_ci
32241cb0ef41Sopenharmony_cistatic int tls_process_cke_gost18(SSL *s, PACKET *pkt)
32251cb0ef41Sopenharmony_ci{
32261cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_GOST
32271cb0ef41Sopenharmony_ci    unsigned char rnd_dgst[32];
32281cb0ef41Sopenharmony_ci    EVP_PKEY_CTX *pkey_ctx = NULL;
32291cb0ef41Sopenharmony_ci    EVP_PKEY *pk = NULL;
32301cb0ef41Sopenharmony_ci    unsigned char premaster_secret[32];
32311cb0ef41Sopenharmony_ci    const unsigned char *start = NULL;
32321cb0ef41Sopenharmony_ci    size_t outlen = 32, inlen = 0;
32331cb0ef41Sopenharmony_ci    int ret = 0;
32341cb0ef41Sopenharmony_ci    int cipher_nid = ossl_gost18_cke_cipher_nid(s);
32351cb0ef41Sopenharmony_ci
32361cb0ef41Sopenharmony_ci    if (cipher_nid == NID_undef) {
32371cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
32381cb0ef41Sopenharmony_ci        return 0;
32391cb0ef41Sopenharmony_ci    }
32401cb0ef41Sopenharmony_ci
32411cb0ef41Sopenharmony_ci    if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
32421cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
32431cb0ef41Sopenharmony_ci        goto err;
32441cb0ef41Sopenharmony_ci    }
32451cb0ef41Sopenharmony_ci
32461cb0ef41Sopenharmony_ci    /* Get our certificate private key */
32471cb0ef41Sopenharmony_ci    pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ?
32481cb0ef41Sopenharmony_ci         s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey :
32491cb0ef41Sopenharmony_ci         s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
32501cb0ef41Sopenharmony_ci    if (pk == NULL) {
32511cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
32521cb0ef41Sopenharmony_ci        goto err;
32531cb0ef41Sopenharmony_ci    }
32541cb0ef41Sopenharmony_ci
32551cb0ef41Sopenharmony_ci    pkey_ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, pk, s->ctx->propq);
32561cb0ef41Sopenharmony_ci    if (pkey_ctx == NULL) {
32571cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
32581cb0ef41Sopenharmony_ci        goto err;
32591cb0ef41Sopenharmony_ci    }
32601cb0ef41Sopenharmony_ci    if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
32611cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
32621cb0ef41Sopenharmony_ci        goto err;
32631cb0ef41Sopenharmony_ci    }
32641cb0ef41Sopenharmony_ci
32651cb0ef41Sopenharmony_ci    /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
32661cb0ef41Sopenharmony_ci    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
32671cb0ef41Sopenharmony_ci                          EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) {
32681cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
32691cb0ef41Sopenharmony_ci        goto err;
32701cb0ef41Sopenharmony_ci    }
32711cb0ef41Sopenharmony_ci
32721cb0ef41Sopenharmony_ci    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
32731cb0ef41Sopenharmony_ci                          EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) {
32741cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
32751cb0ef41Sopenharmony_ci        goto err;
32761cb0ef41Sopenharmony_ci    }
32771cb0ef41Sopenharmony_ci    inlen = PACKET_remaining(pkt);
32781cb0ef41Sopenharmony_ci    start = PACKET_data(pkt);
32791cb0ef41Sopenharmony_ci
32801cb0ef41Sopenharmony_ci    if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
32811cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
32821cb0ef41Sopenharmony_ci        goto err;
32831cb0ef41Sopenharmony_ci    }
32841cb0ef41Sopenharmony_ci    /* Generate master secret */
32851cb0ef41Sopenharmony_ci    if (!ssl_generate_master_secret(s, premaster_secret,
32861cb0ef41Sopenharmony_ci         sizeof(premaster_secret), 0)) {
32871cb0ef41Sopenharmony_ci         /* SSLfatal() already called */
32881cb0ef41Sopenharmony_ci         goto err;
32891cb0ef41Sopenharmony_ci    }
32901cb0ef41Sopenharmony_ci    ret = 1;
32911cb0ef41Sopenharmony_ci
32921cb0ef41Sopenharmony_ci err:
32931cb0ef41Sopenharmony_ci    EVP_PKEY_CTX_free(pkey_ctx);
32941cb0ef41Sopenharmony_ci    return ret;
32951cb0ef41Sopenharmony_ci#else
32961cb0ef41Sopenharmony_ci    /* Should never happen */
32971cb0ef41Sopenharmony_ci    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
32981cb0ef41Sopenharmony_ci    return 0;
32991cb0ef41Sopenharmony_ci#endif
33001cb0ef41Sopenharmony_ci}
33011cb0ef41Sopenharmony_ci
33021cb0ef41Sopenharmony_ciMSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
33031cb0ef41Sopenharmony_ci{
33041cb0ef41Sopenharmony_ci    unsigned long alg_k;
33051cb0ef41Sopenharmony_ci
33061cb0ef41Sopenharmony_ci    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
33071cb0ef41Sopenharmony_ci
33081cb0ef41Sopenharmony_ci    /* For PSK parse and retrieve identity, obtain PSK key */
33091cb0ef41Sopenharmony_ci    if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
33101cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
33111cb0ef41Sopenharmony_ci        goto err;
33121cb0ef41Sopenharmony_ci    }
33131cb0ef41Sopenharmony_ci
33141cb0ef41Sopenharmony_ci    if (alg_k & SSL_kPSK) {
33151cb0ef41Sopenharmony_ci        /* Identity extracted earlier: should be nothing left */
33161cb0ef41Sopenharmony_ci        if (PACKET_remaining(pkt) != 0) {
33171cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
33181cb0ef41Sopenharmony_ci            goto err;
33191cb0ef41Sopenharmony_ci        }
33201cb0ef41Sopenharmony_ci        /* PSK handled by ssl_generate_master_secret */
33211cb0ef41Sopenharmony_ci        if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
33221cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
33231cb0ef41Sopenharmony_ci            goto err;
33241cb0ef41Sopenharmony_ci        }
33251cb0ef41Sopenharmony_ci    } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
33261cb0ef41Sopenharmony_ci        if (!tls_process_cke_rsa(s, pkt)) {
33271cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
33281cb0ef41Sopenharmony_ci            goto err;
33291cb0ef41Sopenharmony_ci        }
33301cb0ef41Sopenharmony_ci    } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
33311cb0ef41Sopenharmony_ci        if (!tls_process_cke_dhe(s, pkt)) {
33321cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
33331cb0ef41Sopenharmony_ci            goto err;
33341cb0ef41Sopenharmony_ci        }
33351cb0ef41Sopenharmony_ci    } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
33361cb0ef41Sopenharmony_ci        if (!tls_process_cke_ecdhe(s, pkt)) {
33371cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
33381cb0ef41Sopenharmony_ci            goto err;
33391cb0ef41Sopenharmony_ci        }
33401cb0ef41Sopenharmony_ci    } else if (alg_k & SSL_kSRP) {
33411cb0ef41Sopenharmony_ci        if (!tls_process_cke_srp(s, pkt)) {
33421cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
33431cb0ef41Sopenharmony_ci            goto err;
33441cb0ef41Sopenharmony_ci        }
33451cb0ef41Sopenharmony_ci    } else if (alg_k & SSL_kGOST) {
33461cb0ef41Sopenharmony_ci        if (!tls_process_cke_gost(s, pkt)) {
33471cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
33481cb0ef41Sopenharmony_ci            goto err;
33491cb0ef41Sopenharmony_ci        }
33501cb0ef41Sopenharmony_ci    } else if (alg_k & SSL_kGOST18) {
33511cb0ef41Sopenharmony_ci        if (!tls_process_cke_gost18(s, pkt)) {
33521cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
33531cb0ef41Sopenharmony_ci            goto err;
33541cb0ef41Sopenharmony_ci        }
33551cb0ef41Sopenharmony_ci    } else {
33561cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);
33571cb0ef41Sopenharmony_ci        goto err;
33581cb0ef41Sopenharmony_ci    }
33591cb0ef41Sopenharmony_ci
33601cb0ef41Sopenharmony_ci    return MSG_PROCESS_CONTINUE_PROCESSING;
33611cb0ef41Sopenharmony_ci err:
33621cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_PSK
33631cb0ef41Sopenharmony_ci    OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
33641cb0ef41Sopenharmony_ci    s->s3.tmp.psk = NULL;
33651cb0ef41Sopenharmony_ci    s->s3.tmp.psklen = 0;
33661cb0ef41Sopenharmony_ci#endif
33671cb0ef41Sopenharmony_ci    return MSG_PROCESS_ERROR;
33681cb0ef41Sopenharmony_ci}
33691cb0ef41Sopenharmony_ci
33701cb0ef41Sopenharmony_ciWORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
33711cb0ef41Sopenharmony_ci{
33721cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_SCTP
33731cb0ef41Sopenharmony_ci    if (wst == WORK_MORE_A) {
33741cb0ef41Sopenharmony_ci        if (SSL_IS_DTLS(s)) {
33751cb0ef41Sopenharmony_ci            unsigned char sctpauthkey[64];
33761cb0ef41Sopenharmony_ci            char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
33771cb0ef41Sopenharmony_ci            size_t labellen;
33781cb0ef41Sopenharmony_ci            /*
33791cb0ef41Sopenharmony_ci             * Add new shared key for SCTP-Auth, will be ignored if no SCTP
33801cb0ef41Sopenharmony_ci             * used.
33811cb0ef41Sopenharmony_ci             */
33821cb0ef41Sopenharmony_ci            memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
33831cb0ef41Sopenharmony_ci                   sizeof(DTLS1_SCTP_AUTH_LABEL));
33841cb0ef41Sopenharmony_ci
33851cb0ef41Sopenharmony_ci            /* Don't include the terminating zero. */
33861cb0ef41Sopenharmony_ci            labellen = sizeof(labelbuffer) - 1;
33871cb0ef41Sopenharmony_ci            if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
33881cb0ef41Sopenharmony_ci                labellen += 1;
33891cb0ef41Sopenharmony_ci
33901cb0ef41Sopenharmony_ci            if (SSL_export_keying_material(s, sctpauthkey,
33911cb0ef41Sopenharmony_ci                                           sizeof(sctpauthkey), labelbuffer,
33921cb0ef41Sopenharmony_ci                                           labellen, NULL, 0,
33931cb0ef41Sopenharmony_ci                                           0) <= 0) {
33941cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
33951cb0ef41Sopenharmony_ci                return WORK_ERROR;
33961cb0ef41Sopenharmony_ci            }
33971cb0ef41Sopenharmony_ci
33981cb0ef41Sopenharmony_ci            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
33991cb0ef41Sopenharmony_ci                     sizeof(sctpauthkey), sctpauthkey);
34001cb0ef41Sopenharmony_ci        }
34011cb0ef41Sopenharmony_ci    }
34021cb0ef41Sopenharmony_ci#endif
34031cb0ef41Sopenharmony_ci
34041cb0ef41Sopenharmony_ci    if (s->statem.no_cert_verify || !s->session->peer) {
34051cb0ef41Sopenharmony_ci        /*
34061cb0ef41Sopenharmony_ci         * No certificate verify or no peer certificate so we no longer need
34071cb0ef41Sopenharmony_ci         * the handshake_buffer
34081cb0ef41Sopenharmony_ci         */
34091cb0ef41Sopenharmony_ci        if (!ssl3_digest_cached_records(s, 0)) {
34101cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
34111cb0ef41Sopenharmony_ci            return WORK_ERROR;
34121cb0ef41Sopenharmony_ci        }
34131cb0ef41Sopenharmony_ci        return WORK_FINISHED_CONTINUE;
34141cb0ef41Sopenharmony_ci    } else {
34151cb0ef41Sopenharmony_ci        if (!s->s3.handshake_buffer) {
34161cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
34171cb0ef41Sopenharmony_ci            return WORK_ERROR;
34181cb0ef41Sopenharmony_ci        }
34191cb0ef41Sopenharmony_ci        /*
34201cb0ef41Sopenharmony_ci         * For sigalgs freeze the handshake buffer. If we support
34211cb0ef41Sopenharmony_ci         * extms we've done this already so this is a no-op
34221cb0ef41Sopenharmony_ci         */
34231cb0ef41Sopenharmony_ci        if (!ssl3_digest_cached_records(s, 1)) {
34241cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
34251cb0ef41Sopenharmony_ci            return WORK_ERROR;
34261cb0ef41Sopenharmony_ci        }
34271cb0ef41Sopenharmony_ci    }
34281cb0ef41Sopenharmony_ci
34291cb0ef41Sopenharmony_ci    return WORK_FINISHED_CONTINUE;
34301cb0ef41Sopenharmony_ci}
34311cb0ef41Sopenharmony_ci
34321cb0ef41Sopenharmony_ciMSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
34331cb0ef41Sopenharmony_ci{
34341cb0ef41Sopenharmony_ci    int i;
34351cb0ef41Sopenharmony_ci    MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
34361cb0ef41Sopenharmony_ci    X509 *x = NULL;
34371cb0ef41Sopenharmony_ci    unsigned long l;
34381cb0ef41Sopenharmony_ci    const unsigned char *certstart, *certbytes;
34391cb0ef41Sopenharmony_ci    STACK_OF(X509) *sk = NULL;
34401cb0ef41Sopenharmony_ci    PACKET spkt, context;
34411cb0ef41Sopenharmony_ci    size_t chainidx;
34421cb0ef41Sopenharmony_ci    SSL_SESSION *new_sess = NULL;
34431cb0ef41Sopenharmony_ci
34441cb0ef41Sopenharmony_ci    /*
34451cb0ef41Sopenharmony_ci     * To get this far we must have read encrypted data from the client. We no
34461cb0ef41Sopenharmony_ci     * longer tolerate unencrypted alerts. This value is ignored if less than
34471cb0ef41Sopenharmony_ci     * TLSv1.3
34481cb0ef41Sopenharmony_ci     */
34491cb0ef41Sopenharmony_ci    s->statem.enc_read_state = ENC_READ_STATE_VALID;
34501cb0ef41Sopenharmony_ci
34511cb0ef41Sopenharmony_ci    if ((sk = sk_X509_new_null()) == NULL) {
34521cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
34531cb0ef41Sopenharmony_ci        goto err;
34541cb0ef41Sopenharmony_ci    }
34551cb0ef41Sopenharmony_ci
34561cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s) && (!PACKET_get_length_prefixed_1(pkt, &context)
34571cb0ef41Sopenharmony_ci                            || (s->pha_context == NULL && PACKET_remaining(&context) != 0)
34581cb0ef41Sopenharmony_ci                            || (s->pha_context != NULL &&
34591cb0ef41Sopenharmony_ci                                !PACKET_equal(&context, s->pha_context, s->pha_context_len)))) {
34601cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
34611cb0ef41Sopenharmony_ci        goto err;
34621cb0ef41Sopenharmony_ci    }
34631cb0ef41Sopenharmony_ci
34641cb0ef41Sopenharmony_ci    if (!PACKET_get_length_prefixed_3(pkt, &spkt)
34651cb0ef41Sopenharmony_ci            || PACKET_remaining(pkt) != 0) {
34661cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
34671cb0ef41Sopenharmony_ci        goto err;
34681cb0ef41Sopenharmony_ci    }
34691cb0ef41Sopenharmony_ci
34701cb0ef41Sopenharmony_ci    for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
34711cb0ef41Sopenharmony_ci        if (!PACKET_get_net_3(&spkt, &l)
34721cb0ef41Sopenharmony_ci            || !PACKET_get_bytes(&spkt, &certbytes, l)) {
34731cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
34741cb0ef41Sopenharmony_ci            goto err;
34751cb0ef41Sopenharmony_ci        }
34761cb0ef41Sopenharmony_ci
34771cb0ef41Sopenharmony_ci        certstart = certbytes;
34781cb0ef41Sopenharmony_ci        x = X509_new_ex(s->ctx->libctx, s->ctx->propq);
34791cb0ef41Sopenharmony_ci        if (x == NULL) {
34801cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_MALLOC_FAILURE);
34811cb0ef41Sopenharmony_ci            goto err;
34821cb0ef41Sopenharmony_ci        }
34831cb0ef41Sopenharmony_ci        if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) {
34841cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
34851cb0ef41Sopenharmony_ci            goto err;
34861cb0ef41Sopenharmony_ci        }
34871cb0ef41Sopenharmony_ci
34881cb0ef41Sopenharmony_ci        if (certbytes != (certstart + l)) {
34891cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
34901cb0ef41Sopenharmony_ci            goto err;
34911cb0ef41Sopenharmony_ci        }
34921cb0ef41Sopenharmony_ci
34931cb0ef41Sopenharmony_ci        if (SSL_IS_TLS13(s)) {
34941cb0ef41Sopenharmony_ci            RAW_EXTENSION *rawexts = NULL;
34951cb0ef41Sopenharmony_ci            PACKET extensions;
34961cb0ef41Sopenharmony_ci
34971cb0ef41Sopenharmony_ci            if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
34981cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
34991cb0ef41Sopenharmony_ci                goto err;
35001cb0ef41Sopenharmony_ci            }
35011cb0ef41Sopenharmony_ci            if (!tls_collect_extensions(s, &extensions,
35021cb0ef41Sopenharmony_ci                                        SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
35031cb0ef41Sopenharmony_ci                                        NULL, chainidx == 0)
35041cb0ef41Sopenharmony_ci                || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
35051cb0ef41Sopenharmony_ci                                             rawexts, x, chainidx,
35061cb0ef41Sopenharmony_ci                                             PACKET_remaining(&spkt) == 0)) {
35071cb0ef41Sopenharmony_ci                OPENSSL_free(rawexts);
35081cb0ef41Sopenharmony_ci                goto err;
35091cb0ef41Sopenharmony_ci            }
35101cb0ef41Sopenharmony_ci            OPENSSL_free(rawexts);
35111cb0ef41Sopenharmony_ci        }
35121cb0ef41Sopenharmony_ci
35131cb0ef41Sopenharmony_ci        if (!sk_X509_push(sk, x)) {
35141cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
35151cb0ef41Sopenharmony_ci            goto err;
35161cb0ef41Sopenharmony_ci        }
35171cb0ef41Sopenharmony_ci        x = NULL;
35181cb0ef41Sopenharmony_ci    }
35191cb0ef41Sopenharmony_ci
35201cb0ef41Sopenharmony_ci    if (sk_X509_num(sk) <= 0) {
35211cb0ef41Sopenharmony_ci        /* TLS does not mind 0 certs returned */
35221cb0ef41Sopenharmony_ci        if (s->version == SSL3_VERSION) {
35231cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
35241cb0ef41Sopenharmony_ci                     SSL_R_NO_CERTIFICATES_RETURNED);
35251cb0ef41Sopenharmony_ci            goto err;
35261cb0ef41Sopenharmony_ci        }
35271cb0ef41Sopenharmony_ci        /* Fail for TLS only if we required a certificate */
35281cb0ef41Sopenharmony_ci        else if ((s->verify_mode & SSL_VERIFY_PEER) &&
35291cb0ef41Sopenharmony_ci                 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
35301cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
35311cb0ef41Sopenharmony_ci                     SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
35321cb0ef41Sopenharmony_ci            goto err;
35331cb0ef41Sopenharmony_ci        }
35341cb0ef41Sopenharmony_ci        /* No client certificate so digest cached records */
35351cb0ef41Sopenharmony_ci        if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
35361cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
35371cb0ef41Sopenharmony_ci            goto err;
35381cb0ef41Sopenharmony_ci        }
35391cb0ef41Sopenharmony_ci    } else {
35401cb0ef41Sopenharmony_ci        EVP_PKEY *pkey;
35411cb0ef41Sopenharmony_ci        i = ssl_verify_cert_chain(s, sk);
35421cb0ef41Sopenharmony_ci        if (i <= 0) {
35431cb0ef41Sopenharmony_ci            SSLfatal(s, ssl_x509err2alert(s->verify_result),
35441cb0ef41Sopenharmony_ci                     SSL_R_CERTIFICATE_VERIFY_FAILED);
35451cb0ef41Sopenharmony_ci            goto err;
35461cb0ef41Sopenharmony_ci        }
35471cb0ef41Sopenharmony_ci        pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
35481cb0ef41Sopenharmony_ci        if (pkey == NULL) {
35491cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
35501cb0ef41Sopenharmony_ci                     SSL_R_UNKNOWN_CERTIFICATE_TYPE);
35511cb0ef41Sopenharmony_ci            goto err;
35521cb0ef41Sopenharmony_ci        }
35531cb0ef41Sopenharmony_ci    }
35541cb0ef41Sopenharmony_ci
35551cb0ef41Sopenharmony_ci    /*
35561cb0ef41Sopenharmony_ci     * Sessions must be immutable once they go into the session cache. Otherwise
35571cb0ef41Sopenharmony_ci     * we can get multi-thread problems. Therefore we don't "update" sessions,
35581cb0ef41Sopenharmony_ci     * we replace them with a duplicate. Here, we need to do this every time
35591cb0ef41Sopenharmony_ci     * a new certificate is received via post-handshake authentication, as the
35601cb0ef41Sopenharmony_ci     * session may have already gone into the session cache.
35611cb0ef41Sopenharmony_ci     */
35621cb0ef41Sopenharmony_ci
35631cb0ef41Sopenharmony_ci    if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
35641cb0ef41Sopenharmony_ci        if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
35651cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
35661cb0ef41Sopenharmony_ci            goto err;
35671cb0ef41Sopenharmony_ci        }
35681cb0ef41Sopenharmony_ci
35691cb0ef41Sopenharmony_ci        SSL_SESSION_free(s->session);
35701cb0ef41Sopenharmony_ci        s->session = new_sess;
35711cb0ef41Sopenharmony_ci    }
35721cb0ef41Sopenharmony_ci
35731cb0ef41Sopenharmony_ci    X509_free(s->session->peer);
35741cb0ef41Sopenharmony_ci    s->session->peer = sk_X509_shift(sk);
35751cb0ef41Sopenharmony_ci    s->session->verify_result = s->verify_result;
35761cb0ef41Sopenharmony_ci
35771cb0ef41Sopenharmony_ci    sk_X509_pop_free(s->session->peer_chain, X509_free);
35781cb0ef41Sopenharmony_ci    s->session->peer_chain = sk;
35791cb0ef41Sopenharmony_ci    sk = NULL;
35801cb0ef41Sopenharmony_ci
35811cb0ef41Sopenharmony_ci    /*
35821cb0ef41Sopenharmony_ci     * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
35831cb0ef41Sopenharmony_ci     * message
35841cb0ef41Sopenharmony_ci     */
35851cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
35861cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
35871cb0ef41Sopenharmony_ci        goto err;
35881cb0ef41Sopenharmony_ci    }
35891cb0ef41Sopenharmony_ci
35901cb0ef41Sopenharmony_ci    /*
35911cb0ef41Sopenharmony_ci     * Inconsistency alert: cert_chain does *not* include the peer's own
35921cb0ef41Sopenharmony_ci     * certificate, while we do include it in statem_clnt.c
35931cb0ef41Sopenharmony_ci     */
35941cb0ef41Sopenharmony_ci
35951cb0ef41Sopenharmony_ci    /* Save the current hash state for when we receive the CertificateVerify */
35961cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
35971cb0ef41Sopenharmony_ci        if (!ssl_handshake_hash(s, s->cert_verify_hash,
35981cb0ef41Sopenharmony_ci                                sizeof(s->cert_verify_hash),
35991cb0ef41Sopenharmony_ci                                &s->cert_verify_hash_len)) {
36001cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
36011cb0ef41Sopenharmony_ci            goto err;
36021cb0ef41Sopenharmony_ci        }
36031cb0ef41Sopenharmony_ci
36041cb0ef41Sopenharmony_ci        /* Resend session tickets */
36051cb0ef41Sopenharmony_ci        s->sent_tickets = 0;
36061cb0ef41Sopenharmony_ci    }
36071cb0ef41Sopenharmony_ci
36081cb0ef41Sopenharmony_ci    ret = MSG_PROCESS_CONTINUE_READING;
36091cb0ef41Sopenharmony_ci
36101cb0ef41Sopenharmony_ci err:
36111cb0ef41Sopenharmony_ci    X509_free(x);
36121cb0ef41Sopenharmony_ci    sk_X509_pop_free(sk, X509_free);
36131cb0ef41Sopenharmony_ci    return ret;
36141cb0ef41Sopenharmony_ci}
36151cb0ef41Sopenharmony_ci
36161cb0ef41Sopenharmony_ciint tls_construct_server_certificate(SSL *s, WPACKET *pkt)
36171cb0ef41Sopenharmony_ci{
36181cb0ef41Sopenharmony_ci    CERT_PKEY *cpk = s->s3.tmp.cert;
36191cb0ef41Sopenharmony_ci
36201cb0ef41Sopenharmony_ci    if (cpk == NULL) {
36211cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
36221cb0ef41Sopenharmony_ci        return 0;
36231cb0ef41Sopenharmony_ci    }
36241cb0ef41Sopenharmony_ci
36251cb0ef41Sopenharmony_ci    /*
36261cb0ef41Sopenharmony_ci     * In TLSv1.3 the certificate chain is always preceded by a 0 length context
36271cb0ef41Sopenharmony_ci     * for the server Certificate message
36281cb0ef41Sopenharmony_ci     */
36291cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
36301cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
36311cb0ef41Sopenharmony_ci        return 0;
36321cb0ef41Sopenharmony_ci    }
36331cb0ef41Sopenharmony_ci    if (!ssl3_output_cert_chain(s, pkt, cpk)) {
36341cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
36351cb0ef41Sopenharmony_ci        return 0;
36361cb0ef41Sopenharmony_ci    }
36371cb0ef41Sopenharmony_ci
36381cb0ef41Sopenharmony_ci    return 1;
36391cb0ef41Sopenharmony_ci}
36401cb0ef41Sopenharmony_ci
36411cb0ef41Sopenharmony_cistatic int create_ticket_prequel(SSL *s, WPACKET *pkt, uint32_t age_add,
36421cb0ef41Sopenharmony_ci                                 unsigned char *tick_nonce)
36431cb0ef41Sopenharmony_ci{
36441cb0ef41Sopenharmony_ci    uint32_t timeout = (uint32_t)s->session->timeout;
36451cb0ef41Sopenharmony_ci
36461cb0ef41Sopenharmony_ci    /*
36471cb0ef41Sopenharmony_ci     * Ticket lifetime hint:
36481cb0ef41Sopenharmony_ci     * In TLSv1.3 we reset the "time" field above, and always specify the
36491cb0ef41Sopenharmony_ci     * timeout, limited to a 1 week period per RFC8446.
36501cb0ef41Sopenharmony_ci     * For TLSv1.2 this is advisory only and we leave this unspecified for
36511cb0ef41Sopenharmony_ci     * resumed session (for simplicity).
36521cb0ef41Sopenharmony_ci     */
36531cb0ef41Sopenharmony_ci#define ONE_WEEK_SEC (7 * 24 * 60 * 60)
36541cb0ef41Sopenharmony_ci
36551cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
36561cb0ef41Sopenharmony_ci        if (s->session->timeout > ONE_WEEK_SEC)
36571cb0ef41Sopenharmony_ci            timeout = ONE_WEEK_SEC;
36581cb0ef41Sopenharmony_ci    } else if (s->hit)
36591cb0ef41Sopenharmony_ci        timeout = 0;
36601cb0ef41Sopenharmony_ci
36611cb0ef41Sopenharmony_ci    if (!WPACKET_put_bytes_u32(pkt, timeout)) {
36621cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
36631cb0ef41Sopenharmony_ci        return 0;
36641cb0ef41Sopenharmony_ci    }
36651cb0ef41Sopenharmony_ci
36661cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
36671cb0ef41Sopenharmony_ci        if (!WPACKET_put_bytes_u32(pkt, age_add)
36681cb0ef41Sopenharmony_ci                || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
36691cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
36701cb0ef41Sopenharmony_ci            return 0;
36711cb0ef41Sopenharmony_ci        }
36721cb0ef41Sopenharmony_ci    }
36731cb0ef41Sopenharmony_ci
36741cb0ef41Sopenharmony_ci    /* Start the sub-packet for the actual ticket data */
36751cb0ef41Sopenharmony_ci    if (!WPACKET_start_sub_packet_u16(pkt)) {
36761cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
36771cb0ef41Sopenharmony_ci        return 0;
36781cb0ef41Sopenharmony_ci    }
36791cb0ef41Sopenharmony_ci
36801cb0ef41Sopenharmony_ci    return 1;
36811cb0ef41Sopenharmony_ci}
36821cb0ef41Sopenharmony_ci
36831cb0ef41Sopenharmony_ci/*
36841cb0ef41Sopenharmony_ci * Returns 1 on success, 0 to abort construction of the ticket (non-fatal), or
36851cb0ef41Sopenharmony_ci * -1 on fatal error
36861cb0ef41Sopenharmony_ci */
36871cb0ef41Sopenharmony_cistatic int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
36881cb0ef41Sopenharmony_ci                                      unsigned char *tick_nonce)
36891cb0ef41Sopenharmony_ci{
36901cb0ef41Sopenharmony_ci    unsigned char *senc = NULL;
36911cb0ef41Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL;
36921cb0ef41Sopenharmony_ci    SSL_HMAC *hctx = NULL;
36931cb0ef41Sopenharmony_ci    unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
36941cb0ef41Sopenharmony_ci    const unsigned char *const_p;
36951cb0ef41Sopenharmony_ci    int len, slen_full, slen, lenfinal;
36961cb0ef41Sopenharmony_ci    SSL_SESSION *sess;
36971cb0ef41Sopenharmony_ci    size_t hlen;
36981cb0ef41Sopenharmony_ci    SSL_CTX *tctx = s->session_ctx;
36991cb0ef41Sopenharmony_ci    unsigned char iv[EVP_MAX_IV_LENGTH];
37001cb0ef41Sopenharmony_ci    unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
37011cb0ef41Sopenharmony_ci    int iv_len, ok = -1;
37021cb0ef41Sopenharmony_ci    size_t macoffset, macendoffset;
37031cb0ef41Sopenharmony_ci
37041cb0ef41Sopenharmony_ci    /* get session encoding length */
37051cb0ef41Sopenharmony_ci    slen_full = i2d_SSL_SESSION(s->session, NULL);
37061cb0ef41Sopenharmony_ci    /*
37071cb0ef41Sopenharmony_ci     * Some length values are 16 bits, so forget it if session is too
37081cb0ef41Sopenharmony_ci     * long
37091cb0ef41Sopenharmony_ci     */
37101cb0ef41Sopenharmony_ci    if (slen_full == 0 || slen_full > 0xFF00) {
37111cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
37121cb0ef41Sopenharmony_ci        goto err;
37131cb0ef41Sopenharmony_ci    }
37141cb0ef41Sopenharmony_ci    senc = OPENSSL_malloc(slen_full);
37151cb0ef41Sopenharmony_ci    if (senc == NULL) {
37161cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
37171cb0ef41Sopenharmony_ci        goto err;
37181cb0ef41Sopenharmony_ci    }
37191cb0ef41Sopenharmony_ci
37201cb0ef41Sopenharmony_ci    ctx = EVP_CIPHER_CTX_new();
37211cb0ef41Sopenharmony_ci    hctx = ssl_hmac_new(tctx);
37221cb0ef41Sopenharmony_ci    if (ctx == NULL || hctx == NULL) {
37231cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
37241cb0ef41Sopenharmony_ci        goto err;
37251cb0ef41Sopenharmony_ci    }
37261cb0ef41Sopenharmony_ci
37271cb0ef41Sopenharmony_ci    p = senc;
37281cb0ef41Sopenharmony_ci    if (!i2d_SSL_SESSION(s->session, &p)) {
37291cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
37301cb0ef41Sopenharmony_ci        goto err;
37311cb0ef41Sopenharmony_ci    }
37321cb0ef41Sopenharmony_ci
37331cb0ef41Sopenharmony_ci    /*
37341cb0ef41Sopenharmony_ci     * create a fresh copy (not shared with other threads) to clean up
37351cb0ef41Sopenharmony_ci     */
37361cb0ef41Sopenharmony_ci    const_p = senc;
37371cb0ef41Sopenharmony_ci    sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
37381cb0ef41Sopenharmony_ci    if (sess == NULL) {
37391cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
37401cb0ef41Sopenharmony_ci        goto err;
37411cb0ef41Sopenharmony_ci    }
37421cb0ef41Sopenharmony_ci
37431cb0ef41Sopenharmony_ci    slen = i2d_SSL_SESSION(sess, NULL);
37441cb0ef41Sopenharmony_ci    if (slen == 0 || slen > slen_full) {
37451cb0ef41Sopenharmony_ci        /* shouldn't ever happen */
37461cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
37471cb0ef41Sopenharmony_ci        SSL_SESSION_free(sess);
37481cb0ef41Sopenharmony_ci        goto err;
37491cb0ef41Sopenharmony_ci    }
37501cb0ef41Sopenharmony_ci    p = senc;
37511cb0ef41Sopenharmony_ci    if (!i2d_SSL_SESSION(sess, &p)) {
37521cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
37531cb0ef41Sopenharmony_ci        SSL_SESSION_free(sess);
37541cb0ef41Sopenharmony_ci        goto err;
37551cb0ef41Sopenharmony_ci    }
37561cb0ef41Sopenharmony_ci    SSL_SESSION_free(sess);
37571cb0ef41Sopenharmony_ci
37581cb0ef41Sopenharmony_ci    /*
37591cb0ef41Sopenharmony_ci     * Initialize HMAC and cipher contexts. If callback present it does
37601cb0ef41Sopenharmony_ci     * all the work otherwise use generated values from parent ctx.
37611cb0ef41Sopenharmony_ci     */
37621cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
37631cb0ef41Sopenharmony_ci    if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
37641cb0ef41Sopenharmony_ci#else
37651cb0ef41Sopenharmony_ci    if (tctx->ext.ticket_key_evp_cb != NULL)
37661cb0ef41Sopenharmony_ci#endif
37671cb0ef41Sopenharmony_ci    {
37681cb0ef41Sopenharmony_ci        int ret = 0;
37691cb0ef41Sopenharmony_ci
37701cb0ef41Sopenharmony_ci        if (tctx->ext.ticket_key_evp_cb != NULL)
37711cb0ef41Sopenharmony_ci            ret = tctx->ext.ticket_key_evp_cb(s, key_name, iv, ctx,
37721cb0ef41Sopenharmony_ci                                              ssl_hmac_get0_EVP_MAC_CTX(hctx),
37731cb0ef41Sopenharmony_ci                                              1);
37741cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
37751cb0ef41Sopenharmony_ci        else if (tctx->ext.ticket_key_cb != NULL)
37761cb0ef41Sopenharmony_ci            /* if 0 is returned, write an empty ticket */
37771cb0ef41Sopenharmony_ci            ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
37781cb0ef41Sopenharmony_ci                                          ssl_hmac_get0_HMAC_CTX(hctx), 1);
37791cb0ef41Sopenharmony_ci#endif
37801cb0ef41Sopenharmony_ci
37811cb0ef41Sopenharmony_ci        if (ret == 0) {
37821cb0ef41Sopenharmony_ci            /*
37831cb0ef41Sopenharmony_ci             * In TLSv1.2 we construct a 0 length ticket. In TLSv1.3 a 0
37841cb0ef41Sopenharmony_ci             * length ticket is not allowed so we abort construction of the
37851cb0ef41Sopenharmony_ci             * ticket
37861cb0ef41Sopenharmony_ci             */
37871cb0ef41Sopenharmony_ci            if (SSL_IS_TLS13(s)) {
37881cb0ef41Sopenharmony_ci                ok = 0;
37891cb0ef41Sopenharmony_ci                goto err;
37901cb0ef41Sopenharmony_ci            }
37911cb0ef41Sopenharmony_ci            /* Put timeout and length */
37921cb0ef41Sopenharmony_ci            if (!WPACKET_put_bytes_u32(pkt, 0)
37931cb0ef41Sopenharmony_ci                    || !WPACKET_put_bytes_u16(pkt, 0)) {
37941cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
37951cb0ef41Sopenharmony_ci                goto err;
37961cb0ef41Sopenharmony_ci            }
37971cb0ef41Sopenharmony_ci            OPENSSL_free(senc);
37981cb0ef41Sopenharmony_ci            EVP_CIPHER_CTX_free(ctx);
37991cb0ef41Sopenharmony_ci            ssl_hmac_free(hctx);
38001cb0ef41Sopenharmony_ci            return 1;
38011cb0ef41Sopenharmony_ci        }
38021cb0ef41Sopenharmony_ci        if (ret < 0) {
38031cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
38041cb0ef41Sopenharmony_ci            goto err;
38051cb0ef41Sopenharmony_ci        }
38061cb0ef41Sopenharmony_ci        iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);
38071cb0ef41Sopenharmony_ci        if (iv_len < 0) {
38081cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
38091cb0ef41Sopenharmony_ci            goto err;
38101cb0ef41Sopenharmony_ci        }
38111cb0ef41Sopenharmony_ci    } else {
38121cb0ef41Sopenharmony_ci        EVP_CIPHER *cipher = EVP_CIPHER_fetch(s->ctx->libctx, "AES-256-CBC",
38131cb0ef41Sopenharmony_ci                                              s->ctx->propq);
38141cb0ef41Sopenharmony_ci
38151cb0ef41Sopenharmony_ci        if (cipher == NULL) {
38161cb0ef41Sopenharmony_ci            /* Error is already recorded */
38171cb0ef41Sopenharmony_ci            SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
38181cb0ef41Sopenharmony_ci            goto err;
38191cb0ef41Sopenharmony_ci        }
38201cb0ef41Sopenharmony_ci
38211cb0ef41Sopenharmony_ci        iv_len = EVP_CIPHER_get_iv_length(cipher);
38221cb0ef41Sopenharmony_ci        if (iv_len < 0
38231cb0ef41Sopenharmony_ci                || RAND_bytes_ex(s->ctx->libctx, iv, iv_len, 0) <= 0
38241cb0ef41Sopenharmony_ci                || !EVP_EncryptInit_ex(ctx, cipher, NULL,
38251cb0ef41Sopenharmony_ci                                       tctx->ext.secure->tick_aes_key, iv)
38261cb0ef41Sopenharmony_ci                || !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
38271cb0ef41Sopenharmony_ci                                  sizeof(tctx->ext.secure->tick_hmac_key),
38281cb0ef41Sopenharmony_ci                                  "SHA256")) {
38291cb0ef41Sopenharmony_ci            EVP_CIPHER_free(cipher);
38301cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
38311cb0ef41Sopenharmony_ci            goto err;
38321cb0ef41Sopenharmony_ci        }
38331cb0ef41Sopenharmony_ci        EVP_CIPHER_free(cipher);
38341cb0ef41Sopenharmony_ci        memcpy(key_name, tctx->ext.tick_key_name,
38351cb0ef41Sopenharmony_ci               sizeof(tctx->ext.tick_key_name));
38361cb0ef41Sopenharmony_ci    }
38371cb0ef41Sopenharmony_ci
38381cb0ef41Sopenharmony_ci    if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
38391cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
38401cb0ef41Sopenharmony_ci        goto err;
38411cb0ef41Sopenharmony_ci    }
38421cb0ef41Sopenharmony_ci
38431cb0ef41Sopenharmony_ci    if (!WPACKET_get_total_written(pkt, &macoffset)
38441cb0ef41Sopenharmony_ci               /* Output key name */
38451cb0ef41Sopenharmony_ci            || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
38461cb0ef41Sopenharmony_ci               /* output IV */
38471cb0ef41Sopenharmony_ci            || !WPACKET_memcpy(pkt, iv, iv_len)
38481cb0ef41Sopenharmony_ci            || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
38491cb0ef41Sopenharmony_ci                                      &encdata1)
38501cb0ef41Sopenharmony_ci               /* Encrypt session data */
38511cb0ef41Sopenharmony_ci            || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
38521cb0ef41Sopenharmony_ci            || !WPACKET_allocate_bytes(pkt, len, &encdata2)
38531cb0ef41Sopenharmony_ci            || encdata1 != encdata2
38541cb0ef41Sopenharmony_ci            || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
38551cb0ef41Sopenharmony_ci            || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
38561cb0ef41Sopenharmony_ci            || encdata1 + len != encdata2
38571cb0ef41Sopenharmony_ci            || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
38581cb0ef41Sopenharmony_ci            || !WPACKET_get_total_written(pkt, &macendoffset)
38591cb0ef41Sopenharmony_ci            || !ssl_hmac_update(hctx,
38601cb0ef41Sopenharmony_ci                                (unsigned char *)s->init_buf->data + macoffset,
38611cb0ef41Sopenharmony_ci                                macendoffset - macoffset)
38621cb0ef41Sopenharmony_ci            || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
38631cb0ef41Sopenharmony_ci            || !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE)
38641cb0ef41Sopenharmony_ci            || hlen > EVP_MAX_MD_SIZE
38651cb0ef41Sopenharmony_ci            || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
38661cb0ef41Sopenharmony_ci            || macdata1 != macdata2) {
38671cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
38681cb0ef41Sopenharmony_ci        goto err;
38691cb0ef41Sopenharmony_ci    }
38701cb0ef41Sopenharmony_ci
38711cb0ef41Sopenharmony_ci    /* Close the sub-packet created by create_ticket_prequel() */
38721cb0ef41Sopenharmony_ci    if (!WPACKET_close(pkt)) {
38731cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
38741cb0ef41Sopenharmony_ci        goto err;
38751cb0ef41Sopenharmony_ci    }
38761cb0ef41Sopenharmony_ci
38771cb0ef41Sopenharmony_ci    ok = 1;
38781cb0ef41Sopenharmony_ci err:
38791cb0ef41Sopenharmony_ci    OPENSSL_free(senc);
38801cb0ef41Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
38811cb0ef41Sopenharmony_ci    ssl_hmac_free(hctx);
38821cb0ef41Sopenharmony_ci    return ok;
38831cb0ef41Sopenharmony_ci}
38841cb0ef41Sopenharmony_ci
38851cb0ef41Sopenharmony_cistatic int construct_stateful_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
38861cb0ef41Sopenharmony_ci                                     unsigned char *tick_nonce)
38871cb0ef41Sopenharmony_ci{
38881cb0ef41Sopenharmony_ci    if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
38891cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
38901cb0ef41Sopenharmony_ci        return 0;
38911cb0ef41Sopenharmony_ci    }
38921cb0ef41Sopenharmony_ci
38931cb0ef41Sopenharmony_ci    if (!WPACKET_memcpy(pkt, s->session->session_id,
38941cb0ef41Sopenharmony_ci                        s->session->session_id_length)
38951cb0ef41Sopenharmony_ci            || !WPACKET_close(pkt)) {
38961cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
38971cb0ef41Sopenharmony_ci        return 0;
38981cb0ef41Sopenharmony_ci    }
38991cb0ef41Sopenharmony_ci
39001cb0ef41Sopenharmony_ci    return 1;
39011cb0ef41Sopenharmony_ci}
39021cb0ef41Sopenharmony_ci
39031cb0ef41Sopenharmony_cistatic void tls_update_ticket_counts(SSL *s)
39041cb0ef41Sopenharmony_ci{
39051cb0ef41Sopenharmony_ci    /*
39061cb0ef41Sopenharmony_ci     * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
39071cb0ef41Sopenharmony_ci     * gets reset to 0 if we send more tickets following a post-handshake
39081cb0ef41Sopenharmony_ci     * auth, but |next_ticket_nonce| does not.  If we're sending extra
39091cb0ef41Sopenharmony_ci     * tickets, decrement the count of pending extra tickets.
39101cb0ef41Sopenharmony_ci     */
39111cb0ef41Sopenharmony_ci    s->sent_tickets++;
39121cb0ef41Sopenharmony_ci    s->next_ticket_nonce++;
39131cb0ef41Sopenharmony_ci    if (s->ext.extra_tickets_expected > 0)
39141cb0ef41Sopenharmony_ci        s->ext.extra_tickets_expected--;
39151cb0ef41Sopenharmony_ci}
39161cb0ef41Sopenharmony_ci
39171cb0ef41Sopenharmony_ciint tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
39181cb0ef41Sopenharmony_ci{
39191cb0ef41Sopenharmony_ci    SSL_CTX *tctx = s->session_ctx;
39201cb0ef41Sopenharmony_ci    unsigned char tick_nonce[TICKET_NONCE_SIZE];
39211cb0ef41Sopenharmony_ci    union {
39221cb0ef41Sopenharmony_ci        unsigned char age_add_c[sizeof(uint32_t)];
39231cb0ef41Sopenharmony_ci        uint32_t age_add;
39241cb0ef41Sopenharmony_ci    } age_add_u;
39251cb0ef41Sopenharmony_ci    int ret = 0;
39261cb0ef41Sopenharmony_ci
39271cb0ef41Sopenharmony_ci    age_add_u.age_add = 0;
39281cb0ef41Sopenharmony_ci
39291cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
39301cb0ef41Sopenharmony_ci        size_t i, hashlen;
39311cb0ef41Sopenharmony_ci        uint64_t nonce;
39321cb0ef41Sopenharmony_ci        static const unsigned char nonce_label[] = "resumption";
39331cb0ef41Sopenharmony_ci        const EVP_MD *md = ssl_handshake_md(s);
39341cb0ef41Sopenharmony_ci        int hashleni = EVP_MD_get_size(md);
39351cb0ef41Sopenharmony_ci
39361cb0ef41Sopenharmony_ci        /* Ensure cast to size_t is safe */
39371cb0ef41Sopenharmony_ci        if (!ossl_assert(hashleni >= 0)) {
39381cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
39391cb0ef41Sopenharmony_ci            goto err;
39401cb0ef41Sopenharmony_ci        }
39411cb0ef41Sopenharmony_ci        hashlen = (size_t)hashleni;
39421cb0ef41Sopenharmony_ci
39431cb0ef41Sopenharmony_ci        /*
39441cb0ef41Sopenharmony_ci         * If we already sent one NewSessionTicket, or we resumed then
39451cb0ef41Sopenharmony_ci         * s->session may already be in a cache and so we must not modify it.
39461cb0ef41Sopenharmony_ci         * Instead we need to take a copy of it and modify that.
39471cb0ef41Sopenharmony_ci         */
39481cb0ef41Sopenharmony_ci        if (s->sent_tickets != 0 || s->hit) {
39491cb0ef41Sopenharmony_ci            SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
39501cb0ef41Sopenharmony_ci
39511cb0ef41Sopenharmony_ci            if (new_sess == NULL) {
39521cb0ef41Sopenharmony_ci                /* SSLfatal already called */
39531cb0ef41Sopenharmony_ci                goto err;
39541cb0ef41Sopenharmony_ci            }
39551cb0ef41Sopenharmony_ci
39561cb0ef41Sopenharmony_ci            SSL_SESSION_free(s->session);
39571cb0ef41Sopenharmony_ci            s->session = new_sess;
39581cb0ef41Sopenharmony_ci        }
39591cb0ef41Sopenharmony_ci
39601cb0ef41Sopenharmony_ci        if (!ssl_generate_session_id(s, s->session)) {
39611cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
39621cb0ef41Sopenharmony_ci            goto err;
39631cb0ef41Sopenharmony_ci        }
39641cb0ef41Sopenharmony_ci        if (RAND_bytes_ex(s->ctx->libctx, age_add_u.age_add_c,
39651cb0ef41Sopenharmony_ci                          sizeof(age_add_u), 0) <= 0) {
39661cb0ef41Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
39671cb0ef41Sopenharmony_ci            goto err;
39681cb0ef41Sopenharmony_ci        }
39691cb0ef41Sopenharmony_ci        s->session->ext.tick_age_add = age_add_u.age_add;
39701cb0ef41Sopenharmony_ci
39711cb0ef41Sopenharmony_ci        nonce = s->next_ticket_nonce;
39721cb0ef41Sopenharmony_ci        for (i = TICKET_NONCE_SIZE; i > 0; i--) {
39731cb0ef41Sopenharmony_ci            tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
39741cb0ef41Sopenharmony_ci            nonce >>= 8;
39751cb0ef41Sopenharmony_ci        }
39761cb0ef41Sopenharmony_ci
39771cb0ef41Sopenharmony_ci        if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
39781cb0ef41Sopenharmony_ci                               nonce_label,
39791cb0ef41Sopenharmony_ci                               sizeof(nonce_label) - 1,
39801cb0ef41Sopenharmony_ci                               tick_nonce,
39811cb0ef41Sopenharmony_ci                               TICKET_NONCE_SIZE,
39821cb0ef41Sopenharmony_ci                               s->session->master_key,
39831cb0ef41Sopenharmony_ci                               hashlen, 1)) {
39841cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
39851cb0ef41Sopenharmony_ci            goto err;
39861cb0ef41Sopenharmony_ci        }
39871cb0ef41Sopenharmony_ci        s->session->master_key_length = hashlen;
39881cb0ef41Sopenharmony_ci
39891cb0ef41Sopenharmony_ci        s->session->time = time(NULL);
39901cb0ef41Sopenharmony_ci        ssl_session_calculate_timeout(s->session);
39911cb0ef41Sopenharmony_ci        if (s->s3.alpn_selected != NULL) {
39921cb0ef41Sopenharmony_ci            OPENSSL_free(s->session->ext.alpn_selected);
39931cb0ef41Sopenharmony_ci            s->session->ext.alpn_selected =
39941cb0ef41Sopenharmony_ci                OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
39951cb0ef41Sopenharmony_ci            if (s->session->ext.alpn_selected == NULL) {
39961cb0ef41Sopenharmony_ci                s->session->ext.alpn_selected_len = 0;
39971cb0ef41Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
39981cb0ef41Sopenharmony_ci                goto err;
39991cb0ef41Sopenharmony_ci            }
40001cb0ef41Sopenharmony_ci            s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
40011cb0ef41Sopenharmony_ci        }
40021cb0ef41Sopenharmony_ci        s->session->ext.max_early_data = s->max_early_data;
40031cb0ef41Sopenharmony_ci    }
40041cb0ef41Sopenharmony_ci
40051cb0ef41Sopenharmony_ci    if (tctx->generate_ticket_cb != NULL &&
40061cb0ef41Sopenharmony_ci        tctx->generate_ticket_cb(s, tctx->ticket_cb_data) == 0) {
40071cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
40081cb0ef41Sopenharmony_ci        goto err;
40091cb0ef41Sopenharmony_ci    }
40101cb0ef41Sopenharmony_ci    /*
40111cb0ef41Sopenharmony_ci     * If we are using anti-replay protection then we behave as if
40121cb0ef41Sopenharmony_ci     * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
40131cb0ef41Sopenharmony_ci     * is no point in using full stateless tickets.
40141cb0ef41Sopenharmony_ci     */
40151cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)
40161cb0ef41Sopenharmony_ci            && ((s->options & SSL_OP_NO_TICKET) != 0
40171cb0ef41Sopenharmony_ci                || (s->max_early_data > 0
40181cb0ef41Sopenharmony_ci                    && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
40191cb0ef41Sopenharmony_ci        if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
40201cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
40211cb0ef41Sopenharmony_ci            goto err;
40221cb0ef41Sopenharmony_ci        }
40231cb0ef41Sopenharmony_ci    } else {
40241cb0ef41Sopenharmony_ci        int tmpret;
40251cb0ef41Sopenharmony_ci
40261cb0ef41Sopenharmony_ci        tmpret = construct_stateless_ticket(s, pkt, age_add_u.age_add,
40271cb0ef41Sopenharmony_ci                                            tick_nonce);
40281cb0ef41Sopenharmony_ci        if (tmpret != 1) {
40291cb0ef41Sopenharmony_ci            if (tmpret == 0) {
40301cb0ef41Sopenharmony_ci                ret = 2; /* Non-fatal. Abort construction but continue */
40311cb0ef41Sopenharmony_ci                /* We count this as a success so update the counts anwyay */
40321cb0ef41Sopenharmony_ci                tls_update_ticket_counts(s);
40331cb0ef41Sopenharmony_ci            }
40341cb0ef41Sopenharmony_ci            /* else SSLfatal() already called */
40351cb0ef41Sopenharmony_ci            goto err;
40361cb0ef41Sopenharmony_ci        }
40371cb0ef41Sopenharmony_ci    }
40381cb0ef41Sopenharmony_ci
40391cb0ef41Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
40401cb0ef41Sopenharmony_ci        if (!tls_construct_extensions(s, pkt,
40411cb0ef41Sopenharmony_ci                                      SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
40421cb0ef41Sopenharmony_ci                                      NULL, 0)) {
40431cb0ef41Sopenharmony_ci            /* SSLfatal() already called */
40441cb0ef41Sopenharmony_ci            goto err;
40451cb0ef41Sopenharmony_ci        }
40461cb0ef41Sopenharmony_ci        tls_update_ticket_counts(s);
40471cb0ef41Sopenharmony_ci        ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
40481cb0ef41Sopenharmony_ci    }
40491cb0ef41Sopenharmony_ci
40501cb0ef41Sopenharmony_ci    ret = 1;
40511cb0ef41Sopenharmony_ci err:
40521cb0ef41Sopenharmony_ci    return ret;
40531cb0ef41Sopenharmony_ci}
40541cb0ef41Sopenharmony_ci
40551cb0ef41Sopenharmony_ci/*
40561cb0ef41Sopenharmony_ci * In TLSv1.3 this is called from the extensions code, otherwise it is used to
40571cb0ef41Sopenharmony_ci * create a separate message. Returns 1 on success or 0 on failure.
40581cb0ef41Sopenharmony_ci */
40591cb0ef41Sopenharmony_ciint tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
40601cb0ef41Sopenharmony_ci{
40611cb0ef41Sopenharmony_ci    if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
40621cb0ef41Sopenharmony_ci            || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
40631cb0ef41Sopenharmony_ci                                       s->ext.ocsp.resp_len)) {
40641cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
40651cb0ef41Sopenharmony_ci        return 0;
40661cb0ef41Sopenharmony_ci    }
40671cb0ef41Sopenharmony_ci
40681cb0ef41Sopenharmony_ci    return 1;
40691cb0ef41Sopenharmony_ci}
40701cb0ef41Sopenharmony_ci
40711cb0ef41Sopenharmony_ciint tls_construct_cert_status(SSL *s, WPACKET *pkt)
40721cb0ef41Sopenharmony_ci{
40731cb0ef41Sopenharmony_ci    if (!tls_construct_cert_status_body(s, pkt)) {
40741cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
40751cb0ef41Sopenharmony_ci        return 0;
40761cb0ef41Sopenharmony_ci    }
40771cb0ef41Sopenharmony_ci
40781cb0ef41Sopenharmony_ci    return 1;
40791cb0ef41Sopenharmony_ci}
40801cb0ef41Sopenharmony_ci
40811cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_NEXTPROTONEG
40821cb0ef41Sopenharmony_ci/*
40831cb0ef41Sopenharmony_ci * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
40841cb0ef41Sopenharmony_ci * It sets the next_proto member in s if found
40851cb0ef41Sopenharmony_ci */
40861cb0ef41Sopenharmony_ciMSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
40871cb0ef41Sopenharmony_ci{
40881cb0ef41Sopenharmony_ci    PACKET next_proto, padding;
40891cb0ef41Sopenharmony_ci    size_t next_proto_len;
40901cb0ef41Sopenharmony_ci
40911cb0ef41Sopenharmony_ci    /*-
40921cb0ef41Sopenharmony_ci     * The payload looks like:
40931cb0ef41Sopenharmony_ci     *   uint8 proto_len;
40941cb0ef41Sopenharmony_ci     *   uint8 proto[proto_len];
40951cb0ef41Sopenharmony_ci     *   uint8 padding_len;
40961cb0ef41Sopenharmony_ci     *   uint8 padding[padding_len];
40971cb0ef41Sopenharmony_ci     */
40981cb0ef41Sopenharmony_ci    if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
40991cb0ef41Sopenharmony_ci        || !PACKET_get_length_prefixed_1(pkt, &padding)
41001cb0ef41Sopenharmony_ci        || PACKET_remaining(pkt) > 0) {
41011cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
41021cb0ef41Sopenharmony_ci        return MSG_PROCESS_ERROR;
41031cb0ef41Sopenharmony_ci    }
41041cb0ef41Sopenharmony_ci
41051cb0ef41Sopenharmony_ci    if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
41061cb0ef41Sopenharmony_ci        s->ext.npn_len = 0;
41071cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
41081cb0ef41Sopenharmony_ci        return MSG_PROCESS_ERROR;
41091cb0ef41Sopenharmony_ci    }
41101cb0ef41Sopenharmony_ci
41111cb0ef41Sopenharmony_ci    s->ext.npn_len = (unsigned char)next_proto_len;
41121cb0ef41Sopenharmony_ci
41131cb0ef41Sopenharmony_ci    return MSG_PROCESS_CONTINUE_READING;
41141cb0ef41Sopenharmony_ci}
41151cb0ef41Sopenharmony_ci#endif
41161cb0ef41Sopenharmony_ci
41171cb0ef41Sopenharmony_cistatic int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
41181cb0ef41Sopenharmony_ci{
41191cb0ef41Sopenharmony_ci    if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
41201cb0ef41Sopenharmony_ci                                  NULL, 0)) {
41211cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
41221cb0ef41Sopenharmony_ci        return 0;
41231cb0ef41Sopenharmony_ci    }
41241cb0ef41Sopenharmony_ci
41251cb0ef41Sopenharmony_ci    return 1;
41261cb0ef41Sopenharmony_ci}
41271cb0ef41Sopenharmony_ci
41281cb0ef41Sopenharmony_ciMSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt)
41291cb0ef41Sopenharmony_ci{
41301cb0ef41Sopenharmony_ci    if (PACKET_remaining(pkt) != 0) {
41311cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
41321cb0ef41Sopenharmony_ci        return MSG_PROCESS_ERROR;
41331cb0ef41Sopenharmony_ci    }
41341cb0ef41Sopenharmony_ci
41351cb0ef41Sopenharmony_ci    if (s->early_data_state != SSL_EARLY_DATA_READING
41361cb0ef41Sopenharmony_ci            && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
41371cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
41381cb0ef41Sopenharmony_ci        return MSG_PROCESS_ERROR;
41391cb0ef41Sopenharmony_ci    }
41401cb0ef41Sopenharmony_ci
41411cb0ef41Sopenharmony_ci    /*
41421cb0ef41Sopenharmony_ci     * EndOfEarlyData signals a key change so the end of the message must be on
41431cb0ef41Sopenharmony_ci     * a record boundary.
41441cb0ef41Sopenharmony_ci     */
41451cb0ef41Sopenharmony_ci    if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
41461cb0ef41Sopenharmony_ci        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
41471cb0ef41Sopenharmony_ci        return MSG_PROCESS_ERROR;
41481cb0ef41Sopenharmony_ci    }
41491cb0ef41Sopenharmony_ci
41501cb0ef41Sopenharmony_ci    s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
41511cb0ef41Sopenharmony_ci    if (!s->method->ssl3_enc->change_cipher_state(s,
41521cb0ef41Sopenharmony_ci                SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
41531cb0ef41Sopenharmony_ci        /* SSLfatal() already called */
41541cb0ef41Sopenharmony_ci        return MSG_PROCESS_ERROR;
41551cb0ef41Sopenharmony_ci    }
41561cb0ef41Sopenharmony_ci
41571cb0ef41Sopenharmony_ci    return MSG_PROCESS_CONTINUE_READING;
41581cb0ef41Sopenharmony_ci}
4159