/src/openssl/ssl/statem/extensions_clnt.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2016-2025 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 "internal/cryptlib.h"  | 
13  |  | #include "internal/ssl_unwrap.h"  | 
14  |  | #include "statem_local.h"  | 
15  |  |  | 
16  |  | EXT_RETURN tls_construct_ctos_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,  | 
17  |  |                                           unsigned int context, X509 *x,  | 
18  |  |                                           size_t chainidx)  | 
19  | 0  | { | 
20  | 0  |     if (!s->renegotiate) { | 
21  |  |         /* If not renegotiating, send an empty RI extension to indicate support */  | 
22  |  | 
  | 
23  |  | #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION  | 
24  |  | # error Internal DTLS version error  | 
25  |  | #endif  | 
26  |  | 
  | 
27  | 0  |         if (!SSL_CONNECTION_IS_DTLS(s)  | 
28  | 0  |             && (s->min_proto_version >= TLS1_3_VERSION  | 
29  | 0  |                 || (ssl_security(s, SSL_SECOP_VERSION, 0, TLS1_VERSION, NULL)  | 
30  | 0  |                     && s->min_proto_version <= TLS1_VERSION))) { | 
31  |  |             /*  | 
32  |  |              * For TLS <= 1.0 SCSV is used instead, and for TLS 1.3 this  | 
33  |  |              * extension isn't used at all.  | 
34  |  |              */  | 
35  | 0  |             return EXT_RETURN_NOT_SENT;  | 
36  | 0  |         }  | 
37  |  |  | 
38  |  |  | 
39  | 0  |         if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)  | 
40  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
41  | 0  |             || !WPACKET_put_bytes_u8(pkt, 0)  | 
42  | 0  |             || !WPACKET_close(pkt)) { | 
43  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
44  | 0  |             return EXT_RETURN_FAIL;  | 
45  | 0  |         }  | 
46  |  |  | 
47  | 0  |         return EXT_RETURN_SENT;  | 
48  | 0  |     }  | 
49  |  |  | 
50  |  |     /* Add a complete RI extension if renegotiating */  | 
51  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)  | 
52  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
53  | 0  |             || !WPACKET_sub_memcpy_u8(pkt, s->s3.previous_client_finished,  | 
54  | 0  |                                s->s3.previous_client_finished_len)  | 
55  | 0  |             || !WPACKET_close(pkt)) { | 
56  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
57  | 0  |         return EXT_RETURN_FAIL;  | 
58  | 0  |     }  | 
59  |  |  | 
60  | 0  |     return EXT_RETURN_SENT;  | 
61  | 0  | }  | 
62  |  |  | 
63  |  | EXT_RETURN tls_construct_ctos_server_name(SSL_CONNECTION *s, WPACKET *pkt,  | 
64  |  |                                           unsigned int context, X509 *x,  | 
65  |  |                                           size_t chainidx)  | 
66  | 0  | { | 
67  | 0  |     if (s->ext.hostname == NULL)  | 
68  | 0  |         return EXT_RETURN_NOT_SENT;  | 
69  |  |  | 
70  |  |     /* Add TLS extension servername to the Client Hello message */  | 
71  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)  | 
72  |  |                /* Sub-packet for server_name extension */  | 
73  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
74  |  |                /* Sub-packet for servername list (always 1 hostname)*/  | 
75  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
76  | 0  |             || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)  | 
77  | 0  |             || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname,  | 
78  | 0  |                                        strlen(s->ext.hostname))  | 
79  | 0  |             || !WPACKET_close(pkt)  | 
80  | 0  |             || !WPACKET_close(pkt)) { | 
81  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
82  | 0  |         return EXT_RETURN_FAIL;  | 
83  | 0  |     }  | 
84  |  |  | 
85  | 0  |     return EXT_RETURN_SENT;  | 
86  | 0  | }  | 
87  |  |  | 
88  |  | /* Push a Max Fragment Len extension into ClientHello */  | 
89  |  | EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,  | 
90  |  |                                              unsigned int context, X509 *x,  | 
91  |  |                                              size_t chainidx)  | 
92  | 0  | { | 
93  | 0  |     if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED)  | 
94  | 0  |         return EXT_RETURN_NOT_SENT;  | 
95  |  |  | 
96  |  |     /* Add Max Fragment Length extension if client enabled it. */  | 
97  |  |     /*-  | 
98  |  |      * 4 bytes for this extension type and extension length  | 
99  |  |      * 1 byte for the Max Fragment Length code value.  | 
100  |  |      */  | 
101  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)  | 
102  |  |             /* Sub-packet for Max Fragment Length extension (1 byte) */  | 
103  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
104  | 0  |             || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode)  | 
105  | 0  |             || !WPACKET_close(pkt)) { | 
106  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
107  | 0  |         return EXT_RETURN_FAIL;  | 
108  | 0  |     }  | 
109  |  |  | 
110  | 0  |     return EXT_RETURN_SENT;  | 
111  | 0  | }  | 
112  |  |  | 
113  |  | #ifndef OPENSSL_NO_SRP  | 
114  |  | EXT_RETURN tls_construct_ctos_srp(SSL_CONNECTION *s, WPACKET *pkt,  | 
115  |  |                                   unsigned int context,  | 
116  |  |                                   X509 *x, size_t chainidx)  | 
117  | 0  | { | 
118  |  |     /* Add SRP username if there is one */  | 
119  | 0  |     if (s->srp_ctx.login == NULL)  | 
120  | 0  |         return EXT_RETURN_NOT_SENT;  | 
121  |  |  | 
122  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)  | 
123  |  |                /* Sub-packet for SRP extension */  | 
124  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
125  | 0  |             || !WPACKET_start_sub_packet_u8(pkt)  | 
126  |  |                /* login must not be zero...internal error if so */  | 
127  | 0  |             || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)  | 
128  | 0  |             || !WPACKET_memcpy(pkt, s->srp_ctx.login,  | 
129  | 0  |                                strlen(s->srp_ctx.login))  | 
130  | 0  |             || !WPACKET_close(pkt)  | 
131  | 0  |             || !WPACKET_close(pkt)) { | 
132  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
133  | 0  |         return EXT_RETURN_FAIL;  | 
134  | 0  |     }  | 
135  |  |  | 
136  | 0  |     return EXT_RETURN_SENT;  | 
137  | 0  | }  | 
138  |  | #endif  | 
139  |  |  | 
140  |  | static int use_ecc(SSL_CONNECTION *s, int min_version, int max_version)  | 
141  | 0  | { | 
142  | 0  |     int i, end, ret = 0;  | 
143  | 0  |     unsigned long alg_k, alg_a;  | 
144  | 0  |     STACK_OF(SSL_CIPHER) *cipher_stack = NULL;  | 
145  | 0  |     const uint16_t *pgroups = NULL;  | 
146  | 0  |     size_t num_groups, j;  | 
147  | 0  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
148  |  |  | 
149  |  |     /* See if we support any ECC ciphersuites */  | 
150  | 0  |     if (s->version == SSL3_VERSION)  | 
151  | 0  |         return 0;  | 
152  |  |  | 
153  | 0  |     cipher_stack = SSL_get1_supported_ciphers(ssl);  | 
154  | 0  |     end = sk_SSL_CIPHER_num(cipher_stack);  | 
155  | 0  |     for (i = 0; i < end; i++) { | 
156  | 0  |         const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);  | 
157  |  | 
  | 
158  | 0  |         alg_k = c->algorithm_mkey;  | 
159  | 0  |         alg_a = c->algorithm_auth;  | 
160  | 0  |         if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))  | 
161  | 0  |                 || (alg_a & SSL_aECDSA)  | 
162  | 0  |                 || c->min_tls >= TLS1_3_VERSION) { | 
163  | 0  |             ret = 1;  | 
164  | 0  |             break;  | 
165  | 0  |         }  | 
166  | 0  |     }  | 
167  | 0  |     sk_SSL_CIPHER_free(cipher_stack);  | 
168  | 0  |     if (!ret)  | 
169  | 0  |         return 0;  | 
170  |  |  | 
171  |  |     /* Check we have at least one EC supported group */  | 
172  | 0  |     tls1_get_supported_groups(s, &pgroups, &num_groups);  | 
173  | 0  |     for (j = 0; j < num_groups; j++) { | 
174  | 0  |         uint16_t ctmp = pgroups[j];  | 
175  |  | 
  | 
176  | 0  |         if (tls_valid_group(s, ctmp, min_version, max_version, 1, NULL)  | 
177  | 0  |                 && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED))  | 
178  | 0  |             return 1;  | 
179  | 0  |     }  | 
180  |  |  | 
181  | 0  |     return 0;  | 
182  | 0  | }  | 
183  |  |  | 
184  |  | EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,  | 
185  |  |                                             unsigned int context, X509 *x,  | 
186  |  |                                             size_t chainidx)  | 
187  | 0  | { | 
188  | 0  |     const unsigned char *pformats;  | 
189  | 0  |     size_t num_formats;  | 
190  | 0  |     int reason, min_version, max_version;  | 
191  |  | 
  | 
192  | 0  |     reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);  | 
193  | 0  |     if (reason != 0) { | 
194  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);  | 
195  | 0  |         return EXT_RETURN_FAIL;  | 
196  | 0  |     }  | 
197  | 0  |     if (!use_ecc(s, min_version, max_version))  | 
198  | 0  |         return EXT_RETURN_NOT_SENT;  | 
199  |  |  | 
200  |  |     /* Add TLS extension ECPointFormats to the ClientHello message */  | 
201  | 0  |     tls1_get_formatlist(s, &pformats, &num_formats);  | 
202  |  | 
  | 
203  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)  | 
204  |  |                /* Sub-packet for formats extension */  | 
205  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
206  | 0  |             || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)  | 
207  | 0  |             || !WPACKET_close(pkt)) { | 
208  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
209  | 0  |         return EXT_RETURN_FAIL;  | 
210  | 0  |     }  | 
211  |  |  | 
212  | 0  |     return EXT_RETURN_SENT;  | 
213  | 0  | }  | 
214  |  |  | 
215  |  | EXT_RETURN tls_construct_ctos_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,  | 
216  |  |                                                unsigned int context, X509 *x,  | 
217  |  |                                                size_t chainidx)  | 
218  | 0  | { | 
219  | 0  |     const uint16_t *pgroups = NULL;  | 
220  | 0  |     size_t num_groups = 0, i, tls13added = 0, added = 0;  | 
221  | 0  |     int min_version, max_version, reason;  | 
222  |  | 
  | 
223  | 0  |     reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);  | 
224  | 0  |     if (reason != 0) { | 
225  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);  | 
226  | 0  |         return EXT_RETURN_FAIL;  | 
227  | 0  |     }  | 
228  |  |  | 
229  |  |     /*  | 
230  |  |      * We only support EC groups in TLSv1.2 or below, and in DTLS. Therefore  | 
231  |  |      * if we don't have EC support then we don't send this extension.  | 
232  |  |      */  | 
233  | 0  |     if (!use_ecc(s, min_version, max_version)  | 
234  | 0  |             && (SSL_CONNECTION_IS_DTLS(s) || max_version < TLS1_3_VERSION))  | 
235  | 0  |         return EXT_RETURN_NOT_SENT;  | 
236  |  |  | 
237  |  |     /*  | 
238  |  |      * Add TLS extension supported_groups to the ClientHello message  | 
239  |  |      */  | 
240  | 0  |     tls1_get_supported_groups(s, &pgroups, &num_groups);  | 
241  |  | 
  | 
