/src/openssl32/ssl/statem/statem_srvr.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved  | 
4  |  |  * Copyright 2005 Nokia. All rights reserved.  | 
5  |  |  *  | 
6  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
7  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
8  |  |  * in the file LICENSE in the source distribution or at  | 
9  |  |  * https://www.openssl.org/source/license.html  | 
10  |  |  */  | 
11  |  |  | 
12  |  | #include <stdio.h>  | 
13  |  | #include "../ssl_local.h"  | 
14  |  | #include "statem_local.h"  | 
15  |  | #include "internal/constant_time.h"  | 
16  |  | #include "internal/cryptlib.h"  | 
17  |  | #include <openssl/buffer.h>  | 
18  |  | #include <openssl/rand.h>  | 
19  |  | #include <openssl/objects.h>  | 
20  |  | #include <openssl/evp.h>  | 
21  |  | #include <openssl/x509.h>  | 
22  |  | #include <openssl/dh.h>  | 
23  |  | #include <openssl/rsa.h>  | 
24  |  | #include <openssl/bn.h>  | 
25  |  | #include <openssl/md5.h>  | 
26  |  | #include <openssl/trace.h>  | 
27  |  | #include <openssl/core_names.h>  | 
28  |  | #include <openssl/asn1t.h>  | 
29  |  | #include <openssl/comp.h>  | 
30  |  |  | 
31  | 0  | #define TICKET_NONCE_SIZE       8  | 
32  |  |  | 
33  |  | typedef struct { | 
34  |  |   ASN1_TYPE *kxBlob;  | 
35  |  |   ASN1_TYPE *opaqueBlob;  | 
36  |  | } GOST_KX_MESSAGE;  | 
37  |  |  | 
38  |  | DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE)  | 
39  |  |  | 
40  |  | ASN1_SEQUENCE(GOST_KX_MESSAGE) = { | 
41  |  |   ASN1_SIMPLE(GOST_KX_MESSAGE,  kxBlob, ASN1_ANY),  | 
42  |  |   ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY),  | 
43  |  | } ASN1_SEQUENCE_END(GOST_KX_MESSAGE)  | 
44  |  |  | 
45  |  | IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE)  | 
46  |  |  | 
47  |  | static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s,  | 
48  |  |                                                           WPACKET *pkt);  | 
49  |  |  | 
50  |  | static ossl_inline int received_client_cert(const SSL_CONNECTION *sc)  | 
51  | 9.45k  | { | 
52  | 9.45k  |     return sc->session->peer_rpk != NULL || sc->session->peer != NULL;  | 
53  | 9.45k  | }  | 
54  |  |  | 
55  |  | /*  | 
56  |  |  * ossl_statem_server13_read_transition() encapsulates the logic for the allowed  | 
57  |  |  * handshake state transitions when a TLSv1.3 server is reading messages from  | 
58  |  |  * the client. The message type that the client has sent is provided in |mt|.  | 
59  |  |  * The current state is in |s->statem.hand_state|.  | 
60  |  |  *  | 
61  |  |  * Return values are 1 for success (transition allowed) and  0 on error  | 
62  |  |  * (transition not allowed)  | 
63  |  |  */  | 
64  |  | static int ossl_statem_server13_read_transition(SSL_CONNECTION *s, int mt)  | 
65  | 87  | { | 
66  | 87  |     OSSL_STATEM *st = &s->statem;  | 
67  |  |  | 
68  |  |     /*  | 
69  |  |      * Note: There is no case for TLS_ST_BEFORE because at that stage we have  | 
70  |  |      * not negotiated TLSv1.3 yet, so that case is handled by  | 
71  |  |      * ossl_statem_server_read_transition()  | 
72  |  |      */  | 
73  | 87  |     switch (st->hand_state) { | 
74  | 0  |     default:  | 
75  | 0  |         break;  | 
76  |  |  | 
77  | 87  |     case TLS_ST_EARLY_DATA:  | 
78  | 87  |         if (s->hello_retry_request == SSL_HRR_PENDING) { | 
79  | 83  |             if (mt == SSL3_MT_CLIENT_HELLO) { | 
80  | 78  |                 st->hand_state = TLS_ST_SR_CLNT_HELLO;  | 
81  | 78  |                 return 1;  | 
82  | 78  |             }  | 
83  | 5  |             break;  | 
84  | 83  |         } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) { | 
85  | 0  |             if (mt == SSL3_MT_END_OF_EARLY_DATA) { | 
86  | 0  |                 st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;  | 
87  | 0  |                 return 1;  | 
88  | 0  |             }  | 
89  | 0  |             break;  | 
90  | 0  |         }  | 
91  |  |         /* Fall through */  | 
92  |  |  | 
93  | 4  |     case TLS_ST_SR_END_OF_EARLY_DATA:  | 
94  | 4  |     case TLS_ST_SW_FINISHED:  | 
95  | 4  |         if (s->s3.tmp.cert_request) { | 
96  | 0  |             if (mt == SSL3_MT_CERTIFICATE) { | 
97  | 0  |                 st->hand_state = TLS_ST_SR_CERT;  | 
98  | 0  |                 return 1;  | 
99  | 0  |             }  | 
100  |  | #ifndef OPENSSL_NO_COMP_ALG  | 
101  |  |             if (mt == SSL3_MT_COMPRESSED_CERTIFICATE  | 
102  |  |                     && s->ext.compress_certificate_sent) { | 
103  |  |                 st->hand_state = TLS_ST_SR_COMP_CERT;  | 
104  |  |                 return 1;  | 
105  |  |             }  | 
106  |  | #endif  | 
107  | 4  |         } else { | 
108  | 4  |             if (mt == SSL3_MT_FINISHED) { | 
109  | 1  |                 st->hand_state = TLS_ST_SR_FINISHED;  | 
110  | 1  |                 return 1;  | 
111  | 1  |             }  | 
112  | 4  |         }  | 
113  | 3  |         break;  | 
114  |  |  | 
115  | 3  |     case TLS_ST_SR_COMP_CERT:  | 
116  | 0  |     case TLS_ST_SR_CERT:  | 
117  | 0  |         if (!received_client_cert(s)) { | 
118  | 0  |             if (mt == SSL3_MT_FINISHED) { | 
119  | 0  |                 st->hand_state = TLS_ST_SR_FINISHED;  | 
120  | 0  |                 return 1;  | 
121  | 0  |             }  | 
122  | 0  |         } else { | 
123  | 0  |             if (mt == SSL3_MT_CERTIFICATE_VERIFY) { | 
124  | 0  |                 st->hand_state = TLS_ST_SR_CERT_VRFY;  | 
125  | 0  |                 return 1;  | 
126  | 0  |             }  | 
127  | 0  |         }  | 
128  | 0  |         break;  | 
129  |  |  | 
130  | 0  |     case TLS_ST_SR_CERT_VRFY:  | 
131  | 0  |         if (mt == SSL3_MT_FINISHED) { | 
132  | 0  |             st->hand_state = TLS_ST_SR_FINISHED;  | 
133  | 0  |             return 1;  | 
134  | 0  |         }  | 
135  | 0  |         break;  | 
136  |  |  | 
137  | 0  |     case TLS_ST_OK:  | 
138  |  |         /*  | 
139  |  |          * Its never ok to start processing handshake messages in the middle of  | 
140  |  |          * early data (i.e. before we've received the end of early data alert)  | 
141  |  |          */  | 
142  | 0  |         if (s->early_data_state == SSL_EARLY_DATA_READING)  | 
143  | 0  |             break;  | 
144  |  |  | 
145  | 0  |         if (s->post_handshake_auth == SSL_PHA_REQUESTED) { | 
146  | 0  |             if (mt == SSL3_MT_CERTIFICATE) { | 
147  | 0  |                 st->hand_state = TLS_ST_SR_CERT;  | 
148  | 0  |                 return 1;  | 
149  | 0  |             }  | 
150  |  | #ifndef OPENSSL_NO_COMP_ALG  | 
151  |  |             if (mt == SSL3_MT_COMPRESSED_CERTIFICATE  | 
152  |  |                     && s->ext.compress_certificate_sent) { | 
153  |  |                 st->hand_state = TLS_ST_SR_COMP_CERT;  | 
154  |  |                 return 1;  | 
155  |  |             }  | 
156  |  | #endif  | 
157  | 0  |         }  | 
158  |  |  | 
159  | 0  |         if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) { | 
160  | 0  |             st->hand_state = TLS_ST_SR_KEY_UPDATE;  | 
161  | 0  |             return 1;  | 
162  | 0  |         }  | 
163  | 0  |         break;  | 
164  | 87  |     }  | 
165  |  |  | 
166  |  |     /* No valid transition found */  | 
167  | 8  |     return 0;  | 
168  | 87  | }  | 
169  |  |  | 
170  |  | /*  | 
171  |  |  * ossl_statem_server_read_transition() encapsulates the logic for the allowed  | 
172  |  |  * handshake state transitions when the server is reading messages from the  | 
173  |  |  * client. The message type that the client has sent is provided in |mt|. The  | 
174  |  |  * current state is in |s->statem.hand_state|.  | 
175  |  |  *  | 
176  |  |  * Return values are 1 for success (transition allowed) and  0 on error  | 
177  |  |  * (transition not allowed)  | 
178  |  |  */  | 
179  |  | int ossl_statem_server_read_transition(SSL_CONNECTION *s, int mt)  | 
180  | 90.2k  | { | 
181  | 90.2k  |     OSSL_STATEM *st = &s->statem;  | 
182  |  |  | 
183  | 90.2k  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
184  | 187  |         if (!ossl_statem_server13_read_transition(s, mt))  | 
185  | 10  |             goto err;  | 
186  | 177  |         return 1;  | 
187  | 187  |     }  | 
188  |  |  | 
189  | 90.0k  |     switch (st->hand_state) { | 
190  | 0  |     default:  | 
191  | 0  |         break;  | 
192  |  |  | 
193  | 13.7k  |     case TLS_ST_BEFORE:  | 
194  | 78.7k  |     case TLS_ST_OK:  | 
195  | 78.7k  |     case DTLS_ST_SW_HELLO_VERIFY_REQUEST:  | 
196  | 78.7k  |         if (mt == SSL3_MT_CLIENT_HELLO) { | 
197  | 78.4k  |             st->hand_state = TLS_ST_SR_CLNT_HELLO;  | 
198  | 78.4k  |             return 1;  | 
199  | 78.4k  |         }  | 
200  | 252  |         break;  | 
201  |  |  | 
202  | 6.44k  |     case TLS_ST_SW_SRVR_DONE:  | 
203  |  |         /*  | 
204  |  |          * If we get a CKE message after a ServerDone then either  | 
205  |  |          * 1) We didn't request a Certificate  | 
206  |  |          * OR  | 
207  |  |          * 2) If we did request one then  | 
208  |  |          *      a) We allow no Certificate to be returned  | 
209  |  |          *      AND  | 
210  |  |          *      b) We are running SSL3 (in TLS1.0+ the client must return a 0  | 
211  |  |          *         list if we requested a certificate)  | 
212  |  |          */  | 
213  | 6.44k  |         if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) { | 
214  | 6.41k  |             if (s->s3.tmp.cert_request) { | 
215  | 0  |                 if (s->version == SSL3_VERSION) { | 
216  | 0  |                     if ((s->verify_mode & SSL_VERIFY_PEER)  | 
217  | 0  |                         && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { | 
218  |  |                         /*  | 
219  |  |                          * This isn't an unexpected message as such - we're just  | 
220  |  |                          * not going to accept it because we require a client  | 
221  |  |                          * cert.  | 
222  |  |                          */  | 
223  | 0  |                         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
224  | 0  |                                  SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);  | 
225  | 0  |                         return 0;  | 
226  | 0  |                     }  | 
227  | 0  |                     st->hand_state = TLS_ST_SR_KEY_EXCH;  | 
228  | 0  |                     return 1;  | 
229  | 0  |                 }  | 
230  | 6.41k  |             } else { | 
231  | 6.41k  |                 st->hand_state = TLS_ST_SR_KEY_EXCH;  | 
232  | 6.41k  |                 return 1;  | 
233  | 6.41k  |             }  | 
234  | 6.41k  |         } else if (s->s3.tmp.cert_request) { | 
235  | 0  |             if (mt == SSL3_MT_CERTIFICATE) { | 
236  | 0  |                 st->hand_state = TLS_ST_SR_CERT;  | 
237  | 0  |                 return 1;  | 
238  | 0  |             }  | 
239  | 0  |         }  | 
240  | 31  |         break;  | 
241  |  |  | 
242  | 31  |     case TLS_ST_SR_CERT:  | 
243  | 0  |         if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) { | 
244  | 0  |             st->hand_state = TLS_ST_SR_KEY_EXCH;  | 
245  | 0  |             return 1;  | 
246  | 0  |         }  | 
247  | 0  |         break;  | 
248  |  |  | 
249  | 3.77k  |     case TLS_ST_SR_KEY_EXCH:  | 
250  |  |         /*  | 
251  |  |          * We should only process a CertificateVerify message if we have  | 
252  |  |          * received a Certificate from the client. If so then |s->session->peer|  | 
253  |  |          * will be non NULL. In some instances a CertificateVerify message is  | 
254  |  |          * not required even if the peer has sent a Certificate (e.g. such as in  | 
255  |  |          * the case of static DH). In that case |st->no_cert_verify| should be  | 
256  |  |          * set.  | 
257  |  |          */  | 
258  | 3.77k  |         if (!received_client_cert(s) || st->no_cert_verify) { | 
259  | 3.77k  |             if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | 
260  |  |                 /*  | 
261  |  |                  * For the ECDH ciphersuites when the client sends its ECDH  | 
262  |  |                  * pub key in a certificate, the CertificateVerify message is  | 
263  |  |                  * not sent. Also for GOST ciphersuites when the client uses  | 
264  |  |                  * its key from the certificate for key exchange.  | 
265  |  |                  */  | 
266  | 3.73k  |                 st->hand_state = TLS_ST_SR_CHANGE;  | 
267  | 3.73k  |                 return 1;  | 
268  | 3.73k  |             }  | 
269  | 3.77k  |         } else { | 
270  | 0  |             if (mt == SSL3_MT_CERTIFICATE_VERIFY) { | 
271  | 0  |                 st->hand_state = TLS_ST_SR_CERT_VRFY;  | 
272  | 0  |                 return 1;  | 
273  | 0  |             }  | 
274  | 0  |         }  | 
275  | 43  |         break;  | 
276  |  |  | 
277  | 43  |     case TLS_ST_SR_CERT_VRFY:  | 
278  | 0  |         if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | 
279  | 0  |             st->hand_state = TLS_ST_SR_CHANGE;  | 
280  | 0  |             return 1;  | 
281  | 0  |         }  | 
282  | 0  |         break;  | 
283  |  |  | 
284  | 1.09k  |     case TLS_ST_SR_CHANGE:  | 
285  | 1.09k  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
286  | 1.09k  |         if (s->s3.npn_seen) { | 
287  | 0  |             if (mt == SSL3_MT_NEXT_PROTO) { | 
288  | 0  |                 st->hand_state = TLS_ST_SR_NEXT_PROTO;  | 
289  | 0  |                 return 1;  | 
290  | 0  |             }  | 
291  | 1.09k  |         } else { | 
292  | 1.09k  | #endif  | 
293  | 1.09k  |             if (mt == SSL3_MT_FINISHED) { | 
294  | 975  |                 st->hand_state = TLS_ST_SR_FINISHED;  | 
295  | 975  |                 return 1;  | 
296  | 975  |             }  | 
297  | 1.09k  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
298  | 1.09k  |         }  | 
299  | 115  | #endif  | 
300  | 115  |         break;  | 
301  |  |  | 
302  | 115  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
303  | 115  |     case TLS_ST_SR_NEXT_PROTO:  | 
304  | 0  |         if (mt == SSL3_MT_FINISHED) { | 
305  | 0  |             st->hand_state = TLS_ST_SR_FINISHED;  | 
306  | 0  |             return 1;  | 
307  | 0  |         }  | 
308  | 0  |         break;  | 
309  | 0  | #endif  | 
310  |  |  | 
311  | 35  |     case TLS_ST_SW_FINISHED:  | 
312  | 35  |         if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | 
313  | 27  |             st->hand_state = TLS_ST_SR_CHANGE;  | 
314  | 27  |             return 1;  | 
315  | 27  |         }  | 
316  | 8  |         break;  | 
317  | 90.0k  |     }  | 
318  |  |  | 
319  | 459  |  err:  | 
320  |  |     /* No valid transition found */  | 
321  | 459  |     if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | 
322  | 3  |         BIO *rbio;  | 
323  |  |  | 
324  |  |         /*  | 
325  |  |          * CCS messages don't have a message sequence number so this is probably  | 
326  |  |          * because of an out-of-order CCS. We'll just drop it.  | 
327  |  |          */  | 
328  | 3  |         s->init_num = 0;  | 
329  | 3  |         s->rwstate = SSL_READING;  | 
330  | 3  |         rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s));  | 
331  | 3  |         BIO_clear_retry_flags(rbio);  | 
332  | 3  |         BIO_set_retry_read(rbio);  | 
333  | 3  |         return 0;  | 
334  | 3  |     }  | 
335  | 459  |     SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);  | 
336  | 456  |     return 0;  | 
337  | 459  | }  | 
338  |  |  | 
339  |  | /*  | 
340  |  |  * Should we send a ServerKeyExchange message?  | 
341  |  |  *  | 
342  |  |  * Valid return values are:  | 
343  |  |  *   1: Yes  | 
344  |  |  *   0: No  | 
345  |  |  */  | 
346  |  | static int send_server_key_exchange(SSL_CONNECTION *s)  | 
347  | 12.0k  | { | 
348  | 12.0k  |     unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;  | 
349  |  |  | 
350  |  |     /*  | 
351  |  |      * only send a ServerKeyExchange if DH or fortezza but we have a  | 
352  |  |      * sign only certificate PSK: may send PSK identity hints For  | 
353  |  |      * ECC ciphersuites, we send a serverKeyExchange message only if  | 
354  |  |      * the cipher suite is either ECDH-anon or ECDHE. In other cases,  | 
355  |  |      * the server certificate contains the server's public key for  | 
356  |  |      * key exchange.  | 
357  |  |      */  | 
358  | 12.0k  |     if (alg_k & (SSL_kDHE | SSL_kECDHE)  | 
359  |  |         /*  | 
360  |  |          * PSK: send ServerKeyExchange if PSK identity hint if  | 
361  |  |          * provided  | 
362  |  |          */  | 
363  | 12.0k  | #ifndef OPENSSL_NO_PSK  | 
364  |  |         /* Only send SKE if we have identity hint for plain PSK */  | 
365  | 12.0k  |         || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))  | 
366  | 6.98k  |             && s->cert->psk_identity_hint)  | 
367  |  |         /* For other PSK always send SKE */  | 
368  | 12.0k  |         || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))  | 
369  | 12.0k  | #endif  | 
370  | 12.0k  | #ifndef OPENSSL_NO_SRP  | 
371  |  |         /* SRP: send ServerKeyExchange */  | 
372  | 12.0k  |         || (alg_k & SSL_kSRP)  | 
373  | 12.0k  | #endif  | 
374  | 12.0k  |         ) { | 
375  | 5.03k  |         return 1;  | 
376  | 5.03k  |     }  | 
377  |  |  | 
378  | 6.98k  |     return 0;  | 
379  | 12.0k  | }  | 
380  |  |  | 
381  |  | /*  | 
382  |  |  * Used to determine if we should send a CompressedCertificate message  | 
383  |  |  *  | 
384  |  |  * Returns the algorithm to use, TLSEXT_comp_cert_none means no compression  | 
385  |  |  */  | 
386  |  | static int get_compressed_certificate_alg(SSL_CONNECTION *sc)  | 
387  | 1.06k  | { | 
388  |  | #ifndef OPENSSL_NO_COMP_ALG  | 
389  |  |     int *alg = sc->ext.compress_certificate_from_peer;  | 
390  |  |  | 
391  |  |     if (sc->s3.tmp.cert == NULL)  | 
392  |  |         return TLSEXT_comp_cert_none;  | 
393  |  |  | 
394  |  |     for (; *alg != TLSEXT_comp_cert_none; alg++) { | 
395  |  |         if (sc->s3.tmp.cert->comp_cert[*alg] != NULL)  | 
396  |  |             return *alg;  | 
397  |  |     }  | 
398  |  | #endif  | 
399  | 1.06k  |     return TLSEXT_comp_cert_none;  | 
400  | 1.06k  | }  | 
401  |  |  | 
402  |  | /*  | 
403  |  |  * Should we send a CertificateRequest message?  | 
404  |  |  *  | 
405  |  |  * Valid return values are:  | 
406  |  |  *   1: Yes  | 
407  |  |  *   0: No  | 
408  |  |  */  | 
409  |  | int send_certificate_request(SSL_CONNECTION *s)  | 
410  | 15.0k  | { | 
411  | 15.0k  |     if (  | 
412  |  |            /* don't request cert unless asked for it: */  | 
413  | 15.0k  |            s->verify_mode & SSL_VERIFY_PEER  | 
414  |  |            /*  | 
415  |  |             * don't request if post-handshake-only unless doing  | 
416  |  |             * post-handshake in TLSv1.3:  | 
417  |  |             */  | 
418  | 15.0k  |            && (!SSL_CONNECTION_IS_TLS13(s)  | 
419  | 0  |                || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)  | 
420  | 0  |                || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)  | 
421  |  |            /*  | 
422  |  |             * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert  | 
423  |  |             * a second time:  | 
424  |  |             */  | 
425  | 15.0k  |            && (s->certreqs_sent < 1 ||  | 
426  | 0  |                !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))  | 
427  |  |            /*  | 
428  |  |             * never request cert in anonymous ciphersuites (see  | 
429  |  |             * section "Certificate request" in SSL 3 drafts and in  | 
430  |  |             * RFC 2246):  | 
431  |  |             */  | 
432  | 15.0k  |            && (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL)  | 
433  |  |                /*  | 
434  |  |                 * ... except when the application insists on  | 
435  |  |                 * verification (against the specs, but statem_clnt.c accepts  | 
436  |  |                 * this for SSL 3)  | 
437  |  |                 */  | 
438  | 0  |                || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))  | 
439  |  |            /* don't request certificate for SRP auth */  | 
440  | 15.0k  |            && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP)  | 
441  |  |            /*  | 
442  |  |             * With normal PSK Certificates and Certificate Requests  | 
443  |  |             * are omitted  | 
444  |  |             */  | 
445  | 15.0k  |            && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) { | 
446  | 0  |         return 1;  | 
447  | 0  |     }  | 
448  |  |  | 
449  | 15.0k  |     return 0;  | 
450  | 15.0k  | }  | 
451  |  |  | 
452  |  | static int do_compressed_cert(SSL_CONNECTION *sc)  | 
453  | 1.06k  | { | 
454  |  |     /* If we negotiated RPK, we won't attempt to compress it */  | 
455  | 1.06k  |     return sc->ext.server_cert_type == TLSEXT_cert_type_x509  | 
456  | 1.06k  |         && get_compressed_certificate_alg(sc) != TLSEXT_comp_cert_none;  | 
457  | 1.06k  | }  | 
458  |  |  | 
459  |  | /*  | 
460  |  |  * ossl_statem_server13_write_transition() works out what handshake state to  | 
461  |  |  * move to next when a TLSv1.3 server is writing messages to be sent to the  | 
462  |  |  * client.  | 
463  |  |  */  | 
464  |  | static WRITE_TRAN ossl_statem_server13_write_transition(SSL_CONNECTION *s)  | 
465  | 9.42k  | { | 
466  | 9.42k  |     OSSL_STATEM *st = &s->statem;  | 
467  |  |  | 
468  |  |     /*  | 
469  |  |      * No case for TLS_ST_BEFORE, because at that stage we have not negotiated  | 
470  |  |      * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()  | 
471  |  |      */  | 
472  |  |  | 
473  | 9.42k  |     switch (st->hand_state) { | 
474  | 0  |     default:  | 
475  |  |         /* Shouldn't happen */  | 
476  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
477  | 0  |         return WRITE_TRAN_ERROR;  | 
478  |  |  | 
479  | 0  |     case TLS_ST_OK:  | 
480  | 0  |         if (s->key_update != SSL_KEY_UPDATE_NONE) { | 
481  | 0  |             st->hand_state = TLS_ST_SW_KEY_UPDATE;  | 
482  | 0  |             return WRITE_TRAN_CONTINUE;  | 
483  | 0  |         }  | 
484  | 0  |         if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { | 
485  | 0  |             st->hand_state = TLS_ST_SW_CERT_REQ;  | 
486  | 0  |             return WRITE_TRAN_CONTINUE;  | 
487  | 0  |         }  | 
488  | 0  |         if (s->ext.extra_tickets_expected > 0) { | 
489  | 0  |             st->hand_state = TLS_ST_SW_SESSION_TICKET;  | 
490  | 0  |             return WRITE_TRAN_CONTINUE;  | 
491  | 0  |         }  | 
492  |  |         /* Try to read from the client instead */  | 
493  | 0  |         return WRITE_TRAN_FINISHED;  | 
494  |  |  | 
495  | 1.31k  |     case TLS_ST_SR_CLNT_HELLO:  | 
496  | 1.31k  |         st->hand_state = TLS_ST_SW_SRVR_HELLO;  | 
497  | 1.31k  |         return WRITE_TRAN_CONTINUE;  | 
498  |  |  | 
499  | 1.30k  |     case TLS_ST_SW_SRVR_HELLO:  | 
500  | 1.30k  |         if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0  | 
501  | 1.30k  |                 && s->hello_retry_request != SSL_HRR_COMPLETE)  | 
502  | 1.24k  |             st->hand_state = TLS_ST_SW_CHANGE;  | 
503  | 56  |         else if (s->hello_retry_request == SSL_HRR_PENDING)  | 
504  | 0  |             st->hand_state = TLS_ST_EARLY_DATA;  | 
505  | 56  |         else  | 
506  | 56  |             st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;  | 
507  | 1.30k  |         return WRITE_TRAN_CONTINUE;  | 
508  |  |  | 
509  | 1.24k  |     case TLS_ST_SW_CHANGE:  | 
510  | 1.24k  |         if (s->hello_retry_request == SSL_HRR_PENDING)  | 
511  | 232  |             st->hand_state = TLS_ST_EARLY_DATA;  | 
512  | 1.01k  |         else  | 
513  | 1.01k  |             st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;  | 
514  | 1.24k  |         return WRITE_TRAN_CONTINUE;  | 
515  |  |  | 
516  | 1.06k  |     case TLS_ST_SW_ENCRYPTED_EXTENSIONS:  | 
517  | 1.06k  |         if (s->hit)  | 
518  | 0  |             st->hand_state = TLS_ST_SW_FINISHED;  | 
519  | 1.06k  |         else if (send_certificate_request(s))  | 
520  | 0  |             st->hand_state = TLS_ST_SW_CERT_REQ;  | 
521  | 1.06k  |         else if (do_compressed_cert(s))  | 
522  | 0  |             st->hand_state = TLS_ST_SW_COMP_CERT;  | 
523  | 1.06k  |         else  | 
524  | 1.06k  |             st->hand_state = TLS_ST_SW_CERT;  | 
525  |  |  | 
526  | 1.06k  |         return WRITE_TRAN_CONTINUE;  | 
527  |  |  | 
528  | 0  |     case TLS_ST_SW_CERT_REQ:  | 
529  | 0  |         if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { | 
530  | 0  |             s->post_handshake_auth = SSL_PHA_REQUESTED;  | 
531  | 0  |             st->hand_state = TLS_ST_OK;  | 
532  | 0  |         } else if (do_compressed_cert(s)) { | 
533  | 0  |             st->hand_state = TLS_ST_SW_COMP_CERT;  | 
534  | 0  |         } else { | 
535  | 0  |             st->hand_state = TLS_ST_SW_CERT;  | 
536  | 0  |         }  | 
537  | 0  |         return WRITE_TRAN_CONTINUE;  | 
538  |  |  | 
539  | 0  |     case TLS_ST_SW_COMP_CERT:  | 
540  | 1.06k  |     case TLS_ST_SW_CERT:  | 
541  | 1.06k  |         st->hand_state = TLS_ST_SW_CERT_VRFY;  | 
542  | 1.06k  |         return WRITE_TRAN_CONTINUE;  | 
543  |  |  | 
544  | 1.06k  |     case TLS_ST_SW_CERT_VRFY:  | 
545  | 1.06k  |         st->hand_state = TLS_ST_SW_FINISHED;  | 
546  | 1.06k  |         return WRITE_TRAN_CONTINUE;  | 
547  |  |  | 
548  | 1.06k  |     case TLS_ST_SW_FINISHED:  | 
549  | 1.06k  |         st->hand_state = TLS_ST_EARLY_DATA;  | 
550  | 1.06k  |         s->ts_msg_write = ossl_time_now();  | 
551  | 1.06k  |         return WRITE_TRAN_CONTINUE;  | 
552  |  |  | 
553  | 1.30k  |     case TLS_ST_EARLY_DATA:  | 
554  | 1.30k  |         return WRITE_TRAN_FINISHED;  | 
555  |  |  | 
556  | 0  |     case TLS_ST_SR_FINISHED:  | 
557  | 0  |         s->ts_msg_read = ossl_time_now();  | 
558  |  |         /*  | 
559  |  |          * Technically we have finished the handshake at this point, but we're  | 
560  |  |          * going to remain "in_init" for now and write out any session tickets  | 
561  |  |          * immediately.  | 
562  |  |          */  | 
563  | 0  |         if (s->post_handshake_auth == SSL_PHA_REQUESTED) { | 
564  | 0  |             s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;  | 
565  | 0  |         } else if (!s->ext.ticket_expected) { | 
566  |  |             /*  | 
567  |  |              * If we're not going to renew the ticket then we just finish the  | 
568  |  |              * handshake at this point.  | 
569  |  |              */  | 
570  | 0  |             st->hand_state = TLS_ST_OK;  | 
571  | 0  |             return WRITE_TRAN_CONTINUE;  | 
572  | 0  |         }  | 
573  | 0  |         if (s->num_tickets > s->sent_tickets)  | 
574  | 0  |             st->hand_state = TLS_ST_SW_SESSION_TICKET;  | 
575  | 0  |         else  | 
576  | 0  |             st->hand_state = TLS_ST_OK;  | 
577  | 0  |         return WRITE_TRAN_CONTINUE;  | 
578  |  |  | 
579  | 0  |     case TLS_ST_SR_KEY_UPDATE:  | 
580  | 0  |     case TLS_ST_SW_KEY_UPDATE:  | 
581  | 0  |         st->hand_state = TLS_ST_OK;  | 
582  | 0  |         return WRITE_TRAN_CONTINUE;  | 
583  |  |  | 
584  | 0  |     case TLS_ST_SW_SESSION_TICKET:  | 
585  |  |         /* In a resumption we only ever send a maximum of one new ticket.  | 
586  |  |          * Following an initial handshake we send the number of tickets we have  | 
587  |  |          * been configured for.  | 
588  |  |          */  | 
589  | 0  |         if (!SSL_IS_FIRST_HANDSHAKE(s) && s->ext.extra_tickets_expected > 0) { | 
590  | 0  |             return WRITE_TRAN_CONTINUE;  | 
591  | 0  |         } else if (s->hit || s->num_tickets <= s->sent_tickets) { | 
592  |  |             /* We've written enough tickets out. */  | 
593  | 0  |             st->hand_state = TLS_ST_OK;  | 
594  | 0  |         }  | 
595  | 0  |         return WRITE_TRAN_CONTINUE;  | 
596  | 9.42k  |     }  | 
597  | 9.42k  | }  | 
598  |  |  | 
599  |  | /*  | 
600  |  |  * ossl_statem_server_write_transition() works out what handshake state to move  | 
601  |  |  * to next when the server is writing messages to be sent to the client.  | 
602  |  |  */  | 
603  |  | WRITE_TRAN ossl_statem_server_write_transition(SSL_CONNECTION *s)  | 
604  | 228k  | { | 
605  | 228k  |     OSSL_STATEM *st = &s->statem;  | 
606  |  |  | 
607  |  |     /*  | 
608  |  |      * Note that before the ClientHello we don't know what version we are going  | 
609  |  |      * to negotiate yet, so we don't take this branch until later  | 
610  |  |      */  | 
611  |  |  | 
612  | 228k  |     if (SSL_CONNECTION_IS_TLS13(s))  | 
613  | 17.6k  |         return ossl_statem_server13_write_transition(s);  | 
614  |  |  | 
615  | 210k  |     switch (st->hand_state) { | 
616  | 0  |     default:  | 
617  |  |         /* Shouldn't happen */  | 
618  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
619  | 0  |         return WRITE_TRAN_ERROR;  | 
620  |  |  | 
621  | 64.9k  |     case TLS_ST_OK:  | 
622  | 64.9k  |         if (st->request_state == TLS_ST_SW_HELLO_REQ) { | 
623  |  |             /* We must be trying to renegotiate */  | 
624  | 0  |             st->hand_state = TLS_ST_SW_HELLO_REQ;  | 
625  | 0  |             st->request_state = TLS_ST_BEFORE;  | 
626  | 0  |             return WRITE_TRAN_CONTINUE;  | 
627  | 0  |         }  | 
628  |  |         /* Must be an incoming ClientHello */  | 
629  | 64.9k  |         if (!tls_setup_handshake(s)) { | 
630  |  |             /* SSLfatal() already called */  | 
631  | 0  |             return WRITE_TRAN_ERROR;  | 
632  | 0  |         }  | 
633  |  |         /* Fall through */  | 
634  |  |  | 
635  | 90.4k  |     case TLS_ST_BEFORE:  | 
636  |  |         /* Just go straight to trying to read from the client */  | 
637  | 90.4k  |         return WRITE_TRAN_FINISHED;  | 
638  |  |  | 
639  | 0  |     case TLS_ST_SW_HELLO_REQ:  | 
640  | 0  |         st->hand_state = TLS_ST_OK;  | 
641  | 0  |         return WRITE_TRAN_CONTINUE;  | 
642  |  |  | 
643  | 76.6k  |     case TLS_ST_SR_CLNT_HELLO:  | 
644  | 76.6k  |         if (SSL_CONNECTION_IS_DTLS(s) && !s->d1->cookie_verified  | 
645  | 76.6k  |             && (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE)) { | 
646  | 0  |             st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;  | 
647  | 76.6k  |         } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) { | 
648  |  |             /* We must have rejected the renegotiation */  | 
649  | 64.5k  |             st->hand_state = TLS_ST_OK;  | 
650  | 64.5k  |             return WRITE_TRAN_CONTINUE;  | 
651  | 64.5k  |         } else { | 
652  | 12.1k  |             st->hand_state = TLS_ST_SW_SRVR_HELLO;  | 
653  | 12.1k  |         }  | 
654  | 12.1k  |         return WRITE_TRAN_CONTINUE;  | 
655  |  |  | 
656  | 0  |     case DTLS_ST_SW_HELLO_VERIFY_REQUEST:  | 
657  | 0  |         return WRITE_TRAN_FINISHED;  | 
658  |  |  | 
659  | 12.1k  |     case TLS_ST_SW_SRVR_HELLO:  | 
660  | 12.1k  |         if (s->hit) { | 
661  | 135  |             if (s->ext.ticket_expected)  | 
662  | 0  |                 st->hand_state = TLS_ST_SW_SESSION_TICKET;  | 
663  | 135  |             else  | 
664  | 135  |                 st->hand_state = TLS_ST_SW_CHANGE;  | 
665  | 12.0k  |         } else { | 
666  |  |             /* Check if it is anon DH or anon ECDH, */  | 
667  |  |             /* normal PSK or SRP */  | 
668  | 12.0k  |             if (!(s->s3.tmp.new_cipher->algorithm_auth &  | 
669  | 12.0k  |                   (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { | 
670  | 11.2k  |                 st->hand_state = TLS_ST_SW_CERT;  | 
671  | 11.2k  |             } else if (send_server_key_exchange(s)) { | 
672  | 781  |                 st->hand_state = TLS_ST_SW_KEY_EXCH;  | 
673  | 781  |             } else if (send_certificate_request(s)) { | 
674  | 0  |                 st->hand_state = TLS_ST_SW_CERT_REQ;  | 
675  | 0  |             } else { | 
676  | 0  |                 st->hand_state = TLS_ST_SW_SRVR_DONE;  | 
677  | 0  |             }  | 
678  | 12.0k  |         }  | 
679  | 12.1k  |         return WRITE_TRAN_CONTINUE;  | 
680  |  |  | 
681  | 11.2k  |     case TLS_ST_SW_CERT:  | 
682  | 11.2k  |         if (s->ext.status_expected) { | 
683  | 0  |             st->hand_state = TLS_ST_SW_CERT_STATUS;  | 
684  | 0  |             return WRITE_TRAN_CONTINUE;  | 
685  | 0  |         }  | 
686  |  |         /* Fall through */  | 
687  |  |  | 
688  | 11.2k  |     case TLS_ST_SW_CERT_STATUS:  | 
689  | 11.2k  |         if (send_server_key_exchange(s)) { | 
690  | 4.25k  |             st->hand_state = TLS_ST_SW_KEY_EXCH;  | 
691  | 4.25k  |             return WRITE_TRAN_CONTINUE;  | 
692  | 4.25k  |         }  | 
693  |  |         /* Fall through */  | 
694  |  |  | 
695  | 12.0k  |     case TLS_ST_SW_KEY_EXCH:  | 
696  | 12.0k  |         if (send_certificate_request(s)) { | 
697  | 0  |             st->hand_state = TLS_ST_SW_CERT_REQ;  | 
698  | 0  |             return WRITE_TRAN_CONTINUE;  | 
699  | 0  |         }  | 
700  |  |         /* Fall through */  | 
701  |  |  | 
702  | 12.0k  |     case TLS_ST_SW_CERT_REQ:  | 
703  | 12.0k  |         st->hand_state = TLS_ST_SW_SRVR_DONE;  | 
704  | 12.0k  |         return WRITE_TRAN_CONTINUE;  | 
705  |  |  | 
706  | 12.0k  |     case TLS_ST_SW_SRVR_DONE:  | 
707  | 12.0k  |         s->ts_msg_write = ossl_time_now();  | 
708  | 12.0k  |         return WRITE_TRAN_FINISHED;  | 
709  |  |  | 
710  | 929  |     case TLS_ST_SR_FINISHED:  | 
711  | 929  |         s->ts_msg_read = ossl_time_now();  | 
712  | 929  |         if (s->hit) { | 
713  | 0  |             st->hand_state = TLS_ST_OK;  | 
714  | 0  |             return WRITE_TRAN_CONTINUE;  | 
715  | 929  |         } else if (s->ext.ticket_expected) { | 
716  | 96  |             st->hand_state = TLS_ST_SW_SESSION_TICKET;  | 
717  | 833  |         } else { | 
718  | 833  |             st->hand_state = TLS_ST_SW_CHANGE;  | 
719  | 833  |         }  | 
720  | 929  |         return WRITE_TRAN_CONTINUE;  | 
721  |  |  | 
722  | 96  |     case TLS_ST_SW_SESSION_TICKET:  | 
723  | 96  |         st->hand_state = TLS_ST_SW_CHANGE;  | 
724  | 96  |         return WRITE_TRAN_CONTINUE;  | 
725  |  |  | 
726  | 1.06k  |     case TLS_ST_SW_CHANGE:  | 
727  | 1.06k  |         st->hand_state = TLS_ST_SW_FINISHED;  | 
728  | 1.06k  |         return WRITE_TRAN_CONTINUE;  | 
729  |  |  | 
730  | 1.06k  |     case TLS_ST_SW_FINISHED:  | 
731  | 1.06k  |         if (s->hit) { | 
732  | 135  |             return WRITE_TRAN_FINISHED;  | 
733  | 135  |         }  | 
734  | 929  |         st->hand_state = TLS_ST_OK;  | 
735  | 929  |         return WRITE_TRAN_CONTINUE;  | 
736  | 210k  |     }  | 
737  | 210k  | }  | 
738  |  |  | 
739  |  | /*  | 
740  |  |  * Perform any pre work that needs to be done prior to sending a message from  | 
741  |  |  * the server to the client.  | 
742  |  |  */  | 
743  |  | WORK_STATE ossl_statem_server_pre_work(SSL_CONNECTION *s, WORK_STATE wst)  | 
744  | 65.5k  | { | 
745  | 65.5k  |     OSSL_STATEM *st = &s->statem;  | 
746  | 65.5k  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
747  |  |  | 
748  | 65.5k  |     switch (st->hand_state) { | 
749  | 15.1k  |     default:  | 
750  |  |         /* No pre work to be done */  | 
751  | 15.1k  |         break;  | 
752  |  |  | 
753  | 15.1k  |     case TLS_ST_SW_HELLO_REQ:  | 
754  | 0  |         s->shutdown = 0;  | 
755  | 0  |         if (SSL_CONNECTION_IS_DTLS(s))  | 
756  | 0  |             dtls1_clear_sent_buffer(s);  | 
757  | 0  |         break;  | 
758  |  |  | 
759  | 0  |     case DTLS_ST_SW_HELLO_VERIFY_REQUEST:  | 
760  | 0  |         s->shutdown = 0;  | 
761  | 0  |         if (SSL_CONNECTION_IS_DTLS(s)) { | 
762  | 0  |             dtls1_clear_sent_buffer(s);  | 
763  |  |             /* We don't buffer this message so don't use the timer */  | 
764  | 0  |             st->use_timer = 0;  | 
765  | 0  |         }  | 
766  | 0  |         break;  | 
767  |  |  | 
768  | 8.73k  |     case TLS_ST_SW_SRVR_HELLO:  | 
769  | 8.73k  |         if (SSL_CONNECTION_IS_DTLS(s)) { | 
770  |  |             /*  | 
771  |  |              * Messages we write from now on should be buffered and  | 
772  |  |              * retransmitted if necessary, so we need to use the timer now  | 
773  |  |              */  | 
774  | 0  |             st->use_timer = 1;  | 
775  | 0  |         }  | 
776  | 8.73k  |         break;  | 
777  |  |  | 
778  | 6.90k  |     case TLS_ST_SW_SRVR_DONE:  | 
779  |  | #ifndef OPENSSL_NO_SCTP  | 
780  |  |         if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) { | 
781  |  |             /* Calls SSLfatal() as required */  | 
782  |  |             return dtls_wait_for_dry(s);  | 
783  |  |         }  | 
784  |  | #endif  | 
785  | 6.90k  |         return WORK_FINISHED_CONTINUE;  | 
786  |  |  | 
787  | 87  |     case TLS_ST_SW_SESSION_TICKET:  | 
788  | 87  |         if (SSL_CONNECTION_IS_TLS13(s) && s->sent_tickets == 0  | 
789  | 87  |                 && s->ext.extra_tickets_expected == 0) { | 
790  |  |             /*  | 
791  |  |              * Actually this is the end of the handshake, but we're going  | 
792  |  |              * straight into writing the session ticket out. So we finish off  | 
793  |  |              * the handshake, but keep the various buffers active.  | 
794  |  |              *  | 
795  |  |              * Calls SSLfatal as required.  | 
796  |  |              */  | 
797  | 0  |             return tls_finish_handshake(s, wst, 0, 0);  | 
798  | 0  |         }  | 
799  | 87  |         if (SSL_CONNECTION_IS_DTLS(s)) { | 
800  |  |             /*  | 
801  |  |              * We're into the last flight. We don't retransmit the last flight  | 
802  |  |              * unless we need to, so we don't use the timer  | 
803  |  |              */  | 
804  | 0  |             st->use_timer = 0;  | 
805  | 0  |         }  | 
806  | 87  |         break;  | 
807  |  |  | 
808  | 2.21k  |     case TLS_ST_SW_CHANGE:  | 
809  | 2.21k  |         if (SSL_CONNECTION_IS_TLS13(s))  | 
810  | 1.66k  |             break;  | 
811  |  |         /* Writes to s->session are only safe for initial handshakes */  | 
812  | 548  |         if (s->session->cipher == NULL) { | 
813  | 0  |             s->session->cipher = s->s3.tmp.new_cipher;  | 
814  | 548  |         } else if (s->session->cipher != s->s3.tmp.new_cipher) { | 
815  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
816  | 0  |             return WORK_ERROR;  | 
817  | 0  |         }  | 
818  | 548  |         if (!ssl->method->ssl3_enc->setup_key_block(s)) { | 
819  |  |             /* SSLfatal() already called */  | 
820  | 0  |             return WORK_ERROR;  | 
821  | 0  |         }  | 
822  | 548  |         if (SSL_CONNECTION_IS_DTLS(s)) { | 
823  |  |             /*  | 
824  |  |              * We're into the last flight. We don't retransmit the last flight  | 
825  |  |              * unless we need to, so we don't use the timer. This might have  | 
826  |  |              * already been set to 0 if we sent a NewSessionTicket message,  | 
827  |  |              * but we'll set it again here in case we didn't.  | 
828  |  |              */  | 
829  | 0  |             st->use_timer = 0;  | 
830  | 0  |         }  | 
831  | 548  |         return WORK_FINISHED_CONTINUE;  | 
832  |  |  | 
833  | 1.72k  |     case TLS_ST_EARLY_DATA:  | 
834  | 1.72k  |         if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING  | 
835  | 1.72k  |                 && (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)  | 
836  | 1.01k  |             return WORK_FINISHED_CONTINUE;  | 
837  |  |         /* Fall through */  | 
838  |  |  | 
839  | 31.4k  |     case TLS_ST_OK:  | 
840  |  |         /* Calls SSLfatal() as required */  | 
841  | 31.4k  |         return tls_finish_handshake(s, wst, 1, 1);  | 
842  | 65.5k  |     }  | 
843  |  |  | 
844  | 25.6k  |     return WORK_FINISHED_CONTINUE;  | 
845  | 65.5k  | }  | 
846  |  |  | 
847  |  | static ossl_inline int conn_is_closed(void)  | 
848  | 0  | { | 
849  | 0  |     switch (get_last_sys_error()) { | 
850  | 0  | #if defined(EPIPE)  | 
851  | 0  |     case EPIPE:  | 
852  | 0  |         return 1;  | 
853  | 0  | #endif  | 
854  | 0  | #if defined(ECONNRESET)  | 
855  | 0  |     case ECONNRESET:  | 
856  | 0  |         return 1;  | 
857  | 0  | #endif  | 
858  |  | #if defined(WSAECONNRESET)  | 
859  |  |     case WSAECONNRESET:  | 
860  |  |         return 1;  | 
861  |  | #endif  | 
862  | 0  |     default:  | 
863  | 0  |         return 0;  | 
864  | 0  |     }  | 
865  | 0  | }  | 
866  |  |  | 
867  |  | /*  | 
868  |  |  * Perform any work that needs to be done after sending a message from the  | 
869  |  |  * server to the client.  | 
870  |  |  */  | 
871  |  | WORK_STATE ossl_statem_server_post_work(SSL_CONNECTION *s, WORK_STATE wst)  | 
872  | 12.9k  | { | 
873  | 12.9k  |     OSSL_STATEM *st = &s->statem;  | 
874  | 12.9k  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
875  |  |  | 
876  | 12.9k  |     s->init_num = 0;  | 
877  |  |  | 
878  | 12.9k  |     switch (st->hand_state) { | 
879  | 4.69k  |     default:  | 
880  |  |         /* No post work to be done */  | 
881  | 4.69k  |         break;  | 
882  |  |  | 
883  | 4.69k  |     case TLS_ST_SW_HELLO_REQ:  | 
884  | 0  |         if (statem_flush(s) != 1)  | 
885  | 0  |             return WORK_MORE_A;  | 
886  | 0  |         if (!ssl3_init_finished_mac(s)) { | 
887  |  |             /* SSLfatal() already called */  | 
888  | 0  |             return WORK_ERROR;  | 
889  | 0  |         }  | 
890  | 0  |         break;  | 
891  |  |  | 
892  | 0  |     case DTLS_ST_SW_HELLO_VERIFY_REQUEST:  | 
893  | 0  |         if (statem_flush(s) != 1)  | 
894  | 0  |             return WORK_MORE_A;  | 
895  |  |         /* HelloVerifyRequest resets Finished MAC */  | 
896  | 0  |         if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) { | 
897  |  |             /* SSLfatal() already called */  | 
898  | 0  |             return WORK_ERROR;  | 
899  | 0  |         }  | 
900  |  |         /*  | 
901  |  |          * The next message should be another ClientHello which we need to  | 
902  |  |          * treat like it was the first packet  | 
903  |  |          */  | 
904  | 0  |         s->first_packet = 1;  | 
905  | 0  |         break;  | 
906  |  |  | 
907  | 3.17k  |     case TLS_ST_SW_SRVR_HELLO:  | 
908  | 3.17k  |         if (SSL_CONNECTION_IS_TLS13(s)  | 
909  | 3.17k  |             && s->hello_retry_request == SSL_HRR_PENDING) { | 
910  | 109  |             if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0  | 
911  | 109  |                     && statem_flush(s) != 1)  | 
912  | 0  |                 return WORK_MORE_A;  | 
913  | 109  |             break;  | 
914  | 109  |         }  | 
915  |  | #ifndef OPENSSL_NO_SCTP  | 
916  |  |         if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { | 
917  |  |             unsigned char sctpauthkey[64];  | 
918  |  |             char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];  | 
919  |  |             size_t labellen;  | 
920  |  |  | 
921  |  |             /*  | 
922  |  |              * Add new shared key for SCTP-Auth, will be ignored if no  | 
923  |  |              * SCTP used.  | 
924  |  |              */  | 
925  |  |             memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,  | 
926  |  |                    sizeof(DTLS1_SCTP_AUTH_LABEL));  | 
927  |  |  | 
928  |  |             /* Don't include the terminating zero. */  | 
929  |  |             labellen = sizeof(labelbuffer) - 1;  | 
930  |  |             if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)  | 
931  |  |                 labellen += 1;  | 
932  |  |  | 
933  |  |             if (SSL_export_keying_material(ssl, sctpauthkey,  | 
934  |  |                                            sizeof(sctpauthkey), labelbuffer,  | 
935  |  |                                            labellen, NULL, 0,  | 
936  |  |                                            0) <= 0) { | 
937  |  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
938  |  |                 return WORK_ERROR;  | 
939  |  |             }  | 
940  |  |  | 
941  |  |             BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,  | 
942  |  |                      sizeof(sctpauthkey), sctpauthkey);  | 
943  |  |         }  | 
944  |  | #endif  | 
945  | 3.06k  |         if (!SSL_CONNECTION_IS_TLS13(s)  | 
946  | 3.06k  |                 || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0  | 
947  | 448  |                     && s->hello_retry_request != SSL_HRR_COMPLETE))  | 
948  | 3.04k  |             break;  | 
949  |  |         /* Fall through */  | 
950  |  |  | 
951  | 1.03k  |     case TLS_ST_SW_CHANGE:  | 
952  | 1.03k  |         if (s->hello_retry_request == SSL_HRR_PENDING) { | 
953  | 109  |             if (!statem_flush(s))  | 
954  | 0  |                 return WORK_MORE_A;  | 
955  | 109  |             break;  | 
956  | 109  |         }  | 
957  |  |  | 
958  | 925  |         if (SSL_CONNECTION_IS_TLS13(s)) { | 
959  | 448  |             if (!ssl->method->ssl3_enc->setup_key_block(s)  | 
960  | 448  |                 || !ssl->method->ssl3_enc->change_cipher_state(s,  | 
961  | 448  |                         SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) { | 
962  |  |                 /* SSLfatal() already called */  | 
963  | 0  |                 return WORK_ERROR;  | 
964  | 0  |             }  | 
965  |  |  | 
966  | 448  |             if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED  | 
967  | 448  |                 && !ssl->method->ssl3_enc->change_cipher_state(s,  | 
968  | 448  |                         SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) { | 
969  |  |                 /* SSLfatal() already called */  | 
970  | 0  |                 return WORK_ERROR;  | 
971  | 0  |             }  | 
972  |  |             /*  | 
973  |  |              * We don't yet know whether the next record we are going to receive  | 
974  |  |              * is an unencrypted alert, an encrypted alert, or an encrypted  | 
975  |  |              * handshake message. We temporarily tolerate unencrypted alerts.  | 
976  |  |              */  | 
977  | 448  |             if (s->rlayer.rrlmethod->set_plain_alerts != NULL)  | 
978  | 448  |                 s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 1);  | 
979  | 448  |             break;  | 
980  | 448  |         }  | 
981  |  |  | 
982  |  | #ifndef OPENSSL_NO_SCTP  | 
983  |  |         if (SSL_CONNECTION_IS_DTLS(s) && !s->hit) { | 
984  |  |             /*  | 
985  |  |              * Change to new shared key of SCTP-Auth, will be ignored if  | 
986  |  |              * no SCTP used.  | 
987  |  |              */  | 
988  |  |             BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,  | 
989  |  |                      0, NULL);  | 
990  |  |         }  | 
991  |  | #endif  | 
992  | 477  |         if (!ssl->method->ssl3_enc->change_cipher_state(s,  | 
993  | 477  |                                 SSL3_CHANGE_CIPHER_SERVER_WRITE)) { | 
994  |  |             /* SSLfatal() already called */  | 
995  | 0  |             return WORK_ERROR;  | 
996  | 0  |         }  | 
997  |  |  | 
998  | 477  |         if (SSL_CONNECTION_IS_DTLS(s))  | 
999  | 0  |             dtls1_increment_epoch(s, SSL3_CC_WRITE);  | 
1000  | 477  |         break;  | 
1001  |  |  | 
1002  | 2.58k  |     case TLS_ST_SW_SRVR_DONE:  | 
1003  | 2.58k  |         if (statem_flush(s) != 1)  | 
1004  | 0  |             return WORK_MORE_A;  | 
1005  | 2.58k  |         break;  | 
1006  |  |  | 
1007  | 2.58k  |     case TLS_ST_SW_FINISHED:  | 
1008  | 925  |         if (statem_flush(s) != 1)  | 
1009  | 0  |             return WORK_MORE_A;  | 
1010  |  | #ifndef OPENSSL_NO_SCTP  | 
1011  |  |         if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { | 
1012  |  |             /*  | 
1013  |  |              * Change to new shared key of SCTP-Auth, will be ignored if  | 
1014  |  |              * no SCTP used.  | 
1015  |  |              */  | 
1016  |  |             BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,  | 
1017  |  |                      0, NULL);  | 
1018  |  |         }  | 
1019  |  | #endif  | 
1020  | 925  |         if (SSL_CONNECTION_IS_TLS13(s)) { | 
1021  |  |             /* TLS 1.3 gets the secret size from the handshake md */  | 
1022  | 448  |             size_t dummy;  | 
1023  | 448  |             if (!ssl->method->ssl3_enc->generate_master_secret(s,  | 
1024  | 448  |                         s->master_secret, s->handshake_secret, 0,  | 
1025  | 448  |                         &dummy)  | 
1026  | 448  |                 || !ssl->method->ssl3_enc->change_cipher_state(s,  | 
1027  | 448  |                         SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))  | 
1028  |  |             /* SSLfatal() already called */  | 
1029  | 0  |             return WORK_ERROR;  | 
1030  | 448  |         }  | 
1031  | 925  |         break;  | 
1032  |  |  | 
1033  | 925  |     case TLS_ST_SW_CERT_REQ:  | 
1034  | 0  |         if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { | 
1035  | 0  |             if (statem_flush(s) != 1)  | 
1036  | 0  |                 return WORK_MORE_A;  | 
1037  | 0  |         } else { | 
1038  | 0  |             if (!SSL_CONNECTION_IS_TLS13(s)  | 
1039  | 0  |                     || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)  | 
1040  | 0  |                 s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;  | 
1041  | 0  |         }  | 
1042  | 0  |         break;  | 
1043  |  |  | 
1044  | 448  |     case TLS_ST_SW_ENCRYPTED_EXTENSIONS:  | 
1045  | 448  |         if (!s->hit && !send_certificate_request(s)) { | 
1046  | 448  |             if (!SSL_CONNECTION_IS_TLS13(s)  | 
1047  | 448  |                     || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)  | 
1048  | 0  |                 s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;  | 
1049  | 448  |         }  | 
1050  | 448  |         break;  | 
1051  |  |  | 
1052  | 0  |     case TLS_ST_SW_KEY_UPDATE:  | 
1053  | 0  |         if (statem_flush(s) != 1)  | 
1054  | 0  |             return WORK_MORE_A;  | 
1055  | 0  |         if (!tls13_update_key(s, 1)) { | 
1056  |  |             /* SSLfatal() already called */  | 
1057  | 0  |             return WORK_ERROR;  | 
1058  | 0  |         }  | 
1059  | 0  |         break;  | 
1060  |  |  | 
1061  | 87  |     case TLS_ST_SW_SESSION_TICKET:  | 
1062  | 87  |         clear_sys_error();  | 
1063  | 87  |         if (SSL_CONNECTION_IS_TLS13(s) && statem_flush(s) != 1) { | 
1064  | 0  |             if (SSL_get_error(ssl, 0) == SSL_ERROR_SYSCALL  | 
1065  | 0  |                     && conn_is_closed()) { | 
1066  |  |                 /*  | 
1067  |  |                  * We ignore connection closed errors in TLSv1.3 when sending a  | 
1068  |  |                  * NewSessionTicket and behave as if we were successful. This is  | 
1069  |  |                  * so that we are still able to read data sent to us by a client  | 
1070  |  |                  * that closes soon after the end of the handshake without  | 
1071  |  |                  * waiting to read our post-handshake NewSessionTickets.  | 
1072  |  |                  */  | 
1073  | 0  |                 s->rwstate = SSL_NOTHING;  | 
1074  | 0  |                 break;  | 
1075  | 0  |             }  | 
1076  |  |  | 
1077  | 0  |             return WORK_MORE_A;  | 
1078  | 0  |         }  | 
1079  | 87  |         break;  | 
1080  | 12.9k  |     }  | 
1081  |  |  | 
1082  | 12.9k  |     return WORK_FINISHED_CONTINUE;  | 
1083  | 12.9k  | }  | 
1084  |  |  | 
1085  |  | /*  | 
1086  |  |  * Get the message construction function and message type for sending from the  | 
1087  |  |  * server  | 
1088  |  |  *  | 
1089  |  |  * Valid return values are:  | 
1090  |  |  *   1: Success  | 
1091  |  |  *   0: Error  | 
1092  |  |  */  | 
1093  |  | int ossl_statem_server_construct_message(SSL_CONNECTION *s,  | 
1094  |  |                                          confunc_f *confunc, int *mt)  | 
1095  | 56.8k  | { | 
1096  | 56.8k  |     OSSL_STATEM *st = &s->statem;  | 
1097  |  |  | 
1098  | 56.8k  |     switch (st->hand_state) { | 
1099  | 0  |     default:  | 
1100  |  |         /* Shouldn't happen */  | 
1101  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);  | 
1102  | 0  |         return 0;  | 
1103  |  |  | 
1104  | 3.44k  |     case TLS_ST_SW_CHANGE:  | 
1105  | 3.44k  |         if (SSL_CONNECTION_IS_DTLS(s))  | 
1106  | 0  |             *confunc = dtls_construct_change_cipher_spec;  | 
1107  | 3.44k  |         else  | 
1108  | 3.44k  |             *confunc = tls_construct_change_cipher_spec;  | 
1109  | 3.44k  |         *mt = SSL3_MT_CHANGE_CIPHER_SPEC;  | 
1110  | 3.44k  |         break;  | 
1111  |  |  | 
1112  | 0  |     case DTLS_ST_SW_HELLO_VERIFY_REQUEST:  | 
1113  | 0  |         *confunc = dtls_construct_hello_verify_request;  | 
1114  | 0  |         *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;  | 
1115  | 0  |         break;  | 
1116  |  |  | 
1117  | 0  |     case TLS_ST_SW_HELLO_REQ:  | 
1118  |  |         /* No construction function needed */  | 
1119  | 0  |         *confunc = NULL;  | 
1120  | 0  |         *mt = SSL3_MT_HELLO_REQUEST;  | 
1121  | 0  |         break;  | 
1122  |  |  | 
1123  | 14.6k  |     case TLS_ST_SW_SRVR_HELLO:  | 
1124  | 14.6k  |         *confunc = tls_construct_server_hello;  | 
1125  | 14.6k  |         *mt = SSL3_MT_SERVER_HELLO;  | 
1126  | 14.6k  |         break;  | 
1127  |  |  | 
1128  | 13.2k  |     case TLS_ST_SW_CERT:  | 
1129  | 13.2k  |         *confunc = tls_construct_server_certificate;  | 
1130  | 13.2k  |         *mt = SSL3_MT_CERTIFICATE;  | 
1131  | 13.2k  |         break;  | 
1132  |  |  | 
1133  |  | #ifndef OPENSSL_NO_COMP_ALG  | 
1134  |  |     case TLS_ST_SW_COMP_CERT:  | 
1135  |  |         *confunc = tls_construct_server_compressed_certificate;  | 
1136  |  |         *mt = SSL3_MT_COMPRESSED_CERTIFICATE;  | 
1137  |  |         break;  | 
1138  |  | #endif  | 
1139  |  |  | 
1140  | 1.96k  |     case TLS_ST_SW_CERT_VRFY:  | 
1141  | 1.96k  |         *confunc = tls_construct_cert_verify;  | 
1142  | 1.96k  |         *mt = SSL3_MT_CERTIFICATE_VERIFY;  | 
1143  | 1.96k  |         break;  | 
1144  |  |  | 
1145  |  |  | 
1146  | 5.03k  |     case TLS_ST_SW_KEY_EXCH:  | 
1147  | 5.03k  |         *confunc = tls_construct_server_key_exchange;  | 
1148  | 5.03k  |         *mt = SSL3_MT_SERVER_KEY_EXCHANGE;  | 
1149  | 5.03k  |         break;  | 
1150  |  |  | 
1151  | 0  |     case TLS_ST_SW_CERT_REQ:  | 
1152  | 0  |         *confunc = tls_construct_certificate_request;  | 
1153  | 0  |         *mt = SSL3_MT_CERTIFICATE_REQUEST;  | 
1154  | 0  |         break;  | 
1155  |  |  | 
1156  | 12.0k  |     case TLS_ST_SW_SRVR_DONE:  | 
1157  | 12.0k  |         *confunc = tls_construct_server_done;  | 
1158  | 12.0k  |         *mt = SSL3_MT_SERVER_DONE;  | 
1159  | 12.0k  |         break;  | 
1160  |  |  | 
1161  | 96  |     case TLS_ST_SW_SESSION_TICKET:  | 
1162  | 96  |         *confunc = tls_construct_new_session_ticket;  | 
1163  | 96  |         *mt = SSL3_MT_NEWSESSION_TICKET;  | 
1164  | 96  |         break;  | 
1165  |  |  | 
1166  | 0  |     case TLS_ST_SW_CERT_STATUS:  | 
1167  | 0  |         *confunc = tls_construct_cert_status;  | 
1168  | 0  |         *mt = SSL3_MT_CERTIFICATE_STATUS;  | 
1169  | 0  |         break;  | 
1170  |  |  | 
1171  | 3.02k  |     case TLS_ST_SW_FINISHED:  | 
1172  | 3.02k  |         *confunc = tls_construct_finished;  | 
1173  | 3.02k  |         *mt = SSL3_MT_FINISHED;  | 
1174  | 3.02k  |         break;  | 
1175  |  |  | 
1176  | 1.47k  |     case TLS_ST_EARLY_DATA:  | 
1177  | 1.47k  |         *confunc = NULL;  | 
1178  | 1.47k  |         *mt = SSL3_MT_DUMMY;  | 
1179  | 1.47k  |         break;  | 
1180  |  |  | 
1181  | 1.96k  |     case TLS_ST_SW_ENCRYPTED_EXTENSIONS:  | 
1182  | 1.96k  |         *confunc = tls_construct_encrypted_extensions;  | 
1183  | 1.96k  |         *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;  | 
1184  | 1.96k  |         break;  | 
1185  |  |  | 
1186  | 0  |     case TLS_ST_SW_KEY_UPDATE:  | 
1187  | 0  |         *confunc = tls_construct_key_update;  | 
1188  | 0  |         *mt = SSL3_MT_KEY_UPDATE;  | 
1189  | 0  |         break;  | 
1190  | 56.8k  |     }  | 
1191  |  |  | 
1192  | 56.8k  |     return 1;  | 
1193  | 56.8k  | }  | 
1194  |  |  | 
1195  |  | /*  | 
1196  |  |  * Maximum size (excluding the Handshake header) of a ClientHello message,  | 
1197  |  |  * calculated as follows:  | 
1198  |  |  *  | 
1199  |  |  *  2 + # client_version  | 
1200  |  |  *  32 + # only valid length for random  | 
1201  |  |  *  1 + # length of session_id  | 
1202  |  |  *  32 + # maximum size for session_id  | 
1203  |  |  *  2 + # length of cipher suites  | 
1204  |  |  *  2^16-2 + # maximum length of cipher suites array  | 
1205  |  |  *  1 + # length of compression_methods  | 
1206  |  |  *  2^8-1 + # maximum length of compression methods  | 
1207  |  |  *  2 + # length of extensions  | 
1208  |  |  *  2^16-1 # maximum length of extensions  | 
1209  |  |  */  | 
1210  | 78.6k  | #define CLIENT_HELLO_MAX_LENGTH         131396  | 
1211  |  |  | 
1212  | 6.41k  | #define CLIENT_KEY_EXCH_MAX_LENGTH      2048  | 
1213  | 0  | #define NEXT_PROTO_MAX_LENGTH           514  | 
1214  |  |  | 
1215  |  | /*  | 
1216  |  |  * Returns the maximum allowed length for the current message that we are  | 
1217  |  |  * reading. Excludes the message header.  | 
1218  |  |  */  | 
1219  |  | size_t ossl_statem_server_max_message_size(SSL_CONNECTION *s)  | 
1220  | 89.7k  | { | 
1221  | 89.7k  |     OSSL_STATEM *st = &s->statem;  | 
1222  |  |  | 
1223  | 89.7k  |     switch (st->hand_state) { | 
1224  | 0  |     default:  | 
1225  |  |         /* Shouldn't happen */  | 
1226  | 0  |         return 0;  | 
1227  |  |  | 
1228  | 78.6k  |     case TLS_ST_SR_CLNT_HELLO:  | 
1229  | 78.6k  |         return CLIENT_HELLO_MAX_LENGTH;  | 
1230  |  |  | 
1231  | 0  |     case TLS_ST_SR_END_OF_EARLY_DATA:  | 
1232  | 0  |         return END_OF_EARLY_DATA_MAX_LENGTH;  | 
1233  |  |  | 
1234  | 0  |     case TLS_ST_SR_COMP_CERT:  | 
1235  | 0  |     case TLS_ST_SR_CERT:  | 
1236  | 0  |         return s->max_cert_list;  | 
1237  |  |  | 
1238  | 6.41k  |     case TLS_ST_SR_KEY_EXCH:  | 
1239  | 6.41k  |         return CLIENT_KEY_EXCH_MAX_LENGTH;  | 
1240  |  |  | 
1241  | 0  |     case TLS_ST_SR_CERT_VRFY:  | 
1242  | 0  |         return CERTIFICATE_VERIFY_MAX_LENGTH;  | 
1243  |  |  | 
1244  | 0  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
1245  | 0  |     case TLS_ST_SR_NEXT_PROTO:  | 
1246  | 0  |         return NEXT_PROTO_MAX_LENGTH;  | 
1247  | 0  | #endif  | 
1248  |  |  | 
1249  | 3.76k  |     case TLS_ST_SR_CHANGE:  | 
1250  | 3.76k  |         return CCS_MAX_LENGTH;  | 
1251  |  |  | 
1252  | 976  |     case TLS_ST_SR_FINISHED:  | 
1253  | 976  |         return FINISHED_MAX_LENGTH;  | 
1254  |  |  | 
1255  | 0  |     case TLS_ST_SR_KEY_UPDATE:  | 
1256  | 0  |         return KEY_UPDATE_MAX_LENGTH;  | 
1257  | 89.7k  |     }  | 
1258  | 89.7k  | }  | 
1259  |  |  | 
1260  |  | /*  | 
1261  |  |  * Process a message that the server has received from the client.  | 
1262  |  |  */  | 
1263  |  | MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL_CONNECTION *s,  | 
1264  |  |                                                       PACKET *pkt)  | 
1265  | 103k  | { | 
1266  | 103k  |     OSSL_STATEM *st = &s->statem;  | 
1267  |  |  | 
1268  | 103k  |     switch (st->hand_state) { | 
1269  | 0  |     default:  | 
1270  |  |         /* Shouldn't happen */  | 
1271  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1272  | 0  |         return MSG_PROCESS_ERROR;  | 
1273  |  |  | 
1274  | 87.2k  |     case TLS_ST_SR_CLNT_HELLO:  | 
1275  | 87.2k  |         return tls_process_client_hello(s, pkt);  | 
1276  |  |  | 
1277  | 0  |     case TLS_ST_SR_END_OF_EARLY_DATA:  | 
1278  | 0  |         return tls_process_end_of_early_data(s, pkt);  | 
1279  |  |  | 
1280  | 0  |     case TLS_ST_SR_CERT:  | 
1281  | 0  |         return tls_process_client_certificate(s, pkt);  | 
1282  |  |  | 
1283  |  | #ifndef OPENSSL_NO_COMP_ALG  | 
1284  |  |     case TLS_ST_SR_COMP_CERT:  | 
1285  |  |         return tls_process_client_compressed_certificate(s, pkt);  | 
1286  |  | #endif  | 
1287  |  |  | 
1288  | 9.80k  |     case TLS_ST_SR_KEY_EXCH:  | 
1289  | 9.80k  |         return tls_process_client_key_exchange(s, pkt);  | 
1290  |  |  | 
1291  | 0  |     case TLS_ST_SR_CERT_VRFY:  | 
1292  | 0  |         return tls_process_cert_verify(s, pkt);  | 
1293  |  |  | 
1294  | 0  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
1295  | 0  |     case TLS_ST_SR_NEXT_PROTO:  | 
1296  | 0  |         return tls_process_next_proto(s, pkt);  | 
1297  | 0  | #endif  | 
1298  |  |  | 
1299  | 4.99k  |     case TLS_ST_SR_CHANGE:  | 
1300  | 4.99k  |         return tls_process_change_cipher_spec(s, pkt);  | 
1301  |  |  | 
1302  | 967  |     case TLS_ST_SR_FINISHED:  | 
1303  | 967  |         return tls_process_finished(s, pkt);  | 
1304  |  |  | 
1305  | 0  |     case TLS_ST_SR_KEY_UPDATE:  | 
1306  | 0  |         return tls_process_key_update(s, pkt);  | 
1307  |  |  | 
1308  | 103k  |     }  | 
1309  | 103k  | }  | 
1310  |  |  | 
1311  |  | /*  | 
1312  |  |  * Perform any further processing required following the receipt of a message  | 
1313  |  |  * from the client  | 
1314  |  |  */  | 
1315  |  | WORK_STATE ossl_statem_server_post_process_message(SSL_CONNECTION *s,  | 
1316  |  |                                                    WORK_STATE wst)  | 
1317  | 30.5k  | { | 
1318  | 30.5k  |     OSSL_STATEM *st = &s->statem;  | 
1319  |  |  | 
1320  | 30.5k  |     switch (st->hand_state) { | 
1321  | 0  |     default:  | 
1322  |  |         /* Shouldn't happen */  | 
1323  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1324  | 0  |         return WORK_ERROR;  | 
1325  |  |  | 
1326  | 21.9k  |     case TLS_ST_SR_CLNT_HELLO:  | 
1327  | 21.9k  |         return tls_post_process_client_hello(s, wst);  | 
1328  |  |  | 
1329  | 8.69k  |     case TLS_ST_SR_KEY_EXCH:  | 
1330  | 8.69k  |         return tls_post_process_client_key_exchange(s, wst);  | 
1331  | 30.5k  |     }  | 
1332  | 30.5k  | }  | 
1333  |  |  | 
1334  |  | #ifndef OPENSSL_NO_SRP  | 
1335  |  | /* Returns 1 on success, 0 for retryable error, -1 for fatal error */  | 
1336  |  | static int ssl_check_srp_ext_ClientHello(SSL_CONNECTION *s)  | 
1337  | 14.6k  | { | 
1338  | 14.6k  |     int ret;  | 
1339  | 14.6k  |     int al = SSL_AD_UNRECOGNIZED_NAME;  | 
1340  |  |  | 
1341  | 14.6k  |     if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&  | 
1342  | 14.6k  |         (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) { | 
1343  | 0  |         if (s->srp_ctx.login == NULL) { | 
1344  |  |             /*  | 
1345  |  |              * RFC 5054 says SHOULD reject, we do so if There is no srp  | 
1346  |  |              * login name  | 
1347  |  |              */  | 
1348  | 0  |             SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,  | 
1349  | 0  |                      SSL_R_PSK_IDENTITY_NOT_FOUND);  | 
1350  | 0  |             return -1;  | 
1351  | 0  |         } else { | 
1352  | 0  |             ret = ssl_srp_server_param_with_username_intern(s, &al);  | 
1353  | 0  |             if (ret < 0)  | 
1354  | 0  |                 return 0;  | 
1355  | 0  |             if (ret == SSL3_AL_FATAL) { | 
1356  | 0  |                 SSLfatal(s, al,  | 
1357  | 0  |                          al == SSL_AD_UNKNOWN_PSK_IDENTITY  | 
1358  | 0  |                          ? SSL_R_PSK_IDENTITY_NOT_FOUND  | 
1359  | 0  |                          : SSL_R_CLIENTHELLO_TLSEXT);  | 
1360  | 0  |                 return -1;  | 
1361  | 0  |             }  | 
1362  | 0  |         }  | 
1363  | 0  |     }  | 
1364  | 14.6k  |     return 1;  | 
1365  | 14.6k  | }  | 
1366  |  | #endif  | 
1367  |  |  | 
1368  |  | int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,  | 
1369  |  |                                   size_t cookie_len)  | 
1370  | 0  | { | 
1371  |  |     /* Always use DTLS 1.0 version: see RFC 6347 */  | 
1372  | 0  |     if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)  | 
1373  | 0  |             || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))  | 
1374  | 0  |         return 0;  | 
1375  |  |  | 
1376  | 0  |     return 1;  | 
1377  | 0  | }  | 
1378  |  |  | 
1379  |  | CON_FUNC_RETURN dtls_construct_hello_verify_request(SSL_CONNECTION *s,  | 
1380  |  |                                                     WPACKET *pkt)  | 
1381  | 0  | { | 
1382  | 0  |     unsigned int cookie_leni;  | 
1383  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
1384  |  | 
  | 
1385  | 0  |     if (sctx->app_gen_cookie_cb == NULL  | 
1386  | 0  |         || sctx->app_gen_cookie_cb(SSL_CONNECTION_GET_USER_SSL(s), s->d1->cookie,  | 
1387  | 0  |                                    &cookie_leni) == 0  | 
1388  | 0  |         || cookie_leni > DTLS1_COOKIE_LENGTH) { | 
1389  | 0  |         SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);  | 
1390  | 0  |         return CON_FUNC_ERROR;  | 
1391  | 0  |     }  | 
1392  | 0  |     s->d1->cookie_len = cookie_leni;  | 
1393  |  | 
  | 
