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