/src/openssl32/ssl/statem/extensions_srvr.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
5  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
6  |  |  * in the file LICENSE in the source distribution or at  | 
7  |  |  * https://www.openssl.org/source/license.html  | 
8  |  |  */  | 
9  |  |  | 
10  |  | #include <openssl/ocsp.h>  | 
11  |  | #include "../ssl_local.h"  | 
12  |  | #include "statem_local.h"  | 
13  |  | #include "internal/cryptlib.h"  | 
14  |  |  | 
15  | 0  | #define COOKIE_STATE_FORMAT_VERSION     1  | 
16  |  |  | 
17  |  | /*  | 
18  |  |  * 2 bytes for packet length, 2 bytes for format version, 2 bytes for  | 
19  |  |  * protocol version, 2 bytes for group id, 2 bytes for cipher id, 1 byte for  | 
20  |  |  * key_share present flag, 8 bytes for timestamp, 2 bytes for the hashlen,  | 
21  |  |  * EVP_MAX_MD_SIZE for transcript hash, 1 byte for app cookie length, app cookie  | 
22  |  |  * length bytes, SHA256_DIGEST_LENGTH bytes for the HMAC of the whole thing.  | 
23  |  |  */  | 
24  | 0  | #define MAX_COOKIE_SIZE (2 + 2 + 2 + 2 + 2 + 1 + 8 + 2 + EVP_MAX_MD_SIZE + 1 \  | 
25  | 0  |                          + SSL_COOKIE_LENGTH + SHA256_DIGEST_LENGTH)  | 
26  |  |  | 
27  |  | /*  | 
28  |  |  * Message header + 2 bytes for protocol version + number of random bytes +  | 
29  |  |  * + 1 byte for legacy session id length + number of bytes in legacy session id  | 
30  |  |  * + 2 bytes for ciphersuite + 1 byte for legacy compression  | 
31  |  |  * + 2 bytes for extension block length + 6 bytes for key_share extension  | 
32  |  |  * + 4 bytes for cookie extension header + the number of bytes in the cookie  | 
33  |  |  */  | 
34  |  | #define MAX_HRR_SIZE    (SSL3_HM_HEADER_LENGTH + 2 + SSL3_RANDOM_SIZE + 1 \  | 
35  |  |                          + SSL_MAX_SSL_SESSION_ID_LENGTH + 2 + 1 + 2 + 6 + 4 \  | 
36  |  |                          + MAX_COOKIE_SIZE)  | 
37  |  |  | 
38  |  | /*  | 
39  |  |  * Parse the client's renegotiation binding and abort if it's not right  | 
40  |  |  */  | 
41  |  | int tls_parse_ctos_renegotiate(SSL_CONNECTION *s, PACKET *pkt,  | 
42  |  |                                unsigned int context,  | 
43  |  |                                X509 *x, size_t chainidx)  | 
44  | 1.04k  | { | 
45  | 1.04k  |     unsigned int ilen;  | 
46  | 1.04k  |     const unsigned char *data;  | 
47  | 1.04k  |     int ok;  | 
48  |  |  | 
49  |  |     /* Parse the length byte */  | 
50  | 1.04k  |     if (!PACKET_get_1(pkt, &ilen)  | 
51  | 1.04k  |         || !PACKET_get_bytes(pkt, &data, ilen)) { | 
52  | 18  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);  | 
53  | 18  |         return 0;  | 
54  | 18  |     }  | 
55  |  |  | 
56  |  |     /* Check that the extension matches */  | 
57  | 1.03k  |     if (ilen != s->s3.previous_client_finished_len) { | 
58  | 9  |         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);  | 
59  | 9  |         return 0;  | 
60  | 9  |     }  | 
61  |  |  | 
62  | 1.02k  |     ok = memcmp(data, s->s3.previous_client_finished,  | 
63  | 1.02k  |                     s->s3.previous_client_finished_len);  | 
64  | 1.02k  | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION  | 
65  | 1.02k  |     if (ok) { | 
66  | 0  |         if ((data[0] ^ s->s3.previous_client_finished[0]) != 0xFF) { | 
67  | 0  |             ok = 0;  | 
68  | 0  |         }  | 
69  | 0  |     }  | 
70  | 1.02k  | #endif  | 
71  | 1.02k  |     if (ok) { | 
72  | 0  |         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);  | 
73  | 0  |         return 0;  | 
74  | 0  |     }  | 
75  |  |  | 
76  | 1.02k  |     s->s3.send_connection_binding = 1;  | 
77  |  |  | 
78  | 1.02k  |     return 1;  | 
79  | 1.02k  | }  | 
80  |  |  | 
81  |  | /*-  | 
82  |  |  * The servername extension is treated as follows:  | 
83  |  |  *  | 
84  |  |  * - Only the hostname type is supported with a maximum length of 255.  | 
85  |  |  * - The servername is rejected if too long or if it contains zeros,  | 
86  |  |  *   in which case an fatal alert is generated.  | 
87  |  |  * - The servername field is maintained together with the session cache.  | 
88  |  |  * - When a session is resumed, the servername call back invoked in order  | 
89  |  |  *   to allow the application to position itself to the right context.  | 
90  |  |  * - The servername is acknowledged if it is new for a session or when  | 
91  |  |  *   it is identical to a previously used for the same session.  | 
92  |  |  *   Applications can control the behaviour.  They can at any time  | 
93  |  |  *   set a 'desirable' servername for a new SSL object. This can be the  | 
94  |  |  *   case for example with HTTPS when a Host: header field is received and  | 
95  |  |  *   a renegotiation is requested. In this case, a possible servername  | 
96  |  |  *   presented in the new client hello is only acknowledged if it matches  | 
97  |  |  *   the value of the Host: field.  | 
98  |  |  * - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION  | 
99  |  |  *   if they provide for changing an explicit servername context for the  | 
100  |  |  *   session, i.e. when the session has been established with a servername  | 
101  |  |  *   extension.  | 
102  |  |  * - On session reconnect, the servername extension may be absent.  | 
103  |  |  */  | 
104  |  | int tls_parse_ctos_server_name(SSL_CONNECTION *s, PACKET *pkt,  | 
105  |  |                                unsigned int context, X509 *x, size_t chainidx)  | 
106  | 3.07k  | { | 
107  | 3.07k  |     unsigned int servname_type;  | 
108  | 3.07k  |     PACKET sni, hostname;  | 
109  |  |  | 
110  | 3.07k  |     if (!PACKET_as_length_prefixed_2(pkt, &sni)  | 
111  |  |         /* ServerNameList must be at least 1 byte long. */  | 
112  | 3.07k  |         || PACKET_remaining(&sni) == 0) { | 
113  | 141  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
114  | 141  |         return 0;  | 
115  | 141  |     }  | 
116  |  |  | 
117  |  |     /*  | 
118  |  |      * Although the intent was for server_name to be extensible, RFC 4366  | 
119  |  |      * was not clear about it; and so OpenSSL among other implementations,  | 
120  |  |      * always and only allows a 'host_name' name types.  | 
121  |  |      * RFC 6066 corrected the mistake but adding new name types  | 
122  |  |      * is nevertheless no longer feasible, so act as if no other  | 
123  |  |      * SNI types can exist, to simplify parsing.  | 
124  |  |      *  | 
125  |  |      * Also note that the RFC permits only one SNI value per type,  | 
126  |  |      * i.e., we can only have a single hostname.  | 
127  |  |      */  | 
128  | 2.93k  |     if (!PACKET_get_1(&sni, &servname_type)  | 
129  | 2.93k  |         || servname_type != TLSEXT_NAMETYPE_host_name  | 
130  | 2.93k  |         || !PACKET_as_length_prefixed_2(&sni, &hostname)) { | 
131  | 79  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
132  | 79  |         return 0;  | 
133  | 79  |     }  | 
134  |  |  | 
135  |  |     /*  | 
136  |  |      * In TLSv1.2 and below the SNI is associated with the session. In TLSv1.3  | 
137  |  |      * we always use the SNI value from the handshake.  | 
138  |  |      */  | 
139  | 2.85k  |     if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { | 
140  | 2.84k  |         if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) { | 
141  | 4  |             SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);  | 
142  | 4  |             return 0;  | 
143  | 4  |         }  | 
144  |  |  | 
145  | 2.84k  |         if (PACKET_contains_zero_byte(&hostname)) { | 
146  | 15  |             SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);  | 
147  | 15  |             return 0;  | 
148  | 15  |         }  | 
149  |  |  | 
150  |  |         /*  | 
151  |  |          * Store the requested SNI in the SSL as temporary storage.  | 
152  |  |          * If we accept it, it will get stored in the SSL_SESSION as well.  | 
153  |  |          */  | 
154  | 2.82k  |         OPENSSL_free(s->ext.hostname);  | 
155  | 2.82k  |         s->ext.hostname = NULL;  | 
156  | 2.82k  |         if (!PACKET_strndup(&hostname, &s->ext.hostname)) { | 
157  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
158  | 0  |             return 0;  | 
159  | 0  |         }  | 
160  |  |  | 
161  | 2.82k  |         s->servername_done = 1;  | 
162  | 2.82k  |     } else { | 
163  |  |         /*  | 
164  |  |          * In TLSv1.2 and below we should check if the SNI is consistent between  | 
165  |  |          * the initial handshake and the resumption. In TLSv1.3 SNI is not  | 
166  |  |          * associated with the session.  | 
167  |  |          */  | 
168  | 8  |         s->servername_done = (s->session->ext.hostname != NULL)  | 
169  | 8  |             && PACKET_equal(&hostname, s->session->ext.hostname,  | 
170  | 4  |                             strlen(s->session->ext.hostname));  | 
171  | 8  |     }  | 
172  |  |  | 
173  | 2.83k  |     return 1;  | 
174  | 2.85k  | }  | 
175  |  |  | 
176  |  | int tls_parse_ctos_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,  | 
177  |  |                                   unsigned int context,  | 
178  |  |                                   X509 *x, size_t chainidx)  | 
179  | 497  | { | 
180  | 497  |     unsigned int value;  | 
181  |  |  | 
182  | 497  |     if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) { | 
183  | 55  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
184  | 55  |         return 0;  | 
185  | 55  |     }  | 
186  |  |  | 
187  |  |     /* Received |value| should be a valid max-fragment-length code. */  | 
188  | 442  |     if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) { | 
189  | 29  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,  | 
190  | 29  |                  SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);  | 
191  | 29  |         return 0;  | 
192  | 29  |     }  | 
193  |  |  | 
194  |  |     /*  | 
195  |  |      * When doing a full handshake or a renegotiation max_fragment_len_mode will  | 
196  |  |      * be TLSEXT_max_fragment_length_UNSPECIFIED  | 
197  |  |      *  | 
198  |  |      * In case of a resumption max_fragment_len_mode will be one of  | 
199  |  |      *      TLSEXT_max_fragment_length_DISABLED, TLSEXT_max_fragment_length_512,  | 
200  |  |      *      TLSEXT_max_fragment_length_1024, TLSEXT_max_fragment_length_2048.  | 
201  |  |      *      TLSEXT_max_fragment_length_4096  | 
202  |  |      *  | 
203  |  |      * RFC 6066: The negotiated length applies for the duration of the session  | 
204  |  |      * including session resumptions.  | 
205  |  |      *  | 
206  |  |      * So we only set the value in case it is unspecified.  | 
207  |  |      */  | 
208  | 413  |     if (s->session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED)  | 
209  |  |         /*  | 
210  |  |          * Store it in session, so it'll become binding for us  | 
211  |  |          * and we'll include it in a next Server Hello.  | 
212  |  |          */  | 
213  | 409  |         s->session->ext.max_fragment_len_mode = value;  | 
214  |  |  | 
215  | 413  |     return 1;  | 
216  | 442  | }  | 
217  |  |  | 
218  |  | #ifndef OPENSSL_NO_SRP  | 
219  |  | int tls_parse_ctos_srp(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
220  |  |                        X509 *x, size_t chainidx)  | 
221  | 67  | { | 
222  | 67  |     PACKET srp_I;  | 
223  |  |  | 
224  | 67  |     if (!PACKET_as_length_prefixed_1(pkt, &srp_I)  | 
225  | 67  |             || PACKET_contains_zero_byte(&srp_I)) { | 
226  | 46  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
227  | 46  |         return 0;  | 
228  | 46  |     }  | 
229  |  |  | 
230  | 21  |     if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) { | 
231  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
232  | 0  |         return 0;  | 
233  | 0  |     }  | 
234  |  |  | 
235  | 21  |     return 1;  | 
236  | 21  | }  | 
237  |  | #endif  | 
238  |  |  | 
239  |  | int tls_parse_ctos_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt,  | 
240  |  |                                  unsigned int context,  | 
241  |  |                                  X509 *x, size_t chainidx)  | 
242  | 2.01k  | { | 
243  | 2.01k  |     PACKET ec_point_format_list;  | 
244  |  |  | 
245  | 2.01k  |     if (!PACKET_as_length_prefixed_1(pkt, &ec_point_format_list)  | 
246  | 2.01k  |         || PACKET_remaining(&ec_point_format_list) == 0) { | 
247  | 72  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
248  | 72  |         return 0;  | 
249  | 72  |     }  | 
250  |  |  | 
251  | 1.94k  |     if (!s->hit) { | 
252  | 1.83k  |         if (!PACKET_memdup(&ec_point_format_list,  | 
253  | 1.83k  |                            &s->ext.peer_ecpointformats,  | 
254  | 1.83k  |                            &s->ext.peer_ecpointformats_len)) { | 
255  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
256  | 0  |             return 0;  | 
257  | 0  |         }  | 
258  | 1.83k  |     }  | 
259  |  |  | 
260  | 1.94k  |     return 1;  | 
261  | 1.94k  | }  | 
262  |  |  | 
263  |  | int tls_parse_ctos_session_ticket(SSL_CONNECTION *s, PACKET *pkt,  | 
264  |  |                                   unsigned int context,  | 
265  |  |                                   X509 *x, size_t chainidx)  | 
266  | 1.42k  | { | 
267  | 1.42k  |     if (s->ext.session_ticket_cb &&  | 
268  | 1.42k  |             !s->ext.session_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s),  | 
269  | 0  |                                       PACKET_data(pkt), PACKET_remaining(pkt),  | 
270  | 0  |                                       s->ext.session_ticket_cb_arg)) { | 
271  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
272  | 0  |         return 0;  | 
273  | 0  |     }  | 
274  |  |  | 
275  | 1.42k  |     return 1;  | 
276  | 1.42k  | }  | 
277  |  |  | 
278  |  | int tls_parse_ctos_sig_algs_cert(SSL_CONNECTION *s, PACKET *pkt,  | 
279  |  |                                  ossl_unused unsigned int context,  | 
280  |  |                                  ossl_unused X509 *x,  | 
281  |  |                                  ossl_unused size_t chainidx)  | 
282  | 643  | { | 
283  | 643  |     PACKET supported_sig_algs;  | 
284  |  |  | 
285  | 643  |     if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)  | 
286  | 643  |             || PACKET_remaining(&supported_sig_algs) == 0) { | 
287  | 60  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
288  | 60  |         return 0;  | 
289  | 60  |     }  | 
290  |  |  | 
291  |  |     /*  | 
292  |  |      * We use this routine on both clients and servers, and when clients  | 
293  |  |      * get asked for PHA we need to always save the sigalgs regardless  | 
294  |  |      * of whether it was a resumption or not.  | 
295  |  |      */  | 
296  | 583  |     if ((!s->server || (s->server && !s->hit))  | 
297  | 583  |             && !tls1_save_sigalgs(s, &supported_sig_algs, 1)) { | 
298  | 6  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
299  | 6  |         return 0;  | 
300  | 6  |     }  | 
301  |  |  | 
302  | 577  |     return 1;  | 
303  | 583  | }  | 
304  |  |  | 
305  |  | int tls_parse_ctos_sig_algs(SSL_CONNECTION *s, PACKET *pkt,  | 
306  |  |                             unsigned int context, X509 *x, size_t chainidx)  | 
307  | 4.41k  | { | 
308  | 4.41k  |     PACKET supported_sig_algs;  | 
309  |  |  | 
310  | 4.41k  |     if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)  | 
311  | 4.41k  |             || PACKET_remaining(&supported_sig_algs) == 0) { | 
312  | 104  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
313  | 104  |         return 0;  | 
314  | 104  |     }  | 
315  |  |  | 
316  |  |     /*  | 
317  |  |      * We use this routine on both clients and servers, and when clients  | 
318  |  |      * get asked for PHA we need to always save the sigalgs regardless  | 
319  |  |      * of whether it was a resumption or not.  | 
320  |  |      */  | 
321  | 4.31k  |     if ((!s->server || (s->server && !s->hit))  | 
322  | 4.31k  |             && !tls1_save_sigalgs(s, &supported_sig_algs, 0)) { | 
323  | 6  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
324  | 6  |         return 0;  | 
325  | 6  |     }  | 
326  |  |  | 
327  | 4.30k  |     return 1;  | 
328  | 4.31k  | }  | 
329  |  |  | 
330  |  | #ifndef OPENSSL_NO_OCSP  | 
331  |  | int tls_parse_ctos_status_request(SSL_CONNECTION *s, PACKET *pkt,  | 
332  |  |                                   unsigned int context,  | 
333  |  |                                   X509 *x, size_t chainidx)  | 
334  | 3.32k  | { | 
335  | 3.32k  |     PACKET responder_id_list, exts;  | 
336  |  |  | 
337  |  |     /* We ignore this in a resumption handshake */  | 
338  | 3.32k  |     if (s->hit)  | 
339  | 31  |         return 1;  | 
340  |  |  | 
341  |  |     /* Not defined if we get one of these in a client Certificate */  | 
342  | 3.29k  |     if (x != NULL)  | 
343  | 0  |         return 1;  | 
344  |  |  | 
345  | 3.29k  |     if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) { | 
346  | 7  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
347  | 7  |         return 0;  | 
348  | 7  |     }  | 
349  |  |  | 
350  | 3.28k  |     if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) { | 
351  |  |         /*  | 
352  |  |          * We don't know what to do with any other type so ignore it.  | 
353  |  |          */  | 
354  | 129  |         s->ext.status_type = TLSEXT_STATUSTYPE_nothing;  | 
355  | 129  |         return 1;  | 
356  | 129  |     }  | 
357  |  |  | 
358  | 3.15k  |     if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) { | 
359  | 56  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
360  | 56  |         return 0;  | 
361  | 56  |     }  | 
362  |  |  | 
363  |  |     /*  | 
364  |  |      * We remove any OCSP_RESPIDs from a previous handshake  | 
365  |  |      * to prevent unbounded memory growth - CVE-2016-6304  | 
366  |  |      */  | 
367  | 3.10k  |     sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);  | 
368  | 3.10k  |     if (PACKET_remaining(&responder_id_list) > 0) { | 
369  | 2.27k  |         s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();  | 
370  | 2.27k  |         if (s->ext.ocsp.ids == NULL) { | 
371  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);  | 
372  | 0  |             return 0;  | 
373  | 0  |         }  | 
374  | 2.27k  |     } else { | 
375  | 830  |         s->ext.ocsp.ids = NULL;  | 
376  | 830  |     }  | 
377  |  |  | 
378  | 3.17k  |     while (PACKET_remaining(&responder_id_list) > 0) { | 
379  | 2.32k  |         OCSP_RESPID *id;  | 
380  | 2.32k  |         PACKET responder_id;  | 
381  | 2.32k  |         const unsigned char *id_data;  | 
382  |  |  | 
383  | 2.32k  |         if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)  | 
384  | 2.32k  |                 || PACKET_remaining(&responder_id) == 0) { | 
385  | 57  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
386  | 57  |             return 0;  | 
387  | 57  |         }  | 
388  |  |  | 
389  | 2.27k  |         id_data = PACKET_data(&responder_id);  | 
390  | 2.27k  |         id = d2i_OCSP_RESPID(NULL, &id_data,  | 
391  | 2.27k  |                              (int)PACKET_remaining(&responder_id));  | 
392  | 2.27k  |         if (id == NULL) { | 
393  | 2.15k  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
394  | 2.15k  |             return 0;  | 
395  | 2.15k  |         }  | 
396  |  |  | 
397  | 115  |         if (id_data != PACKET_end(&responder_id)) { | 
398  | 45  |             OCSP_RESPID_free(id);  | 
399  | 45  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
400  |  |  | 
401  | 45  |             return 0;  | 
402  | 45  |         }  | 
403  |  |  | 
404  | 70  |         if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) { | 
405  | 0  |             OCSP_RESPID_free(id);  | 
406  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
407  |  | 
  | 
408  | 0  |             return 0;  | 
409  | 0  |         }  | 
410  | 70  |     }  | 
411  |  |  | 
412  |  |     /* Read in request_extensions */  | 
413  | 843  |     if (!PACKET_as_length_prefixed_2(pkt, &exts)) { | 
414  | 76  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
415  | 76  |         return 0;  | 
416  | 76  |     }  | 
417  |  |  | 
418  | 767  |     if (PACKET_remaining(&exts) > 0) { | 
419  | 171  |         const unsigned char *ext_data = PACKET_data(&exts);  | 
420  |  |  | 
421  | 171  |         sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,  | 
422  | 171  |                                    X509_EXTENSION_free);  | 
423  | 171  |         s->ext.ocsp.exts =  | 
424  | 171  |             d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));  | 
425  | 171  |         if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) { | 
426  | 167  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
427  | 167  |             return 0;  | 
428  | 167  |         }  | 
429  | 171  |     }  | 
430  |  |  | 
431  | 600  |     return 1;  | 
432  | 767  | }  | 
433  |  | #endif  | 
434  |  |  | 
435  |  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
436  |  | int tls_parse_ctos_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
437  |  |                        X509 *x, size_t chainidx)  | 
438  | 10  | { | 
439  |  |     /*  | 
440  |  |      * We shouldn't accept this extension on a  | 
441  |  |      * renegotiation.  | 
442  |  |      */  | 
443  | 10  |     if (SSL_IS_FIRST_HANDSHAKE(s))  | 
444  | 10  |         s->s3.npn_seen = 1;  | 
445  |  |  | 
446  | 10  |     return 1;  | 
447  | 10  | }  | 
448  |  | #endif  | 
449  |  |  | 
450  |  | /*  | 
451  |  |  * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN  | 
452  |  |  * extension, not including type and length. Returns: 1 on success, 0 on error.  | 
453  |  |  */  | 
454  |  | int tls_parse_ctos_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
455  |  |                         X509 *x, size_t chainidx)  | 
456  | 162  | { | 
457  | 162  |     PACKET protocol_list, save_protocol_list, protocol;  | 
458  |  |  | 
459  | 162  |     if (!SSL_IS_FIRST_HANDSHAKE(s))  | 
460  | 0  |         return 1;  | 
461  |  |  | 
462  | 162  |     if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)  | 
463  | 162  |         || PACKET_remaining(&protocol_list) < 2) { | 
464  | 72  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
465  | 72  |         return 0;  | 
466  | 72  |     }  | 
467  |  |  | 
468  | 90  |     save_protocol_list = protocol_list;  | 
469  | 1.80k  |     do { | 
470  |  |         /* Protocol names can't be empty. */  | 
471  | 1.80k  |         if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)  | 
472  | 1.80k  |                 || PACKET_remaining(&protocol) == 0) { | 
473  | 68  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
474  | 68  |             return 0;  | 
475  | 68  |         }  | 
476  | 1.80k  |     } while (PACKET_remaining(&protocol_list) != 0);  | 
477  |  |  | 
478  | 22  |     OPENSSL_free(s->s3.alpn_proposed);  | 
479  | 22  |     s->s3.alpn_proposed = NULL;  | 
480  | 22  |     s->s3.alpn_proposed_len = 0;  | 
481  | 22  |     if (!PACKET_memdup(&save_protocol_list,  | 
482  | 22  |                        &s->s3.alpn_proposed, &s->s3.alpn_proposed_len)) { | 
483  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
484  | 0  |         return 0;  | 
485  | 0  |     }  | 
486  |  |  | 
487  | 22  |     return 1;  | 
488  | 22  | }  | 
489  |  |  | 
490  |  | #ifndef OPENSSL_NO_SRTP  | 
491  |  | int tls_parse_ctos_use_srtp(SSL_CONNECTION *s, PACKET *pkt,  | 
492  |  |                             unsigned int context, X509 *x, size_t chainidx)  | 
493  | 14  | { | 
494  | 14  |     STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;  | 
495  | 14  |     unsigned int ct, mki_len, id;  | 
496  | 14  |     int i, srtp_pref;  | 
497  | 14  |     PACKET subpkt;  | 
498  | 14  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
499  |  |  | 
500  |  |     /* Ignore this if we have no SRTP profiles */  | 
501  | 14  |     if (SSL_get_srtp_profiles(ssl) == NULL)  | 
502  | 14  |         return 1;  | 
503  |  |  | 
504  |  |     /* Pull off the length of the cipher suite list  and check it is even */  | 
505  | 0  |     if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0  | 
506  | 0  |             || !PACKET_get_sub_packet(pkt, &subpkt, ct)) { | 
507  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR,  | 
508  | 0  |                SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);  | 
509  | 0  |         return 0;  | 
510  | 0  |     }  | 
511  |  |  | 
512  | 0  |     srvr = SSL_get_srtp_profiles(ssl);  | 
513  | 0  |     s->srtp_profile = NULL;  | 
514  |  |     /* Search all profiles for a match initially */  | 
515  | 0  |     srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);  | 
516  |  | 
  | 
