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