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-2005 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.
125 #define REUSE_CIPHER_BUG
126 #define NETSCAPE_HANG_BUG
129 #include "ssl_locl.h"
130 #include "kssl_lcl.h"
131 #include "../crypto/constant_time_locl.h"
132 #include <openssl/buffer.h>
133 #include <openssl/rand.h>
134 #include <openssl/objects.h>
135 #include <openssl/evp.h>
136 #include <openssl/hmac.h>
137 #include <openssl/x509.h>
138 #ifndef OPENSSL_NO_DH
139 # include <openssl/dh.h>
141 #include <openssl/bn.h>
142 #ifndef OPENSSL_NO_KRB5
143 # include <openssl/krb5_asn.h>
145 #include <openssl/md5.h>
147 static SSL_METHOD *ssl3_get_server_method(int ver);
148 #ifndef OPENSSL_NO_ECDH
149 static int nid2curve_id(int nid);
152 static SSL_METHOD *ssl3_get_server_method(int ver)
154 if (ver == SSL3_VERSION)
155 return (SSLv3_server_method());
160 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
162 ssl_undefined_function, ssl3_get_server_method)
164 int ssl3_accept(SSL *s)
167 unsigned long l, Time = (unsigned long)time(NULL);
168 void (*cb) (const SSL *ssl, int type, int val) = NULL;
170 int new_state, state, skip = 0;
172 RAND_add(&Time, sizeof(Time), 0);
176 if (s->info_callback != NULL)
177 cb = s->info_callback;
178 else if (s->ctx->info_callback != NULL)
179 cb = s->ctx->info_callback;
181 /* init things to blank */
183 if (!SSL_in_init(s) || SSL_in_before(s))
186 if (s->cert == NULL) {
187 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
195 case SSL_ST_RENEGOTIATE:
197 /* s->state=SSL_ST_ACCEPT; */
201 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
202 case SSL_ST_OK | SSL_ST_ACCEPT:
206 cb(s, SSL_CB_HANDSHAKE_START, 1);
208 if ((s->version >> 8) != 3) {
209 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
212 s->type = SSL_ST_ACCEPT;
214 if (s->init_buf == NULL) {
215 if ((buf = BUF_MEM_new()) == NULL) {
219 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
226 if (!ssl3_setup_buffers(s)) {
232 s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE;
234 if (s->state != SSL_ST_RENEGOTIATE) {
236 * Ok, we now need to push on a buffering BIO so that the
237 * output is sent in a way that TCP likes :-)
239 if (!ssl_init_wbio_buffer(s, 1)) {
244 ssl3_init_finished_mac(s);
245 s->state = SSL3_ST_SR_CLNT_HELLO_A;
246 s->ctx->stats.sess_accept++;
247 } else if (!s->s3->send_connection_binding &&
249 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
251 * Server attempting to renegotiate with client that doesn't
252 * support secure renegotiation.
254 SSLerr(SSL_F_SSL3_ACCEPT,
255 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
256 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
261 * s->state == SSL_ST_RENEGOTIATE, we will just send a
264 s->ctx->stats.sess_accept_renegotiate++;
265 s->state = SSL3_ST_SW_HELLO_REQ_A;
269 case SSL3_ST_SW_HELLO_REQ_A:
270 case SSL3_ST_SW_HELLO_REQ_B:
273 ret = ssl3_send_hello_request(s);
276 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
277 s->state = SSL3_ST_SW_FLUSH;
280 ssl3_init_finished_mac(s);
283 case SSL3_ST_SW_HELLO_REQ_C:
284 s->state = SSL_ST_OK;
287 case SSL3_ST_SR_CLNT_HELLO_A:
288 case SSL3_ST_SR_CLNT_HELLO_B:
289 case SSL3_ST_SR_CLNT_HELLO_C:
292 ret = ssl3_get_client_hello(s);
296 s->state = SSL3_ST_SW_SRVR_HELLO_A;
300 case SSL3_ST_SW_SRVR_HELLO_A:
301 case SSL3_ST_SW_SRVR_HELLO_B:
302 ret = ssl3_send_server_hello(s);
305 #ifndef OPENSSL_NO_TLSEXT
307 if (s->tlsext_ticket_expected)
308 s->state = SSL3_ST_SW_SESSION_TICKET_A;
310 s->state = SSL3_ST_SW_CHANGE_A;
314 s->state = SSL3_ST_SW_CHANGE_A;
317 s->state = SSL3_ST_SW_CERT_A;
321 case SSL3_ST_SW_CERT_A:
322 case SSL3_ST_SW_CERT_B:
323 /* Check if it is anon DH or anon ECDH or KRB5 */
324 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)
325 && !(s->s3->tmp.new_cipher->algorithms & SSL_aKRB5)) {
326 ret = ssl3_send_server_certificate(s);
329 #ifndef OPENSSL_NO_TLSEXT
330 if (s->tlsext_status_expected)
331 s->state = SSL3_ST_SW_CERT_STATUS_A;
333 s->state = SSL3_ST_SW_KEY_EXCH_A;
336 s->state = SSL3_ST_SW_KEY_EXCH_A;
342 s->state = SSL3_ST_SW_KEY_EXCH_A;
347 case SSL3_ST_SW_KEY_EXCH_A:
348 case SSL3_ST_SW_KEY_EXCH_B:
349 l = s->s3->tmp.new_cipher->algorithms;
352 * clear this, it may get reset by send_server_key_exchange
354 s->s3->tmp.use_rsa_tmp = 0;
357 * only send if a DH key exchange, fortezza or RSA but we have a
358 * sign only certificate For ECC ciphersuites, we send a
359 * serverKeyExchange message only if the cipher suite is either
360 * ECDH-anon or ECDHE. In other cases, the server certificate
361 * contains the server's public key for key exchange.
364 || (l & (SSL_DH | SSL_kFZA))
366 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
367 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
368 && EVP_PKEY_size(s->cert->pkeys
369 [SSL_PKEY_RSA_ENC].privatekey) *
370 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
375 ret = ssl3_send_server_key_exchange(s);
381 s->state = SSL3_ST_SW_CERT_REQ_A;
385 case SSL3_ST_SW_CERT_REQ_A:
386 case SSL3_ST_SW_CERT_REQ_B:
387 if ( /* don't request cert unless asked for it: */
388 !(s->verify_mode & SSL_VERIFY_PEER) ||
390 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
391 * during re-negotiation:
393 ((s->session->peer != NULL) &&
394 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
396 * never request cert in anonymous ciphersuites (see
397 * section "Certificate request" in SSL 3 drafts and in
400 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
402 * ... except when the application insists on
403 * verification (against the specs, but s3_clnt.c accepts
406 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
408 * never request cert in Kerberos ciphersuites
410 (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5)) {
411 /* no cert request */
413 s->s3->tmp.cert_request = 0;
414 s->state = SSL3_ST_SW_SRVR_DONE_A;
416 s->s3->tmp.cert_request = 1;
417 ret = ssl3_send_certificate_request(s);
420 #ifndef NETSCAPE_HANG_BUG
421 s->state = SSL3_ST_SW_SRVR_DONE_A;
423 s->state = SSL3_ST_SW_FLUSH;
424 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
430 case SSL3_ST_SW_SRVR_DONE_A:
431 case SSL3_ST_SW_SRVR_DONE_B:
432 ret = ssl3_send_server_done(s);
435 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
436 s->state = SSL3_ST_SW_FLUSH;
440 case SSL3_ST_SW_FLUSH:
443 * This code originally checked to see if any data was pending
444 * using BIO_CTRL_INFO and then flushed. This caused problems as
445 * documented in PR#1939. The proposed fix doesn't completely
446 * resolve this issue as buggy implementations of
447 * BIO_CTRL_PENDING still exist. So instead we just flush
451 s->rwstate = SSL_WRITING;
452 if (BIO_flush(s->wbio) <= 0) {
456 s->rwstate = SSL_NOTHING;
458 s->state = s->s3->tmp.next_state;
461 case SSL3_ST_SR_CERT_A:
462 case SSL3_ST_SR_CERT_B:
463 /* Check for second client hello (MS SGC) */
464 ret = ssl3_check_client_hello(s);
468 s->state = SSL3_ST_SR_CLNT_HELLO_C;
470 if (s->s3->tmp.cert_request) {
471 ret = ssl3_get_client_certificate(s);
476 s->state = SSL3_ST_SR_KEY_EXCH_A;
480 case SSL3_ST_SR_KEY_EXCH_A:
481 case SSL3_ST_SR_KEY_EXCH_B:
482 ret = ssl3_get_client_key_exchange(s);
487 * For the ECDH ciphersuites when the client sends its ECDH
488 * pub key in a certificate, the CertificateVerify message is
491 s->state = SSL3_ST_SR_FINISHED_A;
494 s->state = SSL3_ST_SR_CERT_VRFY_A;
498 * We need to get hashes here so if there is a client cert,
501 s->method->ssl3_enc->cert_verify_mac(s,
502 &(s->s3->finish_dgst1),
506 s->method->ssl3_enc->cert_verify_mac(s,
507 &(s->s3->finish_dgst2),
510 [MD5_DIGEST_LENGTH]));
514 case SSL3_ST_SR_CERT_VRFY_A:
515 case SSL3_ST_SR_CERT_VRFY_B:
517 s->s3->flags |= SSL3_FLAGS_CCS_OK;
518 /* we should decide if we expected this one */
519 ret = ssl3_get_cert_verify(s);
523 s->state = SSL3_ST_SR_FINISHED_A;
527 case SSL3_ST_SR_FINISHED_A:
528 case SSL3_ST_SR_FINISHED_B:
529 s->s3->flags |= SSL3_FLAGS_CCS_OK;
530 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
531 SSL3_ST_SR_FINISHED_B);
535 s->state = SSL_ST_OK;
536 #ifndef OPENSSL_NO_TLSEXT
537 else if (s->tlsext_ticket_expected)
538 s->state = SSL3_ST_SW_SESSION_TICKET_A;
541 s->state = SSL3_ST_SW_CHANGE_A;
545 #ifndef OPENSSL_NO_TLSEXT
546 case SSL3_ST_SW_SESSION_TICKET_A:
547 case SSL3_ST_SW_SESSION_TICKET_B:
548 ret = ssl3_send_newsession_ticket(s);
551 s->state = SSL3_ST_SW_CHANGE_A;
555 case SSL3_ST_SW_CERT_STATUS_A:
556 case SSL3_ST_SW_CERT_STATUS_B:
557 ret = ssl3_send_cert_status(s);
560 s->state = SSL3_ST_SW_KEY_EXCH_A;
566 case SSL3_ST_SW_CHANGE_A:
567 case SSL3_ST_SW_CHANGE_B:
569 s->session->cipher = s->s3->tmp.new_cipher;
570 if (!s->method->ssl3_enc->setup_key_block(s)) {
575 ret = ssl3_send_change_cipher_spec(s,
577 SSL3_ST_SW_CHANGE_B);
581 s->state = SSL3_ST_SW_FINISHED_A;
584 if (!s->method->ssl3_enc->change_cipher_state(s,
585 SSL3_CHANGE_CIPHER_SERVER_WRITE))
593 case SSL3_ST_SW_FINISHED_A:
594 case SSL3_ST_SW_FINISHED_B:
595 ret = ssl3_send_finished(s,
596 SSL3_ST_SW_FINISHED_A,
597 SSL3_ST_SW_FINISHED_B,
599 ssl3_enc->server_finished_label,
601 ssl3_enc->server_finished_label_len);
604 s->state = SSL3_ST_SW_FLUSH;
606 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
608 s->s3->tmp.next_state = SSL_ST_OK;
613 /* clean a few things up */
614 ssl3_cleanup_key_block(s);
616 BUF_MEM_free(s->init_buf);
619 /* remove buffering on output */
620 ssl_free_wbio_buffer(s);
624 if (s->new_session == 2) { /* skipped if we just sent a
627 * actually not necessarily a 'new' session unless
628 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set
633 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
635 s->ctx->stats.sess_accept_good++;
637 s->handshake_func = ssl3_accept;
640 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
648 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE);
654 if (!s->s3->tmp.reuse_message && !skip) {
656 if ((ret = BIO_flush(s->wbio)) <= 0)
660 if ((cb != NULL) && (s->state != state)) {
661 new_state = s->state;
663 cb(s, SSL_CB_ACCEPT_LOOP, 1);
664 s->state = new_state;
670 /* BIO_flush(s->wbio); */
674 cb(s, SSL_CB_ACCEPT_EXIT, ret);
678 int ssl3_send_hello_request(SSL *s)
682 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
683 p = (unsigned char *)s->init_buf->data;
684 *(p++) = SSL3_MT_HELLO_REQUEST;
689 s->state = SSL3_ST_SW_HELLO_REQ_B;
690 /* number of bytes to write */
695 /* SSL3_ST_SW_HELLO_REQ_B */
696 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
699 int ssl3_check_client_hello(SSL *s)
705 * this function is called when we really expect a Certificate message,
706 * so permit appropriate message length
708 n = s->method->ssl_get_message(s,
711 -1, s->max_cert_list, &ok);
714 s->s3->tmp.reuse_message = 1;
715 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) {
717 * We only allow the client to restart the handshake once per
720 if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) {
721 SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO,
722 SSL_R_MULTIPLE_SGC_RESTARTS);
726 * Throw away what we have done so far in the current handshake,
727 * which will now be aborted. (A full SSL_clear would be too much.)
729 #ifndef OPENSSL_NO_DH
730 if (s->s3->tmp.dh != NULL) {
731 DH_free(s->s3->tmp.dh);
732 s->s3->tmp.dh = NULL;
735 #ifndef OPENSSL_NO_ECDH
736 if (s->s3->tmp.ecdh != NULL) {
737 EC_KEY_free(s->s3->tmp.ecdh);
738 s->s3->tmp.ecdh = NULL;
741 s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE;
747 int ssl3_get_client_hello(SSL *s)
749 int i, j, ok, al, ret = -1;
750 unsigned int cookie_len;
753 unsigned char *p, *d, *q;
755 #ifndef OPENSSL_NO_COMP
756 SSL_COMP *comp = NULL;
758 STACK_OF(SSL_CIPHER) *ciphers = NULL;
761 * We do this so that we will respond with our native type. If we are
762 * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
763 * switching should be handled by a different method. If we are SSLv3, we
764 * will respond with SSLv3, even if prompted with TLSv1.
766 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
767 s->state = SSL3_ST_SR_CLNT_HELLO_B;
770 n = s->method->ssl_get_message(s,
771 SSL3_ST_SR_CLNT_HELLO_B,
772 SSL3_ST_SR_CLNT_HELLO_C,
773 SSL3_MT_CLIENT_HELLO,
774 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
779 d = p = (unsigned char *)s->init_msg;
782 * use version from inside client hello, not from record header (may
783 * differ: see RFC 2246, Appendix E, second paragraph)
785 s->client_version = (((int)p[0]) << 8) | (int)p[1];
788 if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
789 (s->version != DTLS1_VERSION && s->client_version < s->version)) {
790 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
791 if ((s->client_version >> 8) == SSL3_VERSION_MAJOR) {
793 * similar to ssl3_get_record, send alert using remote version
796 s->version = s->client_version;
798 al = SSL_AD_PROTOCOL_VERSION;
803 * If we require cookies and this ClientHello doesn't contain one, just
804 * return since we do not want to allocate any memory yet. So check
807 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
808 unsigned int session_length, cookie_length;
810 session_length = *(p + SSL3_RANDOM_SIZE);
811 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
813 if (cookie_length == 0)
817 /* load the client random */
818 memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
819 p += SSL3_RANDOM_SIZE;
821 /* get the session-id */
826 * Versions before 0.9.7 always allow session reuse during renegotiation
827 * (i.e. when s->new_session is true), option
828 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7. Maybe
829 * this optional behaviour should always have been the default, but we
830 * cannot safely change the default behaviour (or new applications might
831 * be written that become totally unsecure when compiled with an earlier
835 && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
836 if (!ssl_get_new_session(s, 1))
839 i = ssl_get_prev_session(s, p, j, d + n);
840 if (i == 1) { /* previous session */
846 if (!ssl_get_new_session(s, 1))
853 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
858 * The ClientHello may contain a cookie even if the
859 * HelloVerify message has not been sent--make sure that it
860 * does not cause an overflow.
862 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
864 al = SSL_AD_DECODE_ERROR;
865 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
869 /* verify the cookie if appropriate option is set. */
870 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && cookie_len > 0) {
871 memcpy(s->d1->rcvd_cookie, p, cookie_len);
873 if (s->ctx->app_verify_cookie_cb != NULL) {
874 if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
876 al = SSL_AD_HANDSHAKE_FAILURE;
877 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
878 SSL_R_COOKIE_MISMATCH);
881 /* else cookie verification succeeded */
883 /* default verification */
884 else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
885 s->d1->cookie_len) != 0) {
886 al = SSL_AD_HANDSHAKE_FAILURE;
887 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
898 if ((i == 0) && (j != 0)) {
899 /* we need a cipher if we are not resuming a session */
900 al = SSL_AD_ILLEGAL_PARAMETER;
901 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
904 if ((p + i) >= (d + n)) {
905 /* not enough data */
906 al = SSL_AD_DECODE_ERROR;
907 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
910 if ((i > 0) && (ssl_bytes_to_cipher_list(s, p, i, &(ciphers))
916 /* If it is a hit, check that the cipher is in the list */
917 if ((s->hit) && (i > 0)) {
919 id = s->session->cipher->id;
922 printf("client sent %d ciphers\n", sk_num(ciphers));
924 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
925 c = sk_SSL_CIPHER_value(ciphers, i);
927 printf("client [%2d of %2d]:%s\n",
928 i, sk_num(ciphers), SSL_CIPHER_get_name(c));
936 * Disabled because it can be used in a ciphersuite downgrade attack:
940 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
941 && (sk_SSL_CIPHER_num(ciphers) == 1)) {
943 * Special case as client bug workaround: the previously used
944 * cipher may not be in the current list, the client instead
945 * might be trying to continue using a cipher that before wasn't
946 * chosen due to server preferences. We'll have to reject the
947 * connection if the cipher is not enabled, though.
949 c = sk_SSL_CIPHER_value(ciphers, 0);
950 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
951 s->session->cipher = c;
958 * we need to have the cipher in the cipher list if we are asked
961 al = SSL_AD_ILLEGAL_PARAMETER;
962 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
963 SSL_R_REQUIRED_CIPHER_MISSING);
970 if ((p + i) > (d + n)) {
971 /* not enough data */
972 al = SSL_AD_DECODE_ERROR;
973 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
977 for (j = 0; j < i; j++) {
985 al = SSL_AD_DECODE_ERROR;
986 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
989 #ifndef OPENSSL_NO_TLSEXT
991 if (s->version >= SSL3_VERSION) {
992 if (!ssl_parse_clienthello_tlsext(s, &p, d, n, &al)) {
993 /* 'al' set by ssl_parse_clienthello_tlsext */
994 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
998 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
999 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1004 * Worst case, we will use the NULL compression, but if we have other
1005 * options, we will now look for them. We have i-1 compression
1006 * algorithms from the client, starting at q.
1008 s->s3->tmp.new_compression = NULL;
1009 #ifndef OPENSSL_NO_COMP
1010 if (s->ctx->comp_methods != NULL) {
1011 /* See if we have a match */
1012 int m, nn, o, v, done = 0;
1014 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1015 for (m = 0; m < nn; m++) {
1016 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1018 for (o = 0; o < i; o++) {
1028 s->s3->tmp.new_compression = comp;
1034 /* TLS does not mind if there is extra stuff */
1037 * SSL 3.0 does not mind either, so we should disable this test (was
1038 * enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b, in earlier
1039 * SSLeay/OpenSSL releases this test existed but was buggy)
1041 if (s->version == SSL3_VERSION) {
1044 * wrong number of bytes, there could be more to follow
1046 al = SSL_AD_DECODE_ERROR;
1047 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1054 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1058 #ifdef OPENSSL_NO_COMP
1059 s->session->compress_meth = 0;
1061 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1063 if (s->session->ciphers != NULL)
1064 sk_SSL_CIPHER_free(s->session->ciphers);
1065 s->session->ciphers = ciphers;
1066 if (ciphers == NULL) {
1067 al = SSL_AD_ILLEGAL_PARAMETER;
1068 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_PASSED);
1072 c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1075 al = SSL_AD_HANDSHAKE_FAILURE;
1076 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1079 s->s3->tmp.new_cipher = c;
1081 /* Session-id reuse */
1082 #ifdef REUSE_CIPHER_BUG
1083 STACK_OF(SSL_CIPHER) *sk;
1084 SSL_CIPHER *nc = NULL;
1085 SSL_CIPHER *ec = NULL;
1087 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) {
1088 sk = s->session->ciphers;
1089 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1090 c = sk_SSL_CIPHER_value(sk, i);
1091 if (c->algorithms & SSL_eNULL)
1093 if (SSL_C_IS_EXPORT(c))
1097 s->s3->tmp.new_cipher = nc;
1098 else if (ec != NULL)
1099 s->s3->tmp.new_cipher = ec;
1101 s->s3->tmp.new_cipher = s->session->cipher;
1104 s->s3->tmp.new_cipher = s->session->cipher;
1108 * we now have the following setup.
1110 * cipher_list - our prefered list of ciphers
1111 * ciphers - the clients prefered list of ciphers
1112 * compression - basically ignored right now
1113 * ssl version is set - sslv3
1114 * s->session - The ssl session has been setup.
1115 * s->hit - session reuse flag
1116 * s->tmp.new_cipher - the new cipher to use.
1119 #ifndef OPENSSL_NO_TLSEXT
1120 /* Handles TLS extensions that we couldn't check earlier */
1121 if (s->version >= SSL3_VERSION) {
1122 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1123 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1133 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1136 if (ciphers != NULL)
1137 sk_SSL_CIPHER_free(ciphers);
1141 int ssl3_send_server_hello(SSL *s)
1144 unsigned char *p, *d;
1146 unsigned long l, Time;
1148 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1149 buf = (unsigned char *)s->init_buf->data;
1150 p = s->s3->server_random;
1151 Time = (unsigned long)time(NULL); /* Time */
1153 if (RAND_pseudo_bytes(p, SSL3_RANDOM_SIZE - 4) <= 0)
1155 /* Do the message type and length last */
1158 *(p++) = s->version >> 8;
1159 *(p++) = s->version & 0xff;
1162 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1163 p += SSL3_RANDOM_SIZE;
1166 * now in theory we have 3 options to sending back the session id.
1167 * If it is a re-use, we send back the old session-id, if it is a new
1168 * session, we send back the new session-id or we send back a 0
1169 * length session-id if we want it to be single use. Currently I will
1170 * not implement the '0' length session-id 12-Jan-98 - I'll now
1171 * support the '0' length stuff. We also have an additional case
1172 * where stateless session resumption is successful: we always send
1173 * back the old session id. In this case s->hit is non zero: this can
1174 * only happen if stateless session resumption is succesful if session
1175 * caching is disabled so existing functionality is unaffected.
1177 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1179 s->session->session_id_length = 0;
1181 sl = s->session->session_id_length;
1182 if (sl > (int)sizeof(s->session->session_id)) {
1183 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1187 memcpy(p, s->session->session_id, sl);
1190 /* put the cipher */
1191 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1194 /* put the compression method */
1195 #ifdef OPENSSL_NO_COMP
1198 if (s->s3->tmp.new_compression == NULL)
1201 *(p++) = s->s3->tmp.new_compression->id;
1203 #ifndef OPENSSL_NO_TLSEXT
1205 ssl_add_serverhello_tlsext(s, p,
1206 buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
1208 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1215 *(d++) = SSL3_MT_SERVER_HELLO;
1218 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1219 /* number of bytes to write */
1220 s->init_num = p - buf;
1224 /* SSL3_ST_SW_SRVR_HELLO_B */
1225 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1228 int ssl3_send_server_done(SSL *s)
1232 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1233 p = (unsigned char *)s->init_buf->data;
1236 *(p++) = SSL3_MT_SERVER_DONE;
1241 s->state = SSL3_ST_SW_SRVR_DONE_B;
1242 /* number of bytes to write */
1247 /* SSL3_ST_SW_SRVR_DONE_B */
1248 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1251 int ssl3_send_server_key_exchange(SSL *s)
1253 #ifndef OPENSSL_NO_RSA
1257 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1260 #ifndef OPENSSL_NO_DH
1261 DH *dh = NULL, *dhp;
1263 #ifndef OPENSSL_NO_ECDH
1264 EC_KEY *ecdh = NULL, *ecdhp;
1265 unsigned char *encodedPoint = NULL;
1268 BN_CTX *bn_ctx = NULL;
1271 unsigned char *p, *d;
1281 EVP_MD_CTX_init(&md_ctx);
1282 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1283 type = s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1288 r[0] = r[1] = r[2] = r[3] = NULL;
1290 #ifndef OPENSSL_NO_RSA
1291 if (type & SSL_kRSA) {
1292 rsa = cert->rsa_tmp;
1293 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1294 rsa = s->cert->rsa_tmp_cb(s,
1295 SSL_C_IS_EXPORT(s->s3->
1297 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1300 al = SSL_AD_HANDSHAKE_FAILURE;
1301 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1302 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1306 cert->rsa_tmp = rsa;
1309 al = SSL_AD_HANDSHAKE_FAILURE;
1310 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1311 SSL_R_MISSING_TMP_RSA_KEY);
1316 s->s3->tmp.use_rsa_tmp = 1;
1319 #ifndef OPENSSL_NO_DH
1320 if (type & SSL_kEDH) {
1322 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1323 dhp = s->cert->dh_tmp_cb(s,
1324 SSL_C_IS_EXPORT(s->s3->
1326 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1329 al = SSL_AD_HANDSHAKE_FAILURE;
1330 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1331 SSL_R_MISSING_TMP_DH_KEY);
1335 if (s->s3->tmp.dh != NULL) {
1336 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1337 ERR_R_INTERNAL_ERROR);
1341 if ((dh = DHparams_dup(dhp)) == NULL) {
1342 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1347 if ((dhp->pub_key == NULL ||
1348 dhp->priv_key == NULL ||
1349 (s->options & SSL_OP_SINGLE_DH_USE))) {
1350 if (!DH_generate_key(dh)) {
1351 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1355 dh->pub_key = BN_dup(dhp->pub_key);
1356 dh->priv_key = BN_dup(dhp->priv_key);
1357 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1358 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1367 #ifndef OPENSSL_NO_ECDH
1368 if (type & SSL_kECDHE) {
1369 const EC_GROUP *group;
1371 ecdhp = cert->ecdh_tmp;
1372 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) {
1373 ecdhp = s->cert->ecdh_tmp_cb(s,
1374 SSL_C_IS_EXPORT(s->s3->
1376 SSL_C_EXPORT_PKEYLENGTH(s->
1377 s3->tmp.new_cipher));
1379 if (ecdhp == NULL) {
1380 al = SSL_AD_HANDSHAKE_FAILURE;
1381 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1382 SSL_R_MISSING_TMP_ECDH_KEY);
1386 if (s->s3->tmp.ecdh != NULL) {
1387 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1388 ERR_R_INTERNAL_ERROR);
1392 /* Duplicate the ECDH structure. */
1393 if (ecdhp == NULL) {
1394 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1397 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1398 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1402 s->s3->tmp.ecdh = ecdh;
1403 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1404 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1405 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1406 if (!EC_KEY_generate_key(ecdh)) {
1407 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1413 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1414 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1415 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1416 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1420 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1421 (EC_GROUP_get_degree(group) > 163)) {
1422 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1423 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1428 * XXX: For now, we only support ephemeral ECDH keys over named
1429 * (not generic) curves. For supported named curves, curve_id is
1432 if ((curve_id = nid2curve_id(EC_GROUP_get_curve_name(group)))
1434 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1435 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1440 * Encode the public key. First check the size of encoding and
1441 * allocate memory accordingly.
1443 encodedlen = EC_POINT_point2oct(group,
1444 EC_KEY_get0_public_key(ecdh),
1445 POINT_CONVERSION_UNCOMPRESSED,
1448 encodedPoint = (unsigned char *)
1449 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1450 bn_ctx = BN_CTX_new();
1451 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1452 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1453 ERR_R_MALLOC_FAILURE);
1457 encodedlen = EC_POINT_point2oct(group,
1458 EC_KEY_get0_public_key(ecdh),
1459 POINT_CONVERSION_UNCOMPRESSED,
1460 encodedPoint, encodedlen, bn_ctx);
1462 if (encodedlen == 0) {
1463 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1467 BN_CTX_free(bn_ctx);
1471 * XXX: For now, we only support named (not generic) curves in
1472 * ECDH ephemeral key exchanges. In this situation, we need four
1473 * additional bytes to encode the entire ServerECDHParams
1479 * We'll generate the serverKeyExchange message explicitly so we
1480 * can set these to NULLs
1487 #endif /* !OPENSSL_NO_ECDH */
1489 al = SSL_AD_HANDSHAKE_FAILURE;
1490 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1491 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1494 for (i = 0; r[i] != NULL; i++) {
1495 nr[i] = BN_num_bytes(r[i]);
1499 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) {
1500 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher))
1502 al = SSL_AD_DECODE_ERROR;
1505 kn = EVP_PKEY_size(pkey);
1511 if (!BUF_MEM_grow_clean(buf, n + 4 + kn)) {
1512 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1515 d = (unsigned char *)s->init_buf->data;
1518 for (i = 0; r[i] != NULL; i++) {
1524 #ifndef OPENSSL_NO_ECDH
1525 if (type & SSL_kECDHE) {
1527 * XXX: For now, we only support named (not generic) curves. In
1528 * this situation, the serverKeyExchange message has: [1 byte
1529 * CurveType], [2 byte CurveName] [1 byte length of encoded
1530 * point], followed by the actual encoded point itself
1532 *p = NAMED_CURVE_TYPE;
1540 memcpy((unsigned char *)p,
1541 (unsigned char *)encodedPoint, encodedlen);
1542 OPENSSL_free(encodedPoint);
1543 encodedPoint = NULL;
1551 * n is the length of the params, they start at &(d[4]) and p
1552 * points to the space at the end.
1554 #ifndef OPENSSL_NO_RSA
1555 if (pkey->type == EVP_PKEY_RSA) {
1558 for (num = 2; num > 0; num--) {
1559 EVP_MD_CTX_set_flags(&md_ctx,
1560 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1561 EVP_DigestInit_ex(&md_ctx, (num == 2)
1562 ? s->ctx->md5 : s->ctx->sha1, NULL);
1563 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1565 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1567 EVP_DigestUpdate(&md_ctx, &(d[4]), n);
1568 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1572 if (RSA_sign(NID_md5_sha1, md_buf, j,
1573 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1574 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
1581 #if !defined(OPENSSL_NO_DSA)
1582 if (pkey->type == EVP_PKEY_DSA) {
1584 EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL);
1585 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1587 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1589 EVP_SignUpdate(&md_ctx, &(d[4]), n);
1590 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1591 (unsigned int *)&i, pkey)) {
1592 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA);
1599 #if !defined(OPENSSL_NO_ECDSA)
1600 if (pkey->type == EVP_PKEY_EC) {
1601 /* let's do ECDSA */
1602 EVP_SignInit_ex(&md_ctx, EVP_ecdsa(), NULL);
1603 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1605 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1607 EVP_SignUpdate(&md_ctx, &(d[4]), n);
1608 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1609 (unsigned int *)&i, pkey)) {
1610 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1619 /* Is this error check actually needed? */
1620 al = SSL_AD_HANDSHAKE_FAILURE;
1621 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1622 SSL_R_UNKNOWN_PKEY_TYPE);
1627 *(d++) = SSL3_MT_SERVER_KEY_EXCHANGE;
1631 * we should now have things packed up, so lets send it off
1633 s->init_num = n + 4;
1637 s->state = SSL3_ST_SW_KEY_EXCH_B;
1638 EVP_MD_CTX_cleanup(&md_ctx);
1639 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1641 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1643 #ifndef OPENSSL_NO_ECDH
1644 if (encodedPoint != NULL)
1645 OPENSSL_free(encodedPoint);
1646 BN_CTX_free(bn_ctx);
1648 EVP_MD_CTX_cleanup(&md_ctx);
1652 int ssl3_send_certificate_request(SSL *s)
1654 unsigned char *p, *d;
1655 int i, j, nl, off, n;
1656 STACK_OF(X509_NAME) *sk = NULL;
1660 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1663 d = p = (unsigned char *)&(buf->data[4]);
1665 /* get the list of acceptable cert types */
1667 n = ssl3_get_req_cert_type(s, p);
1676 sk = SSL_get_client_CA_list(s);
1679 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1680 name = sk_X509_NAME_value(sk, i);
1681 j = i2d_X509_NAME(name, NULL);
1682 if (!BUF_MEM_grow_clean(buf, 4 + n + j + 2)) {
1683 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
1687 p = (unsigned char *)&(buf->data[4 + n]);
1688 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
1690 i2d_X509_NAME(name, &p);
1695 i2d_X509_NAME(name, &p);
1704 /* else no CA names */
1705 p = (unsigned char *)&(buf->data[4 + off]);
1708 d = (unsigned char *)buf->data;
1709 *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
1713 * we should now have things packed up, so lets send it off
1716 s->init_num = n + 4;
1718 #ifdef NETSCAPE_HANG_BUG
1719 if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) {
1720 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB);
1723 p = (unsigned char *)s->init_buf->data + s->init_num;
1726 *(p++) = SSL3_MT_SERVER_DONE;
1733 s->state = SSL3_ST_SW_CERT_REQ_B;
1736 /* SSL3_ST_SW_CERT_REQ_B */
1737 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1742 int ssl3_get_client_key_exchange(SSL *s)
1748 #ifndef OPENSSL_NO_RSA
1750 EVP_PKEY *pkey = NULL;
1752 #ifndef OPENSSL_NO_DH
1756 #ifndef OPENSSL_NO_KRB5
1758 #endif /* OPENSSL_NO_KRB5 */
1760 #ifndef OPENSSL_NO_ECDH
1761 EC_KEY *srvr_ecdh = NULL;
1762 EVP_PKEY *clnt_pub_pkey = NULL;
1763 EC_POINT *clnt_ecpoint = NULL;
1764 BN_CTX *bn_ctx = NULL;
1767 n = s->method->ssl_get_message(s,
1768 SSL3_ST_SR_KEY_EXCH_A,
1769 SSL3_ST_SR_KEY_EXCH_B,
1770 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
1774 p = (unsigned char *)s->init_msg;
1776 l = s->s3->tmp.new_cipher->algorithms;
1778 #ifndef OPENSSL_NO_RSA
1780 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1782 unsigned char decrypt_good, version_good;
1784 /* FIX THIS UP EAY EAY EAY EAY */
1785 if (s->s3->tmp.use_rsa_tmp) {
1786 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1787 rsa = s->cert->rsa_tmp;
1789 * Don't do a callback because rsa_tmp should be sent already
1792 al = SSL_AD_HANDSHAKE_FAILURE;
1793 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1794 SSL_R_MISSING_TMP_RSA_PKEY);
1799 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1800 if ((pkey == NULL) ||
1801 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
1802 al = SSL_AD_HANDSHAKE_FAILURE;
1803 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1804 SSL_R_MISSING_RSA_CERTIFICATE);
1807 rsa = pkey->pkey.rsa;
1810 /* TLS and [incidentally] DTLS, including pre-0.9.8f */
1811 if (s->version > SSL3_VERSION && s->client_version != DTLS1_BAD_VER) {
1814 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
1815 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1816 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1825 * We must not leak whether a decryption failure occurs because of
1826 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1827 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1828 * generates a random premaster secret for the case that the decrypt
1829 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
1833 * should be RAND_bytes, but we cannot work around a failure.
1835 if (RAND_pseudo_bytes(rand_premaster_secret,
1836 sizeof(rand_premaster_secret)) <= 0)
1839 RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
1843 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
1844 * be 0xff if so and zero otherwise.
1847 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
1850 * If the version in the decrypted pre-master secret is correct then
1851 * version_good will be 0xff, otherwise it'll be zero. The
1852 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1853 * (http://eprint.iacr.org/2003/052/) exploits the version number
1854 * check as a "bad version oracle". Thus version checks are done in
1855 * constant time and are treated like any other decryption error.
1858 constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
1860 constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
1863 * The premaster secret must contain the same version number as the
1864 * ClientHello to detect version rollback attacks (strangely, the
1865 * protocol does not offer such protection for DH ciphersuites).
1866 * However, buggy clients exist that send the negotiated protocol
1867 * version instead if the server does not support the requested
1868 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
1871 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
1872 unsigned char workaround_good;
1874 constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
1876 constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
1877 version_good |= workaround_good;
1881 * Both decryption and version must be good for decrypt_good to
1882 * remain non-zero (0xff).
1884 decrypt_good &= version_good;
1887 * Now copy rand_premaster_secret over p using decrypt_good_mask.
1889 for (i = 0; i < (int)sizeof(rand_premaster_secret); i++) {
1890 p[i] = constant_time_select_8(decrypt_good, p[i],
1891 rand_premaster_secret[i]);
1894 s->session->master_key_length =
1895 s->method->ssl3_enc->generate_master_secret(s,
1897 session->master_key,
1899 OPENSSL_cleanse(p, i);
1902 #ifndef OPENSSL_NO_DH
1903 if (l & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
1906 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
1907 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1908 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1916 if (n == 0L) { /* the parameters are in the cert */
1917 al = SSL_AD_HANDSHAKE_FAILURE;
1918 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1919 SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1922 if (s->s3->tmp.dh == NULL) {
1923 al = SSL_AD_HANDSHAKE_FAILURE;
1924 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1925 SSL_R_MISSING_TMP_DH_KEY);
1928 dh_srvr = s->s3->tmp.dh;
1931 pub = BN_bin2bn(p, i, NULL);
1933 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
1937 i = DH_compute_key(p, pub, dh_srvr);
1940 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1945 DH_free(s->s3->tmp.dh);
1946 s->s3->tmp.dh = NULL;
1950 s->session->master_key_length =
1951 s->method->ssl3_enc->generate_master_secret(s,
1953 session->master_key,
1955 OPENSSL_cleanse(p, i);
1958 #ifndef OPENSSL_NO_KRB5
1959 if (l & SSL_kKRB5) {
1960 krb5_error_code krb5rc;
1961 krb5_data enc_ticket;
1962 krb5_data authenticator;
1964 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1965 EVP_CIPHER_CTX ciph_ctx;
1966 EVP_CIPHER *enc = NULL;
1967 unsigned char iv[EVP_MAX_IV_LENGTH];
1968 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_BLOCK_LENGTH];
1970 krb5_timestamp authtime = 0;
1971 krb5_ticket_times ttimes;
1973 EVP_CIPHER_CTX_init(&ciph_ctx);
1976 kssl_ctx = kssl_ctx_new();
1979 enc_ticket.length = i;
1981 if (n < (int)enc_ticket.length + 6) {
1982 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1983 SSL_R_DATA_LENGTH_TOO_LONG);
1987 enc_ticket.data = (char *)p;
1988 p += enc_ticket.length;
1991 authenticator.length = i;
1993 if (n < (int)(enc_ticket.length + authenticator.length) + 6) {
1994 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1995 SSL_R_DATA_LENGTH_TOO_LONG);
1999 authenticator.data = (char *)p;
2000 p += authenticator.length;
2004 enc_pms.data = (char *)p;
2005 p += enc_pms.length;
2008 * Note that the length is checked again below, ** after decryption
2010 if (enc_pms.length > sizeof pms) {
2011 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2012 SSL_R_DATA_LENGTH_TOO_LONG);
2016 if (n != (long)(enc_ticket.length + authenticator.length +
2017 enc_pms.length + 6)) {
2018 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2019 SSL_R_DATA_LENGTH_TOO_LONG);
2023 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2026 printf("kssl_sget_tkt rtn %d [%d]\n", krb5rc, kssl_err.reason);
2028 printf("kssl_err text= %s\n", kssl_err.text);
2029 # endif /* KSSL_DEBUG */
2030 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2035 * Note: no authenticator is not considered an error, ** but will
2036 * return authtime == 0.
2038 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2039 &authtime, &kssl_err)) != 0) {
2041 printf("kssl_check_authent rtn %d [%d]\n",
2042 krb5rc, kssl_err.reason);
2044 printf("kssl_err text= %s\n", kssl_err.text);
2045 # endif /* KSSL_DEBUG */
2046 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2050 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) {
2051 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2055 kssl_ctx_show(kssl_ctx);
2056 # endif /* KSSL_DEBUG */
2058 enc = kssl_map_enc(kssl_ctx->enctype);
2062 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2064 if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv)) {
2065 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2066 SSL_R_DECRYPTION_FAILED);
2069 if (!EVP_DecryptUpdate(&ciph_ctx, pms, &outl,
2070 (unsigned char *)enc_pms.data, enc_pms.length))
2072 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2073 SSL_R_DECRYPTION_FAILED);
2076 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2077 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2078 SSL_R_DATA_LENGTH_TOO_LONG);
2081 if (!EVP_DecryptFinal_ex(&ciph_ctx, &(pms[outl]), &padl)) {
2082 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2083 SSL_R_DECRYPTION_FAILED);
2087 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2088 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2089 SSL_R_DATA_LENGTH_TOO_LONG);
2092 if (!((pms[0] == (s->client_version >> 8))
2093 && (pms[1] == (s->client_version & 0xff)))) {
2095 * The premaster secret must contain the same version number as
2096 * the ClientHello to detect version rollback attacks (strangely,
2097 * the protocol does not offer such protection for DH
2098 * ciphersuites). However, buggy clients exist that send random
2099 * bytes instead of the protocol version. If
2100 * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2101 * (Perhaps we should have a separate BUG value for the Kerberos
2104 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) {
2105 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2106 SSL_AD_DECODE_ERROR);
2111 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2113 s->session->master_key_length =
2114 s->method->ssl3_enc->generate_master_secret(s,
2116 session->master_key,
2119 if (kssl_ctx->client_princ) {
2120 size_t len = strlen(kssl_ctx->client_princ);
2121 if (len < SSL_MAX_KRB5_PRINCIPAL_LENGTH) {
2122 s->session->krb5_client_princ_len = len;
2123 memcpy(s->session->krb5_client_princ, kssl_ctx->client_princ,
2128 /*- Was doing kssl_ctx_free() here,
2129 * but it caused problems for apache.
2130 * kssl_ctx = kssl_ctx_free(kssl_ctx);
2131 * if (s->kssl_ctx) s->kssl_ctx = NULL;
2134 #endif /* OPENSSL_NO_KRB5 */
2136 #ifndef OPENSSL_NO_ECDH
2137 if ((l & SSL_kECDH) || (l & SSL_kECDHE)) {
2141 const EC_GROUP *group;
2142 const BIGNUM *priv_key;
2144 /* initialize structures for server's ECDH key pair */
2145 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2146 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2150 /* Let's get server private key and group information */
2151 if (l & SSL_kECDH) {
2152 /* use the certificate */
2153 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2156 * use the ephermeral values we saved when generating the
2157 * ServerKeyExchange msg.
2159 tkey = s->s3->tmp.ecdh;
2162 group = EC_KEY_get0_group(tkey);
2163 priv_key = EC_KEY_get0_private_key(tkey);
2165 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2166 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2167 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2171 /* Let's get client's public key */
2172 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2173 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2178 /* Client Publickey was in Client Certificate */
2180 if (l & SSL_kECDHE) {
2181 al = SSL_AD_HANDSHAKE_FAILURE;
2182 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2183 SSL_R_MISSING_TMP_ECDH_KEY);
2186 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2187 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2189 * XXX: For now, we do not support client authentication
2190 * using ECDH certificates so this branch (n == 0L) of the
2191 * code is never executed. When that support is added, we
2192 * ought to ensure the key received in the certificate is
2193 * authorized for key agreement. ECDH_compute_key implicitly
2194 * checks that the two ECDH shares are for the same group.
2196 al = SSL_AD_HANDSHAKE_FAILURE;
2197 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2198 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2202 if (EC_POINT_copy(clnt_ecpoint,
2203 EC_KEY_get0_public_key(clnt_pub_pkey->
2205 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2208 ret = 2; /* Skip certificate verify processing */
2211 * Get client's public key from encoded point in the
2212 * ClientKeyExchange message.
2214 if ((bn_ctx = BN_CTX_new()) == NULL) {
2215 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2216 ERR_R_MALLOC_FAILURE);
2220 /* Get encoded point length */
2224 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2227 if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2228 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2232 * p is pointing to somewhere in the buffer currently, so set it
2235 p = (unsigned char *)s->init_buf->data;
2238 /* Compute the shared pre-master secret */
2239 field_size = EC_GROUP_get_degree(group);
2240 if (field_size <= 0) {
2241 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2244 i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2247 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2251 EVP_PKEY_free(clnt_pub_pkey);
2252 EC_POINT_free(clnt_ecpoint);
2253 if (srvr_ecdh != NULL)
2254 EC_KEY_free(srvr_ecdh);
2255 BN_CTX_free(bn_ctx);
2257 /* Compute the master secret */
2258 s->session->master_key_length =
2259 s->method->ssl3_enc->generate_master_secret(s,
2261 session->master_key,
2264 OPENSSL_cleanse(p, i);
2269 al = SSL_AD_HANDSHAKE_FAILURE;
2270 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2276 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2277 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
2280 #ifndef OPENSSL_NO_ECDH
2281 EVP_PKEY_free(clnt_pub_pkey);
2282 EC_POINT_free(clnt_ecpoint);
2283 if (srvr_ecdh != NULL)
2284 EC_KEY_free(srvr_ecdh);
2285 BN_CTX_free(bn_ctx);
2290 int ssl3_get_cert_verify(SSL *s)
2292 EVP_PKEY *pkey = NULL;
2294 int al, ok, ret = 0;
2299 n = s->method->ssl_get_message(s,
2300 SSL3_ST_SR_CERT_VRFY_A,
2301 SSL3_ST_SR_CERT_VRFY_B,
2302 -1, SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2307 if (s->session->peer != NULL) {
2308 peer = s->session->peer;
2309 pkey = X509_get_pubkey(peer);
2310 type = X509_certificate_type(peer, pkey);
2316 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) {
2317 s->s3->tmp.reuse_message = 1;
2319 al = SSL_AD_UNEXPECTED_MESSAGE;
2320 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_MISSING_VERIFY_MESSAGE);
2328 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_NO_CLIENT_CERT_RECEIVED);
2329 al = SSL_AD_UNEXPECTED_MESSAGE;
2333 if (!(type & EVP_PKT_SIGN)) {
2334 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2335 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2336 al = SSL_AD_ILLEGAL_PARAMETER;
2340 if (s->s3->change_cipher_spec) {
2341 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_CCS_RECEIVED_EARLY);
2342 al = SSL_AD_UNEXPECTED_MESSAGE;
2346 /* we now have a signature that we need to verify */
2347 p = (unsigned char *)s->init_msg;
2351 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2352 al = SSL_AD_DECODE_ERROR;
2356 j = EVP_PKEY_size(pkey);
2357 if ((i > j) || (n > j) || (n <= 0)) {
2358 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2359 al = SSL_AD_DECODE_ERROR;
2362 #ifndef OPENSSL_NO_RSA
2363 if (pkey->type == EVP_PKEY_RSA) {
2364 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2365 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
2368 al = SSL_AD_DECRYPT_ERROR;
2369 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
2373 al = SSL_AD_DECRYPT_ERROR;
2374 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
2379 #ifndef OPENSSL_NO_DSA
2380 if (pkey->type == EVP_PKEY_DSA) {
2381 j = DSA_verify(pkey->save_type,
2382 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2383 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
2386 al = SSL_AD_DECRYPT_ERROR;
2387 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
2392 #ifndef OPENSSL_NO_ECDSA
2393 if (pkey->type == EVP_PKEY_EC) {
2394 j = ECDSA_verify(pkey->save_type,
2395 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2396 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
2399 al = SSL_AD_DECRYPT_ERROR;
2400 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
2406 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2407 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2414 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2417 EVP_PKEY_free(pkey);
2421 int ssl3_get_client_certificate(SSL *s)
2423 int i, ok, al, ret = -1;
2425 unsigned long l, nc, llen, n;
2426 const unsigned char *p, *q;
2428 STACK_OF(X509) *sk = NULL;
2430 n = s->method->ssl_get_message(s,
2433 -1, s->max_cert_list, &ok);
2438 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2439 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2440 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2441 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2442 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2443 al = SSL_AD_HANDSHAKE_FAILURE;
2447 * If tls asked for a client cert, the client must return a 0 list
2449 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
2450 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2451 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2452 al = SSL_AD_UNEXPECTED_MESSAGE;
2455 s->s3->tmp.reuse_message = 1;
2459 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2460 al = SSL_AD_UNEXPECTED_MESSAGE;
2461 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
2464 p = d = (unsigned char *)s->init_msg;
2466 if ((sk = sk_X509_new_null()) == NULL) {
2467 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2472 if (llen + 3 != n) {
2473 al = SSL_AD_DECODE_ERROR;
2474 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
2477 for (nc = 0; nc < llen;) {
2479 if ((l + nc + 3) > llen) {
2480 al = SSL_AD_DECODE_ERROR;
2481 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2482 SSL_R_CERT_LENGTH_MISMATCH);
2487 x = d2i_X509(NULL, &p, l);
2489 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
2493 al = SSL_AD_DECODE_ERROR;
2494 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2495 SSL_R_CERT_LENGTH_MISMATCH);
2498 if (!sk_X509_push(sk, x)) {
2499 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2506 if (sk_X509_num(sk) <= 0) {
2507 /* TLS does not mind 0 certs returned */
2508 if (s->version == SSL3_VERSION) {
2509 al = SSL_AD_HANDSHAKE_FAILURE;
2510 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2511 SSL_R_NO_CERTIFICATES_RETURNED);
2514 /* Fail for TLS only if we required a certificate */
2515 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2516 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2517 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2518 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2519 al = SSL_AD_HANDSHAKE_FAILURE;
2523 i = ssl_verify_cert_chain(s, sk);
2525 al = ssl_verify_alarm_type(s->verify_result);
2526 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2527 SSL_R_NO_CERTIFICATE_RETURNED);
2532 if (s->session->peer != NULL) /* This should not be needed */
2533 X509_free(s->session->peer);
2534 s->session->peer = sk_X509_shift(sk);
2535 s->session->verify_result = s->verify_result;
2538 * With the current implementation, sess_cert will always be NULL when we
2541 if (s->session->sess_cert == NULL) {
2542 s->session->sess_cert = ssl_sess_cert_new();
2543 if (s->session->sess_cert == NULL) {
2544 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2548 if (s->session->sess_cert->cert_chain != NULL)
2549 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2550 s->session->sess_cert->cert_chain = sk;
2552 * Inconsistency alert: cert_chain does *not* include the peer's own
2553 * certificate, while we do include it in s3_clnt.c
2561 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2567 sk_X509_pop_free(sk, X509_free);
2571 int ssl3_send_server_certificate(SSL *s)
2576 if (s->state == SSL3_ST_SW_CERT_A) {
2577 x = ssl_get_server_send_cert(s);
2579 /* VRS: allow null cert if auth == KRB5 */
2580 (s->s3->tmp.new_cipher->algorithms
2581 & (SSL_MKEY_MASK | SSL_AUTH_MASK))
2582 != (SSL_aKRB5 | SSL_kKRB5)) {
2583 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2587 l = ssl3_output_cert_chain(s, x);
2588 s->state = SSL3_ST_SW_CERT_B;
2589 s->init_num = (int)l;
2593 /* SSL3_ST_SW_CERT_B */
2594 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2597 #ifndef OPENSSL_NO_ECDH
2598 /* This is the complement of curve_id2nid in s3_clnt.c. */
2599 static int nid2curve_id(int nid)
2602 * ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001) (no changes
2603 * in draft-ietf-tls-ecc-03.txt [June 2003])
2606 case NID_sect163k1: /* sect163k1 (1) */
2608 case NID_sect163r1: /* sect163r1 (2) */
2610 case NID_sect163r2: /* sect163r2 (3) */
2612 case NID_sect193r1: /* sect193r1 (4) */
2614 case NID_sect193r2: /* sect193r2 (5) */
2616 case NID_sect233k1: /* sect233k1 (6) */
2618 case NID_sect233r1: /* sect233r1 (7) */
2620 case NID_sect239k1: /* sect239k1 (8) */
2622 case NID_sect283k1: /* sect283k1 (9) */
2624 case NID_sect283r1: /* sect283r1 (10) */
2626 case NID_sect409k1: /* sect409k1 (11) */
2628 case NID_sect409r1: /* sect409r1 (12) */
2630 case NID_sect571k1: /* sect571k1 (13) */
2632 case NID_sect571r1: /* sect571r1 (14) */
2634 case NID_secp160k1: /* secp160k1 (15) */
2636 case NID_secp160r1: /* secp160r1 (16) */
2638 case NID_secp160r2: /* secp160r2 (17) */
2640 case NID_secp192k1: /* secp192k1 (18) */
2642 case NID_X9_62_prime192v1: /* secp192r1 (19) */
2644 case NID_secp224k1: /* secp224k1 (20) */
2646 case NID_secp224r1: /* secp224r1 (21) */
2648 case NID_secp256k1: /* secp256k1 (22) */
2650 case NID_X9_62_prime256v1: /* secp256r1 (23) */
2652 case NID_secp384r1: /* secp384r1 (24) */
2654 case NID_secp521r1: /* secp521r1 (25) */
2661 #ifndef OPENSSL_NO_TLSEXT
2662 int ssl3_send_newsession_ticket(SSL *s)
2664 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
2665 unsigned char *p, *senc, *macstart;
2670 SSL_CTX *tctx = s->initial_ctx;
2671 unsigned char iv[EVP_MAX_IV_LENGTH];
2672 unsigned char key_name[16];
2674 /* get session encoding length */
2675 slen = i2d_SSL_SESSION(s->session, NULL);
2677 * Some length values are 16 bits, so forget it if session is too
2683 * Grow buffer if need be: the length calculation is as
2684 * follows 1 (size of message name) + 3 (message length
2685 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
2686 * 16 (key name) + max_iv_len (iv length) +
2687 * session_length + max_enc_block_size (max encrypted session
2688 * length) + max_md_size (HMAC).
2690 if (!BUF_MEM_grow(s->init_buf,
2691 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
2692 EVP_MAX_MD_SIZE + slen))
2694 senc = OPENSSL_malloc(slen);
2698 i2d_SSL_SESSION(s->session, &p);
2700 p = (unsigned char *)s->init_buf->data;
2702 *(p++) = SSL3_MT_NEWSESSION_TICKET;
2703 /* Skip message length for now */
2705 EVP_CIPHER_CTX_init(&ctx);
2706 HMAC_CTX_init(&hctx);
2708 * Initialize HMAC and cipher contexts. If callback present it does
2709 * all the work otherwise use generated values from parent ctx.
2711 if (tctx->tlsext_ticket_key_cb) {
2712 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
2718 RAND_pseudo_bytes(iv, 16);
2719 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2720 tctx->tlsext_tick_aes_key, iv);
2721 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2722 tlsext_tick_md(), NULL);
2723 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2725 l2n(s->session->tlsext_tick_lifetime_hint, p);
2726 /* Skip ticket length for now */
2728 /* Output key name */
2730 memcpy(p, key_name, 16);
2733 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2734 p += EVP_CIPHER_CTX_iv_length(&ctx);
2735 /* Encrypt session data */
2736 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
2738 EVP_EncryptFinal(&ctx, p, &len);
2740 EVP_CIPHER_CTX_cleanup(&ctx);
2742 HMAC_Update(&hctx, macstart, p - macstart);
2743 HMAC_Final(&hctx, p, &hlen);
2744 HMAC_CTX_cleanup(&hctx);
2747 /* Now write out lengths: p points to end of data written */
2749 len = p - (unsigned char *)s->init_buf->data;
2750 p = (unsigned char *)s->init_buf->data + 1;
2751 l2n3(len - 4, p); /* Message length */
2753 s2n(len - 10, p); /* Ticket length */
2755 /* number of bytes to write */
2757 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2762 /* SSL3_ST_SW_SESSION_TICKET_B */
2763 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2766 int ssl3_send_cert_status(SSL *s)
2768 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
2771 * Grow buffer if need be: the length calculation is as
2772 * follows 1 (message type) + 3 (message length) +
2773 * 1 (ocsp response type) + 3 (ocsp response length)
2776 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
2779 p = (unsigned char *)s->init_buf->data;
2782 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
2783 /* message length */
2784 l2n3(s->tlsext_ocsp_resplen + 4, p);
2786 *(p++) = s->tlsext_status_type;
2787 /* length of OCSP response */
2788 l2n3(s->tlsext_ocsp_resplen, p);
2789 /* actual response */
2790 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
2791 /* number of bytes to write */
2792 s->init_num = 8 + s->tlsext_ocsp_resplen;
2793 s->state = SSL3_ST_SW_CERT_STATUS_B;
2797 /* SSL3_ST_SW_CERT_STATUS_B */
2798 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));