1394  | 0  |     if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,  | 
1395  | 0  |                                        s->d1->cookie_len)) { | 
1396  | 0  |         SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);  | 
1397  | 0  |         return CON_FUNC_ERROR;  | 
1398  | 0  |     }  | 
1399  |  |  | 
1400  | 0  |     return CON_FUNC_SUCCESS;  | 
1401  | 0  | }  | 
1402  |  |  | 
1403  |  | /*-  | 
1404  |  |  * ssl_check_for_safari attempts to fingerprint Safari using OS X  | 
1405  |  |  * SecureTransport using the TLS extension block in |hello|.  | 
1406  |  |  * Safari, since 10.6, sends exactly these extensions, in this order:  | 
1407  |  |  *   SNI,  | 
1408  |  |  *   elliptic_curves  | 
1409  |  |  *   ec_point_formats  | 
1410  |  |  *   signature_algorithms (for TLSv1.2 only)  | 
1411  |  |  *  | 
1412  |  |  * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,  | 
1413  |  |  * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.  | 
1414  |  |  * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from  | 
1415  |  |  * 10.8..10.8.3 (which don't work).  | 
1416  |  |  */  | 
1417  |  | static void ssl_check_for_safari(SSL_CONNECTION *s,  | 
1418  |  |                                  const CLIENTHELLO_MSG *hello)  | 
1419  | 0  | { | 
1420  | 0  |     static const unsigned char kSafariExtensionsBlock[] = { | 
1421  | 0  |         0x00, 0x0a,             /* elliptic_curves extension */  | 
1422  | 0  |         0x00, 0x08,             /* 8 bytes */  | 
1423  | 0  |         0x00, 0x06,             /* 6 bytes of curve ids */  | 
1424  | 0  |         0x00, 0x17,             /* P-256 */  | 
1425  | 0  |         0x00, 0x18,             /* P-384 */  | 
1426  | 0  |         0x00, 0x19,             /* P-521 */  | 
1427  |  | 
  | 
1428  | 0  |         0x00, 0x0b,             /* ec_point_formats */  | 
1429  | 0  |         0x00, 0x02,             /* 2 bytes */  | 
1430  | 0  |         0x01,                   /* 1 point format */  | 
1431  | 0  |         0x00,                   /* uncompressed */  | 
1432  |  |         /* The following is only present in TLS 1.2 */  | 
1433  | 0  |         0x00, 0x0d,             /* signature_algorithms */  | 
1434  | 0  |         0x00, 0x0c,             /* 12 bytes */  | 
1435  | 0  |         0x00, 0x0a,             /* 10 bytes */  | 
1436  | 0  |         0x05, 0x01,             /* SHA-384/RSA */  | 
1437  | 0  |         0x04, 0x01,             /* SHA-256/RSA */  | 
1438  | 0  |         0x02, 0x01,             /* SHA-1/RSA */  | 
1439  | 0  |         0x04, 0x03,             /* SHA-256/ECDSA */  | 
1440  | 0  |         0x02, 0x03,             /* SHA-1/ECDSA */  | 
1441  | 0  |     };  | 
1442  |  |     /* Length of the common prefix (first two extensions). */  | 
1443  | 0  |     static const size_t kSafariCommonExtensionsLength = 18;  | 
1444  | 0  |     unsigned int type;  | 
1445  | 0  |     PACKET sni, tmppkt;  | 
1446  | 0  |     size_t ext_len;  | 
1447  |  | 
  | 
1448  | 0  |     tmppkt = hello->extensions;  | 
1449  |  | 
  | 
1450  | 0  |     if (!PACKET_forward(&tmppkt, 2)  | 
1451  | 0  |         || !PACKET_get_net_2(&tmppkt, &type)  | 
1452  | 0  |         || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) { | 
1453  | 0  |         return;  | 
1454  | 0  |     }  | 
1455  |  |  | 
1456  | 0  |     if (type != TLSEXT_TYPE_server_name)  | 
1457  | 0  |         return;  | 
1458  |  |  | 
1459  | 0  |     ext_len = TLS1_get_client_version(  | 
1460  | 0  |         SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION ?  | 
1461  | 0  |                       sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;  | 
1462  |  | 
  | 
1463  | 0  |     s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,  | 
1464  | 0  |                                              ext_len);  | 
1465  | 0  | }  | 
1466  |  |  | 
1467  |  | #define RENEG_OPTIONS_OK(options) \  | 
1468  | 129k  |     ((options & SSL_OP_NO_RENEGOTIATION) == 0 \  | 
1469  | 129k  |      && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0)  | 
1470  |  |  | 
1471  |  | MSG_PROCESS_RETURN tls_process_client_hello(SSL_CONNECTION *s, PACKET *pkt)  | 
1472  | 87.2k  | { | 
1473  |  |     /* |cookie| will only be initialized for DTLS. */  | 
1474  | 87.2k  |     PACKET session_id, compression, extensions, cookie;  | 
1475  | 87.2k  |     static const unsigned char null_compression = 0;  | 
1476  | 87.2k  |     CLIENTHELLO_MSG *clienthello = NULL;  | 
1477  |  |  | 
1478  |  |     /* Check if this is actually an unexpected renegotiation ClientHello */  | 
1479  | 87.2k  |     if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) { | 
1480  | 64.5k  |         if (!ossl_assert(!SSL_CONNECTION_IS_TLS13(s))) { | 
1481  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1482  | 0  |             goto err;  | 
1483  | 0  |         }  | 
1484  | 64.5k  |         if (!RENEG_OPTIONS_OK(s->options)  | 
1485  | 64.5k  |                 || (!s->s3.send_connection_binding  | 
1486  | 0  |                     && (s->options  | 
1487  | 64.5k  |                         & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) { | 
1488  | 64.5k  |             ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);  | 
1489  | 64.5k  |             return MSG_PROCESS_FINISHED_READING;  | 
1490  | 64.5k  |         }  | 
1491  | 0  |         s->renegotiate = 1;  | 
1492  | 0  |         s->new_session = 1;  | 
1493  | 0  |     }  | 
1494  |  |  | 
1495  | 22.7k  |     clienthello = OPENSSL_zalloc(sizeof(*clienthello));  | 
1496  | 22.7k  |     if (clienthello == NULL) { | 
1497  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1498  | 0  |         goto err;  | 
1499  | 0  |     }  | 
1500  |  |  | 
1501  |  |     /*  | 
1502  |  |      * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.  | 
1503  |  |      */  | 
1504  | 22.7k  |     clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);  | 
1505  | 22.7k  |     PACKET_null_init(&cookie);  | 
1506  |  |  | 
1507  | 22.7k  |     if (clienthello->isv2) { | 
1508  | 5.99k  |         unsigned int mt;  | 
1509  |  |  | 
1510  | 5.99k  |         if (!SSL_IS_FIRST_HANDSHAKE(s)  | 
1511  | 5.99k  |                 || s->hello_retry_request != SSL_HRR_NONE) { | 
1512  | 0  |             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);  | 
1513  | 0  |             goto err;  | 
1514  | 0  |         }  | 
1515  |  |  | 
1516  |  |         /*-  | 
1517  |  |          * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2  | 
1518  |  |          * header is sent directly on the wire, not wrapped as a TLS  | 
1519  |  |          * record. Our record layer just processes the message length and passes  | 
1520  |  |          * the rest right through. Its format is:  | 
1521  |  |          * Byte  Content  | 
1522  |  |          * 0-1   msg_length - decoded by the record layer  | 
1523  |  |          * 2     msg_type - s->init_msg points here  | 
1524  |  |          * 3-4   version  | 
1525  |  |          * 5-6   cipher_spec_length  | 
1526  |  |          * 7-8   session_id_length  | 
1527  |  |          * 9-10  challenge_length  | 
1528  |  |          * ...   ...  | 
1529  |  |          */  | 
1530  |  |  | 
1531  | 5.99k  |         if (!PACKET_get_1(pkt, &mt)  | 
1532  | 5.99k  |             || mt != SSL2_MT_CLIENT_HELLO) { | 
1533  |  |             /*  | 
1534  |  |              * Should never happen. We should have tested this in the record  | 
1535  |  |              * layer in order to have determined that this is a SSLv2 record  | 
1536  |  |              * in the first place  | 
1537  |  |              */  | 
1538  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1539  | 0  |             goto err;  | 
1540  | 0  |         }  | 
1541  | 5.99k  |     }  | 
1542  |  |  | 
1543  | 22.7k  |     if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) { | 
1544  | 34  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);  | 
1545  | 34  |         goto err;  | 
1546  | 34  |     }  | 
1547  |  |  | 
1548  |  |     /* Parse the message and load client random. */  | 
1549  | 22.7k  |     if (clienthello->isv2) { | 
1550  |  |         /*  | 
1551  |  |          * Handle an SSLv2 backwards compatible ClientHello  | 
1552  |  |          * Note, this is only for SSLv3+ using the backward compatible format.  | 
1553  |  |          * Real SSLv2 is not supported, and is rejected below.  | 
1554  |  |          */  | 
1555  | 5.99k  |         unsigned int ciphersuite_len, session_id_len, challenge_len;  | 
1556  | 5.99k  |         PACKET challenge;  | 
1557  |  |  | 
1558  | 5.99k  |         if (!PACKET_get_net_2(pkt, &ciphersuite_len)  | 
1559  | 5.99k  |             || !PACKET_get_net_2(pkt, &session_id_len)  | 
1560  | 5.99k  |             || !PACKET_get_net_2(pkt, &challenge_len)) { | 
1561  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);  | 
1562  | 0  |             goto err;  | 
1563  | 0  |         }  | 
1564  |  |  | 
1565  | 5.99k  |         if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { | 
1566  | 93  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_MISMATCH);  | 
1567  | 93  |             goto err;  | 
1568  | 93  |         }  | 
1569  |  |  | 
1570  | 5.89k  |         if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,  | 
1571  | 5.89k  |                                    ciphersuite_len)  | 
1572  | 5.89k  |             || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)  | 
1573  | 5.89k  |             || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)  | 
1574  |  |             /* No extensions. */  | 
1575  | 5.89k  |             || PACKET_remaining(pkt) != 0) { | 
1576  | 285  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);  | 
1577  | 285  |             goto err;  | 
1578  | 285  |         }  | 
1579  | 5.61k  |         clienthello->session_id_len = session_id_len;  | 
1580  |  |  | 
1581  |  |         /* Load the client random and compression list. We use SSL3_RANDOM_SIZE  | 
1582  |  |          * here rather than sizeof(clienthello->random) because that is the limit  | 
1583  |  |          * for SSLv3 and it is fixed. It won't change even if  | 
1584  |  |          * sizeof(clienthello->random) does.  | 
1585  |  |          */  | 
1586  | 5.61k  |         challenge_len = challenge_len > SSL3_RANDOM_SIZE  | 
1587  | 5.61k  |                         ? SSL3_RANDOM_SIZE : challenge_len;  | 
1588  | 5.61k  |         memset(clienthello->random, 0, SSL3_RANDOM_SIZE);  | 
1589  | 5.61k  |         if (!PACKET_copy_bytes(&challenge,  | 
1590  | 5.61k  |                                clienthello->random + SSL3_RANDOM_SIZE -  | 
1591  | 5.61k  |                                challenge_len, challenge_len)  | 
1592  |  |             /* Advertise only null compression. */  | 
1593  | 5.61k  |             || !PACKET_buf_init(&compression, &null_compression, 1)) { | 
1594  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1595  | 0  |             goto err;  | 
1596  | 0  |         }  | 
1597  |  |  | 
1598  | 5.61k  |         PACKET_null_init(&clienthello->extensions);  | 
1599  | 16.7k  |     } else { | 
1600  |  |         /* Regular ClientHello. */  | 
1601  | 16.7k  |         if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)  | 
1602  | 16.7k  |             || !PACKET_get_length_prefixed_1(pkt, &session_id)  | 
1603  | 16.7k  |             || !PACKET_copy_all(&session_id, clienthello->session_id,  | 
1604  | 16.6k  |                     SSL_MAX_SSL_SESSION_ID_LENGTH,  | 
1605  | 16.6k  |                     &clienthello->session_id_len)) { | 
1606  | 78  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
1607  | 78  |             goto err;  | 
1608  | 78  |         }  | 
1609  |  |  | 
1610  | 16.6k  |         if (SSL_CONNECTION_IS_DTLS(s)) { | 
1611  | 3.54k  |             if (!PACKET_get_length_prefixed_1(pkt, &cookie)) { | 
1612  | 5  |                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
1613  | 5  |                 goto err;  | 
1614  | 5  |             }  | 
1615  | 3.53k  |             if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,  | 
1616  | 3.53k  |                                  DTLS1_COOKIE_LENGTH,  | 
1617  | 3.53k  |                                  &clienthello->dtls_cookie_len)) { | 
1618  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1619  | 0  |                 goto err;  | 
1620  | 0  |             }  | 
1621  |  |             /*  | 
1622  |  |              * If we require cookies and this ClientHello doesn't contain one,  | 
1623  |  |              * just return since we do not want to allocate any memory yet.  | 
1624  |  |              * So check cookie length...  | 
1625  |  |              */  | 
1626  | 3.53k  |             if (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE) { | 
1627  | 0  |                 if (clienthello->dtls_cookie_len == 0) { | 
1628  | 0  |                     OPENSSL_free(clienthello);  | 
1629  | 0  |                     return MSG_PROCESS_FINISHED_READING;  | 
1630  | 0  |                 }  | 
1631  | 0  |             }  | 
1632  | 3.53k  |         }  | 
1633  |  |  | 
1634  | 16.6k  |         if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) { | 
1635  | 66  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
1636  | 66  |             goto err;  | 
1637  | 66  |         }  | 
1638  |  |  | 
1639  | 16.5k  |         if (!PACKET_get_length_prefixed_1(pkt, &compression)) { | 
1640  | 25  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
1641  | 25  |             goto err;  | 
1642  | 25  |         }  | 
1643  |  |  | 
1644  |  |         /* Could be empty. */  | 
1645  | 16.5k  |         if (PACKET_remaining(pkt) == 0) { | 
1646  | 748  |             PACKET_null_init(&clienthello->extensions);  | 
1647  | 15.8k  |         } else { | 
1648  | 15.8k  |             if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)  | 
1649  | 15.8k  |                     || PACKET_remaining(pkt) != 0) { | 
1650  | 136  |                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
1651  | 136  |                 goto err;  | 
1652  | 136  |             }  | 
1653  | 15.8k  |         }  | 
1654  | 16.5k  |     }  | 
1655  |  |  | 
1656  | 22.0k  |     if (!PACKET_copy_all(&compression, clienthello->compressions,  | 
1657  | 22.0k  |                          MAX_COMPRESSIONS_SIZE,  | 
1658  | 22.0k  |                          &clienthello->compressions_len)) { | 
1659  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1660  | 0  |         goto err;  | 
1661  | 0  |     }  | 
1662  |  |  | 
1663  |  |     /* Preserve the raw extensions PACKET for later use */  | 
1664  | 22.0k  |     extensions = clienthello->extensions;  | 
1665  | 22.0k  |     if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,  | 
1666  | 22.0k  |                                 &clienthello->pre_proc_exts,  | 
1667  | 22.0k  |                                 &clienthello->pre_proc_exts_len, 1)) { | 
1668  |  |         /* SSLfatal already been called */  | 
1669  | 143  |         goto err;  | 
1670  | 143  |     }  | 
1671  | 21.9k  |     s->clienthello = clienthello;  | 
1672  |  |  | 
1673  | 21.9k  |     return MSG_PROCESS_CONTINUE_PROCESSING;  | 
1674  |  |  | 
1675  | 865  |  err:  | 
1676  | 865  |     if (clienthello != NULL)  | 
1677  | 865  |         OPENSSL_free(clienthello->pre_proc_exts);  | 
1678  | 865  |     OPENSSL_free(clienthello);  | 
1679  |  |  | 
1680  | 865  |     return MSG_PROCESS_ERROR;  | 
1681  | 22.0k  | }  | 
1682  |  |  | 
1683  |  | static int tls_early_post_process_client_hello(SSL_CONNECTION *s)  | 
1684  | 4.80k  | { | 
1685  | 4.80k  |     unsigned int j;  | 
1686  | 4.80k  |     int i, al = SSL_AD_INTERNAL_ERROR;  | 
1687  | 4.80k  |     int protverr;  | 
1688  | 4.80k  |     size_t loop;  | 
1689  | 4.80k  |     unsigned long id;  | 
1690  | 4.80k  | #ifndef OPENSSL_NO_COMP  | 
1691  | 4.80k  |     SSL_COMP *comp = NULL;  | 
1692  | 4.80k  | #endif  | 
1693  | 4.80k  |     const SSL_CIPHER *c;  | 
1694  | 4.80k  |     STACK_OF(SSL_CIPHER) *ciphers = NULL;  | 
1695  | 4.80k  |     STACK_OF(SSL_CIPHER) *scsvs = NULL;  | 
1696  | 4.80k  |     CLIENTHELLO_MSG *clienthello = s->clienthello;  | 
1697  | 4.80k  |     DOWNGRADE dgrd = DOWNGRADE_NONE;  | 
1698  | 4.80k  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
1699  | 4.80k  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
1700  | 4.80k  |     SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);  | 
1701  |  |  | 
1702  |  |     /* Finished parsing the ClientHello, now we can start processing it */  | 
1703  |  |     /* Give the ClientHello callback a crack at things */  | 
1704  | 4.80k  |     if (sctx->client_hello_cb != NULL) { | 
1705  |  |         /* A failure in the ClientHello callback terminates the connection. */  | 
1706  | 0  |         switch (sctx->client_hello_cb(ussl, &al, sctx->client_hello_cb_arg)) { | 
1707  | 0  |         case SSL_CLIENT_HELLO_SUCCESS:  | 
1708  | 0  |             break;  | 
1709  | 0  |         case SSL_CLIENT_HELLO_RETRY:  | 
1710  | 0  |             s->rwstate = SSL_CLIENT_HELLO_CB;  | 
1711  | 0  |             return -1;  | 
1712  | 0  |         case SSL_CLIENT_HELLO_ERROR:  | 
1713  | 0  |         default:  | 
1714  | 0  |             SSLfatal(s, al, SSL_R_CALLBACK_FAILED);  | 
1715  | 0  |             goto err;  | 
1716  | 0  |         }  | 
1717  | 0  |     }  | 
1718  |  |  | 
1719  |  |     /* Set up the client_random */  | 
1720  | 4.80k  |     memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE);  | 
1721  |  |  | 
1722  |  |     /* Choose the version */  | 
1723  |  |  | 
1724  | 4.80k  |     if (clienthello->isv2) { | 
1725  | 1.59k  |         if (clienthello->legacy_version == SSL2_VERSION  | 
1726  | 1.59k  |                 || (clienthello->legacy_version & 0xff00)  | 
1727  | 1.59k  |                    != (SSL3_VERSION_MAJOR << 8)) { | 
1728  |  |             /*  | 
1729  |  |              * This is real SSLv2 or something completely unknown. We don't  | 
1730  |  |              * support it.  | 
1731  |  |              */  | 
1732  | 16  |             SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNKNOWN_PROTOCOL);  | 
1733  | 16  |             goto err;  | 
1734  | 16  |         }  | 
1735  |  |         /* SSLv3/TLS */  | 
1736  | 1.57k  |         s->client_version = clienthello->legacy_version;  | 
1737  | 1.57k  |     }  | 
1738  |  |     /*  | 
1739  |  |      * Do SSL/TLS version negotiation if applicable. For DTLS we just check  | 
1740  |  |      * versions are potentially compatible. Version negotiation comes later.  | 
1741  |  |      */  | 
1742  | 4.78k  |     if (!SSL_CONNECTION_IS_DTLS(s)) { | 
1743  | 4.78k  |         protverr = ssl_choose_server_version(s, clienthello, &dgrd);  | 
1744  | 4.78k  |     } else if (ssl->method->version != DTLS_ANY_VERSION &&  | 
1745  | 0  |                DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) { | 
1746  | 0  |         protverr = SSL_R_VERSION_TOO_LOW;  | 
1747  | 0  |     } else { | 
1748  | 0  |         protverr = 0;  | 
1749  | 0  |     }  | 
1750  |  |  | 
1751  | 4.78k  |     if (protverr) { | 
1752  | 76  |         if (SSL_IS_FIRST_HANDSHAKE(s)) { | 
1753  |  |             /* like ssl3_get_record, send alert using remote version number */  | 
1754  | 76  |             s->version = s->client_version = clienthello->legacy_version;  | 
1755  | 76  |         }  | 
1756  | 76  |         SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);  | 
1757  | 76  |         goto err;  | 
1758  | 76  |     }  | 
1759  |  |  | 
1760  |  |     /* TLSv1.3 specifies that a ClientHello must end on a record boundary */  | 
1761  | 4.71k  |     if (SSL_CONNECTION_IS_TLS13(s)  | 
1762  | 4.71k  |         && RECORD_LAYER_processed_read_pending(&s->rlayer)) { | 
1763  | 1  |         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);  | 
1764  | 1  |         goto err;  | 
1765  | 1  |     }  | 
1766  |  |  | 
1767  | 4.70k  |     if (SSL_CONNECTION_IS_DTLS(s)) { | 
1768  |  |         /* Empty cookie was already handled above by returning early. */  | 
1769  | 0  |         if (SSL_get_options(ssl) & SSL_OP_COOKIE_EXCHANGE) { | 
1770  | 0  |             if (sctx->app_verify_cookie_cb != NULL) { | 
1771  | 0  |                 if (sctx->app_verify_cookie_cb(ussl, clienthello->dtls_cookie,  | 
1772  | 0  |                                                clienthello->dtls_cookie_len) == 0) { | 
1773  | 0  |                     SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
1774  | 0  |                              SSL_R_COOKIE_MISMATCH);  | 
1775  | 0  |                     goto err;  | 
1776  |  |                     /* else cookie verification succeeded */  | 
1777  | 0  |                 }  | 
1778  |  |                 /* default verification */  | 
1779  | 0  |             } else if (s->d1->cookie_len != clienthello->dtls_cookie_len  | 
1780  | 0  |                     || memcmp(clienthello->dtls_cookie, s->d1->cookie,  | 
1781  | 0  |                               s->d1->cookie_len) != 0) { | 
1782  | 0  |                 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_COOKIE_MISMATCH);  | 
1783  | 0  |                 goto err;  | 
1784  | 0  |             }  | 
1785  | 0  |             s->d1->cookie_verified = 1;  | 
1786  | 0  |         }  | 
1787  | 0  |         if (ssl->method->version == DTLS_ANY_VERSION) { | 
1788  | 0  |             protverr = ssl_choose_server_version(s, clienthello, &dgrd);  | 
1789  | 0  |             if (protverr != 0) { | 
1790  | 0  |                 s->version = s->client_version;  | 
1791  | 0  |                 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);  | 
1792  | 0  |                 goto err;  | 
1793  | 0  |             }  | 
1794  | 0  |         }  | 
1795  | 0  |     }  | 
1796  |  |  | 
1797  | 4.70k  |     s->hit = 0;  | 
1798  |  |  | 
1799  | 4.70k  |     if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,  | 
1800  | 4.70k  |                               clienthello->isv2) ||  | 
1801  | 4.70k  |         !ossl_bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers,  | 
1802  | 4.69k  |                                    &scsvs, clienthello->isv2, 1)) { | 
1803  |  |         /* SSLfatal() already called */  | 
1804  | 16  |         goto err;  | 
1805  | 16  |     }  | 
1806  |  |  | 
1807  | 4.69k  |     s->s3.send_connection_binding = 0;  | 
1808  |  |     /* Check what signalling cipher-suite values were received. */  | 
1809  | 4.69k  |     if (scsvs != NULL) { | 
1810  | 13.4k  |         for (i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) { | 
1811  | 8.81k  |             c = sk_SSL_CIPHER_value(scsvs, i);  | 
1812  | 8.81k  |             if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) { | 
1813  | 8.70k  |                 if (s->renegotiate) { | 
1814  |  |                     /* SCSV is fatal if renegotiating */  | 
1815  | 0  |                     SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
1816  | 0  |                              SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);  | 
1817  | 0  |                     goto err;  | 
1818  | 0  |                 }  | 
1819  | 8.70k  |                 s->s3.send_connection_binding = 1;  | 
1820  | 8.70k  |             } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&  | 
1821  | 105  |                        !ssl_check_version_downgrade(s)) { | 
1822  |  |                 /*  | 
1823  |  |                  * This SCSV indicates that the client previously tried  | 
1824  |  |                  * a higher version.  We should fail if the current version  | 
1825  |  |                  * is an unexpected downgrade, as that indicates that the first  | 
1826  |  |                  * connection may have been tampered with in order to trigger  | 
1827  |  |                  * an insecure downgrade.  | 
1828  |  |                  */  | 
1829  | 5  |                 SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,  | 
1830  | 5  |                          SSL_R_INAPPROPRIATE_FALLBACK);  | 
1831  | 5  |                 goto err;  | 
1832  | 5  |             }  | 
1833  | 8.81k  |         }  | 
1834  | 4.69k  |     }  | 
1835  |  |  | 
1836  |  |     /* For TLSv1.3 we must select the ciphersuite *before* session resumption */  | 
1837  | 4.68k  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
1838  | 920  |         const SSL_CIPHER *cipher =  | 
1839  | 920  |             ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(ssl));  | 
1840  |  |  | 
1841  | 920  |         if (cipher == NULL) { | 
1842  | 8  |             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);  | 
1843  | 8  |             goto err;  | 
1844  | 8  |         }  | 
1845  | 912  |         if (s->hello_retry_request == SSL_HRR_PENDING  | 
1846  | 912  |                 && (s->s3.tmp.new_cipher == NULL  | 
1847  | 60  |                     || s->s3.tmp.new_cipher->id != cipher->id)) { | 
1848  |  |             /*  | 
1849  |  |              * A previous HRR picked a different ciphersuite to the one we  | 
1850  |  |              * just selected. Something must have changed.  | 
1851  |  |              */  | 
1852  | 1  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);  | 
1853  | 1  |             goto err;  | 
1854  | 1  |         }  | 
1855  | 911  |         s->s3.tmp.new_cipher = cipher;  | 
1856  | 911  |     }  | 
1857  |  |  | 
1858  |  |     /* We need to do this before getting the session */  | 
1859  | 4.67k  |     if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,  | 
1860  | 4.67k  |                              SSL_EXT_CLIENT_HELLO,  | 
1861  | 4.67k  |                              clienthello->pre_proc_exts, NULL, 0)) { | 
1862  |  |         /* SSLfatal() already called */  | 
1863  | 1  |         goto err;  | 
1864  | 1  |     }  | 
1865  |  |  | 
1866  |  |     /*  | 
1867  |  |      * We don't allow resumption in a backwards compatible ClientHello.  | 
1868  |  |      * In TLS1.1+, session_id MUST be empty.  | 
1869  |  |      *  | 
1870  |  |      * Versions before 0.9.7 always allow clients to resume sessions in  | 
1871  |  |      * renegotiation. 0.9.7 and later allow this by default, but optionally  | 
1872  |  |      * ignore resumption requests with flag  | 
1873  |  |      * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather  | 
1874  |  |      * than a change to default behavior so that applications relying on  | 
1875  |  |      * this for security won't even compile against older library versions).  | 
1876  |  |      * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to  | 
1877  |  |      * request renegotiation but not a new session (s->new_session remains  | 
1878  |  |      * unset): for servers, this essentially just means that the  | 
1879  |  |      * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be  | 
1880  |  |      * ignored.  | 
1881  |  |      */  | 
1882  | 4.67k  |     if (clienthello->isv2 ||  | 
1883  | 4.67k  |         (s->new_session &&  | 
1884  | 3.11k  |          (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) { | 
1885  | 1.56k  |         if (!ssl_get_new_session(s, 1)) { | 
1886  |  |             /* SSLfatal() already called */  | 
1887  | 0  |             goto err;  | 
1888  | 0  |         }  | 
1889  | 3.11k  |     } else { | 
1890  | 3.11k  |         i = ssl_get_prev_session(s, clienthello);  | 
1891  | 3.11k  |         if (i == 1) { | 
1892  |  |             /* previous session */  | 
1893  | 41  |             s->hit = 1;  | 
1894  | 3.07k  |         } else if (i == -1) { | 
1895  |  |             /* SSLfatal() already called */  | 
1896  | 128  |             goto err;  | 
1897  | 2.94k  |         } else { | 
1898  |  |             /* i == 0 */  | 
1899  | 2.94k  |             if (!ssl_get_new_session(s, 1)) { | 
1900  |  |                 /* SSLfatal() already called */  | 
1901  | 0  |                 goto err;  | 
1902  | 0  |             }  | 
1903  | 2.94k  |         }  | 
1904  | 3.11k  |     }  | 
1905  |  |  | 
1906  | 4.55k  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
1907  | 786  |         memcpy(s->tmp_session_id, s->clienthello->session_id,  | 
1908  | 786  |                s->clienthello->session_id_len);  | 
1909  | 786  |         s->tmp_session_id_len = s->clienthello->session_id_len;  | 
1910  | 786  |     }  | 
1911  |  |  | 
1912  |  |     /*  | 
1913  |  |      * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check  | 
1914  |  |      * ciphersuite compatibility with the session as part of resumption.  | 
1915  |  |      */  | 
1916  | 4.55k  |     if (!SSL_CONNECTION_IS_TLS13(s) && s->hit) { | 
1917  | 41  |         j = 0;  | 
1918  | 41  |         id = s->session->cipher->id;  | 
1919  |  |  | 
1920  | 41  |         OSSL_TRACE_BEGIN(TLS_CIPHER) { | 
1921  | 0  |             BIO_printf(trc_out, "client sent %d ciphers\n",  | 
1922  | 0  |                        sk_SSL_CIPHER_num(ciphers));  | 
1923  | 0  |         }  | 
1924  | 233  |         for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { | 
1925  | 224  |             c = sk_SSL_CIPHER_value(ciphers, i);  | 
1926  | 224  |             if (trc_out != NULL)  | 
1927  | 0  |                 BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i,  | 
1928  | 0  |                            sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));  | 
1929  | 224  |             if (c->id == id) { | 
1930  | 32  |                 j = 1;  | 
1931  | 32  |                 break;  | 
1932  | 32  |             }  | 
1933  | 224  |         }  | 
1934  | 41  |         if (j == 0) { | 
1935  |  |             /*  | 
1936  |  |              * we need to have the cipher in the cipher list if we are asked  | 
1937  |  |              * to reuse it  | 
1938  |  |              */  | 
1939  | 9  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,  | 
1940  | 9  |                      SSL_R_REQUIRED_CIPHER_MISSING);  | 
1941  | 9  |             OSSL_TRACE_CANCEL(TLS_CIPHER);  | 
1942  | 9  |             goto err;  | 
1943  | 9  |         }  | 
1944  | 41  |         OSSL_TRACE_END(TLS_CIPHER);  | 
1945  | 41  |     }  | 
1946  |  |  | 
1947  | 6.57k  |     for (loop = 0; loop < clienthello->compressions_len; loop++) { | 
1948  | 6.53k  |         if (clienthello->compressions[loop] == 0)  | 
1949  | 4.50k  |             break;  | 
1950  | 6.53k  |     }  | 
1951  |  |  | 
1952  | 4.54k  |     if (loop >= clienthello->compressions_len) { | 
1953  |  |         /* no compress */  | 
1954  | 38  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_COMPRESSION_SPECIFIED);  | 
1955  | 38  |         goto err;  | 
1956  | 38  |     }  | 
1957  |  |  | 
1958  | 4.50k  |     if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)  | 
1959  | 0  |         ssl_check_for_safari(s, clienthello);  | 
1960  |  |  | 
1961  |  |     /* TLS extensions */  | 
1962  | 4.50k  |     if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,  | 
1963  | 4.50k  |                                   clienthello->pre_proc_exts, NULL, 0, 1)) { | 
1964  |  |         /* SSLfatal() already called */  | 
1965  | 898  |         goto err;  | 
1966  | 898  |     }  | 
1967  |  |  | 
1968  |  |     /*  | 
1969  |  |      * Check if we want to use external pre-shared secret for this handshake  | 
1970  |  |      * for not reused session only. We need to generate server_random before  | 
1971  |  |      * calling tls_session_secret_cb in order to allow SessionTicket  | 
1972  |  |      * processing to use it in key derivation.  | 
1973  |  |      */  | 
1974  | 3.60k  |     { | 
1975  | 3.60k  |         unsigned char *pos;  | 
1976  | 3.60k  |         pos = s->s3.server_random;  | 
1977  | 3.60k  |         if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) { | 
1978  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
1979  | 0  |             goto err;  | 
1980  | 0  |         }  | 
1981  | 3.60k  |     }  | 
1982  |  |  | 
1983  | 3.60k  |     if (!s->hit && !tls1_set_server_sigalgs(s)) { | 
1984  |  |         /* SSLfatal() already called */  | 
1985  | 18  |         goto err;  | 
1986  | 18  |     }  | 
1987  |  |  | 
1988  | 3.58k  |     if (!s->hit  | 
1989  | 3.58k  |             && s->version >= TLS1_VERSION  | 
1990  | 3.58k  |             && !SSL_CONNECTION_IS_TLS13(s)  | 
1991  | 3.58k  |             && !SSL_CONNECTION_IS_DTLS(s)  | 
1992  | 3.58k  |             && s->ext.session_secret_cb != NULL) { | 
1993  | 0  |         const SSL_CIPHER *pref_cipher = NULL;  | 
1994  |  |         /*  | 
1995  |  |          * s->session->master_key_length is a size_t, but this is an int for  | 
1996  |  |          * backwards compat reasons  | 
1997  |  |          */  | 
1998  | 0  |         int master_key_length;  | 
1999  |  | 
  | 
2000  | 0  |         master_key_length = sizeof(s->session->master_key);  | 
2001  | 0  |         if (s->ext.session_secret_cb(ussl, s->session->master_key,  | 
2002  | 0  |                                      &master_key_length, ciphers,  | 
2003  | 0  |                                      &pref_cipher,  | 
2004  | 0  |                                      s->ext.session_secret_cb_arg)  | 
2005  | 0  |                 && master_key_length > 0) { | 
2006  | 0  |             s->session->master_key_length = master_key_length;  | 
2007  | 0  |             s->hit = 1;  | 
2008  | 0  |             s->peer_ciphers = ciphers;  | 
2009  | 0  |             s->session->verify_result = X509_V_OK;  | 
2010  |  | 
  | 
2011  | 0  |             ciphers = NULL;  | 
2012  |  |  | 
2013  |  |             /* check if some cipher was preferred by call back */  | 
2014  | 0  |             if (pref_cipher == NULL)  | 
2015  | 0  |                 pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers,  | 
2016  | 0  |                                                  SSL_get_ciphers(ssl));  | 
2017  | 0  |             if (pref_cipher == NULL) { | 
2018  | 0  |                 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);  | 
2019  | 0  |                 goto err;  | 
2020  | 0  |             }  | 
2021  |  |  | 
2022  | 0  |             s->session->cipher = pref_cipher;  | 
2023  | 0  |             sk_SSL_CIPHER_free(s->cipher_list);  | 
2024  | 0  |             s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers);  | 
2025  | 0  |             sk_SSL_CIPHER_free(s->cipher_list_by_id);  | 
2026  | 0  |             s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers);  | 
2027  | 0  |         }  | 
2028  | 0  |     }  | 
2029  |  |  | 
2030  |  |     /*  | 
2031  |  |      * Worst case, we will use the NULL compression, but if we have other  | 
2032  |  |      * options, we will now look for them.  We have complen-1 compression  | 
2033  |  |      * algorithms from the client, starting at q.  | 
2034  |  |      */  | 
2035  | 3.58k  |     s->s3.tmp.new_compression = NULL;  | 
2036  | 3.58k  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
2037  |  |         /*  | 
2038  |  |          * We already checked above that the NULL compression method appears in  | 
2039  |  |          * the list. Now we check there aren't any others (which is illegal in  | 
2040  |  |          * a TLSv1.3 ClientHello.  | 
2041  |  |          */  | 
2042  | 602  |         if (clienthello->compressions_len != 1) { | 
2043  | 2  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,  | 
2044  | 2  |                      SSL_R_INVALID_COMPRESSION_ALGORITHM);  | 
2045  | 2  |             goto err;  | 
2046  | 2  |         }  | 
2047  | 602  |     }  | 
2048  | 2.98k  | #ifndef OPENSSL_NO_COMP  | 
2049  |  |     /* This only happens if we have a cache hit */  | 
2050  | 2.98k  |     else if (s->session->compress_meth != 0) { | 
2051  | 0  |         int m, comp_id = s->session->compress_meth;  | 
2052  | 0  |         unsigned int k;  | 
2053  |  |         /* Perform sanity checks on resumed compression algorithm */  | 
2054  |  |         /* Can't disable compression */  | 
2055  | 0  |         if (!ssl_allow_compression(s)) { | 
2056  | 0  |             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
2057  | 0  |                      SSL_R_INCONSISTENT_COMPRESSION);  | 
2058  | 0  |             goto err;  | 
2059  | 0  |         }  | 
2060  |  |         /* Look for resumed compression method */  | 
2061  | 0  |         for (m = 0; m < sk_SSL_COMP_num(sctx->comp_methods); m++) { | 
2062  | 0  |             comp = sk_SSL_COMP_value(sctx->comp_methods, m);  | 
2063  | 0  |             if (comp_id == comp->id) { | 
2064  | 0  |                 s->s3.tmp.new_compression = comp;  | 
2065  | 0  |                 break;  | 
2066  | 0  |             }  | 
2067  | 0  |         }  | 
2068  | 0  |         if (s->s3.tmp.new_compression == NULL) { | 
2069  | 0  |             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
2070  | 0  |                      SSL_R_INVALID_COMPRESSION_ALGORITHM);  | 
2071  | 0  |             goto err;  | 
2072  | 0  |         }  | 
2073  |  |         /* Look for resumed method in compression list */  | 
2074  | 0  |         for (k = 0; k < clienthello->compressions_len; k++) { | 
2075  | 0  |             if (clienthello->compressions[k] == comp_id)  | 
2076  | 0  |                 break;  | 
2077  | 0  |         }  | 
2078  | 0  |         if (k >= clienthello->compressions_len) { | 
2079  | 0  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,  | 
2080  | 0  |                      SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);  | 
2081  | 0  |             goto err;  | 
2082  | 0  |         }  | 
2083  | 2.98k  |     } else if (s->hit) { | 
2084  | 29  |         comp = NULL;  | 
2085  | 2.95k  |     } else if (ssl_allow_compression(s) && sctx->comp_methods) { | 
2086  |  |         /* See if we have a match */  | 
2087  | 0  |         int m, nn, v, done = 0;  | 
2088  | 0  |         unsigned int o;  | 
2089  |  | 
  | 
2090  | 0  |         nn = sk_SSL_COMP_num(sctx->comp_methods);  | 
2091  | 0  |         for (m = 0; m < nn; m++) { | 
2092  | 0  |             comp = sk_SSL_COMP_value(sctx->comp_methods, m);  | 
2093  | 0  |             v = comp->id;  | 
2094  | 0  |             for (o = 0; o < clienthello->compressions_len; o++) { | 
2095  | 0  |                 if (v == clienthello->compressions[o]) { | 
2096  | 0  |                     done = 1;  | 
2097  | 0  |                     break;  | 
2098  | 0  |                 }  | 
2099  | 0  |             }  | 
2100  | 0  |             if (done)  | 
2101  | 0  |                 break;  | 
2102  | 0  |         }  | 
2103  | 0  |         if (done)  | 
2104  | 0  |             s->s3.tmp.new_compression = comp;  | 
2105  | 0  |         else  | 
2106  | 0  |             comp = NULL;  | 
2107  | 0  |     }  | 
2108  |  | #else  | 
2109  |  |     /*  | 
2110  |  |      * If compression is disabled we'd better not try to resume a session  | 
2111  |  |      * using compression.  | 
2112  |  |      */  | 
2113  |  |     if (s->session->compress_meth != 0) { | 
2114  |  |         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);  | 
2115  |  |         goto err;  | 
2116  |  |     }  | 
2117  |  | #endif  | 
2118  |  |  | 
2119  |  |     /*  | 
2120  |  |      * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher  | 
2121  |  |      */  | 
2122  |  |  | 
2123  | 3.58k  |     if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { | 
2124  | 3.55k  |         sk_SSL_CIPHER_free(s->peer_ciphers);  | 
2125  | 3.55k  |         s->peer_ciphers = ciphers;  | 
2126  | 3.55k  |         if (ciphers == NULL) { | 
2127  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2128  | 0  |             goto err;  | 
2129  | 0  |         }  | 
2130  | 3.55k  |         ciphers = NULL;  | 
2131  | 3.55k  |     }  | 
2132  |  |  | 
2133  | 3.58k  |     if (!s->hit) { | 
2134  |  | #ifdef OPENSSL_NO_COMP  | 
2135  |  |         s->session->compress_meth = 0;  | 
2136  |  | #else  | 
2137  | 3.55k  |         s->session->compress_meth = (comp == NULL) ? 0 : comp->id;  | 
2138  | 3.55k  | #endif  | 
2139  | 3.55k  |     }  | 
2140  |  |  | 
2141  | 3.58k  |     sk_SSL_CIPHER_free(ciphers);  | 
2142  | 3.58k  |     sk_SSL_CIPHER_free(scsvs);  | 
2143  | 3.58k  |     OPENSSL_free(clienthello->pre_proc_exts);  | 
2144  | 3.58k  |     OPENSSL_free(s->clienthello);  | 
2145  | 3.58k  |     s->clienthello = NULL;  | 
2146  | 3.58k  |     return 1;  | 
2147  | 1.21k  |  err:  | 
2148  | 1.21k  |     sk_SSL_CIPHER_free(ciphers);  | 
2149  | 1.21k  |     sk_SSL_CIPHER_free(scsvs);  | 
2150  | 1.21k  |     OPENSSL_free(clienthello->pre_proc_exts);  | 
2151  | 1.21k  |     OPENSSL_free(s->clienthello);  | 
2152  | 1.21k  |     s->clienthello = NULL;  | 
2153  |  |  | 
2154  | 1.21k  |     return 0;  | 
2155  | 3.58k  | }  | 
2156  |  |  | 
2157  |  | /*  | 
2158  |  |  * Call the status request callback if needed. Upon success, returns 1.  | 
2159  |  |  * Upon failure, returns 0.  | 
2160  |  |  */  | 
2161  |  | static int tls_handle_status_request(SSL_CONNECTION *s)  | 
2162  | 14.6k  | { | 
2163  | 14.6k  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
2164  |  |  | 
2165  | 14.6k  |     s->ext.status_expected = 0;  | 
2166  |  |  | 
2167  |  |     /*  | 
2168  |  |      * If status request then ask callback what to do. Note: this must be  | 
2169  |  |      * called after servername callbacks in case the certificate has changed,  | 
2170  |  |      * and must be called after the cipher has been chosen because this may  | 
2171  |  |      * influence which certificate is sent  | 
2172  |  |      */  | 
2173  | 14.6k  |     if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && sctx != NULL  | 
2174  | 14.6k  |             && sctx->ext.status_cb != NULL) { | 
2175  | 0  |         int ret;  | 
2176  |  |  | 
2177  |  |         /* If no certificate can't return certificate status */  | 
2178  | 0  |         if (s->s3.tmp.cert != NULL) { | 
2179  |  |             /*  | 
2180  |  |              * Set current certificate to one we will use so SSL_get_certificate  | 
2181  |  |              * et al can pick it up.  | 
2182  |  |              */  | 
2183  | 0  |             s->cert->key = s->s3.tmp.cert;  | 
2184  | 0  |             ret = sctx->ext.status_cb(SSL_CONNECTION_GET_USER_SSL(s),  | 
2185  | 0  |                                       sctx->ext.status_arg);  | 
2186  | 0  |             switch (ret) { | 
2187  |  |                 /* We don't want to send a status request response */  | 
2188  | 0  |             case SSL_TLSEXT_ERR_NOACK:  | 
2189  | 0  |                 s->ext.status_expected = 0;  | 
2190  | 0  |                 break;  | 
2191  |  |                 /* status request response should be sent */  | 
2192  | 0  |             case SSL_TLSEXT_ERR_OK:  | 
2193  | 0  |                 if (s->ext.ocsp.resp)  | 
2194  | 0  |                     s->ext.status_expected = 1;  | 
2195  | 0  |                 break;  | 
2196  |  |                 /* something bad happened */  | 
2197  | 0  |             case SSL_TLSEXT_ERR_ALERT_FATAL:  | 
2198  | 0  |             default:  | 
2199  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CLIENTHELLO_TLSEXT);  | 
2200  | 0  |                 return 0;  | 
2201  | 0  |             }  | 
2202  | 0  |         }  | 
2203  | 0  |     }  | 
2204  |  |  | 
2205  | 14.6k  |     return 1;  | 
2206  | 14.6k  | }  | 
2207  |  |  | 
2208  |  | /*  | 
2209  |  |  * Call the alpn_select callback if needed. Upon success, returns 1.  | 
2210  |  |  * Upon failure, returns 0.  | 
2211  |  |  */  | 
2212  |  | int tls_handle_alpn(SSL_CONNECTION *s)  | 
2213  | 14.9k  | { | 
2214  | 14.9k  |     const unsigned char *selected = NULL;  | 
2215  | 14.9k  |     unsigned char selected_len = 0;  | 
2216  | 14.9k  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
2217  |  |  | 
2218  | 14.9k  |     if (sctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) { | 
2219  | 0  |         int r = sctx->ext.alpn_select_cb(SSL_CONNECTION_GET_USER_SSL(s),  | 
2220  | 0  |                                          &selected, &selected_len,  | 
2221  | 0  |                                          s->s3.alpn_proposed,  | 
2222  | 0  |                                          (unsigned int)s->s3.alpn_proposed_len,  | 
2223  | 0  |                                          sctx->ext.alpn_select_cb_arg);  | 
2224  |  | 
  | 
2225  | 0  |         if (r == SSL_TLSEXT_ERR_OK) { | 
2226  | 0  |             OPENSSL_free(s->s3.alpn_selected);  | 
2227  | 0  |             s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len);  | 
2228  | 0  |             if (s->s3.alpn_selected == NULL) { | 
2229  | 0  |                 s->s3.alpn_selected_len = 0;  | 
2230  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2231  | 0  |                 return 0;  | 
2232  | 0  |             }  | 
2233  | 0  |             s->s3.alpn_selected_len = selected_len;  | 
2234  | 0  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
2235  |  |             /* ALPN takes precedence over NPN. */  | 
2236  | 0  |             s->s3.npn_seen = 0;  | 
2237  | 0  | #endif  | 
2238  |  |  | 
2239  |  |             /* Check ALPN is consistent with session */  | 
2240  | 0  |             if (s->session->ext.alpn_selected == NULL  | 
2241  | 0  |                         || selected_len != s->session->ext.alpn_selected_len  | 
2242  | 0  |                         || memcmp(selected, s->session->ext.alpn_selected,  | 
2243  | 0  |                                   selected_len) != 0) { | 
2244  |  |                 /* Not consistent so can't be used for early_data */  | 
2245  | 0  |                 s->ext.early_data_ok = 0;  | 
2246  |  | 
  | 
2247  | 0  |                 if (!s->hit) { | 
2248  |  |                     /*  | 
2249  |  |                      * This is a new session and so alpn_selected should have  | 
2250  |  |                      * been initialised to NULL. We should update it with the  | 
2251  |  |                      * selected ALPN.  | 
2252  |  |                      */  | 
2253  | 0  |                     if (!ossl_assert(s->session->ext.alpn_selected == NULL)) { | 
2254  | 0  |                         SSLfatal(s, SSL_AD_INTERNAL_ERROR,  | 
2255  | 0  |                                  ERR_R_INTERNAL_ERROR);  | 
2256  | 0  |                         return 0;  | 
2257  | 0  |                     }  | 
2258  | 0  |                     s->session->ext.alpn_selected = OPENSSL_memdup(selected,  | 
2259  | 0  |                                                                    selected_len);  | 
2260  | 0  |                     if (s->session->ext.alpn_selected == NULL) { | 
2261  | 0  |                         SSLfatal(s, SSL_AD_INTERNAL_ERROR,  | 
2262  | 0  |                                  ERR_R_INTERNAL_ERROR);  | 
2263  | 0  |                         return 0;  | 
2264  | 0  |                     }  | 
2265  | 0  |                     s->session->ext.alpn_selected_len = selected_len;  | 
2266  | 0  |                 }  | 
2267  | 0  |             }  | 
2268  |  |  | 
2269  | 0  |             return 1;  | 
2270  | 0  |         } else if (r != SSL_TLSEXT_ERR_NOACK) { | 
2271  | 0  |             SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL,  | 
2272  | 0  |                      SSL_R_NO_APPLICATION_PROTOCOL);  | 
2273  | 0  |             return 0;  | 
2274  | 0  |         }  | 
2275  |  |         /*  | 
2276  |  |          * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was  | 
2277  |  |          * present.  | 
2278  |  |          */  | 
2279  | 0  |     }  | 
2280  |  |  | 
2281  |  |     /* Check ALPN is consistent with session */  | 
2282  | 14.9k  |     if (s->session->ext.alpn_selected != NULL) { | 
2283  |  |         /* Not consistent so can't be used for early_data */  | 
2284  | 0  |         s->ext.early_data_ok = 0;  | 
2285  | 0  |     }  | 
2286  |  |  | 
2287  | 14.9k  |     return 1;  | 
2288  | 14.9k  | }  | 
2289  |  |  | 
2290  |  | WORK_STATE tls_post_process_client_hello(SSL_CONNECTION *s, WORK_STATE wst)  | 
2291  | 21.9k  | { | 
2292  | 21.9k  |     const SSL_CIPHER *cipher;  | 
2293  | 21.9k  |     SSL *ssl = SSL_CONNECTION_GET_SSL(s);  | 
2294  | 21.9k  |     SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);  | 
2295  |  |  | 
2296  | 21.9k  |     if (wst == WORK_MORE_A) { | 
2297  | 21.9k  |         int rv = tls_early_post_process_client_hello(s);  | 
2298  | 21.9k  |         if (rv == 0) { | 
2299  |  |             /* SSLfatal() was already called */  | 
2300  | 5.38k  |             goto err;  | 
2301  | 5.38k  |         }  | 
2302  | 16.5k  |         if (rv < 0)  | 
2303  | 0  |             return WORK_MORE_A;  | 
2304  | 16.5k  |         wst = WORK_MORE_B;  | 
2305  | 16.5k  |     }  | 
2306  | 16.5k  |     if (wst == WORK_MORE_B) { | 
2307  | 16.5k  |         if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { | 
2308  |  |             /* Let cert callback update server certificates if required */  | 
2309  | 16.3k  |             if (!s->hit && s->cert->cert_cb != NULL) { | 
2310  | 0  |                 int rv = s->cert->cert_cb(ussl, s->cert->cert_cb_arg);  | 
2311  |  | 
  | 
2312  | 0  |                 if (rv == 0) { | 
2313  | 0  |                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CERT_CB_ERROR);  | 
2314  | 0  |                     goto err;  | 
2315  | 0  |                 }  | 
2316  | 0  |                 if (rv < 0) { | 
2317  | 0  |                     s->rwstate = SSL_X509_LOOKUP;  | 
2318  | 0  |                     return WORK_MORE_B;  | 
2319  | 0  |                 }  | 
2320  | 0  |                 s->rwstate = SSL_NOTHING;  | 
2321  | 0  |             }  | 
2322  |  |  | 
2323  |  |             /* In TLSv1.3 we selected the ciphersuite before resumption */  | 
2324  | 16.3k  |             if (!SSL_CONNECTION_IS_TLS13(s)) { | 
2325  | 13.7k  |                 cipher =  | 
2326  | 13.7k  |                     ssl3_choose_cipher(s, s->peer_ciphers,  | 
2327  | 13.7k  |                                        SSL_get_ciphers(ssl));  | 
2328  |  |  | 
2329  | 13.7k  |                 if (cipher == NULL) { | 
2330  | 873  |                     SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
2331  | 873  |                              SSL_R_NO_SHARED_CIPHER);  | 
2332  | 873  |                     goto err;  | 
2333  | 873  |                 }  | 
2334  | 12.8k  |                 s->s3.tmp.new_cipher = cipher;  | 
2335  | 12.8k  |             }  | 
2336  | 15.5k  |             if (!s->hit) { | 
2337  | 15.5k  |                 if (!tls_choose_sigalg(s, 1)) { | 
2338  |  |                     /* SSLfatal already called */  | 
2339  | 999  |                     goto err;  | 
2340  | 999  |                 }  | 
2341  |  |                 /* check whether we should disable session resumption */  | 
2342  | 14.5k  |                 if (s->not_resumable_session_cb != NULL)  | 
2343  | 0  |                     s->session->not_resumable =  | 
2344  | 0  |                         s->not_resumable_session_cb(ussl,  | 
2345  | 0  |                             ((s->s3.tmp.new_cipher->algorithm_mkey  | 
2346  | 0  |                               & (SSL_kDHE | SSL_kECDHE)) != 0));  | 
2347  | 14.5k  |                 if (s->session->not_resumable)  | 
2348  |  |                     /* do not send a session ticket */  | 
2349  | 0  |                     s->ext.ticket_expected = 0;  | 
2350  | 14.5k  |             }  | 
2351  | 15.5k  |         } else { | 
2352  |  |             /* Session-id reuse */  | 
2353  | 135  |             s->s3.tmp.new_cipher = s->session->cipher;  | 
2354  | 135  |         }  | 
2355  |  |  | 
2356  |  |         /*-  | 
2357  |  |          * we now have the following setup.  | 
2358  |  |          * client_random  | 
2359  |  |          * cipher_list          - our preferred list of ciphers  | 
2360  |  |          * ciphers              - the client's preferred list of ciphers  | 
2361  |  |          * compression          - basically ignored right now  | 
2362  |  |          * ssl version is set   - sslv3  | 
2363  |  |          * s->session           - The ssl session has been setup.  | 
2364  |  |          * s->hit               - session reuse flag  | 
2365  |  |          * s->s3.tmp.new_cipher - the new cipher to use.  | 
2366  |  |          */  | 
2367  |  |  | 
2368  |  |         /*  | 
2369  |  |          * Call status_request callback if needed. Has to be done after the  | 
2370  |  |          * certificate callbacks etc above.  | 
2371  |  |          */  | 
2372  | 14.6k  |         if (!tls_handle_status_request(s)) { | 
2373  |  |             /* SSLfatal() already called */  | 
2374  | 0  |             goto err;  | 
2375  | 0  |         }  | 
2376  |  |         /*  | 
2377  |  |          * Call alpn_select callback if needed.  Has to be done after SNI and  | 
2378  |  |          * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3  | 
2379  |  |          * we already did this because cipher negotiation happens earlier, and  | 
2380  |  |          * we must handle ALPN before we decide whether to accept early_data.  | 
2381  |  |          */  | 
2382  | 14.6k  |         if (!SSL_CONNECTION_IS_TLS13(s) && !tls_handle_alpn(s)) { | 
2383  |  |             /* SSLfatal() already called */  | 
2384  | 0  |             goto err;  | 
2385  | 0  |         }  | 
2386  |  |  | 
2387  | 14.6k  |         wst = WORK_MORE_C;  | 
2388  | 14.6k  |     }  | 
2389  | 14.6k  | #ifndef OPENSSL_NO_SRP  | 
2390  | 14.6k  |     if (wst == WORK_MORE_C) { | 
2391  | 14.6k  |         int ret;  | 
2392  | 14.6k  |         if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) { | 
2393  |  |             /*  | 
2394  |  |              * callback indicates further work to be done  | 
2395  |  |              */  | 
2396  | 0  |             s->rwstate = SSL_X509_LOOKUP;  | 
2397  | 0  |             return WORK_MORE_C;  | 
2398  | 0  |         }  | 
2399  | 14.6k  |         if (ret < 0) { | 
2400  |  |             /* SSLfatal() already called */  | 
2401  | 0  |             goto err;  | 
2402  | 0  |         }  | 
2403  | 14.6k  |     }  | 
2404  | 14.6k  | #endif  | 
2405  |  |  | 
2406  | 14.6k  |     return WORK_FINISHED_STOP;  | 
2407  | 7.25k  |  err:  | 
2408  | 7.25k  |     return WORK_ERROR;  | 
2409  | 14.6k  | }  | 
2410  |  |  | 
2411  |  | CON_FUNC_RETURN tls_construct_server_hello(SSL_CONNECTION *s, WPACKET *pkt)  | 
2412  | 14.6k  | { | 
2413  | 14.6k  |     int compm;  | 
2414  | 14.6k  |     size_t sl, len;  | 
2415  | 14.6k  |     int version;  | 
2416  | 14.6k  |     unsigned char *session_id;  | 
2417  | 14.6k  |     int usetls13 = SSL_CONNECTION_IS_TLS13(s)  | 
2418  | 14.6k  |                    || s->hello_retry_request == SSL_HRR_PENDING;  | 
2419  |  |  | 
2420  | 14.6k  |     version = usetls13 ? TLS1_2_VERSION : s->version;  | 
2421  | 14.6k  |     if (!WPACKET_put_bytes_u16(pkt, version)  | 
2422  |  |                /*  | 
2423  |  |                 * Random stuff. Filling of the server_random takes place in  | 
2424  |  |                 * tls_process_client_hello()  | 
2425  |  |                 */  | 
2426  | 14.6k  |             || !WPACKET_memcpy(pkt,  | 
2427  | 14.6k  |                                s->hello_retry_request == SSL_HRR_PENDING  | 
2428  | 14.6k  |                                    ? hrrrandom : s->s3.server_random,  | 
2429  | 14.6k  |                                SSL3_RANDOM_SIZE)) { | 
2430  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2431  | 0  |         return CON_FUNC_ERROR;  | 
2432  | 0  |     }  | 
2433  |  |  | 
2434  |  |     /*-  | 
2435  |  |      * There are several cases for the session ID to send  | 
2436  |  |      * back in the server hello:  | 
2437  |  |      * - For session reuse from the session cache,  | 
2438  |  |      *   we send back the old session ID.  | 
2439  |  |      * - If stateless session reuse (using a session ticket)  | 
2440  |  |      *   is successful, we send back the client's "session ID"  | 
2441  |  |      *   (which doesn't actually identify the session).  | 
2442  |  |      * - If it is a new session, we send back the new  | 
2443  |  |      *   session ID.  | 
2444  |  |      * - However, if we want the new session to be single-use,  | 
2445  |  |      *   we send back a 0-length session ID.  | 
2446  |  |      * - In TLSv1.3 we echo back the session id sent to us by the client  | 
2447  |  |      *   regardless  | 
2448  |  |      * s->hit is non-zero in either case of session reuse,  | 
2449  |  |      * so the following won't overwrite an ID that we're supposed  | 
2450  |  |      * to send back.  | 
2451  |  |      */  | 
2452  | 14.6k  |     if (!(SSL_CONNECTION_GET_CTX(s)->session_cache_mode & SSL_SESS_CACHE_SERVER)  | 
2453  | 14.6k  |             && !s->hit)  | 
2454  | 0  |         s->session->session_id_length = 0;  | 
2455  |  |  | 
2456  | 14.6k  |     if (usetls13) { | 
2457  | 2.48k  |         sl = s->tmp_session_id_len;  | 
2458  | 2.48k  |         session_id = s->tmp_session_id;  | 
2459  | 12.1k  |     } else { | 
2460  | 12.1k  |         sl = s->session->session_id_length;  | 
2461  | 12.1k  |         session_id = s->session->session_id;  | 
2462  | 12.1k  |     }  | 
2463  |  |  | 
2464  | 14.6k  |     if (sl > sizeof(s->session->session_id)) { | 
2465  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2466  | 0  |         return CON_FUNC_ERROR;  | 
2467  | 0  |     }  | 
2468  |  |  | 
2469  |  |     /* set up the compression method */  | 
2470  |  | #ifdef OPENSSL_NO_COMP  | 
2471  |  |     compm = 0;  | 
2472  |  | #else  | 
2473  | 14.6k  |     if (usetls13 || s->s3.tmp.new_compression == NULL)  | 
2474  | 14.6k  |         compm = 0;  | 
2475  | 0  |     else  | 
2476  | 0  |         compm = s->s3.tmp.new_compression->id;  | 
2477  | 14.6k  | #endif  | 
2478  |  |  | 
2479  | 14.6k  |     if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)  | 
2480  | 14.6k  |             || !SSL_CONNECTION_GET_SSL(s)->method->put_cipher_by_char(s->s3.tmp.new_cipher,  | 
2481  | 14.6k  |                                                                       pkt, &len)  | 
2482  | 14.6k  |             || !WPACKET_put_bytes_u8(pkt, compm)) { | 
2483  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2484  | 0  |         return CON_FUNC_ERROR;  | 
2485  | 0  |     }  | 
2486  |  |  | 
2487  | 14.6k  |     if (!tls_construct_extensions(s, pkt,  | 
2488  | 14.6k  |                                   s->hello_retry_request == SSL_HRR_PENDING  | 
2489  | 14.6k  |                                       ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST  | 
2490  | 14.6k  |                                       : (SSL_CONNECTION_IS_TLS13(s)  | 
2491  | 14.1k  |                                           ? SSL_EXT_TLS1_3_SERVER_HELLO  | 
2492  | 14.1k  |                                           : SSL_EXT_TLS1_2_SERVER_HELLO),  | 
2493  | 14.6k  |                                   NULL, 0)) { | 
2494  |  |         /* SSLfatal() already called */  | 
2495  | 19  |         return CON_FUNC_ERROR;  | 
2496  | 19  |     }  | 
2497  |  |  | 
2498  | 14.6k  |     if (s->hello_retry_request == SSL_HRR_PENDING) { | 
2499  |  |         /* Ditch the session. We'll create a new one next time around */  | 
2500  | 506  |         SSL_SESSION_free(s->session);  | 
2501  | 506  |         s->session = NULL;  | 
2502  | 506  |         s->hit = 0;  | 
2503  |  |  | 
2504  |  |         /*  | 
2505  |  |          * Re-initialise the Transcript Hash. We're going to prepopulate it with  | 
2506  |  |          * a synthetic message_hash in place of ClientHello1.  | 
2507  |  |          */  | 
2508  | 506  |         if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) { | 
2509  |  |             /* SSLfatal() already called */  | 
2510  | 0  |             return CON_FUNC_ERROR;  | 
2511  | 0  |         }  | 
2512  | 14.1k  |     } else if (!(s->verify_mode & SSL_VERIFY_PEER)  | 
2513  | 14.1k  |                 && !ssl3_digest_cached_records(s, 0)) { | 
2514  | 0  |         /* SSLfatal() already called */;  | 
2515  | 0  |         return CON_FUNC_ERROR;  | 
2516  | 0  |     }  | 
2517  |  |  | 
2518  | 14.6k  |     return CON_FUNC_SUCCESS;  | 
2519  | 14.6k  | }  | 
2520  |  |  | 
2521  |  | CON_FUNC_RETURN tls_construct_server_done(SSL_CONNECTION *s, WPACKET *pkt)  | 
2522  | 12.0k  | { | 
2523  | 12.0k  |     if (!s->s3.tmp.cert_request) { | 
2524  | 12.0k  |         if (!ssl3_digest_cached_records(s, 0)) { | 
2525  |  |             /* SSLfatal() already called */  | 
2526  | 0  |             return CON_FUNC_ERROR;  | 
2527  | 0  |         }  | 
2528  | 12.0k  |     }  | 
2529  | 12.0k  |     return CON_FUNC_SUCCESS;  | 
2530  | 12.0k  | }  | 
2531  |  |  | 
2532  |  | CON_FUNC_RETURN tls_construct_server_key_exchange(SSL_CONNECTION *s,  | 
2533  |  |                                                   WPACKET *pkt)  | 
2534  | 5.03k  | { | 
2535  | 5.03k  |     EVP_PKEY *pkdh = NULL;  | 
2536  | 5.03k  |     unsigned char *encodedPoint = NULL;  | 
2537  | 5.03k  |     size_t encodedlen = 0;  | 
2538  | 5.03k  |     int curve_id = 0;  | 
2539  | 5.03k  |     const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;  | 
2540  | 5.03k  |     int i;  | 
2541  | 5.03k  |     unsigned long type;  | 
2542  | 5.03k  |     BIGNUM *r[4];  | 
2543  | 5.03k  |     EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();  | 
2544  | 5.03k  |     EVP_PKEY_CTX *pctx = NULL;  | 
2545  | 5.03k  |     size_t paramlen, paramoffset;  | 
2546  | 5.03k  |     int freer = 0;  | 
2547  | 5.03k  |     CON_FUNC_RETURN ret = CON_FUNC_ERROR;  | 
2548  | 5.03k  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
2549  |  |  | 
2550  | 5.03k  |     if (!WPACKET_get_total_written(pkt, ¶moffset)) { | 
2551  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2552  | 0  |         goto err;  | 
2553  | 0  |     }  | 
2554  |  |  | 
2555  | 5.03k  |     if (md_ctx == NULL) { | 
2556  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);  | 
2557  | 0  |         goto err;  | 
2558  | 0  |     }  | 
2559  |  |  | 
2560  | 5.03k  |     type = s->s3.tmp.new_cipher->algorithm_mkey;  | 
2561  |  |  | 
2562  | 5.03k  |     r[0] = r[1] = r[2] = r[3] = NULL;  | 
2563  | 5.03k  | #ifndef OPENSSL_NO_PSK  | 
2564  |  |     /* Plain PSK or RSAPSK nothing to do */  | 
2565  | 5.03k  |     if (type & (SSL_kPSK | SSL_kRSAPSK)) { | 
2566  | 0  |     } else  | 
2567  | 5.03k  | #endif                          /* !OPENSSL_NO_PSK */  | 
2568  | 5.03k  |     if (type & (SSL_kDHE | SSL_kDHEPSK)) { | 
2569  | 0  |         CERT *cert = s->cert;  | 
2570  | 0  |         EVP_PKEY *pkdhp = NULL;  | 
2571  |  | 
  | 
2572  | 0  |         if (s->cert->dh_tmp_auto) { | 
2573  | 0  |             pkdh = ssl_get_auto_dh(s);  | 
2574  | 0  |             if (pkdh == NULL) { | 
2575  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2576  | 0  |                 goto err;  | 
2577  | 0  |             }  | 
2578  | 0  |             pkdhp = pkdh;  | 
2579  | 0  |         } else { | 
2580  | 0  |             pkdhp = cert->dh_tmp;  | 
2581  | 0  |         }  | 
2582  | 0  | #if !defined(OPENSSL_NO_DEPRECATED_3_0)  | 
2583  | 0  |         if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) { | 
2584  | 0  |             pkdh = ssl_dh_to_pkey(s->cert->dh_tmp_cb(SSL_CONNECTION_GET_USER_SSL(s),  | 
2585  | 0  |                                                      0, 1024));  | 
2586  | 0  |             if (pkdh == NULL) { | 
2587  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2588  | 0  |                 goto err;  | 
2589  | 0  |             }  | 
2590  | 0  |             pkdhp = pkdh;  | 
2591  | 0  |         }  | 
2592  | 0  | #endif  | 
2593  | 0  |         if (pkdhp == NULL) { | 
2594  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);  | 
2595  | 0  |             goto err;  | 
2596  | 0  |         }  | 
2597  | 0  |         if (!ssl_security(s, SSL_SECOP_TMP_DH,  | 
2598  | 0  |                           EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) { | 
2599  | 0  |             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);  | 
2600  | 0  |             goto err;  | 
2601  | 0  |         }  | 
2602  | 0  |         if (s->s3.tmp.pkey != NULL) { | 
2603  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2604  | 0  |             goto err;  | 
2605  | 0  |         }  | 
2606  |  |  | 
2607  | 0  |         s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp);  | 
2608  | 0  |         if (s->s3.tmp.pkey == NULL) { | 
2609  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2610  | 0  |             goto err;  | 
2611  | 0  |         }  | 
2612  |  |  | 
2613  | 0  |         EVP_PKEY_free(pkdh);  | 
2614  | 0  |         pkdh = NULL;  | 
2615  |  |  | 
2616  |  |         /* These BIGNUMs need to be freed when we're finished */  | 
2617  | 0  |         freer = 1;  | 
2618  | 0  |         if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P,  | 
2619  | 0  |                                    &r[0])  | 
2620  | 0  |                 || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G,  | 
2621  | 0  |                                           &r[1])  | 
2622  | 0  |                 || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey,  | 
2623  | 0  |                                           OSSL_PKEY_PARAM_PUB_KEY, &r[2])) { | 
2624  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2625  | 0  |             goto err;  | 
2626  | 0  |         }  | 
2627  | 5.03k  |     } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) { | 
2628  |  |  | 
2629  | 5.03k  |         if (s->s3.tmp.pkey != NULL) { | 
2630  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2631  | 0  |             goto err;  | 
2632  | 0  |         }  | 
2633  |  |  | 
2634  |  |         /* Get NID of appropriate shared curve */  | 
2635  | 5.03k  |         curve_id = tls1_shared_group(s, -2);  | 
2636  | 5.03k  |         if (curve_id == 0) { | 
2637  | 0  |             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
2638  | 0  |                      SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);  | 
2639  | 0  |             goto err;  | 
2640  | 0  |         }  | 
2641  |  |         /* Cache the group used in the SSL_SESSION */  | 
2642  | 5.03k  |         s->session->kex_group = curve_id;  | 
2643  |  |         /* Generate a new key for this curve */  | 
2644  | 5.03k  |         s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id);  | 
2645  | 5.03k  |         if (s->s3.tmp.pkey == NULL) { | 
2646  |  |             /* SSLfatal() already called */  | 
2647  | 0  |             goto err;  | 
2648  | 0  |         }  | 
2649  |  |  | 
2650  |  |         /* Encode the public key. */  | 
2651  | 5.03k  |         encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey,  | 
2652  | 5.03k  |                                                       &encodedPoint);  | 
2653  | 5.03k  |         if (encodedlen == 0) { | 
2654  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);  | 
2655  | 0  |             goto err;  | 
2656  | 0  |         }  | 
2657  |  |  | 
2658  |  |         /*  | 
2659  |  |          * We'll generate the serverKeyExchange message explicitly so we  | 
2660  |  |          * can set these to NULLs  | 
2661  |  |          */  | 
2662  | 5.03k  |         r[0] = NULL;  | 
2663  | 5.03k  |         r[1] = NULL;  | 
2664  | 5.03k  |         r[2] = NULL;  | 
2665  | 5.03k  |         r[3] = NULL;  | 
2666  | 5.03k  |     } else  | 
2667  | 0  | #ifndef OPENSSL_NO_SRP  | 
2668  | 0  |     if (type & SSL_kSRP) { | 
2669  | 0  |         if ((s->srp_ctx.N == NULL) ||  | 
2670  | 0  |             (s->srp_ctx.g == NULL) ||  | 
2671  | 0  |             (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) { | 
2672  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM);  | 
2673  | 0  |             goto err;  | 
2674  | 0  |         }  | 
2675  | 0  |         r[0] = s->srp_ctx.N;  | 
2676  | 0  |         r[1] = s->srp_ctx.g;  | 
2677  | 0  |         r[2] = s->srp_ctx.s;  | 
2678  | 0  |         r[3] = s->srp_ctx.B;  | 
2679  | 0  |     } else  | 
2680  | 0  | #endif  | 
2681  | 0  |     { | 
2682  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);  | 
2683  | 0  |         goto err;  | 
2684  | 0  |     }  | 
2685  |  |  | 
2686  | 5.03k  |     if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)  | 
2687  | 5.03k  |         || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) { | 
2688  | 781  |         lu = NULL;  | 
2689  | 4.25k  |     } else if (lu == NULL) { | 
2690  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);  | 
2691  | 0  |         goto err;  | 
2692  | 0  |     }  | 
2693  |  |  | 
2694  | 5.03k  | #ifndef OPENSSL_NO_PSK  | 
2695  | 5.03k  |     if (type & SSL_PSK) { | 
2696  | 0  |         size_t len = (s->cert->psk_identity_hint == NULL)  | 
2697  | 0  |                         ? 0 : strlen(s->cert->psk_identity_hint);  | 
2698  |  |  | 
2699  |  |         /*  | 
2700  |  |          * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already  | 
2701  |  |          * checked this when we set the identity hint - but just in case  | 
2702  |  |          */  | 
2703  | 0  |         if (len > PSK_MAX_IDENTITY_LEN  | 
2704  | 0  |                 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,  | 
2705  | 0  |                                            len)) { | 
2706  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2707  | 0  |             goto err;  | 
2708  | 0  |         }  | 
2709  | 0  |     }  | 
2710  | 5.03k  | #endif  | 
2711  |  |  | 
2712  | 5.03k  |     for (i = 0; i < 4 && r[i] != NULL; i++) { | 
2713  | 0  |         unsigned char *binval;  | 
2714  | 0  |         int res;  | 
2715  |  | 
  | 
2716  | 0  | #ifndef OPENSSL_NO_SRP  | 
2717  | 0  |         if ((i == 2) && (type & SSL_kSRP)) { | 
2718  | 0  |             res = WPACKET_start_sub_packet_u8(pkt);  | 
2719  | 0  |         } else  | 
2720  | 0  | #endif  | 
2721  | 0  |             res = WPACKET_start_sub_packet_u16(pkt);  | 
2722  |  | 
  | 
2723  | 0  |         if (!res) { | 
2724  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2725  | 0  |             goto err;  | 
2726  | 0  |         }  | 
2727  |  |  | 
2728  |  |         /*-  | 
2729  |  |          * for interoperability with some versions of the Microsoft TLS  | 
2730  |  |          * stack, we need to zero pad the DHE pub key to the same length  | 
2731  |  |          * as the prime  | 
2732  |  |          */  | 
2733  | 0  |         if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) { | 
2734  | 0  |             size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);  | 
2735  |  | 
  | 
