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 s
->state
= SSL3_ST_SW_CERT_A
;
416 case SSL3_ST_SW_CERT_A
:
417 case SSL3_ST_SW_CERT_B
:
418 /* Check if it is anon DH or anon ECDH, */
419 /* normal PSK or KRB5 or SRP */
420 if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
|SSL_aKRB5
|SSL_aSRP
))
421 && !(s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kPSK
))
423 ret
=ssl3_send_server_certificate(s
);
424 if (ret
<= 0) goto end
;
425 #ifndef OPENSSL_NO_TLSEXT
426 if (s
->tlsext_status_expected
)
427 s
->state
=SSL3_ST_SW_CERT_STATUS_A
;
429 s
->state
=SSL3_ST_SW_KEY_EXCH_A
;
434 s
->state
=SSL3_ST_SW_KEY_EXCH_A
;
441 s
->state
=SSL3_ST_SW_KEY_EXCH_A
;
446 case SSL3_ST_SW_KEY_EXCH_A
:
447 case SSL3_ST_SW_KEY_EXCH_B
:
448 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
450 /* clear this, it may get reset by
451 * send_server_key_exchange */
452 if ((s
->options
& SSL_OP_EPHEMERAL_RSA
)
453 #ifndef OPENSSL_NO_KRB5
454 && !(alg_k
& SSL_kKRB5
)
455 #endif /* OPENSSL_NO_KRB5 */
457 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
458 * even when forbidden by protocol specs
459 * (handshake may fail as clients are not required to
460 * be able to handle this) */
461 s
->s3
->tmp
.use_rsa_tmp
=1;
463 s
->s3
->tmp
.use_rsa_tmp
=0;
466 /* only send if a DH key exchange, fortezza or
467 * RSA but we have a sign only certificate
469 * PSK: may send PSK identity hints
471 * For ECC ciphersuites, we send a serverKeyExchange
472 * message only if the cipher suite is either
473 * ECDH-anon or ECDHE. In other cases, the
474 * server certificate contains the server's
475 * public key for key exchange.
477 if (s
->s3
->tmp
.use_rsa_tmp
478 /* PSK: send ServerKeyExchange if PSK identity
479 * hint if provided */
480 #ifndef OPENSSL_NO_PSK
481 || ((alg_k
& SSL_kPSK
) && s
->ctx
->psk_identity_hint
)
483 #ifndef OPENSSL_NO_SRP
484 /* SRP: send ServerKeyExchange */
485 || (alg_k
& SSL_kSRP
)
487 || (alg_k
& SSL_kDHE
)
488 || (alg_k
& SSL_kECDHE
)
489 || ((alg_k
& SSL_kRSA
)
490 && (s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
== NULL
491 || (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
)
492 && EVP_PKEY_size(s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
)*8 > SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
)
498 ret
=ssl3_send_server_key_exchange(s
);
499 if (ret
<= 0) goto end
;
504 s
->state
=SSL3_ST_SW_CERT_REQ_A
;
508 case SSL3_ST_SW_CERT_REQ_A
:
509 case SSL3_ST_SW_CERT_REQ_B
:
510 if (/* don't request cert unless asked for it: */
511 !(s
->verify_mode
& SSL_VERIFY_PEER
) ||
512 /* if SSL_VERIFY_CLIENT_ONCE is set,
513 * don't request cert during re-negotiation: */
514 ((s
->session
->peer
!= NULL
) &&
515 (s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
)) ||
516 /* never request cert in anonymous ciphersuites
517 * (see section "Certificate request" in SSL 3 drafts
518 * and in RFC 2246): */
519 ((s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
) &&
520 /* ... except when the application insists on verification
521 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
522 !(s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)) ||
523 /* never request cert in Kerberos ciphersuites */
524 (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aKRB5
) ||
525 /* don't request certificate for SRP auth */
526 (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aSRP
)
527 /* With normal PSK Certificates and
528 * Certificate Requests are omitted */
529 || (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kPSK
))
531 /* no cert request */
533 s
->s3
->tmp
.cert_request
=0;
534 s
->state
=SSL3_ST_SW_SRVR_DONE_A
;
535 if (s
->s3
->handshake_buffer
)
536 if (!ssl3_digest_cached_records(s
))
541 s
->s3
->tmp
.cert_request
=1;
542 ret
=ssl3_send_certificate_request(s
);
543 if (ret
<= 0) goto end
;
544 #ifndef NETSCAPE_HANG_BUG
545 s
->state
=SSL3_ST_SW_SRVR_DONE_A
;
547 s
->state
=SSL3_ST_SW_FLUSH
;
548 s
->s3
->tmp
.next_state
=SSL3_ST_SR_CERT_A
;
554 case SSL3_ST_SW_SRVR_DONE_A
:
555 case SSL3_ST_SW_SRVR_DONE_B
:
556 ret
=ssl3_send_server_done(s
);
557 if (ret
<= 0) goto end
;
558 s
->s3
->tmp
.next_state
=SSL3_ST_SR_CERT_A
;
559 s
->state
=SSL3_ST_SW_FLUSH
;
563 case SSL3_ST_SW_FLUSH
:
565 /* This code originally checked to see if
566 * any data was pending using BIO_CTRL_INFO
567 * and then flushed. This caused problems
568 * as documented in PR#1939. The proposed
569 * fix doesn't completely resolve this issue
570 * as buggy implementations of BIO_CTRL_PENDING
571 * still exist. So instead we just flush
575 s
->rwstate
=SSL_WRITING
;
576 if (BIO_flush(s
->wbio
) <= 0)
581 s
->rwstate
=SSL_NOTHING
;
583 s
->state
=s
->s3
->tmp
.next_state
;
586 case SSL3_ST_SR_CERT_A
:
587 case SSL3_ST_SR_CERT_B
:
588 /* Check for second client hello (MS SGC) */
589 ret
= ssl3_check_client_hello(s
);
593 s
->state
= SSL3_ST_SR_CLNT_HELLO_C
;
595 if (s
->s3
->tmp
.cert_request
)
597 ret
=ssl3_get_client_certificate(s
);
598 if (ret
<= 0) goto end
;
601 s
->state
=SSL3_ST_SR_KEY_EXCH_A
;
605 case SSL3_ST_SR_KEY_EXCH_A
:
606 case SSL3_ST_SR_KEY_EXCH_B
:
607 ret
=ssl3_get_client_key_exchange(s
);
612 /* For the ECDH ciphersuites when
613 * the client sends its ECDH pub key in
614 * a certificate, the CertificateVerify
615 * message is not sent.
616 * Also for GOST ciphersuites when
617 * the client uses its key from the certificate
620 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
621 s
->state
=SSL3_ST_SR_FINISHED_A
;
623 if (s
->s3
->next_proto_neg_seen
)
624 s
->state
=SSL3_ST_SR_NEXT_PROTO_A
;
626 s
->state
=SSL3_ST_SR_FINISHED_A
;
630 else if (SSL_USE_SIGALGS(s
))
632 s
->state
=SSL3_ST_SR_CERT_VRFY_A
;
634 if (!s
->session
->peer
)
636 /* For sigalgs freeze the handshake buffer
637 * at this point and digest cached records.
639 if (!s
->s3
->handshake_buffer
)
641 SSLerr(SSL_F_SSL3_ACCEPT
,ERR_R_INTERNAL_ERROR
);
644 s
->s3
->flags
|= TLS1_FLAGS_KEEP_HANDSHAKE
;
645 if (!ssl3_digest_cached_records(s
))
653 s
->state
=SSL3_ST_SR_CERT_VRFY_A
;
656 /* We need to get hashes here so if there is
657 * a client cert, it can be verified
658 * FIXME - digest processing for CertificateVerify
659 * should be generalized. But it is next step
661 if (s
->s3
->handshake_buffer
)
662 if (!ssl3_digest_cached_records(s
))
664 for (dgst_num
=0; dgst_num
<SSL_MAX_DIGEST
;dgst_num
++)
665 if (s
->s3
->handshake_dgst
[dgst_num
])
669 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
]));
670 dgst_size
=EVP_MD_CTX_size(s
->s3
->handshake_dgst
[dgst_num
]);
681 case SSL3_ST_SR_CERT_VRFY_A
:
682 case SSL3_ST_SR_CERT_VRFY_B
:
684 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
685 /* we should decide if we expected this one */
686 ret
=ssl3_get_cert_verify(s
);
687 if (ret
<= 0) goto end
;
689 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
690 s
->state
=SSL3_ST_SR_FINISHED_A
;
692 if (s
->s3
->next_proto_neg_seen
)
693 s
->state
=SSL3_ST_SR_NEXT_PROTO_A
;
695 s
->state
=SSL3_ST_SR_FINISHED_A
;
700 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
701 case SSL3_ST_SR_NEXT_PROTO_A
:
702 case SSL3_ST_SR_NEXT_PROTO_B
:
703 ret
=ssl3_get_next_proto(s
);
704 if (ret
<= 0) goto end
;
706 s
->state
=SSL3_ST_SR_FINISHED_A
;
710 case SSL3_ST_SR_FINISHED_A
:
711 case SSL3_ST_SR_FINISHED_B
:
712 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
713 ret
=ssl3_get_finished(s
,SSL3_ST_SR_FINISHED_A
,
714 SSL3_ST_SR_FINISHED_B
);
715 if (ret
<= 0) goto end
;
718 #ifndef OPENSSL_NO_TLSEXT
719 else if (s
->tlsext_ticket_expected
)
720 s
->state
=SSL3_ST_SW_SESSION_TICKET_A
;
723 s
->state
=SSL3_ST_SW_CHANGE_A
;
727 #ifndef OPENSSL_NO_TLSEXT
728 case SSL3_ST_SW_SESSION_TICKET_A
:
729 case SSL3_ST_SW_SESSION_TICKET_B
:
730 ret
=ssl3_send_newsession_ticket(s
);
731 if (ret
<= 0) goto end
;
732 s
->state
=SSL3_ST_SW_CHANGE_A
;
736 case SSL3_ST_SW_CERT_STATUS_A
:
737 case SSL3_ST_SW_CERT_STATUS_B
:
738 ret
=ssl3_send_cert_status(s
);
739 if (ret
<= 0) goto end
;
740 s
->state
=SSL3_ST_SW_KEY_EXCH_A
;
746 case SSL3_ST_SW_CHANGE_A
:
747 case SSL3_ST_SW_CHANGE_B
:
749 s
->session
->cipher
=s
->s3
->tmp
.new_cipher
;
750 if (!s
->method
->ssl3_enc
->setup_key_block(s
))
751 { ret
= -1; goto end
; }
753 ret
=ssl3_send_change_cipher_spec(s
,
754 SSL3_ST_SW_CHANGE_A
,SSL3_ST_SW_CHANGE_B
);
756 if (ret
<= 0) goto end
;
757 s
->state
=SSL3_ST_SW_FINISHED_A
;
760 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
761 SSL3_CHANGE_CIPHER_SERVER_WRITE
))
769 case SSL3_ST_SW_FINISHED_A
:
770 case SSL3_ST_SW_FINISHED_B
:
771 ret
=ssl3_send_finished(s
,
772 SSL3_ST_SW_FINISHED_A
,SSL3_ST_SW_FINISHED_B
,
773 s
->method
->ssl3_enc
->server_finished_label
,
774 s
->method
->ssl3_enc
->server_finished_label_len
);
775 if (ret
<= 0) goto end
;
776 s
->state
=SSL3_ST_SW_FLUSH
;
779 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
780 s
->s3
->tmp
.next_state
=SSL3_ST_SR_FINISHED_A
;
782 if (s
->s3
->next_proto_neg_seen
)
784 s
->s3
->flags
|= SSL3_FLAGS_CCS_OK
;
785 s
->s3
->tmp
.next_state
=SSL3_ST_SR_NEXT_PROTO_A
;
788 s
->s3
->tmp
.next_state
=SSL3_ST_SR_FINISHED_A
;
792 s
->s3
->tmp
.next_state
=SSL_ST_OK
;
797 /* clean a few things up */
798 ssl3_cleanup_key_block(s
);
800 BUF_MEM_free(s
->init_buf
);
803 /* remove buffering on output */
804 ssl_free_wbio_buffer(s
);
808 if (s
->renegotiate
== 2) /* skipped if we just sent a HelloRequest */
813 ssl_update_cache(s
,SSL_SESS_CACHE_SERVER
);
815 s
->ctx
->stats
.sess_accept_good
++;
817 s
->handshake_func
=ssl3_accept
;
819 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_DONE
,1);
827 SSLerr(SSL_F_SSL3_ACCEPT
,SSL_R_UNKNOWN_STATE
);
833 if (!s
->s3
->tmp
.reuse_message
&& !skip
)
837 if ((ret
=BIO_flush(s
->wbio
)) <= 0)
842 if ((cb
!= NULL
) && (s
->state
!= state
))
846 cb(s
,SSL_CB_ACCEPT_LOOP
,1);
853 /* BIO_flush(s->wbio); */
857 cb(s
,SSL_CB_ACCEPT_EXIT
,ret
);
861 int ssl3_send_hello_request(SSL
*s
)
864 if (s
->state
== SSL3_ST_SW_HELLO_REQ_A
)
866 ssl_set_handshake_header(s
, SSL3_MT_HELLO_REQUEST
, 0);
867 s
->state
=SSL3_ST_SW_HELLO_REQ_B
;
870 /* SSL3_ST_SW_HELLO_REQ_B */
871 return ssl_do_write(s
);
874 int ssl3_check_client_hello(SSL
*s
)
879 /* this function is called when we really expect a Certificate message,
880 * so permit appropriate message length */
881 n
=s
->method
->ssl_get_message(s
,
887 if (!ok
) return((int)n
);
888 s
->s3
->tmp
.reuse_message
= 1;
889 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_HELLO
)
891 /* We only allow the client to restart the handshake once per
893 if (s
->s3
->flags
& SSL3_FLAGS_SGC_RESTART_DONE
)
895 SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO
, SSL_R_MULTIPLE_SGC_RESTARTS
);
898 /* Throw away what we have done so far in the current handshake,
899 * which will now be aborted. (A full SSL_clear would be too much.) */
900 #ifndef OPENSSL_NO_DH
901 if (s
->s3
->tmp
.dh
!= NULL
)
903 DH_free(s
->s3
->tmp
.dh
);
904 s
->s3
->tmp
.dh
= NULL
;
907 #ifndef OPENSSL_NO_ECDH
908 if (s
->s3
->tmp
.ecdh
!= NULL
)
910 EC_KEY_free(s
->s3
->tmp
.ecdh
);
911 s
->s3
->tmp
.ecdh
= NULL
;
914 s
->s3
->flags
|= SSL3_FLAGS_SGC_RESTART_DONE
;
920 int ssl3_get_client_hello(SSL
*s
)
922 int i
,j
,ok
,al
=SSL_AD_INTERNAL_ERROR
,ret
= -1;
923 unsigned int cookie_len
;
928 #ifndef OPENSSL_NO_COMP
932 STACK_OF(SSL_CIPHER
) *ciphers
=NULL
;
934 if (s
->state
== SSL3_ST_SR_CLNT_HELLO_C
&& !s
->first_packet
)
937 /* We do this so that we will respond with our native type.
938 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
939 * This down switching should be handled by a different method.
940 * If we are SSLv3, we will respond with SSLv3, even if prompted with
943 if (s
->state
== SSL3_ST_SR_CLNT_HELLO_A
946 s
->state
=SSL3_ST_SR_CLNT_HELLO_B
;
949 n
=s
->method
->ssl_get_message(s
,
950 SSL3_ST_SR_CLNT_HELLO_B
,
951 SSL3_ST_SR_CLNT_HELLO_C
,
952 SSL3_MT_CLIENT_HELLO
,
953 SSL3_RT_MAX_PLAIN_LENGTH
,
956 if (!ok
) return((int)n
);
958 d
=p
=(unsigned char *)s
->init_msg
;
960 /* use version from inside client hello, not from record header
961 * (may differ: see RFC 2246, Appendix E, second paragraph) */
962 s
->client_version
=(((int)p
[0])<<8)|(int)p
[1];
965 if (SSL_IS_DTLS(s
) ? (s
->client_version
> s
->version
&&
966 s
->method
->version
!= DTLS_ANY_VERSION
)
967 : (s
->client_version
< s
->version
))
969 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_WRONG_VERSION_NUMBER
);
970 if ((s
->client_version
>>8) == SSL3_VERSION_MAJOR
&&
971 !s
->enc_write_ctx
&& !s
->write_hash
)
973 /* similar to ssl3_get_record, send alert using remote version number */
974 s
->version
= s
->client_version
;
976 al
= SSL_AD_PROTOCOL_VERSION
;
980 /* If we require cookies and this ClientHello doesn't
981 * contain one, just return since we do not want to
982 * allocate any memory yet. So check cookie length...
984 if (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
)
986 unsigned int session_length
, cookie_length
;
988 session_length
= *(p
+ SSL3_RANDOM_SIZE
);
989 cookie_length
= *(p
+ SSL3_RANDOM_SIZE
+ session_length
+ 1);
991 if (cookie_length
== 0)
995 /* load the client random */
996 memcpy(s
->s3
->client_random
,p
,SSL3_RANDOM_SIZE
);
999 /* get the session-id */
1003 /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation.
1004 * 0.9.7 and later allow this by default, but optionally ignore resumption requests
1005 * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1006 * than a change to default behavior so that applications relying on this for security
1007 * won't even compile against older library versions).
1009 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request
1010 * renegotiation but not a new session (s->new_session remains unset): for servers,
1011 * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1012 * setting will be ignored.
1014 if ((s
->new_session
&& (s
->options
& SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
)))
1016 if (!ssl_get_new_session(s
,1))
1021 i
=ssl_get_prev_session(s
, p
, j
, d
+ n
);
1023 { /* previous session */
1030 if (!ssl_get_new_session(s
,1))
1040 cookie_len
= *(p
++);
1043 * The ClientHello may contain a cookie even if the
1044 * HelloVerify message has not been sent--make sure that it
1045 * does not cause an overflow.
1047 if ( cookie_len
> sizeof(s
->d1
->rcvd_cookie
))
1050 al
= SSL_AD_DECODE_ERROR
;
1051 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_COOKIE_MISMATCH
);
1055 /* verify the cookie if appropriate option is set. */
1056 if ((SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
) &&
1059 memcpy(s
->d1
->rcvd_cookie
, p
, cookie_len
);
1061 if ( s
->ctx
->app_verify_cookie_cb
!= NULL
)
1063 if ( s
->ctx
->app_verify_cookie_cb(s
, s
->d1
->rcvd_cookie
,
1066 al
=SSL_AD_HANDSHAKE_FAILURE
;
1067 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,
1068 SSL_R_COOKIE_MISMATCH
);
1071 /* else cookie verification succeeded */
1073 else if ( memcmp(s
->d1
->rcvd_cookie
, s
->d1
->cookie
,
1074 s
->d1
->cookie_len
) != 0) /* default verification */
1076 al
=SSL_AD_HANDSHAKE_FAILURE
;
1077 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,
1078 SSL_R_COOKIE_MISMATCH
);
1081 /* Set to -2 so if successful we return 2 */
1086 if (s
->method
->version
== DTLS_ANY_VERSION
)
1088 /* Select version to use */
1089 if (s
->client_version
<= DTLS1_2_VERSION
&&
1090 !(s
->options
& SSL_OP_NO_DTLSv1_2
))
1092 s
->version
= DTLS1_2_VERSION
;
1093 s
->method
= DTLSv1_2_server_method();
1095 else if (tls1_suiteb(s
))
1097 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE
);
1098 s
->version
= s
->client_version
;
1099 al
= SSL_AD_PROTOCOL_VERSION
;
1102 else if (s
->client_version
<= DTLS1_VERSION
&&
1103 !(s
->options
& SSL_OP_NO_DTLSv1
))
1105 s
->version
= DTLS1_VERSION
;
1106 s
->method
= DTLSv1_server_method();
1110 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_WRONG_VERSION_NUMBER
);
1111 s
->version
= s
->client_version
;
1112 al
= SSL_AD_PROTOCOL_VERSION
;
1115 s
->session
->ssl_version
= s
->version
;
1120 if ((i
== 0) && (j
!= 0))
1122 /* we need a cipher if we are not resuming a session */
1123 al
=SSL_AD_ILLEGAL_PARAMETER
;
1124 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_CIPHERS_SPECIFIED
);
1129 /* not enough data */
1130 al
=SSL_AD_DECODE_ERROR
;
1131 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
1134 if ((i
> 0) && (ssl_bytes_to_cipher_list(s
,p
,i
,&(ciphers
))
1141 /* If it is a hit, check that the cipher is in the list */
1142 if ((s
->hit
) && (i
> 0))
1145 id
=s
->session
->cipher
->id
;
1148 printf("client sent %d ciphers\n",sk_num(ciphers
));
1150 for (i
=0; i
<sk_SSL_CIPHER_num(ciphers
); i
++)
1152 c
=sk_SSL_CIPHER_value(ciphers
,i
);
1154 printf("client [%2d of %2d]:%s\n",
1155 i
,sk_num(ciphers
),SSL_CIPHER_get_name(c
));
1163 /* Disabled because it can be used in a ciphersuite downgrade
1164 * attack: CVE-2010-4180.
1167 if (j
== 0 && (s
->options
& SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
) && (sk_SSL_CIPHER_num(ciphers
) == 1))
1169 /* Special case as client bug workaround: the previously used cipher may
1170 * not be in the current list, the client instead might be trying to
1171 * continue using a cipher that before wasn't chosen due to server
1172 * preferences. We'll have to reject the connection if the cipher is not
1173 * enabled, though. */
1174 c
= sk_SSL_CIPHER_value(ciphers
, 0);
1175 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s
), c
) >= 0)
1177 s
->session
->cipher
= c
;
1184 /* we need to have the cipher in the cipher
1185 * list if we are asked to reuse it */
1186 al
=SSL_AD_ILLEGAL_PARAMETER
;
1187 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_REQUIRED_CIPHER_MISSING
);
1196 /* not enough data */
1197 al
=SSL_AD_DECODE_ERROR
;
1198 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_LENGTH_MISMATCH
);
1201 #ifndef OPENSSL_NO_COMP
1206 if (p
[j
] == 0) break;
1213 al
=SSL_AD_DECODE_ERROR
;
1214 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_COMPRESSION_SPECIFIED
);
1218 #ifndef OPENSSL_NO_TLSEXT
1220 if (s
->version
>= SSL3_VERSION
)
1222 if (!ssl_parse_clienthello_tlsext(s
,&p
,d
,n
))
1224 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_PARSE_TLSEXT
);
1229 /* Check if we want to use external pre-shared secret for this
1230 * handshake for not reused session only. We need to generate
1231 * server_random before calling tls_session_secret_cb in order to allow
1232 * SessionTicket processing to use it in key derivation. */
1235 pos
=s
->s3
->server_random
;
1236 if (ssl_fill_hello_random(s
, 1, pos
, SSL3_RANDOM_SIZE
) <= 0)
1242 if (!s
->hit
&& s
->version
>= TLS1_VERSION
&& s
->tls_session_secret_cb
)
1244 SSL_CIPHER
*pref_cipher
=NULL
;
1246 s
->session
->master_key_length
=sizeof(s
->session
->master_key
);
1247 if(s
->tls_session_secret_cb(s
, s
->session
->master_key
, &s
->session
->master_key_length
,
1248 ciphers
, &pref_cipher
, s
->tls_session_secret_cb_arg
))
1251 s
->session
->ciphers
=ciphers
;
1252 s
->session
->verify_result
=X509_V_OK
;
1256 /* check if some cipher was preferred by call back */
1257 pref_cipher
=pref_cipher
? pref_cipher
: ssl3_choose_cipher(s
, s
->session
->ciphers
, SSL_get_ciphers(s
));
1258 if (pref_cipher
== NULL
)
1260 al
=SSL_AD_HANDSHAKE_FAILURE
;
1261 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_SHARED_CIPHER
);
1265 s
->session
->cipher
=pref_cipher
;
1268 sk_SSL_CIPHER_free(s
->cipher_list
);
1270 if (s
->cipher_list_by_id
)
1271 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1273 s
->cipher_list
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1274 s
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->session
->ciphers
);
1279 /* Worst case, we will use the NULL compression, but if we have other
1280 * options, we will now look for them. We have i-1 compression
1281 * algorithms from the client, starting at q. */
1282 s
->s3
->tmp
.new_compression
=NULL
;
1283 #ifndef OPENSSL_NO_COMP
1284 /* This only happens if we have a cache hit */
1285 if (s
->session
->compress_meth
!= 0)
1287 int m
, comp_id
= s
->session
->compress_meth
;
1288 /* Perform sanity checks on resumed compression algorithm */
1289 /* Can't disable compression */
1290 if (!ssl_allow_compression(s
))
1292 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_INCONSISTENT_COMPRESSION
);
1295 /* Look for resumed compression method */
1296 for (m
= 0; m
< sk_SSL_COMP_num(s
->ctx
->comp_methods
); m
++)
1298 comp
=sk_SSL_COMP_value(s
->ctx
->comp_methods
,m
);
1299 if (comp_id
== comp
->id
)
1301 s
->s3
->tmp
.new_compression
=comp
;
1305 if (s
->s3
->tmp
.new_compression
== NULL
)
1307 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_INVALID_COMPRESSION_ALGORITHM
);
1310 /* Look for resumed method in compression list */
1311 for (m
= 0; m
< i
; m
++)
1313 if (q
[m
] == comp_id
)
1318 al
=SSL_AD_ILLEGAL_PARAMETER
;
1319 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING
);
1325 else if (ssl_allow_compression(s
) && s
->ctx
->comp_methods
)
1326 { /* See if we have a match */
1327 int m
,nn
,o
,v
,done
=0;
1329 nn
=sk_SSL_COMP_num(s
->ctx
->comp_methods
);
1330 for (m
=0; m
<nn
; m
++)
1332 comp
=sk_SSL_COMP_value(s
->ctx
->comp_methods
,m
);
1345 s
->s3
->tmp
.new_compression
=comp
;
1350 /* If compression is disabled we'd better not try to resume a session
1351 * using compression.
1353 if (s
->session
->compress_meth
!= 0)
1355 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_INCONSISTENT_COMPRESSION
);
1360 /* Given s->session->ciphers and SSL_get_ciphers, we must
1365 #ifdef OPENSSL_NO_COMP
1366 s
->session
->compress_meth
=0;
1368 s
->session
->compress_meth
=(comp
== NULL
)?0:comp
->id
;
1370 if (s
->session
->ciphers
!= NULL
)
1371 sk_SSL_CIPHER_free(s
->session
->ciphers
);
1372 s
->session
->ciphers
=ciphers
;
1373 if (ciphers
== NULL
)
1375 al
=SSL_AD_ILLEGAL_PARAMETER
;
1376 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_CIPHERS_PASSED
);
1380 /* Let cert callback update server certificates if required */
1382 if (s
->cert
->cert_cb
)
1384 int rv
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
1387 al
=SSL_AD_INTERNAL_ERROR
;
1388 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_CERT_CB_ERROR
);
1393 s
->rwstate
=SSL_X509_LOOKUP
;
1396 s
->rwstate
= SSL_NOTHING
;
1398 c
=ssl3_choose_cipher(s
,s
->session
->ciphers
,
1399 SSL_get_ciphers(s
));
1403 al
=SSL_AD_HANDSHAKE_FAILURE
;
1404 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
,SSL_R_NO_SHARED_CIPHER
);
1407 s
->s3
->tmp
.new_cipher
=c
;
1408 /* check whether we should disable session resumption */
1409 if (s
->not_resumable_session_cb
!= NULL
)
1410 s
->session
->not_resumable
=s
->not_resumable_session_cb(s
,
1411 ((c
->algorithm_mkey
& (SSL_kDHE
| SSL_kECDHE
)) != 0));
1412 if (s
->session
->not_resumable
)
1413 /* do not send a session ticket */
1414 s
->tlsext_ticket_expected
= 0;
1418 /* Session-id reuse */
1419 #ifdef REUSE_CIPHER_BUG
1420 STACK_OF(SSL_CIPHER
) *sk
;
1421 SSL_CIPHER
*nc
=NULL
;
1422 SSL_CIPHER
*ec
=NULL
;
1424 if (s
->options
& SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG
)
1426 sk
=s
->session
->ciphers
;
1427 for (i
=0; i
<sk_SSL_CIPHER_num(sk
); i
++)
1429 c
=sk_SSL_CIPHER_value(sk
,i
);
1430 if (c
->algorithm_enc
& SSL_eNULL
)
1432 if (SSL_C_IS_EXPORT(c
))
1436 s
->s3
->tmp
.new_cipher
=nc
;
1437 else if (ec
!= NULL
)
1438 s
->s3
->tmp
.new_cipher
=ec
;
1440 s
->s3
->tmp
.new_cipher
=s
->session
->cipher
;
1444 s
->s3
->tmp
.new_cipher
=s
->session
->cipher
;
1447 if (!SSL_USE_SIGALGS(s
) || !(s
->verify_mode
& SSL_VERIFY_PEER
))
1449 if (!ssl3_digest_cached_records(s
))
1453 /* we now have the following setup.
1455 * cipher_list - our prefered list of ciphers
1456 * ciphers - the clients prefered list of ciphers
1457 * compression - basically ignored right now
1458 * ssl version is set - sslv3
1459 * s->session - The ssl session has been setup.
1460 * s->hit - session reuse flag
1461 * s->s3->tmp.new_cipher- the new cipher to use.
1464 /* Handles TLS extensions that we couldn't check earlier */
1465 if (s
->version
>= SSL3_VERSION
)
1467 if (ssl_check_clienthello_tlsext_late(s
) <= 0)
1469 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
1474 if (ret
< 0) ret
=-ret
;
1478 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
1481 if (ciphers
!= NULL
) sk_SSL_CIPHER_free(ciphers
);
1482 return ret
< 0 ? -1 : ret
;
1485 int ssl3_send_server_hello(SSL
*s
)
1488 unsigned char *p
,*d
;
1493 if (s
->state
== SSL3_ST_SW_SRVR_HELLO_A
)
1495 buf
=(unsigned char *)s
->init_buf
->data
;
1496 #ifdef OPENSSL_NO_TLSEXT
1497 p
=s
->s3
->server_random
;
1498 if (ssl_fill_hello_random(s
, 1, p
, SSL3_RANDOM_SIZE
) <= 0)
1501 /* Do the message type and length last */
1502 d
=p
= ssl_handshake_start(s
);
1504 *(p
++)=s
->version
>>8;
1505 *(p
++)=s
->version
&0xff;
1508 memcpy(p
,s
->s3
->server_random
,SSL3_RANDOM_SIZE
);
1509 p
+=SSL3_RANDOM_SIZE
;
1511 /* There are several cases for the session ID to send
1512 * back in the server hello:
1513 * - For session reuse from the session cache,
1514 * we send back the old session ID.
1515 * - If stateless session reuse (using a session ticket)
1516 * is successful, we send back the client's "session ID"
1517 * (which doesn't actually identify the session).
1518 * - If it is a new session, we send back the new
1520 * - However, if we want the new session to be single-use,
1521 * we send back a 0-length session ID.
1522 * s->hit is non-zero in either case of session reuse,
1523 * so the following won't overwrite an ID that we're supposed
1526 if (s
->session
->not_resumable
||
1527 (!(s
->ctx
->session_cache_mode
& SSL_SESS_CACHE_SERVER
)
1529 s
->session
->session_id_length
=0;
1531 sl
=s
->session
->session_id_length
;
1532 if (sl
> (int)sizeof(s
->session
->session_id
))
1534 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
1538 memcpy(p
,s
->session
->session_id
,sl
);
1541 /* put the cipher */
1542 i
=ssl3_put_cipher_by_char(s
->s3
->tmp
.new_cipher
,p
);
1545 /* put the compression method */
1546 #ifdef OPENSSL_NO_COMP
1549 if (s
->s3
->tmp
.new_compression
== NULL
)
1552 *(p
++)=s
->s3
->tmp
.new_compression
->id
;
1554 #ifndef OPENSSL_NO_TLSEXT
1555 if (ssl_prepare_serverhello_tlsext(s
) <= 0)
1557 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO
,SSL_R_SERVERHELLO_TLSEXT
);
1560 if ((p
= ssl_add_serverhello_tlsext(s
, p
, buf
+SSL3_RT_MAX_PLAIN_LENGTH
, &al
)) == NULL
)
1562 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1563 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO
,ERR_R_INTERNAL_ERROR
);
1569 ssl_set_handshake_header(s
, SSL3_MT_SERVER_HELLO
, l
);
1570 s
->state
=SSL3_ST_SW_SRVR_HELLO_B
;
1573 /* SSL3_ST_SW_SRVR_HELLO_B */
1574 return ssl_do_write(s
);
1577 int ssl3_send_server_done(SSL
*s
)
1580 if (s
->state
== SSL3_ST_SW_SRVR_DONE_A
)
1582 ssl_set_handshake_header(s
, SSL3_MT_SERVER_DONE
, 0);
1583 s
->state
= SSL3_ST_SW_SRVR_DONE_B
;
1586 /* SSL3_ST_SW_SRVR_DONE_B */
1587 return ssl_do_write(s
);
1590 int ssl3_send_server_key_exchange(SSL
*s
)
1592 #ifndef OPENSSL_NO_RSA
1596 unsigned char md_buf
[MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
];
1599 #ifndef OPENSSL_NO_DH
1602 #ifndef OPENSSL_NO_ECDH
1603 EC_KEY
*ecdh
=NULL
, *ecdhp
;
1604 unsigned char *encodedPoint
= NULL
;
1607 BN_CTX
*bn_ctx
= NULL
;
1610 const EVP_MD
*md
= NULL
;
1611 unsigned char *p
,*d
;
1621 EVP_MD_CTX_init(&md_ctx
);
1622 if (s
->state
== SSL3_ST_SW_KEY_EXCH_A
)
1624 type
=s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1629 r
[0]=r
[1]=r
[2]=r
[3]=NULL
;
1631 #ifndef OPENSSL_NO_RSA
1632 if (type
& SSL_kRSA
)
1635 if ((rsa
== NULL
) && (s
->cert
->rsa_tmp_cb
!= NULL
))
1637 rsa
=s
->cert
->rsa_tmp_cb(s
,
1638 SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
),
1639 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
));
1642 al
=SSL_AD_HANDSHAKE_FAILURE
;
1643 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_ERROR_GENERATING_TMP_RSA_KEY
);
1651 al
=SSL_AD_HANDSHAKE_FAILURE
;
1652 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_RSA_KEY
);
1657 s
->s3
->tmp
.use_rsa_tmp
=1;
1661 #ifndef OPENSSL_NO_DH
1662 if (type
& SSL_kDHE
)
1664 if (s
->cert
->dh_tmp_auto
)
1666 dhp
= ssl_get_auto_dh(s
);
1669 al
=SSL_AD_INTERNAL_ERROR
;
1670 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
1676 if ((dhp
== NULL
) && (s
->cert
->dh_tmp_cb
!= NULL
))
1677 dhp
=s
->cert
->dh_tmp_cb(s
,
1678 SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
),
1679 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
));
1682 al
=SSL_AD_HANDSHAKE_FAILURE
;
1683 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
1686 if (!ssl_security(s
, SSL_SECOP_TMP_DH
,
1687 DH_security_bits(dhp
), 0, dhp
))
1689 al
=SSL_AD_HANDSHAKE_FAILURE
;
1690 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_DH_KEY_TOO_SMALL
);
1693 if (s
->s3
->tmp
.dh
!= NULL
)
1695 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1699 if (s
->cert
->dh_tmp_auto
)
1701 else if ((dh
=DHparams_dup(dhp
)) == NULL
)
1703 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1708 if ((dhp
->pub_key
== NULL
||
1709 dhp
->priv_key
== NULL
||
1710 (s
->options
& SSL_OP_SINGLE_DH_USE
)))
1712 if(!DH_generate_key(dh
))
1714 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,
1721 dh
->pub_key
=BN_dup(dhp
->pub_key
);
1722 dh
->priv_key
=BN_dup(dhp
->priv_key
);
1723 if ((dh
->pub_key
== NULL
) ||
1724 (dh
->priv_key
== NULL
))
1726 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_DH_LIB
);
1736 #ifndef OPENSSL_NO_ECDH
1737 if (type
& SSL_kECDHE
)
1739 const EC_GROUP
*group
;
1741 ecdhp
=cert
->ecdh_tmp
;
1742 if (s
->cert
->ecdh_tmp_auto
)
1744 /* Get NID of appropriate shared curve */
1745 int nid
= tls1_shared_curve(s
, -2);
1746 if (nid
!= NID_undef
)
1747 ecdhp
= EC_KEY_new_by_curve_name(nid
);
1749 else if ((ecdhp
== NULL
) && s
->cert
->ecdh_tmp_cb
)
1751 ecdhp
=s
->cert
->ecdh_tmp_cb(s
,
1752 SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
),
1753 SSL_C_EXPORT_PKEYLENGTH(s
->s3
->tmp
.new_cipher
));
1757 al
=SSL_AD_HANDSHAKE_FAILURE
;
1758 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_TMP_ECDH_KEY
);
1762 if (s
->s3
->tmp
.ecdh
!= NULL
)
1764 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1768 /* Duplicate the ECDH structure. */
1771 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_ECDH_LIB
);
1774 if (s
->cert
->ecdh_tmp_auto
)
1776 else if ((ecdh
= EC_KEY_dup(ecdhp
)) == NULL
)
1778 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_ECDH_LIB
);
1782 s
->s3
->tmp
.ecdh
=ecdh
;
1783 if ((EC_KEY_get0_public_key(ecdh
) == NULL
) ||
1784 (EC_KEY_get0_private_key(ecdh
) == NULL
) ||
1785 (s
->options
& SSL_OP_SINGLE_ECDH_USE
))
1787 if(!EC_KEY_generate_key(ecdh
))
1789 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_ECDH_LIB
);
1794 if (((group
= EC_KEY_get0_group(ecdh
)) == NULL
) ||
1795 (EC_KEY_get0_public_key(ecdh
) == NULL
) ||
1796 (EC_KEY_get0_private_key(ecdh
) == NULL
))
1798 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_ECDH_LIB
);
1802 if (SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
) &&
1803 (EC_GROUP_get_degree(group
) > 163))
1805 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER
);
1809 /* XXX: For now, we only support ephemeral ECDH
1810 * keys over named (not generic) curves. For
1811 * supported named curves, curve_id is non-zero.
1814 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group
)))
1817 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE
);
1821 /* Encode the public key.
1822 * First check the size of encoding and
1823 * allocate memory accordingly.
1825 encodedlen
= EC_POINT_point2oct(group
,
1826 EC_KEY_get0_public_key(ecdh
),
1827 POINT_CONVERSION_UNCOMPRESSED
,
1830 encodedPoint
= (unsigned char *)
1831 OPENSSL_malloc(encodedlen
*sizeof(unsigned char));
1832 bn_ctx
= BN_CTX_new();
1833 if ((encodedPoint
== NULL
) || (bn_ctx
== NULL
))
1835 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_MALLOC_FAILURE
);
1840 encodedlen
= EC_POINT_point2oct(group
,
1841 EC_KEY_get0_public_key(ecdh
),
1842 POINT_CONVERSION_UNCOMPRESSED
,
1843 encodedPoint
, encodedlen
, bn_ctx
);
1845 if (encodedlen
== 0)
1847 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_ECDH_LIB
);
1851 BN_CTX_free(bn_ctx
); bn_ctx
=NULL
;
1853 /* XXX: For now, we only support named (not
1854 * generic) curves in ECDH ephemeral key exchanges.
1855 * In this situation, we need four additional bytes
1856 * to encode the entire ServerECDHParams
1861 /* We'll generate the serverKeyExchange message
1862 * explicitly so we can set these to NULLs
1870 #endif /* !OPENSSL_NO_ECDH */
1871 #ifndef OPENSSL_NO_PSK
1872 if (type
& SSL_kPSK
)
1874 /* reserve size for record length and PSK identity hint*/
1875 n
+=2+strlen(s
->ctx
->psk_identity_hint
);
1878 #endif /* !OPENSSL_NO_PSK */
1879 #ifndef OPENSSL_NO_SRP
1880 if (type
& SSL_kSRP
)
1882 if ((s
->srp_ctx
.N
== NULL
) ||
1883 (s
->srp_ctx
.g
== NULL
) ||
1884 (s
->srp_ctx
.s
== NULL
) ||
1885 (s
->srp_ctx
.B
== NULL
))
1887 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_MISSING_SRP_PARAM
);
1898 al
=SSL_AD_HANDSHAKE_FAILURE
;
1899 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE
);
1902 for (i
=0; i
< 4 && r
[i
] != NULL
; i
++)
1904 nr
[i
]=BN_num_bytes(r
[i
]);
1905 #ifndef OPENSSL_NO_SRP
1906 if ((i
== 2) && (type
& SSL_kSRP
))
1913 if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
|SSL_aSRP
))
1914 && !(s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kPSK
))
1916 if ((pkey
=ssl_get_sign_pkey(s
,s
->s3
->tmp
.new_cipher
,&md
))
1919 al
=SSL_AD_DECODE_ERROR
;
1922 kn
=EVP_PKEY_size(pkey
);
1930 if (!BUF_MEM_grow_clean(buf
,n
+SSL_HM_HEADER_LENGTH(s
)+kn
))
1932 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_BUF
);
1935 d
= p
= ssl_handshake_start(s
);
1937 for (i
=0; i
< 4 && r
[i
] != NULL
; i
++)
1939 #ifndef OPENSSL_NO_SRP
1940 if ((i
== 2) && (type
& SSL_kSRP
))
1952 #ifndef OPENSSL_NO_ECDH
1953 if (type
& SSL_kECDHE
)
1955 /* XXX: For now, we only support named (not generic) curves.
1956 * In this situation, the serverKeyExchange message has:
1957 * [1 byte CurveType], [2 byte CurveName]
1958 * [1 byte length of encoded point], followed by
1959 * the actual encoded point itself
1961 *p
= NAMED_CURVE_TYPE
;
1969 memcpy((unsigned char*)p
,
1970 (unsigned char *)encodedPoint
,
1972 OPENSSL_free(encodedPoint
);
1973 encodedPoint
= NULL
;
1978 #ifndef OPENSSL_NO_PSK
1979 if (type
& SSL_kPSK
)
1981 /* copy PSK identity hint */
1982 s2n(strlen(s
->ctx
->psk_identity_hint
), p
);
1983 strncpy((char *)p
, s
->ctx
->psk_identity_hint
, strlen(s
->ctx
->psk_identity_hint
));
1984 p
+=strlen(s
->ctx
->psk_identity_hint
);
1991 /* n is the length of the params, they start at &(d[4])
1992 * and p points to the space at the end. */
1993 #ifndef OPENSSL_NO_RSA
1994 if (pkey
->type
== EVP_PKEY_RSA
&& !SSL_USE_SIGALGS(s
))
1998 for (num
=2; num
> 0; num
--)
2000 EVP_MD_CTX_set_flags(&md_ctx
,
2001 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
);
2002 EVP_DigestInit_ex(&md_ctx
,(num
== 2)
2003 ?s
->ctx
->md5
:s
->ctx
->sha1
, NULL
);
2004 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
2005 EVP_DigestUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
2006 EVP_DigestUpdate(&md_ctx
,d
,n
);
2007 EVP_DigestFinal_ex(&md_ctx
,q
,
2008 (unsigned int *)&i
);
2012 if (RSA_sign(NID_md5_sha1
, md_buf
, j
,
2013 &(p
[2]), &u
, pkey
->pkey
.rsa
) <= 0)
2015 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_RSA
);
2025 /* send signature algorithm */
2026 if (SSL_USE_SIGALGS(s
))
2028 if (!tls12_get_sigandhash(p
, pkey
, md
))
2030 /* Should never happen */
2031 al
=SSL_AD_INTERNAL_ERROR
;
2032 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
2038 fprintf(stderr
, "Using hash %s\n",
2041 EVP_SignInit_ex(&md_ctx
, md
, NULL
);
2042 EVP_SignUpdate(&md_ctx
,&(s
->s3
->client_random
[0]),SSL3_RANDOM_SIZE
);
2043 EVP_SignUpdate(&md_ctx
,&(s
->s3
->server_random
[0]),SSL3_RANDOM_SIZE
);
2044 EVP_SignUpdate(&md_ctx
,d
,n
);
2045 if (!EVP_SignFinal(&md_ctx
,&(p
[2]),
2046 (unsigned int *)&i
,pkey
))
2048 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,ERR_LIB_EVP
);
2053 if (SSL_USE_SIGALGS(s
))
2058 /* Is this error check actually needed? */
2059 al
=SSL_AD_HANDSHAKE_FAILURE
;
2060 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE
,SSL_R_UNKNOWN_PKEY_TYPE
);
2065 ssl_set_handshake_header(s
, SSL3_MT_SERVER_KEY_EXCHANGE
, n
);
2068 s
->state
= SSL3_ST_SW_KEY_EXCH_B
;
2069 EVP_MD_CTX_cleanup(&md_ctx
);
2070 return ssl_do_write(s
);
2072 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
2074 #ifndef OPENSSL_NO_ECDH
2075 if (encodedPoint
!= NULL
) OPENSSL_free(encodedPoint
);
2076 BN_CTX_free(bn_ctx
);
2078 EVP_MD_CTX_cleanup(&md_ctx
);
2082 int ssl3_send_certificate_request(SSL
*s
)
2084 unsigned char *p
,*d
;
2086 STACK_OF(X509_NAME
) *sk
=NULL
;
2090 if (s
->state
== SSL3_ST_SW_CERT_REQ_A
)
2094 d
=p
=ssl_handshake_start(s
);
2096 /* get the list of acceptable cert types */
2098 n
=ssl3_get_req_cert_type(s
,p
);
2103 if (SSL_USE_SIGALGS(s
))
2105 const unsigned char *psigs
;
2106 unsigned char *etmp
= p
;
2107 nl
= tls12_get_psigalgs(s
, &psigs
);
2108 /* Skip over length for now */
2110 nl
= tls12_copy_sigalgs(s
, p
, psigs
, nl
);
2111 /* Now fill in length */
2121 sk
=SSL_get_client_CA_list(s
);
2125 for (i
=0; i
<sk_X509_NAME_num(sk
); i
++)
2127 name
=sk_X509_NAME_value(sk
,i
);
2128 j
=i2d_X509_NAME(name
,NULL
);
2129 if (!BUF_MEM_grow_clean(buf
,SSL_HM_HEADER_LENGTH(s
)+n
+j
+2))
2131 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST
,ERR_R_BUF_LIB
);
2134 p
= ssl_handshake_start(s
) + n
;
2135 if (!(s
->options
& SSL_OP_NETSCAPE_CA_DN_BUG
))
2138 i2d_X509_NAME(name
,&p
);
2145 i2d_X509_NAME(name
,&p
);
2146 j
-=2; s2n(j
,d
); j
+=2;
2152 /* else no CA names */
2153 p
= ssl_handshake_start(s
) + off
;
2156 ssl_set_handshake_header(s
, SSL3_MT_CERTIFICATE_REQUEST
, n
);
2158 #ifdef NETSCAPE_HANG_BUG
2159 if (!SSL_IS_DTLS(s
))
2161 if (!BUF_MEM_grow_clean(buf
, s
->init_num
+ 4))
2163 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST
,ERR_R_BUF_LIB
);
2166 p
=(unsigned char *)s
->init_buf
->data
+ s
->init_num
;
2168 *(p
++)=SSL3_MT_SERVER_DONE
;
2176 s
->state
= SSL3_ST_SW_CERT_REQ_B
;
2179 /* SSL3_ST_SW_CERT_REQ_B */
2180 return ssl_do_write(s
);
2185 int ssl3_get_client_key_exchange(SSL
*s
)
2189 unsigned long alg_k
;
2191 #ifndef OPENSSL_NO_RSA
2193 EVP_PKEY
*pkey
=NULL
;
2195 #ifndef OPENSSL_NO_DH
2197 DH
*dh_srvr
, *dh_clnt
= NULL
;
2199 #ifndef OPENSSL_NO_KRB5
2201 #endif /* OPENSSL_NO_KRB5 */
2203 #ifndef OPENSSL_NO_ECDH
2204 EC_KEY
*srvr_ecdh
= NULL
;
2205 EVP_PKEY
*clnt_pub_pkey
= NULL
;
2206 EC_POINT
*clnt_ecpoint
= NULL
;
2207 BN_CTX
*bn_ctx
= NULL
;
2210 n
=s
->method
->ssl_get_message(s
,
2211 SSL3_ST_SR_KEY_EXCH_A
,
2212 SSL3_ST_SR_KEY_EXCH_B
,
2213 SSL3_MT_CLIENT_KEY_EXCHANGE
,
2217 if (!ok
) return((int)n
);
2218 p
=(unsigned char *)s
->init_msg
;
2220 alg_k
=s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2222 #ifndef OPENSSL_NO_RSA
2223 if (alg_k
& SSL_kRSA
)
2225 unsigned char rand_premaster_secret
[SSL_MAX_MASTER_KEY_LENGTH
];
2226 int decrypt_len
, decrypt_good_mask
;
2227 unsigned char version_good
;
2229 /* FIX THIS UP EAY EAY EAY EAY */
2230 if (s
->s3
->tmp
.use_rsa_tmp
)
2232 if ((s
->cert
!= NULL
) && (s
->cert
->rsa_tmp
!= NULL
))
2233 rsa
=s
->cert
->rsa_tmp
;
2234 /* Don't do a callback because rsa_tmp should
2235 * be sent already */
2238 al
=SSL_AD_HANDSHAKE_FAILURE
;
2239 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_RSA_PKEY
);
2246 pkey
=s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
;
2247 if ( (pkey
== NULL
) ||
2248 (pkey
->type
!= EVP_PKEY_RSA
) ||
2249 (pkey
->pkey
.rsa
== NULL
))
2251 al
=SSL_AD_HANDSHAKE_FAILURE
;
2252 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_RSA_CERTIFICATE
);
2258 /* TLS and [incidentally] DTLS{0xFEFF} */
2259 if (s
->version
> SSL3_VERSION
&& s
->version
!= DTLS1_BAD_VER
)
2264 if (!(s
->options
& SSL_OP_TLS_D5_BUG
))
2266 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG
);
2276 /* We must not leak whether a decryption failure occurs because
2277 * of Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see
2278 * RFC 2246, section 7.4.7.1). The code follows that advice of
2279 * the TLS RFC and generates a random premaster secret for the
2280 * case that the decrypt fails. See
2281 * https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
2283 /* should be RAND_bytes, but we cannot work around a failure. */
2284 if (RAND_pseudo_bytes(rand_premaster_secret
,
2285 sizeof(rand_premaster_secret
)) <= 0)
2287 decrypt_len
= RSA_private_decrypt((int)n
,p
,p
,rsa
,RSA_PKCS1_PADDING
);
2290 /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH.
2291 * decrypt_good_mask will be zero if so and non-zero otherwise. */
2292 decrypt_good_mask
= decrypt_len
^ SSL_MAX_MASTER_KEY_LENGTH
;
2294 /* If the version in the decrypted pre-master secret is correct
2295 * then version_good will be zero. The Klima-Pokorny-Rosa
2296 * extension of Bleichenbacher's attack
2297 * (http://eprint.iacr.org/2003/052/) exploits the version
2298 * number check as a "bad version oracle". Thus version checks
2299 * are done in constant time and are treated like any other
2300 * decryption error. */
2301 version_good
= p
[0] ^ (s
->client_version
>>8);
2302 version_good
|= p
[1] ^ (s
->client_version
&0xff);
2304 /* The premaster secret must contain the same version number as
2305 * the ClientHello to detect version rollback attacks
2306 * (strangely, the protocol does not offer such protection for
2307 * DH ciphersuites). However, buggy clients exist that send the
2308 * negotiated protocol version instead if the server does not
2309 * support the requested protocol version. If
2310 * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
2311 if (s
->options
& SSL_OP_TLS_ROLLBACK_BUG
)
2313 unsigned char workaround_mask
= version_good
;
2314 unsigned char workaround
;
2316 /* workaround_mask will be 0xff if version_good is
2317 * non-zero (i.e. the version match failed). Otherwise
2319 workaround_mask
|= workaround_mask
>> 4;
2320 workaround_mask
|= workaround_mask
>> 2;
2321 workaround_mask
|= workaround_mask
>> 1;
2322 workaround_mask
= ~((workaround_mask
& 1) - 1);
2324 workaround
= p
[0] ^ (s
->version
>>8);
2325 workaround
|= p
[1] ^ (s
->version
&0xff);
2327 /* If workaround_mask is 0xff (i.e. there was a version
2328 * mismatch) then we copy the value of workaround over
2330 version_good
= (workaround
& workaround_mask
) |
2331 (version_good
& ~workaround_mask
);
2334 /* If any bits in version_good are set then they'll poision
2335 * decrypt_good_mask and cause rand_premaster_secret to be
2337 decrypt_good_mask
|= version_good
;
2339 /* decrypt_good_mask will be zero iff decrypt_len ==
2340 * SSL_MAX_MASTER_KEY_LENGTH and the version check passed. We
2341 * fold the bottom 32 bits of it with an OR so that the LSB
2342 * will be zero iff everything is good. This assumes that we'll
2343 * never decrypt a value > 2**31 bytes, which seems safe. */
2344 decrypt_good_mask
|= decrypt_good_mask
>> 16;
2345 decrypt_good_mask
|= decrypt_good_mask
>> 8;
2346 decrypt_good_mask
|= decrypt_good_mask
>> 4;
2347 decrypt_good_mask
|= decrypt_good_mask
>> 2;
2348 decrypt_good_mask
|= decrypt_good_mask
>> 1;
2349 /* Now select only the LSB and subtract one. If decrypt_len ==
2350 * SSL_MAX_MASTER_KEY_LENGTH and the version check passed then
2351 * decrypt_good_mask will be all ones. Otherwise it'll be all
2353 decrypt_good_mask
&= 1;
2354 decrypt_good_mask
--;
2356 /* Now copy rand_premaster_secret over p using
2357 * decrypt_good_mask. */
2358 for (i
= 0; i
< (int) sizeof(rand_premaster_secret
); i
++)
2360 p
[i
] = (p
[i
] & decrypt_good_mask
) |
2361 (rand_premaster_secret
[i
] & ~decrypt_good_mask
);
2364 s
->session
->master_key_length
=
2365 s
->method
->ssl3_enc
->generate_master_secret(s
,
2366 s
->session
->master_key
,
2368 OPENSSL_cleanse(p
,i
);
2372 #ifndef OPENSSL_NO_DH
2373 if (alg_k
& (SSL_kDHE
|SSL_kDHr
|SSL_kDHd
))
2376 EVP_PKEY
*skey
= NULL
;
2383 if (!(s
->options
& SSL_OP_SSLEAY_080_CLIENT_DH_BUG
))
2385 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG
);
2394 if (alg_k
& SSL_kDHr
)
2395 idx
= SSL_PKEY_DH_RSA
;
2396 else if (alg_k
& SSL_kDHd
)
2397 idx
= SSL_PKEY_DH_DSA
;
2400 skey
= s
->cert
->pkeys
[idx
].privatekey
;
2401 if ((skey
== NULL
) ||
2402 (skey
->type
!= EVP_PKEY_DH
) ||
2403 (skey
->pkey
.dh
== NULL
))
2405 al
=SSL_AD_HANDSHAKE_FAILURE
;
2406 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_RSA_CERTIFICATE
);
2409 dh_srvr
= skey
->pkey
.dh
;
2411 else if (s
->s3
->tmp
.dh
== NULL
)
2413 al
=SSL_AD_HANDSHAKE_FAILURE
;
2414 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
2418 dh_srvr
=s
->s3
->tmp
.dh
;
2422 /* Get pubkey from cert */
2423 EVP_PKEY
*clkey
=X509_get_pubkey(s
->session
->peer
);
2426 if (EVP_PKEY_cmp_parameters(clkey
, skey
) == 1)
2427 dh_clnt
= EVP_PKEY_get1_DH(clkey
);
2429 if (dh_clnt
== NULL
)
2431 al
=SSL_AD_HANDSHAKE_FAILURE
;
2432 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_DH_KEY
);
2435 EVP_PKEY_free(clkey
);
2436 pub
= dh_clnt
->pub_key
;
2439 pub
=BN_bin2bn(p
,i
,NULL
);
2442 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BN_LIB
);
2446 i
=DH_compute_key(p
,pub
,dh_srvr
);
2450 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,ERR_R_DH_LIB
);
2455 DH_free(s
->s3
->tmp
.dh
);
2462 s
->session
->master_key_length
=
2463 s
->method
->ssl3_enc
->generate_master_secret(s
,
2464 s
->session
->master_key
,p
,i
);
2465 OPENSSL_cleanse(p
,i
);
2471 #ifndef OPENSSL_NO_KRB5
2472 if (alg_k
& SSL_kKRB5
)
2474 krb5_error_code krb5rc
;
2475 krb5_data enc_ticket
;
2476 krb5_data authenticator
;
2478 KSSL_CTX
*kssl_ctx
= s
->kssl_ctx
;
2479 EVP_CIPHER_CTX ciph_ctx
;
2480 const EVP_CIPHER
*enc
= NULL
;
2481 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2482 unsigned char pms
[SSL_MAX_MASTER_KEY_LENGTH
2483 + EVP_MAX_BLOCK_LENGTH
];
2485 krb5_timestamp authtime
= 0;
2486 krb5_ticket_times ttimes
;
2488 EVP_CIPHER_CTX_init(&ciph_ctx
);
2490 if (!kssl_ctx
) kssl_ctx
= kssl_ctx_new();
2493 enc_ticket
.length
= i
;
2495 if (n
< (long)(enc_ticket
.length
+ 6))
2497 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2498 SSL_R_DATA_LENGTH_TOO_LONG
);
2502 enc_ticket
.data
= (char *)p
;
2503 p
+=enc_ticket
.length
;
2506 authenticator
.length
= i
;
2508 if (n
< (long)(enc_ticket
.length
+ authenticator
.length
+ 6))
2510 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2511 SSL_R_DATA_LENGTH_TOO_LONG
);
2515 authenticator
.data
= (char *)p
;
2516 p
+=authenticator
.length
;
2520 enc_pms
.data
= (char *)p
;
2523 /* Note that the length is checked again below,
2526 if(enc_pms
.length
> sizeof pms
)
2528 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2529 SSL_R_DATA_LENGTH_TOO_LONG
);
2533 if (n
!= (long)(enc_ticket
.length
+ authenticator
.length
+
2534 enc_pms
.length
+ 6))
2536 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2537 SSL_R_DATA_LENGTH_TOO_LONG
);
2541 if ((krb5rc
= kssl_sget_tkt(kssl_ctx
, &enc_ticket
, &ttimes
,
2545 printf("kssl_sget_tkt rtn %d [%d]\n",
2546 krb5rc
, kssl_err
.reason
);
2548 printf("kssl_err text= %s\n", kssl_err
.text
);
2549 #endif /* KSSL_DEBUG */
2550 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2555 /* Note: no authenticator is not considered an error,
2556 ** but will return authtime == 0.
2558 if ((krb5rc
= kssl_check_authent(kssl_ctx
, &authenticator
,
2559 &authtime
, &kssl_err
)) != 0)
2562 printf("kssl_check_authent rtn %d [%d]\n",
2563 krb5rc
, kssl_err
.reason
);
2565 printf("kssl_err text= %s\n", kssl_err
.text
);
2566 #endif /* KSSL_DEBUG */
2567 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2572 if ((krb5rc
= kssl_validate_times(authtime
, &ttimes
)) != 0)
2574 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
, krb5rc
);
2579 kssl_ctx_show(kssl_ctx
);
2580 #endif /* KSSL_DEBUG */
2582 enc
= kssl_map_enc(kssl_ctx
->enctype
);
2586 memset(iv
, 0, sizeof iv
); /* per RFC 1510 */
2588 if (!EVP_DecryptInit_ex(&ciph_ctx
,enc
,NULL
,kssl_ctx
->key
,iv
))
2590 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2591 SSL_R_DECRYPTION_FAILED
);
2594 if (!EVP_DecryptUpdate(&ciph_ctx
, pms
,&outl
,
2595 (unsigned char *)enc_pms
.data
, enc_pms
.length
))
2597 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2598 SSL_R_DECRYPTION_FAILED
);
2601 if (outl
> SSL_MAX_MASTER_KEY_LENGTH
)
2603 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2604 SSL_R_DATA_LENGTH_TOO_LONG
);
2607 if (!EVP_DecryptFinal_ex(&ciph_ctx
,&(pms
[outl
]),&padl
))
2609 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2610 SSL_R_DECRYPTION_FAILED
);
2614 if (outl
> SSL_MAX_MASTER_KEY_LENGTH
)
2616 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2617 SSL_R_DATA_LENGTH_TOO_LONG
);
2620 if (!((pms
[0] == (s
->client_version
>>8)) && (pms
[1] == (s
->client_version
& 0xff))))
2622 /* The premaster secret must contain the same version number as the
2623 * ClientHello to detect version rollback attacks (strangely, the
2624 * protocol does not offer such protection for DH ciphersuites).
2625 * However, buggy clients exist that send random bytes instead of
2626 * the protocol version.
2627 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2628 * (Perhaps we should have a separate BUG value for the Kerberos cipher)
2630 if (!(s
->options
& SSL_OP_TLS_ROLLBACK_BUG
))
2632 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2633 SSL_AD_DECODE_ERROR
);
2638 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2640 s
->session
->master_key_length
=
2641 s
->method
->ssl3_enc
->generate_master_secret(s
,
2642 s
->session
->master_key
, pms
, outl
);
2644 if (kssl_ctx
->client_princ
)
2646 size_t len
= strlen(kssl_ctx
->client_princ
);
2647 if ( len
< SSL_MAX_KRB5_PRINCIPAL_LENGTH
)
2649 s
->session
->krb5_client_princ_len
= len
;
2650 memcpy(s
->session
->krb5_client_princ
,kssl_ctx
->client_princ
,len
);
2655 /* Was doing kssl_ctx_free() here,
2656 ** but it caused problems for apache.
2657 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
2658 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
2662 #endif /* OPENSSL_NO_KRB5 */
2664 #ifndef OPENSSL_NO_ECDH
2665 if (alg_k
& (SSL_kECDHE
|SSL_kECDHr
|SSL_kECDHe
))
2670 const EC_GROUP
*group
;
2671 const BIGNUM
*priv_key
;
2673 /* initialize structures for server's ECDH key pair */
2674 if ((srvr_ecdh
= EC_KEY_new()) == NULL
)
2676 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2677 ERR_R_MALLOC_FAILURE
);
2681 /* Let's get server private key and group information */
2682 if (alg_k
& (SSL_kECDHr
|SSL_kECDHe
))
2684 /* use the certificate */
2685 tkey
= s
->cert
->pkeys
[SSL_PKEY_ECC
].privatekey
->pkey
.ec
;
2689 /* use the ephermeral values we saved when
2690 * generating the ServerKeyExchange msg.
2692 tkey
= s
->s3
->tmp
.ecdh
;
2695 group
= EC_KEY_get0_group(tkey
);
2696 priv_key
= EC_KEY_get0_private_key(tkey
);
2698 if (!EC_KEY_set_group(srvr_ecdh
, group
) ||
2699 !EC_KEY_set_private_key(srvr_ecdh
, priv_key
))
2701 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2706 /* Let's get client's public key */
2707 if ((clnt_ecpoint
= EC_POINT_new(group
)) == NULL
)
2709 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2710 ERR_R_MALLOC_FAILURE
);
2716 /* Client Publickey was in Client Certificate */
2718 if (alg_k
& SSL_kECDHE
)
2720 al
=SSL_AD_HANDSHAKE_FAILURE
;
2721 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_MISSING_TMP_ECDH_KEY
);
2724 if (((clnt_pub_pkey
=X509_get_pubkey(s
->session
->peer
))
2726 (clnt_pub_pkey
->type
!= EVP_PKEY_EC
))
2728 /* XXX: For now, we do not support client
2729 * authentication using ECDH certificates
2730 * so this branch (n == 0L) of the code is
2731 * never executed. When that support is
2732 * added, we ought to ensure the key
2733 * received in the certificate is
2734 * authorized for key agreement.
2735 * ECDH_compute_key implicitly checks that
2736 * the two ECDH shares are for the same
2739 al
=SSL_AD_HANDSHAKE_FAILURE
;
2740 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2741 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS
);
2745 if (EC_POINT_copy(clnt_ecpoint
,
2746 EC_KEY_get0_public_key(clnt_pub_pkey
->pkey
.ec
)) == 0)
2748 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2752 ret
= 2; /* Skip certificate verify processing */
2756 /* Get client's public key from encoded point
2757 * in the ClientKeyExchange message.
2759 if ((bn_ctx
= BN_CTX_new()) == NULL
)
2761 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2762 ERR_R_MALLOC_FAILURE
);
2766 /* Get encoded point length */
2771 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2775 if (EC_POINT_oct2point(group
,
2776 clnt_ecpoint
, p
, i
, bn_ctx
) == 0)
2778 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2782 /* p is pointing to somewhere in the buffer
2783 * currently, so set it to the start
2785 p
=(unsigned char *)s
->init_buf
->data
;
2788 /* Compute the shared pre-master secret */
2789 field_size
= EC_GROUP_get_degree(group
);
2790 if (field_size
<= 0)
2792 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2796 i
= ECDH_compute_key(p
, (field_size
+7)/8, clnt_ecpoint
, srvr_ecdh
, NULL
);
2799 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2804 EVP_PKEY_free(clnt_pub_pkey
);
2805 EC_POINT_free(clnt_ecpoint
);
2806 EC_KEY_free(srvr_ecdh
);
2807 BN_CTX_free(bn_ctx
);
2808 EC_KEY_free(s
->s3
->tmp
.ecdh
);
2809 s
->s3
->tmp
.ecdh
= NULL
;
2811 /* Compute the master secret */
2812 s
->session
->master_key_length
= s
->method
->ssl3_enc
-> \
2813 generate_master_secret(s
, s
->session
->master_key
, p
, i
);
2815 OPENSSL_cleanse(p
, i
);
2820 #ifndef OPENSSL_NO_PSK
2821 if (alg_k
& SSL_kPSK
)
2823 unsigned char *t
= NULL
;
2824 unsigned char psk_or_pre_ms
[PSK_MAX_PSK_LEN
*2+4];
2825 unsigned int pre_ms_len
= 0, psk_len
= 0;
2827 char tmp_id
[PSK_MAX_IDENTITY_LEN
+1];
2829 al
=SSL_AD_HANDSHAKE_FAILURE
;
2834 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2835 SSL_R_LENGTH_MISMATCH
);
2838 if (i
> PSK_MAX_IDENTITY_LEN
)
2840 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2841 SSL_R_DATA_LENGTH_TOO_LONG
);
2844 if (s
->psk_server_callback
== NULL
)
2846 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2847 SSL_R_PSK_NO_SERVER_CB
);
2851 /* Create guaranteed NULL-terminated identity
2852 * string for the callback */
2853 memcpy(tmp_id
, p
, i
);
2854 memset(tmp_id
+i
, 0, PSK_MAX_IDENTITY_LEN
+1-i
);
2855 psk_len
= s
->psk_server_callback(s
, tmp_id
,
2856 psk_or_pre_ms
, sizeof(psk_or_pre_ms
));
2857 OPENSSL_cleanse(tmp_id
, PSK_MAX_IDENTITY_LEN
+1);
2859 if (psk_len
> PSK_MAX_PSK_LEN
)
2861 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2862 ERR_R_INTERNAL_ERROR
);
2865 else if (psk_len
== 0)
2867 /* PSK related to the given identity not found */
2868 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2869 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2870 al
=SSL_AD_UNKNOWN_PSK_IDENTITY
;
2874 /* create PSK pre_master_secret */
2875 pre_ms_len
=2+psk_len
+2+psk_len
;
2877 memmove(psk_or_pre_ms
+psk_len
+4, psk_or_pre_ms
, psk_len
);
2879 memset(t
, 0, psk_len
);
2883 if (s
->session
->psk_identity
!= NULL
)
2884 OPENSSL_free(s
->session
->psk_identity
);
2885 s
->session
->psk_identity
= BUF_strdup((char *)p
);
2886 if (s
->session
->psk_identity
== NULL
)
2888 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2889 ERR_R_MALLOC_FAILURE
);
2893 if (s
->session
->psk_identity_hint
!= NULL
)
2894 OPENSSL_free(s
->session
->psk_identity_hint
);
2895 s
->session
->psk_identity_hint
= BUF_strdup(s
->ctx
->psk_identity_hint
);
2896 if (s
->ctx
->psk_identity_hint
!= NULL
&&
2897 s
->session
->psk_identity_hint
== NULL
)
2899 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2900 ERR_R_MALLOC_FAILURE
);
2904 s
->session
->master_key_length
=
2905 s
->method
->ssl3_enc
->generate_master_secret(s
,
2906 s
->session
->master_key
, psk_or_pre_ms
, pre_ms_len
);
2909 OPENSSL_cleanse(psk_or_pre_ms
, sizeof(psk_or_pre_ms
));
2915 #ifndef OPENSSL_NO_SRP
2916 if (alg_k
& SSL_kSRP
)
2924 al
=SSL_AD_DECODE_ERROR
;
2925 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_SRP_A_LENGTH
);
2928 if (!(s
->srp_ctx
.A
=BN_bin2bn(p
,i
,NULL
)))
2930 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,ERR_R_BN_LIB
);
2933 if (BN_ucmp(s
->srp_ctx
.A
, s
->srp_ctx
.N
) >= 0
2934 || BN_is_zero(s
->srp_ctx
.A
))
2936 al
=SSL_AD_ILLEGAL_PARAMETER
;
2937 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_BAD_SRP_PARAMETERS
);
2940 if (s
->session
->srp_username
!= NULL
)
2941 OPENSSL_free(s
->session
->srp_username
);
2942 s
->session
->srp_username
= BUF_strdup(s
->srp_ctx
.login
);
2943 if (s
->session
->srp_username
== NULL
)
2945 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
2946 ERR_R_MALLOC_FAILURE
);
2950 if ((s
->session
->master_key_length
= SRP_generate_server_master_secret(s
,s
->session
->master_key
))<0)
2952 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,ERR_R_INTERNAL_ERROR
);
2959 #endif /* OPENSSL_NO_SRP */
2960 if (alg_k
& SSL_kGOST
)
2963 EVP_PKEY_CTX
*pkey_ctx
;
2964 EVP_PKEY
*client_pub_pkey
= NULL
, *pk
= NULL
;
2965 unsigned char premaster_secret
[32], *start
;
2966 size_t outlen
=32, inlen
;
2967 unsigned long alg_a
;
2971 /* Get our certificate private key*/
2972 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
2973 if (alg_a
& SSL_aGOST94
)
2974 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST94
].privatekey
;
2975 else if (alg_a
& SSL_aGOST01
)
2976 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
2978 pkey_ctx
= EVP_PKEY_CTX_new(pk
,NULL
);
2979 EVP_PKEY_decrypt_init(pkey_ctx
);
2980 /* If client certificate is present and is of the same type, maybe
2981 * use it for key exchange. Don't mind errors from
2982 * EVP_PKEY_derive_set_peer, because it is completely valid to use
2983 * a client certificate for authorization only. */
2984 client_pub_pkey
= X509_get_pubkey(s
->session
->peer
);
2985 if (client_pub_pkey
)
2987 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
2990 /* Decrypt session key */
2991 if (ASN1_get_object((const unsigned char **)&p
, &Tlen
, &Ttag
, &Tclass
, n
) != V_ASN1_CONSTRUCTED
||
2992 Ttag
!= V_ASN1_SEQUENCE
||
2993 Tclass
!= V_ASN1_UNIVERSAL
)
2995 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_DECRYPTION_FAILED
);
3000 if (EVP_PKEY_decrypt(pkey_ctx
,premaster_secret
,&outlen
,start
,inlen
) <=0)
3003 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_DECRYPTION_FAILED
);
3006 /* Generate master secret */
3007 s
->session
->master_key_length
=
3008 s
->method
->ssl3_enc
->generate_master_secret(s
,
3009 s
->session
->master_key
,premaster_secret
,32);
3010 /* Check if pubkey from client certificate was used */
3011 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0)
3016 EVP_PKEY_free(client_pub_pkey
);
3017 EVP_PKEY_CTX_free(pkey_ctx
);
3025 al
=SSL_AD_HANDSHAKE_FAILURE
;
3026 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
3027 SSL_R_UNKNOWN_CIPHER_TYPE
);
3033 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
3034 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP)
3037 #ifndef OPENSSL_NO_ECDH
3038 EVP_PKEY_free(clnt_pub_pkey
);
3039 EC_POINT_free(clnt_ecpoint
);
3040 if (srvr_ecdh
!= NULL
)
3041 EC_KEY_free(srvr_ecdh
);
3042 BN_CTX_free(bn_ctx
);
3047 int ssl3_get_cert_verify(SSL
*s
)
3049 EVP_PKEY
*pkey
=NULL
;
3055 const EVP_MD
*md
= NULL
;
3057 EVP_MD_CTX_init(&mctx
);
3059 n
=s
->method
->ssl_get_message(s
,
3060 SSL3_ST_SR_CERT_VRFY_A
,
3061 SSL3_ST_SR_CERT_VRFY_B
,
3063 SSL3_RT_MAX_PLAIN_LENGTH
,
3066 if (!ok
) return((int)n
);
3068 if (s
->session
->peer
!= NULL
)
3070 peer
=s
->session
->peer
;
3071 pkey
=X509_get_pubkey(peer
);
3072 type
=X509_certificate_type(peer
,pkey
);
3080 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_VERIFY
)
3082 s
->s3
->tmp
.reuse_message
=1;
3083 if ((peer
!= NULL
) && (type
& EVP_PKT_SIGN
))
3085 al
=SSL_AD_UNEXPECTED_MESSAGE
;
3086 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_MISSING_VERIFY_MESSAGE
);
3095 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_NO_CLIENT_CERT_RECEIVED
);
3096 al
=SSL_AD_UNEXPECTED_MESSAGE
;
3100 if (!(type
& EVP_PKT_SIGN
))
3102 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
3103 al
=SSL_AD_ILLEGAL_PARAMETER
;
3107 if (s
->s3
->change_cipher_spec
)
3109 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_CCS_RECEIVED_EARLY
);
3110 al
=SSL_AD_UNEXPECTED_MESSAGE
;
3114 /* we now have a signature that we need to verify */
3115 p
=(unsigned char *)s
->init_msg
;
3116 /* Check for broken implementations of GOST ciphersuites */
3117 /* If key is GOST and n is exactly 64, it is bare
3118 * signature without length field */
3119 if (n
==64 && (pkey
->type
==NID_id_GostR3410_94
||
3120 pkey
->type
== NID_id_GostR3410_2001
) )
3126 if (SSL_USE_SIGALGS(s
))
3128 int rv
= tls12_check_peer_sigalg(&md
, s
, p
, pkey
);
3131 al
= SSL_AD_INTERNAL_ERROR
;
3136 al
= SSL_AD_DECODE_ERROR
;
3140 fprintf(stderr
, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md
));
3149 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_LENGTH_MISMATCH
);
3150 al
=SSL_AD_DECODE_ERROR
;
3154 j
=EVP_PKEY_size(pkey
);
3155 if ((i
> j
) || (n
> j
) || (n
<= 0))
3157 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_WRONG_SIGNATURE_SIZE
);
3158 al
=SSL_AD_DECODE_ERROR
;
3162 if (SSL_USE_SIGALGS(s
))
3166 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
3169 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3170 al
=SSL_AD_INTERNAL_ERROR
;
3174 fprintf(stderr
, "Using TLS 1.2 with client verify alg %s\n",
3177 if (!EVP_VerifyInit_ex(&mctx
, md
, NULL
)
3178 || !EVP_VerifyUpdate(&mctx
, hdata
, hdatalen
))
3180 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, ERR_R_EVP_LIB
);
3181 al
=SSL_AD_INTERNAL_ERROR
;
3185 if (EVP_VerifyFinal(&mctx
, p
, i
, pkey
) <= 0)
3187 al
=SSL_AD_DECRYPT_ERROR
;
3188 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_SIGNATURE
);
3193 #ifndef OPENSSL_NO_RSA
3194 if (pkey
->type
== EVP_PKEY_RSA
)
3196 i
=RSA_verify(NID_md5_sha1
, s
->s3
->tmp
.cert_verify_md
,
3197 MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
, p
, i
,
3201 al
=SSL_AD_DECRYPT_ERROR
;
3202 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_DECRYPT
);
3207 al
=SSL_AD_DECRYPT_ERROR
;
3208 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_SIGNATURE
);
3214 #ifndef OPENSSL_NO_DSA
3215 if (pkey
->type
== EVP_PKEY_DSA
)
3217 j
=DSA_verify(pkey
->save_type
,
3218 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
3219 SHA_DIGEST_LENGTH
,p
,i
,pkey
->pkey
.dsa
);
3223 al
=SSL_AD_DECRYPT_ERROR
;
3224 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_DSA_SIGNATURE
);
3230 #ifndef OPENSSL_NO_ECDSA
3231 if (pkey
->type
== EVP_PKEY_EC
)
3233 j
=ECDSA_verify(pkey
->save_type
,
3234 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
3235 SHA_DIGEST_LENGTH
,p
,i
,pkey
->pkey
.ec
);
3239 al
=SSL_AD_DECRYPT_ERROR
;
3240 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,
3241 SSL_R_BAD_ECDSA_SIGNATURE
);
3247 if (pkey
->type
== NID_id_GostR3410_94
|| pkey
->type
== NID_id_GostR3410_2001
)
3248 { unsigned char signature
[64];
3250 EVP_PKEY_CTX
*pctx
= EVP_PKEY_CTX_new(pkey
,NULL
);
3251 EVP_PKEY_verify_init(pctx
);
3253 fprintf(stderr
,"GOST signature length is %d",i
);
3255 for (idx
=0;idx
<64;idx
++) {
3256 signature
[63-idx
]=p
[idx
];
3258 j
=EVP_PKEY_verify(pctx
,signature
,64,s
->s3
->tmp
.cert_verify_md
,32);
3259 EVP_PKEY_CTX_free(pctx
);
3262 al
=SSL_AD_DECRYPT_ERROR
;
3263 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,
3264 SSL_R_BAD_ECDSA_SIGNATURE
);
3270 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,ERR_R_INTERNAL_ERROR
);
3271 al
=SSL_AD_UNSUPPORTED_CERTIFICATE
;
3280 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
3283 if (s
->s3
->handshake_buffer
)
3285 BIO_free(s
->s3
->handshake_buffer
);
3286 s
->s3
->handshake_buffer
= NULL
;
3287 s
->s3
->flags
&= ~TLS1_FLAGS_KEEP_HANDSHAKE
;
3289 EVP_MD_CTX_cleanup(&mctx
);
3290 EVP_PKEY_free(pkey
);
3294 int ssl3_get_client_certificate(SSL
*s
)
3296 int i
,ok
,al
,ret
= -1;
3298 unsigned long l
,nc
,llen
,n
;
3299 const unsigned char *p
,*q
;
3301 STACK_OF(X509
) *sk
=NULL
;
3303 n
=s
->method
->ssl_get_message(s
,
3310 if (!ok
) return((int)n
);
3312 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_KEY_EXCHANGE
)
3314 if ( (s
->verify_mode
& SSL_VERIFY_PEER
) &&
3315 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
3317 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3318 al
=SSL_AD_HANDSHAKE_FAILURE
;
3321 /* If tls asked for a client cert, the client must return a 0 list */
3322 if ((s
->version
> SSL3_VERSION
) && s
->s3
->tmp
.cert_request
)
3324 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
);
3325 al
=SSL_AD_UNEXPECTED_MESSAGE
;
3328 s
->s3
->tmp
.reuse_message
=1;
3332 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
)
3334 al
=SSL_AD_UNEXPECTED_MESSAGE
;
3335 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_WRONG_MESSAGE_TYPE
);
3338 p
=d
=(unsigned char *)s
->init_msg
;
3340 if ((sk
=sk_X509_new_null()) == NULL
)
3342 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
3349 al
=SSL_AD_DECODE_ERROR
;
3350 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_LENGTH_MISMATCH
);
3353 for (nc
=0; nc
<llen
; )
3356 if ((l
+nc
+3) > llen
)
3358 al
=SSL_AD_DECODE_ERROR
;
3359 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
3364 x
=d2i_X509(NULL
,&p
,l
);
3367 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_ASN1_LIB
);
3372 al
=SSL_AD_DECODE_ERROR
;
3373 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
3376 if (!sk_X509_push(sk
,x
))
3378 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
3385 if (sk_X509_num(sk
) <= 0)
3387 /* TLS does not mind 0 certs returned */
3388 if (s
->version
== SSL3_VERSION
)
3390 al
=SSL_AD_HANDSHAKE_FAILURE
;
3391 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_NO_CERTIFICATES_RETURNED
);
3394 /* Fail for TLS only if we required a certificate */
3395 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3396 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
3398 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3399 al
=SSL_AD_HANDSHAKE_FAILURE
;
3402 /* No client certificate so digest cached records */
3403 if (s
->s3
->handshake_buffer
&& !ssl3_digest_cached_records(s
))
3405 al
=SSL_AD_INTERNAL_ERROR
;
3412 i
=ssl_verify_cert_chain(s
,sk
);
3415 al
=ssl_verify_alarm_type(s
->verify_result
);
3416 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERTIFICATE_VERIFY_FAILED
);
3421 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, i
);
3422 al
= SSL_AD_HANDSHAKE_FAILURE
;
3425 pkey
= X509_get_pubkey(sk_X509_value(sk
, 0));
3428 al
=SSL3_AD_HANDSHAKE_FAILURE
;
3429 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,
3430 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3433 EVP_PKEY_free(pkey
);
3436 if (s
->session
->peer
!= NULL
) /* This should not be needed */
3437 X509_free(s
->session
->peer
);
3438 s
->session
->peer
=sk_X509_shift(sk
);
3439 s
->session
->verify_result
= s
->verify_result
;
3441 /* With the current implementation, sess_cert will always be NULL
3442 * when we arrive here. */
3443 if (s
->session
->sess_cert
== NULL
)
3445 s
->session
->sess_cert
= ssl_sess_cert_new();
3446 if (s
->session
->sess_cert
== NULL
)
3448 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
3452 if (s
->session
->sess_cert
->cert_chain
!= NULL
)
3453 sk_X509_pop_free(s
->session
->sess_cert
->cert_chain
, X509_free
);
3454 s
->session
->sess_cert
->cert_chain
=sk
;
3455 /* Inconsistency alert: cert_chain does *not* include the
3456 * peer's own certificate, while we do include it in s3_clnt.c */
3464 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
3467 if (x
!= NULL
) X509_free(x
);
3468 if (sk
!= NULL
) sk_X509_pop_free(sk
,X509_free
);
3472 int ssl3_send_server_certificate(SSL
*s
)
3476 if (s
->state
== SSL3_ST_SW_CERT_A
)
3478 cpk
=ssl_get_server_send_pkey(s
);
3481 /* VRS: allow null cert if auth == KRB5 */
3482 if ((s
->s3
->tmp
.new_cipher
->algorithm_auth
!= SSL_aKRB5
) ||
3483 (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kKRB5
))
3485 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE
,ERR_R_INTERNAL_ERROR
);
3490 if (!ssl3_output_cert_chain(s
,cpk
))
3492 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE
,ERR_R_INTERNAL_ERROR
);
3495 s
->state
=SSL3_ST_SW_CERT_B
;
3498 /* SSL3_ST_SW_CERT_B */
3499 return ssl_do_write(s
);
3502 #ifndef OPENSSL_NO_TLSEXT
3503 /* send a new session ticket (not necessarily for a new session) */
3504 int ssl3_send_newsession_ticket(SSL
*s
)
3506 if (s
->state
== SSL3_ST_SW_SESSION_TICKET_A
)
3508 unsigned char *p
, *senc
, *macstart
;
3509 const unsigned char *const_p
;
3510 int len
, slen_full
, slen
;
3515 SSL_CTX
*tctx
= s
->initial_ctx
;
3516 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3517 unsigned char key_name
[16];
3519 /* get session encoding length */
3520 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3521 /* Some length values are 16 bits, so forget it if session is
3524 if (slen_full
> 0xFF00)
3526 senc
= OPENSSL_malloc(slen_full
);
3530 i2d_SSL_SESSION(s
->session
, &p
);
3532 /* create a fresh copy (not shared with other threads) to clean up */
3534 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3540 sess
->session_id_length
= 0; /* ID is irrelevant for the ticket */
3542 slen
= i2d_SSL_SESSION(sess
, NULL
);
3543 if (slen
> slen_full
) /* shouldn't ever happen */
3549 i2d_SSL_SESSION(sess
, &p
);
3550 SSL_SESSION_free(sess
);
3552 /* Grow buffer if need be: the length calculation is as
3553 * follows handshake_header_length +
3554 * 4 (ticket lifetime hint) + 2 (ticket length) +
3555 * 16 (key name) + max_iv_len (iv length) +
3556 * session_length + max_enc_block_size (max encrypted session
3557 * length) + max_md_size (HMAC).
3559 if (!BUF_MEM_grow(s
->init_buf
,
3560 SSL_HM_HEADER_LENGTH(s
) + 22 + EVP_MAX_IV_LENGTH
+
3561 EVP_MAX_BLOCK_LENGTH
+ EVP_MAX_MD_SIZE
+ slen
))
3563 p
= ssl_handshake_start(s
);
3564 EVP_CIPHER_CTX_init(&ctx
);
3565 HMAC_CTX_init(&hctx
);
3566 /* Initialize HMAC and cipher contexts. If callback present
3567 * it does all the work otherwise use generated values
3570 if (tctx
->tlsext_ticket_key_cb
)
3572 if (tctx
->tlsext_ticket_key_cb(s
, key_name
, iv
, &ctx
,
3581 RAND_pseudo_bytes(iv
, 16);
3582 EVP_EncryptInit_ex(&ctx
, EVP_aes_128_cbc(), NULL
,
3583 tctx
->tlsext_tick_aes_key
, iv
);
3584 HMAC_Init_ex(&hctx
, tctx
->tlsext_tick_hmac_key
, 16,
3585 tlsext_tick_md(), NULL
);
3586 memcpy(key_name
, tctx
->tlsext_tick_key_name
, 16);
3589 /* Ticket lifetime hint (advisory only):
3590 * We leave this unspecified for resumed session (for simplicity),
3591 * and guess that tickets for new sessions will live as long
3592 * as their sessions. */
3593 l2n(s
->hit
? 0 : s
->session
->timeout
, p
);
3595 /* Skip ticket length for now */
3597 /* Output key name */
3599 memcpy(p
, key_name
, 16);
3602 memcpy(p
, iv
, EVP_CIPHER_CTX_iv_length(&ctx
));
3603 p
+= EVP_CIPHER_CTX_iv_length(&ctx
);
3604 /* Encrypt session data */
3605 EVP_EncryptUpdate(&ctx
, p
, &len
, senc
, slen
);
3607 EVP_EncryptFinal(&ctx
, p
, &len
);
3609 EVP_CIPHER_CTX_cleanup(&ctx
);
3611 HMAC_Update(&hctx
, macstart
, p
- macstart
);
3612 HMAC_Final(&hctx
, p
, &hlen
);
3613 HMAC_CTX_cleanup(&hctx
);
3616 /* Now write out lengths: p points to end of data written */
3618 len
= p
- ssl_handshake_start(s
);
3619 ssl_set_handshake_header(s
, SSL3_MT_NEWSESSION_TICKET
, len
);
3620 /* Skip ticket lifetime hint */
3621 p
= ssl_handshake_start(s
) + 4;
3623 s
->state
=SSL3_ST_SW_SESSION_TICKET_B
;
3627 /* SSL3_ST_SW_SESSION_TICKET_B */
3628 return ssl_do_write(s
);
3631 int ssl3_send_cert_status(SSL
*s
)
3633 if (s
->state
== SSL3_ST_SW_CERT_STATUS_A
)
3636 /* Grow buffer if need be: the length calculation is as
3637 * follows 1 (message type) + 3 (message length) +
3638 * 1 (ocsp response type) + 3 (ocsp response length)
3641 if (!BUF_MEM_grow(s
->init_buf
, 8 + s
->tlsext_ocsp_resplen
))
3644 p
=(unsigned char *)s
->init_buf
->data
;
3647 *(p
++)=SSL3_MT_CERTIFICATE_STATUS
;
3648 /* message length */
3649 l2n3(s
->tlsext_ocsp_resplen
+ 4, p
);
3651 *(p
++)= s
->tlsext_status_type
;
3652 /* length of OCSP response */
3653 l2n3(s
->tlsext_ocsp_resplen
, p
);
3654 /* actual response */
3655 memcpy(p
, s
->tlsext_ocsp_resp
, s
->tlsext_ocsp_resplen
);
3656 /* number of bytes to write */
3657 s
->init_num
= 8 + s
->tlsext_ocsp_resplen
;
3658 s
->state
=SSL3_ST_SW_CERT_STATUS_B
;
3662 /* SSL3_ST_SW_CERT_STATUS_B */
3663 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
3666 # ifndef OPENSSL_NO_NEXTPROTONEG
3667 /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
3668 * sets the next_proto member in s if found */
3669 int ssl3_get_next_proto(SSL
*s
)
3672 int proto_len
, padding_len
;
3674 const unsigned char *p
;
3676 /* Clients cannot send a NextProtocol message if we didn't see the
3677 * extension in their ClientHello */
3678 if (!s
->s3
->next_proto_neg_seen
)
3680 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO
,SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION
);
3684 n
=s
->method
->ssl_get_message(s
,
3685 SSL3_ST_SR_NEXT_PROTO_A
,
3686 SSL3_ST_SR_NEXT_PROTO_B
,
3688 514, /* See the payload format below */
3694 /* s->state doesn't reflect whether ChangeCipherSpec has been received
3695 * in this handshake, but s->s3->change_cipher_spec does (will be reset
3696 * by ssl3_get_finished). */
3697 if (!s
->s3
->change_cipher_spec
)
3699 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO
,SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS
);
3704 return 0; /* The body must be > 1 bytes long */
3706 p
=(unsigned char *)s
->init_msg
;
3708 /* The payload looks like:
3710 * uint8 proto[proto_len];
3711 * uint8 padding_len;
3712 * uint8 padding[padding_len];
3715 if (proto_len
+ 2 > s
->init_num
)
3717 padding_len
= p
[proto_len
+ 1];
3718 if (proto_len
+ padding_len
+ 2 != s
->init_num
)
3721 s
->next_proto_negotiated
= OPENSSL_malloc(proto_len
);
3722 if (!s
->next_proto_negotiated
)
3724 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO
,ERR_R_MALLOC_FAILURE
);
3727 memcpy(s
->next_proto_negotiated
, p
+ 1, proto_len
);
3728 s
->next_proto_negotiated_len
= proto_len
;