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