242  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)  | 
243  |  |                /* Sub-packet for supported_groups extension */  | 
244  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
245  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
246  | 0  |             || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) { | 
247  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
248  | 0  |         return EXT_RETURN_FAIL;  | 
249  | 0  |     }  | 
250  |  |     /* Copy group ID if supported */  | 
251  | 0  |     for (i = 0; i < num_groups; i++) { | 
252  | 0  |         uint16_t ctmp = pgroups[i];  | 
253  | 0  |         int okfortls13;  | 
254  |  | 
  | 
255  | 0  |         if (tls_valid_group(s, ctmp, min_version, max_version, 0, &okfortls13)  | 
256  | 0  |                 && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) { | 
257  | 0  |             if (!WPACKET_put_bytes_u16(pkt, ctmp)) { | 
258  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
259  | 0  |                 return EXT_RETURN_FAIL;  | 
260  | 0  |             }  | 
261  | 0  |             if (okfortls13 && max_version == TLS1_3_VERSION)  | 
262  | 0  |                 tls13added++;  | 
263  | 0  |             added++;  | 
264  | 0  |         }  | 
265  | 0  |     }  | 
266  | 0  |     if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | 
267  | 0  |         if (added == 0)  | 
268  | 0  |             SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS,  | 
269  | 0  |                           "No groups enabled for max supported SSL/TLS version");  | 
270  | 0  |         else  | 
271  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
272  | 0  |         return EXT_RETURN_FAIL;  | 
273  | 0  |     }  | 
274  |  |  | 
275  | 0  |     if (tls13added == 0 && max_version == TLS1_3_VERSION) { | 
276  | 0  |         SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS,  | 
277  | 0  |                       "No groups enabled for max supported SSL/TLS version");  | 
278  | 0  |         return EXT_RETURN_FAIL;  | 
279  | 0  |     }  | 
280  |  |  | 
281  | 0  |     return EXT_RETURN_SENT;  | 
282  | 0  | }  | 
283  |  |  | 
284  |  | EXT_RETURN tls_construct_ctos_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,  | 
285  |  |                                              unsigned int context, X509 *x,  | 
286  |  |                                              size_t chainidx)  | 
287  | 0  | { | 
288  | 0  |     size_t ticklen;  | 
289  |  | 
  | 
290  | 0  |     if (!tls_use_ticket(s))  | 
291  | 0  |         return EXT_RETURN_NOT_SENT;  | 
292  |  |  | 
293  | 0  |     if (!s->new_session && s->session != NULL  | 
294  | 0  |             && s->session->ext.tick != NULL  | 
295  | 0  |             && s->session->ssl_version != TLS1_3_VERSION) { | 
296  | 0  |         ticklen = s->session->ext.ticklen;  | 
297  | 0  |     } else if (s->session && s->ext.session_ticket != NULL  | 
298  | 0  |                && s->ext.session_ticket->data != NULL) { | 
299  | 0  |         ticklen = s->ext.session_ticket->length;  | 
300  | 0  |         s->session->ext.tick = OPENSSL_malloc(ticklen);  | 
301  | 0  |         if (s->session->ext.tick == NULL) { | 
302  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
303  | 0  |             return EXT_RETURN_FAIL;  | 
304  | 0  |         }  | 
305  | 0  |         memcpy(s->session->ext.tick,  | 
306  | 0  |                s->ext.session_ticket->data, ticklen);  | 
307  | 0  |         s->session->ext.ticklen = ticklen;  | 
308  | 0  |     } else { | 
309  | 0  |         ticklen = 0;  | 
310  | 0  |     }  | 
311  |  |  | 
312  | 0  |     if (ticklen == 0 && s->ext.session_ticket != NULL &&  | 
313  | 0  |             s->ext.session_ticket->data == NULL)  | 
314  | 0  |         return EXT_RETURN_NOT_SENT;  | 
315  |  |  | 
316  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)  | 
317  | 0  |             || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) { | 
318  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
319  | 0  |         return EXT_RETURN_FAIL;  | 
320  | 0  |     }  | 
321  |  |  | 
322  | 0  |     return EXT_RETURN_SENT;  | 
323  | 0  | }  | 
324  |  |  | 
325  |  | EXT_RETURN tls_construct_ctos_sig_algs(SSL_CONNECTION *s, WPACKET *pkt,  | 
326  |  |                                        unsigned int context, X509 *x,  | 
327  |  |                                        size_t chainidx)  | 
328  | 0  | { | 
329  | 0  |     size_t salglen;  | 
330  | 0  |     const uint16_t *salg;  | 
331  |  |  | 
332  |  |     /*  | 
333  |  |      * This used both in the initial hello and as part of renegotiation,  | 
334  |  |      * in the latter case, the client version may be already set and may  | 
335  |  |      * be lower than that initially offered in `client_version`.  | 
336  |  |      */  | 
337  | 0  |     if (!SSL_CONNECTION_IS_DTLS(s)) { | 
338  | 0  |         if (s->client_version < TLS1_2_VERSION  | 
339  | 0  |             || (s->ssl.method->version != TLS_ANY_VERSION  | 
340  | 0  |                 && s->version < TLS1_2_VERSION))  | 
341  | 0  |         return EXT_RETURN_NOT_SENT;  | 
342  | 0  |     } else { | 
343  | 0  |         if (DTLS_VERSION_LT(s->client_version, DTLS1_2_VERSION)  | 
344  | 0  |             || (s->ssl.method->version != DTLS_ANY_VERSION  | 
345  | 0  |                 && DTLS_VERSION_LT(s->version, DTLS1_2_VERSION)))  | 
346  | 0  |         return EXT_RETURN_NOT_SENT;  | 
347  | 0  |     }  | 
348  |  |  | 
349  | 0  |     salglen = tls12_get_psigalgs(s, 1, &salg);  | 
350  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)  | 
351  |  |                /* Sub-packet for sig-algs extension */  | 
352  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
353  |  |                /* Sub-packet for the actual list */  | 
354  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
355  | 0  |             || !tls12_copy_sigalgs(s, pkt, salg, salglen)  | 
356  | 0  |             || !WPACKET_close(pkt)  | 
357  | 0  |             || !WPACKET_close(pkt)) { | 
358  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
359  | 0  |         return EXT_RETURN_FAIL;  | 
360  | 0  |     }  | 
361  |  |  | 
362  | 0  |     return EXT_RETURN_SENT;  | 
363  | 0  | }  | 
364  |  |  | 
365  |  | #ifndef OPENSSL_NO_OCSP  | 
366  |  | EXT_RETURN tls_construct_ctos_status_request(SSL_CONNECTION *s, WPACKET *pkt,  | 
367  |  |                                              unsigned int context, X509 *x,  | 
368  |  |                                              size_t chainidx)  | 
369  | 0  | { | 
370  | 0  |     int i;  | 
371  |  |  | 
372  |  |     /* This extension isn't defined for client Certificates */  | 
373  | 0  |     if (x != NULL)  | 
374  | 0  |         return EXT_RETURN_NOT_SENT;  | 
375  |  |  | 
376  | 0  |     if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp)  | 
377  | 0  |         return EXT_RETURN_NOT_SENT;  | 
378  |  |  | 
379  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)  | 
380  |  |                /* Sub-packet for status request extension */  | 
381  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
382  | 0  |             || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)  | 
383  |  |                /* Sub-packet for the ids */  | 
384  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)) { | 
385  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
386  | 0  |         return EXT_RETURN_FAIL;  | 
387  | 0  |     }  | 
388  | 0  |     for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) { | 
389  | 0  |         unsigned char *idbytes;  | 
390  | 0  |         OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i);  | 
391  | 0  |         int idlen = i2d_OCSP_RESPID(id, NULL);  | 
392  |  | 
  | 
393  | 0  |         if (idlen <= 0  | 
394  |  |                    /* Sub-packet for an individual id */  | 
395  | 0  |                 || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)  | 
396  | 0  |                 || i2d_OCSP_RESPID(id, &idbytes) != idlen) { | 
397  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
398  | 0  |             return EXT_RETURN_FAIL;  | 
399  | 0  |         }  | 
400  | 0  |     }  | 
401  | 0  |     if (!WPACKET_close(pkt)  | 
402  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)) { | 
403  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
404  | 0  |         return EXT_RETURN_FAIL;  | 
405  | 0  |     }  | 
406  | 0  |     if (s->ext.ocsp.exts) { | 
407  | 0  |         unsigned char *extbytes;  | 
408  | 0  |         int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL);  | 
409  |  | 
  | 
410  | 0  |         if (extlen < 0) { | 
411  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
412  | 0  |             return EXT_RETURN_FAIL;  | 
413  | 0  |         }  | 
414  | 0  |         if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)  | 
415  | 0  |                 || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes)  | 
416  | 0  |                    != extlen) { | 
417  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
418  | 0  |             return EXT_RETURN_FAIL;  | 
419  | 0  |        }  | 
420  | 0  |     }  | 
421  | 0  |     if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | 
422  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
423  | 0  |         return EXT_RETURN_FAIL;  | 
424  | 0  |     }  | 
425  |  |  | 
426  | 0  |     return EXT_RETURN_SENT;  | 
427  | 0  | }  | 
428  |  | #endif  | 
429  |  |  | 
430  |  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
431  |  | EXT_RETURN tls_construct_ctos_npn(SSL_CONNECTION *s, WPACKET *pkt,  | 
432  |  |                                   unsigned int context,  | 
433  |  |                                   X509 *x, size_t chainidx)  | 
434  | 0  | { | 
435  | 0  |     if (SSL_CONNECTION_GET_CTX(s)->ext.npn_select_cb == NULL  | 
436  | 0  |         || !SSL_IS_FIRST_HANDSHAKE(s))  | 
437  | 0  |         return EXT_RETURN_NOT_SENT;  | 
438  |  |  | 
439  |  |     /*  | 
440  |  |      * The client advertises an empty extension to indicate its support  | 
441  |  |      * for Next Protocol Negotiation  | 
442  |  |      */  | 
443  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)  | 
444  | 0  |             || !WPACKET_put_bytes_u16(pkt, 0)) { | 
445  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
446  | 0  |         return EXT_RETURN_FAIL;  | 
447  | 0  |     }  | 
448  |  |  | 
449  | 0  |     return EXT_RETURN_SENT;  | 
450  | 0  | }  | 
451  |  | #endif  | 
452  |  |  | 
453  |  | EXT_RETURN tls_construct_ctos_alpn(SSL_CONNECTION *s, WPACKET *pkt,  | 
454  |  |                                    unsigned int context,  | 
455  |  |                                    X509 *x, size_t chainidx)  | 
456  | 0  | { | 
457  | 0  |     s->s3.alpn_sent = 0;  | 
458  |  | 
  | 
459  | 0  |     if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s))  | 
460  | 0  |         return EXT_RETURN_NOT_SENT;  | 
461  |  |  | 
462  | 0  |     if (!WPACKET_put_bytes_u16(pkt,  | 
463  | 0  |                 TLSEXT_TYPE_application_layer_protocol_negotiation)  | 
464  |  |                /* Sub-packet ALPN extension */  | 
465  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
466  | 0  |             || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len)  | 
467  | 0  |             || !WPACKET_close(pkt)) { | 
468  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
469  | 0  |         return EXT_RETURN_FAIL;  | 
470  | 0  |     }  | 
471  | 0  |     s->s3.alpn_sent = 1;  | 
472  |  | 
  | 
473  | 0  |     return EXT_RETURN_SENT;  | 
474  | 0  | }  | 
475  |  |  | 
476  |  |  | 
477  |  | #ifndef OPENSSL_NO_SRTP  | 
478  |  | EXT_RETURN tls_construct_ctos_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,  | 
479  |  |                                        unsigned int context, X509 *x,  | 
480  |  |                                        size_t chainidx)  | 
481  | 0  | { | 
482  | 0  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
483  | 0  |     STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(ssl);  | 
484  | 0  |     int i, end;  | 
485  |  | 
  | 
486  | 0  |     if (clnt == NULL)  | 
487  | 0  |         return EXT_RETURN_NOT_SENT;  | 
488  |  |  | 
489  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)  | 
490  |  |                /* Sub-packet for SRTP extension */  | 
491  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
492  |  |                /* Sub-packet for the protection profile list */  | 
493  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)) { | 
494  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
495  | 0  |         return EXT_RETURN_FAIL;  | 
496  | 0  |     }  | 
497  |  |  | 
498  | 0  |     end = sk_SRTP_PROTECTION_PROFILE_num(clnt);  | 
499  | 0  |     for (i = 0; i < end; i++) { | 
500  | 0  |         const SRTP_PROTECTION_PROFILE *prof =  | 
501  | 0  |             sk_SRTP_PROTECTION_PROFILE_value(clnt, i);  | 
502  |  | 
  | 
503  | 0  |         if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) { | 
504  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
505  | 0  |             return EXT_RETURN_FAIL;  | 
506  | 0  |         }  | 
507  | 0  |     }  | 
508  | 0  |     if (!WPACKET_close(pkt)  | 
509  |  |                /* Add an empty use_mki value */  | 
510  | 0  |             || !WPACKET_put_bytes_u8(pkt, 0)  | 
511  | 0  |             || !WPACKET_close(pkt)) { | 
512  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
513  | 0  |         return EXT_RETURN_FAIL;  | 
514  | 0  |     }  | 
515  |  |  | 
516  | 0  |     return EXT_RETURN_SENT;  | 
517  | 0  | }  | 
518  |  | #endif  | 
519  |  |  | 
520  |  | EXT_RETURN tls_construct_ctos_etm(SSL_CONNECTION *s, WPACKET *pkt,  | 
521  |  |                                   unsigned int context,  | 
522  |  |                                   X509 *x, size_t chainidx)  | 
523  | 0  | { | 
524  | 0  |     if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)  | 
525  | 0  |         return EXT_RETURN_NOT_SENT;  | 
526  |  |  | 
527  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)  | 
528  | 0  |             || !WPACKET_put_bytes_u16(pkt, 0)) { | 
529  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
530  | 0  |         return EXT_RETURN_FAIL;  | 
531  | 0  |     }  | 
532  |  |  | 
533  | 0  |     return EXT_RETURN_SENT;  | 
534  | 0  | }  | 
535  |  |  | 
536  |  | #ifndef OPENSSL_NO_CT  | 
537  |  | EXT_RETURN tls_construct_ctos_sct(SSL_CONNECTION *s, WPACKET *pkt,  | 
538  |  |                                   unsigned int context,  | 
539  |  |                                   X509 *x, size_t chainidx)  | 
540  | 0  | { | 
541  | 0  |     if (s->ct_validation_callback == NULL)  | 
542  | 0  |         return EXT_RETURN_NOT_SENT;  | 
543  |  |  | 
544  |  |     /* Not defined for client Certificates */  | 
545  | 0  |     if (x != NULL)  | 
546  | 0  |         return EXT_RETURN_NOT_SENT;  | 
547  |  |  | 
548  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)  | 
549  | 0  |             || !WPACKET_put_bytes_u16(pkt, 0)) { | 
550  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
551  | 0  |         return EXT_RETURN_FAIL;  | 
552  | 0  |     }  | 
553  |  |  | 
554  | 0  |     return EXT_RETURN_SENT;  | 
555  | 0  | }  | 
556  |  | #endif  | 
557  |  |  | 
558  |  | EXT_RETURN tls_construct_ctos_ems(SSL_CONNECTION *s, WPACKET *pkt,  | 
559  |  |                                   unsigned int context,  | 
560  |  |                                   X509 *x, size_t chainidx)  | 
561  | 0  | { | 
562  | 0  |     if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)  | 
563  | 0  |         return EXT_RETURN_NOT_SENT;  | 
564  |  |  | 
565  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)  | 
566  | 0  |             || !WPACKET_put_bytes_u16(pkt, 0)) { | 
567  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
568  | 0  |         return EXT_RETURN_FAIL;  | 
569  | 0  |     }  | 
570  |  |  | 
571  | 0  |     return EXT_RETURN_SENT;  | 
572  | 0  | }  | 
573  |  |  | 
574  |  | EXT_RETURN tls_construct_ctos_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,  | 
575  |  |                                                  unsigned int context, X509 *x,  | 
576  |  |                                                  size_t chainidx)  | 
577  | 0  | { | 
578  | 0  |     int currv, min_version, max_version, reason;  | 
579  |  | 
  | 
580  | 0  |     reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);  | 
581  | 0  |     if (reason != 0) { | 
582  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);  | 
583  | 0  |         return EXT_RETURN_FAIL;  | 
584  | 0  |     }  | 
585  |  |  | 
586  |  |     /*  | 
587  |  |      * Don't include this if we can't negotiate TLSv1.3. We can do a straight  | 
588  |  |      * comparison here because we will never be called in DTLS.  | 
589  |  |      */  | 
590  | 0  |     if (max_version < TLS1_3_VERSION)  | 
591  | 0  |         return EXT_RETURN_NOT_SENT;  | 
592  |  |  | 
593  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)  | 
594  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
595  | 0  |             || !WPACKET_start_sub_packet_u8(pkt)) { | 
596  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
597  | 0  |         return EXT_RETURN_FAIL;  | 
598  | 0  |     }  | 
599  |  |  | 
600  | 0  |     for (currv = max_version; currv >= min_version; currv--) { | 
601  | 0  |         if (!WPACKET_put_bytes_u16(pkt, currv)) { | 
602  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
603  | 0  |             return EXT_RETURN_FAIL;  | 
604  | 0  |         }  | 
605  | 0  |     }  | 
606  | 0  |     if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | 
607  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
608  | 0  |         return EXT_RETURN_FAIL;  | 
609  | 0  |     }  | 
610  |  |  | 
611  | 0  |     return EXT_RETURN_SENT;  | 
612  | 0  | }  | 
613  |  |  | 
614  |  | /*  | 
615  |  |  * Construct a psk_kex_modes extension.  | 
616  |  |  */  | 
617  |  | EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL_CONNECTION *s, WPACKET *pkt,  | 
618  |  |                                             unsigned int context, X509 *x,  | 
619  |  |                                             size_t chainidx)  | 
620  | 0  | { | 
621  | 0  | #ifndef OPENSSL_NO_TLS1_3  | 
622  | 0  |     int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX;  | 
623  |  | 
  | 
624  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)  | 
625  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
626  | 0  |             || !WPACKET_start_sub_packet_u8(pkt)  | 
627  | 0  |             || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)  | 
628  | 0  |             || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE))  | 
629  | 0  |             || !WPACKET_close(pkt)  | 
630  | 0  |             || !WPACKET_close(pkt)) { | 
631  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
632  | 0  |         return EXT_RETURN_FAIL;  | 
633  | 0  |     }  | 
634  |  |  | 
635  | 0  |     s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE;  | 
636  | 0  |     if (nodhe)  | 
637  | 0  |         s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;  | 
638  | 0  | #endif  | 
639  |  | 
  | 
