]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/statem/statem_clnt.c
Update to SHA256 for TSA signing digest.
[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
855 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
856 SSLerr(SSL_F_SSL_SET_VERSION,
857 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
858 return 0;
859 }
860
861 if (s->version == SSL3_VERSION && FIPS_mode()) {
862 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
863 return 0;
864 }
865
866 } else if (s->method->version == DTLS_ANY_VERSION) {
867 /* Determine which DTLS version to use */
868 /* If DTLS 1.2 disabled correct the version number */
869 if (options & SSL_OP_NO_DTLSv1_2) {
870 if (tls1_suiteb(s)) {
871 SSLerr(SSL_F_SSL_SET_VERSION,
872 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
873 return 0;
874 }
875 /*
876 * Disabling all versions is silly: return an error.
877 */
878 if (options & SSL_OP_NO_DTLSv1) {
879 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
880 return 0;
881 }
882 /*
883 * Update method so we don't use any DTLS 1.2 features.
884 */
885 s->method = DTLSv1_client_method();
886 s->version = DTLS1_VERSION;
887 } else {
888 /*
889 * We only support one version: update method
890 */
891 if (options & SSL_OP_NO_DTLSv1)
892 s->method = DTLSv1_2_client_method();
893 s->version = DTLS1_2_VERSION;
894 }
895 }
896
897 s->client_version = s->version;
898
899 return 1;
900}
901
b9908bf9 902int tls_construct_client_hello(SSL *s)
0f113f3e
MC
903{
904 unsigned char *buf;
905 unsigned char *p, *d;
906 int i;
907 unsigned long l;
908 int al = 0;
09b6c2ef 909#ifndef OPENSSL_NO_COMP
0f113f3e
MC
910 int j;
911 SSL_COMP *comp;
912#endif
b9908bf9 913 SSL_SESSION *sess = s->session;
0f113f3e
MC
914
915 buf = (unsigned char *)s->init_buf->data;
0f113f3e 916
b9908bf9
MC
917 /* Work out what SSL/TLS/DTLS version to use */
918 if (ssl_set_version(s) == 0)
919 goto err;
0f113f3e 920
b9908bf9 921 if ((sess == NULL) || (sess->ssl_version != s->version) ||
0f113f3e 922 /*
b9908bf9
MC
923 * In the case of EAP-FAST, we can have a pre-shared
924 * "ticket" without a session ID.
0f113f3e 925 */
b9908bf9
MC
926 (!sess->session_id_length && !sess->tlsext_tick) ||
927 (sess->not_resumable)) {
928 if (!ssl_get_new_session(s, 0))
e1b568dd 929 goto err;
b9908bf9
MC
930 }
931 /* else use the pre-loaded session */
0f113f3e 932
b9908bf9 933 p = s->s3->client_random;
0f113f3e 934
b9908bf9
MC
935 /*
936 * for DTLS if client_random is initialized, reuse it, we are
937 * required to use same upon reply to HelloVerify
938 */
939 if (SSL_IS_DTLS(s)) {
940 size_t idx;
941 i = 1;
942 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
943 if (p[idx]) {
944 i = 0;
945 break;
0f113f3e 946 }
0f113f3e 947 }
b9908bf9
MC
948 } else
949 i = 1;
0f113f3e 950
b9908bf9
MC
951 if (i && ssl_fill_hello_random(s, 0, p,
952 sizeof(s->s3->client_random)) <= 0)
953 goto err;
954
955 /* Do the message type and length last */
956 d = p = ssl_handshake_start(s);
957
958 /*-
959 * version indicates the negotiated version: for example from
960 * an SSLv2/v3 compatible client hello). The client_version
961 * field is the maximum version we permit and it is also
962 * used in RSA encrypted premaster secrets. Some servers can
963 * choke if we initially report a higher version then
964 * renegotiate to a lower one in the premaster secret. This
965 * didn't happen with TLS 1.0 as most servers supported it
966 * but it can with TLS 1.1 or later if the server only supports
967 * 1.0.
968 *
969 * Possible scenario with previous logic:
970 * 1. Client hello indicates TLS 1.2
971 * 2. Server hello says TLS 1.0
972 * 3. RSA encrypted premaster secret uses 1.2.
973 * 4. Handhaked proceeds using TLS 1.0.
974 * 5. Server sends hello request to renegotiate.
975 * 6. Client hello indicates TLS v1.0 as we now
976 * know that is maximum server supports.
977 * 7. Server chokes on RSA encrypted premaster secret
978 * containing version 1.0.
979 *
980 * For interoperability it should be OK to always use the
981 * maximum version we support in client hello and then rely
982 * on the checking of version to ensure the servers isn't
983 * being inconsistent: for example initially negotiating with
984 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
985 * client_version in client hello and not resetting it to
986 * the negotiated version.
987 */
988 *(p++) = s->client_version >> 8;
989 *(p++) = s->client_version & 0xff;
990
991 /* Random stuff */
992 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
993 p += SSL3_RANDOM_SIZE;
994
995 /* Session ID */
996 if (s->new_session)
997 i = 0;
998 else
999 i = s->session->session_id_length;
1000 *(p++) = i;
1001 if (i != 0) {
1002 if (i > (int)sizeof(s->session->session_id)) {
1003 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1004 goto err;
0f113f3e 1005 }
b9908bf9
MC
1006 memcpy(p, s->session->session_id, i);
1007 p += i;
1008 }
0f113f3e 1009
b9908bf9
MC
1010 /* cookie stuff for DTLS */
1011 if (SSL_IS_DTLS(s)) {
1012 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
1013 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
1014 goto err;
1015 }
b9908bf9
MC
1016 *(p++) = s->d1->cookie_len;
1017 memcpy(p, s->d1->cookie, s->d1->cookie_len);
1018 p += s->d1->cookie_len;
1019 }
1020
1021 /* Ciphers supported */
1022 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
1023 if (i == 0) {
1024 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
1025 goto err;
1026 }
800e1cd9 1027#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
b9908bf9
MC
1028 /*
1029 * Some servers hang if client hello > 256 bytes as hack workaround
1030 * chop number of supported ciphers to keep it well below this if we
1031 * use TLS v1.2
1032 */
1033 if (TLS1_get_version(s) >= TLS1_2_VERSION
1034 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
1035 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
0f113f3e 1036#endif
b9908bf9
MC
1037 s2n(i, p);
1038 p += i;
0f113f3e 1039
b9908bf9 1040 /* COMPRESSION */
09b6c2ef 1041#ifdef OPENSSL_NO_COMP
b9908bf9 1042 *(p++) = 1;
09b6c2ef 1043#else
566dda07 1044
b9908bf9
MC
1045 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
1046 j = 0;
1047 else
1048 j = sk_SSL_COMP_num(s->ctx->comp_methods);
1049 *(p++) = 1 + j;
1050 for (i = 0; i < j; i++) {
1051 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
1052 *(p++) = comp->id;
1053 }
09b6c2ef 1054#endif
b9908bf9 1055 *(p++) = 0; /* Add the NULL method */
761772d7 1056
b9908bf9
MC
1057 /* TLS extensions */
1058 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
1059 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1060 goto err;
1061 }
1062 if ((p =
1063 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1064 &al)) == NULL) {
1065 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1066 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1067 goto err;
1068 }
0f113f3e 1069
b9908bf9
MC
1070 l = p - d;
1071 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
1072 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1073 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1074 goto err;
0f113f3e
MC
1075 }
1076
b9908bf9 1077 return 1;
0f113f3e 1078 err:
fe3a3291 1079 ossl_statem_set_error(s);
b9908bf9 1080 return 0;
0f113f3e 1081}
d02b48c6 1082
be3583fa 1083MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
8ba708e5
MC
1084{
1085 int al;
1086 unsigned int cookie_len;
1087 PACKET cookiepkt;
1088
1089 if (!PACKET_forward(pkt, 2)
1090 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1091 al = SSL_AD_DECODE_ERROR;
1092 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1093 goto f_err;
1094 }
1095
1096 cookie_len = PACKET_remaining(&cookiepkt);
1097 if (cookie_len > sizeof(s->d1->cookie)) {
1098 al = SSL_AD_ILLEGAL_PARAMETER;
1099 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
1100 goto f_err;
1101 }
1102
1103 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1104 al = SSL_AD_DECODE_ERROR;
1105 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1106 goto f_err;
1107 }
1108 s->d1->cookie_len = cookie_len;
1109
1110 return MSG_PROCESS_FINISHED_READING;
1111 f_err:
1112 ssl3_send_alert(s, SSL3_AL_FATAL, al);
fe3a3291 1113 ossl_statem_set_error(s);
8ba708e5
MC
1114 return MSG_PROCESS_ERROR;
1115}
1116
be3583fa 1117MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
b9908bf9
MC
1118{
1119 STACK_OF(SSL_CIPHER) *sk;
1120 const SSL_CIPHER *c;
73999b62 1121 PACKET session_id;
b9908bf9
MC
1122 size_t session_id_len;
1123 unsigned char *cipherchars;
1124 int i, al = SSL_AD_INTERNAL_ERROR;
1125 unsigned int compression;
1126#ifndef OPENSSL_NO_COMP
1127 SSL_COMP *comp;
1128#endif
1129
13c9bb3e 1130 if (s->method->version == TLS_ANY_VERSION) {
50932c4a
MC
1131 unsigned int sversion;
1132
73999b62 1133 if (!PACKET_get_net_2(pkt, &sversion)) {
50932c4a 1134 al = SSL_AD_DECODE_ERROR;
b9908bf9 1135 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
1136 goto f_err;
1137 }
13c9bb3e
MC
1138
1139#if TLS_MAX_VERSION != TLS1_2_VERSION
1140#error Code needs updating for new TLS version
1141#endif
1142#ifndef OPENSSL_NO_SSL3
1143 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
1144 if (FIPS_mode()) {
b9908bf9 1145 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
13c9bb3e 1146 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
d45ba43d
MC
1147 al = SSL_AD_PROTOCOL_VERSION;
1148 goto f_err;
13c9bb3e
MC
1149 }
1150 s->method = SSLv3_client_method();
1151 } else
1152#endif
1153 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1154 s->method = TLSv1_client_method();
1155 } else if ((sversion == TLS1_1_VERSION) &&
1156 !(s->options & SSL_OP_NO_TLSv1_1)) {
1157 s->method = TLSv1_1_client_method();
1158 } else if ((sversion == TLS1_2_VERSION) &&
1159 !(s->options & SSL_OP_NO_TLSv1_2)) {
1160 s->method = TLSv1_2_client_method();
1161 } else {
b9908bf9 1162 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
d45ba43d
MC
1163 al = SSL_AD_PROTOCOL_VERSION;
1164 goto f_err;
13c9bb3e
MC
1165 }
1166 s->session->ssl_version = s->version = s->method->version;
1167
1168 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
b9908bf9 1169 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
d45ba43d
MC
1170 al = SSL_AD_PROTOCOL_VERSION;
1171 goto f_err;
13c9bb3e
MC
1172 }
1173 } else if (s->method->version == DTLS_ANY_VERSION) {
0f113f3e 1174 /* Work out correct protocol version to use */
50932c4a
MC
1175 unsigned int hversion;
1176 int options;
1177
73999b62 1178 if (!PACKET_get_net_2(pkt, &hversion)) {
50932c4a 1179 al = SSL_AD_DECODE_ERROR;
b9908bf9 1180 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
1181 goto f_err;
1182 }
1183
1184 options = s->options;
0f113f3e
MC
1185 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1186 s->method = DTLSv1_2_client_method();
1187 else if (tls1_suiteb(s)) {
b9908bf9 1188 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1189 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1190 s->version = hversion;
1191 al = SSL_AD_PROTOCOL_VERSION;
1192 goto f_err;
1193 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1194 s->method = DTLSv1_client_method();
1195 else {
b9908bf9 1196 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
0f113f3e
MC
1197 s->version = hversion;
1198 al = SSL_AD_PROTOCOL_VERSION;
1199 goto f_err;
1200 }
7322abf5 1201 s->session->ssl_version = s->version = s->method->version;
50932c4a
MC
1202 } else {
1203 unsigned char *vers;
1204
73999b62 1205 if (!PACKET_get_bytes(pkt, &vers, 2)) {
50932c4a 1206 al = SSL_AD_DECODE_ERROR;
b9908bf9 1207 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
1208 goto f_err;
1209 }
1210 if ((vers[0] != (s->version >> 8))
1211 || (vers[1] != (s->version & 0xff))) {
b9908bf9 1212 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
50932c4a
MC
1213 s->version = (s->version & 0xff00) | vers[1];
1214 al = SSL_AD_PROTOCOL_VERSION;
1215 goto f_err;
1216 }
0f113f3e 1217 }
0f113f3e
MC
1218
1219 /* load the server hello data */
1220 /* load the server random */
73999b62 1221 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
50932c4a 1222 al = SSL_AD_DECODE_ERROR;
b9908bf9 1223 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
1224 goto f_err;
1225 }
0f113f3e
MC
1226
1227 s->hit = 0;
1228
fc5ce51d 1229 /* Get the session-id. */
73999b62 1230 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
fc5ce51d 1231 al = SSL_AD_DECODE_ERROR;
f0659bdb 1232 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
fc5ce51d
EK
1233 goto f_err;
1234 }
1235 session_id_len = PACKET_remaining(&session_id);
1236 if (session_id_len > sizeof s->session->session_id
1237 || session_id_len > SSL3_SESSION_ID_SIZE) {
0f113f3e 1238 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1239 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
0f113f3e
MC
1240 goto f_err;
1241 }
e481f9b9 1242
73999b62 1243 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
f0659bdb 1244 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
fc5ce51d
EK
1245 al = SSL_AD_DECODE_ERROR;
1246 goto f_err;
1247 }
1248
0f113f3e 1249 /*
6e3d0153
EK
1250 * Check if we can resume the session based on external pre-shared secret.
1251 * EAP-FAST (RFC 4851) supports two types of session resumption.
1252 * Resumption based on server-side state works with session IDs.
1253 * Resumption based on pre-shared Protected Access Credentials (PACs)
1254 * works by overriding the SessionTicket extension at the application
1255 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1256 * servers would honour the session ID.) Therefore, the session ID alone
1257 * is not a reliable indicator of session resumption, so we first check if
1258 * we can resume, and later peek at the next handshake message to see if the
1259 * server wants to resume.
0f113f3e 1260 */
6e3d0153
EK
1261 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1262 s->session->tlsext_tick) {
0f113f3e
MC
1263 SSL_CIPHER *pref_cipher = NULL;
1264 s->session->master_key_length = sizeof(s->session->master_key);
1265 if (s->tls_session_secret_cb(s, s->session->master_key,
1266 &s->session->master_key_length,
1267 NULL, &pref_cipher,
1268 s->tls_session_secret_cb_arg)) {
1269 s->session->cipher = pref_cipher ?
50932c4a 1270 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
6e3d0153 1271 } else {
b9908bf9 1272 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
6e3d0153
EK
1273 al = SSL_AD_INTERNAL_ERROR;
1274 goto f_err;
0f113f3e 1275 }
50932c4a
MC
1276 }
1277
fc5ce51d
EK
1278 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1279 && memcmp(PACKET_data(&session_id), s->session->session_id,
1280 session_id_len) == 0) {
0f113f3e
MC
1281 if (s->sid_ctx_length != s->session->sid_ctx_length
1282 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1283 /* actually a client application bug */
1284 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1285 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1286 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1287 goto f_err;
1288 }
1289 s->hit = 1;
6e3d0153 1290 } else {
0f113f3e 1291 /*
6e3d0153
EK
1292 * If we were trying for session-id reuse but the server
1293 * didn't echo the ID, make a new SSL_SESSION.
1294 * In the case of EAP-FAST and PAC, we do not send a session ID,
1295 * so the PAC-based session secret is always preserved. It'll be
1296 * overwritten if the server refuses resumption.
0f113f3e
MC
1297 */
1298 if (s->session->session_id_length > 0) {
1299 if (!ssl_get_new_session(s, 0)) {
1300 goto f_err;
1301 }
1302 }
50932c4a 1303
fc5ce51d
EK
1304 s->session->session_id_length = session_id_len;
1305 /* session_id_len could be 0 */
1306 memcpy(s->session->session_id, PACKET_data(&session_id),
1307 session_id_len);
0f113f3e 1308 }
fc5ce51d 1309
50932c4a 1310 c = ssl_get_cipher_by_char(s, cipherchars);
0f113f3e
MC
1311 if (c == NULL) {
1312 /* unknown cipher */
1313 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1314 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
0f113f3e
MC
1315 goto f_err;
1316 }
1317 /* Set version disabled mask now we know version */
1318 if (!SSL_USE_TLS1_2_CIPHERS(s))
4d69f9e6 1319 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
0f113f3e 1320 else
4d69f9e6 1321 s->s3->tmp.mask_ssl = 0;
2b573382
DSH
1322 /* Skip TLS v1.0 ciphersuites if SSLv3 */
1323 if ((c->algorithm_ssl & SSL_TLSV1) && s->version == SSL3_VERSION)
1324 s->s3->tmp.mask_ssl |= SSL_TLSV1;
0f113f3e
MC
1325 /*
1326 * If it is a disabled cipher we didn't send it in client hello, so
1327 * return an error.
1328 */
1329 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1330 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1331 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
0f113f3e
MC
1332 goto f_err;
1333 }
0f113f3e
MC
1334
1335 sk = ssl_get_ciphers_by_id(s);
1336 i = sk_SSL_CIPHER_find(sk, c);
1337 if (i < 0) {
1338 /* we did not say we would use this cipher */
1339 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1340 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
0f113f3e
MC
1341 goto f_err;
1342 }
1343
1344 /*
1345 * Depending on the session caching (internal/external), the cipher
1346 * and/or cipher_id values may not be set. Make sure that cipher_id is
1347 * set and use it for comparison.
1348 */
1349 if (s->session->cipher)
1350 s->session->cipher_id = s->session->cipher->id;
1351 if (s->hit && (s->session->cipher_id != c->id)) {
9e9858d1 1352 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1353 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
9e9858d1
RS
1354 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1355 goto f_err;
0f113f3e
MC
1356 }
1357 s->s3->tmp.new_cipher = c;
0f113f3e
MC
1358 /* lets get the compression algorithm */
1359 /* COMPRESSION */
73999b62 1360 if (!PACKET_get_1(pkt, &compression)) {
f0659bdb 1361 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
1362 al = SSL_AD_DECODE_ERROR;
1363 goto f_err;
1364 }
09b6c2ef 1365#ifdef OPENSSL_NO_COMP
fc5ce51d 1366 if (compression != 0) {
0f113f3e 1367 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1368 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1369 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1370 goto f_err;
1371 }
1372 /*
1373 * If compression is disabled we'd better not try to resume a session
1374 * using compression.
1375 */
1376 if (s->session->compress_meth != 0) {
b9908bf9 1377 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
0f113f3e
MC
1378 goto f_err;
1379 }
09b6c2ef 1380#else
fc5ce51d 1381 if (s->hit && compression != s->session->compress_meth) {
0f113f3e 1382 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1383 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1384 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1385 goto f_err;
1386 }
fc5ce51d 1387 if (compression == 0)
0f113f3e
MC
1388 comp = NULL;
1389 else if (!ssl_allow_compression(s)) {
1390 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1391 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
0f113f3e 1392 goto f_err;
fc5ce51d
EK
1393 } else {
1394 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1395 }
0f113f3e 1396
fc5ce51d 1397 if (compression != 0 && comp == NULL) {
0f113f3e 1398 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1399 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1400 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1401 goto f_err;
1402 } else {
1403 s->s3->tmp.new_compression = comp;
1404 }
09b6c2ef 1405#endif
761772d7 1406
0f113f3e 1407 /* TLS extensions */
73999b62 1408 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
b9908bf9 1409 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
0f113f3e
MC
1410 goto err;
1411 }
0f113f3e 1412
73999b62 1413 if (PACKET_remaining(pkt) != 0) {
0f113f3e
MC
1414 /* wrong packet length */
1415 al = SSL_AD_DECODE_ERROR;
b9908bf9 1416 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
0f113f3e
MC
1417 goto f_err;
1418 }
1419
8723588e
MC
1420#ifndef OPENSSL_NO_SCTP
1421 if (SSL_IS_DTLS(s) && s->hit) {
1422 unsigned char sctpauthkey[64];
1423 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1424
1425 /*
1426 * Add new shared key for SCTP-Auth, will be ignored if
1427 * no SCTP used.
1428 */
141eb8c6
MC
1429 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1430 sizeof(DTLS1_SCTP_AUTH_LABEL));
8723588e
MC
1431
1432 if (SSL_export_keying_material(s, sctpauthkey,
1433 sizeof(sctpauthkey),
1434 labelbuffer,
1435 sizeof(labelbuffer), NULL, 0,
1436 0) <= 0)
1437 goto err;
1438
1439 BIO_ctrl(SSL_get_wbio(s),
1440 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1441 sizeof(sctpauthkey), sctpauthkey);
1442 }
1443#endif
1444
b9908bf9 1445 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
1446 f_err:
1447 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1448 err:
fe3a3291 1449 ossl_statem_set_error(s);
b9908bf9 1450 return MSG_PROCESS_ERROR;
0f113f3e 1451}
d02b48c6 1452
be3583fa 1453MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
b9908bf9
MC
1454{
1455 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1456 unsigned long cert_list_len, cert_len;
1457 X509 *x = NULL;
1458 unsigned char *certstart, *certbytes;
1459 STACK_OF(X509) *sk = NULL;
1460 EVP_PKEY *pkey = NULL;
0f113f3e
MC
1461
1462 if ((sk = sk_X509_new_null()) == NULL) {
b9908bf9 1463 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
cc273a93 1464 goto err;
0f113f3e
MC
1465 }
1466
73999b62
MC
1467 if (!PACKET_get_net_3(pkt, &cert_list_len)
1468 || PACKET_remaining(pkt) != cert_list_len) {
0f113f3e 1469 al = SSL_AD_DECODE_ERROR;
b9908bf9 1470 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1471 goto f_err;
1472 }
73999b62
MC
1473 while (PACKET_remaining(pkt)) {
1474 if (!PACKET_get_net_3(pkt, &cert_len)
1475 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
0f113f3e 1476 al = SSL_AD_DECODE_ERROR;
b9908bf9 1477 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
1478 SSL_R_CERT_LENGTH_MISMATCH);
1479 goto f_err;
1480 }
1481
df758a85
MC
1482 certstart = certbytes;
1483 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
0f113f3e
MC
1484 if (x == NULL) {
1485 al = SSL_AD_BAD_CERTIFICATE;
b9908bf9 1486 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
0f113f3e
MC
1487 goto f_err;
1488 }
df758a85 1489 if (certbytes != (certstart + cert_len)) {
0f113f3e 1490 al = SSL_AD_DECODE_ERROR;
b9908bf9 1491 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
1492 SSL_R_CERT_LENGTH_MISMATCH);
1493 goto f_err;
1494 }
1495 if (!sk_X509_push(sk, x)) {
b9908bf9 1496 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
cc273a93 1497 goto err;
0f113f3e
MC
1498 }
1499 x = NULL;
0f113f3e
MC
1500 }
1501
1502 i = ssl_verify_cert_chain(s, sk);
55a9a16f 1503 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
0f113f3e 1504 al = ssl_verify_alarm_type(s->verify_result);
b9908bf9 1505 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
1506 SSL_R_CERTIFICATE_VERIFY_FAILED);
1507 goto f_err;
1508 }
1509 ERR_clear_error(); /* but we keep s->verify_result */
1510 if (i > 1) {
b9908bf9 1511 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
0f113f3e
MC
1512 al = SSL_AD_HANDSHAKE_FAILURE;
1513 goto f_err;
1514 }
1515
c34b0f99 1516 s->session->peer_chain = sk;
0f113f3e
MC
1517 /*
1518 * Inconsistency alert: cert_chain does include the peer's certificate,
1519 * which we don't include in s3_srvr.c
1520 */
1521 x = sk_X509_value(sk, 0);
1522 sk = NULL;
1523 /*
1524 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1525 */
1526
1527 pkey = X509_get_pubkey(x);
1528
55a9a16f 1529 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
0f113f3e
MC
1530 x = NULL;
1531 al = SSL3_AL_FATAL;
b9908bf9 1532 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
1533 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1534 goto f_err;
1535 }
1536
1537 i = ssl_cert_type(x, pkey);
55a9a16f 1538 if (i < 0) {
0f113f3e
MC
1539 x = NULL;
1540 al = SSL3_AL_FATAL;
b9908bf9 1541 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
0f113f3e
MC
1542 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1543 goto f_err;
1544 }
1545
55a9a16f 1546 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
e44380a9
DB
1547 if (exp_idx >= 0 && i != exp_idx
1548 && (exp_idx != SSL_PKEY_GOST_EC ||
1549 (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
1550 && i != SSL_PKEY_GOST01))) {
55a9a16f
MC
1551 x = NULL;
1552 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1553 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
55a9a16f
MC
1554 SSL_R_WRONG_CERTIFICATE_TYPE);
1555 goto f_err;
0f113f3e 1556 }
a273c6ee 1557 s->session->peer_type = i;
55a9a16f
MC
1558
1559 X509_free(s->session->peer);
05f0fb9f 1560 X509_up_ref(x);
55a9a16f 1561 s->session->peer = x;
0f113f3e
MC
1562 s->session->verify_result = s->verify_result;
1563
1564 x = NULL;
b9908bf9 1565 ret = MSG_PROCESS_CONTINUE_READING;
66696478
RS
1566 goto done;
1567
0f113f3e 1568 f_err:
66696478 1569 ssl3_send_alert(s, SSL3_AL_FATAL, al);
cc273a93 1570 err:
fe3a3291 1571 ossl_statem_set_error(s);
66696478 1572 done:
0f113f3e
MC
1573 EVP_PKEY_free(pkey);
1574 X509_free(x);
1575 sk_X509_pop_free(sk, X509_free);
b9908bf9 1576 return ret;
0f113f3e 1577}
d02b48c6 1578
be3583fa 1579MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
b9908bf9 1580{
6e59a892 1581 EVP_MD_CTX *md_ctx;
192e4bbb 1582 int al, j;
b9908bf9
MC
1583 long alg_k, alg_a;
1584 EVP_PKEY *pkey = NULL;
1585 const EVP_MD *md = NULL;
1586#ifndef OPENSSL_NO_RSA
1587 RSA *rsa = NULL;
1588#endif
b9908bf9 1589#ifndef OPENSSL_NO_EC
61dd9f7a 1590 EVP_PKEY_CTX *pctx = NULL;
b9908bf9 1591#endif
73999b62 1592 PACKET save_param_start, signature;
b9908bf9 1593
bfb0641f 1594 md_ctx = EVP_MD_CTX_new();
6e59a892
RL
1595 if (md_ctx == NULL) {
1596 al = SSL_AD_INTERNAL_ERROR;
1597 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1598 goto f_err;
1599 }
b9908bf9
MC
1600
1601 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1602
73999b62 1603 save_param_start = *pkt;
8d92c1f8 1604
fb79abe3 1605#if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
61dd9f7a
DSH
1606 EVP_PKEY_free(s->s3->peer_tmp);
1607 s->s3->peer_tmp = NULL;
d02b48c6 1608#endif
8d92c1f8 1609
0f113f3e 1610 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
d02b48c6 1611
0f113f3e 1612 al = SSL_AD_DECODE_ERROR;
f2be92b9 1613
ddac1974 1614#ifndef OPENSSL_NO_PSK
7689082b
DSH
1615 /* PSK ciphersuites are preceded by an identity hint */
1616 if (alg_k & SSL_PSK) {
32942870 1617 PACKET psk_identity_hint;
73999b62 1618 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
f0659bdb 1619 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1620 goto f_err;
1621 }
0f113f3e
MC
1622
1623 /*
1624 * Store PSK identity hint for later use, hint is used in
1625 * ssl3_send_client_key_exchange. Assume that the maximum length of
1626 * a PSK identity hint can be as long as the maximum length of a PSK
1627 * identity.
1628 */
32942870 1629 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
0f113f3e 1630 al = SSL_AD_HANDSHAKE_FAILURE;
b9908bf9 1631 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
0f113f3e
MC
1632 goto f_err;
1633 }
8098fc56 1634
558ceae7
DSH
1635 if (PACKET_remaining(&psk_identity_hint) == 0) {
1636 OPENSSL_free(s->session->psk_identity_hint);
1637 s->session->psk_identity_hint = NULL;
1638 } else if (!PACKET_strndup(&psk_identity_hint,
32942870
EK
1639 &s->session->psk_identity_hint)) {
1640 al = SSL_AD_INTERNAL_ERROR;
1641 goto f_err;
0f113f3e 1642 }
7689082b
DSH
1643 }
1644
1645 /* Nothing else to do for plain PSK or RSAPSK */
1646 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
0f113f3e
MC
1647 } else
1648#endif /* !OPENSSL_NO_PSK */
bbf431cb
MC
1649 /*
1650 * Dummy "if" to ensure sane C code in the event of various OPENSSL_NO_*
1651 * options
1652 */
1653 if (0) {
1654 }
edc032b5 1655#ifndef OPENSSL_NO_SRP
bbf431cb 1656 else if (alg_k & SSL_kSRP) {
32942870 1657 PACKET prime, generator, salt, server_pub;
73999b62
MC
1658 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1659 || !PACKET_get_length_prefixed_2(pkt, &generator)
1660 || !PACKET_get_length_prefixed_1(pkt, &salt)
1661 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
f0659bdb 1662 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1663 goto f_err;
1664 }
0f113f3e 1665
32942870
EK
1666 if ((s->srp_ctx.N =
1667 BN_bin2bn(PACKET_data(&prime),
1668 PACKET_remaining(&prime), NULL)) == NULL
1669 || (s->srp_ctx.g =
1670 BN_bin2bn(PACKET_data(&generator),
1671 PACKET_remaining(&generator), NULL)) == NULL
1672 || (s->srp_ctx.s =
1673 BN_bin2bn(PACKET_data(&salt),
1674 PACKET_remaining(&salt), NULL)) == NULL
1675 || (s->srp_ctx.B =
1676 BN_bin2bn(PACKET_data(&server_pub),
1677 PACKET_remaining(&server_pub), NULL)) == NULL) {
f0659bdb 1678 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
0f113f3e
MC
1679 goto err;
1680 }
0f113f3e
MC
1681
1682 if (!srp_verify_server_param(s, &al)) {
b9908bf9 1683 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
0f113f3e
MC
1684 goto f_err;
1685 }
0989790b 1686
edc032b5 1687/* We must check if there is a certificate */
8df53b7a 1688 if (alg_a & (SSL_aRSA|SSL_aDSS))
a273c6ee 1689 pkey = X509_get_pubkey(s->session->peer);
0f113f3e 1690 }
361a1191 1691#endif /* !OPENSSL_NO_SRP */
bc36ee62 1692#ifndef OPENSSL_NO_DH
7689082b 1693 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
32942870 1694 PACKET prime, generator, pub_key;
0f113f3e 1695
fb79abe3
DSH
1696 DH *dh;
1697
73999b62
MC
1698 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1699 || !PACKET_get_length_prefixed_2(pkt, &generator)
1700 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
f0659bdb 1701 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1702 goto f_err;
1703 }
0f113f3e 1704
fb79abe3
DSH
1705 s->s3->peer_tmp = EVP_PKEY_new();
1706 dh = DH_new();
1707
1708 if (s->s3->peer_tmp == NULL || dh == NULL) {
1709 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1710 DH_free(dh);
1711 goto err;
1712 }
1713
1714 if (EVP_PKEY_assign_DH(s->s3->peer_tmp, dh) == 0) {
1715 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1716 DH_free(dh);
0f113f3e
MC
1717 goto err;
1718 }
0f113f3e 1719
32942870
EK
1720 if ((dh->p = BN_bin2bn(PACKET_data(&prime),
1721 PACKET_remaining(&prime), NULL)) == NULL
1722 || (dh->g = BN_bin2bn(PACKET_data(&generator),
1723 PACKET_remaining(&generator), NULL)) == NULL
1724 || (dh->pub_key =
1725 BN_bin2bn(PACKET_data(&pub_key),
1726 PACKET_remaining(&pub_key), NULL)) == NULL) {
f0659bdb 1727 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
0f113f3e
MC
1728 goto err;
1729 }
0f113f3e 1730
32942870 1731 if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
f0659bdb 1732 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
61e72d76
GL
1733 goto f_err;
1734 }
1735
0f113f3e
MC
1736 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1737 al = SSL_AD_HANDSHAKE_FAILURE;
b9908bf9 1738 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
0f113f3e
MC
1739 goto f_err;
1740 }
8df53b7a 1741 if (alg_a & (SSL_aRSA|SSL_aDSS))
a273c6ee 1742 pkey = X509_get_pubkey(s->session->peer);
0f113f3e 1743 /* else anonymous DH, so no certificate or pkey. */
0f113f3e
MC
1744 }
1745#endif /* !OPENSSL_NO_DH */
ea262260 1746
10bf4fc2 1747#ifndef OPENSSL_NO_EC
7689082b 1748 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
32942870
EK
1749 PACKET encoded_pt;
1750 unsigned char *ecparams;
61dd9f7a 1751 int curve_nid;
0f113f3e 1752
0f113f3e
MC
1753 /*
1754 * Extract elliptic curve parameters and the server's ephemeral ECDH
32942870 1755 * public key. For now we only support named (not generic) curves and
8098fc56 1756 * ECParameters in this case is just three bytes.
0f113f3e 1757 */
73999b62 1758 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
f0659bdb 1759 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
0f113f3e
MC
1760 goto f_err;
1761 }
1762 /*
1763 * Check curve is one of our preferences, if not server has sent an
1764 * invalid curve. ECParameters is 3 bytes.
1765 */
32942870 1766 if (!tls1_check_curve(s, ecparams, 3)) {
f0659bdb 1767 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
0f113f3e
MC
1768 goto f_err;
1769 }
1770
61dd9f7a
DSH
1771 curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2));
1772 if (curve_nid == 0) {
0f113f3e 1773 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 1774 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
0f113f3e
MC
1775 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1776 goto f_err;
1777 }
1778
61dd9f7a
DSH
1779 /* Set up EVP_PKEY with named curve as parameters */
1780 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
1781 if (pctx == NULL
1782 || EVP_PKEY_paramgen_init(pctx) <= 0
1783 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
1784 || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
1785 al = SSL_AD_INTERNAL_ERROR;
1786 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1787 goto f_err;
0f113f3e 1788 }
61dd9f7a
DSH
1789 EVP_PKEY_CTX_free(pctx);
1790 pctx = NULL;
0f113f3e 1791
73999b62 1792 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
f0659bdb 1793 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
8098fc56
MC
1794 goto f_err;
1795 }
0f113f3e 1796
61dd9f7a
DSH
1797 if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(s->s3->peer_tmp),
1798 PACKET_data(&encoded_pt),
cae41364 1799 PACKET_remaining(&encoded_pt), NULL) == 0) {
f0659bdb 1800 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
0f113f3e
MC
1801 goto f_err;
1802 }
0f113f3e
MC
1803
1804 /*
1805 * The ECC/TLS specification does not mention the use of DSA to sign
1806 * ECParameters in the server key exchange message. We do support RSA
1807 * and ECDSA.
1808 */
1809 if (0) ;
1810# ifndef OPENSSL_NO_RSA
1811 else if (alg_a & SSL_aRSA)
a273c6ee 1812 pkey = X509_get_pubkey(s->session->peer);
0f113f3e 1813# endif
10bf4fc2 1814# ifndef OPENSSL_NO_EC
0f113f3e 1815 else if (alg_a & SSL_aECDSA)
a273c6ee 1816 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1817# endif
1818 /* else anonymous ECDH, so no certificate or pkey. */
0f113f3e
MC
1819 } else if (alg_k) {
1820 al = SSL_AD_UNEXPECTED_MESSAGE;
b9908bf9 1821 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
0f113f3e
MC
1822 goto f_err;
1823 }
10bf4fc2 1824#endif /* !OPENSSL_NO_EC */
0f113f3e 1825
0f113f3e
MC
1826 /* if it was signed, check the signature */
1827 if (pkey != NULL) {
32942870
EK
1828 PACKET params;
1829 /*
1830 * |pkt| now points to the beginning of the signature, so the difference
1831 * equals the length of the parameters.
1832 */
1833 if (!PACKET_get_sub_packet(&save_param_start, &params,
1834 PACKET_remaining(&save_param_start) -
73999b62 1835 PACKET_remaining(pkt))) {
32942870 1836 al = SSL_AD_INTERNAL_ERROR;
f0659bdb 1837 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
32942870
EK
1838 goto f_err;
1839 }
1840
0f113f3e 1841 if (SSL_USE_SIGALGS(s)) {
32942870 1842 unsigned char *sigalgs;
0f113f3e 1843 int rv;
73999b62 1844 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
f0659bdb 1845 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
0f113f3e
MC
1846 goto f_err;
1847 }
32942870 1848 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
0f113f3e
MC
1849 if (rv == -1)
1850 goto err;
1851 else if (rv == 0) {
1852 goto f_err;
1853 }
a2f9200f 1854#ifdef SSL_DEBUG
0f113f3e
MC
1855 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1856#endif
192e4bbb
DSH
1857 } else if (pkey->type == EVP_PKEY_RSA) {
1858 md = EVP_md5_sha1();
32942870 1859 } else {
0f113f3e 1860 md = EVP_sha1();
32942870 1861 }
0f113f3e 1862
73999b62
MC
1863 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1864 || PACKET_remaining(pkt) != 0) {
f0659bdb 1865 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1866 goto f_err;
1867 }
0f113f3e 1868 j = EVP_PKEY_size(pkey);
8098fc56 1869 if (j < 0) {
b9908bf9 1870 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
8098fc56
MC
1871 goto f_err;
1872 }
0f113f3e
MC
1873
1874 /*
8098fc56 1875 * Check signature length
0f113f3e 1876 */
32942870 1877 if (PACKET_remaining(&signature) > (size_t)j) {
0f113f3e 1878 /* wrong packet length */
b9908bf9 1879 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
0f113f3e
MC
1880 goto f_err;
1881 }
6e59a892
RL
1882 if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0
1883 || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
192e4bbb 1884 SSL3_RANDOM_SIZE) <= 0
6e59a892 1885 || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
192e4bbb 1886 SSL3_RANDOM_SIZE) <= 0
6e59a892 1887 || EVP_VerifyUpdate(md_ctx, PACKET_data(&params),
192e4bbb
DSH
1888 PACKET_remaining(&params)) <= 0) {
1889 al = SSL_AD_INTERNAL_ERROR;
1890 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
1891 goto f_err;
1892 }
6e59a892 1893 if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
192e4bbb
DSH
1894 PACKET_remaining(&signature), pkey) <= 0) {
1895 /* bad signature */
1896 al = SSL_AD_DECRYPT_ERROR;
1897 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1898 goto f_err;
0f113f3e
MC
1899 }
1900 } else {
7689082b
DSH
1901 /* aNULL, aSRP or PSK do not need public keys */
1902 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
0f113f3e
MC
1903 /* Might be wrong key type, check it */
1904 if (ssl3_check_cert_and_algorithm(s))
1905 /* Otherwise this shouldn't happen */
b9908bf9 1906 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
1907 goto err;
1908 }
1909 /* still data left over */
73999b62 1910 if (PACKET_remaining(pkt) != 0) {
b9908bf9 1911 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
0f113f3e
MC
1912 goto f_err;
1913 }
1914 }
1915 EVP_PKEY_free(pkey);
bfb0641f 1916 EVP_MD_CTX_free(md_ctx);
b9908bf9 1917 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
1918 f_err:
1919 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1920 err:
1921 EVP_PKEY_free(pkey);
bc36ee62 1922#ifndef OPENSSL_NO_RSA
d6407083 1923 RSA_free(rsa);
6b521df3 1924#endif
10bf4fc2 1925#ifndef OPENSSL_NO_EC
61dd9f7a 1926 EVP_PKEY_CTX_free(pctx);
6b521df3 1927#endif
bfb0641f 1928 EVP_MD_CTX_free(md_ctx);
fe3a3291 1929 ossl_statem_set_error(s);
b9908bf9 1930 return MSG_PROCESS_ERROR;
0f113f3e 1931}
d02b48c6 1932
be3583fa 1933MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
b9908bf9
MC
1934{
1935 int ret = MSG_PROCESS_ERROR;
1936 unsigned int list_len, ctype_num, i, name_len;
1937 X509_NAME *xn = NULL;
1938 unsigned char *data;
1939 unsigned char *namestart, *namebytes;
1940 STACK_OF(X509_NAME) *ca_sk = NULL;
0f113f3e
MC
1941
1942 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
b9908bf9 1943 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1944 goto err;
1945 }
1946
1947 /* get the certificate types */
73999b62
MC
1948 if (!PACKET_get_1(pkt, &ctype_num)
1949 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
ac112332 1950 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 1951 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
ac112332
MC
1952 goto err;
1953 }
b548a1f1
RS
1954 OPENSSL_free(s->cert->ctypes);
1955 s->cert->ctypes = NULL;
0f113f3e
MC
1956 if (ctype_num > SSL3_CT_NUMBER) {
1957 /* If we exceed static buffer copy all to cert structure */
1958 s->cert->ctypes = OPENSSL_malloc(ctype_num);
1959 if (s->cert->ctypes == NULL) {
b9908bf9 1960 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1961 goto err;
1962 }
ac112332 1963 memcpy(s->cert->ctypes, data, ctype_num);
0f113f3e
MC
1964 s->cert->ctype_num = (size_t)ctype_num;
1965 ctype_num = SSL3_CT_NUMBER;
1966 }
1967 for (i = 0; i < ctype_num; i++)
ac112332
MC
1968 s->s3->tmp.ctype[i] = data[i];
1969
0f113f3e 1970 if (SSL_USE_SIGALGS(s)) {
73999b62
MC
1971 if (!PACKET_get_net_2(pkt, &list_len)
1972 || !PACKET_get_bytes(pkt, &data, list_len)) {
0f113f3e 1973 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9
MC
1974 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
1975 SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1976 goto err;
1977 }
ac112332 1978
0f113f3e
MC
1979 /* Clear certificate digests and validity flags */
1980 for (i = 0; i < SSL_PKEY_NUM; i++) {
d376e57d 1981 s->s3->tmp.md[i] = NULL;
6383d316 1982 s->s3->tmp.valid_flags[i] = 0;
0f113f3e 1983 }
ac112332 1984 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
0f113f3e 1985 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 1986 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
0f113f3e
MC
1987 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1988 goto err;
1989 }
1990 if (!tls1_process_sigalgs(s)) {
1991 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 1992 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1993 goto err;
1994 }
a0f63828
DSH
1995 } else {
1996 ssl_set_default_md(s);
0f113f3e
MC
1997 }
1998
1999 /* get the CA RDNs */
73999b62
MC
2000 if (!PACKET_get_net_2(pkt, &list_len)
2001 || PACKET_remaining(pkt) != list_len) {
0f113f3e 2002 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2003 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2004 goto err;
2005 }
2006
73999b62
MC
2007 while (PACKET_remaining(pkt)) {
2008 if (!PACKET_get_net_2(pkt, &name_len)
2009 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
0f113f3e 2010 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9
MC
2011 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2012 SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2013 goto err;
2014 }
2015
ac112332 2016 namestart = namebytes;
0f113f3e 2017
ac112332
MC
2018 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2019 name_len)) == NULL) {
3c33c6f6 2020 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2021 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
3c33c6f6 2022 goto err;
0f113f3e
MC
2023 }
2024
ac112332 2025 if (namebytes != (namestart + name_len)) {
0f113f3e 2026 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2027 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
0f113f3e
MC
2028 SSL_R_CA_DN_LENGTH_MISMATCH);
2029 goto err;
2030 }
2031 if (!sk_X509_NAME_push(ca_sk, xn)) {
b9908bf9 2032 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2033 goto err;
2034 }
0f113f3e
MC
2035 }
2036
0f113f3e
MC
2037 /* we should setup a certificate to return.... */
2038 s->s3->tmp.cert_req = 1;
2039 s->s3->tmp.ctype_num = ctype_num;
222561fe 2040 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
0f113f3e
MC
2041 s->s3->tmp.ca_names = ca_sk;
2042 ca_sk = NULL;
2043
b9908bf9 2044 ret = MSG_PROCESS_CONTINUE_READING;
cc273a93 2045 goto done;
0f113f3e 2046 err:
fe3a3291 2047 ossl_statem_set_error(s);
cc273a93 2048 done:
222561fe 2049 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
b9908bf9 2050 return ret;
0f113f3e
MC
2051}
2052
2053static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
dfeab068 2054{
0f113f3e 2055 return (X509_NAME_cmp(*a, *b));
dfeab068 2056}
dfeab068 2057
be3583fa 2058MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
b9908bf9
MC
2059{
2060 int al;
2061 unsigned int ticklen;
2062 unsigned long ticket_lifetime_hint;
b9908bf9 2063
73999b62
MC
2064 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2065 || !PACKET_get_net_2(pkt, &ticklen)
2066 || PACKET_remaining(pkt) != ticklen) {
e711da71 2067 al = SSL_AD_DECODE_ERROR;
f0659bdb 2068 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
e711da71
EK
2069 goto f_err;
2070 }
2071
2072 /* Server is allowed to change its mind and send an empty ticket. */
2073 if (ticklen == 0)
c9de4a20 2074 return MSG_PROCESS_CONTINUE_READING;
e711da71 2075
98ece4ee
MC
2076 if (s->session->session_id_length > 0) {
2077 int i = s->session_ctx->session_cache_mode;
2078 SSL_SESSION *new_sess;
2079 /*
2080 * We reused an existing session, so we need to replace it with a new
2081 * one
2082 */
2083 if (i & SSL_SESS_CACHE_CLIENT) {
2084 /*
2085 * Remove the old session from the cache
2086 */
2087 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2088 if (s->session_ctx->remove_session_cb != NULL)
2089 s->session_ctx->remove_session_cb(s->session_ctx,
2090 s->session);
2091 } else {
2092 /* We carry on if this fails */
2093 SSL_CTX_remove_session(s->session_ctx, s->session);
2094 }
2095 }
2096
2097 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2098 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 2099 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
98ece4ee
MC
2100 goto f_err;
2101 }
2102
2103 SSL_SESSION_free(s->session);
2104 s->session = new_sess;
2105 }
2106
b548a1f1
RS
2107 OPENSSL_free(s->session->tlsext_tick);
2108 s->session->tlsext_ticklen = 0;
e711da71 2109
0f113f3e 2110 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
a71edf3b 2111 if (s->session->tlsext_tick == NULL) {
b9908bf9 2112 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2113 goto err;
2114 }
73999b62 2115 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
561e12bb 2116 al = SSL_AD_DECODE_ERROR;
b9908bf9 2117 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
561e12bb
MC
2118 goto f_err;
2119 }
e711da71
EK
2120
2121 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
0f113f3e
MC
2122 s->session->tlsext_ticklen = ticklen;
2123 /*
2124 * There are two ways to detect a resumed ticket session. One is to set
2125 * an appropriate session ID and then the server must return a match in
2126 * ServerHello. This allows the normal client session ID matching to work
2127 * and we know much earlier that the ticket has been accepted. The
2128 * other way is to set zero length session ID when the ticket is
2129 * presented and rely on the handshake to determine session resumption.
2130 * We choose the former approach because this fits in with assumptions
2131 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2132 * SHA256 is disabled) hash of the ticket.
2133 */
561e12bb 2134 EVP_Digest(s->session->tlsext_tick, ticklen,
0f113f3e 2135 s->session->session_id, &s->session->session_id_length,
0f113f3e 2136 EVP_sha256(), NULL);
b9908bf9 2137 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
2138 f_err:
2139 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2140 err:
fe3a3291 2141 ossl_statem_set_error(s);
b9908bf9 2142 return MSG_PROCESS_ERROR;
0f113f3e 2143}
67c8e7f4 2144
be3583fa 2145MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
b9908bf9
MC
2146{
2147 int al;
2148 unsigned long resplen;
2149 unsigned int type;
b9908bf9 2150
73999b62 2151 if (!PACKET_get_1(pkt, &type)
ac63710a 2152 || type != TLSEXT_STATUSTYPE_ocsp) {
0f113f3e 2153 al = SSL_AD_DECODE_ERROR;
b9908bf9 2154 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
0f113f3e
MC
2155 goto f_err;
2156 }
73999b62
MC
2157 if (!PACKET_get_net_3(pkt, &resplen)
2158 || PACKET_remaining(pkt) != resplen) {
0f113f3e 2159 al = SSL_AD_DECODE_ERROR;
b9908bf9 2160 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2161 goto f_err;
2162 }
ac63710a 2163 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
a71edf3b 2164 if (s->tlsext_ocsp_resp == NULL) {
0f113f3e 2165 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 2166 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2167 goto f_err;
2168 }
73999b62 2169 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
ac63710a 2170 al = SSL_AD_DECODE_ERROR;
b9908bf9 2171 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
ac63710a
MC
2172 goto f_err;
2173 }
0f113f3e 2174 s->tlsext_ocsp_resplen = resplen;
b9908bf9 2175 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
2176 f_err:
2177 ssl3_send_alert(s, SSL3_AL_FATAL, al);
fe3a3291 2178 ossl_statem_set_error(s);
b9908bf9 2179 return MSG_PROCESS_ERROR;
0f113f3e 2180}
d02b48c6 2181
be3583fa 2182MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
b9908bf9 2183{
73999b62 2184 if (PACKET_remaining(pkt) > 0) {
0f113f3e
MC
2185 /* should contain no data */
2186 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2187 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
fe3a3291 2188 ossl_statem_set_error(s);
b9908bf9 2189 return MSG_PROCESS_ERROR;
0f113f3e 2190 }
b9908bf9
MC
2191
2192#ifndef OPENSSL_NO_SRP
2193 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2194 if (SRP_Calc_A_param(s) <= 0) {
2195 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2196 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
fe3a3291 2197 ossl_statem_set_error(s);
b9908bf9
MC
2198 return MSG_PROCESS_ERROR;
2199 }
2200 }
2201#endif
2202
a455d0f6
MC
2203 /*
2204 * at this point we check that we have the required stuff from
2205 * the server
2206 */
2207 if (!ssl3_check_cert_and_algorithm(s)) {
2208 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
fe3a3291 2209 ossl_statem_set_error(s);
a455d0f6
MC
2210 return MSG_PROCESS_ERROR;
2211 }
2212
bb1aaab4
MC
2213 /*
2214 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
2215 * |tlsext_ocsp_resplen| values will be set if we actually received a status
2216 * message, or NULL and -1 otherwise
2217 */
b1931d43 2218 if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
bb1aaab4
MC
2219 int ret;
2220 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2221 if (ret == 0) {
2222 ssl3_send_alert(s, SSL3_AL_FATAL,
2223 SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
2224 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,
2225 SSL_R_INVALID_STATUS_RESPONSE);
2226 return MSG_PROCESS_ERROR;
2227 }
2228 if (ret < 0) {
2229 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2230 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);
2231 return MSG_PROCESS_ERROR;
2232 }
2233 }
2234
473483d4
MC
2235#ifndef OPENSSL_NO_SCTP
2236 /* Only applies to renegotiation */
2237 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2238 && s->renegotiate != 0)
2239 return MSG_PROCESS_CONTINUE_PROCESSING;
2240 else
2241#endif
2242 return MSG_PROCESS_FINISHED_READING;
0f113f3e 2243}
176f31dd 2244
b9908bf9 2245int tls_construct_client_key_exchange(SSL *s)
0f113f3e
MC
2246{
2247 unsigned char *p;
2248 int n;
7689082b
DSH
2249#ifndef OPENSSL_NO_PSK
2250 size_t pskhdrlen = 0;
2251#endif
0f113f3e 2252 unsigned long alg_k;
bc36ee62 2253#ifndef OPENSSL_NO_RSA
0f113f3e
MC
2254 unsigned char *q;
2255 EVP_PKEY *pkey = NULL;
923ffa97 2256 EVP_PKEY_CTX *pctx = NULL;
79df9d62 2257#endif
fb79abe3 2258#if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
61dd9f7a 2259 EVP_PKEY *ckey = NULL, *skey = NULL;
fb79abe3
DSH
2260#endif
2261#ifndef OPENSSL_NO_EC
0f113f3e
MC
2262 unsigned char *encodedPoint = NULL;
2263 int encoded_pt_len = 0;
ea262260 2264#endif
c660ec63
DSH
2265 unsigned char *pms = NULL;
2266 size_t pmslen = 0;
7689082b 2267 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
d02b48c6 2268
b9908bf9 2269 p = ssl_handshake_start(s);
d02b48c6 2270
7689082b
DSH
2271
2272#ifndef OPENSSL_NO_PSK
b9908bf9
MC
2273 if (alg_k & SSL_PSK) {
2274 int psk_err = 1;
2275 /*
2276 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2277 * \0-terminated identity. The last byte is for us for simulating
2278 * strnlen.
2279 */
2280 char identity[PSK_MAX_IDENTITY_LEN + 1];
2281 size_t identitylen;
2282 unsigned char psk[PSK_MAX_PSK_LEN];
2283 size_t psklen;
2284
2285 if (s->psk_client_callback == NULL) {
2286 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2287 SSL_R_PSK_NO_CLIENT_CB);
2288 goto err;
2289 }
7689082b 2290
b9908bf9 2291 memset(identity, 0, sizeof(identity));
7689082b 2292
b9908bf9
MC
2293 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2294 identity, sizeof(identity) - 1,
2295 psk, sizeof(psk));
7689082b 2296
b9908bf9
MC
2297 if (psklen > PSK_MAX_PSK_LEN) {
2298 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2299 ERR_R_INTERNAL_ERROR);
2300 goto psk_err;
2301 } else if (psklen == 0) {
2302 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2303 SSL_R_PSK_IDENTITY_NOT_FOUND);
2304 goto psk_err;
2305 }
b9908bf9 2306 OPENSSL_free(s->s3->tmp.psk);
7644a9ae 2307 s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
b9908bf9 2308 OPENSSL_cleanse(psk, psklen);
7689082b 2309
b9908bf9
MC
2310 if (s->s3->tmp.psk == NULL) {
2311 OPENSSL_cleanse(identity, sizeof(identity));
2312 goto memerr;
2313 }
7689082b 2314
b9908bf9 2315 s->s3->tmp.psklen = psklen;
b9908bf9
MC
2316 identitylen = strlen(identity);
2317 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2318 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2319 ERR_R_INTERNAL_ERROR);
2320 goto psk_err;
2321 }
2322 OPENSSL_free(s->session->psk_identity);
7644a9ae 2323 s->session->psk_identity = OPENSSL_strdup(identity);
b9908bf9 2324 if (s->session->psk_identity == NULL) {
7689082b 2325 OPENSSL_cleanse(identity, sizeof(identity));
b9908bf9 2326 goto memerr;
7689082b 2327 }
d02b48c6 2328
b9908bf9
MC
2329 s2n(identitylen, p);
2330 memcpy(p, identity, identitylen);
2331 pskhdrlen = 2 + identitylen;
2332 p += identitylen;
2333 psk_err = 0;
2334psk_err:
2335 OPENSSL_cleanse(identity, sizeof(identity));
2336 if (psk_err != 0) {
2337 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2338 goto err;
0f113f3e 2339 }
b9908bf9
MC
2340 }
2341 if (alg_k & SSL_kPSK) {
2342 n = 0;
2343 } else
2344#endif
2345
2346 /* Fool emacs indentation */
2347 if (0) {
2348 }
bc36ee62 2349#ifndef OPENSSL_NO_RSA
b9908bf9 2350 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
923ffa97 2351 size_t enclen;
b9908bf9
MC
2352 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2353 pms = OPENSSL_malloc(pmslen);
a71edf3b 2354 if (pms == NULL)
b9908bf9
MC
2355 goto memerr;
2356
2357 if (s->session->peer == NULL) {
2358 /*
2359 * We should always have a server certificate with SSL_kRSA.
2360 */
2361 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2362 ERR_R_INTERNAL_ERROR);
2363 goto err;
2364 }
0f113f3e 2365
361a1191
KR
2366 pkey = X509_get_pubkey(s->session->peer);
2367 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2368 || (pkey->pkey.rsa == NULL)) {
2369 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2370 ERR_R_INTERNAL_ERROR);
b9908bf9 2371 EVP_PKEY_free(pkey);
361a1191 2372 goto err;
b9908bf9 2373 }
0f113f3e 2374
b9908bf9
MC
2375 pms[0] = s->client_version >> 8;
2376 pms[1] = s->client_version & 0xff;
2377 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2378 goto err;
0f113f3e 2379
b9908bf9
MC
2380 q = p;
2381 /* Fix buf for TLS and beyond */
2382 if (s->version > SSL3_VERSION)
2383 p += 2;
923ffa97
DSH
2384 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2385 EVP_PKEY_free(pkey);
2386 pkey = NULL;
2387 if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
2388 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
2389 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2390 ERR_R_EVP_LIB);
2391 goto err;
2392 }
2393 if (EVP_PKEY_encrypt(pctx, p, &enclen, pms, pmslen) <= 0) {
2394 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2395 SSL_R_BAD_RSA_ENCRYPT);
2396 goto err;
2397 }
2398 n = enclen;
2399 EVP_PKEY_CTX_free(pctx);
2400 pctx = NULL;
0f113f3e 2401# ifdef PKCS1_CHECK
b9908bf9
MC
2402 if (s->options & SSL_OP_PKCS1_CHECK_1)
2403 p[1]++;
2404 if (s->options & SSL_OP_PKCS1_CHECK_2)
2405 tmp_buf[0] = 0x70;
0f113f3e 2406# endif
0f113f3e 2407
b9908bf9
MC
2408 /* Fix buf for TLS and beyond */
2409 if (s->version > SSL3_VERSION) {
2410 s2n(n, q);
2411 n += 2;
0f113f3e 2412 }
b9908bf9 2413 }
f9b3bff6 2414#endif
bc36ee62 2415#ifndef OPENSSL_NO_DH
bc71f910 2416 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
fb79abe3
DSH
2417 DH *dh_clnt = NULL;
2418 skey = s->s3->peer_tmp;
2419 if (skey == NULL) {
bc71f910
DSH
2420 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2421 ERR_R_INTERNAL_ERROR);
2422 goto err;
0f113f3e 2423 }
fb79abe3
DSH
2424 ckey = ssl_generate_pkey(skey, NID_undef);
2425 dh_clnt = EVP_PKEY_get0_DH(ckey);
0f113f3e 2426
fb79abe3
DSH
2427 if (dh_clnt == NULL || ssl_derive(s, ckey, skey) == 0) {
2428 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2429 ERR_R_INTERNAL_ERROR);
b9908bf9
MC
2430 goto err;
2431 }
fb79abe3 2432
0f113f3e 2433
bc71f910
DSH
2434 /* send off the data */
2435 n = BN_num_bytes(dh_clnt->pub_key);
2436 s2n(n, p);
2437 BN_bn2bin(dh_clnt->pub_key, p);
2438 n += 2;
fb79abe3
DSH
2439 EVP_PKEY_free(ckey);
2440 ckey = NULL;
b9908bf9
MC
2441 }
2442#endif
0f113f3e 2443
b9908bf9
MC
2444#ifndef OPENSSL_NO_EC
2445 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
0f113f3e 2446
61dd9f7a
DSH
2447 if (s->s3->peer_tmp != NULL) {
2448 skey = s->s3->peer_tmp;
b9908bf9
MC
2449 } else {
2450 /* Get the Server Public Key from Cert */
61dd9f7a
DSH
2451 skey = X509_get0_pubkey(s->session->peer);
2452 if ((skey == NULL)
2453 || (skey->type != EVP_PKEY_EC)
2454 || (skey->pkey.ec == NULL)) {
b9908bf9 2455 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
266483d2
MC
2456 ERR_R_INTERNAL_ERROR);
2457 goto err;
0f113f3e 2458 }
b9908bf9
MC
2459 }
2460
61dd9f7a 2461 ckey = ssl_generate_pkey(skey, NID_undef);
b9908bf9 2462
61dd9f7a
DSH
2463 if (ssl_derive(s, ckey, skey) == 0) {
2464 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EVP_LIB);
b9908bf9
MC
2465 goto err;
2466 }
2467
61dd9f7a
DSH
2468 /* Generate encoding of client key */
2469 encoded_pt_len = EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(ckey),
2470 POINT_CONVERSION_UNCOMPRESSED,
2471 &encodedPoint, NULL);
cae41364 2472
c66ce5eb
DSH
2473 if (encoded_pt_len == 0) {
2474 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2475 goto err;
2476 }
b9908bf9 2477
61dd9f7a
DSH
2478 EVP_PKEY_free(ckey);
2479 ckey = NULL;
2480
c66ce5eb 2481 n = encoded_pt_len;
b9908bf9 2482
c66ce5eb
DSH
2483 *p = n; /* length of encoded point */
2484 /* Encoded point will be copied here */
2485 p += 1;
2486 /* copy the point */
2487 memcpy(p, encodedPoint, n);
2488 /* increment n to account for length field */
2489 n += 1;
0f113f3e 2490
b9908bf9 2491 /* Free allocated memory */
b9908bf9 2492 OPENSSL_free(encodedPoint);
b9908bf9
MC
2493 }
2494#endif /* !OPENSSL_NO_EC */
2a9b9654 2495#ifndef OPENSSL_NO_GOST
b9908bf9
MC
2496 else if (alg_k & SSL_kGOST) {
2497 /* GOST key exchange message creation */
2498 EVP_PKEY_CTX *pkey_ctx;
2499 X509 *peer_cert;
2500 size_t msglen;
2501 unsigned int md_len;
2502 unsigned char shared_ukm[32], tmp[256];
2503 EVP_MD_CTX *ukm_hash;
2504 EVP_PKEY *pub_key;
e44380a9
DB
2505 int dgst_nid = NID_id_GostR3411_94;
2506 if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
2507 dgst_nid = NID_id_GostR3411_2012_256;
2508
b9908bf9
MC
2509
2510 pmslen = 32;
2511 pms = OPENSSL_malloc(pmslen);
a71edf3b 2512 if (pms == NULL)
b9908bf9 2513 goto memerr;
7689082b 2514
b9908bf9
MC
2515 /*
2516 * Get server sertificate PKEY and create ctx from it
2517 */
2518 peer_cert = s->session->peer;
2519 if (!peer_cert) {
2520 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2521 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
77d514c5
MC
2522 goto err;
2523 }
2524
b9908bf9
MC
2525 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2526 X509_get_pubkey(peer_cert), NULL);
a71edf3b
MC
2527 if (pkey_ctx == NULL) {
2528 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2529 ERR_R_MALLOC_FAILURE);
2530 goto err;
2531 }
b9908bf9
MC
2532 /*
2533 * If we have send a certificate, and certificate key
5f3d93e4 2534 * parameters match those of server certificate, use
b9908bf9
MC
2535 * certificate key for key exchange
2536 */
0f113f3e 2537
b9908bf9
MC
2538 /* Otherwise, generate ephemeral key pair */
2539
5f3d93e4
MC
2540 if (pkey_ctx == NULL
2541 || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2542 /* Generate session key */
2543 || RAND_bytes(pms, pmslen) <= 0) {
b9908bf9
MC
2544 EVP_PKEY_CTX_free(pkey_ctx);
2545 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2546 ERR_R_INTERNAL_ERROR);
2547 goto err;
2548 };
c660ec63 2549 /*
b9908bf9 2550 * If we have client certificate, use its secret as peer key
c660ec63 2551 */
b9908bf9
MC
2552 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2553 if (EVP_PKEY_derive_set_peer
2554 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2555 /*
2556 * If there was an error - just ignore it. Ephemeral key
2557 * * would be used
2558 */
2559 ERR_clear_error();
c660ec63
DSH
2560 }
2561 }
b9908bf9
MC
2562 /*
2563 * Compute shared IV and store it in algorithm-specific context
2564 * data
2565 */
bfb0641f 2566 ukm_hash = EVP_MD_CTX_new();
5f3d93e4 2567 if (EVP_DigestInit(ukm_hash,
e44380a9 2568 EVP_get_digestbynid(dgst_nid)) <= 0
5f3d93e4
MC
2569 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2570 SSL3_RANDOM_SIZE) <= 0
2571 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2572 SSL3_RANDOM_SIZE) <= 0
2573 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
bfb0641f 2574 EVP_MD_CTX_free(ukm_hash);
5f3d93e4
MC
2575 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2576 ERR_R_INTERNAL_ERROR);
2577 goto err;
2578 }
bfb0641f 2579 EVP_MD_CTX_free(ukm_hash);
b9908bf9
MC
2580 if (EVP_PKEY_CTX_ctrl
2581 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2582 shared_ukm) < 0) {
2583 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2584 SSL_R_LIBRARY_BUG);
2585 goto err;
c660ec63 2586 }
b9908bf9
MC
2587 /* Make GOST keytransport blob message */
2588 /*
2589 * Encapsulate it into sequence
2590 */
2591 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2592 msglen = 255;
5f3d93e4 2593 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
b9908bf9
MC
2594 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2595 SSL_R_LIBRARY_BUG);
c660ec63
DSH
2596 goto err;
2597 }
b9908bf9
MC
2598 if (msglen >= 0x80) {
2599 *(p++) = 0x81;
2600 *(p++) = msglen & 0xff;
2601 n = msglen + 3;
2602 } else {
2603 *(p++) = msglen & 0xff;
2604 n = msglen + 2;
2605 }
2606 memcpy(p, tmp, msglen);
2607 /* Check if pubkey from client certificate was used */
2608 if (EVP_PKEY_CTX_ctrl
2609 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2610 /* Set flag "skip certificate verify" */
2611 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2612 }
2613 EVP_PKEY_CTX_free(pkey_ctx);
2614 EVP_PKEY_free(pub_key);
2615
2616 }
2a9b9654 2617#endif
b9908bf9
MC
2618#ifndef OPENSSL_NO_SRP
2619 else if (alg_k & SSL_kSRP) {
2620 if (s->srp_ctx.A != NULL) {
2621 /* send off the data */
2622 n = BN_num_bytes(s->srp_ctx.A);
2623 s2n(n, p);
2624 BN_bn2bin(s->srp_ctx.A, p);
2625 n += 2;
2626 } else {
2627 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2628 ERR_R_INTERNAL_ERROR);
2629 goto err;
2630 }
2631 OPENSSL_free(s->session->srp_username);
7644a9ae 2632 s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
b9908bf9
MC
2633 if (s->session->srp_username == NULL) {
2634 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2635 ERR_R_MALLOC_FAILURE);
69f68237
MC
2636 goto err;
2637 }
c660ec63 2638 }
b9908bf9
MC
2639#endif
2640 else {
2641 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2642 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2643 goto err;
2644 }
2645
2646#ifndef OPENSSL_NO_PSK
2647 n += pskhdrlen;
2648#endif
2649
2650 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2651 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2652 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2653 goto err;
2654 }
2655
61dd9f7a
DSH
2656 if (pms != NULL) {
2657 s->s3->tmp.pms = pms;
2658 s->s3->tmp.pmslen = pmslen;
2659 }
b9908bf9
MC
2660
2661 return 1;
c660ec63
DSH
2662 memerr:
2663 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 2664 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
0f113f3e 2665 err:
4b45c6e5 2666 OPENSSL_clear_free(pms, pmslen);
76106e60 2667 s->s3->tmp.pms = NULL;
923ffa97
DSH
2668#ifndef OPENSSL_NO_RSA
2669 EVP_PKEY_CTX_free(pctx);
2670#endif
10bf4fc2 2671#ifndef OPENSSL_NO_EC
b548a1f1 2672 OPENSSL_free(encodedPoint);
fb79abe3
DSH
2673#endif
2674#if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
61dd9f7a 2675 EVP_PKEY_free(ckey);
7689082b
DSH
2676#endif
2677#ifndef OPENSSL_NO_PSK
2678 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2679 s->s3->tmp.psk = NULL;
0f113f3e 2680#endif
fe3a3291 2681 ossl_statem_set_error(s);
b9908bf9
MC
2682 return 0;
2683}
2684
2685int tls_client_key_exchange_post_work(SSL *s)
2686{
2687 unsigned char *pms = NULL;
2688 size_t pmslen = 0;
2689
2690#ifndef OPENSSL_NO_SRP
2691 /* Check for SRP */
2692 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2693 if (!srp_generate_client_master_secret(s)) {
2694 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2695 ERR_R_INTERNAL_ERROR);
2696 goto err;
2697 }
2698 return 1;
2699 }
2700#endif
2701 pms = s->s3->tmp.pms;
2702 pmslen = s->s3->tmp.pmslen;
2703
2704 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2705 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2706 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2707 goto err;
2708 }
2709 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2710 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2711 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2712 goto err;
2713 }
473483d4
MC
2714
2715#ifndef OPENSSL_NO_SCTP
2716 if (SSL_IS_DTLS(s)) {
2717 unsigned char sctpauthkey[64];
2718 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2719
2720 /*
2721 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2722 * used.
2723 */
141eb8c6
MC
2724 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2725 sizeof(DTLS1_SCTP_AUTH_LABEL));
473483d4
MC
2726
2727 if (SSL_export_keying_material(s, sctpauthkey,
2728 sizeof(sctpauthkey), labelbuffer,
2729 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2730 goto err;
2731
2732 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2733 sizeof(sctpauthkey), sctpauthkey);
2734 }
2735#endif
2736
b9908bf9
MC
2737 return 1;
2738 err:
2739 OPENSSL_clear_free(pms, pmslen);
2740 s->s3->tmp.pms = NULL;
2741 return 0;
0f113f3e 2742}
d02b48c6 2743
b9908bf9 2744int tls_construct_client_verify(SSL *s)
0f113f3e
MC
2745{
2746 unsigned char *p;
0f113f3e 2747 EVP_PKEY *pkey;
a0f63828 2748 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
6e59a892 2749 EVP_MD_CTX *mctx;
0f113f3e 2750 unsigned u = 0;
2a9b9654 2751 unsigned long n = 0;
a0f63828
DSH
2752 long hdatalen = 0;
2753 void *hdata;
0f113f3e 2754
bfb0641f 2755 mctx = EVP_MD_CTX_new();
6e59a892
RL
2756 if (mctx == NULL) {
2757 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
2758 goto err;
2759 }
0f113f3e 2760
b9908bf9
MC
2761 p = ssl_handshake_start(s);
2762 pkey = s->cert->key->privatekey;
a0f63828
DSH
2763
2764 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2765 if (hdatalen <= 0) {
5f3d93e4
MC
2766 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2767 goto err;
2768 }
a0f63828
DSH
2769 if (SSL_USE_SIGALGS(s)) {
2770 if (!tls12_get_sigandhash(p, pkey, md)) {
b9908bf9
MC
2771 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2772 goto err;
0f113f3e 2773 }
a0f63828 2774 p += 2;
2a9b9654 2775 n = 2;
a0f63828 2776 }
855a54a9 2777#ifdef SSL_DEBUG
a0f63828 2778 fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
b9908bf9 2779#endif
6e59a892
RL
2780 if (!EVP_SignInit_ex(mctx, md, NULL)
2781 || !EVP_SignUpdate(mctx, hdata, hdatalen)
a0f63828 2782 || (s->version == SSL3_VERSION
6e59a892 2783 && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
a0f63828
DSH
2784 s->session->master_key_length,
2785 s->session->master_key))
6e59a892 2786 || !EVP_SignFinal(mctx, p + 2, &u, pkey)) {
a0f63828
DSH
2787 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
2788 goto err;
2789 }
2a9b9654 2790#ifndef OPENSSL_NO_GOST
e44380a9
DB
2791 if (pkey->type == NID_id_GostR3410_2001
2792 || pkey->type == NID_id_GostR3410_2012_256
2793 || pkey->type == NID_id_GostR3410_2012_512) {
2a9b9654 2794 BUF_reverse(p + 2, NULL, u);
b9908bf9 2795 }
2a9b9654 2796#endif
a0f63828
DSH
2797
2798 s2n(u, p);
2a9b9654 2799 n += u + 2;
a0f63828
DSH
2800 /* Digest cached records and discard handshake buffer */
2801 if (!ssl3_digest_cached_records(s, 0))
2802 goto err;
b9908bf9
MC
2803 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
2804 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2805 goto err;
0f113f3e 2806 }
b9908bf9 2807
bfb0641f 2808 EVP_MD_CTX_free(mctx);
b9908bf9 2809 return 1;
0f113f3e 2810 err:
bfb0641f 2811 EVP_MD_CTX_free(mctx);
b9908bf9 2812 return 0;
0f113f3e
MC
2813}
2814
2815/*
2816 * Check a certificate can be used for client authentication. Currently check
2817 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2818 * certificates can be used and optionally checks suitability for Suite B.
0d609395
DSH
2819 */
2820static int ssl3_check_client_certificate(SSL *s)
0f113f3e 2821{
0f113f3e
MC
2822 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
2823 return 0;
2824 /* If no suitable signature algorithm can't use certificate */
d376e57d 2825 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
0f113f3e
MC
2826 return 0;
2827 /*
2828 * If strict mode check suitability of chain before using it. This also
2829 * adjusts suite B digest if necessary.
2830 */
2831 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
2832 !tls1_check_chain(s, NULL, NULL, NULL, -2))
2833 return 0;
0f113f3e
MC
2834 return 1;
2835}
0d609395 2836
be3583fa 2837WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
0f113f3e
MC
2838{
2839 X509 *x509 = NULL;
2840 EVP_PKEY *pkey = NULL;
2841 int i;
2842
b9908bf9 2843 if (wst == WORK_MORE_A) {
0f113f3e
MC
2844 /* Let cert callback update client certificates if required */
2845 if (s->cert->cert_cb) {
2846 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2847 if (i < 0) {
2848 s->rwstate = SSL_X509_LOOKUP;
b9908bf9 2849 return WORK_MORE_A;
0f113f3e
MC
2850 }
2851 if (i == 0) {
2852 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
fe3a3291 2853 ossl_statem_set_error(s);
0f113f3e
MC
2854 return 0;
2855 }
2856 s->rwstate = SSL_NOTHING;
2857 }
2858 if (ssl3_check_client_certificate(s))
b9908bf9
MC
2859 return WORK_FINISHED_CONTINUE;
2860
2861 /* Fall through to WORK_MORE_B */
2862 wst = WORK_MORE_B;
0f113f3e
MC
2863 }
2864
2865 /* We need to get a client cert */
b9908bf9 2866 if (wst == WORK_MORE_B) {
0f113f3e
MC
2867 /*
2868 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2869 * return(-1); We then get retied later
2870 */
0f113f3e
MC
2871 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2872 if (i < 0) {
2873 s->rwstate = SSL_X509_LOOKUP;
b9908bf9 2874 return WORK_MORE_B;
0f113f3e
MC
2875 }
2876 s->rwstate = SSL_NOTHING;
2877 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
0f113f3e
MC
2878 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
2879 i = 0;
2880 } else if (i == 1) {
2881 i = 0;
b9908bf9 2882 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
0f113f3e
MC
2883 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2884 }
2885
222561fe 2886 X509_free(x509);
25aaa98a 2887 EVP_PKEY_free(pkey);
0f113f3e
MC
2888 if (i && !ssl3_check_client_certificate(s))
2889 i = 0;
2890 if (i == 0) {
2891 if (s->version == SSL3_VERSION) {
2892 s->s3->tmp.cert_req = 0;
2893 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
b9908bf9 2894 return WORK_FINISHED_CONTINUE;
0f113f3e
MC
2895 } else {
2896 s->s3->tmp.cert_req = 2;
124037fd 2897 if (!ssl3_digest_cached_records(s, 0)) {
dab18ab5 2898 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
fe3a3291 2899 ossl_statem_set_error(s);
dab18ab5
DSH
2900 return 0;
2901 }
0f113f3e
MC
2902 }
2903 }
2904
b9908bf9 2905 return WORK_FINISHED_CONTINUE;
0f113f3e
MC
2906 }
2907
b9908bf9
MC
2908 /* Shouldn't ever get here */
2909 return WORK_ERROR;
2910}
2911
2912int tls_construct_client_certificate(SSL *s)
2913{
2914 if (!ssl3_output_cert_chain(s,
2915 (s->s3->tmp.cert_req ==
2916 2) ? NULL : s->cert->key)) {
2917 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2918 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
fe3a3291 2919 ossl_statem_set_error(s);
b9908bf9 2920 return 0;
0f113f3e 2921 }
b9908bf9
MC
2922
2923 return 1;
0f113f3e
MC
2924}
2925
2926#define has_bits(i,m) (((i)&(m)) == (m))
d02b48c6 2927
36d16f8e 2928int ssl3_check_cert_and_algorithm(SSL *s)
0f113f3e 2929{
60f43e9e
RL
2930 int i;
2931#ifndef OPENSSL_NO_EC
2932 int idx;
2933#endif
0f113f3e
MC
2934 long alg_k, alg_a;
2935 EVP_PKEY *pkey = NULL;
26c79d56 2936 int al = SSL_AD_HANDSHAKE_FAILURE;
d02b48c6 2937
0f113f3e
MC
2938 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2939 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
d02b48c6 2940
0f113f3e 2941 /* we don't have a certificate */
55a9a16f 2942 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
0f113f3e 2943 return (1);
d02b48c6 2944
0f113f3e 2945 /* This is the passed certificate */
d02b48c6 2946
10bf4fc2 2947#ifndef OPENSSL_NO_EC
60f43e9e 2948 idx = s->session->peer_type;
0f113f3e 2949 if (idx == SSL_PKEY_ECC) {
a273c6ee 2950 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
0f113f3e
MC
2951 /* check failed */
2952 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
2953 goto f_err;
2954 } else {
2955 return 1;
2956 }
2957 } else if (alg_a & SSL_aECDSA) {
2958 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2959 SSL_R_MISSING_ECDSA_SIGNING_CERT);
2960 goto f_err;
2961 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2962 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
2963 goto f_err;
2964 }
2965#endif
a273c6ee 2966 pkey = X509_get_pubkey(s->session->peer);
a273c6ee 2967 i = X509_certificate_type(s->session->peer, pkey);
0f113f3e
MC
2968 EVP_PKEY_free(pkey);
2969
2970 /* Check that we have a certificate if we require one */
2971 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
2972 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2973 SSL_R_MISSING_RSA_SIGNING_CERT);
2974 goto f_err;
2975 }
bc36ee62 2976#ifndef OPENSSL_NO_DSA
0f113f3e
MC
2977 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
2978 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2979 SSL_R_MISSING_DSA_SIGNING_CERT);
2980 goto f_err;
2981 }
d02b48c6 2982#endif
bc36ee62 2983#ifndef OPENSSL_NO_RSA
361a1191
KR
2984 if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
2985 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
2986 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2987 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2988 goto f_err;
0f113f3e 2989 }
79df9d62 2990#endif
bc36ee62 2991#ifndef OPENSSL_NO_DH
fb79abe3 2992 if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
26c79d56
KR
2993 al = SSL_AD_INTERNAL_ERROR;
2994 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
0f113f3e 2995 goto f_err;
0f113f3e 2996 }
d02b48c6
RE
2997#endif
2998
0f113f3e
MC
2999 return (1);
3000 f_err:
26c79d56 3001 ssl3_send_alert(s, SSL3_AL_FATAL, al);
0f113f3e
MC
3002 return (0);
3003}
3004
e481f9b9 3005#ifndef OPENSSL_NO_NEXTPROTONEG
b9908bf9
MC
3006int tls_construct_next_proto(SSL *s)
3007{
3008 unsigned int len, padding_len;
3009 unsigned char *d;
3010
3011 len = s->next_proto_negotiated_len;
3012 padding_len = 32 - ((len + 2) % 32);
3013 d = (unsigned char *)s->init_buf->data;
3014 d[4] = len;
3015 memcpy(d + 5, s->next_proto_negotiated, len);
3016 d[5 + len] = padding_len;
3017 memset(d + 6 + len, 0, padding_len);
3018 *(d++) = SSL3_MT_NEXT_PROTO;
3019 l2n3(2 + len + padding_len, d);
3020 s->init_num = 4 + 2 + len + padding_len;
3021 s->init_off = 0;
3022
3023 return 1;
3024}
6434abbf 3025#endif
368888bc
DSH
3026
3027int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
0f113f3e
MC
3028{
3029 int i = 0;
368888bc 3030#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
3031 if (s->ctx->client_cert_engine) {
3032 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3033 SSL_get_client_CA_list(s),
3034 px509, ppkey, NULL, NULL, NULL);
3035 if (i != 0)
3036 return i;
3037 }
3038#endif
3039 if (s->ctx->client_cert_cb)
3040 i = s->ctx->client_cert_cb(s, px509, ppkey);
3041 return i;
3042}
d45ba43d
MC
3043
3044int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
e9fa092e 3045 unsigned char *p)
d45ba43d
MC
3046{
3047 int i, j = 0;
3048 SSL_CIPHER *c;
3049 unsigned char *q;
3050 int empty_reneg_info_scsv = !s->renegotiate;
3051 /* Set disabled masks for this session */
3052 ssl_set_client_disabled(s);
3053
3054 if (sk == NULL)
3055 return (0);
3056 q = p;
d45ba43d
MC
3057
3058 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3059 c = sk_SSL_CIPHER_value(sk, i);
3060 /* Skip disabled ciphers */
3061 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3062 continue;
3063#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3064 if (c->id == SSL3_CK_SCSV) {
3065 if (!empty_reneg_info_scsv)
3066 continue;
3067 else
3068 empty_reneg_info_scsv = 0;
3069 }
3070#endif
e9fa092e 3071 j = s->method->put_cipher_by_char(c, p);
d45ba43d
MC
3072 p += j;
3073 }
3074 /*
3075 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3076 * applicable SCSVs.
3077 */
3078 if (p != q) {
3079 if (empty_reneg_info_scsv) {
3080 static SSL_CIPHER scsv = {
3081 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3082 };
e9fa092e 3083 j = s->method->put_cipher_by_char(&scsv, p);
d45ba43d
MC
3084 p += j;
3085#ifdef OPENSSL_RI_DEBUG
3086 fprintf(stderr,
3087 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3088#endif
3089 }
3090 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3091 static SSL_CIPHER scsv = {
3092 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3093 };
e9fa092e 3094 j = s->method->put_cipher_by_char(&scsv, p);
d45ba43d
MC
3095 p += j;
3096 }
3097 }
3098
3099 return (p - q);
3100}