517  | 0  |     while (PACKET_remaining(&subpkt)) { | 
518  | 0  |         if (!PACKET_get_net_2(&subpkt, &id)) { | 
519  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR,  | 
520  | 0  |                      SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);  | 
521  | 0  |             return 0;  | 
522  | 0  |         }  | 
523  |  |  | 
524  |  |         /*  | 
525  |  |          * Only look for match in profiles of higher preference than  | 
526  |  |          * current match.  | 
527  |  |          * If no profiles have been have been configured then this  | 
528  |  |          * does nothing.  | 
529  |  |          */  | 
530  | 0  |         for (i = 0; i < srtp_pref; i++) { | 
531  | 0  |             SRTP_PROTECTION_PROFILE *sprof =  | 
532  | 0  |                 sk_SRTP_PROTECTION_PROFILE_value(srvr, i);  | 
533  |  | 
  | 
534  | 0  |             if (sprof->id == id) { | 
535  | 0  |                 s->srtp_profile = sprof;  | 
536  | 0  |                 srtp_pref = i;  | 
537  | 0  |                 break;  | 
538  | 0  |             }  | 
539  | 0  |         }  | 
540  | 0  |     }  | 
541  |  |  | 
542  |  |     /* Now extract the MKI value as a sanity check, but discard it for now */  | 
543  | 0  |     if (!PACKET_get_1(pkt, &mki_len)) { | 
544  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR,  | 
545  | 0  |                  SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);  | 
546  | 0  |         return 0;  | 
547  | 0  |     }  | 
548  |  |  | 
549  | 0  |     if (!PACKET_forward(pkt, mki_len)  | 
550  | 0  |         || PACKET_remaining(pkt)) { | 
551  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRTP_MKI_VALUE);  | 
552  | 0  |         return 0;  | 
553  | 0  |     }  | 
554  |  |  | 
555  | 0  |     return 1;  | 
556  | 0  | }  | 
557  |  | #endif  | 
558  |  |  | 
559  |  | int tls_parse_ctos_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
560  |  |                        X509 *x, size_t chainidx)  | 
561  | 655  | { | 
562  | 655  |     if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))  | 
563  | 655  |         s->ext.use_etm = 1;  | 
564  |  |  | 
565  | 655  |     return 1;  | 
566  | 655  | }  | 
567  |  |  | 
568  |  | /*  | 
569  |  |  * Process a psk_kex_modes extension received in the ClientHello. |pkt| contains  | 
570  |  |  * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.  | 
571  |  |  */  | 
572  |  | int tls_parse_ctos_psk_kex_modes(SSL_CONNECTION *s, PACKET *pkt,  | 
573  |  |                                  unsigned int context,  | 
574  |  |                                  X509 *x, size_t chainidx)  | 
575  | 1.13k  | { | 
576  | 1.13k  | #ifndef OPENSSL_NO_TLS1_3  | 
577  | 1.13k  |     PACKET psk_kex_modes;  | 
578  | 1.13k  |     unsigned int mode;  | 
579  |  |  | 
580  | 1.13k  |     if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)  | 
581  | 1.13k  |             || PACKET_remaining(&psk_kex_modes) == 0) { | 
582  | 32  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
583  | 32  |         return 0;  | 
584  | 32  |     }  | 
585  |  |  | 
586  | 6.05k  |     while (PACKET_get_1(&psk_kex_modes, &mode)) { | 
587  | 4.95k  |         if (mode == TLSEXT_KEX_MODE_KE_DHE)  | 
588  | 1.68k  |             s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;  | 
589  | 3.27k  |         else if (mode == TLSEXT_KEX_MODE_KE  | 
590  | 3.27k  |                 && (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)  | 
591  | 0  |             s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;  | 
592  | 4.95k  |     }  | 
593  | 1.10k  | #endif  | 
594  |  |  | 
595  | 1.10k  |     return 1;  | 
596  | 1.13k  | }  | 
597  |  |  | 
598  |  | /*  | 
599  |  |  * Process a key_share extension received in the ClientHello. |pkt| contains  | 
600  |  |  * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.  | 
601  |  |  */  | 
602  |  | int tls_parse_ctos_key_share(SSL_CONNECTION *s, PACKET *pkt,  | 
603  |  |                              unsigned int context, X509 *x, size_t chainidx)  | 
604  | 2.08k  | { | 
605  | 2.08k  | #ifndef OPENSSL_NO_TLS1_3  | 
606  | 2.08k  |     unsigned int group_id;  | 
607  | 2.08k  |     PACKET key_share_list, encoded_pt;  | 
608  | 2.08k  |     const uint16_t *clntgroups, *srvrgroups;  | 
609  | 2.08k  |     size_t clnt_num_groups, srvr_num_groups;  | 
610  | 2.08k  |     int found = 0;  | 
611  |  |  | 
612  | 2.08k  |     if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)  | 
613  | 0  |         return 1;  | 
614  |  |  | 
615  |  |     /* Sanity check */  | 
616  | 2.08k  |     if (s->s3.peer_tmp != NULL) { | 
617  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
618  | 0  |         return 0;  | 
619  | 0  |     }  | 
620  |  |  | 
621  | 2.08k  |     if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) { | 
622  | 40  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
623  | 40  |         return 0;  | 
624  | 40  |     }  | 
625  |  |  | 
626  |  |     /* Get our list of supported groups */  | 
627  | 2.04k  |     tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups);  | 
628  |  |     /* Get the clients list of supported groups. */  | 
629  | 2.04k  |     tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);  | 
630  | 2.04k  |     if (clnt_num_groups == 0) { | 
631  |  |         /*  | 
632  |  |          * This can only happen if the supported_groups extension was not sent,  | 
633  |  |          * because we verify that the length is non-zero when we process that  | 
634  |  |          * extension.  | 
635  |  |          */  | 
636  | 4  |         SSLfatal(s, SSL_AD_MISSING_EXTENSION,  | 
637  | 4  |                  SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);  | 
638  | 4  |         return 0;  | 
639  | 4  |     }  | 
640  |  |  | 
641  | 2.03k  |     if (s->s3.group_id != 0 && PACKET_remaining(&key_share_list) == 0) { | 
642  |  |         /*  | 
643  |  |          * If we set a group_id already, then we must have sent an HRR  | 
644  |  |          * requesting a new key_share. If we haven't got one then that is an  | 
645  |  |          * error  | 
646  |  |          */  | 
647  | 3  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);  | 
648  | 3  |         return 0;  | 
649  | 3  |     }  | 
650  |  |  | 
651  | 4.21k  |     while (PACKET_remaining(&key_share_list) > 0) { | 
652  | 2.33k  |         if (!PACKET_get_net_2(&key_share_list, &group_id)  | 
653  | 2.33k  |                 || !PACKET_get_length_prefixed_2(&key_share_list, &encoded_pt)  | 
654  | 2.33k  |                 || PACKET_remaining(&encoded_pt) == 0) { | 
655  | 59  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
656  | 59  |             return 0;  | 
657  | 59  |         }  | 
658  |  |  | 
659  |  |         /*  | 
660  |  |          * If we already found a suitable key_share we loop through the  | 
661  |  |          * rest to verify the structure, but don't process them.  | 
662  |  |          */  | 
663  | 2.27k  |         if (found)  | 
664  | 220  |             continue;  | 
665  |  |  | 
666  |  |         /*  | 
667  |  |          * If we sent an HRR then the key_share sent back MUST be for the group  | 
668  |  |          * we requested, and must be the only key_share sent.  | 
669  |  |          */  | 
670  | 2.05k  |         if (s->s3.group_id != 0  | 
671  | 2.05k  |                 && (group_id != s->s3.group_id  | 
672  | 122  |                     || PACKET_remaining(&key_share_list) != 0)) { | 
673  | 29  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);  | 
674  | 29  |             return 0;  | 
675  | 29  |         }  | 
676  |  |  | 
677  |  |         /* Check if this share is in supported_groups sent from client */  | 
678  | 2.02k  |         if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0)) { | 
679  | 23  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);  | 
680  | 23  |             return 0;  | 
681  | 23  |         }  | 
682  |  |  | 
683  |  |         /* Check if this share is for a group we can use */  | 
684  | 2.00k  |         if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1)  | 
685  | 2.00k  |                 || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)  | 
686  |  |                    /*  | 
687  |  |                     * We tolerate but ignore a group id that we don't think is  | 
688  |  |                     * suitable for TLSv1.3  | 
689  |  |                     */  | 
690  | 2.00k  |                 || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,  | 
691  | 1.49k  |                                     0, NULL)) { | 
692  |  |             /* Share not suitable */  | 
693  | 512  |             continue;  | 
694  | 512  |         }  | 
695  |  |  | 
696  | 1.49k  |         s->s3.group_id = group_id;  | 
697  |  |         /* Cache the selected group ID in the SSL_SESSION */  | 
698  | 1.49k  |         s->session->kex_group = group_id;  | 
699  |  |  | 
700  | 1.49k  |         if ((s->s3.peer_tmp = ssl_generate_param_group(s, group_id)) == NULL) { | 
701  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR,  | 
702  | 0  |                    SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);  | 
703  | 0  |             return 0;  | 
704  | 0  |         }  | 
705  |  |  | 
706  | 1.49k  |         if (tls13_set_encoded_pub_key(s->s3.peer_tmp,  | 
707  | 1.49k  |                                       PACKET_data(&encoded_pt),  | 
708  | 1.49k  |                                       PACKET_remaining(&encoded_pt)) <= 0) { | 
709  | 45  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);  | 
710  | 45  |             return 0;  | 
711  | 45  |         }  | 
712  |  |  | 
713  | 1.44k  |         found = 1;  | 
714  | 1.44k  |     }  | 
715  | 1.87k  | #endif  | 
716  |  |  | 
717  | 1.87k  |     return 1;  | 
718  | 2.03k  | }  | 
719  |  |  | 
720  |  | int tls_parse_ctos_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
721  |  |                           X509 *x, size_t chainidx)  | 
722  | 82  | { | 
723  | 82  | #ifndef OPENSSL_NO_TLS1_3  | 
724  | 82  |     unsigned int format, version, key_share, group_id;  | 
725  | 82  |     EVP_MD_CTX *hctx;  | 
726  | 82  |     EVP_PKEY *pkey;  | 
727  | 82  |     PACKET cookie, raw, chhash, appcookie;  | 
728  | 82  |     WPACKET hrrpkt;  | 
729  | 82  |     const unsigned char *data, *mdin, *ciphdata;  | 
730  | 82  |     unsigned char hmac[SHA256_DIGEST_LENGTH];  | 
731  | 82  |     unsigned char hrr[MAX_HRR_SIZE];  | 
732  | 82  |     size_t rawlen, hmaclen, hrrlen, ciphlen;  | 
733  | 82  |     uint64_t tm, now;  | 
734  | 82  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
735  | 82  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
736  |  |  | 
737  |  |     /* Ignore any cookie if we're not set up to verify it */  | 
738  | 82  |     if (sctx->verify_stateless_cookie_cb == NULL  | 
739  | 82  |             || (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)  | 
740  | 82  |         return 1;  | 
741  |  |  | 
742  | 0  |     if (!PACKET_as_length_prefixed_2(pkt, &cookie)) { | 
743  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
744  | 0  |         return 0;  | 
745  | 0  |     }  | 
746  |  |  | 
747  | 0  |     raw = cookie;  | 
748  | 0  |     data = PACKET_data(&raw);  | 
749  | 0  |     rawlen = PACKET_remaining(&raw);  | 
750  | 0  |     if (rawlen < SHA256_DIGEST_LENGTH  | 
751  | 0  |             || !PACKET_forward(&raw, rawlen - SHA256_DIGEST_LENGTH)) { | 
752  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
753  | 0  |         return 0;  | 
754  | 0  |     }  | 
755  | 0  |     mdin = PACKET_data(&raw);  | 
756  |  |  | 
757  |  |     /* Verify the HMAC of the cookie */  | 
758  | 0  |     hctx = EVP_MD_CTX_create();  | 
759  | 0  |     pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",  | 
760  | 0  |                                            sctx->propq,  | 
761  | 0  |                                            s->session_ctx->ext.cookie_hmac_key,  | 
762  | 0  |                                            sizeof(s->session_ctx->ext.cookie_hmac_key));  | 
763  | 0  |     if (hctx == NULL || pkey == NULL) { | 
764  | 0  |         EVP_MD_CTX_free(hctx);  | 
765  | 0  |         EVP_PKEY_free(pkey);  | 
766  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);  | 
767  | 0  |         return 0;  | 
768  | 0  |     }  | 
769  |  |  | 
770  | 0  |     hmaclen = SHA256_DIGEST_LENGTH;  | 
771  | 0  |     if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,  | 
772  | 0  |                               sctx->propq, pkey, NULL) <= 0  | 
773  | 0  |             || EVP_DigestSign(hctx, hmac, &hmaclen, data,  | 
774  | 0  |                               rawlen - SHA256_DIGEST_LENGTH) <= 0  | 
775  | 0  |             || hmaclen != SHA256_DIGEST_LENGTH) { | 
776  | 0  |         EVP_MD_CTX_free(hctx);  | 
777  | 0  |         EVP_PKEY_free(pkey);  | 
778  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
779  | 0  |         return 0;  | 
780  | 0  |     }  | 
781  |  |  | 
782  | 0  |     EVP_MD_CTX_free(hctx);  | 
783  | 0  |     EVP_PKEY_free(pkey);  | 
784  |  | 
  | 