640  | 0  |     return EXT_RETURN_SENT;  | 
641  | 0  | }  | 
642  |  |  | 
643  |  | #ifndef OPENSSL_NO_TLS1_3  | 
644  |  | static int add_key_share(SSL_CONNECTION *s, WPACKET *pkt, unsigned int group_id, size_t loop_num)  | 
645  | 0  | { | 
646  | 0  |     unsigned char *encoded_pubkey = NULL;  | 
647  | 0  |     EVP_PKEY *key_share_key = NULL;  | 
648  | 0  |     size_t encodedlen;  | 
649  |  | 
  | 
650  | 0  |     if (loop_num < s->s3.tmp.num_ks_pkey) { | 
651  | 0  |         if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)  | 
652  | 0  |             || !ossl_assert(s->s3.tmp.ks_pkey[loop_num] != NULL)) { | 
653  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
654  | 0  |             return 0;  | 
655  | 0  |         }  | 
656  |  |         /*  | 
657  |  |          * Could happen if we got an HRR that wasn't requesting a new key_share  | 
658  |  |          */  | 
659  | 0  |         key_share_key = s->s3.tmp.ks_pkey[loop_num];  | 
660  | 0  |     } else { | 
661  | 0  |         key_share_key = ssl_generate_pkey_group(s, group_id);  | 
662  | 0  |         if (key_share_key == NULL) { | 
663  |  |             /* SSLfatal() already called */  | 
664  | 0  |             return 0;  | 
665  | 0  |         }  | 
666  | 0  |     }  | 
667  |  |  | 
668  |  |     /* Encode the public key. */  | 
669  | 0  |     encodedlen = EVP_PKEY_get1_encoded_public_key(key_share_key,  | 
670  | 0  |                                                   &encoded_pubkey);  | 
671  | 0  |     if (encodedlen == 0) { | 
672  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);  | 
673  | 0  |         goto err;  | 
674  | 0  |     }  | 
675  |  |  | 
676  |  |     /* Create KeyShareEntry */  | 
677  | 0  |     if (!WPACKET_put_bytes_u16(pkt, group_id)  | 
678  | 0  |             || !WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encodedlen)) { | 
679  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
680  | 0  |         goto err;  | 
681  | 0  |     }  | 
682  |  |  | 
683  |  |     /* For backward compatibility, we use the first valid group to add a key share */  | 
684  | 0  |     if (loop_num == 0) { | 
685  | 0  |         s->s3.tmp.pkey = key_share_key;  | 
686  | 0  |         s->s3.group_id = group_id;  | 
687  | 0  |     }  | 
688  |  |     /* We ensure in t1_lib.c that the loop number does not exceed OPENSSL_CLIENT_MAX_KEY_SHARES */  | 
689  | 0  |     s->s3.tmp.ks_pkey[loop_num] = key_share_key;  | 
690  | 0  |     s->s3.tmp.ks_group_id[loop_num] = group_id;  | 
691  | 0  |     if (loop_num >= s->s3.tmp.num_ks_pkey)  | 
692  | 0  |         s->s3.tmp.num_ks_pkey++;  | 
693  |  | 
  | 
694  | 0  |     OPENSSL_free(encoded_pubkey);  | 
695  |  | 
  | 
696  | 0  |     return 1;  | 
697  | 0  |  err:  | 
698  | 0  |     if (key_share_key != s->s3.tmp.ks_pkey[loop_num])  | 
699  | 0  |         EVP_PKEY_free(key_share_key);  | 
700  | 0  |     OPENSSL_free(encoded_pubkey);  | 
701  | 0  |     return 0;  | 
702  | 0  | }  | 
703  |  | #endif  | 
704  |  |  | 
705  |  | EXT_RETURN tls_construct_ctos_key_share(SSL_CONNECTION *s, WPACKET *pkt,  | 
706  |  |                                         unsigned int context, X509 *x,  | 
707  |  |                                         size_t chainidx)  | 
708  | 0  | { | 
709  | 0  | #ifndef OPENSSL_NO_TLS1_3  | 
710  | 0  |     size_t i, num_groups = 0;  | 
711  | 0  |     const uint16_t *pgroups = NULL;  | 
712  | 0  |     uint16_t group_id = 0;  | 
713  | 0  |     int add_only_one = 0;  | 
714  | 0  |     size_t valid_keyshare = 0;  | 
715  |  |  | 
716  |  |     /* key_share extension */  | 
717  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)  | 
718  |  |             /* Extension data sub-packet */  | 
719  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
720  |  |             /* KeyShare list sub-packet */  | 
721  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)) { | 
722  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
723  | 0  |         return EXT_RETURN_FAIL;  | 
724  | 0  |     }  | 
725  |  |  | 
726  | 0  |     tls1_get_requested_keyshare_groups(s, &pgroups, &num_groups);  | 
727  | 0  |     if (num_groups == 1 && pgroups[0] == 0) { /* Indication that no * prefix was used */ | 
728  | 0  |         tls1_get_supported_groups(s, &pgroups, &num_groups);  | 
729  | 0  |         add_only_one = 1;  | 
730  | 0  |     }  | 
731  |  |  | 
732  |  |     /* If neither the default nor the keyshares have any entry --> fatal */  | 
733  | 0  |     if (num_groups == 0) { | 
734  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE);  | 
735  | 0  |         return EXT_RETURN_FAIL;  | 
736  | 0  |     }  | 
737  |  |  | 
738  |  |     /* Add key shares */  | 
739  |  |  | 
740  | 0  |     if (s->s3.group_id != 0 && s->s3.tmp.pkey == NULL) { | 
741  |  |         /* new, single key share */  | 
742  | 0  |         group_id = s->s3.group_id;  | 
743  | 0  |         s->s3.tmp.num_ks_pkey = 0;  | 
744  | 0  |         if (!add_key_share(s, pkt, group_id, 0)) { | 
745  |  |             /* SSLfatal() already called */  | 
746  | 0  |             return EXT_RETURN_FAIL;  | 
747  | 0  |         }  | 
748  | 0  |         valid_keyshare++;  | 
749  | 0  |     } else { | 
750  | 0  |         if (s->ext.supportedgroups == NULL) /* use default */  | 
751  | 0  |             add_only_one = 1;  | 
752  |  | 
  | 
753  | 0  |         for (i = 0; i < num_groups; i++) { | 
754  | 0  |             if (!tls_group_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED))  | 
755  | 0  |                 continue;  | 
756  | 0  |             if (!tls_valid_group(s, pgroups[i], TLS1_3_VERSION, TLS1_3_VERSION,  | 
757  | 0  |                                  0, NULL))  | 
758  | 0  |                 continue;  | 
759  |  |  | 
760  | 0  |             group_id = pgroups[i];  | 
761  |  | 
  | 
762  | 0  |             if (group_id == 0) { | 
763  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE);  | 
764  | 0  |                 return EXT_RETURN_FAIL;  | 
765  | 0  |             }  | 
766  | 0  |             if (!add_key_share(s, pkt, group_id, valid_keyshare)) { | 
767  |  |                 /* SSLfatal() already called */  | 
768  | 0  |                 return EXT_RETURN_FAIL;  | 
769  | 0  |             }  | 
770  | 0  |             valid_keyshare++;  | 
771  | 0  |             if (add_only_one)  | 
772  | 0  |                 break;  | 
773  | 0  |         }  | 
774  | 0  |     }  | 
775  |  |  | 
776  | 0  |     if (valid_keyshare == 0) { | 
777  |  |         /* No key shares were allowed */  | 
778  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE);  | 
779  | 0  |         return EXT_RETURN_FAIL;  | 
780  | 0  |     }  | 
781  |  |  | 
782  | 0  |     if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | 
783  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
784  | 0  |         return EXT_RETURN_FAIL;  | 
785  | 0  |     }  | 
786  | 0  |     return EXT_RETURN_SENT;  | 
787  |  | #else  | 
788  |  |     return EXT_RETURN_NOT_SENT;  | 
789  |  | #endif  | 
790  | 0  | }  | 
791  |  |  | 
792  |  | EXT_RETURN tls_construct_ctos_cookie(SSL_CONNECTION *s, WPACKET *pkt,  | 
793  |  |                                      unsigned int context,  | 
794  |  |                                      X509 *x, size_t chainidx)  | 
795  | 0  | { | 
796  | 0  |     EXT_RETURN ret = EXT_RETURN_FAIL;  | 
797  |  |  | 
798  |  |     /* Should only be set if we've had an HRR */  | 
799  | 0  |     if (s->ext.tls13_cookie_len == 0)  | 
800  | 0  |         return EXT_RETURN_NOT_SENT;  | 
801  |  |  | 
802  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)  | 
803  |  |                /* Extension data sub-packet */  | 
804  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
805  | 0  |             || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie,  | 
806  | 0  |                                        s->ext.tls13_cookie_len)  | 
807  | 0  |             || !WPACKET_close(pkt)) { | 
808  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
809  | 0  |         goto end;  | 
810  | 0  |     }  | 
811  |  |  | 
812  | 0  |     ret = EXT_RETURN_SENT;  | 
813  | 0  |  end:  | 
814  | 0  |     OPENSSL_free(s->ext.tls13_cookie);  | 
815  | 0  |     s->ext.tls13_cookie = NULL;  | 
816  | 0  |     s->ext.tls13_cookie_len = 0;  | 
817  |  | 
  | 
818  | 0  |     return ret;  | 
819  | 0  | }  | 
820  |  |  | 
821  |  | EXT_RETURN tls_construct_ctos_early_data(SSL_CONNECTION *s, WPACKET *pkt,  | 
822  |  |                                          unsigned int context, X509 *x,  | 
823  |  |                                          size_t chainidx)  | 
824  | 0  | { | 
825  | 0  | #ifndef OPENSSL_NO_PSK  | 
826  | 0  |     char identity[PSK_MAX_IDENTITY_LEN + 1];  | 
827  | 0  | #endif  /* OPENSSL_NO_PSK */  | 
828  | 0  |     const unsigned char *id = NULL;  | 
829  | 0  |     size_t idlen = 0;  | 
830  | 0  |     SSL_SESSION *psksess = NULL;  | 
831  | 0  |     SSL_SESSION *edsess = NULL;  | 
832  | 0  |     const EVP_MD *handmd = NULL;  | 
833  | 0  |     SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);  | 
834  |  | 
  | 
835  | 0  |     if (s->hello_retry_request == SSL_HRR_PENDING)  | 
836  | 0  |         handmd = ssl_handshake_md(s);  | 
837  |  | 
  | 
838  | 0  |     if (s->psk_use_session_cb != NULL  | 
839  | 0  |             && (!s->psk_use_session_cb(ussl, handmd, &id, &idlen, &psksess)  | 
840  | 0  |                 || (psksess != NULL  | 
841  | 0  |                     && psksess->ssl_version != TLS1_3_VERSION))) { | 
842  | 0  |         SSL_SESSION_free(psksess);  | 
843  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);  | 
844  | 0  |         return EXT_RETURN_FAIL;  | 
845  | 0  |     }  | 
846  |  |  | 
847  | 0  | #ifndef OPENSSL_NO_PSK  | 
848  | 0  |     if (psksess == NULL && s->psk_client_callback != NULL) { | 
849  | 0  |         unsigned char psk[PSK_MAX_PSK_LEN];  | 
850  | 0  |         size_t psklen = 0;  | 
851  |  | 
  | 
852  | 0  |         memset(identity, 0, sizeof(identity));  | 
853  | 0  |         psklen = s->psk_client_callback(ussl, NULL,  | 
854  | 0  |                                         identity, sizeof(identity) - 1,  | 
855  | 0  |                                         psk, sizeof(psk));  | 
856  |  | 
  | 
857  | 0  |         if (psklen > PSK_MAX_PSK_LEN) { | 
858  | 0  |             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);  | 
859  | 0  |             return EXT_RETURN_FAIL;  | 
860  | 0  |         } else if (psklen > 0) { | 
861  | 0  |             const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 }; | 
862  | 0  |             const SSL_CIPHER *cipher;  | 
863  |  | 
  | 
