1 /* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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
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
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
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
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
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
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
151 #define REUSE_CIPHER_BUG
152 #define NETSCAPE_HANG_BUG
155 #include "ssl_locl.h"
156 #include "kssl_lcl.h"
157 #include "../crypto/constant_time_locl.h"
158 #include <openssl/buffer.h>
159 #include <openssl/rand.h>
160 #include <openssl/objects.h>
161 #include <openssl/evp.h>
162 #include <openssl/hmac.h>
163 #include <openssl/x509.h>
164 #ifndef OPENSSL_NO_DH
165 #include <openssl/dh.h>
167 #include <openssl/bn.h>
168 #ifndef OPENSSL_NO_KRB5
169 #include <openssl/krb5_asn.h>
171 #include <openssl/md5.h>
173 #ifndef OPENSSL_NO_SSL3_METHOD
174 static const SSL_METHOD *ssl3_get_server_method(int ver);
176 static const SSL_METHOD *ssl3_get_server_method(int ver)
178 if (ver == SSL3_VERSION)
179 return(SSLv3_server_method());
184 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
186 ssl_undefined_function,
187 ssl3_get_server_method)
190 #ifndef OPENSSL_NO_SRP
191 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
193 int ret = SSL_ERROR_NONE;
195 *al = SSL_AD_UNRECOGNIZED_NAME;
197 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
198 (s->srp_ctx.TLS_ext_srp_username_callback != NULL))
200 if(s->srp_ctx.login == NULL)
202 /* RFC 5054 says SHOULD reject,
203 we do so if There is no srp login name */
205 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
209 ret = SSL_srp_server_param_with_username(s,al);
216 int ssl3_accept(SSL *s)
219 unsigned long alg_k,Time=(unsigned long)time(NULL);
220 void (*cb)(const SSL *ssl,int type,int val)=NULL;
222 int new_state,state,skip=0;
224 RAND_add(&Time,sizeof(Time),0);
228 if (s->info_callback != NULL)
230 else if (s->ctx->info_callback != NULL)
231 cb=s->ctx->info_callback;
233 /* init things to blank */
235 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
239 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
243 #ifndef OPENSSL_NO_HEARTBEATS
244 /* If we're awaiting a HeartbeatResponse, pretend we
245 * already got and don't await it anymore, because
246 * Heartbeats don't make sense during handshakes anyway.
248 if (s->tlsext_hb_pending)
250 s->tlsext_hb_pending = 0;
261 case SSL_ST_RENEGOTIATE:
263 /* s->state=SSL_ST_ACCEPT; */
267 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
268 case SSL_ST_OK|SSL_ST_ACCEPT:
271 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
273 if ((s->version>>8) != 3)
275 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
278 s->type=SSL_ST_ACCEPT;
280 if (s->init_buf == NULL)
282 if ((buf=BUF_MEM_new()) == NULL)
287 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
296 if (!ssl3_setup_buffers(s))
303 s->s3->flags &= ~TLS1_FLAGS_SKIP_CERT_VERIFY;
304 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
305 /* Should have been reset by ssl3_get_finished, too. */
306 s->s3->change_cipher_spec = 0;
308 if (s->state != SSL_ST_RENEGOTIATE)
310 /* Ok, we now need to push on a buffering BIO so that
311 * the output is sent in a way that TCP likes :-)
313 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
315 ssl3_init_finished_mac(s);
316 s->state=SSL3_ST_SR_CLNT_HELLO_A;
317 s->ctx->stats.sess_accept++;
319 else if (!s->s3->send_connection_binding &&
320 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
322 /* Server attempting to renegotiate with
323 * client that doesn't support secure
326 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
327 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
333 /* s->state == SSL_ST_RENEGOTIATE,
334 * we will just send a HelloRequest */
335 s->ctx->stats.sess_accept_renegotiate++;
336 s->state=SSL3_ST_SW_HELLO_REQ_A;
340 case SSL3_ST_SW_HELLO_REQ_A:
341 case SSL3_ST_SW_HELLO_REQ_B:
344 ret=ssl3_send_hello_request(s);
345 if (ret <= 0) goto end;
346 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
347 s->state=SSL3_ST_SW_FLUSH;
350 ssl3_init_finished_mac(s);
353 case SSL3_ST_SW_HELLO_REQ_C:
357 case SSL3_ST_SR_CLNT_HELLO_A:
358 case SSL3_ST_SR_CLNT_HELLO_B:
359 case SSL3_ST_SR_CLNT_HELLO_C:
362 ret=ssl3_get_client_hello(s);
363 if (ret <= 0) goto end;
364 #ifndef OPENSSL_NO_SRP
365 s->state = SSL3_ST_SR_CLNT_HELLO_D;
366 case SSL3_ST_SR_CLNT_HELLO_D:
369 if ((ret = ssl_check_srp_ext_ClientHello(s,&al)) < 0)
371 /* callback indicates firther work to be done */
372 s->rwstate=SSL_X509_LOOKUP;
375 if (ret != SSL_ERROR_NONE)
377 ssl3_send_alert(s,SSL3_AL_FATAL,al);
378 /* This is not really an error but the only means to
379 for a client to detect whether srp is supported. */
380 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
381 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_CLIENTHELLO_TLSEXT);
382 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
390 s->state=SSL3_ST_SW_SRVR_HELLO_A;
394 case SSL3_ST_SW_SRVR_HELLO_A:
395 case SSL3_ST_SW_SRVR_HELLO_B:
396 ret=ssl3_send_server_hello(s);
397 if (ret <= 0) goto end;
398 #ifndef OPENSSL_NO_TLSEXT
401 if (s->tlsext_ticket_expected)
402 s->state=SSL3_ST_SW_SESSION_TICKET_A;
404 s->state=SSL3_ST_SW_CHANGE_A;
408 s->state=SSL3_ST_SW_CHANGE_A;
411 s->state = SSL3_ST_SW_CERT_A;
415 case SSL3_ST_SW_CERT_A:
416 case SSL3_ST_SW_CERT_B:
417 /* Check if it is anon DH or anon ECDH, */
418 /* normal PSK or KRB5 or SRP */
419 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL|SSL_aKRB5|SSL_aSRP))
420 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
422 ret=ssl3_send_server_certificate(s);
423 if (ret <= 0) goto end;
424 #ifndef OPENSSL_NO_TLSEXT
425 if (s->tlsext_status_expected)
426 s->state=SSL3_ST_SW_CERT_STATUS_A;
428 s->state=SSL3_ST_SW_KEY_EXCH_A;
433 s->state=SSL3_ST_SW_KEY_EXCH_A;
440 s->state=SSL3_ST_SW_KEY_EXCH_A;
445 case SSL3_ST_SW_KEY_EXCH_A:
446 case SSL3_ST_SW_KEY_EXCH_B:
447 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
450 * clear this, it may get reset by
451 * send_server_key_exchange
453 s->s3->tmp.use_rsa_tmp=0;
456 /* only send if a DH key exchange, fortezza or
457 * RSA but we have a sign only certificate
459 * PSK: may send PSK identity hints
461 * For ECC ciphersuites, we send a serverKeyExchange
462 * message only if the cipher suite is either
463 * ECDH-anon or ECDHE. In other cases, the
464 * server certificate contains the server's
465 * public key for key exchange.
468 /* PSK: send ServerKeyExchange if PSK identity
469 * hint if provided */
470 #ifndef OPENSSL_NO_PSK
471 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
473 #ifndef OPENSSL_NO_SRP
474 /* SRP: send ServerKeyExchange */
475 || (alg_k & SSL_kSRP)
477 || (alg_k & SSL_kEDH)
478 || (alg_k & SSL_kEECDH)
479 || ((alg_k & SSL_kRSA)
480 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
481 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
482 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
488 ret=ssl3_send_server_key_exchange(s);
489 if (ret <= 0) goto end;
494 s->state=SSL3_ST_SW_CERT_REQ_A;
498 case SSL3_ST_SW_CERT_REQ_A:
499 case SSL3_ST_SW_CERT_REQ_B:
500 if (/* don't request cert unless asked for it: */
501 !(s->verify_mode & SSL_VERIFY_PEER) ||
502 /* if SSL_VERIFY_CLIENT_ONCE is set,
503 * don't request cert during re-negotiation: */
504 ((s->session->peer != NULL) &&
505 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
506 /* never request cert in anonymous ciphersuites
507 * (see section "Certificate request" in SSL 3 drafts
508 * and in RFC 2246): */
509 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
510 /* ... except when the application insists on verification
511 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
512 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
513 /* never request cert in Kerberos ciphersuites */
514 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) ||
515 /* don't request certificate for SRP auth */
516 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
517 /* With normal PSK Certificates and
518 * Certificate Requests are omitted */
519 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
521 /* no cert request */
523 s->s3->tmp.cert_request=0;
524 s->state=SSL3_ST_SW_SRVR_DONE_A;
525 if (s->s3->handshake_buffer)
526 if (!ssl3_digest_cached_records(s))
531 s->s3->tmp.cert_request=1;
532 ret=ssl3_send_certificate_request(s);
533 if (ret <= 0) goto end;
534 #ifndef NETSCAPE_HANG_BUG
535 s->state=SSL3_ST_SW_SRVR_DONE_A;
537 s->state=SSL3_ST_SW_FLUSH;
538 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
544 case SSL3_ST_SW_SRVR_DONE_A:
545 case SSL3_ST_SW_SRVR_DONE_B:
546 ret=ssl3_send_server_done(s);
547 if (ret <= 0) goto end;
548 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
549 s->state=SSL3_ST_SW_FLUSH;
553 case SSL3_ST_SW_FLUSH:
555 /* This code originally checked to see if
556 * any data was pending using BIO_CTRL_INFO
557 * and then flushed. This caused problems
558 * as documented in PR#1939. The proposed
559 * fix doesn't completely resolve this issue
560 * as buggy implementations of BIO_CTRL_PENDING
561 * still exist. So instead we just flush
565 s->rwstate=SSL_WRITING;
566 if (BIO_flush(s->wbio) <= 0)
571 s->rwstate=SSL_NOTHING;
573 s->state=s->s3->tmp.next_state;
576 case SSL3_ST_SR_CERT_A:
577 case SSL3_ST_SR_CERT_B:
578 if (s->s3->tmp.cert_request)
580 ret=ssl3_get_client_certificate(s);
581 if (ret <= 0) goto end;
584 s->state=SSL3_ST_SR_KEY_EXCH_A;
587 case SSL3_ST_SR_KEY_EXCH_A:
588 case SSL3_ST_SR_KEY_EXCH_B:
589 ret=ssl3_get_client_key_exchange(s);
594 /* For the ECDH ciphersuites when
595 * the client sends its ECDH pub key in
596 * a certificate, the CertificateVerify
597 * message is not sent.
598 * Also for GOST ciphersuites when
599 * the client uses its key from the certificate
602 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
603 s->state=SSL3_ST_SR_FINISHED_A;
605 if (s->s3->next_proto_neg_seen)
606 s->state=SSL3_ST_SR_NEXT_PROTO_A;
608 s->state=SSL3_ST_SR_FINISHED_A;
612 else if (SSL_USE_SIGALGS(s))
614 s->state=SSL3_ST_SR_CERT_VRFY_A;
616 if (!s->session->peer)
618 /* For sigalgs freeze the handshake buffer
619 * at this point and digest cached records.
621 if (!s->s3->handshake_buffer)
623 SSLerr(SSL_F_SSL3_ACCEPT,ERR_R_INTERNAL_ERROR);
626 s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
627 if (!ssl3_digest_cached_records(s))
635 s->state=SSL3_ST_SR_CERT_VRFY_A;
638 /* We need to get hashes here so if there is
639 * a client cert, it can be verified
640 * FIXME - digest processing for CertificateVerify
641 * should be generalized. But it is next step
643 if (s->s3->handshake_buffer)
644 if (!ssl3_digest_cached_records(s))
646 for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_num++)
647 if (s->s3->handshake_dgst[dgst_num])
651 s->method->ssl3_enc->cert_verify_mac(s,EVP_MD_CTX_type(s->s3->handshake_dgst[dgst_num]),&(s->s3->tmp.cert_verify_md[offset]));
652 dgst_size=EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
663 case SSL3_ST_SR_CERT_VRFY_A:
664 case SSL3_ST_SR_CERT_VRFY_B:
666 * This *should* be the first time we enable CCS, but be
667 * extra careful about surrounding code changes. We need
668 * to set this here because we don't know if we're
669 * expecting a CertificateVerify or not.
671 if (!s->s3->change_cipher_spec)
672 s->s3->flags |= SSL3_FLAGS_CCS_OK;
673 /* we should decide if we expected this one */
674 ret=ssl3_get_cert_verify(s);
675 if (ret <= 0) goto end;
677 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
678 s->state=SSL3_ST_SR_FINISHED_A;
680 if (s->s3->next_proto_neg_seen)
681 s->state=SSL3_ST_SR_NEXT_PROTO_A;
683 s->state=SSL3_ST_SR_FINISHED_A;
688 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
689 case SSL3_ST_SR_NEXT_PROTO_A:
690 case SSL3_ST_SR_NEXT_PROTO_B:
692 * Enable CCS for resumed handshakes with NPN.
693 * In a full handshake with NPN, we end up here through
694 * SSL3_ST_SR_CERT_VRFY_B, where SSL3_FLAGS_CCS_OK was
695 * already set. Receiving a CCS clears the flag, so make
696 * sure not to re-enable it to ban duplicates.
697 * s->s3->change_cipher_spec is set when a CCS is
698 * processed in s3_pkt.c, and remains set until
699 * the client's Finished message is read.
701 if (!s->s3->change_cipher_spec)
702 s->s3->flags |= SSL3_FLAGS_CCS_OK;
704 ret=ssl3_get_next_proto(s);
705 if (ret <= 0) goto end;
707 s->state=SSL3_ST_SR_FINISHED_A;
711 case SSL3_ST_SR_FINISHED_A:
712 case SSL3_ST_SR_FINISHED_B:
714 * Enable CCS for resumed handshakes without NPN.
715 * In a full handshake, we end up here through
716 * SSL3_ST_SR_CERT_VRFY_B, where SSL3_FLAGS_CCS_OK was
717 * already set. Receiving a CCS clears the flag, so make
718 * sure not to re-enable it to ban duplicates.
719 * s->s3->change_cipher_spec is set when a CCS is
720 * processed in s3_pkt.c, and remains set until
721 * the client's Finished message is read.
723 if (!s->s3->change_cipher_spec)
724 s->s3->flags |= SSL3_FLAGS_CCS_OK;
725 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
726 SSL3_ST_SR_FINISHED_B);
727 if (ret <= 0) goto end;
730 #ifndef OPENSSL_NO_TLSEXT
731 else if (s->tlsext_ticket_expected)
732 s->state=SSL3_ST_SW_SESSION_TICKET_A;
735 s->state=SSL3_ST_SW_CHANGE_A;
739 #ifndef OPENSSL_NO_TLSEXT
740 case SSL3_ST_SW_SESSION_TICKET_A:
741 case SSL3_ST_SW_SESSION_TICKET_B:
742 ret=ssl3_send_newsession_ticket(s);
743 if (ret <= 0) goto end;
744 s->state=SSL3_ST_SW_CHANGE_A;
748 case SSL3_ST_SW_CERT_STATUS_A:
749 case SSL3_ST_SW_CERT_STATUS_B:
750 ret=ssl3_send_cert_status(s);
751 if (ret <= 0) goto end;
752 s->state=SSL3_ST_SW_KEY_EXCH_A;
758 case SSL3_ST_SW_CHANGE_A:
759 case SSL3_ST_SW_CHANGE_B:
761 s->session->cipher=s->s3->tmp.new_cipher;
762 if (!s->method->ssl3_enc->setup_key_block(s))
763 { ret= -1; goto end; }
765 ret=ssl3_send_change_cipher_spec(s,
766 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
768 if (ret <= 0) goto end;
769 s->state=SSL3_ST_SW_FINISHED_A;
772 if (!s->method->ssl3_enc->change_cipher_state(s,
773 SSL3_CHANGE_CIPHER_SERVER_WRITE))
781 case SSL3_ST_SW_FINISHED_A:
782 case SSL3_ST_SW_FINISHED_B:
783 ret=ssl3_send_finished(s,
784 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
785 s->method->ssl3_enc->server_finished_label,
786 s->method->ssl3_enc->server_finished_label_len);
787 if (ret <= 0) goto end;
788 s->state=SSL3_ST_SW_FLUSH;
791 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
792 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
794 if (s->s3->next_proto_neg_seen)
796 s->s3->tmp.next_state=SSL3_ST_SR_NEXT_PROTO_A;
799 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
803 s->s3->tmp.next_state=SSL_ST_OK;
808 /* clean a few things up */
809 ssl3_cleanup_key_block(s);
811 BUF_MEM_free(s->init_buf);
814 /* remove buffering on output */
815 ssl_free_wbio_buffer(s);
819 if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */
824 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
826 s->ctx->stats.sess_accept_good++;
828 s->handshake_func=ssl3_accept;
830 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
838 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
844 if (!s->s3->tmp.reuse_message && !skip)
848 if ((ret=BIO_flush(s->wbio)) <= 0)
853 if ((cb != NULL) && (s->state != state))
857 cb(s,SSL_CB_ACCEPT_LOOP,1);
864 /* BIO_flush(s->wbio); */
868 cb(s,SSL_CB_ACCEPT_EXIT,ret);
872 int ssl3_send_hello_request(SSL *s)
875 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
877 ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0);
878 s->state=SSL3_ST_SW_HELLO_REQ_B;
881 /* SSL3_ST_SW_HELLO_REQ_B */
882 return ssl_do_write(s);
885 int ssl3_get_client_hello(SSL *s)
887 int i,j,ok,al=SSL_AD_INTERNAL_ERROR,ret= -1;
888 unsigned int cookie_len;
893 #ifndef OPENSSL_NO_COMP
897 STACK_OF(SSL_CIPHER) *ciphers=NULL;
899 if (s->state == SSL3_ST_SR_CLNT_HELLO_C && !s->first_packet)
902 /* We do this so that we will respond with our native type.
903 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
904 * This down switching should be handled by a different method.
905 * If we are SSLv3, we will respond with SSLv3, even if prompted with
908 if (s->state == SSL3_ST_SR_CLNT_HELLO_A
911 s->state=SSL3_ST_SR_CLNT_HELLO_B;
914 n=s->method->ssl_get_message(s,
915 SSL3_ST_SR_CLNT_HELLO_B,
916 SSL3_ST_SR_CLNT_HELLO_C,
917 SSL3_MT_CLIENT_HELLO,
918 SSL3_RT_MAX_PLAIN_LENGTH,
921 if (!ok) return((int)n);
923 d=p=(unsigned char *)s->init_msg;
925 /* use version from inside client hello, not from record header
926 * (may differ: see RFC 2246, Appendix E, second paragraph) */
927 s->client_version=(((int)p[0])<<8)|(int)p[1];
930 if (SSL_IS_DTLS(s) ? (s->client_version > s->version &&
931 s->method->version != DTLS_ANY_VERSION)
932 : (s->client_version < s->version))
934 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
935 if ((s->client_version>>8) == SSL3_VERSION_MAJOR &&
936 !s->enc_write_ctx && !s->write_hash)
938 /* similar to ssl3_get_record, send alert using remote version number */
939 s->version = s->client_version;
941 al = SSL_AD_PROTOCOL_VERSION;
945 /* If we require cookies and this ClientHello doesn't
946 * contain one, just return since we do not want to
947 * allocate any memory yet. So check cookie length...
949 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)
951 unsigned int session_length, cookie_length;
953 session_length = *(p + SSL3_RANDOM_SIZE);
954 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
956 if (cookie_length == 0)
960 /* load the client random */
961 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
964 /* get the session-id */
968 /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation.
969 * 0.9.7 and later allow this by default, but optionally ignore resumption requests
970 * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
971 * than a change to default behavior so that applications relying on this for security
972 * won't even compile against older library versions).
974 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request
975 * renegotiation but not a new session (s->new_session remains unset): for servers,
976 * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
977 * setting will be ignored.
979 if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
981 if (!ssl_get_new_session(s,1))
986 i=ssl_get_prev_session(s, p, j, d + n);
988 * Only resume if the session's version matches the negotiated
990 * RFC 5246 does not provide much useful advice on resumption
991 * with a different protocol version. It doesn't forbid it but
992 * the sanity of such behaviour would be questionable.
993 * In practice, clients do not accept a version mismatch and
994 * will abort the handshake with an error.
996 if (i == 1 && s->version == s->session->ssl_version)
997 { /* previous session */
1004 if (!ssl_get_new_session(s,1))
1014 cookie_len = *(p++);
1017 * The ClientHello may contain a cookie even if the
1018 * HelloVerify message has not been sent--make sure that it
1019 * does not cause an overflow.
1021 if ( cookie_len > sizeof(s->d1->rcvd_cookie))
1024 al = SSL_AD_DECODE_ERROR;
1025 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1029 /* verify the cookie if appropriate option is set. */
1030 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
1033 memcpy(s->d1->rcvd_cookie, p, cookie_len);
1035 if ( s->ctx->app_verify_cookie_cb != NULL)
1037 if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
1040 al=SSL_AD_HANDSHAKE_FAILURE;
1041 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1042 SSL_R_COOKIE_MISMATCH);
1045 /* else cookie verification succeeded */
1047 /* default verification */
1048 else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie,
1049 s->d1->cookie_len) != 0)
1051 al=SSL_AD_HANDSHAKE_FAILURE;
1052 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1053 SSL_R_COOKIE_MISMATCH);
1056 /* Set to -2 so if successful we return 2 */
1061 if (s->method->version == DTLS_ANY_VERSION)
1063 /* Select version to use */
1064 if (s->client_version <= DTLS1_2_VERSION &&
1065 !(s->options & SSL_OP_NO_DTLSv1_2))
1067 s->version = DTLS1_2_VERSION;
1068 s->method = DTLSv1_2_server_method();
1070 else if (tls1_suiteb(s))
1072 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1073 s->version = s->client_version;
1074 al = SSL_AD_PROTOCOL_VERSION;
1077 else if (s->client_version <= DTLS1_VERSION &&
1078 !(s->options & SSL_OP_NO_DTLSv1))
1080 s->version = DTLS1_VERSION;
1081 s->method = DTLSv1_server_method();
1085 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
1086 s->version = s->client_version;
1087 al = SSL_AD_PROTOCOL_VERSION;
1090 s->session->ssl_version = s->version;
1095 if ((i == 0) && (j != 0))
1097 /* we need a cipher if we are not resuming a session */
1098 al=SSL_AD_ILLEGAL_PARAMETER;
1099 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
1104 /* not enough data */
1105 al=SSL_AD_DECODE_ERROR;
1106 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
1109 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
1116 /* If it is a hit, check that the cipher is in the list */
1117 if ((s->hit) && (i > 0))
1120 id=s->session->cipher->id;
1123 fprintf(stderr,"client sent %d ciphers\n",sk_SSL_CIPHER_num(ciphers));
1125 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
1127 c=sk_SSL_CIPHER_value(ciphers,i);
1129 fprintf(stderr,"client [%2d of %2d]:%s\n",
1130 i,sk_SSL_CIPHER_num(ciphers),
1131 SSL_CIPHER_get_name(c));
1139 /* Disabled because it can be used in a ciphersuite downgrade
1140 * attack: CVE-2010-4180.
1143 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
1145 /* Special case as client bug workaround: the previously used cipher may
1146 * not be in the current list, the client instead might be trying to
1147 * continue using a cipher that before wasn't chosen due to server
1148 * preferences. We'll have to reject the connection if the cipher is not
1149 * enabled, though. */
1150 c = sk_SSL_CIPHER_value(ciphers, 0);
1151 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0)
1153 s->session->cipher = c;
1160 /* we need to have the cipher in the cipher
1161 * list if we are asked to reuse it */
1162 al=SSL_AD_ILLEGAL_PARAMETER;
1163 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
1172 /* not enough data */
1173 al=SSL_AD_DECODE_ERROR;
1174 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
1177 #ifndef OPENSSL_NO_COMP
1182 if (p[j] == 0) break;
1189 al=SSL_AD_DECODE_ERROR;
1190 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
1194 #ifndef OPENSSL_NO_TLSEXT
1196 if (s->version >= SSL3_VERSION)
1198 if (!ssl_parse_clienthello_tlsext(s,&p,d,n))
1200 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PARSE_TLSEXT);
1205 /* Check if we want to use external pre-shared secret for this
1206 * handshake for not reused session only. We need to generate
1207 * server_random before calling tls_session_secret_cb in order to allow
1208 * SessionTicket processing to use it in key derivation. */
1211 pos=s->s3->server_random;
1212 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0)
1218 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
1220 SSL_CIPHER *pref_cipher=NULL;
1222 s->session->master_key_length=sizeof(s->session->master_key);
1223 if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
1224 ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
1227 s->session->ciphers=ciphers;
1228 s->session->verify_result=X509_V_OK;
1232 /* check if some cipher was preferred by call back */
1233 pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1234 if (pref_cipher == NULL)
1236 al=SSL_AD_HANDSHAKE_FAILURE;
1237 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
1241 s->session->cipher=pref_cipher;
1244 sk_SSL_CIPHER_free(s->cipher_list);
1246 if (s->cipher_list_by_id)
1247 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1249 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1250 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1255 /* Worst case, we will use the NULL compression, but if we have other
1256 * options, we will now look for them. We have i-1 compression
1257 * algorithms from the client, starting at q. */
1258 s->s3->tmp.new_compression=NULL;
1259 #ifndef OPENSSL_NO_COMP
1260 /* This only happens if we have a cache hit */
1261 if (s->session->compress_meth != 0)
1263 int m, comp_id = s->session->compress_meth;
1264 /* Perform sanity checks on resumed compression algorithm */
1265 /* Can't disable compression */
1266 if (s->options & SSL_OP_NO_COMPRESSION)
1268 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
1271 /* Look for resumed compression method */
1272 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++)
1274 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
1275 if (comp_id == comp->id)
1277 s->s3->tmp.new_compression=comp;
1281 if (s->s3->tmp.new_compression == NULL)
1283 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INVALID_COMPRESSION_ALGORITHM);
1286 /* Look for resumed method in compression list */
1287 for (m = 0; m < i; m++)
1289 if (q[m] == comp_id)
1294 al=SSL_AD_ILLEGAL_PARAMETER;
1295 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1301 else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods)
1303 /* See if we have a match */
1304 int m,nn,o,v,done=0;
1306 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
1307 for (m=0; m<nn; m++)
1309 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
1322 s->s3->tmp.new_compression=comp;
1327 /* If compression is disabled we'd better not try to resume a session
1328 * using compression.
1330 if (s->session->compress_meth != 0)
1332 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
1337 /* Given s->session->ciphers and SSL_get_ciphers, we must
1342 #ifdef OPENSSL_NO_COMP
1343 s->session->compress_meth=0;
1345 s->session->compress_meth=(comp == NULL)?0:comp->id;
1347 if (s->session->ciphers != NULL)
1348 sk_SSL_CIPHER_free(s->session->ciphers);
1349 s->session->ciphers=ciphers;
1350 if (ciphers == NULL)
1352 al=SSL_AD_ILLEGAL_PARAMETER;
1353 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
1357 if (!tls1_set_server_sigalgs(s))
1359 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1362 /* Let cert callback update server certificates if required */
1364 if (s->cert->cert_cb)
1366 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1369 al=SSL_AD_INTERNAL_ERROR;
1370 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CERT_CB_ERROR);
1375 s->rwstate=SSL_X509_LOOKUP;
1378 s->rwstate = SSL_NOTHING;
1380 c=ssl3_choose_cipher(s,s->session->ciphers,
1381 SSL_get_ciphers(s));
1385 al=SSL_AD_HANDSHAKE_FAILURE;
1386 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
1389 s->s3->tmp.new_cipher=c;
1393 /* Session-id reuse */
1394 #ifdef REUSE_CIPHER_BUG
1395 STACK_OF(SSL_CIPHER) *sk;
1396 SSL_CIPHER *nc=NULL;
1397 SSL_CIPHER *ec=NULL;
1399 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
1401 sk=s->session->ciphers;
1402 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1404 c=sk_SSL_CIPHER_value(sk,i);
1405 if (c->algorithm_enc & SSL_eNULL)
1407 if (SSL_C_IS_EXPORT(c))
1411 s->s3->tmp.new_cipher=nc;
1412 else if (ec != NULL)
1413 s->s3->tmp.new_cipher=ec;
1415 s->s3->tmp.new_cipher=s->session->cipher;
1419 s->s3->tmp.new_cipher=s->session->cipher;
1422 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER))
1424 if (!ssl3_digest_cached_records(s))
1429 * we now have the following setup.
1431 * cipher_list - our prefered list of ciphers
1432 * ciphers - the clients prefered list of ciphers
1433 * compression - basically ignored right now
1434 * ssl version is set - sslv3
1435 * s->session - The ssl session has been setup.
1436 * s->hit - session reuse flag
1437 * s->tmp.new_cipher - the new cipher to use.
1440 /* Handles TLS extensions that we couldn't check earlier */
1441 if (s->version >= SSL3_VERSION)
1443 if (ssl_check_clienthello_tlsext_late(s) <= 0)
1445 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1450 if (ret < 0) ret=-ret;
1454 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1457 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
1458 return ret < 0 ? -1 : ret;
1461 int ssl3_send_server_hello(SSL *s)
1464 unsigned char *p,*d;
1469 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
1471 buf=(unsigned char *)s->init_buf->data;
1472 #ifdef OPENSSL_NO_TLSEXT
1473 p=s->s3->server_random;
1474 if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0)
1477 /* Do the message type and length last */
1478 d=p= ssl_handshake_start(s);
1480 *(p++)=s->version>>8;
1481 *(p++)=s->version&0xff;
1484 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
1485 p+=SSL3_RANDOM_SIZE;
1488 * There are several cases for the session ID to send
1489 * back in the server hello:
1490 * - For session reuse from the session cache,
1491 * we send back the old session ID.
1492 * - If stateless session reuse (using a session ticket)
1493 * is successful, we send back the client's "session ID"
1494 * (which doesn't actually identify the session).
1495 * - If it is a new session, we send back the new
1497 * - However, if we want the new session to be single-use,
1498 * we send back a 0-length session ID.
1499 * s->hit is non-zero in either case of session reuse,
1500 * so the following won't overwrite an ID that we're supposed
1503 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1505 s->session->session_id_length=0;
1507 sl=s->session->session_id_length;
1508 if (sl > (int)sizeof(s->session->session_id))
1510 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1514 memcpy(p,s->session->session_id,sl);
1517 /* put the cipher */
1518 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
1521 /* put the compression method */
1522 #ifdef OPENSSL_NO_COMP
1525 if (s->s3->tmp.new_compression == NULL)
1528 *(p++)=s->s3->tmp.new_compression->id;
1530 #ifndef OPENSSL_NO_TLSEXT
1531 if (ssl_prepare_serverhello_tlsext(s) <= 0)
1533 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
1536 if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, &al)) == NULL)
1538 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1539 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
1545 ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l);
1546 s->state=SSL3_ST_SW_SRVR_HELLO_B;
1549 /* SSL3_ST_SW_SRVR_HELLO_B */
1550 return ssl_do_write(s);
1553 int ssl3_send_server_done(SSL *s)
1556 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1558 ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0);
1559 s->state = SSL3_ST_SW_SRVR_DONE_B;
1562 /* SSL3_ST_SW_SRVR_DONE_B */
1563 return ssl_do_write(s);
1566 int ssl3_send_server_key_exchange(SSL *s)
1568 #ifndef OPENSSL_NO_RSA
1572 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1575 #ifndef OPENSSL_NO_DH
1578 #ifndef OPENSSL_NO_ECDH
1579 EC_KEY *ecdh=NULL, *ecdhp;
1580 unsigned char *encodedPoint = NULL;
1583 BN_CTX *bn_ctx = NULL;
1586 const EVP_MD *md = NULL;
1587 unsigned char *p,*d;
1597 EVP_MD_CTX_init(&md_ctx);
1598 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1600 type=s->s3->tmp.new_cipher->algorithm_mkey;
1605 r[0]=r[1]=r[2]=r[3]=NULL;
1607 #ifndef OPENSSL_NO_RSA
1608 if (type & SSL_kRSA)
1611 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1613 rsa=s->cert->rsa_tmp_cb(s,
1614 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1615 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1618 al=SSL_AD_HANDSHAKE_FAILURE;
1619 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1627 al=SSL_AD_HANDSHAKE_FAILURE;
1628 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1633 s->s3->tmp.use_rsa_tmp=1;
1637 #ifndef OPENSSL_NO_DH
1638 if (type & SSL_kEDH)
1641 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1642 dhp=s->cert->dh_tmp_cb(s,
1643 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1644 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1647 al=SSL_AD_HANDSHAKE_FAILURE;
1648 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1652 if (s->s3->tmp.dh != NULL)
1654 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1658 if ((dh=DHparams_dup(dhp)) == NULL)
1660 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1665 if ((dhp->pub_key == NULL ||
1666 dhp->priv_key == NULL ||
1667 (s->options & SSL_OP_SINGLE_DH_USE)))
1669 if(!DH_generate_key(dh))
1671 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1678 dh->pub_key=BN_dup(dhp->pub_key);
1679 dh->priv_key=BN_dup(dhp->priv_key);
1680 if ((dh->pub_key == NULL) ||
1681 (dh->priv_key == NULL))
1683 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1693 #ifndef OPENSSL_NO_ECDH
1694 if (type & SSL_kEECDH)
1696 const EC_GROUP *group;
1698 ecdhp=cert->ecdh_tmp;
1699 if (s->cert->ecdh_tmp_auto)
1701 /* Get NID of appropriate shared curve */
1702 int nid = tls1_shared_curve(s, -2);
1703 if (nid != NID_undef)
1704 ecdhp = EC_KEY_new_by_curve_name(nid);
1706 else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb)
1708 ecdhp=s->cert->ecdh_tmp_cb(s,
1709 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1710 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1714 al=SSL_AD_HANDSHAKE_FAILURE;
1715 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
1719 if (s->s3->tmp.ecdh != NULL)
1721 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1725 /* Duplicate the ECDH structure. */
1728 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1731 if (s->cert->ecdh_tmp_auto)
1733 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL)
1735 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1739 s->s3->tmp.ecdh=ecdh;
1740 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1741 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1742 (s->options & SSL_OP_SINGLE_ECDH_USE))
1744 if(!EC_KEY_generate_key(ecdh))
1746 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1751 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1752 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1753 (EC_KEY_get0_private_key(ecdh) == NULL))
1755 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1759 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1760 (EC_GROUP_get_degree(group) > 163))
1762 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1766 /* XXX: For now, we only support ephemeral ECDH
1767 * keys over named (not generic) curves. For
1768 * supported named curves, curve_id is non-zero.
1771 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1774 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1778 /* Encode the public key.
1779 * First check the size of encoding and
1780 * allocate memory accordingly.
1782 encodedlen = EC_POINT_point2oct(group,
1783 EC_KEY_get0_public_key(ecdh),
1784 POINT_CONVERSION_UNCOMPRESSED,
1787 encodedPoint = (unsigned char *)
1788 OPENSSL_malloc(encodedlen*sizeof(unsigned char));
1789 bn_ctx = BN_CTX_new();
1790 if ((encodedPoint == NULL) || (bn_ctx == NULL))
1792 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1797 encodedlen = EC_POINT_point2oct(group,
1798 EC_KEY_get0_public_key(ecdh),
1799 POINT_CONVERSION_UNCOMPRESSED,
1800 encodedPoint, encodedlen, bn_ctx);
1802 if (encodedlen == 0)
1804 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1808 BN_CTX_free(bn_ctx); bn_ctx=NULL;
1810 /* XXX: For now, we only support named (not
1811 * generic) curves in ECDH ephemeral key exchanges.
1812 * In this situation, we need four additional bytes
1813 * to encode the entire ServerECDHParams
1818 /* We'll generate the serverKeyExchange message
1819 * explicitly so we can set these to NULLs
1827 #endif /* !OPENSSL_NO_ECDH */
1828 #ifndef OPENSSL_NO_PSK
1829 if (type & SSL_kPSK)
1831 /* reserve size for record length and PSK identity hint*/
1832 n+=2+strlen(s->ctx->psk_identity_hint);
1835 #endif /* !OPENSSL_NO_PSK */
1836 #ifndef OPENSSL_NO_SRP
1837 if (type & SSL_kSRP)
1839 if ((s->srp_ctx.N == NULL) ||
1840 (s->srp_ctx.g == NULL) ||
1841 (s->srp_ctx.s == NULL) ||
1842 (s->srp_ctx.B == NULL))
1844 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_SRP_PARAM);
1855 al=SSL_AD_HANDSHAKE_FAILURE;
1856 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1859 for (i=0; i < 4 && r[i] != NULL; i++)
1861 nr[i]=BN_num_bytes(r[i]);
1862 #ifndef OPENSSL_NO_SRP
1863 if ((i == 2) && (type & SSL_kSRP))
1870 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL|SSL_aSRP))
1871 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
1873 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher,&md))
1876 al=SSL_AD_DECODE_ERROR;
1879 kn=EVP_PKEY_size(pkey);
1887 if (!BUF_MEM_grow_clean(buf,n+SSL_HM_HEADER_LENGTH(s)+kn))
1889 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1892 d = p = ssl_handshake_start(s);
1894 for (i=0; i < 4 && r[i] != NULL; i++)
1896 #ifndef OPENSSL_NO_SRP
1897 if ((i == 2) && (type & SSL_kSRP))
1909 #ifndef OPENSSL_NO_ECDH
1910 if (type & SSL_kEECDH)
1912 /* XXX: For now, we only support named (not generic) curves.
1913 * In this situation, the serverKeyExchange message has:
1914 * [1 byte CurveType], [2 byte CurveName]
1915 * [1 byte length of encoded point], followed by
1916 * the actual encoded point itself
1918 *p = NAMED_CURVE_TYPE;
1926 memcpy((unsigned char*)p,
1927 (unsigned char *)encodedPoint,
1929 OPENSSL_free(encodedPoint);
1930 encodedPoint = NULL;
1935 #ifndef OPENSSL_NO_PSK
1936 if (type & SSL_kPSK)
1938 /* copy PSK identity hint */
1939 s2n(strlen(s->ctx->psk_identity_hint), p);
1940 strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
1941 p+=strlen(s->ctx->psk_identity_hint);
1948 /* n is the length of the params, they start at &(d[4])
1949 * and p points to the space at the end. */
1950 #ifndef OPENSSL_NO_RSA
1951 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1955 for (num=2; num > 0; num--)
1957 EVP_MD_CTX_set_flags(&md_ctx,
1958 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1959 EVP_DigestInit_ex(&md_ctx,(num == 2)
1960 ?s->ctx->md5:s->ctx->sha1, NULL);
1961 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1962 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1963 EVP_DigestUpdate(&md_ctx,d,n);
1964 EVP_DigestFinal_ex(&md_ctx,q,
1965 (unsigned int *)&i);
1969 if (RSA_sign(NID_md5_sha1, md_buf, j,
1970 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1972 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1982 /* send signature algorithm */
1983 if (SSL_USE_SIGALGS(s))
1985 if (!tls12_get_sigandhash(p, pkey, md))
1987 /* Should never happen */
1988 al=SSL_AD_INTERNAL_ERROR;
1989 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1995 fprintf(stderr, "Using hash %s\n",
1998 EVP_SignInit_ex(&md_ctx, md, NULL);
1999 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
2000 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
2001 EVP_SignUpdate(&md_ctx,d,n);
2002 if (!EVP_SignFinal(&md_ctx,&(p[2]),
2003 (unsigned int *)&i,pkey))
2005 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_EVP);
2010 if (SSL_USE_SIGALGS(s))
2015 /* Is this error check actually needed? */
2016 al=SSL_AD_HANDSHAKE_FAILURE;
2017 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
2022 ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n);
2025 s->state = SSL3_ST_SW_KEY_EXCH_B;
2026 EVP_MD_CTX_cleanup(&md_ctx);
2027 return ssl_do_write(s);
2029 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2031 #ifndef OPENSSL_NO_ECDH
2032 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2033 BN_CTX_free(bn_ctx);
2035 EVP_MD_CTX_cleanup(&md_ctx);
2039 int ssl3_send_certificate_request(SSL *s)
2041 unsigned char *p,*d;
2043 STACK_OF(X509_NAME) *sk=NULL;
2047 if (s->state == SSL3_ST_SW_CERT_REQ_A)
2051 d=p=ssl_handshake_start(s);
2053 /* get the list of acceptable cert types */
2055 n=ssl3_get_req_cert_type(s,p);
2060 if (SSL_USE_SIGALGS(s))
2062 const unsigned char *psigs;
2063 nl = tls12_get_psigalgs(s, &psigs);
2065 memcpy(p, psigs, nl);
2074 sk=SSL_get_client_CA_list(s);
2078 for (i=0; i<sk_X509_NAME_num(sk); i++)
2080 name=sk_X509_NAME_value(sk,i);
2081 j=i2d_X509_NAME(name,NULL);
2082 if (!BUF_MEM_grow_clean(buf,SSL_HM_HEADER_LENGTH(s)+n+j+2))
2084 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
2087 p = ssl_handshake_start(s) + n;
2088 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2091 i2d_X509_NAME(name,&p);
2098 i2d_X509_NAME(name,&p);
2099 j-=2; s2n(j,d); j+=2;
2105 /* else no CA names */
2106 p = ssl_handshake_start(s) + off;
2109 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n);
2111 #ifdef NETSCAPE_HANG_BUG
2112 if (!SSL_IS_DTLS(s))
2114 if (!BUF_MEM_grow_clean(buf, s->init_num + 4))
2116 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
2119 p=(unsigned char *)s->init_buf->data + s->init_num;
2121 *(p++)=SSL3_MT_SERVER_DONE;
2129 s->state = SSL3_ST_SW_CERT_REQ_B;
2132 /* SSL3_ST_SW_CERT_REQ_B */
2133 return ssl_do_write(s);
2138 int ssl3_get_client_key_exchange(SSL *s)
2142 unsigned long alg_k;
2144 #ifndef OPENSSL_NO_RSA
2146 EVP_PKEY *pkey=NULL;
2148 #ifndef OPENSSL_NO_DH
2150 DH *dh_srvr, *dh_clnt = NULL;
2152 #ifndef OPENSSL_NO_KRB5
2154 #endif /* OPENSSL_NO_KRB5 */
2156 #ifndef OPENSSL_NO_ECDH
2157 EC_KEY *srvr_ecdh = NULL;
2158 EVP_PKEY *clnt_pub_pkey = NULL;
2159 EC_POINT *clnt_ecpoint = NULL;
2160 BN_CTX *bn_ctx = NULL;
2163 n=s->method->ssl_get_message(s,
2164 SSL3_ST_SR_KEY_EXCH_A,
2165 SSL3_ST_SR_KEY_EXCH_B,
2166 SSL3_MT_CLIENT_KEY_EXCHANGE,
2170 if (!ok) return((int)n);
2171 p=(unsigned char *)s->init_msg;
2173 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2175 #ifndef OPENSSL_NO_RSA
2176 if (alg_k & SSL_kRSA)
2178 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2180 unsigned char decrypt_good, version_good;
2183 /* FIX THIS UP EAY EAY EAY EAY */
2184 if (s->s3->tmp.use_rsa_tmp)
2186 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2187 rsa=s->cert->rsa_tmp;
2188 /* Don't do a callback because rsa_tmp should
2189 * be sent already */
2192 al=SSL_AD_HANDSHAKE_FAILURE;
2193 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
2200 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2201 if ( (pkey == NULL) ||
2202 (pkey->type != EVP_PKEY_RSA) ||
2203 (pkey->pkey.rsa == NULL))
2205 al=SSL_AD_HANDSHAKE_FAILURE;
2206 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
2212 /* TLS and [incidentally] DTLS{0xFEFF} */
2213 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER)
2218 if (!(s->options & SSL_OP_TLS_D5_BUG))
2220 al = SSL_AD_DECODE_ERROR;
2221 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2232 * Reject overly short RSA ciphertext because we want to be sure
2233 * that the buffer size makes it safe to iterate over the entire
2234 * size of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The
2235 * actual expected size is larger due to RSA padding, but the
2236 * bound is sufficient to be safe.
2238 if (n < SSL_MAX_MASTER_KEY_LENGTH)
2240 al = SSL_AD_DECRYPT_ERROR;
2241 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2245 /* We must not leak whether a decryption failure occurs because
2246 * of Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see
2247 * RFC 2246, section 7.4.7.1). The code follows that advice of
2248 * the TLS RFC and generates a random premaster secret for the
2249 * case that the decrypt fails. See
2250 * https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
2252 /* should be RAND_bytes, but we cannot work around a failure. */
2253 if (RAND_pseudo_bytes(rand_premaster_secret,
2254 sizeof(rand_premaster_secret)) <= 0)
2256 decrypt_len = RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
2259 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH.
2260 * decrypt_good will be 0xff if so and zero otherwise. */
2261 decrypt_good = constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2263 /* If the version in the decrypted pre-master secret is correct
2264 * then version_good will be 0xff, otherwise it'll be zero.
2265 * The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2266 * (http://eprint.iacr.org/2003/052/) exploits the version
2267 * number check as a "bad version oracle". Thus version checks
2268 * are done in constant time and are treated like any other
2269 * decryption error. */
2270 version_good = constant_time_eq_8(p[0], (unsigned)(s->client_version>>8));
2271 version_good &= constant_time_eq_8(p[1], (unsigned)(s->client_version&0xff));
2273 /* The premaster secret must contain the same version number as
2274 * the ClientHello to detect version rollback attacks
2275 * (strangely, the protocol does not offer such protection for
2276 * DH ciphersuites). However, buggy clients exist that send the
2277 * negotiated protocol version instead if the server does not
2278 * support the requested protocol version. If
2279 * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
2280 if (s->options & SSL_OP_TLS_ROLLBACK_BUG)
2282 unsigned char workaround_good;
2283 workaround_good = constant_time_eq_8(p[0], (unsigned)(s->version>>8));
2284 workaround_good &= constant_time_eq_8(p[1], (unsigned)(s->version&0xff));
2285 version_good |= workaround_good;
2288 /* Both decryption and version must be good for decrypt_good
2289 * to remain non-zero (0xff). */
2290 decrypt_good &= version_good;
2293 * Now copy rand_premaster_secret over from p using
2294 * decrypt_good_mask. If decryption failed, then p does not
2295 * contain valid plaintext, however, a check above guarantees
2296 * it is still sufficiently large to read from.
2298 for (j = 0; j < sizeof(rand_premaster_secret); j++)
2300 p[j] = constant_time_select_8(decrypt_good, p[j],
2301 rand_premaster_secret[j]);
2304 s->session->master_key_length=
2305 s->method->ssl3_enc->generate_master_secret(s,
2306 s->session->master_key,
2307 p,sizeof(rand_premaster_secret));
2308 OPENSSL_cleanse(p,sizeof(rand_premaster_secret));
2312 #ifndef OPENSSL_NO_DH
2313 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2316 EVP_PKEY *skey = NULL;
2323 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
2325 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2334 if (alg_k & SSL_kDHr)
2335 idx = SSL_PKEY_DH_RSA;
2336 else if (alg_k & SSL_kDHd)
2337 idx = SSL_PKEY_DH_DSA;
2340 skey = s->cert->pkeys[idx].privatekey;
2341 if ((skey == NULL) ||
2342 (skey->type != EVP_PKEY_DH) ||
2343 (skey->pkey.dh == NULL))
2345 al=SSL_AD_HANDSHAKE_FAILURE;
2346 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
2349 dh_srvr = skey->pkey.dh;
2351 else if (s->s3->tmp.dh == NULL)
2353 al=SSL_AD_HANDSHAKE_FAILURE;
2354 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
2358 dh_srvr=s->s3->tmp.dh;
2362 /* Get pubkey from cert */
2363 EVP_PKEY *clkey=X509_get_pubkey(s->session->peer);
2366 if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2367 dh_clnt = EVP_PKEY_get1_DH(clkey);
2369 if (dh_clnt == NULL)
2371 al=SSL_AD_HANDSHAKE_FAILURE;
2372 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
2375 EVP_PKEY_free(clkey);
2376 pub = dh_clnt->pub_key;
2379 pub=BN_bin2bn(p,i,NULL);
2382 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
2386 i=DH_compute_key(p,pub,dh_srvr);
2390 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2395 DH_free(s->s3->tmp.dh);
2402 s->session->master_key_length=
2403 s->method->ssl3_enc->generate_master_secret(s,
2404 s->session->master_key,p,i);
2405 OPENSSL_cleanse(p,i);
2411 #ifndef OPENSSL_NO_KRB5
2412 if (alg_k & SSL_kKRB5)
2414 krb5_error_code krb5rc;
2415 krb5_data enc_ticket;
2416 krb5_data authenticator;
2418 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2419 EVP_CIPHER_CTX ciph_ctx;
2420 const EVP_CIPHER *enc = NULL;
2421 unsigned char iv[EVP_MAX_IV_LENGTH];
2422 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
2423 + EVP_MAX_BLOCK_LENGTH];
2425 krb5_timestamp authtime = 0;
2426 krb5_ticket_times ttimes;
2428 EVP_CIPHER_CTX_init(&ciph_ctx);
2430 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
2433 enc_ticket.length = i;
2435 if (n < (long)(enc_ticket.length + 6))
2437 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2438 SSL_R_DATA_LENGTH_TOO_LONG);
2442 enc_ticket.data = (char *)p;
2443 p+=enc_ticket.length;
2446 authenticator.length = i;
2448 if (n < (long)(enc_ticket.length + authenticator.length + 6))
2450 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2451 SSL_R_DATA_LENGTH_TOO_LONG);
2455 authenticator.data = (char *)p;
2456 p+=authenticator.length;
2460 enc_pms.data = (char *)p;
2463 /* Note that the length is checked again below,
2466 if(enc_pms.length > sizeof pms)
2468 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2469 SSL_R_DATA_LENGTH_TOO_LONG);
2473 if (n != (long)(enc_ticket.length + authenticator.length +
2474 enc_pms.length + 6))
2476 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2477 SSL_R_DATA_LENGTH_TOO_LONG);
2481 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2485 fprintf(stderr,"kssl_sget_tkt rtn %d [%d]\n",
2486 krb5rc, kssl_err.reason);
2488 fprintf(stderr,"kssl_err text= %s\n", kssl_err.text);
2489 #endif /* KSSL_DEBUG */
2490 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2495 /* Note: no authenticator is not considered an error,
2496 ** but will return authtime == 0.
2498 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2499 &authtime, &kssl_err)) != 0)
2502 fprintf(stderr,"kssl_check_authent rtn %d [%d]\n",
2503 krb5rc, kssl_err.reason);
2505 fprintf(stderr,"kssl_err text= %s\n", kssl_err.text);
2506 #endif /* KSSL_DEBUG */
2507 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2512 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
2514 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2519 kssl_ctx_show(kssl_ctx);
2520 #endif /* KSSL_DEBUG */
2522 enc = kssl_map_enc(kssl_ctx->enctype);
2526 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2528 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
2530 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2531 SSL_R_DECRYPTION_FAILED);
2534 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
2535 (unsigned char *)enc_pms.data, enc_pms.length))
2537 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2538 SSL_R_DECRYPTION_FAILED);
2541 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
2543 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2544 SSL_R_DATA_LENGTH_TOO_LONG);
2547 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
2549 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2550 SSL_R_DECRYPTION_FAILED);
2554 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
2556 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2557 SSL_R_DATA_LENGTH_TOO_LONG);
2560 if (!((pms[0] == (s->client_version>>8)) && (pms[1] == (s->client_version & 0xff))))
2562 /* The premaster secret must contain the same version number as the
2563 * ClientHello to detect version rollback attacks (strangely, the
2564 * protocol does not offer such protection for DH ciphersuites).
2565 * However, buggy clients exist that send random bytes instead of
2566 * the protocol version.
2567 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2568 * (Perhaps we should have a separate BUG value for the Kerberos cipher)
2570 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG))
2572 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2573 SSL_AD_DECODE_ERROR);
2578 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2580 s->session->master_key_length=
2581 s->method->ssl3_enc->generate_master_secret(s,
2582 s->session->master_key, pms, outl);
2584 if (kssl_ctx->client_princ)
2586 size_t len = strlen(kssl_ctx->client_princ);
2587 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
2589 s->session->krb5_client_princ_len = len;
2590 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
2595 /*- Was doing kssl_ctx_free() here,
2596 * but it caused problems for apache.
2597 * kssl_ctx = kssl_ctx_free(kssl_ctx);
2598 * if (s->kssl_ctx) s->kssl_ctx = NULL;
2602 #endif /* OPENSSL_NO_KRB5 */
2604 #ifndef OPENSSL_NO_ECDH
2605 if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2610 const EC_GROUP *group;
2611 const BIGNUM *priv_key;
2613 /* initialize structures for server's ECDH key pair */
2614 if ((srvr_ecdh = EC_KEY_new()) == NULL)
2616 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2617 ERR_R_MALLOC_FAILURE);
2621 /* Let's get server private key and group information */
2622 if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2624 /* use the certificate */
2625 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2629 /* use the ephermeral values we saved when
2630 * generating the ServerKeyExchange msg.
2632 tkey = s->s3->tmp.ecdh;
2635 group = EC_KEY_get0_group(tkey);
2636 priv_key = EC_KEY_get0_private_key(tkey);
2638 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2639 !EC_KEY_set_private_key(srvr_ecdh, priv_key))
2641 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2646 /* Let's get client's public key */
2647 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
2649 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2650 ERR_R_MALLOC_FAILURE);
2656 /* Client Publickey was in Client Certificate */
2658 if (alg_k & SSL_kEECDH)
2660 al=SSL_AD_HANDSHAKE_FAILURE;
2661 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
2664 if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
2666 (clnt_pub_pkey->type != EVP_PKEY_EC))
2668 /* XXX: For now, we do not support client
2669 * authentication using ECDH certificates
2670 * so this branch (n == 0L) of the code is
2671 * never executed. When that support is
2672 * added, we ought to ensure the key
2673 * received in the certificate is
2674 * authorized for key agreement.
2675 * ECDH_compute_key implicitly checks that
2676 * the two ECDH shares are for the same
2679 al=SSL_AD_HANDSHAKE_FAILURE;
2680 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2681 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2685 if (EC_POINT_copy(clnt_ecpoint,
2686 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)
2688 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2692 ret = 2; /* Skip certificate verify processing */
2696 /* Get client's public key from encoded point
2697 * in the ClientKeyExchange message.
2699 if ((bn_ctx = BN_CTX_new()) == NULL)
2701 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2702 ERR_R_MALLOC_FAILURE);
2706 /* Get encoded point length */
2711 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2715 if (EC_POINT_oct2point(group,
2716 clnt_ecpoint, p, i, bn_ctx) == 0)
2718 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2722 /* p is pointing to somewhere in the buffer
2723 * currently, so set it to the start
2725 p=(unsigned char *)s->init_buf->data;
2728 /* Compute the shared pre-master secret */
2729 field_size = EC_GROUP_get_degree(group);
2730 if (field_size <= 0)
2732 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2736 i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
2739 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2744 EVP_PKEY_free(clnt_pub_pkey);
2745 EC_POINT_free(clnt_ecpoint);
2746 EC_KEY_free(srvr_ecdh);
2747 BN_CTX_free(bn_ctx);
2748 EC_KEY_free(s->s3->tmp.ecdh);
2749 s->s3->tmp.ecdh = NULL;
2751 /* Compute the master secret */
2752 s->session->master_key_length = s->method->ssl3_enc-> \
2753 generate_master_secret(s, s->session->master_key, p, i);
2755 OPENSSL_cleanse(p, i);
2760 #ifndef OPENSSL_NO_PSK
2761 if (alg_k & SSL_kPSK)
2763 unsigned char *t = NULL;
2764 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
2765 unsigned int pre_ms_len = 0, psk_len = 0;
2767 char tmp_id[PSK_MAX_IDENTITY_LEN+1];
2769 al=SSL_AD_HANDSHAKE_FAILURE;
2774 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2775 SSL_R_LENGTH_MISMATCH);
2778 if (i > PSK_MAX_IDENTITY_LEN)
2780 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2781 SSL_R_DATA_LENGTH_TOO_LONG);
2784 if (s->psk_server_callback == NULL)
2786 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2787 SSL_R_PSK_NO_SERVER_CB);
2791 /* Create guaranteed NULL-terminated identity
2792 * string for the callback */
2793 memcpy(tmp_id, p, i);
2794 memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
2795 psk_len = s->psk_server_callback(s, tmp_id,
2796 psk_or_pre_ms, sizeof(psk_or_pre_ms));
2797 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1);
2799 if (psk_len > PSK_MAX_PSK_LEN)
2801 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2802 ERR_R_INTERNAL_ERROR);
2805 else if (psk_len == 0)
2807 /* PSK related to the given identity not found */
2808 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2809 SSL_R_PSK_IDENTITY_NOT_FOUND);
2810 al=SSL_AD_UNKNOWN_PSK_IDENTITY;
2814 /* create PSK pre_master_secret */
2815 pre_ms_len=2+psk_len+2+psk_len;
2817 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
2819 memset(t, 0, psk_len);
2823 if (s->session->psk_identity != NULL)
2824 OPENSSL_free(s->session->psk_identity);
2825 s->session->psk_identity = BUF_strdup((char *)p);
2826 if (s->session->psk_identity == NULL)
2828 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2829 ERR_R_MALLOC_FAILURE);
2833 if (s->session->psk_identity_hint != NULL)
2834 OPENSSL_free(s->session->psk_identity_hint);
2835 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2836 if (s->ctx->psk_identity_hint != NULL &&
2837 s->session->psk_identity_hint == NULL)
2839 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2840 ERR_R_MALLOC_FAILURE);
2844 s->session->master_key_length=
2845 s->method->ssl3_enc->generate_master_secret(s,
2846 s->session->master_key, psk_or_pre_ms, pre_ms_len);
2849 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2855 #ifndef OPENSSL_NO_SRP
2856 if (alg_k & SSL_kSRP)
2864 al=SSL_AD_DECODE_ERROR;
2865 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_SRP_A_LENGTH);
2868 if (!(s->srp_ctx.A=BN_bin2bn(p,i,NULL)))
2870 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_BN_LIB);
2873 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2874 || BN_is_zero(s->srp_ctx.A))
2876 al=SSL_AD_ILLEGAL_PARAMETER;
2877 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_SRP_PARAMETERS);
2880 if (s->session->srp_username != NULL)
2881 OPENSSL_free(s->session->srp_username);
2882 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2883 if (s->session->srp_username == NULL)
2885 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2886 ERR_R_MALLOC_FAILURE);
2890 if ((s->session->master_key_length = SRP_generate_server_master_secret(s,s->session->master_key))<0)
2892 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
2899 #endif /* OPENSSL_NO_SRP */
2900 if (alg_k & SSL_kGOST)
2903 EVP_PKEY_CTX *pkey_ctx;
2904 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2905 unsigned char premaster_secret[32], *start;
2906 size_t outlen=32, inlen;
2907 unsigned long alg_a;
2911 /* Get our certificate private key*/
2912 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2913 if (alg_a & SSL_aGOST94)
2914 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
2915 else if (alg_a & SSL_aGOST01)
2916 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2918 pkey_ctx = EVP_PKEY_CTX_new(pk,NULL);
2919 EVP_PKEY_decrypt_init(pkey_ctx);
2920 /* If client certificate is present and is of the same type, maybe
2921 * use it for key exchange. Don't mind errors from
2922 * EVP_PKEY_derive_set_peer, because it is completely valid to use
2923 * a client certificate for authorization only. */
2924 client_pub_pkey = X509_get_pubkey(s->session->peer);
2925 if (client_pub_pkey)
2927 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2930 /* Decrypt session key */
2931 if (ASN1_get_object((const unsigned char **)&p, &Tlen, &Ttag, &Tclass, n) != V_ASN1_CONSTRUCTED ||
2932 Ttag != V_ASN1_SEQUENCE ||
2933 Tclass != V_ASN1_UNIVERSAL)
2935 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
2940 if (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,start,inlen) <=0)
2943 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
2946 /* Generate master secret */
2947 s->session->master_key_length=
2948 s->method->ssl3_enc->generate_master_secret(s,
2949 s->session->master_key,premaster_secret,32);
2950 /* Check if pubkey from client certificate was used */
2951 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2956 EVP_PKEY_free(client_pub_pkey);
2957 EVP_PKEY_CTX_free(pkey_ctx);
2965 al=SSL_AD_HANDSHAKE_FAILURE;
2966 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2967 SSL_R_UNKNOWN_CIPHER_TYPE);
2973 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2974 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP)
2977 #ifndef OPENSSL_NO_ECDH
2978 EVP_PKEY_free(clnt_pub_pkey);
2979 EC_POINT_free(clnt_ecpoint);
2980 if (srvr_ecdh != NULL)
2981 EC_KEY_free(srvr_ecdh);
2982 BN_CTX_free(bn_ctx);
2987 int ssl3_get_cert_verify(SSL *s)
2989 EVP_PKEY *pkey=NULL;
2995 const EVP_MD *md = NULL;
2997 EVP_MD_CTX_init(&mctx);
2999 n=s->method->ssl_get_message(s,
3000 SSL3_ST_SR_CERT_VRFY_A,
3001 SSL3_ST_SR_CERT_VRFY_B,
3003 SSL3_RT_MAX_PLAIN_LENGTH,
3006 if (!ok) return((int)n);
3008 if (s->session->peer != NULL)
3010 peer=s->session->peer;
3011 pkey=X509_get_pubkey(peer);
3012 type=X509_certificate_type(peer,pkey);
3020 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
3022 s->s3->tmp.reuse_message=1;
3025 al=SSL_AD_UNEXPECTED_MESSAGE;
3026 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
3035 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
3036 al=SSL_AD_UNEXPECTED_MESSAGE;
3040 if (!(type & EVP_PKT_SIGN))
3042 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
3043 al=SSL_AD_ILLEGAL_PARAMETER;
3047 if (s->s3->change_cipher_spec)
3049 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
3050 al=SSL_AD_UNEXPECTED_MESSAGE;
3054 /* we now have a signature that we need to verify */
3055 p=(unsigned char *)s->init_msg;
3056 /* Check for broken implementations of GOST ciphersuites */
3057 /* If key is GOST and n is exactly 64, it is bare
3058 * signature without length field */
3059 if (n==64 && (pkey->type==NID_id_GostR3410_94 ||
3060 pkey->type == NID_id_GostR3410_2001) )
3066 if (SSL_USE_SIGALGS(s))
3068 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
3071 al = SSL_AD_INTERNAL_ERROR;
3076 al = SSL_AD_DECODE_ERROR;
3080 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
3089 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
3090 al=SSL_AD_DECODE_ERROR;
3094 j=EVP_PKEY_size(pkey);
3095 if ((i > j) || (n > j) || (n <= 0))
3097 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
3098 al=SSL_AD_DECODE_ERROR;
3102 if (SSL_USE_SIGALGS(s))
3106 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3109 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3110 al=SSL_AD_INTERNAL_ERROR;
3114 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
3117 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
3118 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen))
3120 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
3121 al=SSL_AD_INTERNAL_ERROR;
3125 if (EVP_VerifyFinal(&mctx, p , i, pkey) <= 0)
3127 al=SSL_AD_DECRYPT_ERROR;
3128 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_SIGNATURE);
3133 #ifndef OPENSSL_NO_RSA
3134 if (pkey->type == EVP_PKEY_RSA)
3136 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
3137 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
3141 al=SSL_AD_DECRYPT_ERROR;
3142 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
3147 al=SSL_AD_DECRYPT_ERROR;
3148 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
3154 #ifndef OPENSSL_NO_DSA
3155 if (pkey->type == EVP_PKEY_DSA)
3157 j=DSA_verify(pkey->save_type,
3158 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3159 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
3163 al=SSL_AD_DECRYPT_ERROR;
3164 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
3170 #ifndef OPENSSL_NO_ECDSA
3171 if (pkey->type == EVP_PKEY_EC)
3173 j=ECDSA_verify(pkey->save_type,
3174 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3175 SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
3179 al=SSL_AD_DECRYPT_ERROR;
3180 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
3181 SSL_R_BAD_ECDSA_SIGNATURE);
3187 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
3188 { unsigned char signature[64];
3190 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey,NULL);
3191 EVP_PKEY_verify_init(pctx);
3193 fprintf(stderr,"GOST signature length is %d",i);
3195 for (idx=0;idx<64;idx++) {
3196 signature[63-idx]=p[idx];
3198 j=EVP_PKEY_verify(pctx,signature,64,s->s3->tmp.cert_verify_md,32);
3199 EVP_PKEY_CTX_free(pctx);
3202 al=SSL_AD_DECRYPT_ERROR;
3203 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
3204 SSL_R_BAD_ECDSA_SIGNATURE);
3210 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
3211 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
3220 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3223 if (s->s3->handshake_buffer)
3225 BIO_free(s->s3->handshake_buffer);
3226 s->s3->handshake_buffer = NULL;
3227 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3229 EVP_MD_CTX_cleanup(&mctx);
3230 EVP_PKEY_free(pkey);
3234 int ssl3_get_client_certificate(SSL *s)
3236 int i,ok,al,ret= -1;
3238 unsigned long l,nc,llen,n;
3239 const unsigned char *p,*q;
3241 STACK_OF(X509) *sk=NULL;
3243 n=s->method->ssl_get_message(s,
3250 if (!ok) return((int)n);
3252 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
3254 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
3255 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
3257 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3258 al=SSL_AD_HANDSHAKE_FAILURE;
3261 /* If tls asked for a client cert, the client must return a 0 list */
3262 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
3264 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
3265 al=SSL_AD_UNEXPECTED_MESSAGE;
3268 s->s3->tmp.reuse_message=1;
3272 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
3274 al=SSL_AD_UNEXPECTED_MESSAGE;
3275 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
3278 p=d=(unsigned char *)s->init_msg;
3280 if ((sk=sk_X509_new_null()) == NULL)
3282 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
3289 al=SSL_AD_DECODE_ERROR;
3290 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
3293 for (nc=0; nc<llen; )
3296 if ((l+nc+3) > llen)
3298 al=SSL_AD_DECODE_ERROR;
3299 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
3304 x=d2i_X509(NULL,&p,l);
3307 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
3312 al=SSL_AD_DECODE_ERROR;
3313 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
3316 if (!sk_X509_push(sk,x))
3318 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
3325 if (sk_X509_num(sk) <= 0)
3327 /* TLS does not mind 0 certs returned */
3328 if (s->version == SSL3_VERSION)
3330 al=SSL_AD_HANDSHAKE_FAILURE;
3331 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
3334 /* Fail for TLS only if we required a certificate */
3335 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3336 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
3338 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3339 al=SSL_AD_HANDSHAKE_FAILURE;
3342 /* No client certificate so digest cached records */
3343 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s))
3345 al=SSL_AD_INTERNAL_ERROR;
3351 i=ssl_verify_cert_chain(s,sk);
3354 al=ssl_verify_alarm_type(s->verify_result);
3355 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
3360 if (s->session->peer != NULL) /* This should not be needed */
3361 X509_free(s->session->peer);
3362 s->session->peer=sk_X509_shift(sk);
3363 s->session->verify_result = s->verify_result;
3365 /* With the current implementation, sess_cert will always be NULL
3366 * when we arrive here. */
3367 if (s->session->sess_cert == NULL)
3369 s->session->sess_cert = ssl_sess_cert_new();
3370 if (s->session->sess_cert == NULL)
3372 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3376 if (s->session->sess_cert->cert_chain != NULL)
3377 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
3378 s->session->sess_cert->cert_chain=sk;
3379 /* Inconsistency alert: cert_chain does *not* include the
3380 * peer's own certificate, while we do include it in s3_clnt.c */
3388 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3391 if (x != NULL) X509_free(x);
3392 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
3396 int ssl3_send_server_certificate(SSL *s)
3400 if (s->state == SSL3_ST_SW_CERT_A)
3402 cpk=ssl_get_server_send_pkey(s);
3405 /* VRS: allow null cert if auth == KRB5 */
3406 if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
3407 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5))
3409 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
3414 if (!ssl3_output_cert_chain(s,cpk))
3416 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
3419 s->state=SSL3_ST_SW_CERT_B;
3422 /* SSL3_ST_SW_CERT_B */
3423 return ssl_do_write(s);
3426 #ifndef OPENSSL_NO_TLSEXT
3427 /* send a new session ticket (not necessarily for a new session) */
3428 int ssl3_send_newsession_ticket(SSL *s)
3430 if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
3432 unsigned char *p, *senc, *macstart;
3433 const unsigned char *const_p;
3434 int len, slen_full, slen;
3439 SSL_CTX *tctx = s->initial_ctx;
3440 unsigned char iv[EVP_MAX_IV_LENGTH];
3441 unsigned char key_name[16];
3443 /* get session encoding length */
3444 slen_full = i2d_SSL_SESSION(s->session, NULL);
3445 /* Some length values are 16 bits, so forget it if session is
3448 if (slen_full > 0xFF00)
3450 senc = OPENSSL_malloc(slen_full);
3454 i2d_SSL_SESSION(s->session, &p);
3456 /* create a fresh copy (not shared with other threads) to clean up */
3458 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3464 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3466 slen = i2d_SSL_SESSION(sess, NULL);
3467 if (slen > slen_full) /* shouldn't ever happen */
3473 i2d_SSL_SESSION(sess, &p);
3474 SSL_SESSION_free(sess);
3477 * Grow buffer if need be: the length calculation is as
3478 * follows handshake_header_length +
3479 * 4 (ticket lifetime hint) + 2 (ticket length) +
3480 * 16 (key name) + max_iv_len (iv length) +
3481 * session_length + max_enc_block_size (max encrypted session
3482 * length) + max_md_size (HMAC).
3484 if (!BUF_MEM_grow(s->init_buf,
3485 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3486 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
3488 p = ssl_handshake_start(s);
3489 EVP_CIPHER_CTX_init(&ctx);
3490 HMAC_CTX_init(&hctx);
3491 /* Initialize HMAC and cipher contexts. If callback present
3492 * it does all the work otherwise use generated values
3495 if (tctx->tlsext_ticket_key_cb)
3497 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3506 RAND_pseudo_bytes(iv, 16);
3507 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3508 tctx->tlsext_tick_aes_key, iv);
3509 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3510 tlsext_tick_md(), NULL);
3511 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3514 /* Ticket lifetime hint (advisory only):
3515 * We leave this unspecified for resumed session (for simplicity),
3516 * and guess that tickets for new sessions will live as long
3517 * as their sessions. */
3518 l2n(s->hit ? 0 : s->session->timeout, p);
3520 /* Skip ticket length for now */
3522 /* Output key name */
3524 memcpy(p, key_name, 16);
3527 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3528 p += EVP_CIPHER_CTX_iv_length(&ctx);
3529 /* Encrypt session data */
3530 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
3532 EVP_EncryptFinal(&ctx, p, &len);
3534 EVP_CIPHER_CTX_cleanup(&ctx);
3536 HMAC_Update(&hctx, macstart, p - macstart);
3537 HMAC_Final(&hctx, p, &hlen);
3538 HMAC_CTX_cleanup(&hctx);
3541 /* Now write out lengths: p points to end of data written */
3543 len = p - ssl_handshake_start(s);
3544 ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
3545 /* Skip ticket lifetime hint */
3546 p = ssl_handshake_start(s) + 4;
3548 s->state=SSL3_ST_SW_SESSION_TICKET_B;
3552 /* SSL3_ST_SW_SESSION_TICKET_B */
3553 return ssl_do_write(s);
3556 int ssl3_send_cert_status(SSL *s)
3558 if (s->state == SSL3_ST_SW_CERT_STATUS_A)
3562 * Grow buffer if need be: the length calculation is as
3563 * follows 1 (message type) + 3 (message length) +
3564 * 1 (ocsp response type) + 3 (ocsp response length)
3567 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
3570 p=(unsigned char *)s->init_buf->data;
3573 *(p++)=SSL3_MT_CERTIFICATE_STATUS;
3574 /* message length */
3575 l2n3(s->tlsext_ocsp_resplen + 4, p);
3577 *(p++)= s->tlsext_status_type;
3578 /* length of OCSP response */
3579 l2n3(s->tlsext_ocsp_resplen, p);
3580 /* actual response */
3581 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3582 /* number of bytes to write */
3583 s->init_num = 8 + s->tlsext_ocsp_resplen;
3584 s->state=SSL3_ST_SW_CERT_STATUS_B;
3588 /* SSL3_ST_SW_CERT_STATUS_B */
3589 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
3592 # ifndef OPENSSL_NO_NEXTPROTONEG
3593 /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
3594 * sets the next_proto member in s if found */
3595 int ssl3_get_next_proto(SSL *s)
3598 int proto_len, padding_len;
3600 const unsigned char *p;
3602 /* Clients cannot send a NextProtocol message if we didn't see the
3603 * extension in their ClientHello */
3604 if (!s->s3->next_proto_neg_seen)
3606 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
3610 /* See the payload format below */
3611 n=s->method->ssl_get_message(s,
3612 SSL3_ST_SR_NEXT_PROTO_A,
3613 SSL3_ST_SR_NEXT_PROTO_B,
3621 /* s->state doesn't reflect whether ChangeCipherSpec has been received
3622 * in this handshake, but s->s3->change_cipher_spec does (will be reset
3623 * by ssl3_get_finished). */
3624 if (!s->s3->change_cipher_spec)
3626 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
3631 return 0; /* The body must be > 1 bytes long */
3633 p=(unsigned char *)s->init_msg;
3636 * The payload looks like:
3638 * uint8 proto[proto_len];
3639 * uint8 padding_len;
3640 * uint8 padding[padding_len];
3643 if (proto_len + 2 > s->init_num)
3645 padding_len = p[proto_len + 1];
3646 if (proto_len + padding_len + 2 != s->init_num)
3649 s->next_proto_negotiated = OPENSSL_malloc(proto_len);
3650 if (!s->next_proto_negotiated)
3652 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,ERR_R_MALLOC_FAILURE);
3655 memcpy(s->next_proto_negotiated, p + 1, proto_len);
3656 s->next_proto_negotiated_len = proto_len;