785  | 0  |     if (CRYPTO_memcmp(hmac, mdin, SHA256_DIGEST_LENGTH) != 0) { | 
786  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);  | 
787  | 0  |         return 0;  | 
788  | 0  |     }  | 
789  |  |  | 
790  | 0  |     if (!PACKET_get_net_2(&cookie, &format)) { | 
791  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
792  | 0  |         return 0;  | 
793  | 0  |     }  | 
794  |  |     /* Check the cookie format is something we recognise. Ignore it if not */  | 
795  | 0  |     if (format != COOKIE_STATE_FORMAT_VERSION)  | 
796  | 0  |         return 1;  | 
797  |  |  | 
798  |  |     /*  | 
799  |  |      * The rest of these checks really shouldn't fail since we have verified the  | 
800  |  |      * HMAC above.  | 
801  |  |      */  | 
802  |  |  | 
803  |  |     /* Check the version number is sane */  | 
804  | 0  |     if (!PACKET_get_net_2(&cookie, &version)) { | 
805  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
806  | 0  |         return 0;  | 
807  | 0  |     }  | 
808  | 0  |     if (version != TLS1_3_VERSION) { | 
809  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,  | 
810  | 0  |                  SSL_R_BAD_PROTOCOL_VERSION_NUMBER);  | 
811  | 0  |         return 0;  | 
812  | 0  |     }  | 
813  |  |  | 
814  | 0  |     if (!PACKET_get_net_2(&cookie, &group_id)) { | 
815  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
816  | 0  |         return 0;  | 
817  | 0  |     }  | 
818  |  |  | 
819  | 0  |     ciphdata = PACKET_data(&cookie);  | 
820  | 0  |     if (!PACKET_forward(&cookie, 2)) { | 
821  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
822  | 0  |         return 0;  | 
823  | 0  |     }  | 
824  | 0  |     if (group_id != s->s3.group_id  | 
825  | 0  |             || s->s3.tmp.new_cipher  | 
826  | 0  |                != ssl_get_cipher_by_char(s, ciphdata, 0)) { | 
827  |  |         /*  | 
828  |  |          * We chose a different cipher or group id this time around to what is  | 
829  |  |          * in the cookie. Something must have changed.  | 
830  |  |          */  | 
831  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);  | 
832  | 0  |         return 0;  | 
833  | 0  |     }  | 
834  |  |  | 
835  | 0  |     if (!PACKET_get_1(&cookie, &key_share)  | 
836  | 0  |             || !PACKET_get_net_8(&cookie, &tm)  | 
837  | 0  |             || !PACKET_get_length_prefixed_2(&cookie, &chhash)  | 
838  | 0  |             || !PACKET_get_length_prefixed_1(&cookie, &appcookie)  | 
839  | 0  |             || PACKET_remaining(&cookie) != SHA256_DIGEST_LENGTH) { | 
840  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
841  | 0  |         return 0;  | 
842  | 0  |     }  | 
843  |  |  | 
844  |  |     /* We tolerate a cookie age of up to 10 minutes (= 60 * 10 seconds) */  | 
845  | 0  |     now = time(NULL);  | 
846  | 0  |     if (tm > now || (now - tm) > 600) { | 
847  |  |         /* Cookie is stale. Ignore it */  | 
848  | 0  |         return 1;  | 
849  | 0  |     }  | 
850  |  |  | 
851  |  |     /* Verify the app cookie */  | 
852  | 0  |     if (sctx->verify_stateless_cookie_cb(SSL_CONNECTION_GET_USER_SSL(s),  | 
853  | 0  |                                          PACKET_data(&appcookie),  | 
854  | 0  |                                          PACKET_remaining(&appcookie)) == 0) { | 
855  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);  | 
856  | 0  |         return 0;  | 
857  | 0  |     }  | 
858  |  |  | 
859  |  |     /*  | 
860  |  |      * Reconstruct the HRR that we would have sent in response to the original  | 
861  |  |      * ClientHello so we can add it to the transcript hash.  | 
862  |  |      * Note: This won't work with custom HRR extensions  | 
863  |  |      */  | 
864  | 0  |     if (!WPACKET_init_static_len(&hrrpkt, hrr, sizeof(hrr), 0)) { | 
865  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
866  | 0  |         return 0;  | 
867  | 0  |     }  | 
868  | 0  |     if (!WPACKET_put_bytes_u8(&hrrpkt, SSL3_MT_SERVER_HELLO)  | 
869  | 0  |             || !WPACKET_start_sub_packet_u24(&hrrpkt)  | 
870  | 0  |             || !WPACKET_put_bytes_u16(&hrrpkt, TLS1_2_VERSION)  | 
871  | 0  |             || !WPACKET_memcpy(&hrrpkt, hrrrandom, SSL3_RANDOM_SIZE)  | 
872  | 0  |             || !WPACKET_sub_memcpy_u8(&hrrpkt, s->tmp_session_id,  | 
873  | 0  |                                       s->tmp_session_id_len)  | 
874  | 0  |             || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, &hrrpkt,  | 
875  | 0  |                                                 &ciphlen)  | 
876  | 0  |             || !WPACKET_put_bytes_u8(&hrrpkt, 0)  | 
877  | 0  |             || !WPACKET_start_sub_packet_u16(&hrrpkt)) { | 
878  | 0  |         WPACKET_cleanup(&hrrpkt);  | 
879  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
880  | 0  |         return 0;  | 
881  | 0  |     }  | 
882  | 0  |     if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_supported_versions)  | 
883  | 0  |             || !WPACKET_start_sub_packet_u16(&hrrpkt)  | 
884  | 0  |             || !WPACKET_put_bytes_u16(&hrrpkt, s->version)  | 
885  | 0  |             || !WPACKET_close(&hrrpkt)) { | 
886  | 0  |         WPACKET_cleanup(&hrrpkt);  | 
887  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
888  | 0  |         return 0;  | 
889  | 0  |     }  | 
890  | 0  |     if (key_share) { | 
891  | 0  |         if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_key_share)  | 
892  | 0  |                 || !WPACKET_start_sub_packet_u16(&hrrpkt)  | 
893  | 0  |                 || !WPACKET_put_bytes_u16(&hrrpkt, s->s3.group_id)  | 
894  | 0  |                 || !WPACKET_close(&hrrpkt)) { | 
895  | 0  |             WPACKET_cleanup(&hrrpkt);  | 
896  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
897  | 0  |             return 0;  | 
898  | 0  |         }  | 
899  | 0  |     }  | 
900  | 0  |     if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_cookie)  | 
901  | 0  |             || !WPACKET_start_sub_packet_u16(&hrrpkt)  | 
902  | 0  |             || !WPACKET_sub_memcpy_u16(&hrrpkt, data, rawlen)  | 
903  | 0  |             || !WPACKET_close(&hrrpkt) /* cookie extension */  | 
904  | 0  |             || !WPACKET_close(&hrrpkt) /* extension block */  | 
905  | 0  |             || !WPACKET_close(&hrrpkt) /* message */  | 
906  | 0  |             || !WPACKET_get_total_written(&hrrpkt, &hrrlen)  | 
907  | 0  |             || !WPACKET_finish(&hrrpkt)) { | 
908  | 0  |         WPACKET_cleanup(&hrrpkt);  | 
909  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
910  | 0  |         return 0;  | 
911  | 0  |     }  | 
912  |  |  | 
913  |  |     /* Reconstruct the transcript hash */  | 
914  | 0  |     if (!create_synthetic_message_hash(s, PACKET_data(&chhash),  | 
915  | 0  |                                        PACKET_remaining(&chhash), hrr,  | 
916  | 0  |                                        hrrlen)) { | 
917  |  |         /* SSLfatal() already called */  | 
918  | 0  |         return 0;  | 
919  | 0  |     }  | 
920  |  |  | 
921  |  |     /* Act as if this ClientHello came after a HelloRetryRequest */  | 
922  | 0  |     s->hello_retry_request = SSL_HRR_PENDING;  | 
923  |  | 
  | 