864  | 0  |             idlen = strlen(identity);  | 
865  | 0  |             if (idlen > PSK_MAX_IDENTITY_LEN) { | 
866  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
867  | 0  |                 return EXT_RETURN_FAIL;  | 
868  | 0  |             }  | 
869  | 0  |             id = (unsigned char *)identity;  | 
870  |  |  | 
871  |  |             /*  | 
872  |  |              * We found a PSK using an old style callback. We don't know  | 
873  |  |              * the digest so we default to SHA256 as per the TLSv1.3 spec  | 
874  |  |              */  | 
875  | 0  |             cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s),  | 
876  | 0  |                                      tls13_aes128gcmsha256_id);  | 
877  | 0  |             if (cipher == NULL) { | 
878  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
879  | 0  |                 return EXT_RETURN_FAIL;  | 
880  | 0  |             }  | 
881  |  |  | 
882  | 0  |             psksess = SSL_SESSION_new();  | 
883  | 0  |             if (psksess == NULL  | 
884  | 0  |                     || !SSL_SESSION_set1_master_key(psksess, psk, psklen)  | 
885  | 0  |                     || !SSL_SESSION_set_cipher(psksess, cipher)  | 
886  | 0  |                     || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) { | 
887  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
888  | 0  |                 OPENSSL_cleanse(psk, psklen);  | 
889  | 0  |                 return EXT_RETURN_FAIL;  | 
890  | 0  |             }  | 
891  | 0  |             OPENSSL_cleanse(psk, psklen);  | 
892  | 0  |         }  | 
893  | 0  |     }  | 
894  | 0  | #endif  /* OPENSSL_NO_PSK */  | 
895  |  |  | 
896  | 0  |     SSL_SESSION_free(s->psksession);  | 
897  | 0  |     s->psksession = psksess;  | 
898  | 0  |     if (psksess != NULL) { | 
899  | 0  |         OPENSSL_free(s->psksession_id);  | 
900  | 0  |         s->psksession_id = OPENSSL_memdup(id, idlen);  | 
901  | 0  |         if (s->psksession_id == NULL) { | 
902  | 0  |             s->psksession_id_len = 0;  | 
903  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
904  | 0  |             return EXT_RETURN_FAIL;  | 
905  | 0  |         }  | 
906  | 0  |         s->psksession_id_len = idlen;  | 
907  | 0  |     }  | 
908  |  |  | 
909  | 0  |     if (s->early_data_state != SSL_EARLY_DATA_CONNECTING  | 
910  | 0  |             || (s->session->ext.max_early_data == 0  | 
911  | 0  |                 && (psksess == NULL || psksess->ext.max_early_data == 0))) { | 
912  | 0  |         s->max_early_data = 0;  | 
913  | 0  |         return EXT_RETURN_NOT_SENT;  | 
914  | 0  |     }  | 
915  | 0  |     edsess = s->session->ext.max_early_data != 0 ? s->session : psksess;  | 
916  | 0  |     s->max_early_data = edsess->ext.max_early_data;  | 
917  |  | 
  | 
918  | 0  |     if (edsess->ext.hostname != NULL) { | 
919  | 0  |         if (s->ext.hostname == NULL  | 
920  | 0  |                 || (s->ext.hostname != NULL  | 
921  | 0  |                     && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) { | 
922  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR,  | 
923  | 0  |                      SSL_R_INCONSISTENT_EARLY_DATA_SNI);  | 
924  | 0  |             return EXT_RETURN_FAIL;  | 
925  | 0  |         }  | 
926  | 0  |     }  | 
927  |  |  | 
928  | 0  |     if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) { | 
929  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_INCONSISTENT_EARLY_DATA_ALPN);  | 
930  | 0  |         return EXT_RETURN_FAIL;  | 
931  | 0  |     }  | 
932  |  |  | 
933  |  |     /*  | 
934  |  |      * Verify that we are offering an ALPN protocol consistent with the early  | 
935  |  |      * data.  | 
936  |  |      */  | 
937  | 0  |     if (edsess->ext.alpn_selected != NULL) { | 
938  | 0  |         PACKET prots, alpnpkt;  | 
939  | 0  |         int found = 0;  | 
940  |  | 
  | 
941  | 0  |         if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) { | 
942  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
943  | 0  |             return EXT_RETURN_FAIL;  | 
944  | 0  |         }  | 
945  | 0  |         while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) { | 
946  | 0  |             if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected,  | 
947  | 0  |                              edsess->ext.alpn_selected_len)) { | 
948  | 0  |                 found = 1;  | 
949  | 0  |                 break;  | 
950  | 0  |             }  | 
951  | 0  |         }  | 
952  | 0  |         if (!found) { | 
953  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR,  | 
954  | 0  |                      SSL_R_INCONSISTENT_EARLY_DATA_ALPN);  | 
955  | 0  |             return EXT_RETURN_FAIL;  | 
956  | 0  |         }  | 
957  | 0  |     }  | 
958  |  |  | 
959  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)  | 
960  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
961  | 0  |             || !WPACKET_close(pkt)) { | 
962  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
963  | 0  |         return EXT_RETURN_FAIL;  | 
964  | 0  |     }  | 
965  |  |  | 
966  |  |     /*  | 
967  |  |      * We set this to rejected here. Later, if the server acknowledges the  | 
968  |  |      * extension, we set it to accepted.  | 
969  |  |      */  | 
970  | 0  |     s->ext.early_data = SSL_EARLY_DATA_REJECTED;  | 
971  | 0  |     s->ext.early_data_ok = 1;  | 
972  |  | 
  | 
973  | 0  |     return EXT_RETURN_SENT;  | 
974  | 0  | }  | 
975  |  |  | 
976  | 0  | #define F5_WORKAROUND_MIN_MSG_LEN   0xff  | 
977  | 0  | #define F5_WORKAROUND_MAX_MSG_LEN   0x200  | 
978  |  |  | 
979  |  | /*  | 
980  |  |  * PSK pre binder overhead =  | 
981  |  |  *  2 bytes for TLSEXT_TYPE_psk  | 
982  |  |  *  2 bytes for extension length  | 
983  |  |  *  2 bytes for identities list length  | 
984  |  |  *  2 bytes for identity length  | 
985  |  |  *  4 bytes for obfuscated_ticket_age  | 
986  |  |  *  2 bytes for binder list length  | 
987  |  |  *  1 byte for binder length  | 
988  |  |  * The above excludes the number of bytes for the identity itself and the  | 
989  |  |  * subsequent binder bytes  | 
990  |  |  */  | 
991  | 0  | #define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1)  | 
992  |  |  | 
993  |  | EXT_RETURN tls_construct_ctos_padding(SSL_CONNECTION *s, WPACKET *pkt,  | 
994  |  |                                       unsigned int context, X509 *x,  | 
995  |  |                                       size_t chainidx)  | 
996  | 0  | { | 
997  | 0  |     unsigned char *padbytes;  | 
998  | 0  |     size_t hlen;  | 
999  |  | 
  | 
1000  | 0  |     if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)  | 
1001  | 0  |         return EXT_RETURN_NOT_SENT;  | 
1002  |  |  | 
1003  |  |     /*  | 
1004  |  |      * Add padding to workaround bugs in F5 terminators. See RFC7685.  | 
1005  |  |      * This code calculates the length of all extensions added so far but  | 
1006  |  |      * excludes the PSK extension (because that MUST be written last). Therefore  | 
1007  |  |      * this extension MUST always appear second to last.  | 
1008  |  |      */  | 
1009  | 0  |     if (!WPACKET_get_total_written(pkt, &hlen)) { | 
1010  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1011  | 0  |         return EXT_RETURN_FAIL;  | 
1012  | 0  |     }  | 
1013  |  |  | 
1014  |  |     /*  | 
1015  |  |      * If we're going to send a PSK then that will be written out after this  | 
1016  |  |      * extension, so we need to calculate how long it is going to be.  | 
1017  |  |      */  | 
1018  | 0  |     if (s->session->ssl_version == TLS1_3_VERSION  | 
1019  | 0  |             && s->session->ext.ticklen != 0  | 
1020  | 0  |             && s->session->cipher != NULL) { | 
1021  | 0  |         const EVP_MD *md = ssl_md(SSL_CONNECTION_GET_CTX(s),  | 
1022  | 0  |                                   s->session->cipher->algorithm2);  | 
1023  |  | 
  | 
1024  | 0  |         if (md != NULL) { | 
1025  |  |             /*  | 
1026  |  |              * Add the fixed PSK overhead, the identity length and the binder  | 
1027  |  |              * length.  | 
1028  |  |              */  | 
1029  | 0  |             int md_size = EVP_MD_get_size(md);  | 
1030  |  | 
  | 
1031  | 0  |             if (md_size <= 0)  | 
1032  | 0  |                 return EXT_RETURN_FAIL;  | 
1033  | 0  |             hlen +=  PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen  | 
1034  | 0  |                      + md_size;  | 
1035  | 0  |         }  | 
1036  | 0  |     }  | 
1037  |  |  | 
1038  | 0  |     if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) { | 
1039  |  |         /* Calculate the amount of padding we need to add */  | 
1040  | 0  |         hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;  | 
1041  |  |  | 
1042  |  |         /*  | 
1043  |  |          * Take off the size of extension header itself (2 bytes for type and  | 
1044  |  |          * 2 bytes for length bytes), but ensure that the extension is at least  | 
1045  |  |          * 1 byte long so as not to have an empty extension last (WebSphere 7.x,  | 
1046  |  |          * 8.x are intolerant of that condition)  | 
1047  |  |          */  | 
1048  | 0  |         if (hlen > 4)  | 
1049  | 0  |             hlen -= 4;  | 
1050  | 0  |         else  | 
1051  | 0  |             hlen = 1;  | 
1052  |  | 
  | 
1053  | 0  |         if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)  | 
1054  | 0  |                 || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) { | 
1055  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1056  | 0  |             return EXT_RETURN_FAIL;  | 
1057  | 0  |         }  | 
1058  | 0  |         memset(padbytes, 0, hlen);  | 
1059  | 0  |     }  | 
1060  |  |  | 
1061  | 0  |     return EXT_RETURN_SENT;  | 
1062  | 0  | }  | 
1063  |  |  | 
1064  |  | /*  | 
1065  |  |  * Construct the pre_shared_key extension  | 
1066  |  |  */  | 
1067  |  | EXT_RETURN tls_construct_ctos_psk(SSL_CONNECTION *s, WPACKET *pkt,  | 
1068  |  |                                   unsigned int context,  | 
1069  |  |                                   X509 *x, size_t chainidx)  | 
1070  | 0  | { | 
1071  | 0  | #ifndef OPENSSL_NO_TLS1_3  | 
1072  | 0  |     uint32_t agesec, agems = 0;  | 
1073  | 0  |     size_t binderoffset, msglen;  | 
1074  | 0  |     int reshashsize = 0, pskhashsize = 0;  | 
1075  | 0  |     unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL;  | 
1076  | 0  |     const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL;  | 
1077  | 0  |     int dores = 0;  | 
1078  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
1079  | 0  |     OSSL_TIME t;  | 
1080  |  | 
  | 
1081  | 0  |     s->ext.tick_identity = 0;  | 
1082  |  |  | 
1083  |  |     /*  | 
1084  |  |      * Note: At this stage of the code we only support adding a single  | 
1085  |  |      * resumption PSK. If we add support for multiple PSKs then the length  | 
1086  |  |      * calculations in the padding extension will need to be adjusted.  | 
1087  |  |      */  | 
1088  |  |  | 
1089  |  |     /*  | 
1090  |  |      * If this is an incompatible or new session then we have nothing to resume  | 
1091  |  |      * so don't add this extension.  | 
1092  |  |      */  | 
1093  | 0  |     if (s->session->ssl_version != TLS1_3_VERSION  | 
1094  | 0  |             || (s->session->ext.ticklen == 0 && s->psksession == NULL))  | 
1095  | 0  |         return EXT_RETURN_NOT_SENT;  | 
1096  |  |  | 
1097  | 0  |     if (s->hello_retry_request == SSL_HRR_PENDING)  | 
1098  | 0  |         handmd = ssl_handshake_md(s);  | 
1099  |  | 
  | 
1100  | 0  |     if (s->session->ext.ticklen != 0) { | 
1101  |  |         /* Get the digest associated with the ciphersuite in the session */  | 
1102  | 0  |         if (s->session->cipher == NULL) { | 
1103  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1104  | 0  |             return EXT_RETURN_FAIL;  | 
1105  | 0  |         }  | 
1106  | 0  |         mdres = ssl_md(sctx, s->session->cipher->algorithm2);  | 
1107  | 0  |         if (mdres == NULL) { | 
1108  |  |             /*  | 
1109  |  |              * Don't recognize this cipher so we can't use the session.  | 
1110  |  |              * Ignore it  | 
1111  |  |              */  | 
1112  | 0  |             goto dopsksess;  | 
1113  | 0  |         }  | 
1114  |  |  | 
1115  | 0  |         if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) { | 
1116  |  |             /*  | 
1117  |  |              * Selected ciphersuite hash does not match the hash for the session  | 
1118  |  |              * so we can't use it.  | 
1119  |  |              */  | 
1120  | 0  |             goto dopsksess;  | 
1121  | 0  |         }  | 
1122  |  |  | 
1123  |  |         /*  | 
1124  |  |          * Technically the C standard just says time() returns a time_t and says  | 
1125  |  |          * nothing about the encoding of that type. In practice most  | 
1126  |  |          * implementations follow POSIX which holds it as an integral type in  | 
1127  |  |          * seconds since epoch. We've already made the assumption that we can do  | 
1128  |  |          * this in multiple places in the code, so portability shouldn't be an  | 
1129  |  |          * issue.  | 
1130  |  |          */  | 
1131  | 0  |         t = ossl_time_subtract(ossl_time_now(), s->session->time);  | 
1132  | 0  |         agesec = (uint32_t)ossl_time2seconds(t);  | 
1133  |  |         /*  | 
1134  |  |          * We calculate the age in seconds but the server may work in ms. Due to  | 
1135  |  |          * rounding errors we could overestimate the age by up to 1s. It is  | 
1136  |  |          * better to underestimate it. Otherwise, if the RTT is very short, when  | 
1137  |  |          * the server calculates the age reported by the client it could be  | 
1138  |  |          * bigger than the age calculated on the server - which should never  | 
1139  |  |          * happen.  | 
1140  |  |          */  | 
1141  | 0  |         if (agesec > 0)  | 
1142  | 0  |             agesec--;  | 
1143  |  | 
  | 
1144  | 0  |         if (s->session->ext.tick_lifetime_hint < agesec) { | 
1145  |  |             /* Ticket is too old. Ignore it. */  | 
1146  | 0  |             goto dopsksess;  | 
1147  | 0  |         }  | 
1148  |  |  | 
1149  |  |         /*  | 
1150  |  |          * Calculate age in ms. We're just doing it to nearest second. Should be  | 
1151  |  |          * good enough.  | 
1152  |  |          */  | 
1153  | 0  |         agems = agesec * (uint32_t)1000;  | 
1154  |  | 
  | 
1155  | 0  |         if (agesec != 0 && agems / (uint32_t)1000 != agesec) { | 
1156  |  |             /*  | 
1157  |  |              * Overflow. Shouldn't happen unless this is a *really* old session.  | 
1158  |  |              * If so we just ignore it.  | 
1159  |  |              */  | 
1160  | 0  |             goto dopsksess;  | 
1161  | 0  |         }  | 
1162  |  |  | 
1163  |  |         /*  | 
1164  |  |          * Obfuscate the age. Overflow here is fine, this addition is supposed  | 
1165  |  |          * to be mod 2^32.  | 
1166  |  |          */  | 
1167  | 0  |         agems += s->session->ext.tick_age_add;  | 
1168  |  | 
  | 
1169  | 0  |         reshashsize = EVP_MD_get_size(mdres);  | 
1170  | 0  |         if (reshashsize <= 0)  | 
1171  | 0  |             goto dopsksess;  | 
1172  | 0  |         s->ext.tick_identity++;  | 
1173  | 0  |         dores = 1;  | 
1174  | 0  |     }  | 
1175  |  |  | 
1176  | 0  |  dopsksess:  | 
1177  | 0  |     if (!dores && s->psksession == NULL)  | 
1178  | 0  |         return EXT_RETURN_NOT_SENT;  | 
1179  |  |  | 
1180  | 0  |     if (s->psksession != NULL) { | 
1181  | 0  |         mdpsk = ssl_md(sctx, s->psksession->cipher->algorithm2);  | 
1182  | 0  |         if (mdpsk == NULL) { | 
1183  |  |             /*  | 
1184  |  |              * Don't recognize this cipher so we can't use the session.  | 
1185  |  |              * If this happens it's an application bug.  | 
1186  |  |              */  | 
1187  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);  | 
1188  | 0  |             return EXT_RETURN_FAIL;  | 
1189  | 0  |         }  | 
1190  |  |  | 
1191  | 0  |         if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) { | 
1192  |  |             /*  | 
1193  |  |              * Selected ciphersuite hash does not match the hash for the PSK  | 
1194  |  |              * session. This is an application bug.  | 
1195  |  |              */  | 
1196  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);  | 
1197  | 0  |             return EXT_RETURN_FAIL;  | 
1198  | 0  |         }  | 
1199  |  |  | 
1200  | 0  |         pskhashsize = EVP_MD_get_size(mdpsk);  | 
1201  | 0  |         if (pskhashsize <= 0) { | 
1202  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);  | 
1203  | 0  |             return EXT_RETURN_FAIL;  | 
1204  | 0  |         }  | 
1205  | 0  |     }  | 
1206  |  |  | 
1207  |  |     /* Create the extension, but skip over the binder for now */  | 
1208  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)  | 
1209  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1210  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)) { | 
1211  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1212  | 0  |         return EXT_RETURN_FAIL;  | 
1213  | 0  |     }  | 
1214  |  |  | 
1215  | 0  |     if (dores) { | 
1216  | 0  |         if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,  | 
1217  | 0  |                                            s->session->ext.ticklen)  | 
1218  | 0  |                 || !WPACKET_put_bytes_u32(pkt, agems)) { | 
1219  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1220  | 0  |             return EXT_RETURN_FAIL;  | 
1221  | 0  |         }  | 
1222  | 0  |     }  | 
1223  |  |  | 
1224  | 0  |     if (s->psksession != NULL) { | 
1225  | 0  |         if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id,  | 
1226  | 0  |                                     s->psksession_id_len)  | 
1227  | 0  |                 || !WPACKET_put_bytes_u32(pkt, 0)) { | 
1228  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1229  | 0  |             return EXT_RETURN_FAIL;  | 
1230  | 0  |         }  | 
1231  | 0  |         s->ext.tick_identity++;  | 
1232  | 0  |     }  | 
1233  |  |  | 
1234  | 0  |     if (!WPACKET_close(pkt)  | 
1235  | 0  |             || !WPACKET_get_total_written(pkt, &binderoffset)  | 
1236  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1237  | 0  |             || (dores  | 
1238  | 0  |                 && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder))  | 
1239  | 0  |             || (s->psksession != NULL  | 
1240  | 0  |                 && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder))  | 
1241  | 0  |             || !WPACKET_close(pkt)  | 
1242  | 0  |             || !WPACKET_close(pkt)  | 
1243  | 0  |             || !WPACKET_get_total_written(pkt, &msglen)  | 
1244  |  |                /*  | 
1245  |  |                 * We need to fill in all the sub-packet lengths now so we can  | 
1246  |  |                 * calculate the HMAC of the message up to the binders  | 
1247  |  |                 */  | 
1248  | 0  |             || !WPACKET_fill_lengths(pkt)) { | 
1249  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1250  | 0  |         return EXT_RETURN_FAIL;  | 
1251  | 0  |     }  | 
1252  |  |  | 
1253  | 0  |     msgstart = WPACKET_get_curr(pkt) - msglen;  | 
1254  |  | 
  | 
