drm/linux: implement div64_u64_rem() helper in math64.h
[dragonfly.git] / crypto / libressl / ssl / s3_clnt.c
blob5e31e9aeec17f22a46a6ab9345304cef2b7eddfa
1 /* $OpenBSD: s3_clnt.c,v 1.137 2016/03/11 07:08:45 mmcc Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
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.
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).
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.
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 :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
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.
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.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
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.
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/)"
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.
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.
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/)"
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 * ====================================================================
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).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
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.
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.
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.
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.
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.
151 #include <limits.h>
152 #include <stdint.h>
153 #include <stdio.h>
155 #include "ssl_locl.h"
157 #include <openssl/bn.h>
158 #include <openssl/buffer.h>
159 #include <openssl/dh.h>
160 #include <openssl/evp.h>
161 #include <openssl/md5.h>
162 #include <openssl/objects.h>
164 #ifndef OPENSSL_NO_ENGINE
165 #include <openssl/engine.h>
166 #endif
167 #ifndef OPENSSL_NO_GOST
168 #include <openssl/gost.h>
169 #endif
171 #include "bytestring.h"
173 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b);
176 ssl3_connect(SSL *s)
178 void (*cb)(const SSL *ssl, int type, int val) = NULL;
179 int ret = -1;
180 int new_state, state, skip = 0;
182 ERR_clear_error();
183 errno = 0;
185 if (s->info_callback != NULL)
186 cb = s->info_callback;
187 else if (s->ctx->info_callback != NULL)
188 cb = s->ctx->info_callback;
190 s->in_handshake++;
191 if (!SSL_in_init(s) || SSL_in_before(s))
192 SSL_clear(s);
194 for (;;) {
195 state = s->state;
197 switch (s->state) {
198 case SSL_ST_RENEGOTIATE:
199 s->renegotiate = 1;
200 s->state = SSL_ST_CONNECT;
201 s->ctx->stats.sess_connect_renegotiate++;
202 /* break */
203 case SSL_ST_BEFORE:
204 case SSL_ST_CONNECT:
205 case SSL_ST_BEFORE|SSL_ST_CONNECT:
206 case SSL_ST_OK|SSL_ST_CONNECT:
208 s->server = 0;
209 if (cb != NULL)
210 cb(s, SSL_CB_HANDSHAKE_START, 1);
212 if ((s->version & 0xff00 ) != 0x0300) {
213 SSLerr(SSL_F_SSL3_CONNECT,
214 ERR_R_INTERNAL_ERROR);
215 ret = -1;
216 goto end;
219 /* s->version=SSL3_VERSION; */
220 s->type = SSL_ST_CONNECT;
222 if (!ssl3_setup_init_buffer(s)) {
223 ret = -1;
224 goto end;
226 if (!ssl3_setup_buffers(s)) {
227 ret = -1;
228 goto end;
230 if (!ssl_init_wbio_buffer(s, 0)) {
231 ret = -1;
232 goto end;
235 /* don't push the buffering BIO quite yet */
237 if (!tls1_init_finished_mac(s)) {
238 ret = -1;
239 goto end;
242 s->state = SSL3_ST_CW_CLNT_HELLO_A;
243 s->ctx->stats.sess_connect++;
244 s->init_num = 0;
245 break;
247 case SSL3_ST_CW_CLNT_HELLO_A:
248 case SSL3_ST_CW_CLNT_HELLO_B:
250 s->shutdown = 0;
251 ret = ssl3_client_hello(s);
252 if (ret <= 0)
253 goto end;
254 s->state = SSL3_ST_CR_SRVR_HELLO_A;
255 s->init_num = 0;
257 /* turn on buffering for the next lot of output */
258 if (s->bbio != s->wbio)
259 s->wbio = BIO_push(s->bbio, s->wbio);
261 break;
263 case SSL3_ST_CR_SRVR_HELLO_A:
264 case SSL3_ST_CR_SRVR_HELLO_B:
265 ret = ssl3_get_server_hello(s);
266 if (ret <= 0)
267 goto end;
269 if (s->hit) {
270 s->state = SSL3_ST_CR_FINISHED_A;
271 if (s->tlsext_ticket_expected) {
272 /* receive renewed session ticket */
273 s->state = SSL3_ST_CR_SESSION_TICKET_A;
275 } else
276 s->state = SSL3_ST_CR_CERT_A;
277 s->init_num = 0;
278 break;
280 case SSL3_ST_CR_CERT_A:
281 case SSL3_ST_CR_CERT_B:
282 ret = ssl3_check_finished(s);
283 if (ret <= 0)
284 goto end;
285 if (ret == 2) {
286 s->hit = 1;
287 if (s->tlsext_ticket_expected)
288 s->state = SSL3_ST_CR_SESSION_TICKET_A;
289 else
290 s->state = SSL3_ST_CR_FINISHED_A;
291 s->init_num = 0;
292 break;
294 /* Check if it is anon DH/ECDH. */
295 if (!(s->s3->tmp.new_cipher->algorithm_auth &
296 SSL_aNULL)) {
297 ret = ssl3_get_server_certificate(s);
298 if (ret <= 0)
299 goto end;
300 if (s->tlsext_status_expected)
301 s->state = SSL3_ST_CR_CERT_STATUS_A;
302 else
303 s->state = SSL3_ST_CR_KEY_EXCH_A;
304 } else {
305 skip = 1;
306 s->state = SSL3_ST_CR_KEY_EXCH_A;
308 s->init_num = 0;
309 break;
311 case SSL3_ST_CR_KEY_EXCH_A:
312 case SSL3_ST_CR_KEY_EXCH_B:
313 ret = ssl3_get_key_exchange(s);
314 if (ret <= 0)
315 goto end;
316 s->state = SSL3_ST_CR_CERT_REQ_A;
317 s->init_num = 0;
320 * At this point we check that we have the
321 * required stuff from the server.
323 if (!ssl3_check_cert_and_algorithm(s)) {
324 ret = -1;
325 goto end;
327 break;
329 case SSL3_ST_CR_CERT_REQ_A:
330 case SSL3_ST_CR_CERT_REQ_B:
331 ret = ssl3_get_certificate_request(s);
332 if (ret <= 0)
333 goto end;
334 s->state = SSL3_ST_CR_SRVR_DONE_A;
335 s->init_num = 0;
336 break;
338 case SSL3_ST_CR_SRVR_DONE_A:
339 case SSL3_ST_CR_SRVR_DONE_B:
340 ret = ssl3_get_server_done(s);
341 if (ret <= 0)
342 goto end;
343 if (s->s3->tmp.cert_req)
344 s->state = SSL3_ST_CW_CERT_A;
345 else
346 s->state = SSL3_ST_CW_KEY_EXCH_A;
347 s->init_num = 0;
349 break;
351 case SSL3_ST_CW_CERT_A:
352 case SSL3_ST_CW_CERT_B:
353 case SSL3_ST_CW_CERT_C:
354 case SSL3_ST_CW_CERT_D:
355 ret = ssl3_send_client_certificate(s);
356 if (ret <= 0)
357 goto end;
358 s->state = SSL3_ST_CW_KEY_EXCH_A;
359 s->init_num = 0;
360 break;
362 case SSL3_ST_CW_KEY_EXCH_A:
363 case SSL3_ST_CW_KEY_EXCH_B:
364 ret = ssl3_send_client_key_exchange(s);
365 if (ret <= 0)
366 goto end;
368 * EAY EAY EAY need to check for DH fix cert
369 * sent back
372 * For TLS, cert_req is set to 2, so a cert chain
373 * of nothing is sent, but no verify packet is sent
376 * XXX: For now, we do not support client
377 * authentication in ECDH cipher suites with
378 * ECDH (rather than ECDSA) certificates.
379 * We need to skip the certificate verify
380 * message when client's ECDH public key is sent
381 * inside the client certificate.
383 if (s->s3->tmp.cert_req == 1) {
384 s->state = SSL3_ST_CW_CERT_VRFY_A;
385 } else {
386 s->state = SSL3_ST_CW_CHANGE_A;
387 s->s3->change_cipher_spec = 0;
389 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
390 s->state = SSL3_ST_CW_CHANGE_A;
391 s->s3->change_cipher_spec = 0;
394 s->init_num = 0;
395 break;
397 case SSL3_ST_CW_CERT_VRFY_A:
398 case SSL3_ST_CW_CERT_VRFY_B:
399 ret = ssl3_send_client_verify(s);
400 if (ret <= 0)
401 goto end;
402 s->state = SSL3_ST_CW_CHANGE_A;
403 s->init_num = 0;
404 s->s3->change_cipher_spec = 0;
405 break;
407 case SSL3_ST_CW_CHANGE_A:
408 case SSL3_ST_CW_CHANGE_B:
409 ret = ssl3_send_change_cipher_spec(s,
410 SSL3_ST_CW_CHANGE_A, SSL3_ST_CW_CHANGE_B);
411 if (ret <= 0)
412 goto end;
414 if (s->s3->next_proto_neg_seen)
415 s->state = SSL3_ST_CW_NEXT_PROTO_A;
416 else
417 s->state = SSL3_ST_CW_FINISHED_A;
418 s->init_num = 0;
420 s->session->cipher = s->s3->tmp.new_cipher;
421 if (!s->method->ssl3_enc->setup_key_block(s)) {
422 ret = -1;
423 goto end;
426 if (!s->method->ssl3_enc->change_cipher_state(s,
427 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
428 ret = -1;
429 goto end;
432 break;
434 case SSL3_ST_CW_NEXT_PROTO_A:
435 case SSL3_ST_CW_NEXT_PROTO_B:
436 ret = ssl3_send_next_proto(s);
437 if (ret <= 0)
438 goto end;
439 s->state = SSL3_ST_CW_FINISHED_A;
440 break;
442 case SSL3_ST_CW_FINISHED_A:
443 case SSL3_ST_CW_FINISHED_B:
444 ret = ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A,
445 SSL3_ST_CW_FINISHED_B,
446 s->method->ssl3_enc->client_finished_label,
447 s->method->ssl3_enc->client_finished_label_len);
448 if (ret <= 0)
449 goto end;
450 s->s3->flags |= SSL3_FLAGS_CCS_OK;
451 s->state = SSL3_ST_CW_FLUSH;
453 /* clear flags */
454 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
455 if (s->hit) {
456 s->s3->tmp.next_state = SSL_ST_OK;
457 if (s->s3->flags &
458 SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
459 s->state = SSL_ST_OK;
460 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
461 s->s3->delay_buf_pop_ret = 0;
463 } else {
464 /* Allow NewSessionTicket if ticket expected */
465 if (s->tlsext_ticket_expected)
466 s->s3->tmp.next_state =
467 SSL3_ST_CR_SESSION_TICKET_A;
468 else
470 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
472 s->init_num = 0;
473 break;
475 case SSL3_ST_CR_SESSION_TICKET_A:
476 case SSL3_ST_CR_SESSION_TICKET_B:
477 ret = ssl3_get_new_session_ticket(s);
478 if (ret <= 0)
479 goto end;
480 s->state = SSL3_ST_CR_FINISHED_A;
481 s->init_num = 0;
482 break;
484 case SSL3_ST_CR_CERT_STATUS_A:
485 case SSL3_ST_CR_CERT_STATUS_B:
486 ret = ssl3_get_cert_status(s);
487 if (ret <= 0)
488 goto end;
489 s->state = SSL3_ST_CR_KEY_EXCH_A;
490 s->init_num = 0;
491 break;
493 case SSL3_ST_CR_FINISHED_A:
494 case SSL3_ST_CR_FINISHED_B:
495 s->s3->flags |= SSL3_FLAGS_CCS_OK;
496 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
497 SSL3_ST_CR_FINISHED_B);
498 if (ret <= 0)
499 goto end;
501 if (s->hit)
502 s->state = SSL3_ST_CW_CHANGE_A;
503 else
504 s->state = SSL_ST_OK;
505 s->init_num = 0;
506 break;
508 case SSL3_ST_CW_FLUSH:
509 s->rwstate = SSL_WRITING;
510 if (BIO_flush(s->wbio) <= 0) {
511 ret = -1;
512 goto end;
514 s->rwstate = SSL_NOTHING;
515 s->state = s->s3->tmp.next_state;
516 break;
518 case SSL_ST_OK:
519 /* clean a few things up */
520 tls1_cleanup_key_block(s);
522 if (s->init_buf != NULL) {
523 BUF_MEM_free(s->init_buf);
524 s->init_buf = NULL;
528 * If we are not 'joining' the last two packets,
529 * remove the buffering now
531 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
532 ssl_free_wbio_buffer(s);
533 /* else do it later in ssl3_write */
535 s->init_num = 0;
536 s->renegotiate = 0;
537 s->new_session = 0;
539 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
540 if (s->hit)
541 s->ctx->stats.sess_hit++;
543 ret = 1;
544 /* s->server=0; */
545 s->handshake_func = ssl3_connect;
546 s->ctx->stats.sess_connect_good++;
548 if (cb != NULL)
549 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
551 goto end;
552 /* break; */
554 default:
555 SSLerr(SSL_F_SSL3_CONNECT,
556 SSL_R_UNKNOWN_STATE);
557 ret = -1;
558 goto end;
559 /* break; */
562 /* did we do anything */
563 if (!s->s3->tmp.reuse_message && !skip) {
564 if (s->debug) {
565 if ((ret = BIO_flush(s->wbio)) <= 0)
566 goto end;
569 if ((cb != NULL) && (s->state != state)) {
570 new_state = s->state;
571 s->state = state;
572 cb(s, SSL_CB_CONNECT_LOOP, 1);
573 s->state = new_state;
576 skip = 0;
579 end:
580 s->in_handshake--;
581 if (cb != NULL)
582 cb(s, SSL_CB_CONNECT_EXIT, ret);
584 return (ret);
588 ssl3_client_hello(SSL *s)
590 unsigned char *bufend, *p, *d;
591 int i;
593 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
594 SSL_SESSION *sess = s->session;
596 if ((sess == NULL) ||
597 (sess->ssl_version != s->version) ||
598 (!sess->session_id_length && !sess->tlsext_tick) ||
599 (sess->not_resumable)) {
600 if (!ssl_get_new_session(s, 0))
601 goto err;
603 /* else use the pre-loaded session */
606 * If a DTLS ClientHello message is being resent after a
607 * HelloVerifyRequest, we must retain the original client
608 * random value.
610 if (!SSL_IS_DTLS(s) || s->d1->send_cookie == 0)
611 arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE);
613 d = p = ssl3_handshake_msg_start(s, SSL3_MT_CLIENT_HELLO);
616 * Version indicates the negotiated version: for example from
617 * an SSLv2/v3 compatible client hello). The client_version
618 * field is the maximum version we permit and it is also
619 * used in RSA encrypted premaster secrets. Some servers can
620 * choke if we initially report a higher version then
621 * renegotiate to a lower one in the premaster secret. This
622 * didn't happen with TLS 1.0 as most servers supported it
623 * but it can with TLS 1.1 or later if the server only supports
624 * 1.0.
626 * Possible scenario with previous logic:
627 * 1. Client hello indicates TLS 1.2
628 * 2. Server hello says TLS 1.0
629 * 3. RSA encrypted premaster secret uses 1.2.
630 * 4. Handhaked proceeds using TLS 1.0.
631 * 5. Server sends hello request to renegotiate.
632 * 6. Client hello indicates TLS v1.0 as we now
633 * know that is maximum server supports.
634 * 7. Server chokes on RSA encrypted premaster secret
635 * containing version 1.0.
637 * For interoperability it should be OK to always use the
638 * maximum version we support in client hello and then rely
639 * on the checking of version to ensure the servers isn't
640 * being inconsistent: for example initially negotiating with
641 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
642 * client_version in client hello and not resetting it to
643 * the negotiated version.
645 *(p++) = s->client_version >> 8;
646 *(p++) = s->client_version & 0xff;
648 /* Random stuff */
649 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
650 p += SSL3_RANDOM_SIZE;
652 /* Session ID */
653 if (s->new_session)
654 i = 0;
655 else
656 i = s->session->session_id_length;
657 *(p++) = i;
658 if (i != 0) {
659 if (i > (int)sizeof(s->session->session_id)) {
660 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
661 ERR_R_INTERNAL_ERROR);
662 goto err;
664 memcpy(p, s->session->session_id, i);
665 p += i;
668 /* DTLS Cookie. */
669 if (SSL_IS_DTLS(s)) {
670 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
671 SSLerr(SSL_F_DTLS1_CLIENT_HELLO,
672 ERR_R_INTERNAL_ERROR);
673 goto err;
675 *(p++) = s->d1->cookie_len;
676 memcpy(p, s->d1->cookie, s->d1->cookie_len);
677 p += s->d1->cookie_len;
680 /* Ciphers supported */
681 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
682 if (i == 0) {
683 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
684 SSL_R_NO_CIPHERS_AVAILABLE);
685 goto err;
687 s2n(i, p);
688 p += i;
690 /* add in (no) COMPRESSION */
691 *(p++) = 1;
692 *(p++) = 0; /* Add the NULL method */
694 /* TLS extensions*/
695 bufend = (unsigned char *)s->init_buf->data +
696 SSL3_RT_MAX_PLAIN_LENGTH;
697 if ((p = ssl_add_clienthello_tlsext(s, p, bufend)) == NULL) {
698 SSLerr(SSL_F_SSL3_CLIENT_HELLO,
699 ERR_R_INTERNAL_ERROR);
700 goto err;
703 ssl3_handshake_msg_finish(s, p - d);
705 s->state = SSL3_ST_CW_CLNT_HELLO_B;
708 /* SSL3_ST_CW_CLNT_HELLO_B */
709 return (ssl3_handshake_write(s));
711 err:
712 return (-1);
716 ssl3_get_server_hello(SSL *s)
718 STACK_OF(SSL_CIPHER) *sk;
719 const SSL_CIPHER *c;
720 unsigned char *p, *q, *d;
721 int i, al, ok;
722 unsigned int j;
723 uint16_t cipher_value;
724 long n;
725 unsigned long alg_k;
727 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
728 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, /* ?? */ &ok);
730 if (!ok)
731 return ((int)n);
733 if (SSL_IS_DTLS(s)) {
734 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
735 if (s->d1->send_cookie == 0) {
736 s->s3->tmp.reuse_message = 1;
737 return (1);
738 } else {
739 /* Already sent a cookie. */
740 al = SSL_AD_UNEXPECTED_MESSAGE;
741 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
742 SSL_R_BAD_MESSAGE_TYPE);
743 goto f_err;
748 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
749 al = SSL_AD_UNEXPECTED_MESSAGE;
750 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
751 SSL_R_BAD_MESSAGE_TYPE);
752 goto f_err;
755 d = p = (unsigned char *)s->init_msg;
757 if (2 > n)
758 goto truncated;
759 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
760 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
761 s->version = (s->version&0xff00) | p[1];
762 al = SSL_AD_PROTOCOL_VERSION;
763 goto f_err;
765 p += 2;
767 /* load the server hello data */
769 if (p + SSL3_RANDOM_SIZE + 1 - d > n)
770 goto truncated;
772 /* load the server random */
773 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
774 p += SSL3_RANDOM_SIZE;
776 /* get the session-id */
777 j = *(p++);
779 if ((j > sizeof s->session->session_id) ||
780 (j > SSL3_SESSION_ID_SIZE)) {
781 al = SSL_AD_ILLEGAL_PARAMETER;
782 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
783 SSL_R_SSL3_SESSION_ID_TOO_LONG);
784 goto f_err;
787 if (p + j + 2 - d > n)
788 goto truncated;
790 /* Get the cipher value. */
791 q = p + j;
792 n2s(q, cipher_value);
795 * Check if we want to resume the session based on external
796 * pre-shared secret
798 if (s->tls_session_secret_cb) {
799 SSL_CIPHER *pref_cipher = NULL;
800 s->session->master_key_length = sizeof(s->session->master_key);
801 if (s->tls_session_secret_cb(s, s->session->master_key,
802 &s->session->master_key_length, NULL, &pref_cipher,
803 s->tls_session_secret_cb_arg)) {
804 s->session->cipher = pref_cipher ? pref_cipher :
805 ssl3_get_cipher_by_value(cipher_value);
806 s->s3->flags |= SSL3_FLAGS_CCS_OK;
810 if (j != 0 && j == s->session->session_id_length &&
811 timingsafe_memcmp(p, s->session->session_id, j) == 0) {
812 if (s->sid_ctx_length != s->session->sid_ctx_length ||
813 timingsafe_memcmp(s->session->sid_ctx,
814 s->sid_ctx, s->sid_ctx_length) != 0) {
815 /* actually a client application bug */
816 al = SSL_AD_ILLEGAL_PARAMETER;
817 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
818 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
819 goto f_err;
821 s->s3->flags |= SSL3_FLAGS_CCS_OK;
822 s->hit = 1;
823 } else {
824 /* a miss or crap from the other end */
826 /* If we were trying for session-id reuse, make a new
827 * SSL_SESSION so we don't stuff up other people */
828 s->hit = 0;
829 if (s->session->session_id_length > 0) {
830 if (!ssl_get_new_session(s, 0)) {
831 al = SSL_AD_INTERNAL_ERROR;
832 goto f_err;
835 s->session->session_id_length = j;
836 memcpy(s->session->session_id, p, j); /* j could be 0 */
838 p += j;
840 if ((c = ssl3_get_cipher_by_value(cipher_value)) == NULL) {
841 /* unknown cipher */
842 al = SSL_AD_ILLEGAL_PARAMETER;
843 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
844 SSL_R_UNKNOWN_CIPHER_RETURNED);
845 goto f_err;
848 /* TLS v1.2 only ciphersuites require v1.2 or later */
849 if ((c->algorithm_ssl & SSL_TLSV1_2) &&
850 (TLS1_get_version(s) < TLS1_2_VERSION)) {
851 al = SSL_AD_ILLEGAL_PARAMETER;
852 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
853 SSL_R_WRONG_CIPHER_RETURNED);
854 goto f_err;
856 p += SSL3_CIPHER_VALUE_SIZE;
858 sk = ssl_get_ciphers_by_id(s);
859 i = sk_SSL_CIPHER_find(sk, c);
860 if (i < 0) {
861 /* we did not say we would use this cipher */
862 al = SSL_AD_ILLEGAL_PARAMETER;
863 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
864 SSL_R_WRONG_CIPHER_RETURNED);
865 goto f_err;
869 * Depending on the session caching (internal/external), the cipher
870 * and/or cipher_id values may not be set. Make sure that
871 * cipher_id is set and use it for comparison.
873 if (s->session->cipher)
874 s->session->cipher_id = s->session->cipher->id;
875 if (s->hit && (s->session->cipher_id != c->id)) {
876 al = SSL_AD_ILLEGAL_PARAMETER;
877 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
878 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
879 goto f_err;
881 s->s3->tmp.new_cipher = c;
883 * Don't digest cached records if no sigalgs: we may need them for
884 * client authentication.
886 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
887 if (!(SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) &&
888 !tls1_digest_cached_records(s)) {
889 al = SSL_AD_INTERNAL_ERROR;
890 goto f_err;
892 /* lets get the compression algorithm */
893 /* COMPRESSION */
894 if (p + 1 - d > n)
895 goto truncated;
896 if (*(p++) != 0) {
897 al = SSL_AD_ILLEGAL_PARAMETER;
898 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
899 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
900 goto f_err;
903 /* TLS extensions*/
904 if (!ssl_parse_serverhello_tlsext(s, &p, d, n, &al)) {
905 /* 'al' set by ssl_parse_serverhello_tlsext */
906 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
907 goto f_err;
909 if (ssl_check_serverhello_tlsext(s) <= 0) {
910 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
911 goto err;
914 if (p != d + n)
915 goto truncated;
917 return (1);
919 truncated:
920 /* wrong packet length */
921 al = SSL_AD_DECODE_ERROR;
922 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
923 f_err:
924 ssl3_send_alert(s, SSL3_AL_FATAL, al);
925 err:
926 return (-1);
930 ssl3_get_server_certificate(SSL *s)
932 int al, i, ok, ret = -1;
933 long n;
934 CBS cbs, cert_list;
935 X509 *x = NULL;
936 const unsigned char *q;
937 STACK_OF(X509) *sk = NULL;
938 SESS_CERT *sc;
939 EVP_PKEY *pkey = NULL;
941 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A,
942 SSL3_ST_CR_CERT_B, -1, s->max_cert_list, &ok);
944 if (!ok)
945 return ((int)n);
947 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
948 s->s3->tmp.reuse_message = 1;
949 return (1);
952 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
953 al = SSL_AD_UNEXPECTED_MESSAGE;
954 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
955 SSL_R_BAD_MESSAGE_TYPE);
956 goto f_err;
960 if ((sk = sk_X509_new_null()) == NULL) {
961 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
962 ERR_R_MALLOC_FAILURE);
963 goto err;
966 if (n < 0)
967 goto truncated;
969 CBS_init(&cbs, s->init_msg, n);
970 if (CBS_len(&cbs) < 3)
971 goto truncated;
973 if (!CBS_get_u24_length_prefixed(&cbs, &cert_list) ||
974 CBS_len(&cbs) != 0) {
975 al = SSL_AD_DECODE_ERROR;
976 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
977 SSL_R_LENGTH_MISMATCH);
978 goto f_err;
981 while (CBS_len(&cert_list) > 0) {
982 CBS cert;
984 if (CBS_len(&cert_list) < 3)
985 goto truncated;
986 if (!CBS_get_u24_length_prefixed(&cert_list, &cert)) {
987 al = SSL_AD_DECODE_ERROR;
988 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
989 SSL_R_CERT_LENGTH_MISMATCH);
990 goto f_err;
993 q = CBS_data(&cert);
994 x = d2i_X509(NULL, &q, CBS_len(&cert));
995 if (x == NULL) {
996 al = SSL_AD_BAD_CERTIFICATE;
997 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
998 ERR_R_ASN1_LIB);
999 goto f_err;
1001 if (q != CBS_data(&cert) + CBS_len(&cert)) {
1002 al = SSL_AD_DECODE_ERROR;
1003 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1004 SSL_R_CERT_LENGTH_MISMATCH);
1005 goto f_err;
1007 if (!sk_X509_push(sk, x)) {
1008 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1009 ERR_R_MALLOC_FAILURE);
1010 goto err;
1012 x = NULL;
1015 i = ssl_verify_cert_chain(s, sk);
1016 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)) {
1017 al = ssl_verify_alarm_type(s->verify_result);
1018 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1019 SSL_R_CERTIFICATE_VERIFY_FAILED);
1020 goto f_err;
1023 ERR_clear_error(); /* but we keep s->verify_result */
1025 sc = ssl_sess_cert_new();
1026 if (sc == NULL)
1027 goto err;
1028 if (s->session->sess_cert)
1029 ssl_sess_cert_free(s->session->sess_cert);
1030 s->session->sess_cert = sc;
1032 sc->cert_chain = sk;
1034 * Inconsistency alert: cert_chain does include the peer's
1035 * certificate, which we don't include in s3_srvr.c
1037 x = sk_X509_value(sk, 0);
1038 sk = NULL;
1039 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1041 pkey = X509_get_pubkey(x);
1043 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1044 x = NULL;
1045 al = SSL3_AL_FATAL;
1046 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1047 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1048 goto f_err;
1051 i = ssl_cert_type(x, pkey);
1052 if (i < 0) {
1053 x = NULL;
1054 al = SSL3_AL_FATAL;
1055 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1056 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1057 goto f_err;
1060 sc->peer_cert_type = i;
1061 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1063 * Why would the following ever happen?
1064 * We just created sc a couple of lines ago.
1066 X509_free(sc->peer_pkeys[i].x509);
1067 sc->peer_pkeys[i].x509 = x;
1068 sc->peer_key = &(sc->peer_pkeys[i]);
1070 X509_free(s->session->peer);
1071 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1072 s->session->peer = x;
1073 s->session->verify_result = s->verify_result;
1075 x = NULL;
1076 ret = 1;
1078 if (0) {
1079 truncated:
1080 /* wrong packet length */
1081 al = SSL_AD_DECODE_ERROR;
1082 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1083 SSL_R_BAD_PACKET_LENGTH);
1084 f_err:
1085 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1087 err:
1088 EVP_PKEY_free(pkey);
1089 X509_free(x);
1090 sk_X509_pop_free(sk, X509_free);
1091 return (ret);
1095 ssl3_get_key_exchange(SSL *s)
1097 unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2];
1098 EVP_MD_CTX md_ctx;
1099 unsigned char *param, *p;
1100 int al, i, j, param_len, ok;
1101 long n, alg_k, alg_a;
1102 EVP_PKEY *pkey = NULL;
1103 const EVP_MD *md = NULL;
1104 RSA *rsa = NULL;
1105 DH *dh = NULL;
1106 EC_KEY *ecdh = NULL;
1107 BN_CTX *bn_ctx = NULL;
1108 EC_POINT *srvr_ecpoint = NULL;
1109 int curve_nid = 0;
1110 int encoded_pt_len = 0;
1112 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1113 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1116 * Use same message size as in ssl3_get_certificate_request()
1117 * as ServerKeyExchange message may be skipped.
1119 n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
1120 SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list, &ok);
1121 if (!ok)
1122 return ((int)n);
1124 EVP_MD_CTX_init(&md_ctx);
1126 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1128 * Do not skip server key exchange if this cipher suite uses
1129 * ephemeral keys.
1131 if (alg_k & (SSL_kDHE|SSL_kECDHE)) {
1132 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1133 SSL_R_UNEXPECTED_MESSAGE);
1134 al = SSL_AD_UNEXPECTED_MESSAGE;
1135 goto f_err;
1138 s->s3->tmp.reuse_message = 1;
1139 EVP_MD_CTX_cleanup(&md_ctx);
1140 return (1);
1143 if (s->session->sess_cert != NULL) {
1144 DH_free(s->session->sess_cert->peer_dh_tmp);
1145 s->session->sess_cert->peer_dh_tmp = NULL;
1147 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1148 s->session->sess_cert->peer_ecdh_tmp = NULL;
1149 } else {
1150 s->session->sess_cert = ssl_sess_cert_new();
1151 if (s->session->sess_cert == NULL)
1152 goto err;
1155 param = p = (unsigned char *)s->init_msg;
1156 param_len = 0;
1158 if (alg_k & SSL_kDHE) {
1159 if ((dh = DH_new()) == NULL) {
1160 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1161 ERR_R_DH_LIB);
1162 goto err;
1164 if (2 > n)
1165 goto truncated;
1166 n2s(p, i);
1167 param_len = i + 2;
1168 if (param_len > n) {
1169 al = SSL_AD_DECODE_ERROR;
1170 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1171 SSL_R_BAD_DH_P_LENGTH);
1172 goto f_err;
1174 if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1175 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1176 ERR_R_BN_LIB);
1177 goto err;
1179 p += i;
1181 if (param_len + 2 > n)
1182 goto truncated;
1183 n2s(p, i);
1184 param_len += i + 2;
1185 if (param_len > n) {
1186 al = SSL_AD_DECODE_ERROR;
1187 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1188 SSL_R_BAD_DH_G_LENGTH);
1189 goto f_err;
1191 if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1192 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1193 ERR_R_BN_LIB);
1194 goto err;
1196 p += i;
1198 if (param_len + 2 > n)
1199 goto truncated;
1200 n2s(p, i);
1201 param_len += i + 2;
1202 if (param_len > n) {
1203 al = SSL_AD_DECODE_ERROR;
1204 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1205 SSL_R_BAD_DH_PUB_KEY_LENGTH);
1206 goto f_err;
1208 if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1209 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1210 ERR_R_BN_LIB);
1211 goto err;
1213 p += i;
1214 n -= param_len;
1217 * Check the strength of the DH key just constructed.
1218 * Discard keys weaker than 1024 bits.
1221 if (DH_size(dh) < 1024 / 8) {
1222 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1223 SSL_R_BAD_DH_P_LENGTH);
1224 goto err;
1227 if (alg_a & SSL_aRSA)
1228 pkey = X509_get_pubkey(
1229 s->session->sess_cert->peer_pkeys[
1230 SSL_PKEY_RSA_ENC].x509);
1231 else if (alg_a & SSL_aDSS)
1232 pkey = X509_get_pubkey(
1233 s->session->sess_cert->peer_pkeys[
1234 SSL_PKEY_DSA_SIGN].x509);
1235 /* else anonymous DH, so no certificate or pkey. */
1237 s->session->sess_cert->peer_dh_tmp = dh;
1238 dh = NULL;
1239 } else if (alg_k & SSL_kECDHE) {
1240 const EC_GROUP *group;
1241 EC_GROUP *ngroup;
1243 if ((ecdh = EC_KEY_new()) == NULL) {
1244 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1245 ERR_R_MALLOC_FAILURE);
1246 goto err;
1250 * Extract elliptic curve parameters and the
1251 * server's ephemeral ECDH public key.
1252 * Keep accumulating lengths of various components in
1253 * param_len and make sure it never exceeds n.
1257 * XXX: For now we only support named (not generic) curves
1258 * and the ECParameters in this case is just three bytes.
1260 param_len = 3;
1261 if (param_len > n) {
1262 al = SSL_AD_DECODE_ERROR;
1263 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1264 SSL_R_LENGTH_TOO_SHORT);
1265 goto f_err;
1269 * Check curve is one of our preferences, if not server has
1270 * sent an invalid curve.
1272 if (tls1_check_curve(s, p, param_len) != 1) {
1273 al = SSL_AD_DECODE_ERROR;
1274 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1275 goto f_err;
1278 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1279 al = SSL_AD_INTERNAL_ERROR;
1280 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1281 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1282 goto f_err;
1285 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1286 if (ngroup == NULL) {
1287 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1288 ERR_R_EC_LIB);
1289 goto err;
1291 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1292 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1293 ERR_R_EC_LIB);
1294 goto err;
1296 EC_GROUP_free(ngroup);
1298 group = EC_KEY_get0_group(ecdh);
1300 p += 3;
1302 /* Next, get the encoded ECPoint */
1303 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1304 ((bn_ctx = BN_CTX_new()) == NULL)) {
1305 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1306 ERR_R_MALLOC_FAILURE);
1307 goto err;
1310 if (param_len + 1 > n)
1311 goto truncated;
1312 encoded_pt_len = *p;
1313 /* length of encoded point */
1314 p += 1;
1315 param_len += (1 + encoded_pt_len);
1316 if ((param_len > n) || (EC_POINT_oct2point(group, srvr_ecpoint,
1317 p, encoded_pt_len, bn_ctx) == 0)) {
1318 al = SSL_AD_DECODE_ERROR;
1319 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1320 SSL_R_BAD_ECPOINT);
1321 goto f_err;
1324 n -= param_len;
1325 p += encoded_pt_len;
1328 * The ECC/TLS specification does not mention the use
1329 * of DSA to sign ECParameters in the server key
1330 * exchange message. We do support RSA and ECDSA.
1332 if (alg_a & SSL_aRSA)
1333 pkey = X509_get_pubkey(
1334 s->session->sess_cert->peer_pkeys[
1335 SSL_PKEY_RSA_ENC].x509);
1336 else if (alg_a & SSL_aECDSA)
1337 pkey = X509_get_pubkey(
1338 s->session->sess_cert->peer_pkeys[
1339 SSL_PKEY_ECC].x509);
1340 /* Else anonymous ECDH, so no certificate or pkey. */
1341 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1342 s->session->sess_cert->peer_ecdh_tmp = ecdh;
1343 ecdh = NULL;
1344 BN_CTX_free(bn_ctx);
1345 bn_ctx = NULL;
1346 EC_POINT_free(srvr_ecpoint);
1347 srvr_ecpoint = NULL;
1348 } else if (alg_k) {
1349 al = SSL_AD_UNEXPECTED_MESSAGE;
1350 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1351 SSL_R_UNEXPECTED_MESSAGE);
1352 goto f_err;
1355 /* p points to the next byte, there are 'n' bytes left */
1357 /* if it was signed, check the signature */
1358 if (pkey != NULL) {
1359 if (SSL_USE_SIGALGS(s)) {
1360 int sigalg = tls12_get_sigid(pkey);
1361 /* Should never happen */
1362 if (sigalg == -1) {
1363 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1364 ERR_R_INTERNAL_ERROR);
1365 goto err;
1368 * Check key type is consistent
1369 * with signature
1371 if (2 > n)
1372 goto truncated;
1373 if (sigalg != (int)p[1]) {
1374 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1375 SSL_R_WRONG_SIGNATURE_TYPE);
1376 al = SSL_AD_DECODE_ERROR;
1377 goto f_err;
1379 md = tls12_get_hash(p[0]);
1380 if (md == NULL) {
1381 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1382 SSL_R_UNKNOWN_DIGEST);
1383 al = SSL_AD_DECODE_ERROR;
1384 goto f_err;
1386 p += 2;
1387 n -= 2;
1388 } else
1389 md = EVP_sha1();
1391 if (2 > n)
1392 goto truncated;
1393 n2s(p, i);
1394 n -= 2;
1395 j = EVP_PKEY_size(pkey);
1397 if (i != n || n > j) {
1398 /* wrong packet length */
1399 al = SSL_AD_DECODE_ERROR;
1400 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1401 SSL_R_WRONG_SIGNATURE_LENGTH);
1402 goto f_err;
1405 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1406 int num;
1408 j = 0;
1409 q = md_buf;
1410 for (num = 2; num > 0; num--) {
1411 if (!EVP_DigestInit_ex(&md_ctx,
1412 (num == 2) ? s->ctx->md5 : s->ctx->sha1,
1413 NULL)) {
1414 al = SSL_AD_INTERNAL_ERROR;
1415 goto f_err;
1417 EVP_DigestUpdate(&md_ctx,
1418 s->s3->client_random,
1419 SSL3_RANDOM_SIZE);
1420 EVP_DigestUpdate(&md_ctx,
1421 s->s3->server_random,
1422 SSL3_RANDOM_SIZE);
1423 EVP_DigestUpdate(&md_ctx, param, param_len);
1424 EVP_DigestFinal_ex(&md_ctx, q,
1425 (unsigned int *)&i);
1426 q += i;
1427 j += i;
1429 i = RSA_verify(NID_md5_sha1, md_buf, j,
1430 p, n, pkey->pkey.rsa);
1431 if (i < 0) {
1432 al = SSL_AD_DECRYPT_ERROR;
1433 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1434 SSL_R_BAD_RSA_DECRYPT);
1435 goto f_err;
1437 if (i == 0) {
1438 /* bad signature */
1439 al = SSL_AD_DECRYPT_ERROR;
1440 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1441 SSL_R_BAD_SIGNATURE);
1442 goto f_err;
1444 } else {
1445 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1446 EVP_VerifyUpdate(&md_ctx, s->s3->client_random,
1447 SSL3_RANDOM_SIZE);
1448 EVP_VerifyUpdate(&md_ctx, s->s3->server_random,
1449 SSL3_RANDOM_SIZE);
1450 EVP_VerifyUpdate(&md_ctx, param, param_len);
1451 if (EVP_VerifyFinal(&md_ctx, p,(int)n, pkey) <= 0) {
1452 /* bad signature */
1453 al = SSL_AD_DECRYPT_ERROR;
1454 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1455 SSL_R_BAD_SIGNATURE);
1456 goto f_err;
1459 } else {
1460 /* aNULL does not need public keys. */
1461 if (!(alg_a & SSL_aNULL)) {
1462 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1463 ERR_R_INTERNAL_ERROR);
1464 goto err;
1466 /* still data left over */
1467 if (n != 0) {
1468 al = SSL_AD_DECODE_ERROR;
1469 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1470 SSL_R_EXTRA_DATA_IN_MESSAGE);
1471 goto f_err;
1474 EVP_PKEY_free(pkey);
1475 EVP_MD_CTX_cleanup(&md_ctx);
1476 return (1);
1477 truncated:
1478 /* wrong packet length */
1479 al = SSL_AD_DECODE_ERROR;
1480 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_PACKET_LENGTH);
1481 f_err:
1482 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1483 err:
1484 EVP_PKEY_free(pkey);
1485 RSA_free(rsa);
1486 DH_free(dh);
1487 BN_CTX_free(bn_ctx);
1488 EC_POINT_free(srvr_ecpoint);
1489 EC_KEY_free(ecdh);
1490 EVP_MD_CTX_cleanup(&md_ctx);
1491 return (-1);
1495 ssl3_get_certificate_request(SSL *s)
1497 int ok, ret = 0;
1498 long n;
1499 uint8_t ctype_num;
1500 CBS cert_request, ctypes, rdn_list;
1501 X509_NAME *xn = NULL;
1502 const unsigned char *q;
1503 STACK_OF(X509_NAME) *ca_sk = NULL;
1505 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
1506 SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list, &ok);
1508 if (!ok)
1509 return ((int)n);
1511 s->s3->tmp.cert_req = 0;
1513 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1514 s->s3->tmp.reuse_message = 1;
1516 * If we get here we don't need any cached handshake records
1517 * as we wont be doing client auth.
1519 if (s->s3->handshake_buffer) {
1520 if (!tls1_digest_cached_records(s))
1521 goto err;
1523 return (1);
1526 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1527 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1528 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1529 SSL_R_WRONG_MESSAGE_TYPE);
1530 goto err;
1533 /* TLS does not like anon-DH with client cert */
1534 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
1535 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1536 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1537 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1538 goto err;
1541 if (n < 0)
1542 goto truncated;
1543 CBS_init(&cert_request, s->init_msg, n);
1545 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1546 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1547 ERR_R_MALLOC_FAILURE);
1548 goto err;
1551 /* get the certificate types */
1552 if (!CBS_get_u8(&cert_request, &ctype_num))
1553 goto truncated;
1555 if (ctype_num > SSL3_CT_NUMBER)
1556 ctype_num = SSL3_CT_NUMBER;
1557 if (!CBS_get_bytes(&cert_request, &ctypes, ctype_num) ||
1558 !CBS_write_bytes(&ctypes, s->s3->tmp.ctype,
1559 sizeof(s->s3->tmp.ctype), NULL)) {
1560 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1561 SSL_R_DATA_LENGTH_TOO_LONG);
1562 goto err;
1565 if (SSL_USE_SIGALGS(s)) {
1566 CBS sigalgs;
1568 if (CBS_len(&cert_request) < 2) {
1569 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1570 SSL_R_DATA_LENGTH_TOO_LONG);
1571 goto err;
1574 /* Check we have enough room for signature algorithms and
1575 * following length value.
1577 if (!CBS_get_u16_length_prefixed(&cert_request, &sigalgs)) {
1578 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1579 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1580 SSL_R_DATA_LENGTH_TOO_LONG);
1581 goto err;
1583 if ((CBS_len(&sigalgs) & 1) ||
1584 !tls1_process_sigalgs(s, CBS_data(&sigalgs),
1585 CBS_len(&sigalgs))) {
1586 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1587 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1588 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
1589 goto err;
1593 /* get the CA RDNs */
1594 if (CBS_len(&cert_request) < 2) {
1595 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1596 SSL_R_DATA_LENGTH_TOO_LONG);
1597 goto err;
1600 if (!CBS_get_u16_length_prefixed(&cert_request, &rdn_list) ||
1601 CBS_len(&cert_request) != 0) {
1602 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1603 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1604 SSL_R_LENGTH_MISMATCH);
1605 goto err;
1608 while (CBS_len(&rdn_list) > 0) {
1609 CBS rdn;
1611 if (CBS_len(&rdn_list) < 2) {
1612 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1613 SSL_R_DATA_LENGTH_TOO_LONG);
1614 goto err;
1617 if (!CBS_get_u16_length_prefixed(&rdn_list, &rdn)) {
1618 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1619 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1620 SSL_R_CA_DN_TOO_LONG);
1621 goto err;
1624 q = CBS_data(&rdn);
1625 if ((xn = d2i_X509_NAME(NULL, &q, CBS_len(&rdn))) == NULL) {
1626 ssl3_send_alert(s, SSL3_AL_FATAL,
1627 SSL_AD_DECODE_ERROR);
1628 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1629 ERR_R_ASN1_LIB);
1630 goto err;
1633 if (q != CBS_data(&rdn) + CBS_len(&rdn)) {
1634 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1635 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1636 SSL_R_CA_DN_LENGTH_MISMATCH);
1637 goto err;
1639 if (!sk_X509_NAME_push(ca_sk, xn)) {
1640 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1641 ERR_R_MALLOC_FAILURE);
1642 goto err;
1644 xn = NULL; /* avoid free in err block */
1647 /* we should setup a certificate to return.... */
1648 s->s3->tmp.cert_req = 1;
1649 s->s3->tmp.ctype_num = ctype_num;
1650 if (s->s3->tmp.ca_names != NULL)
1651 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
1652 s->s3->tmp.ca_names = ca_sk;
1653 ca_sk = NULL;
1655 ret = 1;
1656 if (0) {
1657 truncated:
1658 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1659 SSL_R_BAD_PACKET_LENGTH);
1661 err:
1662 X509_NAME_free(xn);
1663 if (ca_sk != NULL)
1664 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1665 return (ret);
1668 static int
1669 ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1671 return (X509_NAME_cmp(*a, *b));
1675 ssl3_get_new_session_ticket(SSL *s)
1677 int ok, al, ret = 0;
1678 uint32_t lifetime_hint;
1679 long n;
1680 CBS cbs, session_ticket;
1682 n = s->method->ssl_get_message(s, SSL3_ST_CR_SESSION_TICKET_A,
1683 SSL3_ST_CR_SESSION_TICKET_B, -1, 16384, &ok);
1684 if (!ok)
1685 return ((int)n);
1687 if (s->s3->tmp.message_type == SSL3_MT_FINISHED) {
1688 s->s3->tmp.reuse_message = 1;
1689 return (1);
1691 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET) {
1692 al = SSL_AD_UNEXPECTED_MESSAGE;
1693 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,
1694 SSL_R_BAD_MESSAGE_TYPE);
1695 goto f_err;
1698 if (n < 0) {
1699 al = SSL_AD_DECODE_ERROR;
1700 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,
1701 SSL_R_LENGTH_MISMATCH);
1702 goto f_err;
1705 CBS_init(&cbs, s->init_msg, n);
1706 if (!CBS_get_u32(&cbs, &lifetime_hint) ||
1707 #if UINT32_MAX > LONG_MAX
1708 lifetime_hint > LONG_MAX ||
1709 #endif
1710 !CBS_get_u16_length_prefixed(&cbs, &session_ticket) ||
1711 CBS_len(&cbs) != 0) {
1712 al = SSL_AD_DECODE_ERROR;
1713 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,
1714 SSL_R_LENGTH_MISMATCH);
1715 goto f_err;
1717 s->session->tlsext_tick_lifetime_hint = (long)lifetime_hint;
1719 if (!CBS_stow(&session_ticket, &s->session->tlsext_tick,
1720 &s->session->tlsext_ticklen)) {
1721 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,
1722 ERR_R_MALLOC_FAILURE);
1723 goto err;
1727 * There are two ways to detect a resumed ticket sesion.
1728 * One is to set an appropriate session ID and then the server
1729 * must return a match in ServerHello. This allows the normal
1730 * client session ID matching to work and we know much
1731 * earlier that the ticket has been accepted.
1733 * The other way is to set zero length session ID when the
1734 * ticket is presented and rely on the handshake to determine
1735 * session resumption.
1737 * We choose the former approach because this fits in with
1738 * assumptions elsewhere in OpenSSL. The session ID is set
1739 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1740 * ticket.
1742 EVP_Digest(CBS_data(&session_ticket), CBS_len(&session_ticket),
1743 s->session->session_id, &s->session->session_id_length,
1744 EVP_sha256(), NULL);
1745 ret = 1;
1746 return (ret);
1747 f_err:
1748 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1749 err:
1750 return (-1);
1754 ssl3_get_cert_status(SSL *s)
1756 CBS cert_status, response;
1757 size_t stow_len;
1758 int ok, al;
1759 long n;
1760 uint8_t status_type;
1762 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_STATUS_A,
1763 SSL3_ST_CR_CERT_STATUS_B, SSL3_MT_CERTIFICATE_STATUS,
1764 16384, &ok);
1766 if (!ok)
1767 return ((int)n);
1769 if (n < 0) {
1770 /* need at least status type + length */
1771 al = SSL_AD_DECODE_ERROR;
1772 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1773 SSL_R_LENGTH_MISMATCH);
1774 goto f_err;
1777 CBS_init(&cert_status, s->init_msg, n);
1778 if (!CBS_get_u8(&cert_status, &status_type) ||
1779 CBS_len(&cert_status) < 3) {
1780 /* need at least status type + length */
1781 al = SSL_AD_DECODE_ERROR;
1782 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1783 SSL_R_LENGTH_MISMATCH);
1784 goto f_err;
1787 if (status_type != TLSEXT_STATUSTYPE_ocsp) {
1788 al = SSL_AD_DECODE_ERROR;
1789 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1790 SSL_R_UNSUPPORTED_STATUS_TYPE);
1791 goto f_err;
1794 if (!CBS_get_u24_length_prefixed(&cert_status, &response) ||
1795 CBS_len(&cert_status) != 0) {
1796 al = SSL_AD_DECODE_ERROR;
1797 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1798 SSL_R_LENGTH_MISMATCH);
1799 goto f_err;
1802 if (!CBS_stow(&response, &s->tlsext_ocsp_resp,
1803 &stow_len) || stow_len > INT_MAX) {
1804 s->tlsext_ocsp_resplen = 0;
1805 al = SSL_AD_INTERNAL_ERROR;
1806 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1807 ERR_R_MALLOC_FAILURE);
1808 goto f_err;
1810 s->tlsext_ocsp_resplen = (int)stow_len;
1812 if (s->ctx->tlsext_status_cb) {
1813 int ret;
1814 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1815 if (ret == 0) {
1816 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1817 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1818 SSL_R_INVALID_STATUS_RESPONSE);
1819 goto f_err;
1821 if (ret < 0) {
1822 al = SSL_AD_INTERNAL_ERROR;
1823 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,
1824 ERR_R_MALLOC_FAILURE);
1825 goto f_err;
1828 return (1);
1829 f_err:
1830 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1831 return (-1);
1835 ssl3_get_server_done(SSL *s)
1837 int ok, ret = 0;
1838 long n;
1840 n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
1841 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
1842 30, /* should be very small, like 0 :-) */ &ok);
1844 if (!ok)
1845 return ((int)n);
1846 if (n > 0) {
1847 /* should contain no data */
1848 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1849 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
1850 return (-1);
1852 ret = 1;
1853 return (ret);
1856 static int
1857 ssl3_send_client_kex_rsa(SSL *s, SESS_CERT *sess_cert, unsigned char *p,
1858 int *outlen)
1860 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1861 EVP_PKEY *pkey = NULL;
1862 unsigned char *q;
1863 int ret = -1;
1864 int n;
1866 pkey = X509_get_pubkey(sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1867 if (pkey == NULL || pkey->type != EVP_PKEY_RSA ||
1868 pkey->pkey.rsa == NULL) {
1869 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1870 ERR_R_INTERNAL_ERROR);
1871 goto err;
1874 tmp_buf[0] = s->client_version >> 8;
1875 tmp_buf[1] = s->client_version & 0xff;
1876 arc4random_buf(&tmp_buf[2], sizeof(tmp_buf) - 2);
1878 s->session->master_key_length = sizeof(tmp_buf);
1880 q = p;
1881 p += 2;
1883 n = RSA_public_encrypt(sizeof(tmp_buf), tmp_buf, p, pkey->pkey.rsa,
1884 RSA_PKCS1_PADDING);
1885 if (n <= 0) {
1886 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1887 SSL_R_BAD_RSA_ENCRYPT);
1888 goto err;
1891 s2n(n, q);
1892 n += 2;
1894 s->session->master_key_length =
1895 s->method->ssl3_enc->generate_master_secret(s,
1896 s->session->master_key, tmp_buf, sizeof(tmp_buf));
1898 *outlen = n;
1899 ret = 1;
1901 err:
1902 explicit_bzero(tmp_buf, sizeof(tmp_buf));
1903 EVP_PKEY_free(pkey);
1905 return (ret);
1908 static int
1909 ssl3_send_client_kex_dhe(SSL *s, SESS_CERT *sess_cert, unsigned char *p,
1910 int *outlen)
1912 DH *dh_srvr = NULL, *dh_clnt = NULL;
1913 unsigned char *key = NULL;
1914 int key_size, n;
1915 int ret = -1;
1917 /* Ensure that we have an ephemeral key for DHE. */
1918 if (sess_cert->peer_dh_tmp == NULL) {
1919 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1920 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1921 SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1922 goto err;
1924 dh_srvr = sess_cert->peer_dh_tmp;
1926 /* Generate a new random key. */
1927 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
1928 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1929 goto err;
1931 if (!DH_generate_key(dh_clnt)) {
1932 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1933 goto err;
1935 key_size = DH_size(dh_clnt);
1936 if ((key = malloc(key_size)) == NULL) {
1937 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1938 ERR_R_MALLOC_FAILURE);
1939 goto err;
1941 n = DH_compute_key(key, dh_srvr->pub_key, dh_clnt);
1942 if (n <= 0) {
1943 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1944 goto err;
1947 /* Generate master key from the result. */
1948 s->session->master_key_length =
1949 s->method->ssl3_enc->generate_master_secret(s,
1950 s->session->master_key, key, n);
1952 /* Send off the data. */
1953 n = BN_num_bytes(dh_clnt->pub_key);
1954 s2n(n, p);
1955 BN_bn2bin(dh_clnt->pub_key, p);
1956 n += 2;
1958 *outlen = n;
1959 ret = 1;
1961 err:
1962 DH_free(dh_clnt);
1963 if (key != NULL)
1964 explicit_bzero(key, key_size);
1965 free(key);
1967 return (ret);
1970 static int
1971 ssl3_send_client_kex_ecdh(SSL *s, SESS_CERT *sess_cert, unsigned char *p,
1972 int *outlen)
1974 EC_KEY *tkey, *clnt_ecdh = NULL;
1975 const EC_GROUP *srvr_group = NULL;
1976 const EC_POINT *srvr_ecpoint = NULL;
1977 EVP_PKEY *srvr_pub_pkey = NULL;
1978 BN_CTX *bn_ctx = NULL;
1979 unsigned char *encodedPoint = NULL;
1980 unsigned char *key = NULL;
1981 unsigned long alg_k;
1982 int encoded_pt_len = 0;
1983 int key_size, n;
1984 int ret = -1;
1986 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1988 /* Ensure that we have an ephemeral key for ECDHE. */
1989 if ((alg_k & SSL_kECDHE) && sess_cert->peer_ecdh_tmp == NULL) {
1990 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1991 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1992 ERR_R_INTERNAL_ERROR);
1993 goto err;
1995 tkey = sess_cert->peer_ecdh_tmp;
1997 if (alg_k & (SSL_kECDHr|SSL_kECDHe)) {
1998 /* Get the Server Public Key from certificate. */
1999 srvr_pub_pkey = X509_get_pubkey(
2000 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2001 if (srvr_pub_pkey != NULL && srvr_pub_pkey->type == EVP_PKEY_EC)
2002 tkey = srvr_pub_pkey->pkey.ec;
2005 if (tkey == NULL) {
2006 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2007 ERR_R_INTERNAL_ERROR);
2008 goto err;
2011 srvr_group = EC_KEY_get0_group(tkey);
2012 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2014 if (srvr_group == NULL || srvr_ecpoint == NULL) {
2015 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2016 ERR_R_INTERNAL_ERROR);
2017 goto err;
2020 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2021 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2022 ERR_R_MALLOC_FAILURE);
2023 goto err;
2026 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2027 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2028 goto err;
2031 /* Generate a new ECDH key pair. */
2032 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2033 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2034 goto err;
2036 key_size = ECDH_size(clnt_ecdh);
2037 if (key_size <= 0) {
2038 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2039 goto err;
2041 if ((key = malloc(key_size)) == NULL) {
2042 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2043 ERR_R_MALLOC_FAILURE);
2045 n = ECDH_compute_key(key, key_size, srvr_ecpoint, clnt_ecdh, NULL);
2046 if (n <= 0) {
2047 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2048 goto err;
2051 /* Generate master key from the result. */
2052 s->session->master_key_length =
2053 s->method->ssl3_enc->generate_master_secret(s,
2054 s->session->master_key, key, n);
2057 * First check the size of encoding and allocate memory accordingly.
2059 encoded_pt_len = EC_POINT_point2oct(srvr_group,
2060 EC_KEY_get0_public_key(clnt_ecdh),
2061 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
2063 bn_ctx = BN_CTX_new();
2064 encodedPoint = malloc(encoded_pt_len);
2065 if (encodedPoint == NULL || bn_ctx == NULL) {
2066 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2067 ERR_R_MALLOC_FAILURE);
2068 goto err;
2071 /* Encode the public key */
2072 n = EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
2073 POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len,
2074 bn_ctx);
2076 *p = n; /* length of encoded point */
2077 /* Encoded point will be copied here */
2078 p += 1;
2080 /* copy the point */
2081 memcpy((unsigned char *)p, encodedPoint, n);
2082 /* increment n to account for length field */
2083 n += 1;
2085 *outlen = n;
2086 ret = 1;
2088 err:
2089 if (key != NULL)
2090 explicit_bzero(key, key_size);
2091 free(key);
2093 BN_CTX_free(bn_ctx);
2094 free(encodedPoint);
2095 EC_KEY_free(clnt_ecdh);
2096 EVP_PKEY_free(srvr_pub_pkey);
2098 return (ret);
2101 static int
2102 ssl3_send_client_kex_gost(SSL *s, SESS_CERT *sess_cert, unsigned char *p,
2103 int *outlen)
2105 unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
2106 EVP_PKEY *pub_key = NULL;
2107 EVP_PKEY_CTX *pkey_ctx;
2108 X509 *peer_cert;
2109 size_t msglen;
2110 unsigned int md_len;
2111 EVP_MD_CTX *ukm_hash;
2112 int ret = -1;
2113 int nid;
2114 int n;
2116 /* Get server sertificate PKEY and create ctx from it */
2117 peer_cert = sess_cert->peer_pkeys[SSL_PKEY_GOST01].x509;
2118 if (peer_cert == NULL) {
2119 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2120 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2121 goto err;
2124 pub_key = X509_get_pubkey(peer_cert);
2125 pkey_ctx = EVP_PKEY_CTX_new(pub_key, NULL);
2128 * If we have send a certificate, and certificate key parameters match
2129 * those of server certificate, use certificate key for key exchange.
2130 * Otherwise, generate ephemeral key pair.
2132 EVP_PKEY_encrypt_init(pkey_ctx);
2134 /* Generate session key. */
2135 arc4random_buf(premaster_secret, 32);
2138 * If we have client certificate, use its secret as peer key.
2140 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2141 if (EVP_PKEY_derive_set_peer(pkey_ctx,
2142 s->cert->key->privatekey) <=0) {
2144 * If there was an error - just ignore it.
2145 * Ephemeral key would be used.
2147 ERR_clear_error();
2152 * Compute shared IV and store it in algorithm-specific context data.
2154 ukm_hash = EVP_MD_CTX_create();
2155 if (ukm_hash == NULL) {
2156 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2157 ERR_R_MALLOC_FAILURE);
2158 goto err;
2161 if (ssl_get_algorithm2(s) & SSL_HANDSHAKE_MAC_GOST94)
2162 nid = NID_id_GostR3411_94;
2163 else
2164 nid = NID_id_tc26_gost3411_2012_256;
2165 if (!EVP_DigestInit(ukm_hash, EVP_get_digestbynid(nid)))
2166 goto err;
2167 EVP_DigestUpdate(ukm_hash, s->s3->client_random, SSL3_RANDOM_SIZE);
2168 EVP_DigestUpdate(ukm_hash, s->s3->server_random, SSL3_RANDOM_SIZE);
2169 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2170 EVP_MD_CTX_destroy(ukm_hash);
2171 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
2172 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
2173 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_LIBRARY_BUG);
2174 goto err;
2178 * Make GOST keytransport blob message, encapsulate it into sequence.
2180 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2181 msglen = 255;
2182 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret,
2183 32) < 0) {
2184 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_LIBRARY_BUG);
2185 goto err;
2187 if (msglen >= 0x80) {
2188 *(p++) = 0x81;
2189 *(p++) = msglen & 0xff;
2190 n = msglen + 3;
2191 } else {
2192 *(p++) = msglen & 0xff;
2193 n = msglen + 2;
2195 memcpy(p, tmp, msglen);
2197 /* Check if pubkey from client certificate was used. */
2198 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
2199 NULL) > 0) {
2200 /* Set flag "skip certificate verify". */
2201 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2203 EVP_PKEY_CTX_free(pkey_ctx);
2204 s->session->master_key_length =
2205 s->method->ssl3_enc->generate_master_secret(s,
2206 s->session->master_key, premaster_secret, 32);
2208 *outlen = n;
2209 ret = 1;
2211 err:
2212 explicit_bzero(premaster_secret, sizeof(premaster_secret));
2213 EVP_PKEY_free(pub_key);
2215 return (ret);
2219 ssl3_send_client_key_exchange(SSL *s)
2221 SESS_CERT *sess_cert;
2222 unsigned long alg_k;
2223 unsigned char *p;
2224 int n = 0;
2226 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2227 p = ssl3_handshake_msg_start(s, SSL3_MT_CLIENT_KEY_EXCHANGE);
2229 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2231 if ((sess_cert = s->session->sess_cert) == NULL) {
2232 ssl3_send_alert(s, SSL3_AL_FATAL,
2233 SSL_AD_UNEXPECTED_MESSAGE);
2234 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2235 ERR_R_INTERNAL_ERROR);
2236 goto err;
2239 if (alg_k & SSL_kRSA) {
2240 if (ssl3_send_client_kex_rsa(s, sess_cert, p, &n) != 1)
2241 goto err;
2242 } else if (alg_k & SSL_kDHE) {
2243 if (ssl3_send_client_kex_dhe(s, sess_cert, p, &n) != 1)
2244 goto err;
2245 } else if (alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) {
2246 if (ssl3_send_client_kex_ecdh(s, sess_cert, p, &n) != 1)
2247 goto err;
2248 } else if (alg_k & SSL_kGOST) {
2249 if (ssl3_send_client_kex_gost(s, sess_cert, p, &n) != 1)
2250 goto err;
2251 } else {
2252 ssl3_send_alert(s, SSL3_AL_FATAL,
2253 SSL_AD_HANDSHAKE_FAILURE);
2254 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2255 ERR_R_INTERNAL_ERROR);
2256 goto err;
2259 ssl3_handshake_msg_finish(s, n);
2261 s->state = SSL3_ST_CW_KEY_EXCH_B;
2264 /* SSL3_ST_CW_KEY_EXCH_B */
2265 return (ssl3_handshake_write(s));
2267 err:
2268 return (-1);
2272 ssl3_send_client_verify(SSL *s)
2274 unsigned char *p;
2275 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2276 EVP_PKEY *pkey;
2277 EVP_PKEY_CTX *pctx = NULL;
2278 EVP_MD_CTX mctx;
2279 unsigned u = 0;
2280 unsigned long n;
2281 int j;
2283 EVP_MD_CTX_init(&mctx);
2285 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2286 p = ssl3_handshake_msg_start(s, SSL3_MT_CERTIFICATE_VERIFY);
2289 * Create context from key and test if sha1 is allowed as
2290 * digest.
2292 pkey = s->cert->key->privatekey;
2293 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2294 EVP_PKEY_sign_init(pctx);
2295 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
2296 if (!SSL_USE_SIGALGS(s))
2297 s->method->ssl3_enc->cert_verify_mac(s,
2298 NID_sha1, &(data[MD5_DIGEST_LENGTH]));
2299 } else {
2300 ERR_clear_error();
2303 * For TLS v1.2 send signature algorithm and signature
2304 * using agreed digest and cached handshake records.
2306 if (SSL_USE_SIGALGS(s)) {
2307 long hdatalen = 0;
2308 void *hdata;
2309 const EVP_MD *md = s->cert->key->digest;
2310 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
2311 &hdata);
2312 if (hdatalen <= 0 ||
2313 !tls12_get_sigandhash(p, pkey, md)) {
2314 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2315 ERR_R_INTERNAL_ERROR);
2316 goto err;
2318 p += 2;
2319 if (!EVP_SignInit_ex(&mctx, md, NULL) ||
2320 !EVP_SignUpdate(&mctx, hdata, hdatalen) ||
2321 !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
2322 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2323 ERR_R_EVP_LIB);
2324 goto err;
2326 s2n(u, p);
2327 n = u + 4;
2328 if (!tls1_digest_cached_records(s))
2329 goto err;
2330 } else if (pkey->type == EVP_PKEY_RSA) {
2331 s->method->ssl3_enc->cert_verify_mac(
2332 s, NID_md5, &(data[0]));
2333 if (RSA_sign(NID_md5_sha1, data,
2334 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, &(p[2]),
2335 &u, pkey->pkey.rsa) <= 0 ) {
2336 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2337 ERR_R_RSA_LIB);
2338 goto err;
2340 s2n(u, p);
2341 n = u + 2;
2342 } else if (pkey->type == EVP_PKEY_DSA) {
2343 if (!DSA_sign(pkey->save_type,
2344 &(data[MD5_DIGEST_LENGTH]),
2345 SHA_DIGEST_LENGTH, &(p[2]),
2346 (unsigned int *)&j, pkey->pkey.dsa)) {
2347 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2348 ERR_R_DSA_LIB);
2349 goto err;
2351 s2n(j, p);
2352 n = j + 2;
2353 } else if (pkey->type == EVP_PKEY_EC) {
2354 if (!ECDSA_sign(pkey->save_type,
2355 &(data[MD5_DIGEST_LENGTH]),
2356 SHA_DIGEST_LENGTH, &(p[2]),
2357 (unsigned int *)&j, pkey->pkey.ec)) {
2358 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2359 ERR_R_ECDSA_LIB);
2360 goto err;
2362 s2n(j, p);
2363 n = j + 2;
2364 #ifndef OPENSSL_NO_GOST
2365 } else if (pkey->type == NID_id_GostR3410_94 ||
2366 pkey->type == NID_id_GostR3410_2001) {
2367 unsigned char signbuf[128];
2368 long hdatalen = 0;
2369 void *hdata;
2370 const EVP_MD *md;
2371 int nid;
2372 size_t sigsize;
2374 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2375 if (hdatalen <= 0) {
2376 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2377 ERR_R_INTERNAL_ERROR);
2378 goto err;
2380 if (!EVP_PKEY_get_default_digest_nid(pkey, &nid) ||
2381 !(md = EVP_get_digestbynid(nid))) {
2382 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2383 ERR_R_EVP_LIB);
2384 goto err;
2386 if (!EVP_DigestInit_ex(&mctx, md, NULL) ||
2387 !EVP_DigestUpdate(&mctx, hdata, hdatalen) ||
2388 !EVP_DigestFinal(&mctx, signbuf, &u) ||
2389 (EVP_PKEY_CTX_set_signature_md(pctx, md) <= 0) ||
2390 (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
2391 EVP_PKEY_CTRL_GOST_SIG_FORMAT,
2392 GOST_SIG_FORMAT_RS_LE,
2393 NULL) <= 0) ||
2394 (EVP_PKEY_sign(pctx, &(p[2]), &sigsize,
2395 signbuf, u) <= 0)) {
2396 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2397 ERR_R_EVP_LIB);
2398 goto err;
2400 if (!tls1_digest_cached_records(s))
2401 goto err;
2402 j = sigsize;
2403 s2n(j, p);
2404 n = j + 2;
2405 #endif
2406 } else {
2407 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2408 ERR_R_INTERNAL_ERROR);
2409 goto err;
2412 s->state = SSL3_ST_CW_CERT_VRFY_B;
2414 ssl3_handshake_msg_finish(s, n);
2417 EVP_MD_CTX_cleanup(&mctx);
2418 EVP_PKEY_CTX_free(pctx);
2420 return (ssl3_handshake_write(s));
2422 err:
2423 EVP_MD_CTX_cleanup(&mctx);
2424 EVP_PKEY_CTX_free(pctx);
2425 return (-1);
2429 ssl3_send_client_certificate(SSL *s)
2431 X509 *x509 = NULL;
2432 EVP_PKEY *pkey = NULL;
2433 int i;
2434 unsigned long l;
2436 if (s->state == SSL3_ST_CW_CERT_A) {
2437 if ((s->cert == NULL) || (s->cert->key->x509 == NULL) ||
2438 (s->cert->key->privatekey == NULL))
2439 s->state = SSL3_ST_CW_CERT_B;
2440 else
2441 s->state = SSL3_ST_CW_CERT_C;
2444 /* We need to get a client cert */
2445 if (s->state == SSL3_ST_CW_CERT_B) {
2447 * If we get an error, we need to
2448 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2449 * We then get retied later
2451 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2452 if (i < 0) {
2453 s->rwstate = SSL_X509_LOOKUP;
2454 return (-1);
2456 s->rwstate = SSL_NOTHING;
2457 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
2458 s->state = SSL3_ST_CW_CERT_B;
2459 if (!SSL_use_certificate(s, x509) ||
2460 !SSL_use_PrivateKey(s, pkey))
2461 i = 0;
2462 } else if (i == 1) {
2463 i = 0;
2464 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
2465 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2468 X509_free(x509);
2469 EVP_PKEY_free(pkey);
2470 if (i == 0)
2471 s->s3->tmp.cert_req = 2;
2473 /* Ok, we have a cert */
2474 s->state = SSL3_ST_CW_CERT_C;
2477 if (s->state == SSL3_ST_CW_CERT_C) {
2478 s->state = SSL3_ST_CW_CERT_D;
2479 l = ssl3_output_cert_chain(s,
2480 (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key->x509);
2481 s->init_num = (int)l;
2482 s->init_off = 0;
2484 /* SSL3_ST_CW_CERT_D */
2485 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2488 #define has_bits(i,m) (((i)&(m)) == (m))
2491 ssl3_check_cert_and_algorithm(SSL *s)
2493 int i, idx;
2494 long alg_k, alg_a;
2495 EVP_PKEY *pkey = NULL;
2496 SESS_CERT *sc;
2497 DH *dh;
2499 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2500 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2502 /* We don't have a certificate. */
2503 if (alg_a & SSL_aNULL)
2504 return (1);
2506 sc = s->session->sess_cert;
2507 if (sc == NULL) {
2508 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2509 ERR_R_INTERNAL_ERROR);
2510 goto err;
2512 dh = s->session->sess_cert->peer_dh_tmp;
2514 /* This is the passed certificate. */
2516 idx = sc->peer_cert_type;
2517 if (idx == SSL_PKEY_ECC) {
2518 if (ssl_check_srvr_ecc_cert_and_alg(
2519 sc->peer_pkeys[idx].x509, s) == 0) {
2520 /* check failed */
2521 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2522 SSL_R_BAD_ECC_CERT);
2523 goto f_err;
2524 } else {
2525 return (1);
2528 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
2529 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
2530 EVP_PKEY_free(pkey);
2532 /* Check that we have a certificate if we require one. */
2533 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA|EVP_PKT_SIGN)) {
2534 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2535 SSL_R_MISSING_RSA_SIGNING_CERT);
2536 goto f_err;
2537 } else if ((alg_a & SSL_aDSS) &&
2538 !has_bits(i, EVP_PK_DSA|EVP_PKT_SIGN)) {
2539 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2540 SSL_R_MISSING_DSA_SIGNING_CERT);
2541 goto f_err;
2543 if ((alg_k & SSL_kRSA) &&
2544 !has_bits(i, EVP_PK_RSA|EVP_PKT_ENC)) {
2545 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2546 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2547 goto f_err;
2549 if ((alg_k & SSL_kDHE) &&
2550 !(has_bits(i, EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) {
2551 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
2552 SSL_R_MISSING_DH_KEY);
2553 goto f_err;
2556 return (1);
2557 f_err:
2558 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2559 err:
2560 return (0);
2564 ssl3_send_next_proto(SSL *s)
2566 unsigned int len, padding_len;
2567 unsigned char *d, *p;
2569 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
2570 d = p = ssl3_handshake_msg_start(s, SSL3_MT_NEXT_PROTO);
2572 len = s->next_proto_negotiated_len;
2573 padding_len = 32 - ((len + 2) % 32);
2574 *(p++) = len;
2575 memcpy(p, s->next_proto_negotiated, len);
2576 p += len;
2577 *(p++) = padding_len;
2578 memset(p, 0, padding_len);
2579 p += padding_len;
2581 ssl3_handshake_msg_finish(s, p - d);
2583 s->state = SSL3_ST_CW_NEXT_PROTO_B;
2586 return (ssl3_handshake_write(s));
2590 * Check to see if handshake is full or resumed. Usually this is just a
2591 * case of checking to see if a cache hit has occurred. In the case of
2592 * session tickets we have to check the next message to be sure.
2596 ssl3_check_finished(SSL *s)
2598 int ok;
2599 long n;
2601 /* If we have no ticket it cannot be a resumed session. */
2602 if (!s->session->tlsext_tick)
2603 return (1);
2604 /* this function is called when we really expect a Certificate
2605 * message, so permit appropriate message length */
2606 n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A,
2607 SSL3_ST_CR_CERT_B, -1, s->max_cert_list, &ok);
2608 if (!ok)
2609 return ((int)n);
2610 s->s3->tmp.reuse_message = 1;
2611 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED) ||
2612 (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
2613 return (2);
2615 return (1);
2619 ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
2621 int i = 0;
2623 #ifndef OPENSSL_NO_ENGINE
2624 if (s->ctx->client_cert_engine) {
2625 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
2626 SSL_get_client_CA_list(s),
2627 px509, ppkey, NULL, NULL, NULL);
2628 if (i != 0)
2629 return (i);
2631 #endif
2632 if (s->ctx->client_cert_cb)
2633 i = s->ctx->client_cert_cb(s, px509, ppkey);
2634 return (i);