924  | 0  |     s->ext.cookieok = 1;  | 
925  | 0  | #endif  | 
926  |  | 
  | 
927  | 0  |     return 1;  | 
928  | 0  | }  | 
929  |  |  | 
930  |  | int tls_parse_ctos_supported_groups(SSL_CONNECTION *s, PACKET *pkt,  | 
931  |  |                                     unsigned int context,  | 
932  |  |                                     X509 *x, size_t chainidx)  | 
933  | 9.00k  | { | 
934  | 9.00k  |     PACKET supported_groups_list;  | 
935  |  |  | 
936  |  |     /* Each group is 2 bytes and we must have at least 1. */  | 
937  | 9.00k  |     if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)  | 
938  | 9.00k  |             || PACKET_remaining(&supported_groups_list) == 0  | 
939  | 9.00k  |             || (PACKET_remaining(&supported_groups_list) % 2) != 0) { | 
940  | 134  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
941  | 134  |         return 0;  | 
942  | 134  |     }  | 
943  |  |  | 
944  | 8.87k  |     if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { | 
945  | 8.81k  |         OPENSSL_free(s->ext.peer_supportedgroups);  | 
946  | 8.81k  |         s->ext.peer_supportedgroups = NULL;  | 
947  | 8.81k  |         s->ext.peer_supportedgroups_len = 0;  | 
948  | 8.81k  |         if (!tls1_save_u16(&supported_groups_list,  | 
949  | 8.81k  |                            &s->ext.peer_supportedgroups,  | 
950  | 8.81k  |                            &s->ext.peer_supportedgroups_len)) { | 
951  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
952  | 0  |             return 0;  | 
953  | 0  |         }  | 
954  | 8.81k  |     }  | 
955  |  |  | 
956  | 8.87k  |     return 1;  | 
957  | 8.87k  | }  | 
958  |  |  | 
959  |  | int tls_parse_ctos_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
960  |  |                        X509 *x, size_t chainidx)  | 
961  | 1.53k  | { | 
962  |  |     /* The extension must always be empty */  | 
963  | 1.53k  |     if (PACKET_remaining(pkt) != 0) { | 
964  | 6  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
965  | 6  |         return 0;  | 
966  | 6  |     }  | 
967  |  |  | 
968  | 1.53k  |     if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)  | 
969  | 0  |         return 1;  | 
970  |  |  | 
971  | 1.53k  |     s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;  | 
972  |  |  | 
973  | 1.53k  |     return 1;  | 
974  | 1.53k  | }  | 
975  |  |  | 
976  |  |  | 
977  |  | int tls_parse_ctos_early_data(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
978  |  |                               X509 *x, size_t chainidx)  | 
979  | 1.62k  | { | 
980  | 1.62k  |     if (PACKET_remaining(pkt) != 0) { | 
981  | 6  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
982  | 6  |         return 0;  | 
983  | 6  |     }  | 
984  |  |  | 
985  | 1.62k  |     if (s->hello_retry_request != SSL_HRR_NONE) { | 
986  | 5  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);  | 
987  | 5  |         return 0;  | 
988  | 5  |     }  | 
989  |  |  | 
990  | 1.61k  |     return 1;  | 
991  | 1.62k  | }  | 
992  |  |  | 
993  |  | static SSL_TICKET_STATUS tls_get_stateful_ticket(SSL_CONNECTION *s, PACKET *tick,  | 
994  |  |                                                  SSL_SESSION **sess)  | 
995  | 0  | { | 
996  | 0  |     SSL_SESSION *tmpsess = NULL;  | 
997  |  | 
  | 
998  | 0  |     s->ext.ticket_expected = 1;  | 
999  |  | 
  | 
1000  | 0  |     switch (PACKET_remaining(tick)) { | 
1001  | 0  |         case 0:  | 
1002  | 0  |             return SSL_TICKET_EMPTY;  | 
1003  |  |  | 
1004  | 0  |         case SSL_MAX_SSL_SESSION_ID_LENGTH:  | 
1005  | 0  |             break;  | 
1006  |  |  | 
1007  | 0  |         default:  | 
1008  | 0  |             return SSL_TICKET_NO_DECRYPT;  | 
1009  | 0  |     }  | 
1010  |  |  | 
1011  | 0  |     tmpsess = lookup_sess_in_cache(s, PACKET_data(tick),  | 
1012  | 0  |                                    SSL_MAX_SSL_SESSION_ID_LENGTH);  | 
1013  |  | 
  | 
1014  | 0  |     if (tmpsess == NULL)  | 
1015  | 0  |         return SSL_TICKET_NO_DECRYPT;  | 
1016  |  |  | 
1017  | 0  |     *sess = tmpsess;  | 
1018  | 0  |     return SSL_TICKET_SUCCESS;  | 
1019  | 0  | }  | 
1020  |  |  | 
1021  |  | int tls_parse_ctos_psk(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
1022  |  |                        X509 *x, size_t chainidx)  | 
1023  | 151  | { | 
1024  | 151  |     PACKET identities, binders, binder;  | 
1025  | 151  |     size_t binderoffset, hashsize;  | 
1026  | 151  |     SSL_SESSION *sess = NULL;  | 
1027  | 151  |     unsigned int id, i, ext = 0;  | 
1028  | 151  |     const EVP_MD *md = NULL;  | 
1029  | 151  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
1030  | 151  |     SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);  | 
1031  |  |  | 
1032  |  |     /*  | 
1033  |  |      * If we have no PSK kex mode that we recognise then we can't resume so  | 
1034  |  |      * ignore this extension  | 
1035  |  |      */  | 
1036  | 151  |     if ((s->ext.psk_kex_mode  | 
1037  | 151  |             & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE)) == 0)  | 
1038  | 10  |         return 1;  | 
1039  |  |  | 
1040  | 141  |     if (!PACKET_get_length_prefixed_2(pkt, &identities)) { | 
1041  | 2  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1042  | 2  |         return 0;  | 
1043  | 2  |     }  | 
1044  |  |  | 
1045  | 139  |     s->ext.ticket_expected = 0;  | 
1046  | 276  |     for (id = 0; PACKET_remaining(&identities) != 0; id++) { | 
1047  | 249  |         PACKET identity;  | 
1048  | 249  |         unsigned long ticket_agel;  | 
1049  | 249  |         size_t idlen;  | 
1050  |  |  | 
1051  | 249  |         if (!PACKET_get_length_prefixed_2(&identities, &identity)  | 
1052  | 249  |                 || !PACKET_get_net_4(&identities, &ticket_agel)) { | 
1053  | 41  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1054  | 41  |             return 0;  | 
1055  | 41  |         }  | 
1056  |  |  | 
1057  | 208  |         idlen = PACKET_remaining(&identity);  | 
1058  | 208  |         if (s->psk_find_session_cb != NULL  | 
1059  | 208  |                 && !s->psk_find_session_cb(ussl, PACKET_data(&identity), idlen,  | 
1060  | 0  |                                            &sess)) { | 
1061  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION);  | 
1062  | 0  |             return 0;  | 
1063  | 0  |         }  | 
1064  |  |  | 
1065  | 208  | #ifndef OPENSSL_NO_PSK  | 
1066  | 208  |         if (sess == NULL  | 
1067  | 208  |                 && s->psk_server_callback != NULL  | 
1068  | 208  |                 && idlen <= PSK_MAX_IDENTITY_LEN) { | 
1069  | 0  |             char *pskid = NULL;  | 
1070  | 0  |             unsigned char pskdata[PSK_MAX_PSK_LEN];  | 
1071  | 0  |             unsigned int pskdatalen;  | 
1072  |  | 
  | 
1073  | 0  |             if (!PACKET_strndup(&identity, &pskid)) { | 
1074  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1075  | 0  |                 return 0;  | 
1076  | 0  |             }  | 
1077  | 0  |             pskdatalen = s->psk_server_callback(ussl, pskid, pskdata,  | 
1078  | 0  |                                                 sizeof(pskdata));  | 
1079  | 0  |             OPENSSL_free(pskid);  | 
1080  | 0  |             if (pskdatalen > PSK_MAX_PSK_LEN) { | 
1081  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1082  | 0  |                 return 0;  | 
1083  | 0  |             } else if (pskdatalen > 0) { | 
1084  | 0  |                 const SSL_CIPHER *cipher;  | 
1085  | 0  |                 const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 }; | 
1086  |  |  | 
1087  |  |                 /*  | 
1088  |  |                  * We found a PSK using an old style callback. We don't know  | 
1089  |  |                  * the digest so we default to SHA256 as per the TLSv1.3 spec  | 
1090  |  |                  */  | 
1091  | 0  |                 cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s),  | 
1092  | 0  |                                          tls13_aes128gcmsha256_id);  | 
1093  | 0  |                 if (cipher == NULL) { | 
1094  | 0  |                     OPENSSL_cleanse(pskdata, pskdatalen);  | 
1095  | 0  |                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1096  | 0  |                     return 0;  | 
1097  | 0  |                 }  | 
1098  |  |  | 
1099  | 0  |                 sess = SSL_SESSION_new();  | 
1100  | 0  |                 if (sess == NULL  | 
1101  | 0  |                         || !SSL_SESSION_set1_master_key(sess, pskdata,  | 
1102  | 0  |                                                         pskdatalen)  | 
1103  | 0  |                         || !SSL_SESSION_set_cipher(sess, cipher)  | 
1104  | 0  |                         || !SSL_SESSION_set_protocol_version(sess,  | 
1105  | 0  |                                                              TLS1_3_VERSION)) { | 
1106  | 0  |                     OPENSSL_cleanse(pskdata, pskdatalen);  | 
1107  | 0  |                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1108  | 0  |                     goto err;  | 
1109  | 0  |                 }  | 
1110  | 0  |                 OPENSSL_cleanse(pskdata, pskdatalen);  | 
1111  | 0  |             }  | 
1112  | 0  |         }  | 
1113  | 208  | #endif /* OPENSSL_NO_PSK */  | 
1114  |  |  | 
1115  | 208  |         if (sess != NULL) { | 
1116  |  |             /* We found a PSK */  | 
1117  | 0  |             SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);  | 
1118  |  | 
  | 
