]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/statem/statem_clnt.c
Minor documentation tweak
[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
MC
167static inline int cert_req_allowed(SSL *s);
168static inline int key_exchange_skip_allowed(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 */
185 if (s->version > SSL3_VERSION
186 && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
187 return 0;
188
189 return 1;
190}
191
192/*
193 * Are we allowed to skip the ServerKeyExchange message?
194 *
195 * Return values are:
196 * 1: Yes
197 * 0: No
198 */
199static inline int key_exchange_skip_allowed(SSL *s)
200{
201 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
202
203 /*
204 * Can't skip server key exchange if this is an ephemeral
205 * ciphersuite.
206 */
207 if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
208 return 0;
209 }
210
211 return 1;
212}
213
214/*
215 * client_read_transition() encapsulates the logic for the allowed handshake
216 * state transitions when the client is reading messages from the server. The
217 * message type that the server has sent is provided in |mt|. The current state
218 * is in |s->statem.hand_state|.
219 *
220 * Return values are:
221 * 1: Success (transition allowed)
222 * 0: Error (transition not allowed)
223 */
224int client_read_transition(SSL *s, int mt)
225{
226 STATEM *st = &s->statem;
227
228 switch(st->hand_state) {
229 case TLS_ST_CW_CLNT_HELLO:
230 if (mt == SSL3_MT_SERVER_HELLO) {
231 st->hand_state = TLS_ST_CR_SRVR_HELLO;
232 return 1;
233 }
234
235 if (SSL_IS_DTLS(s)) {
236 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
237 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
238 return 1;
239 }
240 }
241 break;
242
243 case TLS_ST_CR_SRVR_HELLO:
244 if (s->hit) {
245 if (s->tlsext_ticket_expected) {
246 if (mt == SSL3_MT_NEWSESSION_TICKET) {
247 st->hand_state = TLS_ST_CR_SESSION_TICKET;
248 return 1;
249 }
250 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
251 st->hand_state = TLS_ST_CR_CHANGE;
252 return 1;
253 }
254 } else {
255 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
256 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
257 return 1;
258 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
259 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
260 if (mt == SSL3_MT_CERTIFICATE) {
261 st->hand_state = TLS_ST_CR_CERT;
262 return 1;
263 }
264 } else {
265 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
266 st->hand_state = TLS_ST_CR_KEY_EXCH;
267 return 1;
268 } else if (key_exchange_skip_allowed(s)) {
269 if (mt == SSL3_MT_CERTIFICATE_REQUEST
270 && cert_req_allowed(s)) {
271 st->hand_state = TLS_ST_CR_CERT_REQ;
272 return 1;
273 } else if (mt == SSL3_MT_SERVER_DONE) {
274 st->hand_state = TLS_ST_CR_SRVR_DONE;
275 return 1;
276 }
277 }
278 }
279 }
280 break;
281
282 case TLS_ST_CR_CERT:
283 if (s->tlsext_status_expected) {
284 if (mt == SSL3_MT_CERTIFICATE_STATUS) {
285 st->hand_state = TLS_ST_CR_CERT_STATUS;
286 return 1;
287 }
288 } else {
289 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
290 st->hand_state = TLS_ST_CR_KEY_EXCH;
291 return 1;
292 } else if (key_exchange_skip_allowed(s)) {
293 if (mt == SSL3_MT_CERTIFICATE_REQUEST && cert_req_allowed(s)) {
294 st->hand_state = TLS_ST_CR_CERT_REQ;
295 return 1;
296 } else if (mt == SSL3_MT_SERVER_DONE) {
297 st->hand_state = TLS_ST_CR_SRVR_DONE;
298 return 1;
299 }
300 }
301 }
302 break;
303
304 case TLS_ST_CR_CERT_STATUS:
305 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
306 st->hand_state = TLS_ST_CR_KEY_EXCH;
307 return 1;
308 } else if (key_exchange_skip_allowed(s)) {
309 if (mt == SSL3_MT_CERTIFICATE_REQUEST && cert_req_allowed(s)) {
310 st->hand_state = TLS_ST_CR_CERT_REQ;
311 return 1;
312 } else if (mt == SSL3_MT_SERVER_DONE) {
313 st->hand_state = TLS_ST_CR_SRVR_DONE;
314 return 1;
315 }
316 }
317 break;
318
319 case TLS_ST_CR_KEY_EXCH:
320 if (mt == SSL3_MT_CERTIFICATE_REQUEST && cert_req_allowed(s)) {
321 st->hand_state = TLS_ST_CR_CERT_REQ;
322 return 1;
323 } else if (mt == SSL3_MT_SERVER_DONE) {
324 st->hand_state = TLS_ST_CR_SRVR_DONE;
325 return 1;
326 }
327 break;
328
329 case TLS_ST_CR_CERT_REQ:
330 if (mt == SSL3_MT_SERVER_DONE) {
331 st->hand_state = TLS_ST_CR_SRVR_DONE;
332 return 1;
333 }
334 break;
335
336 case TLS_ST_CW_FINISHED:
337 if (mt == SSL3_MT_NEWSESSION_TICKET && s->tlsext_ticket_expected) {
338 st->hand_state = TLS_ST_CR_SESSION_TICKET;
339 return 1;
340 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
341 st->hand_state = TLS_ST_CR_CHANGE;
342 return 1;
343 }
344 break;
345
346 case TLS_ST_CR_SESSION_TICKET:
347 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
348 st->hand_state = TLS_ST_CR_CHANGE;
349 return 1;
350 }
351 break;
352
353 case TLS_ST_CR_CHANGE:
354 if (mt == SSL3_MT_FINISHED) {
355 st->hand_state = TLS_ST_CR_FINISHED;
356 return 1;
357 }
358 break;
359
360 default:
361 break;
362 }
363
364 /* No valid transition found */
365 return 0;
366}
367
368/*
369 * client_write_transition() works out what handshake state to move to next
370 * when the client is writing messages to be sent to the server.
371 */
372enum WRITE_TRAN client_write_transition(SSL *s)
373{
374 STATEM *st = &s->statem;
375
376 switch(st->hand_state) {
377 case TLS_ST_OK:
378 /* Renegotiation - fall through */
379 case TLS_ST_BEFORE:
380 st->hand_state = TLS_ST_CW_CLNT_HELLO;
381 return WRITE_TRAN_CONTINUE;
382
383 case TLS_ST_CW_CLNT_HELLO:
384 /*
385 * No transition at the end of writing because we don't know what
386 * we will be sent
387 */
388 return WRITE_TRAN_FINISHED;
389
390 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
391 st->hand_state = TLS_ST_CW_CLNT_HELLO;
392 return WRITE_TRAN_CONTINUE;
393
394 case TLS_ST_CR_SRVR_DONE:
395 if (s->s3->tmp.cert_req)
396 st->hand_state = TLS_ST_CW_CERT;
397 else
398 st->hand_state = TLS_ST_CW_KEY_EXCH;
399 return WRITE_TRAN_CONTINUE;
400
401 case TLS_ST_CW_CERT:
402 st->hand_state = TLS_ST_CW_KEY_EXCH;
403 return WRITE_TRAN_CONTINUE;
404
405 case TLS_ST_CW_KEY_EXCH:
406 /*
407 * For TLS, cert_req is set to 2, so a cert chain of nothing is
408 * sent, but no verify packet is sent
409 */
410 /*
411 * XXX: For now, we do not support client authentication in ECDH
412 * cipher suites with ECDH (rather than ECDSA) certificates. We
413 * need to skip the certificate verify message when client's
414 * ECDH public key is sent inside the client certificate.
415 */
416 if (s->s3->tmp.cert_req == 1) {
417 st->hand_state = TLS_ST_CW_CERT_VRFY;
418 } else {
419 st->hand_state = TLS_ST_CW_CHANGE;
420 }
421 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
422 st->hand_state = TLS_ST_CW_CHANGE;
423 }
424 return WRITE_TRAN_CONTINUE;
425
426 case TLS_ST_CW_CERT_VRFY:
427 st->hand_state = TLS_ST_CW_CHANGE;
428 return WRITE_TRAN_CONTINUE;
429
430 case TLS_ST_CW_CHANGE:
431#if defined(OPENSSL_NO_NEXTPROTONEG)
432 st->hand_state = TLS_ST_CW_FINISHED;
433#else
434 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
435 st->hand_state = TLS_ST_CW_NEXT_PROTO;
436 else
437 st->hand_state = TLS_ST_CW_FINISHED;
438#endif
439 return WRITE_TRAN_CONTINUE;
440
441#if !defined(OPENSSL_NO_NEXTPROTONEG)
442 case TLS_ST_CW_NEXT_PROTO:
443 st->hand_state = TLS_ST_CW_FINISHED;
444 return WRITE_TRAN_CONTINUE;
445#endif
446
447 case TLS_ST_CW_FINISHED:
448 if (s->hit) {
449 st->hand_state = TLS_ST_OK;
450 statem_set_in_init(s, 0);
451 return WRITE_TRAN_CONTINUE;
452 } else {
453 return WRITE_TRAN_FINISHED;
454 }
455
456 case TLS_ST_CR_FINISHED:
457 if (s->hit) {
458 st->hand_state = TLS_ST_CW_CHANGE;
459 return WRITE_TRAN_CONTINUE;
460 } else {
461 st->hand_state = TLS_ST_OK;
462 statem_set_in_init(s, 0);
463 return WRITE_TRAN_CONTINUE;
464 }
465
466 default:
467 /* Shouldn't happen */
468 return WRITE_TRAN_ERROR;
469 }
470}
471
472/*
473 * Perform any pre work that needs to be done prior to sending a message from
474 * the client to the server.
475 */
476enum WORK_STATE client_pre_work(SSL *s, enum WORK_STATE wst)
477{
478 STATEM *st = &s->statem;
479
480 switch(st->hand_state) {
481 case TLS_ST_CW_CLNT_HELLO:
482 s->shutdown = 0;
483 if (SSL_IS_DTLS(s)) {
484 /* every DTLS ClientHello resets Finished MAC */
485 ssl3_init_finished_mac(s);
486 }
487 break;
488
489 case TLS_ST_CW_CERT:
490 return tls_prepare_client_certificate(s, wst);
491
492 case TLS_ST_CW_CHANGE:
493 if (SSL_IS_DTLS(s)) {
494 if (s->hit) {
495 /*
496 * We're into the last flight so we don't retransmit these
497 * messages unless we need to.
498 */
499 st->use_timer = 0;
500 }
501#ifndef OPENSSL_NO_SCTP
502 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
503 return dtls_wait_for_dry(s);
504#endif
505 }
506 return WORK_FINISHED_CONTINUE;
507
508 case TLS_ST_OK:
509 return tls_finish_handshake(s, wst);
510
511 default:
512 /* No pre work to be done */
513 break;
514 }
515
516 return WORK_FINISHED_CONTINUE;
517}
518
519/*
520 * Perform any work that needs to be done after sending a message from the
521 * client to the server.
522 */
523enum WORK_STATE client_post_work(SSL *s, enum WORK_STATE wst)
524{
525 STATEM *st = &s->statem;
526
527 s->init_num = 0;
528
529 switch(st->hand_state) {
530 case TLS_ST_CW_CLNT_HELLO:
531 if (SSL_IS_DTLS(s) && s->d1->cookie_len > 0 && statem_flush(s) != 1)
532 return WORK_MORE_A;
533#ifndef OPENSSL_NO_SCTP
534 /* Disable buffering for SCTP */
535 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s))) {
536#endif
537 /*
538 * turn on buffering for the next lot of output
539 */
540 if (s->bbio != s->wbio)
541 s->wbio = BIO_push(s->bbio, s->wbio);
542#ifndef OPENSSL_NO_SCTP
543 }
544#endif
545 if (SSL_IS_DTLS(s)) {
546 /* Treat the next message as the first packet */
547 s->first_packet = 1;
548 }
549 break;
550
551 case TLS_ST_CW_KEY_EXCH:
552 if (tls_client_key_exchange_post_work(s) == 0)
553 return WORK_ERROR;
554 break;
555
556 case TLS_ST_CW_CHANGE:
557 s->session->cipher = s->s3->tmp.new_cipher;
558#ifdef OPENSSL_NO_COMP
559 s->session->compress_meth = 0;
560#else
561 if (s->s3->tmp.new_compression == NULL)
562 s->session->compress_meth = 0;
563 else
564 s->session->compress_meth = s->s3->tmp.new_compression->id;
565#endif
566 if (!s->method->ssl3_enc->setup_key_block(s))
567 return WORK_ERROR;
568
569 if (!s->method->ssl3_enc->change_cipher_state(s,
570 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
571 return WORK_ERROR;
572
573 if (SSL_IS_DTLS(s)) {
574#ifndef OPENSSL_NO_SCTP
575 if (s->hit) {
576 /*
577 * Change to new shared key of SCTP-Auth, will be ignored if
578 * no SCTP used.
579 */
580 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
581 0, NULL);
582 }
583#endif
584
585 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
586 }
587 break;
588
589 case TLS_ST_CW_FINISHED:
590#ifndef OPENSSL_NO_SCTP
591 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
592 /*
593 * Change to new shared key of SCTP-Auth, will be ignored if
594 * no SCTP used.
595 */
596 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
597 0, NULL);
598 }
599#endif
600 if (statem_flush(s) != 1)
601 return WORK_MORE_B;
602
603 if (s->hit && tls_finish_handshake(s, WORK_MORE_A) != 1)
604 return WORK_ERROR;
605 break;
606
607 default:
608 /* No post work to be done */
609 break;
610 }
611
612 return WORK_FINISHED_CONTINUE;
613}
614
615/*
616 * Construct a message to be sent from the client to the server.
617 *
618 * Valid return values are:
619 * 1: Success
620 * 0: Error
621 */
622int client_construct_message(SSL *s)
623{
624 STATEM *st = &s->statem;
625
626 switch(st->hand_state) {
627 case TLS_ST_CW_CLNT_HELLO:
628 return tls_construct_client_hello(s);
629
630 case TLS_ST_CW_CERT:
631 return tls_construct_client_certificate(s);
632
633 case TLS_ST_CW_KEY_EXCH:
634 return tls_construct_client_key_exchange(s);
635
636 case TLS_ST_CW_CERT_VRFY:
637 return tls_construct_client_verify(s);
638
639 case TLS_ST_CW_CHANGE:
640 if (SSL_IS_DTLS(s))
641 return dtls_construct_change_cipher_spec(s);
642 else
643 return tls_construct_change_cipher_spec(s);
644
645#if !defined(OPENSSL_NO_NEXTPROTONEG)
646 case TLS_ST_CW_NEXT_PROTO:
647 return tls_construct_next_proto(s);
648#endif
649 case TLS_ST_CW_FINISHED:
650 return tls_construct_finished(s,
651 s->method->
652 ssl3_enc->client_finished_label,
653 s->method->
654 ssl3_enc->client_finished_label_len);
655
656 default:
657 /* Shouldn't happen */
658 break;
659 }
660
661 return 0;
662}
663
664/*
665 * Returns the maximum allowed length for the current message that we are
666 * reading. Excludes the message header.
667 */
668unsigned long client_max_message_size(SSL *s)
669{
670 STATEM *st = &s->statem;
671
672 switch(st->hand_state) {
673 case TLS_ST_CR_SRVR_HELLO:
674 return SERVER_HELLO_MAX_LENGTH;
675
676 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
677 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
678
679 case TLS_ST_CR_CERT:
680 return s->max_cert_list;
681
682 case TLS_ST_CR_CERT_STATUS:
683 return SSL3_RT_MAX_PLAIN_LENGTH;
684
685 case TLS_ST_CR_KEY_EXCH:
686 return SERVER_KEY_EXCH_MAX_LENGTH;
687
688 case TLS_ST_CR_CERT_REQ:
689 return SSL3_RT_MAX_PLAIN_LENGTH;
690
691 case TLS_ST_CR_SRVR_DONE:
692 return SERVER_HELLO_DONE_MAX_LENGTH;
693
694 case TLS_ST_CR_CHANGE:
695 return CCS_MAX_LENGTH;
696
697 case TLS_ST_CR_SESSION_TICKET:
698 return SSL3_RT_MAX_PLAIN_LENGTH;
699
700 case TLS_ST_CR_FINISHED:
701 return FINISHED_MAX_LENGTH;
702
703 default:
704 /* Shouldn't happen */
705 break;
706 }
707
708 return 0;
709}
710
711/*
712 * Process a message that the client has been received from the server.
713 */
714enum MSG_PROCESS_RETURN client_process_message(SSL *s, PACKET *pkt)
715{
716 STATEM *st = &s->statem;
717
718 switch(st->hand_state) {
719 case TLS_ST_CR_SRVR_HELLO:
720 return tls_process_server_hello(s, pkt);
721
722 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
723 return dtls_process_hello_verify(s, pkt);
724
725 case TLS_ST_CR_CERT:
726 return tls_process_server_certificate(s, pkt);
727
728 case TLS_ST_CR_CERT_STATUS:
729 return tls_process_cert_status(s, pkt);
730
731 case TLS_ST_CR_KEY_EXCH:
732 return tls_process_key_exchange(s, pkt);
733
734 case TLS_ST_CR_CERT_REQ:
735 return tls_process_certificate_request(s, pkt);
736
737 case TLS_ST_CR_SRVR_DONE:
738 return tls_process_server_done(s, pkt);
739
740 case TLS_ST_CR_CHANGE:
741 return tls_process_change_cipher_spec(s, pkt);
742
743 case TLS_ST_CR_SESSION_TICKET:
744 return tls_process_new_session_ticket(s, pkt);
745
746 case TLS_ST_CR_FINISHED:
747 return tls_process_finished(s, pkt);
748
749 default:
750 /* Shouldn't happen */
751 break;
752 }
753
754 return MSG_PROCESS_ERROR;
755}
756
757/*
758 * Perform any further processing required following the receipt of a message
759 * from the server
760 */
761enum WORK_STATE client_post_process_message(SSL *s, enum WORK_STATE wst)
762{
763 STATEM *st = &s->statem;
764
765 switch(st->hand_state) {
766#ifndef OPENSSL_NO_SCTP
767 case TLS_ST_CR_SRVR_DONE:
768 /* We only get here if we are using SCTP and we are renegotiating */
769 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
770 s->s3->in_read_app_data = 2;
771 s->rwstate = SSL_READING;
772 BIO_clear_retry_flags(SSL_get_rbio(s));
773 BIO_set_retry_read(SSL_get_rbio(s));
774 statem_set_sctp_read_sock(s, 1);
775 return WORK_MORE_A;
776 }
777 statem_set_sctp_read_sock(s, 0);
778 return WORK_FINISHED_STOP;
779#endif
780
781 case TLS_ST_CR_FINISHED:
782 if (!s->hit)
783 return tls_finish_handshake(s, wst);
784 else
785 return WORK_FINISHED_STOP;
786 default:
787 break;
788 }
789
790 /* Shouldn't happen */
791 return WORK_ERROR;
792}
793
d45ba43d
MC
794/*
795 * Work out what version we should be using for the initial ClientHello if
796 * the version is currently set to (D)TLS_ANY_VERSION.
797 * Returns 1 on success
798 * Returns 0 on error
799 */
800static int ssl_set_version(SSL *s)
801{
802 unsigned long mask, options = s->options;
803
804 if (s->method->version == TLS_ANY_VERSION) {
805 /*
806 * SSL_OP_NO_X disables all protocols above X *if* there are
807 * some protocols below X enabled. This is required in order
808 * to maintain "version capability" vector contiguous. So
809 * that if application wants to disable TLS1.0 in favour of
810 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
811 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
812 */
813 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
814#if !defined(OPENSSL_NO_SSL3)
815 | SSL_OP_NO_SSLv3
816#endif
817 ;
818#if !defined(OPENSSL_NO_TLS1_2_CLIENT)
819 if (options & SSL_OP_NO_TLSv1_2) {
820 if ((options & mask) != mask) {
821 s->version = TLS1_1_VERSION;
822 } else {
823 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
824 return 0;
825 }
826 } else {
827 s->version = TLS1_2_VERSION;
828 }
829#else
830 if ((options & mask) == mask) {
831 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
832 return 0;
833 }
834 s->version = TLS1_1_VERSION;
835#endif
836
837 mask &= ~SSL_OP_NO_TLSv1_1;
838 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
839 s->version = TLS1_VERSION;
840 mask &= ~SSL_OP_NO_TLSv1;
841#if !defined(OPENSSL_NO_SSL3)
842 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
843 s->version = SSL3_VERSION;
844#endif
845
846 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
847 SSLerr(SSL_F_SSL_SET_VERSION,
848 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
849 return 0;
850 }
851
852 if (s->version == SSL3_VERSION && FIPS_mode()) {
853 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
854 return 0;
855 }
856
857 } else if (s->method->version == DTLS_ANY_VERSION) {
858 /* Determine which DTLS version to use */
859 /* If DTLS 1.2 disabled correct the version number */
860 if (options & SSL_OP_NO_DTLSv1_2) {
861 if (tls1_suiteb(s)) {
862 SSLerr(SSL_F_SSL_SET_VERSION,
863 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
864 return 0;
865 }
866 /*
867 * Disabling all versions is silly: return an error.
868 */
869 if (options & SSL_OP_NO_DTLSv1) {
870 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
871 return 0;
872 }
873 /*
874 * Update method so we don't use any DTLS 1.2 features.
875 */
876 s->method = DTLSv1_client_method();
877 s->version = DTLS1_VERSION;
878 } else {
879 /*
880 * We only support one version: update method
881 */
882 if (options & SSL_OP_NO_DTLSv1)
883 s->method = DTLSv1_2_client_method();
884 s->version = DTLS1_2_VERSION;
885 }
886 }
887
888 s->client_version = s->version;
889
890 return 1;
891}
892
b9908bf9 893int tls_construct_client_hello(SSL *s)
0f113f3e
MC
894{
895 unsigned char *buf;
896 unsigned char *p, *d;
897 int i;
898 unsigned long l;
899 int al = 0;
09b6c2ef 900#ifndef OPENSSL_NO_COMP
0f113f3e
MC
901 int j;
902 SSL_COMP *comp;
903#endif
b9908bf9 904 SSL_SESSION *sess = s->session;
0f113f3e
MC
905
906 buf = (unsigned char *)s->init_buf->data;
0f113f3e 907
b9908bf9
MC
908 /* Work out what SSL/TLS/DTLS version to use */
909 if (ssl_set_version(s) == 0)
910 goto err;
0f113f3e 911
b9908bf9 912 if ((sess == NULL) || (sess->ssl_version != s->version) ||
0f113f3e 913 /*
b9908bf9
MC
914 * In the case of EAP-FAST, we can have a pre-shared
915 * "ticket" without a session ID.
0f113f3e 916 */
b9908bf9
MC
917 (!sess->session_id_length && !sess->tlsext_tick) ||
918 (sess->not_resumable)) {
919 if (!ssl_get_new_session(s, 0))
e1b568dd 920 goto err;
b9908bf9
MC
921 }
922 /* else use the pre-loaded session */
0f113f3e 923
b9908bf9 924 p = s->s3->client_random;
0f113f3e 925
b9908bf9
MC
926 /*
927 * for DTLS if client_random is initialized, reuse it, we are
928 * required to use same upon reply to HelloVerify
929 */
930 if (SSL_IS_DTLS(s)) {
931 size_t idx;
932 i = 1;
933 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
934 if (p[idx]) {
935 i = 0;
936 break;
0f113f3e 937 }
0f113f3e 938 }
b9908bf9
MC
939 } else
940 i = 1;
0f113f3e 941
b9908bf9
MC
942 if (i && ssl_fill_hello_random(s, 0, p,
943 sizeof(s->s3->client_random)) <= 0)
944 goto err;
945
946 /* Do the message type and length last */
947 d = p = ssl_handshake_start(s);
948
949 /*-
950 * version indicates the negotiated version: for example from
951 * an SSLv2/v3 compatible client hello). The client_version
952 * field is the maximum version we permit and it is also
953 * used in RSA encrypted premaster secrets. Some servers can
954 * choke if we initially report a higher version then
955 * renegotiate to a lower one in the premaster secret. This
956 * didn't happen with TLS 1.0 as most servers supported it
957 * but it can with TLS 1.1 or later if the server only supports
958 * 1.0.
959 *
960 * Possible scenario with previous logic:
961 * 1. Client hello indicates TLS 1.2
962 * 2. Server hello says TLS 1.0
963 * 3. RSA encrypted premaster secret uses 1.2.
964 * 4. Handhaked proceeds using TLS 1.0.
965 * 5. Server sends hello request to renegotiate.
966 * 6. Client hello indicates TLS v1.0 as we now
967 * know that is maximum server supports.
968 * 7. Server chokes on RSA encrypted premaster secret
969 * containing version 1.0.
970 *
971 * For interoperability it should be OK to always use the
972 * maximum version we support in client hello and then rely
973 * on the checking of version to ensure the servers isn't
974 * being inconsistent: for example initially negotiating with
975 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
976 * client_version in client hello and not resetting it to
977 * the negotiated version.
978 */
979 *(p++) = s->client_version >> 8;
980 *(p++) = s->client_version & 0xff;
981
982 /* Random stuff */
983 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
984 p += SSL3_RANDOM_SIZE;
985
986 /* Session ID */
987 if (s->new_session)
988 i = 0;
989 else
990 i = s->session->session_id_length;
991 *(p++) = i;
992 if (i != 0) {
993 if (i > (int)sizeof(s->session->session_id)) {
994 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
995 goto err;
0f113f3e 996 }
b9908bf9
MC
997 memcpy(p, s->session->session_id, i);
998 p += i;
999 }
0f113f3e 1000
b9908bf9
MC
1001 /* cookie stuff for DTLS */
1002 if (SSL_IS_DTLS(s)) {
1003 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
1004 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
1005 goto err;
1006 }
b9908bf9
MC
1007 *(p++) = s->d1->cookie_len;
1008 memcpy(p, s->d1->cookie, s->d1->cookie_len);
1009 p += s->d1->cookie_len;
1010 }
1011
1012 /* Ciphers supported */
1013 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
1014 if (i == 0) {
1015 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
1016 goto err;
1017 }
800e1cd9 1018#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
b9908bf9
MC
1019 /*
1020 * Some servers hang if client hello > 256 bytes as hack workaround
1021 * chop number of supported ciphers to keep it well below this if we
1022 * use TLS v1.2
1023 */
1024 if (TLS1_get_version(s) >= TLS1_2_VERSION
1025 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
1026 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
0f113f3e 1027#endif
b9908bf9
MC
1028 s2n(i, p);
1029 p += i;
0f113f3e 1030
b9908bf9 1031 /* COMPRESSION */
09b6c2ef 1032#ifdef OPENSSL_NO_COMP
b9908bf9 1033 *(p++) = 1;
09b6c2ef 1034#else
566dda07 1035
b9908bf9
MC
1036 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
1037 j = 0;
1038 else
1039 j = sk_SSL_COMP_num(s->ctx->comp_methods);
1040 *(p++) = 1 + j;
1041 for (i = 0; i < j; i++) {
1042 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
1043 *(p++) = comp->id;
1044 }
09b6c2ef 1045#endif
b9908bf9 1046 *(p++) = 0; /* Add the NULL method */
761772d7 1047
b9908bf9
MC
1048 /* TLS extensions */
1049 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
1050 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1051 goto err;
1052 }
1053 if ((p =
1054 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1055 &al)) == NULL) {
1056 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1057 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1058 goto err;
1059 }
0f113f3e 1060
b9908bf9
MC
1061 l = p - d;
1062 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
1063 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1064 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1065 goto err;
0f113f3e
MC
1066 }
1067
b9908bf9 1068 return 1;
0f113f3e 1069 err:
b9908bf9
MC
1070 statem_set_error(s);
1071 return 0;
0f113f3e 1072}
d02b48c6 1073
8ba708e5
MC
1074enum MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
1075{
1076 int al;
1077 unsigned int cookie_len;
1078 PACKET cookiepkt;
1079
1080 if (!PACKET_forward(pkt, 2)
1081 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1082 al = SSL_AD_DECODE_ERROR;
1083 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1084 goto f_err;
1085 }
1086
1087 cookie_len = PACKET_remaining(&cookiepkt);
1088 if (cookie_len > sizeof(s->d1->cookie)) {
1089 al = SSL_AD_ILLEGAL_PARAMETER;
1090 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
1091 goto f_err;
1092 }
1093
1094 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1095 al = SSL_AD_DECODE_ERROR;
1096 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1097 goto f_err;
1098 }
1099 s->d1->cookie_len = cookie_len;
1100
1101 return MSG_PROCESS_FINISHED_READING;
1102 f_err:
1103 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1104 statem_set_error(s);
1105 return MSG_PROCESS_ERROR;
1106}
1107
73999b62 1108enum MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
b9908bf9
MC
1109{
1110 STACK_OF(SSL_CIPHER) *sk;
1111 const SSL_CIPHER *c;
73999b62 1112 PACKET session_id;
b9908bf9
MC
1113 size_t session_id_len;
1114 unsigned char *cipherchars;
1115 int i, al = SSL_AD_INTERNAL_ERROR;
1116 unsigned int compression;
1117#ifndef OPENSSL_NO_COMP
1118 SSL_COMP *comp;
1119#endif
1120
13c9bb3e 1121 if (s->method->version == TLS_ANY_VERSION) {
50932c4a
MC
1122 unsigned int sversion;
1123
73999b62 1124 if (!PACKET_get_net_2(pkt, &sversion)) {
50932c4a 1125 al = SSL_AD_DECODE_ERROR;
b9908bf9 1126 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
1127 goto f_err;
1128 }
13c9bb3e
MC
1129
1130#if TLS_MAX_VERSION != TLS1_2_VERSION
1131#error Code needs updating for new TLS version
1132#endif
1133#ifndef OPENSSL_NO_SSL3
1134 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
1135 if (FIPS_mode()) {
b9908bf9 1136 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
13c9bb3e 1137 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
d45ba43d
MC
1138 al = SSL_AD_PROTOCOL_VERSION;
1139 goto f_err;
13c9bb3e
MC
1140 }
1141 s->method = SSLv3_client_method();
1142 } else
1143#endif
1144 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1145 s->method = TLSv1_client_method();
1146 } else if ((sversion == TLS1_1_VERSION) &&
1147 !(s->options & SSL_OP_NO_TLSv1_1)) {
1148 s->method = TLSv1_1_client_method();
1149 } else if ((sversion == TLS1_2_VERSION) &&
1150 !(s->options & SSL_OP_NO_TLSv1_2)) {
1151 s->method = TLSv1_2_client_method();
1152 } else {
b9908bf9 1153 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
d45ba43d
MC
1154 al = SSL_AD_PROTOCOL_VERSION;
1155 goto f_err;
13c9bb3e
MC
1156 }
1157 s->session->ssl_version = s->version = s->method->version;
1158
1159 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
b9908bf9 1160 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
d45ba43d
MC
1161 al = SSL_AD_PROTOCOL_VERSION;
1162 goto f_err;
13c9bb3e
MC
1163 }
1164 } else if (s->method->version == DTLS_ANY_VERSION) {
0f113f3e 1165 /* Work out correct protocol version to use */
50932c4a
MC
1166 unsigned int hversion;
1167 int options;
1168
73999b62 1169 if (!PACKET_get_net_2(pkt, &hversion)) {
50932c4a 1170 al = SSL_AD_DECODE_ERROR;
b9908bf9 1171 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
1172 goto f_err;
1173 }
1174
1175 options = s->options;
0f113f3e
MC
1176 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1177 s->method = DTLSv1_2_client_method();
1178 else if (tls1_suiteb(s)) {
b9908bf9 1179 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1180 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1181 s->version = hversion;
1182 al = SSL_AD_PROTOCOL_VERSION;
1183 goto f_err;
1184 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1185 s->method = DTLSv1_client_method();
1186 else {
b9908bf9 1187 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
0f113f3e
MC
1188 s->version = hversion;
1189 al = SSL_AD_PROTOCOL_VERSION;
1190 goto f_err;
1191 }
7322abf5 1192 s->session->ssl_version = s->version = s->method->version;
50932c4a
MC
1193 } else {
1194 unsigned char *vers;
1195
73999b62 1196 if (!PACKET_get_bytes(pkt, &vers, 2)) {
50932c4a 1197 al = SSL_AD_DECODE_ERROR;
b9908bf9 1198 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
1199 goto f_err;
1200 }
1201 if ((vers[0] != (s->version >> 8))
1202 || (vers[1] != (s->version & 0xff))) {
b9908bf9 1203 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
50932c4a
MC
1204 s->version = (s->version & 0xff00) | vers[1];
1205 al = SSL_AD_PROTOCOL_VERSION;
1206 goto f_err;
1207 }
0f113f3e 1208 }
0f113f3e
MC
1209
1210 /* load the server hello data */
1211 /* load the server random */
73999b62 1212 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
50932c4a 1213 al = SSL_AD_DECODE_ERROR;
b9908bf9 1214 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
50932c4a
MC
1215 goto f_err;
1216 }
0f113f3e
MC
1217
1218 s->hit = 0;
1219
fc5ce51d 1220 /* Get the session-id. */
73999b62 1221 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
fc5ce51d
EK
1222 al = SSL_AD_DECODE_ERROR;
1223 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1224 goto f_err;
1225 }
1226 session_id_len = PACKET_remaining(&session_id);
1227 if (session_id_len > sizeof s->session->session_id
1228 || session_id_len > SSL3_SESSION_ID_SIZE) {
0f113f3e 1229 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1230 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
0f113f3e
MC
1231 goto f_err;
1232 }
e481f9b9 1233
73999b62 1234 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
fc5ce51d
EK
1235 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1236 al = SSL_AD_DECODE_ERROR;
1237 goto f_err;
1238 }
1239
0f113f3e 1240 /*
6e3d0153
EK
1241 * Check if we can resume the session based on external pre-shared secret.
1242 * EAP-FAST (RFC 4851) supports two types of session resumption.
1243 * Resumption based on server-side state works with session IDs.
1244 * Resumption based on pre-shared Protected Access Credentials (PACs)
1245 * works by overriding the SessionTicket extension at the application
1246 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1247 * servers would honour the session ID.) Therefore, the session ID alone
1248 * is not a reliable indicator of session resumption, so we first check if
1249 * we can resume, and later peek at the next handshake message to see if the
1250 * server wants to resume.
0f113f3e 1251 */
6e3d0153
EK
1252 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1253 s->session->tlsext_tick) {
0f113f3e
MC
1254 SSL_CIPHER *pref_cipher = NULL;
1255 s->session->master_key_length = sizeof(s->session->master_key);
1256 if (s->tls_session_secret_cb(s, s->session->master_key,
1257 &s->session->master_key_length,
1258 NULL, &pref_cipher,
1259 s->tls_session_secret_cb_arg)) {
1260 s->session->cipher = pref_cipher ?
50932c4a 1261 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
6e3d0153 1262 } else {
b9908bf9 1263 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
6e3d0153
EK
1264 al = SSL_AD_INTERNAL_ERROR;
1265 goto f_err;
0f113f3e 1266 }
50932c4a
MC
1267 }
1268
fc5ce51d
EK
1269 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1270 && memcmp(PACKET_data(&session_id), s->session->session_id,
1271 session_id_len) == 0) {
0f113f3e
MC
1272 if (s->sid_ctx_length != s->session->sid_ctx_length
1273 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1274 /* actually a client application bug */
1275 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1276 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1277 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1278 goto f_err;
1279 }
1280 s->hit = 1;
6e3d0153 1281 } else {
0f113f3e 1282 /*
6e3d0153
EK
1283 * If we were trying for session-id reuse but the server
1284 * didn't echo the ID, make a new SSL_SESSION.
1285 * In the case of EAP-FAST and PAC, we do not send a session ID,
1286 * so the PAC-based session secret is always preserved. It'll be
1287 * overwritten if the server refuses resumption.
0f113f3e
MC
1288 */
1289 if (s->session->session_id_length > 0) {
1290 if (!ssl_get_new_session(s, 0)) {
1291 goto f_err;
1292 }
1293 }
50932c4a 1294
fc5ce51d
EK
1295 s->session->session_id_length = session_id_len;
1296 /* session_id_len could be 0 */
1297 memcpy(s->session->session_id, PACKET_data(&session_id),
1298 session_id_len);
0f113f3e 1299 }
fc5ce51d 1300
50932c4a 1301 c = ssl_get_cipher_by_char(s, cipherchars);
0f113f3e
MC
1302 if (c == NULL) {
1303 /* unknown cipher */
1304 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1305 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
0f113f3e
MC
1306 goto f_err;
1307 }
1308 /* Set version disabled mask now we know version */
1309 if (!SSL_USE_TLS1_2_CIPHERS(s))
4d69f9e6 1310 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
0f113f3e 1311 else
4d69f9e6 1312 s->s3->tmp.mask_ssl = 0;
0f113f3e
MC
1313 /*
1314 * If it is a disabled cipher we didn't send it in client hello, so
1315 * return an error.
1316 */
1317 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1318 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1319 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
0f113f3e
MC
1320 goto f_err;
1321 }
0f113f3e
MC
1322
1323 sk = ssl_get_ciphers_by_id(s);
1324 i = sk_SSL_CIPHER_find(sk, c);
1325 if (i < 0) {
1326 /* we did not say we would use this cipher */
1327 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1328 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
0f113f3e
MC
1329 goto f_err;
1330 }
1331
1332 /*
1333 * Depending on the session caching (internal/external), the cipher
1334 * and/or cipher_id values may not be set. Make sure that cipher_id is
1335 * set and use it for comparison.
1336 */
1337 if (s->session->cipher)
1338 s->session->cipher_id = s->session->cipher->id;
1339 if (s->hit && (s->session->cipher_id != c->id)) {
9e9858d1 1340 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1341 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
9e9858d1
RS
1342 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1343 goto f_err;
0f113f3e
MC
1344 }
1345 s->s3->tmp.new_cipher = c;
1346 /*
1347 * Don't digest cached records if no sigalgs: we may need them for client
1348 * authentication.
1349 */
124037fd 1350 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
0f113f3e
MC
1351 goto f_err;
1352 /* lets get the compression algorithm */
1353 /* COMPRESSION */
73999b62 1354 if (!PACKET_get_1(pkt, &compression)) {
50932c4a
MC
1355 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1356 al = SSL_AD_DECODE_ERROR;
1357 goto f_err;
1358 }
09b6c2ef 1359#ifdef OPENSSL_NO_COMP
fc5ce51d 1360 if (compression != 0) {
0f113f3e 1361 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1362 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1363 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1364 goto f_err;
1365 }
1366 /*
1367 * If compression is disabled we'd better not try to resume a session
1368 * using compression.
1369 */
1370 if (s->session->compress_meth != 0) {
b9908bf9 1371 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
0f113f3e
MC
1372 goto f_err;
1373 }
09b6c2ef 1374#else
fc5ce51d 1375 if (s->hit && compression != s->session->compress_meth) {
0f113f3e 1376 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1377 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1378 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1379 goto f_err;
1380 }
fc5ce51d 1381 if (compression == 0)
0f113f3e
MC
1382 comp = NULL;
1383 else if (!ssl_allow_compression(s)) {
1384 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1385 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
0f113f3e 1386 goto f_err;
fc5ce51d
EK
1387 } else {
1388 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1389 }
0f113f3e 1390
fc5ce51d 1391 if (compression != 0 && comp == NULL) {
0f113f3e 1392 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1393 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
0f113f3e
MC
1394 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1395 goto f_err;
1396 } else {
1397 s->s3->tmp.new_compression = comp;
1398 }
09b6c2ef 1399#endif
761772d7 1400
0f113f3e 1401 /* TLS extensions */
73999b62 1402 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
b9908bf9 1403 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
0f113f3e
MC
1404 goto err;
1405 }
0f113f3e 1406
73999b62 1407 if (PACKET_remaining(pkt) != 0) {
0f113f3e
MC
1408 /* wrong packet length */
1409 al = SSL_AD_DECODE_ERROR;
b9908bf9 1410 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
0f113f3e
MC
1411 goto f_err;
1412 }
1413
8723588e
MC
1414#ifndef OPENSSL_NO_SCTP
1415 if (SSL_IS_DTLS(s) && s->hit) {
1416 unsigned char sctpauthkey[64];
1417 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1418
1419 /*
1420 * Add new shared key for SCTP-Auth, will be ignored if
1421 * no SCTP used.
1422 */
1423 snprintf((char *)labelbuffer,
1424 sizeof(DTLS1_SCTP_AUTH_LABEL),
1425 DTLS1_SCTP_AUTH_LABEL);
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:
b9908bf9
MC
1444 statem_set_error(s);
1445 return MSG_PROCESS_ERROR;
0f113f3e 1446}
d02b48c6 1447
73999b62 1448enum MSG_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
MC
1541 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1542 if (exp_idx >= 0 && i != exp_idx) {
1543 x = NULL;
1544 al = SSL_AD_ILLEGAL_PARAMETER;
b9908bf9 1545 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
55a9a16f
MC
1546 SSL_R_WRONG_CERTIFICATE_TYPE);
1547 goto f_err;
0f113f3e 1548 }
a273c6ee 1549 s->session->peer_type = i;
55a9a16f
MC
1550
1551 X509_free(s->session->peer);
05f0fb9f 1552 X509_up_ref(x);
55a9a16f 1553 s->session->peer = x;
0f113f3e
MC
1554 s->session->verify_result = s->verify_result;
1555
1556 x = NULL;
b9908bf9 1557 ret = MSG_PROCESS_CONTINUE_READING;
66696478
RS
1558 goto done;
1559
0f113f3e 1560 f_err:
66696478 1561 ssl3_send_alert(s, SSL3_AL_FATAL, al);
cc273a93 1562 err:
b9908bf9 1563 statem_set_error(s);
66696478 1564 done:
0f113f3e
MC
1565 EVP_PKEY_free(pkey);
1566 X509_free(x);
1567 sk_X509_pop_free(sk, X509_free);
b9908bf9 1568 return ret;
0f113f3e 1569}
d02b48c6 1570
73999b62 1571enum MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
b9908bf9
MC
1572{
1573#ifndef OPENSSL_NO_RSA
1574 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1575#endif
1576 EVP_MD_CTX md_ctx;
1577 int al, j, verify_ret;
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
1588 EC_KEY *ecdh = NULL;
1589 BN_CTX *bn_ctx = NULL;
1590 EC_POINT *srvr_ecpoint = NULL;
1591 int curve_nid = 0;
1592#endif
73999b62 1593 PACKET save_param_start, signature;
b9908bf9
MC
1594
1595 EVP_MD_CTX_init(&md_ctx);
1596
1597 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1598
73999b62 1599 save_param_start = *pkt;
8d92c1f8 1600
bc36ee62 1601#ifndef OPENSSL_NO_RSA
8d92c1f8
DSH
1602 RSA_free(s->s3->peer_rsa_tmp);
1603 s->s3->peer_rsa_tmp = NULL;
d02b48c6 1604#endif
bc36ee62 1605#ifndef OPENSSL_NO_DH
8d92c1f8
DSH
1606 DH_free(s->s3->peer_dh_tmp);
1607 s->s3->peer_dh_tmp = NULL;
ea262260 1608#endif
10bf4fc2 1609#ifndef OPENSSL_NO_EC
8d92c1f8
DSH
1610 EC_KEY_free(s->s3->peer_ecdh_tmp);
1611 s->s3->peer_ecdh_tmp = NULL;
d02b48c6 1612#endif
8d92c1f8 1613
0f113f3e 1614 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
d02b48c6 1615
0f113f3e 1616 al = SSL_AD_DECODE_ERROR;
f2be92b9 1617
ddac1974 1618#ifndef OPENSSL_NO_PSK
7689082b
DSH
1619 /* PSK ciphersuites are preceded by an identity hint */
1620 if (alg_k & SSL_PSK) {
32942870 1621 PACKET psk_identity_hint;
73999b62 1622 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
32942870 1623 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1624 goto f_err;
1625 }
0f113f3e
MC
1626
1627 /*
1628 * Store PSK identity hint for later use, hint is used in
1629 * ssl3_send_client_key_exchange. Assume that the maximum length of
1630 * a PSK identity hint can be as long as the maximum length of a PSK
1631 * identity.
1632 */
32942870 1633 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
0f113f3e 1634 al = SSL_AD_HANDSHAKE_FAILURE;
b9908bf9 1635 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
0f113f3e
MC
1636 goto f_err;
1637 }
8098fc56 1638
32942870
EK
1639 if (!PACKET_strndup(&psk_identity_hint,
1640 &s->session->psk_identity_hint)) {
1641 al = SSL_AD_INTERNAL_ERROR;
1642 goto f_err;
0f113f3e 1643 }
7689082b
DSH
1644 }
1645
1646 /* Nothing else to do for plain PSK or RSAPSK */
1647 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
0f113f3e
MC
1648 } else
1649#endif /* !OPENSSL_NO_PSK */
edc032b5 1650#ifndef OPENSSL_NO_SRP
0f113f3e 1651 if (alg_k & SSL_kSRP) {
32942870 1652 PACKET prime, generator, salt, server_pub;
73999b62
MC
1653 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1654 || !PACKET_get_length_prefixed_2(pkt, &generator)
1655 || !PACKET_get_length_prefixed_1(pkt, &salt)
1656 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
32942870 1657 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1658 goto f_err;
1659 }
0f113f3e 1660
32942870
EK
1661 if ((s->srp_ctx.N =
1662 BN_bin2bn(PACKET_data(&prime),
1663 PACKET_remaining(&prime), NULL)) == NULL
1664 || (s->srp_ctx.g =
1665 BN_bin2bn(PACKET_data(&generator),
1666 PACKET_remaining(&generator), NULL)) == NULL
1667 || (s->srp_ctx.s =
1668 BN_bin2bn(PACKET_data(&salt),
1669 PACKET_remaining(&salt), NULL)) == NULL
1670 || (s->srp_ctx.B =
1671 BN_bin2bn(PACKET_data(&server_pub),
1672 PACKET_remaining(&server_pub), NULL)) == NULL) {
0f113f3e
MC
1673 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1674 goto err;
1675 }
0f113f3e
MC
1676
1677 if (!srp_verify_server_param(s, &al)) {
b9908bf9 1678 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
0f113f3e
MC
1679 goto f_err;
1680 }
0989790b 1681
edc032b5 1682/* We must check if there is a certificate */
8df53b7a 1683 if (alg_a & (SSL_aRSA|SSL_aDSS))
a273c6ee 1684 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1685 } else
1686#endif /* !OPENSSL_NO_SRP */
edc032b5 1687#ifndef OPENSSL_NO_RSA
0f113f3e 1688 if (alg_k & SSL_kRSA) {
32942870 1689 PACKET mod, exp;
0f113f3e
MC
1690 /* Temporary RSA keys only allowed in export ciphersuites */
1691 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1692 al = SSL_AD_UNEXPECTED_MESSAGE;
b9908bf9 1693 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
0f113f3e
MC
1694 goto f_err;
1695 }
0f113f3e 1696
73999b62
MC
1697 if (!PACKET_get_length_prefixed_2(pkt, &mod)
1698 || !PACKET_get_length_prefixed_2(pkt, &exp)) {
32942870 1699 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1700 goto f_err;
1701 }
0f113f3e 1702
32942870
EK
1703 if ((rsa = RSA_new()) == NULL) {
1704 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1705 goto err;
1706 }
0f113f3e 1707
32942870
EK
1708 if ((rsa->n = BN_bin2bn(PACKET_data(&mod), PACKET_remaining(&mod),
1709 rsa->n)) == NULL
1710 || (rsa->e = BN_bin2bn(PACKET_data(&exp), PACKET_remaining(&exp),
1711 rsa->e)) == NULL) {
0f113f3e
MC
1712 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1713 goto err;
1714 }
0f113f3e
MC
1715
1716 /* this should be because we are using an export cipher */
1717 if (alg_a & SSL_aRSA)
a273c6ee 1718 pkey = X509_get_pubkey(s->session->peer);
0f113f3e 1719 else {
b9908bf9 1720 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
1721 goto err;
1722 }
1dece951
KR
1723
1724 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1725 al = SSL_AD_UNEXPECTED_MESSAGE;
b9908bf9 1726 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1dece951
KR
1727 goto f_err;
1728 }
1729
8d92c1f8 1730 s->s3->peer_rsa_tmp = rsa;
0f113f3e
MC
1731 rsa = NULL;
1732 }
1733#else /* OPENSSL_NO_RSA */
1734 if (0) ;
d02b48c6 1735#endif
bc36ee62 1736#ifndef OPENSSL_NO_DH
7689082b 1737 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
32942870 1738 PACKET prime, generator, pub_key;
0f113f3e 1739
73999b62
MC
1740 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1741 || !PACKET_get_length_prefixed_2(pkt, &generator)
1742 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
32942870 1743 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1744 goto f_err;
1745 }
0f113f3e 1746
32942870
EK
1747 if ((dh = DH_new()) == NULL) {
1748 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
0f113f3e
MC
1749 goto err;
1750 }
0f113f3e 1751
32942870
EK
1752 if ((dh->p = BN_bin2bn(PACKET_data(&prime),
1753 PACKET_remaining(&prime), NULL)) == NULL
1754 || (dh->g = BN_bin2bn(PACKET_data(&generator),
1755 PACKET_remaining(&generator), NULL)) == NULL
1756 || (dh->pub_key =
1757 BN_bin2bn(PACKET_data(&pub_key),
1758 PACKET_remaining(&pub_key), NULL)) == NULL) {
0f113f3e
MC
1759 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1760 goto err;
1761 }
0f113f3e 1762
32942870
EK
1763 if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
1764 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
61e72d76
GL
1765 goto f_err;
1766 }
1767
0f113f3e
MC
1768 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1769 al = SSL_AD_HANDSHAKE_FAILURE;
b9908bf9 1770 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
0f113f3e
MC
1771 goto f_err;
1772 }
8df53b7a 1773 if (alg_a & (SSL_aRSA|SSL_aDSS))
a273c6ee 1774 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1775 /* else anonymous DH, so no certificate or pkey. */
1776
8d92c1f8 1777 s->s3->peer_dh_tmp = dh;
0f113f3e 1778 dh = NULL;
0f113f3e
MC
1779 }
1780#endif /* !OPENSSL_NO_DH */
ea262260 1781
10bf4fc2 1782#ifndef OPENSSL_NO_EC
7689082b 1783 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
0f113f3e
MC
1784 EC_GROUP *ngroup;
1785 const EC_GROUP *group;
32942870
EK
1786 PACKET encoded_pt;
1787 unsigned char *ecparams;
0f113f3e
MC
1788
1789 if ((ecdh = EC_KEY_new()) == NULL) {
b9908bf9 1790 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1791 goto err;
1792 }
1793
1794 /*
1795 * Extract elliptic curve parameters and the server's ephemeral ECDH
32942870 1796 * public key. For now we only support named (not generic) curves and
8098fc56 1797 * ECParameters in this case is just three bytes.
0f113f3e 1798 */
73999b62 1799 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
0f113f3e
MC
1800 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1801 goto f_err;
1802 }
1803 /*
1804 * Check curve is one of our preferences, if not server has sent an
1805 * invalid curve. ECParameters is 3 bytes.
1806 */
32942870 1807 if (!tls1_check_curve(s, ecparams, 3)) {
0f113f3e
MC
1808 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1809 goto f_err;
1810 }
1811
32942870 1812 if ((curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2))) == 0) {
0f113f3e 1813 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 1814 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
0f113f3e
MC
1815 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1816 goto f_err;
1817 }
1818
1819 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1820 if (ngroup == NULL) {
b9908bf9 1821 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
0f113f3e
MC
1822 goto err;
1823 }
1824 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
b9908bf9 1825 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
0f113f3e
MC
1826 goto err;
1827 }
1828 EC_GROUP_free(ngroup);
1829
1830 group = EC_KEY_get0_group(ecdh);
1831
1832 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1833 (EC_GROUP_get_degree(group) > 163)) {
1834 al = SSL_AD_EXPORT_RESTRICTION;
b9908bf9 1835 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
0f113f3e
MC
1836 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1837 goto f_err;
1838 }
1839
0f113f3e
MC
1840 /* Next, get the encoded ECPoint */
1841 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1842 ((bn_ctx = BN_CTX_new()) == NULL)) {
b9908bf9 1843 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
1844 goto err;
1845 }
1846
73999b62 1847 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
32942870 1848 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
8098fc56
MC
1849 goto f_err;
1850 }
0f113f3e 1851
32942870
EK
1852 if (EC_POINT_oct2point(group, srvr_ecpoint, PACKET_data(&encoded_pt),
1853 PACKET_remaining(&encoded_pt), bn_ctx) == 0) {
0f113f3e
MC
1854 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1855 goto f_err;
1856 }
0f113f3e
MC
1857
1858 /*
1859 * The ECC/TLS specification does not mention the use of DSA to sign
1860 * ECParameters in the server key exchange message. We do support RSA
1861 * and ECDSA.
1862 */
1863 if (0) ;
1864# ifndef OPENSSL_NO_RSA
1865 else if (alg_a & SSL_aRSA)
a273c6ee 1866 pkey = X509_get_pubkey(s->session->peer);
0f113f3e 1867# endif
10bf4fc2 1868# ifndef OPENSSL_NO_EC
0f113f3e 1869 else if (alg_a & SSL_aECDSA)
a273c6ee 1870 pkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
1871# endif
1872 /* else anonymous ECDH, so no certificate or pkey. */
1873 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
8d92c1f8 1874 s->s3->peer_ecdh_tmp = ecdh;
0f113f3e
MC
1875 ecdh = NULL;
1876 BN_CTX_free(bn_ctx);
1877 bn_ctx = NULL;
1878 EC_POINT_free(srvr_ecpoint);
1879 srvr_ecpoint = NULL;
1880 } else if (alg_k) {
1881 al = SSL_AD_UNEXPECTED_MESSAGE;
b9908bf9 1882 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
0f113f3e
MC
1883 goto f_err;
1884 }
10bf4fc2 1885#endif /* !OPENSSL_NO_EC */
0f113f3e 1886
0f113f3e
MC
1887 /* if it was signed, check the signature */
1888 if (pkey != NULL) {
32942870
EK
1889 PACKET params;
1890 /*
1891 * |pkt| now points to the beginning of the signature, so the difference
1892 * equals the length of the parameters.
1893 */
1894 if (!PACKET_get_sub_packet(&save_param_start, &params,
1895 PACKET_remaining(&save_param_start) -
73999b62 1896 PACKET_remaining(pkt))) {
32942870
EK
1897 al = SSL_AD_INTERNAL_ERROR;
1898 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1899 goto f_err;
1900 }
1901
0f113f3e 1902 if (SSL_USE_SIGALGS(s)) {
32942870 1903 unsigned char *sigalgs;
0f113f3e 1904 int rv;
73999b62 1905 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
0f113f3e
MC
1906 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1907 goto f_err;
1908 }
32942870 1909 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
0f113f3e
MC
1910 if (rv == -1)
1911 goto err;
1912 else if (rv == 0) {
1913 goto f_err;
1914 }
a2f9200f 1915#ifdef SSL_DEBUG
0f113f3e
MC
1916 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1917#endif
32942870 1918 } else {
0f113f3e 1919 md = EVP_sha1();
32942870 1920 }
0f113f3e 1921
73999b62
MC
1922 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1923 || PACKET_remaining(pkt) != 0) {
32942870 1924 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
1925 goto f_err;
1926 }
0f113f3e 1927 j = EVP_PKEY_size(pkey);
8098fc56 1928 if (j < 0) {
b9908bf9 1929 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
8098fc56
MC
1930 goto f_err;
1931 }
0f113f3e
MC
1932
1933 /*
8098fc56 1934 * Check signature length
0f113f3e 1935 */
32942870 1936 if (PACKET_remaining(&signature) > (size_t)j) {
0f113f3e 1937 /* wrong packet length */
b9908bf9 1938 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
0f113f3e
MC
1939 goto f_err;
1940 }
bc36ee62 1941#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1942 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1943 int num;
1944 unsigned int size;
1945
1946 j = 0;
1947 q = md_buf;
1948 for (num = 2; num > 0; num--) {
1949 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1950 EVP_DigestInit_ex(&md_ctx, (num == 2)
1951 ? s->ctx->md5 : s->ctx->sha1, NULL);
1952 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1953 SSL3_RANDOM_SIZE);
1954 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1955 SSL3_RANDOM_SIZE);
32942870
EK
1956 EVP_DigestUpdate(&md_ctx, PACKET_data(&params),
1957 PACKET_remaining(&params));
0f113f3e
MC
1958 EVP_DigestFinal_ex(&md_ctx, q, &size);
1959 q += size;
1960 j += size;
1961 }
8098fc56 1962 verify_ret =
32942870
EK
1963 RSA_verify(NID_md5_sha1, md_buf, j, PACKET_data(&signature),
1964 PACKET_remaining(&signature), pkey->pkey.rsa);
8098fc56 1965 if (verify_ret < 0) {
0f113f3e 1966 al = SSL_AD_DECRYPT_ERROR;
b9908bf9 1967 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
0f113f3e
MC
1968 goto f_err;
1969 }
8098fc56 1970 if (verify_ret == 0) {
0f113f3e
MC
1971 /* bad signature */
1972 al = SSL_AD_DECRYPT_ERROR;
b9908bf9 1973 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
0f113f3e
MC
1974 goto f_err;
1975 }
1976 } else
1977#endif
1978 {
1979 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1980 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1981 SSL3_RANDOM_SIZE);
1982 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1983 SSL3_RANDOM_SIZE);
32942870
EK
1984 EVP_VerifyUpdate(&md_ctx, PACKET_data(&params),
1985 PACKET_remaining(&params));
1986 if (EVP_VerifyFinal(&md_ctx, PACKET_data(&signature),
1987 PACKET_remaining(&signature), pkey) <= 0) {
0f113f3e
MC
1988 /* bad signature */
1989 al = SSL_AD_DECRYPT_ERROR;
b9908bf9 1990 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
0f113f3e
MC
1991 goto f_err;
1992 }
1993 }
1994 } else {
7689082b
DSH
1995 /* aNULL, aSRP or PSK do not need public keys */
1996 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
0f113f3e
MC
1997 /* Might be wrong key type, check it */
1998 if (ssl3_check_cert_and_algorithm(s))
1999 /* Otherwise this shouldn't happen */
b9908bf9 2000 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
2001 goto err;
2002 }
2003 /* still data left over */
73999b62 2004 if (PACKET_remaining(pkt) != 0) {
b9908bf9 2005 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
0f113f3e
MC
2006 goto f_err;
2007 }
2008 }
2009 EVP_PKEY_free(pkey);
2010 EVP_MD_CTX_cleanup(&md_ctx);
b9908bf9 2011 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
2012 f_err:
2013 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2014 err:
2015 EVP_PKEY_free(pkey);
bc36ee62 2016#ifndef OPENSSL_NO_RSA
d6407083 2017 RSA_free(rsa);
6b521df3 2018#endif
bc36ee62 2019#ifndef OPENSSL_NO_DH
d6407083 2020 DH_free(dh);
ea262260 2021#endif
10bf4fc2 2022#ifndef OPENSSL_NO_EC
0f113f3e
MC
2023 BN_CTX_free(bn_ctx);
2024 EC_POINT_free(srvr_ecpoint);
8fdc3734 2025 EC_KEY_free(ecdh);
6b521df3 2026#endif
0f113f3e 2027 EVP_MD_CTX_cleanup(&md_ctx);
b9908bf9
MC
2028 statem_set_error(s);
2029 return MSG_PROCESS_ERROR;
0f113f3e 2030}
d02b48c6 2031
73999b62 2032enum MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
b9908bf9
MC
2033{
2034 int ret = MSG_PROCESS_ERROR;
2035 unsigned int list_len, ctype_num, i, name_len;
2036 X509_NAME *xn = NULL;
2037 unsigned char *data;
2038 unsigned char *namestart, *namebytes;
2039 STACK_OF(X509_NAME) *ca_sk = NULL;
0f113f3e
MC
2040
2041 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
b9908bf9 2042 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2043 goto err;
2044 }
2045
2046 /* get the certificate types */
73999b62
MC
2047 if (!PACKET_get_1(pkt, &ctype_num)
2048 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
ac112332 2049 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2050 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
ac112332
MC
2051 goto err;
2052 }
b548a1f1
RS
2053 OPENSSL_free(s->cert->ctypes);
2054 s->cert->ctypes = NULL;
0f113f3e
MC
2055 if (ctype_num > SSL3_CT_NUMBER) {
2056 /* If we exceed static buffer copy all to cert structure */
2057 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2058 if (s->cert->ctypes == NULL) {
b9908bf9 2059 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2060 goto err;
2061 }
ac112332 2062 memcpy(s->cert->ctypes, data, ctype_num);
0f113f3e
MC
2063 s->cert->ctype_num = (size_t)ctype_num;
2064 ctype_num = SSL3_CT_NUMBER;
2065 }
2066 for (i = 0; i < ctype_num; i++)
ac112332
MC
2067 s->s3->tmp.ctype[i] = data[i];
2068
0f113f3e 2069 if (SSL_USE_SIGALGS(s)) {
73999b62
MC
2070 if (!PACKET_get_net_2(pkt, &list_len)
2071 || !PACKET_get_bytes(pkt, &data, list_len)) {
0f113f3e 2072 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9
MC
2073 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2074 SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2075 goto err;
2076 }
ac112332 2077
0f113f3e
MC
2078 /* Clear certificate digests and validity flags */
2079 for (i = 0; i < SSL_PKEY_NUM; i++) {
d376e57d 2080 s->s3->tmp.md[i] = NULL;
6383d316 2081 s->s3->tmp.valid_flags[i] = 0;
0f113f3e 2082 }
ac112332 2083 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
0f113f3e 2084 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2085 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
0f113f3e
MC
2086 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2087 goto err;
2088 }
2089 if (!tls1_process_sigalgs(s)) {
2090 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 2091 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2092 goto err;
2093 }
0f113f3e
MC
2094 }
2095
2096 /* get the CA RDNs */
73999b62
MC
2097 if (!PACKET_get_net_2(pkt, &list_len)
2098 || PACKET_remaining(pkt) != list_len) {
0f113f3e 2099 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2100 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2101 goto err;
2102 }
2103
73999b62
MC
2104 while (PACKET_remaining(pkt)) {
2105 if (!PACKET_get_net_2(pkt, &name_len)
2106 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
0f113f3e 2107 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9
MC
2108 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2109 SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2110 goto err;
2111 }
2112
ac112332 2113 namestart = namebytes;
0f113f3e 2114
ac112332
MC
2115 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2116 name_len)) == NULL) {
3c33c6f6 2117 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2118 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
3c33c6f6 2119 goto err;
0f113f3e
MC
2120 }
2121
ac112332 2122 if (namebytes != (namestart + name_len)) {
0f113f3e 2123 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9 2124 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
0f113f3e
MC
2125 SSL_R_CA_DN_LENGTH_MISMATCH);
2126 goto err;
2127 }
2128 if (!sk_X509_NAME_push(ca_sk, xn)) {
b9908bf9 2129 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2130 goto err;
2131 }
0f113f3e
MC
2132 }
2133
0f113f3e
MC
2134 /* we should setup a certificate to return.... */
2135 s->s3->tmp.cert_req = 1;
2136 s->s3->tmp.ctype_num = ctype_num;
222561fe 2137 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
0f113f3e
MC
2138 s->s3->tmp.ca_names = ca_sk;
2139 ca_sk = NULL;
2140
b9908bf9 2141 ret = MSG_PROCESS_CONTINUE_READING;
cc273a93 2142 goto done;
0f113f3e 2143 err:
b9908bf9 2144 statem_set_error(s);
cc273a93 2145 done:
222561fe 2146 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
b9908bf9 2147 return ret;
0f113f3e
MC
2148}
2149
2150static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
dfeab068 2151{
0f113f3e 2152 return (X509_NAME_cmp(*a, *b));
dfeab068 2153}
dfeab068 2154
73999b62 2155enum MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
b9908bf9
MC
2156{
2157 int al;
2158 unsigned int ticklen;
2159 unsigned long ticket_lifetime_hint;
b9908bf9 2160
73999b62
MC
2161 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2162 || !PACKET_get_net_2(pkt, &ticklen)
2163 || PACKET_remaining(pkt) != ticklen) {
e711da71
EK
2164 al = SSL_AD_DECODE_ERROR;
2165 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2166 goto f_err;
2167 }
2168
2169 /* Server is allowed to change its mind and send an empty ticket. */
2170 if (ticklen == 0)
2171 return 1;
2172
98ece4ee
MC
2173 if (s->session->session_id_length > 0) {
2174 int i = s->session_ctx->session_cache_mode;
2175 SSL_SESSION *new_sess;
2176 /*
2177 * We reused an existing session, so we need to replace it with a new
2178 * one
2179 */
2180 if (i & SSL_SESS_CACHE_CLIENT) {
2181 /*
2182 * Remove the old session from the cache
2183 */
2184 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2185 if (s->session_ctx->remove_session_cb != NULL)
2186 s->session_ctx->remove_session_cb(s->session_ctx,
2187 s->session);
2188 } else {
2189 /* We carry on if this fails */
2190 SSL_CTX_remove_session(s->session_ctx, s->session);
2191 }
2192 }
2193
2194 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2195 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 2196 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
98ece4ee
MC
2197 goto f_err;
2198 }
2199
2200 SSL_SESSION_free(s->session);
2201 s->session = new_sess;
2202 }
2203
b548a1f1
RS
2204 OPENSSL_free(s->session->tlsext_tick);
2205 s->session->tlsext_ticklen = 0;
e711da71 2206
0f113f3e
MC
2207 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2208 if (!s->session->tlsext_tick) {
b9908bf9 2209 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2210 goto err;
2211 }
73999b62 2212 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
561e12bb 2213 al = SSL_AD_DECODE_ERROR;
b9908bf9 2214 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
561e12bb
MC
2215 goto f_err;
2216 }
e711da71
EK
2217
2218 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
0f113f3e
MC
2219 s->session->tlsext_ticklen = ticklen;
2220 /*
2221 * There are two ways to detect a resumed ticket session. One is to set
2222 * an appropriate session ID and then the server must return a match in
2223 * ServerHello. This allows the normal client session ID matching to work
2224 * and we know much earlier that the ticket has been accepted. The
2225 * other way is to set zero length session ID when the ticket is
2226 * presented and rely on the handshake to determine session resumption.
2227 * We choose the former approach because this fits in with assumptions
2228 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2229 * SHA256 is disabled) hash of the ticket.
2230 */
561e12bb 2231 EVP_Digest(s->session->tlsext_tick, ticklen,
0f113f3e 2232 s->session->session_id, &s->session->session_id_length,
0f113f3e 2233 EVP_sha256(), NULL);
b9908bf9 2234 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
2235 f_err:
2236 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2237 err:
b9908bf9
MC
2238 statem_set_error(s);
2239 return MSG_PROCESS_ERROR;
0f113f3e 2240}
67c8e7f4 2241
73999b62 2242enum MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
b9908bf9
MC
2243{
2244 int al;
2245 unsigned long resplen;
2246 unsigned int type;
b9908bf9 2247
73999b62 2248 if (!PACKET_get_1(pkt, &type)
ac63710a 2249 || type != TLSEXT_STATUSTYPE_ocsp) {
0f113f3e 2250 al = SSL_AD_DECODE_ERROR;
b9908bf9 2251 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
0f113f3e
MC
2252 goto f_err;
2253 }
73999b62
MC
2254 if (!PACKET_get_net_3(pkt, &resplen)
2255 || PACKET_remaining(pkt) != resplen) {
0f113f3e 2256 al = SSL_AD_DECODE_ERROR;
b9908bf9 2257 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
0f113f3e
MC
2258 goto f_err;
2259 }
b548a1f1 2260 OPENSSL_free(s->tlsext_ocsp_resp);
ac63710a 2261 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
0f113f3e
MC
2262 if (!s->tlsext_ocsp_resp) {
2263 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 2264 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2265 goto f_err;
2266 }
73999b62 2267 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
ac63710a 2268 al = SSL_AD_DECODE_ERROR;
b9908bf9 2269 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
ac63710a
MC
2270 goto f_err;
2271 }
0f113f3e
MC
2272 s->tlsext_ocsp_resplen = resplen;
2273 if (s->ctx->tlsext_status_cb) {
2274 int ret;
2275 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2276 if (ret == 0) {
2277 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
b9908bf9 2278 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
0f113f3e
MC
2279 goto f_err;
2280 }
2281 if (ret < 0) {
2282 al = SSL_AD_INTERNAL_ERROR;
b9908bf9 2283 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2284 goto f_err;
2285 }
2286 }
b9908bf9 2287 return MSG_PROCESS_CONTINUE_READING;
0f113f3e
MC
2288 f_err:
2289 ssl3_send_alert(s, SSL3_AL_FATAL, al);
b9908bf9
MC
2290 statem_set_error(s);
2291 return MSG_PROCESS_ERROR;
0f113f3e 2292}
d02b48c6 2293
73999b62 2294enum MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
b9908bf9 2295{
73999b62 2296 if (PACKET_remaining(pkt) > 0) {
0f113f3e
MC
2297 /* should contain no data */
2298 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
b9908bf9
MC
2299 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2300 statem_set_error(s);
2301 return MSG_PROCESS_ERROR;
0f113f3e 2302 }
b9908bf9
MC
2303
2304#ifndef OPENSSL_NO_SRP
2305 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2306 if (SRP_Calc_A_param(s) <= 0) {
2307 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2308 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2309 statem_set_error(s);
2310 return MSG_PROCESS_ERROR;
2311 }
2312 }
2313#endif
2314
473483d4
MC
2315#ifndef OPENSSL_NO_SCTP
2316 /* Only applies to renegotiation */
2317 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2318 && s->renegotiate != 0)
2319 return MSG_PROCESS_CONTINUE_PROCESSING;
2320 else
2321#endif
2322 return MSG_PROCESS_FINISHED_READING;
0f113f3e 2323}
176f31dd 2324
b9908bf9 2325int tls_construct_client_key_exchange(SSL *s)
0f113f3e
MC
2326{
2327 unsigned char *p;
2328 int n;
7689082b
DSH
2329#ifndef OPENSSL_NO_PSK
2330 size_t pskhdrlen = 0;
2331#endif
0f113f3e 2332 unsigned long alg_k;
bc36ee62 2333#ifndef OPENSSL_NO_RSA
0f113f3e
MC
2334 unsigned char *q;
2335 EVP_PKEY *pkey = NULL;
79df9d62 2336#endif
10bf4fc2 2337#ifndef OPENSSL_NO_EC
0f113f3e
MC
2338 EC_KEY *clnt_ecdh = NULL;
2339 const EC_POINT *srvr_ecpoint = NULL;
2340 EVP_PKEY *srvr_pub_pkey = NULL;
2341 unsigned char *encodedPoint = NULL;
2342 int encoded_pt_len = 0;
2343 BN_CTX *bn_ctx = NULL;
ea262260 2344#endif
c660ec63
DSH
2345 unsigned char *pms = NULL;
2346 size_t pmslen = 0;
7689082b 2347 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
d02b48c6 2348
b9908bf9 2349 p = ssl_handshake_start(s);
d02b48c6 2350
7689082b
DSH
2351
2352#ifndef OPENSSL_NO_PSK
b9908bf9
MC
2353 if (alg_k & SSL_PSK) {
2354 int psk_err = 1;
2355 /*
2356 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2357 * \0-terminated identity. The last byte is for us for simulating
2358 * strnlen.
2359 */
2360 char identity[PSK_MAX_IDENTITY_LEN + 1];
2361 size_t identitylen;
2362 unsigned char psk[PSK_MAX_PSK_LEN];
2363 size_t psklen;
2364
2365 if (s->psk_client_callback == NULL) {
2366 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2367 SSL_R_PSK_NO_CLIENT_CB);
2368 goto err;
2369 }
7689082b 2370
b9908bf9 2371 memset(identity, 0, sizeof(identity));
7689082b 2372
b9908bf9
MC
2373 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2374 identity, sizeof(identity) - 1,
2375 psk, sizeof(psk));
7689082b 2376
b9908bf9
MC
2377 if (psklen > PSK_MAX_PSK_LEN) {
2378 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2379 ERR_R_INTERNAL_ERROR);
2380 goto psk_err;
2381 } else if (psklen == 0) {
2382 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2383 SSL_R_PSK_IDENTITY_NOT_FOUND);
2384 goto psk_err;
2385 }
7689082b 2386
b9908bf9
MC
2387 OPENSSL_free(s->s3->tmp.psk);
2388 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2389 OPENSSL_cleanse(psk, psklen);
7689082b 2390
b9908bf9
MC
2391 if (s->s3->tmp.psk == NULL) {
2392 OPENSSL_cleanse(identity, sizeof(identity));
2393 goto memerr;
2394 }
7689082b 2395
b9908bf9 2396 s->s3->tmp.psklen = psklen;
7689082b 2397
b9908bf9
MC
2398 identitylen = strlen(identity);
2399 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2400 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2401 ERR_R_INTERNAL_ERROR);
2402 goto psk_err;
2403 }
2404 OPENSSL_free(s->session->psk_identity);
2405 s->session->psk_identity = BUF_strdup(identity);
2406 if (s->session->psk_identity == NULL) {
7689082b 2407 OPENSSL_cleanse(identity, sizeof(identity));
b9908bf9 2408 goto memerr;
7689082b 2409 }
d02b48c6 2410
b9908bf9
MC
2411 s2n(identitylen, p);
2412 memcpy(p, identity, identitylen);
2413 pskhdrlen = 2 + identitylen;
2414 p += identitylen;
2415 psk_err = 0;
2416psk_err:
2417 OPENSSL_cleanse(identity, sizeof(identity));
2418 if (psk_err != 0) {
2419 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2420 goto err;
0f113f3e 2421 }
b9908bf9
MC
2422 }
2423 if (alg_k & SSL_kPSK) {
2424 n = 0;
2425 } else
2426#endif
2427
2428 /* Fool emacs indentation */
2429 if (0) {
2430 }
bc36ee62 2431#ifndef OPENSSL_NO_RSA
b9908bf9
MC
2432 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2433 RSA *rsa;
2434 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2435 pms = OPENSSL_malloc(pmslen);
2436 if (!pms)
2437 goto memerr;
2438
2439 if (s->session->peer == NULL) {
2440 /*
2441 * We should always have a server certificate with SSL_kRSA.
2442 */
2443 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2444 ERR_R_INTERNAL_ERROR);
2445 goto err;
2446 }
0f113f3e 2447
b9908bf9
MC
2448 if (s->s3->peer_rsa_tmp != NULL)
2449 rsa = s->s3->peer_rsa_tmp;
2450 else {
2451 pkey = X509_get_pubkey(s->session->peer);
2452 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2453 || (pkey->pkey.rsa == NULL)) {
2454 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2455 ERR_R_INTERNAL_ERROR);
0f113f3e 2456 EVP_PKEY_free(pkey);
b9908bf9 2457 goto err;
0f113f3e 2458 }
b9908bf9
MC
2459 rsa = pkey->pkey.rsa;
2460 EVP_PKEY_free(pkey);
2461 }
0f113f3e 2462
b9908bf9
MC
2463 pms[0] = s->client_version >> 8;
2464 pms[1] = s->client_version & 0xff;
2465 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2466 goto err;
0f113f3e 2467
b9908bf9
MC
2468 q = p;
2469 /* Fix buf for TLS and beyond */
2470 if (s->version > SSL3_VERSION)
2471 p += 2;
2472 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
0f113f3e 2473# ifdef PKCS1_CHECK
b9908bf9
MC
2474 if (s->options & SSL_OP_PKCS1_CHECK_1)
2475 p[1]++;
2476 if (s->options & SSL_OP_PKCS1_CHECK_2)
2477 tmp_buf[0] = 0x70;
0f113f3e 2478# endif
b9908bf9
MC
2479 if (n <= 0) {
2480 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2481 SSL_R_BAD_RSA_ENCRYPT);
2482 goto err;
2483 }
0f113f3e 2484
b9908bf9
MC
2485 /* Fix buf for TLS and beyond */
2486 if (s->version > SSL3_VERSION) {
2487 s2n(n, q);
2488 n += 2;
0f113f3e 2489 }
b9908bf9 2490 }
f9b3bff6 2491#endif
bc36ee62 2492#ifndef OPENSSL_NO_DH
b9908bf9
MC
2493 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2494 DH *dh_srvr, *dh_clnt;
2495 if (s->s3->peer_dh_tmp != NULL)
2496 dh_srvr = s->s3->peer_dh_tmp;
2497 else {
2498 /* we get them from the cert */
2499 EVP_PKEY *spkey = NULL;
2500 dh_srvr = NULL;
2501 spkey = X509_get_pubkey(s->session->peer);
2502 if (spkey) {
2503 dh_srvr = EVP_PKEY_get1_DH(spkey);
2504 EVP_PKEY_free(spkey);
0f113f3e 2505 }
b9908bf9
MC
2506 if (dh_srvr == NULL) {
2507 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2508 ERR_R_INTERNAL_ERROR);
0f113f3e
MC
2509 goto err;
2510 }
0f113f3e 2511 }
b9908bf9
MC
2512 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2513 /* Use client certificate key */
2514 EVP_PKEY *clkey = s->cert->key->privatekey;
2515 dh_clnt = NULL;
2516 if (clkey)
2517 dh_clnt = EVP_PKEY_get1_DH(clkey);
2518 if (dh_clnt == NULL) {
2519 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
0f113f3e
MC
2520 ERR_R_INTERNAL_ERROR);
2521 goto err;
2522 }
b9908bf9
MC
2523 } else {
2524 /* generate a new random key */
2525 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2526 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
0f113f3e
MC
2527 goto err;
2528 }
b9908bf9
MC
2529 if (!DH_generate_key(dh_clnt)) {
2530 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2531 DH_free(dh_clnt);
0f113f3e
MC
2532 goto err;
2533 }
b9908bf9 2534 }
0f113f3e 2535
b9908bf9
MC
2536 pmslen = DH_size(dh_clnt);
2537 pms = OPENSSL_malloc(pmslen);
2538 if (!pms)
2539 goto memerr;
0f113f3e 2540
b9908bf9
MC
2541 /*
2542 * use the 'p' output buffer for the DH key, but make sure to
2543 * clear it out afterwards
2544 */
0f113f3e 2545
b9908bf9
MC
2546 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2547 if (s->s3->peer_dh_tmp == NULL)
2548 DH_free(dh_srvr);
0f113f3e 2549
b9908bf9
MC
2550 if (n <= 0) {
2551 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2552 DH_free(dh_clnt);
2553 goto err;
2554 }
2555 pmslen = n;
0f113f3e 2556
b9908bf9
MC
2557 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2558 n = 0;
2559 else {
2560 /* send off the data */
2561 n = BN_num_bytes(dh_clnt->pub_key);
2562 s2n(n, p);
2563 BN_bn2bin(dh_clnt->pub_key, p);
2564 n += 2;
0f113f3e 2565 }
c660ec63 2566
b9908bf9
MC
2567 DH_free(dh_clnt);
2568 }
2569#endif
0f113f3e 2570
b9908bf9
MC
2571#ifndef OPENSSL_NO_EC
2572 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2573 const EC_GROUP *srvr_group = NULL;
2574 EC_KEY *tkey;
2575 int ecdh_clnt_cert = 0;
2576 int field_size = 0;
2577 /*
2578 * Did we send out the client's ECDH share for use in premaster
2579 * computation as part of client certificate? If so, set
2580 * ecdh_clnt_cert to 1.
2581 */
2582 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2583 /*-
2584 * XXX: For now, we do not support client
2585 * authentication using ECDH certificates.
2586 * To add such support, one needs to add
2587 * code that checks for appropriate
2588 * conditions and sets ecdh_clnt_cert to 1.
2589 * For example, the cert have an ECC
2590 * key on the same curve as the server's
2591 * and the key should be authorized for
2592 * key agreement.
0f113f3e 2593 *
b9908bf9
MC
2594 * One also needs to add code in ssl3_connect
2595 * to skip sending the certificate verify
2596 * message.
2597 *
2598 * if ((s->cert->key->privatekey != NULL) &&
2599 * (s->cert->key->privatekey->type ==
2600 * EVP_PKEY_EC) && ...)
2601 * ecdh_clnt_cert = 1;
0f113f3e 2602 */
b9908bf9 2603 }
0f113f3e 2604
b9908bf9
MC
2605 if (s->s3->peer_ecdh_tmp != NULL) {
2606 tkey = s->s3->peer_ecdh_tmp;
2607 } else {
2608 /* Get the Server Public Key from Cert */
2609 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2610 if ((srvr_pub_pkey == NULL)
2611 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2612 || (srvr_pub_pkey->pkey.ec == NULL)) {
2613 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
266483d2
MC
2614 ERR_R_INTERNAL_ERROR);
2615 goto err;
0f113f3e 2616 }
b9908bf9
MC
2617
2618 tkey = srvr_pub_pkey->pkey.ec;
2619 }
2620
2621 srvr_group = EC_KEY_get0_group(tkey);
2622 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2623
2624 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2625 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2626 ERR_R_INTERNAL_ERROR);
2627 goto err;
2628 }
2629
2630 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2631 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2632 ERR_R_MALLOC_FAILURE);
2633 goto err;
2634 }
2635
2636 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2637 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2638 goto err;
2639 }
2640 if (ecdh_clnt_cert) {
0f113f3e 2641 /*
b9908bf9
MC
2642 * Reuse key info from our certificate We only need our
2643 * private key to perform the ECDH computation.
0f113f3e 2644 */
b9908bf9
MC
2645 const BIGNUM *priv_key;
2646 tkey = s->cert->key->privatekey->pkey.ec;
2647 priv_key = EC_KEY_get0_private_key(tkey);
2648 if (priv_key == NULL) {
2649 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2650 ERR_R_MALLOC_FAILURE);
0f113f3e
MC
2651 goto err;
2652 }
b9908bf9
MC
2653 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2654 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
0f113f3e
MC
2655 goto err;
2656 }
b9908bf9
MC
2657 } else {
2658 /* Generate a new ECDH key pair */
2659 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2660 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2661 ERR_R_ECDH_LIB);
2662 goto err;
0f113f3e 2663 }
b9908bf9
MC
2664 }
2665
2666 /*
2667 * use the 'p' output buffer for the ECDH key, but make sure to
2668 * clear it out afterwards
2669 */
0f113f3e 2670
b9908bf9
MC
2671 field_size = EC_GROUP_get_degree(srvr_group);
2672 if (field_size <= 0) {
2673 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2674 goto err;
0f113f3e 2675 }
b9908bf9
MC
2676 pmslen = (field_size + 7) / 8;
2677 pms = OPENSSL_malloc(pmslen);
2678 if (!pms)
2679 goto memerr;
2680 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2681 if (n <= 0 || pmslen != (size_t)n) {
2682 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2683 goto err;
2684 }
2685
2686 if (ecdh_clnt_cert) {
2687 /* Send empty client key exch message */
2688 n = 0;
2689 } else {
2690 /*
2691 * First check the size of encoding and allocate memory
2692 * accordingly.
2693 */
2694 encoded_pt_len =
2695 EC_POINT_point2oct(srvr_group,
2696 EC_KEY_get0_public_key(clnt_ecdh),
2697 POINT_CONVERSION_UNCOMPRESSED,
2698 NULL, 0, NULL);
2699
2700 encodedPoint = (unsigned char *)
2701 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2702 bn_ctx = BN_CTX_new();
2703 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2704 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
0f113f3e
MC
2705 ERR_R_MALLOC_FAILURE);
2706 goto err;
2707 }
b9908bf9
MC
2708
2709 /* Encode the public key */
2710 n = EC_POINT_point2oct(srvr_group,
2711 EC_KEY_get0_public_key(clnt_ecdh),
2712 POINT_CONVERSION_UNCOMPRESSED,
2713 encodedPoint, encoded_pt_len, bn_ctx);
2714
2715 *p = n; /* length of encoded point */
2716 /* Encoded point will be copied here */
2717 p += 1;
2718 /* copy the point */
2719 memcpy(p, encodedPoint, n);
2720 /* increment n to account for length field */
2721 n += 1;
0f113f3e
MC
2722 }
2723
b9908bf9
MC
2724 /* Free allocated memory */
2725 BN_CTX_free(bn_ctx);
2726 OPENSSL_free(encodedPoint);
2727 EC_KEY_free(clnt_ecdh);
2728 EVP_PKEY_free(srvr_pub_pkey);
2729 }
2730#endif /* !OPENSSL_NO_EC */
2731 else if (alg_k & SSL_kGOST) {
2732 /* GOST key exchange message creation */
2733 EVP_PKEY_CTX *pkey_ctx;
2734 X509 *peer_cert;
2735 size_t msglen;
2736 unsigned int md_len;
2737 unsigned char shared_ukm[32], tmp[256];
2738 EVP_MD_CTX *ukm_hash;
2739 EVP_PKEY *pub_key;
2740
2741 pmslen = 32;
2742 pms = OPENSSL_malloc(pmslen);
2743 if (!pms)
2744 goto memerr;
7689082b 2745
b9908bf9
MC
2746 /*
2747 * Get server sertificate PKEY and create ctx from it
2748 */
2749 peer_cert = s->session->peer;
2750 if (!peer_cert) {
2751 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2752 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
77d514c5
MC
2753 goto err;
2754 }
2755
b9908bf9
MC
2756 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2757 X509_get_pubkey(peer_cert), NULL);
2758 /*
2759 * If we have send a certificate, and certificate key
2760 *
2761 * * parameters match those of server certificate, use
2762 * certificate key for key exchange
2763 */
0f113f3e 2764
b9908bf9
MC
2765 /* Otherwise, generate ephemeral key pair */
2766
2767 EVP_PKEY_encrypt_init(pkey_ctx);
2768 /* Generate session key */
2769 if (RAND_bytes(pms, pmslen) <= 0) {
2770 EVP_PKEY_CTX_free(pkey_ctx);
2771 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2772 ERR_R_INTERNAL_ERROR);
2773 goto err;
2774 };
c660ec63 2775 /*
b9908bf9 2776 * If we have client certificate, use its secret as peer key
c660ec63 2777 */
b9908bf9
MC
2778 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2779 if (EVP_PKEY_derive_set_peer
2780 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2781 /*
2782 * If there was an error - just ignore it. Ephemeral key
2783 * * would be used
2784 */
2785 ERR_clear_error();
c660ec63
DSH
2786 }
2787 }
b9908bf9
MC
2788 /*
2789 * Compute shared IV and store it in algorithm-specific context
2790 * data
2791 */
2792 ukm_hash = EVP_MD_CTX_create();
2793 EVP_DigestInit(ukm_hash,
2794 EVP_get_digestbynid(NID_id_GostR3411_94));
2795 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2796 SSL3_RANDOM_SIZE);
2797 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2798 SSL3_RANDOM_SIZE);
2799 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2800 EVP_MD_CTX_destroy(ukm_hash);
2801 if (EVP_PKEY_CTX_ctrl
2802 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2803 shared_ukm) < 0) {
2804 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2805 SSL_R_LIBRARY_BUG);
2806 goto err;
c660ec63 2807 }
b9908bf9
MC
2808 /* Make GOST keytransport blob message */
2809 /*
2810 * Encapsulate it into sequence
2811 */
2812 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2813 msglen = 255;
2814 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2815 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2816 SSL_R_LIBRARY_BUG);
c660ec63
DSH
2817 goto err;
2818 }
b9908bf9
MC
2819 if (msglen >= 0x80) {
2820 *(p++) = 0x81;
2821 *(p++) = msglen & 0xff;
2822 n = msglen + 3;
2823 } else {
2824 *(p++) = msglen & 0xff;
2825 n = msglen + 2;
2826 }
2827 memcpy(p, tmp, msglen);
2828 /* Check if pubkey from client certificate was used */
2829 if (EVP_PKEY_CTX_ctrl
2830 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2831 /* Set flag "skip certificate verify" */
2832 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2833 }
2834 EVP_PKEY_CTX_free(pkey_ctx);
2835 EVP_PKEY_free(pub_key);
2836
2837 }
2838#ifndef OPENSSL_NO_SRP
2839 else if (alg_k & SSL_kSRP) {
2840 if (s->srp_ctx.A != NULL) {
2841 /* send off the data */
2842 n = BN_num_bytes(s->srp_ctx.A);
2843 s2n(n, p);
2844 BN_bn2bin(s->srp_ctx.A, p);
2845 n += 2;
2846 } else {
2847 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2848 ERR_R_INTERNAL_ERROR);
2849 goto err;
2850 }
2851 OPENSSL_free(s->session->srp_username);
2852 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2853 if (s->session->srp_username == NULL) {
2854 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2855 ERR_R_MALLOC_FAILURE);
69f68237
MC
2856 goto err;
2857 }
c660ec63 2858 }
b9908bf9
MC
2859#endif
2860 else {
2861 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2862 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2863 goto err;
2864 }
2865
2866#ifndef OPENSSL_NO_PSK
2867 n += pskhdrlen;
2868#endif
2869
2870 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2871 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2872 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2873 goto err;
2874 }
2875
2876 s->s3->tmp.pms = pms;
2877 s->s3->tmp.pmslen = pmslen;
2878
2879 return 1;
c660ec63
DSH
2880 memerr:
2881 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 2882 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
0f113f3e 2883 err:
4b45c6e5 2884 OPENSSL_clear_free(pms, pmslen);
76106e60 2885 s->s3->tmp.pms = NULL;
10bf4fc2 2886#ifndef OPENSSL_NO_EC
0f113f3e 2887 BN_CTX_free(bn_ctx);
b548a1f1 2888 OPENSSL_free(encodedPoint);
8fdc3734 2889 EC_KEY_free(clnt_ecdh);
0f113f3e 2890 EVP_PKEY_free(srvr_pub_pkey);
7689082b
DSH
2891#endif
2892#ifndef OPENSSL_NO_PSK
2893 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2894 s->s3->tmp.psk = NULL;
0f113f3e 2895#endif
b9908bf9
MC
2896 statem_set_error(s);
2897 return 0;
2898}
2899
2900int tls_client_key_exchange_post_work(SSL *s)
2901{
2902 unsigned char *pms = NULL;
2903 size_t pmslen = 0;
2904
2905#ifndef OPENSSL_NO_SRP
2906 /* Check for SRP */
2907 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2908 if (!srp_generate_client_master_secret(s)) {
2909 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2910 ERR_R_INTERNAL_ERROR);
2911 goto err;
2912 }
2913 return 1;
2914 }
2915#endif
2916 pms = s->s3->tmp.pms;
2917 pmslen = s->s3->tmp.pmslen;
2918
2919 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2920 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2921 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2922 goto err;
2923 }
2924 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2925 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2926 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2927 goto err;
2928 }
473483d4
MC
2929
2930#ifndef OPENSSL_NO_SCTP
2931 if (SSL_IS_DTLS(s)) {
2932 unsigned char sctpauthkey[64];
2933 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2934
2935 /*
2936 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2937 * used.
2938 */
2939 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
2940 DTLS1_SCTP_AUTH_LABEL);
2941
2942 if (SSL_export_keying_material(s, sctpauthkey,
2943 sizeof(sctpauthkey), labelbuffer,
2944 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2945 goto err;
2946
2947 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2948 sizeof(sctpauthkey), sctpauthkey);
2949 }
2950#endif
2951
b9908bf9
MC
2952 return 1;
2953 err:
2954 OPENSSL_clear_free(pms, pmslen);
2955 s->s3->tmp.pms = NULL;
2956 return 0;
0f113f3e 2957}
d02b48c6 2958
b9908bf9 2959int tls_construct_client_verify(SSL *s)
0f113f3e
MC
2960{
2961 unsigned char *p;
2962 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2963 EVP_PKEY *pkey;
2964 EVP_PKEY_CTX *pctx = NULL;
2965 EVP_MD_CTX mctx;
2966 unsigned u = 0;
2967 unsigned long n;
2968 int j;
2969
2970 EVP_MD_CTX_init(&mctx);
2971
b9908bf9
MC
2972 p = ssl_handshake_start(s);
2973 pkey = s->cert->key->privatekey;
0e1dba93 2974/* Create context from key and test if sha1 is allowed as digest */
b9908bf9
MC
2975 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2976 EVP_PKEY_sign_init(pctx);
2977 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
2978 if (!SSL_USE_SIGALGS(s))
2979 s->method->ssl3_enc->cert_verify_mac(s,
2980 NID_sha1,
2981 &(data
2982 [MD5_DIGEST_LENGTH]));
2983 } else {
2984 ERR_clear_error();
2985 }
2986 /*
2987 * For TLS v1.2 send signature algorithm and signature using agreed
2988 * digest and cached handshake records.
2989 */
2990 if (SSL_USE_SIGALGS(s)) {
2991 long hdatalen = 0;
2992 void *hdata;
2993 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
2994 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2995 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
2996 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
2997 goto err;
0f113f3e 2998 }
b9908bf9 2999 p += 2;
855a54a9 3000#ifdef SSL_DEBUG
b9908bf9
MC
3001 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3002 EVP_MD_name(md));
0f113f3e 3003#endif
b9908bf9
MC
3004 if (!EVP_SignInit_ex(&mctx, md, NULL)
3005 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3006 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3007 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
3008 goto err;
3009 }
3010 s2n(u, p);
3011 n = u + 4;
3012 /* Digest cached records and discard handshake buffer */
3013 if (!ssl3_digest_cached_records(s, 0))
3014 goto err;
3015 } else
bc36ee62 3016#ifndef OPENSSL_NO_RSA
b9908bf9
MC
3017 if (pkey->type == EVP_PKEY_RSA) {
3018 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3019 if (RSA_sign(NID_md5_sha1, data,
3020 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3021 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3022 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_RSA_LIB);
3023 goto err;
3024 }
3025 s2n(u, p);
3026 n = u + 2;
3027 } else
d02b48c6 3028#endif
bc36ee62 3029#ifndef OPENSSL_NO_DSA
b9908bf9
MC
3030 if (pkey->type == EVP_PKEY_DSA) {
3031 if (!DSA_sign(pkey->save_type,
3032 &(data[MD5_DIGEST_LENGTH]),
3033 SHA_DIGEST_LENGTH, &(p[2]),
3034 (unsigned int *)&j, pkey->pkey.dsa)) {
3035 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_DSA_LIB);
3036 goto err;
3037 }
3038 s2n(j, p);
3039 n = j + 2;
3040 } else
ea262260 3041#endif
10bf4fc2 3042#ifndef OPENSSL_NO_EC
b9908bf9
MC
3043 if (pkey->type == EVP_PKEY_EC) {
3044 if (!ECDSA_sign(pkey->save_type,
3045 &(data[MD5_DIGEST_LENGTH]),
3046 SHA_DIGEST_LENGTH, &(p[2]),
3047 (unsigned int *)&j, pkey->pkey.ec)) {
3048 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
0f113f3e
MC
3049 goto err;
3050 }
b9908bf9
MC
3051 s2n(j, p);
3052 n = j + 2;
3053 } else
3054#endif
3055 if (pkey->type == NID_id_GostR3410_2001) {
3056 unsigned char signbuf[64];
3057 int i;
3058 size_t sigsize = 64;
3059 s->method->ssl3_enc->cert_verify_mac(s,
3060 NID_id_GostR3411_94, data);
3061 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3062 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
77d514c5
MC
3063 goto err;
3064 }
b9908bf9
MC
3065 for (i = 63, j = 0; i >= 0; j++, i--) {
3066 p[2 + j] = signbuf[i];
3067 }
3068 s2n(j, p);
3069 n = j + 2;
3070 } else {
3071 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3072 goto err;
3073 }
3074 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3075 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3076 goto err;
0f113f3e 3077 }
b9908bf9 3078
0f113f3e
MC
3079 EVP_MD_CTX_cleanup(&mctx);
3080 EVP_PKEY_CTX_free(pctx);
b9908bf9 3081 return 1;
0f113f3e
MC
3082 err:
3083 EVP_MD_CTX_cleanup(&mctx);
3084 EVP_PKEY_CTX_free(pctx);
b9908bf9 3085 return 0;
0f113f3e
MC
3086}
3087
3088/*
3089 * Check a certificate can be used for client authentication. Currently check
3090 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3091 * certificates can be used and optionally checks suitability for Suite B.
0d609395
DSH
3092 */
3093static int ssl3_check_client_certificate(SSL *s)
0f113f3e
MC
3094{
3095 unsigned long alg_k;
3096 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3097 return 0;
3098 /* If no suitable signature algorithm can't use certificate */
d376e57d 3099 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
0f113f3e
MC
3100 return 0;
3101 /*
3102 * If strict mode check suitability of chain before using it. This also
3103 * adjusts suite B digest if necessary.
3104 */
3105 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3106 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3107 return 0;
3108 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3109 /* See if we can use client certificate for fixed DH */
3110 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
a273c6ee 3111 int i = s->session->peer_type;
0f113f3e
MC
3112 EVP_PKEY *clkey = NULL, *spkey = NULL;
3113 clkey = s->cert->key->privatekey;
3114 /* If client key not DH assume it can be used */
3115 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3116 return 1;
3117 if (i >= 0)
a273c6ee 3118 spkey = X509_get_pubkey(s->session->peer);
0f113f3e
MC
3119 if (spkey) {
3120 /* Compare server and client parameters */
3121 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3122 EVP_PKEY_free(spkey);
3123 if (i != 1)
3124 return 0;
3125 }
3126 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3127 }
3128 return 1;
3129}
0d609395 3130
b9908bf9 3131enum WORK_STATE tls_prepare_client_certificate(SSL *s, enum WORK_STATE wst)
0f113f3e
MC
3132{
3133 X509 *x509 = NULL;
3134 EVP_PKEY *pkey = NULL;
3135 int i;
3136
b9908bf9 3137 if (wst == WORK_MORE_A) {
0f113f3e
MC
3138 /* Let cert callback update client certificates if required */
3139 if (s->cert->cert_cb) {
3140 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3141 if (i < 0) {
3142 s->rwstate = SSL_X509_LOOKUP;
b9908bf9 3143 return WORK_MORE_A;
0f113f3e
MC
3144 }
3145 if (i == 0) {
3146 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 3147 statem_set_error(s);
0f113f3e
MC
3148 return 0;
3149 }
3150 s->rwstate = SSL_NOTHING;
3151 }
3152 if (ssl3_check_client_certificate(s))
b9908bf9
MC
3153 return WORK_FINISHED_CONTINUE;
3154
3155 /* Fall through to WORK_MORE_B */
3156 wst = WORK_MORE_B;
0f113f3e
MC
3157 }
3158
3159 /* We need to get a client cert */
b9908bf9 3160 if (wst == WORK_MORE_B) {
0f113f3e
MC
3161 /*
3162 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3163 * return(-1); We then get retied later
3164 */
0f113f3e
MC
3165 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3166 if (i < 0) {
3167 s->rwstate = SSL_X509_LOOKUP;
b9908bf9 3168 return WORK_MORE_B;
0f113f3e
MC
3169 }
3170 s->rwstate = SSL_NOTHING;
3171 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
0f113f3e
MC
3172 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3173 i = 0;
3174 } else if (i == 1) {
3175 i = 0;
b9908bf9 3176 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
0f113f3e
MC
3177 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3178 }
3179
222561fe 3180 X509_free(x509);
25aaa98a 3181 EVP_PKEY_free(pkey);
0f113f3e
MC
3182 if (i && !ssl3_check_client_certificate(s))
3183 i = 0;
3184 if (i == 0) {
3185 if (s->version == SSL3_VERSION) {
3186 s->s3->tmp.cert_req = 0;
3187 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
b9908bf9 3188 return WORK_FINISHED_CONTINUE;
0f113f3e
MC
3189 } else {
3190 s->s3->tmp.cert_req = 2;
124037fd 3191 if (!ssl3_digest_cached_records(s, 0)) {
dab18ab5 3192 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
b9908bf9 3193 statem_set_error(s);
dab18ab5
DSH
3194 return 0;
3195 }
0f113f3e
MC
3196 }
3197 }
3198
b9908bf9 3199 return WORK_FINISHED_CONTINUE;
0f113f3e
MC
3200 }
3201
b9908bf9
MC
3202 /* Shouldn't ever get here */
3203 return WORK_ERROR;
3204}
3205
3206int tls_construct_client_certificate(SSL *s)
3207{
3208 if (!ssl3_output_cert_chain(s,
3209 (s->s3->tmp.cert_req ==
3210 2) ? NULL : s->cert->key)) {
3211 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3212 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3213 statem_set_error(s);
3214 return 0;
0f113f3e 3215 }
b9908bf9
MC
3216
3217 return 1;
0f113f3e
MC
3218}
3219
3220#define has_bits(i,m) (((i)&(m)) == (m))
d02b48c6 3221
36d16f8e 3222int ssl3_check_cert_and_algorithm(SSL *s)
0f113f3e
MC
3223{
3224 int i, idx;
3225 long alg_k, alg_a;
3226 EVP_PKEY *pkey = NULL;
ac38115c 3227 int pkey_bits;
bc36ee62 3228#ifndef OPENSSL_NO_RSA
0f113f3e 3229 RSA *rsa;
79df9d62 3230#endif
bc36ee62 3231#ifndef OPENSSL_NO_DH
0f113f3e 3232 DH *dh;
79df9d62 3233#endif
26c79d56 3234 int al = SSL_AD_HANDSHAKE_FAILURE;
d02b48c6 3235
0f113f3e
MC
3236 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3237 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
d02b48c6 3238
0f113f3e 3239 /* we don't have a certificate */
55a9a16f 3240 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
0f113f3e 3241 return (1);
bc36ee62 3242#ifndef OPENSSL_NO_RSA
8d92c1f8 3243 rsa = s->s3->peer_rsa_tmp;
79df9d62 3244#endif
bc36ee62 3245#ifndef OPENSSL_NO_DH
8d92c1f8 3246 dh = s->s3->peer_dh_tmp;
79df9d62 3247#endif
d02b48c6 3248
0f113f3e 3249 /* This is the passed certificate */
d02b48c6 3250
a273c6ee 3251 idx = s->session->peer_type;
10bf4fc2 3252#ifndef OPENSSL_NO_EC
0f113f3e 3253 if (idx == SSL_PKEY_ECC) {
a273c6ee 3254 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
0f113f3e
MC
3255 /* check failed */
3256 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3257 goto f_err;
3258 } else {
3259 return 1;
3260 }
3261 } else if (alg_a & SSL_aECDSA) {
3262 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3263 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3264 goto f_err;
3265 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3266 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3267 goto f_err;
3268 }
3269#endif
a273c6ee 3270 pkey = X509_get_pubkey(s->session->peer);
ac38115c 3271 pkey_bits = EVP_PKEY_bits(pkey);
a273c6ee 3272 i = X509_certificate_type(s->session->peer, pkey);
0f113f3e
MC
3273 EVP_PKEY_free(pkey);
3274
3275 /* Check that we have a certificate if we require one */
3276 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3277 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3278 SSL_R_MISSING_RSA_SIGNING_CERT);
3279 goto f_err;
3280 }
bc36ee62 3281#ifndef OPENSSL_NO_DSA
0f113f3e
MC
3282 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3283 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3284 SSL_R_MISSING_DSA_SIGNING_CERT);
3285 goto f_err;
3286 }
d02b48c6 3287#endif
bc36ee62 3288#ifndef OPENSSL_NO_RSA
7689082b 3289 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
26c79d56
KR
3290 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3291 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3292 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3293 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3294 goto f_err;
3295 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3296 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3297 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3298 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3299 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3300 goto f_err;
3301 }
3302 if (rsa != NULL) {
3303 /* server key exchange is not allowed. */
3304 al = SSL_AD_INTERNAL_ERROR;
3305 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3306 goto f_err;
3307 }
3308 }
3309 }
0f113f3e 3310 }
79df9d62 3311#endif
bc36ee62 3312#ifndef OPENSSL_NO_DH
26c79d56
KR
3313 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3314 al = SSL_AD_INTERNAL_ERROR;
3315 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
3316 goto f_err;
3317 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3318 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3319 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3320 SSL_R_MISSING_DH_RSA_CERT);
3321 goto f_err;
3322 }
3323# ifndef OPENSSL_NO_DSA
3324 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3325 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3326 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3327 SSL_R_MISSING_DH_DSA_CERT);
3328 goto f_err;
3329 }
3330# endif
d02b48c6
RE
3331#endif
3332
ac38115c
KR
3333 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3334 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
bc36ee62 3335#ifndef OPENSSL_NO_RSA
0f113f3e 3336 if (alg_k & SSL_kRSA) {
26c79d56
KR
3337 if (rsa == NULL) {
3338 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3339 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3340 goto f_err;
3341 } else if (RSA_bits(rsa) >
0f113f3e 3342 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
26c79d56
KR
3343 /* We have a temporary RSA key but it's too large. */
3344 al = SSL_AD_EXPORT_RESTRICTION;
0f113f3e
MC
3345 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3346 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3347 goto f_err;
3348 }
3349 } else
d02b48c6 3350#endif
bc36ee62 3351#ifndef OPENSSL_NO_DH
26c79d56
KR
3352 if (alg_k & SSL_kDHE) {
3353 if (DH_bits(dh) >
0f113f3e 3354 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
26c79d56
KR
3355 /* We have a temporary DH key but it's too large. */
3356 al = SSL_AD_EXPORT_RESTRICTION;
0f113f3e
MC
3357 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3358 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3359 goto f_err;
3360 }
26c79d56
KR
3361 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3362 /* The cert should have had an export DH key. */
3363 al = SSL_AD_EXPORT_RESTRICTION;
3364 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3365 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3366 goto f_err;
0f113f3e
MC
3367 } else
3368#endif
3369 {
3370 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3371 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3372 goto f_err;
3373 }
3374 }
3375 return (1);
3376 f_err:
26c79d56 3377 ssl3_send_alert(s, SSL3_AL_FATAL, al);
0f113f3e
MC
3378 return (0);
3379}
3380
e481f9b9 3381#ifndef OPENSSL_NO_NEXTPROTONEG
b9908bf9
MC
3382int tls_construct_next_proto(SSL *s)
3383{
3384 unsigned int len, padding_len;
3385 unsigned char *d;
3386
3387 len = s->next_proto_negotiated_len;
3388 padding_len = 32 - ((len + 2) % 32);
3389 d = (unsigned char *)s->init_buf->data;
3390 d[4] = len;
3391 memcpy(d + 5, s->next_proto_negotiated, len);
3392 d[5 + len] = padding_len;
3393 memset(d + 6 + len, 0, padding_len);
3394 *(d++) = SSL3_MT_NEXT_PROTO;
3395 l2n3(2 + len + padding_len, d);
3396 s->init_num = 4 + 2 + len + padding_len;
3397 s->init_off = 0;
3398
3399 return 1;
3400}
6434abbf 3401#endif
368888bc
DSH
3402
3403int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
0f113f3e
MC
3404{
3405 int i = 0;
368888bc 3406#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
3407 if (s->ctx->client_cert_engine) {
3408 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3409 SSL_get_client_CA_list(s),
3410 px509, ppkey, NULL, NULL, NULL);
3411 if (i != 0)
3412 return i;
3413 }
3414#endif
3415 if (s->ctx->client_cert_cb)
3416 i = s->ctx->client_cert_cb(s, px509, ppkey);
3417 return i;
3418}
d45ba43d
MC
3419
3420int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
e9fa092e 3421 unsigned char *p)
d45ba43d
MC
3422{
3423 int i, j = 0;
3424 SSL_CIPHER *c;
3425 unsigned char *q;
3426 int empty_reneg_info_scsv = !s->renegotiate;
3427 /* Set disabled masks for this session */
3428 ssl_set_client_disabled(s);
3429
3430 if (sk == NULL)
3431 return (0);
3432 q = p;
d45ba43d
MC
3433
3434 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3435 c = sk_SSL_CIPHER_value(sk, i);
3436 /* Skip disabled ciphers */
3437 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3438 continue;
3439#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3440 if (c->id == SSL3_CK_SCSV) {
3441 if (!empty_reneg_info_scsv)
3442 continue;
3443 else
3444 empty_reneg_info_scsv = 0;
3445 }
3446#endif
e9fa092e 3447 j = s->method->put_cipher_by_char(c, p);
d45ba43d
MC
3448 p += j;
3449 }
3450 /*
3451 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3452 * applicable SCSVs.
3453 */
3454 if (p != q) {
3455 if (empty_reneg_info_scsv) {
3456 static SSL_CIPHER scsv = {
3457 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3458 };
e9fa092e 3459 j = s->method->put_cipher_by_char(&scsv, p);
d45ba43d
MC
3460 p += j;
3461#ifdef OPENSSL_RI_DEBUG
3462 fprintf(stderr,
3463 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3464#endif
3465 }
3466 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3467 static SSL_CIPHER scsv = {
3468 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3469 };
e9fa092e 3470 j = s->method->put_cipher_by_char(&scsv, p);
d45ba43d
MC
3471 p += j;
3472 }
3473 }
3474
3475 return (p - q);
3476}