1255  | 0  |     if (dores  | 
1256  | 0  |             && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL,  | 
1257  | 0  |                                  resbinder, s->session, 1, 0) != 1) { | 
1258  |  |         /* SSLfatal() already called */  | 
1259  | 0  |         return EXT_RETURN_FAIL;  | 
1260  | 0  |     }  | 
1261  |  |  | 
1262  | 0  |     if (s->psksession != NULL  | 
1263  | 0  |             && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL,  | 
1264  | 0  |                                  pskbinder, s->psksession, 1, 1) != 1) { | 
1265  |  |         /* SSLfatal() already called */  | 
1266  | 0  |         return EXT_RETURN_FAIL;  | 
1267  | 0  |     }  | 
1268  |  |  | 
1269  | 0  |     return EXT_RETURN_SENT;  | 
1270  |  | #else  | 
1271  |  |     return EXT_RETURN_NOT_SENT;  | 
1272  |  | #endif  | 
1273  | 0  | }  | 
1274  |  |  | 
1275  |  | EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL_CONNECTION *s, WPACKET *pkt,  | 
1276  |  |                                                   ossl_unused unsigned int context,  | 
1277  |  |                                                   ossl_unused X509 *x,  | 
1278  |  |                                                   ossl_unused size_t chainidx)  | 
1279  | 0  | { | 
1280  | 0  | #ifndef OPENSSL_NO_TLS1_3  | 
1281  | 0  |     if (!s->pha_enabled)  | 
1282  | 0  |         return EXT_RETURN_NOT_SENT;  | 
1283  |  |  | 
1284  |  |     /* construct extension - 0 length, no contents */  | 
1285  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth)  | 
1286  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
1287  | 0  |             || !WPACKET_close(pkt)) { | 
1288  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1289  | 0  |         return EXT_RETURN_FAIL;  | 
1290  | 0  |     }  | 
1291  |  |  | 
1292  | 0  |     s->post_handshake_auth = SSL_PHA_EXT_SENT;  | 
1293  |  | 
  | 
1294  | 0  |     return EXT_RETURN_SENT;  | 
1295  |  | #else  | 
1296  |  |     return EXT_RETURN_NOT_SENT;  | 
1297  |  | #endif  | 
1298  | 0  | }  | 
1299  |  |  | 
1300  |  |  | 
1301  |  | /*  | 
1302  |  |  * Parse the server's renegotiation binding and abort if it's not right  | 
1303  |  |  */  | 
1304  |  | int tls_parse_stoc_renegotiate(SSL_CONNECTION *s, PACKET *pkt,  | 
1305  |  |                                unsigned int context,  | 
1306  |  |                                X509 *x, size_t chainidx)  | 
1307  | 0  | { | 
1308  | 0  |     size_t expected_len = s->s3.previous_client_finished_len  | 
1309  | 0  |         + s->s3.previous_server_finished_len;  | 
1310  | 0  |     size_t ilen;  | 
1311  | 0  |     const unsigned char *data;  | 
1312  |  |  | 
1313  |  |     /* Check for logic errors */  | 
1314  | 0  |     if (!ossl_assert(expected_len == 0  | 
1315  | 0  |                      || s->s3.previous_client_finished_len != 0)  | 
1316  | 0  |         || !ossl_assert(expected_len == 0  | 
1317  | 0  |                         || s->s3.previous_server_finished_len != 0)) { | 
1318  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1319  | 0  |         return 0;  | 
1320  | 0  |     }  | 
1321  |  |  | 
1322  |  |     /* Parse the length byte */  | 
1323  | 0  |     if (!PACKET_get_1_len(pkt, &ilen)) { | 
1324  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);  | 
1325  | 0  |         return 0;  | 
1326  | 0  |     }  | 
1327  |  |  | 
1328  |  |     /* Consistency check */  | 
1329  | 0  |     if (PACKET_remaining(pkt) != ilen) { | 
1330  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);  | 
1331  | 0  |         return 0;  | 
1332  | 0  |     }  | 
1333  |  |  | 
1334  |  |     /* Check that the extension matches */  | 
1335  | 0  |     if (ilen != expected_len) { | 
1336  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);  | 
1337  | 0  |         return 0;  | 
1338  | 0  |     }  | 
1339  |  |  | 
1340  | 0  |     if (!PACKET_get_bytes(pkt, &data, s->s3.previous_client_finished_len)  | 
1341  | 0  |         || memcmp(data, s->s3.previous_client_finished,  | 
1342  | 0  |                   s->s3.previous_client_finished_len) != 0) { | 
1343  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);  | 
1344  | 0  |         return 0;  | 
1345  | 0  |     }  | 
1346  |  |  | 
1347  | 0  |     if (!PACKET_get_bytes(pkt, &data, s->s3.previous_server_finished_len)  | 
1348  | 0  |         || memcmp(data, s->s3.previous_server_finished,  | 
1349  | 0  |                   s->s3.previous_server_finished_len) != 0) { | 
1350  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);  | 
1351  | 0  |         return 0;  | 
1352  | 0  |     }  | 
1353  | 0  |     s->s3.send_connection_binding = 1;  | 
1354  |  | 
  | 
1355  | 0  |     return 1;  | 
1356  | 0  | }  | 
1357  |  |  | 
1358  |  | /* Parse the server's max fragment len extension packet */  | 
1359  |  | int tls_parse_stoc_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,  | 
1360  |  |                                   unsigned int context,  | 
1361  |  |                                   X509 *x, size_t chainidx)  | 
1362  | 0  | { | 
1363  | 0  |     unsigned int value;  | 
1364  |  | 
  | 
1365  | 0  |     if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) { | 
1366  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1367  | 0  |         return 0;  | 
1368  | 0  |     }  | 
1369  |  |  | 
1370  |  |     /* |value| should contains a valid max-fragment-length code. */  | 
1371  | 0  |     if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) { | 
1372  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,  | 
1373  | 0  |                  SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);  | 
1374  | 0  |         return 0;  | 
1375  | 0  |     }  | 
1376  |  |  | 
1377  |  |     /* Must be the same value as client-configured one who was sent to server */  | 
1378  |  |     /*-  | 
1379  |  |      * RFC 6066: if a client receives a maximum fragment length negotiation  | 
1380  |  |      * response that differs from the length it requested, ...  | 
1381  |  |      * It must abort with SSL_AD_ILLEGAL_PARAMETER alert  | 
1382  |  |      */  | 
1383  | 0  |     if (value != s->ext.max_fragment_len_mode) { | 
1384  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,  | 
1385  | 0  |                  SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);  | 
1386  | 0  |         return 0;  | 
1387  | 0  |     }  | 
1388  |  |  | 
1389  |  |     /*  | 
1390  |  |      * Maximum Fragment Length Negotiation succeeded.  | 
1391  |  |      * The negotiated Maximum Fragment Length is binding now.  | 
1392  |  |      */  | 
1393  | 0  |     s->session->ext.max_fragment_len_mode = value;  | 
1394  |  | 
  | 
