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-2006 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/x509.h>
162 #ifndef OPENSSL_NO_DH
163 #include <openssl/dh.h>
165 #include <openssl/bn.h>
166 #ifndef OPENSSL_NO_KRB5
167 #include <openssl/krb5_asn.h>
169 #include <openssl/md5.h>
171 static const SSL_METHOD *ssl3_get_server_method(int ver);
173 #ifndef OPENSSL_NO_ECDH
174 static int nid2curve_id(int nid);
177 static const SSL_METHOD *ssl3_get_server_method(int ver)
179 if (ver == SSL3_VERSION)
180 return(SSLv3_server_method());
185 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
187 ssl_undefined_function,
188 ssl3_get_server_method)
190 int ssl3_accept(SSL *s)
193 unsigned long l,Time=(unsigned long)time(NULL);
194 void (*cb)(const SSL *ssl,int type,int val)=NULL;
197 int new_state,state,skip=0;
199 RAND_add(&Time,sizeof(Time),0);
203 if (s->info_callback != NULL)
205 else if (s->ctx->info_callback != NULL)
206 cb=s->ctx->info_callback;
208 /* init things to blank */
210 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
214 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
224 case SSL_ST_RENEGOTIATE:
226 /* s->state=SSL_ST_ACCEPT; */
230 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
231 case SSL_ST_OK|SSL_ST_ACCEPT:
234 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
236 if ((s->version>>8) != 3)
238 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
241 s->type=SSL_ST_ACCEPT;
243 if (s->init_buf == NULL)
245 if ((buf=BUF_MEM_new()) == NULL)
250 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
258 if (!ssl3_setup_buffers(s))
266 if (s->state != SSL_ST_RENEGOTIATE)
268 /* Ok, we now need to push on a buffering BIO so that
269 * the output is sent in a way that TCP likes :-)
271 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
273 ssl3_init_finished_mac(s);
274 s->state=SSL3_ST_SR_CLNT_HELLO_A;
275 s->ctx->stats.sess_accept++;
279 /* s->state == SSL_ST_RENEGOTIATE,
280 * we will just send a HelloRequest */
281 s->ctx->stats.sess_accept_renegotiate++;
282 s->state=SSL3_ST_SW_HELLO_REQ_A;
286 case SSL3_ST_SW_HELLO_REQ_A:
287 case SSL3_ST_SW_HELLO_REQ_B:
290 ret=ssl3_send_hello_request(s);
291 if (ret <= 0) goto end;
292 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
293 s->state=SSL3_ST_SW_FLUSH;
296 ssl3_init_finished_mac(s);
299 case SSL3_ST_SW_HELLO_REQ_C:
303 case SSL3_ST_SR_CLNT_HELLO_A:
304 case SSL3_ST_SR_CLNT_HELLO_B:
305 case SSL3_ST_SR_CLNT_HELLO_C:
308 ret=ssl3_get_client_hello(s);
309 if (ret <= 0) goto end;
312 s->state=SSL3_ST_SW_SRVR_HELLO_A;
316 case SSL3_ST_SW_SRVR_HELLO_A:
317 case SSL3_ST_SW_SRVR_HELLO_B:
318 ret=ssl3_send_server_hello(s);
319 if (ret <= 0) goto end;
322 s->state=SSL3_ST_SW_CHANGE_A;
324 s->state=SSL3_ST_SW_CERT_A;
328 case SSL3_ST_SW_CERT_A:
329 case SSL3_ST_SW_CERT_B:
330 /* Check if it is anon DH or anon ECDH */
332 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)
333 && !(s->s3->tmp.new_cipher->algorithms & SSL_kPSK))
335 ret=ssl3_send_server_certificate(s);
336 if (ret <= 0) goto end;
340 s->state=SSL3_ST_SW_KEY_EXCH_A;
344 case SSL3_ST_SW_KEY_EXCH_A:
345 case SSL3_ST_SW_KEY_EXCH_B:
346 l=s->s3->tmp.new_cipher->algorithms;
348 /* clear this, it may get reset by
349 * send_server_key_exchange */
350 if ((s->options & SSL_OP_EPHEMERAL_RSA)
351 #ifndef OPENSSL_NO_KRB5
353 #endif /* OPENSSL_NO_KRB5 */
355 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
356 * even when forbidden by protocol specs
357 * (handshake may fail as clients are not required to
358 * be able to handle this) */
359 s->s3->tmp.use_rsa_tmp=1;
361 s->s3->tmp.use_rsa_tmp=0;
364 /* only send if a DH key exchange, fortezza or
365 * RSA but we have a sign only certificate
367 * PSK: may send PSK identity hints
369 * For ECC ciphersuites, we send a serverKeyExchange
370 * message only if the cipher suite is either
371 * ECDH-anon or ECDHE. In other cases, the
372 * server certificate contains the server's
373 * public key for key exchange.
375 if (s->s3->tmp.use_rsa_tmp
376 /* PSK: send ServerKeyExchange if PSK identity
377 * hint if provided */
378 #ifndef OPENSSL_NO_PSK
379 || ((l & SSL_kPSK) && s->ctx->psk_identity_hint)
382 || (l & (SSL_DH|SSL_kFZA))
384 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
385 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
386 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
392 ret=ssl3_send_server_key_exchange(s);
393 if (ret <= 0) goto end;
398 s->state=SSL3_ST_SW_CERT_REQ_A;
402 case SSL3_ST_SW_CERT_REQ_A:
403 case SSL3_ST_SW_CERT_REQ_B:
404 if (/* don't request cert unless asked for it: */
405 !(s->verify_mode & SSL_VERIFY_PEER) ||
406 /* if SSL_VERIFY_CLIENT_ONCE is set,
407 * don't request cert during re-negotiation: */
408 ((s->session->peer != NULL) &&
409 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
410 /* never request cert in anonymous ciphersuites
411 * (see section "Certificate request" in SSL 3 drafts
412 * and in RFC 2246): */
413 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
414 /* ... except when the application insists on verification
415 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
416 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
417 /* never request cert in Kerberos ciphersuites */
418 (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5)
419 /* With normal PSK Certificates and
420 * Certificate Requests are omitted */
421 || (s->s3->tmp.new_cipher->algorithms & SSL_kPSK))
423 /* no cert request */
425 s->s3->tmp.cert_request=0;
426 s->state=SSL3_ST_SW_SRVR_DONE_A;
430 s->s3->tmp.cert_request=1;
431 ret=ssl3_send_certificate_request(s);
432 if (ret <= 0) goto end;
433 #ifndef NETSCAPE_HANG_BUG
434 s->state=SSL3_ST_SW_SRVR_DONE_A;
436 s->state=SSL3_ST_SW_FLUSH;
437 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
443 case SSL3_ST_SW_SRVR_DONE_A:
444 case SSL3_ST_SW_SRVR_DONE_B:
445 ret=ssl3_send_server_done(s);
446 if (ret <= 0) goto end;
447 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
448 s->state=SSL3_ST_SW_FLUSH;
452 case SSL3_ST_SW_FLUSH:
453 /* number of bytes to be flushed */
454 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
457 s->rwstate=SSL_WRITING;
458 num1=BIO_flush(s->wbio);
459 if (num1 <= 0) { ret= -1; goto end; }
460 s->rwstate=SSL_NOTHING;
463 s->state=s->s3->tmp.next_state;
466 case SSL3_ST_SR_CERT_A:
467 case SSL3_ST_SR_CERT_B:
468 /* Check for second client hello (MS SGC) */
469 ret = ssl3_check_client_hello(s);
473 s->state = SSL3_ST_SR_CLNT_HELLO_C;
475 if (s->s3->tmp.cert_request)
477 ret=ssl3_get_client_certificate(s);
478 if (ret <= 0) goto end;
481 s->state=SSL3_ST_SR_KEY_EXCH_A;
485 case SSL3_ST_SR_KEY_EXCH_A:
486 case SSL3_ST_SR_KEY_EXCH_B:
487 ret=ssl3_get_client_key_exchange(s);
492 /* For the ECDH ciphersuites when
493 * the client sends its ECDH pub key in
494 * a certificate, the CertificateVerify
495 * message is not sent.
497 s->state=SSL3_ST_SR_FINISHED_A;
502 s->state=SSL3_ST_SR_CERT_VRFY_A;
505 /* We need to get hashes here so if there is
506 * a client cert, it can be verified
508 s->method->ssl3_enc->cert_verify_mac(s,
509 &(s->s3->finish_dgst1),
510 &(s->s3->tmp.cert_verify_md[0]));
511 s->method->ssl3_enc->cert_verify_mac(s,
512 &(s->s3->finish_dgst2),
513 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
517 case SSL3_ST_SR_CERT_VRFY_A:
518 case SSL3_ST_SR_CERT_VRFY_B:
520 /* we should decide if we expected this one */
521 ret=ssl3_get_cert_verify(s);
522 if (ret <= 0) goto end;
524 s->state=SSL3_ST_SR_FINISHED_A;
528 case SSL3_ST_SR_FINISHED_A:
529 case SSL3_ST_SR_FINISHED_B:
530 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
531 SSL3_ST_SR_FINISHED_B);
532 if (ret <= 0) goto end;
536 s->state=SSL3_ST_SW_CHANGE_A;
540 case SSL3_ST_SW_CHANGE_A:
541 case SSL3_ST_SW_CHANGE_B:
543 s->session->cipher=s->s3->tmp.new_cipher;
544 if (!s->method->ssl3_enc->setup_key_block(s))
545 { ret= -1; goto end; }
547 ret=ssl3_send_change_cipher_spec(s,
548 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
550 if (ret <= 0) goto end;
551 s->state=SSL3_ST_SW_FINISHED_A;
554 if (!s->method->ssl3_enc->change_cipher_state(s,
555 SSL3_CHANGE_CIPHER_SERVER_WRITE))
563 case SSL3_ST_SW_FINISHED_A:
564 case SSL3_ST_SW_FINISHED_B:
565 ret=ssl3_send_finished(s,
566 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
567 s->method->ssl3_enc->server_finished_label,
568 s->method->ssl3_enc->server_finished_label_len);
569 if (ret <= 0) goto end;
570 s->state=SSL3_ST_SW_FLUSH;
572 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
574 s->s3->tmp.next_state=SSL_ST_OK;
579 /* clean a few things up */
580 ssl3_cleanup_key_block(s);
582 BUF_MEM_free(s->init_buf);
585 /* remove buffering on output */
586 ssl_free_wbio_buffer(s);
590 if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
592 /* actually not necessarily a 'new' session unless
593 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
597 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
599 s->ctx->stats.sess_accept_good++;
601 s->handshake_func=ssl3_accept;
603 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
611 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
617 if (!s->s3->tmp.reuse_message && !skip)
621 if ((ret=BIO_flush(s->wbio)) <= 0)
626 if ((cb != NULL) && (s->state != state))
630 cb(s,SSL_CB_ACCEPT_LOOP,1);
637 /* BIO_flush(s->wbio); */
641 cb(s,SSL_CB_ACCEPT_EXIT,ret);
645 int ssl3_send_hello_request(SSL *s)
649 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
651 p=(unsigned char *)s->init_buf->data;
652 *(p++)=SSL3_MT_HELLO_REQUEST;
657 s->state=SSL3_ST_SW_HELLO_REQ_B;
658 /* number of bytes to write */
663 /* SSL3_ST_SW_HELLO_REQ_B */
664 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
667 int ssl3_check_client_hello(SSL *s)
672 /* this function is called when we really expect a Certificate message,
673 * so permit appropriate message length */
674 n=s->method->ssl_get_message(s,
680 if (!ok) return((int)n);
681 s->s3->tmp.reuse_message = 1;
682 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
684 /* Throw away what we have done so far in the current handshake,
685 * which will now be aborted. (A full SSL_clear would be too much.)
686 * I hope that tmp.dh is the only thing that may need to be cleared
687 * when a handshake is not completed ... */
688 #ifndef OPENSSL_NO_DH
689 if (s->s3->tmp.dh != NULL)
691 DH_free(s->s3->tmp.dh);
692 s->s3->tmp.dh = NULL;
700 int ssl3_get_client_hello(SSL *s)
702 int i,j,ok,al,ret= -1;
703 unsigned int cookie_len;
706 unsigned char *p,*d,*q;
708 #ifndef OPENSSL_NO_COMP
711 STACK_OF(SSL_CIPHER) *ciphers=NULL;
713 /* We do this so that we will respond with our native type.
714 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
715 * This down switching should be handled by a different method.
716 * If we are SSLv3, we will respond with SSLv3, even if prompted with
719 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
722 s->state=SSL3_ST_SR_CLNT_HELLO_B;
724 n=s->method->ssl_get_message(s,
725 SSL3_ST_SR_CLNT_HELLO_B,
726 SSL3_ST_SR_CLNT_HELLO_C,
727 SSL3_MT_CLIENT_HELLO,
728 SSL3_RT_MAX_PLAIN_LENGTH,
731 if (!ok) return((int)n);
732 d=p=(unsigned char *)s->init_msg;
734 /* use version from inside client hello, not from record header
735 * (may differ: see RFC 2246, Appendix E, second paragraph) */
736 s->client_version=(((int)p[0])<<8)|(int)p[1];
739 if (s->client_version < s->version)
741 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
742 if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
744 /* similar to ssl3_get_record, send alert using remote version number */
745 s->version = s->client_version;
747 al = SSL_AD_PROTOCOL_VERSION;
751 /* load the client random */
752 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
755 /* get the session-id */
759 /* Versions before 0.9.7 always allow session reuse during renegotiation
760 * (i.e. when s->new_session is true), option
761 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
762 * Maybe this optional behaviour should always have been the default,
763 * but we cannot safely change the default behaviour (or new applications
764 * might be written that become totally unsecure when compiled with
765 * an earlier library version)
767 if (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
769 if (!ssl_get_new_session(s,1))
774 i=ssl_get_prev_session(s,p,j);
776 { /* previous session */
783 if (!ssl_get_new_session(s,1))
790 if (SSL_version(s) == DTLS1_VERSION)
795 if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
796 s->d1->send_cookie == 0)
798 /* HelloVerifyMessage has already been sent */
799 if ( cookie_len != s->d1->cookie_len)
801 al = SSL_AD_HANDSHAKE_FAILURE;
802 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
808 * The ClientHello may contain a cookie even if the
809 * HelloVerify message has not been sent--make sure that it
810 * does not cause an overflow.
812 if ( cookie_len > sizeof(s->d1->rcvd_cookie))
815 al = SSL_AD_DECODE_ERROR;
816 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
820 /* verify the cookie if appropriate option is set. */
821 if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
824 memcpy(s->d1->rcvd_cookie, p, cookie_len);
826 if ( s->ctx->app_verify_cookie_cb != NULL)
828 if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
831 al=SSL_AD_HANDSHAKE_FAILURE;
832 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
833 SSL_R_COOKIE_MISMATCH);
836 /* else cookie verification succeeded */
838 else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie,
839 s->d1->cookie_len) != 0) /* default verification */
841 al=SSL_AD_HANDSHAKE_FAILURE;
842 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
843 SSL_R_COOKIE_MISMATCH);
852 if ((i == 0) && (j != 0))
854 /* we need a cipher if we are not resuming a session */
855 al=SSL_AD_ILLEGAL_PARAMETER;
856 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
861 /* not enough data */
862 al=SSL_AD_DECODE_ERROR;
863 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
866 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
873 /* If it is a hit, check that the cipher is in the list */
874 if ((s->hit) && (i > 0))
877 id=s->session->cipher->id;
880 printf("client sent %d ciphers\n",sk_num(ciphers));
882 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
884 c=sk_SSL_CIPHER_value(ciphers,i);
886 printf("client [%2d of %2d]:%s\n",
887 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
897 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
899 /* Very bad for multi-threading.... */
900 s->session->cipher=sk_SSL_CIPHER_value(ciphers, 0);
904 /* we need to have the cipher in the cipher
905 * list if we are asked to reuse it */
906 al=SSL_AD_ILLEGAL_PARAMETER;
907 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
917 /* not enough data */
918 al=SSL_AD_DECODE_ERROR;
919 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
925 if (p[j] == 0) break;
932 al=SSL_AD_DECODE_ERROR;
933 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
937 #ifndef OPENSSL_NO_TLSEXT
939 if (s->version > SSL3_VERSION)
941 if (!ssl_parse_clienthello_tlsext(s,&p,d,n, &al))
943 /* 'al' set by ssl_parse_clienthello_tlsext */
944 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PARSE_TLSEXT);
948 if (ssl_check_clienthello_tlsext(s) <= 0) {
949 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_CLIENTHELLO_TLSEXT);
954 /* Worst case, we will use the NULL compression, but if we have other
955 * options, we will now look for them. We have i-1 compression
956 * algorithms from the client, starting at q. */
957 s->s3->tmp.new_compression=NULL;
958 #ifndef OPENSSL_NO_COMP
959 if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods)
960 { /* See if we have a match */
963 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
966 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
979 s->s3->tmp.new_compression=comp;
985 /* Given s->session->ciphers and SSL_get_ciphers, we must
990 #ifdef OPENSSL_NO_COMP
991 s->session->compress_meth=0;
993 s->session->compress_meth=(comp == NULL)?0:comp->id;
995 if (s->session->ciphers != NULL)
996 sk_SSL_CIPHER_free(s->session->ciphers);
997 s->session->ciphers=ciphers;
1000 al=SSL_AD_ILLEGAL_PARAMETER;
1001 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
1005 c=ssl3_choose_cipher(s,s->session->ciphers,
1006 SSL_get_ciphers(s));
1010 al=SSL_AD_HANDSHAKE_FAILURE;
1011 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
1014 s->s3->tmp.new_cipher=c;
1018 /* Session-id reuse */
1019 #ifdef REUSE_CIPHER_BUG
1020 STACK_OF(SSL_CIPHER) *sk;
1021 SSL_CIPHER *nc=NULL;
1022 SSL_CIPHER *ec=NULL;
1024 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
1026 sk=s->session->ciphers;
1027 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1029 c=sk_SSL_CIPHER_value(sk,i);
1030 if (c->algorithms & SSL_eNULL)
1032 if (SSL_C_IS_EXPORT(c))
1036 s->s3->tmp.new_cipher=nc;
1037 else if (ec != NULL)
1038 s->s3->tmp.new_cipher=ec;
1040 s->s3->tmp.new_cipher=s->session->cipher;
1044 s->s3->tmp.new_cipher=s->session->cipher;
1047 /* we now have the following setup.
1049 * cipher_list - our prefered list of ciphers
1050 * ciphers - the clients prefered list of ciphers
1051 * compression - basically ignored right now
1052 * ssl version is set - sslv3
1053 * s->session - The ssl session has been setup.
1054 * s->hit - session reuse flag
1055 * s->tmp.new_cipher - the new cipher to use.
1062 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1065 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
1069 int ssl3_send_server_hello(SSL *s)
1072 unsigned char *p,*d;
1074 unsigned long l,Time;
1076 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
1078 buf=(unsigned char *)s->init_buf->data;
1079 p=s->s3->server_random;
1080 Time=(unsigned long)time(NULL); /* Time */
1082 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
1084 /* Do the message type and length last */
1087 *(p++)=s->version>>8;
1088 *(p++)=s->version&0xff;
1091 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
1092 p+=SSL3_RANDOM_SIZE;
1094 /* now in theory we have 3 options to sending back the
1095 * session id. If it is a re-use, we send back the
1096 * old session-id, if it is a new session, we send
1097 * back the new session-id or we send back a 0 length
1098 * session-id if we want it to be single use.
1099 * Currently I will not implement the '0' length session-id
1100 * 12-Jan-98 - I'll now support the '0' length stuff.
1102 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
1103 s->session->session_id_length=0;
1105 sl=s->session->session_id_length;
1106 if (sl > (int)sizeof(s->session->session_id))
1108 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1112 memcpy(p,s->session->session_id,sl);
1115 /* put the cipher */
1116 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
1119 /* put the compression method */
1120 #ifdef OPENSSL_NO_COMP
1123 if (s->s3->tmp.new_compression == NULL)
1126 *(p++)=s->s3->tmp.new_compression->id;
1128 #ifndef OPENSSL_NO_TLSEXT
1129 if (ssl_prepare_serverhello_tlsext(s) <= 0)
1131 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
1134 if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
1136 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
1144 *(d++)=SSL3_MT_SERVER_HELLO;
1147 s->state=SSL3_ST_CW_CLNT_HELLO_B;
1148 /* number of bytes to write */
1153 /* SSL3_ST_CW_CLNT_HELLO_B */
1154 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1157 int ssl3_send_server_done(SSL *s)
1161 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1163 p=(unsigned char *)s->init_buf->data;
1166 *(p++)=SSL3_MT_SERVER_DONE;
1171 s->state=SSL3_ST_SW_SRVR_DONE_B;
1172 /* number of bytes to write */
1177 /* SSL3_ST_CW_CLNT_HELLO_B */
1178 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1181 int ssl3_send_server_key_exchange(SSL *s)
1183 #ifndef OPENSSL_NO_RSA
1187 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1190 #ifndef OPENSSL_NO_DH
1193 #ifndef OPENSSL_NO_ECDH
1194 EC_KEY *ecdh=NULL, *ecdhp;
1195 unsigned char *encodedPoint = NULL;
1198 BN_CTX *bn_ctx = NULL;
1201 unsigned char *p,*d;
1211 EVP_MD_CTX_init(&md_ctx);
1212 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1214 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1219 r[0]=r[1]=r[2]=r[3]=NULL;
1221 #ifndef OPENSSL_NO_RSA
1222 if (type & SSL_kRSA)
1225 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1227 rsa=s->cert->rsa_tmp_cb(s,
1228 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1229 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1232 al=SSL_AD_HANDSHAKE_FAILURE;
1233 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1241 al=SSL_AD_HANDSHAKE_FAILURE;
1242 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1247 s->s3->tmp.use_rsa_tmp=1;
1251 #ifndef OPENSSL_NO_DH
1252 if (type & SSL_kEDH)
1255 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1256 dhp=s->cert->dh_tmp_cb(s,
1257 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1258 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1261 al=SSL_AD_HANDSHAKE_FAILURE;
1262 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1266 if (s->s3->tmp.dh != NULL)
1269 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1273 if ((dh=DHparams_dup(dhp)) == NULL)
1275 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1280 if ((dhp->pub_key == NULL ||
1281 dhp->priv_key == NULL ||
1282 (s->options & SSL_OP_SINGLE_DH_USE)))
1284 if(!DH_generate_key(dh))
1286 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1293 dh->pub_key=BN_dup(dhp->pub_key);
1294 dh->priv_key=BN_dup(dhp->priv_key);
1295 if ((dh->pub_key == NULL) ||
1296 (dh->priv_key == NULL))
1298 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1308 #ifndef OPENSSL_NO_ECDH
1309 if (type & SSL_kECDHE)
1311 const EC_GROUP *group;
1313 ecdhp=cert->ecdh_tmp;
1314 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
1316 ecdhp=s->cert->ecdh_tmp_cb(s,
1317 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1318 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1322 al=SSL_AD_HANDSHAKE_FAILURE;
1323 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
1327 if (s->s3->tmp.ecdh != NULL)
1329 EC_KEY_free(s->s3->tmp.ecdh);
1330 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1334 /* Duplicate the ECDH structure. */
1337 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1340 if (!EC_KEY_up_ref(ecdhp))
1342 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1347 s->s3->tmp.ecdh=ecdh;
1348 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1349 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1350 (s->options & SSL_OP_SINGLE_ECDH_USE))
1352 if(!EC_KEY_generate_key(ecdh))
1354 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1359 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1360 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1361 (EC_KEY_get0_private_key(ecdh) == NULL))
1363 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1367 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1368 (EC_GROUP_get_degree(group) > 163))
1370 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1374 /* XXX: For now, we only support ephemeral ECDH
1375 * keys over named (not generic) curves. For
1376 * supported named curves, curve_id is non-zero.
1379 nid2curve_id(EC_GROUP_get_curve_name(group)))
1382 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1386 /* Encode the public key.
1387 * First check the size of encoding and
1388 * allocate memory accordingly.
1390 encodedlen = EC_POINT_point2oct(group,
1391 EC_KEY_get0_public_key(ecdh),
1392 POINT_CONVERSION_UNCOMPRESSED,
1395 encodedPoint = (unsigned char *)
1396 OPENSSL_malloc(encodedlen*sizeof(unsigned char));
1397 bn_ctx = BN_CTX_new();
1398 if ((encodedPoint == NULL) || (bn_ctx == NULL))
1400 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1405 encodedlen = EC_POINT_point2oct(group,
1406 EC_KEY_get0_public_key(ecdh),
1407 POINT_CONVERSION_UNCOMPRESSED,
1408 encodedPoint, encodedlen, bn_ctx);
1410 if (encodedlen == 0)
1412 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1416 BN_CTX_free(bn_ctx); bn_ctx=NULL;
1418 /* XXX: For now, we only support named (not
1419 * generic) curves in ECDH ephemeral key exchanges.
1420 * In this situation, we need four additional bytes
1421 * to encode the entire ServerECDHParams
1426 /* We'll generate the serverKeyExchange message
1427 * explicitly so we can set these to NULLs
1435 #endif /* !OPENSSL_NO_ECDH */
1436 #ifndef OPENSSL_NO_PSK
1437 if (type & SSL_kPSK)
1439 /* reserve size for record length and PSK identity hint*/
1440 n+=2+strlen(s->ctx->psk_identity_hint);
1443 #endif /* !OPENSSL_NO_PSK */
1445 al=SSL_AD_HANDSHAKE_FAILURE;
1446 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1449 for (i=0; r[i] != NULL; i++)
1451 nr[i]=BN_num_bytes(r[i]);
1455 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)
1456 && !(s->s3->tmp.new_cipher->algorithms & SSL_kPSK))
1458 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1461 al=SSL_AD_DECODE_ERROR;
1464 kn=EVP_PKEY_size(pkey);
1472 if (!BUF_MEM_grow_clean(buf,n+4+kn))
1474 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1477 d=(unsigned char *)s->init_buf->data;
1480 for (i=0; r[i] != NULL; i++)
1487 #ifndef OPENSSL_NO_ECDH
1488 if (type & SSL_kECDHE)
1490 /* XXX: For now, we only support named (not generic) curves.
1491 * In this situation, the serverKeyExchange message has:
1492 * [1 byte CurveType], [2 byte CurveName]
1493 * [1 byte length of encoded point], followed by
1494 * the actual encoded point itself
1496 *p = NAMED_CURVE_TYPE;
1504 memcpy((unsigned char*)p,
1505 (unsigned char *)encodedPoint,
1507 OPENSSL_free(encodedPoint);
1512 #ifndef OPENSSL_NO_PSK
1513 if (type & SSL_kPSK)
1515 /* copy PSK identity hint */
1516 s2n(strlen(s->ctx->psk_identity_hint), p);
1517 strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
1518 p+=strlen(s->ctx->psk_identity_hint);
1525 /* n is the length of the params, they start at &(d[4])
1526 * and p points to the space at the end. */
1527 #ifndef OPENSSL_NO_RSA
1528 if (pkey->type == EVP_PKEY_RSA)
1532 for (num=2; num > 0; num--)
1534 EVP_DigestInit_ex(&md_ctx,(num == 2)
1535 ?s->ctx->md5:s->ctx->sha1, NULL);
1536 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1537 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1538 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1539 EVP_DigestFinal_ex(&md_ctx,q,
1540 (unsigned int *)&i);
1544 if (RSA_sign(NID_md5_sha1, md_buf, j,
1545 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1547 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1555 #if !defined(OPENSSL_NO_DSA)
1556 if (pkey->type == EVP_PKEY_DSA)
1559 EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
1560 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1561 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1562 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1563 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1564 (unsigned int *)&i,pkey))
1566 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1574 #if !defined(OPENSSL_NO_ECDSA)
1575 if (pkey->type == EVP_PKEY_EC)
1577 /* let's do ECDSA */
1578 EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
1579 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1580 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1581 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1582 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1583 (unsigned int *)&i,pkey))
1585 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
1594 /* Is this error check actually needed? */
1595 al=SSL_AD_HANDSHAKE_FAILURE;
1596 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1601 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1604 /* we should now have things packed up, so lets send
1610 s->state = SSL3_ST_SW_KEY_EXCH_B;
1611 EVP_MD_CTX_cleanup(&md_ctx);
1612 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1614 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1616 #ifndef OPENSSL_NO_ECDH
1617 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
1618 BN_CTX_free(bn_ctx);
1620 EVP_MD_CTX_cleanup(&md_ctx);
1624 int ssl3_send_certificate_request(SSL *s)
1626 unsigned char *p,*d;
1628 STACK_OF(X509_NAME) *sk=NULL;
1632 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1636 d=p=(unsigned char *)&(buf->data[4]);
1638 /* get the list of acceptable cert types */
1640 n=ssl3_get_req_cert_type(s,p);
1649 sk=SSL_get_client_CA_list(s);
1653 for (i=0; i<sk_X509_NAME_num(sk); i++)
1655 name=sk_X509_NAME_value(sk,i);
1656 j=i2d_X509_NAME(name,NULL);
1657 if (!BUF_MEM_grow_clean(buf,4+n+j+2))
1659 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1662 p=(unsigned char *)&(buf->data[4+n]);
1663 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1666 i2d_X509_NAME(name,&p);
1673 i2d_X509_NAME(name,&p);
1674 j-=2; s2n(j,d); j+=2;
1680 /* else no CA names */
1681 p=(unsigned char *)&(buf->data[4+off]);
1684 d=(unsigned char *)buf->data;
1685 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1688 /* we should now have things packed up, so lets send
1693 #ifdef NETSCAPE_HANG_BUG
1694 p=(unsigned char *)s->init_buf->data + s->init_num;
1697 *(p++)=SSL3_MT_SERVER_DONE;
1704 s->state = SSL3_ST_SW_CERT_REQ_B;
1707 /* SSL3_ST_SW_CERT_REQ_B */
1708 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1713 int ssl3_get_client_key_exchange(SSL *s)
1719 #ifndef OPENSSL_NO_RSA
1721 EVP_PKEY *pkey=NULL;
1723 #ifndef OPENSSL_NO_DH
1727 #ifndef OPENSSL_NO_KRB5
1729 #endif /* OPENSSL_NO_KRB5 */
1731 #ifndef OPENSSL_NO_ECDH
1732 EC_KEY *srvr_ecdh = NULL;
1733 EVP_PKEY *clnt_pub_pkey = NULL;
1734 EC_POINT *clnt_ecpoint = NULL;
1735 BN_CTX *bn_ctx = NULL;
1738 n=s->method->ssl_get_message(s,
1739 SSL3_ST_SR_KEY_EXCH_A,
1740 SSL3_ST_SR_KEY_EXCH_B,
1741 SSL3_MT_CLIENT_KEY_EXCHANGE,
1745 if (!ok) return((int)n);
1746 p=(unsigned char *)s->init_msg;
1748 l=s->s3->tmp.new_cipher->algorithms;
1750 #ifndef OPENSSL_NO_RSA
1753 /* FIX THIS UP EAY EAY EAY EAY */
1754 if (s->s3->tmp.use_rsa_tmp)
1756 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1757 rsa=s->cert->rsa_tmp;
1758 /* Don't do a callback because rsa_tmp should
1759 * be sent already */
1762 al=SSL_AD_HANDSHAKE_FAILURE;
1763 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1770 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1771 if ( (pkey == NULL) ||
1772 (pkey->type != EVP_PKEY_RSA) ||
1773 (pkey->pkey.rsa == NULL))
1775 al=SSL_AD_HANDSHAKE_FAILURE;
1776 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1783 if (s->version > SSL3_VERSION)
1788 if (!(s->options & SSL_OP_TLS_D5_BUG))
1790 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1800 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1804 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1806 al=SSL_AD_DECODE_ERROR;
1807 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
1810 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
1812 /* The premaster secret must contain the same version number as the
1813 * ClientHello to detect version rollback attacks (strangely, the
1814 * protocol does not offer such protection for DH ciphersuites).
1815 * However, buggy clients exist that send the negotiated protocol
1816 * version instead if the server does not support the requested
1818 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1819 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1820 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
1822 al=SSL_AD_DECODE_ERROR;
1823 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
1825 /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1826 * (http://eprint.iacr.org/2003/052/) exploits the version
1827 * number check as a "bad version oracle" -- an alert would
1828 * reveal that the plaintext corresponding to some ciphertext
1829 * made up by the adversary is properly formatted except
1830 * that the version number is wrong. To avoid such attacks,
1831 * we should treat this just like any other decryption error. */
1837 /* Some decryption failure -- use random value instead as countermeasure
1838 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1839 * (see RFC 2246, section 7.4.7.1). */
1841 i = SSL_MAX_MASTER_KEY_LENGTH;
1842 p[0] = s->client_version >> 8;
1843 p[1] = s->client_version & 0xff;
1844 if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_bytes, but we cannot work around a failure */
1848 s->session->master_key_length=
1849 s->method->ssl3_enc->generate_master_secret(s,
1850 s->session->master_key,
1852 OPENSSL_cleanse(p,i);
1856 #ifndef OPENSSL_NO_DH
1857 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1862 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1864 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1874 if (n == 0L) /* the parameters are in the cert */
1876 al=SSL_AD_HANDSHAKE_FAILURE;
1877 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1882 if (s->s3->tmp.dh == NULL)
1884 al=SSL_AD_HANDSHAKE_FAILURE;
1885 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1889 dh_srvr=s->s3->tmp.dh;
1892 pub=BN_bin2bn(p,i,NULL);
1895 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1899 i=DH_compute_key(p,pub,dh_srvr);
1903 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1907 DH_free(s->s3->tmp.dh);
1912 s->session->master_key_length=
1913 s->method->ssl3_enc->generate_master_secret(s,
1914 s->session->master_key,p,i);
1915 OPENSSL_cleanse(p,i);
1919 #ifndef OPENSSL_NO_KRB5
1922 krb5_error_code krb5rc;
1923 krb5_data enc_ticket;
1924 krb5_data authenticator;
1926 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1927 EVP_CIPHER_CTX ciph_ctx;
1928 EVP_CIPHER *enc = NULL;
1929 unsigned char iv[EVP_MAX_IV_LENGTH];
1930 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
1931 + EVP_MAX_BLOCK_LENGTH];
1933 krb5_timestamp authtime = 0;
1934 krb5_ticket_times ttimes;
1936 EVP_CIPHER_CTX_init(&ciph_ctx);
1938 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
1941 enc_ticket.length = i;
1943 if (n < enc_ticket.length + 6)
1945 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1946 SSL_R_DATA_LENGTH_TOO_LONG);
1950 enc_ticket.data = (char *)p;
1951 p+=enc_ticket.length;
1954 authenticator.length = i;
1956 if (n < enc_ticket.length + authenticator.length + 6)
1958 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1959 SSL_R_DATA_LENGTH_TOO_LONG);
1963 authenticator.data = (char *)p;
1964 p+=authenticator.length;
1968 enc_pms.data = (char *)p;
1971 /* Note that the length is checked again below,
1974 if(enc_pms.length > sizeof pms)
1976 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1977 SSL_R_DATA_LENGTH_TOO_LONG);
1981 if (n != (long)(enc_ticket.length + authenticator.length +
1982 enc_pms.length + 6))
1984 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1985 SSL_R_DATA_LENGTH_TOO_LONG);
1989 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
1993 printf("kssl_sget_tkt rtn %d [%d]\n",
1994 krb5rc, kssl_err.reason);
1996 printf("kssl_err text= %s\n", kssl_err.text);
1997 #endif /* KSSL_DEBUG */
1998 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2003 /* Note: no authenticator is not considered an error,
2004 ** but will return authtime == 0.
2006 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2007 &authtime, &kssl_err)) != 0)
2010 printf("kssl_check_authent rtn %d [%d]\n",
2011 krb5rc, kssl_err.reason);
2013 printf("kssl_err text= %s\n", kssl_err.text);
2014 #endif /* KSSL_DEBUG */
2015 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2020 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
2022 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2027 kssl_ctx_show(kssl_ctx);
2028 #endif /* KSSL_DEBUG */
2030 enc = kssl_map_enc(kssl_ctx->enctype);
2034 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2036 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
2038 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2039 SSL_R_DECRYPTION_FAILED);
2042 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
2043 (unsigned char *)enc_pms.data, enc_pms.length))
2045 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2046 SSL_R_DECRYPTION_FAILED);
2049 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
2051 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2052 SSL_R_DATA_LENGTH_TOO_LONG);
2055 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
2057 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2058 SSL_R_DECRYPTION_FAILED);
2062 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
2064 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2065 SSL_R_DATA_LENGTH_TOO_LONG);
2068 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2070 s->session->master_key_length=
2071 s->method->ssl3_enc->generate_master_secret(s,
2072 s->session->master_key, pms, outl);
2074 if (kssl_ctx->client_princ)
2076 int len = strlen(kssl_ctx->client_princ);
2077 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
2079 s->session->krb5_client_princ_len = len;
2080 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
2085 /* Was doing kssl_ctx_free() here,
2086 ** but it caused problems for apache.
2087 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
2088 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
2092 #endif /* OPENSSL_NO_KRB5 */
2094 #ifndef OPENSSL_NO_ECDH
2095 if ((l & SSL_kECDH) || (l & SSL_kECDHE))
2100 const EC_GROUP *group;
2101 const BIGNUM *priv_key;
2103 /* initialize structures for server's ECDH key pair */
2104 if ((srvr_ecdh = EC_KEY_new()) == NULL)
2106 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2107 ERR_R_MALLOC_FAILURE);
2111 /* Let's get server private key and group information */
2114 /* use the certificate */
2115 tkey = s->cert->key->privatekey->pkey.ec;
2119 /* use the ephermeral values we saved when
2120 * generating the ServerKeyExchange msg.
2122 tkey = s->s3->tmp.ecdh;
2125 group = EC_KEY_get0_group(tkey);
2126 priv_key = EC_KEY_get0_private_key(tkey);
2128 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2129 !EC_KEY_set_private_key(srvr_ecdh, priv_key))
2131 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2136 /* Let's get client's public key */
2137 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
2139 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2140 ERR_R_MALLOC_FAILURE);
2146 /* Client Publickey was in Client Certificate */
2150 al=SSL_AD_HANDSHAKE_FAILURE;
2151 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
2154 if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
2156 (clnt_pub_pkey->type != EVP_PKEY_EC))
2158 /* XXX: For now, we do not support client
2159 * authentication using ECDH certificates
2160 * so this branch (n == 0L) of the code is
2161 * never executed. When that support is
2162 * added, we ought to ensure the key
2163 * received in the certificate is
2164 * authorized for key agreement.
2165 * ECDH_compute_key implicitly checks that
2166 * the two ECDH shares are for the same
2169 al=SSL_AD_HANDSHAKE_FAILURE;
2170 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2171 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2175 if (EC_POINT_copy(clnt_ecpoint,
2176 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)
2178 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2182 ret = 2; /* Skip certificate verify processing */
2186 /* Get client's public key from encoded point
2187 * in the ClientKeyExchange message.
2189 if ((bn_ctx = BN_CTX_new()) == NULL)
2191 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2192 ERR_R_MALLOC_FAILURE);
2196 /* Get encoded point length */
2199 if (EC_POINT_oct2point(group,
2200 clnt_ecpoint, p, i, bn_ctx) == 0)
2202 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2206 /* p is pointing to somewhere in the buffer
2207 * currently, so set it to the start
2209 p=(unsigned char *)s->init_buf->data;
2212 /* Compute the shared pre-master secret */
2213 field_size = EC_GROUP_get_degree(group);
2214 if (field_size <= 0)
2216 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2220 i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
2223 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2228 EVP_PKEY_free(clnt_pub_pkey);
2229 EC_POINT_free(clnt_ecpoint);
2230 if (srvr_ecdh != NULL)
2231 EC_KEY_free(srvr_ecdh);
2232 BN_CTX_free(bn_ctx);
2234 /* Compute the master secret */
2235 s->session->master_key_length = s->method->ssl3_enc-> \
2236 generate_master_secret(s, s->session->master_key, p, i);
2238 OPENSSL_cleanse(p, i);
2243 #ifndef OPENSSL_NO_PSK
2246 unsigned char *t = NULL;
2247 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
2248 unsigned int pre_ms_len = 0, psk_len = 0;
2250 char tmp_id[PSK_MAX_IDENTITY_LEN+1];
2252 al=SSL_AD_HANDSHAKE_FAILURE;
2257 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2258 SSL_R_LENGTH_MISMATCH);
2261 if (i > PSK_MAX_IDENTITY_LEN)
2263 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2264 SSL_R_DATA_LENGTH_TOO_LONG);
2267 if (s->psk_server_callback == NULL)
2269 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2270 SSL_R_PSK_NO_SERVER_CB);
2274 /* Create guaranteed NULL-terminated identity
2275 * string for the callback */
2276 memcpy(tmp_id, p, i);
2277 memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
2278 psk_len = s->psk_server_callback(s, tmp_id,
2279 psk_or_pre_ms, sizeof(psk_or_pre_ms));
2280 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1);
2282 if (psk_len > PSK_MAX_PSK_LEN)
2284 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2285 ERR_R_INTERNAL_ERROR);
2288 else if (psk_len == 0)
2290 /* PSK related to the given identity not found */
2291 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2292 SSL_R_PSK_IDENTITY_NOT_FOUND);
2293 al=SSL_AD_UNKNOWN_PSK_IDENTITY;
2297 /* create PSK pre_master_secret */
2298 pre_ms_len=2+psk_len+2+psk_len;
2300 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
2302 memset(t, 0, psk_len);
2306 if (s->session->psk_identity != NULL)
2307 OPENSSL_free(s->session->psk_identity);
2308 s->session->psk_identity = BUF_strdup((char *)p);
2309 if (s->session->psk_identity == NULL)
2311 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2312 ERR_R_MALLOC_FAILURE);
2316 if (s->session->psk_identity_hint != NULL)
2317 OPENSSL_free(s->session->psk_identity_hint);
2318 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2319 if (s->ctx->psk_identity_hint != NULL &&
2320 s->session->psk_identity_hint == NULL)
2322 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2323 ERR_R_MALLOC_FAILURE);
2327 s->session->master_key_length=
2328 s->method->ssl3_enc->generate_master_secret(s,
2329 s->session->master_key, psk_or_pre_ms, pre_ms_len);
2332 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2339 al=SSL_AD_HANDSHAKE_FAILURE;
2340 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2341 SSL_R_UNKNOWN_CIPHER_TYPE);
2347 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2348 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
2351 #ifndef OPENSSL_NO_ECDH
2352 EVP_PKEY_free(clnt_pub_pkey);
2353 EC_POINT_free(clnt_ecpoint);
2354 if (srvr_ecdh != NULL)
2355 EC_KEY_free(srvr_ecdh);
2356 BN_CTX_free(bn_ctx);
2361 int ssl3_get_cert_verify(SSL *s)
2363 EVP_PKEY *pkey=NULL;
2370 n=s->method->ssl_get_message(s,
2371 SSL3_ST_SR_CERT_VRFY_A,
2372 SSL3_ST_SR_CERT_VRFY_B,
2377 if (!ok) return((int)n);
2379 if (s->session->peer != NULL)
2381 peer=s->session->peer;
2382 pkey=X509_get_pubkey(peer);
2383 type=X509_certificate_type(peer,pkey);
2391 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
2393 s->s3->tmp.reuse_message=1;
2394 if ((peer != NULL) && (type | EVP_PKT_SIGN))
2396 al=SSL_AD_UNEXPECTED_MESSAGE;
2397 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
2406 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
2407 al=SSL_AD_UNEXPECTED_MESSAGE;
2411 if (!(type & EVP_PKT_SIGN))
2413 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2414 al=SSL_AD_ILLEGAL_PARAMETER;
2418 if (s->s3->change_cipher_spec)
2420 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
2421 al=SSL_AD_UNEXPECTED_MESSAGE;
2425 /* we now have a signature that we need to verify */
2426 p=(unsigned char *)s->init_msg;
2431 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
2432 al=SSL_AD_DECODE_ERROR;
2436 j=EVP_PKEY_size(pkey);
2437 if ((i > j) || (n > j) || (n <= 0))
2439 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
2440 al=SSL_AD_DECODE_ERROR;
2444 #ifndef OPENSSL_NO_RSA
2445 if (pkey->type == EVP_PKEY_RSA)
2447 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2448 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
2452 al=SSL_AD_DECRYPT_ERROR;
2453 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
2458 al=SSL_AD_DECRYPT_ERROR;
2459 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
2465 #ifndef OPENSSL_NO_DSA
2466 if (pkey->type == EVP_PKEY_DSA)
2468 j=DSA_verify(pkey->save_type,
2469 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2470 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
2474 al=SSL_AD_DECRYPT_ERROR;
2475 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
2481 #ifndef OPENSSL_NO_ECDSA
2482 if (pkey->type == EVP_PKEY_EC)
2484 j=ECDSA_verify(pkey->save_type,
2485 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2486 SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
2490 al=SSL_AD_DECRYPT_ERROR;
2491 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2492 SSL_R_BAD_ECDSA_SIGNATURE);
2499 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
2500 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
2509 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2512 EVP_PKEY_free(pkey);
2516 int ssl3_get_client_certificate(SSL *s)
2518 int i,ok,al,ret= -1;
2520 unsigned long l,nc,llen,n;
2521 const unsigned char *p,*q;
2523 STACK_OF(X509) *sk=NULL;
2525 n=s->method->ssl_get_message(s,
2532 if (!ok) return((int)n);
2534 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
2536 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
2537 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
2539 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2540 al=SSL_AD_HANDSHAKE_FAILURE;
2543 /* If tls asked for a client cert, the client must return a 0 list */
2544 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
2546 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2547 al=SSL_AD_UNEXPECTED_MESSAGE;
2550 s->s3->tmp.reuse_message=1;
2554 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
2556 al=SSL_AD_UNEXPECTED_MESSAGE;
2557 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
2560 p=d=(unsigned char *)s->init_msg;
2562 if ((sk=sk_X509_new_null()) == NULL)
2564 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
2571 al=SSL_AD_DECODE_ERROR;
2572 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
2575 for (nc=0; nc<llen; )
2578 if ((l+nc+3) > llen)
2580 al=SSL_AD_DECODE_ERROR;
2581 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
2586 x=d2i_X509(NULL,&p,l);
2589 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
2594 al=SSL_AD_DECODE_ERROR;
2595 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
2598 if (!sk_X509_push(sk,x))
2600 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
2607 if (sk_X509_num(sk) <= 0)
2609 /* TLS does not mind 0 certs returned */
2610 if (s->version == SSL3_VERSION)
2612 al=SSL_AD_HANDSHAKE_FAILURE;
2613 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
2616 /* Fail for TLS only if we required a certificate */
2617 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2618 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
2620 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2621 al=SSL_AD_HANDSHAKE_FAILURE;
2627 i=ssl_verify_cert_chain(s,sk);
2630 al=ssl_verify_alarm_type(s->verify_result);
2631 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
2636 if (s->session->peer != NULL) /* This should not be needed */
2637 X509_free(s->session->peer);
2638 s->session->peer=sk_X509_shift(sk);
2639 s->session->verify_result = s->verify_result;
2641 /* With the current implementation, sess_cert will always be NULL
2642 * when we arrive here. */
2643 if (s->session->sess_cert == NULL)
2645 s->session->sess_cert = ssl_sess_cert_new();
2646 if (s->session->sess_cert == NULL)
2648 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2652 if (s->session->sess_cert->cert_chain != NULL)
2653 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2654 s->session->sess_cert->cert_chain=sk;
2655 /* Inconsistency alert: cert_chain does *not* include the
2656 * peer's own certificate, while we do include it in s3_clnt.c */
2664 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2667 if (x != NULL) X509_free(x);
2668 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
2672 int ssl3_send_server_certificate(SSL *s)
2677 if (s->state == SSL3_ST_SW_CERT_A)
2679 x=ssl_get_server_send_cert(s);
2681 /* VRS: allow null cert if auth == KRB5 */
2682 (s->s3->tmp.new_cipher->algorithms
2683 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
2684 != (SSL_aKRB5|SSL_kKRB5))
2686 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
2690 l=ssl3_output_cert_chain(s,x);
2691 s->state=SSL3_ST_SW_CERT_B;
2696 /* SSL3_ST_SW_CERT_B */
2697 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2701 #ifndef OPENSSL_NO_ECDH
2702 /* This is the complement of curve_id2nid in s3_clnt.c. */
2703 static int nid2curve_id(int nid)
2705 /* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001)
2706 * (no changes in draft-ietf-tls-ecc-03.txt [June 2003]) */
2708 case NID_sect163k1: /* sect163k1 (1) */
2710 case NID_sect163r1: /* sect163r1 (2) */
2712 case NID_sect163r2: /* sect163r2 (3) */
2714 case NID_sect193r1: /* sect193r1 (4) */
2716 case NID_sect193r2: /* sect193r2 (5) */
2718 case NID_sect233k1: /* sect233k1 (6) */
2720 case NID_sect233r1: /* sect233r1 (7) */
2722 case NID_sect239k1: /* sect239k1 (8) */
2724 case NID_sect283k1: /* sect283k1 (9) */
2726 case NID_sect283r1: /* sect283r1 (10) */
2728 case NID_sect409k1: /* sect409k1 (11) */
2730 case NID_sect409r1: /* sect409r1 (12) */
2732 case NID_sect571k1: /* sect571k1 (13) */
2734 case NID_sect571r1: /* sect571r1 (14) */
2736 case NID_secp160k1: /* secp160k1 (15) */
2738 case NID_secp160r1: /* secp160r1 (16) */
2740 case NID_secp160r2: /* secp160r2 (17) */
2742 case NID_secp192k1: /* secp192k1 (18) */
2744 case NID_X9_62_prime192v1: /* secp192r1 (19) */
2746 case NID_secp224k1: /* secp224k1 (20) */
2748 case NID_secp224r1: /* secp224r1 (21) */
2750 case NID_secp256k1: /* secp256k1 (22) */
2752 case NID_X9_62_prime256v1: /* secp256r1 (23) */
2754 case NID_secp384r1: /* secp384r1 (24) */
2756 case NID_secp521r1: /* secp521r1 (25) */