2736  | 0  |             if (len > 0) { | 
2737  | 0  |                 if (!WPACKET_allocate_bytes(pkt, len, &binval)) { | 
2738  | 0  |                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2739  | 0  |                     goto err;  | 
2740  | 0  |                 }  | 
2741  | 0  |                 memset(binval, 0, len);  | 
2742  | 0  |             }  | 
2743  | 0  |         }  | 
2744  |  |  | 
2745  | 0  |         if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)  | 
2746  | 0  |                 || !WPACKET_close(pkt)) { | 
2747  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2748  | 0  |             goto err;  | 
2749  | 0  |         }  | 
2750  |  |  | 
2751  | 0  |         BN_bn2bin(r[i], binval);  | 
2752  | 0  |     }  | 
2753  |  |  | 
2754  | 5.03k  |     if (type & (SSL_kECDHE | SSL_kECDHEPSK)) { | 
2755  |  |         /*  | 
2756  |  |          * We only support named (not generic) curves. In this situation, the  | 
2757  |  |          * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]  | 
2758  |  |          * [1 byte length of encoded point], followed by the actual encoded  | 
2759  |  |          * point itself  | 
2760  |  |          */  | 
2761  | 5.03k  |         if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)  | 
2762  | 5.03k  |                 || !WPACKET_put_bytes_u8(pkt, 0)  | 
2763  | 5.03k  |                 || !WPACKET_put_bytes_u8(pkt, curve_id)  | 
2764  | 5.03k  |                 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) { | 
2765  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2766  | 0  |             goto err;  | 
2767  | 0  |         }  | 
2768  | 5.03k  |         OPENSSL_free(encodedPoint);  | 
2769  | 5.03k  |         encodedPoint = NULL;  | 
2770  | 5.03k  |     }  | 
2771  |  |  | 
2772  |  |     /* not anonymous */  | 
2773  | 5.03k  |     if (lu != NULL) { | 
2774  | 4.25k  |         EVP_PKEY *pkey = s->s3.tmp.cert->privatekey;  | 
2775  | 4.25k  |         const EVP_MD *md;  | 
2776  | 4.25k  |         unsigned char *sigbytes1, *sigbytes2, *tbs;  | 
2777  | 4.25k  |         size_t siglen = 0, tbslen;  | 
2778  |  |  | 
2779  | 4.25k  |         if (pkey == NULL || !tls1_lookup_md(sctx, lu, &md)) { | 
2780  |  |             /* Should never happen */  | 
2781  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2782  | 0  |             goto err;  | 
2783  | 0  |         }  | 
2784  |  |         /* Get length of the parameters we have written above */  | 
2785  | 4.25k  |         if (!WPACKET_get_length(pkt, ¶mlen)) { | 
2786  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2787  | 0  |             goto err;  | 
2788  | 0  |         }  | 
2789  |  |         /* send signature algorithm */  | 
2790  | 4.25k  |         if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) { | 
2791  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2792  | 0  |             goto err;  | 
2793  | 0  |         }  | 
2794  |  |  | 
2795  | 4.25k  |         if (EVP_DigestSignInit_ex(md_ctx, &pctx,  | 
2796  | 4.25k  |                                   md == NULL ? NULL : EVP_MD_get0_name(md),  | 
2797  | 4.25k  |                                   sctx->libctx, sctx->propq, pkey,  | 
2798  | 4.25k  |                                   NULL) <= 0) { | 
2799  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2800  | 0  |             goto err;  | 
2801  | 0  |         }  | 
2802  | 4.25k  |         if (lu->sig == EVP_PKEY_RSA_PSS) { | 
2803  | 360  |             if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0  | 
2804  | 360  |                 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) { | 
2805  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);  | 
2806  | 0  |                 goto err;  | 
2807  | 0  |             }  | 
2808  | 360  |         }  | 
2809  | 4.25k  |         tbslen = construct_key_exchange_tbs(s, &tbs,  | 
2810  | 4.25k  |                                             s->init_buf->data + paramoffset,  | 
2811  | 4.25k  |                                             paramlen);  | 
2812  | 4.25k  |         if (tbslen == 0) { | 
2813  |  |             /* SSLfatal() already called */  | 
2814  | 0  |             goto err;  | 
2815  | 0  |         }  | 
2816  |  |  | 
2817  | 4.25k  |         if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <=0  | 
2818  | 4.25k  |                 || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)  | 
2819  | 4.25k  |                 || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0  | 
2820  | 4.25k  |                 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)  | 
2821  | 4.25k  |                 || sigbytes1 != sigbytes2) { | 
2822  | 0  |             OPENSSL_free(tbs);  | 
2823  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2824  | 0  |             goto err;  | 
2825  | 0  |         }  | 
2826  | 4.25k  |         OPENSSL_free(tbs);  | 
2827  | 4.25k  |     }  | 
2828  |  |  | 
2829  | 5.03k  |     ret = CON_FUNC_SUCCESS;  | 
2830  | 5.03k  |  err:  | 
2831  | 5.03k  |     EVP_PKEY_free(pkdh);  | 
2832  | 5.03k  |     OPENSSL_free(encodedPoint);  | 
2833  | 5.03k  |     EVP_MD_CTX_free(md_ctx);  | 
2834  | 5.03k  |     if (freer) { | 
2835  | 0  |         BN_free(r[0]);  | 
2836  | 0  |         BN_free(r[1]);  | 
2837  | 0  |         BN_free(r[2]);  | 
2838  | 0  |         BN_free(r[3]);  | 
2839  | 0  |     }  | 
2840  | 5.03k  |     return ret;  | 
2841  | 5.03k  | }  | 
2842  |  |  | 
2843  |  | CON_FUNC_RETURN tls_construct_certificate_request(SSL_CONNECTION *s,  | 
2844  |  |                                                   WPACKET *pkt)  | 
2845  | 0  | { | 
2846  | 0  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
2847  |  |         /* Send random context when doing post-handshake auth */  | 
2848  | 0  |         if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { | 
2849  | 0  |             OPENSSL_free(s->pha_context);  | 
2850  | 0  |             s->pha_context_len = 32;  | 
2851  | 0  |             if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) { | 
2852  | 0  |                 s->pha_context_len = 0;  | 
2853  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2854  | 0  |                 return CON_FUNC_ERROR;  | 
2855  | 0  |             }  | 
2856  | 0  |             if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,  | 
2857  | 0  |                               s->pha_context, s->pha_context_len, 0) <= 0  | 
2858  | 0  |                     || !WPACKET_sub_memcpy_u8(pkt, s->pha_context,  | 
2859  | 0  |                                               s->pha_context_len)) { | 
2860  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2861  | 0  |                 return CON_FUNC_ERROR;  | 
2862  | 0  |             }  | 
2863  |  |             /* reset the handshake hash back to just after the ClientFinished */  | 
2864  | 0  |             if (!tls13_restore_handshake_digest_for_pha(s)) { | 
2865  |  |                 /* SSLfatal() already called */  | 
2866  | 0  |                 return CON_FUNC_ERROR;  | 
2867  | 0  |             }  | 
2868  | 0  |         } else { | 
2869  | 0  |             if (!WPACKET_put_bytes_u8(pkt, 0)) { | 
2870  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2871  | 0  |                 return CON_FUNC_ERROR;  | 
2872  | 0  |             }  | 
2873  | 0  |         }  | 
2874  |  |  | 
2875  | 0  |         if (!tls_construct_extensions(s, pkt,  | 
2876  | 0  |                                       SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,  | 
2877  | 0  |                                       0)) { | 
2878  |  |             /* SSLfatal() already called */  | 
2879  | 0  |             return CON_FUNC_ERROR;  | 
2880  | 0  |         }  | 
2881  | 0  |         goto done;  | 
2882  | 0  |     }  | 
2883  |  |  | 
2884  |  |     /* get the list of acceptable cert types */  | 
2885  | 0  |     if (!WPACKET_start_sub_packet_u8(pkt)  | 
2886  | 0  |         || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) { | 
2887  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2888  | 0  |         return CON_FUNC_ERROR;  | 
2889  | 0  |     }  | 
2890  |  |  | 
2891  | 0  |     if (SSL_USE_SIGALGS(s)) { | 
2892  | 0  |         const uint16_t *psigs;  | 
2893  | 0  |         size_t nl = tls12_get_psigalgs(s, 1, &psigs);  | 
2894  |  | 
  | 
2895  | 0  |         if (!WPACKET_start_sub_packet_u16(pkt)  | 
2896  | 0  |                 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)  | 
2897  | 0  |                 || !tls12_copy_sigalgs(s, pkt, psigs, nl)  | 
2898  | 0  |                 || !WPACKET_close(pkt)) { | 
2899  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2900  | 0  |             return CON_FUNC_ERROR;  | 
2901  | 0  |         }  | 
2902  | 0  |     }  | 
2903  |  |  | 
2904  | 0  |     if (!construct_ca_names(s, get_ca_names(s), pkt)) { | 
2905  |  |         /* SSLfatal() already called */  | 
2906  | 0  |         return CON_FUNC_ERROR;  | 
2907  | 0  |     }  | 
2908  |  |  | 
2909  | 0  |  done:  | 
2910  | 0  |     s->certreqs_sent++;  | 
2911  | 0  |     s->s3.tmp.cert_request = 1;  | 
2912  | 0  |     return CON_FUNC_SUCCESS;  | 
2913  | 0  | }  | 
2914  |  |  | 
2915  |  | static int tls_process_cke_psk_preamble(SSL_CONNECTION *s, PACKET *pkt)  | 
2916  | 0  | { | 
2917  | 0  | #ifndef OPENSSL_NO_PSK  | 
2918  | 0  |     unsigned char psk[PSK_MAX_PSK_LEN];  | 
2919  | 0  |     size_t psklen;  | 
2920  | 0  |     PACKET psk_identity;  | 
2921  |  | 
  | 
2922  | 0  |     if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) { | 
2923  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
2924  | 0  |         return 0;  | 
2925  | 0  |     }  | 
2926  | 0  |     if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) { | 
2927  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG);  | 
2928  | 0  |         return 0;  | 
2929  | 0  |     }  | 
2930  | 0  |     if (s->psk_server_callback == NULL) { | 
2931  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB);  | 
2932  | 0  |         return 0;  | 
2933  | 0  |     }  | 
2934  |  |  | 
2935  | 0  |     if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) { | 
2936  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2937  | 0  |         return 0;  | 
2938  | 0  |     }  | 
2939  |  |  | 
2940  | 0  |     psklen = s->psk_server_callback(SSL_CONNECTION_GET_USER_SSL(s),  | 
2941  | 0  |                                     s->session->psk_identity,  | 
2942  | 0  |                                     psk, sizeof(psk));  | 
2943  |  | 
  | 
