]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/statem/statem_clnt.c
Remove old style NewSessionTicket from TLSv1.3
[thirdparty/openssl.git] / ssl / statem / statem_clnt.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 <openssl/buffer.h>
54 #include <openssl/rand.h>
55 #include <openssl/objects.h>
56 #include <openssl/evp.h>
57 #include <openssl/md5.h>
58 #include <openssl/dh.h>
59 #include <openssl/bn.h>
60 #include <openssl/engine.h>
61
62 static ossl_inline int cert_req_allowed(SSL *s);
63 static int key_exchange_expected(SSL *s);
64 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
65 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
66 WPACKET *pkt);
67
68 /*
69 * Is a CertificateRequest message allowed at the moment or not?
70 *
71 * Return values are:
72 * 1: Yes
73 * 0: No
74 */
75 static ossl_inline int cert_req_allowed(SSL *s)
76 {
77 /* TLS does not like anon-DH with client cert */
78 if ((s->version > SSL3_VERSION
79 && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
80 || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
81 return 0;
82
83 return 1;
84 }
85
86 /*
87 * Should we expect the ServerKeyExchange message or not?
88 *
89 * Return values are:
90 * 1: Yes
91 * 0: No
92 */
93 static int key_exchange_expected(SSL *s)
94 {
95 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
96
97 /*
98 * Can't skip server key exchange if this is an ephemeral
99 * ciphersuite or for SRP
100 */
101 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
102 | SSL_kSRP)) {
103 return 1;
104 }
105
106 return 0;
107 }
108
109 /*
110 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
111 * handshake state transitions when a TLS1.3 client is reading messages from the
112 * server. The message type that the server has sent is provided in |mt|. The
113 * current state is in |s->statem.hand_state|.
114 *
115 * Return values are 1 for success (transition allowed) and 0 on error
116 * (transition not allowed)
117 */
118 static int ossl_statem_client13_read_transition(SSL *s, int mt)
119 {
120 OSSL_STATEM *st = &s->statem;
121
122 /*
123 * TODO(TLS1.3): This is still based on the TLSv1.2 state machine. Over time
124 * we will update this to look more like real TLSv1.3
125 */
126
127 /*
128 * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
129 * yet negotiated TLSv1.3 at that point so that is handled by
130 * ossl_statem_client_read_transition()
131 */
132
133 switch (st->hand_state) {
134 default:
135 break;
136
137 case TLS_ST_CR_SRVR_HELLO:
138 if (s->hit) {
139 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
140 st->hand_state = TLS_ST_CR_CHANGE;
141 return 1;
142 }
143 } else {
144 if (mt == SSL3_MT_CERTIFICATE) {
145 st->hand_state = TLS_ST_CR_CERT;
146 return 1;
147 }
148 }
149 break;
150
151 case TLS_ST_CR_CERT:
152 /*
153 * The CertificateStatus message is optional even if
154 * |tlsext_status_expected| is set
155 */
156 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
157 st->hand_state = TLS_ST_CR_CERT_STATUS;
158 return 1;
159 }
160 /* Fall through */
161
162 case TLS_ST_CR_CERT_STATUS:
163 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
164 if (cert_req_allowed(s)) {
165 st->hand_state = TLS_ST_CR_CERT_REQ;
166 return 1;
167 }
168 goto err;
169 }
170 /* Fall through */
171
172 case TLS_ST_CR_CERT_REQ:
173 if (mt == SSL3_MT_SERVER_DONE) {
174 st->hand_state = TLS_ST_CR_SRVR_DONE;
175 return 1;
176 }
177 break;
178
179 case TLS_ST_CW_FINISHED:
180 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
181 st->hand_state = TLS_ST_CR_CHANGE;
182 return 1;
183 }
184 break;
185
186 case TLS_ST_CR_CHANGE:
187 if (mt == SSL3_MT_FINISHED) {
188 st->hand_state = TLS_ST_CR_FINISHED;
189 return 1;
190 }
191 break;
192 }
193
194 err:
195 /* No valid transition found */
196 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
197 SSLerr(SSL_F_OSSL_STATEM_CLIENT13_READ_TRANSITION,
198 SSL_R_UNEXPECTED_MESSAGE);
199 return 0;
200 }
201
202 /*
203 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
204 * handshake state transitions when the client is reading messages from the
205 * server. The message type that the server has sent is provided in |mt|. The
206 * current state is in |s->statem.hand_state|.
207 *
208 * Return values are 1 for success (transition allowed) and 0 on error
209 * (transition not allowed)
210 */
211 int ossl_statem_client_read_transition(SSL *s, int mt)
212 {
213 OSSL_STATEM *st = &s->statem;
214 int ske_expected;
215
216 /*
217 * Note that after a ClientHello we don't know what version we are going
218 * to negotiate yet, so we don't take this branch until later
219 */
220 if (s->method->version == TLS1_3_VERSION)
221 return ossl_statem_client13_read_transition(s, mt);
222
223 switch (st->hand_state) {
224 default:
225 break;
226
227 case TLS_ST_CW_CLNT_HELLO:
228 if (mt == SSL3_MT_SERVER_HELLO) {
229 st->hand_state = TLS_ST_CR_SRVR_HELLO;
230 return 1;
231 }
232
233 if (SSL_IS_DTLS(s)) {
234 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
235 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
236 return 1;
237 }
238 }
239 break;
240
241 case TLS_ST_CR_SRVR_HELLO:
242 if (s->hit) {
243 if (s->tlsext_ticket_expected) {
244 if (mt == SSL3_MT_NEWSESSION_TICKET) {
245 st->hand_state = TLS_ST_CR_SESSION_TICKET;
246 return 1;
247 }
248 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
249 st->hand_state = TLS_ST_CR_CHANGE;
250 return 1;
251 }
252 } else {
253 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
254 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
255 return 1;
256 } else if (s->version >= TLS1_VERSION
257 && s->tls_session_secret_cb != NULL
258 && s->session->tlsext_tick != NULL
259 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
260 /*
261 * Normally, we can tell if the server is resuming the session
262 * from the session ID. EAP-FAST (RFC 4851), however, relies on
263 * the next server message after the ServerHello to determine if
264 * the server is resuming.
265 */
266 s->hit = 1;
267 st->hand_state = TLS_ST_CR_CHANGE;
268 return 1;
269 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
270 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
271 if (mt == SSL3_MT_CERTIFICATE) {
272 st->hand_state = TLS_ST_CR_CERT;
273 return 1;
274 }
275 } else {
276 ske_expected = key_exchange_expected(s);
277 /* SKE is optional for some PSK ciphersuites */
278 if (ske_expected
279 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
280 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
281 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
282 st->hand_state = TLS_ST_CR_KEY_EXCH;
283 return 1;
284 }
285 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
286 && cert_req_allowed(s)) {
287 st->hand_state = TLS_ST_CR_CERT_REQ;
288 return 1;
289 } else if (mt == SSL3_MT_SERVER_DONE) {
290 st->hand_state = TLS_ST_CR_SRVR_DONE;
291 return 1;
292 }
293 }
294 }
295 break;
296
297 case TLS_ST_CR_CERT:
298 /*
299 * The CertificateStatus message is optional even if
300 * |tlsext_status_expected| is set
301 */
302 if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
303 st->hand_state = TLS_ST_CR_CERT_STATUS;
304 return 1;
305 }
306 /* Fall through */
307
308 case TLS_ST_CR_CERT_STATUS:
309 ske_expected = key_exchange_expected(s);
310 /* SKE is optional for some PSK ciphersuites */
311 if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
312 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
313 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
314 st->hand_state = TLS_ST_CR_KEY_EXCH;
315 return 1;
316 }
317 goto err;
318 }
319 /* Fall through */
320
321 case TLS_ST_CR_KEY_EXCH:
322 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
323 if (cert_req_allowed(s)) {
324 st->hand_state = TLS_ST_CR_CERT_REQ;
325 return 1;
326 }
327 goto err;
328 }
329 /* Fall through */
330
331 case TLS_ST_CR_CERT_REQ:
332 if (mt == SSL3_MT_SERVER_DONE) {
333 st->hand_state = TLS_ST_CR_SRVR_DONE;
334 return 1;
335 }
336 break;
337
338 case TLS_ST_CW_FINISHED:
339 if (s->tlsext_ticket_expected) {
340 if (mt == SSL3_MT_NEWSESSION_TICKET) {
341 st->hand_state = TLS_ST_CR_SESSION_TICKET;
342 return 1;
343 }
344 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
345 st->hand_state = TLS_ST_CR_CHANGE;
346 return 1;
347 }
348 break;
349
350 case TLS_ST_CR_SESSION_TICKET:
351 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
352 st->hand_state = TLS_ST_CR_CHANGE;
353 return 1;
354 }
355 break;
356
357 case TLS_ST_CR_CHANGE:
358 if (mt == SSL3_MT_FINISHED) {
359 st->hand_state = TLS_ST_CR_FINISHED;
360 return 1;
361 }
362 break;
363 }
364
365 err:
366 /* No valid transition found */
367 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
368 SSLerr(SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
369 return 0;
370 }
371
372 /*
373 * ossl_statem_client13_write_transition() works out what handshake state to
374 * move to next when the TLSv1.3 client is writing messages to be sent to the
375 * server.
376 */
377 static WRITE_TRAN ossl_statem_client13_write_transition(SSL *s)
378 {
379 OSSL_STATEM *st = &s->statem;
380
381 /*
382 * TODO(TLS1.3): This is still based on the TLSv1.2 state machine. Over time
383 * we will update this to look more like real TLSv1.3
384 */
385
386 /*
387 * Note: There are no cases for TLS_ST_BEFORE or TLS_ST_CW_CLNT_HELLO,
388 * because we haven't negotiated TLSv1.3 yet at that point. They are
389 * handled by ossl_statem_client_write_transition().
390 */
391 switch (st->hand_state) {
392 default:
393 /* Shouldn't happen */
394 return WRITE_TRAN_ERROR;
395
396 case TLS_ST_CR_SRVR_DONE:
397 st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT
398 : TLS_ST_CW_CHANGE;
399 return WRITE_TRAN_CONTINUE;
400
401 case TLS_ST_CW_CERT:
402 /* If a non-empty Certificate we also send CertificateVerify */
403 st->hand_state = (s->s3->tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
404 : TLS_ST_CW_CHANGE;
405 return WRITE_TRAN_CONTINUE;
406
407 case TLS_ST_CW_CERT_VRFY:
408 st->hand_state = TLS_ST_CW_CHANGE;
409 return WRITE_TRAN_CONTINUE;
410
411 case TLS_ST_CW_CHANGE:
412 st->hand_state = TLS_ST_CW_FINISHED;
413 return WRITE_TRAN_CONTINUE;
414
415 case TLS_ST_CW_FINISHED:
416 if (s->hit) {
417 st->hand_state = TLS_ST_OK;
418 ossl_statem_set_in_init(s, 0);
419 return WRITE_TRAN_CONTINUE;
420 }
421 return WRITE_TRAN_FINISHED;
422
423 case TLS_ST_CR_FINISHED:
424 if (s->hit) {
425 st->hand_state = TLS_ST_CW_CHANGE;
426 return WRITE_TRAN_CONTINUE;
427 }
428 st->hand_state = TLS_ST_OK;
429 ossl_statem_set_in_init(s, 0);
430 return WRITE_TRAN_CONTINUE;
431 }
432 }
433
434 /*
435 * ossl_statem_client_write_transition() works out what handshake state to
436 * move to next when the client is writing messages to be sent to the server.
437 */
438 WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
439 {
440 OSSL_STATEM *st = &s->statem;
441
442 /*
443 * Note that immediately before/after a ClientHello we don't know what
444 * version we are going to negotiate yet, so we don't take this branch until
445 * later
446 */
447 if (s->method->version == TLS1_3_VERSION)
448 return ossl_statem_client13_write_transition(s);
449
450 switch (st->hand_state) {
451 default:
452 /* Shouldn't happen */
453 return WRITE_TRAN_ERROR;
454
455 case TLS_ST_OK:
456 /* Renegotiation - fall through */
457 case TLS_ST_BEFORE:
458 st->hand_state = TLS_ST_CW_CLNT_HELLO;
459 return WRITE_TRAN_CONTINUE;
460
461 case TLS_ST_CW_CLNT_HELLO:
462 /*
463 * No transition at the end of writing because we don't know what
464 * we will be sent
465 */
466 return WRITE_TRAN_FINISHED;
467
468 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
469 st->hand_state = TLS_ST_CW_CLNT_HELLO;
470 return WRITE_TRAN_CONTINUE;
471
472 case TLS_ST_CR_SRVR_DONE:
473 if (s->s3->tmp.cert_req)
474 st->hand_state = TLS_ST_CW_CERT;
475 else
476 st->hand_state = TLS_ST_CW_KEY_EXCH;
477 return WRITE_TRAN_CONTINUE;
478
479 case TLS_ST_CW_CERT:
480 st->hand_state = TLS_ST_CW_KEY_EXCH;
481 return WRITE_TRAN_CONTINUE;
482
483 case TLS_ST_CW_KEY_EXCH:
484 /*
485 * For TLS, cert_req is set to 2, so a cert chain of nothing is
486 * sent, but no verify packet is sent
487 */
488 /*
489 * XXX: For now, we do not support client authentication in ECDH
490 * cipher suites with ECDH (rather than ECDSA) certificates. We
491 * need to skip the certificate verify message when client's
492 * ECDH public key is sent inside the client certificate.
493 */
494 if (s->s3->tmp.cert_req == 1) {
495 st->hand_state = TLS_ST_CW_CERT_VRFY;
496 } else {
497 st->hand_state = TLS_ST_CW_CHANGE;
498 }
499 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
500 st->hand_state = TLS_ST_CW_CHANGE;
501 }
502 return WRITE_TRAN_CONTINUE;
503
504 case TLS_ST_CW_CERT_VRFY:
505 st->hand_state = TLS_ST_CW_CHANGE;
506 return WRITE_TRAN_CONTINUE;
507
508 case TLS_ST_CW_CHANGE:
509 #if defined(OPENSSL_NO_NEXTPROTONEG)
510 st->hand_state = TLS_ST_CW_FINISHED;
511 #else
512 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
513 st->hand_state = TLS_ST_CW_NEXT_PROTO;
514 else
515 st->hand_state = TLS_ST_CW_FINISHED;
516 #endif
517 return WRITE_TRAN_CONTINUE;
518
519 #if !defined(OPENSSL_NO_NEXTPROTONEG)
520 case TLS_ST_CW_NEXT_PROTO:
521 st->hand_state = TLS_ST_CW_FINISHED;
522 return WRITE_TRAN_CONTINUE;
523 #endif
524
525 case TLS_ST_CW_FINISHED:
526 if (s->hit) {
527 st->hand_state = TLS_ST_OK;
528 ossl_statem_set_in_init(s, 0);
529 return WRITE_TRAN_CONTINUE;
530 } else {
531 return WRITE_TRAN_FINISHED;
532 }
533
534 case TLS_ST_CR_FINISHED:
535 if (s->hit) {
536 st->hand_state = TLS_ST_CW_CHANGE;
537 return WRITE_TRAN_CONTINUE;
538 } else {
539 st->hand_state = TLS_ST_OK;
540 ossl_statem_set_in_init(s, 0);
541 return WRITE_TRAN_CONTINUE;
542 }
543 }
544 }
545
546 /*
547 * Perform any pre work that needs to be done prior to sending a message from
548 * the client to the server.
549 */
550 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
551 {
552 OSSL_STATEM *st = &s->statem;
553
554 switch (st->hand_state) {
555 default:
556 /* No pre work to be done */
557 break;
558
559 case TLS_ST_CW_CLNT_HELLO:
560 s->shutdown = 0;
561 if (SSL_IS_DTLS(s)) {
562 /* every DTLS ClientHello resets Finished MAC */
563 if (!ssl3_init_finished_mac(s)) {
564 ossl_statem_set_error(s);
565 return WORK_ERROR;
566 }
567 }
568 break;
569
570 case TLS_ST_CW_CHANGE:
571 if (SSL_IS_DTLS(s)) {
572 if (s->hit) {
573 /*
574 * We're into the last flight so we don't retransmit these
575 * messages unless we need to.
576 */
577 st->use_timer = 0;
578 }
579 #ifndef OPENSSL_NO_SCTP
580 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
581 return dtls_wait_for_dry(s);
582 #endif
583 }
584 break;
585
586 case TLS_ST_OK:
587 return tls_finish_handshake(s, wst);
588 }
589
590 return WORK_FINISHED_CONTINUE;
591 }
592
593 /*
594 * Perform any work that needs to be done after sending a message from the
595 * client to the server.
596 */
597 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
598 {
599 OSSL_STATEM *st = &s->statem;
600
601 s->init_num = 0;
602
603 switch (st->hand_state) {
604 default:
605 /* No post work to be done */
606 break;
607
608 case TLS_ST_CW_CLNT_HELLO:
609 if (wst == WORK_MORE_A && statem_flush(s) != 1)
610 return WORK_MORE_A;
611
612 if (SSL_IS_DTLS(s)) {
613 /* Treat the next message as the first packet */
614 s->first_packet = 1;
615 }
616 break;
617
618 case TLS_ST_CW_KEY_EXCH:
619 if (tls_client_key_exchange_post_work(s) == 0)
620 return WORK_ERROR;
621 break;
622
623 case TLS_ST_CW_CHANGE:
624 s->session->cipher = s->s3->tmp.new_cipher;
625 #ifdef OPENSSL_NO_COMP
626 s->session->compress_meth = 0;
627 #else
628 if (s->s3->tmp.new_compression == NULL)
629 s->session->compress_meth = 0;
630 else
631 s->session->compress_meth = s->s3->tmp.new_compression->id;
632 #endif
633 if (!s->method->ssl3_enc->setup_key_block(s))
634 return WORK_ERROR;
635
636 if (!s->method->ssl3_enc->change_cipher_state(s,
637 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
638 return WORK_ERROR;
639
640 if (SSL_IS_DTLS(s)) {
641 #ifndef OPENSSL_NO_SCTP
642 if (s->hit) {
643 /*
644 * Change to new shared key of SCTP-Auth, will be ignored if
645 * no SCTP used.
646 */
647 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
648 0, NULL);
649 }
650 #endif
651
652 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
653 }
654 break;
655
656 case TLS_ST_CW_FINISHED:
657 #ifndef OPENSSL_NO_SCTP
658 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
659 /*
660 * Change to new shared key of SCTP-Auth, will be ignored if
661 * no SCTP used.
662 */
663 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
664 0, NULL);
665 }
666 #endif
667 if (statem_flush(s) != 1)
668 return WORK_MORE_B;
669 break;
670 }
671
672 return WORK_FINISHED_CONTINUE;
673 }
674
675 /*
676 * Get the message construction function and message type for sending from the
677 * client
678 *
679 * Valid return values are:
680 * 1: Success
681 * 0: Error
682 */
683 int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
684 confunc_f *confunc, int *mt)
685 {
686 OSSL_STATEM *st = &s->statem;
687
688 switch (st->hand_state) {
689 default:
690 /* Shouldn't happen */
691 return 0;
692
693 case TLS_ST_CW_CHANGE:
694 if (SSL_IS_DTLS(s))
695 *confunc = dtls_construct_change_cipher_spec;
696 else
697 *confunc = tls_construct_change_cipher_spec;
698 *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
699 break;
700
701 case TLS_ST_CW_CLNT_HELLO:
702 *confunc = tls_construct_client_hello;
703 *mt = SSL3_MT_CLIENT_HELLO;
704 break;
705
706 case TLS_ST_CW_CERT:
707 *confunc = tls_construct_client_certificate;
708 *mt = SSL3_MT_CERTIFICATE;
709 break;
710
711 case TLS_ST_CW_KEY_EXCH:
712 *confunc = tls_construct_client_key_exchange;
713 *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
714 break;
715
716 case TLS_ST_CW_CERT_VRFY:
717 *confunc = tls_construct_client_verify;
718 *mt = SSL3_MT_CERTIFICATE_VERIFY;
719 break;
720
721 #if !defined(OPENSSL_NO_NEXTPROTONEG)
722 case TLS_ST_CW_NEXT_PROTO:
723 *confunc = tls_construct_next_proto;
724 *mt = SSL3_MT_NEXT_PROTO;
725 break;
726 #endif
727 case TLS_ST_CW_FINISHED:
728 *confunc = tls_construct_finished;
729 *mt = SSL3_MT_FINISHED;
730 break;
731 }
732
733 return 1;
734 }
735
736 /*
737 * Returns the maximum allowed length for the current message that we are
738 * reading. Excludes the message header.
739 */
740 size_t ossl_statem_client_max_message_size(SSL *s)
741 {
742 OSSL_STATEM *st = &s->statem;
743
744 switch (st->hand_state) {
745 default:
746 /* Shouldn't happen */
747 return 0;
748
749 case TLS_ST_CR_SRVR_HELLO:
750 return SERVER_HELLO_MAX_LENGTH;
751
752 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
753 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
754
755 case TLS_ST_CR_CERT:
756 return s->max_cert_list;
757
758 case TLS_ST_CR_CERT_STATUS:
759 return SSL3_RT_MAX_PLAIN_LENGTH;
760
761 case TLS_ST_CR_KEY_EXCH:
762 return SERVER_KEY_EXCH_MAX_LENGTH;
763
764 case TLS_ST_CR_CERT_REQ:
765 /*
766 * Set to s->max_cert_list for compatibility with previous releases. In
767 * practice these messages can get quite long if servers are configured
768 * to provide a long list of acceptable CAs
769 */
770 return s->max_cert_list;
771
772 case TLS_ST_CR_SRVR_DONE:
773 return SERVER_HELLO_DONE_MAX_LENGTH;
774
775 case TLS_ST_CR_CHANGE:
776 if (s->version == DTLS1_BAD_VER)
777 return 3;
778 return CCS_MAX_LENGTH;
779
780 case TLS_ST_CR_SESSION_TICKET:
781 return SSL3_RT_MAX_PLAIN_LENGTH;
782
783 case TLS_ST_CR_FINISHED:
784 return FINISHED_MAX_LENGTH;
785 }
786 }
787
788 /*
789 * Process a message that the client has been received from the server.
790 */
791 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
792 {
793 OSSL_STATEM *st = &s->statem;
794
795 switch (st->hand_state) {
796 default:
797 /* Shouldn't happen */
798 return MSG_PROCESS_ERROR;
799
800 case TLS_ST_CR_SRVR_HELLO:
801 return tls_process_server_hello(s, pkt);
802
803 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
804 return dtls_process_hello_verify(s, pkt);
805
806 case TLS_ST_CR_CERT:
807 return tls_process_server_certificate(s, pkt);
808
809 case TLS_ST_CR_CERT_STATUS:
810 return tls_process_cert_status(s, pkt);
811
812 case TLS_ST_CR_KEY_EXCH:
813 return tls_process_key_exchange(s, pkt);
814
815 case TLS_ST_CR_CERT_REQ:
816 return tls_process_certificate_request(s, pkt);
817
818 case TLS_ST_CR_SRVR_DONE:
819 return tls_process_server_done(s, pkt);
820
821 case TLS_ST_CR_CHANGE:
822 return tls_process_change_cipher_spec(s, pkt);
823
824 case TLS_ST_CR_SESSION_TICKET:
825 return tls_process_new_session_ticket(s, pkt);
826
827 case TLS_ST_CR_FINISHED:
828 return tls_process_finished(s, pkt);
829 }
830 }
831
832 /*
833 * Perform any further processing required following the receipt of a message
834 * from the server
835 */
836 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
837 {
838 OSSL_STATEM *st = &s->statem;
839
840 switch (st->hand_state) {
841 default:
842 /* Shouldn't happen */
843 return WORK_ERROR;
844
845 case TLS_ST_CR_CERT_REQ:
846 return tls_prepare_client_certificate(s, wst);
847
848 #ifndef OPENSSL_NO_SCTP
849 case TLS_ST_CR_SRVR_DONE:
850 /* We only get here if we are using SCTP and we are renegotiating */
851 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
852 s->s3->in_read_app_data = 2;
853 s->rwstate = SSL_READING;
854 BIO_clear_retry_flags(SSL_get_rbio(s));
855 BIO_set_retry_read(SSL_get_rbio(s));
856 ossl_statem_set_sctp_read_sock(s, 1);
857 return WORK_MORE_A;
858 }
859 ossl_statem_set_sctp_read_sock(s, 0);
860 return WORK_FINISHED_STOP;
861 #endif
862 }
863 }
864
865 int tls_construct_client_hello(SSL *s, WPACKET *pkt)
866 {
867 unsigned char *p;
868 size_t sess_id_len;
869 int i, protverr;
870 int al = SSL_AD_HANDSHAKE_FAILURE;
871 #ifndef OPENSSL_NO_COMP
872 SSL_COMP *comp;
873 #endif
874 SSL_SESSION *sess = s->session;
875 int client_version;
876
877 if (!WPACKET_set_max_size(pkt, SSL3_RT_MAX_PLAIN_LENGTH)) {
878 /* Should not happen */
879 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
880 return 0;
881 }
882
883 /* Work out what SSL/TLS/DTLS version to use */
884 protverr = ssl_set_client_hello_version(s);
885 if (protverr != 0) {
886 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
887 return 0;
888 }
889
890 if ((sess == NULL) || !ssl_version_supported(s, sess->ssl_version) ||
891 /*
892 * In the case of EAP-FAST, we can have a pre-shared
893 * "ticket" without a session ID.
894 */
895 (!sess->session_id_length && !sess->tlsext_tick) ||
896 (sess->not_resumable)) {
897 if (!ssl_get_new_session(s, 0))
898 return 0;
899 }
900 /* else use the pre-loaded session */
901
902 p = s->s3->client_random;
903
904 /*
905 * for DTLS if client_random is initialized, reuse it, we are
906 * required to use same upon reply to HelloVerify
907 */
908 if (SSL_IS_DTLS(s)) {
909 size_t idx;
910 i = 1;
911 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
912 if (p[idx]) {
913 i = 0;
914 break;
915 }
916 }
917 } else
918 i = 1;
919
920 if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random)) <= 0)
921 return 0;
922
923 /*-
924 * version indicates the negotiated version: for example from
925 * an SSLv2/v3 compatible client hello). The client_version
926 * field is the maximum version we permit and it is also
927 * used in RSA encrypted premaster secrets. Some servers can
928 * choke if we initially report a higher version then
929 * renegotiate to a lower one in the premaster secret. This
930 * didn't happen with TLS 1.0 as most servers supported it
931 * but it can with TLS 1.1 or later if the server only supports
932 * 1.0.
933 *
934 * Possible scenario with previous logic:
935 * 1. Client hello indicates TLS 1.2
936 * 2. Server hello says TLS 1.0
937 * 3. RSA encrypted premaster secret uses 1.2.
938 * 4. Handshake proceeds using TLS 1.0.
939 * 5. Server sends hello request to renegotiate.
940 * 6. Client hello indicates TLS v1.0 as we now
941 * know that is maximum server supports.
942 * 7. Server chokes on RSA encrypted premaster secret
943 * containing version 1.0.
944 *
945 * For interoperability it should be OK to always use the
946 * maximum version we support in client hello and then rely
947 * on the checking of version to ensure the servers isn't
948 * being inconsistent: for example initially negotiating with
949 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
950 * client_version in client hello and not resetting it to
951 * the negotiated version.
952 *
953 * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
954 * supported_versions extension for the real supported versions.
955 */
956 client_version = SSL_IS_TLS13(s) ? TLS1_2_VERSION : s->client_version;
957 if (!WPACKET_put_bytes_u16(pkt, client_version)
958 || !WPACKET_memcpy(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)) {
959 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
960 return 0;
961 }
962
963 /* Session ID */
964 if (s->new_session)
965 sess_id_len = 0;
966 else
967 sess_id_len = s->session->session_id_length;
968 if (sess_id_len > sizeof(s->session->session_id)
969 || !WPACKET_start_sub_packet_u8(pkt)
970 || (sess_id_len != 0 && !WPACKET_memcpy(pkt, s->session->session_id,
971 sess_id_len))
972 || !WPACKET_close(pkt)) {
973 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
974 return 0;
975 }
976
977 /* cookie stuff for DTLS */
978 if (SSL_IS_DTLS(s)) {
979 if (s->d1->cookie_len > sizeof(s->d1->cookie)
980 || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
981 s->d1->cookie_len)) {
982 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
983 return 0;
984 }
985 }
986
987 /* Ciphers supported */
988 if (!WPACKET_start_sub_packet_u16(pkt)) {
989 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
990 return 0;
991 }
992 /* ssl_cipher_list_to_bytes() raises SSLerr if appropriate */
993 if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), pkt))
994 return 0;
995 if (!WPACKET_close(pkt)) {
996 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
997 return 0;
998 }
999
1000 /* COMPRESSION */
1001 if (!WPACKET_start_sub_packet_u8(pkt)) {
1002 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1003 return 0;
1004 }
1005 #ifndef OPENSSL_NO_COMP
1006 if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1007 int compnum = sk_SSL_COMP_num(s->ctx->comp_methods);
1008 for (i = 0; i < compnum; i++) {
1009 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
1010 if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
1011 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1012 return 0;
1013 }
1014 }
1015 }
1016 #endif
1017 /* Add the NULL method */
1018 if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
1019 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1020 return 0;
1021 }
1022
1023 /* TLS extensions */
1024 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
1025 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1026 return 0;
1027 }
1028 if (!WPACKET_start_sub_packet_u16(pkt)
1029 /*
1030 * If extensions are of zero length then we don't even add the
1031 * extensions length bytes
1032 */
1033 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
1034 || !ssl_add_clienthello_tlsext(s, pkt, &al)
1035 || !WPACKET_close(pkt)) {
1036 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1037 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1038 return 0;
1039 }
1040
1041 return 1;
1042 }
1043
1044 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
1045 {
1046 int al;
1047 size_t cookie_len;
1048 PACKET cookiepkt;
1049
1050 if (!PACKET_forward(pkt, 2)
1051 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1052 al = SSL_AD_DECODE_ERROR;
1053 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1054 goto f_err;
1055 }
1056
1057 cookie_len = PACKET_remaining(&cookiepkt);
1058 if (cookie_len > sizeof(s->d1->cookie)) {
1059 al = SSL_AD_ILLEGAL_PARAMETER;
1060 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
1061 goto f_err;
1062 }
1063
1064 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1065 al = SSL_AD_DECODE_ERROR;
1066 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1067 goto f_err;
1068 }
1069 s->d1->cookie_len = cookie_len;
1070
1071 return MSG_PROCESS_FINISHED_READING;
1072 f_err:
1073 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1074 ossl_statem_set_error(s);
1075 return MSG_PROCESS_ERROR;
1076 }
1077
1078 MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
1079 {
1080 STACK_OF(SSL_CIPHER) *sk;
1081 const SSL_CIPHER *c;
1082 PACKET session_id;
1083 size_t session_id_len;
1084 const unsigned char *cipherchars;
1085 int i, al = SSL_AD_INTERNAL_ERROR;
1086 unsigned int compression;
1087 unsigned int sversion;
1088 int protverr;
1089 #ifndef OPENSSL_NO_COMP
1090 SSL_COMP *comp;
1091 #endif
1092
1093 if (!PACKET_get_net_2(pkt, &sversion)) {
1094 al = SSL_AD_DECODE_ERROR;
1095 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1096 goto f_err;
1097 }
1098
1099 protverr = ssl_choose_client_version(s, sversion);
1100 if (protverr != 0) {
1101 al = SSL_AD_PROTOCOL_VERSION;
1102 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
1103 goto f_err;
1104 }
1105
1106 /* load the server hello data */
1107 /* load the server random */
1108 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1109 al = SSL_AD_DECODE_ERROR;
1110 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1111 goto f_err;
1112 }
1113
1114 s->hit = 0;
1115
1116 /* Get the session-id. */
1117 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
1118 al = SSL_AD_DECODE_ERROR;
1119 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1120 goto f_err;
1121 }
1122 session_id_len = PACKET_remaining(&session_id);
1123 if (session_id_len > sizeof s->session->session_id
1124 || session_id_len > SSL3_SESSION_ID_SIZE) {
1125 al = SSL_AD_ILLEGAL_PARAMETER;
1126 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1127 goto f_err;
1128 }
1129
1130 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
1131 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1132 al = SSL_AD_DECODE_ERROR;
1133 goto f_err;
1134 }
1135
1136 /*
1137 * Check if we can resume the session based on external pre-shared secret.
1138 * EAP-FAST (RFC 4851) supports two types of session resumption.
1139 * Resumption based on server-side state works with session IDs.
1140 * Resumption based on pre-shared Protected Access Credentials (PACs)
1141 * works by overriding the SessionTicket extension at the application
1142 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1143 * servers would honour the session ID.) Therefore, the session ID alone
1144 * is not a reliable indicator of session resumption, so we first check if
1145 * we can resume, and later peek at the next handshake message to see if the
1146 * server wants to resume.
1147 */
1148 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1149 s->session->tlsext_tick) {
1150 const SSL_CIPHER *pref_cipher = NULL;
1151 /*
1152 * s->session->master_key_length is a size_t, but this is an int for
1153 * backwards compat reasons
1154 */
1155 int master_key_length;
1156 master_key_length = sizeof(s->session->master_key);
1157 if (s->tls_session_secret_cb(s, s->session->master_key,
1158 &master_key_length,
1159 NULL, &pref_cipher,
1160 s->tls_session_secret_cb_arg)
1161 && master_key_length > 0) {
1162 s->session->master_key_length = master_key_length;
1163 s->session->cipher = pref_cipher ?
1164 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
1165 } else {
1166 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1167 al = SSL_AD_INTERNAL_ERROR;
1168 goto f_err;
1169 }
1170 }
1171
1172 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1173 && memcmp(PACKET_data(&session_id), s->session->session_id,
1174 session_id_len) == 0) {
1175 if (s->sid_ctx_length != s->session->sid_ctx_length
1176 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1177 /* actually a client application bug */
1178 al = SSL_AD_ILLEGAL_PARAMETER;
1179 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1180 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1181 goto f_err;
1182 }
1183 s->hit = 1;
1184 } else {
1185 /*
1186 * If we were trying for session-id reuse but the server
1187 * didn't echo the ID, make a new SSL_SESSION.
1188 * In the case of EAP-FAST and PAC, we do not send a session ID,
1189 * so the PAC-based session secret is always preserved. It'll be
1190 * overwritten if the server refuses resumption.
1191 */
1192 if (s->session->session_id_length > 0) {
1193 s->ctx->stats.sess_miss++;
1194 if (!ssl_get_new_session(s, 0)) {
1195 goto f_err;
1196 }
1197 }
1198
1199 s->session->ssl_version = s->version;
1200 s->session->session_id_length = session_id_len;
1201 /* session_id_len could be 0 */
1202 memcpy(s->session->session_id, PACKET_data(&session_id),
1203 session_id_len);
1204 }
1205
1206 /* Session version and negotiated protocol version should match */
1207 if (s->version != s->session->ssl_version) {
1208 al = SSL_AD_PROTOCOL_VERSION;
1209
1210 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1211 SSL_R_SSL_SESSION_VERSION_MISMATCH);
1212 goto f_err;
1213 }
1214
1215 c = ssl_get_cipher_by_char(s, cipherchars);
1216 if (c == NULL) {
1217 /* unknown cipher */
1218 al = SSL_AD_ILLEGAL_PARAMETER;
1219 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1220 goto f_err;
1221 }
1222 /*
1223 * Now that we know the version, update the check to see if it's an allowed
1224 * version.
1225 */
1226 s->s3->tmp.min_ver = s->version;
1227 s->s3->tmp.max_ver = s->version;
1228 /*
1229 * If it is a disabled cipher we either didn't send it in client hello,
1230 * or it's not allowed for the selected protocol. So we return an error.
1231 */
1232 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1233 al = SSL_AD_ILLEGAL_PARAMETER;
1234 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1235 goto f_err;
1236 }
1237
1238 sk = ssl_get_ciphers_by_id(s);
1239 i = sk_SSL_CIPHER_find(sk, c);
1240 if (i < 0) {
1241 /* we did not say we would use this cipher */
1242 al = SSL_AD_ILLEGAL_PARAMETER;
1243 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1244 goto f_err;
1245 }
1246
1247 /*
1248 * Depending on the session caching (internal/external), the cipher
1249 * and/or cipher_id values may not be set. Make sure that cipher_id is
1250 * set and use it for comparison.
1251 */
1252 if (s->session->cipher)
1253 s->session->cipher_id = s->session->cipher->id;
1254 if (s->hit && (s->session->cipher_id != c->id)) {
1255 al = SSL_AD_ILLEGAL_PARAMETER;
1256 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1257 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1258 goto f_err;
1259 }
1260 s->s3->tmp.new_cipher = c;
1261 /* lets get the compression algorithm */
1262 /* COMPRESSION */
1263 if (!PACKET_get_1(pkt, &compression)) {
1264 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1265 al = SSL_AD_DECODE_ERROR;
1266 goto f_err;
1267 }
1268 #ifdef OPENSSL_NO_COMP
1269 if (compression != 0) {
1270 al = SSL_AD_ILLEGAL_PARAMETER;
1271 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1272 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1273 goto f_err;
1274 }
1275 /*
1276 * If compression is disabled we'd better not try to resume a session
1277 * using compression.
1278 */
1279 if (s->session->compress_meth != 0) {
1280 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1281 goto f_err;
1282 }
1283 #else
1284 if (s->hit && compression != s->session->compress_meth) {
1285 al = SSL_AD_ILLEGAL_PARAMETER;
1286 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1287 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1288 goto f_err;
1289 }
1290 if (compression == 0)
1291 comp = NULL;
1292 else if (!ssl_allow_compression(s)) {
1293 al = SSL_AD_ILLEGAL_PARAMETER;
1294 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1295 goto f_err;
1296 } else {
1297 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1298 }
1299
1300 if (compression != 0 && comp == NULL) {
1301 al = SSL_AD_ILLEGAL_PARAMETER;
1302 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1303 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1304 goto f_err;
1305 } else {
1306 s->s3->tmp.new_compression = comp;
1307 }
1308 #endif
1309
1310 /* TLS extensions */
1311 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
1312 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1313 goto err;
1314 }
1315
1316 if (PACKET_remaining(pkt) != 0) {
1317 /* wrong packet length */
1318 al = SSL_AD_DECODE_ERROR;
1319 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1320 goto f_err;
1321 }
1322 #ifndef OPENSSL_NO_SCTP
1323 if (SSL_IS_DTLS(s) && s->hit) {
1324 unsigned char sctpauthkey[64];
1325 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1326
1327 /*
1328 * Add new shared key for SCTP-Auth, will be ignored if
1329 * no SCTP used.
1330 */
1331 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1332 sizeof(DTLS1_SCTP_AUTH_LABEL));
1333
1334 if (SSL_export_keying_material(s, sctpauthkey,
1335 sizeof(sctpauthkey),
1336 labelbuffer,
1337 sizeof(labelbuffer), NULL, 0, 0) <= 0)
1338 goto err;
1339
1340 BIO_ctrl(SSL_get_wbio(s),
1341 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1342 sizeof(sctpauthkey), sctpauthkey);
1343 }
1344 #endif
1345
1346 return MSG_PROCESS_CONTINUE_READING;
1347 f_err:
1348 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1349 err:
1350 ossl_statem_set_error(s);
1351 return MSG_PROCESS_ERROR;
1352 }
1353
1354 MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
1355 {
1356 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1357 unsigned long cert_list_len, cert_len;
1358 X509 *x = NULL;
1359 const unsigned char *certstart, *certbytes;
1360 STACK_OF(X509) *sk = NULL;
1361 EVP_PKEY *pkey = NULL;
1362
1363 if ((sk = sk_X509_new_null()) == NULL) {
1364 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1365 goto err;
1366 }
1367
1368 if (!PACKET_get_net_3(pkt, &cert_list_len)
1369 || PACKET_remaining(pkt) != cert_list_len) {
1370 al = SSL_AD_DECODE_ERROR;
1371 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1372 goto f_err;
1373 }
1374 while (PACKET_remaining(pkt)) {
1375 if (!PACKET_get_net_3(pkt, &cert_len)
1376 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1377 al = SSL_AD_DECODE_ERROR;
1378 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1379 SSL_R_CERT_LENGTH_MISMATCH);
1380 goto f_err;
1381 }
1382
1383 certstart = certbytes;
1384 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1385 if (x == NULL) {
1386 al = SSL_AD_BAD_CERTIFICATE;
1387 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1388 goto f_err;
1389 }
1390 if (certbytes != (certstart + cert_len)) {
1391 al = SSL_AD_DECODE_ERROR;
1392 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1393 SSL_R_CERT_LENGTH_MISMATCH);
1394 goto f_err;
1395 }
1396 if (!sk_X509_push(sk, x)) {
1397 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1398 goto err;
1399 }
1400 x = NULL;
1401 }
1402
1403 i = ssl_verify_cert_chain(s, sk);
1404 /*
1405 * The documented interface is that SSL_VERIFY_PEER should be set in order
1406 * for client side verification of the server certificate to take place.
1407 * However, historically the code has only checked that *any* flag is set
1408 * to cause server verification to take place. Use of the other flags makes
1409 * no sense in client mode. An attempt to clean up the semantics was
1410 * reverted because at least one application *only* set
1411 * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
1412 * server verification to take place, after the clean up it silently did
1413 * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
1414 * sent to them because they are void functions. Therefore, we now use the
1415 * (less clean) historic behaviour of performing validation if any flag is
1416 * set. The *documented* interface remains the same.
1417 */
1418 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1419 al = ssl_verify_alarm_type(s->verify_result);
1420 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1421 SSL_R_CERTIFICATE_VERIFY_FAILED);
1422 goto f_err;
1423 }
1424 ERR_clear_error(); /* but we keep s->verify_result */
1425 if (i > 1) {
1426 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1427 al = SSL_AD_HANDSHAKE_FAILURE;
1428 goto f_err;
1429 }
1430
1431 s->session->peer_chain = sk;
1432 /*
1433 * Inconsistency alert: cert_chain does include the peer's certificate,
1434 * which we don't include in statem_srvr.c
1435 */
1436 x = sk_X509_value(sk, 0);
1437 sk = NULL;
1438 /*
1439 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1440 */
1441
1442 pkey = X509_get0_pubkey(x);
1443
1444 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1445 x = NULL;
1446 al = SSL3_AL_FATAL;
1447 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1448 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1449 goto f_err;
1450 }
1451
1452 i = ssl_cert_type(x, pkey);
1453 if (i < 0) {
1454 x = NULL;
1455 al = SSL3_AL_FATAL;
1456 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1457 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1458 goto f_err;
1459 }
1460
1461 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1462 if (exp_idx >= 0 && i != exp_idx
1463 && (exp_idx != SSL_PKEY_GOST_EC ||
1464 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1465 && i != SSL_PKEY_GOST01))) {
1466 x = NULL;
1467 al = SSL_AD_ILLEGAL_PARAMETER;
1468 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1469 SSL_R_WRONG_CERTIFICATE_TYPE);
1470 goto f_err;
1471 }
1472 s->session->peer_type = i;
1473
1474 X509_free(s->session->peer);
1475 X509_up_ref(x);
1476 s->session->peer = x;
1477 s->session->verify_result = s->verify_result;
1478
1479 x = NULL;
1480 ret = MSG_PROCESS_CONTINUE_READING;
1481 goto done;
1482
1483 f_err:
1484 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1485 err:
1486 ossl_statem_set_error(s);
1487 done:
1488 X509_free(x);
1489 sk_X509_pop_free(sk, X509_free);
1490 return ret;
1491 }
1492
1493 static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt, int *al)
1494 {
1495 #ifndef OPENSSL_NO_PSK
1496 PACKET psk_identity_hint;
1497
1498 /* PSK ciphersuites are preceded by an identity hint */
1499
1500 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1501 *al = SSL_AD_DECODE_ERROR;
1502 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
1503 return 0;
1504 }
1505
1506 /*
1507 * Store PSK identity hint for later use, hint is used in
1508 * tls_construct_client_key_exchange. Assume that the maximum length of
1509 * a PSK identity hint can be as long as the maximum length of a PSK
1510 * identity.
1511 */
1512 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1513 *al = SSL_AD_HANDSHAKE_FAILURE;
1514 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
1515 return 0;
1516 }
1517
1518 if (PACKET_remaining(&psk_identity_hint) == 0) {
1519 OPENSSL_free(s->session->psk_identity_hint);
1520 s->session->psk_identity_hint = NULL;
1521 } else if (!PACKET_strndup(&psk_identity_hint,
1522 &s->session->psk_identity_hint)) {
1523 *al = SSL_AD_INTERNAL_ERROR;
1524 return 0;
1525 }
1526
1527 return 1;
1528 #else
1529 SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
1530 *al = SSL_AD_INTERNAL_ERROR;
1531 return 0;
1532 #endif
1533 }
1534
1535 static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1536 {
1537 #ifndef OPENSSL_NO_SRP
1538 PACKET prime, generator, salt, server_pub;
1539
1540 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1541 || !PACKET_get_length_prefixed_2(pkt, &generator)
1542 || !PACKET_get_length_prefixed_1(pkt, &salt)
1543 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1544 *al = SSL_AD_DECODE_ERROR;
1545 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_LENGTH_MISMATCH);
1546 return 0;
1547 }
1548
1549 /* TODO(size_t): Convert BN_bin2bn() calls */
1550 if ((s->srp_ctx.N =
1551 BN_bin2bn(PACKET_data(&prime),
1552 (int)PACKET_remaining(&prime), NULL)) == NULL
1553 || (s->srp_ctx.g =
1554 BN_bin2bn(PACKET_data(&generator),
1555 (int)PACKET_remaining(&generator), NULL)) == NULL
1556 || (s->srp_ctx.s =
1557 BN_bin2bn(PACKET_data(&salt),
1558 (int)PACKET_remaining(&salt), NULL)) == NULL
1559 || (s->srp_ctx.B =
1560 BN_bin2bn(PACKET_data(&server_pub),
1561 (int)PACKET_remaining(&server_pub), NULL)) == NULL) {
1562 *al = SSL_AD_INTERNAL_ERROR;
1563 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_BN_LIB);
1564 return 0;
1565 }
1566
1567 if (!srp_verify_server_param(s, al)) {
1568 *al = SSL_AD_DECODE_ERROR;
1569 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
1570 return 0;
1571 }
1572
1573 /* We must check if there is a certificate */
1574 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
1575 *pkey = X509_get0_pubkey(s->session->peer);
1576
1577 return 1;
1578 #else
1579 SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_INTERNAL_ERROR);
1580 *al = SSL_AD_INTERNAL_ERROR;
1581 return 0;
1582 #endif
1583 }
1584
1585 static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1586 {
1587 #ifndef OPENSSL_NO_DH
1588 PACKET prime, generator, pub_key;
1589 EVP_PKEY *peer_tmp = NULL;
1590
1591 DH *dh = NULL;
1592 BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
1593
1594 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1595 || !PACKET_get_length_prefixed_2(pkt, &generator)
1596 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1597 *al = SSL_AD_DECODE_ERROR;
1598 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_LENGTH_MISMATCH);
1599 return 0;
1600 }
1601
1602 peer_tmp = EVP_PKEY_new();
1603 dh = DH_new();
1604
1605 if (peer_tmp == NULL || dh == NULL) {
1606 *al = SSL_AD_INTERNAL_ERROR;
1607 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_MALLOC_FAILURE);
1608 goto err;
1609 }
1610
1611 /* TODO(size_t): Convert these calls */
1612 p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
1613 g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
1614 NULL);
1615 bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
1616 (int)PACKET_remaining(&pub_key), NULL);
1617 if (p == NULL || g == NULL || bnpub_key == NULL) {
1618 *al = SSL_AD_INTERNAL_ERROR;
1619 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1620 goto err;
1621 }
1622
1623 if (BN_is_zero(p) || BN_is_zero(g) || BN_is_zero(bnpub_key)) {
1624 *al = SSL_AD_DECODE_ERROR;
1625 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE);
1626 goto err;
1627 }
1628
1629 if (!DH_set0_pqg(dh, p, NULL, g)) {
1630 *al = SSL_AD_INTERNAL_ERROR;
1631 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1632 goto err;
1633 }
1634 p = g = NULL;
1635
1636 if (!DH_set0_key(dh, bnpub_key, NULL)) {
1637 *al = SSL_AD_INTERNAL_ERROR;
1638 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
1639 goto err;
1640 }
1641 bnpub_key = NULL;
1642
1643 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1644 *al = SSL_AD_HANDSHAKE_FAILURE;
1645 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_DH_KEY_TOO_SMALL);
1646 goto err;
1647 }
1648
1649 if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
1650 *al = SSL_AD_INTERNAL_ERROR;
1651 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_EVP_LIB);
1652 goto err;
1653 }
1654
1655 s->s3->peer_tmp = peer_tmp;
1656
1657 /*
1658 * FIXME: This makes assumptions about which ciphersuites come with
1659 * public keys. We should have a less ad-hoc way of doing this
1660 */
1661 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
1662 *pkey = X509_get0_pubkey(s->session->peer);
1663 /* else anonymous DH, so no certificate or pkey. */
1664
1665 return 1;
1666
1667 err:
1668 BN_free(p);
1669 BN_free(g);
1670 BN_free(bnpub_key);
1671 DH_free(dh);
1672 EVP_PKEY_free(peer_tmp);
1673
1674 return 0;
1675 #else
1676 SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_INTERNAL_ERROR);
1677 *al = SSL_AD_INTERNAL_ERROR;
1678 return 0;
1679 #endif
1680 }
1681
1682 static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
1683 {
1684 #ifndef OPENSSL_NO_EC
1685 PACKET encoded_pt;
1686 const unsigned char *ecparams;
1687 int curve_nid;
1688 unsigned int curve_flags;
1689 EVP_PKEY_CTX *pctx = NULL;
1690
1691 /*
1692 * Extract elliptic curve parameters and the server's ephemeral ECDH
1693 * public key. For now we only support named (not generic) curves and
1694 * ECParameters in this case is just three bytes.
1695 */
1696 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1697 *al = SSL_AD_DECODE_ERROR;
1698 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_TOO_SHORT);
1699 return 0;
1700 }
1701 /*
1702 * Check curve is one of our preferences, if not server has sent an
1703 * invalid curve. ECParameters is 3 bytes.
1704 */
1705 if (!tls1_check_curve(s, ecparams, 3)) {
1706 *al = SSL_AD_DECODE_ERROR;
1707 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_WRONG_CURVE);
1708 return 0;
1709 }
1710
1711 curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2), &curve_flags);
1712
1713 if (curve_nid == 0) {
1714 *al = SSL_AD_INTERNAL_ERROR;
1715 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE,
1716 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1717 return 0;
1718 }
1719
1720 if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) {
1721 EVP_PKEY *key = EVP_PKEY_new();
1722
1723 if (key == NULL || !EVP_PKEY_set_type(key, curve_nid)) {
1724 *al = SSL_AD_INTERNAL_ERROR;
1725 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1726 EVP_PKEY_free(key);
1727 return 0;
1728 }
1729 s->s3->peer_tmp = key;
1730 } else {
1731 /* Set up EVP_PKEY with named curve as parameters */
1732 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
1733 if (pctx == NULL
1734 || EVP_PKEY_paramgen_init(pctx) <= 0
1735 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
1736 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
1737 *al = SSL_AD_INTERNAL_ERROR;
1738 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
1739 EVP_PKEY_CTX_free(pctx);
1740 return 0;
1741 }
1742 EVP_PKEY_CTX_free(pctx);
1743 pctx = NULL;
1744 }
1745
1746 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1747 *al = SSL_AD_DECODE_ERROR;
1748 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_MISMATCH);
1749 return 0;
1750 }
1751
1752 if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
1753 PACKET_data(&encoded_pt),
1754 PACKET_remaining(&encoded_pt))) {
1755 *al = SSL_AD_DECODE_ERROR;
1756 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_BAD_ECPOINT);
1757 return 0;
1758 }
1759
1760 /*
1761 * The ECC/TLS specification does not mention the use of DSA to sign
1762 * ECParameters in the server key exchange message. We do support RSA
1763 * and ECDSA.
1764 */
1765 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA)
1766 *pkey = X509_get0_pubkey(s->session->peer);
1767 else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA)
1768 *pkey = X509_get0_pubkey(s->session->peer);
1769 /* else anonymous ECDH, so no certificate or pkey. */
1770
1771 return 1;
1772 #else
1773 SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_INTERNAL_ERROR);
1774 *al = SSL_AD_INTERNAL_ERROR;
1775 return 0;
1776 #endif
1777 }
1778
1779 MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
1780 {
1781 int al = -1;
1782 long alg_k;
1783 EVP_PKEY *pkey = NULL;
1784 PACKET save_param_start, signature;
1785
1786 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1787
1788 save_param_start = *pkt;
1789
1790 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1791 EVP_PKEY_free(s->s3->peer_tmp);
1792 s->s3->peer_tmp = NULL;
1793 #endif
1794
1795 if (alg_k & SSL_PSK) {
1796 if (!tls_process_ske_psk_preamble(s, pkt, &al))
1797 goto err;
1798 }
1799
1800 /* Nothing else to do for plain PSK or RSAPSK */
1801 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1802 } else if (alg_k & SSL_kSRP) {
1803 if (!tls_process_ske_srp(s, pkt, &pkey, &al))
1804 goto err;
1805 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1806 if (!tls_process_ske_dhe(s, pkt, &pkey, &al))
1807 goto err;
1808 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1809 if (!tls_process_ske_ecdhe(s, pkt, &pkey, &al))
1810 goto err;
1811 } else if (alg_k) {
1812 al = SSL_AD_UNEXPECTED_MESSAGE;
1813 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1814 goto err;
1815 }
1816
1817 /* if it was signed, check the signature */
1818 if (pkey != NULL) {
1819 PACKET params;
1820 int maxsig;
1821 const EVP_MD *md = NULL;
1822 EVP_MD_CTX *md_ctx;
1823
1824 /*
1825 * |pkt| now points to the beginning of the signature, so the difference
1826 * equals the length of the parameters.
1827 */
1828 if (!PACKET_get_sub_packet(&save_param_start, &params,
1829 PACKET_remaining(&save_param_start) -
1830 PACKET_remaining(pkt))) {
1831 al = SSL_AD_INTERNAL_ERROR;
1832 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1833 goto err;
1834 }
1835
1836 if (SSL_USE_SIGALGS(s)) {
1837 const unsigned char *sigalgs;
1838 int rv;
1839 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1840 al = SSL_AD_DECODE_ERROR;
1841 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1842 goto err;
1843 }
1844 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1845 if (rv == -1) {
1846 al = SSL_AD_INTERNAL_ERROR;
1847 goto err;
1848 } else if (rv == 0) {
1849 al = SSL_AD_DECODE_ERROR;
1850 goto err;
1851 }
1852 #ifdef SSL_DEBUG
1853 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1854 #endif
1855 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
1856 md = EVP_md5_sha1();
1857 } else {
1858 md = EVP_sha1();
1859 }
1860
1861 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1862 || PACKET_remaining(pkt) != 0) {
1863 al = SSL_AD_DECODE_ERROR;
1864 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1865 goto err;
1866 }
1867 maxsig = EVP_PKEY_size(pkey);
1868 if (maxsig < 0) {
1869 al = SSL_AD_INTERNAL_ERROR;
1870 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1871 goto err;
1872 }
1873
1874 /*
1875 * Check signature length
1876 */
1877 if (PACKET_remaining(&signature) > (size_t)maxsig) {
1878 /* wrong packet length */
1879 al = SSL_AD_DECODE_ERROR;
1880 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1881 SSL_R_WRONG_SIGNATURE_LENGTH);
1882 goto err;
1883 }
1884
1885 md_ctx = EVP_MD_CTX_new();
1886 if (md_ctx == NULL) {
1887 al = SSL_AD_INTERNAL_ERROR;
1888 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1889 goto err;
1890 }
1891
1892 if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
1893 || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
1894 SSL3_RANDOM_SIZE) <= 0
1895 || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
1896 SSL3_RANDOM_SIZE) <= 0
1897 || EVP_VerifyUpdate(md_ctx, PACKET_data(&params),
1898 PACKET_remaining(&params)) <= 0) {
1899 EVP_MD_CTX_free(md_ctx);
1900 al = SSL_AD_INTERNAL_ERROR;
1901 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1902 goto err;
1903 }
1904 /* TODO(size_t): Convert this call */
1905 if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
1906 (unsigned int)PACKET_remaining(&signature),
1907 pkey) <= 0) {
1908 /* bad signature */
1909 EVP_MD_CTX_free(md_ctx);
1910 al = SSL_AD_DECRYPT_ERROR;
1911 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1912 goto err;
1913 }
1914 EVP_MD_CTX_free(md_ctx);
1915 } else {
1916 /* aNULL, aSRP or PSK do not need public keys */
1917 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
1918 && !(alg_k & SSL_PSK)) {
1919 /* Might be wrong key type, check it */
1920 if (ssl3_check_cert_and_algorithm(s)) {
1921 /* Otherwise this shouldn't happen */
1922 al = SSL_AD_INTERNAL_ERROR;
1923 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1924 } else {
1925 al = SSL_AD_DECODE_ERROR;
1926 }
1927 goto err;
1928 }
1929 /* still data left over */
1930 if (PACKET_remaining(pkt) != 0) {
1931 al = SSL_AD_DECODE_ERROR;
1932 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1933 goto err;
1934 }
1935 }
1936
1937 return MSG_PROCESS_CONTINUE_READING;
1938 err:
1939 if (al != -1)
1940 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1941 ossl_statem_set_error(s);
1942 return MSG_PROCESS_ERROR;
1943 }
1944
1945 MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
1946 {
1947 int ret = MSG_PROCESS_ERROR;
1948 unsigned int list_len, ctype_num, i, name_len;
1949 X509_NAME *xn = NULL;
1950 const unsigned char *data;
1951 const unsigned char *namestart, *namebytes;
1952 STACK_OF(X509_NAME) *ca_sk = NULL;
1953
1954 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1955 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1956 goto err;
1957 }
1958
1959 /* get the certificate types */
1960 if (!PACKET_get_1(pkt, &ctype_num)
1961 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
1962 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1963 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
1964 goto err;
1965 }
1966 OPENSSL_free(s->cert->ctypes);
1967 s->cert->ctypes = NULL;
1968 if (ctype_num > SSL3_CT_NUMBER) {
1969 /* If we exceed static buffer copy all to cert structure */
1970 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1971 if (s->cert->ctypes == NULL) {
1972 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
1973 goto err;
1974 }
1975 memcpy(s->cert->ctypes, data, ctype_num);
1976 s->cert->ctype_num = ctype_num;
1977 ctype_num = SSL3_CT_NUMBER;
1978 }
1979 for (i = 0; i < ctype_num; i++)
1980 s->s3->tmp.ctype[i] = data[i];
1981
1982 if (SSL_USE_SIGALGS(s)) {
1983 if (!PACKET_get_net_2(pkt, &list_len)
1984 || !PACKET_get_bytes(pkt, &data, list_len)) {
1985 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1986 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1987 SSL_R_LENGTH_MISMATCH);
1988 goto err;
1989 }
1990
1991 /* Clear certificate digests and validity flags */
1992 for (i = 0; i < SSL_PKEY_NUM; i++) {
1993 s->s3->tmp.md[i] = NULL;
1994 s->s3->tmp.valid_flags[i] = 0;
1995 }
1996 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
1997 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1998 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1999 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2000 goto err;
2001 }
2002 if (!tls1_process_sigalgs(s)) {
2003 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2004 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2005 goto err;
2006 }
2007 } else {
2008 ssl_set_default_md(s);
2009 }
2010
2011 /* get the CA RDNs */
2012 if (!PACKET_get_net_2(pkt, &list_len)
2013 || PACKET_remaining(pkt) != list_len) {
2014 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2015 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2016 goto err;
2017 }
2018
2019 while (PACKET_remaining(pkt)) {
2020 if (!PACKET_get_net_2(pkt, &name_len)
2021 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
2022 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2023 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2024 SSL_R_LENGTH_MISMATCH);
2025 goto err;
2026 }
2027
2028 namestart = namebytes;
2029
2030 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2031 name_len)) == NULL) {
2032 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2033 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2034 goto err;
2035 }
2036
2037 if (namebytes != (namestart + name_len)) {
2038 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2039 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2040 SSL_R_CA_DN_LENGTH_MISMATCH);
2041 goto err;
2042 }
2043 if (!sk_X509_NAME_push(ca_sk, xn)) {
2044 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2045 goto err;
2046 }
2047 xn = NULL;
2048 }
2049
2050 /* we should setup a certificate to return.... */
2051 s->s3->tmp.cert_req = 1;
2052 s->s3->tmp.ctype_num = ctype_num;
2053 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2054 s->s3->tmp.ca_names = ca_sk;
2055 ca_sk = NULL;
2056
2057 ret = MSG_PROCESS_CONTINUE_PROCESSING;
2058 goto done;
2059 err:
2060 ossl_statem_set_error(s);
2061 done:
2062 X509_NAME_free(xn);
2063 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2064 return ret;
2065 }
2066
2067 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2068 {
2069 return (X509_NAME_cmp(*a, *b));
2070 }
2071
2072 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
2073 {
2074 int al;
2075 unsigned int ticklen;
2076 unsigned long ticket_lifetime_hint;
2077 unsigned int sess_len;
2078
2079 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2080 || !PACKET_get_net_2(pkt, &ticklen)
2081 || PACKET_remaining(pkt) != ticklen) {
2082 al = SSL_AD_DECODE_ERROR;
2083 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2084 goto f_err;
2085 }
2086
2087 /* Server is allowed to change its mind and send an empty ticket. */
2088 if (ticklen == 0)
2089 return MSG_PROCESS_CONTINUE_READING;
2090
2091 if (s->session->session_id_length > 0) {
2092 int i = s->session_ctx->session_cache_mode;
2093 SSL_SESSION *new_sess;
2094 /*
2095 * We reused an existing session, so we need to replace it with a new
2096 * one
2097 */
2098 if (i & SSL_SESS_CACHE_CLIENT) {
2099 /*
2100 * Remove the old session from the cache. We carry on if this fails
2101 */
2102 SSL_CTX_remove_session(s->session_ctx, s->session);
2103 }
2104
2105 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2106 al = SSL_AD_INTERNAL_ERROR;
2107 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2108 goto f_err;
2109 }
2110
2111 SSL_SESSION_free(s->session);
2112 s->session = new_sess;
2113 }
2114
2115 OPENSSL_free(s->session->tlsext_tick);
2116 s->session->tlsext_ticklen = 0;
2117
2118 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2119 if (s->session->tlsext_tick == NULL) {
2120 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2121 goto err;
2122 }
2123 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
2124 al = SSL_AD_DECODE_ERROR;
2125 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2126 goto f_err;
2127 }
2128
2129 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2130 s->session->tlsext_ticklen = ticklen;
2131 /*
2132 * There are two ways to detect a resumed ticket session. One is to set
2133 * an appropriate session ID and then the server must return a match in
2134 * ServerHello. This allows the normal client session ID matching to work
2135 * and we know much earlier that the ticket has been accepted. The
2136 * other way is to set zero length session ID when the ticket is
2137 * presented and rely on the handshake to determine session resumption.
2138 * We choose the former approach because this fits in with assumptions
2139 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2140 * SHA256 is disabled) hash of the ticket.
2141 */
2142 /*
2143 * TODO(size_t): we use sess_len here because EVP_Digest expects an int
2144 * but s->session->session_id_length is a size_t
2145 */
2146 if (!EVP_Digest(s->session->tlsext_tick, ticklen,
2147 s->session->session_id, &sess_len,
2148 EVP_sha256(), NULL)) {
2149 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB);
2150 goto err;
2151 }
2152 s->session->session_id_length = sess_len;
2153 return MSG_PROCESS_CONTINUE_READING;
2154 f_err:
2155 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2156 err:
2157 ossl_statem_set_error(s);
2158 return MSG_PROCESS_ERROR;
2159 }
2160
2161 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
2162 {
2163 int al;
2164 size_t resplen;
2165 unsigned int type;
2166
2167 if (!PACKET_get_1(pkt, &type)
2168 || type != TLSEXT_STATUSTYPE_ocsp) {
2169 al = SSL_AD_DECODE_ERROR;
2170 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2171 goto f_err;
2172 }
2173 if (!PACKET_get_net_3_len(pkt, &resplen)
2174 || PACKET_remaining(pkt) != resplen) {
2175 al = SSL_AD_DECODE_ERROR;
2176 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2177 goto f_err;
2178 }
2179 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
2180 if (s->tlsext_ocsp_resp == NULL) {
2181 al = SSL_AD_INTERNAL_ERROR;
2182 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2183 goto f_err;
2184 }
2185 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
2186 al = SSL_AD_DECODE_ERROR;
2187 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2188 goto f_err;
2189 }
2190 s->tlsext_ocsp_resplen = resplen;
2191 return MSG_PROCESS_CONTINUE_READING;
2192 f_err:
2193 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2194 ossl_statem_set_error(s);
2195 return MSG_PROCESS_ERROR;
2196 }
2197
2198 MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
2199 {
2200 if (PACKET_remaining(pkt) > 0) {
2201 /* should contain no data */
2202 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2203 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2204 ossl_statem_set_error(s);
2205 return MSG_PROCESS_ERROR;
2206 }
2207 #ifndef OPENSSL_NO_SRP
2208 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2209 if (SRP_Calc_A_param(s) <= 0) {
2210 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2211 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2212 ossl_statem_set_error(s);
2213 return MSG_PROCESS_ERROR;
2214 }
2215 }
2216 #endif
2217
2218 /*
2219 * at this point we check that we have the required stuff from
2220 * the server
2221 */
2222 if (!ssl3_check_cert_and_algorithm(s)) {
2223 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2224 ossl_statem_set_error(s);
2225 return MSG_PROCESS_ERROR;
2226 }
2227
2228 /*
2229 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
2230 * |tlsext_ocsp_resplen| values will be set if we actually received a status
2231 * message, or NULL and -1 otherwise
2232 */
2233 if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
2234 int ret;
2235 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2236 if (ret == 0) {
2237 ssl3_send_alert(s, SSL3_AL_FATAL,
2238 SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
2239 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,
2240 SSL_R_INVALID_STATUS_RESPONSE);
2241 return MSG_PROCESS_ERROR;
2242 }
2243 if (ret < 0) {
2244 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2245 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);
2246 return MSG_PROCESS_ERROR;
2247 }
2248 }
2249 #ifndef OPENSSL_NO_CT
2250 if (s->ct_validation_callback != NULL) {
2251 /* Note we validate the SCTs whether or not we abort on error */
2252 if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
2253 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2254 return MSG_PROCESS_ERROR;
2255 }
2256 }
2257 #endif
2258
2259 #ifndef OPENSSL_NO_SCTP
2260 /* Only applies to renegotiation */
2261 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2262 && s->renegotiate != 0)
2263 return MSG_PROCESS_CONTINUE_PROCESSING;
2264 else
2265 #endif
2266 return MSG_PROCESS_FINISHED_READING;
2267 }
2268
2269 static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt, int *al)
2270 {
2271 #ifndef OPENSSL_NO_PSK
2272 int ret = 0;
2273 /*
2274 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2275 * \0-terminated identity. The last byte is for us for simulating
2276 * strnlen.
2277 */
2278 char identity[PSK_MAX_IDENTITY_LEN + 1];
2279 size_t identitylen = 0;
2280 unsigned char psk[PSK_MAX_PSK_LEN];
2281 unsigned char *tmppsk = NULL;
2282 char *tmpidentity = NULL;
2283 size_t psklen = 0;
2284
2285 if (s->psk_client_callback == NULL) {
2286 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_CLIENT_CB);
2287 *al = SSL_AD_INTERNAL_ERROR;
2288 goto err;
2289 }
2290
2291 memset(identity, 0, sizeof(identity));
2292
2293 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2294 identity, sizeof(identity) - 1,
2295 psk, sizeof(psk));
2296
2297 if (psklen > PSK_MAX_PSK_LEN) {
2298 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2299 *al = SSL_AD_HANDSHAKE_FAILURE;
2300 goto err;
2301 } else if (psklen == 0) {
2302 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
2303 SSL_R_PSK_IDENTITY_NOT_FOUND);
2304 *al = SSL_AD_HANDSHAKE_FAILURE;
2305 goto err;
2306 }
2307
2308 identitylen = strlen(identity);
2309 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2310 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2311 *al = SSL_AD_HANDSHAKE_FAILURE;
2312 goto err;
2313 }
2314
2315 tmppsk = OPENSSL_memdup(psk, psklen);
2316 tmpidentity = OPENSSL_strdup(identity);
2317 if (tmppsk == NULL || tmpidentity == NULL) {
2318 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2319 *al = SSL_AD_INTERNAL_ERROR;
2320 goto err;
2321 }
2322
2323 OPENSSL_free(s->s3->tmp.psk);
2324 s->s3->tmp.psk = tmppsk;
2325 s->s3->tmp.psklen = psklen;
2326 tmppsk = NULL;
2327 OPENSSL_free(s->session->psk_identity);
2328 s->session->psk_identity = tmpidentity;
2329 tmpidentity = NULL;
2330
2331 if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
2332 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2333 *al = SSL_AD_INTERNAL_ERROR;
2334 goto err;
2335 }
2336
2337 ret = 1;
2338
2339 err:
2340 OPENSSL_cleanse(psk, psklen);
2341 OPENSSL_cleanse(identity, sizeof(identity));
2342 OPENSSL_clear_free(tmppsk, psklen);
2343 OPENSSL_clear_free(tmpidentity, identitylen);
2344
2345 return ret;
2346 #else
2347 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
2348 *al = SSL_AD_INTERNAL_ERROR;
2349 return 0;
2350 #endif
2351 }
2352
2353 static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al)
2354 {
2355 #ifndef OPENSSL_NO_RSA
2356 unsigned char *encdata = NULL;
2357 EVP_PKEY *pkey = NULL;
2358 EVP_PKEY_CTX *pctx = NULL;
2359 size_t enclen;
2360 unsigned char *pms = NULL;
2361 size_t pmslen = 0;
2362
2363 if (s->session->peer == NULL) {
2364 /*
2365 * We should always have a server certificate with SSL_kRSA.
2366 */
2367 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2368 return 0;
2369 }
2370
2371 pkey = X509_get0_pubkey(s->session->peer);
2372 if (EVP_PKEY_get0_RSA(pkey) == NULL) {
2373 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2374 return 0;
2375 }
2376
2377 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2378 pms = OPENSSL_malloc(pmslen);
2379 if (pms == NULL) {
2380 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);
2381 *al = SSL_AD_INTERNAL_ERROR;
2382 return 0;
2383 }
2384
2385 pms[0] = s->client_version >> 8;
2386 pms[1] = s->client_version & 0xff;
2387 /* TODO(size_t): Convert this function */
2388 if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) {
2389 goto err;
2390 }
2391
2392 /* Fix buf for TLS and beyond */
2393 if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
2394 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2395 goto err;
2396 }
2397 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2398 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2399 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2400 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);
2401 goto err;
2402 }
2403 if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
2404 || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
2405 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);
2406 goto err;
2407 }
2408 EVP_PKEY_CTX_free(pctx);
2409 pctx = NULL;
2410 # ifdef PKCS1_CHECK
2411 if (s->options & SSL_OP_PKCS1_CHECK_1)
2412 (*p)[1]++;
2413 if (s->options & SSL_OP_PKCS1_CHECK_2)
2414 tmp_buf[0] = 0x70;
2415 # endif
2416
2417 /* Fix buf for TLS and beyond */
2418 if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
2419 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2420 goto err;
2421 }
2422
2423 s->s3->tmp.pms = pms;
2424 s->s3->tmp.pmslen = pmslen;
2425
2426 return 1;
2427 err:
2428 OPENSSL_clear_free(pms, pmslen);
2429 EVP_PKEY_CTX_free(pctx);
2430
2431 return 0;
2432 #else
2433 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
2434 *al = SSL_AD_INTERNAL_ERROR;
2435 return 0;
2436 #endif
2437 }
2438
2439 static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt, int *al)
2440 {
2441 #ifndef OPENSSL_NO_DH
2442 DH *dh_clnt = NULL;
2443 const BIGNUM *pub_key;
2444 EVP_PKEY *ckey = NULL, *skey = NULL;
2445 unsigned char *keybytes = NULL;
2446
2447 skey = s->s3->peer_tmp;
2448 if (skey == NULL)
2449 goto err;
2450
2451 ckey = ssl_generate_pkey(skey);
2452 dh_clnt = EVP_PKEY_get0_DH(ckey);
2453
2454 if (dh_clnt == NULL || ssl_derive(s, ckey, skey, 0) == 0)
2455 goto err;
2456
2457 /* send off the data */
2458 DH_get0_key(dh_clnt, &pub_key, NULL);
2459 if (!WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(pub_key), &keybytes))
2460 goto err;
2461
2462 BN_bn2bin(pub_key, keybytes);
2463 EVP_PKEY_free(ckey);
2464
2465 return 1;
2466 err:
2467 EVP_PKEY_free(ckey);
2468 #endif
2469 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
2470 *al = SSL_AD_INTERNAL_ERROR;
2471 return 0;
2472 }
2473
2474 static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt, int *al)
2475 {
2476 #ifndef OPENSSL_NO_EC
2477 unsigned char *encodedPoint = NULL;
2478 size_t encoded_pt_len = 0;
2479 EVP_PKEY *ckey = NULL, *skey = NULL;
2480 int ret = 0;
2481
2482 skey = s->s3->peer_tmp;
2483 if (skey == NULL) {
2484 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2485 return 0;
2486 }
2487
2488 ckey = ssl_generate_pkey(skey);
2489
2490 if (ssl_derive(s, ckey, skey, 0) == 0) {
2491 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EVP_LIB);
2492 goto err;
2493 }
2494
2495 /* Generate encoding of client key */
2496 encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint);
2497
2498 if (encoded_pt_len == 0) {
2499 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EC_LIB);
2500 goto err;
2501 }
2502
2503 if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
2504 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2505 goto err;
2506 }
2507
2508 ret = 1;
2509 err:
2510 OPENSSL_free(encodedPoint);
2511 EVP_PKEY_free(ckey);
2512 return ret;
2513 #else
2514 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
2515 *al = SSL_AD_INTERNAL_ERROR;
2516 return 0;
2517 #endif
2518 }
2519
2520 static int tls_construct_cke_gost(SSL *s, WPACKET *pkt, int *al)
2521 {
2522 #ifndef OPENSSL_NO_GOST
2523 /* GOST key exchange message creation */
2524 EVP_PKEY_CTX *pkey_ctx = NULL;
2525 X509 *peer_cert;
2526 size_t msglen;
2527 unsigned int md_len;
2528 unsigned char shared_ukm[32], tmp[256];
2529 EVP_MD_CTX *ukm_hash = NULL;
2530 int dgst_nid = NID_id_GostR3411_94;
2531 unsigned char *pms = NULL;
2532 size_t pmslen = 0;
2533
2534 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2535 dgst_nid = NID_id_GostR3411_2012_256;
2536
2537 /*
2538 * Get server sertificate PKEY and create ctx from it
2539 */
2540 peer_cert = s->session->peer;
2541 if (!peer_cert) {
2542 *al = SSL_AD_HANDSHAKE_FAILURE;
2543 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST,
2544 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2545 return 0;
2546 }
2547
2548 pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
2549 if (pkey_ctx == NULL) {
2550 *al = SSL_AD_INTERNAL_ERROR;
2551 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2552 return 0;
2553 }
2554 /*
2555 * If we have send a certificate, and certificate key
2556 * parameters match those of server certificate, use
2557 * certificate key for key exchange
2558 */
2559
2560 /* Otherwise, generate ephemeral key pair */
2561 pmslen = 32;
2562 pms = OPENSSL_malloc(pmslen);
2563 if (pms == NULL) {
2564 *al = SSL_AD_INTERNAL_ERROR;
2565 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
2566 goto err;
2567 }
2568
2569 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2570 /* Generate session key
2571 * TODO(size_t): Convert this function
2572 */
2573 || RAND_bytes(pms, (int)pmslen) <= 0) {
2574 *al = SSL_AD_INTERNAL_ERROR;
2575 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2576 goto err;
2577 };
2578 /*
2579 * Compute shared IV and store it in algorithm-specific context
2580 * data
2581 */
2582 ukm_hash = EVP_MD_CTX_new();
2583 if (ukm_hash == NULL
2584 || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
2585 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2586 SSL3_RANDOM_SIZE) <= 0
2587 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2588 SSL3_RANDOM_SIZE) <= 0
2589 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
2590 *al = SSL_AD_INTERNAL_ERROR;
2591 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2592 goto err;
2593 }
2594 EVP_MD_CTX_free(ukm_hash);
2595 ukm_hash = NULL;
2596 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
2597 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
2598 *al = SSL_AD_INTERNAL_ERROR;
2599 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
2600 goto err;
2601 }
2602 /* Make GOST keytransport blob message */
2603 /*
2604 * Encapsulate it into sequence
2605 */
2606 msglen = 255;
2607 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
2608 *al = SSL_AD_INTERNAL_ERROR;
2609 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
2610 goto err;
2611 }
2612
2613 if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
2614 || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
2615 || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
2616 *al = SSL_AD_INTERNAL_ERROR;
2617 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2618 goto err;
2619 }
2620
2621 EVP_PKEY_CTX_free(pkey_ctx);
2622 s->s3->tmp.pms = pms;
2623 s->s3->tmp.pmslen = pmslen;
2624
2625 return 1;
2626 err:
2627 EVP_PKEY_CTX_free(pkey_ctx);
2628 OPENSSL_clear_free(pms, pmslen);
2629 EVP_MD_CTX_free(ukm_hash);
2630 return 0;
2631 #else
2632 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
2633 *al = SSL_AD_INTERNAL_ERROR;
2634 return 0;
2635 #endif
2636 }
2637
2638 static int tls_construct_cke_srp(SSL *s, WPACKET *pkt, int *al)
2639 {
2640 #ifndef OPENSSL_NO_SRP
2641 unsigned char *abytes = NULL;
2642
2643 if (s->srp_ctx.A == NULL
2644 || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
2645 &abytes)) {
2646 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
2647 return 0;
2648 }
2649 BN_bn2bin(s->srp_ctx.A, abytes);
2650
2651 OPENSSL_free(s->session->srp_username);
2652 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
2653 if (s->session->srp_username == NULL) {
2654 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_MALLOC_FAILURE);
2655 return 0;
2656 }
2657
2658 return 1;
2659 #else
2660 SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
2661 *al = SSL_AD_INTERNAL_ERROR;
2662 return 0;
2663 #endif
2664 }
2665
2666 int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt)
2667 {
2668 unsigned long alg_k;
2669 int al = -1;
2670
2671 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2672
2673 if ((alg_k & SSL_PSK)
2674 && !tls_construct_cke_psk_preamble(s, pkt, &al))
2675 goto err;
2676
2677 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2678 if (!tls_construct_cke_rsa(s, pkt, &al))
2679 goto err;
2680 } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2681 if (!tls_construct_cke_dhe(s, pkt, &al))
2682 goto err;
2683 } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2684 if (!tls_construct_cke_ecdhe(s, pkt, &al))
2685 goto err;
2686 } else if (alg_k & SSL_kGOST) {
2687 if (!tls_construct_cke_gost(s, pkt, &al))
2688 goto err;
2689 } else if (alg_k & SSL_kSRP) {
2690 if (!tls_construct_cke_srp(s, pkt, &al))
2691 goto err;
2692 } else if (!(alg_k & SSL_kPSK)) {
2693 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2694 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2695 goto err;
2696 }
2697
2698 return 1;
2699 err:
2700 if (al != -1)
2701 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2702 OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen);
2703 s->s3->tmp.pms = NULL;
2704 #ifndef OPENSSL_NO_PSK
2705 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2706 s->s3->tmp.psk = NULL;
2707 #endif
2708 return 0;
2709 }
2710
2711 int tls_client_key_exchange_post_work(SSL *s)
2712 {
2713 unsigned char *pms = NULL;
2714 size_t pmslen = 0;
2715
2716 pms = s->s3->tmp.pms;
2717 pmslen = s->s3->tmp.pmslen;
2718
2719 #ifndef OPENSSL_NO_SRP
2720 /* Check for SRP */
2721 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2722 if (!srp_generate_client_master_secret(s)) {
2723 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2724 ERR_R_INTERNAL_ERROR);
2725 goto err;
2726 }
2727 return 1;
2728 }
2729 #endif
2730
2731 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2732 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2733 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2734 goto err;
2735 }
2736 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2737 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2738 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2739 /* ssl_generate_master_secret frees the pms even on error */
2740 pms = NULL;
2741 pmslen = 0;
2742 goto err;
2743 }
2744 pms = NULL;
2745 pmslen = 0;
2746
2747 #ifndef OPENSSL_NO_SCTP
2748 if (SSL_IS_DTLS(s)) {
2749 unsigned char sctpauthkey[64];
2750 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2751
2752 /*
2753 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2754 * used.
2755 */
2756 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2757 sizeof(DTLS1_SCTP_AUTH_LABEL));
2758
2759 if (SSL_export_keying_material(s, sctpauthkey,
2760 sizeof(sctpauthkey), labelbuffer,
2761 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2762 goto err;
2763
2764 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2765 sizeof(sctpauthkey), sctpauthkey);
2766 }
2767 #endif
2768
2769 return 1;
2770 err:
2771 OPENSSL_clear_free(pms, pmslen);
2772 s->s3->tmp.pms = NULL;
2773 return 0;
2774 }
2775
2776 int tls_construct_client_verify(SSL *s, WPACKET *pkt)
2777 {
2778 EVP_PKEY *pkey;
2779 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2780 EVP_MD_CTX *mctx = NULL;
2781 unsigned u = 0;
2782 long hdatalen = 0;
2783 void *hdata;
2784 unsigned char *sig = NULL;
2785
2786 mctx = EVP_MD_CTX_new();
2787 if (mctx == NULL) {
2788 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2789 goto err;
2790 }
2791 pkey = s->cert->key->privatekey;
2792
2793 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2794 if (hdatalen <= 0) {
2795 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2796 goto err;
2797 }
2798
2799 if (SSL_USE_SIGALGS(s)&& !tls12_get_sigandhash(pkt, pkey, md)) {
2800 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2801 goto err;
2802 }
2803 #ifdef SSL_DEBUG
2804 fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
2805 #endif
2806 sig = OPENSSL_malloc(EVP_PKEY_size(pkey));
2807 if (sig == NULL) {
2808 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2809 goto err;
2810 }
2811 if (!EVP_SignInit_ex(mctx, md, NULL)
2812 || !EVP_SignUpdate(mctx, hdata, hdatalen)
2813 || (s->version == SSL3_VERSION
2814 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
2815 (int)s->session->master_key_length,
2816 s->session->master_key))
2817 || !EVP_SignFinal(mctx, sig, &u, pkey)) {
2818 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2819 goto err;
2820 }
2821 #ifndef OPENSSL_NO_GOST
2822 {
2823 int pktype = EVP_PKEY_id(pkey);
2824 if (pktype == NID_id_GostR3410_2001
2825 || pktype == NID_id_GostR3410_2012_256
2826 || pktype == NID_id_GostR3410_2012_512)
2827 BUF_reverse(sig, NULL, u);
2828 }
2829 #endif
2830
2831 if (!WPACKET_sub_memcpy_u16(pkt, sig, u)) {
2832 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2833 goto err;
2834 }
2835
2836 /* Digest cached records and discard handshake buffer */
2837 if (!ssl3_digest_cached_records(s, 0))
2838 goto err;
2839
2840 OPENSSL_free(sig);
2841 EVP_MD_CTX_free(mctx);
2842 return 1;
2843 err:
2844 OPENSSL_free(sig);
2845 EVP_MD_CTX_free(mctx);
2846 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2847 return 0;
2848 }
2849
2850 /*
2851 * Check a certificate can be used for client authentication. Currently check
2852 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2853 * certificates can be used and optionally checks suitability for Suite B.
2854 */
2855 static int ssl3_check_client_certificate(SSL *s)
2856 {
2857 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2858 return 0;
2859 /* If no suitable signature algorithm can't use certificate */
2860 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
2861 return 0;
2862 /*
2863 * If strict mode check suitability of chain before using it. This also
2864 * adjusts suite B digest if necessary.
2865 */
2866 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2867 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2868 return 0;
2869 return 1;
2870 }
2871
2872 WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
2873 {
2874 X509 *x509 = NULL;
2875 EVP_PKEY *pkey = NULL;
2876 int i;
2877
2878 if (wst == WORK_MORE_A) {
2879 /* Let cert callback update client certificates if required */
2880 if (s->cert->cert_cb) {
2881 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2882 if (i < 0) {
2883 s->rwstate = SSL_X509_LOOKUP;
2884 return WORK_MORE_A;
2885 }
2886 if (i == 0) {
2887 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2888 ossl_statem_set_error(s);
2889 return 0;
2890 }
2891 s->rwstate = SSL_NOTHING;
2892 }
2893 if (ssl3_check_client_certificate(s))
2894 return WORK_FINISHED_CONTINUE;
2895
2896 /* Fall through to WORK_MORE_B */
2897 wst = WORK_MORE_B;
2898 }
2899
2900 /* We need to get a client cert */
2901 if (wst == WORK_MORE_B) {
2902 /*
2903 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2904 * return(-1); We then get retied later
2905 */
2906 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2907 if (i < 0) {
2908 s->rwstate = SSL_X509_LOOKUP;
2909 return WORK_MORE_B;
2910 }
2911 s->rwstate = SSL_NOTHING;
2912 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2913 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2914 i = 0;
2915 } else if (i == 1) {
2916 i = 0;
2917 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
2918 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2919 }
2920
2921 X509_free(x509);
2922 EVP_PKEY_free(pkey);
2923 if (i && !ssl3_check_client_certificate(s))
2924 i = 0;
2925 if (i == 0) {
2926 if (s->version == SSL3_VERSION) {
2927 s->s3->tmp.cert_req = 0;
2928 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
2929 return WORK_FINISHED_CONTINUE;
2930 } else {
2931 s->s3->tmp.cert_req = 2;
2932 if (!ssl3_digest_cached_records(s, 0)) {
2933 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2934 ossl_statem_set_error(s);
2935 return 0;
2936 }
2937 }
2938 }
2939
2940 return WORK_FINISHED_CONTINUE;
2941 }
2942
2943 /* Shouldn't ever get here */
2944 return WORK_ERROR;
2945 }
2946
2947 int tls_construct_client_certificate(SSL *s, WPACKET *pkt)
2948 {
2949 if (!ssl3_output_cert_chain(s, pkt,
2950 (s->s3->tmp.cert_req == 2) ? NULL
2951 : s->cert->key)) {
2952 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2953 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2954 return 0;
2955 }
2956
2957 return 1;
2958 }
2959
2960 #define has_bits(i,m) (((i)&(m)) == (m))
2961
2962 int ssl3_check_cert_and_algorithm(SSL *s)
2963 {
2964 int i;
2965 #ifndef OPENSSL_NO_EC
2966 int idx;
2967 #endif
2968 long alg_k, alg_a;
2969 EVP_PKEY *pkey = NULL;
2970 int al = SSL_AD_HANDSHAKE_FAILURE;
2971
2972 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2973 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2974
2975 /* we don't have a certificate */
2976 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
2977 return (1);
2978
2979 /* This is the passed certificate */
2980
2981 #ifndef OPENSSL_NO_EC
2982 idx = s->session->peer_type;
2983 if (idx == SSL_PKEY_ECC) {
2984 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
2985 /* check failed */
2986 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2987 goto f_err;
2988 } else {
2989 return 1;
2990 }
2991 } else if (alg_a & SSL_aECDSA) {
2992 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2993 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2994 goto f_err;
2995 }
2996 #endif
2997 pkey = X509_get0_pubkey(s->session->peer);
2998 i = X509_certificate_type(s->session->peer, pkey);
2999
3000 /* Check that we have a certificate if we require one */
3001 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3002 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3003 SSL_R_MISSING_RSA_SIGNING_CERT);
3004 goto f_err;
3005 }
3006 #ifndef OPENSSL_NO_DSA
3007 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3008 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3009 SSL_R_MISSING_DSA_SIGNING_CERT);
3010 goto f_err;
3011 }
3012 #endif
3013 #ifndef OPENSSL_NO_RSA
3014 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
3015 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3016 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3017 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3018 goto f_err;
3019 }
3020 #endif
3021 #ifndef OPENSSL_NO_DH
3022 if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
3023 al = SSL_AD_INTERNAL_ERROR;
3024 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3025 goto f_err;
3026 }
3027 #endif
3028
3029 return (1);
3030 f_err:
3031 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3032 return (0);
3033 }
3034
3035 #ifndef OPENSSL_NO_NEXTPROTONEG
3036 int tls_construct_next_proto(SSL *s, WPACKET *pkt)
3037 {
3038 size_t len, padding_len;
3039 unsigned char *padding = NULL;
3040
3041 len = s->next_proto_negotiated_len;
3042 padding_len = 32 - ((len + 2) % 32);
3043
3044 if (!WPACKET_sub_memcpy_u8(pkt, s->next_proto_negotiated, len)
3045 || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
3046 SSLerr(SSL_F_TLS_CONSTRUCT_NEXT_PROTO, ERR_R_INTERNAL_ERROR);
3047 goto err;
3048 }
3049
3050 memset(padding, 0, padding_len);
3051
3052 return 1;
3053 err:
3054 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3055 return 0;
3056 }
3057 #endif
3058
3059 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3060 {
3061 int i = 0;
3062 #ifndef OPENSSL_NO_ENGINE
3063 if (s->ctx->client_cert_engine) {
3064 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3065 SSL_get_client_CA_list(s),
3066 px509, ppkey, NULL, NULL, NULL);
3067 if (i != 0)
3068 return i;
3069 }
3070 #endif
3071 if (s->ctx->client_cert_cb)
3072 i = s->ctx->client_cert_cb(s, px509, ppkey);
3073 return i;
3074 }
3075
3076 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt)
3077 {
3078 int i;
3079 size_t totlen = 0, len, maxlen;
3080 int empty_reneg_info_scsv = !s->renegotiate;
3081 /* Set disabled masks for this session */
3082 ssl_set_client_disabled(s);
3083
3084 if (sk == NULL)
3085 return (0);
3086
3087 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
3088 # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
3089 # error Max cipher length too short
3090 # endif
3091 /*
3092 * Some servers hang if client hello > 256 bytes as hack workaround
3093 * chop number of supported ciphers to keep it well below this if we
3094 * use TLS v1.2
3095 */
3096 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3097 maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
3098 else
3099 #endif
3100 /* Maximum length that can be stored in 2 bytes. Length must be even */
3101 maxlen = 0xfffe;
3102
3103 if (empty_reneg_info_scsv)
3104 maxlen -= 2;
3105 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
3106 maxlen -= 2;
3107
3108 for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
3109 const SSL_CIPHER *c;
3110
3111 c = sk_SSL_CIPHER_value(sk, i);
3112 /* Skip disabled ciphers */
3113 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3114 continue;
3115
3116 if (!s->method->put_cipher_by_char(c, pkt, &len)) {
3117 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
3118 return 0;
3119 }
3120
3121 totlen += len;
3122 }
3123
3124 if (totlen == 0) {
3125 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, SSL_R_NO_CIPHERS_AVAILABLE);
3126 return 0;
3127 }
3128
3129 if (totlen != 0) {
3130 if (empty_reneg_info_scsv) {
3131 static SSL_CIPHER scsv = {
3132 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3133 };
3134 if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
3135 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
3136 return 0;
3137 }
3138 }
3139 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3140 static SSL_CIPHER scsv = {
3141 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3142 };
3143 if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) {
3144 SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR);
3145 return 0;
3146 }
3147 }
3148 }
3149
3150 return 1;
3151 }