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