1119  | 0  |             if (sesstmp == NULL) { | 
1120  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1121  | 0  |                 goto err;  | 
1122  | 0  |             }  | 
1123  | 0  |             SSL_SESSION_free(sess);  | 
1124  | 0  |             sess = sesstmp;  | 
1125  |  |  | 
1126  |  |             /*  | 
1127  |  |              * We've just been told to use this session for this context so  | 
1128  |  |              * make sure the sid_ctx matches up.  | 
1129  |  |              */  | 
1130  | 0  |             memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);  | 
1131  | 0  |             sess->sid_ctx_length = s->sid_ctx_length;  | 
1132  | 0  |             ext = 1;  | 
1133  | 0  |             if (id == 0)  | 
1134  | 0  |                 s->ext.early_data_ok = 1;  | 
1135  | 0  |             s->ext.ticket_expected = 1;  | 
1136  | 208  |         } else { | 
1137  | 208  |             OSSL_TIME t, age, expire;  | 
1138  | 208  |             int ret;  | 
1139  |  |  | 
1140  |  |             /*  | 
1141  |  |              * If we are using anti-replay protection then we behave as if  | 
1142  |  |              * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there  | 
1143  |  |              * is no point in using full stateless tickets.  | 
1144  |  |              */  | 
1145  | 208  |             if ((s->options & SSL_OP_NO_TICKET) != 0  | 
1146  | 208  |                     || (s->max_early_data > 0  | 
1147  | 208  |                         && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))  | 
1148  | 0  |                 ret = tls_get_stateful_ticket(s, &identity, &sess);  | 
1149  | 208  |             else  | 
1150  | 208  |                 ret = tls_decrypt_ticket(s, PACKET_data(&identity),  | 
1151  | 208  |                                          PACKET_remaining(&identity), NULL, 0,  | 
1152  | 208  |                                          &sess);  | 
1153  |  |  | 
1154  | 208  |             if (ret == SSL_TICKET_EMPTY) { | 
1155  | 4  |                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1156  | 4  |                 return 0;  | 
1157  | 4  |             }  | 
1158  |  |  | 
1159  | 204  |             if (ret == SSL_TICKET_FATAL_ERR_MALLOC  | 
1160  | 204  |                     || ret == SSL_TICKET_FATAL_ERR_OTHER) { | 
1161  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1162  | 0  |                 return 0;  | 
1163  | 0  |             }  | 
1164  | 204  |             if (ret == SSL_TICKET_NONE || ret == SSL_TICKET_NO_DECRYPT)  | 
1165  | 109  |                 continue;  | 
1166  |  |  | 
1167  |  |             /* Check for replay */  | 
1168  | 95  |             if (s->max_early_data > 0  | 
1169  | 95  |                     && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0  | 
1170  | 95  |                     && !SSL_CTX_remove_session(s->session_ctx, sess)) { | 
1171  | 0  |                 SSL_SESSION_free(sess);  | 
1172  | 0  |                 sess = NULL;  | 
1173  | 0  |                 continue;  | 
1174  | 0  |             }  | 
1175  |  |  | 
1176  | 95  |             age = ossl_time_subtract(ossl_ms2time(ticket_agel),  | 
1177  | 95  |                                      ossl_ms2time(sess->ext.tick_age_add));  | 
1178  | 95  |             t = ossl_time_subtract(ossl_time_now(), sess->time);  | 
1179  |  |  | 
1180  |  |             /*  | 
1181  |  |              * Although internally we use OSS_TIME which has ns granularity,  | 
1182  |  |              * when SSL_SESSION structures are serialised/deserialised we use  | 
1183  |  |              * second granularity for the sess->time field. Therefore it could  | 
1184  |  |              * appear that the client's ticket age is longer than ours (our  | 
1185  |  |              * ticket age calculation should always be slightly longer than the  | 
1186  |  |              * client's due to the network latency). Therefore we add 1000ms to  | 
1187  |  |              * our age calculation to adjust for rounding errors.  | 
1188  |  |              */  | 
1189  | 95  |             expire = ossl_time_add(t, ossl_ms2time(1000));  | 
1190  |  |  | 
1191  | 95  |             if (id == 0  | 
1192  | 95  |                     && ossl_time_compare(sess->timeout, t) >= 0  | 
1193  | 95  |                     && ossl_time_compare(age, expire) <= 0  | 
1194  | 95  |                     && ossl_time_compare(ossl_time_add(age, TICKET_AGE_ALLOWANCE),  | 
1195  | 56  |                                          expire) >= 0) { | 
1196  |  |                 /*  | 
1197  |  |                  * Ticket age is within tolerance and not expired. We allow it  | 
1198  |  |                  * for early data  | 
1199  |  |                  */  | 
1200  | 31  |                 s->ext.early_data_ok = 1;  | 
1201  | 31  |             }  | 
1202  | 95  |         }  | 
1203  |  |  | 
1204  | 95  |         md = ssl_md(sctx, sess->cipher->algorithm2);  | 
1205  | 95  |         if (md == NULL) { | 
1206  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1207  | 0  |             goto err;  | 
1208  | 0  |         }  | 
1209  | 95  |         if (!EVP_MD_is_a(md,  | 
1210  | 95  |                 EVP_MD_get0_name(ssl_md(sctx,  | 
1211  | 95  |                                         s->s3.tmp.new_cipher->algorithm2)))) { | 
1212  |  |             /* The ciphersuite is not compatible with this session. */  | 
1213  | 28  |             SSL_SESSION_free(sess);  | 
1214  | 28  |             sess = NULL;  | 
1215  | 28  |             s->ext.early_data_ok = 0;  | 
1216  | 28  |             s->ext.ticket_expected = 0;  | 
1217  | 28  |             continue;  | 
1218  | 28  |         }  | 
1219  | 67  |         break;  | 
1220  | 95  |     }  | 
1221  |  |  | 
1222  | 94  |     if (sess == NULL)  | 
1223  | 27  |         return 1;  | 
1224  |  |  | 
1225  | 67  |     binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data;  | 
1226  | 67  |     hashsize = EVP_MD_get_size(md);  | 
1227  |  |  | 
1228  | 67  |     if (!PACKET_get_length_prefixed_2(pkt, &binders)) { | 
1229  | 31  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1230  | 31  |         goto err;  | 
1231  | 31  |     }  | 
1232  |  |  | 
1233  | 52  |     for (i = 0; i <= id; i++) { | 
1234  | 36  |         if (!PACKET_get_length_prefixed_1(&binders, &binder)) { | 
1235  | 20  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1236  | 20  |             goto err;  | 
1237  | 20  |         }  | 
1238  | 36  |     }  | 
1239  |  |  | 
1240  | 16  |     if (PACKET_remaining(&binder) != hashsize) { | 
1241  | 5  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1242  | 5  |         goto err;  | 
1243  | 5  |     }  | 
1244  | 11  |     if (tls_psk_do_binder(s, md, (const unsigned char *)s->init_buf->data,  | 
1245  | 11  |                           binderoffset, PACKET_data(&binder), NULL, sess, 0,  | 
1246  | 11  |                           ext) != 1) { | 
1247  |  |         /* SSLfatal() already called */  | 
1248  | 9  |         goto err;  | 
1249  | 9  |     }  | 
1250  |  |  | 
1251  | 2  |     s->ext.tick_identity = id;  | 
1252  |  |  | 
1253  | 2  |     SSL_SESSION_free(s->session);  | 
1254  | 2  |     s->session = sess;  | 
1255  | 2  |     return 1;  | 
1256  | 65  | err:  | 
1257  | 65  |     SSL_SESSION_free(sess);  | 
1258  | 65  |     return 0;  | 
1259  | 11  | }  | 
1260  |  |  | 
1261  |  | int tls_parse_ctos_post_handshake_auth(SSL_CONNECTION *s, PACKET *pkt,  | 
1262  |  |                                        ossl_unused unsigned int context,  | 
1263  |  |                                        ossl_unused X509 *x,  | 
1264  |  |                                        ossl_unused size_t chainidx)  | 
1265  | 112  | { | 
1266  | 112  |     if (PACKET_remaining(pkt) != 0) { | 
1267  | 8  |         SSLfatal(s, SSL_AD_DECODE_ERROR,  | 
1268  | 8  |                  SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);  | 
1269  | 8  |         return 0;  | 
1270  | 8  |     }  | 
1271  |  |  | 
1272  | 104  |     s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;  | 
1273  |  |  | 
1274  | 104  |     return 1;  | 
1275  | 112  | }  | 
1276  |  |  | 
1277  |  | /*  | 
1278  |  |  * Add the server's renegotiation binding  | 
1279  |  |  */  | 
1280  |  | EXT_RETURN tls_construct_stoc_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,  | 
1281  |  |                                           unsigned int context, X509 *x,  | 
1282  |  |                                           size_t chainidx)  | 
1283  | 12.1k  | { | 
1284  | 12.1k  |     if (!s->s3.send_connection_binding)  | 
1285  | 10.2k  |         return EXT_RETURN_NOT_SENT;  | 
1286  |  |  | 
1287  |  |     /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */  | 
1288  | 1.90k  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)  | 
1289  | 1.90k  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1290  | 1.90k  |             || !WPACKET_start_sub_packet_u8(pkt)  | 
1291  | 1.90k  |             || !WPACKET_memcpy(pkt, s->s3.previous_client_finished,  | 
1292  | 1.90k  |                                s->s3.previous_client_finished_len)  | 
1293  | 1.90k  |             || !WPACKET_memcpy(pkt, s->s3.previous_server_finished,  | 
1294  | 1.90k  |                                s->s3.previous_server_finished_len)  | 
1295  | 1.90k  |             || !WPACKET_close(pkt)  | 
1296  | 1.90k  |             || !WPACKET_close(pkt)) { | 
1297  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1298  | 0  |         return EXT_RETURN_FAIL;  | 
1299  | 0  |     }  | 
1300  |  |  | 
1301  | 1.90k  |     return EXT_RETURN_SENT;  | 
1302  | 1.90k  | }  | 
1303  |  |  | 
1304  |  | EXT_RETURN tls_construct_stoc_server_name(SSL_CONNECTION *s, WPACKET *pkt,  | 
1305  |  |                                           unsigned int context, X509 *x,  | 
1306  |  |                                           size_t chainidx)  | 
1307  | 11.2k  | { | 
1308  | 11.2k  |     if (s->servername_done != 1)  | 
1309  | 11.2k  |         return EXT_RETURN_NOT_SENT;  | 
1310  |  |  | 
1311  |  |     /*  | 
1312  |  |      * Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.  | 
1313  |  |      * We just use the servername from the initial handshake.  | 
1314  |  |      */  | 
1315  | 0  |     if (s->hit && !SSL_CONNECTION_IS_TLS13(s))  | 
1316  | 0  |         return EXT_RETURN_NOT_SENT;  | 
1317  |  |  | 
1318  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)  | 
1319  | 0  |             || !WPACKET_put_bytes_u16(pkt, 0)) { | 
1320  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1321  | 0  |         return EXT_RETURN_FAIL;  | 
1322  | 0  |     }  | 
1323  |  |  | 
1324  | 0  |     return EXT_RETURN_SENT;  | 
1325  | 0  | }  | 
1326  |  |  | 
1327  |  | /* Add/include the server's max fragment len extension into ServerHello */  | 
1328  |  | EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,  | 
1329  |  |                                              unsigned int context, X509 *x,  | 
1330  |  |                                              size_t chainidx)  | 
1331  | 11.2k  | { | 
1332  | 11.2k  |     if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))  | 
1333  | 10.8k  |         return EXT_RETURN_NOT_SENT;  | 
1334  |  |  | 
1335  |  |     /*-  | 
1336  |  |      * 4 bytes for this extension type and extension length  | 
1337  |  |      * 1 byte for the Max Fragment Length code value.  | 
1338  |  |      */  | 
1339  | 382  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)  | 
1340  | 382  |         || !WPACKET_start_sub_packet_u16(pkt)  | 
1341  | 382  |         || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)  | 
1342  | 382  |         || !WPACKET_close(pkt)) { | 
1343  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1344  | 0  |         return EXT_RETURN_FAIL;  | 
1345  | 0  |     }  | 
1346  |  |  | 
1347  | 382  |     return EXT_RETURN_SENT;  | 
1348  | 382  | }  | 
1349  |  |  | 
1350  |  | EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,  | 
1351  |  |                                             unsigned int context, X509 *x,  | 
1352  |  |                                             size_t chainidx)  | 
1353  | 9.30k  | { | 
1354  | 9.30k  |     unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;  | 
1355  | 9.30k  |     unsigned long alg_a = s->s3.tmp.new_cipher->algorithm_auth;  | 
1356  | 9.30k  |     int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))  | 
1357  | 9.30k  |                     && (s->ext.peer_ecpointformats != NULL);  | 
1358  | 9.30k  |     const unsigned char *plist;  | 
1359  | 9.30k  |     size_t plistlen;  | 
1360  |  |  | 
1361  | 9.30k  |     if (!using_ecc)  | 
1362  | 7.90k  |         return EXT_RETURN_NOT_SENT;  | 
1363  |  |  | 
1364  | 1.40k  |     tls1_get_formatlist(s, &plist, &plistlen);  | 
1365  | 1.40k  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)  | 
1366  | 1.40k  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1367  | 1.40k  |             || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)  | 
1368  | 1.40k  |             || !WPACKET_close(pkt)) { | 
1369  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1370  | 0  |         return EXT_RETURN_FAIL;  | 
1371  | 0  |     }  | 
1372  |  |  | 
1373  | 1.40k  |     return EXT_RETURN_SENT;  | 
1374  | 1.40k  | }  | 
1375  |  |  | 
1376  |  | EXT_RETURN tls_construct_stoc_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,  | 
1377  |  |                                                unsigned int context, X509 *x,  | 
1378  |  |                                                size_t chainidx)  | 
1379  | 11.2k  | { | 
1380  | 11.2k  |     const uint16_t *groups;  | 
1381  | 11.2k  |     size_t numgroups, i, first = 1;  | 
1382  | 11.2k  |     int version;  | 
1383  |  |  | 
1384  |  |     /* s->s3.group_id is non zero if we accepted a key_share */  | 
1385  | 11.2k  |     if (s->s3.group_id == 0)  | 
1386  | 9.30k  |         return EXT_RETURN_NOT_SENT;  | 
1387  |  |  | 
1388  |  |     /* Get our list of supported groups */  | 
1389  | 1.96k  |     tls1_get_supported_groups(s, &groups, &numgroups);  | 
1390  | 1.96k  |     if (numgroups == 0) { | 
1391  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1392  | 0  |         return EXT_RETURN_FAIL;  | 
1393  | 0  |     }  | 
1394  |  |  | 
1395  |  |     /* Copy group ID if supported */  | 
1396  | 1.96k  |     version = SSL_version(SSL_CONNECTION_GET_SSL(s));  | 
1397  | 7.10k  |     for (i = 0; i < numgroups; i++) { | 
1398  | 6.46k  |         uint16_t group = groups[i];  | 
1399  |  |  | 
1400  | 6.46k  |         if (tls_valid_group(s, group, version, version, 0, NULL)  | 
1401  | 6.46k  |                 && tls_group_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) { | 
1402  | 6.46k  |             if (first) { | 
1403  |  |                 /*  | 
1404  |  |                  * Check if the client is already using our preferred group. If  | 
1405  |  |                  * so we don't need to add this extension  | 
1406  |  |                  */  | 
1407  | 1.96k  |                 if (s->s3.group_id == group)  | 
1408  | 1.33k  |                     return EXT_RETURN_NOT_SENT;  | 
1409  |  |  | 
1410  |  |                 /* Add extension header */  | 
1411  | 633  |                 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)  | 
1412  |  |                            /* Sub-packet for supported_groups extension */  | 
1413  | 633  |                         || !WPACKET_start_sub_packet_u16(pkt)  | 
1414  | 633  |                         || !WPACKET_start_sub_packet_u16(pkt)) { | 
1415  | 0  |                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1416  | 0  |                     return EXT_RETURN_FAIL;  | 
1417  | 0  |                 }  | 
1418  |  |  | 
1419  | 633  |                 first = 0;  | 
1420  | 633  |             }  | 
1421  | 5.13k  |             if (!WPACKET_put_bytes_u16(pkt, group)) { | 
1422  | 0  |                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1423  | 0  |                     return EXT_RETURN_FAIL;  | 
1424  | 0  |                 }  | 
1425  | 5.13k  |         }  | 
1426  | 6.46k  |     }  | 
1427  |  |  | 
1428  | 633  |     if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | 
1429  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1430  | 0  |         return EXT_RETURN_FAIL;  | 
1431  | 0  |     }  | 
1432  |  |  | 
1433  | 633  |     return EXT_RETURN_SENT;  | 
1434  | 633  | }  | 
1435  |  |  | 
1436  |  | EXT_RETURN tls_construct_stoc_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,  | 
1437  |  |                                              unsigned int context, X509 *x,  | 
1438  |  |                                              size_t chainidx)  | 
1439  | 9.30k  | { | 
1440  | 9.30k  |     if (!s->ext.ticket_expected || !tls_use_ticket(s)) { | 
1441  | 8.28k  |         s->ext.ticket_expected = 0;  | 
1442  | 8.28k  |         return EXT_RETURN_NOT_SENT;  | 
1443  | 8.28k  |     }  | 
1444  |  |  | 
1445  | 1.02k  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)  | 
1446  | 1.02k  |             || !WPACKET_put_bytes_u16(pkt, 0)) { | 
1447  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1448  | 0  |         return EXT_RETURN_FAIL;  | 
1449  | 0  |     }  | 
1450  |  |  | 
1451  | 1.02k  |     return EXT_RETURN_SENT;  | 
1452  | 1.02k  | }  | 
1453  |  |  | 
1454  |  | #ifndef OPENSSL_NO_OCSP  | 
1455  |  | EXT_RETURN tls_construct_stoc_status_request(SSL_CONNECTION *s, WPACKET *pkt,  | 
1456  |  |                                              unsigned int context, X509 *x,  | 
1457  |  |                                              size_t chainidx)  | 
1458  | 11.2k  | { | 
1459  |  |     /* We don't currently support this extension inside a CertificateRequest */  | 
1460  | 11.2k  |     if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)  | 
1461  | 0  |         return EXT_RETURN_NOT_SENT;  | 
1462  |  |  | 
1463  | 11.2k  |     if (!s->ext.status_expected)  | 
1464  | 11.2k  |         return EXT_RETURN_NOT_SENT;  | 
1465  |  |  | 
1466  | 0  |     if (SSL_CONNECTION_IS_TLS13(s) && chainidx != 0)  | 
1467  | 0  |         return EXT_RETURN_NOT_SENT;  | 
1468  |  |  | 
1469  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)  | 
1470  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)) { | 
1471  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1472  | 0  |         return EXT_RETURN_FAIL;  | 
1473  | 0  |     }  | 
1474  |  |  | 
1475  |  |     /*  | 
1476  |  |      * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we  | 
1477  |  |      * send back an empty extension, with the certificate status appearing as a  | 
1478  |  |      * separate message  | 
1479  |  |      */  | 
1480  | 0  |     if (SSL_CONNECTION_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt)) { | 
1481  |  |        /* SSLfatal() already called */  | 
1482  | 0  |        return EXT_RETURN_FAIL;  | 
1483  | 0  |     }  | 
1484  | 0  |     if (!WPACKET_close(pkt)) { | 
1485  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1486  | 0  |         return EXT_RETURN_FAIL;  | 
1487  | 0  |     }  | 
1488  |  |  | 
1489  | 0  |     return EXT_RETURN_SENT;  | 
1490  | 0  | }  | 
1491  |  | #endif  | 
1492  |  |  | 
1493  |  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
1494  |  | EXT_RETURN tls_construct_stoc_next_proto_neg(SSL_CONNECTION *s, WPACKET *pkt,  | 
1495  |  |                                              unsigned int context, X509 *x,  | 
1496  |  |                                              size_t chainidx)  | 
1497  | 9.30k  | { | 
1498  | 9.30k  |     const unsigned char *npa;  | 
1499  | 9.30k  |     unsigned int npalen;  | 
1500  | 9.30k  |     int ret;  | 
1501  | 9.30k  |     int npn_seen = s->s3.npn_seen;  | 
1502  | 9.30k  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
1503  |  |  | 
1504  | 9.30k  |     s->s3.npn_seen = 0;  | 
1505  | 9.30k  |     if (!npn_seen || sctx->ext.npn_advertised_cb == NULL)  | 
1506  | 9.30k  |         return EXT_RETURN_NOT_SENT;  | 
1507  |  |  | 
1508  | 0  |     ret = sctx->ext.npn_advertised_cb(SSL_CONNECTION_GET_USER_SSL(s), &npa,  | 
1509  | 0  |                                       &npalen, sctx->ext.npn_advertised_cb_arg);  | 
1510  | 0  |     if (ret == SSL_TLSEXT_ERR_OK) { | 
1511  | 0  |         if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)  | 
1512  | 0  |                 || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) { | 
1513  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1514  | 0  |             return EXT_RETURN_FAIL;  | 
1515  | 0  |         }  | 
1516  | 0  |         s->s3.npn_seen = 1;  | 
1517  | 0  |         return EXT_RETURN_SENT;  | 
1518  | 0  |     }  | 
1519  |  |  | 
1520  | 0  |     return EXT_RETURN_NOT_SENT;  | 
1521  | 0  | }  | 
1522  |  | #endif  | 
1523  |  |  | 
1524  |  | EXT_RETURN tls_construct_stoc_alpn(SSL_CONNECTION *s, WPACKET *pkt, unsigned int context,  | 
1525  |  |                                    X509 *x, size_t chainidx)  | 
1526  | 11.2k  | { | 
1527  | 11.2k  |     if (s->s3.alpn_selected == NULL)  | 
1528  | 11.2k  |         return EXT_RETURN_NOT_SENT;  | 
1529  |  |  | 
1530  | 0  |     if (!WPACKET_put_bytes_u16(pkt,  | 
1531  | 0  |                 TLSEXT_TYPE_application_layer_protocol_negotiation)  | 
1532  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1533  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1534  | 0  |             || !WPACKET_sub_memcpy_u8(pkt, s->s3.alpn_selected,  | 
1535  | 0  |                                       s->s3.alpn_selected_len)  | 
1536  | 0  |             || !WPACKET_close(pkt)  | 
1537  | 0  |             || !WPACKET_close(pkt)) { | 
1538  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1539  | 0  |         return EXT_RETURN_FAIL;  | 
1540  | 0  |     }  | 
1541  |  |  | 
1542  | 0  |     return EXT_RETURN_SENT;  | 
1543  | 0  | }  | 
1544  |  |  | 
1545  |  | #ifndef OPENSSL_NO_SRTP  | 
1546  |  | EXT_RETURN tls_construct_stoc_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,  | 
1547  |  |                                        unsigned int context, X509 *x,  | 
1548  |  |                                        size_t chainidx)  | 
1549  | 11.2k  | { | 
1550  | 11.2k  |     if (s->srtp_profile == NULL)  | 
1551  | 11.2k  |         return EXT_RETURN_NOT_SENT;  | 
1552  |  |  | 
1553  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)  | 
1554  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1555  | 0  |             || !WPACKET_put_bytes_u16(pkt, 2)  | 
1556  | 0  |             || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)  | 
1557  | 0  |             || !WPACKET_put_bytes_u8(pkt, 0)  | 
1558  | 0  |             || !WPACKET_close(pkt)) { | 
1559  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1560  | 0  |         return EXT_RETURN_FAIL;  | 
1561  | 0  |     }  | 
1562  |  |  | 
1563  | 0  |     return EXT_RETURN_SENT;  | 
1564  | 0  | }  | 
1565  |  | #endif  | 
1566  |  |  | 
1567  |  | EXT_RETURN tls_construct_stoc_etm(SSL_CONNECTION *s, WPACKET *pkt,  | 
1568  |  |                                   unsigned int context,  | 
1569  |  |                                   X509 *x, size_t chainidx)  | 
1570  | 9.30k  | { | 
1571  | 9.30k  |     if (!s->ext.use_etm)  | 
1572  | 8.79k  |         return EXT_RETURN_NOT_SENT;  | 
1573  |  |  | 
1574  |  |     /*  | 
1575  |  |      * Don't use encrypt_then_mac if AEAD or RC4 might want to disable  | 
1576  |  |      * for other cases too.  | 
1577  |  |      */  | 
1578  | 514  |     if (s->s3.tmp.new_cipher->algorithm_mac == SSL_AEAD  | 
1579  | 514  |         || s->s3.tmp.new_cipher->algorithm_enc == SSL_RC4  | 
1580  | 514  |         || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT  | 
1581  | 514  |         || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12  | 
1582  | 514  |         || s->s3.tmp.new_cipher->algorithm_enc == SSL_MAGMA  | 
1583  | 514  |         || s->s3.tmp.new_cipher->algorithm_enc == SSL_KUZNYECHIK) { | 
1584  | 93  |         s->ext.use_etm = 0;  | 
1585  | 93  |         return EXT_RETURN_NOT_SENT;  | 
1586  | 93  |     }  | 
1587  |  |  | 
1588  | 421  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)  | 
1589  | 421  |             || !WPACKET_put_bytes_u16(pkt, 0)) { | 
1590  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1591  | 0  |         return EXT_RETURN_FAIL;  | 
1592  | 0  |     }  | 
1593  |  |  | 
1594  | 421  |     return EXT_RETURN_SENT;  | 
1595  | 421  | }  | 
1596  |  |  | 
1597  |  | EXT_RETURN tls_construct_stoc_ems(SSL_CONNECTION *s, WPACKET *pkt,  | 
1598  |  |                                   unsigned int context,  | 
1599  |  |                                   X509 *x, size_t chainidx)  | 
1600  | 9.30k  | { | 
1601  | 9.30k  |     if ((s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)  | 
1602  | 7.97k  |         return EXT_RETURN_NOT_SENT;  | 
1603  |  |  | 
1604  | 1.33k  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)  | 
1605  | 1.33k  |             || !WPACKET_put_bytes_u16(pkt, 0)) { | 
1606  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1607  | 0  |         return EXT_RETURN_FAIL;  | 
1608  | 0  |     }  | 
1609  |  |  | 
1610  | 1.33k  |     return EXT_RETURN_SENT;  | 
1611  | 1.33k  | }  | 
1612  |  |  | 
1613  |  | EXT_RETURN tls_construct_stoc_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,  | 
1614  |  |                                                  unsigned int context, X509 *x,  | 
1615  |  |                                                  size_t chainidx)  | 
1616  | 2.48k  | { | 
1617  | 2.48k  |     if (!ossl_assert(SSL_CONNECTION_IS_TLS13(s))) { | 
1618  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1619  | 0  |         return EXT_RETURN_FAIL;  | 
1620  | 0  |     }  | 
1621  |  |  | 
1622  | 2.48k  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)  | 
1623  | 2.48k  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1624  | 2.48k  |             || !WPACKET_put_bytes_u16(pkt, s->version)  | 
1625  | 2.48k  |             || !WPACKET_close(pkt)) { | 
1626  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1627  | 0  |         return EXT_RETURN_FAIL;  | 
1628  | 0  |     }  | 
1629  |  |  | 
1630  | 2.48k  |     return EXT_RETURN_SENT;  | 
1631  | 2.48k  | }  | 
1632  |  |  | 
1633  |  | EXT_RETURN tls_construct_stoc_key_share(SSL_CONNECTION *s, WPACKET *pkt,  | 
1634  |  |                                         unsigned int context, X509 *x,  | 
1635  |  |                                         size_t chainidx)  | 
1636  | 2.48k  | { | 
1637  | 2.48k  | #ifndef OPENSSL_NO_TLS1_3  | 
1638  | 2.48k  |     unsigned char *encodedPoint;  | 
1639  | 2.48k  |     size_t encoded_pt_len = 0;  | 
1640  | 2.48k  |     EVP_PKEY *ckey = s->s3.peer_tmp, *skey = NULL;  | 
1641  | 2.48k  |     const TLS_GROUP_INFO *ginf = NULL;  | 
1642  |  |  | 
1643  | 2.48k  |     if (s->hello_retry_request == SSL_HRR_PENDING) { | 
1644  | 506  |         if (ckey != NULL) { | 
1645  |  |             /* Original key_share was acceptable so don't ask for another one */  | 
1646  | 0  |             return EXT_RETURN_NOT_SENT;  | 
1647  | 0  |         }  | 
1648  | 506  |         if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)  | 
1649  | 506  |                 || !WPACKET_start_sub_packet_u16(pkt)  | 
1650  | 506  |                 || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)  | 
1651  | 506  |                 || !WPACKET_close(pkt)) { | 
1652  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1653  | 0  |             return EXT_RETURN_FAIL;  | 
1654  | 0  |         }  | 
1655  |  |  | 
1656  | 506  |         return EXT_RETURN_SENT;  | 
1657  | 506  |     }  | 
1658  |  |  | 
1659  | 1.98k  |     if (ckey == NULL) { | 
1660  |  |         /* No key_share received from client - must be resuming */  | 
1661  | 0  |         if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) { | 
1662  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1663  | 0  |             return EXT_RETURN_FAIL;  | 
1664  | 0  |         }  | 
1665  | 0  |         return EXT_RETURN_NOT_SENT;  | 
1666  | 0  |     }  | 
1667  | 1.98k  |     if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) { | 
1668  |  |         /*  | 
1669  |  |          * PSK ('hit') and explicitly not doing DHE (if the client sent the | 
1670  |  |          * DHE option we always take it); don't send key share.  | 
1671  |  |          */  | 
1672  | 0  |         return EXT_RETURN_NOT_SENT;  | 
1673  | 0  |     }  | 
1674  |  |  | 
1675  | 1.98k  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)  | 
1676  | 1.98k  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1677  | 1.98k  |             || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)) { | 
1678  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1679  | 0  |         return EXT_RETURN_FAIL;  | 
1680  | 0  |     }  | 
1681  |  |  | 
1682  | 1.98k  |     if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),  | 
1683  | 1.98k  |                                      s->s3.group_id)) == NULL) { | 
1684  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1685  | 0  |         return EXT_RETURN_FAIL;  | 
1686  | 0  |     }  | 
1687  |  |  | 
1688  | 1.98k  |     if (!ginf->is_kem) { | 
1689  |  |         /* Regular KEX */  | 
1690  | 1.95k  |         skey = ssl_generate_pkey(s, ckey);  | 
1691  | 1.95k  |         if (skey == NULL) { | 
1692  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);  | 
1693  | 0  |             return EXT_RETURN_FAIL;  | 
1694  | 0  |         }  | 
1695  |  |  | 
1696  |  |         /* Generate encoding of server key */  | 
1697  | 1.95k  |         encoded_pt_len = EVP_PKEY_get1_encoded_public_key(skey, &encodedPoint);  | 
1698  | 1.95k  |         if (encoded_pt_len == 0) { | 
1699  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);  | 
1700  | 0  |             EVP_PKEY_free(skey);  | 
1701  | 0  |             return EXT_RETURN_FAIL;  | 
1702  | 0  |         }  | 
1703  |  |  | 
1704  | 1.95k  |         if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len)  | 
1705  | 1.95k  |                 || !WPACKET_close(pkt)) { | 
1706  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1707  | 0  |             EVP_PKEY_free(skey);  | 
1708  | 0  |             OPENSSL_free(encodedPoint);  | 
1709  | 0  |             return EXT_RETURN_FAIL;  | 
1710  | 0  |         }  | 
1711  | 1.95k  |         OPENSSL_free(encodedPoint);  | 
1712  |  |  | 
1713  |  |         /*  | 
1714  |  |          * This causes the crypto state to be updated based on the derived keys  | 
1715  |  |          */  | 
1716  | 1.95k  |         s->s3.tmp.pkey = skey;  | 
1717  | 1.95k  |         if (ssl_derive(s, skey, ckey, 1) == 0) { | 
1718  |  |             /* SSLfatal() already called */  | 
1719  | 15  |             return EXT_RETURN_FAIL;  | 
1720  | 15  |         }  | 
1721  | 1.95k  |     } else { | 
1722  |  |         /* KEM mode */  | 
1723  | 27  |         unsigned char *ct = NULL;  | 
1724  | 27  |         size_t ctlen = 0;  | 
1725  |  |  | 
1726  |  |         /*  | 
1727  |  |          * This does not update the crypto state.  | 
1728  |  |          *  | 
1729  |  |          * The generated pms is stored in `s->s3.tmp.pms` to be later used via  | 
1730  |  |          * ssl_gensecret().  | 
1731  |  |          */  | 
1732  | 27  |         if (ssl_encapsulate(s, ckey, &ct, &ctlen, 0) == 0) { | 
1733  |  |             /* SSLfatal() already called */  | 
1734  | 4  |             return EXT_RETURN_FAIL;  | 
1735  | 4  |         }  | 
1736  |  |  | 
1737  | 23  |         if (ctlen == 0) { | 
1738  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1739  | 0  |             OPENSSL_free(ct);  | 
1740  | 0  |             return EXT_RETURN_FAIL;  | 
1741  | 0  |         }  | 
1742  |  |  | 
1743  | 23  |         if (!WPACKET_sub_memcpy_u16(pkt, ct, ctlen)  | 
1744  | 23  |                 || !WPACKET_close(pkt)) { | 
1745  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1746  | 0  |             OPENSSL_free(ct);  | 
1747  | 0  |             return EXT_RETURN_FAIL;  | 
1748  | 0  |         }  | 
1749  | 23  |         OPENSSL_free(ct);  | 
1750  |  |  | 
1751  |  |         /*  | 
1752  |  |          * This causes the crypto state to be updated based on the generated pms  | 
1753  |  |          */  | 
1754  | 23  |         if (ssl_gensecret(s, s->s3.tmp.pms, s->s3.tmp.pmslen) == 0) { | 
1755  |  |             /* SSLfatal() already called */  | 
1756  | 0  |             return EXT_RETURN_FAIL;  | 
1757  | 0  |         }  | 
1758  | 23  |     }  | 
1759  | 1.96k  |     s->s3.did_kex = 1;  | 
1760  | 1.96k  |     return EXT_RETURN_SENT;  | 
1761  |  | #else  | 
1762  |  |     return EXT_RETURN_FAIL;  | 
1763  |  | #endif  | 
1764  | 1.98k  | }  | 
1765  |  |  | 
1766  |  | EXT_RETURN tls_construct_stoc_cookie(SSL_CONNECTION *s, WPACKET *pkt,  | 
1767  |  |                                      unsigned int context,  | 
1768  |  |                                      X509 *x, size_t chainidx)  | 
1769  | 506  | { | 
1770  | 506  | #ifndef OPENSSL_NO_TLS1_3  | 
1771  | 506  |     unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;  | 
1772  | 506  |     unsigned char *hmac, *hmac2;  | 
1773  | 506  |     size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;  | 
1774  | 506  |     EVP_MD_CTX *hctx;  | 
1775  | 506  |     EVP_PKEY *pkey;  | 
1776  | 506  |     int ret = EXT_RETURN_FAIL;  | 
1777  | 506  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
1778  | 506  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
1779  | 506  |     SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);  | 
1780  |  |  | 
1781  | 506  |     if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0)  | 
1782  | 506  |         return EXT_RETURN_NOT_SENT;  | 
1783  |  |  | 
1784  | 0  |     if (sctx->gen_stateless_cookie_cb == NULL) { | 
1785  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_COOKIE_CALLBACK_SET);  | 
1786  | 0  |         return EXT_RETURN_FAIL;  | 
1787  | 0  |     }  | 
1788  |  |  | 
1789  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)  | 
1790  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1791  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1792  | 0  |             || !WPACKET_get_total_written(pkt, &startlen)  | 
1793  | 0  |             || !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)  | 
1794  | 0  |             || !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)  | 
1795  | 0  |             || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)  | 
1796  | 0  |             || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)  | 
1797  | 0  |             || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt,  | 
1798  | 0  |                                                 &ciphlen)  | 
1799  |  |                /* Is there a key_share extension present in this HRR? */  | 
1800  | 0  |             || !WPACKET_put_bytes_u8(pkt, s->s3.peer_tmp == NULL)  | 
1801  | 0  |             || !WPACKET_put_bytes_u64(pkt, time(NULL))  | 
1802  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1803  | 0  |             || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) { | 
1804  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1805  | 0  |         return EXT_RETURN_FAIL;  | 
1806  | 0  |     }  | 
1807  |  |  | 
1808  |  |     /*  | 
1809  |  |      * Get the hash of the initial ClientHello. ssl_handshake_hash() operates  | 
1810  |  |      * on raw buffers, so we first reserve sufficient bytes (above) and then  | 
1811  |  |      * subsequently allocate them (below)  | 
1812  |  |      */  | 
1813  | 0  |     if (!ssl3_digest_cached_records(s, 0)  | 
1814  | 0  |             || !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) { | 
1815  |  |         /* SSLfatal() already called */  | 
1816  | 0  |         return EXT_RETURN_FAIL;  | 
1817  | 0  |     }  | 
1818  |  |  | 
1819  | 0  |     if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)  | 
1820  | 0  |             || !ossl_assert(hashval1 == hashval2)  | 
1821  | 0  |             || !WPACKET_close(pkt)  | 
1822  | 0  |             || !WPACKET_start_sub_packet_u8(pkt)  | 
1823  | 0  |             || !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) { | 
1824  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1825  | 0  |         return EXT_RETURN_FAIL;  | 
1826  | 0  |     }  | 
1827  |  |  | 
1828  |  |     /* Generate the application cookie */  | 
1829  | 0  |     if (sctx->gen_stateless_cookie_cb(ussl, appcookie1,  | 
1830  | 0  |                                       &appcookielen) == 0) { | 
1831  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);  | 
1832  | 0  |         return EXT_RETURN_FAIL;  | 
1833  | 0  |     }  | 
1834  |  |  | 
1835  | 0  |     if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)  | 
1836  | 0  |             || !ossl_assert(appcookie1 == appcookie2)  | 
1837  | 0  |             || !WPACKET_close(pkt)  | 
1838  | 0  |             || !WPACKET_get_total_written(pkt, &totcookielen)  | 
1839  | 0  |             || !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) { | 
1840  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1841  | 0  |         return EXT_RETURN_FAIL;  | 
1842  | 0  |     }  | 
1843  | 0  |     hmaclen = SHA256_DIGEST_LENGTH;  | 
1844  |  | 
  | 