2944  | 0  |     if (psklen > PSK_MAX_PSK_LEN) { | 
2945  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2946  | 0  |         return 0;  | 
2947  | 0  |     } else if (psklen == 0) { | 
2948  |  |         /*  | 
2949  |  |          * PSK related to the given identity not found  | 
2950  |  |          */  | 
2951  | 0  |         SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND);  | 
2952  | 0  |         return 0;  | 
2953  | 0  |     }  | 
2954  |  |  | 
2955  | 0  |     OPENSSL_free(s->s3.tmp.psk);  | 
2956  | 0  |     s->s3.tmp.psk = OPENSSL_memdup(psk, psklen);  | 
2957  | 0  |     OPENSSL_cleanse(psk, psklen);  | 
2958  |  | 
  | 
2959  | 0  |     if (s->s3.tmp.psk == NULL) { | 
2960  | 0  |         s->s3.tmp.psklen = 0;  | 
2961  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);  | 
2962  | 0  |         return 0;  | 
2963  | 0  |     }  | 
2964  |  |  | 
2965  | 0  |     s->s3.tmp.psklen = psklen;  | 
2966  |  | 
  | 
2967  | 0  |     return 1;  | 
2968  |  | #else  | 
2969  |  |     /* Should never happen */  | 
2970  |  |     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
2971  |  |     return 0;  | 
2972  |  | #endif  | 
2973  | 0  | }  | 
2974  |  |  | 
2975  |  | static int tls_process_cke_rsa(SSL_CONNECTION *s, PACKET *pkt)  | 
2976  | 5.73k  | { | 
2977  | 5.73k  |     size_t outlen;  | 
2978  | 5.73k  |     PACKET enc_premaster;  | 
2979  | 5.73k  |     EVP_PKEY *rsa = NULL;  | 
2980  | 5.73k  |     unsigned char *rsa_decrypt = NULL;  | 
2981  | 5.73k  |     int ret = 0;  | 
2982  | 5.73k  |     EVP_PKEY_CTX *ctx = NULL;  | 
2983  | 5.73k  |     OSSL_PARAM params[3], *p = params;  | 
2984  | 5.73k  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
2985  |  |  | 
2986  | 5.73k  |     rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey;  | 
2987  | 5.73k  |     if (rsa == NULL) { | 
2988  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE);  | 
2989  | 0  |         return 0;  | 
2990  | 0  |     }  | 
2991  |  |  | 
2992  |  |     /* SSLv3 and pre-standard DTLS omit the length bytes. */  | 
2993  | 5.73k  |     if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) { | 
2994  | 2.47k  |         enc_premaster = *pkt;  | 
2995  | 3.26k  |     } else { | 
2996  | 3.26k  |         if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)  | 
2997  | 3.26k  |             || PACKET_remaining(pkt) != 0) { | 
2998  | 125  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
2999  | 125  |             return 0;  | 
3000  | 125  |         }  | 
3001  | 3.26k  |     }  | 
3002  |  |  | 
3003  | 5.60k  |     outlen = SSL_MAX_MASTER_KEY_LENGTH;  | 
3004  | 5.60k  |     rsa_decrypt = OPENSSL_malloc(outlen);  | 
3005  | 5.60k  |     if (rsa_decrypt == NULL) { | 
3006  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);  | 
3007  | 0  |         return 0;  | 
3008  | 0  |     }  | 
3009  |  |  | 
3010  | 5.60k  |     ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, rsa, sctx->propq);  | 
3011  | 5.60k  |     if (ctx == NULL) { | 
3012  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);  | 
3013  | 0  |         goto err;  | 
3014  | 0  |     }  | 
3015  |  |  | 
3016  |  |     /*  | 
3017  |  |      * We must not leak whether a decryption failure occurs because of  | 
3018  |  |      * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,  | 
3019  |  |      * section 7.4.7.1). We use the special padding type  | 
3020  |  |      * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the  | 
3021  |  |      * RSA, check the padding and check that the client version is as expected  | 
3022  |  |      * in the premaster secret. If any of that fails then the function appears  | 
3023  |  |      * to return successfully but with a random result. The call below could  | 
3024  |  |      * still fail if the input is publicly invalid.  | 
3025  |  |      * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1  | 
3026  |  |      */  | 
3027  | 5.60k  |     if (EVP_PKEY_decrypt_init(ctx) <= 0  | 
3028  | 5.60k  |             || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) { | 
3029  | 0  |         SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);  | 
3030  | 0  |         goto err;  | 
3031  | 0  |     }  | 
3032  |  |  | 
3033  | 5.60k  |     *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION,  | 
3034  | 5.60k  |                                      (unsigned int *)&s->client_version);  | 
3035  | 5.60k  |    if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0)  | 
3036  | 0  |         *p++ = OSSL_PARAM_construct_uint(  | 
3037  | 0  |             OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION,  | 
3038  | 0  |             (unsigned int *)&s->version);  | 
3039  | 5.60k  |     *p++ = OSSL_PARAM_construct_end();  | 
3040  |  |  | 
3041  | 5.60k  |     if (!EVP_PKEY_CTX_set_params(ctx, params)  | 
3042  | 5.60k  |             || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen,  | 
3043  | 5.60k  |                                 PACKET_data(&enc_premaster),  | 
3044  | 5.60k  |                                 PACKET_remaining(&enc_premaster)) <= 0) { | 
3045  | 33  |         SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);  | 
3046  | 33  |         goto err;  | 
3047  | 33  |     }  | 
3048  |  |  | 
3049  |  |     /*  | 
3050  |  |      * This test should never fail (otherwise we should have failed above) but  | 
3051  |  |      * we double check anyway.  | 
3052  |  |      */  | 
3053  | 5.57k  |     if (outlen != SSL_MAX_MASTER_KEY_LENGTH) { | 
3054  | 0  |         OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH);  | 
3055  | 0  |         SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);  | 
3056  | 0  |         goto err;  | 
3057  | 0  |     }  | 
3058  |  |  | 
3059  |  |     /* Also cleanses rsa_decrypt (on success or failure) */  | 
3060  | 5.57k  |     if (!ssl_generate_master_secret(s, rsa_decrypt, outlen, 0)) { | 
3061  |  |         /* SSLfatal() already called */  | 
3062  | 0  |         goto err;  | 
3063  | 0  |     }  | 
3064  |  |  | 
3065  | 5.57k  |     ret = 1;  | 
3066  | 5.60k  |  err:  | 
3067  | 5.60k  |     OPENSSL_free(rsa_decrypt);  | 
3068  | 5.60k  |     EVP_PKEY_CTX_free(ctx);  | 
3069  | 5.60k  |     return ret;  | 
3070  | 5.57k  | }  | 
3071  |  |  | 
3072  |  | static int tls_process_cke_dhe(SSL_CONNECTION *s, PACKET *pkt)  | 
3073  | 0  | { | 
3074  | 0  |     EVP_PKEY *skey = NULL;  | 
3075  | 0  |     unsigned int i;  | 
3076  | 0  |     const unsigned char *data;  | 
3077  | 0  |     EVP_PKEY *ckey = NULL;  | 
3078  | 0  |     int ret = 0;  | 
3079  |  | 
  | 
3080  | 0  |     if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) { | 
3081  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);  | 
3082  | 0  |         goto err;  | 
3083  | 0  |     }  | 
3084  | 0  |     skey = s->s3.tmp.pkey;  | 
3085  | 0  |     if (skey == NULL) { | 
3086  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);  | 
3087  | 0  |         goto err;  | 
3088  | 0  |     }  | 
3089  |  |  | 
3090  | 0  |     if (PACKET_remaining(pkt) == 0L) { | 
3091  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY);  | 
3092  | 0  |         goto err;  | 
3093  | 0  |     }  | 
3094  | 0  |     if (!PACKET_get_bytes(pkt, &data, i)) { | 
3095  |  |         /* We already checked we have enough data */  | 
3096  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3097  | 0  |         goto err;  | 
3098  | 0  |     }  | 
3099  | 0  |     ckey = EVP_PKEY_new();  | 
3100  | 0  |     if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) { | 
3101  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);  | 
3102  | 0  |         goto err;  | 
3103  | 0  |     }  | 
3104  |  |  | 
3105  | 0  |     if (!EVP_PKEY_set1_encoded_public_key(ckey, data, i)) { | 
3106  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);  | 
3107  | 0  |         goto err;  | 
3108  | 0  |     }  | 
3109  |  |  | 
3110  | 0  |     if (ssl_derive(s, skey, ckey, 1) == 0) { | 
3111  |  |         /* SSLfatal() already called */  | 
3112  | 0  |         goto err;  | 
3113  | 0  |     }  | 
3114  |  |  | 
3115  | 0  |     ret = 1;  | 
3116  | 0  |     EVP_PKEY_free(s->s3.tmp.pkey);  | 
3117  | 0  |     s->s3.tmp.pkey = NULL;  | 
3118  | 0  |  err:  | 
3119  | 0  |     EVP_PKEY_free(ckey);  | 
3120  | 0  |     return ret;  | 
3121  | 0  | }  | 
3122  |  |  | 
3123  |  | static int tls_process_cke_ecdhe(SSL_CONNECTION *s, PACKET *pkt)  | 
3124  | 2.42k  | { | 
3125  | 2.42k  |     EVP_PKEY *skey = s->s3.tmp.pkey;  | 
3126  | 2.42k  |     EVP_PKEY *ckey = NULL;  | 
3127  | 2.42k  |     int ret = 0;  | 
3128  |  |  | 
3129  | 2.42k  |     if (PACKET_remaining(pkt) == 0L) { | 
3130  |  |         /* We don't support ECDH client auth */  | 
3131  | 6  |         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY);  | 
3132  | 6  |         goto err;  | 
3133  | 2.41k  |     } else { | 
3134  | 2.41k  |         unsigned int i;  | 
3135  | 2.41k  |         const unsigned char *data;  | 
3136  |  |  | 
3137  |  |         /*  | 
3138  |  |          * Get client's public key from encoded point in the  | 
3139  |  |          * ClientKeyExchange message.  | 
3140  |  |          */  | 
3141  |  |  | 
3142  |  |         /* Get encoded point length */  | 
3143  | 2.41k  |         if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)  | 
3144  | 2.41k  |             || PACKET_remaining(pkt) != 0) { | 
3145  | 59  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
3146  | 59  |             goto err;  | 
3147  | 59  |         }  | 
3148  | 2.36k  |         if (skey == NULL) { | 
3149  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY);  | 
3150  | 0  |             goto err;  | 
3151  | 0  |         }  | 
3152  |  |  | 
3153  | 2.36k  |         ckey = EVP_PKEY_new();  | 
3154  | 2.36k  |         if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) { | 
3155  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);  | 
3156  | 0  |             goto err;  | 
3157  | 0  |         }  | 
3158  |  |  | 
3159  | 2.36k  |         if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) { | 
3160  | 466  |             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);  | 
3161  | 466  |             goto err;  | 
3162  | 466  |         }  | 
3163  | 2.36k  |     }  | 
3164  |  |  | 
3165  | 1.89k  |     if (ssl_derive(s, skey, ckey, 1) == 0) { | 
3166  |  |         /* SSLfatal() already called */  | 
3167  | 15  |         goto err;  | 
3168  | 15  |     }  | 
3169  |  |  | 
3170  | 1.87k  |     ret = 1;  | 
3171  | 1.87k  |     EVP_PKEY_free(s->s3.tmp.pkey);  | 
3172  | 1.87k  |     s->s3.tmp.pkey = NULL;  | 
3173  | 2.42k  |  err:  | 
3174  | 2.42k  |     EVP_PKEY_free(ckey);  | 
3175  |  |  | 
3176  | 2.42k  |     return ret;  | 
3177  | 1.87k  | }  | 
3178  |  |  | 
3179  |  | static int tls_process_cke_srp(SSL_CONNECTION *s, PACKET *pkt)  | 
3180  | 0  | { | 
3181  | 0  | #ifndef OPENSSL_NO_SRP  | 
3182  | 0  |     unsigned int i;  | 
3183  | 0  |     const unsigned char *data;  | 
3184  |  | 
  | 
3185  | 0  |     if (!PACKET_get_net_2(pkt, &i)  | 
3186  | 0  |         || !PACKET_get_bytes(pkt, &data, i)) { | 
3187  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH);  | 
3188  | 0  |         return 0;  | 
3189  | 0  |     }  | 
3190  | 0  |     if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) { | 
3191  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);  | 
3192  | 0  |         return 0;  | 
3193  | 0  |     }  | 
3194  | 0  |     if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) { | 
3195  | 0  |         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS);  | 
3196  | 0  |         return 0;  | 
3197  | 0  |     }  | 
3198  | 0  |     OPENSSL_free(s->session->srp_username);  | 
3199  | 0  |     s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);  | 
3200  | 0  |     if (s->session->srp_username == NULL) { | 
3201  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);  | 
3202  | 0  |         return 0;  | 
3203  | 0  |     }  | 
3204  |  |  | 
3205  | 0  |     if (!srp_generate_server_master_secret(s)) { | 
3206  |  |         /* SSLfatal() already called */  | 
3207  | 0  |         return 0;  | 
3208  | 0  |     }  | 
3209  |  |  | 
3210  | 0  |     return 1;  | 
3211  |  | #else  | 
3212  |  |     /* Should never happen */  | 
3213  |  |     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3214  |  |     return 0;  | 
3215  |  | #endif  | 
3216  | 0  | }  | 
3217  |  |  | 
3218  |  | static int tls_process_cke_gost(SSL_CONNECTION *s, PACKET *pkt)  | 
3219  | 0  | { | 
3220  | 0  | #ifndef OPENSSL_NO_GOST  | 
3221  | 0  |     EVP_PKEY_CTX *pkey_ctx;  | 
3222  | 0  |     EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;  | 
3223  | 0  |     unsigned char premaster_secret[32];  | 
3224  | 0  |     const unsigned char *start;  | 
3225  | 0  |     size_t outlen = sizeof(premaster_secret), inlen;  | 
3226  | 0  |     unsigned long alg_a;  | 
3227  | 0  |     GOST_KX_MESSAGE *pKX = NULL;  | 
3228  | 0  |     const unsigned char *ptr;  | 
3229  | 0  |     int ret = 0;  | 
3230  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
3231  |  |  | 
3232  |  |     /* Get our certificate private key */  | 
3233  | 0  |     alg_a = s->s3.tmp.new_cipher->algorithm_auth;  | 
3234  | 0  |     if (alg_a & SSL_aGOST12) { | 
3235  |  |         /*  | 
3236  |  |          * New GOST ciphersuites have SSL_aGOST01 bit too  | 
3237  |  |          */  | 
3238  | 0  |         pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;  | 
3239  | 0  |         if (pk == NULL) { | 
3240  | 0  |             pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;  | 
3241  | 0  |         }  | 
3242  | 0  |         if (pk == NULL) { | 
3243  | 0  |             pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;  | 
3244  | 0  |         }  | 
3245  | 0  |     } else if (alg_a & SSL_aGOST01) { | 
3246  | 0  |         pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;  | 
3247  | 0  |     }  | 
3248  |  | 
  | 
3249  | 0  |     pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq);  | 
3250  | 0  |     if (pkey_ctx == NULL) { | 
3251  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);  | 
3252  | 0  |         return 0;  | 
3253  | 0  |     }  | 
3254  | 0  |     if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) { | 
3255  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3256  | 0  |         goto err;  | 
3257  | 0  |     }  | 
3258  |  |     /*  | 
3259  |  |      * If client certificate is present and is of the same type, maybe  | 
3260  |  |      * use it for key exchange.  Don't mind errors from  | 
3261  |  |      * EVP_PKEY_derive_set_peer, because it is completely valid to use a  | 
3262  |  |      * client certificate for authorization only.  | 
3263  |  |      */  | 
3264  | 0  |     client_pub_pkey = tls_get_peer_pkey(s);  | 
3265  | 0  |     if (client_pub_pkey) { | 
3266  | 0  |         if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)  | 
3267  | 0  |             ERR_clear_error();  | 
3268  | 0  |     }  | 
3269  |  | 
  | 
