1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4e1051a39Sopenharmony_ci *
5e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
6e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
7e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
8e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
9e1051a39Sopenharmony_ci */
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_ci#include <limits.h>
12e1051a39Sopenharmony_ci#include <string.h>
13e1051a39Sopenharmony_ci#include <stdio.h>
14e1051a39Sopenharmony_ci#include "../ssl_local.h"
15e1051a39Sopenharmony_ci#include "statem_local.h"
16e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
17e1051a39Sopenharmony_ci#include <openssl/buffer.h>
18e1051a39Sopenharmony_ci#include <openssl/objects.h>
19e1051a39Sopenharmony_ci#include <openssl/evp.h>
20e1051a39Sopenharmony_ci#include <openssl/rsa.h>
21e1051a39Sopenharmony_ci#include <openssl/x509.h>
22e1051a39Sopenharmony_ci#include <openssl/trace.h>
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ci/*
25e1051a39Sopenharmony_ci * Map error codes to TLS/SSL alart types.
26e1051a39Sopenharmony_ci */
27e1051a39Sopenharmony_citypedef struct x509err2alert_st {
28e1051a39Sopenharmony_ci    int x509err;
29e1051a39Sopenharmony_ci    int alert;
30e1051a39Sopenharmony_ci} X509ERR2ALERT;
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci/* Fixed value used in the ServerHello random field to identify an HRR */
33e1051a39Sopenharmony_ciconst unsigned char hrrrandom[] = {
34e1051a39Sopenharmony_ci    0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02,
35e1051a39Sopenharmony_ci    0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e,
36e1051a39Sopenharmony_ci    0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c
37e1051a39Sopenharmony_ci};
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci/*
40e1051a39Sopenharmony_ci * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
41e1051a39Sopenharmony_ci * SSL3_RT_CHANGE_CIPHER_SPEC)
42e1051a39Sopenharmony_ci */
43e1051a39Sopenharmony_ciint ssl3_do_write(SSL *s, int type)
44e1051a39Sopenharmony_ci{
45e1051a39Sopenharmony_ci    int ret;
46e1051a39Sopenharmony_ci    size_t written = 0;
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ci    ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
49e1051a39Sopenharmony_ci                           s->init_num, &written);
50e1051a39Sopenharmony_ci    if (ret < 0)
51e1051a39Sopenharmony_ci        return -1;
52e1051a39Sopenharmony_ci    if (type == SSL3_RT_HANDSHAKE)
53e1051a39Sopenharmony_ci        /*
54e1051a39Sopenharmony_ci         * should not be done for 'Hello Request's, but in that case we'll
55e1051a39Sopenharmony_ci         * ignore the result anyway
56e1051a39Sopenharmony_ci         * TLS1.3 KeyUpdate and NewSessionTicket do not need to be added
57e1051a39Sopenharmony_ci         */
58e1051a39Sopenharmony_ci        if (!SSL_IS_TLS13(s) || (s->statem.hand_state != TLS_ST_SW_SESSION_TICKET
59e1051a39Sopenharmony_ci                                 && s->statem.hand_state != TLS_ST_CW_KEY_UPDATE
60e1051a39Sopenharmony_ci                                 && s->statem.hand_state != TLS_ST_SW_KEY_UPDATE))
61e1051a39Sopenharmony_ci            if (!ssl3_finish_mac(s,
62e1051a39Sopenharmony_ci                                 (unsigned char *)&s->init_buf->data[s->init_off],
63e1051a39Sopenharmony_ci                                 written))
64e1051a39Sopenharmony_ci                return -1;
65e1051a39Sopenharmony_ci    if (written == s->init_num) {
66e1051a39Sopenharmony_ci        if (s->msg_callback)
67e1051a39Sopenharmony_ci            s->msg_callback(1, s->version, type, s->init_buf->data,
68e1051a39Sopenharmony_ci                            (size_t)(s->init_off + s->init_num), s,
69e1051a39Sopenharmony_ci                            s->msg_callback_arg);
70e1051a39Sopenharmony_ci        return 1;
71e1051a39Sopenharmony_ci    }
72e1051a39Sopenharmony_ci    s->init_off += written;
73e1051a39Sopenharmony_ci    s->init_num -= written;
74e1051a39Sopenharmony_ci    return 0;
75e1051a39Sopenharmony_ci}
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_ciint tls_close_construct_packet(SSL *s, WPACKET *pkt, int htype)
78e1051a39Sopenharmony_ci{
79e1051a39Sopenharmony_ci    size_t msglen;
80e1051a39Sopenharmony_ci
81e1051a39Sopenharmony_ci    if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
82e1051a39Sopenharmony_ci            || !WPACKET_get_length(pkt, &msglen)
83e1051a39Sopenharmony_ci            || msglen > INT_MAX)
84e1051a39Sopenharmony_ci        return 0;
85e1051a39Sopenharmony_ci    s->init_num = (int)msglen;
86e1051a39Sopenharmony_ci    s->init_off = 0;
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_ci    return 1;
89e1051a39Sopenharmony_ci}
90e1051a39Sopenharmony_ci
91e1051a39Sopenharmony_ciint tls_setup_handshake(SSL *s)
92e1051a39Sopenharmony_ci{
93e1051a39Sopenharmony_ci    int ver_min, ver_max, ok;
94e1051a39Sopenharmony_ci
95e1051a39Sopenharmony_ci    if (!ssl3_init_finished_mac(s)) {
96e1051a39Sopenharmony_ci        /* SSLfatal() already called */
97e1051a39Sopenharmony_ci        return 0;
98e1051a39Sopenharmony_ci    }
99e1051a39Sopenharmony_ci
100e1051a39Sopenharmony_ci    /* Reset any extension flags */
101e1051a39Sopenharmony_ci    memset(s->ext.extflags, 0, sizeof(s->ext.extflags));
102e1051a39Sopenharmony_ci
103e1051a39Sopenharmony_ci    if (ssl_get_min_max_version(s, &ver_min, &ver_max, NULL) != 0) {
104e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
105e1051a39Sopenharmony_ci        return 0;
106e1051a39Sopenharmony_ci    }
107e1051a39Sopenharmony_ci
108e1051a39Sopenharmony_ci    /* Sanity check that we have MD5-SHA1 if we need it */
109e1051a39Sopenharmony_ci    if (s->ctx->ssl_digest_methods[SSL_MD_MD5_SHA1_IDX] == NULL) {
110e1051a39Sopenharmony_ci        int md5sha1_needed = 0;
111e1051a39Sopenharmony_ci
112e1051a39Sopenharmony_ci        /* We don't have MD5-SHA1 - do we need it? */
113e1051a39Sopenharmony_ci        if (SSL_IS_DTLS(s)) {
114e1051a39Sopenharmony_ci            if (DTLS_VERSION_LE(ver_max, DTLS1_VERSION))
115e1051a39Sopenharmony_ci                md5sha1_needed = 1;
116e1051a39Sopenharmony_ci        } else {
117e1051a39Sopenharmony_ci            if (ver_max <= TLS1_1_VERSION)
118e1051a39Sopenharmony_ci                md5sha1_needed = 1;
119e1051a39Sopenharmony_ci        }
120e1051a39Sopenharmony_ci        if (md5sha1_needed) {
121e1051a39Sopenharmony_ci            SSLfatal_data(s, SSL_AD_HANDSHAKE_FAILURE,
122e1051a39Sopenharmony_ci                          SSL_R_NO_SUITABLE_DIGEST_ALGORITHM,
123e1051a39Sopenharmony_ci                          "The max supported SSL/TLS version needs the"
124e1051a39Sopenharmony_ci                          " MD5-SHA1 digest but it is not available"
125e1051a39Sopenharmony_ci                          " in the loaded providers. Use (D)TLSv1.2 or"
126e1051a39Sopenharmony_ci                          " above, or load different providers");
127e1051a39Sopenharmony_ci            return 0;
128e1051a39Sopenharmony_ci        }
129e1051a39Sopenharmony_ci
130e1051a39Sopenharmony_ci        ok = 1;
131e1051a39Sopenharmony_ci        /* Don't allow TLSv1.1 or below to be negotiated */
132e1051a39Sopenharmony_ci        if (SSL_IS_DTLS(s)) {
133e1051a39Sopenharmony_ci            if (DTLS_VERSION_LT(ver_min, DTLS1_2_VERSION))
134e1051a39Sopenharmony_ci                ok = SSL_set_min_proto_version(s, DTLS1_2_VERSION);
135e1051a39Sopenharmony_ci        } else {
136e1051a39Sopenharmony_ci            if (ver_min < TLS1_2_VERSION)
137e1051a39Sopenharmony_ci                ok = SSL_set_min_proto_version(s, TLS1_2_VERSION);
138e1051a39Sopenharmony_ci        }
139e1051a39Sopenharmony_ci        if (!ok) {
140e1051a39Sopenharmony_ci            /* Shouldn't happen */
141e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
142e1051a39Sopenharmony_ci            return 0;
143e1051a39Sopenharmony_ci        }
144e1051a39Sopenharmony_ci    }
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci    ok = 0;
147e1051a39Sopenharmony_ci    if (s->server) {
148e1051a39Sopenharmony_ci        STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(s);
149e1051a39Sopenharmony_ci        int i;
150e1051a39Sopenharmony_ci
151e1051a39Sopenharmony_ci        /*
152e1051a39Sopenharmony_ci         * Sanity check that the maximum version we accept has ciphers
153e1051a39Sopenharmony_ci         * enabled. For clients we do this check during construction of the
154e1051a39Sopenharmony_ci         * ClientHello.
155e1051a39Sopenharmony_ci         */
156e1051a39Sopenharmony_ci        for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
157e1051a39Sopenharmony_ci            const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
158e1051a39Sopenharmony_ci
159e1051a39Sopenharmony_ci            if (SSL_IS_DTLS(s)) {
160e1051a39Sopenharmony_ci                if (DTLS_VERSION_GE(ver_max, c->min_dtls) &&
161e1051a39Sopenharmony_ci                        DTLS_VERSION_LE(ver_max, c->max_dtls))
162e1051a39Sopenharmony_ci                    ok = 1;
163e1051a39Sopenharmony_ci            } else if (ver_max >= c->min_tls && ver_max <= c->max_tls) {
164e1051a39Sopenharmony_ci                ok = 1;
165e1051a39Sopenharmony_ci            }
166e1051a39Sopenharmony_ci            if (ok)
167e1051a39Sopenharmony_ci                break;
168e1051a39Sopenharmony_ci        }
169e1051a39Sopenharmony_ci        if (!ok) {
170e1051a39Sopenharmony_ci            SSLfatal_data(s, SSL_AD_HANDSHAKE_FAILURE,
171e1051a39Sopenharmony_ci                          SSL_R_NO_CIPHERS_AVAILABLE,
172e1051a39Sopenharmony_ci                          "No ciphers enabled for max supported "
173e1051a39Sopenharmony_ci                          "SSL/TLS version");
174e1051a39Sopenharmony_ci            return 0;
175e1051a39Sopenharmony_ci        }
176e1051a39Sopenharmony_ci        if (SSL_IS_FIRST_HANDSHAKE(s)) {
177e1051a39Sopenharmony_ci            /* N.B. s->session_ctx == s->ctx here */
178e1051a39Sopenharmony_ci            ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_accept);
179e1051a39Sopenharmony_ci        } else {
180e1051a39Sopenharmony_ci            /* N.B. s->ctx may not equal s->session_ctx */
181e1051a39Sopenharmony_ci            ssl_tsan_counter(s->ctx, &s->ctx->stats.sess_accept_renegotiate);
182e1051a39Sopenharmony_ci
183e1051a39Sopenharmony_ci            s->s3.tmp.cert_request = 0;
184e1051a39Sopenharmony_ci        }
185e1051a39Sopenharmony_ci    } else {
186e1051a39Sopenharmony_ci        if (SSL_IS_FIRST_HANDSHAKE(s))
187e1051a39Sopenharmony_ci            ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_connect);
188e1051a39Sopenharmony_ci        else
189e1051a39Sopenharmony_ci            ssl_tsan_counter(s->session_ctx,
190e1051a39Sopenharmony_ci                         &s->session_ctx->stats.sess_connect_renegotiate);
191e1051a39Sopenharmony_ci
192e1051a39Sopenharmony_ci        /* mark client_random uninitialized */
193e1051a39Sopenharmony_ci        memset(s->s3.client_random, 0, sizeof(s->s3.client_random));
194e1051a39Sopenharmony_ci        s->hit = 0;
195e1051a39Sopenharmony_ci
196e1051a39Sopenharmony_ci        s->s3.tmp.cert_req = 0;
197e1051a39Sopenharmony_ci
198e1051a39Sopenharmony_ci        if (SSL_IS_DTLS(s))
199e1051a39Sopenharmony_ci            s->statem.use_timer = 1;
200e1051a39Sopenharmony_ci    }
201e1051a39Sopenharmony_ci
202e1051a39Sopenharmony_ci    return 1;
203e1051a39Sopenharmony_ci}
204e1051a39Sopenharmony_ci
205e1051a39Sopenharmony_ci/*
206e1051a39Sopenharmony_ci * Size of the to-be-signed TLS13 data, without the hash size itself:
207e1051a39Sopenharmony_ci * 64 bytes of value 32, 33 context bytes, 1 byte separator
208e1051a39Sopenharmony_ci */
209e1051a39Sopenharmony_ci#define TLS13_TBS_START_SIZE            64
210e1051a39Sopenharmony_ci#define TLS13_TBS_PREAMBLE_SIZE         (TLS13_TBS_START_SIZE + 33 + 1)
211e1051a39Sopenharmony_ci
212e1051a39Sopenharmony_cistatic int get_cert_verify_tbs_data(SSL *s, unsigned char *tls13tbs,
213e1051a39Sopenharmony_ci                                    void **hdata, size_t *hdatalen)
214e1051a39Sopenharmony_ci{
215e1051a39Sopenharmony_ci#ifdef CHARSET_EBCDIC
216e1051a39Sopenharmony_ci    static const char servercontext[] = { 0x54, 0x4c, 0x53, 0x20, 0x31, 0x2e,
217e1051a39Sopenharmony_ci     0x33, 0x2c, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x43, 0x65,
218e1051a39Sopenharmony_ci     0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x56, 0x65, 0x72,
219e1051a39Sopenharmony_ci     0x69, 0x66, 0x79, 0x00 };
220e1051a39Sopenharmony_ci    static const char clientcontext[] = { 0x54, 0x4c, 0x53, 0x20, 0x31, 0x2e,
221e1051a39Sopenharmony_ci     0x33, 0x2c, 0x20, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x43, 0x65,
222e1051a39Sopenharmony_ci     0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x56, 0x65, 0x72,
223e1051a39Sopenharmony_ci     0x69, 0x66, 0x79, 0x00 };
224e1051a39Sopenharmony_ci#else
225e1051a39Sopenharmony_ci    static const char servercontext[] = "TLS 1.3, server CertificateVerify";
226e1051a39Sopenharmony_ci    static const char clientcontext[] = "TLS 1.3, client CertificateVerify";
227e1051a39Sopenharmony_ci#endif
228e1051a39Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
229e1051a39Sopenharmony_ci        size_t hashlen;
230e1051a39Sopenharmony_ci
231e1051a39Sopenharmony_ci        /* Set the first 64 bytes of to-be-signed data to octet 32 */
232e1051a39Sopenharmony_ci        memset(tls13tbs, 32, TLS13_TBS_START_SIZE);
233e1051a39Sopenharmony_ci        /* This copies the 33 bytes of context plus the 0 separator byte */
234e1051a39Sopenharmony_ci        if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
235e1051a39Sopenharmony_ci                 || s->statem.hand_state == TLS_ST_SW_CERT_VRFY)
236e1051a39Sopenharmony_ci            strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext);
237e1051a39Sopenharmony_ci        else
238e1051a39Sopenharmony_ci            strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext);
239e1051a39Sopenharmony_ci
240e1051a39Sopenharmony_ci        /*
241e1051a39Sopenharmony_ci         * If we're currently reading then we need to use the saved handshake
242e1051a39Sopenharmony_ci         * hash value. We can't use the current handshake hash state because
243e1051a39Sopenharmony_ci         * that includes the CertVerify itself.
244e1051a39Sopenharmony_ci         */
245e1051a39Sopenharmony_ci        if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
246e1051a39Sopenharmony_ci                || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) {
247e1051a39Sopenharmony_ci            memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash,
248e1051a39Sopenharmony_ci                   s->cert_verify_hash_len);
249e1051a39Sopenharmony_ci            hashlen = s->cert_verify_hash_len;
250e1051a39Sopenharmony_ci        } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE,
251e1051a39Sopenharmony_ci                                       EVP_MAX_MD_SIZE, &hashlen)) {
252e1051a39Sopenharmony_ci            /* SSLfatal() already called */
253e1051a39Sopenharmony_ci            return 0;
254e1051a39Sopenharmony_ci        }
255e1051a39Sopenharmony_ci
256e1051a39Sopenharmony_ci        *hdata = tls13tbs;
257e1051a39Sopenharmony_ci        *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen;
258e1051a39Sopenharmony_ci    } else {
259e1051a39Sopenharmony_ci        size_t retlen;
260e1051a39Sopenharmony_ci        long retlen_l;
261e1051a39Sopenharmony_ci
262e1051a39Sopenharmony_ci        retlen = retlen_l = BIO_get_mem_data(s->s3.handshake_buffer, hdata);
263e1051a39Sopenharmony_ci        if (retlen_l <= 0) {
264e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
265e1051a39Sopenharmony_ci            return 0;
266e1051a39Sopenharmony_ci        }
267e1051a39Sopenharmony_ci        *hdatalen = retlen;
268e1051a39Sopenharmony_ci    }
269e1051a39Sopenharmony_ci
270e1051a39Sopenharmony_ci    return 1;
271e1051a39Sopenharmony_ci}
272e1051a39Sopenharmony_ci
273e1051a39Sopenharmony_ciint tls_construct_cert_verify(SSL *s, WPACKET *pkt)
274e1051a39Sopenharmony_ci{
275e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
276e1051a39Sopenharmony_ci    const EVP_MD *md = NULL;
277e1051a39Sopenharmony_ci    EVP_MD_CTX *mctx = NULL;
278e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL;
279e1051a39Sopenharmony_ci    size_t hdatalen = 0, siglen = 0;
280e1051a39Sopenharmony_ci    void *hdata;
281e1051a39Sopenharmony_ci    unsigned char *sig = NULL;
282e1051a39Sopenharmony_ci    unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
283e1051a39Sopenharmony_ci    const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;
284e1051a39Sopenharmony_ci
285e1051a39Sopenharmony_ci    if (lu == NULL || s->s3.tmp.cert == NULL) {
286e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
287e1051a39Sopenharmony_ci        goto err;
288e1051a39Sopenharmony_ci    }
289e1051a39Sopenharmony_ci    pkey = s->s3.tmp.cert->privatekey;
290e1051a39Sopenharmony_ci
291e1051a39Sopenharmony_ci    if (pkey == NULL || !tls1_lookup_md(s->ctx, lu, &md)) {
292e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
293e1051a39Sopenharmony_ci        goto err;
294e1051a39Sopenharmony_ci    }
295e1051a39Sopenharmony_ci
296e1051a39Sopenharmony_ci    mctx = EVP_MD_CTX_new();
297e1051a39Sopenharmony_ci    if (mctx == NULL) {
298e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
299e1051a39Sopenharmony_ci        goto err;
300e1051a39Sopenharmony_ci    }
301e1051a39Sopenharmony_ci
302e1051a39Sopenharmony_ci    /* Get the data to be signed */
303e1051a39Sopenharmony_ci    if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
304e1051a39Sopenharmony_ci        /* SSLfatal() already called */
305e1051a39Sopenharmony_ci        goto err;
306e1051a39Sopenharmony_ci    }
307e1051a39Sopenharmony_ci
308e1051a39Sopenharmony_ci    if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
309e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
310e1051a39Sopenharmony_ci        goto err;
311e1051a39Sopenharmony_ci    }
312e1051a39Sopenharmony_ci
313e1051a39Sopenharmony_ci    if (EVP_DigestSignInit_ex(mctx, &pctx,
314e1051a39Sopenharmony_ci                              md == NULL ? NULL : EVP_MD_get0_name(md),
315e1051a39Sopenharmony_ci                              s->ctx->libctx, s->ctx->propq, pkey,
316e1051a39Sopenharmony_ci                              NULL) <= 0) {
317e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
318e1051a39Sopenharmony_ci        goto err;
319e1051a39Sopenharmony_ci    }
320e1051a39Sopenharmony_ci
321e1051a39Sopenharmony_ci    if (lu->sig == EVP_PKEY_RSA_PSS) {
322e1051a39Sopenharmony_ci        if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
323e1051a39Sopenharmony_ci            || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
324e1051a39Sopenharmony_ci                                                RSA_PSS_SALTLEN_DIGEST) <= 0) {
325e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
326e1051a39Sopenharmony_ci            goto err;
327e1051a39Sopenharmony_ci        }
328e1051a39Sopenharmony_ci    }
329e1051a39Sopenharmony_ci    if (s->version == SSL3_VERSION) {
330e1051a39Sopenharmony_ci        /*
331e1051a39Sopenharmony_ci         * Here we use EVP_DigestSignUpdate followed by EVP_DigestSignFinal
332e1051a39Sopenharmony_ci         * in order to add the EVP_CTRL_SSL3_MASTER_SECRET call between them.
333e1051a39Sopenharmony_ci         */
334e1051a39Sopenharmony_ci        if (EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0
335e1051a39Sopenharmony_ci            || EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
336e1051a39Sopenharmony_ci                               (int)s->session->master_key_length,
337e1051a39Sopenharmony_ci                               s->session->master_key) <= 0
338e1051a39Sopenharmony_ci            || EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0) {
339e1051a39Sopenharmony_ci
340e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
341e1051a39Sopenharmony_ci            goto err;
342e1051a39Sopenharmony_ci        }
343e1051a39Sopenharmony_ci        sig = OPENSSL_malloc(siglen);
344e1051a39Sopenharmony_ci        if (sig == NULL
345e1051a39Sopenharmony_ci                || EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) {
346e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
347e1051a39Sopenharmony_ci            goto err;
348e1051a39Sopenharmony_ci        }
349e1051a39Sopenharmony_ci    } else {
350e1051a39Sopenharmony_ci        /*
351e1051a39Sopenharmony_ci         * Here we *must* use EVP_DigestSign() because Ed25519/Ed448 does not
352e1051a39Sopenharmony_ci         * support streaming via EVP_DigestSignUpdate/EVP_DigestSignFinal
353e1051a39Sopenharmony_ci         */
354e1051a39Sopenharmony_ci        if (EVP_DigestSign(mctx, NULL, &siglen, hdata, hdatalen) <= 0) {
355e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
356e1051a39Sopenharmony_ci            goto err;
357e1051a39Sopenharmony_ci        }
358e1051a39Sopenharmony_ci        sig = OPENSSL_malloc(siglen);
359e1051a39Sopenharmony_ci        if (sig == NULL
360e1051a39Sopenharmony_ci                || EVP_DigestSign(mctx, sig, &siglen, hdata, hdatalen) <= 0) {
361e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
362e1051a39Sopenharmony_ci            goto err;
363e1051a39Sopenharmony_ci        }
364e1051a39Sopenharmony_ci    }
365e1051a39Sopenharmony_ci
366e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_GOST
367e1051a39Sopenharmony_ci    {
368e1051a39Sopenharmony_ci        int pktype = lu->sig;
369e1051a39Sopenharmony_ci
370e1051a39Sopenharmony_ci        if (pktype == NID_id_GostR3410_2001
371e1051a39Sopenharmony_ci            || pktype == NID_id_GostR3410_2012_256
372e1051a39Sopenharmony_ci            || pktype == NID_id_GostR3410_2012_512)
373e1051a39Sopenharmony_ci            BUF_reverse(sig, NULL, siglen);
374e1051a39Sopenharmony_ci    }
375e1051a39Sopenharmony_ci#endif
376e1051a39Sopenharmony_ci
377e1051a39Sopenharmony_ci    if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) {
378e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
379e1051a39Sopenharmony_ci        goto err;
380e1051a39Sopenharmony_ci    }
381e1051a39Sopenharmony_ci
382e1051a39Sopenharmony_ci    /* Digest cached records and discard handshake buffer */
383e1051a39Sopenharmony_ci    if (!ssl3_digest_cached_records(s, 0)) {
384e1051a39Sopenharmony_ci        /* SSLfatal() already called */
385e1051a39Sopenharmony_ci        goto err;
386e1051a39Sopenharmony_ci    }
387e1051a39Sopenharmony_ci
388e1051a39Sopenharmony_ci    OPENSSL_free(sig);
389e1051a39Sopenharmony_ci    EVP_MD_CTX_free(mctx);
390e1051a39Sopenharmony_ci    return 1;
391e1051a39Sopenharmony_ci err:
392e1051a39Sopenharmony_ci    OPENSSL_free(sig);
393e1051a39Sopenharmony_ci    EVP_MD_CTX_free(mctx);
394e1051a39Sopenharmony_ci    return 0;
395e1051a39Sopenharmony_ci}
396e1051a39Sopenharmony_ci
397e1051a39Sopenharmony_ciMSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
398e1051a39Sopenharmony_ci{
399e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
400e1051a39Sopenharmony_ci    const unsigned char *data;
401e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_GOST
402e1051a39Sopenharmony_ci    unsigned char *gost_data = NULL;
403e1051a39Sopenharmony_ci#endif
404e1051a39Sopenharmony_ci    MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
405e1051a39Sopenharmony_ci    int j;
406e1051a39Sopenharmony_ci    unsigned int len;
407e1051a39Sopenharmony_ci    X509 *peer;
408e1051a39Sopenharmony_ci    const EVP_MD *md = NULL;
409e1051a39Sopenharmony_ci    size_t hdatalen = 0;
410e1051a39Sopenharmony_ci    void *hdata;
411e1051a39Sopenharmony_ci    unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
412e1051a39Sopenharmony_ci    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
413e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL;
414e1051a39Sopenharmony_ci
415e1051a39Sopenharmony_ci    if (mctx == NULL) {
416e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
417e1051a39Sopenharmony_ci        goto err;
418e1051a39Sopenharmony_ci    }
419e1051a39Sopenharmony_ci
420e1051a39Sopenharmony_ci    peer = s->session->peer;
421e1051a39Sopenharmony_ci    pkey = X509_get0_pubkey(peer);
422e1051a39Sopenharmony_ci    if (pkey == NULL) {
423e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
424e1051a39Sopenharmony_ci        goto err;
425e1051a39Sopenharmony_ci    }
426e1051a39Sopenharmony_ci
427e1051a39Sopenharmony_ci    if (ssl_cert_lookup_by_pkey(pkey, NULL) == NULL) {
428e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
429e1051a39Sopenharmony_ci                 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
430e1051a39Sopenharmony_ci        goto err;
431e1051a39Sopenharmony_ci    }
432e1051a39Sopenharmony_ci
433e1051a39Sopenharmony_ci    if (SSL_USE_SIGALGS(s)) {
434e1051a39Sopenharmony_ci        unsigned int sigalg;
435e1051a39Sopenharmony_ci
436e1051a39Sopenharmony_ci        if (!PACKET_get_net_2(pkt, &sigalg)) {
437e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET);
438e1051a39Sopenharmony_ci            goto err;
439e1051a39Sopenharmony_ci        }
440e1051a39Sopenharmony_ci        if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {
441e1051a39Sopenharmony_ci            /* SSLfatal() already called */
442e1051a39Sopenharmony_ci            goto err;
443e1051a39Sopenharmony_ci        }
444e1051a39Sopenharmony_ci    } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
445e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
446e1051a39Sopenharmony_ci                     SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED);
447e1051a39Sopenharmony_ci            goto err;
448e1051a39Sopenharmony_ci    }
449e1051a39Sopenharmony_ci
450e1051a39Sopenharmony_ci    if (!tls1_lookup_md(s->ctx, s->s3.tmp.peer_sigalg, &md)) {
451e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
452e1051a39Sopenharmony_ci        goto err;
453e1051a39Sopenharmony_ci    }
454e1051a39Sopenharmony_ci
455e1051a39Sopenharmony_ci    if (SSL_USE_SIGALGS(s))
456e1051a39Sopenharmony_ci        OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n",
457e1051a39Sopenharmony_ci                    md == NULL ? "n/a" : EVP_MD_get0_name(md));
458e1051a39Sopenharmony_ci
459e1051a39Sopenharmony_ci    /* Check for broken implementations of GOST ciphersuites */
460e1051a39Sopenharmony_ci    /*
461e1051a39Sopenharmony_ci     * If key is GOST and len is exactly 64 or 128, it is signature without
462e1051a39Sopenharmony_ci     * length field (CryptoPro implementations at least till TLS 1.2)
463e1051a39Sopenharmony_ci     */
464e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_GOST
465e1051a39Sopenharmony_ci    if (!SSL_USE_SIGALGS(s)
466e1051a39Sopenharmony_ci        && ((PACKET_remaining(pkt) == 64
467e1051a39Sopenharmony_ci             && (EVP_PKEY_get_id(pkey) == NID_id_GostR3410_2001
468e1051a39Sopenharmony_ci                 || EVP_PKEY_get_id(pkey) == NID_id_GostR3410_2012_256))
469e1051a39Sopenharmony_ci            || (PACKET_remaining(pkt) == 128
470e1051a39Sopenharmony_ci                && EVP_PKEY_get_id(pkey) == NID_id_GostR3410_2012_512))) {
471e1051a39Sopenharmony_ci        len = PACKET_remaining(pkt);
472e1051a39Sopenharmony_ci    } else
473e1051a39Sopenharmony_ci#endif
474e1051a39Sopenharmony_ci    if (!PACKET_get_net_2(pkt, &len)) {
475e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
476e1051a39Sopenharmony_ci        goto err;
477e1051a39Sopenharmony_ci    }
478e1051a39Sopenharmony_ci
479e1051a39Sopenharmony_ci    if (!PACKET_get_bytes(pkt, &data, len)) {
480e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
481e1051a39Sopenharmony_ci        goto err;
482e1051a39Sopenharmony_ci    }
483e1051a39Sopenharmony_ci
484e1051a39Sopenharmony_ci    if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
485e1051a39Sopenharmony_ci        /* SSLfatal() already called */
486e1051a39Sopenharmony_ci        goto err;
487e1051a39Sopenharmony_ci    }
488e1051a39Sopenharmony_ci
489e1051a39Sopenharmony_ci    OSSL_TRACE1(TLS, "Using client verify alg %s\n",
490e1051a39Sopenharmony_ci                md == NULL ? "n/a" : EVP_MD_get0_name(md));
491e1051a39Sopenharmony_ci
492e1051a39Sopenharmony_ci    if (EVP_DigestVerifyInit_ex(mctx, &pctx,
493e1051a39Sopenharmony_ci                                md == NULL ? NULL : EVP_MD_get0_name(md),
494e1051a39Sopenharmony_ci                                s->ctx->libctx, s->ctx->propq, pkey,
495e1051a39Sopenharmony_ci                                NULL) <= 0) {
496e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
497e1051a39Sopenharmony_ci        goto err;
498e1051a39Sopenharmony_ci    }
499e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_GOST
500e1051a39Sopenharmony_ci    {
501e1051a39Sopenharmony_ci        int pktype = EVP_PKEY_get_id(pkey);
502e1051a39Sopenharmony_ci        if (pktype == NID_id_GostR3410_2001
503e1051a39Sopenharmony_ci            || pktype == NID_id_GostR3410_2012_256
504e1051a39Sopenharmony_ci            || pktype == NID_id_GostR3410_2012_512) {
505e1051a39Sopenharmony_ci            if ((gost_data = OPENSSL_malloc(len)) == NULL) {
506e1051a39Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
507e1051a39Sopenharmony_ci                goto err;
508e1051a39Sopenharmony_ci            }
509e1051a39Sopenharmony_ci            BUF_reverse(gost_data, data, len);
510e1051a39Sopenharmony_ci            data = gost_data;
511e1051a39Sopenharmony_ci        }
512e1051a39Sopenharmony_ci    }
513e1051a39Sopenharmony_ci#endif
514e1051a39Sopenharmony_ci
515e1051a39Sopenharmony_ci    if (SSL_USE_PSS(s)) {
516e1051a39Sopenharmony_ci        if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
517e1051a39Sopenharmony_ci            || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
518e1051a39Sopenharmony_ci                                                RSA_PSS_SALTLEN_DIGEST) <= 0) {
519e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
520e1051a39Sopenharmony_ci            goto err;
521e1051a39Sopenharmony_ci        }
522e1051a39Sopenharmony_ci    }
523e1051a39Sopenharmony_ci    if (s->version == SSL3_VERSION) {
524e1051a39Sopenharmony_ci        if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0
525e1051a39Sopenharmony_ci                || EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
526e1051a39Sopenharmony_ci                                   (int)s->session->master_key_length,
527e1051a39Sopenharmony_ci                                    s->session->master_key) <= 0) {
528e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
529e1051a39Sopenharmony_ci            goto err;
530e1051a39Sopenharmony_ci        }
531e1051a39Sopenharmony_ci        if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) {
532e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE);
533e1051a39Sopenharmony_ci            goto err;
534e1051a39Sopenharmony_ci        }
535e1051a39Sopenharmony_ci    } else {
536e1051a39Sopenharmony_ci        j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen);
537e1051a39Sopenharmony_ci        if (j <= 0) {
538e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE);
539e1051a39Sopenharmony_ci            goto err;
540e1051a39Sopenharmony_ci        }
541e1051a39Sopenharmony_ci    }
542e1051a39Sopenharmony_ci
543e1051a39Sopenharmony_ci    /*
544e1051a39Sopenharmony_ci     * In TLSv1.3 on the client side we make sure we prepare the client
545e1051a39Sopenharmony_ci     * certificate after the CertVerify instead of when we get the
546e1051a39Sopenharmony_ci     * CertificateRequest. This is because in TLSv1.3 the CertificateRequest
547e1051a39Sopenharmony_ci     * comes *before* the Certificate message. In TLSv1.2 it comes after. We
548e1051a39Sopenharmony_ci     * want to make sure that SSL_get1_peer_certificate() will return the actual
549e1051a39Sopenharmony_ci     * server certificate from the client_cert_cb callback.
550e1051a39Sopenharmony_ci     */
551e1051a39Sopenharmony_ci    if (!s->server && SSL_IS_TLS13(s) && s->s3.tmp.cert_req == 1)
552e1051a39Sopenharmony_ci        ret = MSG_PROCESS_CONTINUE_PROCESSING;
553e1051a39Sopenharmony_ci    else
554e1051a39Sopenharmony_ci        ret = MSG_PROCESS_CONTINUE_READING;
555e1051a39Sopenharmony_ci err:
556e1051a39Sopenharmony_ci    BIO_free(s->s3.handshake_buffer);
557e1051a39Sopenharmony_ci    s->s3.handshake_buffer = NULL;
558e1051a39Sopenharmony_ci    EVP_MD_CTX_free(mctx);
559e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_GOST
560e1051a39Sopenharmony_ci    OPENSSL_free(gost_data);
561e1051a39Sopenharmony_ci#endif
562e1051a39Sopenharmony_ci    return ret;
563e1051a39Sopenharmony_ci}
564e1051a39Sopenharmony_ci
565e1051a39Sopenharmony_ciint tls_construct_finished(SSL *s, WPACKET *pkt)
566e1051a39Sopenharmony_ci{
567e1051a39Sopenharmony_ci    size_t finish_md_len;
568e1051a39Sopenharmony_ci    const char *sender;
569e1051a39Sopenharmony_ci    size_t slen;
570e1051a39Sopenharmony_ci
571e1051a39Sopenharmony_ci    /* This is a real handshake so make sure we clean it up at the end */
572e1051a39Sopenharmony_ci    if (!s->server && s->post_handshake_auth != SSL_PHA_REQUESTED)
573e1051a39Sopenharmony_ci        s->statem.cleanuphand = 1;
574e1051a39Sopenharmony_ci
575e1051a39Sopenharmony_ci    /*
576e1051a39Sopenharmony_ci     * We only change the keys if we didn't already do this when we sent the
577e1051a39Sopenharmony_ci     * client certificate
578e1051a39Sopenharmony_ci     */
579e1051a39Sopenharmony_ci    if (SSL_IS_TLS13(s)
580e1051a39Sopenharmony_ci            && !s->server
581e1051a39Sopenharmony_ci            && s->s3.tmp.cert_req == 0
582e1051a39Sopenharmony_ci            && (!s->method->ssl3_enc->change_cipher_state(s,
583e1051a39Sopenharmony_ci                    SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {;
584e1051a39Sopenharmony_ci        /* SSLfatal() already called */
585e1051a39Sopenharmony_ci        return 0;
586e1051a39Sopenharmony_ci    }
587e1051a39Sopenharmony_ci
588e1051a39Sopenharmony_ci    if (s->server) {
589e1051a39Sopenharmony_ci        sender = s->method->ssl3_enc->server_finished_label;
590e1051a39Sopenharmony_ci        slen = s->method->ssl3_enc->server_finished_label_len;
591e1051a39Sopenharmony_ci    } else {
592e1051a39Sopenharmony_ci        sender = s->method->ssl3_enc->client_finished_label;
593e1051a39Sopenharmony_ci        slen = s->method->ssl3_enc->client_finished_label_len;
594e1051a39Sopenharmony_ci    }
595e1051a39Sopenharmony_ci
596e1051a39Sopenharmony_ci    finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
597e1051a39Sopenharmony_ci                                                          sender, slen,
598e1051a39Sopenharmony_ci                                                          s->s3.tmp.finish_md);
599e1051a39Sopenharmony_ci    if (finish_md_len == 0) {
600e1051a39Sopenharmony_ci        /* SSLfatal() already called */
601e1051a39Sopenharmony_ci        return 0;
602e1051a39Sopenharmony_ci    }
603e1051a39Sopenharmony_ci
604e1051a39Sopenharmony_ci    s->s3.tmp.finish_md_len = finish_md_len;
605e1051a39Sopenharmony_ci
606e1051a39Sopenharmony_ci    if (!WPACKET_memcpy(pkt, s->s3.tmp.finish_md, finish_md_len)) {
607e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
608e1051a39Sopenharmony_ci        return 0;
609e1051a39Sopenharmony_ci    }
610e1051a39Sopenharmony_ci
611e1051a39Sopenharmony_ci    /*
612e1051a39Sopenharmony_ci     * Log the master secret, if logging is enabled. We don't log it for
613e1051a39Sopenharmony_ci     * TLSv1.3: there's a different key schedule for that.
614e1051a39Sopenharmony_ci     */
615e1051a39Sopenharmony_ci    if (!SSL_IS_TLS13(s) && !ssl_log_secret(s, MASTER_SECRET_LABEL,
616e1051a39Sopenharmony_ci                                            s->session->master_key,
617e1051a39Sopenharmony_ci                                            s->session->master_key_length)) {
618e1051a39Sopenharmony_ci        /* SSLfatal() already called */
619e1051a39Sopenharmony_ci        return 0;
620e1051a39Sopenharmony_ci    }
621e1051a39Sopenharmony_ci
622e1051a39Sopenharmony_ci    /*
623e1051a39Sopenharmony_ci     * Copy the finished so we can use it for renegotiation checks
624e1051a39Sopenharmony_ci     */
625e1051a39Sopenharmony_ci    if (!ossl_assert(finish_md_len <= EVP_MAX_MD_SIZE)) {
626e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
627e1051a39Sopenharmony_ci        return 0;
628e1051a39Sopenharmony_ci    }
629e1051a39Sopenharmony_ci    if (!s->server) {
630e1051a39Sopenharmony_ci        memcpy(s->s3.previous_client_finished, s->s3.tmp.finish_md,
631e1051a39Sopenharmony_ci               finish_md_len);
632e1051a39Sopenharmony_ci        s->s3.previous_client_finished_len = finish_md_len;
633e1051a39Sopenharmony_ci    } else {
634e1051a39Sopenharmony_ci        memcpy(s->s3.previous_server_finished, s->s3.tmp.finish_md,
635e1051a39Sopenharmony_ci               finish_md_len);
636e1051a39Sopenharmony_ci        s->s3.previous_server_finished_len = finish_md_len;
637e1051a39Sopenharmony_ci    }
638e1051a39Sopenharmony_ci
639e1051a39Sopenharmony_ci    return 1;
640e1051a39Sopenharmony_ci}
641e1051a39Sopenharmony_ci
642e1051a39Sopenharmony_ciint tls_construct_key_update(SSL *s, WPACKET *pkt)
643e1051a39Sopenharmony_ci{
644e1051a39Sopenharmony_ci    if (!WPACKET_put_bytes_u8(pkt, s->key_update)) {
645e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
646e1051a39Sopenharmony_ci        return 0;
647e1051a39Sopenharmony_ci    }
648e1051a39Sopenharmony_ci
649e1051a39Sopenharmony_ci    s->key_update = SSL_KEY_UPDATE_NONE;
650e1051a39Sopenharmony_ci    return 1;
651e1051a39Sopenharmony_ci}
652e1051a39Sopenharmony_ci
653e1051a39Sopenharmony_ciMSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt)
654e1051a39Sopenharmony_ci{
655e1051a39Sopenharmony_ci    unsigned int updatetype;
656e1051a39Sopenharmony_ci
657e1051a39Sopenharmony_ci    /*
658e1051a39Sopenharmony_ci     * A KeyUpdate message signals a key change so the end of the message must
659e1051a39Sopenharmony_ci     * be on a record boundary.
660e1051a39Sopenharmony_ci     */
661e1051a39Sopenharmony_ci    if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
662e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
663e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
664e1051a39Sopenharmony_ci    }
665e1051a39Sopenharmony_ci
666e1051a39Sopenharmony_ci    if (!PACKET_get_1(pkt, &updatetype)
667e1051a39Sopenharmony_ci            || PACKET_remaining(pkt) != 0) {
668e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_KEY_UPDATE);
669e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
670e1051a39Sopenharmony_ci    }
671e1051a39Sopenharmony_ci
672e1051a39Sopenharmony_ci    /*
673e1051a39Sopenharmony_ci     * There are only two defined key update types. Fail if we get a value we
674e1051a39Sopenharmony_ci     * didn't recognise.
675e1051a39Sopenharmony_ci     */
676e1051a39Sopenharmony_ci    if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
677e1051a39Sopenharmony_ci            && updatetype != SSL_KEY_UPDATE_REQUESTED) {
678e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_UPDATE);
679e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
680e1051a39Sopenharmony_ci    }
681e1051a39Sopenharmony_ci
682e1051a39Sopenharmony_ci    /*
683e1051a39Sopenharmony_ci     * If we get a request for us to update our sending keys too then, we need
684e1051a39Sopenharmony_ci     * to additionally send a KeyUpdate message. However that message should
685e1051a39Sopenharmony_ci     * not also request an update (otherwise we get into an infinite loop).
686e1051a39Sopenharmony_ci     */
687e1051a39Sopenharmony_ci    if (updatetype == SSL_KEY_UPDATE_REQUESTED)
688e1051a39Sopenharmony_ci        s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED;
689e1051a39Sopenharmony_ci
690e1051a39Sopenharmony_ci    if (!tls13_update_key(s, 0)) {
691e1051a39Sopenharmony_ci        /* SSLfatal() already called */
692e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
693e1051a39Sopenharmony_ci    }
694e1051a39Sopenharmony_ci
695e1051a39Sopenharmony_ci    return MSG_PROCESS_FINISHED_READING;
696e1051a39Sopenharmony_ci}
697e1051a39Sopenharmony_ci
698e1051a39Sopenharmony_ci/*
699e1051a39Sopenharmony_ci * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
700e1051a39Sopenharmony_ci * to far.
701e1051a39Sopenharmony_ci */
702e1051a39Sopenharmony_ciint ssl3_take_mac(SSL *s)
703e1051a39Sopenharmony_ci{
704e1051a39Sopenharmony_ci    const char *sender;
705e1051a39Sopenharmony_ci    size_t slen;
706e1051a39Sopenharmony_ci
707e1051a39Sopenharmony_ci    if (!s->server) {
708e1051a39Sopenharmony_ci        sender = s->method->ssl3_enc->server_finished_label;
709e1051a39Sopenharmony_ci        slen = s->method->ssl3_enc->server_finished_label_len;
710e1051a39Sopenharmony_ci    } else {
711e1051a39Sopenharmony_ci        sender = s->method->ssl3_enc->client_finished_label;
712e1051a39Sopenharmony_ci        slen = s->method->ssl3_enc->client_finished_label_len;
713e1051a39Sopenharmony_ci    }
714e1051a39Sopenharmony_ci
715e1051a39Sopenharmony_ci    s->s3.tmp.peer_finish_md_len =
716e1051a39Sopenharmony_ci        s->method->ssl3_enc->final_finish_mac(s, sender, slen,
717e1051a39Sopenharmony_ci                                              s->s3.tmp.peer_finish_md);
718e1051a39Sopenharmony_ci
719e1051a39Sopenharmony_ci    if (s->s3.tmp.peer_finish_md_len == 0) {
720e1051a39Sopenharmony_ci        /* SSLfatal() already called */
721e1051a39Sopenharmony_ci        return 0;
722e1051a39Sopenharmony_ci    }
723e1051a39Sopenharmony_ci
724e1051a39Sopenharmony_ci    return 1;
725e1051a39Sopenharmony_ci}
726e1051a39Sopenharmony_ci
727e1051a39Sopenharmony_ciMSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
728e1051a39Sopenharmony_ci{
729e1051a39Sopenharmony_ci    size_t remain;
730e1051a39Sopenharmony_ci
731e1051a39Sopenharmony_ci    remain = PACKET_remaining(pkt);
732e1051a39Sopenharmony_ci    /*
733e1051a39Sopenharmony_ci     * 'Change Cipher Spec' is just a single byte, which should already have
734e1051a39Sopenharmony_ci     * been consumed by ssl_get_message() so there should be no bytes left,
735e1051a39Sopenharmony_ci     * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
736e1051a39Sopenharmony_ci     */
737e1051a39Sopenharmony_ci    if (SSL_IS_DTLS(s)) {
738e1051a39Sopenharmony_ci        if ((s->version == DTLS1_BAD_VER
739e1051a39Sopenharmony_ci             && remain != DTLS1_CCS_HEADER_LENGTH + 1)
740e1051a39Sopenharmony_ci            || (s->version != DTLS1_BAD_VER
741e1051a39Sopenharmony_ci                && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
742e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_CHANGE_CIPHER_SPEC);
743e1051a39Sopenharmony_ci            return MSG_PROCESS_ERROR;
744e1051a39Sopenharmony_ci        }
745e1051a39Sopenharmony_ci    } else {
746e1051a39Sopenharmony_ci        if (remain != 0) {
747e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_CHANGE_CIPHER_SPEC);
748e1051a39Sopenharmony_ci            return MSG_PROCESS_ERROR;
749e1051a39Sopenharmony_ci        }
750e1051a39Sopenharmony_ci    }
751e1051a39Sopenharmony_ci
752e1051a39Sopenharmony_ci    /* Check we have a cipher to change to */
753e1051a39Sopenharmony_ci    if (s->s3.tmp.new_cipher == NULL) {
754e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
755e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
756e1051a39Sopenharmony_ci    }
757e1051a39Sopenharmony_ci
758e1051a39Sopenharmony_ci    s->s3.change_cipher_spec = 1;
759e1051a39Sopenharmony_ci    if (!ssl3_do_change_cipher_spec(s)) {
760e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
761e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
762e1051a39Sopenharmony_ci    }
763e1051a39Sopenharmony_ci
764e1051a39Sopenharmony_ci    if (SSL_IS_DTLS(s)) {
765e1051a39Sopenharmony_ci        dtls1_reset_seq_numbers(s, SSL3_CC_READ);
766e1051a39Sopenharmony_ci
767e1051a39Sopenharmony_ci        if (s->version == DTLS1_BAD_VER)
768e1051a39Sopenharmony_ci            s->d1->handshake_read_seq++;
769e1051a39Sopenharmony_ci
770e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SCTP
771e1051a39Sopenharmony_ci        /*
772e1051a39Sopenharmony_ci         * Remember that a CCS has been received, so that an old key of
773e1051a39Sopenharmony_ci         * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
774e1051a39Sopenharmony_ci         * SCTP is used
775e1051a39Sopenharmony_ci         */
776e1051a39Sopenharmony_ci        BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
777e1051a39Sopenharmony_ci#endif
778e1051a39Sopenharmony_ci    }
779e1051a39Sopenharmony_ci
780e1051a39Sopenharmony_ci    return MSG_PROCESS_CONTINUE_READING;
781e1051a39Sopenharmony_ci}
782e1051a39Sopenharmony_ci
783e1051a39Sopenharmony_ciMSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
784e1051a39Sopenharmony_ci{
785e1051a39Sopenharmony_ci    size_t md_len;
786e1051a39Sopenharmony_ci
787e1051a39Sopenharmony_ci
788e1051a39Sopenharmony_ci    /* This is a real handshake so make sure we clean it up at the end */
789e1051a39Sopenharmony_ci    if (s->server) {
790e1051a39Sopenharmony_ci        /*
791e1051a39Sopenharmony_ci        * To get this far we must have read encrypted data from the client. We
792e1051a39Sopenharmony_ci        * no longer tolerate unencrypted alerts. This value is ignored if less
793e1051a39Sopenharmony_ci        * than TLSv1.3
794e1051a39Sopenharmony_ci        */
795e1051a39Sopenharmony_ci        s->statem.enc_read_state = ENC_READ_STATE_VALID;
796e1051a39Sopenharmony_ci        if (s->post_handshake_auth != SSL_PHA_REQUESTED)
797e1051a39Sopenharmony_ci            s->statem.cleanuphand = 1;
798e1051a39Sopenharmony_ci        if (SSL_IS_TLS13(s) && !tls13_save_handshake_digest_for_pha(s)) {
799e1051a39Sopenharmony_ci                /* SSLfatal() already called */
800e1051a39Sopenharmony_ci                return MSG_PROCESS_ERROR;
801e1051a39Sopenharmony_ci        }
802e1051a39Sopenharmony_ci    }
803e1051a39Sopenharmony_ci
804e1051a39Sopenharmony_ci    /*
805e1051a39Sopenharmony_ci     * In TLSv1.3 a Finished message signals a key change so the end of the
806e1051a39Sopenharmony_ci     * message must be on a record boundary.
807e1051a39Sopenharmony_ci     */
808e1051a39Sopenharmony_ci    if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
809e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
810e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
811e1051a39Sopenharmony_ci    }
812e1051a39Sopenharmony_ci
813e1051a39Sopenharmony_ci    /* If this occurs, we have missed a message */
814e1051a39Sopenharmony_ci    if (!SSL_IS_TLS13(s) && !s->s3.change_cipher_spec) {
815e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
816e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
817e1051a39Sopenharmony_ci    }
818e1051a39Sopenharmony_ci    s->s3.change_cipher_spec = 0;
819e1051a39Sopenharmony_ci
820e1051a39Sopenharmony_ci    md_len = s->s3.tmp.peer_finish_md_len;
821e1051a39Sopenharmony_ci
822e1051a39Sopenharmony_ci    if (md_len != PACKET_remaining(pkt)) {
823e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DIGEST_LENGTH);
824e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
825e1051a39Sopenharmony_ci    }
826e1051a39Sopenharmony_ci
827e1051a39Sopenharmony_ci    if (CRYPTO_memcmp(PACKET_data(pkt), s->s3.tmp.peer_finish_md,
828e1051a39Sopenharmony_ci                      md_len) != 0) {
829e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DIGEST_CHECK_FAILED);
830e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
831e1051a39Sopenharmony_ci    }
832e1051a39Sopenharmony_ci
833e1051a39Sopenharmony_ci    /*
834e1051a39Sopenharmony_ci     * Copy the finished so we can use it for renegotiation checks
835e1051a39Sopenharmony_ci     */
836e1051a39Sopenharmony_ci    if (!ossl_assert(md_len <= EVP_MAX_MD_SIZE)) {
837e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
838e1051a39Sopenharmony_ci        return MSG_PROCESS_ERROR;
839e1051a39Sopenharmony_ci    }
840e1051a39Sopenharmony_ci    if (s->server) {
841e1051a39Sopenharmony_ci        memcpy(s->s3.previous_client_finished, s->s3.tmp.peer_finish_md,
842e1051a39Sopenharmony_ci               md_len);
843e1051a39Sopenharmony_ci        s->s3.previous_client_finished_len = md_len;
844e1051a39Sopenharmony_ci    } else {
845e1051a39Sopenharmony_ci        memcpy(s->s3.previous_server_finished, s->s3.tmp.peer_finish_md,
846e1051a39Sopenharmony_ci               md_len);
847e1051a39Sopenharmony_ci        s->s3.previous_server_finished_len = md_len;
848e1051a39Sopenharmony_ci    }
849e1051a39Sopenharmony_ci
850e1051a39Sopenharmony_ci    /*
851e1051a39Sopenharmony_ci     * In TLS1.3 we also have to change cipher state and do any final processing
852e1051a39Sopenharmony_ci     * of the initial server flight (if we are a client)
853e1051a39Sopenharmony_ci     */
854e1051a39Sopenharmony_ci    if (SSL_IS_TLS13(s)) {
855e1051a39Sopenharmony_ci        if (s->server) {
856e1051a39Sopenharmony_ci            if (s->post_handshake_auth != SSL_PHA_REQUESTED &&
857e1051a39Sopenharmony_ci                    !s->method->ssl3_enc->change_cipher_state(s,
858e1051a39Sopenharmony_ci                    SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) {
859e1051a39Sopenharmony_ci                /* SSLfatal() already called */
860e1051a39Sopenharmony_ci                return MSG_PROCESS_ERROR;
861e1051a39Sopenharmony_ci            }
862e1051a39Sopenharmony_ci        } else {
863e1051a39Sopenharmony_ci            /* TLS 1.3 gets the secret size from the handshake md */
864e1051a39Sopenharmony_ci            size_t dummy;
865e1051a39Sopenharmony_ci            if (!s->method->ssl3_enc->generate_master_secret(s,
866e1051a39Sopenharmony_ci                    s->master_secret, s->handshake_secret, 0,
867e1051a39Sopenharmony_ci                    &dummy)) {
868e1051a39Sopenharmony_ci                /* SSLfatal() already called */
869e1051a39Sopenharmony_ci                return MSG_PROCESS_ERROR;
870e1051a39Sopenharmony_ci            }
871e1051a39Sopenharmony_ci            if (!s->method->ssl3_enc->change_cipher_state(s,
872e1051a39Sopenharmony_ci                    SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
873e1051a39Sopenharmony_ci                /* SSLfatal() already called */
874e1051a39Sopenharmony_ci                return MSG_PROCESS_ERROR;
875e1051a39Sopenharmony_ci            }
876e1051a39Sopenharmony_ci            if (!tls_process_initial_server_flight(s)) {
877e1051a39Sopenharmony_ci                /* SSLfatal() already called */
878e1051a39Sopenharmony_ci                return MSG_PROCESS_ERROR;
879e1051a39Sopenharmony_ci            }
880e1051a39Sopenharmony_ci        }
881e1051a39Sopenharmony_ci    }
882e1051a39Sopenharmony_ci
883e1051a39Sopenharmony_ci    return MSG_PROCESS_FINISHED_READING;
884e1051a39Sopenharmony_ci}
885e1051a39Sopenharmony_ci
886e1051a39Sopenharmony_ciint tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
887e1051a39Sopenharmony_ci{
888e1051a39Sopenharmony_ci    if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) {
889e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
890e1051a39Sopenharmony_ci        return 0;
891e1051a39Sopenharmony_ci    }
892e1051a39Sopenharmony_ci
893e1051a39Sopenharmony_ci    return 1;
894e1051a39Sopenharmony_ci}
895e1051a39Sopenharmony_ci
896e1051a39Sopenharmony_ci/* Add a certificate to the WPACKET */
897e1051a39Sopenharmony_cistatic int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain)
898e1051a39Sopenharmony_ci{
899e1051a39Sopenharmony_ci    int len;
900e1051a39Sopenharmony_ci    unsigned char *outbytes;
901e1051a39Sopenharmony_ci
902e1051a39Sopenharmony_ci    len = i2d_X509(x, NULL);
903e1051a39Sopenharmony_ci    if (len < 0) {
904e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB);
905e1051a39Sopenharmony_ci        return 0;
906e1051a39Sopenharmony_ci    }
907e1051a39Sopenharmony_ci    if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes)
908e1051a39Sopenharmony_ci            || i2d_X509(x, &outbytes) != len) {
909e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
910e1051a39Sopenharmony_ci        return 0;
911e1051a39Sopenharmony_ci    }
912e1051a39Sopenharmony_ci
913e1051a39Sopenharmony_ci    if (SSL_IS_TLS13(s)
914e1051a39Sopenharmony_ci            && !tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_CERTIFICATE, x,
915e1051a39Sopenharmony_ci                                         chain)) {
916e1051a39Sopenharmony_ci        /* SSLfatal() already called */
917e1051a39Sopenharmony_ci        return 0;
918e1051a39Sopenharmony_ci    }
919e1051a39Sopenharmony_ci
920e1051a39Sopenharmony_ci    return 1;
921e1051a39Sopenharmony_ci}
922e1051a39Sopenharmony_ci
923e1051a39Sopenharmony_ci/* Add certificate chain to provided WPACKET */
924e1051a39Sopenharmony_cistatic int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)
925e1051a39Sopenharmony_ci{
926e1051a39Sopenharmony_ci    int i, chain_count;
927e1051a39Sopenharmony_ci    X509 *x;
928e1051a39Sopenharmony_ci    STACK_OF(X509) *extra_certs;
929e1051a39Sopenharmony_ci    STACK_OF(X509) *chain = NULL;
930e1051a39Sopenharmony_ci    X509_STORE *chain_store;
931e1051a39Sopenharmony_ci
932e1051a39Sopenharmony_ci    if (cpk == NULL || cpk->x509 == NULL)
933e1051a39Sopenharmony_ci        return 1;
934e1051a39Sopenharmony_ci
935e1051a39Sopenharmony_ci    x = cpk->x509;
936e1051a39Sopenharmony_ci
937e1051a39Sopenharmony_ci    /*
938e1051a39Sopenharmony_ci     * If we have a certificate specific chain use it, else use parent ctx.
939e1051a39Sopenharmony_ci     */
940e1051a39Sopenharmony_ci    if (cpk->chain != NULL)
941e1051a39Sopenharmony_ci        extra_certs = cpk->chain;
942e1051a39Sopenharmony_ci    else
943e1051a39Sopenharmony_ci        extra_certs = s->ctx->extra_certs;
944e1051a39Sopenharmony_ci
945e1051a39Sopenharmony_ci    if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
946e1051a39Sopenharmony_ci        chain_store = NULL;
947e1051a39Sopenharmony_ci    else if (s->cert->chain_store)
948e1051a39Sopenharmony_ci        chain_store = s->cert->chain_store;
949e1051a39Sopenharmony_ci    else
950e1051a39Sopenharmony_ci        chain_store = s->ctx->cert_store;
951e1051a39Sopenharmony_ci
952e1051a39Sopenharmony_ci    if (chain_store != NULL) {
953e1051a39Sopenharmony_ci        X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new_ex(s->ctx->libctx,
954e1051a39Sopenharmony_ci                                                       s->ctx->propq);
955e1051a39Sopenharmony_ci
956e1051a39Sopenharmony_ci        if (xs_ctx == NULL) {
957e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
958e1051a39Sopenharmony_ci            return 0;
959e1051a39Sopenharmony_ci        }
960e1051a39Sopenharmony_ci        if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
961e1051a39Sopenharmony_ci            X509_STORE_CTX_free(xs_ctx);
962e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_X509_LIB);
963e1051a39Sopenharmony_ci            return 0;
964e1051a39Sopenharmony_ci        }
965e1051a39Sopenharmony_ci        /*
966e1051a39Sopenharmony_ci         * It is valid for the chain not to be complete (because normally we
967e1051a39Sopenharmony_ci         * don't include the root cert in the chain). Therefore we deliberately
968e1051a39Sopenharmony_ci         * ignore the error return from this call. We're not actually verifying
969e1051a39Sopenharmony_ci         * the cert - we're just building as much of the chain as we can
970e1051a39Sopenharmony_ci         */
971e1051a39Sopenharmony_ci        (void)X509_verify_cert(xs_ctx);
972e1051a39Sopenharmony_ci        /* Don't leave errors in the queue */
973e1051a39Sopenharmony_ci        ERR_clear_error();
974e1051a39Sopenharmony_ci        chain = X509_STORE_CTX_get0_chain(xs_ctx);
975e1051a39Sopenharmony_ci        i = ssl_security_cert_chain(s, chain, NULL, 0);
976e1051a39Sopenharmony_ci        if (i != 1) {
977e1051a39Sopenharmony_ci#if 0
978e1051a39Sopenharmony_ci            /* Dummy error calls so mkerr generates them */
979e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_SSL, SSL_R_EE_KEY_TOO_SMALL);
980e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_SSL, SSL_R_CA_KEY_TOO_SMALL);
981e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_SSL, SSL_R_CA_MD_TOO_WEAK);
982e1051a39Sopenharmony_ci#endif
983e1051a39Sopenharmony_ci            X509_STORE_CTX_free(xs_ctx);
984e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, i);
985e1051a39Sopenharmony_ci            return 0;
986e1051a39Sopenharmony_ci        }
987e1051a39Sopenharmony_ci        chain_count = sk_X509_num(chain);
988e1051a39Sopenharmony_ci        for (i = 0; i < chain_count; i++) {
989e1051a39Sopenharmony_ci            x = sk_X509_value(chain, i);
990e1051a39Sopenharmony_ci
991e1051a39Sopenharmony_ci            if (!ssl_add_cert_to_wpacket(s, pkt, x, i)) {
992e1051a39Sopenharmony_ci                /* SSLfatal() already called */
993e1051a39Sopenharmony_ci                X509_STORE_CTX_free(xs_ctx);
994e1051a39Sopenharmony_ci                return 0;
995e1051a39Sopenharmony_ci            }
996e1051a39Sopenharmony_ci        }
997e1051a39Sopenharmony_ci        X509_STORE_CTX_free(xs_ctx);
998e1051a39Sopenharmony_ci    } else {
999e1051a39Sopenharmony_ci        i = ssl_security_cert_chain(s, extra_certs, x, 0);
1000e1051a39Sopenharmony_ci        if (i != 1) {
1001e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, i);
1002e1051a39Sopenharmony_ci            return 0;
1003e1051a39Sopenharmony_ci        }
1004e1051a39Sopenharmony_ci        if (!ssl_add_cert_to_wpacket(s, pkt, x, 0)) {
1005e1051a39Sopenharmony_ci            /* SSLfatal() already called */
1006e1051a39Sopenharmony_ci            return 0;
1007e1051a39Sopenharmony_ci        }
1008e1051a39Sopenharmony_ci        for (i = 0; i < sk_X509_num(extra_certs); i++) {
1009e1051a39Sopenharmony_ci            x = sk_X509_value(extra_certs, i);
1010e1051a39Sopenharmony_ci            if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1)) {
1011e1051a39Sopenharmony_ci                /* SSLfatal() already called */
1012e1051a39Sopenharmony_ci                return 0;
1013e1051a39Sopenharmony_ci            }
1014e1051a39Sopenharmony_ci        }
1015e1051a39Sopenharmony_ci    }
1016e1051a39Sopenharmony_ci    return 1;
1017e1051a39Sopenharmony_ci}
1018e1051a39Sopenharmony_ci
1019e1051a39Sopenharmony_ciunsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)
1020e1051a39Sopenharmony_ci{
1021e1051a39Sopenharmony_ci    if (!WPACKET_start_sub_packet_u24(pkt)) {
1022e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1023e1051a39Sopenharmony_ci        return 0;
1024e1051a39Sopenharmony_ci    }
1025e1051a39Sopenharmony_ci
1026e1051a39Sopenharmony_ci    if (!ssl_add_cert_chain(s, pkt, cpk))
1027e1051a39Sopenharmony_ci        return 0;
1028e1051a39Sopenharmony_ci
1029e1051a39Sopenharmony_ci    if (!WPACKET_close(pkt)) {
1030e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1031e1051a39Sopenharmony_ci        return 0;
1032e1051a39Sopenharmony_ci    }
1033e1051a39Sopenharmony_ci
1034e1051a39Sopenharmony_ci    return 1;
1035e1051a39Sopenharmony_ci}
1036e1051a39Sopenharmony_ci
1037e1051a39Sopenharmony_ci/*
1038e1051a39Sopenharmony_ci * Tidy up after the end of a handshake. In the case of SCTP this may result
1039e1051a39Sopenharmony_ci * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
1040e1051a39Sopenharmony_ci * freed up as well.
1041e1051a39Sopenharmony_ci */
1042e1051a39Sopenharmony_ciWORK_STATE tls_finish_handshake(SSL *s, ossl_unused WORK_STATE wst,
1043e1051a39Sopenharmony_ci                                int clearbufs, int stop)
1044e1051a39Sopenharmony_ci{
1045e1051a39Sopenharmony_ci    void (*cb) (const SSL *ssl, int type, int val) = NULL;
1046e1051a39Sopenharmony_ci    int cleanuphand = s->statem.cleanuphand;
1047e1051a39Sopenharmony_ci
1048e1051a39Sopenharmony_ci    if (clearbufs) {
1049e1051a39Sopenharmony_ci        if (!SSL_IS_DTLS(s)
1050e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SCTP
1051e1051a39Sopenharmony_ci            /*
1052e1051a39Sopenharmony_ci             * RFC6083: SCTP provides a reliable and in-sequence transport service for DTLS
1053e1051a39Sopenharmony_ci             * messages that require it. Therefore, DTLS procedures for retransmissions
1054e1051a39Sopenharmony_ci             * MUST NOT be used.
1055e1051a39Sopenharmony_ci             * Hence the init_buf can be cleared when DTLS over SCTP as transport is used.
1056e1051a39Sopenharmony_ci             */
1057e1051a39Sopenharmony_ci            || BIO_dgram_is_sctp(SSL_get_wbio(s))
1058e1051a39Sopenharmony_ci#endif
1059e1051a39Sopenharmony_ci            ) {
1060e1051a39Sopenharmony_ci            /*
1061e1051a39Sopenharmony_ci             * We don't do this in DTLS over UDP because we may still need the init_buf
1062e1051a39Sopenharmony_ci             * in case there are any unexpected retransmits
1063e1051a39Sopenharmony_ci             */
1064e1051a39Sopenharmony_ci            BUF_MEM_free(s->init_buf);
1065e1051a39Sopenharmony_ci            s->init_buf = NULL;
1066e1051a39Sopenharmony_ci        }
1067e1051a39Sopenharmony_ci
1068e1051a39Sopenharmony_ci        if (!ssl_free_wbio_buffer(s)) {
1069e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1070e1051a39Sopenharmony_ci            return WORK_ERROR;
1071e1051a39Sopenharmony_ci        }
1072e1051a39Sopenharmony_ci        s->init_num = 0;
1073e1051a39Sopenharmony_ci    }
1074e1051a39Sopenharmony_ci
1075e1051a39Sopenharmony_ci    if (SSL_IS_TLS13(s) && !s->server
1076e1051a39Sopenharmony_ci            && s->post_handshake_auth == SSL_PHA_REQUESTED)
1077e1051a39Sopenharmony_ci        s->post_handshake_auth = SSL_PHA_EXT_SENT;
1078e1051a39Sopenharmony_ci
1079e1051a39Sopenharmony_ci    /*
1080e1051a39Sopenharmony_ci     * Only set if there was a Finished message and this isn't after a TLSv1.3
1081e1051a39Sopenharmony_ci     * post handshake exchange
1082e1051a39Sopenharmony_ci     */
1083e1051a39Sopenharmony_ci    if (cleanuphand) {
1084e1051a39Sopenharmony_ci        /* skipped if we just sent a HelloRequest */
1085e1051a39Sopenharmony_ci        s->renegotiate = 0;
1086e1051a39Sopenharmony_ci        s->new_session = 0;
1087e1051a39Sopenharmony_ci        s->statem.cleanuphand = 0;
1088e1051a39Sopenharmony_ci        s->ext.ticket_expected = 0;
1089e1051a39Sopenharmony_ci
1090e1051a39Sopenharmony_ci        ssl3_cleanup_key_block(s);
1091e1051a39Sopenharmony_ci
1092e1051a39Sopenharmony_ci        if (s->server) {
1093e1051a39Sopenharmony_ci            /*
1094e1051a39Sopenharmony_ci             * In TLSv1.3 we update the cache as part of constructing the
1095e1051a39Sopenharmony_ci             * NewSessionTicket
1096e1051a39Sopenharmony_ci             */
1097e1051a39Sopenharmony_ci            if (!SSL_IS_TLS13(s))
1098e1051a39Sopenharmony_ci                ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
1099e1051a39Sopenharmony_ci
1100e1051a39Sopenharmony_ci            /* N.B. s->ctx may not equal s->session_ctx */
1101e1051a39Sopenharmony_ci            ssl_tsan_counter(s->ctx, &s->ctx->stats.sess_accept_good);
1102e1051a39Sopenharmony_ci            s->handshake_func = ossl_statem_accept;
1103e1051a39Sopenharmony_ci        } else {
1104e1051a39Sopenharmony_ci            if (SSL_IS_TLS13(s)) {
1105e1051a39Sopenharmony_ci                /*
1106e1051a39Sopenharmony_ci                 * We encourage applications to only use TLSv1.3 tickets once,
1107e1051a39Sopenharmony_ci                 * so we remove this one from the cache.
1108e1051a39Sopenharmony_ci                 */
1109e1051a39Sopenharmony_ci                if ((s->session_ctx->session_cache_mode
1110e1051a39Sopenharmony_ci                     & SSL_SESS_CACHE_CLIENT) != 0)
1111e1051a39Sopenharmony_ci                    SSL_CTX_remove_session(s->session_ctx, s->session);
1112e1051a39Sopenharmony_ci            } else {
1113e1051a39Sopenharmony_ci                /*
1114e1051a39Sopenharmony_ci                 * In TLSv1.3 we update the cache as part of processing the
1115e1051a39Sopenharmony_ci                 * NewSessionTicket
1116e1051a39Sopenharmony_ci                 */
1117e1051a39Sopenharmony_ci                ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
1118e1051a39Sopenharmony_ci            }
1119e1051a39Sopenharmony_ci            if (s->hit)
1120e1051a39Sopenharmony_ci                ssl_tsan_counter(s->session_ctx,
1121e1051a39Sopenharmony_ci                                 &s->session_ctx->stats.sess_hit);
1122e1051a39Sopenharmony_ci
1123e1051a39Sopenharmony_ci            s->handshake_func = ossl_statem_connect;
1124e1051a39Sopenharmony_ci            ssl_tsan_counter(s->session_ctx,
1125e1051a39Sopenharmony_ci                             &s->session_ctx->stats.sess_connect_good);
1126e1051a39Sopenharmony_ci        }
1127e1051a39Sopenharmony_ci
1128e1051a39Sopenharmony_ci        if (SSL_IS_DTLS(s)) {
1129e1051a39Sopenharmony_ci            /* done with handshaking */
1130e1051a39Sopenharmony_ci            s->d1->handshake_read_seq = 0;
1131e1051a39Sopenharmony_ci            s->d1->handshake_write_seq = 0;
1132e1051a39Sopenharmony_ci            s->d1->next_handshake_write_seq = 0;
1133e1051a39Sopenharmony_ci            dtls1_clear_received_buffer(s);
1134e1051a39Sopenharmony_ci        }
1135e1051a39Sopenharmony_ci    }
1136e1051a39Sopenharmony_ci
1137e1051a39Sopenharmony_ci    if (s->info_callback != NULL)
1138e1051a39Sopenharmony_ci        cb = s->info_callback;
1139e1051a39Sopenharmony_ci    else if (s->ctx->info_callback != NULL)
1140e1051a39Sopenharmony_ci        cb = s->ctx->info_callback;
1141e1051a39Sopenharmony_ci
1142e1051a39Sopenharmony_ci    /* The callback may expect us to not be in init at handshake done */
1143e1051a39Sopenharmony_ci    ossl_statem_set_in_init(s, 0);
1144e1051a39Sopenharmony_ci
1145e1051a39Sopenharmony_ci    if (cb != NULL) {
1146e1051a39Sopenharmony_ci        if (cleanuphand
1147e1051a39Sopenharmony_ci                || !SSL_IS_TLS13(s)
1148e1051a39Sopenharmony_ci                || SSL_IS_FIRST_HANDSHAKE(s))
1149e1051a39Sopenharmony_ci            cb(s, SSL_CB_HANDSHAKE_DONE, 1);
1150e1051a39Sopenharmony_ci    }
1151e1051a39Sopenharmony_ci
1152e1051a39Sopenharmony_ci    if (!stop) {
1153e1051a39Sopenharmony_ci        /* If we've got more work to do we go back into init */
1154e1051a39Sopenharmony_ci        ossl_statem_set_in_init(s, 1);
1155e1051a39Sopenharmony_ci        return WORK_FINISHED_CONTINUE;
1156e1051a39Sopenharmony_ci    }
1157e1051a39Sopenharmony_ci
1158e1051a39Sopenharmony_ci    return WORK_FINISHED_STOP;
1159e1051a39Sopenharmony_ci}
1160e1051a39Sopenharmony_ci
1161e1051a39Sopenharmony_ciint tls_get_message_header(SSL *s, int *mt)
1162e1051a39Sopenharmony_ci{
1163e1051a39Sopenharmony_ci    /* s->init_num < SSL3_HM_HEADER_LENGTH */
1164e1051a39Sopenharmony_ci    int skip_message, i, recvd_type;
1165e1051a39Sopenharmony_ci    unsigned char *p;
1166e1051a39Sopenharmony_ci    size_t l, readbytes;
1167e1051a39Sopenharmony_ci
1168e1051a39Sopenharmony_ci    p = (unsigned char *)s->init_buf->data;
1169e1051a39Sopenharmony_ci
1170e1051a39Sopenharmony_ci    do {
1171e1051a39Sopenharmony_ci        while (s->init_num < SSL3_HM_HEADER_LENGTH) {
1172e1051a39Sopenharmony_ci            i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
1173e1051a39Sopenharmony_ci                                          &p[s->init_num],
1174e1051a39Sopenharmony_ci                                          SSL3_HM_HEADER_LENGTH - s->init_num,
1175e1051a39Sopenharmony_ci                                          0, &readbytes);
1176e1051a39Sopenharmony_ci            if (i <= 0) {
1177e1051a39Sopenharmony_ci                s->rwstate = SSL_READING;
1178e1051a39Sopenharmony_ci                return 0;
1179e1051a39Sopenharmony_ci            }
1180e1051a39Sopenharmony_ci            if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1181e1051a39Sopenharmony_ci                /*
1182e1051a39Sopenharmony_ci                 * A ChangeCipherSpec must be a single byte and may not occur
1183e1051a39Sopenharmony_ci                 * in the middle of a handshake message.
1184e1051a39Sopenharmony_ci                 */
1185e1051a39Sopenharmony_ci                if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) {
1186e1051a39Sopenharmony_ci                    SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1187e1051a39Sopenharmony_ci                             SSL_R_BAD_CHANGE_CIPHER_SPEC);
1188e1051a39Sopenharmony_ci                    return 0;
1189e1051a39Sopenharmony_ci                }
1190e1051a39Sopenharmony_ci                if (s->statem.hand_state == TLS_ST_BEFORE
1191e1051a39Sopenharmony_ci                        && (s->s3.flags & TLS1_FLAGS_STATELESS) != 0) {
1192e1051a39Sopenharmony_ci                    /*
1193e1051a39Sopenharmony_ci                     * We are stateless and we received a CCS. Probably this is
1194e1051a39Sopenharmony_ci                     * from a client between the first and second ClientHellos.
1195e1051a39Sopenharmony_ci                     * We should ignore this, but return an error because we do
1196e1051a39Sopenharmony_ci                     * not return success until we see the second ClientHello
1197e1051a39Sopenharmony_ci                     * with a valid cookie.
1198e1051a39Sopenharmony_ci                     */
1199e1051a39Sopenharmony_ci                    return 0;
1200e1051a39Sopenharmony_ci                }
1201e1051a39Sopenharmony_ci                s->s3.tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1202e1051a39Sopenharmony_ci                s->init_num = readbytes - 1;
1203e1051a39Sopenharmony_ci                s->init_msg = s->init_buf->data;
1204e1051a39Sopenharmony_ci                s->s3.tmp.message_size = readbytes;
1205e1051a39Sopenharmony_ci                return 1;
1206e1051a39Sopenharmony_ci            } else if (recvd_type != SSL3_RT_HANDSHAKE) {
1207e1051a39Sopenharmony_ci                SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1208e1051a39Sopenharmony_ci                         SSL_R_CCS_RECEIVED_EARLY);
1209e1051a39Sopenharmony_ci                return 0;
1210e1051a39Sopenharmony_ci            }
1211e1051a39Sopenharmony_ci            s->init_num += readbytes;
1212e1051a39Sopenharmony_ci        }
1213e1051a39Sopenharmony_ci
1214e1051a39Sopenharmony_ci        skip_message = 0;
1215e1051a39Sopenharmony_ci        if (!s->server)
1216e1051a39Sopenharmony_ci            if (s->statem.hand_state != TLS_ST_OK
1217e1051a39Sopenharmony_ci                    && p[0] == SSL3_MT_HELLO_REQUEST)
1218e1051a39Sopenharmony_ci                /*
1219e1051a39Sopenharmony_ci                 * The server may always send 'Hello Request' messages --
1220e1051a39Sopenharmony_ci                 * we are doing a handshake anyway now, so ignore them if
1221e1051a39Sopenharmony_ci                 * their format is correct. Does not count for 'Finished'
1222e1051a39Sopenharmony_ci                 * MAC.
1223e1051a39Sopenharmony_ci                 */
1224e1051a39Sopenharmony_ci                if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
1225e1051a39Sopenharmony_ci                    s->init_num = 0;
1226e1051a39Sopenharmony_ci                    skip_message = 1;
1227e1051a39Sopenharmony_ci
1228e1051a39Sopenharmony_ci                    if (s->msg_callback)
1229e1051a39Sopenharmony_ci                        s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
1230e1051a39Sopenharmony_ci                                        p, SSL3_HM_HEADER_LENGTH, s,
1231e1051a39Sopenharmony_ci                                        s->msg_callback_arg);
1232e1051a39Sopenharmony_ci                }
1233e1051a39Sopenharmony_ci    } while (skip_message);
1234e1051a39Sopenharmony_ci    /* s->init_num == SSL3_HM_HEADER_LENGTH */
1235e1051a39Sopenharmony_ci
1236e1051a39Sopenharmony_ci    *mt = *p;
1237e1051a39Sopenharmony_ci    s->s3.tmp.message_type = *(p++);
1238e1051a39Sopenharmony_ci
1239e1051a39Sopenharmony_ci    if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1240e1051a39Sopenharmony_ci        /*
1241e1051a39Sopenharmony_ci         * Only happens with SSLv3+ in an SSLv2 backward compatible
1242e1051a39Sopenharmony_ci         * ClientHello
1243e1051a39Sopenharmony_ci         *
1244e1051a39Sopenharmony_ci         * Total message size is the remaining record bytes to read
1245e1051a39Sopenharmony_ci         * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
1246e1051a39Sopenharmony_ci         */
1247e1051a39Sopenharmony_ci        l = RECORD_LAYER_get_rrec_length(&s->rlayer)
1248e1051a39Sopenharmony_ci            + SSL3_HM_HEADER_LENGTH;
1249e1051a39Sopenharmony_ci        s->s3.tmp.message_size = l;
1250e1051a39Sopenharmony_ci
1251e1051a39Sopenharmony_ci        s->init_msg = s->init_buf->data;
1252e1051a39Sopenharmony_ci        s->init_num = SSL3_HM_HEADER_LENGTH;
1253e1051a39Sopenharmony_ci    } else {
1254e1051a39Sopenharmony_ci        n2l3(p, l);
1255e1051a39Sopenharmony_ci        /* BUF_MEM_grow takes an 'int' parameter */
1256e1051a39Sopenharmony_ci        if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
1257e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1258e1051a39Sopenharmony_ci                     SSL_R_EXCESSIVE_MESSAGE_SIZE);
1259e1051a39Sopenharmony_ci            return 0;
1260e1051a39Sopenharmony_ci        }
1261e1051a39Sopenharmony_ci        s->s3.tmp.message_size = l;
1262e1051a39Sopenharmony_ci
1263e1051a39Sopenharmony_ci        s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
1264e1051a39Sopenharmony_ci        s->init_num = 0;
1265e1051a39Sopenharmony_ci    }
1266e1051a39Sopenharmony_ci
1267e1051a39Sopenharmony_ci    return 1;
1268e1051a39Sopenharmony_ci}
1269e1051a39Sopenharmony_ci
1270e1051a39Sopenharmony_ciint tls_get_message_body(SSL *s, size_t *len)
1271e1051a39Sopenharmony_ci{
1272e1051a39Sopenharmony_ci    size_t n, readbytes;
1273e1051a39Sopenharmony_ci    unsigned char *p;
1274e1051a39Sopenharmony_ci    int i;
1275e1051a39Sopenharmony_ci
1276e1051a39Sopenharmony_ci    if (s->s3.tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
1277e1051a39Sopenharmony_ci        /* We've already read everything in */
1278e1051a39Sopenharmony_ci        *len = (unsigned long)s->init_num;
1279e1051a39Sopenharmony_ci        return 1;
1280e1051a39Sopenharmony_ci    }
1281e1051a39Sopenharmony_ci
1282e1051a39Sopenharmony_ci    p = s->init_msg;
1283e1051a39Sopenharmony_ci    n = s->s3.tmp.message_size - s->init_num;
1284e1051a39Sopenharmony_ci    while (n > 0) {
1285e1051a39Sopenharmony_ci        i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
1286e1051a39Sopenharmony_ci                                      &p[s->init_num], n, 0, &readbytes);
1287e1051a39Sopenharmony_ci        if (i <= 0) {
1288e1051a39Sopenharmony_ci            s->rwstate = SSL_READING;
1289e1051a39Sopenharmony_ci            *len = 0;
1290e1051a39Sopenharmony_ci            return 0;
1291e1051a39Sopenharmony_ci        }
1292e1051a39Sopenharmony_ci        s->init_num += readbytes;
1293e1051a39Sopenharmony_ci        n -= readbytes;
1294e1051a39Sopenharmony_ci    }
1295e1051a39Sopenharmony_ci
1296e1051a39Sopenharmony_ci    /*
1297e1051a39Sopenharmony_ci     * If receiving Finished, record MAC of prior handshake messages for
1298e1051a39Sopenharmony_ci     * Finished verification.
1299e1051a39Sopenharmony_ci     */
1300e1051a39Sopenharmony_ci    if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
1301e1051a39Sopenharmony_ci        /* SSLfatal() already called */
1302e1051a39Sopenharmony_ci        *len = 0;
1303e1051a39Sopenharmony_ci        return 0;
1304e1051a39Sopenharmony_ci    }
1305e1051a39Sopenharmony_ci
1306e1051a39Sopenharmony_ci    /* Feed this message into MAC computation. */
1307e1051a39Sopenharmony_ci    if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1308e1051a39Sopenharmony_ci        if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1309e1051a39Sopenharmony_ci                             s->init_num)) {
1310e1051a39Sopenharmony_ci            /* SSLfatal() already called */
1311e1051a39Sopenharmony_ci            *len = 0;
1312e1051a39Sopenharmony_ci            return 0;
1313e1051a39Sopenharmony_ci        }
1314e1051a39Sopenharmony_ci        if (s->msg_callback)
1315e1051a39Sopenharmony_ci            s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
1316e1051a39Sopenharmony_ci                            (size_t)s->init_num, s, s->msg_callback_arg);
1317e1051a39Sopenharmony_ci    } else {
1318e1051a39Sopenharmony_ci        /*
1319e1051a39Sopenharmony_ci         * We defer feeding in the HRR until later. We'll do it as part of
1320e1051a39Sopenharmony_ci         * processing the message
1321e1051a39Sopenharmony_ci         * The TLsv1.3 handshake transcript stops at the ClientFinished
1322e1051a39Sopenharmony_ci         * message.
1323e1051a39Sopenharmony_ci         */
1324e1051a39Sopenharmony_ci#define SERVER_HELLO_RANDOM_OFFSET  (SSL3_HM_HEADER_LENGTH + 2)
1325e1051a39Sopenharmony_ci        /* KeyUpdate and NewSessionTicket do not need to be added */
1326e1051a39Sopenharmony_ci        if (!SSL_IS_TLS13(s) || (s->s3.tmp.message_type != SSL3_MT_NEWSESSION_TICKET
1327e1051a39Sopenharmony_ci                                 && s->s3.tmp.message_type != SSL3_MT_KEY_UPDATE)) {
1328e1051a39Sopenharmony_ci            if (s->s3.tmp.message_type != SSL3_MT_SERVER_HELLO
1329e1051a39Sopenharmony_ci                    || s->init_num < SERVER_HELLO_RANDOM_OFFSET + SSL3_RANDOM_SIZE
1330e1051a39Sopenharmony_ci                    || memcmp(hrrrandom,
1331e1051a39Sopenharmony_ci                              s->init_buf->data + SERVER_HELLO_RANDOM_OFFSET,
1332e1051a39Sopenharmony_ci                              SSL3_RANDOM_SIZE) != 0) {
1333e1051a39Sopenharmony_ci                if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1334e1051a39Sopenharmony_ci                                     s->init_num + SSL3_HM_HEADER_LENGTH)) {
1335e1051a39Sopenharmony_ci                    /* SSLfatal() already called */
1336e1051a39Sopenharmony_ci                    *len = 0;
1337e1051a39Sopenharmony_ci                    return 0;
1338e1051a39Sopenharmony_ci                }
1339e1051a39Sopenharmony_ci            }
1340e1051a39Sopenharmony_ci        }
1341e1051a39Sopenharmony_ci        if (s->msg_callback)
1342e1051a39Sopenharmony_ci            s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
1343e1051a39Sopenharmony_ci                            (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
1344e1051a39Sopenharmony_ci                            s->msg_callback_arg);
1345e1051a39Sopenharmony_ci    }
1346e1051a39Sopenharmony_ci
1347e1051a39Sopenharmony_ci    *len = s->init_num;
1348e1051a39Sopenharmony_ci    return 1;
1349e1051a39Sopenharmony_ci}
1350e1051a39Sopenharmony_ci
1351e1051a39Sopenharmony_cistatic const X509ERR2ALERT x509table[] = {
1352e1051a39Sopenharmony_ci    {X509_V_ERR_APPLICATION_VERIFICATION, SSL_AD_HANDSHAKE_FAILURE},
1353e1051a39Sopenharmony_ci    {X509_V_ERR_CA_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
1354e1051a39Sopenharmony_ci    {X509_V_ERR_EC_KEY_EXPLICIT_PARAMS, SSL_AD_BAD_CERTIFICATE},
1355e1051a39Sopenharmony_ci    {X509_V_ERR_CA_MD_TOO_WEAK, SSL_AD_BAD_CERTIFICATE},
1356e1051a39Sopenharmony_ci    {X509_V_ERR_CERT_CHAIN_TOO_LONG, SSL_AD_UNKNOWN_CA},
1357e1051a39Sopenharmony_ci    {X509_V_ERR_CERT_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
1358e1051a39Sopenharmony_ci    {X509_V_ERR_CERT_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
1359e1051a39Sopenharmony_ci    {X509_V_ERR_CERT_REJECTED, SSL_AD_BAD_CERTIFICATE},
1360e1051a39Sopenharmony_ci    {X509_V_ERR_CERT_REVOKED, SSL_AD_CERTIFICATE_REVOKED},
1361e1051a39Sopenharmony_ci    {X509_V_ERR_CERT_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
1362e1051a39Sopenharmony_ci    {X509_V_ERR_CERT_UNTRUSTED, SSL_AD_BAD_CERTIFICATE},
1363e1051a39Sopenharmony_ci    {X509_V_ERR_CRL_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
1364e1051a39Sopenharmony_ci    {X509_V_ERR_CRL_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
1365e1051a39Sopenharmony_ci    {X509_V_ERR_CRL_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
1366e1051a39Sopenharmony_ci    {X509_V_ERR_DANE_NO_MATCH, SSL_AD_BAD_CERTIFICATE},
1367e1051a39Sopenharmony_ci    {X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT, SSL_AD_UNKNOWN_CA},
1368e1051a39Sopenharmony_ci    {X509_V_ERR_EE_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
1369e1051a39Sopenharmony_ci    {X509_V_ERR_EMAIL_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1370e1051a39Sopenharmony_ci    {X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD, SSL_AD_BAD_CERTIFICATE},
1371e1051a39Sopenharmony_ci    {X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD, SSL_AD_BAD_CERTIFICATE},
1372e1051a39Sopenharmony_ci    {X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
1373e1051a39Sopenharmony_ci    {X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
1374e1051a39Sopenharmony_ci    {X509_V_ERR_HOSTNAME_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1375e1051a39Sopenharmony_ci    {X509_V_ERR_INVALID_CA, SSL_AD_UNKNOWN_CA},
1376e1051a39Sopenharmony_ci    {X509_V_ERR_INVALID_CALL, SSL_AD_INTERNAL_ERROR},
1377e1051a39Sopenharmony_ci    {X509_V_ERR_INVALID_PURPOSE, SSL_AD_UNSUPPORTED_CERTIFICATE},
1378e1051a39Sopenharmony_ci    {X509_V_ERR_IP_ADDRESS_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1379e1051a39Sopenharmony_ci    {X509_V_ERR_OUT_OF_MEM, SSL_AD_INTERNAL_ERROR},
1380e1051a39Sopenharmony_ci    {X509_V_ERR_PATH_LENGTH_EXCEEDED, SSL_AD_UNKNOWN_CA},
1381e1051a39Sopenharmony_ci    {X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN, SSL_AD_UNKNOWN_CA},
1382e1051a39Sopenharmony_ci    {X509_V_ERR_STORE_LOOKUP, SSL_AD_INTERNAL_ERROR},
1383e1051a39Sopenharmony_ci    {X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, SSL_AD_BAD_CERTIFICATE},
1384e1051a39Sopenharmony_ci    {X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
1385e1051a39Sopenharmony_ci    {X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
1386e1051a39Sopenharmony_ci    {X509_V_ERR_UNABLE_TO_GET_CRL, SSL_AD_UNKNOWN_CA},
1387e1051a39Sopenharmony_ci    {X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER, SSL_AD_UNKNOWN_CA},
1388e1051a39Sopenharmony_ci    {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT, SSL_AD_UNKNOWN_CA},
1389e1051a39Sopenharmony_ci    {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, SSL_AD_UNKNOWN_CA},
1390e1051a39Sopenharmony_ci    {X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE, SSL_AD_UNKNOWN_CA},
1391e1051a39Sopenharmony_ci    {X509_V_ERR_UNSPECIFIED, SSL_AD_INTERNAL_ERROR},
1392e1051a39Sopenharmony_ci
1393e1051a39Sopenharmony_ci    /* Last entry; return this if we don't find the value above. */
1394e1051a39Sopenharmony_ci    {X509_V_OK, SSL_AD_CERTIFICATE_UNKNOWN}
1395e1051a39Sopenharmony_ci};
1396e1051a39Sopenharmony_ci
1397e1051a39Sopenharmony_ciint ssl_x509err2alert(int x509err)
1398e1051a39Sopenharmony_ci{
1399e1051a39Sopenharmony_ci    const X509ERR2ALERT *tp;
1400e1051a39Sopenharmony_ci
1401e1051a39Sopenharmony_ci    for (tp = x509table; tp->x509err != X509_V_OK; ++tp)
1402e1051a39Sopenharmony_ci        if (tp->x509err == x509err)
1403e1051a39Sopenharmony_ci            break;
1404e1051a39Sopenharmony_ci    return tp->alert;
1405e1051a39Sopenharmony_ci}
1406e1051a39Sopenharmony_ci
1407e1051a39Sopenharmony_ciint ssl_allow_compression(SSL *s)
1408e1051a39Sopenharmony_ci{
1409e1051a39Sopenharmony_ci    if (s->options & SSL_OP_NO_COMPRESSION)
1410e1051a39Sopenharmony_ci        return 0;
1411e1051a39Sopenharmony_ci    return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
1412e1051a39Sopenharmony_ci}
1413e1051a39Sopenharmony_ci
1414e1051a39Sopenharmony_cistatic int version_cmp(const SSL *s, int a, int b)
1415e1051a39Sopenharmony_ci{
1416e1051a39Sopenharmony_ci    int dtls = SSL_IS_DTLS(s);
1417e1051a39Sopenharmony_ci
1418e1051a39Sopenharmony_ci    if (a == b)
1419e1051a39Sopenharmony_ci        return 0;
1420e1051a39Sopenharmony_ci    if (!dtls)
1421e1051a39Sopenharmony_ci        return a < b ? -1 : 1;
1422e1051a39Sopenharmony_ci    return DTLS_VERSION_LT(a, b) ? -1 : 1;
1423e1051a39Sopenharmony_ci}
1424e1051a39Sopenharmony_ci
1425e1051a39Sopenharmony_citypedef struct {
1426e1051a39Sopenharmony_ci    int version;
1427e1051a39Sopenharmony_ci    const SSL_METHOD *(*cmeth) (void);
1428e1051a39Sopenharmony_ci    const SSL_METHOD *(*smeth) (void);
1429e1051a39Sopenharmony_ci} version_info;
1430e1051a39Sopenharmony_ci
1431e1051a39Sopenharmony_ci#if TLS_MAX_VERSION_INTERNAL != TLS1_3_VERSION
1432e1051a39Sopenharmony_ci# error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1433e1051a39Sopenharmony_ci#endif
1434e1051a39Sopenharmony_ci
1435e1051a39Sopenharmony_ci/* Must be in order high to low */
1436e1051a39Sopenharmony_cistatic const version_info tls_version_table[] = {
1437e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_3
1438e1051a39Sopenharmony_ci    {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method},
1439e1051a39Sopenharmony_ci#else
1440e1051a39Sopenharmony_ci    {TLS1_3_VERSION, NULL, NULL},
1441e1051a39Sopenharmony_ci#endif
1442e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
1443e1051a39Sopenharmony_ci    {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method},
1444e1051a39Sopenharmony_ci#else
1445e1051a39Sopenharmony_ci    {TLS1_2_VERSION, NULL, NULL},
1446e1051a39Sopenharmony_ci#endif
1447e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_1
1448e1051a39Sopenharmony_ci    {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method},
1449e1051a39Sopenharmony_ci#else
1450e1051a39Sopenharmony_ci    {TLS1_1_VERSION, NULL, NULL},
1451e1051a39Sopenharmony_ci#endif
1452e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1
1453e1051a39Sopenharmony_ci    {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method},
1454e1051a39Sopenharmony_ci#else
1455e1051a39Sopenharmony_ci    {TLS1_VERSION, NULL, NULL},
1456e1051a39Sopenharmony_ci#endif
1457e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SSL3
1458e1051a39Sopenharmony_ci    {SSL3_VERSION, sslv3_client_method, sslv3_server_method},
1459e1051a39Sopenharmony_ci#else
1460e1051a39Sopenharmony_ci    {SSL3_VERSION, NULL, NULL},
1461e1051a39Sopenharmony_ci#endif
1462e1051a39Sopenharmony_ci    {0, NULL, NULL},
1463e1051a39Sopenharmony_ci};
1464e1051a39Sopenharmony_ci
1465e1051a39Sopenharmony_ci#if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
1466e1051a39Sopenharmony_ci# error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1467e1051a39Sopenharmony_ci#endif
1468e1051a39Sopenharmony_ci
1469e1051a39Sopenharmony_ci/* Must be in order high to low */
1470e1051a39Sopenharmony_cistatic const version_info dtls_version_table[] = {
1471e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DTLS1_2
1472e1051a39Sopenharmony_ci    {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method},
1473e1051a39Sopenharmony_ci#else
1474e1051a39Sopenharmony_ci    {DTLS1_2_VERSION, NULL, NULL},
1475e1051a39Sopenharmony_ci#endif
1476e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DTLS1
1477e1051a39Sopenharmony_ci    {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method},
1478e1051a39Sopenharmony_ci    {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL},
1479e1051a39Sopenharmony_ci#else
1480e1051a39Sopenharmony_ci    {DTLS1_VERSION, NULL, NULL},
1481e1051a39Sopenharmony_ci    {DTLS1_BAD_VER, NULL, NULL},
1482e1051a39Sopenharmony_ci#endif
1483e1051a39Sopenharmony_ci    {0, NULL, NULL},
1484e1051a39Sopenharmony_ci};
1485e1051a39Sopenharmony_ci
1486e1051a39Sopenharmony_ci/*
1487e1051a39Sopenharmony_ci * ssl_method_error - Check whether an SSL_METHOD is enabled.
1488e1051a39Sopenharmony_ci *
1489e1051a39Sopenharmony_ci * @s: The SSL handle for the candidate method
1490e1051a39Sopenharmony_ci * @method: the intended method.
1491e1051a39Sopenharmony_ci *
1492e1051a39Sopenharmony_ci * Returns 0 on success, or an SSL error reason on failure.
1493e1051a39Sopenharmony_ci */
1494e1051a39Sopenharmony_cistatic int ssl_method_error(const SSL *s, const SSL_METHOD *method)
1495e1051a39Sopenharmony_ci{
1496e1051a39Sopenharmony_ci    int version = method->version;
1497e1051a39Sopenharmony_ci
1498e1051a39Sopenharmony_ci    if ((s->min_proto_version != 0 &&
1499e1051a39Sopenharmony_ci         version_cmp(s, version, s->min_proto_version) < 0) ||
1500e1051a39Sopenharmony_ci        ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
1501e1051a39Sopenharmony_ci        return SSL_R_VERSION_TOO_LOW;
1502e1051a39Sopenharmony_ci
1503e1051a39Sopenharmony_ci    if (s->max_proto_version != 0 &&
1504e1051a39Sopenharmony_ci        version_cmp(s, version, s->max_proto_version) > 0)
1505e1051a39Sopenharmony_ci        return SSL_R_VERSION_TOO_HIGH;
1506e1051a39Sopenharmony_ci
1507e1051a39Sopenharmony_ci    if ((s->options & method->mask) != 0)
1508e1051a39Sopenharmony_ci        return SSL_R_UNSUPPORTED_PROTOCOL;
1509e1051a39Sopenharmony_ci    if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
1510e1051a39Sopenharmony_ci        return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
1511e1051a39Sopenharmony_ci
1512e1051a39Sopenharmony_ci    return 0;
1513e1051a39Sopenharmony_ci}
1514e1051a39Sopenharmony_ci
1515e1051a39Sopenharmony_ci/*
1516e1051a39Sopenharmony_ci * Only called by servers. Returns 1 if the server has a TLSv1.3 capable
1517e1051a39Sopenharmony_ci * certificate type, or has PSK or a certificate callback configured, or has
1518e1051a39Sopenharmony_ci * a servername callback configure. Otherwise returns 0.
1519e1051a39Sopenharmony_ci */
1520e1051a39Sopenharmony_cistatic int is_tls13_capable(const SSL *s)
1521e1051a39Sopenharmony_ci{
1522e1051a39Sopenharmony_ci    int i;
1523e1051a39Sopenharmony_ci    int curve;
1524e1051a39Sopenharmony_ci
1525e1051a39Sopenharmony_ci    if (!ossl_assert(s->ctx != NULL) || !ossl_assert(s->session_ctx != NULL))
1526e1051a39Sopenharmony_ci        return 0;
1527e1051a39Sopenharmony_ci
1528e1051a39Sopenharmony_ci    /*
1529e1051a39Sopenharmony_ci     * A servername callback can change the available certs, so if a servername
1530e1051a39Sopenharmony_ci     * cb is set then we just assume TLSv1.3 will be ok
1531e1051a39Sopenharmony_ci     */
1532e1051a39Sopenharmony_ci    if (s->ctx->ext.servername_cb != NULL
1533e1051a39Sopenharmony_ci            || s->session_ctx->ext.servername_cb != NULL)
1534e1051a39Sopenharmony_ci        return 1;
1535e1051a39Sopenharmony_ci
1536e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_PSK
1537e1051a39Sopenharmony_ci    if (s->psk_server_callback != NULL)
1538e1051a39Sopenharmony_ci        return 1;
1539e1051a39Sopenharmony_ci#endif
1540e1051a39Sopenharmony_ci
1541e1051a39Sopenharmony_ci    if (s->psk_find_session_cb != NULL || s->cert->cert_cb != NULL)
1542e1051a39Sopenharmony_ci        return 1;
1543e1051a39Sopenharmony_ci
1544e1051a39Sopenharmony_ci    for (i = 0; i < SSL_PKEY_NUM; i++) {
1545e1051a39Sopenharmony_ci        /* Skip over certs disallowed for TLSv1.3 */
1546e1051a39Sopenharmony_ci        switch (i) {
1547e1051a39Sopenharmony_ci        case SSL_PKEY_DSA_SIGN:
1548e1051a39Sopenharmony_ci        case SSL_PKEY_GOST01:
1549e1051a39Sopenharmony_ci        case SSL_PKEY_GOST12_256:
1550e1051a39Sopenharmony_ci        case SSL_PKEY_GOST12_512:
1551e1051a39Sopenharmony_ci            continue;
1552e1051a39Sopenharmony_ci        default:
1553e1051a39Sopenharmony_ci            break;
1554e1051a39Sopenharmony_ci        }
1555e1051a39Sopenharmony_ci        if (!ssl_has_cert(s, i))
1556e1051a39Sopenharmony_ci            continue;
1557e1051a39Sopenharmony_ci        if (i != SSL_PKEY_ECC)
1558e1051a39Sopenharmony_ci            return 1;
1559e1051a39Sopenharmony_ci        /*
1560e1051a39Sopenharmony_ci         * Prior to TLSv1.3 sig algs allowed any curve to be used. TLSv1.3 is
1561e1051a39Sopenharmony_ci         * more restrictive so check that our sig algs are consistent with this
1562e1051a39Sopenharmony_ci         * EC cert. See section 4.2.3 of RFC8446.
1563e1051a39Sopenharmony_ci         */
1564e1051a39Sopenharmony_ci        curve = ssl_get_EC_curve_nid(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
1565e1051a39Sopenharmony_ci        if (tls_check_sigalg_curve(s, curve))
1566e1051a39Sopenharmony_ci            return 1;
1567e1051a39Sopenharmony_ci    }
1568e1051a39Sopenharmony_ci
1569e1051a39Sopenharmony_ci    return 0;
1570e1051a39Sopenharmony_ci}
1571e1051a39Sopenharmony_ci
1572e1051a39Sopenharmony_ci/*
1573e1051a39Sopenharmony_ci * ssl_version_supported - Check that the specified `version` is supported by
1574e1051a39Sopenharmony_ci * `SSL *` instance
1575e1051a39Sopenharmony_ci *
1576e1051a39Sopenharmony_ci * @s: The SSL handle for the candidate method
1577e1051a39Sopenharmony_ci * @version: Protocol version to test against
1578e1051a39Sopenharmony_ci *
1579e1051a39Sopenharmony_ci * Returns 1 when supported, otherwise 0
1580e1051a39Sopenharmony_ci */
1581e1051a39Sopenharmony_ciint ssl_version_supported(const SSL *s, int version, const SSL_METHOD **meth)
1582e1051a39Sopenharmony_ci{
1583e1051a39Sopenharmony_ci    const version_info *vent;
1584e1051a39Sopenharmony_ci    const version_info *table;
1585e1051a39Sopenharmony_ci
1586e1051a39Sopenharmony_ci    switch (s->method->version) {
1587e1051a39Sopenharmony_ci    default:
1588e1051a39Sopenharmony_ci        /* Version should match method version for non-ANY method */
1589e1051a39Sopenharmony_ci        return version_cmp(s, version, s->version) == 0;
1590e1051a39Sopenharmony_ci    case TLS_ANY_VERSION:
1591e1051a39Sopenharmony_ci        table = tls_version_table;
1592e1051a39Sopenharmony_ci        break;
1593e1051a39Sopenharmony_ci    case DTLS_ANY_VERSION:
1594e1051a39Sopenharmony_ci        table = dtls_version_table;
1595e1051a39Sopenharmony_ci        break;
1596e1051a39Sopenharmony_ci    }
1597e1051a39Sopenharmony_ci
1598e1051a39Sopenharmony_ci    for (vent = table;
1599e1051a39Sopenharmony_ci         vent->version != 0 && version_cmp(s, version, vent->version) <= 0;
1600e1051a39Sopenharmony_ci         ++vent) {
1601e1051a39Sopenharmony_ci        if (vent->cmeth != NULL
1602e1051a39Sopenharmony_ci                && version_cmp(s, version, vent->version) == 0
1603e1051a39Sopenharmony_ci                && ssl_method_error(s, vent->cmeth()) == 0
1604e1051a39Sopenharmony_ci                && (!s->server
1605e1051a39Sopenharmony_ci                    || version != TLS1_3_VERSION
1606e1051a39Sopenharmony_ci                    || is_tls13_capable(s))) {
1607e1051a39Sopenharmony_ci            if (meth != NULL)
1608e1051a39Sopenharmony_ci                *meth = vent->cmeth();
1609e1051a39Sopenharmony_ci            return 1;
1610e1051a39Sopenharmony_ci        }
1611e1051a39Sopenharmony_ci    }
1612e1051a39Sopenharmony_ci    return 0;
1613e1051a39Sopenharmony_ci}
1614e1051a39Sopenharmony_ci
1615e1051a39Sopenharmony_ci/*
1616e1051a39Sopenharmony_ci * ssl_check_version_downgrade - In response to RFC7507 SCSV version
1617e1051a39Sopenharmony_ci * fallback indication from a client check whether we're using the highest
1618e1051a39Sopenharmony_ci * supported protocol version.
1619e1051a39Sopenharmony_ci *
1620e1051a39Sopenharmony_ci * @s server SSL handle.
1621e1051a39Sopenharmony_ci *
1622e1051a39Sopenharmony_ci * Returns 1 when using the highest enabled version, 0 otherwise.
1623e1051a39Sopenharmony_ci */
1624e1051a39Sopenharmony_ciint ssl_check_version_downgrade(SSL *s)
1625e1051a39Sopenharmony_ci{
1626e1051a39Sopenharmony_ci    const version_info *vent;
1627e1051a39Sopenharmony_ci    const version_info *table;
1628e1051a39Sopenharmony_ci
1629e1051a39Sopenharmony_ci    /*
1630e1051a39Sopenharmony_ci     * Check that the current protocol is the highest enabled version
1631e1051a39Sopenharmony_ci     * (according to s->ctx->method, as version negotiation may have changed
1632e1051a39Sopenharmony_ci     * s->method).
1633e1051a39Sopenharmony_ci     */
1634e1051a39Sopenharmony_ci    if (s->version == s->ctx->method->version)
1635e1051a39Sopenharmony_ci        return 1;
1636e1051a39Sopenharmony_ci
1637e1051a39Sopenharmony_ci    /*
1638e1051a39Sopenharmony_ci     * Apparently we're using a version-flexible SSL_METHOD (not at its
1639e1051a39Sopenharmony_ci     * highest protocol version).
1640e1051a39Sopenharmony_ci     */
1641e1051a39Sopenharmony_ci    if (s->ctx->method->version == TLS_method()->version)
1642e1051a39Sopenharmony_ci        table = tls_version_table;
1643e1051a39Sopenharmony_ci    else if (s->ctx->method->version == DTLS_method()->version)
1644e1051a39Sopenharmony_ci        table = dtls_version_table;
1645e1051a39Sopenharmony_ci    else {
1646e1051a39Sopenharmony_ci        /* Unexpected state; fail closed. */
1647e1051a39Sopenharmony_ci        return 0;
1648e1051a39Sopenharmony_ci    }
1649e1051a39Sopenharmony_ci
1650e1051a39Sopenharmony_ci    for (vent = table; vent->version != 0; ++vent) {
1651e1051a39Sopenharmony_ci        if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0)
1652e1051a39Sopenharmony_ci            return s->version == vent->version;
1653e1051a39Sopenharmony_ci    }
1654e1051a39Sopenharmony_ci    return 0;
1655e1051a39Sopenharmony_ci}
1656e1051a39Sopenharmony_ci
1657e1051a39Sopenharmony_ci/*
1658e1051a39Sopenharmony_ci * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
1659e1051a39Sopenharmony_ci * protocols, provided the initial (D)TLS method is version-flexible.  This
1660e1051a39Sopenharmony_ci * function sanity-checks the proposed value and makes sure the method is
1661e1051a39Sopenharmony_ci * version-flexible, then sets the limit if all is well.
1662e1051a39Sopenharmony_ci *
1663e1051a39Sopenharmony_ci * @method_version: The version of the current SSL_METHOD.
1664e1051a39Sopenharmony_ci * @version: the intended limit.
1665e1051a39Sopenharmony_ci * @bound: pointer to limit to be updated.
1666e1051a39Sopenharmony_ci *
1667e1051a39Sopenharmony_ci * Returns 1 on success, 0 on failure.
1668e1051a39Sopenharmony_ci */
1669e1051a39Sopenharmony_ciint ssl_set_version_bound(int method_version, int version, int *bound)
1670e1051a39Sopenharmony_ci{
1671e1051a39Sopenharmony_ci    int valid_tls;
1672e1051a39Sopenharmony_ci    int valid_dtls;
1673e1051a39Sopenharmony_ci
1674e1051a39Sopenharmony_ci    if (version == 0) {
1675e1051a39Sopenharmony_ci        *bound = version;
1676e1051a39Sopenharmony_ci        return 1;
1677e1051a39Sopenharmony_ci    }
1678e1051a39Sopenharmony_ci
1679e1051a39Sopenharmony_ci    valid_tls = version >= SSL3_VERSION && version <= TLS_MAX_VERSION_INTERNAL;
1680e1051a39Sopenharmony_ci    valid_dtls =
1681e1051a39Sopenharmony_ci        DTLS_VERSION_LE(version, DTLS_MAX_VERSION_INTERNAL) &&
1682e1051a39Sopenharmony_ci        DTLS_VERSION_GE(version, DTLS1_BAD_VER);
1683e1051a39Sopenharmony_ci
1684e1051a39Sopenharmony_ci    if (!valid_tls && !valid_dtls)
1685e1051a39Sopenharmony_ci        return 0;
1686e1051a39Sopenharmony_ci
1687e1051a39Sopenharmony_ci    /*-
1688e1051a39Sopenharmony_ci     * Restrict TLS methods to TLS protocol versions.
1689e1051a39Sopenharmony_ci     * Restrict DTLS methods to DTLS protocol versions.
1690e1051a39Sopenharmony_ci     * Note, DTLS version numbers are decreasing, use comparison macros.
1691e1051a39Sopenharmony_ci     *
1692e1051a39Sopenharmony_ci     * Note that for both lower-bounds we use explicit versions, not
1693e1051a39Sopenharmony_ci     * (D)TLS_MIN_VERSION.  This is because we don't want to break user
1694e1051a39Sopenharmony_ci     * configurations.  If the MIN (supported) version ever rises, the user's
1695e1051a39Sopenharmony_ci     * "floor" remains valid even if no longer available.  We don't expect the
1696e1051a39Sopenharmony_ci     * MAX ceiling to ever get lower, so making that variable makes sense.
1697e1051a39Sopenharmony_ci     *
1698e1051a39Sopenharmony_ci     * We ignore attempts to set bounds on version-inflexible methods,
1699e1051a39Sopenharmony_ci     * returning success.
1700e1051a39Sopenharmony_ci     */
1701e1051a39Sopenharmony_ci    switch (method_version) {
1702e1051a39Sopenharmony_ci    default:
1703e1051a39Sopenharmony_ci        break;
1704e1051a39Sopenharmony_ci
1705e1051a39Sopenharmony_ci    case TLS_ANY_VERSION:
1706e1051a39Sopenharmony_ci        if (valid_tls)
1707e1051a39Sopenharmony_ci            *bound = version;
1708e1051a39Sopenharmony_ci        break;
1709e1051a39Sopenharmony_ci
1710e1051a39Sopenharmony_ci    case DTLS_ANY_VERSION:
1711e1051a39Sopenharmony_ci        if (valid_dtls)
1712e1051a39Sopenharmony_ci            *bound = version;
1713e1051a39Sopenharmony_ci        break;
1714e1051a39Sopenharmony_ci    }
1715e1051a39Sopenharmony_ci    return 1;
1716e1051a39Sopenharmony_ci}
1717e1051a39Sopenharmony_ci
1718e1051a39Sopenharmony_cistatic void check_for_downgrade(SSL *s, int vers, DOWNGRADE *dgrd)
1719e1051a39Sopenharmony_ci{
1720e1051a39Sopenharmony_ci    if (vers == TLS1_2_VERSION
1721e1051a39Sopenharmony_ci            && ssl_version_supported(s, TLS1_3_VERSION, NULL)) {
1722e1051a39Sopenharmony_ci        *dgrd = DOWNGRADE_TO_1_2;
1723e1051a39Sopenharmony_ci    } else if (!SSL_IS_DTLS(s)
1724e1051a39Sopenharmony_ci            && vers < TLS1_2_VERSION
1725e1051a39Sopenharmony_ci               /*
1726e1051a39Sopenharmony_ci                * We need to ensure that a server that disables TLSv1.2
1727e1051a39Sopenharmony_ci                * (creating a hole between TLSv1.3 and TLSv1.1) can still
1728e1051a39Sopenharmony_ci                * complete handshakes with clients that support TLSv1.2 and
1729e1051a39Sopenharmony_ci                * below. Therefore we do not enable the sentinel if TLSv1.3 is
1730e1051a39Sopenharmony_ci                * enabled and TLSv1.2 is not.
1731e1051a39Sopenharmony_ci                */
1732e1051a39Sopenharmony_ci            && ssl_version_supported(s, TLS1_2_VERSION, NULL)) {
1733e1051a39Sopenharmony_ci        *dgrd = DOWNGRADE_TO_1_1;
1734e1051a39Sopenharmony_ci    } else {
1735e1051a39Sopenharmony_ci        *dgrd = DOWNGRADE_NONE;
1736e1051a39Sopenharmony_ci    }
1737e1051a39Sopenharmony_ci}
1738e1051a39Sopenharmony_ci
1739e1051a39Sopenharmony_ci/*
1740e1051a39Sopenharmony_ci * ssl_choose_server_version - Choose server (D)TLS version.  Called when the
1741e1051a39Sopenharmony_ci * client HELLO is received to select the final server protocol version and
1742e1051a39Sopenharmony_ci * the version specific method.
1743e1051a39Sopenharmony_ci *
1744e1051a39Sopenharmony_ci * @s: server SSL handle.
1745e1051a39Sopenharmony_ci *
1746e1051a39Sopenharmony_ci * Returns 0 on success or an SSL error reason number on failure.
1747e1051a39Sopenharmony_ci */
1748e1051a39Sopenharmony_ciint ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello, DOWNGRADE *dgrd)
1749e1051a39Sopenharmony_ci{
1750e1051a39Sopenharmony_ci    /*-
1751e1051a39Sopenharmony_ci     * With version-flexible methods we have an initial state with:
1752e1051a39Sopenharmony_ci     *
1753e1051a39Sopenharmony_ci     *   s->method->version == (D)TLS_ANY_VERSION,
1754e1051a39Sopenharmony_ci     *   s->version == (D)TLS_MAX_VERSION_INTERNAL.
1755e1051a39Sopenharmony_ci     *
1756e1051a39Sopenharmony_ci     * So we detect version-flexible methods via the method version, not the
1757e1051a39Sopenharmony_ci     * handle version.
1758e1051a39Sopenharmony_ci     */
1759e1051a39Sopenharmony_ci    int server_version = s->method->version;
1760e1051a39Sopenharmony_ci    int client_version = hello->legacy_version;
1761e1051a39Sopenharmony_ci    const version_info *vent;
1762e1051a39Sopenharmony_ci    const version_info *table;
1763e1051a39Sopenharmony_ci    int disabled = 0;
1764e1051a39Sopenharmony_ci    RAW_EXTENSION *suppversions;
1765e1051a39Sopenharmony_ci
1766e1051a39Sopenharmony_ci    s->client_version = client_version;
1767e1051a39Sopenharmony_ci
1768e1051a39Sopenharmony_ci    switch (server_version) {
1769e1051a39Sopenharmony_ci    default:
1770e1051a39Sopenharmony_ci        if (!SSL_IS_TLS13(s)) {
1771e1051a39Sopenharmony_ci            if (version_cmp(s, client_version, s->version) < 0)
1772e1051a39Sopenharmony_ci                return SSL_R_WRONG_SSL_VERSION;
1773e1051a39Sopenharmony_ci            *dgrd = DOWNGRADE_NONE;
1774e1051a39Sopenharmony_ci            /*
1775e1051a39Sopenharmony_ci             * If this SSL handle is not from a version flexible method we don't
1776e1051a39Sopenharmony_ci             * (and never did) check min/max FIPS or Suite B constraints.  Hope
1777e1051a39Sopenharmony_ci             * that's OK.  It is up to the caller to not choose fixed protocol
1778e1051a39Sopenharmony_ci             * versions they don't want.  If not, then easy to fix, just return
1779e1051a39Sopenharmony_ci             * ssl_method_error(s, s->method)
1780e1051a39Sopenharmony_ci             */
1781e1051a39Sopenharmony_ci            return 0;
1782e1051a39Sopenharmony_ci        }
1783e1051a39Sopenharmony_ci        /*
1784e1051a39Sopenharmony_ci         * Fall through if we are TLSv1.3 already (this means we must be after
1785e1051a39Sopenharmony_ci         * a HelloRetryRequest
1786e1051a39Sopenharmony_ci         */
1787e1051a39Sopenharmony_ci        /* fall thru */
1788e1051a39Sopenharmony_ci    case TLS_ANY_VERSION:
1789e1051a39Sopenharmony_ci        table = tls_version_table;
1790e1051a39Sopenharmony_ci        break;
1791e1051a39Sopenharmony_ci    case DTLS_ANY_VERSION:
1792e1051a39Sopenharmony_ci        table = dtls_version_table;
1793e1051a39Sopenharmony_ci        break;
1794e1051a39Sopenharmony_ci    }
1795e1051a39Sopenharmony_ci
1796e1051a39Sopenharmony_ci    suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];
1797e1051a39Sopenharmony_ci
1798e1051a39Sopenharmony_ci    /* If we did an HRR then supported versions is mandatory */
1799e1051a39Sopenharmony_ci    if (!suppversions->present && s->hello_retry_request != SSL_HRR_NONE)
1800e1051a39Sopenharmony_ci        return SSL_R_UNSUPPORTED_PROTOCOL;
1801e1051a39Sopenharmony_ci
1802e1051a39Sopenharmony_ci    if (suppversions->present && !SSL_IS_DTLS(s)) {
1803e1051a39Sopenharmony_ci        unsigned int candidate_vers = 0;
1804e1051a39Sopenharmony_ci        unsigned int best_vers = 0;
1805e1051a39Sopenharmony_ci        const SSL_METHOD *best_method = NULL;
1806e1051a39Sopenharmony_ci        PACKET versionslist;
1807e1051a39Sopenharmony_ci
1808e1051a39Sopenharmony_ci        suppversions->parsed = 1;
1809e1051a39Sopenharmony_ci
1810e1051a39Sopenharmony_ci        if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) {
1811e1051a39Sopenharmony_ci            /* Trailing or invalid data? */
1812e1051a39Sopenharmony_ci            return SSL_R_LENGTH_MISMATCH;
1813e1051a39Sopenharmony_ci        }
1814e1051a39Sopenharmony_ci
1815e1051a39Sopenharmony_ci        /*
1816e1051a39Sopenharmony_ci         * The TLSv1.3 spec says the client MUST set this to TLS1_2_VERSION.
1817e1051a39Sopenharmony_ci         * The spec only requires servers to check that it isn't SSLv3:
1818e1051a39Sopenharmony_ci         * "Any endpoint receiving a Hello message with
1819e1051a39Sopenharmony_ci         * ClientHello.legacy_version or ServerHello.legacy_version set to
1820e1051a39Sopenharmony_ci         * 0x0300 MUST abort the handshake with a "protocol_version" alert."
1821e1051a39Sopenharmony_ci         * We are slightly stricter and require that it isn't SSLv3 or lower.
1822e1051a39Sopenharmony_ci         * We tolerate TLSv1 and TLSv1.1.
1823e1051a39Sopenharmony_ci         */
1824e1051a39Sopenharmony_ci        if (client_version <= SSL3_VERSION)
1825e1051a39Sopenharmony_ci            return SSL_R_BAD_LEGACY_VERSION;
1826e1051a39Sopenharmony_ci
1827e1051a39Sopenharmony_ci        while (PACKET_get_net_2(&versionslist, &candidate_vers)) {
1828e1051a39Sopenharmony_ci            if (version_cmp(s, candidate_vers, best_vers) <= 0)
1829e1051a39Sopenharmony_ci                continue;
1830e1051a39Sopenharmony_ci            if (ssl_version_supported(s, candidate_vers, &best_method))
1831e1051a39Sopenharmony_ci                best_vers = candidate_vers;
1832e1051a39Sopenharmony_ci        }
1833e1051a39Sopenharmony_ci        if (PACKET_remaining(&versionslist) != 0) {
1834e1051a39Sopenharmony_ci            /* Trailing data? */
1835e1051a39Sopenharmony_ci            return SSL_R_LENGTH_MISMATCH;
1836e1051a39Sopenharmony_ci        }
1837e1051a39Sopenharmony_ci
1838e1051a39Sopenharmony_ci        if (best_vers > 0) {
1839e1051a39Sopenharmony_ci            if (s->hello_retry_request != SSL_HRR_NONE) {
1840e1051a39Sopenharmony_ci                /*
1841e1051a39Sopenharmony_ci                 * This is after a HelloRetryRequest so we better check that we
1842e1051a39Sopenharmony_ci                 * negotiated TLSv1.3
1843e1051a39Sopenharmony_ci                 */
1844e1051a39Sopenharmony_ci                if (best_vers != TLS1_3_VERSION)
1845e1051a39Sopenharmony_ci                    return SSL_R_UNSUPPORTED_PROTOCOL;
1846e1051a39Sopenharmony_ci                return 0;
1847e1051a39Sopenharmony_ci            }
1848e1051a39Sopenharmony_ci            check_for_downgrade(s, best_vers, dgrd);
1849e1051a39Sopenharmony_ci            s->version = best_vers;
1850e1051a39Sopenharmony_ci            s->method = best_method;
1851e1051a39Sopenharmony_ci            return 0;
1852e1051a39Sopenharmony_ci        }
1853e1051a39Sopenharmony_ci        return SSL_R_UNSUPPORTED_PROTOCOL;
1854e1051a39Sopenharmony_ci    }
1855e1051a39Sopenharmony_ci
1856e1051a39Sopenharmony_ci    /*
1857e1051a39Sopenharmony_ci     * If the supported versions extension isn't present, then the highest
1858e1051a39Sopenharmony_ci     * version we can negotiate is TLSv1.2
1859e1051a39Sopenharmony_ci     */
1860e1051a39Sopenharmony_ci    if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0)
1861e1051a39Sopenharmony_ci        client_version = TLS1_2_VERSION;
1862e1051a39Sopenharmony_ci
1863e1051a39Sopenharmony_ci    /*
1864e1051a39Sopenharmony_ci     * No supported versions extension, so we just use the version supplied in
1865e1051a39Sopenharmony_ci     * the ClientHello.
1866e1051a39Sopenharmony_ci     */
1867e1051a39Sopenharmony_ci    for (vent = table; vent->version != 0; ++vent) {
1868e1051a39Sopenharmony_ci        const SSL_METHOD *method;
1869e1051a39Sopenharmony_ci
1870e1051a39Sopenharmony_ci        if (vent->smeth == NULL ||
1871e1051a39Sopenharmony_ci            version_cmp(s, client_version, vent->version) < 0)
1872e1051a39Sopenharmony_ci            continue;
1873e1051a39Sopenharmony_ci        method = vent->smeth();
1874e1051a39Sopenharmony_ci        if (ssl_method_error(s, method) == 0) {
1875e1051a39Sopenharmony_ci            check_for_downgrade(s, vent->version, dgrd);
1876e1051a39Sopenharmony_ci            s->version = vent->version;
1877e1051a39Sopenharmony_ci            s->method = method;
1878e1051a39Sopenharmony_ci            return 0;
1879e1051a39Sopenharmony_ci        }
1880e1051a39Sopenharmony_ci        disabled = 1;
1881e1051a39Sopenharmony_ci    }
1882e1051a39Sopenharmony_ci    return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
1883e1051a39Sopenharmony_ci}
1884e1051a39Sopenharmony_ci
1885e1051a39Sopenharmony_ci/*
1886e1051a39Sopenharmony_ci * ssl_choose_client_version - Choose client (D)TLS version.  Called when the
1887e1051a39Sopenharmony_ci * server HELLO is received to select the final client protocol version and
1888e1051a39Sopenharmony_ci * the version specific method.
1889e1051a39Sopenharmony_ci *
1890e1051a39Sopenharmony_ci * @s: client SSL handle.
1891e1051a39Sopenharmony_ci * @version: The proposed version from the server's HELLO.
1892e1051a39Sopenharmony_ci * @extensions: The extensions received
1893e1051a39Sopenharmony_ci *
1894e1051a39Sopenharmony_ci * Returns 1 on success or 0 on error.
1895e1051a39Sopenharmony_ci */
1896e1051a39Sopenharmony_ciint ssl_choose_client_version(SSL *s, int version, RAW_EXTENSION *extensions)
1897e1051a39Sopenharmony_ci{
1898e1051a39Sopenharmony_ci    const version_info *vent;
1899e1051a39Sopenharmony_ci    const version_info *table;
1900e1051a39Sopenharmony_ci    int ret, ver_min, ver_max, real_max, origv;
1901e1051a39Sopenharmony_ci
1902e1051a39Sopenharmony_ci    origv = s->version;
1903e1051a39Sopenharmony_ci    s->version = version;
1904e1051a39Sopenharmony_ci
1905e1051a39Sopenharmony_ci    /* This will overwrite s->version if the extension is present */
1906e1051a39Sopenharmony_ci    if (!tls_parse_extension(s, TLSEXT_IDX_supported_versions,
1907e1051a39Sopenharmony_ci                             SSL_EXT_TLS1_2_SERVER_HELLO
1908e1051a39Sopenharmony_ci                             | SSL_EXT_TLS1_3_SERVER_HELLO, extensions,
1909e1051a39Sopenharmony_ci                             NULL, 0)) {
1910e1051a39Sopenharmony_ci        s->version = origv;
1911e1051a39Sopenharmony_ci        return 0;
1912e1051a39Sopenharmony_ci    }
1913e1051a39Sopenharmony_ci
1914e1051a39Sopenharmony_ci    if (s->hello_retry_request != SSL_HRR_NONE
1915e1051a39Sopenharmony_ci            && s->version != TLS1_3_VERSION) {
1916e1051a39Sopenharmony_ci        s->version = origv;
1917e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_WRONG_SSL_VERSION);
1918e1051a39Sopenharmony_ci        return 0;
1919e1051a39Sopenharmony_ci    }
1920e1051a39Sopenharmony_ci
1921e1051a39Sopenharmony_ci    switch (s->method->version) {
1922e1051a39Sopenharmony_ci    default:
1923e1051a39Sopenharmony_ci        if (s->version != s->method->version) {
1924e1051a39Sopenharmony_ci            s->version = origv;
1925e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_WRONG_SSL_VERSION);
1926e1051a39Sopenharmony_ci            return 0;
1927e1051a39Sopenharmony_ci        }
1928e1051a39Sopenharmony_ci        /*
1929e1051a39Sopenharmony_ci         * If this SSL handle is not from a version flexible method we don't
1930e1051a39Sopenharmony_ci         * (and never did) check min/max, FIPS or Suite B constraints.  Hope
1931e1051a39Sopenharmony_ci         * that's OK.  It is up to the caller to not choose fixed protocol
1932e1051a39Sopenharmony_ci         * versions they don't want.  If not, then easy to fix, just return
1933e1051a39Sopenharmony_ci         * ssl_method_error(s, s->method)
1934e1051a39Sopenharmony_ci         */
1935e1051a39Sopenharmony_ci        return 1;
1936e1051a39Sopenharmony_ci    case TLS_ANY_VERSION:
1937e1051a39Sopenharmony_ci        table = tls_version_table;
1938e1051a39Sopenharmony_ci        break;
1939e1051a39Sopenharmony_ci    case DTLS_ANY_VERSION:
1940e1051a39Sopenharmony_ci        table = dtls_version_table;
1941e1051a39Sopenharmony_ci        break;
1942e1051a39Sopenharmony_ci    }
1943e1051a39Sopenharmony_ci
1944e1051a39Sopenharmony_ci    ret = ssl_get_min_max_version(s, &ver_min, &ver_max, &real_max);
1945e1051a39Sopenharmony_ci    if (ret != 0) {
1946e1051a39Sopenharmony_ci        s->version = origv;
1947e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_PROTOCOL_VERSION, ret);
1948e1051a39Sopenharmony_ci        return 0;
1949e1051a39Sopenharmony_ci    }
1950e1051a39Sopenharmony_ci    if (SSL_IS_DTLS(s) ? DTLS_VERSION_LT(s->version, ver_min)
1951e1051a39Sopenharmony_ci                       : s->version < ver_min) {
1952e1051a39Sopenharmony_ci        s->version = origv;
1953e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNSUPPORTED_PROTOCOL);
1954e1051a39Sopenharmony_ci        return 0;
1955e1051a39Sopenharmony_ci    } else if (SSL_IS_DTLS(s) ? DTLS_VERSION_GT(s->version, ver_max)
1956e1051a39Sopenharmony_ci                              : s->version > ver_max) {
1957e1051a39Sopenharmony_ci        s->version = origv;
1958e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNSUPPORTED_PROTOCOL);
1959e1051a39Sopenharmony_ci        return 0;
1960e1051a39Sopenharmony_ci    }
1961e1051a39Sopenharmony_ci
1962e1051a39Sopenharmony_ci    if ((s->mode & SSL_MODE_SEND_FALLBACK_SCSV) == 0)
1963e1051a39Sopenharmony_ci        real_max = ver_max;
1964e1051a39Sopenharmony_ci
1965e1051a39Sopenharmony_ci    /* Check for downgrades */
1966e1051a39Sopenharmony_ci    if (s->version == TLS1_2_VERSION && real_max > s->version) {
1967e1051a39Sopenharmony_ci        if (memcmp(tls12downgrade,
1968e1051a39Sopenharmony_ci                   s->s3.server_random + SSL3_RANDOM_SIZE
1969e1051a39Sopenharmony_ci                                        - sizeof(tls12downgrade),
1970e1051a39Sopenharmony_ci                   sizeof(tls12downgrade)) == 0) {
1971e1051a39Sopenharmony_ci            s->version = origv;
1972e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1973e1051a39Sopenharmony_ci                     SSL_R_INAPPROPRIATE_FALLBACK);
1974e1051a39Sopenharmony_ci            return 0;
1975e1051a39Sopenharmony_ci        }
1976e1051a39Sopenharmony_ci    } else if (!SSL_IS_DTLS(s)
1977e1051a39Sopenharmony_ci               && s->version < TLS1_2_VERSION
1978e1051a39Sopenharmony_ci               && real_max > s->version) {
1979e1051a39Sopenharmony_ci        if (memcmp(tls11downgrade,
1980e1051a39Sopenharmony_ci                   s->s3.server_random + SSL3_RANDOM_SIZE
1981e1051a39Sopenharmony_ci                                        - sizeof(tls11downgrade),
1982e1051a39Sopenharmony_ci                   sizeof(tls11downgrade)) == 0) {
1983e1051a39Sopenharmony_ci            s->version = origv;
1984e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1985e1051a39Sopenharmony_ci                     SSL_R_INAPPROPRIATE_FALLBACK);
1986e1051a39Sopenharmony_ci            return 0;
1987e1051a39Sopenharmony_ci        }
1988e1051a39Sopenharmony_ci    }
1989e1051a39Sopenharmony_ci
1990e1051a39Sopenharmony_ci    for (vent = table; vent->version != 0; ++vent) {
1991e1051a39Sopenharmony_ci        if (vent->cmeth == NULL || s->version != vent->version)
1992e1051a39Sopenharmony_ci            continue;
1993e1051a39Sopenharmony_ci
1994e1051a39Sopenharmony_ci        s->method = vent->cmeth();
1995e1051a39Sopenharmony_ci        return 1;
1996e1051a39Sopenharmony_ci    }
1997e1051a39Sopenharmony_ci
1998e1051a39Sopenharmony_ci    s->version = origv;
1999e1051a39Sopenharmony_ci    SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNSUPPORTED_PROTOCOL);
2000e1051a39Sopenharmony_ci    return 0;
2001e1051a39Sopenharmony_ci}
2002e1051a39Sopenharmony_ci
2003e1051a39Sopenharmony_ci/*
2004e1051a39Sopenharmony_ci * ssl_get_min_max_version - get minimum and maximum protocol version
2005e1051a39Sopenharmony_ci * @s: The SSL connection
2006e1051a39Sopenharmony_ci * @min_version: The minimum supported version
2007e1051a39Sopenharmony_ci * @max_version: The maximum supported version
2008e1051a39Sopenharmony_ci * @real_max:    The highest version below the lowest compile time version hole
2009e1051a39Sopenharmony_ci *               where that hole lies above at least one run-time enabled
2010e1051a39Sopenharmony_ci *               protocol.
2011e1051a39Sopenharmony_ci *
2012e1051a39Sopenharmony_ci * Work out what version we should be using for the initial ClientHello if the
2013e1051a39Sopenharmony_ci * version is initially (D)TLS_ANY_VERSION.  We apply any explicit SSL_OP_NO_xxx
2014e1051a39Sopenharmony_ci * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
2015e1051a39Sopenharmony_ci * constraints and any floor imposed by the security level here,
2016e1051a39Sopenharmony_ci * so we don't advertise the wrong protocol version to only reject the outcome later.
2017e1051a39Sopenharmony_ci *
2018e1051a39Sopenharmony_ci * Computing the right floor matters.  If, e.g., TLS 1.0 and 1.2 are enabled,
2019e1051a39Sopenharmony_ci * TLS 1.1 is disabled, but the security level, Suite-B  and/or MinProtocol
2020e1051a39Sopenharmony_ci * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
2021e1051a39Sopenharmony_ci *
2022e1051a39Sopenharmony_ci * Returns 0 on success or an SSL error reason number on failure.  On failure
2023e1051a39Sopenharmony_ci * min_version and max_version will also be set to 0.
2024e1051a39Sopenharmony_ci */
2025e1051a39Sopenharmony_ciint ssl_get_min_max_version(const SSL *s, int *min_version, int *max_version,
2026e1051a39Sopenharmony_ci                            int *real_max)
2027e1051a39Sopenharmony_ci{
2028e1051a39Sopenharmony_ci    int version, tmp_real_max;
2029e1051a39Sopenharmony_ci    int hole;
2030e1051a39Sopenharmony_ci    const SSL_METHOD *single = NULL;
2031e1051a39Sopenharmony_ci    const SSL_METHOD *method;
2032e1051a39Sopenharmony_ci    const version_info *table;
2033e1051a39Sopenharmony_ci    const version_info *vent;
2034e1051a39Sopenharmony_ci
2035e1051a39Sopenharmony_ci    switch (s->method->version) {
2036e1051a39Sopenharmony_ci    default:
2037e1051a39Sopenharmony_ci        /*
2038e1051a39Sopenharmony_ci         * If this SSL handle is not from a version flexible method we don't
2039e1051a39Sopenharmony_ci         * (and never did) check min/max FIPS or Suite B constraints.  Hope
2040e1051a39Sopenharmony_ci         * that's OK.  It is up to the caller to not choose fixed protocol
2041e1051a39Sopenharmony_ci         * versions they don't want.  If not, then easy to fix, just return
2042e1051a39Sopenharmony_ci         * ssl_method_error(s, s->method)
2043e1051a39Sopenharmony_ci         */
2044e1051a39Sopenharmony_ci        *min_version = *max_version = s->version;
2045e1051a39Sopenharmony_ci        /*
2046e1051a39Sopenharmony_ci         * Providing a real_max only makes sense where we're using a version
2047e1051a39Sopenharmony_ci         * flexible method.
2048e1051a39Sopenharmony_ci         */
2049e1051a39Sopenharmony_ci        if (!ossl_assert(real_max == NULL))
2050e1051a39Sopenharmony_ci            return ERR_R_INTERNAL_ERROR;
2051e1051a39Sopenharmony_ci        return 0;
2052e1051a39Sopenharmony_ci    case TLS_ANY_VERSION:
2053e1051a39Sopenharmony_ci        table = tls_version_table;
2054e1051a39Sopenharmony_ci        break;
2055e1051a39Sopenharmony_ci    case DTLS_ANY_VERSION:
2056e1051a39Sopenharmony_ci        table = dtls_version_table;
2057e1051a39Sopenharmony_ci        break;
2058e1051a39Sopenharmony_ci    }
2059e1051a39Sopenharmony_ci
2060e1051a39Sopenharmony_ci    /*
2061e1051a39Sopenharmony_ci     * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
2062e1051a39Sopenharmony_ci     * below X enabled. This is required in order to maintain the "version
2063e1051a39Sopenharmony_ci     * capability" vector contiguous. Any versions with a NULL client method
2064e1051a39Sopenharmony_ci     * (protocol version client is disabled at compile-time) is also a "hole".
2065e1051a39Sopenharmony_ci     *
2066e1051a39Sopenharmony_ci     * Our initial state is hole == 1, version == 0.  That is, versions above
2067e1051a39Sopenharmony_ci     * the first version in the method table are disabled (a "hole" above
2068e1051a39Sopenharmony_ci     * the valid protocol entries) and we don't have a selected version yet.
2069e1051a39Sopenharmony_ci     *
2070e1051a39Sopenharmony_ci     * Whenever "hole == 1", and we hit an enabled method, its version becomes
2071e1051a39Sopenharmony_ci     * the selected version, and the method becomes a candidate "single"
2072e1051a39Sopenharmony_ci     * method.  We're no longer in a hole, so "hole" becomes 0.
2073e1051a39Sopenharmony_ci     *
2074e1051a39Sopenharmony_ci     * If "hole == 0" and we hit an enabled method, then "single" is cleared,
2075e1051a39Sopenharmony_ci     * as we support a contiguous range of at least two methods.  If we hit
2076e1051a39Sopenharmony_ci     * a disabled method, then hole becomes true again, but nothing else
2077e1051a39Sopenharmony_ci     * changes yet, because all the remaining methods may be disabled too.
2078e1051a39Sopenharmony_ci     * If we again hit an enabled method after the new hole, it becomes
2079e1051a39Sopenharmony_ci     * selected, as we start from scratch.
2080e1051a39Sopenharmony_ci     */
2081e1051a39Sopenharmony_ci    *min_version = version = 0;
2082e1051a39Sopenharmony_ci    hole = 1;
2083e1051a39Sopenharmony_ci    if (real_max != NULL)
2084e1051a39Sopenharmony_ci        *real_max = 0;
2085e1051a39Sopenharmony_ci    tmp_real_max = 0;
2086e1051a39Sopenharmony_ci    for (vent = table; vent->version != 0; ++vent) {
2087e1051a39Sopenharmony_ci        /*
2088e1051a39Sopenharmony_ci         * A table entry with a NULL client method is still a hole in the
2089e1051a39Sopenharmony_ci         * "version capability" vector.
2090e1051a39Sopenharmony_ci         */
2091e1051a39Sopenharmony_ci        if (vent->cmeth == NULL) {
2092e1051a39Sopenharmony_ci            hole = 1;
2093e1051a39Sopenharmony_ci            tmp_real_max = 0;
2094e1051a39Sopenharmony_ci            continue;
2095e1051a39Sopenharmony_ci        }
2096e1051a39Sopenharmony_ci        method = vent->cmeth();
2097e1051a39Sopenharmony_ci
2098e1051a39Sopenharmony_ci        if (hole == 1 && tmp_real_max == 0)
2099e1051a39Sopenharmony_ci            tmp_real_max = vent->version;
2100e1051a39Sopenharmony_ci
2101e1051a39Sopenharmony_ci        if (ssl_method_error(s, method) != 0) {
2102e1051a39Sopenharmony_ci            hole = 1;
2103e1051a39Sopenharmony_ci        } else if (!hole) {
2104e1051a39Sopenharmony_ci            single = NULL;
2105e1051a39Sopenharmony_ci            *min_version = method->version;
2106e1051a39Sopenharmony_ci        } else {
2107e1051a39Sopenharmony_ci            if (real_max != NULL && tmp_real_max != 0)
2108e1051a39Sopenharmony_ci                *real_max = tmp_real_max;
2109e1051a39Sopenharmony_ci            version = (single = method)->version;
2110e1051a39Sopenharmony_ci            *min_version = version;
2111e1051a39Sopenharmony_ci            hole = 0;
2112e1051a39Sopenharmony_ci        }
2113e1051a39Sopenharmony_ci    }
2114e1051a39Sopenharmony_ci
2115e1051a39Sopenharmony_ci    *max_version = version;
2116e1051a39Sopenharmony_ci
2117e1051a39Sopenharmony_ci    /* Fail if everything is disabled */
2118e1051a39Sopenharmony_ci    if (version == 0)
2119e1051a39Sopenharmony_ci        return SSL_R_NO_PROTOCOLS_AVAILABLE;
2120e1051a39Sopenharmony_ci
2121e1051a39Sopenharmony_ci    return 0;
2122e1051a39Sopenharmony_ci}
2123e1051a39Sopenharmony_ci
2124e1051a39Sopenharmony_ci/*
2125e1051a39Sopenharmony_ci * ssl_set_client_hello_version - Work out what version we should be using for
2126e1051a39Sopenharmony_ci * the initial ClientHello.legacy_version field.
2127e1051a39Sopenharmony_ci *
2128e1051a39Sopenharmony_ci * @s: client SSL handle.
2129e1051a39Sopenharmony_ci *
2130e1051a39Sopenharmony_ci * Returns 0 on success or an SSL error reason number on failure.
2131e1051a39Sopenharmony_ci */
2132e1051a39Sopenharmony_ciint ssl_set_client_hello_version(SSL *s)
2133e1051a39Sopenharmony_ci{
2134e1051a39Sopenharmony_ci    int ver_min, ver_max, ret;
2135e1051a39Sopenharmony_ci
2136e1051a39Sopenharmony_ci    /*
2137e1051a39Sopenharmony_ci     * In a renegotiation we always send the same client_version that we sent
2138e1051a39Sopenharmony_ci     * last time, regardless of which version we eventually negotiated.
2139e1051a39Sopenharmony_ci     */
2140e1051a39Sopenharmony_ci    if (!SSL_IS_FIRST_HANDSHAKE(s))
2141e1051a39Sopenharmony_ci        return 0;
2142e1051a39Sopenharmony_ci
2143e1051a39Sopenharmony_ci    ret = ssl_get_min_max_version(s, &ver_min, &ver_max, NULL);
2144e1051a39Sopenharmony_ci
2145e1051a39Sopenharmony_ci    if (ret != 0)
2146e1051a39Sopenharmony_ci        return ret;
2147e1051a39Sopenharmony_ci
2148e1051a39Sopenharmony_ci    s->version = ver_max;
2149e1051a39Sopenharmony_ci
2150e1051a39Sopenharmony_ci    /* TLS1.3 always uses TLS1.2 in the legacy_version field */
2151e1051a39Sopenharmony_ci    if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION)
2152e1051a39Sopenharmony_ci        ver_max = TLS1_2_VERSION;
2153e1051a39Sopenharmony_ci
2154e1051a39Sopenharmony_ci    s->client_version = ver_max;
2155e1051a39Sopenharmony_ci    return 0;
2156e1051a39Sopenharmony_ci}
2157e1051a39Sopenharmony_ci
2158e1051a39Sopenharmony_ci/*
2159e1051a39Sopenharmony_ci * Checks a list of |groups| to determine if the |group_id| is in it. If it is
2160e1051a39Sopenharmony_ci * and |checkallow| is 1 then additionally check if the group is allowed to be
2161e1051a39Sopenharmony_ci * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
2162e1051a39Sopenharmony_ci * 1) or 0 otherwise.
2163e1051a39Sopenharmony_ci */
2164e1051a39Sopenharmony_ciint check_in_list(SSL *s, uint16_t group_id, const uint16_t *groups,
2165e1051a39Sopenharmony_ci                  size_t num_groups, int checkallow)
2166e1051a39Sopenharmony_ci{
2167e1051a39Sopenharmony_ci    size_t i;
2168e1051a39Sopenharmony_ci
2169e1051a39Sopenharmony_ci    if (groups == NULL || num_groups == 0)
2170e1051a39Sopenharmony_ci        return 0;
2171e1051a39Sopenharmony_ci
2172e1051a39Sopenharmony_ci    for (i = 0; i < num_groups; i++) {
2173e1051a39Sopenharmony_ci        uint16_t group = groups[i];
2174e1051a39Sopenharmony_ci
2175e1051a39Sopenharmony_ci        if (group_id == group
2176e1051a39Sopenharmony_ci                && (!checkallow
2177e1051a39Sopenharmony_ci                    || tls_group_allowed(s, group, SSL_SECOP_CURVE_CHECK))) {
2178e1051a39Sopenharmony_ci            return 1;
2179e1051a39Sopenharmony_ci        }
2180e1051a39Sopenharmony_ci    }
2181e1051a39Sopenharmony_ci
2182e1051a39Sopenharmony_ci    return 0;
2183e1051a39Sopenharmony_ci}
2184e1051a39Sopenharmony_ci
2185e1051a39Sopenharmony_ci/* Replace ClientHello1 in the transcript hash with a synthetic message */
2186e1051a39Sopenharmony_ciint create_synthetic_message_hash(SSL *s, const unsigned char *hashval,
2187e1051a39Sopenharmony_ci                                  size_t hashlen, const unsigned char *hrr,
2188e1051a39Sopenharmony_ci                                  size_t hrrlen)
2189e1051a39Sopenharmony_ci{
2190e1051a39Sopenharmony_ci    unsigned char hashvaltmp[EVP_MAX_MD_SIZE];
2191e1051a39Sopenharmony_ci    unsigned char msghdr[SSL3_HM_HEADER_LENGTH];
2192e1051a39Sopenharmony_ci
2193e1051a39Sopenharmony_ci    memset(msghdr, 0, sizeof(msghdr));
2194e1051a39Sopenharmony_ci
2195e1051a39Sopenharmony_ci    if (hashval == NULL) {
2196e1051a39Sopenharmony_ci        hashval = hashvaltmp;
2197e1051a39Sopenharmony_ci        hashlen = 0;
2198e1051a39Sopenharmony_ci        /* Get the hash of the initial ClientHello */
2199e1051a39Sopenharmony_ci        if (!ssl3_digest_cached_records(s, 0)
2200e1051a39Sopenharmony_ci                || !ssl_handshake_hash(s, hashvaltmp, sizeof(hashvaltmp),
2201e1051a39Sopenharmony_ci                                       &hashlen)) {
2202e1051a39Sopenharmony_ci            /* SSLfatal() already called */
2203e1051a39Sopenharmony_ci            return 0;
2204e1051a39Sopenharmony_ci        }
2205e1051a39Sopenharmony_ci    }
2206e1051a39Sopenharmony_ci
2207e1051a39Sopenharmony_ci    /* Reinitialise the transcript hash */
2208e1051a39Sopenharmony_ci    if (!ssl3_init_finished_mac(s)) {
2209e1051a39Sopenharmony_ci        /* SSLfatal() already called */
2210e1051a39Sopenharmony_ci        return 0;
2211e1051a39Sopenharmony_ci    }
2212e1051a39Sopenharmony_ci
2213e1051a39Sopenharmony_ci    /* Inject the synthetic message_hash message */
2214e1051a39Sopenharmony_ci    msghdr[0] = SSL3_MT_MESSAGE_HASH;
2215e1051a39Sopenharmony_ci    msghdr[SSL3_HM_HEADER_LENGTH - 1] = (unsigned char)hashlen;
2216e1051a39Sopenharmony_ci    if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH)
2217e1051a39Sopenharmony_ci            || !ssl3_finish_mac(s, hashval, hashlen)) {
2218e1051a39Sopenharmony_ci        /* SSLfatal() already called */
2219e1051a39Sopenharmony_ci        return 0;
2220e1051a39Sopenharmony_ci    }
2221e1051a39Sopenharmony_ci
2222e1051a39Sopenharmony_ci    /*
2223e1051a39Sopenharmony_ci     * Now re-inject the HRR and current message if appropriate (we just deleted
2224e1051a39Sopenharmony_ci     * it when we reinitialised the transcript hash above). Only necessary after
2225e1051a39Sopenharmony_ci     * receiving a ClientHello2 with a cookie.
2226e1051a39Sopenharmony_ci     */
2227e1051a39Sopenharmony_ci    if (hrr != NULL
2228e1051a39Sopenharmony_ci            && (!ssl3_finish_mac(s, hrr, hrrlen)
2229e1051a39Sopenharmony_ci                || !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
2230e1051a39Sopenharmony_ci                                    s->s3.tmp.message_size
2231e1051a39Sopenharmony_ci                                    + SSL3_HM_HEADER_LENGTH))) {
2232e1051a39Sopenharmony_ci        /* SSLfatal() already called */
2233e1051a39Sopenharmony_ci        return 0;
2234e1051a39Sopenharmony_ci    }
2235e1051a39Sopenharmony_ci
2236e1051a39Sopenharmony_ci    return 1;
2237e1051a39Sopenharmony_ci}
2238e1051a39Sopenharmony_ci
2239e1051a39Sopenharmony_cistatic int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2240e1051a39Sopenharmony_ci{
2241e1051a39Sopenharmony_ci    return X509_NAME_cmp(*a, *b);
2242e1051a39Sopenharmony_ci}
2243e1051a39Sopenharmony_ci
2244e1051a39Sopenharmony_ciint parse_ca_names(SSL *s, PACKET *pkt)
2245e1051a39Sopenharmony_ci{
2246e1051a39Sopenharmony_ci    STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp);
2247e1051a39Sopenharmony_ci    X509_NAME *xn = NULL;
2248e1051a39Sopenharmony_ci    PACKET cadns;
2249e1051a39Sopenharmony_ci
2250e1051a39Sopenharmony_ci    if (ca_sk == NULL) {
2251e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2252e1051a39Sopenharmony_ci        goto err;
2253e1051a39Sopenharmony_ci    }
2254e1051a39Sopenharmony_ci    /* get the CA RDNs */
2255e1051a39Sopenharmony_ci    if (!PACKET_get_length_prefixed_2(pkt, &cadns)) {
2256e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2257e1051a39Sopenharmony_ci        goto err;
2258e1051a39Sopenharmony_ci    }
2259e1051a39Sopenharmony_ci
2260e1051a39Sopenharmony_ci    while (PACKET_remaining(&cadns)) {
2261e1051a39Sopenharmony_ci        const unsigned char *namestart, *namebytes;
2262e1051a39Sopenharmony_ci        unsigned int name_len;
2263e1051a39Sopenharmony_ci
2264e1051a39Sopenharmony_ci        if (!PACKET_get_net_2(&cadns, &name_len)
2265e1051a39Sopenharmony_ci            || !PACKET_get_bytes(&cadns, &namebytes, name_len)) {
2266e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2267e1051a39Sopenharmony_ci            goto err;
2268e1051a39Sopenharmony_ci        }
2269e1051a39Sopenharmony_ci
2270e1051a39Sopenharmony_ci        namestart = namebytes;
2271e1051a39Sopenharmony_ci        if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) {
2272e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
2273e1051a39Sopenharmony_ci            goto err;
2274e1051a39Sopenharmony_ci        }
2275e1051a39Sopenharmony_ci        if (namebytes != (namestart + name_len)) {
2276e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CA_DN_LENGTH_MISMATCH);
2277e1051a39Sopenharmony_ci            goto err;
2278e1051a39Sopenharmony_ci        }
2279e1051a39Sopenharmony_ci
2280e1051a39Sopenharmony_ci        if (!sk_X509_NAME_push(ca_sk, xn)) {
2281e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2282e1051a39Sopenharmony_ci            goto err;
2283e1051a39Sopenharmony_ci        }
2284e1051a39Sopenharmony_ci        xn = NULL;
2285e1051a39Sopenharmony_ci    }
2286e1051a39Sopenharmony_ci
2287e1051a39Sopenharmony_ci    sk_X509_NAME_pop_free(s->s3.tmp.peer_ca_names, X509_NAME_free);
2288e1051a39Sopenharmony_ci    s->s3.tmp.peer_ca_names = ca_sk;
2289e1051a39Sopenharmony_ci
2290e1051a39Sopenharmony_ci    return 1;
2291e1051a39Sopenharmony_ci
2292e1051a39Sopenharmony_ci err:
2293e1051a39Sopenharmony_ci    sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2294e1051a39Sopenharmony_ci    X509_NAME_free(xn);
2295e1051a39Sopenharmony_ci    return 0;
2296e1051a39Sopenharmony_ci}
2297e1051a39Sopenharmony_ci
2298e1051a39Sopenharmony_ciconst STACK_OF(X509_NAME) *get_ca_names(SSL *s)
2299e1051a39Sopenharmony_ci{
2300e1051a39Sopenharmony_ci    const STACK_OF(X509_NAME) *ca_sk = NULL;;
2301e1051a39Sopenharmony_ci
2302e1051a39Sopenharmony_ci    if (s->server) {
2303e1051a39Sopenharmony_ci        ca_sk = SSL_get_client_CA_list(s);
2304e1051a39Sopenharmony_ci        if (ca_sk != NULL && sk_X509_NAME_num(ca_sk) == 0)
2305e1051a39Sopenharmony_ci            ca_sk = NULL;
2306e1051a39Sopenharmony_ci    }
2307e1051a39Sopenharmony_ci
2308e1051a39Sopenharmony_ci    if (ca_sk == NULL)
2309e1051a39Sopenharmony_ci        ca_sk = SSL_get0_CA_list(s);
2310e1051a39Sopenharmony_ci
2311e1051a39Sopenharmony_ci    return ca_sk;
2312e1051a39Sopenharmony_ci}
2313e1051a39Sopenharmony_ci
2314e1051a39Sopenharmony_ciint construct_ca_names(SSL *s, const STACK_OF(X509_NAME) *ca_sk, WPACKET *pkt)
2315e1051a39Sopenharmony_ci{
2316e1051a39Sopenharmony_ci    /* Start sub-packet for client CA list */
2317e1051a39Sopenharmony_ci    if (!WPACKET_start_sub_packet_u16(pkt)) {
2318e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2319e1051a39Sopenharmony_ci        return 0;
2320e1051a39Sopenharmony_ci    }
2321e1051a39Sopenharmony_ci
2322e1051a39Sopenharmony_ci    if ((ca_sk != NULL) && !(s->options & SSL_OP_DISABLE_TLSEXT_CA_NAMES)) {
2323e1051a39Sopenharmony_ci        int i;
2324e1051a39Sopenharmony_ci
2325e1051a39Sopenharmony_ci        for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) {
2326e1051a39Sopenharmony_ci            unsigned char *namebytes;
2327e1051a39Sopenharmony_ci            X509_NAME *name = sk_X509_NAME_value(ca_sk, i);
2328e1051a39Sopenharmony_ci            int namelen;
2329e1051a39Sopenharmony_ci
2330e1051a39Sopenharmony_ci            if (name == NULL
2331e1051a39Sopenharmony_ci                    || (namelen = i2d_X509_NAME(name, NULL)) < 0
2332e1051a39Sopenharmony_ci                    || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2333e1051a39Sopenharmony_ci                                                       &namebytes)
2334e1051a39Sopenharmony_ci                    || i2d_X509_NAME(name, &namebytes) != namelen) {
2335e1051a39Sopenharmony_ci                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2336e1051a39Sopenharmony_ci                return 0;
2337e1051a39Sopenharmony_ci            }
2338e1051a39Sopenharmony_ci        }
2339e1051a39Sopenharmony_ci    }
2340e1051a39Sopenharmony_ci
2341e1051a39Sopenharmony_ci    if (!WPACKET_close(pkt)) {
2342e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2343e1051a39Sopenharmony_ci        return 0;
2344e1051a39Sopenharmony_ci    }
2345e1051a39Sopenharmony_ci
2346e1051a39Sopenharmony_ci    return 1;
2347e1051a39Sopenharmony_ci}
2348e1051a39Sopenharmony_ci
2349e1051a39Sopenharmony_ci/* Create a buffer containing data to be signed for server key exchange */
2350e1051a39Sopenharmony_cisize_t construct_key_exchange_tbs(SSL *s, unsigned char **ptbs,
2351e1051a39Sopenharmony_ci                                  const void *param, size_t paramlen)
2352e1051a39Sopenharmony_ci{
2353e1051a39Sopenharmony_ci    size_t tbslen = 2 * SSL3_RANDOM_SIZE + paramlen;
2354e1051a39Sopenharmony_ci    unsigned char *tbs = OPENSSL_malloc(tbslen);
2355e1051a39Sopenharmony_ci
2356e1051a39Sopenharmony_ci    if (tbs == NULL) {
2357e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2358e1051a39Sopenharmony_ci        return 0;
2359e1051a39Sopenharmony_ci    }
2360e1051a39Sopenharmony_ci    memcpy(tbs, s->s3.client_random, SSL3_RANDOM_SIZE);
2361e1051a39Sopenharmony_ci    memcpy(tbs + SSL3_RANDOM_SIZE, s->s3.server_random, SSL3_RANDOM_SIZE);
2362e1051a39Sopenharmony_ci
2363e1051a39Sopenharmony_ci    memcpy(tbs + SSL3_RANDOM_SIZE * 2, param, paramlen);
2364e1051a39Sopenharmony_ci
2365e1051a39Sopenharmony_ci    *ptbs = tbs;
2366e1051a39Sopenharmony_ci    return tbslen;
2367e1051a39Sopenharmony_ci}
2368e1051a39Sopenharmony_ci
2369e1051a39Sopenharmony_ci/*
2370e1051a39Sopenharmony_ci * Saves the current handshake digest for Post-Handshake Auth,
2371e1051a39Sopenharmony_ci * Done after ClientFinished is processed, done exactly once
2372e1051a39Sopenharmony_ci */
2373e1051a39Sopenharmony_ciint tls13_save_handshake_digest_for_pha(SSL *s)
2374e1051a39Sopenharmony_ci{
2375e1051a39Sopenharmony_ci    if (s->pha_dgst == NULL) {
2376e1051a39Sopenharmony_ci        if (!ssl3_digest_cached_records(s, 1))
2377e1051a39Sopenharmony_ci            /* SSLfatal() already called */
2378e1051a39Sopenharmony_ci            return 0;
2379e1051a39Sopenharmony_ci
2380e1051a39Sopenharmony_ci        s->pha_dgst = EVP_MD_CTX_new();
2381e1051a39Sopenharmony_ci        if (s->pha_dgst == NULL) {
2382e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2383e1051a39Sopenharmony_ci            return 0;
2384e1051a39Sopenharmony_ci        }
2385e1051a39Sopenharmony_ci        if (!EVP_MD_CTX_copy_ex(s->pha_dgst,
2386e1051a39Sopenharmony_ci                                s->s3.handshake_dgst)) {
2387e1051a39Sopenharmony_ci            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2388e1051a39Sopenharmony_ci            EVP_MD_CTX_free(s->pha_dgst);
2389e1051a39Sopenharmony_ci            s->pha_dgst = NULL;
2390e1051a39Sopenharmony_ci            return 0;
2391e1051a39Sopenharmony_ci        }
2392e1051a39Sopenharmony_ci    }
2393e1051a39Sopenharmony_ci    return 1;
2394e1051a39Sopenharmony_ci}
2395e1051a39Sopenharmony_ci
2396e1051a39Sopenharmony_ci/*
2397e1051a39Sopenharmony_ci * Restores the Post-Handshake Auth handshake digest
2398e1051a39Sopenharmony_ci * Done just before sending/processing the Cert Request
2399e1051a39Sopenharmony_ci */
2400e1051a39Sopenharmony_ciint tls13_restore_handshake_digest_for_pha(SSL *s)
2401e1051a39Sopenharmony_ci{
2402e1051a39Sopenharmony_ci    if (s->pha_dgst == NULL) {
2403e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2404e1051a39Sopenharmony_ci        return 0;
2405e1051a39Sopenharmony_ci    }
2406e1051a39Sopenharmony_ci    if (!EVP_MD_CTX_copy_ex(s->s3.handshake_dgst,
2407e1051a39Sopenharmony_ci                            s->pha_dgst)) {
2408e1051a39Sopenharmony_ci        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2409e1051a39Sopenharmony_ci        return 0;
2410e1051a39Sopenharmony_ci    }
2411e1051a39Sopenharmony_ci    return 1;
2412e1051a39Sopenharmony_ci}
2413