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
;
2983 /* Get our certificate private key*/
2984 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
2985 if (alg_a
& SSL_aGOST94
)
2986 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST94
].privatekey
;
2987 else if (alg_a
& SSL_aGOST01
)
2988 pk
= s
->cert
->pkeys
[SSL_PKEY_GOST01
].privatekey
;
2990 pkey_ctx
= EVP_PKEY_CTX_new(pk
,NULL
);
2991 EVP_PKEY_decrypt_init(pkey_ctx
);
2992 /* If client certificate is present and is of the same type, maybe
2993 * use it for key exchange. Don't mind errors from
2994 * EVP_PKEY_derive_set_peer, because it is completely valid to use
2995 * a client certificate for authorization only. */
2996 client_pub_pkey
= X509_get_pubkey(s
->session
->peer
);
2997 if (client_pub_pkey
)
2999 if (EVP_PKEY_derive_set_peer(pkey_ctx
, client_pub_pkey
) <= 0)
3002 /* Decrypt session key */
3003 if ((*p
!=( V_ASN1_SEQUENCE
| V_ASN1_CONSTRUCTED
)))
3005 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_DECRYPTION_FAILED
);
3013 else if (p
[1] < 0x80)
3020 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_DECRYPTION_FAILED
);
3023 if (EVP_PKEY_decrypt(pkey_ctx
,premaster_secret
,&outlen
,start
,inlen
) <=0)
3026 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,SSL_R_DECRYPTION_FAILED
);
3029 /* Generate master secret */
3030 s
->session
->master_key_length
=
3031 s
->method
->ssl3_enc
->generate_master_secret(s
,
3032 s
->session
->master_key
,premaster_secret
,32);
3033 /* Check if pubkey from client certificate was used */
3034 if (EVP_PKEY_CTX_ctrl(pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0)
3039 EVP_PKEY_free(client_pub_pkey
);
3040 EVP_PKEY_CTX_free(pkey_ctx
);
3048 al
=SSL_AD_HANDSHAKE_FAILURE
;
3049 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE
,
3050 SSL_R_UNKNOWN_CIPHER_TYPE
);
3056 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
3057 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP)
3060 #ifndef OPENSSL_NO_ECDH
3061 EVP_PKEY_free(clnt_pub_pkey
);
3062 EC_POINT_free(clnt_ecpoint
);
3063 if (srvr_ecdh
!= NULL
)
3064 EC_KEY_free(srvr_ecdh
);
3065 BN_CTX_free(bn_ctx
);
3070 int ssl3_get_cert_verify(SSL
*s
)
3072 EVP_PKEY
*pkey
=NULL
;
3078 const EVP_MD
*md
= NULL
;
3080 EVP_MD_CTX_init(&mctx
);
3082 n
=s
->method
->ssl_get_message(s
,
3083 SSL3_ST_SR_CERT_VRFY_A
,
3084 SSL3_ST_SR_CERT_VRFY_B
,
3086 516, /* Enough for 4096 bit RSA key with TLS v1.2 */
3089 if (!ok
) return((int)n
);
3091 if (s
->session
->peer
!= NULL
)
3093 peer
=s
->session
->peer
;
3094 pkey
=X509_get_pubkey(peer
);
3095 type
=X509_certificate_type(peer
,pkey
);
3103 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE_VERIFY
)
3105 s
->s3
->tmp
.reuse_message
=1;
3106 if ((peer
!= NULL
) && (type
& EVP_PKT_SIGN
))
3108 al
=SSL_AD_UNEXPECTED_MESSAGE
;
3109 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_MISSING_VERIFY_MESSAGE
);
3118 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_NO_CLIENT_CERT_RECEIVED
);
3119 al
=SSL_AD_UNEXPECTED_MESSAGE
;
3123 if (!(type
& EVP_PKT_SIGN
))
3125 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
3126 al
=SSL_AD_ILLEGAL_PARAMETER
;
3130 if (s
->s3
->change_cipher_spec
)
3132 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_CCS_RECEIVED_EARLY
);
3133 al
=SSL_AD_UNEXPECTED_MESSAGE
;
3137 /* we now have a signature that we need to verify */
3138 p
=(unsigned char *)s
->init_msg
;
3139 /* Check for broken implementations of GOST ciphersuites */
3140 /* If key is GOST and n is exactly 64, it is bare
3141 * signature without length field */
3142 if (n
==64 && (pkey
->type
==NID_id_GostR3410_94
||
3143 pkey
->type
== NID_id_GostR3410_2001
) )
3149 if (SSL_USE_SIGALGS(s
))
3151 int rv
= tls12_check_peer_sigalg(&md
, s
, p
, pkey
);
3154 al
= SSL_AD_INTERNAL_ERROR
;
3159 al
= SSL_AD_DECODE_ERROR
;
3163 fprintf(stderr
, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md
));
3172 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_LENGTH_MISMATCH
);
3173 al
=SSL_AD_DECODE_ERROR
;
3177 j
=EVP_PKEY_size(pkey
);
3178 if ((i
> j
) || (n
> j
) || (n
<= 0))
3180 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_WRONG_SIGNATURE_SIZE
);
3181 al
=SSL_AD_DECODE_ERROR
;
3185 if (SSL_USE_SIGALGS(s
))
3189 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
3192 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
3193 al
=SSL_AD_INTERNAL_ERROR
;
3197 fprintf(stderr
, "Using TLS 1.2 with client verify alg %s\n",
3200 if (!EVP_VerifyInit_ex(&mctx
, md
, NULL
)
3201 || !EVP_VerifyUpdate(&mctx
, hdata
, hdatalen
))
3203 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
, ERR_R_EVP_LIB
);
3204 al
=SSL_AD_INTERNAL_ERROR
;
3208 if (EVP_VerifyFinal(&mctx
, p
, i
, pkey
) <= 0)
3210 al
=SSL_AD_DECRYPT_ERROR
;
3211 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_SIGNATURE
);
3216 #ifndef OPENSSL_NO_RSA
3217 if (pkey
->type
== EVP_PKEY_RSA
)
3219 i
=RSA_verify(NID_md5_sha1
, s
->s3
->tmp
.cert_verify_md
,
3220 MD5_DIGEST_LENGTH
+SHA_DIGEST_LENGTH
, p
, i
,
3224 al
=SSL_AD_DECRYPT_ERROR
;
3225 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_DECRYPT
);
3230 al
=SSL_AD_DECRYPT_ERROR
;
3231 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_RSA_SIGNATURE
);
3237 #ifndef OPENSSL_NO_DSA
3238 if (pkey
->type
== EVP_PKEY_DSA
)
3240 j
=DSA_verify(pkey
->save_type
,
3241 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
3242 SHA_DIGEST_LENGTH
,p
,i
,pkey
->pkey
.dsa
);
3246 al
=SSL_AD_DECRYPT_ERROR
;
3247 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,SSL_R_BAD_DSA_SIGNATURE
);
3253 #ifndef OPENSSL_NO_ECDSA
3254 if (pkey
->type
== EVP_PKEY_EC
)
3256 j
=ECDSA_verify(pkey
->save_type
,
3257 &(s
->s3
->tmp
.cert_verify_md
[MD5_DIGEST_LENGTH
]),
3258 SHA_DIGEST_LENGTH
,p
,i
,pkey
->pkey
.ec
);
3262 al
=SSL_AD_DECRYPT_ERROR
;
3263 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,
3264 SSL_R_BAD_ECDSA_SIGNATURE
);
3270 if (pkey
->type
== NID_id_GostR3410_94
|| pkey
->type
== NID_id_GostR3410_2001
)
3271 { unsigned char signature
[64];
3273 EVP_PKEY_CTX
*pctx
= EVP_PKEY_CTX_new(pkey
,NULL
);
3274 EVP_PKEY_verify_init(pctx
);
3276 fprintf(stderr
,"GOST signature length is %d",i
);
3278 for (idx
=0;idx
<64;idx
++) {
3279 signature
[63-idx
]=p
[idx
];
3281 j
=EVP_PKEY_verify(pctx
,signature
,64,s
->s3
->tmp
.cert_verify_md
,32);
3282 EVP_PKEY_CTX_free(pctx
);
3285 al
=SSL_AD_DECRYPT_ERROR
;
3286 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,
3287 SSL_R_BAD_ECDSA_SIGNATURE
);
3293 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY
,ERR_R_INTERNAL_ERROR
);
3294 al
=SSL_AD_UNSUPPORTED_CERTIFICATE
;
3303 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
3306 if (s
->s3
->handshake_buffer
)
3308 BIO_free(s
->s3
->handshake_buffer
);
3309 s
->s3
->handshake_buffer
= NULL
;
3310 s
->s3
->flags
&= ~TLS1_FLAGS_KEEP_HANDSHAKE
;
3312 EVP_MD_CTX_cleanup(&mctx
);
3313 EVP_PKEY_free(pkey
);
3317 int ssl3_get_client_certificate(SSL
*s
)
3319 int i
,ok
,al
,ret
= -1;
3321 unsigned long l
,nc
,llen
,n
;
3322 const unsigned char *p
,*q
;
3324 STACK_OF(X509
) *sk
=NULL
;
3326 n
=s
->method
->ssl_get_message(s
,
3333 if (!ok
) return((int)n
);
3335 if (s
->s3
->tmp
.message_type
== SSL3_MT_CLIENT_KEY_EXCHANGE
)
3337 if ( (s
->verify_mode
& SSL_VERIFY_PEER
) &&
3338 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
3340 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3341 al
=SSL_AD_HANDSHAKE_FAILURE
;
3344 /* If tls asked for a client cert, the client must return a 0 list */
3345 if ((s
->version
> SSL3_VERSION
) && s
->s3
->tmp
.cert_request
)
3347 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST
);
3348 al
=SSL_AD_UNEXPECTED_MESSAGE
;
3351 s
->s3
->tmp
.reuse_message
=1;
3355 if (s
->s3
->tmp
.message_type
!= SSL3_MT_CERTIFICATE
)
3357 al
=SSL_AD_UNEXPECTED_MESSAGE
;
3358 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_WRONG_MESSAGE_TYPE
);
3361 p
=d
=(unsigned char *)s
->init_msg
;
3363 if ((sk
=sk_X509_new_null()) == NULL
)
3365 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
3372 al
=SSL_AD_DECODE_ERROR
;
3373 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_LENGTH_MISMATCH
);
3376 for (nc
=0; nc
<llen
; )
3379 if ((l
+nc
+3) > llen
)
3381 al
=SSL_AD_DECODE_ERROR
;
3382 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
3387 x
=d2i_X509(NULL
,&p
,l
);
3390 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_ASN1_LIB
);
3395 al
=SSL_AD_DECODE_ERROR
;
3396 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERT_LENGTH_MISMATCH
);
3399 if (!sk_X509_push(sk
,x
))
3401 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
3408 if (sk_X509_num(sk
) <= 0)
3410 /* TLS does not mind 0 certs returned */
3411 if (s
->version
== SSL3_VERSION
)
3413 al
=SSL_AD_HANDSHAKE_FAILURE
;
3414 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_NO_CERTIFICATES_RETURNED
);
3417 /* Fail for TLS only if we required a certificate */
3418 else if ((s
->verify_mode
& SSL_VERIFY_PEER
) &&
3419 (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))
3421 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
3422 al
=SSL_AD_HANDSHAKE_FAILURE
;
3425 /* No client certificate so digest cached records */
3426 if (s
->s3
->handshake_buffer
&& !ssl3_digest_cached_records(s
))
3428 al
=SSL_AD_INTERNAL_ERROR
;
3435 i
=ssl_verify_cert_chain(s
,sk
);
3438 al
=ssl_verify_alarm_type(s
->verify_result
);
3439 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,SSL_R_CERTIFICATE_VERIFY_FAILED
);
3444 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, i
);
3445 al
= SSL_AD_HANDSHAKE_FAILURE
;
3448 pkey
= X509_get_pubkey(sk_X509_value(sk
, 0));
3451 al
=SSL3_AD_HANDSHAKE_FAILURE
;
3452 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
,
3453 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
3456 EVP_PKEY_free(pkey
);
3459 if (s
->session
->peer
!= NULL
) /* This should not be needed */
3460 X509_free(s
->session
->peer
);
3461 s
->session
->peer
=sk_X509_shift(sk
);
3462 s
->session
->verify_result
= s
->verify_result
;
3464 /* With the current implementation, sess_cert will always be NULL
3465 * when we arrive here. */
3466 if (s
->session
->sess_cert
== NULL
)
3468 s
->session
->sess_cert
= ssl_sess_cert_new();
3469 if (s
->session
->sess_cert
== NULL
)
3471 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
3475 if (s
->session
->sess_cert
->cert_chain
!= NULL
)
3476 sk_X509_pop_free(s
->session
->sess_cert
->cert_chain
, X509_free
);
3477 s
->session
->sess_cert
->cert_chain
=sk
;
3478 /* Inconsistency alert: cert_chain does *not* include the
3479 * peer's own certificate, while we do include it in s3_clnt.c */
3487 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
3490 if (x
!= NULL
) X509_free(x
);
3491 if (sk
!= NULL
) sk_X509_pop_free(sk
,X509_free
);
3495 int ssl3_send_server_certificate(SSL
*s
)
3499 if (s
->state
== SSL3_ST_SW_CERT_A
)
3501 cpk
=ssl_get_server_send_pkey(s
);
3504 /* VRS: allow null cert if auth == KRB5 */
3505 if ((s
->s3
->tmp
.new_cipher
->algorithm_auth
!= SSL_aKRB5
) ||
3506 (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kKRB5
))
3508 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE
,ERR_R_INTERNAL_ERROR
);
3513 if (!ssl3_output_cert_chain(s
,cpk
))
3515 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE
,ERR_R_INTERNAL_ERROR
);
3518 s
->state
=SSL3_ST_SW_CERT_B
;
3521 /* SSL3_ST_SW_CERT_B */
3522 return ssl_do_write(s
);
3525 #ifndef OPENSSL_NO_TLSEXT
3526 /* send a new session ticket (not necessarily for a new session) */
3527 int ssl3_send_newsession_ticket(SSL
*s
)
3529 if (s
->state
== SSL3_ST_SW_SESSION_TICKET_A
)
3531 unsigned char *p
, *senc
, *macstart
;
3532 const unsigned char *const_p
;
3533 int len
, slen_full
, slen
;
3538 SSL_CTX
*tctx
= s
->initial_ctx
;
3539 unsigned char iv
[EVP_MAX_IV_LENGTH
];
3540 unsigned char key_name
[16];
3542 /* get session encoding length */
3543 slen_full
= i2d_SSL_SESSION(s
->session
, NULL
);
3544 /* Some length values are 16 bits, so forget it if session is
3547 if (slen_full
> 0xFF00)
3549 senc
= OPENSSL_malloc(slen_full
);
3553 i2d_SSL_SESSION(s
->session
, &p
);
3555 /* create a fresh copy (not shared with other threads) to clean up */
3557 sess
= d2i_SSL_SESSION(NULL
, &const_p
, slen_full
);
3563 sess
->session_id_length
= 0; /* ID is irrelevant for the ticket */
3565 slen
= i2d_SSL_SESSION(sess
, NULL
);
3566 if (slen
> slen_full
) /* shouldn't ever happen */
3572 i2d_SSL_SESSION(sess
, &p
);
3573 SSL_SESSION_free(sess
);
3575 /* Grow buffer if need be: the length calculation is as
3576 * follows handshake_header_length +
3577 * 4 (ticket lifetime hint) + 2 (ticket length) +
3578 * 16 (key name) + max_iv_len (iv length) +
3579 * session_length + max_enc_block_size (max encrypted session
3580 * length) + max_md_size (HMAC).
3582 if (!BUF_MEM_grow(s
->init_buf
,
3583 SSL_HM_HEADER_LENGTH(s
) + 22 + EVP_MAX_IV_LENGTH
+
3584 EVP_MAX_BLOCK_LENGTH
+ EVP_MAX_MD_SIZE
+ slen
))
3586 p
= ssl_handshake_start(s
);
3587 EVP_CIPHER_CTX_init(&ctx
);
3588 HMAC_CTX_init(&hctx
);
3589 /* Initialize HMAC and cipher contexts. If callback present
3590 * it does all the work otherwise use generated values
3593 if (tctx
->tlsext_ticket_key_cb
)
3595 if (tctx
->tlsext_ticket_key_cb(s
, key_name
, iv
, &ctx
,
3604 RAND_pseudo_bytes(iv
, 16);
3605 EVP_EncryptInit_ex(&ctx
, EVP_aes_128_cbc(), NULL
,
3606 tctx
->tlsext_tick_aes_key
, iv
);
3607 HMAC_Init_ex(&hctx
, tctx
->tlsext_tick_hmac_key
, 16,
3608 tlsext_tick_md(), NULL
);
3609 memcpy(key_name
, tctx
->tlsext_tick_key_name
, 16);
3612 /* Ticket lifetime hint (advisory only):
3613 * We leave this unspecified for resumed session (for simplicity),
3614 * and guess that tickets for new sessions will live as long
3615 * as their sessions. */
3616 l2n(s
->hit
? 0 : s
->session
->timeout
, p
);
3618 /* Skip ticket length for now */
3620 /* Output key name */
3622 memcpy(p
, key_name
, 16);
3625 memcpy(p
, iv
, EVP_CIPHER_CTX_iv_length(&ctx
));
3626 p
+= EVP_CIPHER_CTX_iv_length(&ctx
);
3627 /* Encrypt session data */
3628 EVP_EncryptUpdate(&ctx
, p
, &len
, senc
, slen
);
3630 EVP_EncryptFinal(&ctx
, p
, &len
);
3632 EVP_CIPHER_CTX_cleanup(&ctx
);
3634 HMAC_Update(&hctx
, macstart
, p
- macstart
);
3635 HMAC_Final(&hctx
, p
, &hlen
);
3636 HMAC_CTX_cleanup(&hctx
);
3639 /* Now write out lengths: p points to end of data written */
3641 len
= p
- ssl_handshake_start(s
);
3642 ssl_set_handshake_header(s
, SSL3_MT_NEWSESSION_TICKET
, len
);
3643 /* Skip ticket lifetime hint */
3644 p
= ssl_handshake_start(s
) + 4;
3646 s
->state
=SSL3_ST_SW_SESSION_TICKET_B
;
3650 /* SSL3_ST_SW_SESSION_TICKET_B */
3651 return ssl_do_write(s
);
3654 int ssl3_send_cert_status(SSL
*s
)
3656 if (s
->state
== SSL3_ST_SW_CERT_STATUS_A
)
3659 /* Grow buffer if need be: the length calculation is as
3660 * follows 1 (message type) + 3 (message length) +
3661 * 1 (ocsp response type) + 3 (ocsp response length)
3664 if (!BUF_MEM_grow(s
->init_buf
, 8 + s
->tlsext_ocsp_resplen
))
3667 p
=(unsigned char *)s
->init_buf
->data
;
3670 *(p
++)=SSL3_MT_CERTIFICATE_STATUS
;
3671 /* message length */
3672 l2n3(s
->tlsext_ocsp_resplen
+ 4, p
);
3674 *(p
++)= s
->tlsext_status_type
;
3675 /* length of OCSP response */
3676 l2n3(s
->tlsext_ocsp_resplen
, p
);
3677 /* actual response */
3678 memcpy(p
, s
->tlsext_ocsp_resp
, s
->tlsext_ocsp_resplen
);
3679 /* number of bytes to write */
3680 s
->init_num
= 8 + s
->tlsext_ocsp_resplen
;
3681 s
->state
=SSL3_ST_SW_CERT_STATUS_B
;
3685 /* SSL3_ST_SW_CERT_STATUS_B */
3686 return(ssl3_do_write(s
,SSL3_RT_HANDSHAKE
));
3689 # ifndef OPENSSL_NO_NEXTPROTONEG
3690 /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
3691 * sets the next_proto member in s if found */
3692 int ssl3_get_next_proto(SSL
*s
)
3695 int proto_len
, padding_len
;
3697 const unsigned char *p
;
3699 /* Clients cannot send a NextProtocol message if we didn't see the
3700 * extension in their ClientHello */
3701 if (!s
->s3
->next_proto_neg_seen
)
3703 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO
,SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION
);
3707 n
=s
->method
->ssl_get_message(s
,
3708 SSL3_ST_SR_NEXT_PROTO_A
,
3709 SSL3_ST_SR_NEXT_PROTO_B
,
3711 514, /* See the payload format below */
3717 /* s->state doesn't reflect whether ChangeCipherSpec has been received
3718 * in this handshake, but s->s3->change_cipher_spec does (will be reset
3719 * by ssl3_get_finished). */
3720 if (!s
->s3
->change_cipher_spec
)
3722 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO
,SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS
);
3727 return 0; /* The body must be > 1 bytes long */
3729 p
=(unsigned char *)s
->init_msg
;
3731 /* The payload looks like:
3733 * uint8 proto[proto_len];
3734 * uint8 padding_len;
3735 * uint8 padding[padding_len];
3738 if (proto_len
+ 2 > s
->init_num
)
3740 padding_len
= p
[proto_len
+ 1];
3741 if (proto_len
+ padding_len
+ 2 != s
->init_num
)
3744 s
->next_proto_negotiated
= OPENSSL_malloc(proto_len
);
3745 if (!s
->next_proto_negotiated
)
3747 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO
,ERR_R_MALLOC_FAILURE
);
3750 memcpy(s
->next_proto_negotiated
, p
+ 1, proto_len
);
3751 s
->next_proto_negotiated_len
= proto_len
;
3757 int tls1_send_server_supplemental_data(SSL
*s
, int *skip
)
3760 if (s
->ctx
->srv_supp_data_records_count
)
3762 unsigned char *p
= NULL
;
3763 unsigned char *size_loc
= NULL
;
3764 srv_supp_data_record
*record
= NULL
;
3768 for (i
= 0; i
< s
->ctx
->srv_supp_data_records_count
; i
++)
3770 const unsigned char *out
= NULL
;
3771 unsigned short outlen
= 0;
3773 record
= &s
->ctx
->srv_supp_data_records
[i
];
3775 /* NULL callback or -1 omits supp data entry */
3778 cb_retval
= record
->fn1(s
, record
->supp_data_type
,
3779 &out
, &outlen
, &al
, record
->arg
);
3780 if (cb_retval
== -1)
3781 continue; /* skip this supp data entry */
3784 SSLerr(SSL_F_TLS1_SEND_SERVER_SUPPLEMENTAL_DATA
,ERR_R_BUF_LIB
);
3787 if (outlen
== 0 || TLSEXT_MAXLEN_supplemental_data
< outlen
+ 4 + length
)
3789 SSLerr(SSL_F_TLS1_SEND_SERVER_SUPPLEMENTAL_DATA
,ERR_R_BUF_LIB
);
3792 /* write supp data entry...
3793 * if first entry, write handshake message type
3794 * jump back to write length at end */
3797 /* 1 byte message type + 3 bytes for
3799 if (!BUF_MEM_grow_clean(s
->init_buf
, 4))
3801 SSLerr(SSL_F_TLS1_SEND_SERVER_SUPPLEMENTAL_DATA
,ERR_R_BUF_LIB
);
3804 p
= (unsigned char *)s
->init_buf
->data
;
3805 *(p
++) = SSL3_MT_SUPPLEMENTAL_DATA
;
3806 /* hold on to length field to update later */
3808 /* skip over handshake length field (3
3809 * bytes) and supp_data length field
3814 /* 2 byte supp data type + 2 byte length + outlen */
3815 if (!BUF_MEM_grow(s
->init_buf
, outlen
+ 4))
3817 SSLerr(SSL_F_TLS1_SEND_SERVER_SUPPLEMENTAL_DATA
,ERR_R_BUF_LIB
);
3820 s2n(record
->supp_data_type
, p
);
3822 memcpy(p
, out
, outlen
);
3823 /* update length to supp data type (2 bytes) +
3824 * supp data length (2 bytes) + supp data */
3825 length
+= (outlen
+ 4);
3830 /* write handshake length */
3831 l2n3(length
- 4, size_loc
);
3832 /* supp_data length */
3833 l2n3(length
- 7, size_loc
);
3834 s
->state
= SSL3_ST_SW_SUPPLEMENTAL_DATA_B
;
3835 s
->init_num
= length
;
3838 return ssl3_do_write(s
, SSL3_RT_HANDSHAKE
);
3842 /* no supp data message sent */
3848 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);
3852 int tls1_get_client_supplemental_data(SSL
*s
)
3858 const unsigned char *p
, *d
;
3859 unsigned short supp_data_entry_type
= 0;
3860 unsigned short supp_data_entry_len
= 0;
3861 unsigned long supp_data_len
= 0;
3864 n
=s
->method
->ssl_get_message(s
,
3865 SSL3_ST_SR_SUPPLEMENTAL_DATA_A
,
3866 SSL3_ST_SR_SUPPLEMENTAL_DATA_B
,
3867 SSL3_MT_SUPPLEMENTAL_DATA
,
3868 /* use default limit */
3869 TLSEXT_MAXLEN_supplemental_data
,
3872 if (!ok
) return((int)n
);
3874 p
= (unsigned char *)s
->init_msg
;
3877 /* The message cannot be empty */
3880 al
= SSL_AD_DECODE_ERROR
;
3881 SSLerr(SSL_F_TLS1_GET_CLIENT_SUPPLEMENTAL_DATA
,SSL_R_LENGTH_MISMATCH
);
3884 n2l3(p
, supp_data_len
);
3885 while (p
<d
+supp_data_len
)
3887 n2s(p
, supp_data_entry_type
);
3888 n2s(p
, supp_data_entry_len
);
3889 /* if there is a callback for this supp data type, send it */
3890 for (i
=0; i
< s
->ctx
->srv_supp_data_records_count
; i
++)
3892 if (s
->ctx
->srv_supp_data_records
[i
].supp_data_type
== supp_data_entry_type
&& s
->ctx
->srv_supp_data_records
[i
].fn2
)
3894 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
);
3897 SSLerr(SSL_F_TLS1_GET_CLIENT_SUPPLEMENTAL_DATA
, ERR_R_SSL_LIB
);
3902 p
+=supp_data_entry_len
;
3906 ssl3_send_alert(s
,SSL3_AL_FATAL
,al
);