3270  | 0  |     ptr = PACKET_data(pkt);  | 
3271  |  |     /* Some implementations provide extra data in the opaqueBlob  | 
3272  |  |      * We have nothing to do with this blob so we just skip it */  | 
3273  | 0  |     pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt));  | 
3274  | 0  |     if (pKX == NULL  | 
3275  | 0  |        || pKX->kxBlob == NULL  | 
3276  | 0  |        || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) { | 
3277  | 0  |          SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);  | 
3278  | 0  |          goto err;  | 
3279  | 0  |     }  | 
3280  |  |  | 
3281  | 0  |     if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) { | 
3282  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);  | 
3283  | 0  |         goto err;  | 
3284  | 0  |     }  | 
3285  |  |  | 
3286  | 0  |     if (PACKET_remaining(pkt) != 0) { | 
3287  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);  | 
3288  | 0  |         goto err;  | 
3289  | 0  |     }  | 
3290  |  |  | 
3291  | 0  |     inlen = pKX->kxBlob->value.sequence->length;  | 
3292  | 0  |     start = pKX->kxBlob->value.sequence->data;  | 
3293  |  | 
  | 
3294  | 0  |     if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,  | 
3295  | 0  |                          inlen) <= 0) { | 
3296  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);  | 
3297  | 0  |         goto err;  | 
3298  | 0  |     }  | 
3299  |  |     /* Generate master secret */  | 
3300  | 0  |     if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) { | 
3301  |  |         /* SSLfatal() already called */  | 
3302  | 0  |         goto err;  | 
3303  | 0  |     }  | 
3304  |  |     /* Check if pubkey from client certificate was used */  | 
3305  | 0  |     if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,  | 
3306  | 0  |                           NULL) > 0)  | 
3307  | 0  |         s->statem.no_cert_verify = 1;  | 
3308  |  | 
  | 