1845  | 0  |     totcookielen -= startlen;  | 
1846  | 0  |     if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) { | 
1847  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1848  | 0  |         return EXT_RETURN_FAIL;  | 
1849  | 0  |     }  | 
1850  |  |  | 
1851  |  |     /* HMAC the cookie */  | 
1852  | 0  |     hctx = EVP_MD_CTX_create();  | 
1853  | 0  |     pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",  | 
1854  | 0  |                                            sctx->propq,  | 
1855  | 0  |                                            s->session_ctx->ext.cookie_hmac_key,  | 
1856  | 0  |                                            sizeof(s->session_ctx->ext.cookie_hmac_key));  | 
1857  | 0  |     if (hctx == NULL || pkey == NULL) { | 
1858  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);  | 
1859  | 0  |         goto err;  | 
1860  | 0  |     }  | 
1861  |  |  | 
1862  | 0  |     if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,  | 
1863  | 0  |                               sctx->propq, pkey, NULL) <= 0  | 
1864  | 0  |             || EVP_DigestSign(hctx, hmac, &hmaclen, cookie,  | 
1865  | 0  |                               totcookielen) <= 0) { | 
1866  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1867  | 0  |         goto err;  | 
1868  | 0  |     }  | 
1869  |  |  | 
1870  | 0  |     if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) { | 
1871  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1872  | 0  |         goto err;  | 
1873  | 0  |     }  | 
1874  |  |  | 
1875  | 0  |     if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)  | 
1876  | 0  |             || !ossl_assert(hmac == hmac2)  | 
1877  | 0  |             || !ossl_assert(cookie == hmac - totcookielen)  | 
1878  | 0  |             || !WPACKET_close(pkt)  | 
1879  | 0  |             || !WPACKET_close(pkt)) { | 
1880  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1881  | 0  |         goto err;  | 
1882  | 0  |     }  | 
1883  |  |  | 
1884  | 0  |     ret = EXT_RETURN_SENT;  | 
1885  |  | 
  | 
