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