3309  | 0  |     ret = 1;  | 
3310  | 0  |  err:  | 
3311  | 0  |     EVP_PKEY_CTX_free(pkey_ctx);  | 
3312  | 0  |     GOST_KX_MESSAGE_free(pKX);  | 
3313  | 0  |     return ret;  | 
3314  |  | #else  | 
3315  |  |     /* Should never happen */  | 
3316  |  |     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3317  |  |     return 0;  | 
3318  |  | #endif  | 
3319  | 0  | }  | 
3320  |  |  | 
3321  |  | static int tls_process_cke_gost18(SSL_CONNECTION *s, PACKET *pkt)  | 
3322  | 0  | { | 
3323  | 0  | #ifndef OPENSSL_NO_GOST  | 
3324  | 0  |     unsigned char rnd_dgst[32];  | 
3325  | 0  |     EVP_PKEY_CTX *pkey_ctx = NULL;  | 
3326  | 0  |     EVP_PKEY *pk = NULL;  | 
3327  | 0  |     unsigned char premaster_secret[32];  | 
3328  | 0  |     const unsigned char *start = NULL;  | 
3329  | 0  |     size_t outlen = sizeof(premaster_secret), inlen = 0;  | 
3330  | 0  |     int ret = 0;  | 
3331  | 0  |     int cipher_nid = ossl_gost18_cke_cipher_nid(s);  | 
3332  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
3333  |  | 
  | 
3334  | 0  |     if (cipher_nid == NID_undef) { | 
3335  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3336  | 0  |         return 0;  | 
3337  | 0  |     }  | 
3338  |  |  | 
3339  | 0  |     if (ossl_gost_ukm(s, rnd_dgst) <= 0) { | 
3340  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3341  | 0  |         goto err;  | 
3342  | 0  |     }  | 
3343  |  |  | 
3344  |  |     /* Get our certificate private key */  | 
3345  | 0  |     pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ?  | 
3346  | 0  |          s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey :  | 
3347  | 0  |          s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;  | 
3348  | 0  |     if (pk == NULL) { | 
3349  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);  | 
3350  | 0  |         goto err;  | 
3351  | 0  |     }  | 
3352  |  |  | 
3353  | 0  |     pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq);  | 
3354  | 0  |     if (pkey_ctx == NULL) { | 
3355  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);  | 
3356  | 0  |         goto err;  | 
3357  | 0  |     }  | 
3358  | 0  |     if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) { | 
3359  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3360  | 0  |         goto err;  | 
3361  | 0  |     }  | 
3362  |  |  | 
3363  |  |     /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */  | 
3364  | 0  |     if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,  | 
3365  | 0  |                           EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) <= 0) { | 
3366  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);  | 
3367  | 0  |         goto err;  | 
3368  | 0  |     }  | 
3369  |  |  | 
3370  | 0  |     if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,  | 
3371  | 0  |                           EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) <= 0) { | 
3372  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);  | 
3373  | 0  |         goto err;  | 
3374  | 0  |     }  | 
3375  | 0  |     inlen = PACKET_remaining(pkt);  | 
3376  | 0  |     start = PACKET_data(pkt);  | 
3377  |  | 
  | 