1886  | 0  |  err:  | 
1887  | 0  |     EVP_MD_CTX_free(hctx);  | 
1888  | 0  |     EVP_PKEY_free(pkey);  | 
1889  | 0  |     return ret;  | 
1890  |  | #else  | 
1891  |  |     return EXT_RETURN_FAIL;  | 
1892  |  | #endif  | 
1893  | 0  | }  | 
1894  |  |  | 
1895  |  | EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL_CONNECTION *s, WPACKET *pkt,  | 
1896  |  |                                             unsigned int context, X509 *x,  | 
1897  |  |                                             size_t chainidx)  | 
1898  | 9.30k  | { | 
1899  | 9.30k  |     const unsigned char cryptopro_ext[36] = { | 
1900  | 9.30k  |         0xfd, 0xe8,         /* 65000 */  | 
1901  | 9.30k  |         0x00, 0x20,         /* 32 bytes length */  | 
1902  | 9.30k  |         0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,  | 
1903  | 9.30k  |         0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,  | 
1904  | 9.30k  |         0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,  | 
1905  | 9.30k  |         0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17  | 
1906  | 9.30k  |     };  | 
1907  |  |  | 
1908  | 9.30k  |     if (((s->s3.tmp.new_cipher->id & 0xFFFF) != 0x80  | 
1909  | 9.30k  |          && (s->s3.tmp.new_cipher->id & 0xFFFF) != 0x81)  | 
1910  | 9.30k  |             || (SSL_get_options(SSL_CONNECTION_GET_SSL(s))  | 
1911  | 0  |                 & SSL_OP_CRYPTOPRO_TLSEXT_BUG) == 0)  | 
1912  | 9.30k  |         return EXT_RETURN_NOT_SENT;  | 
1913  |  |  | 
1914  | 0  |     if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) { | 
1915  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1916  | 0  |         return EXT_RETURN_FAIL;  | 
1917  | 0  |     }  | 
1918  |  |  | 
1919  | 0  |     return EXT_RETURN_SENT;  | 
1920  | 0  | }  | 
1921  |  |  | 
1922  |  | EXT_RETURN tls_construct_stoc_early_data(SSL_CONNECTION *s, WPACKET *pkt,  | 
1923  |  |                                          unsigned int context, X509 *x,  | 
1924  |  |                                          size_t chainidx)  | 
1925  | 1.96k  | { | 
1926  | 1.96k  |     if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) { | 
1927  | 0  |         if (s->max_early_data == 0)  | 
1928  | 0  |             return EXT_RETURN_NOT_SENT;  | 
1929  |  |  | 
1930  | 0  |         if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)  | 
1931  | 0  |                 || !WPACKET_start_sub_packet_u16(pkt)  | 
1932  | 0  |                 || !WPACKET_put_bytes_u32(pkt, s->max_early_data)  | 
1933  | 0  |                 || !WPACKET_close(pkt)) { | 
1934  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1935  | 0  |             return EXT_RETURN_FAIL;  | 
1936  | 0  |         }  | 
1937  |  |  | 
1938  | 0  |         return EXT_RETURN_SENT;  | 
1939  | 0  |     }  | 
1940  |  |  | 
1941  | 1.96k  |     if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)  | 
1942  | 1.96k  |         return EXT_RETURN_NOT_SENT;  | 
1943  |  |  | 
1944  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)  | 
1945  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1946  | 0  |             || !WPACKET_close(pkt)) { | 
1947  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1948  | 0  |         return EXT_RETURN_FAIL;  | 
1949  | 0  |     }  | 
1950  |  |  | 
1951  | 0  |     return EXT_RETURN_SENT;  | 
1952  | 0  | }  | 
1953  |  |  | 
1954  |  | EXT_RETURN tls_construct_stoc_psk(SSL_CONNECTION *s, WPACKET *pkt,  | 
1955  |  |                                   unsigned int context,  | 
1956  |  |                                   X509 *x, size_t chainidx)  | 
1957  | 1.96k  | { | 
1958  | 1.96k  |     if (!s->hit)  | 
1959  | 1.96k  |         return EXT_RETURN_NOT_SENT;  | 
1960  |  |  | 
1961  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)  | 
1962  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1963  | 0  |             || !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity)  | 
1964  | 0  |             || !WPACKET_close(pkt)) { | 
1965  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1966  | 0  |         return EXT_RETURN_FAIL;  | 
1967  | 0  |     }  | 
1968  |  |  | 
1969  | 0  |     return EXT_RETURN_SENT;  | 
1970  | 0  | }  | 
1971  |  |  | 
1972  |  | EXT_RETURN tls_construct_stoc_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,  | 
1973  |  |                                                unsigned int context,  | 
1974  |  |                                                X509 *x, size_t chainidx)  | 
1975  | 7.64k  | { | 
1976  | 7.64k  |     if (sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_ERROR  | 
1977  | 7.64k  |         && (send_certificate_request(sc)  | 
1978  | 0  |             || sc->post_handshake_auth == SSL_PHA_EXT_RECEIVED)) { | 
1979  |  |         /* Did not receive an acceptable cert type - and doing client auth */  | 
1980  | 0  |         SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);  | 
1981  | 0  |         return EXT_RETURN_FAIL;  | 
1982  | 0  |     }  | 
1983  |  |  | 
1984  | 7.64k  |     if (sc->ext.client_cert_type == TLSEXT_cert_type_x509) { | 
1985  | 7.64k  |         sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;  | 
1986  | 7.64k  |         return EXT_RETURN_NOT_SENT;  | 
1987  | 7.64k  |     }  | 
1988  |  |  | 
1989  |  |     /*  | 
1990  |  |      * Note: only supposed to send this if we are going to do a cert request,  | 
1991  |  |      * but TLSv1.3 could do a PHA request if the client supports it  | 
1992  |  |      */  | 
1993  | 0  |     if ((!send_certificate_request(sc) && sc->post_handshake_auth != SSL_PHA_EXT_RECEIVED)  | 
1994  | 0  |             || sc->ext.client_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD  | 
1995  | 0  |             || sc->client_cert_type == NULL) { | 
1996  |  |         /* if we don't send it, reset to TLSEXT_cert_type_x509 */  | 
1997  | 0  |         sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;  | 
1998  | 0  |         sc->ext.client_cert_type = TLSEXT_cert_type_x509;  | 
1999  | 0  |         return EXT_RETURN_NOT_SENT;  | 
2000  | 0  |     }  | 
2001  |  |  | 
2002  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)  | 
2003  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
2004  | 0  |             || !WPACKET_put_bytes_u8(pkt, sc->ext.client_cert_type)  | 
2005  | 0  |             || !WPACKET_close(pkt)) { | 
2006  | 0  |         SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2007  | 0  |         return EXT_RETURN_FAIL;  | 
2008  | 0  |     }  | 
2009  | 0  |     return EXT_RETURN_SENT;  | 
2010  | 0  | }  | 
2011  |  |  | 
2012  |  | /* One of |pref|, |other| is configured and the values are sanitized */  | 
2013  |  | static int reconcile_cert_type(const unsigned char *pref, size_t pref_len,  | 
2014  |  |                                const unsigned char *other, size_t other_len,  | 
2015  |  |                                uint8_t *chosen_cert_type)  | 
2016  | 0  | { | 
2017  | 0  |     size_t i;  | 
2018  |  | 
  | 
2019  | 0  |     for (i = 0; i < pref_len; i++) { | 
2020  | 0  |         if (memchr(other, pref[i], other_len) != NULL) { | 
2021  | 0  |             *chosen_cert_type = pref[i];  | 
2022  | 0  |             return OSSL_CERT_TYPE_CTOS_GOOD;  | 
2023  | 0  |         }  | 
2024  | 0  |     }  | 
2025  | 0  |     return OSSL_CERT_TYPE_CTOS_ERROR;  | 
2026  | 0  | }  | 
2027  |  |  | 
2028  |  | int tls_parse_ctos_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,  | 
2029  |  |                                     unsigned int context,  | 
2030  |  |                                     X509 *x, size_t chainidx)  | 
2031  | 81  | { | 
2032  | 81  |     PACKET supported_cert_types;  | 
2033  | 81  |     const unsigned char *data;  | 
2034  | 81  |     size_t len;  | 
2035  |  |  | 
2036  |  |     /* Ignore the extension */  | 
2037  | 81  |     if (sc->client_cert_type == NULL) { | 
2038  | 81  |         sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;  | 
2039  | 81  |         sc->ext.client_cert_type = TLSEXT_cert_type_x509;  | 
2040  | 81  |         return 1;  | 
2041  | 81  |     }  | 
2042  |  |  | 
2043  | 0  |     if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) { | 
2044  | 0  |         sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;  | 
2045  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2046  | 0  |         return 0;  | 
2047  | 0  |     }  | 
2048  | 0  |     if ((len = PACKET_remaining(&supported_cert_types)) == 0) { | 
2049  | 0  |         sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;  | 
2050  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2051  | 0  |         return 0;  | 
2052  | 0  |     }  | 
2053  | 0  |     if (!PACKET_get_bytes(&supported_cert_types, &data, len)) { | 
2054  | 0  |         sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;  | 
2055  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2056  | 0  |         return 0;  | 
2057  | 0  |     }  | 
2058  |  |     /* client_cert_type: client (peer) has priority */  | 
2059  | 0  |     sc->ext.client_cert_type_ctos = reconcile_cert_type(data, len,  | 
2060  | 0  |                                                         sc->client_cert_type, sc->client_cert_type_len,  | 
2061  | 0  |                                                         &sc->ext.client_cert_type);  | 
2062  |  |  | 
2063  |  |     /* Ignore the error until sending - so we can check cert auth*/  | 
2064  | 0  |     return 1;  | 
2065  | 0  | }  | 
2066  |  |  | 
2067  |  | EXT_RETURN tls_construct_stoc_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,  | 
2068  |  |                                                unsigned int context,  | 
2069  |  |                                                X509 *x, size_t chainidx)  | 
2070  | 7.64k  | { | 
2071  | 7.64k  |     if (sc->ext.server_cert_type == TLSEXT_cert_type_x509) { | 
2072  | 7.64k  |         sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;  | 
2073  | 7.64k  |         return EXT_RETURN_NOT_SENT;  | 
2074  | 7.64k  |     }  | 
2075  | 0  |     if (sc->ext.server_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD  | 
2076  | 0  |             || sc->server_cert_type == NULL) { | 
2077  |  |         /* if we don't send it, reset to TLSEXT_cert_type_x509 */  | 
2078  | 0  |         sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;  | 
2079  | 0  |         sc->ext.server_cert_type = TLSEXT_cert_type_x509;  | 
2080  | 0  |         return EXT_RETURN_NOT_SENT;  | 
2081  | 0  |     }  | 
2082  |  |  | 
2083  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)  | 
2084  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
2085  | 0  |             || !WPACKET_put_bytes_u8(pkt, sc->ext.server_cert_type)  | 
2086  | 0  |             || !WPACKET_close(pkt)) { | 
2087  | 0  |         SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2088  | 0  |         return EXT_RETURN_FAIL;  | 
2089  | 0  |     }  | 
2090  | 0  |     return EXT_RETURN_SENT;  | 
2091  | 0  | }  | 
2092  |  |  | 
2093  |  | int tls_parse_ctos_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,  | 
2094  |  |                                     unsigned int context,  | 
2095  |  |                                     X509 *x, size_t chainidx)  | 
2096  | 43  | { | 
2097  | 43  |     PACKET supported_cert_types;  | 
2098  | 43  |     const unsigned char *data;  | 
2099  | 43  |     size_t len;  | 
2100  |  |  | 
2101  |  |     /* Ignore the extension */  | 
2102  | 43  |     if (sc->server_cert_type == NULL) { | 
2103  | 43  |         sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;  | 
2104  | 43  |         sc->ext.server_cert_type = TLSEXT_cert_type_x509;  | 
2105  | 43  |         return 1;  | 
2106  | 43  |     }  | 
2107  |  |  | 
2108  | 0  |     if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) { | 
2109  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2110  | 0  |         return 0;  | 
2111  | 0  |     }  | 
2112  |  |  | 
2113  | 0  |     if ((len = PACKET_remaining(&supported_cert_types)) == 0) { | 
2114  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2115  | 0  |         return 0;  | 
2116  | 0  |     }  | 
2117  | 0  |     if (!PACKET_get_bytes(&supported_cert_types, &data, len)) { | 
2118  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2119  | 0  |         return 0;  | 
2120  | 0  |     }  | 
2121  |  |     /* server_cert_type: server (this) has priority */  | 
2122  | 0  |     sc->ext.server_cert_type_ctos = reconcile_cert_type(sc->server_cert_type, sc->server_cert_type_len,  | 
2123  | 0  |                                                         data, len,  | 
2124  | 0  |                                                         &sc->ext.server_cert_type);  | 
2125  | 0  |     if (sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)  | 
2126  | 0  |         return 1;  | 
2127  |  |  | 
2128  |  |     /* Did not receive an acceptable cert type */  | 
2129  | 0  |     SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);  | 
2130  | 0  |     return 0;  | 
2131  | 0  | }  |