1395  | 0  |     return 1;  | 
1396  | 0  | }  | 
1397  |  |  | 
1398  |  | int tls_parse_stoc_server_name(SSL_CONNECTION *s, PACKET *pkt,  | 
1399  |  |                                unsigned int context,  | 
1400  |  |                                X509 *x, size_t chainidx)  | 
1401  | 0  | { | 
1402  | 0  |     if (s->ext.hostname == NULL) { | 
1403  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1404  | 0  |         return 0;  | 
1405  | 0  |     }  | 
1406  |  |  | 
1407  | 0  |     if (PACKET_remaining(pkt) > 0) { | 
1408  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1409  | 0  |         return 0;  | 
1410  | 0  |     }  | 
1411  |  |  | 
1412  | 0  |     if (!s->hit) { | 
1413  | 0  |         if (s->session->ext.hostname != NULL) { | 
1414  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1415  | 0  |             return 0;  | 
1416  | 0  |         }  | 
1417  | 0  |         s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);  | 
1418  | 0  |         if (s->session->ext.hostname == NULL) { | 
1419  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1420  | 0  |             return 0;  | 
1421  | 0  |         }  | 
1422  | 0  |     }  | 
1423  |  |  | 
1424  | 0  |     return 1;  | 
1425  | 0  | }  | 
1426  |  |  | 
1427  |  | int tls_parse_stoc_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt,  | 
1428  |  |                                  unsigned int context,  | 
1429  |  |                                  X509 *x, size_t chainidx)  | 
1430  | 0  | { | 
1431  | 0  |     size_t ecpointformats_len;  | 
1432  | 0  |     PACKET ecptformatlist;  | 
1433  |  | 
  | 
1434  | 0  |     if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) { | 
1435  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1436  | 0  |         return 0;  | 
1437  | 0  |     }  | 
1438  | 0  |     if (!s->hit) { | 
1439  | 0  |         ecpointformats_len = PACKET_remaining(&ecptformatlist);  | 
1440  | 0  |         if (ecpointformats_len == 0) { | 
1441  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);  | 
1442  | 0  |             return 0;  | 
1443  | 0  |         }  | 
1444  |  |  | 
1445  | 0  |         s->ext.peer_ecpointformats_len = 0;  | 
1446  | 0  |         OPENSSL_free(s->ext.peer_ecpointformats);  | 
1447  | 0  |         s->ext.peer_ecpointformats = OPENSSL_malloc(ecpointformats_len);  | 
1448  | 0  |         if (s->ext.peer_ecpointformats == NULL) { | 
1449  | 0  |             s->ext.peer_ecpointformats_len = 0;  | 
1450  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1451  | 0  |             return 0;  | 
1452  | 0  |         }  | 
1453  |  |  | 
1454  | 0  |         s->ext.peer_ecpointformats_len = ecpointformats_len;  | 
1455  |  | 
  | 
1456  | 0  |         if (!PACKET_copy_bytes(&ecptformatlist,  | 
1457  | 0  |                                s->ext.peer_ecpointformats,  | 
1458  | 0  |                                ecpointformats_len)) { | 
1459  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1460  | 0  |             return 0;  | 
1461  | 0  |         }  | 
1462  | 0  |     }  | 
1463  |  |  | 
1464  | 0  |     return 1;  | 
1465  | 0  | }  | 
1466  |  |  | 
1467  |  | int tls_parse_stoc_session_ticket(SSL_CONNECTION *s, PACKET *pkt,  | 
1468  |  |                                   unsigned int context,  | 
1469  |  |                                   X509 *x, size_t chainidx)  | 
1470  | 0  | { | 
1471  | 0  |     SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);  | 
1472  |  | 
  | 
1473  | 0  |     if (s->ext.session_ticket_cb != NULL &&  | 
1474  | 0  |         !s->ext.session_ticket_cb(ssl, PACKET_data(pkt),  | 
1475  | 0  |                                   (int)PACKET_remaining(pkt),  | 
1476  | 0  |                                   s->ext.session_ticket_cb_arg)) { | 
1477  | 0  |         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);  | 
1478  | 0  |         return 0;  | 
1479  | 0  |     }  | 
1480  |  |  | 
1481  | 0  |     if (!tls_use_ticket(s)) { | 
1482  | 0  |         SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);  | 
1483  | 0  |         return 0;  | 
1484  | 0  |     }  | 
1485  | 0  |     if (PACKET_remaining(pkt) > 0) { | 
1486  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1487  | 0  |         return 0;  | 
1488  | 0  |     }  | 
1489  |  |  | 
1490  | 0  |     s->ext.ticket_expected = 1;  | 
1491  |  | 
  | 
1492  | 0  |     return 1;  | 
1493  | 0  | }  | 
1494  |  |  | 
1495  |  | #ifndef OPENSSL_NO_OCSP  | 
1496  |  | int tls_parse_stoc_status_request(SSL_CONNECTION *s, PACKET *pkt,  | 
1497  |  |                                   unsigned int context,  | 
1498  |  |                                   X509 *x, size_t chainidx)  | 
1499  | 0  | { | 
1500  | 0  |     if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) { | 
1501  |  |         /* We ignore this if the server sends a CertificateRequest */  | 
1502  | 0  |         return 1;  | 
1503  | 0  |     }  | 
1504  |  |  | 
1505  |  |     /*  | 
1506  |  |      * MUST only be sent if we've requested a status  | 
1507  |  |      * request message. In TLS <= 1.2 it must also be empty.  | 
1508  |  |      */  | 
1509  | 0  |     if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) { | 
1510  | 0  |         SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);  | 
1511  | 0  |         return 0;  | 
1512  | 0  |     }  | 
1513  | 0  |     if (!SSL_CONNECTION_IS_TLS13(s) && PACKET_remaining(pkt) > 0) { | 
1514  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1515  | 0  |         return 0;  | 
1516  | 0  |     }  | 
1517  |  |  | 
1518  | 0  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
1519  |  |         /* SSLfatal() already called */  | 
1520  | 0  |         return tls_process_cert_status_body(s, chainidx, pkt);  | 
1521  | 0  |     }  | 
1522  |  |  | 
1523  |  |     /* Set flag to expect CertificateStatus message */  | 
1524  | 0  |     s->ext.status_expected = 1;  | 
1525  |  | 
  | 
1526  | 0  |     return 1;  | 
1527  | 0  | }  | 
1528  |  | #endif  | 
1529  |  |  | 
1530  |  |  | 
1531  |  | #ifndef OPENSSL_NO_CT  | 
1532  |  | int tls_parse_stoc_sct(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
1533  |  |                        X509 *x, size_t chainidx)  | 
1534  | 0  | { | 
1535  | 0  |     if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) { | 
1536  |  |         /* We ignore this if the server sends it in a CertificateRequest */  | 
1537  | 0  |         return 1;  | 
1538  | 0  |     }  | 
1539  |  |  | 
1540  |  |     /*  | 
1541  |  |      * Only take it if we asked for it - i.e if there is no CT validation  | 
1542  |  |      * callback set, then a custom extension MAY be processing it, so we  | 
1543  |  |      * need to let control continue to flow to that.  | 
1544  |  |      */  | 
1545  | 0  |     if (s->ct_validation_callback != NULL) { | 
1546  | 0  |         size_t size = PACKET_remaining(pkt);  | 
1547  |  |  | 
1548  |  |         /* Simply copy it off for later processing */  | 
1549  | 0  |         OPENSSL_free(s->ext.scts);  | 
1550  | 0  |         s->ext.scts = NULL;  | 
1551  |  | 
  | 
1552  | 0  |         s->ext.scts_len = (uint16_t)size;  | 
1553  | 0  |         if (size > 0) { | 
1554  | 0  |             s->ext.scts = OPENSSL_malloc(size);  | 
1555  | 0  |             if (s->ext.scts == NULL) { | 
1556  | 0  |                 s->ext.scts_len = 0;  | 
1557  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);  | 
1558  | 0  |                 return 0;  | 
1559  | 0  |             }  | 
1560  | 0  |             if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) { | 
1561  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1562  | 0  |                 return 0;  | 
1563  | 0  |             }  | 
1564  | 0  |         }  | 
1565  | 0  |     } else { | 
1566  | 0  |         ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0  | 
1567  | 0  |                         ? ENDPOINT_CLIENT : ENDPOINT_BOTH;  | 
1568  |  |  | 
1569  |  |         /*  | 
1570  |  |          * If we didn't ask for it then there must be a custom extension,  | 
1571  |  |          * otherwise this is unsolicited.  | 
1572  |  |          */  | 
1573  | 0  |         if (custom_ext_find(&s->cert->custext, role,  | 
1574  | 0  |                             TLSEXT_TYPE_signed_certificate_timestamp,  | 
1575  | 0  |                             NULL) == NULL) { | 
1576  | 0  |             SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);  | 
1577  | 0  |             return 0;  | 
1578  | 0  |         }  | 
1579  |  |  | 
1580  | 0  |         if (!custom_ext_parse(s, context,  | 
1581  | 0  |                              TLSEXT_TYPE_signed_certificate_timestamp,  | 
1582  | 0  |                              PACKET_data(pkt), PACKET_remaining(pkt),  | 
1583  | 0  |                              x, chainidx)) { | 
1584  |  |             /* SSLfatal already called */  | 
1585  | 0  |             return 0;  | 
1586  | 0  |         }  | 
1587  | 0  |     }  | 
1588  |  |  | 
1589  | 0  |     return 1;  | 
1590  | 0  | }  | 
1591  |  | #endif  | 
1592  |  |  | 
1593  |  |  | 
1594  |  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
1595  |  | /*  | 
1596  |  |  * ssl_next_proto_validate validates a Next Protocol Negotiation block. No  | 
1597  |  |  * elements of zero length are allowed and the set of elements must exactly  | 
1598  |  |  * fill the length of the block. Returns 1 on success or 0 on failure.  | 
1599  |  |  */  | 
1600  |  | static int ssl_next_proto_validate(SSL_CONNECTION *s, PACKET *pkt)  | 
1601  | 0  | { | 
1602  | 0  |     PACKET tmp_protocol;  | 
1603  |  | 
  | 
1604  | 0  |     while (PACKET_remaining(pkt)) { | 
1605  | 0  |         if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)  | 
1606  | 0  |             || PACKET_remaining(&tmp_protocol) == 0) { | 
1607  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1608  | 0  |             return 0;  | 
1609  | 0  |         }  | 
1610  | 0  |     }  | 
1611  |  |  | 
1612  | 0  |     return 1;  | 
1613  | 0  | }  | 
1614  |  |  | 
1615  |  | int tls_parse_stoc_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
1616  |  |                        X509 *x, size_t chainidx)  | 
1617  | 0  | { | 
1618  | 0  |     unsigned char *selected;  | 
1619  | 0  |     unsigned char selected_len;  | 
1620  | 0  |     PACKET tmppkt;  | 
1621  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
1622  |  |  | 
1623  |  |     /* Check if we are in a renegotiation. If so ignore this extension */  | 
1624  | 0  |     if (!SSL_IS_FIRST_HANDSHAKE(s))  | 
1625  | 0  |         return 1;  | 
1626  |  |  | 
1627  |  |     /* We must have requested it. */  | 
1628  | 0  |     if (sctx->ext.npn_select_cb == NULL) { | 
1629  | 0  |         SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);  | 
1630  | 0  |         return 0;  | 
1631  | 0  |     }  | 
1632  |  |  | 
1633  |  |     /* The data must be valid */  | 
1634  | 0  |     tmppkt = *pkt;  | 
1635  | 0  |     if (!ssl_next_proto_validate(s, &tmppkt)) { | 
1636  |  |         /* SSLfatal() already called */  | 
1637  | 0  |         return 0;  | 
1638  | 0  |     }  | 
1639  | 0  |     if (sctx->ext.npn_select_cb(SSL_CONNECTION_GET_USER_SSL(s),  | 
1640  | 0  |                                 &selected, &selected_len,  | 
1641  | 0  |                                 PACKET_data(pkt), (unsigned int)PACKET_remaining(pkt),  | 
1642  | 0  |                                 sctx->ext.npn_select_cb_arg) != SSL_TLSEXT_ERR_OK  | 
1643  | 0  |             || selected_len == 0) { | 
1644  | 0  |         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);  | 
1645  | 0  |         return 0;  | 
1646  | 0  |     }  | 
1647  |  |  | 
1648  |  |     /*  | 
1649  |  |      * Could be non-NULL if server has sent multiple NPN extensions in  | 
1650  |  |      * a single Serverhello  | 
1651  |  |      */  | 
1652  | 0  |     OPENSSL_free(s->ext.npn);  | 
1653  | 0  |     s->ext.npn = OPENSSL_malloc(selected_len);  | 
1654  | 0  |     if (s->ext.npn == NULL) { | 
1655  | 0  |         s->ext.npn_len = 0;  | 
1656  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1657  | 0  |         return 0;  | 
1658  | 0  |     }  | 
1659  |  |  | 
1660  | 0  |     memcpy(s->ext.npn, selected, selected_len);  | 
1661  | 0  |     s->ext.npn_len = selected_len;  | 
1662  | 0  |     s->s3.npn_seen = 1;  | 
1663  |  | 
  | 
1664  | 0  |     return 1;  | 
1665  | 0  | }  | 
1666  |  | #endif  | 
1667  |  |  | 
1668  |  | int tls_parse_stoc_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
1669  |  |                         X509 *x, size_t chainidx)  | 
1670  | 0  | { | 
1671  | 0  |     size_t len;  | 
1672  | 0  |     PACKET confpkt, protpkt;  | 
1673  | 0  |     int valid = 0;  | 
1674  |  |  | 
1675  |  |     /* We must have requested it. */  | 
1676  | 0  |     if (!s->s3.alpn_sent) { | 
1677  | 0  |         SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);  | 
1678  | 0  |         return 0;  | 
1679  | 0  |     }  | 
1680  |  |     /*-  | 
1681  |  |      * The extension data consists of:  | 
1682  |  |      *   uint16 list_length  | 
1683  |  |      *   uint8 proto_length;  | 
1684  |  |      *   uint8 proto[proto_length];  | 
1685  |  |      */  | 
1686  | 0  |     if (!PACKET_get_net_2_len(pkt, &len)  | 
1687  | 0  |         || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)  | 
1688  | 0  |         || PACKET_remaining(pkt) != len) { | 
1689  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1690  | 0  |         return 0;  | 
1691  | 0  |     }  | 
1692  |  |  | 
1693  |  |     /* It must be a protocol that we sent */  | 
1694  | 0  |     if (!PACKET_buf_init(&confpkt, s->ext.alpn, s->ext.alpn_len)) { | 
1695  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1696  | 0  |         return 0;  | 
1697  | 0  |     }  | 
1698  | 0  |     while (PACKET_get_length_prefixed_1(&confpkt, &protpkt)) { | 
1699  | 0  |         if (PACKET_remaining(&protpkt) != len)  | 
1700  | 0  |             continue;  | 
1701  | 0  |         if (memcmp(PACKET_data(pkt), PACKET_data(&protpkt), len) == 0) { | 
1702  |  |             /* Valid protocol found */  | 
1703  | 0  |             valid = 1;  | 
1704  | 0  |             break;  | 
1705  | 0  |         }  | 
1706  | 0  |     }  | 
1707  |  | 
  | 
