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