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 <openssl/buffer.h>
158 #include <openssl/rand.h>
159 #include <openssl/objects.h>
160 #include <openssl/evp.h>
161 #include <openssl/hmac.h>
162 #include <openssl/x509.h>
163 #ifndef OPENSSL_NO_DH
164 #include <openssl/dh.h>
166 #include <openssl/bn.h>
167 #ifndef OPENSSL_NO_KRB5
168 #include <openssl/krb5_asn.h>
170 #include <openssl/md5.h>
172 static const SSL_METHOD *ssl3_get_server_method(int ver);
174 static const SSL_METHOD *ssl3_get_server_method(int ver)
176 if (ver == SSL3_VERSION)
177 return(SSLv3_server_method());
182 #ifndef OPENSSL_NO_SRP
183 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
185 int ret = SSL_ERROR_NONE;
187 *al = SSL_AD_UNRECOGNIZED_NAME;
189 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
190 (s->srp_ctx.TLS_ext_srp_username_callback != NULL))
192 if(s->srp_ctx.login == NULL)
194 /* RFC 5054 says SHOULD reject,
195 we do so if There is no srp login name */
197 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
201 ret = SSL_srp_server_param_with_username(s,al);
208 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
210 ssl_undefined_function,
211 ssl3_get_server_method)
213 int ssl3_accept(SSL *s)
216 unsigned long alg_k,Time=(unsigned long)time(NULL);
217 void (*cb)(const SSL *ssl,int type,int val)=NULL;
219 int new_state,state,skip=0;
221 RAND_add(&Time,sizeof(Time),0);
225 if (s->info_callback != NULL)
227 else if (s->ctx->info_callback != NULL)
228 cb=s->ctx->info_callback;
230 /* init things to blank */
232 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
236 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
240 #ifndef OPENSSL_NO_HEARTBEATS
241 /* If we're awaiting a HeartbeatResponse, pretend we
242 * already got and don't await it anymore, because
243 * Heartbeats don't make sense during handshakes anyway.
245 if (s->tlsext_hb_pending)
247 s->tlsext_hb_pending = 0;
258 case SSL_ST_RENEGOTIATE:
260 /* s->state=SSL_ST_ACCEPT; */
264 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
265 case SSL_ST_OK|SSL_ST_ACCEPT:
268 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
270 if ((s->version>>8) != 3)
272 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
276 if (!ssl_security(s, SSL_SECOP_VERSION, 0,
279 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_VERSION_TOO_LOW);
283 s->type=SSL_ST_ACCEPT;
285 if (s->init_buf == NULL)
287 if ((buf=BUF_MEM_new()) == NULL)
292 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
300 if (!ssl3_setup_buffers(s))
307 s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE;
308 s->s3->flags &= ~TLS1_FLAGS_SKIP_CERT_VERIFY;
310 if (s->state != SSL_ST_RENEGOTIATE)
312 /* Ok, we now need to push on a buffering BIO so that
313 * the output is sent in a way that TCP likes :-)
315 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
317 ssl3_init_finished_mac(s);
318 s->state=SSL3_ST_SR_CLNT_HELLO_A;
319 s->ctx->stats.sess_accept++;
321 else if (!s->s3->send_connection_binding &&
322 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
324 /* Server attempting to renegotiate with
325 * client that doesn't support secure
328 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
329 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
335 /* s->state == SSL_ST_RENEGOTIATE,
336 * we will just send a HelloRequest */
337 s->ctx->stats.sess_accept_renegotiate++;
338 s->state=SSL3_ST_SW_HELLO_REQ_A;
342 case SSL3_ST_SW_HELLO_REQ_A:
343 case SSL3_ST_SW_HELLO_REQ_B:
346 ret=ssl3_send_hello_request(s);
347 if (ret <= 0) goto end;
348 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
349 s->state=SSL3_ST_SW_FLUSH;
352 ssl3_init_finished_mac(s);
355 case SSL3_ST_SW_HELLO_REQ_C:
359 case SSL3_ST_SR_CLNT_HELLO_A:
360 case SSL3_ST_SR_CLNT_HELLO_B:
361 case SSL3_ST_SR_CLNT_HELLO_C:
363 ret=ssl3_get_client_hello(s);
364 if (ret <= 0) goto end;
365 #ifndef OPENSSL_NO_SRP
366 s->state = SSL3_ST_SR_CLNT_HELLO_D;
367 case SSL3_ST_SR_CLNT_HELLO_D:
370 if ((ret = ssl_check_srp_ext_ClientHello(s,&al)) < 0)
372 /* callback indicates firther work to be done */
373 s->rwstate=SSL_X509_LOOKUP;
376 if (ret != SSL_ERROR_NONE)
378 ssl3_send_alert(s,SSL3_AL_FATAL,al);
379 /* This is not really an error but the only means to
380 for a client to detect whether srp is supported. */
381 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
382 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_CLIENTHELLO_TLSEXT);
383 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
391 s->state=SSL3_ST_SW_SRVR_HELLO_A;
395 case SSL3_ST_SW_SRVR_HELLO_A:
396 case SSL3_ST_SW_SRVR_HELLO_B:
397 ret=ssl3_send_server_hello(s);
398 if (ret <= 0) goto end;
399 #ifndef OPENSSL_NO_TLSEXT
402 if (s->tlsext_ticket_expected)
403 s->state=SSL3_ST_SW_SESSION_TICKET_A;
405 s->state=SSL3_ST_SW_CHANGE_A;
409 s->state=SSL3_ST_SW_CHANGE_A;
412 #ifndef OPENSSL_NO_TLSEXT
413 s->state = SSL3_ST_SW_SUPPLEMENTAL_DATA_A;
415 s->state = SSL3_ST_SW_CERT_A;
420 #ifndef OPENSSL_NO_TLSEXT
421 case SSL3_ST_SW_SUPPLEMENTAL_DATA_A:
422 case SSL3_ST_SW_SUPPLEMENTAL_DATA_B:
423 ret = tls1_send_server_supplemental_data(s, &skip);
424 if (ret <= 0) goto end;
426 s->state = SSL3_ST_SW_CERT_A;
431 case SSL3_ST_SW_CERT_A:
432 case SSL3_ST_SW_CERT_B:
433 /* Check if it is anon DH or anon ECDH, */
434 /* normal PSK or KRB5 or SRP */
435 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
436 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
437 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
439 ret=ssl3_send_server_certificate(s);
440 if (ret <= 0) goto end;
441 #ifndef OPENSSL_NO_TLSEXT
442 if (s->tlsext_status_expected)
443 s->state=SSL3_ST_SW_CERT_STATUS_A;
445 s->state=SSL3_ST_SW_KEY_EXCH_A;
450 s->state=SSL3_ST_SW_KEY_EXCH_A;
457 s->state=SSL3_ST_SW_KEY_EXCH_A;
462 case SSL3_ST_SW_KEY_EXCH_A:
463 case SSL3_ST_SW_KEY_EXCH_B:
464 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
466 /* clear this, it may get reset by
467 * send_server_key_exchange */
468 if ((s->options & SSL_OP_EPHEMERAL_RSA)
469 #ifndef OPENSSL_NO_KRB5
470 && !(alg_k & SSL_kKRB5)
471 #endif /* OPENSSL_NO_KRB5 */
473 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
474 * even when forbidden by protocol specs
475 * (handshake may fail as clients are not required to
476 * be able to handle this) */
477 s->s3->tmp.use_rsa_tmp=1;
479 s->s3->tmp.use_rsa_tmp=0;
482 /* only send if a DH key exchange, fortezza or
483 * RSA but we have a sign only certificate
485 * PSK: may send PSK identity hints
487 * For ECC ciphersuites, we send a serverKeyExchange
488 * message only if the cipher suite is either
489 * ECDH-anon or ECDHE. In other cases, the
490 * server certificate contains the server's
491 * public key for key exchange.
493 if (s->s3->tmp.use_rsa_tmp
494 /* PSK: send ServerKeyExchange if PSK identity
495 * hint if provided */
496 #ifndef OPENSSL_NO_PSK
497 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
499 #ifndef OPENSSL_NO_SRP
500 /* SRP: send ServerKeyExchange */
501 || (alg_k & SSL_kSRP)
503 || (alg_k & SSL_kDHE)
504 || (alg_k & SSL_kECDHE)
505 || ((alg_k & SSL_kRSA)
506 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
507 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
508 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
514 ret=ssl3_send_server_key_exchange(s);
515 if (ret <= 0) goto end;
520 s->state=SSL3_ST_SW_CERT_REQ_A;
524 case SSL3_ST_SW_CERT_REQ_A:
525 case SSL3_ST_SW_CERT_REQ_B:
526 if (/* don't request cert unless asked for it: */
527 !(s->verify_mode & SSL_VERIFY_PEER) ||
528 /* if SSL_VERIFY_CLIENT_ONCE is set,
529 * don't request cert during re-negotiation: */
530 ((s->session->peer != NULL) &&
531 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
532 /* never request cert in anonymous ciphersuites
533 * (see section "Certificate request" in SSL 3 drafts
534 * and in RFC 2246): */
535 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
536 /* ... except when the application insists on verification
537 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
538 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
539 /* never request cert in Kerberos ciphersuites */
540 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
541 /* With normal PSK Certificates and
542 * Certificate Requests are omitted */
543 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
545 /* no cert request */
547 s->s3->tmp.cert_request=0;
548 s->state=SSL3_ST_SW_SRVR_DONE_A;
549 if (s->s3->handshake_buffer)
550 if (!ssl3_digest_cached_records(s))
555 s->s3->tmp.cert_request=1;
556 ret=ssl3_send_certificate_request(s);
557 if (ret <= 0) goto end;
558 #ifndef NETSCAPE_HANG_BUG
559 s->state=SSL3_ST_SW_SRVR_DONE_A;
561 s->state=SSL3_ST_SW_FLUSH;
562 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
568 case SSL3_ST_SW_SRVR_DONE_A:
569 case SSL3_ST_SW_SRVR_DONE_B:
570 ret=ssl3_send_server_done(s);
571 if (ret <= 0) goto end;
572 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
573 s->state=SSL3_ST_SW_FLUSH;
577 case SSL3_ST_SW_FLUSH:
579 /* This code originally checked to see if
580 * any data was pending using BIO_CTRL_INFO
581 * and then flushed. This caused problems
582 * as documented in PR#1939. The proposed
583 * fix doesn't completely resolve this issue
584 * as buggy implementations of BIO_CTRL_PENDING
585 * still exist. So instead we just flush
589 s->rwstate=SSL_WRITING;
590 if (BIO_flush(s->wbio) <= 0)
595 s->rwstate=SSL_NOTHING;
597 s->state=s->s3->tmp.next_state;
599 #ifndef OPENSSL_NO_TLSEXT
600 case SSL3_ST_SR_SUPPLEMENTAL_DATA_A:
601 case SSL3_ST_SR_SUPPLEMENTAL_DATA_B:
602 ret=tls1_get_client_supplemental_data(s);
603 if (ret <= 0) goto end;
604 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
605 s->state=SSL3_ST_SW_FLUSH;
609 case SSL3_ST_SR_CERT_A:
610 case SSL3_ST_SR_CERT_B:
611 /* Check for second client hello (MS SGC) */
612 ret = ssl3_check_client_hello(s);
616 s->state = SSL3_ST_SR_CLNT_HELLO_C;
617 #ifndef OPENSSL_NO_TLSEXT
619 s->state = SSL3_ST_SR_SUPPLEMENTAL_DATA_A;
622 if (s->s3->tmp.cert_request)
624 ret=ssl3_get_client_certificate(s);
625 if (ret <= 0) goto end;
628 s->state=SSL3_ST_SR_KEY_EXCH_A;
632 case SSL3_ST_SR_KEY_EXCH_A:
633 case SSL3_ST_SR_KEY_EXCH_B:
634 ret=ssl3_get_client_key_exchange(s);
639 /* For the ECDH ciphersuites when
640 * the client sends its ECDH pub key in
641 * a certificate, the CertificateVerify
642 * message is not sent.
643 * Also for GOST ciphersuites when
644 * the client uses its key from the certificate
647 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
648 s->state=SSL3_ST_SR_FINISHED_A;
650 if (s->s3->next_proto_neg_seen)
651 s->state=SSL3_ST_SR_NEXT_PROTO_A;
653 s->state=SSL3_ST_SR_FINISHED_A;
657 else if (SSL_USE_SIGALGS(s))
659 s->state=SSL3_ST_SR_CERT_VRFY_A;
661 if (!s->session->peer)
663 /* For sigalgs freeze the handshake buffer
664 * at this point and digest cached records.
666 if (!s->s3->handshake_buffer)
668 SSLerr(SSL_F_SSL3_ACCEPT,ERR_R_INTERNAL_ERROR);
671 s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
672 if (!ssl3_digest_cached_records(s))
680 s->state=SSL3_ST_SR_CERT_VRFY_A;
683 /* We need to get hashes here so if there is
684 * a client cert, it can be verified
685 * FIXME - digest processing for CertificateVerify
686 * should be generalized. But it is next step
688 if (s->s3->handshake_buffer)
689 if (!ssl3_digest_cached_records(s))
691 for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_num++)
692 if (s->s3->handshake_dgst[dgst_num])
696 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]));
697 dgst_size=EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
708 case SSL3_ST_SR_CERT_VRFY_A:
709 case SSL3_ST_SR_CERT_VRFY_B:
711 /* we should decide if we expected this one */
712 ret=ssl3_get_cert_verify(s);
713 if (ret <= 0) goto end;
715 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
716 s->state=SSL3_ST_SR_FINISHED_A;
718 if (s->s3->next_proto_neg_seen)
719 s->state=SSL3_ST_SR_NEXT_PROTO_A;
721 s->state=SSL3_ST_SR_FINISHED_A;
726 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
727 case SSL3_ST_SR_NEXT_PROTO_A:
728 case SSL3_ST_SR_NEXT_PROTO_B:
729 ret=ssl3_get_next_proto(s);
730 if (ret <= 0) goto end;
732 s->state=SSL3_ST_SR_FINISHED_A;
736 case SSL3_ST_SR_FINISHED_A:
737 case SSL3_ST_SR_FINISHED_B:
738 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
739 SSL3_ST_SR_FINISHED_B);
740 if (ret <= 0) goto end;
743 #ifndef OPENSSL_NO_TLSEXT
744 else if (s->tlsext_ticket_expected)
745 s->state=SSL3_ST_SW_SESSION_TICKET_A;
748 s->state=SSL3_ST_SW_CHANGE_A;
752 #ifndef OPENSSL_NO_TLSEXT
753 case SSL3_ST_SW_SESSION_TICKET_A:
754 case SSL3_ST_SW_SESSION_TICKET_B:
755 ret=ssl3_send_newsession_ticket(s);
756 if (ret <= 0) goto end;
757 s->state=SSL3_ST_SW_CHANGE_A;
761 case SSL3_ST_SW_CERT_STATUS_A:
762 case SSL3_ST_SW_CERT_STATUS_B:
763 ret=ssl3_send_cert_status(s);
764 if (ret <= 0) goto end;
765 s->state=SSL3_ST_SW_KEY_EXCH_A;
771 case SSL3_ST_SW_CHANGE_A:
772 case SSL3_ST_SW_CHANGE_B:
774 s->session->cipher=s->s3->tmp.new_cipher;
775 if (!s->method->ssl3_enc->setup_key_block(s))
776 { ret= -1; goto end; }
778 ret=ssl3_send_change_cipher_spec(s,
779 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
781 if (ret <= 0) goto end;
782 s->state=SSL3_ST_SW_FINISHED_A;
785 if (!s->method->ssl3_enc->change_cipher_state(s,
786 SSL3_CHANGE_CIPHER_SERVER_WRITE))
794 case SSL3_ST_SW_FINISHED_A:
795 case SSL3_ST_SW_FINISHED_B:
796 ret=ssl3_send_finished(s,
797 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
798 s->method->ssl3_enc->server_finished_label,
799 s->method->ssl3_enc->server_finished_label_len);
800 if (ret <= 0) goto end;
801 s->state=SSL3_ST_SW_FLUSH;
804 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
805 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
807 if (s->s3->next_proto_neg_seen)
808 s->s3->tmp.next_state=SSL3_ST_SR_NEXT_PROTO_A;
810 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
814 s->s3->tmp.next_state=SSL_ST_OK;
819 /* clean a few things up */
820 ssl3_cleanup_key_block(s);
822 BUF_MEM_free(s->init_buf);
825 /* remove buffering on output */
826 ssl_free_wbio_buffer(s);
830 if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */
835 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
837 s->ctx->stats.sess_accept_good++;
839 s->handshake_func=ssl3_accept;
841 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
849 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
855 if (!s->s3->tmp.reuse_message && !skip)
859 if ((ret=BIO_flush(s->wbio)) <= 0)
864 if ((cb != NULL) && (s->state != state))
868 cb(s,SSL_CB_ACCEPT_LOOP,1);
875 /* BIO_flush(s->wbio); */
879 cb(s,SSL_CB_ACCEPT_EXIT,ret);
883 int ssl3_send_hello_request(SSL *s)
886 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
888 ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0);
889 s->state=SSL3_ST_SW_HELLO_REQ_B;
892 /* SSL3_ST_SW_HELLO_REQ_B */
893 return ssl_do_write(s);
896 int ssl3_check_client_hello(SSL *s)
901 /* this function is called when we really expect a Certificate message,
902 * so permit appropriate message length */
903 n=s->method->ssl_get_message(s,
909 if (!ok) return((int)n);
910 s->s3->tmp.reuse_message = 1;
911 #ifndef OPENSSL_NO_TLSEXT
912 if (s->s3->tmp.message_type == SSL3_MT_SUPPLEMENTAL_DATA)
915 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
917 /* We only allow the client to restart the handshake once per
919 if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE)
921 SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO, SSL_R_MULTIPLE_SGC_RESTARTS);
924 /* Throw away what we have done so far in the current handshake,
925 * which will now be aborted. (A full SSL_clear would be too much.) */
926 #ifndef OPENSSL_NO_DH
927 if (s->s3->tmp.dh != NULL)
929 DH_free(s->s3->tmp.dh);
930 s->s3->tmp.dh = NULL;
933 #ifndef OPENSSL_NO_ECDH
934 if (s->s3->tmp.ecdh != NULL)
936 EC_KEY_free(s->s3->tmp.ecdh);
937 s->s3->tmp.ecdh = NULL;
940 s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE;
946 int ssl3_get_client_hello(SSL *s)
948 int i,j,ok,al=SSL_AD_INTERNAL_ERROR,ret= -1;
949 unsigned int cookie_len;
954 #ifndef OPENSSL_NO_COMP
958 STACK_OF(SSL_CIPHER) *ciphers=NULL;
960 if (s->state == SSL3_ST_SR_CLNT_HELLO_C && !s->first_packet)
963 /* We do this so that we will respond with our native type.
964 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
965 * This down switching should be handled by a different method.
966 * If we are SSLv3, we will respond with SSLv3, even if prompted with
969 if (s->state == SSL3_ST_SR_CLNT_HELLO_A
972 s->state=SSL3_ST_SR_CLNT_HELLO_B;
975 n=s->method->ssl_get_message(s,
976 SSL3_ST_SR_CLNT_HELLO_B,
977 SSL3_ST_SR_CLNT_HELLO_C,
978 SSL3_MT_CLIENT_HELLO,
979 SSL3_RT_MAX_PLAIN_LENGTH,
982 if (!ok) return((int)n);
984 d=p=(unsigned char *)s->init_msg;
986 /* use version from inside client hello, not from record header
987 * (may differ: see RFC 2246, Appendix E, second paragraph) */
988 s->client_version=(((int)p[0])<<8)|(int)p[1];
991 if (SSL_IS_DTLS(s) ? (s->client_version > s->version &&
992 s->method->version != DTLS_ANY_VERSION)
993 : (s->client_version < s->version))
995 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
996 if ((s->client_version>>8) == SSL3_VERSION_MAJOR &&
997 !s->enc_write_ctx && !s->write_hash)
999 /* similar to ssl3_get_record, send alert using remote version number */
1000 s->version = s->client_version;
1002 al = SSL_AD_PROTOCOL_VERSION;
1006 /* If we require cookies and this ClientHello doesn't
1007 * contain one, just return since we do not want to
1008 * allocate any memory yet. So check cookie length...
1010 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)
1012 unsigned int session_length, cookie_length;
1014 session_length = *(p + SSL3_RANDOM_SIZE);
1015 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
1017 if (cookie_length == 0)
1021 /* load the client random */
1022 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
1023 p+=SSL3_RANDOM_SIZE;
1025 /* get the session-id */
1029 /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation.
1030 * 0.9.7 and later allow this by default, but optionally ignore resumption requests
1031 * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1032 * than a change to default behavior so that applications relying on this for security
1033 * won't even compile against older library versions).
1035 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request
1036 * renegotiation but not a new session (s->new_session remains unset): for servers,
1037 * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1038 * setting will be ignored.
1040 if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
1042 if (!ssl_get_new_session(s,1))
1047 i=ssl_get_prev_session(s, p, j, d + n);
1049 { /* previous session */
1056 if (!ssl_get_new_session(s,1))
1066 cookie_len = *(p++);
1069 * The ClientHello may contain a cookie even if the
1070 * HelloVerify message has not been sent--make sure that it
1071 * does not cause an overflow.
1073 if ( cookie_len > sizeof(s->d1->rcvd_cookie))
1076 al = SSL_AD_DECODE_ERROR;
1077 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1081 /* verify the cookie if appropriate option is set. */
1082 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
1085 memcpy(s->d1->rcvd_cookie, p, cookie_len);
1087 if ( s->ctx->app_verify_cookie_cb != NULL)
1089 if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
1092 al=SSL_AD_HANDSHAKE_FAILURE;
1093 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1094 SSL_R_COOKIE_MISMATCH);
1097 /* else cookie verification succeeded */
1099 else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie,
1100 s->d1->cookie_len) != 0) /* default verification */
1102 al=SSL_AD_HANDSHAKE_FAILURE;
1103 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1104 SSL_R_COOKIE_MISMATCH);
1107 /* Set to -2 so if successful we return 2 */
1112 if (s->method->version == DTLS_ANY_VERSION)
1114 /* Select version to use */
1115 if (s->client_version <= DTLS1_2_VERSION &&
1116 !(s->options & SSL_OP_NO_DTLSv1_2))
1118 s->version = DTLS1_2_VERSION;
1119 s->method = DTLSv1_2_server_method();
1121 else if (tls1_suiteb(s))
1123 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1124 s->version = s->client_version;
1125 al = SSL_AD_PROTOCOL_VERSION;
1128 else if (s->client_version <= DTLS1_VERSION &&
1129 !(s->options & SSL_OP_NO_DTLSv1))
1131 s->version = DTLS1_VERSION;
1132 s->method = DTLSv1_server_method();
1136 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
1137 s->version = s->client_version;
1138 al = SSL_AD_PROTOCOL_VERSION;
1141 s->session->ssl_version = s->version;
1146 if ((i == 0) && (j != 0))
1148 /* we need a cipher if we are not resuming a session */
1149 al=SSL_AD_ILLEGAL_PARAMETER;
1150 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
1155 /* not enough data */
1156 al=SSL_AD_DECODE_ERROR;
1157 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
1160 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
1167 /* If it is a hit, check that the cipher is in the list */
1168 if ((s->hit) && (i > 0))
1171 id=s->session->cipher->id;
1174 printf("client sent %d ciphers\n",sk_num(ciphers));
1176 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
1178 c=sk_SSL_CIPHER_value(ciphers,i);
1180 printf("client [%2d of %2d]:%s\n",
1181 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
1189 /* Disabled because it can be used in a ciphersuite downgrade
1190 * attack: CVE-2010-4180.
1193 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
1195 /* Special case as client bug workaround: the previously used cipher may
1196 * not be in the current list, the client instead might be trying to
1197 * continue using a cipher that before wasn't chosen due to server
1198 * preferences. We'll have to reject the connection if the cipher is not
1199 * enabled, though. */
1200 c = sk_SSL_CIPHER_value(ciphers, 0);
1201 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0)
1203 s->session->cipher = c;
1210 /* we need to have the cipher in the cipher
1211 * list if we are asked to reuse it */
1212 al=SSL_AD_ILLEGAL_PARAMETER;
1213 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
1222 /* not enough data */
1223 al=SSL_AD_DECODE_ERROR;
1224 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
1227 #ifndef OPENSSL_NO_COMP
1232 if (p[j] == 0) break;
1239 al=SSL_AD_DECODE_ERROR;
1240 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
1244 #ifndef OPENSSL_NO_TLSEXT
1246 if (s->version >= SSL3_VERSION)
1248 if (!ssl_parse_clienthello_tlsext(s,&p,d,n))
1250 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PARSE_TLSEXT);
1255 /* Check if we want to use external pre-shared secret for this
1256 * handshake for not reused session only. We need to generate
1257 * server_random before calling tls_session_secret_cb in order to allow
1258 * SessionTicket processing to use it in key derivation. */
1261 pos=s->s3->server_random;
1262 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0)
1268 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
1270 SSL_CIPHER *pref_cipher=NULL;
1272 s->session->master_key_length=sizeof(s->session->master_key);
1273 if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
1274 ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
1277 s->session->ciphers=ciphers;
1278 s->session->verify_result=X509_V_OK;
1282 /* check if some cipher was preferred by call back */
1283 pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1284 if (pref_cipher == NULL)
1286 al=SSL_AD_HANDSHAKE_FAILURE;
1287 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
1291 s->session->cipher=pref_cipher;
1294 sk_SSL_CIPHER_free(s->cipher_list);
1296 if (s->cipher_list_by_id)
1297 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1299 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1300 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1305 /* Worst case, we will use the NULL compression, but if we have other
1306 * options, we will now look for them. We have i-1 compression
1307 * algorithms from the client, starting at q. */
1308 s->s3->tmp.new_compression=NULL;
1309 #ifndef OPENSSL_NO_COMP
1310 /* This only happens if we have a cache hit */
1311 if (s->session->compress_meth != 0)
1313 int m, comp_id = s->session->compress_meth;
1314 /* Perform sanity checks on resumed compression algorithm */
1315 /* Can't disable compression */
1316 if (!ssl_allow_compression(s))
1318 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
1321 /* Look for resumed compression method */
1322 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++)
1324 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
1325 if (comp_id == comp->id)
1327 s->s3->tmp.new_compression=comp;
1331 if (s->s3->tmp.new_compression == NULL)
1333 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INVALID_COMPRESSION_ALGORITHM);
1336 /* Look for resumed method in compression list */
1337 for (m = 0; m < i; m++)
1339 if (q[m] == comp_id)
1344 al=SSL_AD_ILLEGAL_PARAMETER;
1345 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1351 else if (ssl_allow_compression(s) && s->ctx->comp_methods)
1352 { /* See if we have a match */
1353 int m,nn,o,v,done=0;
1355 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
1356 for (m=0; m<nn; m++)
1358 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
1371 s->s3->tmp.new_compression=comp;
1376 /* If compression is disabled we'd better not try to resume a session
1377 * using compression.
1379 if (s->session->compress_meth != 0)
1381 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
1386 /* Given s->session->ciphers and SSL_get_ciphers, we must
1391 #ifdef OPENSSL_NO_COMP
1392 s->session->compress_meth=0;
1394 s->session->compress_meth=(comp == NULL)?0:comp->id;
1396 if (s->session->ciphers != NULL)
1397 sk_SSL_CIPHER_free(s->session->ciphers);
1398 s->session->ciphers=ciphers;
1399 if (ciphers == NULL)
1401 al=SSL_AD_ILLEGAL_PARAMETER;
1402 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
1406 /* Let cert callback update server certificates if required */
1408 if (s->cert->cert_cb)
1410 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1413 al=SSL_AD_INTERNAL_ERROR;
1414 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CERT_CB_ERROR);
1419 s->rwstate=SSL_X509_LOOKUP;
1422 s->rwstate = SSL_NOTHING;
1424 c=ssl3_choose_cipher(s,s->session->ciphers,
1425 SSL_get_ciphers(s));
1429 al=SSL_AD_HANDSHAKE_FAILURE;
1430 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
1433 s->s3->tmp.new_cipher=c;
1434 /* check whether we should disable session resumption */
1435 if (s->not_resumable_session_cb != NULL)
1436 s->session->not_resumable=s->not_resumable_session_cb(s,
1437 ((c->algorithm_mkey & (SSL_kDHE | SSL_kECDHE)) != 0));
1438 if (s->session->not_resumable)
1439 /* do not send a session ticket */
1440 s->tlsext_ticket_expected = 0;
1444 /* Session-id reuse */
1445 #ifdef REUSE_CIPHER_BUG
1446 STACK_OF(SSL_CIPHER) *sk;
1447 SSL_CIPHER *nc=NULL;
1448 SSL_CIPHER *ec=NULL;
1450 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
1452 sk=s->session->ciphers;
1453 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1455 c=sk_SSL_CIPHER_value(sk,i);
1456 if (c->algorithm_enc & SSL_eNULL)
1458 if (SSL_C_IS_EXPORT(c))
1462 s->s3->tmp.new_cipher=nc;
1463 else if (ec != NULL)
1464 s->s3->tmp.new_cipher=ec;
1466 s->s3->tmp.new_cipher=s->session->cipher;
1470 s->s3->tmp.new_cipher=s->session->cipher;
1473 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER))
1475 if (!ssl3_digest_cached_records(s))
1479 /* we now have the following setup.
1481 * cipher_list - our prefered list of ciphers
1482 * ciphers - the clients prefered list of ciphers
1483 * compression - basically ignored right now
1484 * ssl version is set - sslv3
1485 * s->session - The ssl session has been setup.
1486 * s->hit - session reuse flag
1487 * s->tmp.new_cipher - the new cipher to use.
1490 /* Handles TLS extensions that we couldn't check earlier */
1491 if (s->version >= SSL3_VERSION)
1493 if (ssl_check_clienthello_tlsext_late(s) <= 0)
1495 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1500 if (ret < 0) ret=-ret;
1504 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1507 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
1508 return ret < 0 ? -1 : ret;
1511 int ssl3_send_server_hello(SSL *s)
1514 unsigned char *p,*d;
1519 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
1521 buf=(unsigned char *)s->init_buf->data;
1522 #ifdef OPENSSL_NO_TLSEXT
1523 p=s->s3->server_random;
1524 if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0)
1527 /* Do the message type and length last */
1528 d=p= ssl_handshake_start(s);
1530 *(p++)=s->version>>8;
1531 *(p++)=s->version&0xff;
1534 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
1535 p+=SSL3_RANDOM_SIZE;
1537 /* There are several cases for the session ID to send
1538 * back in the server hello:
1539 * - For session reuse from the session cache,
1540 * we send back the old session ID.
1541 * - If stateless session reuse (using a session ticket)
1542 * is successful, we send back the client's "session ID"
1543 * (which doesn't actually identify the session).
1544 * - If it is a new session, we send back the new
1546 * - However, if we want the new session to be single-use,
1547 * we send back a 0-length session ID.
1548 * s->hit is non-zero in either case of session reuse,
1549 * so the following won't overwrite an ID that we're supposed
1552 if (s->session->not_resumable ||
1553 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1555 s->session->session_id_length=0;
1557 sl=s->session->session_id_length;
1558 if (sl > (int)sizeof(s->session->session_id))
1560 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1564 memcpy(p,s->session->session_id,sl);
1567 /* put the cipher */
1568 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
1571 /* put the compression method */
1572 #ifdef OPENSSL_NO_COMP
1575 if (s->s3->tmp.new_compression == NULL)
1578 *(p++)=s->s3->tmp.new_compression->id;
1580 #ifndef OPENSSL_NO_TLSEXT
1581 if (ssl_prepare_serverhello_tlsext(s) <= 0)
1583 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
1586 if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, &al)) == NULL)
1588 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1589 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
1595 ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l);
1596 s->state=SSL3_ST_SW_SRVR_HELLO_B;
1599 /* SSL3_ST_SW_SRVR_HELLO_B */
1600 return ssl_do_write(s);
1603 int ssl3_send_server_done(SSL *s)
1606 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1608 ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0);
1609 s->state = SSL3_ST_SW_SRVR_DONE_B;
1612 /* SSL3_ST_SW_SRVR_DONE_B */
1613 return ssl_do_write(s);
1616 int ssl3_send_server_key_exchange(SSL *s)
1618 #ifndef OPENSSL_NO_RSA
1622 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1625 #ifndef OPENSSL_NO_DH
1628 #ifndef OPENSSL_NO_ECDH
1629 EC_KEY *ecdh=NULL, *ecdhp;
1630 unsigned char *encodedPoint = NULL;
1633 BN_CTX *bn_ctx = NULL;
1636 const EVP_MD *md = NULL;
1637 unsigned char *p,*d;
1647 EVP_MD_CTX_init(&md_ctx);
1648 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1650 type=s->s3->tmp.new_cipher->algorithm_mkey;
1655 r[0]=r[1]=r[2]=r[3]=NULL;
1657 #ifndef OPENSSL_NO_RSA
1658 if (type & SSL_kRSA)
1661 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1663 rsa=s->cert->rsa_tmp_cb(s,
1664 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1665 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1668 al=SSL_AD_HANDSHAKE_FAILURE;
1669 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1677 al=SSL_AD_HANDSHAKE_FAILURE;
1678 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1683 s->s3->tmp.use_rsa_tmp=1;
1687 #ifndef OPENSSL_NO_DH
1688 if (type & SSL_kDHE)
1690 if (s->cert->dh_tmp_auto)
1692 dhp = ssl_get_auto_dh(s);
1695 al=SSL_AD_INTERNAL_ERROR;
1696 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1702 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1703 dhp=s->cert->dh_tmp_cb(s,
1704 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1705 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1708 al=SSL_AD_HANDSHAKE_FAILURE;
1709 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1712 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1713 DH_security_bits(dhp), 0, dhp))
1715 al=SSL_AD_HANDSHAKE_FAILURE;
1716 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_DH_KEY_TOO_SMALL);
1719 if (s->s3->tmp.dh != NULL)
1721 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1725 if (s->cert->dh_tmp_auto)
1727 else if ((dh=DHparams_dup(dhp)) == NULL)
1729 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1734 if ((dhp->pub_key == NULL ||
1735 dhp->priv_key == NULL ||
1736 (s->options & SSL_OP_SINGLE_DH_USE)))
1738 if(!DH_generate_key(dh))
1740 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1747 dh->pub_key=BN_dup(dhp->pub_key);
1748 dh->priv_key=BN_dup(dhp->priv_key);
1749 if ((dh->pub_key == NULL) ||
1750 (dh->priv_key == NULL))
1752 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1762 #ifndef OPENSSL_NO_ECDH
1763 if (type & SSL_kECDHE)
1765 const EC_GROUP *group;
1767 ecdhp=cert->ecdh_tmp;
1768 if (s->cert->ecdh_tmp_auto)
1770 /* Get NID of appropriate shared curve */
1771 int nid = tls1_shared_curve(s, -2);
1772 if (nid != NID_undef)
1773 ecdhp = EC_KEY_new_by_curve_name(nid);
1775 else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb)
1777 ecdhp=s->cert->ecdh_tmp_cb(s,
1778 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1779 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1783 al=SSL_AD_HANDSHAKE_FAILURE;
1784 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
1788 if (s->s3->tmp.ecdh != NULL)
1790 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1794 /* Duplicate the ECDH structure. */
1797 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1800 if (s->cert->ecdh_tmp_auto)
1802 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL)
1804 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1808 s->s3->tmp.ecdh=ecdh;
1809 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1810 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1811 (s->options & SSL_OP_SINGLE_ECDH_USE))
1813 if(!EC_KEY_generate_key(ecdh))
1815 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1820 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1821 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1822 (EC_KEY_get0_private_key(ecdh) == NULL))
1824 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1828 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1829 (EC_GROUP_get_degree(group) > 163))
1831 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1835 /* XXX: For now, we only support ephemeral ECDH
1836 * keys over named (not generic) curves. For
1837 * supported named curves, curve_id is non-zero.
1840 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1843 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1847 /* Encode the public key.
1848 * First check the size of encoding and
1849 * allocate memory accordingly.
1851 encodedlen = EC_POINT_point2oct(group,
1852 EC_KEY_get0_public_key(ecdh),
1853 POINT_CONVERSION_UNCOMPRESSED,
1856 encodedPoint = (unsigned char *)
1857 OPENSSL_malloc(encodedlen*sizeof(unsigned char));
1858 bn_ctx = BN_CTX_new();
1859 if ((encodedPoint == NULL) || (bn_ctx == NULL))
1861 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1866 encodedlen = EC_POINT_point2oct(group,
1867 EC_KEY_get0_public_key(ecdh),
1868 POINT_CONVERSION_UNCOMPRESSED,
1869 encodedPoint, encodedlen, bn_ctx);
1871 if (encodedlen == 0)
1873 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1877 BN_CTX_free(bn_ctx); bn_ctx=NULL;
1879 /* XXX: For now, we only support named (not
1880 * generic) curves in ECDH ephemeral key exchanges.
1881 * In this situation, we need four additional bytes
1882 * to encode the entire ServerECDHParams
1887 /* We'll generate the serverKeyExchange message
1888 * explicitly so we can set these to NULLs
1896 #endif /* !OPENSSL_NO_ECDH */
1897 #ifndef OPENSSL_NO_PSK
1898 if (type & SSL_kPSK)
1900 /* reserve size for record length and PSK identity hint*/
1901 n+=2+strlen(s->ctx->psk_identity_hint);
1904 #endif /* !OPENSSL_NO_PSK */
1905 #ifndef OPENSSL_NO_SRP
1906 if (type & SSL_kSRP)
1908 if ((s->srp_ctx.N == NULL) ||
1909 (s->srp_ctx.g == NULL) ||
1910 (s->srp_ctx.s == NULL) ||
1911 (s->srp_ctx.B == NULL))
1913 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_SRP_PARAM);
1924 al=SSL_AD_HANDSHAKE_FAILURE;
1925 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1928 for (i=0; i < 4 && r[i] != NULL; i++)
1930 nr[i]=BN_num_bytes(r[i]);
1931 #ifndef OPENSSL_NO_SRP
1932 if ((i == 2) && (type & SSL_kSRP))
1939 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1940 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
1942 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher,&md))
1945 al=SSL_AD_DECODE_ERROR;
1948 kn=EVP_PKEY_size(pkey);
1956 if (!BUF_MEM_grow_clean(buf,n+SSL_HM_HEADER_LENGTH(s)+kn))
1958 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1961 d = p = ssl_handshake_start(s);
1963 for (i=0; i < 4 && r[i] != NULL; i++)
1965 #ifndef OPENSSL_NO_SRP
1966 if ((i == 2) && (type & SSL_kSRP))
1978 #ifndef OPENSSL_NO_ECDH
1979 if (type & SSL_kECDHE)
1981 /* XXX: For now, we only support named (not generic) curves.
1982 * In this situation, the serverKeyExchange message has:
1983 * [1 byte CurveType], [2 byte CurveName]
1984 * [1 byte length of encoded point], followed by
1985 * the actual encoded point itself
1987 *p = NAMED_CURVE_TYPE;
1995 memcpy((unsigned char*)p,
1996 (unsigned char *)encodedPoint,
1998 OPENSSL_free(encodedPoint);
1999 encodedPoint = NULL;
2004 #ifndef OPENSSL_NO_PSK
2005 if (type & SSL_kPSK)
2007 /* copy PSK identity hint */
2008 s2n(strlen(s->ctx->psk_identity_hint), p);
2009 strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
2010 p+=strlen(s->ctx->psk_identity_hint);
2017 /* n is the length of the params, they start at &(d[4])
2018 * and p points to the space at the end. */
2019 #ifndef OPENSSL_NO_RSA
2020 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
2024 for (num=2; num > 0; num--)
2026 EVP_MD_CTX_set_flags(&md_ctx,
2027 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
2028 EVP_DigestInit_ex(&md_ctx,(num == 2)
2029 ?s->ctx->md5:s->ctx->sha1, NULL);
2030 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
2031 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
2032 EVP_DigestUpdate(&md_ctx,d,n);
2033 EVP_DigestFinal_ex(&md_ctx,q,
2034 (unsigned int *)&i);
2038 if (RSA_sign(NID_md5_sha1, md_buf, j,
2039 &(p[2]), &u, pkey->pkey.rsa) <= 0)
2041 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
2051 /* send signature algorithm */
2052 if (SSL_USE_SIGALGS(s))
2054 if (!tls12_get_sigandhash(p, pkey, md))
2056 /* Should never happen */
2057 al=SSL_AD_INTERNAL_ERROR;
2058 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
2064 fprintf(stderr, "Using hash %s\n",
2067 EVP_SignInit_ex(&md_ctx, md, NULL);
2068 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
2069 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
2070 EVP_SignUpdate(&md_ctx,d,n);
2071 if (!EVP_SignFinal(&md_ctx,&(p[2]),
2072 (unsigned int *)&i,pkey))
2074 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_EVP);
2079 if (SSL_USE_SIGALGS(s))
2084 /* Is this error check actually needed? */
2085 al=SSL_AD_HANDSHAKE_FAILURE;
2086 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
2091 ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n);
2094 s->state = SSL3_ST_SW_KEY_EXCH_B;
2095 EVP_MD_CTX_cleanup(&md_ctx);
2096 return ssl_do_write(s);
2098 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2100 #ifndef OPENSSL_NO_ECDH
2101 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2102 BN_CTX_free(bn_ctx);
2104 EVP_MD_CTX_cleanup(&md_ctx);
2108 int ssl3_send_certificate_request(SSL *s)
2110 unsigned char *p,*d;
2112 STACK_OF(X509_NAME) *sk=NULL;
2116 if (s->state == SSL3_ST_SW_CERT_REQ_A)
2120 d=p=ssl_handshake_start(s);
2122 /* get the list of acceptable cert types */
2124 n=ssl3_get_req_cert_type(s,p);
2129 if (SSL_USE_SIGALGS(s))
2131 const unsigned char *psigs;
2132 unsigned char *etmp = p;
2133 nl = tls12_get_psigalgs(s, &psigs);
2134 /* Skip over length for now */
2136 nl = tls12_copy_sigalgs(s, p, psigs, nl);
2137 /* Now fill in length */
2147 sk=SSL_get_client_CA_list(s);
2151 for (i=0; i<sk_X509_NAME_num(sk); i++)
2153 name=sk_X509_NAME_value(sk,i);
2154 j=i2d_X509_NAME(name,NULL);
2155 if (!BUF_MEM_grow_clean(buf,SSL_HM_HEADER_LENGTH(s)+n+j+2))
2157 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
2160 p = ssl_handshake_start(s) + n;
2161 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2164 i2d_X509_NAME(name,&p);
2171 i2d_X509_NAME(name,&p);
2172 j-=2; s2n(j,d); j+=2;
2178 /* else no CA names */
2179 p = ssl_handshake_start(s) + off;
2182 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n);
2184 #ifdef NETSCAPE_HANG_BUG
2185 if (!SSL_IS_DTLS(s))
2187 p=(unsigned char *)s->init_buf->data + s->init_num;
2189 *(p++)=SSL3_MT_SERVER_DONE;
2197 s->state = SSL3_ST_SW_CERT_REQ_B;
2200 /* SSL3_ST_SW_CERT_REQ_B */
2201 return ssl_do_write(s);
2206 int ssl3_get_client_key_exchange(SSL *s)
2210 unsigned long alg_k;
2212 #ifndef OPENSSL_NO_RSA
2214 EVP_PKEY *pkey=NULL;
2216 #ifndef OPENSSL_NO_DH
2218 DH *dh_srvr, *dh_clnt = NULL;
2220 #ifndef OPENSSL_NO_KRB5
2222 #endif /* OPENSSL_NO_KRB5 */
2224 #ifndef OPENSSL_NO_ECDH
2225 EC_KEY *srvr_ecdh = NULL;
2226 EVP_PKEY *clnt_pub_pkey = NULL;
2227 EC_POINT *clnt_ecpoint = NULL;
2228 BN_CTX *bn_ctx = NULL;
2231 n=s->method->ssl_get_message(s,
2232 SSL3_ST_SR_KEY_EXCH_A,
2233 SSL3_ST_SR_KEY_EXCH_B,
2234 SSL3_MT_CLIENT_KEY_EXCHANGE,
2238 if (!ok) return((int)n);
2239 p=(unsigned char *)s->init_msg;
2241 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2243 #ifndef OPENSSL_NO_RSA
2244 if (alg_k & SSL_kRSA)
2246 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2247 int decrypt_len, decrypt_good_mask;
2248 unsigned char version_good;
2250 /* FIX THIS UP EAY EAY EAY EAY */
2251 if (s->s3->tmp.use_rsa_tmp)
2253 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2254 rsa=s->cert->rsa_tmp;
2255 /* Don't do a callback because rsa_tmp should
2256 * be sent already */
2259 al=SSL_AD_HANDSHAKE_FAILURE;
2260 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
2267 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2268 if ( (pkey == NULL) ||
2269 (pkey->type != EVP_PKEY_RSA) ||
2270 (pkey->pkey.rsa == NULL))
2272 al=SSL_AD_HANDSHAKE_FAILURE;
2273 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
2279 /* TLS and [incidentally] DTLS{0xFEFF} */
2280 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER)
2285 if (!(s->options & SSL_OP_TLS_D5_BUG))
2287 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2297 /* We must not leak whether a decryption failure occurs because
2298 * of Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see
2299 * RFC 2246, section 7.4.7.1). The code follows that advice of
2300 * the TLS RFC and generates a random premaster secret for the
2301 * case that the decrypt fails. See
2302 * https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
2304 /* should be RAND_bytes, but we cannot work around a failure. */
2305 if (RAND_pseudo_bytes(rand_premaster_secret,
2306 sizeof(rand_premaster_secret)) <= 0)
2308 decrypt_len = RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
2311 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH.
2312 * decrypt_good_mask will be zero if so and non-zero otherwise. */
2313 decrypt_good_mask = decrypt_len ^ SSL_MAX_MASTER_KEY_LENGTH;
2315 /* If the version in the decrypted pre-master secret is correct
2316 * then version_good will be zero. The Klima-Pokorny-Rosa
2317 * extension of Bleichenbacher's attack
2318 * (http://eprint.iacr.org/2003/052/) exploits the version
2319 * number check as a "bad version oracle". Thus version checks
2320 * are done in constant time and are treated like any other
2321 * decryption error. */
2322 version_good = p[0] ^ (s->client_version>>8);
2323 version_good |= p[1] ^ (s->client_version&0xff);
2325 /* The premaster secret must contain the same version number as
2326 * the ClientHello to detect version rollback attacks
2327 * (strangely, the protocol does not offer such protection for
2328 * DH ciphersuites). However, buggy clients exist that send the
2329 * negotiated protocol version instead if the server does not
2330 * support the requested protocol version. If
2331 * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
2332 if (s->options & SSL_OP_TLS_ROLLBACK_BUG)
2334 unsigned char workaround_mask = version_good;
2335 unsigned char workaround;
2337 /* workaround_mask will be 0xff if version_good is
2338 * non-zero (i.e. the version match failed). Otherwise
2340 workaround_mask |= workaround_mask >> 4;
2341 workaround_mask |= workaround_mask >> 2;
2342 workaround_mask |= workaround_mask >> 1;
2343 workaround_mask = ~((workaround_mask & 1) - 1);
2345 workaround = p[0] ^ (s->version>>8);
2346 workaround |= p[1] ^ (s->version&0xff);
2348 /* If workaround_mask is 0xff (i.e. there was a version
2349 * mismatch) then we copy the value of workaround over
2351 version_good = (workaround & workaround_mask) |
2352 (version_good & ~workaround_mask);
2355 /* If any bits in version_good are set then they'll poision
2356 * decrypt_good_mask and cause rand_premaster_secret to be
2358 decrypt_good_mask |= version_good;
2360 /* decrypt_good_mask will be zero iff decrypt_len ==
2361 * SSL_MAX_MASTER_KEY_LENGTH and the version check passed. We
2362 * fold the bottom 32 bits of it with an OR so that the LSB
2363 * will be zero iff everything is good. This assumes that we'll
2364 * never decrypt a value > 2**31 bytes, which seems safe. */
2365 decrypt_good_mask |= decrypt_good_mask >> 16;
2366 decrypt_good_mask |= decrypt_good_mask >> 8;
2367 decrypt_good_mask |= decrypt_good_mask >> 4;
2368 decrypt_good_mask |= decrypt_good_mask >> 2;
2369 decrypt_good_mask |= decrypt_good_mask >> 1;
2370 /* Now select only the LSB and subtract one. If decrypt_len ==
2371 * SSL_MAX_MASTER_KEY_LENGTH and the version check passed then
2372 * decrypt_good_mask will be all ones. Otherwise it'll be all
2374 decrypt_good_mask &= 1;
2375 decrypt_good_mask--;
2377 /* Now copy rand_premaster_secret over p using
2378 * decrypt_good_mask. */
2379 for (i = 0; i < (int) sizeof(rand_premaster_secret); i++)
2381 p[i] = (p[i] & decrypt_good_mask) |
2382 (rand_premaster_secret[i] & ~decrypt_good_mask);
2385 s->session->master_key_length=
2386 s->method->ssl3_enc->generate_master_secret(s,
2387 s->session->master_key,
2389 OPENSSL_cleanse(p,i);
2393 #ifndef OPENSSL_NO_DH
2394 if (alg_k & (SSL_kDHE|SSL_kDHr|SSL_kDHd))
2397 EVP_PKEY *skey = NULL;
2404 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
2406 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2415 if (alg_k & SSL_kDHr)
2416 idx = SSL_PKEY_DH_RSA;
2417 else if (alg_k & SSL_kDHd)
2418 idx = SSL_PKEY_DH_DSA;
2421 skey = s->cert->pkeys[idx].privatekey;
2422 if ((skey == NULL) ||
2423 (skey->type != EVP_PKEY_DH) ||
2424 (skey->pkey.dh == NULL))
2426 al=SSL_AD_HANDSHAKE_FAILURE;
2427 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
2430 dh_srvr = skey->pkey.dh;
2432 else if (s->s3->tmp.dh == NULL)
2434 al=SSL_AD_HANDSHAKE_FAILURE;
2435 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
2439 dh_srvr=s->s3->tmp.dh;
2443 /* Get pubkey from cert */
2444 EVP_PKEY *clkey=X509_get_pubkey(s->session->peer);
2447 if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2448 dh_clnt = EVP_PKEY_get1_DH(clkey);
2450 if (dh_clnt == NULL)
2452 al=SSL_AD_HANDSHAKE_FAILURE;
2453 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
2456 EVP_PKEY_free(clkey);
2457 pub = dh_clnt->pub_key;
2460 pub=BN_bin2bn(p,i,NULL);
2463 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
2467 i=DH_compute_key(p,pub,dh_srvr);
2471 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2476 DH_free(s->s3->tmp.dh);
2483 s->session->master_key_length=
2484 s->method->ssl3_enc->generate_master_secret(s,
2485 s->session->master_key,p,i);
2486 OPENSSL_cleanse(p,i);
2492 #ifndef OPENSSL_NO_KRB5
2493 if (alg_k & SSL_kKRB5)
2495 krb5_error_code krb5rc;
2496 krb5_data enc_ticket;
2497 krb5_data authenticator;
2499 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2500 EVP_CIPHER_CTX ciph_ctx;
2501 const EVP_CIPHER *enc = NULL;
2502 unsigned char iv[EVP_MAX_IV_LENGTH];
2503 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
2504 + EVP_MAX_BLOCK_LENGTH];
2506 krb5_timestamp authtime = 0;
2507 krb5_ticket_times ttimes;
2509 EVP_CIPHER_CTX_init(&ciph_ctx);
2511 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
2514 enc_ticket.length = i;
2516 if (n < (long)(enc_ticket.length + 6))
2518 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2519 SSL_R_DATA_LENGTH_TOO_LONG);
2523 enc_ticket.data = (char *)p;
2524 p+=enc_ticket.length;
2527 authenticator.length = i;
2529 if (n < (long)(enc_ticket.length + authenticator.length + 6))
2531 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2532 SSL_R_DATA_LENGTH_TOO_LONG);
2536 authenticator.data = (char *)p;
2537 p+=authenticator.length;
2541 enc_pms.data = (char *)p;
2544 /* Note that the length is checked again below,
2547 if(enc_pms.length > sizeof pms)
2549 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2550 SSL_R_DATA_LENGTH_TOO_LONG);
2554 if (n != (long)(enc_ticket.length + authenticator.length +
2555 enc_pms.length + 6))
2557 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2558 SSL_R_DATA_LENGTH_TOO_LONG);
2562 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2566 printf("kssl_sget_tkt rtn %d [%d]\n",
2567 krb5rc, kssl_err.reason);
2569 printf("kssl_err text= %s\n", kssl_err.text);
2570 #endif /* KSSL_DEBUG */
2571 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2576 /* Note: no authenticator is not considered an error,
2577 ** but will return authtime == 0.
2579 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2580 &authtime, &kssl_err)) != 0)
2583 printf("kssl_check_authent rtn %d [%d]\n",
2584 krb5rc, kssl_err.reason);
2586 printf("kssl_err text= %s\n", kssl_err.text);
2587 #endif /* KSSL_DEBUG */
2588 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2593 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
2595 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2600 kssl_ctx_show(kssl_ctx);
2601 #endif /* KSSL_DEBUG */
2603 enc = kssl_map_enc(kssl_ctx->enctype);
2607 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2609 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
2611 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2612 SSL_R_DECRYPTION_FAILED);
2615 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
2616 (unsigned char *)enc_pms.data, enc_pms.length))
2618 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2619 SSL_R_DECRYPTION_FAILED);
2622 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
2624 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2625 SSL_R_DATA_LENGTH_TOO_LONG);
2628 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
2630 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2631 SSL_R_DECRYPTION_FAILED);
2635 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
2637 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2638 SSL_R_DATA_LENGTH_TOO_LONG);
2641 if (!((pms[0] == (s->client_version>>8)) && (pms[1] == (s->client_version & 0xff))))
2643 /* The premaster secret must contain the same version number as the
2644 * ClientHello to detect version rollback attacks (strangely, the
2645 * protocol does not offer such protection for DH ciphersuites).
2646 * However, buggy clients exist that send random bytes instead of
2647 * the protocol version.
2648 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2649 * (Perhaps we should have a separate BUG value for the Kerberos cipher)
2651 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG))
2653 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2654 SSL_AD_DECODE_ERROR);
2659 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2661 s->session->master_key_length=
2662 s->method->ssl3_enc->generate_master_secret(s,
2663 s->session->master_key, pms, outl);
2665 if (kssl_ctx->client_princ)
2667 size_t len = strlen(kssl_ctx->client_princ);
2668 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
2670 s->session->krb5_client_princ_len = len;
2671 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
2676 /* Was doing kssl_ctx_free() here,
2677 ** but it caused problems for apache.
2678 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
2679 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
2683 #endif /* OPENSSL_NO_KRB5 */
2685 #ifndef OPENSSL_NO_ECDH
2686 if (alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe))
2691 const EC_GROUP *group;
2692 const BIGNUM *priv_key;
2694 /* initialize structures for server's ECDH key pair */
2695 if ((srvr_ecdh = EC_KEY_new()) == NULL)
2697 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2698 ERR_R_MALLOC_FAILURE);
2702 /* Let's get server private key and group information */
2703 if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2705 /* use the certificate */
2706 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2710 /* use the ephermeral values we saved when
2711 * generating the ServerKeyExchange msg.
2713 tkey = s->s3->tmp.ecdh;
2716 group = EC_KEY_get0_group(tkey);
2717 priv_key = EC_KEY_get0_private_key(tkey);
2719 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2720 !EC_KEY_set_private_key(srvr_ecdh, priv_key))
2722 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2727 /* Let's get client's public key */
2728 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
2730 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2731 ERR_R_MALLOC_FAILURE);
2737 /* Client Publickey was in Client Certificate */
2739 if (alg_k & SSL_kECDHE)
2741 al=SSL_AD_HANDSHAKE_FAILURE;
2742 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
2745 if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
2747 (clnt_pub_pkey->type != EVP_PKEY_EC))
2749 /* XXX: For now, we do not support client
2750 * authentication using ECDH certificates
2751 * so this branch (n == 0L) of the code is
2752 * never executed. When that support is
2753 * added, we ought to ensure the key
2754 * received in the certificate is
2755 * authorized for key agreement.
2756 * ECDH_compute_key implicitly checks that
2757 * the two ECDH shares are for the same
2760 al=SSL_AD_HANDSHAKE_FAILURE;
2761 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2762 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2766 if (EC_POINT_copy(clnt_ecpoint,
2767 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)
2769 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2773 ret = 2; /* Skip certificate verify processing */
2777 /* Get client's public key from encoded point
2778 * in the ClientKeyExchange message.
2780 if ((bn_ctx = BN_CTX_new()) == NULL)
2782 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2783 ERR_R_MALLOC_FAILURE);
2787 /* Get encoded point length */
2792 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2796 if (EC_POINT_oct2point(group,
2797 clnt_ecpoint, p, i, bn_ctx) == 0)
2799 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2803 /* p is pointing to somewhere in the buffer
2804 * currently, so set it to the start
2806 p=(unsigned char *)s->init_buf->data;
2809 /* Compute the shared pre-master secret */
2810 field_size = EC_GROUP_get_degree(group);
2811 if (field_size <= 0)
2813 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2817 i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
2820 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2825 EVP_PKEY_free(clnt_pub_pkey);
2826 EC_POINT_free(clnt_ecpoint);
2827 EC_KEY_free(srvr_ecdh);
2828 BN_CTX_free(bn_ctx);
2829 EC_KEY_free(s->s3->tmp.ecdh);
2830 s->s3->tmp.ecdh = NULL;
2832 /* Compute the master secret */
2833 s->session->master_key_length = s->method->ssl3_enc-> \
2834 generate_master_secret(s, s->session->master_key, p, i);
2836 OPENSSL_cleanse(p, i);
2841 #ifndef OPENSSL_NO_PSK
2842 if (alg_k & SSL_kPSK)
2844 unsigned char *t = NULL;
2845 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
2846 unsigned int pre_ms_len = 0, psk_len = 0;
2848 char tmp_id[PSK_MAX_IDENTITY_LEN+1];
2850 al=SSL_AD_HANDSHAKE_FAILURE;
2855 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2856 SSL_R_LENGTH_MISMATCH);
2859 if (i > PSK_MAX_IDENTITY_LEN)
2861 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2862 SSL_R_DATA_LENGTH_TOO_LONG);
2865 if (s->psk_server_callback == NULL)
2867 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2868 SSL_R_PSK_NO_SERVER_CB);
2872 /* Create guaranteed NULL-terminated identity
2873 * string for the callback */
2874 memcpy(tmp_id, p, i);
2875 memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
2876 psk_len = s->psk_server_callback(s, tmp_id,
2877 psk_or_pre_ms, sizeof(psk_or_pre_ms));
2878 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1);
2880 if (psk_len > PSK_MAX_PSK_LEN)
2882 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2883 ERR_R_INTERNAL_ERROR);
2886 else if (psk_len == 0)
2888 /* PSK related to the given identity not found */
2889 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2890 SSL_R_PSK_IDENTITY_NOT_FOUND);
2891 al=SSL_AD_UNKNOWN_PSK_IDENTITY;
2895 /* create PSK pre_master_secret */
2896 pre_ms_len=2+psk_len+2+psk_len;
2898 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
2900 memset(t, 0, psk_len);
2904 if (s->session->psk_identity != NULL)
2905 OPENSSL_free(s->session->psk_identity);
2906 s->session->psk_identity = BUF_strdup((char *)p);
2907 if (s->session->psk_identity == NULL)
2909 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2910 ERR_R_MALLOC_FAILURE);
2914 if (s->session->psk_identity_hint != NULL)
2915 OPENSSL_free(s->session->psk_identity_hint);
2916 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2917 if (s->ctx->psk_identity_hint != NULL &&
2918 s->session->psk_identity_hint == NULL)
2920 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2921 ERR_R_MALLOC_FAILURE);
2925 s->session->master_key_length=
2926 s->method->ssl3_enc->generate_master_secret(s,
2927 s->session->master_key, psk_or_pre_ms, pre_ms_len);
2930 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2936 #ifndef OPENSSL_NO_SRP
2937 if (alg_k & SSL_kSRP)
2945 al=SSL_AD_DECODE_ERROR;
2946 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_SRP_A_LENGTH);
2949 if (!(s->srp_ctx.A=BN_bin2bn(p,i,NULL)))
2951 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_BN_LIB);
2954 if (s->session->srp_username != NULL)
2955 OPENSSL_free(s->session->srp_username);
2956 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2957 if (s->session->srp_username == NULL)
2959 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2960 ERR_R_MALLOC_FAILURE);
2964 if ((s->session->master_key_length = SRP_generate_server_master_secret(s,s->session->master_key))<0)
2966 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
2973 #endif /* OPENSSL_NO_SRP */
2974 if (alg_k & SSL_kGOST)
2977 EVP_PKEY_CTX *pkey_ctx;
2978 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2979 unsigned char premaster_secret[32], *start;
2980 size_t outlen=32, inlen;
2981 unsigned long alg_a;
2985 /* Get our certificate private key*/
2986 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2987 if (alg_a & SSL_aGOST94)
2988 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
2989 else if (alg_a & SSL_aGOST01)
2990 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2992 pkey_ctx = EVP_PKEY_CTX_new(pk,NULL);
2993 EVP_PKEY_decrypt_init(pkey_ctx);
2994 /* If client certificate is present and is of the same type, maybe
2995 * use it for key exchange. Don't mind errors from
2996 * EVP_PKEY_derive_set_peer, because it is completely valid to use
2997 * a client certificate for authorization only. */
2998 client_pub_pkey = X509_get_pubkey(s->session->peer);
2999 if (client_pub_pkey)
3001 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
3004 /* Decrypt session key */
3005 if (ASN1_get_object((const unsigned char **)&p, &Tlen, &Ttag, &Tclass, n) != V_ASN1_CONSTRUCTED ||
3006 Ttag != V_ASN1_SEQUENCE ||
3007 Tclass != V_ASN1_UNIVERSAL)
3009 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
3014 if (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,start,inlen) <=0)
3017 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
3020 /* Generate master secret */
3021 s->session->master_key_length=
3022 s->method->ssl3_enc->generate_master_secret(s,
3023 s->session->master_key,premaster_secret,32);
3024 /* Check if pubkey from client certificate was used */
3025 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
3030 EVP_PKEY_free(client_pub_pkey);
3031 EVP_PKEY_CTX_free(pkey_ctx);
3039 al=SSL_AD_HANDSHAKE_FAILURE;
3040 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
3041 SSL_R_UNKNOWN_CIPHER_TYPE);
3047 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3048 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP)
3051 #ifndef OPENSSL_NO_ECDH
3052 EVP_PKEY_free(clnt_pub_pkey);
3053 EC_POINT_free(clnt_ecpoint);
3054 if (srvr_ecdh != NULL)
3055 EC_KEY_free(srvr_ecdh);
3056 BN_CTX_free(bn_ctx);
3061 int ssl3_get_cert_verify(SSL *s)
3063 EVP_PKEY *pkey=NULL;
3069 const EVP_MD *md = NULL;
3071 EVP_MD_CTX_init(&mctx);
3073 n=s->method->ssl_get_message(s,
3074 SSL3_ST_SR_CERT_VRFY_A,
3075 SSL3_ST_SR_CERT_VRFY_B,
3077 516, /* Enough for 4096 bit RSA key with TLS v1.2 */
3080 if (!ok) return((int)n);
3082 if (s->session->peer != NULL)
3084 peer=s->session->peer;
3085 pkey=X509_get_pubkey(peer);
3086 type=X509_certificate_type(peer,pkey);
3094 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
3096 s->s3->tmp.reuse_message=1;
3097 if ((peer != NULL) && (type & EVP_PKT_SIGN))
3099 al=SSL_AD_UNEXPECTED_MESSAGE;
3100 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
3109 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
3110 al=SSL_AD_UNEXPECTED_MESSAGE;
3114 if (!(type & EVP_PKT_SIGN))
3116 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
3117 al=SSL_AD_ILLEGAL_PARAMETER;
3121 if (s->s3->change_cipher_spec)
3123 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
3124 al=SSL_AD_UNEXPECTED_MESSAGE;
3128 /* we now have a signature that we need to verify */
3129 p=(unsigned char *)s->init_msg;
3130 /* Check for broken implementations of GOST ciphersuites */
3131 /* If key is GOST and n is exactly 64, it is bare
3132 * signature without length field */
3133 if (n==64 && (pkey->type==NID_id_GostR3410_94 ||
3134 pkey->type == NID_id_GostR3410_2001) )
3140 if (SSL_USE_SIGALGS(s))
3142 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
3145 al = SSL_AD_INTERNAL_ERROR;
3150 al = SSL_AD_DECODE_ERROR;
3154 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
3163 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
3164 al=SSL_AD_DECODE_ERROR;
3168 j=EVP_PKEY_size(pkey);
3169 if ((i > j) || (n > j) || (n <= 0))
3171 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
3172 al=SSL_AD_DECODE_ERROR;
3176 if (SSL_USE_SIGALGS(s))
3180 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3183 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3184 al=SSL_AD_INTERNAL_ERROR;
3188 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
3191 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
3192 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen))
3194 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
3195 al=SSL_AD_INTERNAL_ERROR;
3199 if (EVP_VerifyFinal(&mctx, p , i, pkey) <= 0)
3201 al=SSL_AD_DECRYPT_ERROR;
3202 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_SIGNATURE);
3207 #ifndef OPENSSL_NO_RSA
3208 if (pkey->type == EVP_PKEY_RSA)
3210 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
3211 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
3215 al=SSL_AD_DECRYPT_ERROR;
3216 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
3221 al=SSL_AD_DECRYPT_ERROR;
3222 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
3228 #ifndef OPENSSL_NO_DSA
3229 if (pkey->type == EVP_PKEY_DSA)
3231 j=DSA_verify(pkey->save_type,
3232 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3233 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
3237 al=SSL_AD_DECRYPT_ERROR;
3238 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
3244 #ifndef OPENSSL_NO_ECDSA
3245 if (pkey->type == EVP_PKEY_EC)
3247 j=ECDSA_verify(pkey->save_type,
3248 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3249 SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
3253 al=SSL_AD_DECRYPT_ERROR;
3254 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
3255 SSL_R_BAD_ECDSA_SIGNATURE);
3261 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
3262 { unsigned char signature[64];
3264 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey,NULL);
3265 EVP_PKEY_verify_init(pctx);
3267 fprintf(stderr,"GOST signature length is %d",i);
3269 for (idx=0;idx<64;idx++) {
3270 signature[63-idx]=p[idx];
3272 j=EVP_PKEY_verify(pctx,signature,64,s->s3->tmp.cert_verify_md,32);
3273 EVP_PKEY_CTX_free(pctx);
3276 al=SSL_AD_DECRYPT_ERROR;
3277 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
3278 SSL_R_BAD_ECDSA_SIGNATURE);
3284 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
3285 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
3294 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3297 if (s->s3->handshake_buffer)
3299 BIO_free(s->s3->handshake_buffer);
3300 s->s3->handshake_buffer = NULL;
3301 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3303 EVP_MD_CTX_cleanup(&mctx);
3304 EVP_PKEY_free(pkey);
3308 int ssl3_get_client_certificate(SSL *s)
3310 int i,ok,al,ret= -1;
3312 unsigned long l,nc,llen,n;
3313 const unsigned char *p,*q;
3315 STACK_OF(X509) *sk=NULL;
3317 n=s->method->ssl_get_message(s,
3324 if (!ok) return((int)n);
3326 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
3328 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
3329 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
3331 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3332 al=SSL_AD_HANDSHAKE_FAILURE;
3335 /* If tls asked for a client cert, the client must return a 0 list */
3336 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
3338 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
3339 al=SSL_AD_UNEXPECTED_MESSAGE;
3342 s->s3->tmp.reuse_message=1;
3346 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
3348 al=SSL_AD_UNEXPECTED_MESSAGE;
3349 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
3352 p=d=(unsigned char *)s->init_msg;
3354 if ((sk=sk_X509_new_null()) == NULL)
3356 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
3363 al=SSL_AD_DECODE_ERROR;
3364 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
3367 for (nc=0; nc<llen; )
3370 if ((l+nc+3) > llen)
3372 al=SSL_AD_DECODE_ERROR;
3373 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
3378 x=d2i_X509(NULL,&p,l);
3381 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
3386 al=SSL_AD_DECODE_ERROR;
3387 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
3390 if (!sk_X509_push(sk,x))
3392 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
3399 if (sk_X509_num(sk) <= 0)
3401 /* TLS does not mind 0 certs returned */
3402 if (s->version == SSL3_VERSION)
3404 al=SSL_AD_HANDSHAKE_FAILURE;
3405 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
3408 /* Fail for TLS only if we required a certificate */
3409 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3410 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
3412 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3413 al=SSL_AD_HANDSHAKE_FAILURE;
3416 /* No client certificate so digest cached records */
3417 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s))
3419 al=SSL_AD_INTERNAL_ERROR;
3426 i=ssl_verify_cert_chain(s,sk);
3429 al=ssl_verify_alarm_type(s->verify_result);
3430 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
3435 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, i);
3436 al = SSL_AD_HANDSHAKE_FAILURE;
3439 pkey = X509_get_pubkey(sk_X509_value(sk, 0));
3442 al=SSL3_AD_HANDSHAKE_FAILURE;
3443 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3444 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3447 EVP_PKEY_free(pkey);
3450 if (s->session->peer != NULL) /* This should not be needed */
3451 X509_free(s->session->peer);
3452 s->session->peer=sk_X509_shift(sk);
3453 s->session->verify_result = s->verify_result;
3455 /* With the current implementation, sess_cert will always be NULL
3456 * when we arrive here. */
3457 if (s->session->sess_cert == NULL)
3459 s->session->sess_cert = ssl_sess_cert_new();
3460 if (s->session->sess_cert == NULL)
3462 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3466 if (s->session->sess_cert->cert_chain != NULL)
3467 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
3468 s->session->sess_cert->cert_chain=sk;
3469 /* Inconsistency alert: cert_chain does *not* include the
3470 * peer's own certificate, while we do include it in s3_clnt.c */
3478 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3481 if (x != NULL) X509_free(x);
3482 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
3486 int ssl3_send_server_certificate(SSL *s)
3490 if (s->state == SSL3_ST_SW_CERT_A)
3492 cpk=ssl_get_server_send_pkey(s);
3495 /* VRS: allow null cert if auth == KRB5 */
3496 if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
3497 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5))
3499 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
3504 if (!ssl3_output_cert_chain(s,cpk))
3506 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
3509 s->state=SSL3_ST_SW_CERT_B;
3512 /* SSL3_ST_SW_CERT_B */
3513 return ssl_do_write(s);
3516 #ifndef OPENSSL_NO_TLSEXT
3517 /* send a new session ticket (not necessarily for a new session) */
3518 int ssl3_send_newsession_ticket(SSL *s)
3520 if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
3522 unsigned char *p, *senc, *macstart;
3523 const unsigned char *const_p;
3524 int len, slen_full, slen;
3529 SSL_CTX *tctx = s->initial_ctx;
3530 unsigned char iv[EVP_MAX_IV_LENGTH];
3531 unsigned char key_name[16];
3533 /* get session encoding length */
3534 slen_full = i2d_SSL_SESSION(s->session, NULL);
3535 /* Some length values are 16 bits, so forget it if session is
3538 if (slen_full > 0xFF00)
3540 senc = OPENSSL_malloc(slen_full);
3544 i2d_SSL_SESSION(s->session, &p);
3546 /* create a fresh copy (not shared with other threads) to clean up */
3548 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3554 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3556 slen = i2d_SSL_SESSION(sess, NULL);
3557 if (slen > slen_full) /* shouldn't ever happen */
3563 i2d_SSL_SESSION(sess, &p);
3564 SSL_SESSION_free(sess);
3566 /* Grow buffer if need be: the length calculation is as
3567 * follows handshake_header_length +
3568 * 4 (ticket lifetime hint) + 2 (ticket length) +
3569 * 16 (key name) + max_iv_len (iv length) +
3570 * session_length + max_enc_block_size (max encrypted session
3571 * length) + max_md_size (HMAC).
3573 if (!BUF_MEM_grow(s->init_buf,
3574 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3575 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
3577 p = ssl_handshake_start(s);
3578 EVP_CIPHER_CTX_init(&ctx);
3579 HMAC_CTX_init(&hctx);
3580 /* Initialize HMAC and cipher contexts. If callback present
3581 * it does all the work otherwise use generated values
3584 if (tctx->tlsext_ticket_key_cb)
3586 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3595 RAND_pseudo_bytes(iv, 16);
3596 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3597 tctx->tlsext_tick_aes_key, iv);
3598 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3599 tlsext_tick_md(), NULL);
3600 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3603 /* Ticket lifetime hint (advisory only):
3604 * We leave this unspecified for resumed session (for simplicity),
3605 * and guess that tickets for new sessions will live as long
3606 * as their sessions. */
3607 l2n(s->hit ? 0 : s->session->timeout, p);
3609 /* Skip ticket length for now */
3611 /* Output key name */
3613 memcpy(p, key_name, 16);
3616 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3617 p += EVP_CIPHER_CTX_iv_length(&ctx);
3618 /* Encrypt session data */
3619 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
3621 EVP_EncryptFinal(&ctx, p, &len);
3623 EVP_CIPHER_CTX_cleanup(&ctx);
3625 HMAC_Update(&hctx, macstart, p - macstart);
3626 HMAC_Final(&hctx, p, &hlen);
3627 HMAC_CTX_cleanup(&hctx);
3630 /* Now write out lengths: p points to end of data written */
3632 len = p - ssl_handshake_start(s);
3633 ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
3634 /* Skip ticket lifetime hint */
3635 p = ssl_handshake_start(s) + 4;
3637 s->state=SSL3_ST_SW_SESSION_TICKET_B;
3641 /* SSL3_ST_SW_SESSION_TICKET_B */
3642 return ssl_do_write(s);
3645 int ssl3_send_cert_status(SSL *s)
3647 if (s->state == SSL3_ST_SW_CERT_STATUS_A)
3650 /* Grow buffer if need be: the length calculation is as
3651 * follows 1 (message type) + 3 (message length) +
3652 * 1 (ocsp response type) + 3 (ocsp response length)
3655 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
3658 p=(unsigned char *)s->init_buf->data;
3661 *(p++)=SSL3_MT_CERTIFICATE_STATUS;
3662 /* message length */
3663 l2n3(s->tlsext_ocsp_resplen + 4, p);
3665 *(p++)= s->tlsext_status_type;
3666 /* length of OCSP response */
3667 l2n3(s->tlsext_ocsp_resplen, p);
3668 /* actual response */
3669 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3670 /* number of bytes to write */
3671 s->init_num = 8 + s->tlsext_ocsp_resplen;
3672 s->state=SSL3_ST_SW_CERT_STATUS_B;
3676 /* SSL3_ST_SW_CERT_STATUS_B */
3677 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
3680 # ifndef OPENSSL_NO_NEXTPROTONEG
3681 /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
3682 * sets the next_proto member in s if found */
3683 int ssl3_get_next_proto(SSL *s)
3686 int proto_len, padding_len;
3688 const unsigned char *p;
3690 /* Clients cannot send a NextProtocol message if we didn't see the
3691 * extension in their ClientHello */
3692 if (!s->s3->next_proto_neg_seen)
3694 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
3698 n=s->method->ssl_get_message(s,
3699 SSL3_ST_SR_NEXT_PROTO_A,
3700 SSL3_ST_SR_NEXT_PROTO_B,
3702 514, /* See the payload format below */
3708 /* s->state doesn't reflect whether ChangeCipherSpec has been received
3709 * in this handshake, but s->s3->change_cipher_spec does (will be reset
3710 * by ssl3_get_finished). */
3711 if (!s->s3->change_cipher_spec)
3713 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
3718 return 0; /* The body must be > 1 bytes long */
3720 p=(unsigned char *)s->init_msg;
3722 /* The payload looks like:
3724 * uint8 proto[proto_len];
3725 * uint8 padding_len;
3726 * uint8 padding[padding_len];
3729 if (proto_len + 2 > s->init_num)
3731 padding_len = p[proto_len + 1];
3732 if (proto_len + padding_len + 2 != s->init_num)
3735 s->next_proto_negotiated = OPENSSL_malloc(proto_len);
3736 if (!s->next_proto_negotiated)
3738 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,ERR_R_MALLOC_FAILURE);
3741 memcpy(s->next_proto_negotiated, p + 1, proto_len);
3742 s->next_proto_negotiated_len = proto_len;
3748 int tls1_send_server_supplemental_data(SSL *s, int *skip)
3751 if (s->ctx->srv_supp_data_records_count)
3753 unsigned char *p = NULL;
3754 unsigned char *size_loc = NULL;
3755 srv_supp_data_record *record = NULL;
3759 for (i = 0; i < s->ctx->srv_supp_data_records_count; i++)
3761 const unsigned char *out = NULL;
3762 unsigned short outlen = 0;
3764 record = &s->ctx->srv_supp_data_records[i];
3766 /* NULL callback or -1 omits supp data entry */
3769 cb_retval = record->fn1(s, record->supp_data_type,
3770 &out, &outlen, &al, record->arg);
3771 if (cb_retval == -1)
3772 continue; /* skip this supp data entry */
3775 SSLerr(SSL_F_TLS1_SEND_SERVER_SUPPLEMENTAL_DATA,ERR_R_BUF_LIB);
3778 if (outlen == 0 || TLSEXT_MAXLEN_supplemental_data < outlen + 4 + length)
3780 SSLerr(SSL_F_TLS1_SEND_SERVER_SUPPLEMENTAL_DATA,ERR_R_BUF_LIB);
3783 /* write supp data entry...
3784 * if first entry, write handshake message type
3785 * jump back to write length at end */
3788 /* 1 byte message type + 3 bytes for
3790 if (!BUF_MEM_grow_clean(s->init_buf, 4))
3792 SSLerr(SSL_F_TLS1_SEND_SERVER_SUPPLEMENTAL_DATA,ERR_R_BUF_LIB);
3795 p = (unsigned char *)s->init_buf->data;
3796 *(p++) = SSL3_MT_SUPPLEMENTAL_DATA;
3797 /* hold on to length field to update later */
3799 /* skip over handshake length field (3
3800 * bytes) and supp_data length field
3805 /* 2 byte supp data type + 2 byte length + outlen */
3806 if (!BUF_MEM_grow(s->init_buf, outlen + 4))
3808 SSLerr(SSL_F_TLS1_SEND_SERVER_SUPPLEMENTAL_DATA,ERR_R_BUF_LIB);
3811 s2n(record->supp_data_type, p);
3813 memcpy(p, out, outlen);
3814 /* update length to supp data type (2 bytes) +
3815 * supp data length (2 bytes) + supp data */
3816 length += (outlen + 4);
3821 /* write handshake length */
3822 l2n3(length - 4, size_loc);
3823 /* supp_data length */
3824 l2n3(length - 7, size_loc);
3825 s->state = SSL3_ST_SW_SUPPLEMENTAL_DATA_B;
3826 s->init_num = length;
3829 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3833 /* no supp data message sent */
3839 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3843 int tls1_get_client_supplemental_data(SSL *s)
3849 const unsigned char *p, *d;
3850 unsigned short supp_data_entry_type = 0;
3851 unsigned short supp_data_entry_len = 0;
3852 unsigned long supp_data_len = 0;
3855 n=s->method->ssl_get_message(s,
3856 SSL3_ST_SR_SUPPLEMENTAL_DATA_A,
3857 SSL3_ST_SR_SUPPLEMENTAL_DATA_B,
3858 SSL3_MT_SUPPLEMENTAL_DATA,
3859 /* use default limit */
3860 TLSEXT_MAXLEN_supplemental_data,
3863 if (!ok) return((int)n);
3865 p = (unsigned char *)s->init_msg;
3868 /* The message cannot be empty */
3871 al = SSL_AD_DECODE_ERROR;
3872 SSLerr(SSL_F_TLS1_GET_CLIENT_SUPPLEMENTAL_DATA,SSL_R_LENGTH_MISMATCH);
3875 n2l3(p, supp_data_len);
3876 while (p<d+supp_data_len)
3878 n2s(p, supp_data_entry_type);
3879 n2s(p, supp_data_entry_len);
3880 /* if there is a callback for this supp data type, send it */
3881 for (i=0; i < s->ctx->srv_supp_data_records_count; i++)
3883 if (s->ctx->srv_supp_data_records[i].supp_data_type == supp_data_entry_type && s->ctx->srv_supp_data_records[i].fn2)
3885 cb_retval = s->ctx->srv_supp_data_records[i].fn2(s, supp_data_entry_type, p, supp_data_entry_len, &al, s->ctx->srv_supp_data_records[i].arg);
3888 SSLerr(SSL_F_TLS1_GET_CLIENT_SUPPLEMENTAL_DATA, ERR_R_SSL_LIB);
3893 p+=supp_data_entry_len;
3897 ssl3_send_alert(s,SSL3_AL_FATAL,al);