]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/statem/statem_srvr.c
Export SSL_bytes_to_cipher_list()
[thirdparty/openssl.git] / ssl / statem / statem_srvr.c
1 /*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 *
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
15 *
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
18 *
19 * ECC cipher suite support in OpenSSL originally written by
20 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
21 *
22 */
23 /* ====================================================================
24 * Copyright 2005 Nokia. All rights reserved.
25 *
26 * The portions of the attached software ("Contribution") is developed by
27 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
28 * license.
29 *
30 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
31 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
32 * support (see RFC 4279) to OpenSSL.
33 *
34 * No patent licenses or other rights except those expressly stated in
35 * the OpenSSL open source license shall be deemed granted or received
36 * expressly, by implication, estoppel, or otherwise.
37 *
38 * No assurances are provided by Nokia that the Contribution does not
39 * infringe the patent or other intellectual property rights of any third
40 * party or that the license provides you with all the necessary rights
41 * to make use of the Contribution.
42 *
43 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
44 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
45 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
46 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
47 * OTHERWISE.
48 */
49
50 #include <stdio.h>
51 #include "../ssl_locl.h"
52 #include "statem_locl.h"
53 #include "internal/constant_time_locl.h"
54 #include <openssl/buffer.h>
55 #include <openssl/rand.h>
56 #include <openssl/objects.h>
57 #include <openssl/evp.h>
58 #include <openssl/hmac.h>
59 #include <openssl/x509.h>
60 #include <openssl/dh.h>
61 #include <openssl/bn.h>
62 #include <openssl/md5.h>
63
64 static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
65 static int tls_construct_hello_retry_request(SSL *s, WPACKET *pkt);
66
67 /*
68 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
69 * handshake state transitions when a TLSv1.3 server is reading messages from
70 * the client. The message type that the client has sent is provided in |mt|.
71 * The current state is in |s->statem.hand_state|.
72 *
73 * Return values are 1 for success (transition allowed) and 0 on error
74 * (transition not allowed)
75 */
76 static int ossl_statem_server13_read_transition(SSL *s, int mt)
77 {
78 OSSL_STATEM *st = &s->statem;
79
80 /*
81 * Note: There is no case for TLS_ST_BEFORE because at that stage we have
82 * not negotiated TLSv1.3 yet, so that case is handled by
83 * ossl_statem_server_read_transition()
84 */
85 switch (st->hand_state) {
86 default:
87 break;
88
89 case TLS_ST_SW_HELLO_RETRY_REQUEST:
90 if (mt == SSL3_MT_CLIENT_HELLO) {
91 st->hand_state = TLS_ST_SR_CLNT_HELLO;
92 return 1;
93 }
94 break;
95
96 case TLS_ST_SW_FINISHED:
97 if (s->s3->tmp.cert_request) {
98 if (mt == SSL3_MT_CERTIFICATE) {
99 st->hand_state = TLS_ST_SR_CERT;
100 return 1;
101 }
102 } else {
103 if (mt == SSL3_MT_FINISHED) {
104 st->hand_state = TLS_ST_SR_FINISHED;
105 return 1;
106 }
107 }
108 break;
109
110 case TLS_ST_SR_CERT:
111 if (s->session->peer == NULL) {
112 if (mt == SSL3_MT_FINISHED) {
113 st->hand_state = TLS_ST_SR_FINISHED;
114 return 1;
115 }
116 } else {
117 if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
118 st->hand_state = TLS_ST_SR_CERT_VRFY;
119 return 1;
120 }
121 }
122 break;
123
124 case TLS_ST_SR_CERT_VRFY:
125 if (mt == SSL3_MT_FINISHED) {
126 st->hand_state = TLS_ST_SR_FINISHED;
127 return 1;
128 }
129 break;
130
131 case TLS_ST_OK:
132 if (mt == SSL3_MT_KEY_UPDATE) {
133 st->hand_state = TLS_ST_SR_KEY_UPDATE;
134 return 1;
135 }
136 break;
137 }
138
139 /* No valid transition found */
140 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
141 SSLerr(SSL_F_OSSL_STATEM_SERVER13_READ_TRANSITION,
142 SSL_R_UNEXPECTED_MESSAGE);
143 return 0;
144 }
145
146 /*
147 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
148 * handshake state transitions when the server is reading messages from the
149 * client. The message type that the client has sent is provided in |mt|. The
150 * current state is in |s->statem.hand_state|.
151 *
152 * Return values are 1 for success (transition allowed) and 0 on error
153 * (transition not allowed)
154 */
155 int ossl_statem_server_read_transition(SSL *s, int mt)
156 {
157 OSSL_STATEM *st = &s->statem;
158
159 if (SSL_IS_TLS13(s)) {
160 if (!ossl_statem_server13_read_transition(s, mt))
161 goto err;
162 return 1;
163 }
164
165 switch (st->hand_state) {
166 default:
167 break;
168
169 case TLS_ST_BEFORE:
170 case TLS_ST_OK:
171 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
172 if (mt == SSL3_MT_CLIENT_HELLO) {
173 st->hand_state = TLS_ST_SR_CLNT_HELLO;
174 return 1;
175 }
176 break;
177
178 case TLS_ST_SW_SRVR_DONE:
179 /*
180 * If we get a CKE message after a ServerDone then either
181 * 1) We didn't request a Certificate
182 * OR
183 * 2) If we did request one then
184 * a) We allow no Certificate to be returned
185 * AND
186 * b) We are running SSL3 (in TLS1.0+ the client must return a 0
187 * list if we requested a certificate)
188 */
189 if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
190 if (s->s3->tmp.cert_request) {
191 if (s->version == SSL3_VERSION) {
192 if ((s->verify_mode & SSL_VERIFY_PEER)
193 && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
194 /*
195 * This isn't an unexpected message as such - we're just
196 * not going to accept it because we require a client
197 * cert.
198 */
199 ssl3_send_alert(s, SSL3_AL_FATAL,
200 SSL3_AD_HANDSHAKE_FAILURE);
201 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
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 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
300 SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
301 return 0;
302 }
303
304 /*
305 * Should we send a ServerKeyExchange message?
306 *
307 * Valid return values are:
308 * 1: Yes
309 * 0: No
310 */
311 static int send_server_key_exchange(SSL *s)
312 {
313 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
314
315 /*
316 * only send a ServerKeyExchange if DH or fortezza but we have a
317 * sign only certificate PSK: may send PSK identity hints For
318 * ECC ciphersuites, we send a serverKeyExchange message only if
319 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
320 * the server certificate contains the server's public key for
321 * key exchange.
322 */
323 if (alg_k & (SSL_kDHE | SSL_kECDHE)
324 /*
325 * PSK: send ServerKeyExchange if PSK identity hint if
326 * provided
327 */
328 #ifndef OPENSSL_NO_PSK
329 /* Only send SKE if we have identity hint for plain PSK */
330 || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
331 && s->cert->psk_identity_hint)
332 /* For other PSK always send SKE */
333 || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
334 #endif
335 #ifndef OPENSSL_NO_SRP
336 /* SRP: send ServerKeyExchange */
337 || (alg_k & SSL_kSRP)
338 #endif
339 ) {
340 return 1;
341 }
342
343 return 0;
344 }
345
346 /*
347 * Should we send a CertificateRequest message?
348 *
349 * Valid return values are:
350 * 1: Yes
351 * 0: No
352 */
353 static int send_certificate_request(SSL *s)
354 {
355 if (
356 /* don't request cert unless asked for it: */
357 s->verify_mode & SSL_VERIFY_PEER
358 /*
359 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
360 * during re-negotiation:
361 */
362 && (s->s3->tmp.finish_md_len == 0 ||
363 !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
364 /*
365 * never request cert in anonymous ciphersuites (see
366 * section "Certificate request" in SSL 3 drafts and in
367 * RFC 2246):
368 */
369 && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
370 /*
371 * ... except when the application insists on
372 * verification (against the specs, but statem_clnt.c accepts
373 * this for SSL 3)
374 */
375 || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
376 /* don't request certificate for SRP auth */
377 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
378 /*
379 * With normal PSK Certificates and Certificate Requests
380 * are omitted
381 */
382 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
383 return 1;
384 }
385
386 return 0;
387 }
388
389 /*
390 * ossl_statem_server13_write_transition() works out what handshake state to
391 * move to next when a TLSv1.3 server is writing messages to be sent to the
392 * client.
393 */
394 static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
395 {
396 OSSL_STATEM *st = &s->statem;
397
398 /*
399 * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
400 * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
401 */
402
403 switch (st->hand_state) {
404 default:
405 /* Shouldn't happen */
406 return WRITE_TRAN_ERROR;
407
408 case TLS_ST_OK:
409 if (s->key_update != SSL_KEY_UPDATE_NONE) {
410 st->hand_state = TLS_ST_SW_KEY_UPDATE;
411 return WRITE_TRAN_CONTINUE;
412 }
413 /* Try to read from the client instead */
414 return WRITE_TRAN_FINISHED;
415
416 case TLS_ST_SR_CLNT_HELLO:
417 if (s->hello_retry_request)
418 st->hand_state = TLS_ST_SW_HELLO_RETRY_REQUEST;
419 else
420 st->hand_state = TLS_ST_SW_SRVR_HELLO;
421 return WRITE_TRAN_CONTINUE;
422
423 case TLS_ST_SW_HELLO_RETRY_REQUEST:
424 return WRITE_TRAN_FINISHED;
425
426 case TLS_ST_SW_SRVR_HELLO:
427 st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
428 return WRITE_TRAN_CONTINUE;
429
430 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
431 if (s->hit)
432 st->hand_state = TLS_ST_SW_FINISHED;
433 else if (send_certificate_request(s))
434 st->hand_state = TLS_ST_SW_CERT_REQ;
435 else
436 st->hand_state = TLS_ST_SW_CERT;
437
438 return WRITE_TRAN_CONTINUE;
439
440 case TLS_ST_SW_CERT_REQ:
441 st->hand_state = TLS_ST_SW_CERT;
442 return WRITE_TRAN_CONTINUE;
443
444 case TLS_ST_SW_CERT:
445 st->hand_state = TLS_ST_SW_CERT_VRFY;
446 return WRITE_TRAN_CONTINUE;
447
448 case TLS_ST_SW_CERT_VRFY:
449 st->hand_state = TLS_ST_SW_FINISHED;
450 return WRITE_TRAN_CONTINUE;
451
452 case TLS_ST_SW_FINISHED:
453 return WRITE_TRAN_FINISHED;
454
455 case TLS_ST_SR_FINISHED:
456 /*
457 * Technically we have finished the handshake at this point, but we're
458 * going to remain "in_init" for now and write out the session ticket
459 * immediately.
460 * TODO(TLS1.3): Perhaps we need to be able to control this behaviour
461 * and give the application the opportunity to delay sending the
462 * session ticket?
463 */
464 st->hand_state = TLS_ST_SW_SESSION_TICKET;
465 return WRITE_TRAN_CONTINUE;
466
467 case TLS_ST_SR_KEY_UPDATE:
468 if (s->key_update != SSL_KEY_UPDATE_NONE) {
469 st->hand_state = TLS_ST_SW_KEY_UPDATE;
470 return WRITE_TRAN_CONTINUE;
471 }
472 /* Fall through */
473
474 case TLS_ST_SW_KEY_UPDATE:
475 case TLS_ST_SW_SESSION_TICKET:
476 st->hand_state = TLS_ST_OK;
477 ossl_statem_set_in_init(s, 0);
478 return WRITE_TRAN_CONTINUE;
479 }
480 }
481
482 /*
483 * ossl_statem_server_write_transition() works out what handshake state to move
484 * to next when the server is writing messages to be sent to the client.
485 */
486 WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
487 {
488 OSSL_STATEM *st = &s->statem;
489
490 /*
491 * Note that before the ClientHello we don't know what version we are going
492 * to negotiate yet, so we don't take this branch until later
493 */
494
495 if (SSL_IS_TLS13(s))
496 return ossl_statem_server13_write_transition(s);
497
498 switch (st->hand_state) {
499 default:
500 /* Shouldn't happen */
501 return WRITE_TRAN_ERROR;
502
503 case TLS_ST_OK:
504 if (st->request_state == TLS_ST_SW_HELLO_REQ) {
505 /* We must be trying to renegotiate */
506 st->hand_state = TLS_ST_SW_HELLO_REQ;
507 st->request_state = TLS_ST_BEFORE;
508 return WRITE_TRAN_CONTINUE;
509 }
510 /* Must be an incoming ClientHello */
511 if (!tls_setup_handshake(s)) {
512 ossl_statem_set_error(s);
513 return WRITE_TRAN_ERROR;
514 }
515 /* Fall through */
516
517 case TLS_ST_BEFORE:
518 /* Just go straight to trying to read from the client */
519 return WRITE_TRAN_FINISHED;
520
521 case TLS_ST_SW_HELLO_REQ:
522 st->hand_state = TLS_ST_OK;
523 ossl_statem_set_in_init(s, 0);
524 return WRITE_TRAN_CONTINUE;
525
526 case TLS_ST_SR_CLNT_HELLO:
527 if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
528 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
529 st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
530 else
531 st->hand_state = TLS_ST_SW_SRVR_HELLO;
532 return WRITE_TRAN_CONTINUE;
533
534 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
535 return WRITE_TRAN_FINISHED;
536
537 case TLS_ST_SW_SRVR_HELLO:
538 if (s->hit) {
539 if (s->ext.ticket_expected)
540 st->hand_state = TLS_ST_SW_SESSION_TICKET;
541 else
542 st->hand_state = TLS_ST_SW_CHANGE;
543 } else {
544 /* Check if it is anon DH or anon ECDH, */
545 /* normal PSK or SRP */
546 if (!(s->s3->tmp.new_cipher->algorithm_auth &
547 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
548 st->hand_state = TLS_ST_SW_CERT;
549 } else if (send_server_key_exchange(s)) {
550 st->hand_state = TLS_ST_SW_KEY_EXCH;
551 } else if (send_certificate_request(s)) {
552 st->hand_state = TLS_ST_SW_CERT_REQ;
553 } else {
554 st->hand_state = TLS_ST_SW_SRVR_DONE;
555 }
556 }
557 return WRITE_TRAN_CONTINUE;
558
559 case TLS_ST_SW_CERT:
560 if (s->ext.status_expected) {
561 st->hand_state = TLS_ST_SW_CERT_STATUS;
562 return WRITE_TRAN_CONTINUE;
563 }
564 /* Fall through */
565
566 case TLS_ST_SW_CERT_STATUS:
567 if (send_server_key_exchange(s)) {
568 st->hand_state = TLS_ST_SW_KEY_EXCH;
569 return WRITE_TRAN_CONTINUE;
570 }
571 /* Fall through */
572
573 case TLS_ST_SW_KEY_EXCH:
574 if (send_certificate_request(s)) {
575 st->hand_state = TLS_ST_SW_CERT_REQ;
576 return WRITE_TRAN_CONTINUE;
577 }
578 /* Fall through */
579
580 case TLS_ST_SW_CERT_REQ:
581 st->hand_state = TLS_ST_SW_SRVR_DONE;
582 return WRITE_TRAN_CONTINUE;
583
584 case TLS_ST_SW_SRVR_DONE:
585 return WRITE_TRAN_FINISHED;
586
587 case TLS_ST_SR_FINISHED:
588 if (s->hit) {
589 st->hand_state = TLS_ST_OK;
590 ossl_statem_set_in_init(s, 0);
591 return WRITE_TRAN_CONTINUE;
592 } else if (s->ext.ticket_expected) {
593 st->hand_state = TLS_ST_SW_SESSION_TICKET;
594 } else {
595 st->hand_state = TLS_ST_SW_CHANGE;
596 }
597 return WRITE_TRAN_CONTINUE;
598
599 case TLS_ST_SW_SESSION_TICKET:
600 st->hand_state = TLS_ST_SW_CHANGE;
601 return WRITE_TRAN_CONTINUE;
602
603 case TLS_ST_SW_CHANGE:
604 st->hand_state = TLS_ST_SW_FINISHED;
605 return WRITE_TRAN_CONTINUE;
606
607 case TLS_ST_SW_FINISHED:
608 if (s->hit) {
609 return WRITE_TRAN_FINISHED;
610 }
611 st->hand_state = TLS_ST_OK;
612 ossl_statem_set_in_init(s, 0);
613 return WRITE_TRAN_CONTINUE;
614 }
615 }
616
617 /*
618 * Perform any pre work that needs to be done prior to sending a message from
619 * the server to the client.
620 */
621 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
622 {
623 OSSL_STATEM *st = &s->statem;
624
625 switch (st->hand_state) {
626 default:
627 /* No pre work to be done */
628 break;
629
630 case TLS_ST_SW_HELLO_REQ:
631 s->shutdown = 0;
632 if (SSL_IS_DTLS(s))
633 dtls1_clear_sent_buffer(s);
634 break;
635
636 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
637 s->shutdown = 0;
638 if (SSL_IS_DTLS(s)) {
639 dtls1_clear_sent_buffer(s);
640 /* We don't buffer this message so don't use the timer */
641 st->use_timer = 0;
642 }
643 break;
644
645 case TLS_ST_SW_SRVR_HELLO:
646 if (SSL_IS_DTLS(s)) {
647 /*
648 * Messages we write from now on should be bufferred and
649 * retransmitted if necessary, so we need to use the timer now
650 */
651 st->use_timer = 1;
652 }
653 break;
654
655 case TLS_ST_SW_SRVR_DONE:
656 #ifndef OPENSSL_NO_SCTP
657 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)))
658 return dtls_wait_for_dry(s);
659 #endif
660 return WORK_FINISHED_CONTINUE;
661
662 case TLS_ST_SW_SESSION_TICKET:
663 if (SSL_IS_TLS13(s)) {
664 /*
665 * Actually this is the end of the handshake, but we're going
666 * straight into writing the session ticket out. So we finish off
667 * the handshake, but keep the various buffers active.
668 */
669 return tls_finish_handshake(s, wst, 0);
670 } if (SSL_IS_DTLS(s)) {
671 /*
672 * We're into the last flight. We don't retransmit the last flight
673 * unless we need to, so we don't use the timer
674 */
675 st->use_timer = 0;
676 }
677 break;
678
679 case TLS_ST_SW_CHANGE:
680 s->session->cipher = s->s3->tmp.new_cipher;
681 if (!s->method->ssl3_enc->setup_key_block(s)) {
682 ossl_statem_set_error(s);
683 return WORK_ERROR;
684 }
685 if (SSL_IS_DTLS(s)) {
686 /*
687 * We're into the last flight. We don't retransmit the last flight
688 * unless we need to, so we don't use the timer. This might have
689 * already been set to 0 if we sent a NewSessionTicket message,
690 * but we'll set it again here in case we didn't.
691 */
692 st->use_timer = 0;
693 }
694 return WORK_FINISHED_CONTINUE;
695
696 case TLS_ST_OK:
697 return tls_finish_handshake(s, wst, 1);
698 }
699
700 return WORK_FINISHED_CONTINUE;
701 }
702
703 /*
704 * Perform any work that needs to be done after sending a message from the
705 * server to the client.
706 */
707 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
708 {
709 OSSL_STATEM *st = &s->statem;
710
711 s->init_num = 0;
712
713 switch (st->hand_state) {
714 default:
715 /* No post work to be done */
716 break;
717
718 case TLS_ST_SW_HELLO_RETRY_REQUEST:
719 if (statem_flush(s) != 1)
720 return WORK_MORE_A;
721 break;
722
723 case TLS_ST_SW_HELLO_REQ:
724 if (statem_flush(s) != 1)
725 return WORK_MORE_A;
726 if (!ssl3_init_finished_mac(s)) {
727 ossl_statem_set_error(s);
728 return WORK_ERROR;
729 }
730 break;
731
732 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
733 if (statem_flush(s) != 1)
734 return WORK_MORE_A;
735 /* HelloVerifyRequest resets Finished MAC */
736 if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
737 ossl_statem_set_error(s);
738 return WORK_ERROR;
739 }
740 /*
741 * The next message should be another ClientHello which we need to
742 * treat like it was the first packet
743 */
744 s->first_packet = 1;
745 break;
746
747 case TLS_ST_SW_SRVR_HELLO:
748 #ifndef OPENSSL_NO_SCTP
749 if (SSL_IS_DTLS(s) && s->hit) {
750 unsigned char sctpauthkey[64];
751 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
752
753 /*
754 * Add new shared key for SCTP-Auth, will be ignored if no
755 * SCTP used.
756 */
757 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
758 sizeof(DTLS1_SCTP_AUTH_LABEL));
759
760 if (SSL_export_keying_material(s, sctpauthkey,
761 sizeof(sctpauthkey), labelbuffer,
762 sizeof(labelbuffer), NULL, 0,
763 0) <= 0) {
764 ossl_statem_set_error(s);
765 return WORK_ERROR;
766 }
767
768 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
769 sizeof(sctpauthkey), sctpauthkey);
770 }
771 #endif
772 /*
773 * TODO(TLS1.3): This actually causes a problem. We don't yet know
774 * whether the next record we are going to receive is an unencrypted
775 * alert, or an encrypted handshake message. We're going to need
776 * something clever in the record layer for this.
777 */
778 if (SSL_IS_TLS13(s)) {
779 if (!s->method->ssl3_enc->setup_key_block(s)
780 || !s->method->ssl3_enc->change_cipher_state(s,
781 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)
782 || !s->method->ssl3_enc->change_cipher_state(s,
783 SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ))
784 return WORK_ERROR;
785 }
786 break;
787
788 case TLS_ST_SW_CHANGE:
789 #ifndef OPENSSL_NO_SCTP
790 if (SSL_IS_DTLS(s) && !s->hit) {
791 /*
792 * Change to new shared key of SCTP-Auth, will be ignored if
793 * no SCTP used.
794 */
795 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
796 0, NULL);
797 }
798 #endif
799 if (!s->method->ssl3_enc->change_cipher_state(s,
800 SSL3_CHANGE_CIPHER_SERVER_WRITE))
801 {
802 ossl_statem_set_error(s);
803 return WORK_ERROR;
804 }
805
806 if (SSL_IS_DTLS(s))
807 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
808 break;
809
810 case TLS_ST_SW_SRVR_DONE:
811 if (statem_flush(s) != 1)
812 return WORK_MORE_A;
813 break;
814
815 case TLS_ST_SW_FINISHED:
816 if (statem_flush(s) != 1)
817 return WORK_MORE_A;
818 #ifndef OPENSSL_NO_SCTP
819 if (SSL_IS_DTLS(s) && s->hit) {
820 /*
821 * Change to new shared key of SCTP-Auth, will be ignored if
822 * no SCTP used.
823 */
824 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
825 0, NULL);
826 }
827 #endif
828 if (SSL_IS_TLS13(s)) {
829 if (!s->method->ssl3_enc->generate_master_secret(s,
830 s->master_secret, s->handshake_secret, 0,
831 &s->session->master_key_length)
832 || !s->method->ssl3_enc->change_cipher_state(s,
833 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
834 return WORK_ERROR;
835 }
836 break;
837
838 case TLS_ST_SW_KEY_UPDATE:
839 if (statem_flush(s) != 1)
840 return WORK_MORE_A;
841 if (!tls13_update_key(s, 1))
842 return WORK_ERROR;
843 break;
844
845 case TLS_ST_SW_SESSION_TICKET:
846 if (SSL_IS_TLS13(s) && statem_flush(s) != 1)
847 return WORK_MORE_A;
848 break;
849 }
850
851 return WORK_FINISHED_CONTINUE;
852 }
853
854 /*
855 * Get the message construction function and message type for sending from the
856 * server
857 *
858 * Valid return values are:
859 * 1: Success
860 * 0: Error
861 */
862 int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
863 confunc_f *confunc, int *mt)
864 {
865 OSSL_STATEM *st = &s->statem;
866
867 switch (st->hand_state) {
868 default:
869 /* Shouldn't happen */
870 return 0;
871
872 case TLS_ST_SW_CHANGE:
873 if (SSL_IS_DTLS(s))
874 *confunc = dtls_construct_change_cipher_spec;
875 else
876 *confunc = tls_construct_change_cipher_spec;
877 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
878 break;
879
880 case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
881 *confunc = dtls_construct_hello_verify_request;
882 *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
883 break;
884
885 case TLS_ST_SW_HELLO_REQ:
886 /* No construction function needed */
887 *confunc = NULL;
888 *mt = SSL3_MT_HELLO_REQUEST;
889 break;
890
891 case TLS_ST_SW_SRVR_HELLO:
892 *confunc = tls_construct_server_hello;
893 *mt = SSL3_MT_SERVER_HELLO;
894 break;
895
896 case TLS_ST_SW_CERT:
897 *confunc = tls_construct_server_certificate;
898 *mt = SSL3_MT_CERTIFICATE;
899 break;
900
901 case TLS_ST_SW_CERT_VRFY:
902 *confunc = tls_construct_cert_verify;
903 *mt = SSL3_MT_CERTIFICATE_VERIFY;
904 break;
905
906
907 case TLS_ST_SW_KEY_EXCH:
908 *confunc = tls_construct_server_key_exchange;
909 *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
910 break;
911
912 case TLS_ST_SW_CERT_REQ:
913 *confunc = tls_construct_certificate_request;
914 *mt = SSL3_MT_CERTIFICATE_REQUEST;
915 break;
916
917 case TLS_ST_SW_SRVR_DONE:
918 *confunc = tls_construct_server_done;
919 *mt = SSL3_MT_SERVER_DONE;
920 break;
921
922 case TLS_ST_SW_SESSION_TICKET:
923 *confunc = tls_construct_new_session_ticket;
924 *mt = SSL3_MT_NEWSESSION_TICKET;
925 break;
926
927 case TLS_ST_SW_CERT_STATUS:
928 *confunc = tls_construct_cert_status;
929 *mt = SSL3_MT_CERTIFICATE_STATUS;
930 break;
931
932 case TLS_ST_SW_FINISHED:
933 *confunc = tls_construct_finished;
934 *mt = SSL3_MT_FINISHED;
935 break;
936
937 case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
938 *confunc = tls_construct_encrypted_extensions;
939 *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
940 break;
941
942 case TLS_ST_SW_HELLO_RETRY_REQUEST:
943 *confunc = tls_construct_hello_retry_request;
944 *mt = SSL3_MT_HELLO_RETRY_REQUEST;
945 break;
946
947 case TLS_ST_SW_KEY_UPDATE:
948 *confunc = tls_construct_key_update;
949 *mt = SSL3_MT_KEY_UPDATE;
950 break;
951 }
952
953 return 1;
954 }
955
956 /*
957 * Maximum size (excluding the Handshake header) of a ClientHello message,
958 * calculated as follows:
959 *
960 * 2 + # client_version
961 * 32 + # only valid length for random
962 * 1 + # length of session_id
963 * 32 + # maximum size for session_id
964 * 2 + # length of cipher suites
965 * 2^16-2 + # maximum length of cipher suites array
966 * 1 + # length of compression_methods
967 * 2^8-1 + # maximum length of compression methods
968 * 2 + # length of extensions
969 * 2^16-1 # maximum length of extensions
970 */
971 #define CLIENT_HELLO_MAX_LENGTH 131396
972
973 #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
974 #define NEXT_PROTO_MAX_LENGTH 514
975
976 /*
977 * Returns the maximum allowed length for the current message that we are
978 * reading. Excludes the message header.
979 */
980 size_t ossl_statem_server_max_message_size(SSL *s)
981 {
982 OSSL_STATEM *st = &s->statem;
983
984 switch (st->hand_state) {
985 default:
986 /* Shouldn't happen */
987 return 0;
988
989 case TLS_ST_SR_CLNT_HELLO:
990 return CLIENT_HELLO_MAX_LENGTH;
991
992 case TLS_ST_SR_CERT:
993 return s->max_cert_list;
994
995 case TLS_ST_SR_KEY_EXCH:
996 return CLIENT_KEY_EXCH_MAX_LENGTH;
997
998 case TLS_ST_SR_CERT_VRFY:
999 return SSL3_RT_MAX_PLAIN_LENGTH;
1000
1001 #ifndef OPENSSL_NO_NEXTPROTONEG
1002 case TLS_ST_SR_NEXT_PROTO:
1003 return NEXT_PROTO_MAX_LENGTH;
1004 #endif
1005
1006 case TLS_ST_SR_CHANGE:
1007 return CCS_MAX_LENGTH;
1008
1009 case TLS_ST_SR_FINISHED:
1010 return FINISHED_MAX_LENGTH;
1011
1012 case TLS_ST_SR_KEY_UPDATE:
1013 return KEY_UPDATE_MAX_LENGTH;
1014 }
1015 }
1016
1017 /*
1018 * Process a message that the server has received from the client.
1019 */
1020 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
1021 {
1022 OSSL_STATEM *st = &s->statem;
1023
1024 switch (st->hand_state) {
1025 default:
1026 /* Shouldn't happen */
1027 return MSG_PROCESS_ERROR;
1028
1029 case TLS_ST_SR_CLNT_HELLO:
1030 return tls_process_client_hello(s, pkt);
1031
1032 case TLS_ST_SR_CERT:
1033 return tls_process_client_certificate(s, pkt);
1034
1035 case TLS_ST_SR_KEY_EXCH:
1036 return tls_process_client_key_exchange(s, pkt);
1037
1038 case TLS_ST_SR_CERT_VRFY:
1039 return tls_process_cert_verify(s, pkt);
1040
1041 #ifndef OPENSSL_NO_NEXTPROTONEG
1042 case TLS_ST_SR_NEXT_PROTO:
1043 return tls_process_next_proto(s, pkt);
1044 #endif
1045
1046 case TLS_ST_SR_CHANGE:
1047 return tls_process_change_cipher_spec(s, pkt);
1048
1049 case TLS_ST_SR_FINISHED:
1050 return tls_process_finished(s, pkt);
1051
1052 case TLS_ST_SR_KEY_UPDATE:
1053 return tls_process_key_update(s, pkt);
1054
1055 }
1056 }
1057
1058 /*
1059 * Perform any further processing required following the receipt of a message
1060 * from the client
1061 */
1062 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
1063 {
1064 OSSL_STATEM *st = &s->statem;
1065
1066 switch (st->hand_state) {
1067 default:
1068 /* Shouldn't happen */
1069 return WORK_ERROR;
1070
1071 case TLS_ST_SR_CLNT_HELLO:
1072 return tls_post_process_client_hello(s, wst);
1073
1074 case TLS_ST_SR_KEY_EXCH:
1075 return tls_post_process_client_key_exchange(s, wst);
1076
1077 case TLS_ST_SR_CERT_VRFY:
1078 #ifndef OPENSSL_NO_SCTP
1079 if ( /* Is this SCTP? */
1080 BIO_dgram_is_sctp(SSL_get_wbio(s))
1081 /* Are we renegotiating? */
1082 && s->renegotiate && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
1083 s->s3->in_read_app_data = 2;
1084 s->rwstate = SSL_READING;
1085 BIO_clear_retry_flags(SSL_get_rbio(s));
1086 BIO_set_retry_read(SSL_get_rbio(s));
1087 ossl_statem_set_sctp_read_sock(s, 1);
1088 return WORK_MORE_A;
1089 } else {
1090 ossl_statem_set_sctp_read_sock(s, 0);
1091 }
1092 #endif
1093 return WORK_FINISHED_CONTINUE;
1094 }
1095 return WORK_FINISHED_CONTINUE;
1096 }
1097
1098 #ifndef OPENSSL_NO_SRP
1099 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
1100 {
1101 int ret = SSL_ERROR_NONE;
1102
1103 *al = SSL_AD_UNRECOGNIZED_NAME;
1104
1105 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1106 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1107 if (s->srp_ctx.login == NULL) {
1108 /*
1109 * RFC 5054 says SHOULD reject, we do so if There is no srp
1110 * login name
1111 */
1112 ret = SSL3_AL_FATAL;
1113 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1114 } else {
1115 ret = SSL_srp_server_param_with_username(s, al);
1116 }
1117 }
1118 return ret;
1119 }
1120 #endif
1121
1122 int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1123 size_t cookie_len)
1124 {
1125 /* Always use DTLS 1.0 version: see RFC 6347 */
1126 if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1127 || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1128 return 0;
1129
1130 return 1;
1131 }
1132
1133 int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
1134 {
1135 unsigned int cookie_leni;
1136 if (s->ctx->app_gen_cookie_cb == NULL ||
1137 s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
1138 &cookie_leni) == 0 ||
1139 cookie_leni > 255) {
1140 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1141 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1142 return 0;
1143 }
1144 s->d1->cookie_len = cookie_leni;
1145
1146 if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1147 s->d1->cookie_len)) {
1148 SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST, ERR_R_INTERNAL_ERROR);
1149 return 0;
1150 }
1151
1152 return 1;
1153 }
1154
1155 #ifndef OPENSSL_NO_EC
1156 /*-
1157 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1158 * SecureTransport using the TLS extension block in |hello|.
1159 * Safari, since 10.6, sends exactly these extensions, in this order:
1160 * SNI,
1161 * elliptic_curves
1162 * ec_point_formats
1163 *
1164 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1165 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1166 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1167 * 10.8..10.8.3 (which don't work).
1168 */
1169 static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1170 {
1171 static const unsigned char kSafariExtensionsBlock[] = {
1172 0x00, 0x0a, /* elliptic_curves extension */
1173 0x00, 0x08, /* 8 bytes */
1174 0x00, 0x06, /* 6 bytes of curve ids */
1175 0x00, 0x17, /* P-256 */
1176 0x00, 0x18, /* P-384 */
1177 0x00, 0x19, /* P-521 */
1178
1179 0x00, 0x0b, /* ec_point_formats */
1180 0x00, 0x02, /* 2 bytes */
1181 0x01, /* 1 point format */
1182 0x00, /* uncompressed */
1183 /* The following is only present in TLS 1.2 */
1184 0x00, 0x0d, /* signature_algorithms */
1185 0x00, 0x0c, /* 12 bytes */
1186 0x00, 0x0a, /* 10 bytes */
1187 0x05, 0x01, /* SHA-384/RSA */
1188 0x04, 0x01, /* SHA-256/RSA */
1189 0x02, 0x01, /* SHA-1/RSA */
1190 0x04, 0x03, /* SHA-256/ECDSA */
1191 0x02, 0x03, /* SHA-1/ECDSA */
1192 };
1193 /* Length of the common prefix (first two extensions). */
1194 static const size_t kSafariCommonExtensionsLength = 18;
1195 unsigned int type;
1196 PACKET sni, tmppkt;
1197 size_t ext_len;
1198
1199 tmppkt = hello->extensions;
1200
1201 if (!PACKET_forward(&tmppkt, 2)
1202 || !PACKET_get_net_2(&tmppkt, &type)
1203 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1204 return;
1205 }
1206
1207 if (type != TLSEXT_TYPE_server_name)
1208 return;
1209
1210 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1211 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1212
1213 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1214 ext_len);
1215 }
1216 #endif /* !OPENSSL_NO_EC */
1217
1218 MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
1219 {
1220 int i, al = SSL_AD_INTERNAL_ERROR;
1221 unsigned int j;
1222 size_t loop;
1223 unsigned long id;
1224 const SSL_CIPHER *c;
1225 #ifndef OPENSSL_NO_COMP
1226 SSL_COMP *comp = NULL;
1227 #endif
1228 STACK_OF(SSL_CIPHER) *ciphers = NULL;
1229 int protverr;
1230 /* |cookie| will only be initialized for DTLS. */
1231 PACKET session_id, compression, extensions, cookie;
1232 static const unsigned char null_compression = 0;
1233 CLIENTHELLO_MSG clienthello;
1234
1235 /* Check if this is actually an unexpected renegotiation ClientHello */
1236 if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1237 s->renegotiate = 1;
1238 s->new_session = 1;
1239 }
1240
1241 /* This is a real handshake so make sure we clean it up at the end */
1242 s->statem.cleanuphand = 1;
1243
1244 /*
1245 * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1246 */
1247 memset(&clienthello, 0, sizeof(clienthello));
1248 clienthello.isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1249 PACKET_null_init(&cookie);
1250
1251 if (clienthello.isv2) {
1252 unsigned int mt;
1253
1254 if (!SSL_IS_FIRST_HANDSHAKE(s) || s->hello_retry_request) {
1255 al = SSL_AD_HANDSHAKE_FAILURE;
1256 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
1257 goto f_err;
1258 }
1259
1260 /*-
1261 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1262 * header is sent directly on the wire, not wrapped as a TLS
1263 * record. Our record layer just processes the message length and passes
1264 * the rest right through. Its format is:
1265 * Byte Content
1266 * 0-1 msg_length - decoded by the record layer
1267 * 2 msg_type - s->init_msg points here
1268 * 3-4 version
1269 * 5-6 cipher_spec_length
1270 * 7-8 session_id_length
1271 * 9-10 challenge_length
1272 * ... ...
1273 */
1274
1275 if (!PACKET_get_1(pkt, &mt)
1276 || mt != SSL2_MT_CLIENT_HELLO) {
1277 /*
1278 * Should never happen. We should have tested this in the record
1279 * layer in order to have determined that this is a SSLv2 record
1280 * in the first place
1281 */
1282 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1283 goto err;
1284 }
1285 }
1286
1287 if (!PACKET_get_net_2(pkt, &clienthello.legacy_version)) {
1288 al = SSL_AD_DECODE_ERROR;
1289 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1290 goto err;
1291 }
1292
1293 /* Parse the message and load client random. */
1294 if (clienthello.isv2) {
1295 /*
1296 * Handle an SSLv2 backwards compatible ClientHello
1297 * Note, this is only for SSLv3+ using the backward compatible format.
1298 * Real SSLv2 is not supported, and is rejected below.
1299 */
1300 unsigned int ciphersuite_len, session_id_len, challenge_len;
1301 PACKET challenge;
1302
1303 if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1304 || !PACKET_get_net_2(pkt, &session_id_len)
1305 || !PACKET_get_net_2(pkt, &challenge_len)) {
1306 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1307 SSL_R_RECORD_LENGTH_MISMATCH);
1308 al = SSL_AD_DECODE_ERROR;
1309 goto f_err;
1310 }
1311
1312 if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1313 al = SSL_AD_DECODE_ERROR;
1314 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1315 goto f_err;
1316 }
1317
1318 if (!PACKET_get_sub_packet(pkt, &clienthello.ciphersuites,
1319 ciphersuite_len)
1320 || !PACKET_copy_bytes(pkt, clienthello.session_id, session_id_len)
1321 || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1322 /* No extensions. */
1323 || PACKET_remaining(pkt) != 0) {
1324 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1325 SSL_R_RECORD_LENGTH_MISMATCH);
1326 al = SSL_AD_DECODE_ERROR;
1327 goto f_err;
1328 }
1329 clienthello.session_id_len = session_id_len;
1330
1331 /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1332 * here rather than sizeof(clienthello.random) because that is the limit
1333 * for SSLv3 and it is fixed. It won't change even if
1334 * sizeof(clienthello.random) does.
1335 */
1336 challenge_len = challenge_len > SSL3_RANDOM_SIZE
1337 ? SSL3_RANDOM_SIZE : challenge_len;
1338 memset(clienthello.random, 0, SSL3_RANDOM_SIZE);
1339 if (!PACKET_copy_bytes(&challenge,
1340 clienthello.random + SSL3_RANDOM_SIZE -
1341 challenge_len, challenge_len)
1342 /* Advertise only null compression. */
1343 || !PACKET_buf_init(&compression, &null_compression, 1)) {
1344 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1345 al = SSL_AD_INTERNAL_ERROR;
1346 goto f_err;
1347 }
1348
1349 PACKET_null_init(&clienthello.extensions);
1350 } else {
1351 /* Regular ClientHello. */
1352 if (!PACKET_copy_bytes(pkt, clienthello.random, SSL3_RANDOM_SIZE)
1353 || !PACKET_get_length_prefixed_1(pkt, &session_id)
1354 || !PACKET_copy_all(&session_id, clienthello.session_id,
1355 SSL_MAX_SSL_SESSION_ID_LENGTH,
1356 &clienthello.session_id_len)) {
1357 al = SSL_AD_DECODE_ERROR;
1358 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1359 goto f_err;
1360 }
1361
1362 if (SSL_IS_DTLS(s)) {
1363 if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1364 al = SSL_AD_DECODE_ERROR;
1365 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1366 goto f_err;
1367 }
1368 if (!PACKET_copy_all(&cookie, clienthello.dtls_cookie,
1369 DTLS1_COOKIE_LENGTH,
1370 &clienthello.dtls_cookie_len)) {
1371 al = SSL_AD_DECODE_ERROR;
1372 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1373 goto f_err;
1374 }
1375 /*
1376 * If we require cookies and this ClientHello doesn't contain one,
1377 * just return since we do not want to allocate any memory yet.
1378 * So check cookie length...
1379 */
1380 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1381 if (clienthello.dtls_cookie_len == 0)
1382 return 1;
1383 }
1384 }
1385
1386 if (!PACKET_get_length_prefixed_2(pkt, &clienthello.ciphersuites)) {
1387 al = SSL_AD_DECODE_ERROR;
1388 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1389 goto f_err;
1390 }
1391
1392 if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1393 al = SSL_AD_DECODE_ERROR;
1394 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1395 goto f_err;
1396 }
1397
1398 /* Could be empty. */
1399 if (PACKET_remaining(pkt) == 0) {
1400 PACKET_null_init(&clienthello.extensions);
1401 } else {
1402 if (!PACKET_get_length_prefixed_2(pkt, &clienthello.extensions)) {
1403 al = SSL_AD_DECODE_ERROR;
1404 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1405 goto f_err;
1406 }
1407 }
1408 }
1409
1410 if (!PACKET_copy_all(&compression, clienthello.compressions,
1411 MAX_COMPRESSIONS_SIZE,
1412 &clienthello.compressions_len)) {
1413 al = SSL_AD_DECODE_ERROR;
1414 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1415 goto f_err;
1416 }
1417
1418 /* Preserve the raw extensions PACKET for later use */
1419 extensions = clienthello.extensions;
1420 if (!tls_collect_extensions(s, &extensions, EXT_CLIENT_HELLO,
1421 &clienthello.pre_proc_exts, &al,
1422 &clienthello.pre_proc_exts_len)) {
1423 /* SSLerr already been called */
1424 goto f_err;
1425 }
1426
1427 /* Finished parsing the ClientHello, now we can start processing it */
1428
1429 /* Set up the client_random */
1430 memcpy(s->s3->client_random, clienthello.random, SSL3_RANDOM_SIZE);
1431
1432 /* Choose the version */
1433
1434 if (clienthello.isv2) {
1435 if (clienthello.legacy_version == SSL2_VERSION
1436 || (clienthello.legacy_version & 0xff00)
1437 != (SSL3_VERSION_MAJOR << 8)) {
1438 /*
1439 * This is real SSLv2 or something complete unknown. We don't
1440 * support it.
1441 */
1442 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1443 goto err;
1444 }
1445 /* SSLv3/TLS */
1446 s->client_version = clienthello.legacy_version;
1447 }
1448 /*
1449 * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1450 * versions are potentially compatible. Version negotiation comes later.
1451 */
1452 if (!SSL_IS_DTLS(s)) {
1453 protverr = ssl_choose_server_version(s, &clienthello);
1454 } else if (s->method->version != DTLS_ANY_VERSION &&
1455 DTLS_VERSION_LT((int)clienthello.legacy_version, s->version)) {
1456 protverr = SSL_R_VERSION_TOO_LOW;
1457 } else {
1458 protverr = 0;
1459 }
1460
1461 if (protverr) {
1462 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);
1463 if (SSL_IS_FIRST_HANDSHAKE(s)) {
1464 /* like ssl3_get_record, send alert using remote version number */
1465 s->version = s->client_version = clienthello.legacy_version;
1466 }
1467 al = SSL_AD_PROTOCOL_VERSION;
1468 goto f_err;
1469 }
1470
1471 if (SSL_IS_DTLS(s)) {
1472 /* Empty cookie was already handled above by returning early. */
1473 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1474 if (s->ctx->app_verify_cookie_cb != NULL) {
1475 if (s->ctx->app_verify_cookie_cb(s, clienthello.dtls_cookie,
1476 clienthello.dtls_cookie_len) == 0) {
1477 al = SSL_AD_HANDSHAKE_FAILURE;
1478 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1479 SSL_R_COOKIE_MISMATCH);
1480 goto f_err;
1481 /* else cookie verification succeeded */
1482 }
1483 /* default verification */
1484 } else if (s->d1->cookie_len != clienthello.dtls_cookie_len
1485 || memcmp(clienthello.dtls_cookie, s->d1->cookie,
1486 s->d1->cookie_len) != 0) {
1487 al = SSL_AD_HANDSHAKE_FAILURE;
1488 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1489 goto f_err;
1490 }
1491 s->d1->cookie_verified = 1;
1492 }
1493 if (s->method->version == DTLS_ANY_VERSION) {
1494 protverr = ssl_choose_server_version(s, &clienthello);
1495 if (protverr != 0) {
1496 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);
1497 s->version = s->client_version;
1498 al = SSL_AD_PROTOCOL_VERSION;
1499 goto f_err;
1500 }
1501 }
1502 }
1503
1504 s->hit = 0;
1505
1506 /* We need to do this before getting the session */
1507 if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
1508 EXT_CLIENT_HELLO,
1509 clienthello.pre_proc_exts, NULL, 0, &al)) {
1510 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1511 goto f_err;
1512 }
1513
1514 /*
1515 * We don't allow resumption in a backwards compatible ClientHello.
1516 * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1517 *
1518 * Versions before 0.9.7 always allow clients to resume sessions in
1519 * renegotiation. 0.9.7 and later allow this by default, but optionally
1520 * ignore resumption requests with flag
1521 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1522 * than a change to default behavior so that applications relying on
1523 * this for security won't even compile against older library versions).
1524 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1525 * request renegotiation but not a new session (s->new_session remains
1526 * unset): for servers, this essentially just means that the
1527 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1528 * ignored.
1529 */
1530 if (clienthello.isv2 ||
1531 (s->new_session &&
1532 (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1533 if (!ssl_get_new_session(s, 1))
1534 goto err;
1535 } else {
1536 i = ssl_get_prev_session(s, &clienthello, &al);
1537 if (i == 1) {
1538 /* previous session */
1539 s->hit = 1;
1540 } else if (i == -1) {
1541 goto f_err;
1542 } else {
1543 /* i == 0 */
1544 if (!ssl_get_new_session(s, 1))
1545 goto err;
1546 }
1547 }
1548
1549 if (bytes_to_cipher_list(s, &clienthello.ciphersuites, &ciphers,
1550 clienthello.isv2, &al) == NULL) {
1551 goto f_err;
1552 }
1553
1554 /* If it is a hit, check that the cipher is in the list */
1555 if (s->hit) {
1556 j = 0;
1557 id = s->session->cipher->id;
1558
1559 #ifdef CIPHER_DEBUG
1560 fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
1561 #endif
1562 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1563 c = sk_SSL_CIPHER_value(ciphers, i);
1564 #ifdef CIPHER_DEBUG
1565 fprintf(stderr, "client [%2d of %2d]:%s\n",
1566 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1567 #endif
1568 if (c->id == id) {
1569 j = 1;
1570 break;
1571 }
1572 }
1573 if (j == 0) {
1574 /*
1575 * we need to have the cipher in the cipher list if we are asked
1576 * to reuse it
1577 */
1578 al = SSL_AD_ILLEGAL_PARAMETER;
1579 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1580 SSL_R_REQUIRED_CIPHER_MISSING);
1581 goto f_err;
1582 }
1583 }
1584
1585 for (loop = 0; loop < clienthello.compressions_len; loop++) {
1586 if (clienthello.compressions[loop] == 0)
1587 break;
1588 }
1589
1590 if (loop >= clienthello.compressions_len) {
1591 /* no compress */
1592 al = SSL_AD_DECODE_ERROR;
1593 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1594 goto f_err;
1595 }
1596
1597 #ifndef OPENSSL_NO_EC
1598 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1599 ssl_check_for_safari(s, &clienthello);
1600 #endif /* !OPENSSL_NO_EC */
1601
1602 /* TLS extensions */
1603 if (!tls_parse_all_extensions(s, EXT_CLIENT_HELLO,
1604 clienthello.pre_proc_exts, NULL, 0, &al)) {
1605 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1606 goto f_err;
1607 }
1608
1609 /*
1610 * Check if we want to use external pre-shared secret for this handshake
1611 * for not reused session only. We need to generate server_random before
1612 * calling tls_session_secret_cb in order to allow SessionTicket
1613 * processing to use it in key derivation.
1614 */
1615 {
1616 unsigned char *pos;
1617 pos = s->s3->server_random;
1618 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1619 goto f_err;
1620 }
1621 }
1622
1623 if (!s->hit && s->version >= TLS1_VERSION && s->ext.session_secret_cb) {
1624 const SSL_CIPHER *pref_cipher = NULL;
1625 /*
1626 * s->session->master_key_length is a size_t, but this is an int for
1627 * backwards compat reasons
1628 */
1629 int master_key_length;
1630
1631 master_key_length = sizeof(s->session->master_key);
1632 if (s->ext.session_secret_cb(s, s->session->master_key,
1633 &master_key_length, ciphers,
1634 &pref_cipher,
1635 s->ext.session_secret_cb_arg)
1636 && master_key_length > 0) {
1637 s->session->master_key_length = master_key_length;
1638 s->hit = 1;
1639 s->session->ciphers = ciphers;
1640 s->session->verify_result = X509_V_OK;
1641
1642 ciphers = NULL;
1643
1644 /* check if some cipher was preferred by call back */
1645 if (pref_cipher == NULL)
1646 pref_cipher = ssl3_choose_cipher(s, s->session->ciphers,
1647 SSL_get_ciphers(s));
1648 if (pref_cipher == NULL) {
1649 al = SSL_AD_HANDSHAKE_FAILURE;
1650 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1651 goto f_err;
1652 }
1653
1654 s->session->cipher = pref_cipher;
1655 sk_SSL_CIPHER_free(s->cipher_list);
1656 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1657 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1658 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1659 }
1660 }
1661
1662 /*
1663 * Worst case, we will use the NULL compression, but if we have other
1664 * options, we will now look for them. We have complen-1 compression
1665 * algorithms from the client, starting at q.
1666 */
1667 s->s3->tmp.new_compression = NULL;
1668 #ifndef OPENSSL_NO_COMP
1669 /* This only happens if we have a cache hit */
1670 if (s->session->compress_meth != 0) {
1671 int m, comp_id = s->session->compress_meth;
1672 unsigned int k;
1673 /* Perform sanity checks on resumed compression algorithm */
1674 /* Can't disable compression */
1675 if (!ssl_allow_compression(s)) {
1676 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1677 SSL_R_INCONSISTENT_COMPRESSION);
1678 goto f_err;
1679 }
1680 /* Look for resumed compression method */
1681 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1682 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1683 if (comp_id == comp->id) {
1684 s->s3->tmp.new_compression = comp;
1685 break;
1686 }
1687 }
1688 if (s->s3->tmp.new_compression == NULL) {
1689 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1690 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1691 goto f_err;
1692 }
1693 /* Look for resumed method in compression list */
1694 for (k = 0; k < clienthello.compressions_len; k++) {
1695 if (clienthello.compressions[k] == comp_id)
1696 break;
1697 }
1698 if (k >= clienthello.compressions_len) {
1699 al = SSL_AD_ILLEGAL_PARAMETER;
1700 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
1701 SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1702 goto f_err;
1703 }
1704 } else if (s->hit)
1705 comp = NULL;
1706 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1707 /* See if we have a match */
1708 int m, nn, v, done = 0;
1709 unsigned int o;
1710
1711 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1712 for (m = 0; m < nn; m++) {
1713 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1714 v = comp->id;
1715 for (o = 0; o < clienthello.compressions_len; o++) {
1716 if (v == clienthello.compressions[o]) {
1717 done = 1;
1718 break;
1719 }
1720 }
1721 if (done)
1722 break;
1723 }
1724 if (done)
1725 s->s3->tmp.new_compression = comp;
1726 else
1727 comp = NULL;
1728 }
1729 #else
1730 /*
1731 * If compression is disabled we'd better not try to resume a session
1732 * using compression.
1733 */
1734 if (s->session->compress_meth != 0) {
1735 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1736 goto f_err;
1737 }
1738 #endif
1739
1740 /*
1741 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1742 */
1743
1744 if (!s->hit) {
1745 #ifdef OPENSSL_NO_COMP
1746 s->session->compress_meth = 0;
1747 #else
1748 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1749 #endif
1750 sk_SSL_CIPHER_free(s->session->ciphers);
1751 s->session->ciphers = ciphers;
1752 if (ciphers == NULL) {
1753 al = SSL_AD_INTERNAL_ERROR;
1754 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1755 goto f_err;
1756 }
1757 ciphers = NULL;
1758 if (!tls1_set_server_sigalgs(s)) {
1759 SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1760 goto err;
1761 }
1762 }
1763
1764 sk_SSL_CIPHER_free(ciphers);
1765 OPENSSL_free(clienthello.pre_proc_exts);
1766 return MSG_PROCESS_CONTINUE_PROCESSING;
1767 f_err:
1768 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1769 err:
1770 ossl_statem_set_error(s);
1771
1772 sk_SSL_CIPHER_free(ciphers);
1773 OPENSSL_free(clienthello.pre_proc_exts);
1774
1775 return MSG_PROCESS_ERROR;
1776 }
1777
1778 /*
1779 * Call the status request callback if needed. Upon success, returns 1.
1780 * Upon failure, returns 0 and sets |*al| to the appropriate fatal alert.
1781 */
1782 static int tls_handle_status_request(SSL *s, int *al)
1783 {
1784 s->ext.status_expected = 0;
1785
1786 /*
1787 * If status request then ask callback what to do. Note: this must be
1788 * called after servername callbacks in case the certificate has changed,
1789 * and must be called after the cipher has been chosen because this may
1790 * influence which certificate is sent
1791 */
1792 if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
1793 && s->ctx->ext.status_cb != NULL) {
1794 int ret;
1795
1796 /* If no certificate can't return certificate status */
1797 if (s->s3->tmp.cert != NULL) {
1798 /*
1799 * Set current certificate to one we will use so SSL_get_certificate
1800 * et al can pick it up.
1801 */
1802 s->cert->key = s->s3->tmp.cert;
1803 ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
1804 switch (ret) {
1805 /* We don't want to send a status request response */
1806 case SSL_TLSEXT_ERR_NOACK:
1807 s->ext.status_expected = 0;
1808 break;
1809 /* status request response should be sent */
1810 case SSL_TLSEXT_ERR_OK:
1811 if (s->ext.ocsp.resp)
1812 s->ext.status_expected = 1;
1813 break;
1814 /* something bad happened */
1815 case SSL_TLSEXT_ERR_ALERT_FATAL:
1816 default:
1817 *al = SSL_AD_INTERNAL_ERROR;
1818 return 0;
1819 }
1820 }
1821 }
1822
1823 return 1;
1824 }
1825
1826 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
1827 {
1828 int al = SSL_AD_HANDSHAKE_FAILURE;
1829 const SSL_CIPHER *cipher;
1830
1831 if (wst == WORK_MORE_A) {
1832 if (!s->hit) {
1833 /* Let cert callback update server certificates if required */
1834 if (s->cert->cert_cb) {
1835 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1836 if (rv == 0) {
1837 al = SSL_AD_INTERNAL_ERROR;
1838 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1839 SSL_R_CERT_CB_ERROR);
1840 goto f_err;
1841 }
1842 if (rv < 0) {
1843 s->rwstate = SSL_X509_LOOKUP;
1844 return WORK_MORE_A;
1845 }
1846 s->rwstate = SSL_NOTHING;
1847 }
1848 cipher =
1849 ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1850
1851 if (cipher == NULL) {
1852 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1853 SSL_R_NO_SHARED_CIPHER);
1854 goto f_err;
1855 }
1856 s->s3->tmp.new_cipher = cipher;
1857 if (!tls_choose_sigalg(s, &al))
1858 goto f_err;
1859 /* check whether we should disable session resumption */
1860 if (s->not_resumable_session_cb != NULL)
1861 s->session->not_resumable =
1862 s->not_resumable_session_cb(s, ((cipher->algorithm_mkey
1863 & (SSL_kDHE | SSL_kECDHE))
1864 != 0));
1865 if (s->session->not_resumable)
1866 /* do not send a session ticket */
1867 s->ext.ticket_expected = 0;
1868 } else {
1869 /* Session-id reuse */
1870 s->s3->tmp.new_cipher = s->session->cipher;
1871 }
1872
1873 /*-
1874 * we now have the following setup.
1875 * client_random
1876 * cipher_list - our preferred list of ciphers
1877 * ciphers - the clients preferred list of ciphers
1878 * compression - basically ignored right now
1879 * ssl version is set - sslv3
1880 * s->session - The ssl session has been setup.
1881 * s->hit - session reuse flag
1882 * s->s3->tmp.new_cipher- the new cipher to use.
1883 */
1884
1885 /*
1886 * Call status_request callback if needed. Has to be done after the
1887 * certificate callbacks etc above.
1888 */
1889 if (!tls_handle_status_request(s, &al)) {
1890 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1891 SSL_R_CLIENTHELLO_TLSEXT);
1892 goto f_err;
1893 }
1894
1895 wst = WORK_MORE_B;
1896 }
1897 #ifndef OPENSSL_NO_SRP
1898 if (wst == WORK_MORE_B) {
1899 int ret;
1900 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
1901 /*
1902 * callback indicates further work to be done
1903 */
1904 s->rwstate = SSL_X509_LOOKUP;
1905 return WORK_MORE_B;
1906 }
1907 if (ret != SSL_ERROR_NONE) {
1908 /*
1909 * This is not really an error but the only means to for
1910 * a client to detect whether srp is supported.
1911 */
1912 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
1913 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1914 SSL_R_CLIENTHELLO_TLSEXT);
1915 else
1916 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
1917 SSL_R_PSK_IDENTITY_NOT_FOUND);
1918 goto f_err;
1919 }
1920 }
1921 #endif
1922
1923 return WORK_FINISHED_STOP;
1924 f_err:
1925 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1926 ossl_statem_set_error(s);
1927 return WORK_ERROR;
1928 }
1929
1930 int tls_construct_server_hello(SSL *s, WPACKET *pkt)
1931 {
1932 int compm, al = SSL_AD_INTERNAL_ERROR;
1933 size_t sl, len;
1934 int version;
1935
1936 /* TODO(TLS1.3): Remove the DRAFT conditional before release */
1937 version = SSL_IS_TLS13(s) ? TLS1_3_VERSION_DRAFT : s->version;
1938 if (!WPACKET_put_bytes_u16(pkt, version)
1939 /*
1940 * Random stuff. Filling of the server_random takes place in
1941 * tls_process_client_hello()
1942 */
1943 || !WPACKET_memcpy(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1944 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1945 goto err;
1946 }
1947
1948 /*-
1949 * There are several cases for the session ID to send
1950 * back in the server hello:
1951 * - For session reuse from the session cache,
1952 * we send back the old session ID.
1953 * - If stateless session reuse (using a session ticket)
1954 * is successful, we send back the client's "session ID"
1955 * (which doesn't actually identify the session).
1956 * - If it is a new session, we send back the new
1957 * session ID.
1958 * - However, if we want the new session to be single-use,
1959 * we send back a 0-length session ID.
1960 * s->hit is non-zero in either case of session reuse,
1961 * so the following won't overwrite an ID that we're supposed
1962 * to send back.
1963 */
1964 if (s->session->not_resumable ||
1965 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1966 && !s->hit))
1967 s->session->session_id_length = 0;
1968
1969 sl = s->session->session_id_length;
1970 if (sl > sizeof(s->session->session_id)) {
1971 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1972 goto err;
1973 }
1974
1975 /* set up the compression method */
1976 #ifdef OPENSSL_NO_COMP
1977 compm = 0;
1978 #else
1979 if (s->s3->tmp.new_compression == NULL)
1980 compm = 0;
1981 else
1982 compm = s->s3->tmp.new_compression->id;
1983 #endif
1984
1985 if ((!SSL_IS_TLS13(s)
1986 && !WPACKET_sub_memcpy_u8(pkt, s->session->session_id, sl))
1987 || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
1988 || (!SSL_IS_TLS13(s)
1989 && !WPACKET_put_bytes_u8(pkt, compm))
1990 || !tls_construct_extensions(s, pkt,
1991 SSL_IS_TLS13(s)
1992 ? EXT_TLS1_3_SERVER_HELLO
1993 : EXT_TLS1_2_SERVER_HELLO,
1994 NULL, 0, &al)) {
1995 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1996 goto err;
1997 }
1998
1999 if (!(s->verify_mode & SSL_VERIFY_PEER)
2000 && !ssl3_digest_cached_records(s, 0)) {
2001 al = SSL_AD_INTERNAL_ERROR;
2002 goto err;
2003 }
2004
2005 return 1;
2006 err:
2007 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2008 return 0;
2009 }
2010
2011 int tls_construct_server_done(SSL *s, WPACKET *pkt)
2012 {
2013 if (!s->s3->tmp.cert_request) {
2014 if (!ssl3_digest_cached_records(s, 0)) {
2015 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2016 return 0;
2017 }
2018 }
2019 return 1;
2020 }
2021
2022 int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
2023 {
2024 #ifndef OPENSSL_NO_DH
2025 EVP_PKEY *pkdh = NULL;
2026 #endif
2027 #ifndef OPENSSL_NO_EC
2028 unsigned char *encodedPoint = NULL;
2029 size_t encodedlen = 0;
2030 int curve_id = 0;
2031 #endif
2032 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
2033 int al = SSL_AD_INTERNAL_ERROR, i;
2034 unsigned long type;
2035 const BIGNUM *r[4];
2036 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
2037 EVP_PKEY_CTX *pctx = NULL;
2038 size_t paramlen, paramoffset;
2039
2040 if (!WPACKET_get_total_written(pkt, &paramoffset)) {
2041 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2042 goto f_err;
2043 }
2044
2045 if (md_ctx == NULL) {
2046 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2047 goto f_err;
2048 }
2049
2050 type = s->s3->tmp.new_cipher->algorithm_mkey;
2051
2052 r[0] = r[1] = r[2] = r[3] = NULL;
2053 #ifndef OPENSSL_NO_PSK
2054 /* Plain PSK or RSAPSK nothing to do */
2055 if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2056 } else
2057 #endif /* !OPENSSL_NO_PSK */
2058 #ifndef OPENSSL_NO_DH
2059 if (type & (SSL_kDHE | SSL_kDHEPSK)) {
2060 CERT *cert = s->cert;
2061
2062 EVP_PKEY *pkdhp = NULL;
2063 DH *dh;
2064
2065 if (s->cert->dh_tmp_auto) {
2066 DH *dhp = ssl_get_auto_dh(s);
2067 pkdh = EVP_PKEY_new();
2068 if (pkdh == NULL || dhp == NULL) {
2069 DH_free(dhp);
2070 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2071 ERR_R_INTERNAL_ERROR);
2072 goto f_err;
2073 }
2074 EVP_PKEY_assign_DH(pkdh, dhp);
2075 pkdhp = pkdh;
2076 } else {
2077 pkdhp = cert->dh_tmp;
2078 }
2079 if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2080 DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
2081 pkdh = ssl_dh_to_pkey(dhp);
2082 if (pkdh == NULL) {
2083 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2084 ERR_R_INTERNAL_ERROR);
2085 goto f_err;
2086 }
2087 pkdhp = pkdh;
2088 }
2089 if (pkdhp == NULL) {
2090 al = SSL_AD_HANDSHAKE_FAILURE;
2091 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2092 SSL_R_MISSING_TMP_DH_KEY);
2093 goto f_err;
2094 }
2095 if (!ssl_security(s, SSL_SECOP_TMP_DH,
2096 EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
2097 al = SSL_AD_HANDSHAKE_FAILURE;
2098 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2099 SSL_R_DH_KEY_TOO_SMALL);
2100 goto f_err;
2101 }
2102 if (s->s3->tmp.pkey != NULL) {
2103 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2104 ERR_R_INTERNAL_ERROR);
2105 goto err;
2106 }
2107
2108 s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
2109
2110 if (s->s3->tmp.pkey == NULL) {
2111 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
2112 goto err;
2113 }
2114
2115 dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
2116
2117 EVP_PKEY_free(pkdh);
2118 pkdh = NULL;
2119
2120 DH_get0_pqg(dh, &r[0], NULL, &r[1]);
2121 DH_get0_key(dh, &r[2], NULL);
2122 } else
2123 #endif
2124 #ifndef OPENSSL_NO_EC
2125 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2126 int nid;
2127
2128 if (s->s3->tmp.pkey != NULL) {
2129 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2130 ERR_R_INTERNAL_ERROR);
2131 goto err;
2132 }
2133
2134 /* Get NID of appropriate shared curve */
2135 nid = tls1_shared_group(s, -2);
2136 curve_id = tls1_ec_nid2curve_id(nid);
2137 if (curve_id == 0) {
2138 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2139 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2140 goto err;
2141 }
2142 s->s3->tmp.pkey = ssl_generate_pkey_curve(curve_id);
2143 /* Generate a new key for this curve */
2144 if (s->s3->tmp.pkey == NULL) {
2145 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
2146 goto f_err;
2147 }
2148
2149 /* Encode the public key. */
2150 encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
2151 &encodedPoint);
2152 if (encodedlen == 0) {
2153 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
2154 goto err;
2155 }
2156
2157 /*
2158 * We'll generate the serverKeyExchange message explicitly so we
2159 * can set these to NULLs
2160 */
2161 r[0] = NULL;
2162 r[1] = NULL;
2163 r[2] = NULL;
2164 r[3] = NULL;
2165 } else
2166 #endif /* !OPENSSL_NO_EC */
2167 #ifndef OPENSSL_NO_SRP
2168 if (type & SSL_kSRP) {
2169 if ((s->srp_ctx.N == NULL) ||
2170 (s->srp_ctx.g == NULL) ||
2171 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2172 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2173 SSL_R_MISSING_SRP_PARAM);
2174 goto err;
2175 }
2176 r[0] = s->srp_ctx.N;
2177 r[1] = s->srp_ctx.g;
2178 r[2] = s->srp_ctx.s;
2179 r[3] = s->srp_ctx.B;
2180 } else
2181 #endif
2182 {
2183 al = SSL_AD_HANDSHAKE_FAILURE;
2184 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2185 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2186 goto f_err;
2187 }
2188
2189 if (((s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2190 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2191 lu = NULL;
2192 } else if (lu == NULL) {
2193 al = SSL_AD_DECODE_ERROR;
2194 goto f_err;
2195 }
2196
2197 #ifndef OPENSSL_NO_PSK
2198 if (type & SSL_PSK) {
2199 size_t len = (s->cert->psk_identity_hint == NULL)
2200 ? 0 : strlen(s->cert->psk_identity_hint);
2201
2202 /*
2203 * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2204 * checked this when we set the identity hint - but just in case
2205 */
2206 if (len > PSK_MAX_IDENTITY_LEN
2207 || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
2208 len)) {
2209 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2210 ERR_R_INTERNAL_ERROR);
2211 goto f_err;
2212 }
2213 }
2214 #endif
2215
2216 for (i = 0; i < 4 && r[i] != NULL; i++) {
2217 unsigned char *binval;
2218 int res;
2219
2220 #ifndef OPENSSL_NO_SRP
2221 if ((i == 2) && (type & SSL_kSRP)) {
2222 res = WPACKET_start_sub_packet_u8(pkt);
2223 } else
2224 #endif
2225 res = WPACKET_start_sub_packet_u16(pkt);
2226
2227 if (!res) {
2228 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2229 ERR_R_INTERNAL_ERROR);
2230 goto f_err;
2231 }
2232
2233 #ifndef OPENSSL_NO_DH
2234 /*-
2235 * for interoperability with some versions of the Microsoft TLS
2236 * stack, we need to zero pad the DHE pub key to the same length
2237 * as the prime
2238 */
2239 if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2240 size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2241
2242 if (len > 0) {
2243 if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2244 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2245 ERR_R_INTERNAL_ERROR);
2246 goto f_err;
2247 }
2248 memset(binval, 0, len);
2249 }
2250 }
2251 #endif
2252 if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2253 || !WPACKET_close(pkt)) {
2254 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2255 ERR_R_INTERNAL_ERROR);
2256 goto f_err;
2257 }
2258
2259 BN_bn2bin(r[i], binval);
2260 }
2261
2262 #ifndef OPENSSL_NO_EC
2263 if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2264 /*
2265 * We only support named (not generic) curves. In this situation, the
2266 * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2267 * [1 byte length of encoded point], followed by the actual encoded
2268 * point itself
2269 */
2270 if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2271 || !WPACKET_put_bytes_u8(pkt, 0)
2272 || !WPACKET_put_bytes_u8(pkt, curve_id)
2273 || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2274 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2275 ERR_R_INTERNAL_ERROR);
2276 goto f_err;
2277 }
2278 OPENSSL_free(encodedPoint);
2279 encodedPoint = NULL;
2280 }
2281 #endif
2282
2283 /* not anonymous */
2284 if (lu != NULL) {
2285 EVP_PKEY *pkey = s->s3->tmp.cert->privatekey;
2286 const EVP_MD *md = ssl_md(lu->hash_idx);
2287 unsigned char *sigbytes1, *sigbytes2;
2288 size_t siglen;
2289
2290 if (pkey == NULL || md == NULL) {
2291 /* Should never happen */
2292 al = SSL_AD_INTERNAL_ERROR;
2293 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2294 ERR_R_INTERNAL_ERROR);
2295 goto f_err;
2296 }
2297 /*
2298 * n is the length of the params, they start at &(d[4]) and p
2299 * points to the space at the end.
2300 */
2301
2302 /* Get length of the parameters we have written above */
2303 if (!WPACKET_get_length(pkt, &paramlen)) {
2304 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2305 ERR_R_INTERNAL_ERROR);
2306 goto f_err;
2307 }
2308 /* send signature algorithm */
2309 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg))
2310 return 0;
2311 /*
2312 * Create the signature. We don't know the actual length of the sig
2313 * until after we've created it, so we reserve enough bytes for it
2314 * up front, and then properly allocate them in the WPACKET
2315 * afterwards.
2316 */
2317 siglen = EVP_PKEY_size(pkey);
2318 if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2319 || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
2320 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2321 ERR_R_INTERNAL_ERROR);
2322 goto f_err;
2323 }
2324 if (lu->sig == EVP_PKEY_RSA_PSS) {
2325 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2326 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
2327 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2328 ERR_R_EVP_LIB);
2329 goto f_err;
2330 }
2331 }
2332 if (EVP_DigestSignUpdate(md_ctx, &(s->s3->client_random[0]),
2333 SSL3_RANDOM_SIZE) <= 0
2334 || EVP_DigestSignUpdate(md_ctx, &(s->s3->server_random[0]),
2335 SSL3_RANDOM_SIZE) <= 0
2336 || EVP_DigestSignUpdate(md_ctx,
2337 s->init_buf->data + paramoffset,
2338 paramlen) <= 0
2339 || EVP_DigestSignFinal(md_ctx, sigbytes1, &siglen) <= 0
2340 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2341 || sigbytes1 != sigbytes2) {
2342 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2343 ERR_R_INTERNAL_ERROR);
2344 goto f_err;
2345 }
2346 }
2347
2348 EVP_MD_CTX_free(md_ctx);
2349 return 1;
2350 f_err:
2351 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2352 err:
2353 #ifndef OPENSSL_NO_DH
2354 EVP_PKEY_free(pkdh);
2355 #endif
2356 #ifndef OPENSSL_NO_EC
2357 OPENSSL_free(encodedPoint);
2358 #endif
2359 EVP_MD_CTX_free(md_ctx);
2360 return 0;
2361 }
2362
2363 int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
2364 {
2365 int i;
2366 STACK_OF(X509_NAME) *sk = NULL;
2367
2368 /* get the list of acceptable cert types */
2369 if (!WPACKET_start_sub_packet_u8(pkt)
2370 || !ssl3_get_req_cert_type(s, pkt)
2371 || !WPACKET_close(pkt)) {
2372 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2373 goto err;
2374 }
2375
2376 if (SSL_USE_SIGALGS(s)) {
2377 const uint16_t *psigs;
2378 size_t nl = tls12_get_psigalgs(s, 1, &psigs);
2379
2380 if (!WPACKET_start_sub_packet_u16(pkt)
2381 || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2382 || !WPACKET_close(pkt)) {
2383 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2384 ERR_R_INTERNAL_ERROR);
2385 goto err;
2386 }
2387 }
2388
2389 /* Start sub-packet for client CA list */
2390 if (!WPACKET_start_sub_packet_u16(pkt)) {
2391 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2392 goto err;
2393 }
2394
2395 sk = SSL_get_client_CA_list(s);
2396 if (sk != NULL) {
2397 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2398 unsigned char *namebytes;
2399 X509_NAME *name = sk_X509_NAME_value(sk, i);
2400 int namelen;
2401
2402 if (name == NULL
2403 || (namelen = i2d_X509_NAME(name, NULL)) < 0
2404 || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2405 &namebytes)
2406 || i2d_X509_NAME(name, &namebytes) != namelen) {
2407 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2408 ERR_R_INTERNAL_ERROR);
2409 goto err;
2410 }
2411 }
2412 }
2413 /* else no CA names */
2414
2415 if (!WPACKET_close(pkt)) {
2416 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2417 goto err;
2418 }
2419
2420 s->s3->tmp.cert_request = 1;
2421
2422 return 1;
2423 err:
2424 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2425 return 0;
2426 }
2427
2428 static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt, int *al)
2429 {
2430 #ifndef OPENSSL_NO_PSK
2431 unsigned char psk[PSK_MAX_PSK_LEN];
2432 size_t psklen;
2433 PACKET psk_identity;
2434
2435 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2436 *al = SSL_AD_DECODE_ERROR;
2437 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
2438 return 0;
2439 }
2440 if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2441 *al = SSL_AD_DECODE_ERROR;
2442 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
2443 return 0;
2444 }
2445 if (s->psk_server_callback == NULL) {
2446 *al = SSL_AD_INTERNAL_ERROR;
2447 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_SERVER_CB);
2448 return 0;
2449 }
2450
2451 if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2452 *al = SSL_AD_INTERNAL_ERROR;
2453 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2454 return 0;
2455 }
2456
2457 psklen = s->psk_server_callback(s, s->session->psk_identity,
2458 psk, sizeof(psk));
2459
2460 if (psklen > PSK_MAX_PSK_LEN) {
2461 *al = SSL_AD_INTERNAL_ERROR;
2462 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2463 return 0;
2464 } else if (psklen == 0) {
2465 /*
2466 * PSK related to the given identity not found
2467 */
2468 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2469 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2470 SSL_R_PSK_IDENTITY_NOT_FOUND);
2471 return 0;
2472 }
2473
2474 OPENSSL_free(s->s3->tmp.psk);
2475 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2476 OPENSSL_cleanse(psk, psklen);
2477
2478 if (s->s3->tmp.psk == NULL) {
2479 *al = SSL_AD_INTERNAL_ERROR;
2480 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2481 return 0;
2482 }
2483
2484 s->s3->tmp.psklen = psklen;
2485
2486 return 1;
2487 #else
2488 /* Should never happen */
2489 *al = SSL_AD_INTERNAL_ERROR;
2490 SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2491 return 0;
2492 #endif
2493 }
2494
2495 static int tls_process_cke_rsa(SSL *s, PACKET *pkt, int *al)
2496 {
2497 #ifndef OPENSSL_NO_RSA
2498 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2499 int decrypt_len;
2500 unsigned char decrypt_good, version_good;
2501 size_t j, padding_len;
2502 PACKET enc_premaster;
2503 RSA *rsa = NULL;
2504 unsigned char *rsa_decrypt = NULL;
2505 int ret = 0;
2506
2507 rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA].privatekey);
2508 if (rsa == NULL) {
2509 *al = SSL_AD_HANDSHAKE_FAILURE;
2510 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_MISSING_RSA_CERTIFICATE);
2511 return 0;
2512 }
2513
2514 /* SSLv3 and pre-standard DTLS omit the length bytes. */
2515 if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2516 enc_premaster = *pkt;
2517 } else {
2518 if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2519 || PACKET_remaining(pkt) != 0) {
2520 *al = SSL_AD_DECODE_ERROR;
2521 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_LENGTH_MISMATCH);
2522 return 0;
2523 }
2524 }
2525
2526 /*
2527 * We want to be sure that the plaintext buffer size makes it safe to
2528 * iterate over the entire size of a premaster secret
2529 * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2530 * their ciphertext cannot accommodate a premaster secret anyway.
2531 */
2532 if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2533 *al = SSL_AD_INTERNAL_ERROR;
2534 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
2535 return 0;
2536 }
2537
2538 rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2539 if (rsa_decrypt == NULL) {
2540 *al = SSL_AD_INTERNAL_ERROR;
2541 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_MALLOC_FAILURE);
2542 return 0;
2543 }
2544
2545 /*
2546 * We must not leak whether a decryption failure occurs because of
2547 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2548 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2549 * generates a random premaster secret for the case that the decrypt
2550 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2551 */
2552
2553 if (RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret)) <= 0)
2554 goto err;
2555
2556 /*
2557 * Decrypt with no padding. PKCS#1 padding will be removed as part of
2558 * the timing-sensitive code below.
2559 */
2560 /* TODO(size_t): Convert this function */
2561 decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
2562 PACKET_data(&enc_premaster),
2563 rsa_decrypt, rsa, RSA_NO_PADDING);
2564 if (decrypt_len < 0)
2565 goto err;
2566
2567 /* Check the padding. See RFC 3447, section 7.2.2. */
2568
2569 /*
2570 * The smallest padded premaster is 11 bytes of overhead. Small keys
2571 * are publicly invalid, so this may return immediately. This ensures
2572 * PS is at least 8 bytes.
2573 */
2574 if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
2575 *al = SSL_AD_DECRYPT_ERROR;
2576 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_DECRYPTION_FAILED);
2577 goto err;
2578 }
2579
2580 padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
2581 decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
2582 constant_time_eq_int_8(rsa_decrypt[1], 2);
2583 for (j = 2; j < padding_len - 1; j++) {
2584 decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
2585 }
2586 decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
2587
2588 /*
2589 * If the version in the decrypted pre-master secret is correct then
2590 * version_good will be 0xff, otherwise it'll be zero. The
2591 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2592 * (http://eprint.iacr.org/2003/052/) exploits the version number
2593 * check as a "bad version oracle". Thus version checks are done in
2594 * constant time and are treated like any other decryption error.
2595 */
2596 version_good =
2597 constant_time_eq_8(rsa_decrypt[padding_len],
2598 (unsigned)(s->client_version >> 8));
2599 version_good &=
2600 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2601 (unsigned)(s->client_version & 0xff));
2602
2603 /*
2604 * The premaster secret must contain the same version number as the
2605 * ClientHello to detect version rollback attacks (strangely, the
2606 * protocol does not offer such protection for DH ciphersuites).
2607 * However, buggy clients exist that send the negotiated protocol
2608 * version instead if the server does not support the requested
2609 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2610 * clients.
2611 */
2612 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2613 unsigned char workaround_good;
2614 workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
2615 (unsigned)(s->version >> 8));
2616 workaround_good &=
2617 constant_time_eq_8(rsa_decrypt[padding_len + 1],
2618 (unsigned)(s->version & 0xff));
2619 version_good |= workaround_good;
2620 }
2621
2622 /*
2623 * Both decryption and version must be good for decrypt_good to
2624 * remain non-zero (0xff).
2625 */
2626 decrypt_good &= version_good;
2627
2628 /*
2629 * Now copy rand_premaster_secret over from p using
2630 * decrypt_good_mask. If decryption failed, then p does not
2631 * contain valid plaintext, however, a check above guarantees
2632 * it is still sufficiently large to read from.
2633 */
2634 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2635 rsa_decrypt[padding_len + j] =
2636 constant_time_select_8(decrypt_good,
2637 rsa_decrypt[padding_len + j],
2638 rand_premaster_secret[j]);
2639 }
2640
2641 if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
2642 sizeof(rand_premaster_secret), 0)) {
2643 *al = SSL_AD_INTERNAL_ERROR;
2644 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
2645 goto err;
2646 }
2647
2648 ret = 1;
2649 err:
2650 OPENSSL_free(rsa_decrypt);
2651 return ret;
2652 #else
2653 /* Should never happen */
2654 *al = SSL_AD_INTERNAL_ERROR;
2655 SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
2656 return 0;
2657 #endif
2658 }
2659
2660 static int tls_process_cke_dhe(SSL *s, PACKET *pkt, int *al)
2661 {
2662 #ifndef OPENSSL_NO_DH
2663 EVP_PKEY *skey = NULL;
2664 DH *cdh;
2665 unsigned int i;
2666 BIGNUM *pub_key;
2667 const unsigned char *data;
2668 EVP_PKEY *ckey = NULL;
2669 int ret = 0;
2670
2671 if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
2672 *al = SSL_AD_HANDSHAKE_FAILURE;
2673 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE,
2674 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2675 goto err;
2676 }
2677 skey = s->s3->tmp.pkey;
2678 if (skey == NULL) {
2679 *al = SSL_AD_HANDSHAKE_FAILURE;
2680 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
2681 goto err;
2682 }
2683
2684 if (PACKET_remaining(pkt) == 0L) {
2685 *al = SSL_AD_HANDSHAKE_FAILURE;
2686 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
2687 goto err;
2688 }
2689 if (!PACKET_get_bytes(pkt, &data, i)) {
2690 /* We already checked we have enough data */
2691 *al = SSL_AD_INTERNAL_ERROR;
2692 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2693 goto err;
2694 }
2695 ckey = EVP_PKEY_new();
2696 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
2697 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_BN_LIB);
2698 goto err;
2699 }
2700 cdh = EVP_PKEY_get0_DH(ckey);
2701 pub_key = BN_bin2bn(data, i, NULL);
2702
2703 if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
2704 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2705 if (pub_key != NULL)
2706 BN_free(pub_key);
2707 goto err;
2708 }
2709
2710 if (ssl_derive(s, skey, ckey, 1) == 0) {
2711 *al = SSL_AD_INTERNAL_ERROR;
2712 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2713 goto err;
2714 }
2715
2716 ret = 1;
2717 EVP_PKEY_free(s->s3->tmp.pkey);
2718 s->s3->tmp.pkey = NULL;
2719 err:
2720 EVP_PKEY_free(ckey);
2721 return ret;
2722 #else
2723 /* Should never happen */
2724 *al = SSL_AD_INTERNAL_ERROR;
2725 SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
2726 return 0;
2727 #endif
2728 }
2729
2730 static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt, int *al)
2731 {
2732 #ifndef OPENSSL_NO_EC
2733 EVP_PKEY *skey = s->s3->tmp.pkey;
2734 EVP_PKEY *ckey = NULL;
2735 int ret = 0;
2736
2737 if (PACKET_remaining(pkt) == 0L) {
2738 /* We don't support ECDH client auth */
2739 *al = SSL_AD_HANDSHAKE_FAILURE;
2740 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_MISSING_TMP_ECDH_KEY);
2741 goto err;
2742 } else {
2743 unsigned int i;
2744 const unsigned char *data;
2745
2746 /*
2747 * Get client's public key from encoded point in the
2748 * ClientKeyExchange message.
2749 */
2750
2751 /* Get encoded point length */
2752 if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
2753 || PACKET_remaining(pkt) != 0) {
2754 *al = SSL_AD_DECODE_ERROR;
2755 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_LENGTH_MISMATCH);
2756 goto err;
2757 }
2758 ckey = EVP_PKEY_new();
2759 if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
2760 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EVP_LIB);
2761 goto err;
2762 }
2763 if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
2764 *al = SSL_AD_HANDSHAKE_FAILURE;
2765 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EC_LIB);
2766 goto err;
2767 }
2768 }
2769
2770 if (ssl_derive(s, skey, ckey, 1) == 0) {
2771 *al = SSL_AD_INTERNAL_ERROR;
2772 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2773 goto err;
2774 }
2775
2776 ret = 1;
2777 EVP_PKEY_free(s->s3->tmp.pkey);
2778 s->s3->tmp.pkey = NULL;
2779 err:
2780 EVP_PKEY_free(ckey);
2781
2782 return ret;
2783 #else
2784 /* Should never happen */
2785 *al = SSL_AD_INTERNAL_ERROR;
2786 SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2787 return 0;
2788 #endif
2789 }
2790
2791 static int tls_process_cke_srp(SSL *s, PACKET *pkt, int *al)
2792 {
2793 #ifndef OPENSSL_NO_SRP
2794 unsigned int i;
2795 const unsigned char *data;
2796
2797 if (!PACKET_get_net_2(pkt, &i)
2798 || !PACKET_get_bytes(pkt, &data, i)) {
2799 *al = SSL_AD_DECODE_ERROR;
2800 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_A_LENGTH);
2801 return 0;
2802 }
2803 if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
2804 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_BN_LIB);
2805 return 0;
2806 }
2807 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
2808 *al = SSL_AD_ILLEGAL_PARAMETER;
2809 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
2810 return 0;
2811 }
2812 OPENSSL_free(s->session->srp_username);
2813 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2814 if (s->session->srp_username == NULL) {
2815 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_MALLOC_FAILURE);
2816 return 0;
2817 }
2818
2819 if (!srp_generate_server_master_secret(s)) {
2820 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
2821 return 0;
2822 }
2823
2824 return 1;
2825 #else
2826 /* Should never happen */
2827 *al = SSL_AD_INTERNAL_ERROR;
2828 SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
2829 return 0;
2830 #endif
2831 }
2832
2833 static int tls_process_cke_gost(SSL *s, PACKET *pkt, int *al)
2834 {
2835 #ifndef OPENSSL_NO_GOST
2836 EVP_PKEY_CTX *pkey_ctx;
2837 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2838 unsigned char premaster_secret[32];
2839 const unsigned char *start;
2840 size_t outlen = 32, inlen;
2841 unsigned long alg_a;
2842 int Ttag, Tclass;
2843 long Tlen;
2844 size_t sess_key_len;
2845 const unsigned char *data;
2846 int ret = 0;
2847
2848 /* Get our certificate private key */
2849 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2850 if (alg_a & SSL_aGOST12) {
2851 /*
2852 * New GOST ciphersuites have SSL_aGOST01 bit too
2853 */
2854 pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
2855 if (pk == NULL) {
2856 pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
2857 }
2858 if (pk == NULL) {
2859 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2860 }
2861 } else if (alg_a & SSL_aGOST01) {
2862 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2863 }
2864
2865 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2866 if (pkey_ctx == NULL) {
2867 *al = SSL_AD_INTERNAL_ERROR;
2868 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_MALLOC_FAILURE);
2869 return 0;
2870 }
2871 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
2872 *al = SSL_AD_INTERNAL_ERROR;
2873 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2874 return 0;
2875 }
2876 /*
2877 * If client certificate is present and is of the same type, maybe
2878 * use it for key exchange. Don't mind errors from
2879 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2880 * client certificate for authorization only.
2881 */
2882 client_pub_pkey = X509_get0_pubkey(s->session->peer);
2883 if (client_pub_pkey) {
2884 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2885 ERR_clear_error();
2886 }
2887 /* Decrypt session key */
2888 sess_key_len = PACKET_remaining(pkt);
2889 if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
2890 *al = SSL_AD_INTERNAL_ERROR;
2891 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2892 goto err;
2893 }
2894 /* TODO(size_t): Convert this function */
2895 if (ASN1_get_object((const unsigned char **)&data, &Tlen, &Ttag,
2896 &Tclass, (long)sess_key_len) != V_ASN1_CONSTRUCTED
2897 || Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
2898 *al = SSL_AD_DECODE_ERROR;
2899 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
2900 goto err;
2901 }
2902 start = data;
2903 inlen = Tlen;
2904 if (EVP_PKEY_decrypt
2905 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2906 *al = SSL_AD_DECODE_ERROR;
2907 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
2908 goto err;
2909 }
2910 /* Generate master secret */
2911 if (!ssl_generate_master_secret(s, premaster_secret,
2912 sizeof(premaster_secret), 0)) {
2913 *al = SSL_AD_INTERNAL_ERROR;
2914 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2915 goto err;
2916 }
2917 /* Check if pubkey from client certificate was used */
2918 if (EVP_PKEY_CTX_ctrl
2919 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2920 s->statem.no_cert_verify = 1;
2921
2922 ret = 1;
2923 err:
2924 EVP_PKEY_CTX_free(pkey_ctx);
2925 return ret;
2926 #else
2927 /* Should never happen */
2928 *al = SSL_AD_INTERNAL_ERROR;
2929 SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
2930 return 0;
2931 #endif
2932 }
2933
2934 MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
2935 {
2936 int al = -1;
2937 unsigned long alg_k;
2938
2939 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2940
2941 /* For PSK parse and retrieve identity, obtain PSK key */
2942 if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt, &al))
2943 goto err;
2944
2945 if (alg_k & SSL_kPSK) {
2946 /* Identity extracted earlier: should be nothing left */
2947 if (PACKET_remaining(pkt) != 0) {
2948 al = SSL_AD_HANDSHAKE_FAILURE;
2949 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2950 SSL_R_LENGTH_MISMATCH);
2951 goto err;
2952 }
2953 /* PSK handled by ssl_generate_master_secret */
2954 if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
2955 al = SSL_AD_INTERNAL_ERROR;
2956 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2957 goto err;
2958 }
2959 } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2960 if (!tls_process_cke_rsa(s, pkt, &al))
2961 goto err;
2962 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2963 if (!tls_process_cke_dhe(s, pkt, &al))
2964 goto err;
2965 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2966 if (!tls_process_cke_ecdhe(s, pkt, &al))
2967 goto err;
2968 } else if (alg_k & SSL_kSRP) {
2969 if (!tls_process_cke_srp(s, pkt, &al))
2970 goto err;
2971 } else if (alg_k & SSL_kGOST) {
2972 if (!tls_process_cke_gost(s, pkt, &al))
2973 goto err;
2974 } else {
2975 al = SSL_AD_HANDSHAKE_FAILURE;
2976 SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
2977 SSL_R_UNKNOWN_CIPHER_TYPE);
2978 goto err;
2979 }
2980
2981 return MSG_PROCESS_CONTINUE_PROCESSING;
2982 err:
2983 if (al != -1)
2984 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2985 #ifndef OPENSSL_NO_PSK
2986 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2987 s->s3->tmp.psk = NULL;
2988 #endif
2989 ossl_statem_set_error(s);
2990 return MSG_PROCESS_ERROR;
2991 }
2992
2993 WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
2994 {
2995 #ifndef OPENSSL_NO_SCTP
2996 if (wst == WORK_MORE_A) {
2997 if (SSL_IS_DTLS(s)) {
2998 unsigned char sctpauthkey[64];
2999 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3000 /*
3001 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3002 * used.
3003 */
3004 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3005 sizeof(DTLS1_SCTP_AUTH_LABEL));
3006
3007 if (SSL_export_keying_material(s, sctpauthkey,
3008 sizeof(sctpauthkey), labelbuffer,
3009 sizeof(labelbuffer), NULL, 0,
3010 0) <= 0) {
3011 ossl_statem_set_error(s);
3012 return WORK_ERROR;
3013 }
3014
3015 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3016 sizeof(sctpauthkey), sctpauthkey);
3017 }
3018 wst = WORK_MORE_B;
3019 }
3020
3021 if ((wst == WORK_MORE_B)
3022 /* Is this SCTP? */
3023 && BIO_dgram_is_sctp(SSL_get_wbio(s))
3024 /* Are we renegotiating? */
3025 && s->renegotiate
3026 /* Are we going to skip the CertificateVerify? */
3027 && (s->session->peer == NULL || s->statem.no_cert_verify)
3028 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
3029 s->s3->in_read_app_data = 2;
3030 s->rwstate = SSL_READING;
3031 BIO_clear_retry_flags(SSL_get_rbio(s));
3032 BIO_set_retry_read(SSL_get_rbio(s));
3033 ossl_statem_set_sctp_read_sock(s, 1);
3034 return WORK_MORE_B;
3035 } else {
3036 ossl_statem_set_sctp_read_sock(s, 0);
3037 }
3038 #endif
3039
3040 if (s->statem.no_cert_verify || !s->session->peer) {
3041 /*
3042 * No certificate verify or no peer certificate so we no longer need
3043 * the handshake_buffer
3044 */
3045 if (!ssl3_digest_cached_records(s, 0)) {
3046 ossl_statem_set_error(s);
3047 return WORK_ERROR;
3048 }
3049 return WORK_FINISHED_CONTINUE;
3050 } else {
3051 if (!s->s3->handshake_buffer) {
3052 SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3053 ERR_R_INTERNAL_ERROR);
3054 ossl_statem_set_error(s);
3055 return WORK_ERROR;
3056 }
3057 /*
3058 * For sigalgs freeze the handshake buffer. If we support
3059 * extms we've done this already so this is a no-op
3060 */
3061 if (!ssl3_digest_cached_records(s, 1)) {
3062 ossl_statem_set_error(s);
3063 return WORK_ERROR;
3064 }
3065 }
3066
3067 return WORK_FINISHED_CONTINUE;
3068 }
3069
3070 MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3071 {
3072 int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
3073 X509 *x = NULL;
3074 unsigned long l, llen;
3075 const unsigned char *certstart, *certbytes;
3076 STACK_OF(X509) *sk = NULL;
3077 PACKET spkt, context;
3078 size_t chainidx;
3079
3080 if ((sk = sk_X509_new_null()) == NULL) {
3081 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3082 goto f_err;
3083 }
3084
3085 /* TODO(TLS1.3): For now we ignore the context. We need to verify this */
3086 if ((SSL_IS_TLS13(s) && !PACKET_get_length_prefixed_1(pkt, &context))
3087 || !PACKET_get_net_3(pkt, &llen)
3088 || !PACKET_get_sub_packet(pkt, &spkt, llen)
3089 || PACKET_remaining(pkt) != 0) {
3090 al = SSL_AD_DECODE_ERROR;
3091 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3092 goto f_err;
3093 }
3094
3095 for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
3096 if (!PACKET_get_net_3(&spkt, &l)
3097 || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3098 al = SSL_AD_DECODE_ERROR;
3099 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3100 SSL_R_CERT_LENGTH_MISMATCH);
3101 goto f_err;
3102 }
3103
3104 certstart = certbytes;
3105 x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3106 if (x == NULL) {
3107 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3108 goto f_err;
3109 }
3110 if (certbytes != (certstart + l)) {
3111 al = SSL_AD_DECODE_ERROR;
3112 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3113 SSL_R_CERT_LENGTH_MISMATCH);
3114 goto f_err;
3115 }
3116
3117 if (SSL_IS_TLS13(s)) {
3118 RAW_EXTENSION *rawexts = NULL;
3119 PACKET extensions;
3120
3121 if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3122 al = SSL_AD_DECODE_ERROR;
3123 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_BAD_LENGTH);
3124 goto f_err;
3125 }
3126 if (!tls_collect_extensions(s, &extensions, EXT_TLS1_3_CERTIFICATE,
3127 &rawexts, &al, NULL)
3128 || !tls_parse_all_extensions(s, EXT_TLS1_3_CERTIFICATE,
3129 rawexts, x, chainidx, &al)) {
3130 OPENSSL_free(rawexts);
3131 goto f_err;
3132 }
3133 OPENSSL_free(rawexts);
3134 }
3135
3136 if (!sk_X509_push(sk, x)) {
3137 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3138 goto f_err;
3139 }
3140 x = NULL;
3141 }
3142
3143 if (sk_X509_num(sk) <= 0) {
3144 /* TLS does not mind 0 certs returned */
3145 if (s->version == SSL3_VERSION) {
3146 al = SSL_AD_HANDSHAKE_FAILURE;
3147 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3148 SSL_R_NO_CERTIFICATES_RETURNED);
3149 goto f_err;
3150 }
3151 /* Fail for TLS only if we required a certificate */
3152 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3153 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3154 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3155 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3156 al = SSL_AD_HANDSHAKE_FAILURE;
3157 goto f_err;
3158 }
3159 /* No client certificate so digest cached records */
3160 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3161 goto f_err;
3162 }
3163 } else {
3164 EVP_PKEY *pkey;
3165 i = ssl_verify_cert_chain(s, sk);
3166 if (i <= 0) {
3167 al = ssl_verify_alarm_type(s->verify_result);
3168 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3169 SSL_R_CERTIFICATE_VERIFY_FAILED);
3170 goto f_err;
3171 }
3172 if (i > 1) {
3173 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3174 al = SSL_AD_HANDSHAKE_FAILURE;
3175 goto f_err;
3176 }
3177 pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3178 if (pkey == NULL) {
3179 al = SSL3_AD_HANDSHAKE_FAILURE;
3180 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3181 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3182 goto f_err;
3183 }
3184 }
3185
3186 X509_free(s->session->peer);
3187 s->session->peer = sk_X509_shift(sk);
3188 s->session->verify_result = s->verify_result;
3189
3190 sk_X509_pop_free(s->session->peer_chain, X509_free);
3191 s->session->peer_chain = sk;
3192
3193 /*
3194 * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3195 * message
3196 */
3197 if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
3198 al = SSL_AD_INTERNAL_ERROR;
3199 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3200 goto f_err;
3201 }
3202
3203 /*
3204 * Inconsistency alert: cert_chain does *not* include the peer's own
3205 * certificate, while we do include it in statem_clnt.c
3206 */
3207 sk = NULL;
3208
3209 /* Save the current hash state for when we receive the CertificateVerify */
3210 if (SSL_IS_TLS13(s)
3211 && !ssl_handshake_hash(s, s->cert_verify_hash,
3212 sizeof(s->cert_verify_hash),
3213 &s->cert_verify_hash_len)) {
3214 al = SSL_AD_INTERNAL_ERROR;
3215 SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3216 goto f_err;
3217 }
3218
3219 ret = MSG_PROCESS_CONTINUE_READING;
3220 goto done;
3221
3222 f_err:
3223 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3224 ossl_statem_set_error(s);
3225 done:
3226 X509_free(x);
3227 sk_X509_pop_free(sk, X509_free);
3228 return ret;
3229 }
3230
3231 int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
3232 {
3233 CERT_PKEY *cpk = s->s3->tmp.cert;
3234 int al = SSL_AD_INTERNAL_ERROR;
3235
3236 if (cpk == NULL) {
3237 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3238 return 0;
3239 }
3240
3241 /*
3242 * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3243 * for the server Certificate message
3244 */
3245 if ((SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0))
3246 || !ssl3_output_cert_chain(s, pkt, cpk, &al)) {
3247 SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3248 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3249 return 0;
3250 }
3251
3252 return 1;
3253 }
3254
3255 int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
3256 {
3257 unsigned char *senc = NULL;
3258 EVP_CIPHER_CTX *ctx = NULL;
3259 HMAC_CTX *hctx = NULL;
3260 unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
3261 const unsigned char *const_p;
3262 int len, slen_full, slen, lenfinal;
3263 SSL_SESSION *sess;
3264 unsigned int hlen;
3265 SSL_CTX *tctx = s->session_ctx;
3266 unsigned char iv[EVP_MAX_IV_LENGTH];
3267 unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
3268 int iv_len, al = SSL_AD_INTERNAL_ERROR;
3269 size_t macoffset, macendoffset;
3270 union {
3271 unsigned char age_add_c[sizeof(uint32_t)];
3272 uint32_t age_add;
3273 } age_add_u;
3274
3275 if (SSL_IS_TLS13(s)) {
3276 if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0)
3277 goto err;
3278 s->session->ext.tick_age_add = age_add_u.age_add;
3279 }
3280
3281 /* get session encoding length */
3282 slen_full = i2d_SSL_SESSION(s->session, NULL);
3283 /*
3284 * Some length values are 16 bits, so forget it if session is too
3285 * long
3286 */
3287 if (slen_full == 0 || slen_full > 0xFF00) {
3288 ossl_statem_set_error(s);
3289 return 0;
3290 }
3291 senc = OPENSSL_malloc(slen_full);
3292 if (senc == NULL) {
3293 ossl_statem_set_error(s);
3294 return 0;
3295 }
3296
3297 ctx = EVP_CIPHER_CTX_new();
3298 hctx = HMAC_CTX_new();
3299 if (ctx == NULL || hctx == NULL) {
3300 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
3301 goto err;
3302 }
3303
3304 p = senc;
3305 if (!i2d_SSL_SESSION(s->session, &p))
3306 goto err;
3307
3308 /*
3309 * create a fresh copy (not shared with other threads) to clean up
3310 */
3311 const_p = senc;
3312 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3313 if (sess == NULL)
3314 goto err;
3315 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3316
3317 slen = i2d_SSL_SESSION(sess, NULL);
3318 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3319 SSL_SESSION_free(sess);
3320 goto err;
3321 }
3322 p = senc;
3323 if (!i2d_SSL_SESSION(sess, &p)) {
3324 SSL_SESSION_free(sess);
3325 goto err;
3326 }
3327 SSL_SESSION_free(sess);
3328
3329 /*
3330 * Initialize HMAC and cipher contexts. If callback present it does
3331 * all the work otherwise use generated values from parent ctx.
3332 */
3333 if (tctx->ext.ticket_key_cb) {
3334 /* if 0 is returned, write an empty ticket */
3335 int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
3336 hctx, 1);
3337
3338 if (ret == 0) {
3339
3340 /* Put timeout and length */
3341 if (!WPACKET_put_bytes_u32(pkt, 0)
3342 || !WPACKET_put_bytes_u16(pkt, 0)) {
3343 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3344 ERR_R_INTERNAL_ERROR);
3345 goto err;
3346 }
3347 OPENSSL_free(senc);
3348 EVP_CIPHER_CTX_free(ctx);
3349 HMAC_CTX_free(hctx);
3350 return 1;
3351 }
3352 if (ret < 0)
3353 goto err;
3354 iv_len = EVP_CIPHER_CTX_iv_length(ctx);
3355 } else {
3356 const EVP_CIPHER *cipher = EVP_aes_256_cbc();
3357
3358 iv_len = EVP_CIPHER_iv_length(cipher);
3359 if (RAND_bytes(iv, iv_len) <= 0)
3360 goto err;
3361 if (!EVP_EncryptInit_ex(ctx, cipher, NULL,
3362 tctx->ext.tick_aes_key, iv))
3363 goto err;
3364 if (!HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key,
3365 sizeof(tctx->ext.tick_hmac_key),
3366 EVP_sha256(), NULL))
3367 goto err;
3368 memcpy(key_name, tctx->ext.tick_key_name,
3369 sizeof(tctx->ext.tick_key_name));
3370 }
3371
3372 /*
3373 * Ticket lifetime hint (advisory only): We leave this unspecified
3374 * for resumed session (for simplicity), and guess that tickets for
3375 * new sessions will live as long as their sessions.
3376 */
3377 if (!WPACKET_put_bytes_u32(pkt, s->hit ? 0 : s->session->timeout)
3378 || (SSL_IS_TLS13(s)
3379 && !WPACKET_put_bytes_u32(pkt, age_add_u.age_add))
3380 /* Now the actual ticket data */
3381 || !WPACKET_start_sub_packet_u16(pkt)
3382 || !WPACKET_get_total_written(pkt, &macoffset)
3383 /* Output key name */
3384 || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
3385 /* output IV */
3386 || !WPACKET_memcpy(pkt, iv, iv_len)
3387 || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
3388 &encdata1)
3389 /* Encrypt session data */
3390 || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
3391 || !WPACKET_allocate_bytes(pkt, len, &encdata2)
3392 || encdata1 != encdata2
3393 || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
3394 || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
3395 || encdata1 + len != encdata2
3396 || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
3397 || !WPACKET_get_total_written(pkt, &macendoffset)
3398 || !HMAC_Update(hctx,
3399 (unsigned char *)s->init_buf->data + macoffset,
3400 macendoffset - macoffset)
3401 || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
3402 || !HMAC_Final(hctx, macdata1, &hlen)
3403 || hlen > EVP_MAX_MD_SIZE
3404 || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
3405 || macdata1 != macdata2
3406 || !WPACKET_close(pkt)
3407 || (SSL_IS_TLS13(s)
3408 && !tls_construct_extensions(s, pkt,
3409 EXT_TLS1_3_NEW_SESSION_TICKET,
3410 NULL, 0, &al))) {
3411 SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
3412 goto err;
3413 }
3414 EVP_CIPHER_CTX_free(ctx);
3415 HMAC_CTX_free(hctx);
3416 OPENSSL_free(senc);
3417
3418 return 1;
3419 err:
3420 OPENSSL_free(senc);
3421 EVP_CIPHER_CTX_free(ctx);
3422 HMAC_CTX_free(hctx);
3423 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3424 return 0;
3425 }
3426
3427 /*
3428 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
3429 * create a separate message. Returns 1 on success or 0 on failure.
3430 */
3431 int tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
3432 {
3433 if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
3434 || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
3435 s->ext.ocsp.resp_len)) {
3436 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY, ERR_R_INTERNAL_ERROR);
3437 return 0;
3438 }
3439
3440 return 1;
3441 }
3442
3443 int tls_construct_cert_status(SSL *s, WPACKET *pkt)
3444 {
3445 if (!tls_construct_cert_status_body(s, pkt)) {
3446 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3447 return 0;
3448 }
3449
3450 return 1;
3451 }
3452
3453 #ifndef OPENSSL_NO_NEXTPROTONEG
3454 /*
3455 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
3456 * It sets the next_proto member in s if found
3457 */
3458 MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
3459 {
3460 PACKET next_proto, padding;
3461 size_t next_proto_len;
3462
3463 /*-
3464 * The payload looks like:
3465 * uint8 proto_len;
3466 * uint8 proto[proto_len];
3467 * uint8 padding_len;
3468 * uint8 padding[padding_len];
3469 */
3470 if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
3471 || !PACKET_get_length_prefixed_1(pkt, &padding)
3472 || PACKET_remaining(pkt) > 0) {
3473 SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO, SSL_R_LENGTH_MISMATCH);
3474 goto err;
3475 }
3476
3477 if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
3478 s->ext.npn_len = 0;
3479 goto err;
3480 }
3481
3482 s->ext.npn_len = (unsigned char)next_proto_len;
3483
3484 return MSG_PROCESS_CONTINUE_READING;
3485 err:
3486 ossl_statem_set_error(s);
3487 return MSG_PROCESS_ERROR;
3488 }
3489 #endif
3490
3491 static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
3492 {
3493 int al;
3494
3495 if (!tls_construct_extensions(s, pkt, EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
3496 NULL, 0, &al)) {
3497 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3498 SSLerr(SSL_F_TLS_CONSTRUCT_ENCRYPTED_EXTENSIONS, ERR_R_INTERNAL_ERROR);
3499 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3500 return 0;
3501 }
3502
3503 return 1;
3504 }
3505
3506 static int tls_construct_hello_retry_request(SSL *s, WPACKET *pkt)
3507 {
3508 int al = SSL_AD_INTERNAL_ERROR;
3509
3510 /*
3511 * TODO(TLS1.3): Remove the DRAFT version before release
3512 * (should be s->version)
3513 */
3514 if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)
3515 || !tls_construct_extensions(s, pkt, EXT_TLS1_3_HELLO_RETRY_REQUEST,
3516 NULL, 0, &al)) {
3517 SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_RETRY_REQUEST, ERR_R_INTERNAL_ERROR);
3518 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3519 return 0;
3520 }
3521
3522 /* Ditch the session. We'll create a new one next time around */
3523 SSL_SESSION_free(s->session);
3524 s->session = NULL;
3525 s->hit = 0;
3526
3527 return 1;
3528 }