1 /* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
151 #define REUSE_CIPHER_BUG
152 #define NETSCAPE_HANG_BUG
155 #include "ssl_locl.h"
156 #include "kssl_lcl.h"
157 #include <openssl/buffer.h>
158 #include <openssl/rand.h>
159 #include <openssl/objects.h>
160 #include <openssl/evp.h>
161 #include <openssl/hmac.h>
162 #include <openssl/x509.h>
163 #ifndef OPENSSL_NO_DH
164 #include <openssl/dh.h>
166 #include <openssl/bn.h>
167 #ifndef OPENSSL_NO_KRB5
168 #include <openssl/krb5_asn.h>
170 #include <openssl/md5.h>
172 static const SSL_METHOD *ssl3_get_server_method(int ver);
174 static const SSL_METHOD *ssl3_get_server_method(int ver)
176 if (ver == SSL3_VERSION)
177 return(SSLv3_server_method());
182 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
184 ssl_undefined_function,
185 ssl3_get_server_method)
187 int ssl3_accept(SSL *s)
190 unsigned long alg_k,Time=(unsigned long)time(NULL);
191 void (*cb)(const SSL *ssl,int type,int val)=NULL;
194 int new_state,state,skip=0;
196 RAND_add(&Time,sizeof(Time),0);
200 if (s->info_callback != NULL)
202 else if (s->ctx->info_callback != NULL)
203 cb=s->ctx->info_callback;
205 /* init things to blank */
207 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
211 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
221 case SSL_ST_RENEGOTIATE:
223 /* s->state=SSL_ST_ACCEPT; */
227 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
228 case SSL_ST_OK|SSL_ST_ACCEPT:
231 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
233 if ((s->version>>8) != 3)
235 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
238 s->type=SSL_ST_ACCEPT;
240 if (s->init_buf == NULL)
242 if ((buf=BUF_MEM_new()) == NULL)
247 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
255 if (!ssl3_setup_buffers(s))
263 if (s->state != SSL_ST_RENEGOTIATE)
265 /* Ok, we now need to push on a buffering BIO so that
266 * the output is sent in a way that TCP likes :-)
268 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
270 ssl3_init_finished_mac(s);
271 s->state=SSL3_ST_SR_CLNT_HELLO_A;
272 s->ctx->stats.sess_accept++;
276 /* s->state == SSL_ST_RENEGOTIATE,
277 * we will just send a HelloRequest */
278 s->ctx->stats.sess_accept_renegotiate++;
279 s->state=SSL3_ST_SW_HELLO_REQ_A;
283 case SSL3_ST_SW_HELLO_REQ_A:
284 case SSL3_ST_SW_HELLO_REQ_B:
287 ret=ssl3_send_hello_request(s);
288 if (ret <= 0) goto end;
289 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
290 s->state=SSL3_ST_SW_FLUSH;
293 ssl3_init_finished_mac(s);
296 case SSL3_ST_SW_HELLO_REQ_C:
300 case SSL3_ST_SR_CLNT_HELLO_A:
301 case SSL3_ST_SR_CLNT_HELLO_B:
302 case SSL3_ST_SR_CLNT_HELLO_C:
305 ret=ssl3_get_client_hello(s);
306 if (ret <= 0) goto end;
309 s->state=SSL3_ST_SW_SRVR_HELLO_A;
313 case SSL3_ST_SW_SRVR_HELLO_A:
314 case SSL3_ST_SW_SRVR_HELLO_B:
315 ret=ssl3_send_server_hello(s);
316 if (ret <= 0) goto end;
319 s->state=SSL3_ST_SW_CHANGE_A;
321 s->state=SSL3_ST_SW_CERT_A;
325 case SSL3_ST_SW_CERT_A:
326 case SSL3_ST_SW_CERT_B:
327 /* Check if it is anon DH or anon ECDH, */
328 /* normal PSK or KRB5 */
329 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
330 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
331 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
333 ret=ssl3_send_server_certificate(s);
334 if (ret <= 0) goto end;
335 #ifndef OPENSSL_NO_TLSEXT
336 if (s->tlsext_status_expected)
337 s->state=SSL3_ST_SW_CERT_STATUS_A;
339 s->state=SSL3_ST_SW_KEY_EXCH_A;
344 s->state=SSL3_ST_SW_KEY_EXCH_A;
351 s->state=SSL3_ST_SW_KEY_EXCH_A;
356 case SSL3_ST_SW_KEY_EXCH_A:
357 case SSL3_ST_SW_KEY_EXCH_B:
358 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
360 /* clear this, it may get reset by
361 * send_server_key_exchange */
362 if ((s->options & SSL_OP_EPHEMERAL_RSA)
363 #ifndef OPENSSL_NO_KRB5
364 && !(alg_k & SSL_kKRB5)
365 #endif /* OPENSSL_NO_KRB5 */
367 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
368 * even when forbidden by protocol specs
369 * (handshake may fail as clients are not required to
370 * be able to handle this) */
371 s->s3->tmp.use_rsa_tmp=1;
373 s->s3->tmp.use_rsa_tmp=0;
376 /* only send if a DH key exchange, fortezza or
377 * RSA but we have a sign only certificate
379 * PSK: may send PSK identity hints
381 * For ECC ciphersuites, we send a serverKeyExchange
382 * message only if the cipher suite is either
383 * ECDH-anon or ECDHE. In other cases, the
384 * server certificate contains the server's
385 * public key for key exchange.
387 if (s->s3->tmp.use_rsa_tmp
388 /* PSK: send ServerKeyExchange if PSK identity
389 * hint if provided */
390 #ifndef OPENSSL_NO_PSK
391 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
393 || (alg_k & (SSL_kDHr|SSL_kDHd|SSL_kEDH))
394 || (alg_k & SSL_kEECDH)
395 || ((alg_k & SSL_kRSA)
396 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
397 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
398 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
404 ret=ssl3_send_server_key_exchange(s);
405 if (ret <= 0) goto end;
410 s->state=SSL3_ST_SW_CERT_REQ_A;
414 case SSL3_ST_SW_CERT_REQ_A:
415 case SSL3_ST_SW_CERT_REQ_B:
416 if (/* don't request cert unless asked for it: */
417 !(s->verify_mode & SSL_VERIFY_PEER) ||
418 /* if SSL_VERIFY_CLIENT_ONCE is set,
419 * don't request cert during re-negotiation: */
420 ((s->session->peer != NULL) &&
421 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
422 /* never request cert in anonymous ciphersuites
423 * (see section "Certificate request" in SSL 3 drafts
424 * and in RFC 2246): */
425 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
426 /* ... except when the application insists on verification
427 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
428 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
429 /* never request cert in Kerberos ciphersuites */
430 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
431 /* With normal PSK Certificates and
432 * Certificate Requests are omitted */
433 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
435 /* no cert request */
437 s->s3->tmp.cert_request=0;
438 s->state=SSL3_ST_SW_SRVR_DONE_A;
442 s->s3->tmp.cert_request=1;
443 ret=ssl3_send_certificate_request(s);
444 if (ret <= 0) goto end;
445 #ifndef NETSCAPE_HANG_BUG
446 s->state=SSL3_ST_SW_SRVR_DONE_A;
448 s->state=SSL3_ST_SW_FLUSH;
449 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
455 case SSL3_ST_SW_SRVR_DONE_A:
456 case SSL3_ST_SW_SRVR_DONE_B:
457 ret=ssl3_send_server_done(s);
458 if (ret <= 0) goto end;
459 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
460 s->state=SSL3_ST_SW_FLUSH;
464 case SSL3_ST_SW_FLUSH:
465 /* number of bytes to be flushed */
466 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
469 s->rwstate=SSL_WRITING;
470 num1=BIO_flush(s->wbio);
471 if (num1 <= 0) { ret= -1; goto end; }
472 s->rwstate=SSL_NOTHING;
475 s->state=s->s3->tmp.next_state;
478 case SSL3_ST_SR_CERT_A:
479 case SSL3_ST_SR_CERT_B:
480 /* Check for second client hello (MS SGC) */
481 ret = ssl3_check_client_hello(s);
485 s->state = SSL3_ST_SR_CLNT_HELLO_C;
487 if (s->s3->tmp.cert_request)
489 ret=ssl3_get_client_certificate(s);
490 if (ret <= 0) goto end;
493 s->state=SSL3_ST_SR_KEY_EXCH_A;
497 case SSL3_ST_SR_KEY_EXCH_A:
498 case SSL3_ST_SR_KEY_EXCH_B:
499 ret=ssl3_get_client_key_exchange(s);
504 /* For the ECDH ciphersuites when
505 * the client sends its ECDH pub key in
506 * a certificate, the CertificateVerify
507 * message is not sent.
509 s->state=SSL3_ST_SR_FINISHED_A;
516 s->state=SSL3_ST_SR_CERT_VRFY_A;
519 /* We need to get hashes here so if there is
520 * a client cert, it can be verified
521 * FIXME - digest processing for CertificateVerify
522 * should be generalized. But it is next step
524 if (s->s3->handshake_buffer)
525 ssl3_digest_cached_records(s);
526 for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_num++)
527 if (s->s3->handshake_dgst[dgst_num])
529 s->method->ssl3_enc->cert_verify_mac(s,EVP_MD_CTX_type(s->s3->handshake_dgst[dgst_num]),&(s->s3->tmp.cert_verify_md[offset]));
530 offset+=EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
535 case SSL3_ST_SR_CERT_VRFY_A:
536 case SSL3_ST_SR_CERT_VRFY_B:
538 /* we should decide if we expected this one */
539 ret=ssl3_get_cert_verify(s);
540 if (ret <= 0) goto end;
542 s->state=SSL3_ST_SR_FINISHED_A;
546 case SSL3_ST_SR_FINISHED_A:
547 case SSL3_ST_SR_FINISHED_B:
548 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
549 SSL3_ST_SR_FINISHED_B);
550 if (ret <= 0) goto end;
553 #ifndef OPENSSL_NO_TLSEXT
554 else if (s->tlsext_ticket_expected)
555 s->state=SSL3_ST_SW_SESSION_TICKET_A;
558 s->state=SSL3_ST_SW_CHANGE_A;
562 #ifndef OPENSSL_NO_TLSEXT
563 case SSL3_ST_SW_SESSION_TICKET_A:
564 case SSL3_ST_SW_SESSION_TICKET_B:
565 ret=ssl3_send_newsession_ticket(s);
566 if (ret <= 0) goto end;
567 s->state=SSL3_ST_SW_CHANGE_A;
571 case SSL3_ST_SW_CERT_STATUS_A:
572 case SSL3_ST_SW_CERT_STATUS_B:
573 ret=ssl3_send_cert_status(s);
574 if (ret <= 0) goto end;
575 s->state=SSL3_ST_SW_KEY_EXCH_A;
581 case SSL3_ST_SW_CHANGE_A:
582 case SSL3_ST_SW_CHANGE_B:
584 s->session->cipher=s->s3->tmp.new_cipher;
585 if (!s->method->ssl3_enc->setup_key_block(s))
586 { ret= -1; goto end; }
588 ret=ssl3_send_change_cipher_spec(s,
589 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
591 if (ret <= 0) goto end;
592 s->state=SSL3_ST_SW_FINISHED_A;
595 if (!s->method->ssl3_enc->change_cipher_state(s,
596 SSL3_CHANGE_CIPHER_SERVER_WRITE))
604 case SSL3_ST_SW_FINISHED_A:
605 case SSL3_ST_SW_FINISHED_B:
606 ret=ssl3_send_finished(s,
607 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
608 s->method->ssl3_enc->server_finished_label,
609 s->method->ssl3_enc->server_finished_label_len);
610 if (ret <= 0) goto end;
611 s->state=SSL3_ST_SW_FLUSH;
613 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
615 s->s3->tmp.next_state=SSL_ST_OK;
620 /* clean a few things up */
621 ssl3_cleanup_key_block(s);
623 BUF_MEM_free(s->init_buf);
626 /* remove buffering on output */
627 ssl_free_wbio_buffer(s);
631 if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
633 /* actually not necessarily a 'new' session unless
634 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
638 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
640 s->ctx->stats.sess_accept_good++;
642 s->handshake_func=ssl3_accept;
644 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
652 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
658 if (!s->s3->tmp.reuse_message && !skip)
662 if ((ret=BIO_flush(s->wbio)) <= 0)
667 if ((cb != NULL) && (s->state != state))
671 cb(s,SSL_CB_ACCEPT_LOOP,1);
678 /* BIO_flush(s->wbio); */
682 cb(s,SSL_CB_ACCEPT_EXIT,ret);
686 int ssl3_send_hello_request(SSL *s)
690 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
692 p=(unsigned char *)s->init_buf->data;
693 *(p++)=SSL3_MT_HELLO_REQUEST;
698 s->state=SSL3_ST_SW_HELLO_REQ_B;
699 /* number of bytes to write */
704 /* SSL3_ST_SW_HELLO_REQ_B */
705 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
708 int ssl3_check_client_hello(SSL *s)
713 /* this function is called when we really expect a Certificate message,
714 * so permit appropriate message length */
715 n=s->method->ssl_get_message(s,
721 if (!ok) return((int)n);
722 s->s3->tmp.reuse_message = 1;
723 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
725 /* Throw away what we have done so far in the current handshake,
726 * which will now be aborted. (A full SSL_clear would be too much.)
727 * I hope that tmp.dh is the only thing that may need to be cleared
728 * when a handshake is not completed ... */
729 #ifndef OPENSSL_NO_DH
730 if (s->s3->tmp.dh != NULL)
732 DH_free(s->s3->tmp.dh);
733 s->s3->tmp.dh = NULL;
741 int ssl3_get_client_hello(SSL *s)
743 int i,j,ok,al,ret= -1;
744 unsigned int cookie_len;
747 unsigned char *p,*d,*q;
749 #ifndef OPENSSL_NO_COMP
752 STACK_OF(SSL_CIPHER) *ciphers=NULL;
754 /* We do this so that we will respond with our native type.
755 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
756 * This down switching should be handled by a different method.
757 * If we are SSLv3, we will respond with SSLv3, even if prompted with
760 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
762 s->state=SSL3_ST_SR_CLNT_HELLO_B;
765 n=s->method->ssl_get_message(s,
766 SSL3_ST_SR_CLNT_HELLO_B,
767 SSL3_ST_SR_CLNT_HELLO_C,
768 SSL3_MT_CLIENT_HELLO,
769 SSL3_RT_MAX_PLAIN_LENGTH,
772 if (!ok) return((int)n);
774 d=p=(unsigned char *)s->init_msg;
776 /* use version from inside client hello, not from record header
777 * (may differ: see RFC 2246, Appendix E, second paragraph) */
778 s->client_version=(((int)p[0])<<8)|(int)p[1];
781 if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
782 (s->version != DTLS1_VERSION && s->client_version < s->version))
784 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
785 if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
787 /* similar to ssl3_get_record, send alert using remote version number */
788 s->version = s->client_version;
790 al = SSL_AD_PROTOCOL_VERSION;
794 /* load the client random */
795 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
798 /* get the session-id */
802 /* Versions before 0.9.7 always allow session reuse during renegotiation
803 * (i.e. when s->new_session is true), option
804 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
805 * Maybe this optional behaviour should always have been the default,
806 * but we cannot safely change the default behaviour (or new applications
807 * might be written that become totally unsecure when compiled with
808 * an earlier library version)
810 if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
812 if (!ssl_get_new_session(s,1))
817 i=ssl_get_prev_session(s, p, j, d + n);
819 { /* previous session */
826 if (!ssl_get_new_session(s,1))
833 if (s->version == DTLS1_VERSION)
838 if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
839 s->d1->send_cookie == 0)
841 /* HelloVerifyMessage has already been sent */
842 if ( cookie_len != s->d1->cookie_len)
844 al = SSL_AD_HANDSHAKE_FAILURE;
845 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
851 * The ClientHello may contain a cookie even if the
852 * HelloVerify message has not been sent--make sure that it
853 * does not cause an overflow.
855 if ( cookie_len > sizeof(s->d1->rcvd_cookie))
858 al = SSL_AD_DECODE_ERROR;
859 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
863 /* verify the cookie if appropriate option is set. */
864 if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
867 memcpy(s->d1->rcvd_cookie, p, cookie_len);
869 if ( s->ctx->app_verify_cookie_cb != NULL)
871 if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
874 al=SSL_AD_HANDSHAKE_FAILURE;
875 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
876 SSL_R_COOKIE_MISMATCH);
879 /* else cookie verification succeeded */
881 else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie,
882 s->d1->cookie_len) != 0) /* default verification */
884 al=SSL_AD_HANDSHAKE_FAILURE;
885 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
886 SSL_R_COOKIE_MISMATCH);
895 if ((i == 0) && (j != 0))
897 /* we need a cipher if we are not resuming a session */
898 al=SSL_AD_ILLEGAL_PARAMETER;
899 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
904 /* not enough data */
905 al=SSL_AD_DECODE_ERROR;
906 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
909 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))
920 id=s->session->cipher->id;
923 printf("client sent %d ciphers\n",sk_num(ciphers));
925 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
927 c=sk_SSL_CIPHER_value(ciphers,i);
929 printf("client [%2d of %2d]:%s\n",
930 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
940 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
942 /* Very bad for multi-threading.... */
943 s->session->cipher=sk_SSL_CIPHER_value(ciphers, 0);
947 /* we need to have the cipher in the cipher
948 * list if we are asked to reuse it */
949 al=SSL_AD_ILLEGAL_PARAMETER;
950 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
960 /* not enough data */
961 al=SSL_AD_DECODE_ERROR;
962 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
968 if (p[j] == 0) break;
975 al=SSL_AD_DECODE_ERROR;
976 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
980 #ifndef OPENSSL_NO_TLSEXT
982 if (s->version > SSL3_VERSION)
984 if (!ssl_parse_clienthello_tlsext(s,&p,d,n, &al))
986 /* 'al' set by ssl_parse_clienthello_tlsext */
987 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PARSE_TLSEXT);
991 if (ssl_check_clienthello_tlsext(s) <= 0) {
992 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
997 /* Worst case, we will use the NULL compression, but if we have other
998 * options, we will now look for them. We have i-1 compression
999 * algorithms from the client, starting at q. */
1000 s->s3->tmp.new_compression=NULL;
1001 #ifndef OPENSSL_NO_COMP
1002 if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods)
1003 { /* See if we have a match */
1004 int m,nn,o,v,done=0;
1006 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
1007 for (m=0; m<nn; m++)
1009 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
1022 s->s3->tmp.new_compression=comp;
1028 /* Given s->session->ciphers and SSL_get_ciphers, we must
1033 #ifdef OPENSSL_NO_COMP
1034 s->session->compress_meth=0;
1036 s->session->compress_meth=(comp == NULL)?0:comp->id;
1038 if (s->session->ciphers != NULL)
1039 sk_SSL_CIPHER_free(s->session->ciphers);
1040 s->session->ciphers=ciphers;
1041 if (ciphers == NULL)
1043 al=SSL_AD_ILLEGAL_PARAMETER;
1044 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
1048 c=ssl3_choose_cipher(s,s->session->ciphers,
1049 SSL_get_ciphers(s));
1053 al=SSL_AD_HANDSHAKE_FAILURE;
1054 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
1057 s->s3->tmp.new_cipher=c;
1058 ssl3_digest_cached_records(s);
1062 /* Session-id reuse */
1063 #ifdef REUSE_CIPHER_BUG
1064 STACK_OF(SSL_CIPHER) *sk;
1065 SSL_CIPHER *nc=NULL;
1066 SSL_CIPHER *ec=NULL;
1068 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
1070 sk=s->session->ciphers;
1071 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1073 c=sk_SSL_CIPHER_value(sk,i);
1074 if (c->algorithm_enc & SSL_eNULL)
1076 if (SSL_C_IS_EXPORT(c))
1080 s->s3->tmp.new_cipher=nc;
1081 else if (ec != NULL)
1082 s->s3->tmp.new_cipher=ec;
1084 s->s3->tmp.new_cipher=s->session->cipher;
1088 s->s3->tmp.new_cipher=s->session->cipher;
1089 /* Clear cached handshake records */
1090 BIO_free(s->s3->handshake_buffer);
1091 s->s3->handshake_buffer = NULL;
1094 /* we now have the following setup.
1096 * cipher_list - our prefered list of ciphers
1097 * ciphers - the clients prefered list of ciphers
1098 * compression - basically ignored right now
1099 * ssl version is set - sslv3
1100 * s->session - The ssl session has been setup.
1101 * s->hit - session reuse flag
1102 * s->tmp.new_cipher - the new cipher to use.
1109 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1112 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
1116 int ssl3_send_server_hello(SSL *s)
1119 unsigned char *p,*d;
1121 unsigned long l,Time;
1123 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
1125 buf=(unsigned char *)s->init_buf->data;
1126 p=s->s3->server_random;
1127 Time=(unsigned long)time(NULL); /* Time */
1129 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
1131 /* Do the message type and length last */
1134 *(p++)=s->version>>8;
1135 *(p++)=s->version&0xff;
1138 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
1139 p+=SSL3_RANDOM_SIZE;
1141 /* now in theory we have 3 options to sending back the
1142 * session id. If it is a re-use, we send back the
1143 * old session-id, if it is a new session, we send
1144 * back the new session-id or we send back a 0 length
1145 * session-id if we want it to be single use.
1146 * Currently I will not implement the '0' length session-id
1147 * 12-Jan-98 - I'll now support the '0' length stuff.
1149 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
1150 s->session->session_id_length=0;
1152 sl=s->session->session_id_length;
1153 if (sl > (int)sizeof(s->session->session_id))
1155 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1159 memcpy(p,s->session->session_id,sl);
1162 /* put the cipher */
1163 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
1166 /* put the compression method */
1167 #ifdef OPENSSL_NO_COMP
1170 if (s->s3->tmp.new_compression == NULL)
1173 *(p++)=s->s3->tmp.new_compression->id;
1175 #ifndef OPENSSL_NO_TLSEXT
1176 if (ssl_prepare_serverhello_tlsext(s) <= 0)
1178 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
1181 if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
1183 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
1190 *(d++)=SSL3_MT_SERVER_HELLO;
1193 s->state=SSL3_ST_CW_CLNT_HELLO_B;
1194 /* number of bytes to write */
1199 /* SSL3_ST_CW_CLNT_HELLO_B */
1200 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1203 int ssl3_send_server_done(SSL *s)
1207 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1209 p=(unsigned char *)s->init_buf->data;
1212 *(p++)=SSL3_MT_SERVER_DONE;
1217 s->state=SSL3_ST_SW_SRVR_DONE_B;
1218 /* number of bytes to write */
1223 /* SSL3_ST_CW_CLNT_HELLO_B */
1224 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1227 int ssl3_send_server_key_exchange(SSL *s)
1229 #ifndef OPENSSL_NO_RSA
1233 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1236 #ifndef OPENSSL_NO_DH
1239 #ifndef OPENSSL_NO_ECDH
1240 EC_KEY *ecdh=NULL, *ecdhp;
1241 unsigned char *encodedPoint = NULL;
1244 BN_CTX *bn_ctx = NULL;
1247 unsigned char *p,*d;
1257 EVP_MD_CTX_init(&md_ctx);
1258 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1260 type=s->s3->tmp.new_cipher->algorithm_mkey;
1265 r[0]=r[1]=r[2]=r[3]=NULL;
1267 #ifndef OPENSSL_NO_RSA
1268 if (type & SSL_kRSA)
1271 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1273 rsa=s->cert->rsa_tmp_cb(s,
1274 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1275 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1278 al=SSL_AD_HANDSHAKE_FAILURE;
1279 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1287 al=SSL_AD_HANDSHAKE_FAILURE;
1288 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1293 s->s3->tmp.use_rsa_tmp=1;
1297 #ifndef OPENSSL_NO_DH
1298 if (type & SSL_kEDH)
1301 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1302 dhp=s->cert->dh_tmp_cb(s,
1303 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1304 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1307 al=SSL_AD_HANDSHAKE_FAILURE;
1308 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1312 if (s->s3->tmp.dh != NULL)
1315 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1319 if ((dh=DHparams_dup(dhp)) == NULL)
1321 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1326 if ((dhp->pub_key == NULL ||
1327 dhp->priv_key == NULL ||
1328 (s->options & SSL_OP_SINGLE_DH_USE)))
1330 if(!DH_generate_key(dh))
1332 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1339 dh->pub_key=BN_dup(dhp->pub_key);
1340 dh->priv_key=BN_dup(dhp->priv_key);
1341 if ((dh->pub_key == NULL) ||
1342 (dh->priv_key == NULL))
1344 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1354 #ifndef OPENSSL_NO_ECDH
1355 if (type & SSL_kEECDH)
1357 const EC_GROUP *group;
1359 ecdhp=cert->ecdh_tmp;
1360 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
1362 ecdhp=s->cert->ecdh_tmp_cb(s,
1363 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1364 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1368 al=SSL_AD_HANDSHAKE_FAILURE;
1369 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
1373 if (s->s3->tmp.ecdh != NULL)
1375 EC_KEY_free(s->s3->tmp.ecdh);
1376 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1380 /* Duplicate the ECDH structure. */
1383 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1386 if (!EC_KEY_up_ref(ecdhp))
1388 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1393 s->s3->tmp.ecdh=ecdh;
1394 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1395 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1396 (s->options & SSL_OP_SINGLE_ECDH_USE))
1398 if(!EC_KEY_generate_key(ecdh))
1400 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1405 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1406 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1407 (EC_KEY_get0_private_key(ecdh) == NULL))
1409 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1413 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1414 (EC_GROUP_get_degree(group) > 163))
1416 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1420 /* XXX: For now, we only support ephemeral ECDH
1421 * keys over named (not generic) curves. For
1422 * supported named curves, curve_id is non-zero.
1425 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1428 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1432 /* Encode the public key.
1433 * First check the size of encoding and
1434 * allocate memory accordingly.
1436 encodedlen = EC_POINT_point2oct(group,
1437 EC_KEY_get0_public_key(ecdh),
1438 POINT_CONVERSION_UNCOMPRESSED,
1441 encodedPoint = (unsigned char *)
1442 OPENSSL_malloc(encodedlen*sizeof(unsigned char));
1443 bn_ctx = BN_CTX_new();
1444 if ((encodedPoint == NULL) || (bn_ctx == NULL))
1446 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1451 encodedlen = EC_POINT_point2oct(group,
1452 EC_KEY_get0_public_key(ecdh),
1453 POINT_CONVERSION_UNCOMPRESSED,
1454 encodedPoint, encodedlen, bn_ctx);
1456 if (encodedlen == 0)
1458 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1462 BN_CTX_free(bn_ctx); bn_ctx=NULL;
1464 /* XXX: For now, we only support named (not
1465 * generic) curves in ECDH ephemeral key exchanges.
1466 * In this situation, we need four additional bytes
1467 * to encode the entire ServerECDHParams
1472 /* We'll generate the serverKeyExchange message
1473 * explicitly so we can set these to NULLs
1481 #endif /* !OPENSSL_NO_ECDH */
1482 #ifndef OPENSSL_NO_PSK
1483 if (type & SSL_kPSK)
1485 /* reserve size for record length and PSK identity hint*/
1486 n+=2+strlen(s->ctx->psk_identity_hint);
1489 #endif /* !OPENSSL_NO_PSK */
1491 al=SSL_AD_HANDSHAKE_FAILURE;
1492 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1495 for (i=0; r[i] != NULL; i++)
1497 nr[i]=BN_num_bytes(r[i]);
1501 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1502 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
1504 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1507 al=SSL_AD_DECODE_ERROR;
1510 kn=EVP_PKEY_size(pkey);
1518 if (!BUF_MEM_grow_clean(buf,n+4+kn))
1520 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1523 d=(unsigned char *)s->init_buf->data;
1526 for (i=0; r[i] != NULL; i++)
1533 #ifndef OPENSSL_NO_ECDH
1534 if (type & SSL_kEECDH)
1536 /* XXX: For now, we only support named (not generic) curves.
1537 * In this situation, the serverKeyExchange message has:
1538 * [1 byte CurveType], [2 byte CurveName]
1539 * [1 byte length of encoded point], followed by
1540 * the actual encoded point itself
1542 *p = NAMED_CURVE_TYPE;
1550 memcpy((unsigned char*)p,
1551 (unsigned char *)encodedPoint,
1553 OPENSSL_free(encodedPoint);
1558 #ifndef OPENSSL_NO_PSK
1559 if (type & SSL_kPSK)
1561 /* copy PSK identity hint */
1562 s2n(strlen(s->ctx->psk_identity_hint), p);
1563 strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
1564 p+=strlen(s->ctx->psk_identity_hint);
1571 /* n is the length of the params, they start at &(d[4])
1572 * and p points to the space at the end. */
1573 #ifndef OPENSSL_NO_RSA
1574 if (pkey->type == EVP_PKEY_RSA)
1578 for (num=2; num > 0; num--)
1580 EVP_DigestInit_ex(&md_ctx,(num == 2)
1581 ?s->ctx->md5:s->ctx->sha1, NULL);
1582 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1583 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1584 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1585 EVP_DigestFinal_ex(&md_ctx,q,
1586 (unsigned int *)&i);
1590 if (RSA_sign(NID_md5_sha1, md_buf, j,
1591 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1593 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1601 #if !defined(OPENSSL_NO_DSA)
1602 if (pkey->type == EVP_PKEY_DSA)
1605 EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
1606 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1607 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1608 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1609 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1610 (unsigned int *)&i,pkey))
1612 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1620 #if !defined(OPENSSL_NO_ECDSA)
1621 if (pkey->type == EVP_PKEY_EC)
1623 /* let's do ECDSA */
1624 EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
1625 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1626 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1627 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1628 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1629 (unsigned int *)&i,pkey))
1631 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
1640 /* Is this error check actually needed? */
1641 al=SSL_AD_HANDSHAKE_FAILURE;
1642 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1647 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1650 /* we should now have things packed up, so lets send
1656 s->state = SSL3_ST_SW_KEY_EXCH_B;
1657 EVP_MD_CTX_cleanup(&md_ctx);
1658 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1660 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1662 #ifndef OPENSSL_NO_ECDH
1663 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
1664 BN_CTX_free(bn_ctx);
1666 EVP_MD_CTX_cleanup(&md_ctx);
1670 int ssl3_send_certificate_request(SSL *s)
1672 unsigned char *p,*d;
1674 STACK_OF(X509_NAME) *sk=NULL;
1678 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1682 d=p=(unsigned char *)&(buf->data[4]);
1684 /* get the list of acceptable cert types */
1686 n=ssl3_get_req_cert_type(s,p);
1695 sk=SSL_get_client_CA_list(s);
1699 for (i=0; i<sk_X509_NAME_num(sk); i++)
1701 name=sk_X509_NAME_value(sk,i);
1702 j=i2d_X509_NAME(name,NULL);
1703 if (!BUF_MEM_grow_clean(buf,4+n+j+2))
1705 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1708 p=(unsigned char *)&(buf->data[4+n]);
1709 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1712 i2d_X509_NAME(name,&p);
1719 i2d_X509_NAME(name,&p);
1720 j-=2; s2n(j,d); j+=2;
1726 /* else no CA names */
1727 p=(unsigned char *)&(buf->data[4+off]);
1730 d=(unsigned char *)buf->data;
1731 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1734 /* we should now have things packed up, so lets send
1739 #ifdef NETSCAPE_HANG_BUG
1740 p=(unsigned char *)s->init_buf->data + s->init_num;
1743 *(p++)=SSL3_MT_SERVER_DONE;
1750 s->state = SSL3_ST_SW_CERT_REQ_B;
1753 /* SSL3_ST_SW_CERT_REQ_B */
1754 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1759 int ssl3_get_client_key_exchange(SSL *s)
1763 unsigned long alg_k;
1765 #ifndef OPENSSL_NO_RSA
1767 EVP_PKEY *pkey=NULL;
1769 #ifndef OPENSSL_NO_DH
1773 #ifndef OPENSSL_NO_KRB5
1775 #endif /* OPENSSL_NO_KRB5 */
1777 #ifndef OPENSSL_NO_ECDH
1778 EC_KEY *srvr_ecdh = NULL;
1779 EVP_PKEY *clnt_pub_pkey = NULL;
1780 EC_POINT *clnt_ecpoint = NULL;
1781 BN_CTX *bn_ctx = NULL;
1784 n=s->method->ssl_get_message(s,
1785 SSL3_ST_SR_KEY_EXCH_A,
1786 SSL3_ST_SR_KEY_EXCH_B,
1787 SSL3_MT_CLIENT_KEY_EXCHANGE,
1791 if (!ok) return((int)n);
1792 p=(unsigned char *)s->init_msg;
1794 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1796 #ifndef OPENSSL_NO_RSA
1797 if (alg_k & SSL_kRSA)
1799 /* FIX THIS UP EAY EAY EAY EAY */
1800 if (s->s3->tmp.use_rsa_tmp)
1802 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1803 rsa=s->cert->rsa_tmp;
1804 /* Don't do a callback because rsa_tmp should
1805 * be sent already */
1808 al=SSL_AD_HANDSHAKE_FAILURE;
1809 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1816 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1817 if ( (pkey == NULL) ||
1818 (pkey->type != EVP_PKEY_RSA) ||
1819 (pkey->pkey.rsa == NULL))
1821 al=SSL_AD_HANDSHAKE_FAILURE;
1822 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1828 /* TLS and [incidentally] DTLS{0xFEFF} */
1829 if (s->version > SSL3_VERSION)
1834 if (!(s->options & SSL_OP_TLS_D5_BUG))
1836 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1846 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1850 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1852 al=SSL_AD_DECODE_ERROR;
1853 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
1856 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
1858 /* The premaster secret must contain the same version number as the
1859 * ClientHello to detect version rollback attacks (strangely, the
1860 * protocol does not offer such protection for DH ciphersuites).
1861 * However, buggy clients exist that send the negotiated protocol
1862 * version instead if the server does not support the requested
1864 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1865 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1866 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
1868 al=SSL_AD_DECODE_ERROR;
1869 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
1871 /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1872 * (http://eprint.iacr.org/2003/052/) exploits the version
1873 * number check as a "bad version oracle" -- an alert would
1874 * reveal that the plaintext corresponding to some ciphertext
1875 * made up by the adversary is properly formatted except
1876 * that the version number is wrong. To avoid such attacks,
1877 * we should treat this just like any other decryption error. */
1883 /* Some decryption failure -- use random value instead as countermeasure
1884 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1885 * (see RFC 2246, section 7.4.7.1). */
1887 i = SSL_MAX_MASTER_KEY_LENGTH;
1888 p[0] = s->client_version >> 8;
1889 p[1] = s->client_version & 0xff;
1890 if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_bytes, but we cannot work around a failure */
1894 s->session->master_key_length=
1895 s->method->ssl3_enc->generate_master_secret(s,
1896 s->session->master_key,
1898 OPENSSL_cleanse(p,i);
1902 #ifndef OPENSSL_NO_DH
1903 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1908 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1910 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1920 if (n == 0L) /* the parameters are in the cert */
1922 al=SSL_AD_HANDSHAKE_FAILURE;
1923 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1928 if (s->s3->tmp.dh == NULL)
1930 al=SSL_AD_HANDSHAKE_FAILURE;
1931 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1935 dh_srvr=s->s3->tmp.dh;
1938 pub=BN_bin2bn(p,i,NULL);
1941 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1945 i=DH_compute_key(p,pub,dh_srvr);
1949 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1953 DH_free(s->s3->tmp.dh);
1958 s->session->master_key_length=
1959 s->method->ssl3_enc->generate_master_secret(s,
1960 s->session->master_key,p,i);
1961 OPENSSL_cleanse(p,i);
1965 #ifndef OPENSSL_NO_KRB5
1966 if (alg_k & SSL_kKRB5)
1968 krb5_error_code krb5rc;
1969 krb5_data enc_ticket;
1970 krb5_data authenticator;
1972 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1973 EVP_CIPHER_CTX ciph_ctx;
1974 EVP_CIPHER *enc = NULL;
1975 unsigned char iv[EVP_MAX_IV_LENGTH];
1976 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
1977 + EVP_MAX_BLOCK_LENGTH];
1979 krb5_timestamp authtime = 0;
1980 krb5_ticket_times ttimes;
1982 EVP_CIPHER_CTX_init(&ciph_ctx);
1984 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
1987 enc_ticket.length = i;
1989 if (n < enc_ticket.length + 6)
1991 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1992 SSL_R_DATA_LENGTH_TOO_LONG);
1996 enc_ticket.data = (char *)p;
1997 p+=enc_ticket.length;
2000 authenticator.length = i;
2002 if (n < enc_ticket.length + authenticator.length + 6)
2004 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2005 SSL_R_DATA_LENGTH_TOO_LONG);
2009 authenticator.data = (char *)p;
2010 p+=authenticator.length;
2014 enc_pms.data = (char *)p;
2017 /* Note that the length is checked again below,
2020 if(enc_pms.length > sizeof pms)
2022 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2023 SSL_R_DATA_LENGTH_TOO_LONG);
2027 if (n != (long)(enc_ticket.length + authenticator.length +
2028 enc_pms.length + 6))
2030 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2031 SSL_R_DATA_LENGTH_TOO_LONG);
2035 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2039 printf("kssl_sget_tkt rtn %d [%d]\n",
2040 krb5rc, kssl_err.reason);
2042 printf("kssl_err text= %s\n", kssl_err.text);
2043 #endif /* KSSL_DEBUG */
2044 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2049 /* Note: no authenticator is not considered an error,
2050 ** but will return authtime == 0.
2052 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2053 &authtime, &kssl_err)) != 0)
2056 printf("kssl_check_authent rtn %d [%d]\n",
2057 krb5rc, kssl_err.reason);
2059 printf("kssl_err text= %s\n", kssl_err.text);
2060 #endif /* KSSL_DEBUG */
2061 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2066 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
2068 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2073 kssl_ctx_show(kssl_ctx);
2074 #endif /* KSSL_DEBUG */
2076 enc = kssl_map_enc(kssl_ctx->enctype);
2080 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2082 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
2084 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2085 SSL_R_DECRYPTION_FAILED);
2088 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
2089 (unsigned char *)enc_pms.data, enc_pms.length))
2091 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2092 SSL_R_DECRYPTION_FAILED);
2095 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
2097 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2098 SSL_R_DATA_LENGTH_TOO_LONG);
2101 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
2103 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2104 SSL_R_DECRYPTION_FAILED);
2108 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
2110 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2111 SSL_R_DATA_LENGTH_TOO_LONG);
2114 if (!((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
2116 /* The premaster secret must contain the same version number as the
2117 * ClientHello to detect version rollback attacks (strangely, the
2118 * protocol does not offer such protection for DH ciphersuites).
2119 * However, buggy clients exist that send random bytes instead of
2120 * the protocol version.
2121 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2122 * (Perhaps we should have a separate BUG value for the Kerberos cipher)
2124 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
2125 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
2127 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2128 SSL_AD_DECODE_ERROR);
2133 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2135 s->session->master_key_length=
2136 s->method->ssl3_enc->generate_master_secret(s,
2137 s->session->master_key, pms, outl);
2139 if (kssl_ctx->client_princ)
2141 size_t len = strlen(kssl_ctx->client_princ);
2142 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
2144 s->session->krb5_client_princ_len = len;
2145 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
2150 /* Was doing kssl_ctx_free() here,
2151 ** but it caused problems for apache.
2152 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
2153 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
2157 #endif /* OPENSSL_NO_KRB5 */
2159 #ifndef OPENSSL_NO_ECDH
2160 if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2165 const EC_GROUP *group;
2166 const BIGNUM *priv_key;
2168 /* initialize structures for server's ECDH key pair */
2169 if ((srvr_ecdh = EC_KEY_new()) == NULL)
2171 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2172 ERR_R_MALLOC_FAILURE);
2176 /* Let's get server private key and group information */
2177 if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2179 /* use the certificate */
2180 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2184 /* use the ephermeral values we saved when
2185 * generating the ServerKeyExchange msg.
2187 tkey = s->s3->tmp.ecdh;
2190 group = EC_KEY_get0_group(tkey);
2191 priv_key = EC_KEY_get0_private_key(tkey);
2193 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2194 !EC_KEY_set_private_key(srvr_ecdh, priv_key))
2196 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2201 /* Let's get client's public key */
2202 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
2204 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2205 ERR_R_MALLOC_FAILURE);
2211 /* Client Publickey was in Client Certificate */
2213 if (alg_k & SSL_kEECDH)
2215 al=SSL_AD_HANDSHAKE_FAILURE;
2216 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
2219 if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
2221 (clnt_pub_pkey->type != EVP_PKEY_EC))
2223 /* XXX: For now, we do not support client
2224 * authentication using ECDH certificates
2225 * so this branch (n == 0L) of the code is
2226 * never executed. When that support is
2227 * added, we ought to ensure the key
2228 * received in the certificate is
2229 * authorized for key agreement.
2230 * ECDH_compute_key implicitly checks that
2231 * the two ECDH shares are for the same
2234 al=SSL_AD_HANDSHAKE_FAILURE;
2235 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2236 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2240 if (EC_POINT_copy(clnt_ecpoint,
2241 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)
2243 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2247 ret = 2; /* Skip certificate verify processing */
2251 /* Get client's public key from encoded point
2252 * in the ClientKeyExchange message.
2254 if ((bn_ctx = BN_CTX_new()) == NULL)
2256 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2257 ERR_R_MALLOC_FAILURE);
2261 /* Get encoded point length */
2264 if (EC_POINT_oct2point(group,
2265 clnt_ecpoint, p, i, bn_ctx) == 0)
2267 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2271 /* p is pointing to somewhere in the buffer
2272 * currently, so set it to the start
2274 p=(unsigned char *)s->init_buf->data;
2277 /* Compute the shared pre-master secret */
2278 field_size = EC_GROUP_get_degree(group);
2279 if (field_size <= 0)
2281 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2285 i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
2288 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2293 EVP_PKEY_free(clnt_pub_pkey);
2294 EC_POINT_free(clnt_ecpoint);
2295 if (srvr_ecdh != NULL)
2296 EC_KEY_free(srvr_ecdh);
2297 BN_CTX_free(bn_ctx);
2299 /* Compute the master secret */
2300 s->session->master_key_length = s->method->ssl3_enc-> \
2301 generate_master_secret(s, s->session->master_key, p, i);
2303 OPENSSL_cleanse(p, i);
2308 #ifndef OPENSSL_NO_PSK
2309 if (alg_k & SSL_kPSK)
2311 unsigned char *t = NULL;
2312 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
2313 unsigned int pre_ms_len = 0, psk_len = 0;
2315 char tmp_id[PSK_MAX_IDENTITY_LEN+1];
2317 al=SSL_AD_HANDSHAKE_FAILURE;
2322 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2323 SSL_R_LENGTH_MISMATCH);
2326 if (i > PSK_MAX_IDENTITY_LEN)
2328 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2329 SSL_R_DATA_LENGTH_TOO_LONG);
2332 if (s->psk_server_callback == NULL)
2334 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2335 SSL_R_PSK_NO_SERVER_CB);
2339 /* Create guaranteed NULL-terminated identity
2340 * string for the callback */
2341 memcpy(tmp_id, p, i);
2342 memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
2343 psk_len = s->psk_server_callback(s, tmp_id,
2344 psk_or_pre_ms, sizeof(psk_or_pre_ms));
2345 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1);
2347 if (psk_len > PSK_MAX_PSK_LEN)
2349 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2350 ERR_R_INTERNAL_ERROR);
2353 else if (psk_len == 0)
2355 /* PSK related to the given identity not found */
2356 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2357 SSL_R_PSK_IDENTITY_NOT_FOUND);
2358 al=SSL_AD_UNKNOWN_PSK_IDENTITY;
2362 /* create PSK pre_master_secret */
2363 pre_ms_len=2+psk_len+2+psk_len;
2365 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
2367 memset(t, 0, psk_len);
2371 if (s->session->psk_identity != NULL)
2372 OPENSSL_free(s->session->psk_identity);
2373 s->session->psk_identity = BUF_strdup((char *)p);
2374 if (s->session->psk_identity == NULL)
2376 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2377 ERR_R_MALLOC_FAILURE);
2381 if (s->session->psk_identity_hint != NULL)
2382 OPENSSL_free(s->session->psk_identity_hint);
2383 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2384 if (s->ctx->psk_identity_hint != NULL &&
2385 s->session->psk_identity_hint == NULL)
2387 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2388 ERR_R_MALLOC_FAILURE);
2392 s->session->master_key_length=
2393 s->method->ssl3_enc->generate_master_secret(s,
2394 s->session->master_key, psk_or_pre_ms, pre_ms_len);
2397 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2403 if (alg_k & SSL_kGOST)
2405 EVP_PKEY_CTX *pkey_ctx;
2406 unsigned char premaster_secret[32];
2409 /* Get our certificate privatec key*/
2410 pkey_ctx = EVP_PKEY_CTX_new(s->cert->key->privatekey,NULL);
2411 EVP_PKEY_decrypt_init(pkey_ctx);
2412 /* Decrypt session key */
2413 if ((*p!=( V_ASN1_SEQUENCE| V_ASN1_CONSTRUCTED)) || p[1]!=0x81 )
2415 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
2418 if (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,p+3,p[2]) <0)
2421 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
2424 /* Generate master secret */
2425 EVP_PKEY_CTX_free(pkey_ctx);
2426 s->session->master_key_length=
2427 s->method->ssl3_enc->generate_master_secret(s,
2428 s->session->master_key,premaster_secret,32);
2433 al=SSL_AD_HANDSHAKE_FAILURE;
2434 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2435 SSL_R_UNKNOWN_CIPHER_TYPE);
2441 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2442 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
2445 #ifndef OPENSSL_NO_ECDH
2446 EVP_PKEY_free(clnt_pub_pkey);
2447 EC_POINT_free(clnt_ecpoint);
2448 if (srvr_ecdh != NULL)
2449 EC_KEY_free(srvr_ecdh);
2450 BN_CTX_free(bn_ctx);
2455 int ssl3_get_cert_verify(SSL *s)
2457 EVP_PKEY *pkey=NULL;
2464 n=s->method->ssl_get_message(s,
2465 SSL3_ST_SR_CERT_VRFY_A,
2466 SSL3_ST_SR_CERT_VRFY_B,
2471 if (!ok) return((int)n);
2473 if (s->session->peer != NULL)
2475 peer=s->session->peer;
2476 pkey=X509_get_pubkey(peer);
2477 type=X509_certificate_type(peer,pkey);
2485 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
2487 s->s3->tmp.reuse_message=1;
2488 if ((peer != NULL) && (type | EVP_PKT_SIGN))
2490 al=SSL_AD_UNEXPECTED_MESSAGE;
2491 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
2500 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
2501 al=SSL_AD_UNEXPECTED_MESSAGE;
2505 if (!(type & EVP_PKT_SIGN))
2507 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2508 al=SSL_AD_ILLEGAL_PARAMETER;
2512 if (s->s3->change_cipher_spec)
2514 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
2515 al=SSL_AD_UNEXPECTED_MESSAGE;
2519 /* we now have a signature that we need to verify */
2520 p=(unsigned char *)s->init_msg;
2521 /* Check for broken implementations of GOST ciphersuites */
2522 /* If key is GOST and n is exactly 64, it is bare
2523 * signature without length field */
2524 if (n==64 && (pkey->type==NID_id_GostR3410_94 ||
2525 pkey->type == NID_id_GostR3410_2001) )
2535 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
2536 al=SSL_AD_DECODE_ERROR;
2540 j=EVP_PKEY_size(pkey);
2541 if ((i > j) || (n > j) || (n <= 0))
2543 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
2544 al=SSL_AD_DECODE_ERROR;
2548 #ifndef OPENSSL_NO_RSA
2549 if (pkey->type == EVP_PKEY_RSA)
2551 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2552 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
2556 al=SSL_AD_DECRYPT_ERROR;
2557 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
2562 al=SSL_AD_DECRYPT_ERROR;
2563 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
2569 #ifndef OPENSSL_NO_DSA
2570 if (pkey->type == EVP_PKEY_DSA)
2572 j=DSA_verify(pkey->save_type,
2573 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2574 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
2578 al=SSL_AD_DECRYPT_ERROR;
2579 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
2585 #ifndef OPENSSL_NO_ECDSA
2586 if (pkey->type == EVP_PKEY_EC)
2588 j=ECDSA_verify(pkey->save_type,
2589 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2590 SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
2594 al=SSL_AD_DECRYPT_ERROR;
2595 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2596 SSL_R_BAD_ECDSA_SIGNATURE);
2602 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
2603 { unsigned char signature[64];
2605 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey,NULL);
2606 EVP_PKEY_verify_init(pctx);
2608 fprintf(stderr,"GOST signature length is %d",i);
2610 for (idx=0;idx<64;idx++) {
2611 signature[63-idx]=p[idx];
2613 j=EVP_PKEY_verify(pctx,signature,64,s->s3->tmp.cert_verify_md,32);
2614 EVP_PKEY_CTX_free(pctx);
2617 al=SSL_AD_DECRYPT_ERROR;
2618 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2619 SSL_R_BAD_ECDSA_SIGNATURE);
2625 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
2626 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
2635 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2638 EVP_PKEY_free(pkey);
2642 int ssl3_get_client_certificate(SSL *s)
2644 int i,ok,al,ret= -1;
2646 unsigned long l,nc,llen,n;
2647 const unsigned char *p,*q;
2649 STACK_OF(X509) *sk=NULL;
2651 n=s->method->ssl_get_message(s,
2658 if (!ok) return((int)n);
2660 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
2662 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
2663 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
2665 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2666 al=SSL_AD_HANDSHAKE_FAILURE;
2669 /* If tls asked for a client cert, the client must return a 0 list */
2670 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
2672 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2673 al=SSL_AD_UNEXPECTED_MESSAGE;
2676 s->s3->tmp.reuse_message=1;
2680 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
2682 al=SSL_AD_UNEXPECTED_MESSAGE;
2683 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
2686 p=d=(unsigned char *)s->init_msg;
2688 if ((sk=sk_X509_new_null()) == NULL)
2690 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
2697 al=SSL_AD_DECODE_ERROR;
2698 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
2701 for (nc=0; nc<llen; )
2704 if ((l+nc+3) > llen)
2706 al=SSL_AD_DECODE_ERROR;
2707 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
2712 x=d2i_X509(NULL,&p,l);
2715 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
2720 al=SSL_AD_DECODE_ERROR;
2721 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
2724 if (!sk_X509_push(sk,x))
2726 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
2733 if (sk_X509_num(sk) <= 0)
2735 /* TLS does not mind 0 certs returned */
2736 if (s->version == SSL3_VERSION)
2738 al=SSL_AD_HANDSHAKE_FAILURE;
2739 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
2742 /* Fail for TLS only if we required a certificate */
2743 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2744 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
2746 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2747 al=SSL_AD_HANDSHAKE_FAILURE;
2753 i=ssl_verify_cert_chain(s,sk);
2756 al=ssl_verify_alarm_type(s->verify_result);
2757 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
2762 if (s->session->peer != NULL) /* This should not be needed */
2763 X509_free(s->session->peer);
2764 s->session->peer=sk_X509_shift(sk);
2765 s->session->verify_result = s->verify_result;
2767 /* With the current implementation, sess_cert will always be NULL
2768 * when we arrive here. */
2769 if (s->session->sess_cert == NULL)
2771 s->session->sess_cert = ssl_sess_cert_new();
2772 if (s->session->sess_cert == NULL)
2774 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2778 if (s->session->sess_cert->cert_chain != NULL)
2779 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2780 s->session->sess_cert->cert_chain=sk;
2781 /* Inconsistency alert: cert_chain does *not* include the
2782 * peer's own certificate, while we do include it in s3_clnt.c */
2790 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2793 if (x != NULL) X509_free(x);
2794 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
2798 int ssl3_send_server_certificate(SSL *s)
2803 if (s->state == SSL3_ST_SW_CERT_A)
2805 x=ssl_get_server_send_cert(s);
2808 /* VRS: allow null cert if auth == KRB5 */
2809 if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
2810 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5))
2812 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
2817 l=ssl3_output_cert_chain(s,x);
2818 s->state=SSL3_ST_SW_CERT_B;
2823 /* SSL3_ST_SW_CERT_B */
2824 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2826 #ifndef OPENSSL_NO_TLSEXT
2827 int ssl3_send_newsession_ticket(SSL *s)
2829 if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
2831 unsigned char *p, *senc, *macstart;
2837 /* get session encoding length */
2838 slen = i2d_SSL_SESSION(s->session, NULL);
2839 /* Some length values are 16 bits, so forget it if session is
2844 /* Grow buffer if need be: the length calculation is as
2845 * follows 1 (size of message name) + 3 (message length
2846 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
2847 * 16 (key name) + max_iv_len (iv length) +
2848 * session_length + max_enc_block_size (max encrypted session
2849 * length) + max_md_size (HMAC).
2851 if (!BUF_MEM_grow(s->init_buf,
2852 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
2853 EVP_MAX_MD_SIZE + slen))
2855 senc = OPENSSL_malloc(slen);
2859 i2d_SSL_SESSION(s->session, &p);
2861 p=(unsigned char *)s->init_buf->data;
2863 *(p++)=SSL3_MT_NEWSESSION_TICKET;
2864 /* Skip message length for now */
2866 l2n(s->session->tlsext_tick_lifetime_hint, p);
2867 /* Skip ticket length for now */
2869 /* Output key name */
2871 memcpy(p, s->ctx->tlsext_tick_key_name, 16);
2873 /* Generate and output IV */
2874 RAND_pseudo_bytes(p, 16);
2875 EVP_CIPHER_CTX_init(&ctx);
2876 /* Encrypt session data */
2877 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2878 s->ctx->tlsext_tick_aes_key, p);
2880 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
2882 EVP_EncryptFinal(&ctx, p, &len);
2884 EVP_CIPHER_CTX_cleanup(&ctx);
2886 HMAC_CTX_init(&hctx);
2887 HMAC_Init_ex(&hctx, s->ctx->tlsext_tick_hmac_key, 16,
2888 tlsext_tick_md(), NULL);
2889 HMAC_Update(&hctx, macstart, p - macstart);
2890 HMAC_Final(&hctx, p, &hlen);
2891 HMAC_CTX_cleanup(&hctx);
2894 /* Now write out lengths: p points to end of data written */
2896 len = p - (unsigned char *)s->init_buf->data;
2897 p=(unsigned char *)s->init_buf->data + 1;
2898 l2n3(len - 4, p); /* Message length */
2900 s2n(len - 10, p); /* Ticket length */
2902 /* number of bytes to write */
2904 s->state=SSL3_ST_SW_SESSION_TICKET_B;
2909 /* SSL3_ST_SW_SESSION_TICKET_B */
2910 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2913 int ssl3_send_cert_status(SSL *s)
2915 if (s->state == SSL3_ST_SW_CERT_STATUS_A)
2918 /* Grow buffer if need be: the length calculation is as
2919 * follows 1 (message type) + 3 (message length) +
2920 * 1 (ocsp response type) + 3 (ocsp response length)
2923 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
2926 p=(unsigned char *)s->init_buf->data;
2929 *(p++)=SSL3_MT_CERTIFICATE_STATUS;
2930 /* message length */
2931 l2n3(s->tlsext_ocsp_resplen + 4, p);
2933 *(p++)= s->tlsext_status_type;
2934 /* length of OCSP response */
2935 l2n3(s->tlsext_ocsp_resplen, p);
2936 /* actual response */
2937 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
2938 /* number of bytes to write */
2939 s->init_num = 8 + s->tlsext_ocsp_resplen;
2940 s->state=SSL3_ST_SW_CERT_STATUS_B;
2944 /* SSL3_ST_SW_CERT_STATUS_B */
2945 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));