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_TLS_EXT);
948 if (ssl_check_tlsext(s,1) <= 0) {
949 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_CLIENTHELLO_TLS_EXT);
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 ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
1131 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
1139 *(d++)=SSL3_MT_SERVER_HELLO;
1142 s->state=SSL3_ST_CW_CLNT_HELLO_B;
1143 /* number of bytes to write */
1148 /* SSL3_ST_CW_CLNT_HELLO_B */
1149 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1152 int ssl3_send_server_done(SSL *s)
1156 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1158 p=(unsigned char *)s->init_buf->data;
1161 *(p++)=SSL3_MT_SERVER_DONE;
1166 s->state=SSL3_ST_SW_SRVR_DONE_B;
1167 /* number of bytes to write */
1172 /* SSL3_ST_CW_CLNT_HELLO_B */
1173 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1176 int ssl3_send_server_key_exchange(SSL *s)
1178 #ifndef OPENSSL_NO_RSA
1182 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1185 #ifndef OPENSSL_NO_DH
1188 #ifndef OPENSSL_NO_ECDH
1189 EC_KEY *ecdh=NULL, *ecdhp;
1190 unsigned char *encodedPoint = NULL;
1193 BN_CTX *bn_ctx = NULL;
1196 unsigned char *p,*d;
1206 EVP_MD_CTX_init(&md_ctx);
1207 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1209 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1214 r[0]=r[1]=r[2]=r[3]=NULL;
1216 #ifndef OPENSSL_NO_RSA
1217 if (type & SSL_kRSA)
1220 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1222 rsa=s->cert->rsa_tmp_cb(s,
1223 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1224 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1227 al=SSL_AD_HANDSHAKE_FAILURE;
1228 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1236 al=SSL_AD_HANDSHAKE_FAILURE;
1237 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1242 s->s3->tmp.use_rsa_tmp=1;
1246 #ifndef OPENSSL_NO_DH
1247 if (type & SSL_kEDH)
1250 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1251 dhp=s->cert->dh_tmp_cb(s,
1252 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1253 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1256 al=SSL_AD_HANDSHAKE_FAILURE;
1257 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1261 if (s->s3->tmp.dh != NULL)
1264 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1268 if ((dh=DHparams_dup(dhp)) == NULL)
1270 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1275 if ((dhp->pub_key == NULL ||
1276 dhp->priv_key == NULL ||
1277 (s->options & SSL_OP_SINGLE_DH_USE)))
1279 if(!DH_generate_key(dh))
1281 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1288 dh->pub_key=BN_dup(dhp->pub_key);
1289 dh->priv_key=BN_dup(dhp->priv_key);
1290 if ((dh->pub_key == NULL) ||
1291 (dh->priv_key == NULL))
1293 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1303 #ifndef OPENSSL_NO_ECDH
1304 if (type & SSL_kECDHE)
1306 const EC_GROUP *group;
1308 ecdhp=cert->ecdh_tmp;
1309 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
1311 ecdhp=s->cert->ecdh_tmp_cb(s,
1312 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1313 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1317 al=SSL_AD_HANDSHAKE_FAILURE;
1318 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
1322 if (s->s3->tmp.ecdh != NULL)
1324 EC_KEY_free(s->s3->tmp.ecdh);
1325 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1329 /* Duplicate the ECDH structure. */
1332 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1335 if (!EC_KEY_up_ref(ecdhp))
1337 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1342 s->s3->tmp.ecdh=ecdh;
1343 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1344 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1345 (s->options & SSL_OP_SINGLE_ECDH_USE))
1347 if(!EC_KEY_generate_key(ecdh))
1349 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1354 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1355 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1356 (EC_KEY_get0_private_key(ecdh) == NULL))
1358 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1362 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1363 (EC_GROUP_get_degree(group) > 163))
1365 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1369 /* XXX: For now, we only support ephemeral ECDH
1370 * keys over named (not generic) curves. For
1371 * supported named curves, curve_id is non-zero.
1374 nid2curve_id(EC_GROUP_get_curve_name(group)))
1377 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1381 /* Encode the public key.
1382 * First check the size of encoding and
1383 * allocate memory accordingly.
1385 encodedlen = EC_POINT_point2oct(group,
1386 EC_KEY_get0_public_key(ecdh),
1387 POINT_CONVERSION_UNCOMPRESSED,
1390 encodedPoint = (unsigned char *)
1391 OPENSSL_malloc(encodedlen*sizeof(unsigned char));
1392 bn_ctx = BN_CTX_new();
1393 if ((encodedPoint == NULL) || (bn_ctx == NULL))
1395 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1400 encodedlen = EC_POINT_point2oct(group,
1401 EC_KEY_get0_public_key(ecdh),
1402 POINT_CONVERSION_UNCOMPRESSED,
1403 encodedPoint, encodedlen, bn_ctx);
1405 if (encodedlen == 0)
1407 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1411 BN_CTX_free(bn_ctx); bn_ctx=NULL;
1413 /* XXX: For now, we only support named (not
1414 * generic) curves in ECDH ephemeral key exchanges.
1415 * In this situation, we need four additional bytes
1416 * to encode the entire ServerECDHParams
1421 /* We'll generate the serverKeyExchange message
1422 * explicitly so we can set these to NULLs
1430 #endif /* !OPENSSL_NO_ECDH */
1431 #ifndef OPENSSL_NO_PSK
1432 if (type & SSL_kPSK)
1434 /* reserve size for record length and PSK identity hint*/
1435 n+=2+strlen(s->ctx->psk_identity_hint);
1438 #endif /* !OPENSSL_NO_PSK */
1440 al=SSL_AD_HANDSHAKE_FAILURE;
1441 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1444 for (i=0; r[i] != NULL; i++)
1446 nr[i]=BN_num_bytes(r[i]);
1450 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)
1451 && !(s->s3->tmp.new_cipher->algorithms & SSL_kPSK))
1453 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1456 al=SSL_AD_DECODE_ERROR;
1459 kn=EVP_PKEY_size(pkey);
1467 if (!BUF_MEM_grow_clean(buf,n+4+kn))
1469 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1472 d=(unsigned char *)s->init_buf->data;
1475 for (i=0; r[i] != NULL; i++)
1482 #ifndef OPENSSL_NO_ECDH
1483 if (type & SSL_kECDHE)
1485 /* XXX: For now, we only support named (not generic) curves.
1486 * In this situation, the serverKeyExchange message has:
1487 * [1 byte CurveType], [2 byte CurveName]
1488 * [1 byte length of encoded point], followed by
1489 * the actual encoded point itself
1491 *p = NAMED_CURVE_TYPE;
1499 memcpy((unsigned char*)p,
1500 (unsigned char *)encodedPoint,
1502 OPENSSL_free(encodedPoint);
1507 #ifndef OPENSSL_NO_PSK
1508 if (type & SSL_kPSK)
1510 /* copy PSK identity hint */
1511 s2n(strlen(s->ctx->psk_identity_hint), p);
1512 strncpy(p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
1513 p+=strlen(s->ctx->psk_identity_hint);
1520 /* n is the length of the params, they start at &(d[4])
1521 * and p points to the space at the end. */
1522 #ifndef OPENSSL_NO_RSA
1523 if (pkey->type == EVP_PKEY_RSA)
1527 for (num=2; num > 0; num--)
1529 EVP_DigestInit_ex(&md_ctx,(num == 2)
1530 ?s->ctx->md5:s->ctx->sha1, NULL);
1531 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1532 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1533 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1534 EVP_DigestFinal_ex(&md_ctx,q,
1535 (unsigned int *)&i);
1539 if (RSA_sign(NID_md5_sha1, md_buf, j,
1540 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1542 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1550 #if !defined(OPENSSL_NO_DSA)
1551 if (pkey->type == EVP_PKEY_DSA)
1554 EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
1555 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1556 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1557 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1558 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1559 (unsigned int *)&i,pkey))
1561 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1569 #if !defined(OPENSSL_NO_ECDSA)
1570 if (pkey->type == EVP_PKEY_EC)
1572 /* let's do ECDSA */
1573 EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
1574 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1575 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1576 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1577 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1578 (unsigned int *)&i,pkey))
1580 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
1589 /* Is this error check actually needed? */
1590 al=SSL_AD_HANDSHAKE_FAILURE;
1591 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1596 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1599 /* we should now have things packed up, so lets send
1605 s->state = SSL3_ST_SW_KEY_EXCH_B;
1606 EVP_MD_CTX_cleanup(&md_ctx);
1607 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1609 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1611 #ifndef OPENSSL_NO_ECDH
1612 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
1613 BN_CTX_free(bn_ctx);
1615 EVP_MD_CTX_cleanup(&md_ctx);
1619 int ssl3_send_certificate_request(SSL *s)
1621 unsigned char *p,*d;
1623 STACK_OF(X509_NAME) *sk=NULL;
1627 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1631 d=p=(unsigned char *)&(buf->data[4]);
1633 /* get the list of acceptable cert types */
1635 n=ssl3_get_req_cert_type(s,p);
1644 sk=SSL_get_client_CA_list(s);
1648 for (i=0; i<sk_X509_NAME_num(sk); i++)
1650 name=sk_X509_NAME_value(sk,i);
1651 j=i2d_X509_NAME(name,NULL);
1652 if (!BUF_MEM_grow_clean(buf,4+n+j+2))
1654 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1657 p=(unsigned char *)&(buf->data[4+n]);
1658 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1661 i2d_X509_NAME(name,&p);
1668 i2d_X509_NAME(name,&p);
1669 j-=2; s2n(j,d); j+=2;
1675 /* else no CA names */
1676 p=(unsigned char *)&(buf->data[4+off]);
1679 d=(unsigned char *)buf->data;
1680 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1683 /* we should now have things packed up, so lets send
1688 #ifdef NETSCAPE_HANG_BUG
1689 p=(unsigned char *)s->init_buf->data + s->init_num;
1692 *(p++)=SSL3_MT_SERVER_DONE;
1699 s->state = SSL3_ST_SW_CERT_REQ_B;
1702 /* SSL3_ST_SW_CERT_REQ_B */
1703 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1708 int ssl3_get_client_key_exchange(SSL *s)
1714 #ifndef OPENSSL_NO_RSA
1716 EVP_PKEY *pkey=NULL;
1718 #ifndef OPENSSL_NO_DH
1722 #ifndef OPENSSL_NO_KRB5
1724 #endif /* OPENSSL_NO_KRB5 */
1726 #ifndef OPENSSL_NO_ECDH
1727 EC_KEY *srvr_ecdh = NULL;
1728 EVP_PKEY *clnt_pub_pkey = NULL;
1729 EC_POINT *clnt_ecpoint = NULL;
1730 BN_CTX *bn_ctx = NULL;
1733 n=s->method->ssl_get_message(s,
1734 SSL3_ST_SR_KEY_EXCH_A,
1735 SSL3_ST_SR_KEY_EXCH_B,
1736 SSL3_MT_CLIENT_KEY_EXCHANGE,
1740 if (!ok) return((int)n);
1741 p=(unsigned char *)s->init_msg;
1743 l=s->s3->tmp.new_cipher->algorithms;
1745 #ifndef OPENSSL_NO_RSA
1748 /* FIX THIS UP EAY EAY EAY EAY */
1749 if (s->s3->tmp.use_rsa_tmp)
1751 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1752 rsa=s->cert->rsa_tmp;
1753 /* Don't do a callback because rsa_tmp should
1754 * be sent already */
1757 al=SSL_AD_HANDSHAKE_FAILURE;
1758 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1765 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1766 if ( (pkey == NULL) ||
1767 (pkey->type != EVP_PKEY_RSA) ||
1768 (pkey->pkey.rsa == NULL))
1770 al=SSL_AD_HANDSHAKE_FAILURE;
1771 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1778 if (s->version > SSL3_VERSION)
1783 if (!(s->options & SSL_OP_TLS_D5_BUG))
1785 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1795 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1799 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1801 al=SSL_AD_DECODE_ERROR;
1802 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
1805 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
1807 /* The premaster secret must contain the same version number as the
1808 * ClientHello to detect version rollback attacks (strangely, the
1809 * protocol does not offer such protection for DH ciphersuites).
1810 * However, buggy clients exist that send the negotiated protocol
1811 * version instead if the server does not support the requested
1813 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1814 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1815 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
1817 al=SSL_AD_DECODE_ERROR;
1818 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
1820 /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1821 * (http://eprint.iacr.org/2003/052/) exploits the version
1822 * number check as a "bad version oracle" -- an alert would
1823 * reveal that the plaintext corresponding to some ciphertext
1824 * made up by the adversary is properly formatted except
1825 * that the version number is wrong. To avoid such attacks,
1826 * we should treat this just like any other decryption error. */
1832 /* Some decryption failure -- use random value instead as countermeasure
1833 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1834 * (see RFC 2246, section 7.4.7.1). */
1836 i = SSL_MAX_MASTER_KEY_LENGTH;
1837 p[0] = s->client_version >> 8;
1838 p[1] = s->client_version & 0xff;
1839 if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_bytes, but we cannot work around a failure */
1843 s->session->master_key_length=
1844 s->method->ssl3_enc->generate_master_secret(s,
1845 s->session->master_key,
1847 OPENSSL_cleanse(p,i);
1851 #ifndef OPENSSL_NO_DH
1852 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1857 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1859 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1869 if (n == 0L) /* the parameters are in the cert */
1871 al=SSL_AD_HANDSHAKE_FAILURE;
1872 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1877 if (s->s3->tmp.dh == NULL)
1879 al=SSL_AD_HANDSHAKE_FAILURE;
1880 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1884 dh_srvr=s->s3->tmp.dh;
1887 pub=BN_bin2bn(p,i,NULL);
1890 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1894 i=DH_compute_key(p,pub,dh_srvr);
1898 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1902 DH_free(s->s3->tmp.dh);
1907 s->session->master_key_length=
1908 s->method->ssl3_enc->generate_master_secret(s,
1909 s->session->master_key,p,i);
1910 OPENSSL_cleanse(p,i);
1914 #ifndef OPENSSL_NO_KRB5
1917 krb5_error_code krb5rc;
1918 krb5_data enc_ticket;
1919 krb5_data authenticator;
1921 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1922 EVP_CIPHER_CTX ciph_ctx;
1923 EVP_CIPHER *enc = NULL;
1924 unsigned char iv[EVP_MAX_IV_LENGTH];
1925 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
1926 + EVP_MAX_BLOCK_LENGTH];
1928 krb5_timestamp authtime = 0;
1929 krb5_ticket_times ttimes;
1931 EVP_CIPHER_CTX_init(&ciph_ctx);
1933 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
1936 enc_ticket.length = i;
1938 if (n < enc_ticket.length + 6)
1940 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1941 SSL_R_DATA_LENGTH_TOO_LONG);
1945 enc_ticket.data = (char *)p;
1946 p+=enc_ticket.length;
1949 authenticator.length = i;
1951 if (n < enc_ticket.length + authenticator.length + 6)
1953 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1954 SSL_R_DATA_LENGTH_TOO_LONG);
1958 authenticator.data = (char *)p;
1959 p+=authenticator.length;
1963 enc_pms.data = (char *)p;
1966 /* Note that the length is checked again below,
1969 if(enc_pms.length > sizeof pms)
1971 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1972 SSL_R_DATA_LENGTH_TOO_LONG);
1976 if (n != (long)(enc_ticket.length + authenticator.length +
1977 enc_pms.length + 6))
1979 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1980 SSL_R_DATA_LENGTH_TOO_LONG);
1984 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
1988 printf("kssl_sget_tkt rtn %d [%d]\n",
1989 krb5rc, kssl_err.reason);
1991 printf("kssl_err text= %s\n", kssl_err.text);
1992 #endif /* KSSL_DEBUG */
1993 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1998 /* Note: no authenticator is not considered an error,
1999 ** but will return authtime == 0.
2001 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2002 &authtime, &kssl_err)) != 0)
2005 printf("kssl_check_authent rtn %d [%d]\n",
2006 krb5rc, kssl_err.reason);
2008 printf("kssl_err text= %s\n", kssl_err.text);
2009 #endif /* KSSL_DEBUG */
2010 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2015 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
2017 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2022 kssl_ctx_show(kssl_ctx);
2023 #endif /* KSSL_DEBUG */
2025 enc = kssl_map_enc(kssl_ctx->enctype);
2029 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2031 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
2033 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2034 SSL_R_DECRYPTION_FAILED);
2037 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
2038 (unsigned char *)enc_pms.data, enc_pms.length))
2040 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2041 SSL_R_DECRYPTION_FAILED);
2044 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
2046 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2047 SSL_R_DATA_LENGTH_TOO_LONG);
2050 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
2052 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2053 SSL_R_DECRYPTION_FAILED);
2057 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
2059 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2060 SSL_R_DATA_LENGTH_TOO_LONG);
2063 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2065 s->session->master_key_length=
2066 s->method->ssl3_enc->generate_master_secret(s,
2067 s->session->master_key, pms, outl);
2069 if (kssl_ctx->client_princ)
2071 int len = strlen(kssl_ctx->client_princ);
2072 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
2074 s->session->krb5_client_princ_len = len;
2075 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
2080 /* Was doing kssl_ctx_free() here,
2081 ** but it caused problems for apache.
2082 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
2083 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
2087 #endif /* OPENSSL_NO_KRB5 */
2089 #ifndef OPENSSL_NO_ECDH
2090 if ((l & SSL_kECDH) || (l & SSL_kECDHE))
2095 const EC_GROUP *group;
2096 const BIGNUM *priv_key;
2098 /* initialize structures for server's ECDH key pair */
2099 if ((srvr_ecdh = EC_KEY_new()) == NULL)
2101 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2102 ERR_R_MALLOC_FAILURE);
2106 /* Let's get server private key and group information */
2109 /* use the certificate */
2110 tkey = s->cert->key->privatekey->pkey.ec;
2114 /* use the ephermeral values we saved when
2115 * generating the ServerKeyExchange msg.
2117 tkey = s->s3->tmp.ecdh;
2120 group = EC_KEY_get0_group(tkey);
2121 priv_key = EC_KEY_get0_private_key(tkey);
2123 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2124 !EC_KEY_set_private_key(srvr_ecdh, priv_key))
2126 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2131 /* Let's get client's public key */
2132 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
2134 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2135 ERR_R_MALLOC_FAILURE);
2141 /* Client Publickey was in Client Certificate */
2145 al=SSL_AD_HANDSHAKE_FAILURE;
2146 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
2149 if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
2151 (clnt_pub_pkey->type != EVP_PKEY_EC))
2153 /* XXX: For now, we do not support client
2154 * authentication using ECDH certificates
2155 * so this branch (n == 0L) of the code is
2156 * never executed. When that support is
2157 * added, we ought to ensure the key
2158 * received in the certificate is
2159 * authorized for key agreement.
2160 * ECDH_compute_key implicitly checks that
2161 * the two ECDH shares are for the same
2164 al=SSL_AD_HANDSHAKE_FAILURE;
2165 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2166 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2170 if (EC_POINT_copy(clnt_ecpoint,
2171 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)
2173 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2177 ret = 2; /* Skip certificate verify processing */
2181 /* Get client's public key from encoded point
2182 * in the ClientKeyExchange message.
2184 if ((bn_ctx = BN_CTX_new()) == NULL)
2186 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2187 ERR_R_MALLOC_FAILURE);
2191 /* Get encoded point length */
2194 if (EC_POINT_oct2point(group,
2195 clnt_ecpoint, p, i, bn_ctx) == 0)
2197 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2201 /* p is pointing to somewhere in the buffer
2202 * currently, so set it to the start
2204 p=(unsigned char *)s->init_buf->data;
2207 /* Compute the shared pre-master secret */
2208 field_size = EC_GROUP_get_degree(group);
2209 if (field_size <= 0)
2211 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2215 i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
2218 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2223 EVP_PKEY_free(clnt_pub_pkey);
2224 EC_POINT_free(clnt_ecpoint);
2225 if (srvr_ecdh != NULL)
2226 EC_KEY_free(srvr_ecdh);
2227 BN_CTX_free(bn_ctx);
2229 /* Compute the master secret */
2230 s->session->master_key_length = s->method->ssl3_enc-> \
2231 generate_master_secret(s, s->session->master_key, p, i);
2233 OPENSSL_cleanse(p, i);
2238 #ifndef OPENSSL_NO_PSK
2241 unsigned char *t = NULL;
2242 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
2243 unsigned int pre_ms_len = 0, psk_len = 0;
2245 char tmp_id[PSK_MAX_IDENTITY_LEN+1];
2247 al=SSL_AD_HANDSHAKE_FAILURE;
2252 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2253 SSL_R_LENGTH_MISMATCH);
2256 if (i > PSK_MAX_IDENTITY_LEN)
2258 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2259 SSL_R_DATA_LENGTH_TOO_LONG);
2262 if (s->psk_server_callback == NULL)
2264 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2265 SSL_R_PSK_NO_SERVER_CB);
2269 /* Create guaranteed NULL-terminated identity
2270 * string for the callback */
2271 memcpy(tmp_id, p, i);
2272 memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
2273 psk_len = s->psk_server_callback(s, tmp_id,
2274 psk_or_pre_ms, sizeof(psk_or_pre_ms));
2275 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1);
2277 if (psk_len > PSK_MAX_PSK_LEN)
2279 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2280 ERR_R_INTERNAL_ERROR);
2283 else if (psk_len == 0)
2285 /* PSK related to the given identity not found */
2286 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2287 SSL_R_PSK_IDENTITY_NOT_FOUND);
2288 al=SSL_AD_UNKNOWN_PSK_IDENTITY;
2292 /* create PSK pre_master_secret */
2293 pre_ms_len=2+psk_len+2+psk_len;
2295 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
2297 memset(t, 0, psk_len);
2301 if (s->session->psk_identity != NULL)
2302 OPENSSL_free(s->session->psk_identity);
2303 s->session->psk_identity = BUF_strdup(p);
2304 if (s->session->psk_identity == NULL)
2306 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2307 ERR_R_MALLOC_FAILURE);
2311 if (s->session->psk_identity_hint != NULL)
2312 OPENSSL_free(s->session->psk_identity_hint);
2313 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2314 if (s->ctx->psk_identity_hint != NULL &&
2315 s->session->psk_identity_hint == NULL)
2317 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2318 ERR_R_MALLOC_FAILURE);
2322 s->session->master_key_length=
2323 s->method->ssl3_enc->generate_master_secret(s,
2324 s->session->master_key, psk_or_pre_ms, pre_ms_len);
2327 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2334 al=SSL_AD_HANDSHAKE_FAILURE;
2335 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2336 SSL_R_UNKNOWN_CIPHER_TYPE);
2342 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2343 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
2346 #ifndef OPENSSL_NO_ECDH
2347 EVP_PKEY_free(clnt_pub_pkey);
2348 EC_POINT_free(clnt_ecpoint);
2349 if (srvr_ecdh != NULL)
2350 EC_KEY_free(srvr_ecdh);
2351 BN_CTX_free(bn_ctx);
2356 int ssl3_get_cert_verify(SSL *s)
2358 EVP_PKEY *pkey=NULL;
2365 n=s->method->ssl_get_message(s,
2366 SSL3_ST_SR_CERT_VRFY_A,
2367 SSL3_ST_SR_CERT_VRFY_B,
2372 if (!ok) return((int)n);
2374 if (s->session->peer != NULL)
2376 peer=s->session->peer;
2377 pkey=X509_get_pubkey(peer);
2378 type=X509_certificate_type(peer,pkey);
2386 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
2388 s->s3->tmp.reuse_message=1;
2389 if ((peer != NULL) && (type | EVP_PKT_SIGN))
2391 al=SSL_AD_UNEXPECTED_MESSAGE;
2392 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
2401 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
2402 al=SSL_AD_UNEXPECTED_MESSAGE;
2406 if (!(type & EVP_PKT_SIGN))
2408 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2409 al=SSL_AD_ILLEGAL_PARAMETER;
2413 if (s->s3->change_cipher_spec)
2415 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
2416 al=SSL_AD_UNEXPECTED_MESSAGE;
2420 /* we now have a signature that we need to verify */
2421 p=(unsigned char *)s->init_msg;
2426 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
2427 al=SSL_AD_DECODE_ERROR;
2431 j=EVP_PKEY_size(pkey);
2432 if ((i > j) || (n > j) || (n <= 0))
2434 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
2435 al=SSL_AD_DECODE_ERROR;
2439 #ifndef OPENSSL_NO_RSA
2440 if (pkey->type == EVP_PKEY_RSA)
2442 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2443 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
2447 al=SSL_AD_DECRYPT_ERROR;
2448 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
2453 al=SSL_AD_DECRYPT_ERROR;
2454 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
2460 #ifndef OPENSSL_NO_DSA
2461 if (pkey->type == EVP_PKEY_DSA)
2463 j=DSA_verify(pkey->save_type,
2464 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2465 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
2469 al=SSL_AD_DECRYPT_ERROR;
2470 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
2476 #ifndef OPENSSL_NO_ECDSA
2477 if (pkey->type == EVP_PKEY_EC)
2479 j=ECDSA_verify(pkey->save_type,
2480 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2481 SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
2485 al=SSL_AD_DECRYPT_ERROR;
2486 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2487 SSL_R_BAD_ECDSA_SIGNATURE);
2494 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
2495 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
2504 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2507 EVP_PKEY_free(pkey);
2511 int ssl3_get_client_certificate(SSL *s)
2513 int i,ok,al,ret= -1;
2515 unsigned long l,nc,llen,n;
2516 const unsigned char *p,*q;
2518 STACK_OF(X509) *sk=NULL;
2520 n=s->method->ssl_get_message(s,
2527 if (!ok) return((int)n);
2529 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
2531 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
2532 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
2534 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2535 al=SSL_AD_HANDSHAKE_FAILURE;
2538 /* If tls asked for a client cert, the client must return a 0 list */
2539 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
2541 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2542 al=SSL_AD_UNEXPECTED_MESSAGE;
2545 s->s3->tmp.reuse_message=1;
2549 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
2551 al=SSL_AD_UNEXPECTED_MESSAGE;
2552 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
2555 p=d=(unsigned char *)s->init_msg;
2557 if ((sk=sk_X509_new_null()) == NULL)
2559 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
2566 al=SSL_AD_DECODE_ERROR;
2567 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
2570 for (nc=0; nc<llen; )
2573 if ((l+nc+3) > llen)
2575 al=SSL_AD_DECODE_ERROR;
2576 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
2581 x=d2i_X509(NULL,&p,l);
2584 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
2589 al=SSL_AD_DECODE_ERROR;
2590 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
2593 if (!sk_X509_push(sk,x))
2595 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
2602 if (sk_X509_num(sk) <= 0)
2604 /* TLS does not mind 0 certs returned */
2605 if (s->version == SSL3_VERSION)
2607 al=SSL_AD_HANDSHAKE_FAILURE;
2608 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
2611 /* Fail for TLS only if we required a certificate */
2612 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2613 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
2615 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2616 al=SSL_AD_HANDSHAKE_FAILURE;
2622 i=ssl_verify_cert_chain(s,sk);
2625 al=ssl_verify_alarm_type(s->verify_result);
2626 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
2631 if (s->session->peer != NULL) /* This should not be needed */
2632 X509_free(s->session->peer);
2633 s->session->peer=sk_X509_shift(sk);
2634 s->session->verify_result = s->verify_result;
2636 /* With the current implementation, sess_cert will always be NULL
2637 * when we arrive here. */
2638 if (s->session->sess_cert == NULL)
2640 s->session->sess_cert = ssl_sess_cert_new();
2641 if (s->session->sess_cert == NULL)
2643 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2647 if (s->session->sess_cert->cert_chain != NULL)
2648 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2649 s->session->sess_cert->cert_chain=sk;
2650 /* Inconsistency alert: cert_chain does *not* include the
2651 * peer's own certificate, while we do include it in s3_clnt.c */
2659 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2662 if (x != NULL) X509_free(x);
2663 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
2667 int ssl3_send_server_certificate(SSL *s)
2672 if (s->state == SSL3_ST_SW_CERT_A)
2674 x=ssl_get_server_send_cert(s);
2676 /* VRS: allow null cert if auth == KRB5 */
2677 (s->s3->tmp.new_cipher->algorithms
2678 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
2679 != (SSL_aKRB5|SSL_kKRB5))
2681 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
2685 l=ssl3_output_cert_chain(s,x);
2686 s->state=SSL3_ST_SW_CERT_B;
2691 /* SSL3_ST_SW_CERT_B */
2692 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2696 #ifndef OPENSSL_NO_ECDH
2697 /* This is the complement of curve_id2nid in s3_clnt.c. */
2698 static int nid2curve_id(int nid)
2700 /* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001)
2701 * (no changes in draft-ietf-tls-ecc-03.txt [June 2003]) */
2703 case NID_sect163k1: /* sect163k1 (1) */
2705 case NID_sect163r1: /* sect163r1 (2) */
2707 case NID_sect163r2: /* sect163r2 (3) */
2709 case NID_sect193r1: /* sect193r1 (4) */
2711 case NID_sect193r2: /* sect193r2 (5) */
2713 case NID_sect233k1: /* sect233k1 (6) */
2715 case NID_sect233r1: /* sect233r1 (7) */
2717 case NID_sect239k1: /* sect239k1 (8) */
2719 case NID_sect283k1: /* sect283k1 (9) */
2721 case NID_sect283r1: /* sect283r1 (10) */
2723 case NID_sect409k1: /* sect409k1 (11) */
2725 case NID_sect409r1: /* sect409r1 (12) */
2727 case NID_sect571k1: /* sect571k1 (13) */
2729 case NID_sect571r1: /* sect571r1 (14) */
2731 case NID_secp160k1: /* secp160k1 (15) */
2733 case NID_secp160r1: /* secp160r1 (16) */
2735 case NID_secp160r2: /* secp160r2 (17) */
2737 case NID_secp192k1: /* secp192k1 (18) */
2739 case NID_X9_62_prime192v1: /* secp192r1 (19) */
2741 case NID_secp224k1: /* secp224k1 (20) */
2743 case NID_secp224r1: /* secp224r1 (21) */
2745 case NID_secp256k1: /* secp256k1 (22) */
2747 case NID_X9_62_prime256v1: /* secp256r1 (23) */
2749 case NID_secp384r1: /* secp384r1 (24) */
2751 case NID_secp521r1: /* secp521r1 (25) */