3378  | 0  |     if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) { | 
3379  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);  | 
3380  | 0  |         goto err;  | 
3381  | 0  |     }  | 
3382  |  |     /* Generate master secret */  | 
3383  | 0  |     if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) { | 
3384  |  |          /* SSLfatal() already called */  | 
3385  | 0  |          goto err;  | 
3386  | 0  |     }  | 
3387  | 0  |     ret = 1;  | 
3388  |  | 
  | 
3389  | 0  |  err:  | 
3390  | 0  |     EVP_PKEY_CTX_free(pkey_ctx);  | 
3391  | 0  |     return ret;  | 
3392  |  | #else  | 
3393  |  |     /* Should never happen */  | 
3394  |  |     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3395  |  |     return 0;  | 
3396  |  | #endif  | 
3397  | 0  | }  | 
3398  |  |  | 
3399  |  | MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL_CONNECTION *s,  | 
3400  |  |                                                    PACKET *pkt)  | 
3401  | 9.80k  | { | 
3402  | 9.80k  |     unsigned long alg_k;  | 
3403  |  |  | 
3404  | 9.80k  |     alg_k = s->s3.tmp.new_cipher->algorithm_mkey;  | 
3405  |  |  | 
3406  |  |     /* For PSK parse and retrieve identity, obtain PSK key */  | 
3407  | 9.80k  |     if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) { | 
3408  |  |         /* SSLfatal() already called */  | 
3409  | 0  |         goto err;  | 
3410  | 0  |     }  | 
3411  |  |  | 
3412  | 9.80k  |     if (alg_k & SSL_kPSK) { | 
3413  |  |         /* Identity extracted earlier: should be nothing left */  | 
3414  | 0  |         if (PACKET_remaining(pkt) != 0) { | 
3415  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
3416  | 0  |             goto err;  | 
3417  | 0  |         }  | 
3418  |  |         /* PSK handled by ssl_generate_master_secret */  | 
3419  | 0  |         if (!ssl_generate_master_secret(s, NULL, 0, 0)) { | 
3420  |  |             /* SSLfatal() already called */  | 
3421  | 0  |             goto err;  | 
3422  | 0  |         }  | 
3423  | 9.80k  |     } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { | 
3424  | 5.73k  |         if (!tls_process_cke_rsa(s, pkt)) { | 
3425  |  |             /* SSLfatal() already called */  | 
3426  | 158  |             goto err;  | 
3427  | 158  |         }  | 
3428  | 5.73k  |     } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { | 
3429  | 0  |         if (!tls_process_cke_dhe(s, pkt)) { | 
3430  |  |             /* SSLfatal() already called */  | 
3431  | 0  |             goto err;  | 
3432  | 0  |         }  | 
3433  | 4.06k  |     } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { | 
3434  | 4.06k  |         if (!tls_process_cke_ecdhe(s, pkt)) { | 
3435  |  |             /* SSLfatal() already called */  | 
3436  | 945  |             goto err;  | 
3437  | 945  |         }  | 
3438  | 4.06k  |     } else if (alg_k & SSL_kSRP) { | 
3439  | 0  |         if (!tls_process_cke_srp(s, pkt)) { | 
3440  |  |             /* SSLfatal() already called */  | 
3441  | 0  |             goto err;  | 
3442  | 0  |         }  | 
3443  | 0  |     } else if (alg_k & SSL_kGOST) { | 
3444  | 0  |         if (!tls_process_cke_gost(s, pkt)) { | 
3445  |  |             /* SSLfatal() already called */  | 
3446  | 0  |             goto err;  | 
3447  | 0  |         }  | 
3448  | 0  |     } else if (alg_k & SSL_kGOST18) { | 
3449  | 0  |         if (!tls_process_cke_gost18(s, pkt)) { | 
3450  |  |             /* SSLfatal() already called */  | 
3451  | 0  |             goto err;  | 
3452  | 0  |         }  | 
3453  | 0  |     } else { | 
3454  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);  | 
3455  | 0  |         goto err;  | 
3456  | 0  |     }  | 
3457  |  |  | 
3458  | 8.69k  |     return MSG_PROCESS_CONTINUE_PROCESSING;  | 
3459  | 1.10k  |  err:  | 
3460  | 1.10k  | #ifndef OPENSSL_NO_PSK  | 
3461  | 1.10k  |     OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);  | 
3462  | 1.10k  |     s->s3.tmp.psk = NULL;  | 
3463  | 1.10k  |     s->s3.tmp.psklen = 0;  | 
3464  | 1.10k  | #endif  | 
3465  | 1.10k  |     return MSG_PROCESS_ERROR;  | 
3466  | 9.80k  | }  | 
3467  |  |  | 
3468  |  | WORK_STATE tls_post_process_client_key_exchange(SSL_CONNECTION *s,  | 
3469  |  |                                                 WORK_STATE wst)  | 
3470  | 8.69k  | { | 
3471  |  | #ifndef OPENSSL_NO_SCTP  | 
3472  |  |     if (wst == WORK_MORE_A) { | 
3473  |  |         if (SSL_CONNECTION_IS_DTLS(s)) { | 
3474  |  |             unsigned char sctpauthkey[64];  | 
3475  |  |             char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];  | 
3476  |  |             size_t labellen;  | 
3477  |  |             /*  | 
3478  |  |              * Add new shared key for SCTP-Auth, will be ignored if no SCTP  | 
3479  |  |              * used.  | 
3480  |  |              */  | 
3481  |  |             memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,  | 
3482  |  |                    sizeof(DTLS1_SCTP_AUTH_LABEL));  | 
3483  |  |  | 
3484  |  |             /* Don't include the terminating zero. */  | 
3485  |  |             labellen = sizeof(labelbuffer) - 1;  | 
3486  |  |             if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)  | 
3487  |  |                 labellen += 1;  | 
3488  |  |  | 
3489  |  |             if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s),  | 
3490  |  |                                            sctpauthkey,  | 
3491  |  |                                            sizeof(sctpauthkey), labelbuffer,  | 
3492  |  |                                            labellen, NULL, 0,  | 
3493  |  |                                            0) <= 0) { | 
3494  |  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3495  |  |                 return WORK_ERROR;  | 
3496  |  |             }  | 
3497  |  |  | 
3498  |  |             BIO_ctrl(s->wbio, BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,  | 
3499  |  |                      sizeof(sctpauthkey), sctpauthkey);  | 
3500  |  |         }  | 
3501  |  |     }  | 
3502  |  | #endif  | 
3503  |  |  | 
3504  | 8.69k  |     if (s->statem.no_cert_verify || !received_client_cert(s)) { | 
3505  |  |         /*  | 
3506  |  |          * No certificate verify or no peer certificate so we no longer need  | 
3507  |  |          * the handshake_buffer  | 
3508  |  |          */  | 
3509  | 8.69k  |         if (!ssl3_digest_cached_records(s, 0)) { | 
3510  |  |             /* SSLfatal() already called */  | 
3511  | 0  |             return WORK_ERROR;  | 
3512  | 0  |         }  | 
3513  | 8.69k  |         return WORK_FINISHED_CONTINUE;  | 
3514  | 8.69k  |     } else { | 
3515  | 0  |         if (!s->s3.handshake_buffer) { | 
3516  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3517  | 0  |             return WORK_ERROR;  | 
3518  | 0  |         }  | 
3519  |  |         /*  | 
3520  |  |          * For sigalgs freeze the handshake buffer. If we support  | 
3521  |  |          * extms we've done this already so this is a no-op  | 
3522  |  |          */  | 
3523  | 0  |         if (!ssl3_digest_cached_records(s, 1)) { | 
3524  |  |             /* SSLfatal() already called */  | 
3525  | 0  |             return WORK_ERROR;  | 
3526  | 0  |         }  | 
3527  | 0  |     }  | 
3528  |  |  | 
3529  | 0  |     return WORK_FINISHED_CONTINUE;  | 
3530  | 8.69k  | }  | 
3531  |  |  | 
3532  |  | MSG_PROCESS_RETURN tls_process_client_rpk(SSL_CONNECTION *sc, PACKET *pkt)  | 
3533  | 0  | { | 
3534  | 0  |     MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;  | 
3535  | 0  |     SSL_SESSION *new_sess = NULL;  | 
3536  | 0  |     EVP_PKEY *peer_rpk = NULL;  | 
3537  |  | 
  | 
3538  | 0  |     if (!tls_process_rpk(sc, pkt, &peer_rpk)) { | 
3539  |  |         /* SSLfatal already called */  | 
3540  | 0  |         goto err;  | 
3541  | 0  |     }  | 
3542  |  |  | 
3543  | 0  |     if (peer_rpk == NULL) { | 
3544  | 0  |         if ((sc->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)  | 
3545  | 0  |                 && (sc->verify_mode & SSL_VERIFY_PEER)) { | 
3546  | 0  |             SSLfatal(sc, SSL_AD_CERTIFICATE_REQUIRED,  | 
3547  | 0  |                      SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);  | 
3548  | 0  |             goto err;  | 
3549  | 0  |         }  | 
3550  | 0  |     } else { | 
3551  | 0  |         if (ssl_verify_rpk(sc, peer_rpk) <= 0) { | 
3552  | 0  |             SSLfatal(sc, ssl_x509err2alert(sc->verify_result),  | 
3553  | 0  |                      SSL_R_CERTIFICATE_VERIFY_FAILED);  | 
3554  | 0  |             goto err;  | 
3555  | 0  |         }  | 
3556  | 0  |     }  | 
3557  |  |  | 
3558  |  |     /*  | 
3559  |  |      * Sessions must be immutable once they go into the session cache. Otherwise  | 
3560  |  |      * we can get multi-thread problems. Therefore we don't "update" sessions,  | 
3561  |  |      * we replace them with a duplicate. Here, we need to do this every time  | 
3562  |  |      * a new RPK (or certificate) is received via post-handshake authentication,  | 
3563  |  |      * as the session may have already gone into the session cache.  | 
3564  |  |      */  | 
3565  |  |  | 
3566  | 0  |     if (sc->post_handshake_auth == SSL_PHA_REQUESTED) { | 
3567  | 0  |         if ((new_sess = ssl_session_dup(sc->session, 0)) == NULL) { | 
3568  | 0  |             SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);  | 
3569  | 0  |             goto err;  | 
3570  | 0  |         }  | 
3571  |  |  | 
3572  | 0  |         SSL_SESSION_free(sc->session);  | 
3573  | 0  |         sc->session = new_sess;  | 
3574  | 0  |     }  | 
3575  |  |  | 
3576  |  |     /* Ensure there is no peer/peer_chain */  | 
3577  | 0  |     X509_free(sc->session->peer);  | 
3578  | 0  |     sc->session->peer = NULL;  | 
3579  | 0  |     sk_X509_pop_free(sc->session->peer_chain, X509_free);  | 
3580  | 0  |     sc->session->peer_chain = NULL;  | 
3581  |  |     /* Save RPK */  | 
3582  | 0  |     EVP_PKEY_free(sc->session->peer_rpk);  | 
3583  | 0  |     sc->session->peer_rpk = peer_rpk;  | 
3584  | 0  |     peer_rpk = NULL;  | 
3585  |  | 
  | 
3586  | 0  |     sc->session->verify_result = sc->verify_result;  | 
3587  |  |  | 
3588  |  |     /*  | 
3589  |  |      * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE  | 
3590  |  |      * message  | 
3591  |  |      */  | 
3592  | 0  |     if (SSL_CONNECTION_IS_TLS13(sc)) { | 
3593  | 0  |         if (!ssl3_digest_cached_records(sc, 1)) { | 
3594  |  |             /* SSLfatal() already called */  | 
3595  | 0  |             goto err;  | 
3596  | 0  |         }  | 
3597  |  |  | 
3598  |  |         /* Save the current hash state for when we receive the CertificateVerify */  | 
3599  | 0  |         if (!ssl_handshake_hash(sc, sc->cert_verify_hash,  | 
3600  | 0  |                                 sizeof(sc->cert_verify_hash),  | 
3601  | 0  |                                 &sc->cert_verify_hash_len)) { | 
3602  | 0  |             /* SSLfatal() already called */;  | 
3603  | 0  |             goto err;  | 
3604  | 0  |         }  | 
3605  |  |  | 
3606  |  |         /* resend session tickets */  | 
3607  | 0  |         sc->sent_tickets = 0;  | 
3608  | 0  |     }  | 
3609  |  |  | 
3610  | 0  |     ret = MSG_PROCESS_CONTINUE_READING;  | 
3611  |  | 
  | 
3612  | 0  |  err:  | 
3613  | 0  |     EVP_PKEY_free(peer_rpk);  | 
3614  | 0  |     return ret;  | 
3615  | 0  | }  | 
3616  |  |  | 
3617  |  | MSG_PROCESS_RETURN tls_process_client_certificate(SSL_CONNECTION *s,  | 
3618  |  |                                                   PACKET *pkt)  | 
3619  | 0  | { | 
3620  | 0  |     int i;  | 
3621  | 0  |     MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;  | 
3622  | 0  |     X509 *x = NULL;  | 
3623  | 0  |     unsigned long l;  | 
3624  | 0  |     const unsigned char *certstart, *certbytes;  | 
3625  | 0  |     STACK_OF(X509) *sk = NULL;  | 
3626  | 0  |     PACKET spkt, context;  | 
3627  | 0  |     size_t chainidx;  | 
3628  | 0  |     SSL_SESSION *new_sess = NULL;  | 
3629  | 0  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
3630  |  |  | 
3631  |  |     /*  | 
3632  |  |      * To get this far we must have read encrypted data from the client. We no  | 
3633  |  |      * longer tolerate unencrypted alerts. This is ignored if less than TLSv1.3  | 
3634  |  |      */  | 
3635  | 0  |     if (s->rlayer.rrlmethod->set_plain_alerts != NULL)  | 
3636  | 0  |         s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 0);  | 
3637  |  | 
  | 
3638  | 0  |     if (s->ext.client_cert_type == TLSEXT_cert_type_rpk)  | 
3639  | 0  |         return tls_process_client_rpk(s, pkt);  | 
3640  |  |  | 
3641  | 0  |     if (s->ext.client_cert_type != TLSEXT_cert_type_x509) { | 
3642  | 0  |         SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE,  | 
3643  | 0  |                  SSL_R_UNKNOWN_CERTIFICATE_TYPE);  | 
3644  | 0  |         goto err;  | 
3645  | 0  |     }  | 
3646  |  |  | 
3647  | 0  |     if ((sk = sk_X509_new_null()) == NULL) { | 
3648  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);  | 
3649  | 0  |         goto err;  | 
3650  | 0  |     }  | 
3651  |  |  | 
3652  | 0  |     if (SSL_CONNECTION_IS_TLS13(s)  | 
3653  | 0  |         && (!PACKET_get_length_prefixed_1(pkt, &context)  | 
3654  | 0  |                 || (s->pha_context == NULL && PACKET_remaining(&context) != 0)  | 
3655  | 0  |                 || (s->pha_context != NULL  | 
3656  | 0  |                     && !PACKET_equal(&context, s->pha_context,  | 
3657  | 0  |                                      s->pha_context_len)))) { | 
3658  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);  | 
3659  | 0  |         goto err;  | 
3660  | 0  |     }  | 
3661  |  |  | 
3662  | 0  |     if (!PACKET_get_length_prefixed_3(pkt, &spkt)  | 
3663  | 0  |             || PACKET_remaining(pkt) != 0) { | 
3664  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
3665  | 0  |         goto err;  | 
3666  | 0  |     }  | 
3667  |  |  | 
3668  | 0  |     for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) { | 
3669  | 0  |         if (!PACKET_get_net_3(&spkt, &l)  | 
3670  | 0  |             || !PACKET_get_bytes(&spkt, &certbytes, l)) { | 
3671  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);  | 
3672  | 0  |             goto err;  | 
3673  | 0  |         }  | 
3674  |  |  | 
3675  | 0  |         certstart = certbytes;  | 
3676  | 0  |         x = X509_new_ex(sctx->libctx, sctx->propq);  | 
3677  | 0  |         if (x == NULL) { | 
3678  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_X509_LIB);  | 
3679  | 0  |             goto err;  | 
3680  | 0  |         }  | 
3681  | 0  |         if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) { | 
3682  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);  | 
3683  | 0  |             goto err;  | 
3684  | 0  |         }  | 
3685  |  |  | 
3686  | 0  |         if (certbytes != (certstart + l)) { | 
3687  | 0  |             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);  | 
3688  | 0  |             goto err;  | 
3689  | 0  |         }  | 
3690  |  |  | 
3691  | 0  |         if (SSL_CONNECTION_IS_TLS13(s)) { | 
3692  | 0  |             RAW_EXTENSION *rawexts = NULL;  | 
3693  | 0  |             PACKET extensions;  | 
3694  |  | 
  | 
3695  | 0  |             if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) { | 
3696  | 0  |                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);  | 
3697  | 0  |                 goto err;  | 
3698  | 0  |             }  | 
3699  | 0  |             if (!tls_collect_extensions(s, &extensions,  | 
3700  | 0  |                                         SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,  | 
3701  | 0  |                                         NULL, chainidx == 0)  | 
3702  | 0  |                 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,  | 
3703  | 0  |                                              rawexts, x, chainidx,  | 
3704  | 0  |                                              PACKET_remaining(&spkt) == 0)) { | 
3705  | 0  |                 OPENSSL_free(rawexts);  | 
3706  | 0  |                 goto err;  | 
3707  | 0  |             }  | 
3708  | 0  |             OPENSSL_free(rawexts);  | 
3709  | 0  |         }  | 
3710  |  |  | 
3711  | 0  |         if (!sk_X509_push(sk, x)) { | 
3712  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);  | 
3713  | 0  |             goto err;  | 
3714  | 0  |         }  | 
3715  | 0  |         x = NULL;  | 
3716  | 0  |     }  | 
3717  |  |  | 
3718  | 0  |     if (sk_X509_num(sk) <= 0) { | 
3719  |  |         /* TLS does not mind 0 certs returned */  | 
3720  | 0  |         if (s->version == SSL3_VERSION) { | 
3721  | 0  |             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
3722  | 0  |                      SSL_R_NO_CERTIFICATES_RETURNED);  | 
3723  | 0  |             goto err;  | 
3724  | 0  |         }  | 
3725  |  |         /* Fail for TLS only if we required a certificate */  | 
3726  | 0  |         else if ((s->verify_mode & SSL_VERIFY_PEER) &&  | 
3727  | 0  |                  (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { | 
3728  | 0  |             SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,  | 
3729  | 0  |                      SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);  | 
3730  | 0  |             goto err;  | 
3731  | 0  |         }  | 
3732  |  |         /* No client certificate so digest cached records */  | 
3733  | 0  |         if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) { | 
3734  |  |             /* SSLfatal() already called */  | 
3735  | 0  |             goto err;  | 
3736  | 0  |         }  | 
3737  | 0  |     } else { | 
3738  | 0  |         EVP_PKEY *pkey;  | 
3739  | 0  |         i = ssl_verify_cert_chain(s, sk);  | 
3740  | 0  |         if (i <= 0) { | 
3741  | 0  |             SSLfatal(s, ssl_x509err2alert(s->verify_result),  | 
3742  | 0  |                      SSL_R_CERTIFICATE_VERIFY_FAILED);  | 
3743  | 0  |             goto err;  | 
3744  | 0  |         }  | 
3745  | 0  |         pkey = X509_get0_pubkey(sk_X509_value(sk, 0));  | 
3746  | 0  |         if (pkey == NULL) { | 
3747  | 0  |             SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,  | 
3748  | 0  |                      SSL_R_UNKNOWN_CERTIFICATE_TYPE);  | 
3749  | 0  |             goto err;  | 
3750  | 0  |         }  | 
3751  | 0  |     }  | 
3752  |  |  | 
3753  |  |     /*  | 
3754  |  |      * Sessions must be immutable once they go into the session cache. Otherwise  | 
3755  |  |      * we can get multi-thread problems. Therefore we don't "update" sessions,  | 
3756  |  |      * we replace them with a duplicate. Here, we need to do this every time  | 
3757  |  |      * a new certificate is received via post-handshake authentication, as the  | 
3758  |  |      * session may have already gone into the session cache.  | 
3759  |  |      */  | 
3760  |  |  | 
3761  | 0  |     if (s->post_handshake_auth == SSL_PHA_REQUESTED) { | 
3762  | 0  |         if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { | 
3763  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);  | 
3764  | 0  |             goto err;  | 
3765  | 0  |         }  | 
3766  |  |  | 
3767  | 0  |         SSL_SESSION_free(s->session);  | 
3768  | 0  |         s->session = new_sess;  | 
3769  | 0  |     }  | 
3770  |  |  | 
3771  | 0  |     X509_free(s->session->peer);  | 
3772  | 0  |     s->session->peer = sk_X509_shift(sk);  | 
3773  | 0  |     s->session->verify_result = s->verify_result;  | 
3774  |  | 
  | 
3775  | 0  |     OSSL_STACK_OF_X509_free(s->session->peer_chain);  | 
3776  | 0  |     s->session->peer_chain = sk;  | 
3777  | 0  |     sk = NULL;  | 
3778  |  |     /* Ensure there is no RPK */  | 
3779  | 0  |     EVP_PKEY_free(s->session->peer_rpk);  | 
3780  | 0  |     s->session->peer_rpk = NULL;  | 
3781  |  |  | 
3782  |  |     /*  | 
3783  |  |      * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE  | 
3784  |  |      * message  | 
3785  |  |      */  | 
3786  | 0  |     if (SSL_CONNECTION_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) { | 
3787  |  |         /* SSLfatal() already called */  | 
3788  | 0  |         goto err;  | 
3789  | 0  |     }  | 
3790  |  |  | 
3791  |  |     /*  | 
3792  |  |      * Inconsistency alert: cert_chain does *not* include the peer's own  | 
3793  |  |      * certificate, while we do include it in statem_clnt.c  | 
3794  |  |      */  | 
3795  |  |  | 
3796  |  |     /* Save the current hash state for when we receive the CertificateVerify */  | 
3797  | 0  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
3798  | 0  |         if (!ssl_handshake_hash(s, s->cert_verify_hash,  | 
3799  | 0  |                                 sizeof(s->cert_verify_hash),  | 
3800  | 0  |                                 &s->cert_verify_hash_len)) { | 
3801  |  |             /* SSLfatal() already called */  | 
3802  | 0  |             goto err;  | 
3803  | 0  |         }  | 
3804  |  |  | 
3805  |  |         /* Resend session tickets */  | 
3806  | 0  |         s->sent_tickets = 0;  | 
3807  | 0  |     }  | 
3808  |  |  | 
3809  | 0  |     ret = MSG_PROCESS_CONTINUE_READING;  | 
3810  |  | 
  | 