1708  | 0  |     if (!valid) { | 
1709  |  |         /* The protocol sent from the server does not match one we advertised */  | 
1710  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1711  | 0  |         return 0;  | 
1712  | 0  |     }  | 
1713  |  |  | 
1714  | 0  |     OPENSSL_free(s->s3.alpn_selected);  | 
1715  | 0  |     s->s3.alpn_selected = OPENSSL_malloc(len);  | 
1716  | 0  |     if (s->s3.alpn_selected == NULL) { | 
1717  | 0  |         s->s3.alpn_selected_len = 0;  | 
1718  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1719  | 0  |         return 0;  | 
1720  | 0  |     }  | 
1721  | 0  |     if (!PACKET_copy_bytes(pkt, s->s3.alpn_selected, len)) { | 
1722  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
1723  | 0  |         return 0;  | 
1724  | 0  |     }  | 
1725  | 0  |     s->s3.alpn_selected_len = len;  | 
1726  |  | 
  | 
1727  | 0  |     if (s->session->ext.alpn_selected == NULL  | 
1728  | 0  |             || s->session->ext.alpn_selected_len != len  | 
1729  | 0  |             || memcmp(s->session->ext.alpn_selected, s->s3.alpn_selected, len)  | 
1730  | 0  |                != 0) { | 
1731  |  |         /* ALPN not consistent with the old session so cannot use early_data */  | 
1732  | 0  |         s->ext.early_data_ok = 0;  | 
1733  | 0  |     }  | 
1734  | 0  |     if (!s->hit) { | 
1735  |  |         /*  | 
1736  |  |          * This is a new session and so alpn_selected should have been  | 
1737  |  |          * initialised to NULL. We should update it with the selected ALPN.  | 
1738  |  |          */  | 
1739  | 0  |         if (!ossl_assert(s->session->ext.alpn_selected == NULL)) { | 
1740  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1741  | 0  |             return 0;  | 
1742  | 0  |         }  | 
1743  | 0  |         s->session->ext.alpn_selected =  | 
1744  | 0  |             OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);  | 
1745  | 0  |         if (s->session->ext.alpn_selected == NULL) { | 
1746  | 0  |             s->session->ext.alpn_selected_len = 0;  | 
1747  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1748  | 0  |             return 0;  | 
1749  | 0  |         }  | 
1750  | 0  |         s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;  | 
1751  | 0  |     }  | 
1752  |  |  | 
1753  | 0  |     return 1;  | 
1754  | 0  | }  | 
1755  |  |  | 
1756  |  | #ifndef OPENSSL_NO_SRTP  | 
1757  |  | int tls_parse_stoc_use_srtp(SSL_CONNECTION *s, PACKET *pkt,  | 
1758  |  |                             unsigned int context, X509 *x, size_t chainidx)  | 
1759  | 0  | { | 
1760  | 0  |     unsigned int id, ct, mki;  | 
1761  | 0  |     int i;  | 
1762  | 0  |     STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;  | 
1763  | 0  |     SRTP_PROTECTION_PROFILE *prof;  | 
1764  |  | 
  | 
1765  | 0  |     if (!PACKET_get_net_2(pkt, &ct) || ct != 2  | 
1766  | 0  |             || !PACKET_get_net_2(pkt, &id)  | 
1767  | 0  |             || !PACKET_get_1(pkt, &mki)  | 
1768  | 0  |             || PACKET_remaining(pkt) != 0) { | 
1769  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR,  | 
1770  | 0  |                  SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);  | 
1771  | 0  |         return 0;  | 
1772  | 0  |     }  | 
1773  |  |  | 
1774  | 0  |     if (mki != 0) { | 
1775  |  |         /* Must be no MKI, since we never offer one */  | 
1776  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRTP_MKI_VALUE);  | 
1777  | 0  |         return 0;  | 
1778  | 0  |     }  | 
1779  |  |  | 
1780  |  |     /* Throw an error if the server gave us an unsolicited extension */  | 
1781  | 0  |     clnt = SSL_get_srtp_profiles(SSL_CONNECTION_GET_SSL(s));  | 
1782  | 0  |     if (clnt == NULL) { | 
1783  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_SRTP_PROFILES);  | 
1784  | 0  |         return 0;  | 
1785  | 0  |     }  | 
1786  |  |  | 
1787  |  |     /*  | 
1788  |  |      * Check to see if the server gave us something we support (and  | 
1789  |  |      * presumably offered)  | 
1790  |  |      */  | 
1791  | 0  |     for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) { | 
1792  | 0  |         prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);  | 
1793  |  | 
  | 
1794  | 0  |         if (prof->id == id) { | 
1795  | 0  |             s->srtp_profile = prof;  | 
1796  | 0  |             return 1;  | 
1797  | 0  |         }  | 
1798  | 0  |     }  | 
1799  |  |  | 
1800  | 0  |     SSLfatal(s, SSL_AD_DECODE_ERROR,  | 
1801  | 0  |              SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);  | 
1802  | 0  |     return 0;  | 
1803  | 0  | }  | 
1804  |  | #endif  | 
1805  |  |  | 
1806  |  | int tls_parse_stoc_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
1807  |  |                        X509 *x, size_t chainidx)  | 
1808  | 0  | { | 
1809  |  |     /* Ignore if inappropriate ciphersuite */  | 
1810  | 0  |     if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)  | 
1811  | 0  |             && s->s3.tmp.new_cipher->algorithm_mac != SSL_AEAD  | 
1812  | 0  |             && s->s3.tmp.new_cipher->algorithm_enc != SSL_RC4  | 
1813  | 0  |             && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT  | 
1814  | 0  |             && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT12  | 
1815  | 0  |             && s->s3.tmp.new_cipher->algorithm_enc != SSL_MAGMA  | 
1816  | 0  |             && s->s3.tmp.new_cipher->algorithm_enc != SSL_KUZNYECHIK)  | 
1817  | 0  |         s->ext.use_etm = 1;  | 
1818  |  | 
  | 
1819  | 0  |     return 1;  | 
1820  | 0  | }  | 
1821  |  |  | 
1822  |  | int tls_parse_stoc_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
1823  |  |                        X509 *x, size_t chainidx)  | 
1824  | 0  | { | 
1825  | 0  |     if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)  | 
1826  | 0  |         return 1;  | 
1827  | 0  |     s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;  | 
1828  | 0  |     if (!s->hit)  | 
1829  | 0  |         s->session->flags |= SSL_SESS_FLAG_EXTMS;  | 
1830  |  | 
  | 
1831  | 0  |     return 1;  | 
1832  | 0  | }  | 
1833  |  |  | 
1834  |  | int tls_parse_stoc_supported_versions(SSL_CONNECTION *s, PACKET *pkt,  | 
1835  |  |                                       unsigned int context,  | 
1836  |  |                                       X509 *x, size_t chainidx)  | 
1837  | 0  | { | 
1838  | 0  |     unsigned int version;  | 
1839  |  | 
  | 
1840  | 0  |     if (!PACKET_get_net_2(pkt, &version)  | 
1841  | 0  |             || PACKET_remaining(pkt) != 0) { | 
1842  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
1843  | 0  |         return 0;  | 
1844  | 0  |     }  | 
1845  |  |  | 
1846  |  |     /*  | 
1847  |  |      * The only protocol version we support which is valid in this extension in  | 
1848  |  |      * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else.  | 
1849  |  |      */  | 
1850  | 0  |     if (version != TLS1_3_VERSION) { | 
1851  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,  | 
1852  | 0  |                  SSL_R_BAD_PROTOCOL_VERSION_NUMBER);  | 
1853  | 0  |         return 0;  | 
1854  | 0  |     }  | 
1855  |  |  | 
1856  |  |     /* We ignore this extension for HRRs except to sanity check it */  | 
1857  | 0  |     if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)  | 
1858  | 0  |         return 1;  | 
1859  |  |  | 
1860  |  |     /* We just set it here. We validate it in ssl_choose_client_version */  | 
1861  | 0  |     s->version = version;  | 
1862  | 0  |     if (!ssl_set_record_protocol_version(s, version)) { | 
1863  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1864  | 0  |         return 0;  | 
1865  | 0  |     }  | 
1866  |  |  | 
1867  | 0  |     return 1;  | 
1868  | 0  | }  | 
1869  |  |  | 
1870  |  | int tls_parse_stoc_key_share(SSL_CONNECTION *s, PACKET *pkt,  | 
1871  |  |                              unsigned int context, X509 *x,  | 
1872  |  |                              size_t chainidx)  | 
1873  | 0  | { | 
1874  | 0  | #ifndef OPENSSL_NO_TLS1_3  | 
1875  | 0  |     unsigned int group_id;  | 
1876  | 0  |     PACKET encoded_pt;  | 
1877  | 0  |     EVP_PKEY *ckey = s->s3.tmp.pkey, *skey = NULL;  | 
1878  | 0  |     const TLS_GROUP_INFO *ginf = NULL;  | 
1879  | 0  |     uint16_t valid_ks_id = 0;  | 
1880  | 0  |     size_t i;  | 
1881  |  |  | 
1882  |  |     /* Sanity check */  | 
1883  | 0  |     if (ckey == NULL || s->s3.peer_tmp != NULL) { | 
1884  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1885  | 0  |         return 0;  | 
1886  | 0  |     }  | 
1887  |  |  | 
1888  |  |     /* Which group ID does the server want -> group_id */  | 
1889  | 0  |     if (!PACKET_get_net_2(pkt, &group_id)) { | 
1890  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
1891  | 0  |         return 0;  | 
1892  | 0  |     }  | 
1893  |  |  | 
1894  | 0  |     if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) { | 
1895  | 0  |         const uint16_t *pgroups = NULL;  | 
1896  | 0  |         size_t num_groups;  | 
1897  |  | 
  | 
1898  | 0  |         if (PACKET_remaining(pkt) != 0) { | 
1899  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
1900  | 0  |             return 0;  | 
1901  | 0  |         }  | 
1902  |  |  | 
1903  |  |         /*  | 
1904  |  |          * It is an error if the HelloRetryRequest wants a key_share that we  | 
1905  |  |          * already sent in the first ClientHello  | 
1906  |  |          */  | 
1907  | 0  |         for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) { | 
1908  | 0  |             if (s->s3.tmp.ks_group_id[i] == group_id) { | 
1909  | 0  |                 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);  | 
1910  | 0  |                 return 0;  | 
1911  | 0  |             }  | 
1912  | 0  |         }  | 
1913  |  |  | 
1914  |  |         /* Validate the selected group is one we support */  | 
1915  | 0  |         tls1_get_supported_groups(s, &pgroups, &num_groups);  | 
1916  | 0  |         for (i = 0; i < num_groups; i++) { | 
1917  | 0  |             if (group_id == pgroups[i])  | 
1918  | 0  |                 break;  | 
1919  | 0  |         }  | 
1920  | 0  |         if (i >= num_groups  | 
1921  | 0  |                 || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)  | 
1922  | 0  |                 || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,  | 
1923  | 0  |                                     0, NULL)) { | 
1924  | 0  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);  | 
1925  | 0  |             return 0;  | 
1926  | 0  |         }  | 
1927  |  |  | 
1928  |  |         /* Memorize which groupID the server wants */  | 
1929  | 0  |         s->s3.group_id = group_id;  | 
1930  |  |  | 
1931  |  |         /* The initial keyshares are obsolete now, hence free memory */  | 
1932  | 0  |         for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) { | 
1933  | 0  |             if (s->s3.tmp.ks_pkey[i] != NULL) { | 
1934  | 0  |                 EVP_PKEY_free(s->s3.tmp.ks_pkey[i]);  | 
1935  | 0  |                 s->s3.tmp.ks_pkey[i] = NULL;  | 
1936  | 0  |             }  | 
1937  | 0  |         }  | 
1938  | 0  |         s->s3.tmp.num_ks_pkey = 0;  | 
1939  | 0  |         s->s3.tmp.pkey = NULL;  | 
1940  |  | 
  | 
