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