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