1941  | 0  |         return 1;  | 
1942  | 0  |     }  | 
1943  |  |  | 
1944  |  |     /*  | 
1945  |  |      * check that the group requested by the server is one we've  | 
1946  |  |      * sent a key share for, and if so: memorize which one  | 
1947  |  |      */  | 
1948  | 0  |     for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) { | 
1949  | 0  |         if (s->s3.tmp.ks_group_id[i] == group_id) { | 
1950  | 0  |             valid_ks_id = group_id;  | 
1951  | 0  |             ckey = s->s3.tmp.ks_pkey[i];  | 
1952  | 0  |             s->s3.group_id = group_id;  | 
1953  | 0  |             s->s3.tmp.pkey = ckey;  | 
1954  | 0  |             break;  | 
1955  | 0  |         }  | 
1956  | 0  |     }  | 
1957  | 0  |     if (valid_ks_id == 0) { | 
1958  |  |         /*  | 
1959  |  |          * This isn't for the group that we sent in the original  | 
1960  |  |          * key_share!  | 
1961  |  |          */  | 
1962  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);  | 
1963  | 0  |         return 0;  | 
1964  | 0  |     }  | 
1965  |  |     /* Retain this group in the SSL_SESSION */  | 
1966  | 0  |     if (!s->hit) { | 
1967  | 0  |         s->session->kex_group = group_id;  | 
1968  | 0  |     } else if (group_id != s->session->kex_group) { | 
1969  |  |         /*  | 
1970  |  |          * If this is a resumption but changed what group was used, we need  | 
1971  |  |          * to record the new group in the session, but the session is not  | 
1972  |  |          * a new session and could be in use by other threads.  So, make  | 
1973  |  |          * a copy of the session to record the new information so that it's  | 
1974  |  |          * useful for any sessions resumed from tickets issued on this  | 
1975  |  |          * connection.  | 
1976  |  |          */  | 
1977  | 0  |         SSL_SESSION *new_sess;  | 
1978  |  | 
  | 
1979  | 0  |         if ((new_sess = ssl_session_dup(s->session, 0)) == NULL) { | 
1980  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);  | 
1981  | 0  |             return 0;  | 
1982  | 0  |         }  | 
1983  | 0  |         SSL_SESSION_free(s->session);  | 
1984  | 0  |         s->session = new_sess;  | 
1985  | 0  |         s->session->kex_group = group_id;  | 
1986  | 0  |     }  | 
1987  |  |  | 
1988  | 0  |     if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),  | 
1989  | 0  |                                      group_id)) == NULL) { | 
1990  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);  | 
1991  | 0  |         return 0;  | 
1992  | 0  |     }  | 
1993  |  |  | 
1994  | 0  |     if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)  | 
1995  | 0  |             || PACKET_remaining(&encoded_pt) == 0) { | 
1996  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
1997  | 0  |         return 0;  | 
1998  | 0  |     }  | 
1999  |  |  | 
2000  | 0  |     if (!ginf->is_kem) { | 
2001  |  |         /* Regular KEX */  | 
2002  | 0  |         skey = EVP_PKEY_new();  | 
2003  | 0  |         if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) { | 
2004  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);  | 
2005  | 0  |             EVP_PKEY_free(skey);  | 
2006  | 0  |             return 0;  | 
2007  | 0  |         }  | 
2008  |  |  | 
2009  | 0  |         if (tls13_set_encoded_pub_key(skey, PACKET_data(&encoded_pt),  | 
2010  | 0  |                                       PACKET_remaining(&encoded_pt)) <= 0) { | 
2011  | 0  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);  | 
2012  | 0  |             EVP_PKEY_free(skey);  | 
2013  | 0  |             return 0;  | 
2014  | 0  |         }  | 
2015  |  |  | 
2016  | 0  |         if (ssl_derive(s, ckey, skey, 1) == 0) { | 
2017  |  |             /* SSLfatal() already called */  | 
2018  | 0  |             EVP_PKEY_free(skey);  | 
2019  | 0  |             return 0;  | 
2020  | 0  |         }  | 
2021  | 0  |         s->s3.peer_tmp = skey;  | 
2022  | 0  |     } else { | 
2023  |  |         /* KEM Mode */  | 
2024  | 0  |         const unsigned char *ct = PACKET_data(&encoded_pt);  | 
2025  | 0  |         size_t ctlen = PACKET_remaining(&encoded_pt);  | 
2026  |  | 
  | 
2027  | 0  |         if (ssl_decapsulate(s, ckey, ct, ctlen, 1) == 0) { | 
2028  |  |             /* SSLfatal() already called */  | 
2029  | 0  |             return 0;  | 
2030  | 0  |         }  | 
2031  | 0  |     }  | 
2032  | 0  |     s->s3.did_kex = 1;  | 
2033  | 0  | #endif  | 
2034  |  | 
  | 
2035  | 0  |     return 1;  | 
2036  | 0  | }  | 
2037  |  |  | 
2038  |  | int tls_parse_stoc_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,  | 
2039  |  |                           X509 *x, size_t chainidx)  | 
2040  | 0  | { | 
2041  | 0  |     PACKET cookie;  | 
2042  |  | 
  | 
2043  | 0  |     if (!PACKET_as_length_prefixed_2(pkt, &cookie)  | 
2044  | 0  |             || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,  | 
2045  | 0  |                               &s->ext.tls13_cookie_len)) { | 
2046  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
2047  | 0  |         return 0;  | 
2048  | 0  |     }  | 
2049  |  |  | 
2050  | 0  |     return 1;  | 
2051  | 0  | }  | 
2052  |  |  | 
2053  |  | int tls_parse_stoc_early_data(SSL_CONNECTION *s, PACKET *pkt,  | 
2054  |  |                               unsigned int context,  | 
2055  |  |                               X509 *x, size_t chainidx)  | 
2056  | 0  | { | 
2057  | 0  |     if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) { | 
2058  | 0  |         unsigned long max_early_data;  | 
2059  |  | 
  | 
2060  | 0  |         if (!PACKET_get_net_4(pkt, &max_early_data)  | 
2061  | 0  |                 || PACKET_remaining(pkt) != 0) { | 
2062  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_MAX_EARLY_DATA);  | 
2063  | 0  |             return 0;  | 
2064  | 0  |         }  | 
2065  |  |  | 
2066  | 0  |         s->session->ext.max_early_data = max_early_data;  | 
2067  |  | 
  | 
2068  | 0  |         if (SSL_IS_QUIC_HANDSHAKE(s) && max_early_data != 0xffffffff) { | 
2069  |  |             /*  | 
2070  |  |              * QUIC allows missing max_early_data, or a max_early_data value  | 
2071  |  |              * of 0xffffffff. Missing max_early_data is stored in the session  | 
2072  |  |              * as 0. This is indistinguishable in OpenSSL from a present  | 
2073  |  |              * max_early_data value that was 0. In order that later checks for  | 
2074  |  |              * invalid max_early_data correctly treat as an error the case where  | 
2075  |  |              * max_early_data is present and it is 0, we store any invalid  | 
2076  |  |              * value in the same (non-zero) way. Otherwise we would have to  | 
2077  |  |              * introduce a new flag just for this.  | 
2078  |  |              */  | 
2079  | 0  |             s->session->ext.max_early_data = 1;  | 
2080  | 0  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_MAX_EARLY_DATA);  | 
2081  | 0  |             return 0;  | 
2082  | 0  |         }  | 
2083  |  |  | 
2084  | 0  |         return 1;  | 
2085  | 0  |     }  | 
2086  |  |  | 
2087  | 0  |     if (PACKET_remaining(pkt) != 0) { | 
2088  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2089  | 0  |         return 0;  | 
2090  | 0  |     }  | 
2091  |  |  | 
2092  | 0  |     if (!s->ext.early_data_ok  | 
2093  | 0  |             || !s->hit) { | 
2094  |  |         /*  | 
2095  |  |          * If we get here then we didn't send early data, or we didn't resume  | 
2096  |  |          * using the first identity, or the SNI/ALPN is not consistent so the  | 
2097  |  |          * server should not be accepting it.  | 
2098  |  |          */  | 
2099  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);  | 
2100  | 0  |         return 0;  | 
2101  | 0  |     }  | 
2102  |  |  | 
2103  | 0  |     s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;  | 
2104  |  | 
  | 
2105  | 0  |     return 1;  | 
2106  | 0  | }  | 
2107  |  |  | 
2108  |  | int tls_parse_stoc_psk(SSL_CONNECTION *s, PACKET *pkt,  | 
2109  |  |                        unsigned int context, X509 *x,  | 
2110  |  |                        size_t chainidx)  | 
2111  | 0  | { | 
2112  | 0  | #ifndef OPENSSL_NO_TLS1_3  | 
2113  | 0  |     unsigned int identity;  | 
2114  |  | 
  | 
2115  | 0  |     if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) { | 
2116  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
2117  | 0  |         return 0;  | 
2118  | 0  |     }  | 
2119  |  |  | 
2120  | 0  |     if (identity >= (unsigned int)s->ext.tick_identity) { | 
2121  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_PSK_IDENTITY);  | 
2122  | 0  |         return 0;  | 
2123  | 0  |     }  | 
2124  |  |  | 
2125  |  |     /*  | 
2126  |  |      * Session resumption tickets are always sent before PSK tickets. If the  | 
2127  |  |      * ticket index is 0 then it must be for a session resumption ticket if we  | 
2128  |  |      * sent two tickets, or if we didn't send a PSK ticket.  | 
2129  |  |      */  | 
2130  | 0  |     if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) { | 
2131  | 0  |         s->hit = 1;  | 
2132  | 0  |         SSL_SESSION_free(s->psksession);  | 
2133  | 0  |         s->psksession = NULL;  | 
2134  | 0  |         return 1;  | 
2135  | 0  |     }  | 
2136  |  |  | 
2137  | 0  |     if (s->psksession == NULL) { | 
2138  |  |         /* Should never happen */  | 
2139  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2140  | 0  |         return 0;  | 
2141  | 0  |     }  | 
2142  |  |  | 
2143  |  |     /*  | 
2144  |  |      * If we used the external PSK for sending early_data then s->early_secret  | 
2145  |  |      * is already set up, so don't overwrite it. Otherwise we copy the  | 
2146  |  |      * early_secret across that we generated earlier.  | 
2147  |  |      */  | 
2148  | 0  |     if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY  | 
2149  | 0  |                 && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)  | 
2150  | 0  |             || s->session->ext.max_early_data > 0  | 
2151  | 0  |             || s->psksession->ext.max_early_data == 0)  | 
2152  | 0  |         memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE);  | 
2153  |  | 
  | 
2154  | 0  |     SSL_SESSION_free(s->session);  | 
2155  | 0  |     s->session = s->psksession;  | 
2156  | 0  |     s->psksession = NULL;  | 
2157  | 0  |     s->hit = 1;  | 
2158  |  |     /* Early data is only allowed if we used the first ticket */  | 
2159  | 0  |     if (identity != 0)  | 
2160  | 0  |         s->ext.early_data_ok = 0;  | 
2161  | 0  | #endif  | 
2162  |  | 
  | 
2163  | 0  |     return 1;  | 
2164  | 0  | }  | 
2165  |  |  | 
2166  |  | EXT_RETURN tls_construct_ctos_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,  | 
2167  |  |                                                unsigned int context,  | 
2168  |  |                                                X509 *x, size_t chainidx)  | 
2169  | 0  | { | 
2170  | 0  |     sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;  | 
2171  | 0  |     if (sc->client_cert_type == NULL)  | 
2172  | 0  |         return EXT_RETURN_NOT_SENT;  | 
2173  |  |  | 
2174  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)  | 
2175  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
2176  | 0  |             || !WPACKET_sub_memcpy_u8(pkt, sc->client_cert_type, sc->client_cert_type_len)  | 
2177  | 0  |             || !WPACKET_close(pkt)) { | 
2178  | 0  |         SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2179  | 0  |         return EXT_RETURN_FAIL;  | 
2180  | 0  |     }  | 
2181  | 0  |     sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;  | 
2182  | 0  |     return EXT_RETURN_SENT;  | 
2183  | 0  | }  | 
2184  |  |  | 
2185  |  | int tls_parse_stoc_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,  | 
2186  |  |                                     unsigned int context,  | 
2187  |  |                                     X509 *x, size_t chainidx)  | 
2188  | 0  | { | 
2189  | 0  |     unsigned int type;  | 
2190  |  | 
  | 
2191  | 0  |     if (PACKET_remaining(pkt) != 1) { | 
2192  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2193  | 0  |         return 0;  | 
2194  | 0  |     }  | 
2195  | 0  |     if (!PACKET_get_1(pkt, &type)) { | 
2196  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2197  | 0  |         return 0;  | 
2198  | 0  |     }  | 
2199  |  |     /* We did not send/ask for this */  | 
2200  | 0  |     if (!ossl_assert(sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) { | 
2201  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2202  | 0  |         return 0;  | 
2203  | 0  |     }  | 
2204  |  |     /* We don't have this enabled */  | 
2205  | 0  |     if (sc->client_cert_type == NULL) { | 
2206  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2207  | 0  |         return 0;  | 
2208  | 0  |     }  | 
2209  |  |     /* Given back a value we didn't configure */  | 
2210  | 0  |     if (memchr(sc->client_cert_type, type, sc->client_cert_type_len) == NULL) { | 
2211  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);  | 
2212  | 0  |         return 0;  | 
2213  | 0  |     }  | 
2214  | 0  |     sc->ext.client_cert_type = type;  | 
2215  | 0  |     return 1;  | 
2216  | 0  | }  | 
2217  |  |  | 
2218  |  | EXT_RETURN tls_construct_ctos_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,  | 
2219  |  |                                                unsigned int context,  | 
2220  |  |                                                X509 *x, size_t chainidx)  | 
2221  | 0  | { | 
2222  | 0  |     sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;  | 
2223  | 0  |     if (sc->server_cert_type == NULL)  | 
2224  | 0  |         return EXT_RETURN_NOT_SENT;  | 
2225  |  |  | 
2226  | 0  |     if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)  | 
2227  | 0  |             || !WPACKET_start_sub_packet_u16(pkt)  | 
2228  | 0  |             || !WPACKET_sub_memcpy_u8(pkt, sc->server_cert_type, sc->server_cert_type_len)  | 
2229  | 0  |             || !WPACKET_close(pkt)) { | 
2230  | 0  |         SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2231  | 0  |         return EXT_RETURN_FAIL;  | 
2232  | 0  |     }  | 
2233  | 0  |     sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;  | 
2234  | 0  |     return EXT_RETURN_SENT;  | 
2235  | 0  | }  | 
2236  |  |  | 
2237  |  | int tls_parse_stoc_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,  | 
2238  |  |                                     unsigned int context,  | 
2239  |  |                                     X509 *x, size_t chainidx)  | 
2240  | 0  | { | 
2241  | 0  |     unsigned int type;  | 
2242  |  | 
  | 
2243  | 0  |     if (PACKET_remaining(pkt) != 1) { | 
2244  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2245  | 0  |         return 0;  | 
2246  | 0  |     }  | 
2247  | 0  |     if (!PACKET_get_1(pkt, &type)) { | 
2248  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2249  | 0  |         return 0;  | 
2250  | 0  |     }  | 
2251  |  |     /* We did not send/ask for this */  | 
2252  | 0  |     if (!ossl_assert(sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) { | 
2253  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2254  | 0  |         return 0;  | 
2255  | 0  |     }  | 
2256  |  |     /* We don't have this enabled */  | 
2257  | 0  |     if (sc->server_cert_type == NULL) { | 
2258  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);  | 
2259  | 0  |         return 0;  | 
2260  | 0  |     }  | 
2261  |  |     /* Given back a value we didn't configure */  | 
2262  | 0  |     if (memchr(sc->server_cert_type, type, sc->server_cert_type_len) == NULL) { | 
2263  | 0  |         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);  | 
2264  | 0  |         return 0;  | 
2265  | 0  |     }  | 
2266  | 0  |     sc->ext.server_cert_type = type;  | 
2267  | 0  |     return 1;  | 
2268  | 0  | }  |