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 * 2 bytes for client version, SSL3_RANDOM_SIZE bytes for random, 1 byte
783 * for session id length
785 if (n < 2 + SSL3_RANDOM_SIZE + 1) {
786 al = SSL_AD_DECODE_ERROR;
787 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
792 * use version from inside client hello, not from record header (may
793 * differ: see RFC 2246, Appendix E, second paragraph)
795 s->client_version = (((int)p[0]) << 8) | (int)p[1];
798 if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
799 (s->version != DTLS1_VERSION && s->client_version < s->version)) {
800 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
801 if ((s->client_version >> 8) == SSL3_VERSION_MAJOR) {
803 * similar to ssl3_get_record, send alert using remote version
806 s->version = s->client_version;
808 al = SSL_AD_PROTOCOL_VERSION;
813 * If we require cookies and this ClientHello doesn't contain one, just
814 * return since we do not want to allocate any memory yet. So check
817 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
818 unsigned int session_length, cookie_length;
820 session_length = *(p + SSL3_RANDOM_SIZE);
822 if (p + SSL3_RANDOM_SIZE + session_length + 1 >= d + n) {
823 al = SSL_AD_DECODE_ERROR;
824 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
827 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
829 if (cookie_length == 0)
833 /* load the client random */
834 memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
835 p += SSL3_RANDOM_SIZE;
837 /* get the session-id */
841 al = SSL_AD_DECODE_ERROR;
842 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
848 * Versions before 0.9.7 always allow session reuse during renegotiation
849 * (i.e. when s->new_session is true), option
850 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7. Maybe
851 * this optional behaviour should always have been the default, but we
852 * cannot safely change the default behaviour (or new applications might
853 * be written that become totally unsecure when compiled with an earlier
857 && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
858 if (!ssl_get_new_session(s, 1))
861 i = ssl_get_prev_session(s, p, j, d + n);
862 if (i == 1) { /* previous session */
868 if (!ssl_get_new_session(s, 1))
875 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
878 al = SSL_AD_DECODE_ERROR;
879 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
884 if (p + cookie_len > d + n) {
885 al = SSL_AD_DECODE_ERROR;
886 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
891 * The ClientHello may contain a cookie even if the
892 * HelloVerify message has not been sent--make sure that it
893 * does not cause an overflow.
895 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
897 al = SSL_AD_DECODE_ERROR;
898 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
902 /* verify the cookie if appropriate option is set. */
903 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && cookie_len > 0) {
904 memcpy(s->d1->rcvd_cookie, p, cookie_len);
906 if (s->ctx->app_verify_cookie_cb != NULL) {
907 if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
909 al = SSL_AD_HANDSHAKE_FAILURE;
910 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
911 SSL_R_COOKIE_MISMATCH);
914 /* else cookie verification succeeded */
916 /* default verification */
917 else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
918 s->d1->cookie_len) != 0) {
919 al = SSL_AD_HANDSHAKE_FAILURE;
920 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
931 al = SSL_AD_DECODE_ERROR;
932 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
936 if ((i == 0) && (j != 0)) {
937 /* we need a cipher if we are not resuming a session */
938 al = SSL_AD_ILLEGAL_PARAMETER;
939 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
943 /* i bytes of cipher data + 1 byte for compression length later */
944 if ((p + i + 1) > (d + n)) {
945 /* not enough data */
946 al = SSL_AD_DECODE_ERROR;
947 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
950 if ((i > 0) && (ssl_bytes_to_cipher_list(s, p, i, &(ciphers))
956 /* If it is a hit, check that the cipher is in the list */
957 if ((s->hit) && (i > 0)) {
959 id = s->session->cipher->id;
962 printf("client sent %d ciphers\n", sk_num(ciphers));
964 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
965 c = sk_SSL_CIPHER_value(ciphers, i);
967 printf("client [%2d of %2d]:%s\n",
968 i, sk_num(ciphers), SSL_CIPHER_get_name(c));
976 * Disabled because it can be used in a ciphersuite downgrade attack:
980 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
981 && (sk_SSL_CIPHER_num(ciphers) == 1)) {
983 * Special case as client bug workaround: the previously used
984 * cipher may not be in the current list, the client instead
985 * might be trying to continue using a cipher that before wasn't
986 * chosen due to server preferences. We'll have to reject the
987 * connection if the cipher is not enabled, though.
989 c = sk_SSL_CIPHER_value(ciphers, 0);
990 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
991 s->session->cipher = c;
998 * we need to have the cipher in the cipher list if we are asked
1001 al = SSL_AD_ILLEGAL_PARAMETER;
1002 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1003 SSL_R_REQUIRED_CIPHER_MISSING);
1010 if ((p + i) > (d + n)) {
1011 /* not enough data */
1012 al = SSL_AD_DECODE_ERROR;
1013 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1017 for (j = 0; j < i; j++) {
1025 al = SSL_AD_DECODE_ERROR;
1026 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1029 #ifndef OPENSSL_NO_TLSEXT
1030 /* TLS extensions */
1031 if (s->version >= SSL3_VERSION) {
1032 if (!ssl_parse_clienthello_tlsext(s, &p, d, n, &al)) {
1033 /* 'al' set by ssl_parse_clienthello_tlsext */
1034 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1038 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
1039 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1044 * Worst case, we will use the NULL compression, but if we have other
1045 * options, we will now look for them. We have i-1 compression
1046 * algorithms from the client, starting at q.
1048 s->s3->tmp.new_compression = NULL;
1049 #ifndef OPENSSL_NO_COMP
1050 if (s->ctx->comp_methods != NULL) {
1051 /* See if we have a match */
1052 int m, nn, o, v, done = 0;
1054 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1055 for (m = 0; m < nn; m++) {
1056 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1058 for (o = 0; o < i; o++) {
1068 s->s3->tmp.new_compression = comp;
1074 /* TLS does not mind if there is extra stuff */
1077 * SSL 3.0 does not mind either, so we should disable this test (was
1078 * enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b, in earlier
1079 * SSLeay/OpenSSL releases this test existed but was buggy)
1081 if (s->version == SSL3_VERSION) {
1084 * wrong number of bytes, there could be more to follow
1086 al = SSL_AD_DECODE_ERROR;
1087 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1094 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1098 #ifdef OPENSSL_NO_COMP
1099 s->session->compress_meth = 0;
1101 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1103 if (s->session->ciphers != NULL)
1104 sk_SSL_CIPHER_free(s->session->ciphers);
1105 s->session->ciphers = ciphers;
1106 if (ciphers == NULL) {
1107 al = SSL_AD_ILLEGAL_PARAMETER;
1108 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_PASSED);
1112 c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1115 al = SSL_AD_HANDSHAKE_FAILURE;
1116 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1119 s->s3->tmp.new_cipher = c;
1121 /* Session-id reuse */
1122 #ifdef REUSE_CIPHER_BUG
1123 STACK_OF(SSL_CIPHER) *sk;
1124 SSL_CIPHER *nc = NULL;
1125 SSL_CIPHER *ec = NULL;
1127 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) {
1128 sk = s->session->ciphers;
1129 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1130 c = sk_SSL_CIPHER_value(sk, i);
1131 if (c->algorithms & SSL_eNULL)
1133 if (SSL_C_IS_EXPORT(c))
1137 s->s3->tmp.new_cipher = nc;
1138 else if (ec != NULL)
1139 s->s3->tmp.new_cipher = ec;
1141 s->s3->tmp.new_cipher = s->session->cipher;
1144 s->s3->tmp.new_cipher = s->session->cipher;
1148 * we now have the following setup.
1150 * cipher_list - our prefered list of ciphers
1151 * ciphers - the clients prefered list of ciphers
1152 * compression - basically ignored right now
1153 * ssl version is set - sslv3
1154 * s->session - The ssl session has been setup.
1155 * s->hit - session reuse flag
1156 * s->tmp.new_cipher - the new cipher to use.
1159 #ifndef OPENSSL_NO_TLSEXT
1160 /* Handles TLS extensions that we couldn't check earlier */
1161 if (s->version >= SSL3_VERSION) {
1162 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1163 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1173 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1176 if (ciphers != NULL)
1177 sk_SSL_CIPHER_free(ciphers);
1181 int ssl3_send_server_hello(SSL *s)
1184 unsigned char *p, *d;
1186 unsigned long l, Time;
1188 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1189 buf = (unsigned char *)s->init_buf->data;
1190 p = s->s3->server_random;
1191 Time = (unsigned long)time(NULL); /* Time */
1193 if (RAND_pseudo_bytes(p, SSL3_RANDOM_SIZE - 4) <= 0)
1195 /* Do the message type and length last */
1198 *(p++) = s->version >> 8;
1199 *(p++) = s->version & 0xff;
1202 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1203 p += SSL3_RANDOM_SIZE;
1206 * now in theory we have 3 options to sending back the session id.
1207 * If it is a re-use, we send back the old session-id, if it is a new
1208 * session, we send back the new session-id or we send back a 0
1209 * length session-id if we want it to be single use. Currently I will
1210 * not implement the '0' length session-id 12-Jan-98 - I'll now
1211 * support the '0' length stuff. We also have an additional case
1212 * where stateless session resumption is successful: we always send
1213 * back the old session id. In this case s->hit is non zero: this can
1214 * only happen if stateless session resumption is succesful if session
1215 * caching is disabled so existing functionality is unaffected.
1217 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1219 s->session->session_id_length = 0;
1221 sl = s->session->session_id_length;
1222 if (sl > (int)sizeof(s->session->session_id)) {
1223 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1227 memcpy(p, s->session->session_id, sl);
1230 /* put the cipher */
1231 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1234 /* put the compression method */
1235 #ifdef OPENSSL_NO_COMP
1238 if (s->s3->tmp.new_compression == NULL)
1241 *(p++) = s->s3->tmp.new_compression->id;
1243 #ifndef OPENSSL_NO_TLSEXT
1245 ssl_add_serverhello_tlsext(s, p,
1246 buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
1248 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1255 *(d++) = SSL3_MT_SERVER_HELLO;
1258 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1259 /* number of bytes to write */
1260 s->init_num = p - buf;
1264 /* SSL3_ST_SW_SRVR_HELLO_B */
1265 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1268 int ssl3_send_server_done(SSL *s)
1272 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1273 p = (unsigned char *)s->init_buf->data;
1276 *(p++) = SSL3_MT_SERVER_DONE;
1281 s->state = SSL3_ST_SW_SRVR_DONE_B;
1282 /* number of bytes to write */
1287 /* SSL3_ST_SW_SRVR_DONE_B */
1288 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1291 int ssl3_send_server_key_exchange(SSL *s)
1293 #ifndef OPENSSL_NO_RSA
1297 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1300 #ifndef OPENSSL_NO_DH
1301 DH *dh = NULL, *dhp;
1303 #ifndef OPENSSL_NO_ECDH
1304 EC_KEY *ecdh = NULL, *ecdhp;
1305 unsigned char *encodedPoint = NULL;
1308 BN_CTX *bn_ctx = NULL;
1311 unsigned char *p, *d;
1321 EVP_MD_CTX_init(&md_ctx);
1322 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1323 type = s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1328 r[0] = r[1] = r[2] = r[3] = NULL;
1330 #ifndef OPENSSL_NO_RSA
1331 if (type & SSL_kRSA) {
1332 rsa = cert->rsa_tmp;
1333 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1334 rsa = s->cert->rsa_tmp_cb(s,
1335 SSL_C_IS_EXPORT(s->s3->
1337 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1340 al = SSL_AD_HANDSHAKE_FAILURE;
1341 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1342 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1346 cert->rsa_tmp = rsa;
1349 al = SSL_AD_HANDSHAKE_FAILURE;
1350 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1351 SSL_R_MISSING_TMP_RSA_KEY);
1356 s->s3->tmp.use_rsa_tmp = 1;
1359 #ifndef OPENSSL_NO_DH
1360 if (type & SSL_kEDH) {
1362 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1363 dhp = s->cert->dh_tmp_cb(s,
1364 SSL_C_IS_EXPORT(s->s3->
1366 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1369 al = SSL_AD_HANDSHAKE_FAILURE;
1370 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1371 SSL_R_MISSING_TMP_DH_KEY);
1375 if (s->s3->tmp.dh != NULL) {
1376 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1377 ERR_R_INTERNAL_ERROR);
1381 if ((dh = DHparams_dup(dhp)) == NULL) {
1382 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1387 if ((dhp->pub_key == NULL ||
1388 dhp->priv_key == NULL ||
1389 (s->options & SSL_OP_SINGLE_DH_USE))) {
1390 if (!DH_generate_key(dh)) {
1391 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1395 dh->pub_key = BN_dup(dhp->pub_key);
1396 dh->priv_key = BN_dup(dhp->priv_key);
1397 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1398 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1407 #ifndef OPENSSL_NO_ECDH
1408 if (type & SSL_kECDHE) {
1409 const EC_GROUP *group;
1411 ecdhp = cert->ecdh_tmp;
1412 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) {
1413 ecdhp = s->cert->ecdh_tmp_cb(s,
1414 SSL_C_IS_EXPORT(s->s3->
1416 SSL_C_EXPORT_PKEYLENGTH(s->
1417 s3->tmp.new_cipher));
1419 if (ecdhp == NULL) {
1420 al = SSL_AD_HANDSHAKE_FAILURE;
1421 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1422 SSL_R_MISSING_TMP_ECDH_KEY);
1426 if (s->s3->tmp.ecdh != NULL) {
1427 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1428 ERR_R_INTERNAL_ERROR);
1432 /* Duplicate the ECDH structure. */
1433 if (ecdhp == NULL) {
1434 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1437 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1438 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1442 s->s3->tmp.ecdh = ecdh;
1443 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1444 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1445 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1446 if (!EC_KEY_generate_key(ecdh)) {
1447 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1453 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1454 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1455 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1456 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1460 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1461 (EC_GROUP_get_degree(group) > 163)) {
1462 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1463 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1468 * XXX: For now, we only support ephemeral ECDH keys over named
1469 * (not generic) curves. For supported named curves, curve_id is
1472 if ((curve_id = nid2curve_id(EC_GROUP_get_curve_name(group)))
1474 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1475 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1480 * Encode the public key. First check the size of encoding and
1481 * allocate memory accordingly.
1483 encodedlen = EC_POINT_point2oct(group,
1484 EC_KEY_get0_public_key(ecdh),
1485 POINT_CONVERSION_UNCOMPRESSED,
1488 encodedPoint = (unsigned char *)
1489 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1490 bn_ctx = BN_CTX_new();
1491 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1492 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1493 ERR_R_MALLOC_FAILURE);
1497 encodedlen = EC_POINT_point2oct(group,
1498 EC_KEY_get0_public_key(ecdh),
1499 POINT_CONVERSION_UNCOMPRESSED,
1500 encodedPoint, encodedlen, bn_ctx);
1502 if (encodedlen == 0) {
1503 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1507 BN_CTX_free(bn_ctx);
1511 * XXX: For now, we only support named (not generic) curves in
1512 * ECDH ephemeral key exchanges. In this situation, we need four
1513 * additional bytes to encode the entire ServerECDHParams
1519 * We'll generate the serverKeyExchange message explicitly so we
1520 * can set these to NULLs
1527 #endif /* !OPENSSL_NO_ECDH */
1529 al = SSL_AD_HANDSHAKE_FAILURE;
1530 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1531 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1534 for (i = 0; r[i] != NULL; i++) {
1535 nr[i] = BN_num_bytes(r[i]);
1539 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) {
1540 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher))
1542 al = SSL_AD_DECODE_ERROR;
1545 kn = EVP_PKEY_size(pkey);
1551 if (!BUF_MEM_grow_clean(buf, n + 4 + kn)) {
1552 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1555 d = (unsigned char *)s->init_buf->data;
1558 for (i = 0; r[i] != NULL; i++) {
1564 #ifndef OPENSSL_NO_ECDH
1565 if (type & SSL_kECDHE) {
1567 * XXX: For now, we only support named (not generic) curves. In
1568 * this situation, the serverKeyExchange message has: [1 byte
1569 * CurveType], [2 byte CurveName] [1 byte length of encoded
1570 * point], followed by the actual encoded point itself
1572 *p = NAMED_CURVE_TYPE;
1580 memcpy((unsigned char *)p,
1581 (unsigned char *)encodedPoint, encodedlen);
1582 OPENSSL_free(encodedPoint);
1583 encodedPoint = NULL;
1591 * n is the length of the params, they start at &(d[4]) and p
1592 * points to the space at the end.
1594 #ifndef OPENSSL_NO_RSA
1595 if (pkey->type == EVP_PKEY_RSA) {
1598 for (num = 2; num > 0; num--) {
1599 EVP_MD_CTX_set_flags(&md_ctx,
1600 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1601 EVP_DigestInit_ex(&md_ctx, (num == 2)
1602 ? s->ctx->md5 : s->ctx->sha1, NULL);
1603 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1605 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1607 EVP_DigestUpdate(&md_ctx, &(d[4]), n);
1608 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1612 if (RSA_sign(NID_md5_sha1, md_buf, j,
1613 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1614 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
1621 #if !defined(OPENSSL_NO_DSA)
1622 if (pkey->type == EVP_PKEY_DSA) {
1624 EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL);
1625 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1627 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1629 EVP_SignUpdate(&md_ctx, &(d[4]), n);
1630 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1631 (unsigned int *)&i, pkey)) {
1632 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA);
1639 #if !defined(OPENSSL_NO_ECDSA)
1640 if (pkey->type == EVP_PKEY_EC) {
1641 /* let's do ECDSA */
1642 EVP_SignInit_ex(&md_ctx, EVP_ecdsa(), NULL);
1643 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1645 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1647 EVP_SignUpdate(&md_ctx, &(d[4]), n);
1648 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1649 (unsigned int *)&i, pkey)) {
1650 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1659 /* Is this error check actually needed? */
1660 al = SSL_AD_HANDSHAKE_FAILURE;
1661 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1662 SSL_R_UNKNOWN_PKEY_TYPE);
1667 *(d++) = SSL3_MT_SERVER_KEY_EXCHANGE;
1671 * we should now have things packed up, so lets send it off
1673 s->init_num = n + 4;
1677 s->state = SSL3_ST_SW_KEY_EXCH_B;
1678 EVP_MD_CTX_cleanup(&md_ctx);
1679 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1681 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1683 #ifndef OPENSSL_NO_ECDH
1684 if (encodedPoint != NULL)
1685 OPENSSL_free(encodedPoint);
1686 BN_CTX_free(bn_ctx);
1688 EVP_MD_CTX_cleanup(&md_ctx);
1692 int ssl3_send_certificate_request(SSL *s)
1694 unsigned char *p, *d;
1695 int i, j, nl, off, n;
1696 STACK_OF(X509_NAME) *sk = NULL;
1700 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1703 d = p = (unsigned char *)&(buf->data[4]);
1705 /* get the list of acceptable cert types */
1707 n = ssl3_get_req_cert_type(s, p);
1716 sk = SSL_get_client_CA_list(s);
1719 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1720 name = sk_X509_NAME_value(sk, i);
1721 j = i2d_X509_NAME(name, NULL);
1722 if (!BUF_MEM_grow_clean(buf, 4 + n + j + 2)) {
1723 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
1727 p = (unsigned char *)&(buf->data[4 + n]);
1728 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
1730 i2d_X509_NAME(name, &p);
1735 i2d_X509_NAME(name, &p);
1744 /* else no CA names */
1745 p = (unsigned char *)&(buf->data[4 + off]);
1748 d = (unsigned char *)buf->data;
1749 *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
1753 * we should now have things packed up, so lets send it off
1756 s->init_num = n + 4;
1758 #ifdef NETSCAPE_HANG_BUG
1759 if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) {
1760 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB);
1763 p = (unsigned char *)s->init_buf->data + s->init_num;
1766 *(p++) = SSL3_MT_SERVER_DONE;
1773 s->state = SSL3_ST_SW_CERT_REQ_B;
1776 /* SSL3_ST_SW_CERT_REQ_B */
1777 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1782 int ssl3_get_client_key_exchange(SSL *s)
1788 #ifndef OPENSSL_NO_RSA
1790 EVP_PKEY *pkey = NULL;
1792 #ifndef OPENSSL_NO_DH
1796 #ifndef OPENSSL_NO_KRB5
1798 #endif /* OPENSSL_NO_KRB5 */
1800 #ifndef OPENSSL_NO_ECDH
1801 EC_KEY *srvr_ecdh = NULL;
1802 EVP_PKEY *clnt_pub_pkey = NULL;
1803 EC_POINT *clnt_ecpoint = NULL;
1804 BN_CTX *bn_ctx = NULL;
1807 n = s->method->ssl_get_message(s,
1808 SSL3_ST_SR_KEY_EXCH_A,
1809 SSL3_ST_SR_KEY_EXCH_B,
1810 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
1814 p = (unsigned char *)s->init_msg;
1816 l = s->s3->tmp.new_cipher->algorithms;
1818 #ifndef OPENSSL_NO_RSA
1820 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1822 unsigned char decrypt_good, version_good;
1824 /* FIX THIS UP EAY EAY EAY EAY */
1825 if (s->s3->tmp.use_rsa_tmp) {
1826 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1827 rsa = s->cert->rsa_tmp;
1829 * Don't do a callback because rsa_tmp should be sent already
1832 al = SSL_AD_HANDSHAKE_FAILURE;
1833 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1834 SSL_R_MISSING_TMP_RSA_PKEY);
1839 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1840 if ((pkey == NULL) ||
1841 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
1842 al = SSL_AD_HANDSHAKE_FAILURE;
1843 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1844 SSL_R_MISSING_RSA_CERTIFICATE);
1847 rsa = pkey->pkey.rsa;
1850 /* TLS and [incidentally] DTLS, including pre-0.9.8f */
1851 if (s->version > SSL3_VERSION && s->client_version != DTLS1_BAD_VER) {
1854 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
1855 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1856 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1865 * We must not leak whether a decryption failure occurs because of
1866 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1867 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1868 * generates a random premaster secret for the case that the decrypt
1869 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
1873 * should be RAND_bytes, but we cannot work around a failure.
1875 if (RAND_pseudo_bytes(rand_premaster_secret,
1876 sizeof(rand_premaster_secret)) <= 0)
1879 RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
1883 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
1884 * be 0xff if so and zero otherwise.
1887 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
1890 * If the version in the decrypted pre-master secret is correct then
1891 * version_good will be 0xff, otherwise it'll be zero. The
1892 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1893 * (http://eprint.iacr.org/2003/052/) exploits the version number
1894 * check as a "bad version oracle". Thus version checks are done in
1895 * constant time and are treated like any other decryption error.
1898 constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
1900 constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
1903 * The premaster secret must contain the same version number as the
1904 * ClientHello to detect version rollback attacks (strangely, the
1905 * protocol does not offer such protection for DH ciphersuites).
1906 * However, buggy clients exist that send the negotiated protocol
1907 * version instead if the server does not support the requested
1908 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
1911 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
1912 unsigned char workaround_good;
1914 constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
1916 constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
1917 version_good |= workaround_good;
1921 * Both decryption and version must be good for decrypt_good to
1922 * remain non-zero (0xff).
1924 decrypt_good &= version_good;
1927 * Now copy rand_premaster_secret over p using decrypt_good_mask.
1929 for (i = 0; i < (int)sizeof(rand_premaster_secret); i++) {
1930 p[i] = constant_time_select_8(decrypt_good, p[i],
1931 rand_premaster_secret[i]);
1934 s->session->master_key_length =
1935 s->method->ssl3_enc->generate_master_secret(s,
1937 session->master_key,
1939 OPENSSL_cleanse(p, i);
1942 #ifndef OPENSSL_NO_DH
1943 if (l & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
1946 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
1947 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1948 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1956 if (n == 0L) { /* the parameters are in the cert */
1957 al = SSL_AD_HANDSHAKE_FAILURE;
1958 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1959 SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1962 if (s->s3->tmp.dh == NULL) {
1963 al = SSL_AD_HANDSHAKE_FAILURE;
1964 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1965 SSL_R_MISSING_TMP_DH_KEY);
1968 dh_srvr = s->s3->tmp.dh;
1971 pub = BN_bin2bn(p, i, NULL);
1973 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
1977 i = DH_compute_key(p, pub, dh_srvr);
1980 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1985 DH_free(s->s3->tmp.dh);
1986 s->s3->tmp.dh = NULL;
1990 s->session->master_key_length =
1991 s->method->ssl3_enc->generate_master_secret(s,
1993 session->master_key,
1995 OPENSSL_cleanse(p, i);
1998 #ifndef OPENSSL_NO_KRB5
1999 if (l & SSL_kKRB5) {
2000 krb5_error_code krb5rc;
2001 krb5_data enc_ticket;
2002 krb5_data authenticator;
2004 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2005 EVP_CIPHER_CTX ciph_ctx;
2006 EVP_CIPHER *enc = NULL;
2007 unsigned char iv[EVP_MAX_IV_LENGTH];
2008 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_BLOCK_LENGTH];
2010 krb5_timestamp authtime = 0;
2011 krb5_ticket_times ttimes;
2014 EVP_CIPHER_CTX_init(&ciph_ctx);
2017 kssl_ctx = kssl_ctx_new();
2020 enc_ticket.length = i;
2022 if (n < (int)enc_ticket.length + 6) {
2023 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2024 SSL_R_DATA_LENGTH_TOO_LONG);
2028 enc_ticket.data = (char *)p;
2029 p += enc_ticket.length;
2032 authenticator.length = i;
2034 if (n < (int)(enc_ticket.length + authenticator.length) + 6) {
2035 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2036 SSL_R_DATA_LENGTH_TOO_LONG);
2040 authenticator.data = (char *)p;
2041 p += authenticator.length;
2045 enc_pms.data = (char *)p;
2046 p += enc_pms.length;
2049 * Note that the length is checked again below, ** after decryption
2051 if (enc_pms.length > sizeof pms) {
2052 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2053 SSL_R_DATA_LENGTH_TOO_LONG);
2057 if (n != (long)(enc_ticket.length + authenticator.length +
2058 enc_pms.length + 6)) {
2059 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2060 SSL_R_DATA_LENGTH_TOO_LONG);
2064 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2067 printf("kssl_sget_tkt rtn %d [%d]\n", krb5rc, kssl_err.reason);
2069 printf("kssl_err text= %s\n", kssl_err.text);
2070 # endif /* KSSL_DEBUG */
2071 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2076 * Note: no authenticator is not considered an error, ** but will
2077 * return authtime == 0.
2079 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2080 &authtime, &kssl_err)) != 0) {
2082 printf("kssl_check_authent rtn %d [%d]\n",
2083 krb5rc, kssl_err.reason);
2085 printf("kssl_err text= %s\n", kssl_err.text);
2086 # endif /* KSSL_DEBUG */
2087 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2091 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) {
2092 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2096 kssl_ctx_show(kssl_ctx);
2097 # endif /* KSSL_DEBUG */
2099 enc = kssl_map_enc(kssl_ctx->enctype);
2103 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2105 if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv)) {
2106 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2107 SSL_R_DECRYPTION_FAILED);
2110 if (!EVP_DecryptUpdate(&ciph_ctx, pms, &outl,
2111 (unsigned char *)enc_pms.data, enc_pms.length))
2113 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2114 SSL_R_DECRYPTION_FAILED);
2118 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2119 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2120 SSL_R_DATA_LENGTH_TOO_LONG);
2124 if (!EVP_DecryptFinal_ex(&ciph_ctx, &(pms[outl]), &padl)) {
2125 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2126 SSL_R_DECRYPTION_FAILED);
2131 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2132 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2133 SSL_R_DATA_LENGTH_TOO_LONG);
2137 if (!((pms[0] == (s->client_version >> 8))
2138 && (pms[1] == (s->client_version & 0xff)))) {
2140 * The premaster secret must contain the same version number as
2141 * the ClientHello to detect version rollback attacks (strangely,
2142 * the protocol does not offer such protection for DH
2143 * ciphersuites). However, buggy clients exist that send random
2144 * bytes instead of the protocol version. If
2145 * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2146 * (Perhaps we should have a separate BUG value for the Kerberos
2149 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) {
2150 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2151 SSL_AD_DECODE_ERROR);
2157 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2159 s->session->master_key_length =
2160 s->method->ssl3_enc->generate_master_secret(s,
2162 session->master_key,
2165 if (kssl_ctx->client_princ) {
2166 size_t len = strlen(kssl_ctx->client_princ);
2167 if (len < SSL_MAX_KRB5_PRINCIPAL_LENGTH) {
2168 s->session->krb5_client_princ_len = len;
2169 memcpy(s->session->krb5_client_princ, kssl_ctx->client_princ,
2174 /*- Was doing kssl_ctx_free() here,
2175 * but it caused problems for apache.
2176 * kssl_ctx = kssl_ctx_free(kssl_ctx);
2177 * if (s->kssl_ctx) s->kssl_ctx = NULL;
2181 OPENSSL_cleanse(pms, sizeof(pms));
2185 #endif /* OPENSSL_NO_KRB5 */
2187 #ifndef OPENSSL_NO_ECDH
2188 if ((l & SSL_kECDH) || (l & SSL_kECDHE)) {
2192 const EC_GROUP *group;
2193 const BIGNUM *priv_key;
2195 /* initialize structures for server's ECDH key pair */
2196 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2197 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2201 /* Let's get server private key and group information */
2202 if (l & SSL_kECDH) {
2203 /* use the certificate */
2204 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2207 * use the ephermeral values we saved when generating the
2208 * ServerKeyExchange msg.
2210 tkey = s->s3->tmp.ecdh;
2213 group = EC_KEY_get0_group(tkey);
2214 priv_key = EC_KEY_get0_private_key(tkey);
2216 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2217 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2218 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2222 /* Let's get client's public key */
2223 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2224 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2229 /* Client Publickey was in Client Certificate */
2231 if (l & SSL_kECDHE) {
2232 al = SSL_AD_HANDSHAKE_FAILURE;
2233 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2234 SSL_R_MISSING_TMP_ECDH_KEY);
2237 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2238 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2240 * XXX: For now, we do not support client authentication
2241 * using ECDH certificates so this branch (n == 0L) of the
2242 * code is never executed. When that support is added, we
2243 * ought to ensure the key received in the certificate is
2244 * authorized for key agreement. ECDH_compute_key implicitly
2245 * checks that the two ECDH shares are for the same group.
2247 al = SSL_AD_HANDSHAKE_FAILURE;
2248 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2249 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2253 if (EC_POINT_copy(clnt_ecpoint,
2254 EC_KEY_get0_public_key(clnt_pub_pkey->
2256 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2259 ret = 2; /* Skip certificate verify processing */
2262 * Get client's public key from encoded point in the
2263 * ClientKeyExchange message.
2265 if ((bn_ctx = BN_CTX_new()) == NULL) {
2266 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2267 ERR_R_MALLOC_FAILURE);
2271 /* Get encoded point length */
2275 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2278 if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2279 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2283 * p is pointing to somewhere in the buffer currently, so set it
2286 p = (unsigned char *)s->init_buf->data;
2289 /* Compute the shared pre-master secret */
2290 field_size = EC_GROUP_get_degree(group);
2291 if (field_size <= 0) {
2292 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2295 i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2298 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2302 EVP_PKEY_free(clnt_pub_pkey);
2303 EC_POINT_free(clnt_ecpoint);
2304 if (srvr_ecdh != NULL)
2305 EC_KEY_free(srvr_ecdh);
2306 BN_CTX_free(bn_ctx);
2308 /* Compute the master secret */
2309 s->session->master_key_length =
2310 s->method->ssl3_enc->generate_master_secret(s,
2312 session->master_key,
2315 OPENSSL_cleanse(p, i);
2320 al = SSL_AD_HANDSHAKE_FAILURE;
2321 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2327 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2328 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
2331 #ifndef OPENSSL_NO_ECDH
2332 EVP_PKEY_free(clnt_pub_pkey);
2333 EC_POINT_free(clnt_ecpoint);
2334 if (srvr_ecdh != NULL)
2335 EC_KEY_free(srvr_ecdh);
2336 BN_CTX_free(bn_ctx);
2341 int ssl3_get_cert_verify(SSL *s)
2343 EVP_PKEY *pkey = NULL;
2345 int al, ok, ret = 0;
2350 n = s->method->ssl_get_message(s,
2351 SSL3_ST_SR_CERT_VRFY_A,
2352 SSL3_ST_SR_CERT_VRFY_B,
2353 -1, SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2358 if (s->session->peer != NULL) {
2359 peer = s->session->peer;
2360 pkey = X509_get_pubkey(peer);
2361 type = X509_certificate_type(peer, pkey);
2367 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) {
2368 s->s3->tmp.reuse_message = 1;
2370 al = SSL_AD_UNEXPECTED_MESSAGE;
2371 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_MISSING_VERIFY_MESSAGE);
2379 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_NO_CLIENT_CERT_RECEIVED);
2380 al = SSL_AD_UNEXPECTED_MESSAGE;
2384 if (!(type & EVP_PKT_SIGN)) {
2385 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2386 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2387 al = SSL_AD_ILLEGAL_PARAMETER;
2391 if (s->s3->change_cipher_spec) {
2392 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_CCS_RECEIVED_EARLY);
2393 al = SSL_AD_UNEXPECTED_MESSAGE;
2397 /* we now have a signature that we need to verify */
2398 p = (unsigned char *)s->init_msg;
2402 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2403 al = SSL_AD_DECODE_ERROR;
2407 j = EVP_PKEY_size(pkey);
2408 if ((i > j) || (n > j) || (n <= 0)) {
2409 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2410 al = SSL_AD_DECODE_ERROR;
2413 #ifndef OPENSSL_NO_RSA
2414 if (pkey->type == EVP_PKEY_RSA) {
2415 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2416 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
2419 al = SSL_AD_DECRYPT_ERROR;
2420 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
2424 al = SSL_AD_DECRYPT_ERROR;
2425 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
2430 #ifndef OPENSSL_NO_DSA
2431 if (pkey->type == EVP_PKEY_DSA) {
2432 j = DSA_verify(pkey->save_type,
2433 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2434 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
2437 al = SSL_AD_DECRYPT_ERROR;
2438 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
2443 #ifndef OPENSSL_NO_ECDSA
2444 if (pkey->type == EVP_PKEY_EC) {
2445 j = ECDSA_verify(pkey->save_type,
2446 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2447 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
2450 al = SSL_AD_DECRYPT_ERROR;
2451 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
2457 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2458 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2465 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2468 EVP_PKEY_free(pkey);
2472 int ssl3_get_client_certificate(SSL *s)
2474 int i, ok, al, ret = -1;
2476 unsigned long l, nc, llen, n;
2477 const unsigned char *p, *q;
2479 STACK_OF(X509) *sk = NULL;
2481 n = s->method->ssl_get_message(s,
2484 -1, s->max_cert_list, &ok);
2489 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2490 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2491 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2492 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2493 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2494 al = SSL_AD_HANDSHAKE_FAILURE;
2498 * If tls asked for a client cert, the client must return a 0 list
2500 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
2501 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2502 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2503 al = SSL_AD_UNEXPECTED_MESSAGE;
2506 s->s3->tmp.reuse_message = 1;
2510 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2511 al = SSL_AD_UNEXPECTED_MESSAGE;
2512 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
2515 p = d = (unsigned char *)s->init_msg;
2517 if ((sk = sk_X509_new_null()) == NULL) {
2518 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2523 if (llen + 3 != n) {
2524 al = SSL_AD_DECODE_ERROR;
2525 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
2528 for (nc = 0; nc < llen;) {
2530 if ((l + nc + 3) > llen) {
2531 al = SSL_AD_DECODE_ERROR;
2532 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2533 SSL_R_CERT_LENGTH_MISMATCH);
2538 x = d2i_X509(NULL, &p, l);
2540 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
2544 al = SSL_AD_DECODE_ERROR;
2545 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2546 SSL_R_CERT_LENGTH_MISMATCH);
2549 if (!sk_X509_push(sk, x)) {
2550 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2557 if (sk_X509_num(sk) <= 0) {
2558 /* TLS does not mind 0 certs returned */
2559 if (s->version == SSL3_VERSION) {
2560 al = SSL_AD_HANDSHAKE_FAILURE;
2561 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2562 SSL_R_NO_CERTIFICATES_RETURNED);
2565 /* Fail for TLS only if we required a certificate */
2566 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2567 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2568 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2569 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2570 al = SSL_AD_HANDSHAKE_FAILURE;
2574 i = ssl_verify_cert_chain(s, sk);
2576 al = ssl_verify_alarm_type(s->verify_result);
2577 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2578 SSL_R_NO_CERTIFICATE_RETURNED);
2583 if (s->session->peer != NULL) /* This should not be needed */
2584 X509_free(s->session->peer);
2585 s->session->peer = sk_X509_shift(sk);
2586 s->session->verify_result = s->verify_result;
2589 * With the current implementation, sess_cert will always be NULL when we
2592 if (s->session->sess_cert == NULL) {
2593 s->session->sess_cert = ssl_sess_cert_new();
2594 if (s->session->sess_cert == NULL) {
2595 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2599 if (s->session->sess_cert->cert_chain != NULL)
2600 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2601 s->session->sess_cert->cert_chain = sk;
2603 * Inconsistency alert: cert_chain does *not* include the peer's own
2604 * certificate, while we do include it in s3_clnt.c
2612 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2618 sk_X509_pop_free(sk, X509_free);
2622 int ssl3_send_server_certificate(SSL *s)
2627 if (s->state == SSL3_ST_SW_CERT_A) {
2628 x = ssl_get_server_send_cert(s);
2630 /* VRS: allow null cert if auth == KRB5 */
2631 (s->s3->tmp.new_cipher->algorithms
2632 & (SSL_MKEY_MASK | SSL_AUTH_MASK))
2633 != (SSL_aKRB5 | SSL_kKRB5)) {
2634 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2638 l = ssl3_output_cert_chain(s, x);
2639 s->state = SSL3_ST_SW_CERT_B;
2640 s->init_num = (int)l;
2644 /* SSL3_ST_SW_CERT_B */
2645 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2648 #ifndef OPENSSL_NO_ECDH
2649 /* This is the complement of curve_id2nid in s3_clnt.c. */
2650 static int nid2curve_id(int nid)
2653 * ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001) (no changes
2654 * in draft-ietf-tls-ecc-03.txt [June 2003])
2657 case NID_sect163k1: /* sect163k1 (1) */
2659 case NID_sect163r1: /* sect163r1 (2) */
2661 case NID_sect163r2: /* sect163r2 (3) */
2663 case NID_sect193r1: /* sect193r1 (4) */
2665 case NID_sect193r2: /* sect193r2 (5) */
2667 case NID_sect233k1: /* sect233k1 (6) */
2669 case NID_sect233r1: /* sect233r1 (7) */
2671 case NID_sect239k1: /* sect239k1 (8) */
2673 case NID_sect283k1: /* sect283k1 (9) */
2675 case NID_sect283r1: /* sect283r1 (10) */
2677 case NID_sect409k1: /* sect409k1 (11) */
2679 case NID_sect409r1: /* sect409r1 (12) */
2681 case NID_sect571k1: /* sect571k1 (13) */
2683 case NID_sect571r1: /* sect571r1 (14) */
2685 case NID_secp160k1: /* secp160k1 (15) */
2687 case NID_secp160r1: /* secp160r1 (16) */
2689 case NID_secp160r2: /* secp160r2 (17) */
2691 case NID_secp192k1: /* secp192k1 (18) */
2693 case NID_X9_62_prime192v1: /* secp192r1 (19) */
2695 case NID_secp224k1: /* secp224k1 (20) */
2697 case NID_secp224r1: /* secp224r1 (21) */
2699 case NID_secp256k1: /* secp256k1 (22) */
2701 case NID_X9_62_prime256v1: /* secp256r1 (23) */
2703 case NID_secp384r1: /* secp384r1 (24) */
2705 case NID_secp521r1: /* secp521r1 (25) */
2712 #ifndef OPENSSL_NO_TLSEXT
2713 int ssl3_send_newsession_ticket(SSL *s)
2715 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
2716 unsigned char *p, *senc, *macstart;
2721 SSL_CTX *tctx = s->initial_ctx;
2722 unsigned char iv[EVP_MAX_IV_LENGTH];
2723 unsigned char key_name[16];
2725 /* get session encoding length */
2726 slen = i2d_SSL_SESSION(s->session, NULL);
2728 * Some length values are 16 bits, so forget it if session is too
2734 * Grow buffer if need be: the length calculation is as
2735 * follows 1 (size of message name) + 3 (message length
2736 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
2737 * 16 (key name) + max_iv_len (iv length) +
2738 * session_length + max_enc_block_size (max encrypted session
2739 * length) + max_md_size (HMAC).
2741 if (!BUF_MEM_grow(s->init_buf,
2742 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
2743 EVP_MAX_MD_SIZE + slen))
2745 senc = OPENSSL_malloc(slen);
2749 i2d_SSL_SESSION(s->session, &p);
2751 p = (unsigned char *)s->init_buf->data;
2753 *(p++) = SSL3_MT_NEWSESSION_TICKET;
2754 /* Skip message length for now */
2756 EVP_CIPHER_CTX_init(&ctx);
2757 HMAC_CTX_init(&hctx);
2759 * Initialize HMAC and cipher contexts. If callback present it does
2760 * all the work otherwise use generated values from parent ctx.
2762 if (tctx->tlsext_ticket_key_cb) {
2763 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
2769 RAND_pseudo_bytes(iv, 16);
2770 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2771 tctx->tlsext_tick_aes_key, iv);
2772 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2773 tlsext_tick_md(), NULL);
2774 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2776 l2n(s->session->tlsext_tick_lifetime_hint, p);
2777 /* Skip ticket length for now */
2779 /* Output key name */
2781 memcpy(p, key_name, 16);
2784 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2785 p += EVP_CIPHER_CTX_iv_length(&ctx);
2786 /* Encrypt session data */
2787 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
2789 EVP_EncryptFinal(&ctx, p, &len);
2791 EVP_CIPHER_CTX_cleanup(&ctx);
2793 HMAC_Update(&hctx, macstart, p - macstart);
2794 HMAC_Final(&hctx, p, &hlen);
2795 HMAC_CTX_cleanup(&hctx);
2798 /* Now write out lengths: p points to end of data written */
2800 len = p - (unsigned char *)s->init_buf->data;
2801 p = (unsigned char *)s->init_buf->data + 1;
2802 l2n3(len - 4, p); /* Message length */
2804 s2n(len - 10, p); /* Ticket length */
2806 /* number of bytes to write */
2808 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2813 /* SSL3_ST_SW_SESSION_TICKET_B */
2814 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2817 int ssl3_send_cert_status(SSL *s)
2819 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
2822 * Grow buffer if need be: the length calculation is as
2823 * follows 1 (message type) + 3 (message length) +
2824 * 1 (ocsp response type) + 3 (ocsp response length)
2827 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
2830 p = (unsigned char *)s->init_buf->data;
2833 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
2834 /* message length */
2835 l2n3(s->tlsext_ocsp_resplen + 4, p);
2837 *(p++) = s->tlsext_status_type;
2838 /* length of OCSP response */
2839 l2n3(s->tlsext_ocsp_resplen, p);
2840 /* actual response */
2841 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
2842 /* number of bytes to write */
2843 s->init_num = 8 + s->tlsext_ocsp_resplen;
2844 s->state = SSL3_ST_SW_CERT_STATUS_B;
2848 /* SSL3_ST_SW_CERT_STATUS_B */
2849 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));