3811  | 0  |  err:  | 
3812  | 0  |     X509_free(x);  | 
3813  | 0  |     OSSL_STACK_OF_X509_free(sk);  | 
3814  | 0  |     return ret;  | 
3815  | 0  | }  | 
3816  |  |  | 
3817  |  | #ifndef OPENSSL_NO_COMP_ALG  | 
3818  |  | MSG_PROCESS_RETURN tls_process_client_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt)  | 
3819  |  | { | 
3820  |  |     MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;  | 
3821  |  |     PACKET tmppkt;  | 
3822  |  |     BUF_MEM *buf = BUF_MEM_new();  | 
3823  |  |  | 
3824  |  |     if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR)  | 
3825  |  |         ret = tls_process_client_certificate(sc, &tmppkt);  | 
3826  |  |  | 
3827  |  |     BUF_MEM_free(buf);  | 
3828  |  |     return ret;  | 
3829  |  | }  | 
3830  |  | #endif  | 
3831  |  |  | 
3832  |  | CON_FUNC_RETURN tls_construct_server_certificate(SSL_CONNECTION *s, WPACKET *pkt)  | 
3833  | 8.35k  | { | 
3834  | 8.35k  |     CERT_PKEY *cpk = s->s3.tmp.cert;  | 
3835  |  |  | 
3836  | 8.35k  |     if (cpk == NULL) { | 
3837  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3838  | 0  |         return CON_FUNC_ERROR;  | 
3839  | 0  |     }  | 
3840  |  |  | 
3841  |  |     /*  | 
3842  |  |      * In TLSv1.3 the certificate chain is always preceded by a 0 length context  | 
3843  |  |      * for the server Certificate message  | 
3844  |  |      */  | 
3845  | 8.35k  |     if (SSL_CONNECTION_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) { | 
3846  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3847  | 0  |         return CON_FUNC_ERROR;  | 
3848  | 0  |     }  | 
3849  | 8.35k  |     switch (s->ext.server_cert_type) { | 
3850  | 0  |     case TLSEXT_cert_type_rpk:  | 
3851  | 0  |         if (!tls_output_rpk(s, pkt, cpk)) { | 
3852  |  |             /* SSLfatal() already called */  | 
3853  | 0  |             return 0;  | 
3854  | 0  |         }  | 
3855  | 0  |         break;  | 
3856  | 8.35k  |     case TLSEXT_cert_type_x509:  | 
3857  | 8.35k  |         if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) { | 
3858  |  |             /* SSLfatal() already called */  | 
3859  | 0  |             return 0;  | 
3860  | 0  |         }  | 
3861  | 8.35k  |         break;  | 
3862  | 8.35k  |     default:  | 
3863  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3864  | 0  |         return 0;  | 
3865  | 8.35k  |     }  | 
3866  |  |  | 
3867  | 8.35k  |     return CON_FUNC_SUCCESS;  | 
3868  | 8.35k  | }  | 
3869  |  |  | 
3870  |  | #ifndef OPENSSL_NO_COMP_ALG  | 
3871  |  | CON_FUNC_RETURN tls_construct_server_compressed_certificate(SSL_CONNECTION *sc, WPACKET *pkt)  | 
3872  |  | { | 
3873  |  |     int alg = get_compressed_certificate_alg(sc);  | 
3874  |  |     OSSL_COMP_CERT *cc = sc->s3.tmp.cert->comp_cert[alg];  | 
3875  |  |  | 
3876  |  |     if (!ossl_assert(cc != NULL)) { | 
3877  |  |         SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3878  |  |         return 0;  | 
3879  |  |     }  | 
3880  |  |     /*  | 
3881  |  |      * Server can't compress on-demand  | 
3882  |  |      * Use pre-compressed certificate  | 
3883  |  |      */  | 
3884  |  |     if (!WPACKET_put_bytes_u16(pkt, alg)  | 
3885  |  |             || !WPACKET_put_bytes_u24(pkt, cc->orig_len)  | 
3886  |  |             || !WPACKET_start_sub_packet_u24(pkt)  | 
3887  |  |             || !WPACKET_memcpy(pkt, cc->data, cc->len)  | 
3888  |  |             || !WPACKET_close(pkt))  | 
3889  |  |         return 0;  | 
3890  |  |  | 
3891  |  |     sc->s3.tmp.cert->cert_comp_used++;  | 
3892  |  |     return 1;  | 
3893  |  | }  | 
3894  |  | #endif  | 
3895  |  |  | 
3896  |  | static int create_ticket_prequel(SSL_CONNECTION *s, WPACKET *pkt,  | 
3897  |  |                                  uint32_t age_add, unsigned char *tick_nonce)  | 
3898  | 96  | { | 
3899  | 96  |     uint32_t timeout = (uint32_t)ossl_time2seconds(s->session->timeout);  | 
3900  |  |  | 
3901  |  |     /*  | 
3902  |  |      * Ticket lifetime hint:  | 
3903  |  |      * In TLSv1.3 we reset the "time" field above, and always specify the  | 
3904  |  |      * timeout, limited to a 1 week period per RFC8446.  | 
3905  |  |      * For TLSv1.2 this is advisory only and we leave this unspecified for  | 
3906  |  |      * resumed session (for simplicity).  | 
3907  |  |      */  | 
3908  | 96  | #define ONE_WEEK_SEC (7 * 24 * 60 * 60)  | 
3909  |  |  | 
3910  | 96  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
3911  | 0  |         if (ossl_time_compare(s->session->timeout,  | 
3912  | 0  |                               ossl_seconds2time(ONE_WEEK_SEC)) > 0)  | 
3913  | 0  |             timeout = ONE_WEEK_SEC;  | 
3914  | 96  |     } else if (s->hit)  | 
3915  | 0  |         timeout = 0;  | 
3916  |  |  | 
3917  | 96  |     if (!WPACKET_put_bytes_u32(pkt, timeout)) { | 
3918  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3919  | 0  |         return 0;  | 
3920  | 0  |     }  | 
3921  |  |  | 
3922  | 96  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
3923  | 0  |         if (!WPACKET_put_bytes_u32(pkt, age_add)  | 
3924  | 0  |                 || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) { | 
3925  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3926  | 0  |             return 0;  | 
3927  | 0  |         }  | 
3928  | 0  |     }  | 
3929  |  |  | 
3930  |  |     /* Start the sub-packet for the actual ticket data */  | 
3931  | 96  |     if (!WPACKET_start_sub_packet_u16(pkt)) { | 
3932  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3933  | 0  |         return 0;  | 
3934  | 0  |     }  | 
3935  |  |  | 
3936  | 96  |     return 1;  | 
3937  | 96  | }  | 
3938  |  |  | 
3939  |  | static CON_FUNC_RETURN construct_stateless_ticket(SSL_CONNECTION *s,  | 
3940  |  |                                                   WPACKET *pkt,  | 
3941  |  |                                                   uint32_t age_add,  | 
3942  |  |                                                   unsigned char *tick_nonce)  | 
3943  | 96  | { | 
3944  | 96  |     unsigned char *senc = NULL;  | 
3945  | 96  |     EVP_CIPHER_CTX *ctx = NULL;  | 
3946  | 96  |     SSL_HMAC *hctx = NULL;  | 
3947  | 96  |     unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;  | 
3948  | 96  |     const unsigned char *const_p;  | 
3949  | 96  |     int len, slen_full, slen, lenfinal;  | 
3950  | 96  |     SSL_SESSION *sess;  | 
3951  | 96  |     size_t hlen;  | 
3952  | 96  |     SSL_CTX *tctx = s->session_ctx;  | 
3953  | 96  |     unsigned char iv[EVP_MAX_IV_LENGTH];  | 
3954  | 96  |     unsigned char key_name[TLSEXT_KEYNAME_LENGTH];  | 
3955  | 96  |     int iv_len;  | 
3956  | 96  |     CON_FUNC_RETURN ok = CON_FUNC_ERROR;  | 
3957  | 96  |     size_t macoffset, macendoffset;  | 
3958  | 96  |     SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);  | 
3959  | 96  |     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);  | 
3960  |  |  | 
3961  |  |     /* get session encoding length */  | 
3962  | 96  |     slen_full = i2d_SSL_SESSION(s->session, NULL);  | 
3963  |  |     /*  | 
3964  |  |      * Some length values are 16 bits, so forget it if session is too  | 
3965  |  |      * long  | 
3966  |  |      */  | 
3967  | 96  |     if (slen_full == 0 || slen_full > 0xFF00) { | 
3968  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3969  | 0  |         goto err;  | 
3970  | 0  |     }  | 
3971  | 96  |     senc = OPENSSL_malloc(slen_full);  | 
3972  | 96  |     if (senc == NULL) { | 
3973  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);  | 
3974  | 0  |         goto err;  | 
3975  | 0  |     }  | 
3976  |  |  | 
3977  | 96  |     ctx = EVP_CIPHER_CTX_new();  | 
3978  | 96  |     if (ctx == NULL) { | 
3979  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);  | 
3980  | 0  |         goto err;  | 
3981  | 0  |     }  | 
3982  | 96  |     hctx = ssl_hmac_new(tctx);  | 
3983  | 96  |     if (hctx == NULL) { | 
3984  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);  | 
3985  | 0  |         goto err;  | 
3986  | 0  |     }  | 
3987  |  |  | 
3988  | 96  |     p = senc;  | 
3989  | 96  |     if (!i2d_SSL_SESSION(s->session, &p)) { | 
3990  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
3991  | 0  |         goto err;  | 
3992  | 0  |     }  | 
3993  |  |  | 
3994  |  |     /*  | 
3995  |  |      * create a fresh copy (not shared with other threads) to clean up  | 
3996  |  |      */  | 
3997  | 96  |     const_p = senc;  | 
3998  | 96  |     sess = d2i_SSL_SESSION_ex(NULL, &const_p, slen_full, sctx->libctx,  | 
3999  | 96  |                               sctx->propq);  | 
4000  | 96  |     if (sess == NULL) { | 
4001  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4002  | 0  |         goto err;  | 
4003  | 0  |     }  | 
4004  |  |  | 
4005  | 96  |     slen = i2d_SSL_SESSION(sess, NULL);  | 
4006  | 96  |     if (slen == 0 || slen > slen_full) { | 
4007  |  |         /* shouldn't ever happen */  | 
4008  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4009  | 0  |         SSL_SESSION_free(sess);  | 
4010  | 0  |         goto err;  | 
4011  | 0  |     }  | 
4012  | 96  |     p = senc;  | 
4013  | 96  |     if (!i2d_SSL_SESSION(sess, &p)) { | 
4014  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4015  | 0  |         SSL_SESSION_free(sess);  | 
4016  | 0  |         goto err;  | 
4017  | 0  |     }  | 
4018  | 96  |     SSL_SESSION_free(sess);  | 
4019  |  |  | 
4020  |  |     /*  | 
4021  |  |      * Initialize HMAC and cipher contexts. If callback present it does  | 
4022  |  |      * all the work otherwise use generated values from parent ctx.  | 
4023  |  |      */  | 
4024  | 96  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
4025  | 96  |     if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)  | 
4026  |  | #else  | 
4027  |  |     if (tctx->ext.ticket_key_evp_cb != NULL)  | 
4028  |  | #endif  | 
4029  | 0  |     { | 
4030  | 0  |         int ret = 0;  | 
4031  |  | 
  | 
4032  | 0  |         if (tctx->ext.ticket_key_evp_cb != NULL)  | 
4033  | 0  |             ret = tctx->ext.ticket_key_evp_cb(ssl, key_name, iv, ctx,  | 
4034  | 0  |                                               ssl_hmac_get0_EVP_MAC_CTX(hctx),  | 
4035  | 0  |                                               1);  | 
4036  | 0  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
4037  | 0  |         else if (tctx->ext.ticket_key_cb != NULL)  | 
4038  |  |             /* if 0 is returned, write an empty ticket */  | 
4039  | 0  |             ret = tctx->ext.ticket_key_cb(ssl, key_name, iv, ctx,  | 
4040  | 0  |                                           ssl_hmac_get0_HMAC_CTX(hctx), 1);  | 
4041  | 0  | #endif  | 
4042  |  | 
  | 
4043  | 0  |         if (ret == 0) { | 
4044  |  |             /*  | 
4045  |  |              * In TLSv1.2 we construct a 0 length ticket. In TLSv1.3 a 0  | 
4046  |  |              * length ticket is not allowed so we abort construction of the  | 
4047  |  |              * ticket  | 
4048  |  |              */  | 
4049  | 0  |             if (SSL_CONNECTION_IS_TLS13(s)) { | 
4050  | 0  |                 ok = CON_FUNC_DONT_SEND;  | 
4051  | 0  |                 goto err;  | 
4052  | 0  |             }  | 
4053  |  |             /* Put timeout and length */  | 
4054  | 0  |             if (!WPACKET_put_bytes_u32(pkt, 0)  | 
4055  | 0  |                     || !WPACKET_put_bytes_u16(pkt, 0)) { | 
4056  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4057  | 0  |                 goto err;  | 
4058  | 0  |             }  | 
4059  | 0  |             OPENSSL_free(senc);  | 
4060  | 0  |             EVP_CIPHER_CTX_free(ctx);  | 
4061  | 0  |             ssl_hmac_free(hctx);  | 
4062  | 0  |             return CON_FUNC_SUCCESS;  | 
4063  | 0  |         }  | 
4064  | 0  |         if (ret < 0) { | 
4065  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);  | 
4066  | 0  |             goto err;  | 
4067  | 0  |         }  | 
4068  | 0  |         iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);  | 
4069  | 0  |         if (iv_len < 0) { | 
4070  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4071  | 0  |             goto err;  | 
4072  | 0  |         }  | 
4073  | 96  |     } else { | 
4074  | 96  |         EVP_CIPHER *cipher = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC",  | 
4075  | 96  |                                               sctx->propq);  | 
4076  |  |  | 
4077  | 96  |         if (cipher == NULL) { | 
4078  |  |             /* Error is already recorded */  | 
4079  | 0  |             SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);  | 
4080  | 0  |             goto err;  | 
4081  | 0  |         }  | 
4082  |  |  | 
4083  | 96  |         iv_len = EVP_CIPHER_get_iv_length(cipher);  | 
4084  | 96  |         if (iv_len < 0  | 
4085  | 96  |                 || RAND_bytes_ex(sctx->libctx, iv, iv_len, 0) <= 0  | 
4086  | 96  |                 || !EVP_EncryptInit_ex(ctx, cipher, NULL,  | 
4087  | 96  |                                        tctx->ext.secure->tick_aes_key, iv)  | 
4088  | 96  |                 || !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,  | 
4089  | 96  |                                   sizeof(tctx->ext.secure->tick_hmac_key),  | 
4090  | 96  |                                   "SHA256")) { | 
4091  | 0  |             EVP_CIPHER_free(cipher);  | 
4092  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4093  | 0  |             goto err;  | 
4094  | 0  |         }  | 
4095  | 96  |         EVP_CIPHER_free(cipher);  | 
4096  | 96  |         memcpy(key_name, tctx->ext.tick_key_name,  | 
4097  | 96  |                sizeof(tctx->ext.tick_key_name));  | 
4098  | 96  |     }  | 
4099  |  |  | 
4100  | 96  |     if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) { | 
4101  |  |         /* SSLfatal() already called */  | 
4102  | 0  |         goto err;  | 
4103  | 0  |     }  | 
4104  |  |  | 
4105  | 96  |     if (!WPACKET_get_total_written(pkt, &macoffset)  | 
4106  |  |                /* Output key name */  | 
4107  | 96  |             || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))  | 
4108  |  |                /* output IV */  | 
4109  | 96  |             || !WPACKET_memcpy(pkt, iv, iv_len)  | 
4110  | 96  |             || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,  | 
4111  | 96  |                                       &encdata1)  | 
4112  |  |                /* Encrypt session data */  | 
4113  | 96  |             || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)  | 
4114  | 96  |             || !WPACKET_allocate_bytes(pkt, len, &encdata2)  | 
4115  | 96  |             || encdata1 != encdata2  | 
4116  | 96  |             || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)  | 
4117  | 96  |             || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)  | 
4118  | 96  |             || encdata1 + len != encdata2  | 
4119  | 96  |             || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH  | 
4120  | 96  |             || !WPACKET_get_total_written(pkt, &macendoffset)  | 
4121  | 96  |             || !ssl_hmac_update(hctx,  | 
4122  | 96  |                                 (unsigned char *)s->init_buf->data + macoffset,  | 
4123  | 96  |                                 macendoffset - macoffset)  | 
4124  | 96  |             || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)  | 
4125  | 96  |             || !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE)  | 
4126  | 96  |             || hlen > EVP_MAX_MD_SIZE  | 
4127  | 96  |             || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)  | 
4128  | 96  |             || macdata1 != macdata2) { | 
4129  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4130  | 0  |         goto err;  | 
4131  | 0  |     }  | 
4132  |  |  | 
4133  |  |     /* Close the sub-packet created by create_ticket_prequel() */  | 
4134  | 96  |     if (!WPACKET_close(pkt)) { | 
4135  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4136  | 0  |         goto err;  | 
4137  | 0  |     }  | 
4138  |  |  | 
4139  | 96  |     ok = CON_FUNC_SUCCESS;  | 
4140  | 96  |  err:  | 
4141  | 96  |     OPENSSL_free(senc);  | 
4142  | 96  |     EVP_CIPHER_CTX_free(ctx);  | 
4143  | 96  |     ssl_hmac_free(hctx);  | 
4144  | 96  |     return ok;  | 
4145  | 96  | }  | 
4146  |  |  | 
4147  |  | static int construct_stateful_ticket(SSL_CONNECTION *s, WPACKET *pkt,  | 
4148  |  |                                      uint32_t age_add,  | 
4149  |  |                                      unsigned char *tick_nonce)  | 
4150  | 0  | { | 
4151  | 0  |     if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) { | 
4152  |  |         /* SSLfatal() already called */  | 
4153  | 0  |         return 0;  | 
4154  | 0  |     }  | 
4155  |  |  | 
4156  | 0  |     if (!WPACKET_memcpy(pkt, s->session->session_id,  | 
4157  | 0  |                         s->session->session_id_length)  | 
4158  | 0  |             || !WPACKET_close(pkt)) { | 
4159  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4160  | 0  |         return 0;  | 
4161  | 0  |     }  | 
4162  |  |  | 
4163  | 0  |     return 1;  | 
4164  | 0  | }  | 
4165  |  |  | 
4166  |  | static void tls_update_ticket_counts(SSL_CONNECTION *s)  | 
4167  | 0  | { | 
4168  |  |     /*  | 
4169  |  |      * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|  | 
4170  |  |      * gets reset to 0 if we send more tickets following a post-handshake  | 
4171  |  |      * auth, but |next_ticket_nonce| does not.  If we're sending extra  | 
4172  |  |      * tickets, decrement the count of pending extra tickets.  | 
4173  |  |      */  | 
4174  | 0  |     s->sent_tickets++;  | 
4175  | 0  |     s->next_ticket_nonce++;  | 
4176  | 0  |     if (s->ext.extra_tickets_expected > 0)  | 
4177  | 0  |         s->ext.extra_tickets_expected--;  | 
4178  | 0  | }  | 
4179  |  |  | 
4180  |  | CON_FUNC_RETURN tls_construct_new_session_ticket(SSL_CONNECTION *s, WPACKET *pkt)  | 
4181  | 87  | { | 
4182  | 87  |     SSL_CTX *tctx = s->session_ctx;  | 
4183  | 87  |     unsigned char tick_nonce[TICKET_NONCE_SIZE];  | 
4184  | 87  |     union { | 
4185  | 87  |         unsigned char age_add_c[sizeof(uint32_t)];  | 
4186  | 87  |         uint32_t age_add;  | 
4187  | 87  |     } age_add_u;  | 
4188  | 87  |     CON_FUNC_RETURN ret = CON_FUNC_ERROR;  | 
4189  |  |  | 
4190  | 87  |     age_add_u.age_add = 0;  | 
4191  |  |  | 
4192  | 87  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
4193  | 0  |         size_t i, hashlen;  | 
4194  | 0  |         uint64_t nonce;  | 
4195  | 0  |         static const unsigned char nonce_label[] = "resumption";  | 
4196  | 0  |         const EVP_MD *md = ssl_handshake_md(s);  | 
4197  | 0  |         int hashleni = EVP_MD_get_size(md);  | 
4198  |  |  | 
4199  |  |         /* Ensure cast to size_t is safe */  | 
4200  | 0  |         if (!ossl_assert(hashleni >= 0)) { | 
4201  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4202  | 0  |             goto err;  | 
4203  | 0  |         }  | 
4204  | 0  |         hashlen = (size_t)hashleni;  | 
4205  |  |  | 
4206  |  |         /*  | 
4207  |  |          * If we already sent one NewSessionTicket, or we resumed then  | 
4208  |  |          * s->session may already be in a cache and so we must not modify it.  | 
4209  |  |          * Instead we need to take a copy of it and modify that.  | 
4210  |  |          */  | 
4211  | 0  |         if (s->sent_tickets != 0 || s->hit) { | 
4212  | 0  |             SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);  | 
4213  |  | 
  | 
4214  | 0  |             if (new_sess == NULL) { | 
4215  |  |                 /* SSLfatal already called */  | 
4216  | 0  |                 goto err;  | 
4217  | 0  |             }  | 
4218  |  |  | 
4219  | 0  |             SSL_SESSION_free(s->session);  | 
4220  | 0  |             s->session = new_sess;  | 
4221  | 0  |         }  | 
4222  |  |  | 
4223  | 0  |         if (!ssl_generate_session_id(s, s->session)) { | 
4224  |  |             /* SSLfatal() already called */  | 
4225  | 0  |             goto err;  | 
4226  | 0  |         }  | 
4227  | 0  |         if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx,  | 
4228  | 0  |                           age_add_u.age_add_c, sizeof(age_add_u), 0) <= 0) { | 
4229  | 0  |             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4230  | 0  |             goto err;  | 
4231  | 0  |         }  | 
4232  | 0  |         s->session->ext.tick_age_add = age_add_u.age_add;  | 
4233  |  | 
  | 
4234  | 0  |         nonce = s->next_ticket_nonce;  | 
4235  | 0  |         for (i = TICKET_NONCE_SIZE; i > 0; i--) { | 
4236  | 0  |             tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);  | 
4237  | 0  |             nonce >>= 8;  | 
4238  | 0  |         }  | 
4239  |  | 
  | 
4240  | 0  |         if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,  | 
4241  | 0  |                                nonce_label,  | 
4242  | 0  |                                sizeof(nonce_label) - 1,  | 
4243  | 0  |                                tick_nonce,  | 
4244  | 0  |                                TICKET_NONCE_SIZE,  | 
4245  | 0  |                                s->session->master_key,  | 
4246  | 0  |                                hashlen, 1)) { | 
4247  |  |             /* SSLfatal() already called */  | 
4248  | 0  |             goto err;  | 
4249  | 0  |         }  | 
4250  | 0  |         s->session->master_key_length = hashlen;  | 
4251  |  | 
  | 
4252  | 0  |         s->session->time = ossl_time_now();  | 
4253  | 0  |         ssl_session_calculate_timeout(s->session);  | 
4254  | 0  |         if (s->s3.alpn_selected != NULL) { | 
4255  | 0  |             OPENSSL_free(s->session->ext.alpn_selected);  | 
4256  | 0  |             s->session->ext.alpn_selected =  | 
4257  | 0  |                 OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);  | 
4258  | 0  |             if (s->session->ext.alpn_selected == NULL) { | 
4259  | 0  |                 s->session->ext.alpn_selected_len = 0;  | 
4260  | 0  |                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);  | 
4261  | 0  |                 goto err;  | 
4262  | 0  |             }  | 
4263  | 0  |             s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;  | 
4264  | 0  |         }  | 
4265  | 0  |         s->session->ext.max_early_data = s->max_early_data;  | 
4266  | 0  |     }  | 
4267  |  |  | 
4268  | 87  |     if (tctx->generate_ticket_cb != NULL &&  | 
4269  | 87  |         tctx->generate_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s),  | 
4270  | 0  |                                  tctx->ticket_cb_data) == 0) { | 
4271  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4272  | 0  |         goto err;  | 
4273  | 0  |     }  | 
4274  |  |     /*  | 
4275  |  |      * If we are using anti-replay protection then we behave as if  | 
4276  |  |      * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there  | 
4277  |  |      * is no point in using full stateless tickets.  | 
4278  |  |      */  | 
4279  | 87  |     if (SSL_CONNECTION_IS_TLS13(s)  | 
4280  | 87  |             && ((s->options & SSL_OP_NO_TICKET) != 0  | 
4281  | 0  |                 || (s->max_early_data > 0  | 
4282  | 0  |                     && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) { | 
4283  | 0  |         if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) { | 
4284  |  |             /* SSLfatal() already called */  | 
4285  | 0  |             goto err;  | 
4286  | 0  |         }  | 
4287  | 87  |     } else { | 
4288  | 87  |         CON_FUNC_RETURN tmpret;  | 
4289  |  |  | 
4290  | 87  |         tmpret = construct_stateless_ticket(s, pkt, age_add_u.age_add,  | 
4291  | 87  |                                             tick_nonce);  | 
4292  | 87  |         if (tmpret != CON_FUNC_SUCCESS) { | 
4293  | 0  |             if (tmpret == CON_FUNC_DONT_SEND) { | 
4294  |  |                 /* Non-fatal. Abort construction but continue */  | 
4295  | 0  |                 ret = CON_FUNC_DONT_SEND;  | 
4296  |  |                 /* We count this as a success so update the counts anwyay */  | 
4297  | 0  |                 tls_update_ticket_counts(s);  | 
4298  | 0  |             }  | 
4299  |  |             /* else SSLfatal() already called */  | 
4300  | 0  |             goto err;  | 
4301  | 0  |         }  | 
4302  | 87  |     }  | 
4303  |  |  | 
4304  | 87  |     if (SSL_CONNECTION_IS_TLS13(s)) { | 
4305  | 0  |         if (!tls_construct_extensions(s, pkt,  | 
4306  | 0  |                                       SSL_EXT_TLS1_3_NEW_SESSION_TICKET,  | 
4307  | 0  |                                       NULL, 0)) { | 
4308  |  |             /* SSLfatal() already called */  | 
4309  | 0  |             goto err;  | 
4310  | 0  |         }  | 
4311  | 0  |         tls_update_ticket_counts(s);  | 
4312  | 0  |         ssl_update_cache(s, SSL_SESS_CACHE_SERVER);  | 
4313  | 0  |     }  | 
4314  |  |  | 
4315  | 87  |     ret = CON_FUNC_SUCCESS;  | 
4316  | 87  |  err:  | 
4317  | 87  |     return ret;  | 
4318  | 87  | }  | 
4319  |  |  | 
4320  |  | /*  | 
4321  |  |  * In TLSv1.3 this is called from the extensions code, otherwise it is used to  | 
4322  |  |  * create a separate message. Returns 1 on success or 0 on failure.  | 
4323  |  |  */  | 
4324  |  | int tls_construct_cert_status_body(SSL_CONNECTION *s, WPACKET *pkt)  | 
4325  | 0  | { | 
4326  | 0  |     if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)  | 
4327  | 0  |             || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,  | 
4328  | 0  |                                        s->ext.ocsp.resp_len)) { | 
4329  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4330  | 0  |         return 0;  | 
4331  | 0  |     }  | 
4332  |  |  | 
4333  | 0  |     return 1;  | 
4334  | 0  | }  | 
4335  |  |  | 
4336  |  | CON_FUNC_RETURN tls_construct_cert_status(SSL_CONNECTION *s, WPACKET *pkt)  | 
4337  | 0  | { | 
4338  | 0  |     if (!tls_construct_cert_status_body(s, pkt)) { | 
4339  |  |         /* SSLfatal() already called */  | 
4340  | 0  |         return CON_FUNC_ERROR;  | 
4341  | 0  |     }  | 
4342  |  |  | 
4343  | 0  |     return CON_FUNC_SUCCESS;  | 
4344  | 0  | }  | 
4345  |  |  | 
4346  |  | #ifndef OPENSSL_NO_NEXTPROTONEG  | 
4347  |  | /*  | 
4348  |  |  * tls_process_next_proto reads a Next Protocol Negotiation handshake message.  | 
4349  |  |  * It sets the next_proto member in s if found  | 
4350  |  |  */  | 
4351  |  | MSG_PROCESS_RETURN tls_process_next_proto(SSL_CONNECTION *s, PACKET *pkt)  | 
4352  | 0  | { | 
4353  | 0  |     PACKET next_proto, padding;  | 
4354  | 0  |     size_t next_proto_len;  | 
4355  |  |  | 
4356  |  |     /*-  | 
4357  |  |      * The payload looks like:  | 
4358  |  |      *   uint8 proto_len;  | 
4359  |  |      *   uint8 proto[proto_len];  | 
4360  |  |      *   uint8 padding_len;  | 
4361  |  |      *   uint8 padding[padding_len];  | 
4362  |  |      */  | 
4363  | 0  |     if (!PACKET_get_length_prefixed_1(pkt, &next_proto)  | 
4364  | 0  |         || !PACKET_get_length_prefixed_1(pkt, &padding)  | 
4365  | 0  |         || PACKET_remaining(pkt) > 0) { | 
4366  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
4367  | 0  |         return MSG_PROCESS_ERROR;  | 
4368  | 0  |     }  | 
4369  |  |  | 
4370  | 0  |     if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) { | 
4371  | 0  |         s->ext.npn_len = 0;  | 
4372  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4373  | 0  |         return MSG_PROCESS_ERROR;  | 
4374  | 0  |     }  | 
4375  |  |  | 
4376  | 0  |     s->ext.npn_len = (unsigned char)next_proto_len;  | 
4377  |  | 
  | 
4378  | 0  |     return MSG_PROCESS_CONTINUE_READING;  | 
4379  | 0  | }  | 
4380  |  | #endif  | 
4381  |  |  | 
4382  |  | static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s,  | 
4383  |  |                                                           WPACKET *pkt)  | 
4384  | 1.96k  | { | 
4385  | 1.96k  |     if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,  | 
4386  | 1.96k  |                                   NULL, 0)) { | 
4387  |  |         /* SSLfatal() already called */  | 
4388  | 0  |         return CON_FUNC_ERROR;  | 
4389  | 0  |     }  | 
4390  |  |  | 
4391  | 1.96k  |     return CON_FUNC_SUCCESS;  | 
4392  | 1.96k  | }  | 
4393  |  |  | 
4394  |  | MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL_CONNECTION *s, PACKET *pkt)  | 
4395  | 0  | { | 
4396  | 0  |     if (PACKET_remaining(pkt) != 0) { | 
4397  | 0  |         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);  | 
4398  | 0  |         return MSG_PROCESS_ERROR;  | 
4399  | 0  |     }  | 
4400  |  |  | 
4401  | 0  |     if (s->early_data_state != SSL_EARLY_DATA_READING  | 
4402  | 0  |             && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) { | 
4403  | 0  |         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);  | 
4404  | 0  |         return MSG_PROCESS_ERROR;  | 
4405  | 0  |     }  | 
4406  |  |  | 
4407  |  |     /*  | 
4408  |  |      * EndOfEarlyData signals a key change so the end of the message must be on  | 
4409  |  |      * a record boundary.  | 
4410  |  |      */  | 
4411  | 0  |     if (RECORD_LAYER_processed_read_pending(&s->rlayer)) { | 
4412  | 0  |         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);  | 
4413  | 0  |         return MSG_PROCESS_ERROR;  | 
4414  | 0  |     }  | 
4415  |  |  | 
4416  | 0  |     s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;  | 
4417  | 0  |     if (!SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->change_cipher_state(s,  | 
4418  | 0  |                 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) { | 
4419  |  |         /* SSLfatal() already called */  | 
4420  | 0  |         return MSG_PROCESS_ERROR;  | 
4421  | 0  |     }  | 
4422  |  |  | 
4423  | 0  |     return MSG_PROCESS_CONTINUE_READING;  | 
4424  | 0  | }  |