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