1 /* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
151 #define REUSE_CIPHER_BUG
152 #define NETSCAPE_HANG_BUG
155 #include "ssl_locl.h"
156 #include "kssl_lcl.h"
157 #include "../crypto/constant_time_locl.h"
158 #include <openssl/buffer.h>
159 #include <openssl/rand.h>
160 #include <openssl/objects.h>
161 #include <openssl/evp.h>
162 #include <openssl/hmac.h>
163 #include <openssl/x509.h>
164 #ifndef OPENSSL_NO_DH
165 # include <openssl/dh.h>
167 #include <openssl/bn.h>
168 #ifndef OPENSSL_NO_KRB5
169 # include <openssl/krb5_asn.h>
171 #include <openssl/md5.h>
173 static const SSL_METHOD *ssl3_get_server_method(int ver);
175 static const SSL_METHOD *ssl3_get_server_method(int ver)
177 if (ver == SSL3_VERSION)
178 return (SSLv3_server_method());
183 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
185 ssl_undefined_function, ssl3_get_server_method)
187 int ssl3_accept(SSL *s)
190 unsigned long alg_k, Time = (unsigned long)time(NULL);
191 void (*cb) (const SSL *ssl, int type, int val) = NULL;
193 int new_state, state, skip = 0;
195 RAND_add(&Time, sizeof(Time), 0);
199 if (s->info_callback != NULL)
200 cb = s->info_callback;
201 else if (s->ctx->info_callback != NULL)
202 cb = s->ctx->info_callback;
204 /* init things to blank */
206 if (!SSL_in_init(s) || SSL_in_before(s))
209 if (s->cert == NULL) {
210 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
218 case SSL_ST_RENEGOTIATE:
220 /* s->state=SSL_ST_ACCEPT; */
224 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
225 case SSL_ST_OK | SSL_ST_ACCEPT:
229 cb(s, SSL_CB_HANDSHAKE_START, 1);
231 if ((s->version >> 8) != 3) {
232 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
235 s->type = SSL_ST_ACCEPT;
237 if (s->init_buf == NULL) {
238 if ((buf = BUF_MEM_new()) == NULL) {
242 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
250 if (!ssl3_setup_buffers(s)) {
256 s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE;
258 if (s->state != SSL_ST_RENEGOTIATE) {
260 * Ok, we now need to push on a buffering BIO so that the
261 * output is sent in a way that TCP likes :-)
263 if (!ssl_init_wbio_buffer(s, 1)) {
268 ssl3_init_finished_mac(s);
269 s->state = SSL3_ST_SR_CLNT_HELLO_A;
270 s->ctx->stats.sess_accept++;
271 } else if (!s->s3->send_connection_binding &&
273 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
275 * Server attempting to renegotiate with client that doesn't
276 * support secure renegotiation.
278 SSLerr(SSL_F_SSL3_ACCEPT,
279 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
280 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
285 * s->state == SSL_ST_RENEGOTIATE, we will just send a
288 s->ctx->stats.sess_accept_renegotiate++;
289 s->state = SSL3_ST_SW_HELLO_REQ_A;
293 case SSL3_ST_SW_HELLO_REQ_A:
294 case SSL3_ST_SW_HELLO_REQ_B:
297 ret = ssl3_send_hello_request(s);
300 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
301 s->state = SSL3_ST_SW_FLUSH;
304 ssl3_init_finished_mac(s);
307 case SSL3_ST_SW_HELLO_REQ_C:
308 s->state = SSL_ST_OK;
311 case SSL3_ST_SR_CLNT_HELLO_A:
312 case SSL3_ST_SR_CLNT_HELLO_B:
313 case SSL3_ST_SR_CLNT_HELLO_C:
316 ret = ssl3_get_client_hello(s);
321 s->state = SSL3_ST_SW_SRVR_HELLO_A;
325 case SSL3_ST_SW_SRVR_HELLO_A:
326 case SSL3_ST_SW_SRVR_HELLO_B:
327 ret = ssl3_send_server_hello(s);
330 #ifndef OPENSSL_NO_TLSEXT
332 if (s->tlsext_ticket_expected)
333 s->state = SSL3_ST_SW_SESSION_TICKET_A;
335 s->state = SSL3_ST_SW_CHANGE_A;
339 s->state = SSL3_ST_SW_CHANGE_A;
342 s->state = SSL3_ST_SW_CERT_A;
346 case SSL3_ST_SW_CERT_A:
347 case SSL3_ST_SW_CERT_B:
348 /* Check if it is anon DH or anon ECDH, */
349 /* normal PSK or KRB5 */
350 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
351 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
352 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) {
353 ret = ssl3_send_server_certificate(s);
356 #ifndef OPENSSL_NO_TLSEXT
357 if (s->tlsext_status_expected)
358 s->state = SSL3_ST_SW_CERT_STATUS_A;
360 s->state = SSL3_ST_SW_KEY_EXCH_A;
363 s->state = SSL3_ST_SW_KEY_EXCH_A;
369 s->state = SSL3_ST_SW_KEY_EXCH_A;
374 case SSL3_ST_SW_KEY_EXCH_A:
375 case SSL3_ST_SW_KEY_EXCH_B:
376 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
379 * clear this, it may get reset by
380 * send_server_key_exchange
382 s->s3->tmp.use_rsa_tmp = 0;
385 * only send if a DH key exchange, fortezza or RSA but we have a
386 * sign only certificate PSK: may send PSK identity hints For
387 * ECC ciphersuites, we send a serverKeyExchange message only if
388 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
389 * the server certificate contains the server's public key for
394 * PSK: send ServerKeyExchange if PSK identity hint if
397 #ifndef OPENSSL_NO_PSK
398 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
400 || (alg_k & (SSL_kDHr | SSL_kDHd | SSL_kEDH))
401 || (alg_k & SSL_kEECDH)
402 || ((alg_k & SSL_kRSA)
403 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
404 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
405 && EVP_PKEY_size(s->cert->pkeys
406 [SSL_PKEY_RSA_ENC].privatekey) *
407 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
412 ret = ssl3_send_server_key_exchange(s);
418 s->state = SSL3_ST_SW_CERT_REQ_A;
422 case SSL3_ST_SW_CERT_REQ_A:
423 case SSL3_ST_SW_CERT_REQ_B:
424 if ( /* don't request cert unless asked for it: */
425 !(s->verify_mode & SSL_VERIFY_PEER) ||
427 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
428 * during re-negotiation:
430 ((s->session->peer != NULL) &&
431 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
433 * never request cert in anonymous ciphersuites (see
434 * section "Certificate request" in SSL 3 drafts and in
437 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
439 * ... except when the application insists on
440 * verification (against the specs, but s3_clnt.c accepts
443 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
445 * never request cert in Kerberos ciphersuites
447 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
449 * With normal PSK Certificates and Certificate Requests
452 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
453 /* no cert request */
455 s->s3->tmp.cert_request = 0;
456 s->state = SSL3_ST_SW_SRVR_DONE_A;
458 s->s3->tmp.cert_request = 1;
459 ret = ssl3_send_certificate_request(s);
462 #ifndef NETSCAPE_HANG_BUG
463 s->state = SSL3_ST_SW_SRVR_DONE_A;
465 s->state = SSL3_ST_SW_FLUSH;
466 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
472 case SSL3_ST_SW_SRVR_DONE_A:
473 case SSL3_ST_SW_SRVR_DONE_B:
474 ret = ssl3_send_server_done(s);
477 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
478 s->state = SSL3_ST_SW_FLUSH;
482 case SSL3_ST_SW_FLUSH:
485 * This code originally checked to see if any data was pending
486 * using BIO_CTRL_INFO and then flushed. This caused problems as
487 * documented in PR#1939. The proposed fix doesn't completely
488 * resolve this issue as buggy implementations of
489 * BIO_CTRL_PENDING still exist. So instead we just flush
493 s->rwstate = SSL_WRITING;
494 if (BIO_flush(s->wbio) <= 0) {
498 s->rwstate = SSL_NOTHING;
500 s->state = s->s3->tmp.next_state;
503 case SSL3_ST_SR_CERT_A:
504 case SSL3_ST_SR_CERT_B:
505 /* Check for second client hello (MS SGC) */
506 ret = ssl3_check_client_hello(s);
510 s->state = SSL3_ST_SR_CLNT_HELLO_C;
512 if (s->s3->tmp.cert_request) {
513 ret = ssl3_get_client_certificate(s);
518 s->state = SSL3_ST_SR_KEY_EXCH_A;
522 case SSL3_ST_SR_KEY_EXCH_A:
523 case SSL3_ST_SR_KEY_EXCH_B:
524 ret = ssl3_get_client_key_exchange(s);
529 * For the ECDH ciphersuites when the client sends its ECDH
530 * pub key in a certificate, the CertificateVerify message is
531 * not sent. Also for GOST ciphersuites when the client uses
532 * its key from the certificate for key exchange.
534 s->state = SSL3_ST_SR_FINISHED_A;
540 s->state = SSL3_ST_SR_CERT_VRFY_A;
544 * We need to get hashes here so if there is a client cert,
545 * it can be verified FIXME - digest processing for
546 * CertificateVerify should be generalized. But it is next
549 if (s->s3->handshake_buffer)
550 if (!ssl3_digest_cached_records(s))
552 for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; dgst_num++)
553 if (s->s3->handshake_dgst[dgst_num]) {
556 s->method->ssl3_enc->cert_verify_mac(s,
565 EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
575 case SSL3_ST_SR_CERT_VRFY_A:
576 case SSL3_ST_SR_CERT_VRFY_B:
578 s->s3->flags |= SSL3_FLAGS_CCS_OK;
579 /* we should decide if we expected this one */
580 ret = ssl3_get_cert_verify(s);
584 s->state = SSL3_ST_SR_FINISHED_A;
588 case SSL3_ST_SR_FINISHED_A:
589 case SSL3_ST_SR_FINISHED_B:
590 s->s3->flags |= SSL3_FLAGS_CCS_OK;
591 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
592 SSL3_ST_SR_FINISHED_B);
595 #ifndef OPENSSL_NO_TLSEXT
596 if (s->tlsext_ticket_expected)
597 s->state = SSL3_ST_SW_SESSION_TICKET_A;
599 s->state = SSL_ST_OK;
602 s->state = SSL_ST_OK;
605 s->state = SSL3_ST_SW_CHANGE_A;
609 #ifndef OPENSSL_NO_TLSEXT
610 case SSL3_ST_SW_SESSION_TICKET_A:
611 case SSL3_ST_SW_SESSION_TICKET_B:
612 ret = ssl3_send_newsession_ticket(s);
615 s->state = SSL3_ST_SW_CHANGE_A;
619 case SSL3_ST_SW_CERT_STATUS_A:
620 case SSL3_ST_SW_CERT_STATUS_B:
621 ret = ssl3_send_cert_status(s);
624 s->state = SSL3_ST_SW_KEY_EXCH_A;
630 case SSL3_ST_SW_CHANGE_A:
631 case SSL3_ST_SW_CHANGE_B:
633 s->session->cipher = s->s3->tmp.new_cipher;
634 if (!s->method->ssl3_enc->setup_key_block(s)) {
639 ret = ssl3_send_change_cipher_spec(s,
641 SSL3_ST_SW_CHANGE_B);
645 s->state = SSL3_ST_SW_FINISHED_A;
648 if (!s->method->ssl3_enc->change_cipher_state(s,
649 SSL3_CHANGE_CIPHER_SERVER_WRITE))
657 case SSL3_ST_SW_FINISHED_A:
658 case SSL3_ST_SW_FINISHED_B:
659 ret = ssl3_send_finished(s,
660 SSL3_ST_SW_FINISHED_A,
661 SSL3_ST_SW_FINISHED_B,
663 ssl3_enc->server_finished_label,
665 ssl3_enc->server_finished_label_len);
668 s->state = SSL3_ST_SW_FLUSH;
670 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
672 s->s3->tmp.next_state = SSL_ST_OK;
677 /* clean a few things up */
678 ssl3_cleanup_key_block(s);
680 BUF_MEM_free(s->init_buf);
683 /* remove buffering on output */
684 ssl_free_wbio_buffer(s);
688 if (s->new_session == 2) { /* skipped if we just sent a
691 * actually not necessarily a 'new' session unless
692 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set
697 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
699 s->ctx->stats.sess_accept_good++;
701 s->handshake_func = ssl3_accept;
704 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
712 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE);
718 if (!s->s3->tmp.reuse_message && !skip) {
720 if ((ret = BIO_flush(s->wbio)) <= 0)
724 if ((cb != NULL) && (s->state != state)) {
725 new_state = s->state;
727 cb(s, SSL_CB_ACCEPT_LOOP, 1);
728 s->state = new_state;
734 /* BIO_flush(s->wbio); */
738 cb(s, SSL_CB_ACCEPT_EXIT, ret);
742 int ssl3_send_hello_request(SSL *s)
746 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
747 p = (unsigned char *)s->init_buf->data;
748 *(p++) = SSL3_MT_HELLO_REQUEST;
753 s->state = SSL3_ST_SW_HELLO_REQ_B;
754 /* number of bytes to write */
759 /* SSL3_ST_SW_HELLO_REQ_B */
760 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
763 int ssl3_check_client_hello(SSL *s)
769 * this function is called when we really expect a Certificate message,
770 * so permit appropriate message length
772 n = s->method->ssl_get_message(s,
775 -1, s->max_cert_list, &ok);
778 s->s3->tmp.reuse_message = 1;
779 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) {
781 * We only allow the client to restart the handshake once per
784 if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) {
785 SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO,
786 SSL_R_MULTIPLE_SGC_RESTARTS);
790 * Throw away what we have done so far in the current handshake,
791 * which will now be aborted. (A full SSL_clear would be too much.)
793 #ifndef OPENSSL_NO_DH
794 if (s->s3->tmp.dh != NULL) {
795 DH_free(s->s3->tmp.dh);
796 s->s3->tmp.dh = NULL;
799 #ifndef OPENSSL_NO_ECDH
800 if (s->s3->tmp.ecdh != NULL) {
801 EC_KEY_free(s->s3->tmp.ecdh);
802 s->s3->tmp.ecdh = NULL;
805 s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE;
811 int ssl3_get_client_hello(SSL *s)
813 int i, j, ok, al, ret = -1;
814 unsigned int cookie_len;
817 unsigned char *p, *d, *q;
819 #ifndef OPENSSL_NO_COMP
820 SSL_COMP *comp = NULL;
822 STACK_OF(SSL_CIPHER) *ciphers = NULL;
825 * We do this so that we will respond with our native type. If we are
826 * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
827 * switching should be handled by a different method. If we are SSLv3, we
828 * will respond with SSLv3, even if prompted with TLSv1.
830 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
831 s->state = SSL3_ST_SR_CLNT_HELLO_B;
834 n = s->method->ssl_get_message(s,
835 SSL3_ST_SR_CLNT_HELLO_B,
836 SSL3_ST_SR_CLNT_HELLO_C,
837 SSL3_MT_CLIENT_HELLO,
838 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
843 d = p = (unsigned char *)s->init_msg;
846 * use version from inside client hello, not from record header (may
847 * differ: see RFC 2246, Appendix E, second paragraph)
849 s->client_version = (((int)p[0]) << 8) | (int)p[1];
852 if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
853 (s->version != DTLS1_VERSION && s->client_version < s->version)) {
854 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
855 if ((s->client_version >> 8) == SSL3_VERSION_MAJOR) {
857 * similar to ssl3_get_record, send alert using remote version
860 s->version = s->client_version;
862 al = SSL_AD_PROTOCOL_VERSION;
867 * If we require cookies and this ClientHello doesn't contain one, just
868 * return since we do not want to allocate any memory yet. So check
871 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
872 unsigned int session_length, cookie_length;
874 session_length = *(p + SSL3_RANDOM_SIZE);
875 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
877 if (cookie_length == 0)
881 /* load the client random */
882 memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
883 p += SSL3_RANDOM_SIZE;
885 /* get the session-id */
890 * Versions before 0.9.7 always allow session reuse during renegotiation
891 * (i.e. when s->new_session is true), option
892 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7. Maybe
893 * this optional behaviour should always have been the default, but we
894 * cannot safely change the default behaviour (or new applications might
895 * be written that become totally unsecure when compiled with an earlier
899 && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
900 if (!ssl_get_new_session(s, 1))
903 i = ssl_get_prev_session(s, p, j, d + n);
904 if (i == 1) { /* previous session */
910 if (!ssl_get_new_session(s, 1))
917 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
922 * The ClientHello may contain a cookie even if the
923 * HelloVerify message has not been sent--make sure that it
924 * does not cause an overflow.
926 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
928 al = SSL_AD_DECODE_ERROR;
929 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
933 /* verify the cookie if appropriate option is set. */
934 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && cookie_len > 0) {
935 memcpy(s->d1->rcvd_cookie, p, cookie_len);
937 if (s->ctx->app_verify_cookie_cb != NULL) {
938 if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
940 al = SSL_AD_HANDSHAKE_FAILURE;
941 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
942 SSL_R_COOKIE_MISMATCH);
945 /* else cookie verification succeeded */
947 /* default verification */
948 else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
949 s->d1->cookie_len) != 0) {
950 al = SSL_AD_HANDSHAKE_FAILURE;
951 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
962 if ((i == 0) && (j != 0)) {
963 /* we need a cipher if we are not resuming a session */
964 al = SSL_AD_ILLEGAL_PARAMETER;
965 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
968 if ((p + i) >= (d + n)) {
969 /* not enough data */
970 al = SSL_AD_DECODE_ERROR;
971 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
974 if ((i > 0) && (ssl_bytes_to_cipher_list(s, p, i, &(ciphers))
980 /* If it is a hit, check that the cipher is in the list */
981 if ((s->hit) && (i > 0)) {
983 id = s->session->cipher->id;
986 printf("client sent %d ciphers\n", sk_num(ciphers));
988 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
989 c = sk_SSL_CIPHER_value(ciphers, i);
991 printf("client [%2d of %2d]:%s\n",
992 i, sk_num(ciphers), SSL_CIPHER_get_name(c));
1000 * Disabled because it can be used in a ciphersuite downgrade attack:
1004 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
1005 && (sk_SSL_CIPHER_num(ciphers) == 1)) {
1007 * Special case as client bug workaround: the previously used
1008 * cipher may not be in the current list, the client instead
1009 * might be trying to continue using a cipher that before wasn't
1010 * chosen due to server preferences. We'll have to reject the
1011 * connection if the cipher is not enabled, though.
1013 c = sk_SSL_CIPHER_value(ciphers, 0);
1014 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
1015 s->session->cipher = c;
1022 * we need to have the cipher in the cipher list if we are asked
1025 al = SSL_AD_ILLEGAL_PARAMETER;
1026 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1027 SSL_R_REQUIRED_CIPHER_MISSING);
1034 if ((p + i) > (d + n)) {
1035 /* not enough data */
1036 al = SSL_AD_DECODE_ERROR;
1037 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1041 for (j = 0; j < i; j++) {
1049 al = SSL_AD_DECODE_ERROR;
1050 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1053 #ifndef OPENSSL_NO_TLSEXT
1054 /* TLS extensions */
1055 if (s->version >= SSL3_VERSION) {
1056 if (!ssl_parse_clienthello_tlsext(s, &p, d, n, &al)) {
1057 /* 'al' set by ssl_parse_clienthello_tlsext */
1058 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1062 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
1063 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1068 * Check if we want to use external pre-shared secret for this handshake
1069 * for not reused session only. We need to generate server_random before
1070 * calling tls_session_secret_cb in order to allow SessionTicket
1071 * processing to use it in key derivation.
1076 Time = (unsigned long)time(NULL); /* Time */
1077 pos = s->s3->server_random;
1079 if (RAND_pseudo_bytes(pos, SSL3_RANDOM_SIZE - 4) <= 0) {
1080 al = SSL_AD_INTERNAL_ERROR;
1085 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1086 SSL_CIPHER *pref_cipher = NULL;
1088 s->session->master_key_length = sizeof(s->session->master_key);
1089 if (s->tls_session_secret_cb(s, s->session->master_key,
1090 &s->session->master_key_length, ciphers,
1092 s->tls_session_secret_cb_arg)) {
1094 s->session->ciphers = ciphers;
1095 s->session->verify_result = X509_V_OK;
1099 /* check if some cipher was preferred by call back */
1101 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1106 if (pref_cipher == NULL) {
1107 al = SSL_AD_HANDSHAKE_FAILURE;
1108 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1112 s->session->cipher = pref_cipher;
1115 sk_SSL_CIPHER_free(s->cipher_list);
1117 if (s->cipher_list_by_id)
1118 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1120 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1121 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1127 * Worst case, we will use the NULL compression, but if we have other
1128 * options, we will now look for them. We have i-1 compression
1129 * algorithms from the client, starting at q.
1131 s->s3->tmp.new_compression = NULL;
1132 #ifndef OPENSSL_NO_COMP
1133 /* This only happens if we have a cache hit */
1134 if (s->session->compress_meth != 0) {
1135 int m, comp_id = s->session->compress_meth;
1136 /* Perform sanity checks on resumed compression algorithm */
1137 /* Can't disable compression */
1138 if (s->options & SSL_OP_NO_COMPRESSION) {
1139 al = SSL_AD_INTERNAL_ERROR;
1140 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1141 SSL_R_INCONSISTENT_COMPRESSION);
1144 /* Look for resumed compression method */
1145 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1146 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1147 if (comp_id == comp->id) {
1148 s->s3->tmp.new_compression = comp;
1152 if (s->s3->tmp.new_compression == NULL) {
1153 al = SSL_AD_INTERNAL_ERROR;
1154 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1155 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1158 /* Look for resumed method in compression list */
1159 for (m = 0; m < i; m++) {
1160 if (q[m] == comp_id)
1164 al = SSL_AD_ILLEGAL_PARAMETER;
1165 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1166 SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1171 else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods) {
1172 /* See if we have a match */
1173 int m, nn, o, v, done = 0;
1175 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1176 for (m = 0; m < nn; m++) {
1177 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1179 for (o = 0; o < i; o++) {
1189 s->s3->tmp.new_compression = comp;
1195 * If compression is disabled we'd better not try to resume a session
1196 * using compression.
1198 if (s->session->compress_meth != 0) {
1199 al = SSL_AD_INTERNAL_ERROR;
1200 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1206 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1210 #ifdef OPENSSL_NO_COMP
1211 s->session->compress_meth = 0;
1213 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1215 if (s->session->ciphers != NULL)
1216 sk_SSL_CIPHER_free(s->session->ciphers);
1217 s->session->ciphers = ciphers;
1218 if (ciphers == NULL) {
1219 al = SSL_AD_ILLEGAL_PARAMETER;
1220 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_PASSED);
1224 c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1227 al = SSL_AD_HANDSHAKE_FAILURE;
1228 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1231 s->s3->tmp.new_cipher = c;
1233 /* Session-id reuse */
1234 #ifdef REUSE_CIPHER_BUG
1235 STACK_OF(SSL_CIPHER) *sk;
1236 SSL_CIPHER *nc = NULL;
1237 SSL_CIPHER *ec = NULL;
1239 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) {
1240 sk = s->session->ciphers;
1241 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1242 c = sk_SSL_CIPHER_value(sk, i);
1243 if (c->algorithm_enc & SSL_eNULL)
1245 if (SSL_C_IS_EXPORT(c))
1249 s->s3->tmp.new_cipher = nc;
1250 else if (ec != NULL)
1251 s->s3->tmp.new_cipher = ec;
1253 s->s3->tmp.new_cipher = s->session->cipher;
1256 s->s3->tmp.new_cipher = s->session->cipher;
1259 if (!ssl3_digest_cached_records(s)) {
1260 al = SSL_AD_INTERNAL_ERROR;
1265 * we now have the following setup.
1267 * cipher_list - our prefered list of ciphers
1268 * ciphers - the clients prefered list of ciphers
1269 * compression - basically ignored right now
1270 * ssl version is set - sslv3
1271 * s->session - The ssl session has been setup.
1272 * s->hit - session reuse flag
1273 * s->tmp.new_cipher - the new cipher to use.
1276 #ifndef OPENSSL_NO_TLSEXT
1277 /* Handles TLS extensions that we couldn't check earlier */
1278 if (s->version >= SSL3_VERSION) {
1279 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1280 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1290 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1293 if (ciphers != NULL)
1294 sk_SSL_CIPHER_free(ciphers);
1298 int ssl3_send_server_hello(SSL *s)
1301 unsigned char *p, *d;
1304 #ifdef OPENSSL_NO_TLSEXT
1308 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1309 buf = (unsigned char *)s->init_buf->data;
1310 #ifdef OPENSSL_NO_TLSEXT
1311 p = s->s3->server_random;
1312 /* Generate server_random if it was not needed previously */
1313 Time = (unsigned long)time(NULL); /* Time */
1315 if (RAND_pseudo_bytes(p, SSL3_RANDOM_SIZE - 4) <= 0)
1318 /* Do the message type and length last */
1321 *(p++) = s->version >> 8;
1322 *(p++) = s->version & 0xff;
1325 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1326 p += SSL3_RANDOM_SIZE;
1329 * now in theory we have 3 options to sending back the session id.
1330 * If it is a re-use, we send back the old session-id, if it is a new
1331 * session, we send back the new session-id or we send back a 0
1332 * length session-id if we want it to be single use. Currently I will
1333 * not implement the '0' length session-id 12-Jan-98 - I'll now
1334 * support the '0' length stuff. We also have an additional case
1335 * where stateless session resumption is successful: we always send
1336 * back the old session id. In this case s->hit is non zero: this can
1337 * only happen if stateless session resumption is succesful if session
1338 * caching is disabled so existing functionality is unaffected.
1340 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1342 s->session->session_id_length = 0;
1344 sl = s->session->session_id_length;
1345 if (sl > (int)sizeof(s->session->session_id)) {
1346 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1350 memcpy(p, s->session->session_id, sl);
1353 /* put the cipher */
1354 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1357 /* put the compression method */
1358 #ifdef OPENSSL_NO_COMP
1361 if (s->s3->tmp.new_compression == NULL)
1364 *(p++) = s->s3->tmp.new_compression->id;
1366 #ifndef OPENSSL_NO_TLSEXT
1367 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1368 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1372 ssl_add_serverhello_tlsext(s, p,
1373 buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
1375 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1382 *(d++) = SSL3_MT_SERVER_HELLO;
1385 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1386 /* number of bytes to write */
1387 s->init_num = p - buf;
1391 /* SSL3_ST_SW_SRVR_HELLO_B */
1392 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1395 int ssl3_send_server_done(SSL *s)
1399 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1400 p = (unsigned char *)s->init_buf->data;
1403 *(p++) = SSL3_MT_SERVER_DONE;
1408 s->state = SSL3_ST_SW_SRVR_DONE_B;
1409 /* number of bytes to write */
1414 /* SSL3_ST_SW_SRVR_DONE_B */
1415 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1418 int ssl3_send_server_key_exchange(SSL *s)
1420 #ifndef OPENSSL_NO_RSA
1424 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1427 #ifndef OPENSSL_NO_DH
1428 DH *dh = NULL, *dhp;
1430 #ifndef OPENSSL_NO_ECDH
1431 EC_KEY *ecdh = NULL, *ecdhp;
1432 unsigned char *encodedPoint = NULL;
1435 BN_CTX *bn_ctx = NULL;
1438 unsigned char *p, *d;
1448 EVP_MD_CTX_init(&md_ctx);
1449 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1450 type = s->s3->tmp.new_cipher->algorithm_mkey;
1455 r[0] = r[1] = r[2] = r[3] = NULL;
1457 #ifndef OPENSSL_NO_RSA
1458 if (type & SSL_kRSA) {
1459 rsa = cert->rsa_tmp;
1460 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1461 rsa = s->cert->rsa_tmp_cb(s,
1462 SSL_C_IS_EXPORT(s->s3->
1464 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1467 al = SSL_AD_HANDSHAKE_FAILURE;
1468 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1469 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1473 cert->rsa_tmp = rsa;
1476 al = SSL_AD_HANDSHAKE_FAILURE;
1477 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1478 SSL_R_MISSING_TMP_RSA_KEY);
1483 s->s3->tmp.use_rsa_tmp = 1;
1486 #ifndef OPENSSL_NO_DH
1487 if (type & SSL_kEDH) {
1489 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1490 dhp = s->cert->dh_tmp_cb(s,
1491 SSL_C_IS_EXPORT(s->s3->
1493 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1496 al = SSL_AD_HANDSHAKE_FAILURE;
1497 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1498 SSL_R_MISSING_TMP_DH_KEY);
1502 if (s->s3->tmp.dh != NULL) {
1503 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1504 ERR_R_INTERNAL_ERROR);
1508 if ((dh = DHparams_dup(dhp)) == NULL) {
1509 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1514 if ((dhp->pub_key == NULL ||
1515 dhp->priv_key == NULL ||
1516 (s->options & SSL_OP_SINGLE_DH_USE))) {
1517 if (!DH_generate_key(dh)) {
1518 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1522 dh->pub_key = BN_dup(dhp->pub_key);
1523 dh->priv_key = BN_dup(dhp->priv_key);
1524 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1525 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1534 #ifndef OPENSSL_NO_ECDH
1535 if (type & SSL_kEECDH) {
1536 const EC_GROUP *group;
1538 ecdhp = cert->ecdh_tmp;
1539 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) {
1540 ecdhp = s->cert->ecdh_tmp_cb(s,
1541 SSL_C_IS_EXPORT(s->s3->
1543 SSL_C_EXPORT_PKEYLENGTH(s->
1544 s3->tmp.new_cipher));
1546 if (ecdhp == NULL) {
1547 al = SSL_AD_HANDSHAKE_FAILURE;
1548 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1549 SSL_R_MISSING_TMP_ECDH_KEY);
1553 if (s->s3->tmp.ecdh != NULL) {
1554 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1555 ERR_R_INTERNAL_ERROR);
1559 /* Duplicate the ECDH structure. */
1560 if (ecdhp == NULL) {
1561 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1564 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1565 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1569 s->s3->tmp.ecdh = ecdh;
1570 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1571 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1572 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1573 if (!EC_KEY_generate_key(ecdh)) {
1574 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1580 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1581 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1582 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1583 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1587 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1588 (EC_GROUP_get_degree(group) > 163)) {
1589 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1590 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1595 * XXX: For now, we only support ephemeral ECDH keys over named
1596 * (not generic) curves. For supported named curves, curve_id is
1600 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1602 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1603 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1608 * Encode the public key. First check the size of encoding and
1609 * allocate memory accordingly.
1611 encodedlen = EC_POINT_point2oct(group,
1612 EC_KEY_get0_public_key(ecdh),
1613 POINT_CONVERSION_UNCOMPRESSED,
1616 encodedPoint = (unsigned char *)
1617 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1618 bn_ctx = BN_CTX_new();
1619 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1620 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1621 ERR_R_MALLOC_FAILURE);
1625 encodedlen = EC_POINT_point2oct(group,
1626 EC_KEY_get0_public_key(ecdh),
1627 POINT_CONVERSION_UNCOMPRESSED,
1628 encodedPoint, encodedlen, bn_ctx);
1630 if (encodedlen == 0) {
1631 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1635 BN_CTX_free(bn_ctx);
1639 * XXX: For now, we only support named (not generic) curves in
1640 * ECDH ephemeral key exchanges. In this situation, we need four
1641 * additional bytes to encode the entire ServerECDHParams
1647 * We'll generate the serverKeyExchange message explicitly so we
1648 * can set these to NULLs
1655 #endif /* !OPENSSL_NO_ECDH */
1656 #ifndef OPENSSL_NO_PSK
1657 if (type & SSL_kPSK) {
1659 * reserve size for record length and PSK identity hint
1661 n += 2 + strlen(s->ctx->psk_identity_hint);
1663 #endif /* !OPENSSL_NO_PSK */
1665 al = SSL_AD_HANDSHAKE_FAILURE;
1666 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1667 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1670 for (i = 0; r[i] != NULL; i++) {
1671 nr[i] = BN_num_bytes(r[i]);
1675 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1676 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
1677 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher))
1679 al = SSL_AD_DECODE_ERROR;
1682 kn = EVP_PKEY_size(pkey);
1688 if (!BUF_MEM_grow_clean(buf, n + 4 + kn)) {
1689 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1692 d = (unsigned char *)s->init_buf->data;
1695 for (i = 0; r[i] != NULL; i++) {
1701 #ifndef OPENSSL_NO_ECDH
1702 if (type & SSL_kEECDH) {
1704 * XXX: For now, we only support named (not generic) curves. In
1705 * this situation, the serverKeyExchange message has: [1 byte
1706 * CurveType], [2 byte CurveName] [1 byte length of encoded
1707 * point], followed by the actual encoded point itself
1709 *p = NAMED_CURVE_TYPE;
1717 memcpy((unsigned char *)p,
1718 (unsigned char *)encodedPoint, encodedlen);
1719 OPENSSL_free(encodedPoint);
1720 encodedPoint = NULL;
1725 #ifndef OPENSSL_NO_PSK
1726 if (type & SSL_kPSK) {
1727 /* copy PSK identity hint */
1728 s2n(strlen(s->ctx->psk_identity_hint), p);
1729 strncpy((char *)p, s->ctx->psk_identity_hint,
1730 strlen(s->ctx->psk_identity_hint));
1731 p += strlen(s->ctx->psk_identity_hint);
1738 * n is the length of the params, they start at &(d[4]) and p
1739 * points to the space at the end.
1741 #ifndef OPENSSL_NO_RSA
1742 if (pkey->type == EVP_PKEY_RSA) {
1745 for (num = 2; num > 0; num--) {
1746 EVP_DigestInit_ex(&md_ctx, (num == 2)
1747 ? s->ctx->md5 : s->ctx->sha1, NULL);
1748 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1750 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1752 EVP_DigestUpdate(&md_ctx, &(d[4]), n);
1753 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1757 if (RSA_sign(NID_md5_sha1, md_buf, j,
1758 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1759 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
1766 #if !defined(OPENSSL_NO_DSA)
1767 if (pkey->type == EVP_PKEY_DSA) {
1769 EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL);
1770 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1772 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1774 EVP_SignUpdate(&md_ctx, &(d[4]), n);
1775 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1776 (unsigned int *)&i, pkey)) {
1777 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA);
1784 #if !defined(OPENSSL_NO_ECDSA)
1785 if (pkey->type == EVP_PKEY_EC) {
1786 /* let's do ECDSA */
1787 EVP_SignInit_ex(&md_ctx, EVP_ecdsa(), NULL);
1788 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1790 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1792 EVP_SignUpdate(&md_ctx, &(d[4]), n);
1793 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1794 (unsigned int *)&i, pkey)) {
1795 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1804 /* Is this error check actually needed? */
1805 al = SSL_AD_HANDSHAKE_FAILURE;
1806 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1807 SSL_R_UNKNOWN_PKEY_TYPE);
1812 *(d++) = SSL3_MT_SERVER_KEY_EXCHANGE;
1816 * we should now have things packed up, so lets send it off
1818 s->init_num = n + 4;
1822 s->state = SSL3_ST_SW_KEY_EXCH_B;
1823 EVP_MD_CTX_cleanup(&md_ctx);
1824 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1826 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1828 #ifndef OPENSSL_NO_ECDH
1829 if (encodedPoint != NULL)
1830 OPENSSL_free(encodedPoint);
1831 BN_CTX_free(bn_ctx);
1833 EVP_MD_CTX_cleanup(&md_ctx);
1837 int ssl3_send_certificate_request(SSL *s)
1839 unsigned char *p, *d;
1840 int i, j, nl, off, n;
1841 STACK_OF(X509_NAME) *sk = NULL;
1845 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1848 d = p = (unsigned char *)&(buf->data[4]);
1850 /* get the list of acceptable cert types */
1852 n = ssl3_get_req_cert_type(s, p);
1861 sk = SSL_get_client_CA_list(s);
1864 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1865 name = sk_X509_NAME_value(sk, i);
1866 j = i2d_X509_NAME(name, NULL);
1867 if (!BUF_MEM_grow_clean(buf, 4 + n + j + 2)) {
1868 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
1872 p = (unsigned char *)&(buf->data[4 + n]);
1873 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
1875 i2d_X509_NAME(name, &p);
1880 i2d_X509_NAME(name, &p);
1889 /* else no CA names */
1890 p = (unsigned char *)&(buf->data[4 + off]);
1893 d = (unsigned char *)buf->data;
1894 *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
1898 * we should now have things packed up, so lets send it off
1901 s->init_num = n + 4;
1903 #ifdef NETSCAPE_HANG_BUG
1904 if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) {
1905 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB);
1908 p = (unsigned char *)s->init_buf->data + s->init_num;
1911 *(p++) = SSL3_MT_SERVER_DONE;
1918 s->state = SSL3_ST_SW_CERT_REQ_B;
1921 /* SSL3_ST_SW_CERT_REQ_B */
1922 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1927 int ssl3_get_client_key_exchange(SSL *s)
1931 unsigned long alg_k;
1933 #ifndef OPENSSL_NO_RSA
1935 EVP_PKEY *pkey = NULL;
1937 #ifndef OPENSSL_NO_DH
1941 #ifndef OPENSSL_NO_KRB5
1943 #endif /* OPENSSL_NO_KRB5 */
1945 #ifndef OPENSSL_NO_ECDH
1946 EC_KEY *srvr_ecdh = NULL;
1947 EVP_PKEY *clnt_pub_pkey = NULL;
1948 EC_POINT *clnt_ecpoint = NULL;
1949 BN_CTX *bn_ctx = NULL;
1952 n = s->method->ssl_get_message(s,
1953 SSL3_ST_SR_KEY_EXCH_A,
1954 SSL3_ST_SR_KEY_EXCH_B,
1955 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
1959 p = (unsigned char *)s->init_msg;
1961 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1963 #ifndef OPENSSL_NO_RSA
1964 if (alg_k & SSL_kRSA) {
1965 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1967 unsigned char decrypt_good, version_good;
1970 /* FIX THIS UP EAY EAY EAY EAY */
1971 if (s->s3->tmp.use_rsa_tmp) {
1972 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1973 rsa = s->cert->rsa_tmp;
1975 * Don't do a callback because rsa_tmp should be sent already
1978 al = SSL_AD_HANDSHAKE_FAILURE;
1979 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1980 SSL_R_MISSING_TMP_RSA_PKEY);
1985 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1986 if ((pkey == NULL) ||
1987 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
1988 al = SSL_AD_HANDSHAKE_FAILURE;
1989 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1990 SSL_R_MISSING_RSA_CERTIFICATE);
1993 rsa = pkey->pkey.rsa;
1996 /* TLS and [incidentally] DTLS{0xFEFF} */
1997 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) {
2000 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
2001 al = SSL_AD_DECODE_ERROR;
2002 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2003 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2012 * Reject overly short RSA ciphertext because we want to be sure
2013 * that the buffer size makes it safe to iterate over the entire
2014 * size of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The
2015 * actual expected size is larger due to RSA padding, but the
2016 * bound is sufficient to be safe.
2018 if (n < SSL_MAX_MASTER_KEY_LENGTH) {
2019 al = SSL_AD_DECRYPT_ERROR;
2020 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2021 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2026 * We must not leak whether a decryption failure occurs because of
2027 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2028 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2029 * generates a random premaster secret for the case that the decrypt
2030 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2034 * should be RAND_bytes, but we cannot work around a failure.
2036 if (RAND_pseudo_bytes(rand_premaster_secret,
2037 sizeof(rand_premaster_secret)) <= 0)
2040 RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
2044 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2045 * be 0xff if so and zero otherwise.
2048 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2051 * If the version in the decrypted pre-master secret is correct then
2052 * version_good will be 0xff, otherwise it'll be zero. The
2053 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2054 * (http://eprint.iacr.org/2003/052/) exploits the version number
2055 * check as a "bad version oracle". Thus version checks are done in
2056 * constant time and are treated like any other decryption error.
2059 constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
2061 constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
2064 * The premaster secret must contain the same version number as the
2065 * ClientHello to detect version rollback attacks (strangely, the
2066 * protocol does not offer such protection for DH ciphersuites).
2067 * However, buggy clients exist that send the negotiated protocol
2068 * version instead if the server does not support the requested
2069 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2072 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2073 unsigned char workaround_good;
2075 constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
2077 constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
2078 version_good |= workaround_good;
2082 * Both decryption and version must be good for decrypt_good to
2083 * remain non-zero (0xff).
2085 decrypt_good &= version_good;
2088 * Now copy rand_premaster_secret over from p using
2089 * decrypt_good_mask. If decryption failed, then p does not
2090 * contain valid plaintext, however, a check above guarantees
2091 * it is still sufficiently large to read from.
2093 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2094 p[j] = constant_time_select_8(decrypt_good, p[j],
2095 rand_premaster_secret[j]);
2098 s->session->master_key_length =
2099 s->method->ssl3_enc->generate_master_secret(s,
2101 session->master_key,
2104 (rand_premaster_secret));
2105 OPENSSL_cleanse(p, sizeof(rand_premaster_secret));
2108 #ifndef OPENSSL_NO_DH
2109 if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
2112 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2113 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2114 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2122 if (n == 0L) { /* the parameters are in the cert */
2123 al = SSL_AD_HANDSHAKE_FAILURE;
2124 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2125 SSL_R_UNABLE_TO_DECODE_DH_CERTS);
2128 if (s->s3->tmp.dh == NULL) {
2129 al = SSL_AD_HANDSHAKE_FAILURE;
2130 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2131 SSL_R_MISSING_TMP_DH_KEY);
2134 dh_srvr = s->s3->tmp.dh;
2137 pub = BN_bin2bn(p, i, NULL);
2139 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2143 i = DH_compute_key(p, pub, dh_srvr);
2146 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2151 DH_free(s->s3->tmp.dh);
2152 s->s3->tmp.dh = NULL;
2156 s->session->master_key_length =
2157 s->method->ssl3_enc->generate_master_secret(s,
2159 session->master_key,
2161 OPENSSL_cleanse(p, i);
2164 #ifndef OPENSSL_NO_KRB5
2165 if (alg_k & SSL_kKRB5) {
2166 krb5_error_code krb5rc;
2167 krb5_data enc_ticket;
2168 krb5_data authenticator;
2170 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2171 EVP_CIPHER_CTX ciph_ctx;
2172 const EVP_CIPHER *enc = NULL;
2173 unsigned char iv[EVP_MAX_IV_LENGTH];
2174 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_BLOCK_LENGTH];
2176 krb5_timestamp authtime = 0;
2177 krb5_ticket_times ttimes;
2179 EVP_CIPHER_CTX_init(&ciph_ctx);
2182 kssl_ctx = kssl_ctx_new();
2185 enc_ticket.length = i;
2187 if (n < (long)(enc_ticket.length + 6)) {
2188 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2189 SSL_R_DATA_LENGTH_TOO_LONG);
2193 enc_ticket.data = (char *)p;
2194 p += enc_ticket.length;
2197 authenticator.length = i;
2199 if (n < (long)(enc_ticket.length + authenticator.length + 6)) {
2200 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2201 SSL_R_DATA_LENGTH_TOO_LONG);
2205 authenticator.data = (char *)p;
2206 p += authenticator.length;
2210 enc_pms.data = (char *)p;
2211 p += enc_pms.length;
2214 * Note that the length is checked again below, ** after decryption
2216 if (enc_pms.length > sizeof pms) {
2217 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2218 SSL_R_DATA_LENGTH_TOO_LONG);
2222 if (n != (long)(enc_ticket.length + authenticator.length +
2223 enc_pms.length + 6)) {
2224 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2225 SSL_R_DATA_LENGTH_TOO_LONG);
2229 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2232 printf("kssl_sget_tkt rtn %d [%d]\n", krb5rc, kssl_err.reason);
2234 printf("kssl_err text= %s\n", kssl_err.text);
2235 # endif /* KSSL_DEBUG */
2236 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2241 * Note: no authenticator is not considered an error, ** but will
2242 * return authtime == 0.
2244 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2245 &authtime, &kssl_err)) != 0) {
2247 printf("kssl_check_authent rtn %d [%d]\n",
2248 krb5rc, kssl_err.reason);
2250 printf("kssl_err text= %s\n", kssl_err.text);
2251 # endif /* KSSL_DEBUG */
2252 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2256 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) {
2257 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2261 kssl_ctx_show(kssl_ctx);
2262 # endif /* KSSL_DEBUG */
2264 enc = kssl_map_enc(kssl_ctx->enctype);
2268 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2270 if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv)) {
2271 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2272 SSL_R_DECRYPTION_FAILED);
2275 if (!EVP_DecryptUpdate(&ciph_ctx, pms, &outl,
2276 (unsigned char *)enc_pms.data, enc_pms.length))
2278 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2279 SSL_R_DECRYPTION_FAILED);
2282 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2283 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2284 SSL_R_DATA_LENGTH_TOO_LONG);
2287 if (!EVP_DecryptFinal_ex(&ciph_ctx, &(pms[outl]), &padl)) {
2288 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2289 SSL_R_DECRYPTION_FAILED);
2293 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2294 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2295 SSL_R_DATA_LENGTH_TOO_LONG);
2298 if (!((pms[0] == (s->client_version >> 8))
2299 && (pms[1] == (s->client_version & 0xff)))) {
2301 * The premaster secret must contain the same version number as
2302 * the ClientHello to detect version rollback attacks (strangely,
2303 * the protocol does not offer such protection for DH
2304 * ciphersuites). However, buggy clients exist that send random
2305 * bytes instead of the protocol version. If
2306 * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2307 * (Perhaps we should have a separate BUG value for the Kerberos
2310 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) {
2311 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2312 SSL_AD_DECODE_ERROR);
2317 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2319 s->session->master_key_length =
2320 s->method->ssl3_enc->generate_master_secret(s,
2322 session->master_key,
2325 if (kssl_ctx->client_princ) {
2326 size_t len = strlen(kssl_ctx->client_princ);
2327 if (len < SSL_MAX_KRB5_PRINCIPAL_LENGTH) {
2328 s->session->krb5_client_princ_len = len;
2329 memcpy(s->session->krb5_client_princ, kssl_ctx->client_princ,
2334 /*- Was doing kssl_ctx_free() here,
2335 * but it caused problems for apache.
2336 * kssl_ctx = kssl_ctx_free(kssl_ctx);
2337 * if (s->kssl_ctx) s->kssl_ctx = NULL;
2340 #endif /* OPENSSL_NO_KRB5 */
2342 #ifndef OPENSSL_NO_ECDH
2343 if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
2347 const EC_GROUP *group;
2348 const BIGNUM *priv_key;
2350 /* initialize structures for server's ECDH key pair */
2351 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2352 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2356 /* Let's get server private key and group information */
2357 if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2358 /* use the certificate */
2359 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2362 * use the ephermeral values we saved when generating the
2363 * ServerKeyExchange msg.
2365 tkey = s->s3->tmp.ecdh;
2368 group = EC_KEY_get0_group(tkey);
2369 priv_key = EC_KEY_get0_private_key(tkey);
2371 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2372 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2373 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2377 /* Let's get client's public key */
2378 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2379 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2384 /* Client Publickey was in Client Certificate */
2386 if (alg_k & SSL_kEECDH) {
2387 al = SSL_AD_HANDSHAKE_FAILURE;
2388 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2389 SSL_R_MISSING_TMP_ECDH_KEY);
2392 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2393 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2395 * XXX: For now, we do not support client authentication
2396 * using ECDH certificates so this branch (n == 0L) of the
2397 * code is never executed. When that support is added, we
2398 * ought to ensure the key received in the certificate is
2399 * authorized for key agreement. ECDH_compute_key implicitly
2400 * checks that the two ECDH shares are for the same group.
2402 al = SSL_AD_HANDSHAKE_FAILURE;
2403 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2404 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2408 if (EC_POINT_copy(clnt_ecpoint,
2409 EC_KEY_get0_public_key(clnt_pub_pkey->
2411 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2414 ret = 2; /* Skip certificate verify processing */
2417 * Get client's public key from encoded point in the
2418 * ClientKeyExchange message.
2420 if ((bn_ctx = BN_CTX_new()) == NULL) {
2421 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2422 ERR_R_MALLOC_FAILURE);
2426 /* Get encoded point length */
2430 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2433 if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2434 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2438 * p is pointing to somewhere in the buffer currently, so set it
2441 p = (unsigned char *)s->init_buf->data;
2444 /* Compute the shared pre-master secret */
2445 field_size = EC_GROUP_get_degree(group);
2446 if (field_size <= 0) {
2447 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2450 i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2453 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2457 EVP_PKEY_free(clnt_pub_pkey);
2458 EC_POINT_free(clnt_ecpoint);
2459 EC_KEY_free(srvr_ecdh);
2460 BN_CTX_free(bn_ctx);
2461 EC_KEY_free(s->s3->tmp.ecdh);
2462 s->s3->tmp.ecdh = NULL;
2464 /* Compute the master secret */
2465 s->session->master_key_length =
2466 s->method->ssl3_enc->generate_master_secret(s,
2468 session->master_key,
2471 OPENSSL_cleanse(p, i);
2475 #ifndef OPENSSL_NO_PSK
2476 if (alg_k & SSL_kPSK) {
2477 unsigned char *t = NULL;
2478 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
2479 unsigned int pre_ms_len = 0, psk_len = 0;
2481 char tmp_id[PSK_MAX_IDENTITY_LEN + 1];
2483 al = SSL_AD_HANDSHAKE_FAILURE;
2487 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2490 if (i > PSK_MAX_IDENTITY_LEN) {
2491 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2492 SSL_R_DATA_LENGTH_TOO_LONG);
2495 if (s->psk_server_callback == NULL) {
2496 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2497 SSL_R_PSK_NO_SERVER_CB);
2502 * Create guaranteed NULL-terminated identity string for the callback
2504 memcpy(tmp_id, p, i);
2505 memset(tmp_id + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
2506 psk_len = s->psk_server_callback(s, tmp_id,
2508 sizeof(psk_or_pre_ms));
2509 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN + 1);
2511 if (psk_len > PSK_MAX_PSK_LEN) {
2512 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2514 } else if (psk_len == 0) {
2516 * PSK related to the given identity not found
2518 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2519 SSL_R_PSK_IDENTITY_NOT_FOUND);
2520 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2524 /* create PSK pre_master_secret */
2525 pre_ms_len = 2 + psk_len + 2 + psk_len;
2527 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
2529 memset(t, 0, psk_len);
2533 if (s->session->psk_identity != NULL)
2534 OPENSSL_free(s->session->psk_identity);
2535 s->session->psk_identity = BUF_strdup((char *)p);
2536 if (s->session->psk_identity == NULL) {
2537 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2541 if (s->session->psk_identity_hint != NULL)
2542 OPENSSL_free(s->session->psk_identity_hint);
2543 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2544 if (s->ctx->psk_identity_hint != NULL &&
2545 s->session->psk_identity_hint == NULL) {
2546 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2550 s->session->master_key_length =
2551 s->method->ssl3_enc->generate_master_secret(s,
2553 session->master_key,
2558 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2563 if (alg_k & SSL_kGOST) {
2565 EVP_PKEY_CTX *pkey_ctx;
2566 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2567 unsigned char premaster_secret[32], *start;
2568 size_t outlen = 32, inlen;
2569 unsigned long alg_a;
2573 /* Get our certificate private key */
2574 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2575 if (alg_a & SSL_aGOST94)
2576 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
2577 else if (alg_a & SSL_aGOST01)
2578 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2580 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2581 EVP_PKEY_decrypt_init(pkey_ctx);
2583 * If client certificate is present and is of the same type, maybe
2584 * use it for key exchange. Don't mind errors from
2585 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2586 * client certificate for authorization only.
2588 client_pub_pkey = X509_get_pubkey(s->session->peer);
2589 if (client_pub_pkey) {
2590 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2593 /* Decrypt session key */
2595 ((const unsigned char **)&p, &Tlen, &Ttag, &Tclass,
2596 n) != V_ASN1_CONSTRUCTED || Ttag != V_ASN1_SEQUENCE
2597 || Tclass != V_ASN1_UNIVERSAL) {
2598 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2599 SSL_R_DECRYPTION_FAILED);
2604 if (EVP_PKEY_decrypt
2605 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2606 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2607 SSL_R_DECRYPTION_FAILED);
2610 /* Generate master secret */
2611 s->session->master_key_length =
2612 s->method->ssl3_enc->generate_master_secret(s,
2614 session->master_key,
2615 premaster_secret, 32);
2616 /* Check if pubkey from client certificate was used */
2617 if (EVP_PKEY_CTX_ctrl
2618 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2623 EVP_PKEY_free(client_pub_pkey);
2624 EVP_PKEY_CTX_free(pkey_ctx);
2630 al = SSL_AD_HANDSHAKE_FAILURE;
2631 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2637 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2638 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
2641 #ifndef OPENSSL_NO_ECDH
2642 EVP_PKEY_free(clnt_pub_pkey);
2643 EC_POINT_free(clnt_ecpoint);
2644 if (srvr_ecdh != NULL)
2645 EC_KEY_free(srvr_ecdh);
2646 BN_CTX_free(bn_ctx);
2651 int ssl3_get_cert_verify(SSL *s)
2653 EVP_PKEY *pkey = NULL;
2655 int al, ok, ret = 0;
2660 n = s->method->ssl_get_message(s,
2661 SSL3_ST_SR_CERT_VRFY_A,
2662 SSL3_ST_SR_CERT_VRFY_B,
2663 -1, SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2668 if (s->session->peer != NULL) {
2669 peer = s->session->peer;
2670 pkey = X509_get_pubkey(peer);
2671 type = X509_certificate_type(peer, pkey);
2677 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) {
2678 s->s3->tmp.reuse_message = 1;
2680 al = SSL_AD_UNEXPECTED_MESSAGE;
2681 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_MISSING_VERIFY_MESSAGE);
2689 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_NO_CLIENT_CERT_RECEIVED);
2690 al = SSL_AD_UNEXPECTED_MESSAGE;
2694 if (!(type & EVP_PKT_SIGN)) {
2695 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2696 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2697 al = SSL_AD_ILLEGAL_PARAMETER;
2701 if (s->s3->change_cipher_spec) {
2702 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_CCS_RECEIVED_EARLY);
2703 al = SSL_AD_UNEXPECTED_MESSAGE;
2707 /* we now have a signature that we need to verify */
2708 p = (unsigned char *)s->init_msg;
2709 /* Check for broken implementations of GOST ciphersuites */
2711 * If key is GOST and n is exactly 64, it is bare signature without
2714 if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
2715 pkey->type == NID_id_GostR3410_2001)) {
2721 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2722 al = SSL_AD_DECODE_ERROR;
2726 j = EVP_PKEY_size(pkey);
2727 if ((i > j) || (n > j) || (n <= 0)) {
2728 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2729 al = SSL_AD_DECODE_ERROR;
2732 #ifndef OPENSSL_NO_RSA
2733 if (pkey->type == EVP_PKEY_RSA) {
2734 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2735 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
2738 al = SSL_AD_DECRYPT_ERROR;
2739 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
2743 al = SSL_AD_DECRYPT_ERROR;
2744 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
2749 #ifndef OPENSSL_NO_DSA
2750 if (pkey->type == EVP_PKEY_DSA) {
2751 j = DSA_verify(pkey->save_type,
2752 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2753 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
2756 al = SSL_AD_DECRYPT_ERROR;
2757 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
2762 #ifndef OPENSSL_NO_ECDSA
2763 if (pkey->type == EVP_PKEY_EC) {
2764 j = ECDSA_verify(pkey->save_type,
2765 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2766 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
2769 al = SSL_AD_DECRYPT_ERROR;
2770 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
2775 if (pkey->type == NID_id_GostR3410_94
2776 || pkey->type == NID_id_GostR3410_2001) {
2777 unsigned char signature[64];
2779 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
2780 EVP_PKEY_verify_init(pctx);
2782 fprintf(stderr, "GOST signature length is %d", i);
2784 for (idx = 0; idx < 64; idx++) {
2785 signature[63 - idx] = p[idx];
2787 j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
2789 EVP_PKEY_CTX_free(pctx);
2791 al = SSL_AD_DECRYPT_ERROR;
2792 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
2796 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2797 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2804 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2807 EVP_PKEY_free(pkey);
2811 int ssl3_get_client_certificate(SSL *s)
2813 int i, ok, al, ret = -1;
2815 unsigned long l, nc, llen, n;
2816 const unsigned char *p, *q;
2818 STACK_OF(X509) *sk = NULL;
2820 n = s->method->ssl_get_message(s,
2823 -1, s->max_cert_list, &ok);
2828 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2829 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2830 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2831 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2832 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2833 al = SSL_AD_HANDSHAKE_FAILURE;
2837 * If tls asked for a client cert, the client must return a 0 list
2839 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
2840 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2841 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2842 al = SSL_AD_UNEXPECTED_MESSAGE;
2845 s->s3->tmp.reuse_message = 1;
2849 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2850 al = SSL_AD_UNEXPECTED_MESSAGE;
2851 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
2854 p = d = (unsigned char *)s->init_msg;
2856 if ((sk = sk_X509_new_null()) == NULL) {
2857 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2862 if (llen + 3 != n) {
2863 al = SSL_AD_DECODE_ERROR;
2864 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
2867 for (nc = 0; nc < llen;) {
2869 if ((l + nc + 3) > llen) {
2870 al = SSL_AD_DECODE_ERROR;
2871 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2872 SSL_R_CERT_LENGTH_MISMATCH);
2877 x = d2i_X509(NULL, &p, l);
2879 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
2883 al = SSL_AD_DECODE_ERROR;
2884 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2885 SSL_R_CERT_LENGTH_MISMATCH);
2888 if (!sk_X509_push(sk, x)) {
2889 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2896 if (sk_X509_num(sk) <= 0) {
2897 /* TLS does not mind 0 certs returned */
2898 if (s->version == SSL3_VERSION) {
2899 al = SSL_AD_HANDSHAKE_FAILURE;
2900 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2901 SSL_R_NO_CERTIFICATES_RETURNED);
2904 /* Fail for TLS only if we required a certificate */
2905 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2906 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2907 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2908 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2909 al = SSL_AD_HANDSHAKE_FAILURE;
2913 i = ssl_verify_cert_chain(s, sk);
2915 al = ssl_verify_alarm_type(s->verify_result);
2916 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2917 SSL_R_NO_CERTIFICATE_RETURNED);
2922 if (s->session->peer != NULL) /* This should not be needed */
2923 X509_free(s->session->peer);
2924 s->session->peer = sk_X509_shift(sk);
2925 s->session->verify_result = s->verify_result;
2928 * With the current implementation, sess_cert will always be NULL when we
2931 if (s->session->sess_cert == NULL) {
2932 s->session->sess_cert = ssl_sess_cert_new();
2933 if (s->session->sess_cert == NULL) {
2934 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2938 if (s->session->sess_cert->cert_chain != NULL)
2939 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2940 s->session->sess_cert->cert_chain = sk;
2942 * Inconsistency alert: cert_chain does *not* include the peer's own
2943 * certificate, while we do include it in s3_clnt.c
2951 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2957 sk_X509_pop_free(sk, X509_free);
2961 int ssl3_send_server_certificate(SSL *s)
2966 if (s->state == SSL3_ST_SW_CERT_A) {
2967 x = ssl_get_server_send_cert(s);
2969 /* VRS: allow null cert if auth == KRB5 */
2970 if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
2971 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5)) {
2972 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,
2973 ERR_R_INTERNAL_ERROR);
2978 l = ssl3_output_cert_chain(s, x);
2980 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2983 s->state = SSL3_ST_SW_CERT_B;
2984 s->init_num = (int)l;
2988 /* SSL3_ST_SW_CERT_B */
2989 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2992 #ifndef OPENSSL_NO_TLSEXT
2993 int ssl3_send_newsession_ticket(SSL *s)
2995 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
2996 unsigned char *p, *senc, *macstart;
3001 SSL_CTX *tctx = s->initial_ctx;
3002 unsigned char iv[EVP_MAX_IV_LENGTH];
3003 unsigned char key_name[16];
3005 /* get session encoding length */
3006 slen = i2d_SSL_SESSION(s->session, NULL);
3008 * Some length values are 16 bits, so forget it if session is too
3014 * Grow buffer if need be: the length calculation is as
3015 * follows 1 (size of message name) + 3 (message length
3016 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
3017 * 16 (key name) + max_iv_len (iv length) +
3018 * session_length + max_enc_block_size (max encrypted session
3019 * length) + max_md_size (HMAC).
3021 if (!BUF_MEM_grow(s->init_buf,
3022 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
3023 EVP_MAX_MD_SIZE + slen))
3025 senc = OPENSSL_malloc(slen);
3029 i2d_SSL_SESSION(s->session, &p);
3031 p = (unsigned char *)s->init_buf->data;
3033 *(p++) = SSL3_MT_NEWSESSION_TICKET;
3034 /* Skip message length for now */
3036 EVP_CIPHER_CTX_init(&ctx);
3037 HMAC_CTX_init(&hctx);
3039 * Initialize HMAC and cipher contexts. If callback present it does
3040 * all the work otherwise use generated values from parent ctx.
3042 if (tctx->tlsext_ticket_key_cb) {
3043 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3049 RAND_pseudo_bytes(iv, 16);
3050 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3051 tctx->tlsext_tick_aes_key, iv);
3052 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3053 tlsext_tick_md(), NULL);
3054 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3056 l2n(s->session->tlsext_tick_lifetime_hint, p);
3057 /* Skip ticket length for now */
3059 /* Output key name */
3061 memcpy(p, key_name, 16);
3064 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3065 p += EVP_CIPHER_CTX_iv_length(&ctx);
3066 /* Encrypt session data */
3067 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
3069 EVP_EncryptFinal(&ctx, p, &len);
3071 EVP_CIPHER_CTX_cleanup(&ctx);
3073 HMAC_Update(&hctx, macstart, p - macstart);
3074 HMAC_Final(&hctx, p, &hlen);
3075 HMAC_CTX_cleanup(&hctx);
3078 /* Now write out lengths: p points to end of data written */
3080 len = p - (unsigned char *)s->init_buf->data;
3081 p = (unsigned char *)s->init_buf->data + 1;
3082 l2n3(len - 4, p); /* Message length */
3084 s2n(len - 10, p); /* Ticket length */
3086 /* number of bytes to write */
3088 s->state = SSL3_ST_SW_SESSION_TICKET_B;
3093 /* SSL3_ST_SW_SESSION_TICKET_B */
3094 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3097 int ssl3_send_cert_status(SSL *s)
3099 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
3102 * Grow buffer if need be: the length calculation is as
3103 * follows 1 (message type) + 3 (message length) +
3104 * 1 (ocsp response type) + 3 (ocsp response length)
3107 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
3110 p = (unsigned char *)s->init_buf->data;
3113 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3114 /* message length */
3115 l2n3(s->tlsext_ocsp_resplen + 4, p);
3117 *(p++) = s->tlsext_status_type;
3118 /* length of OCSP response */
3119 l2n3(s->tlsext_ocsp_resplen, p);
3120 /* actual response */
3121 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3122 /* number of bytes to write */
3123 s->init_num = 8 + s->tlsext_ocsp_resplen;
3124 s->state = SSL3_ST_SW_CERT_STATUS_B;
3128 /* SSL3_ST_SW_CERT_STATUS_B */
3129 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));