2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
152 #include "ssl_locl.h"
153 #include "kssl_lcl.h"
154 #include <openssl/buffer.h>
155 #include <openssl/rand.h>
156 #include <openssl/objects.h>
157 #include <openssl/evp.h>
158 #include <openssl/md5.h>
159 #ifndef OPENSSL_NO_DH
160 #include <openssl/dh.h>
162 #include <openssl/bn.h>
164 static const SSL_METHOD *ssl3_get_client_method(int ver);
165 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
166 #ifndef OPENSSL_NO_TLSEXT
167 static int ssl3_check_finished(SSL *s);
170 static const SSL_METHOD *ssl3_get_client_method(int ver)
172 if (ver == SSL3_VERSION)
173 return(SSLv3_client_method());
178 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
179 ssl_undefined_function,
181 ssl3_get_client_method)
183 int ssl3_connect(SSL *s)
186 unsigned long Time=(unsigned long)time(NULL);
188 void (*cb)(const SSL *ssl,int type,int val)=NULL;
190 int new_state,state,skip=0;;
192 RAND_add(&Time,sizeof(Time),0);
196 if (s->info_callback != NULL)
198 else if (s->ctx->info_callback != NULL)
199 cb=s->ctx->info_callback;
202 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
210 case SSL_ST_RENEGOTIATE:
212 s->state=SSL_ST_CONNECT;
213 s->ctx->stats.sess_connect_renegotiate++;
217 case SSL_ST_BEFORE|SSL_ST_CONNECT:
218 case SSL_ST_OK|SSL_ST_CONNECT:
221 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
223 if ((s->version & 0xff00 ) != 0x0300)
225 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
230 /* s->version=SSL3_VERSION; */
231 s->type=SSL_ST_CONNECT;
233 if (s->init_buf == NULL)
235 if ((buf=BUF_MEM_new()) == NULL)
240 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
249 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
251 /* setup buffing BIO */
252 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
254 /* don't push the buffering BIO quite yet */
256 ssl3_init_finished_mac(s);
258 s->state=SSL3_ST_CW_CLNT_HELLO_A;
259 s->ctx->stats.sess_connect++;
263 case SSL3_ST_CW_CLNT_HELLO_A:
264 case SSL3_ST_CW_CLNT_HELLO_B:
267 ret=ssl3_client_hello(s);
268 if (ret <= 0) goto end;
269 s->state=SSL3_ST_CR_SRVR_HELLO_A;
272 /* turn on buffering for the next lot of output */
273 if (s->bbio != s->wbio)
274 s->wbio=BIO_push(s->bbio,s->wbio);
278 case SSL3_ST_CR_SRVR_HELLO_A:
279 case SSL3_ST_CR_SRVR_HELLO_B:
280 ret=ssl3_get_server_hello(s);
281 if (ret <= 0) goto end;
284 s->state=SSL3_ST_CR_FINISHED_A;
286 s->state=SSL3_ST_CR_CERT_A;
290 case SSL3_ST_CR_CERT_A:
291 case SSL3_ST_CR_CERT_B:
292 #ifndef OPENSSL_NO_TLSEXT
293 ret=ssl3_check_finished(s);
294 if (ret <= 0) goto end;
298 s->state=SSL3_ST_CR_FINISHED_A;
303 /* Check if it is anon DH/ECDH */
305 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
306 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
308 ret=ssl3_get_server_certificate(s);
309 if (ret <= 0) goto end;
310 #ifndef OPENSSL_NO_TLSEXT
311 if (s->tlsext_status_expected)
312 s->state=SSL3_ST_CR_CERT_STATUS_A;
314 s->state=SSL3_ST_CR_KEY_EXCH_A;
319 s->state=SSL3_ST_CR_KEY_EXCH_A;
325 s->state=SSL3_ST_CR_KEY_EXCH_A;
330 case SSL3_ST_CR_KEY_EXCH_A:
331 case SSL3_ST_CR_KEY_EXCH_B:
332 ret=ssl3_get_key_exchange(s);
333 if (ret <= 0) goto end;
334 s->state=SSL3_ST_CR_CERT_REQ_A;
337 /* at this point we check that we have the
338 * required stuff from the server */
339 if (!ssl3_check_cert_and_algorithm(s))
346 case SSL3_ST_CR_CERT_REQ_A:
347 case SSL3_ST_CR_CERT_REQ_B:
348 ret=ssl3_get_certificate_request(s);
349 if (ret <= 0) goto end;
350 s->state=SSL3_ST_CR_SRVR_DONE_A;
354 case SSL3_ST_CR_SRVR_DONE_A:
355 case SSL3_ST_CR_SRVR_DONE_B:
356 ret=ssl3_get_server_done(s);
357 if (ret <= 0) goto end;
358 if (s->s3->tmp.cert_req)
359 s->state=SSL3_ST_CW_CERT_A;
361 s->state=SSL3_ST_CW_KEY_EXCH_A;
366 case SSL3_ST_CW_CERT_A:
367 case SSL3_ST_CW_CERT_B:
368 case SSL3_ST_CW_CERT_C:
369 case SSL3_ST_CW_CERT_D:
370 ret=ssl3_send_client_certificate(s);
371 if (ret <= 0) goto end;
372 s->state=SSL3_ST_CW_KEY_EXCH_A;
376 case SSL3_ST_CW_KEY_EXCH_A:
377 case SSL3_ST_CW_KEY_EXCH_B:
378 ret=ssl3_send_client_key_exchange(s);
379 if (ret <= 0) goto end;
380 /* EAY EAY EAY need to check for DH fix cert
382 /* For TLS, cert_req is set to 2, so a cert chain
383 * of nothing is sent, but no verify packet is sent */
384 /* XXX: For now, we do not support client
385 * authentication in ECDH cipher suites with
386 * ECDH (rather than ECDSA) certificates.
387 * We need to skip the certificate verify
388 * message when client's ECDH public key is sent
389 * inside the client certificate.
391 if (s->s3->tmp.cert_req == 1)
393 s->state=SSL3_ST_CW_CERT_VRFY_A;
397 s->state=SSL3_ST_CW_CHANGE_A;
398 s->s3->change_cipher_spec=0;
404 case SSL3_ST_CW_CERT_VRFY_A:
405 case SSL3_ST_CW_CERT_VRFY_B:
406 ret=ssl3_send_client_verify(s);
407 if (ret <= 0) goto end;
408 s->state=SSL3_ST_CW_CHANGE_A;
410 s->s3->change_cipher_spec=0;
413 case SSL3_ST_CW_CHANGE_A:
414 case SSL3_ST_CW_CHANGE_B:
415 ret=ssl3_send_change_cipher_spec(s,
416 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
417 if (ret <= 0) goto end;
418 s->state=SSL3_ST_CW_FINISHED_A;
421 s->session->cipher=s->s3->tmp.new_cipher;
422 #ifdef OPENSSL_NO_COMP
423 s->session->compress_meth=0;
425 if (s->s3->tmp.new_compression == NULL)
426 s->session->compress_meth=0;
428 s->session->compress_meth=
429 s->s3->tmp.new_compression->id;
431 if (!s->method->ssl3_enc->setup_key_block(s))
437 if (!s->method->ssl3_enc->change_cipher_state(s,
438 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
446 case SSL3_ST_CW_FINISHED_A:
447 case SSL3_ST_CW_FINISHED_B:
448 ret=ssl3_send_finished(s,
449 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
450 s->method->ssl3_enc->client_finished_label,
451 s->method->ssl3_enc->client_finished_label_len);
452 if (ret <= 0) goto end;
453 s->state=SSL3_ST_CW_FLUSH;
456 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
459 s->s3->tmp.next_state=SSL_ST_OK;
460 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
463 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
464 s->s3->delay_buf_pop_ret=0;
469 #ifndef OPENSSL_NO_TLSEXT
470 /* Allow NewSessionTicket if ticket expected */
471 if (s->tlsext_ticket_expected)
472 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
476 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
481 #ifndef OPENSSL_NO_TLSEXT
482 case SSL3_ST_CR_SESSION_TICKET_A:
483 case SSL3_ST_CR_SESSION_TICKET_B:
484 ret=ssl3_get_new_session_ticket(s);
485 if (ret <= 0) goto end;
486 s->state=SSL3_ST_CR_FINISHED_A;
490 case SSL3_ST_CR_CERT_STATUS_A:
491 case SSL3_ST_CR_CERT_STATUS_B:
492 ret=ssl3_get_cert_status(s);
493 if (ret <= 0) goto end;
494 s->state=SSL3_ST_CR_KEY_EXCH_A;
499 case SSL3_ST_CR_FINISHED_A:
500 case SSL3_ST_CR_FINISHED_B:
502 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
503 SSL3_ST_CR_FINISHED_B);
504 if (ret <= 0) goto end;
507 s->state=SSL3_ST_CW_CHANGE_A;
513 case SSL3_ST_CW_FLUSH:
514 /* number of bytes to be flushed */
515 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
518 s->rwstate=SSL_WRITING;
519 num1=BIO_flush(s->wbio);
520 if (num1 <= 0) { ret= -1; goto end; }
521 s->rwstate=SSL_NOTHING;
524 s->state=s->s3->tmp.next_state;
528 /* clean a few things up */
529 ssl3_cleanup_key_block(s);
531 if (s->init_buf != NULL)
533 BUF_MEM_free(s->init_buf);
537 /* If we are not 'joining' the last two packets,
538 * remove the buffering now */
539 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
540 ssl_free_wbio_buffer(s);
541 /* else do it later in ssl3_write */
546 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
547 if (s->hit) s->ctx->stats.sess_hit++;
551 s->handshake_func=ssl3_connect;
552 s->ctx->stats.sess_connect_good++;
554 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
560 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
566 /* did we do anything */
567 if (!s->s3->tmp.reuse_message && !skip)
571 if ((ret=BIO_flush(s->wbio)) <= 0)
575 if ((cb != NULL) && (s->state != state))
579 cb(s,SSL_CB_CONNECT_LOOP,1);
590 cb(s,SSL_CB_CONNECT_EXIT,ret);
595 int ssl3_client_hello(SSL *s)
600 unsigned long Time,l;
601 #ifndef OPENSSL_NO_COMP
606 buf=(unsigned char *)s->init_buf->data;
607 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
609 if ((s->session == NULL) ||
610 (s->session->ssl_version != s->version) ||
611 (s->session->not_resumable))
613 if (!ssl_get_new_session(s,0))
616 /* else use the pre-loaded session */
618 p=s->s3->client_random;
619 Time=(unsigned long)time(NULL); /* Time */
621 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
624 /* Do the message type and length last */
627 *(p++)=s->version>>8;
628 *(p++)=s->version&0xff;
629 s->client_version=s->version;
632 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
639 i=s->session->session_id_length;
643 if (i > (int)sizeof(s->session->session_id))
645 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
648 memcpy(p,s->session->session_id,i);
652 /* Ciphers supported */
653 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
656 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
663 #ifdef OPENSSL_NO_COMP
667 if ((s->options & SSL_OP_NO_COMPRESSION)
668 || !s->ctx->comp_methods)
671 j=sk_SSL_COMP_num(s->ctx->comp_methods);
675 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
679 *(p++)=0; /* Add the NULL method */
681 #ifndef OPENSSL_NO_TLSEXT
683 if (ssl_prepare_clienthello_tlsext(s) <= 0)
685 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
688 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
690 SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
697 *(d++)=SSL3_MT_CLIENT_HELLO;
700 s->state=SSL3_ST_CW_CLNT_HELLO_B;
701 /* number of bytes to write */
706 /* SSL3_ST_CW_CLNT_HELLO_B */
707 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
712 int ssl3_get_server_hello(SSL *s)
714 STACK_OF(SSL_CIPHER) *sk;
720 #ifndef OPENSSL_NO_COMP
724 n=s->method->ssl_get_message(s,
725 SSL3_ST_CR_SRVR_HELLO_A,
726 SSL3_ST_CR_SRVR_HELLO_B,
731 if (!ok) return((int)n);
733 if ( SSL_version(s) == DTLS1_VERSION)
735 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
737 if ( s->d1->send_cookie == 0)
739 s->s3->tmp.reuse_message = 1;
742 else /* already sent a cookie */
744 al=SSL_AD_UNEXPECTED_MESSAGE;
745 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
751 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
753 al=SSL_AD_UNEXPECTED_MESSAGE;
754 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
758 d=p=(unsigned char *)s->init_msg;
760 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
762 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
763 s->version=(s->version&0xff00)|p[1];
764 al=SSL_AD_PROTOCOL_VERSION;
769 /* load the server hello data */
770 /* load the server random */
771 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
774 /* get the session-id */
777 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
779 al=SSL_AD_ILLEGAL_PARAMETER;
780 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
784 if (j != 0 && j == s->session->session_id_length
785 && memcmp(p,s->session->session_id,j) == 0)
787 if(s->sid_ctx_length != s->session->sid_ctx_length
788 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
790 /* actually a client application bug */
791 al=SSL_AD_ILLEGAL_PARAMETER;
792 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
797 else /* a miss or crap from the other end */
799 /* If we were trying for session-id reuse, make a new
800 * SSL_SESSION so we don't stuff up other people */
802 if (s->session->session_id_length > 0)
804 if (!ssl_get_new_session(s,0))
806 al=SSL_AD_INTERNAL_ERROR;
810 s->session->session_id_length=j;
811 memcpy(s->session->session_id,p,j); /* j could be 0 */
814 c=ssl_get_cipher_by_char(s,p);
818 al=SSL_AD_ILLEGAL_PARAMETER;
819 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
822 p+=ssl_put_cipher_by_char(s,NULL,NULL);
824 sk=ssl_get_ciphers_by_id(s);
825 i=sk_SSL_CIPHER_find(sk,c);
828 /* we did not say we would use this cipher */
829 al=SSL_AD_ILLEGAL_PARAMETER;
830 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
834 /* Depending on the session caching (internal/external), the cipher
835 and/or cipher_id values may not be set. Make sure that
836 cipher_id is set and use it for comparison. */
837 if (s->session->cipher)
838 s->session->cipher_id = s->session->cipher->id;
839 if (s->hit && (s->session->cipher_id != c->id))
842 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
844 al=SSL_AD_ILLEGAL_PARAMETER;
845 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
849 s->s3->tmp.new_cipher=c;
850 ssl3_digest_cached_records(s);
852 /* lets get the compression algorithm */
854 #ifdef OPENSSL_NO_COMP
857 al=SSL_AD_ILLEGAL_PARAMETER;
858 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
863 if ((j == 0) || (s->options & SSL_OP_NO_COMPRESSION))
866 comp=ssl3_comp_find(s->ctx->comp_methods,j);
868 if ((j != 0) && (comp == NULL))
870 al=SSL_AD_ILLEGAL_PARAMETER;
871 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
876 s->s3->tmp.new_compression=comp;
880 #ifndef OPENSSL_NO_TLSEXT
882 if (s->version > SSL3_VERSION)
884 if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
886 /* 'al' set by ssl_parse_serverhello_tlsext */
887 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
890 if (ssl_check_serverhello_tlsext(s) <= 0)
892 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
900 /* wrong packet length */
901 al=SSL_AD_DECODE_ERROR;
902 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
908 ssl3_send_alert(s,SSL3_AL_FATAL,al);
913 int ssl3_get_server_certificate(SSL *s)
916 unsigned long n,nc,llen,l;
918 const unsigned char *q,*p;
920 STACK_OF(X509) *sk=NULL;
923 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
925 n=s->method->ssl_get_message(s,
932 if (!ok) return((int)n);
934 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
936 s->s3->tmp.reuse_message=1;
940 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
942 al=SSL_AD_UNEXPECTED_MESSAGE;
943 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
946 p=d=(unsigned char *)s->init_msg;
948 if ((sk=sk_X509_new_null()) == NULL)
950 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
957 al=SSL_AD_DECODE_ERROR;
958 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
961 for (nc=0; nc<llen; )
966 al=SSL_AD_DECODE_ERROR;
967 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
972 x=d2i_X509(NULL,&q,l);
975 al=SSL_AD_BAD_CERTIFICATE;
976 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
981 al=SSL_AD_DECODE_ERROR;
982 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
985 if (!sk_X509_push(sk,x))
987 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
995 i=ssl_verify_cert_chain(s,sk);
996 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
997 #ifndef OPENSSL_NO_KRB5
998 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
999 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1000 #endif /* OPENSSL_NO_KRB5 */
1003 al=ssl_verify_alarm_type(s->verify_result);
1004 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
1007 ERR_clear_error(); /* but we keep s->verify_result */
1009 sc=ssl_sess_cert_new();
1010 if (sc == NULL) goto err;
1012 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1013 s->session->sess_cert=sc;
1016 /* Inconsistency alert: cert_chain does include the peer's
1017 * certificate, which we don't include in s3_srvr.c */
1018 x=sk_X509_value(sk,0);
1020 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1022 pkey=X509_get_pubkey(x);
1024 /* VRS: allow null cert if auth == KRB5 */
1025 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1026 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1030 printf("pkey,x = %p, %p\n", pkey,x);
1031 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
1032 printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name,
1033 s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1034 #endif /* KSSL_DEBUG */
1036 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
1040 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1041 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1045 i=ssl_cert_type(x,pkey);
1046 if (need_cert && i < 0)
1050 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1051 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1057 sc->peer_cert_type=i;
1058 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1059 /* Why would the following ever happen?
1060 * We just created sc a couple of lines ago. */
1061 if (sc->peer_pkeys[i].x509 != NULL)
1062 X509_free(sc->peer_pkeys[i].x509);
1063 sc->peer_pkeys[i].x509=x;
1064 sc->peer_key= &(sc->peer_pkeys[i]);
1066 if (s->session->peer != NULL)
1067 X509_free(s->session->peer);
1068 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1073 sc->peer_cert_type=i;
1076 if (s->session->peer != NULL)
1077 X509_free(s->session->peer);
1078 s->session->peer=NULL;
1080 s->session->verify_result = s->verify_result;
1088 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1091 EVP_PKEY_free(pkey);
1093 sk_X509_pop_free(sk,X509_free);
1097 int ssl3_get_key_exchange(SSL *s)
1099 #ifndef OPENSSL_NO_RSA
1100 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
1103 unsigned char *param,*p;
1104 int al,i,j,param_len,ok;
1106 EVP_PKEY *pkey=NULL;
1107 #ifndef OPENSSL_NO_RSA
1110 #ifndef OPENSSL_NO_DH
1113 #ifndef OPENSSL_NO_ECDH
1114 EC_KEY *ecdh = NULL;
1115 BN_CTX *bn_ctx = NULL;
1116 EC_POINT *srvr_ecpoint = NULL;
1118 int encoded_pt_len = 0;
1121 /* use same message size as in ssl3_get_certificate_request()
1122 * as ServerKeyExchange message may be skipped */
1123 n=s->method->ssl_get_message(s,
1124 SSL3_ST_CR_KEY_EXCH_A,
1125 SSL3_ST_CR_KEY_EXCH_B,
1129 if (!ok) return((int)n);
1131 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1133 #ifndef OPENSSL_NO_PSK
1134 /* In plain PSK ciphersuite, ServerKeyExchange can be
1135 omitted if no identity hint is sent. Set
1136 session->sess_cert anyway to avoid problems
1138 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
1140 s->session->sess_cert=ssl_sess_cert_new();
1141 if (s->ctx->psk_identity_hint)
1142 OPENSSL_free(s->ctx->psk_identity_hint);
1143 s->ctx->psk_identity_hint = NULL;
1146 s->s3->tmp.reuse_message=1;
1150 param=p=(unsigned char *)s->init_msg;
1151 if (s->session->sess_cert != NULL)
1153 #ifndef OPENSSL_NO_RSA
1154 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1156 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1157 s->session->sess_cert->peer_rsa_tmp=NULL;
1160 #ifndef OPENSSL_NO_DH
1161 if (s->session->sess_cert->peer_dh_tmp)
1163 DH_free(s->session->sess_cert->peer_dh_tmp);
1164 s->session->sess_cert->peer_dh_tmp=NULL;
1167 #ifndef OPENSSL_NO_ECDH
1168 if (s->session->sess_cert->peer_ecdh_tmp)
1170 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1171 s->session->sess_cert->peer_ecdh_tmp=NULL;
1177 s->session->sess_cert=ssl_sess_cert_new();
1181 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1182 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1183 EVP_MD_CTX_init(&md_ctx);
1185 #ifndef OPENSSL_NO_PSK
1186 if (alg_k & SSL_kPSK)
1188 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
1190 al=SSL_AD_HANDSHAKE_FAILURE;
1193 /* Store PSK identity hint for later use, hint is used
1194 * in ssl3_send_client_key_exchange. Assume that the
1195 * maximum length of a PSK identity hint can be as
1196 * long as the maximum length of a PSK identity. */
1197 if (i > PSK_MAX_IDENTITY_LEN)
1199 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1200 SSL_R_DATA_LENGTH_TOO_LONG);
1205 al=SSL_AD_DECODE_ERROR;
1206 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1207 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1210 /* If received PSK identity hint contains NULL
1211 * characters, the hint is truncated from the first
1212 * NULL. p may not be ending with NULL, so create a
1213 * NULL-terminated string. */
1214 memcpy(tmp_id_hint, p, i);
1215 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
1216 if (s->ctx->psk_identity_hint != NULL)
1217 OPENSSL_free(s->ctx->psk_identity_hint);
1218 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1219 if (s->ctx->psk_identity_hint == NULL)
1221 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1229 #endif /* !OPENSSL_NO_PSK */
1230 #ifndef OPENSSL_NO_RSA
1231 if (alg_k & SSL_kRSA)
1233 if ((rsa=RSA_new()) == NULL)
1235 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1242 al=SSL_AD_DECODE_ERROR;
1243 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
1246 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1248 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1257 al=SSL_AD_DECODE_ERROR;
1258 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
1261 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1263 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1269 /* this should be because we are using an export cipher */
1270 if (alg_a & SSL_aRSA)
1271 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1274 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1277 s->session->sess_cert->peer_rsa_tmp=rsa;
1280 #else /* OPENSSL_NO_RSA */
1284 #ifndef OPENSSL_NO_DH
1285 else if (alg_k & SSL_kEDH)
1287 if ((dh=DH_new()) == NULL)
1289 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
1296 al=SSL_AD_DECODE_ERROR;
1297 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
1300 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1302 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1311 al=SSL_AD_DECODE_ERROR;
1312 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
1315 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1317 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1326 al=SSL_AD_DECODE_ERROR;
1327 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
1330 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1332 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1338 #ifndef OPENSSL_NO_RSA
1339 if (alg_a & SSL_aRSA)
1340 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1345 #ifndef OPENSSL_NO_DSA
1346 else if (alg_a & SSL_aDSS)
1347 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1349 /* else anonymous DH, so no certificate or pkey. */
1351 s->session->sess_cert->peer_dh_tmp=dh;
1354 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1356 al=SSL_AD_ILLEGAL_PARAMETER;
1357 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1360 #endif /* !OPENSSL_NO_DH */
1362 #ifndef OPENSSL_NO_ECDH
1363 else if (alg_k & SSL_kEECDH)
1366 const EC_GROUP *group;
1368 if ((ecdh=EC_KEY_new()) == NULL)
1370 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1374 /* Extract elliptic curve parameters and the
1375 * server's ephemeral ECDH public key.
1376 * Keep accumulating lengths of various components in
1377 * param_len and make sure it never exceeds n.
1380 /* XXX: For now we only support named (not generic) curves
1381 * and the ECParameters in this case is just three bytes.
1384 if ((param_len > n) ||
1385 (*p != NAMED_CURVE_TYPE) ||
1386 ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0))
1388 al=SSL_AD_INTERNAL_ERROR;
1389 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1393 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1396 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1399 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1401 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1404 EC_GROUP_free(ngroup);
1406 group = EC_KEY_get0_group(ecdh);
1408 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1409 (EC_GROUP_get_degree(group) > 163))
1411 al=SSL_AD_EXPORT_RESTRICTION;
1412 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1418 /* Next, get the encoded ECPoint */
1419 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1420 ((bn_ctx = BN_CTX_new()) == NULL))
1422 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1426 encoded_pt_len = *p; /* length of encoded point */
1428 param_len += (1 + encoded_pt_len);
1429 if ((param_len > n) ||
1430 (EC_POINT_oct2point(group, srvr_ecpoint,
1431 p, encoded_pt_len, bn_ctx) == 0))
1433 al=SSL_AD_DECODE_ERROR;
1434 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
1441 /* The ECC/TLS specification does not mention
1442 * the use of DSA to sign ECParameters in the server
1443 * key exchange message. We do support RSA and ECDSA.
1446 #ifndef OPENSSL_NO_RSA
1447 else if (alg_a & SSL_aRSA)
1448 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1450 #ifndef OPENSSL_NO_ECDSA
1451 else if (alg_a & SSL_aECDSA)
1452 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1454 /* else anonymous ECDH, so no certificate or pkey. */
1455 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1456 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1458 BN_CTX_free(bn_ctx);
1459 EC_POINT_free(srvr_ecpoint);
1460 srvr_ecpoint = NULL;
1464 al=SSL_AD_UNEXPECTED_MESSAGE;
1465 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
1468 #endif /* !OPENSSL_NO_ECDH */
1471 /* p points to the next byte, there are 'n' bytes left */
1473 /* if it was signed, check the signature */
1478 j=EVP_PKEY_size(pkey);
1480 if ((i != n) || (n > j) || (n <= 0))
1482 /* wrong packet length */
1483 al=SSL_AD_DECODE_ERROR;
1484 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1488 #ifndef OPENSSL_NO_RSA
1489 if (pkey->type == EVP_PKEY_RSA)
1495 for (num=2; num > 0; num--)
1497 EVP_DigestInit_ex(&md_ctx,(num == 2)
1498 ?s->ctx->md5:s->ctx->sha1, NULL);
1499 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1500 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1501 EVP_DigestUpdate(&md_ctx,param,param_len);
1502 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1506 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1510 al=SSL_AD_DECRYPT_ERROR;
1511 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1517 al=SSL_AD_DECRYPT_ERROR;
1518 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1524 #ifndef OPENSSL_NO_DSA
1525 if (pkey->type == EVP_PKEY_DSA)
1528 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
1529 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1530 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1531 EVP_VerifyUpdate(&md_ctx,param,param_len);
1532 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1535 al=SSL_AD_DECRYPT_ERROR;
1536 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1542 #ifndef OPENSSL_NO_ECDSA
1543 if (pkey->type == EVP_PKEY_EC)
1545 /* let's do ECDSA */
1546 EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL);
1547 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1548 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1549 EVP_VerifyUpdate(&md_ctx,param,param_len);
1550 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1553 al=SSL_AD_DECRYPT_ERROR;
1554 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1561 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1567 if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK))
1568 /* aNULL or kPSK do not need public keys */
1570 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1573 /* still data left over */
1576 al=SSL_AD_DECODE_ERROR;
1577 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1581 EVP_PKEY_free(pkey);
1582 EVP_MD_CTX_cleanup(&md_ctx);
1585 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1587 EVP_PKEY_free(pkey);
1588 #ifndef OPENSSL_NO_RSA
1592 #ifndef OPENSSL_NO_DH
1596 #ifndef OPENSSL_NO_ECDH
1597 BN_CTX_free(bn_ctx);
1598 EC_POINT_free(srvr_ecpoint);
1602 EVP_MD_CTX_cleanup(&md_ctx);
1606 int ssl3_get_certificate_request(SSL *s)
1609 unsigned long n,nc,l;
1610 unsigned int llen,ctype_num,i;
1612 const unsigned char *p,*q;
1614 STACK_OF(X509_NAME) *ca_sk=NULL;
1616 n=s->method->ssl_get_message(s,
1617 SSL3_ST_CR_CERT_REQ_A,
1618 SSL3_ST_CR_CERT_REQ_B,
1623 if (!ok) return((int)n);
1625 s->s3->tmp.cert_req=0;
1627 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1629 s->s3->tmp.reuse_message=1;
1633 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1635 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1636 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1640 /* TLS does not like anon-DH with client cert */
1641 if (s->version > SSL3_VERSION)
1643 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1645 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1646 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1651 p=d=(unsigned char *)s->init_msg;
1653 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1655 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1659 /* get the certificate types */
1661 if (ctype_num > SSL3_CT_NUMBER)
1662 ctype_num=SSL3_CT_NUMBER;
1663 for (i=0; i<ctype_num; i++)
1664 s->s3->tmp.ctype[i]= p[i];
1667 /* get the CA RDNs */
1672 out=fopen("/tmp/vsign.der","w");
1673 fwrite(p,1,llen,out);
1678 if ((llen+ctype_num+2+1) != n)
1680 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1681 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1685 for (nc=0; nc<llen; )
1688 if ((l+nc+2) > llen)
1690 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1691 goto cont; /* netscape bugs */
1692 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1693 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1699 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1701 /* If netscape tolerance is on, ignore errors */
1702 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1706 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1707 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1714 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1715 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1718 if (!sk_X509_NAME_push(ca_sk,xn))
1720 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1734 /* we should setup a certificate to return.... */
1735 s->s3->tmp.cert_req=1;
1736 s->s3->tmp.ctype_num=ctype_num;
1737 if (s->s3->tmp.ca_names != NULL)
1738 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1739 s->s3->tmp.ca_names=ca_sk;
1744 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1748 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1750 return(X509_NAME_cmp(*a,*b));
1752 #ifndef OPENSSL_NO_TLSEXT
1753 int ssl3_get_new_session_ticket(SSL *s)
1755 int ok,al,ret=0, ticklen;
1757 const unsigned char *p;
1760 n=s->method->ssl_get_message(s,
1761 SSL3_ST_CR_SESSION_TICKET_A,
1762 SSL3_ST_CR_SESSION_TICKET_B,
1770 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
1772 s->s3->tmp.reuse_message=1;
1775 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
1777 al=SSL_AD_UNEXPECTED_MESSAGE;
1778 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE);
1783 /* need at least ticket_lifetime_hint + ticket length */
1784 al = SSL3_AL_FATAL,SSL_AD_DECODE_ERROR;
1785 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
1788 p=d=(unsigned char *)s->init_msg;
1789 n2l(p, s->session->tlsext_tick_lifetime_hint);
1791 /* ticket_lifetime_hint + ticket_length + ticket */
1792 if (ticklen + 6 != n)
1794 al = SSL3_AL_FATAL,SSL_AD_DECODE_ERROR;
1795 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
1798 if (s->session->tlsext_tick)
1800 OPENSSL_free(s->session->tlsext_tick);
1801 s->session->tlsext_ticklen = 0;
1803 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1804 if (!s->session->tlsext_tick)
1806 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE);
1809 memcpy(s->session->tlsext_tick, p, ticklen);
1810 s->session->tlsext_ticklen = ticklen;
1815 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1820 int ssl3_get_cert_status(SSL *s)
1823 unsigned long resplen;
1825 const unsigned char *p;
1827 n=s->method->ssl_get_message(s,
1828 SSL3_ST_CR_CERT_STATUS_A,
1829 SSL3_ST_CR_CERT_STATUS_B,
1830 SSL3_MT_CERTIFICATE_STATUS,
1834 if (!ok) return((int)n);
1837 /* need at least status type + length */
1838 al = SSL_AD_DECODE_ERROR;
1839 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
1842 p = (unsigned char *)s->init_msg;
1843 if (*p++ != TLSEXT_STATUSTYPE_ocsp)
1845 al = SSL_AD_DECODE_ERROR;
1846 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE);
1850 if (resplen + 4 != n)
1852 al = SSL_AD_DECODE_ERROR;
1853 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
1856 if (s->tlsext_ocsp_resp)
1857 OPENSSL_free(s->tlsext_ocsp_resp);
1858 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
1859 if (!s->tlsext_ocsp_resp)
1861 al = SSL_AD_INTERNAL_ERROR;
1862 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
1865 s->tlsext_ocsp_resplen = resplen;
1866 if (s->ctx->tlsext_status_cb)
1869 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1872 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1873 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_RESPONSE);
1878 al = SSL_AD_INTERNAL_ERROR;
1879 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
1885 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1890 int ssl3_get_server_done(SSL *s)
1895 n=s->method->ssl_get_message(s,
1896 SSL3_ST_CR_SRVR_DONE_A,
1897 SSL3_ST_CR_SRVR_DONE_B,
1898 SSL3_MT_SERVER_DONE,
1899 30, /* should be very small, like 0 :-) */
1902 if (!ok) return((int)n);
1905 /* should contain no data */
1906 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1907 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1915 int ssl3_send_client_key_exchange(SSL *s)
1917 unsigned char *p,*d;
1919 unsigned long alg_k;
1920 #ifndef OPENSSL_NO_RSA
1922 EVP_PKEY *pkey=NULL;
1924 #ifndef OPENSSL_NO_KRB5
1926 #endif /* OPENSSL_NO_KRB5 */
1927 #ifndef OPENSSL_NO_ECDH
1928 EC_KEY *clnt_ecdh = NULL;
1929 const EC_POINT *srvr_ecpoint = NULL;
1930 EVP_PKEY *srvr_pub_pkey = NULL;
1931 unsigned char *encodedPoint = NULL;
1932 int encoded_pt_len = 0;
1933 BN_CTX * bn_ctx = NULL;
1936 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1938 d=(unsigned char *)s->init_buf->data;
1941 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1943 /* Fool emacs indentation */
1945 #ifndef OPENSSL_NO_RSA
1946 else if (alg_k & SSL_kRSA)
1949 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1951 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1952 rsa=s->session->sess_cert->peer_rsa_tmp;
1955 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1956 if ((pkey == NULL) ||
1957 (pkey->type != EVP_PKEY_RSA) ||
1958 (pkey->pkey.rsa == NULL))
1960 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1964 EVP_PKEY_free(pkey);
1967 tmp_buf[0]=s->client_version>>8;
1968 tmp_buf[1]=s->client_version&0xff;
1969 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
1972 s->session->master_key_length=sizeof tmp_buf;
1975 /* Fix buf for TLS and beyond */
1976 if (s->version > SSL3_VERSION)
1978 n=RSA_public_encrypt(sizeof tmp_buf,
1979 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1981 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1982 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1986 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1990 /* Fix buf for TLS and beyond */
1991 if (s->version > SSL3_VERSION)
1997 s->session->master_key_length=
1998 s->method->ssl3_enc->generate_master_secret(s,
1999 s->session->master_key,
2000 tmp_buf,sizeof tmp_buf);
2001 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2004 #ifndef OPENSSL_NO_KRB5
2005 else if (alg_k & SSL_kKRB5)
2007 krb5_error_code krb5rc;
2008 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2009 /* krb5_data krb5_ap_req; */
2010 krb5_data *enc_ticket;
2011 krb5_data authenticator, *authp = NULL;
2012 EVP_CIPHER_CTX ciph_ctx;
2013 EVP_CIPHER *enc = NULL;
2014 unsigned char iv[EVP_MAX_IV_LENGTH];
2015 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2016 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
2017 + EVP_MAX_IV_LENGTH];
2018 int padl, outl = sizeof(epms);
2020 EVP_CIPHER_CTX_init(&ciph_ctx);
2023 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
2025 #endif /* KSSL_DEBUG */
2029 if (KRB5SENDAUTH) authp = &authenticator;
2030 #endif /* KRB5SENDAUTH */
2032 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
2034 enc = kssl_map_enc(kssl_ctx->enctype);
2039 printf("kssl_cget_tkt rtn %d\n", krb5rc);
2040 if (krb5rc && kssl_err.text)
2041 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
2043 #endif /* KSSL_DEBUG */
2047 ssl3_send_alert(s,SSL3_AL_FATAL,
2048 SSL_AD_HANDSHAKE_FAILURE);
2049 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2054 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
2055 ** in place of RFC 2712 KerberosWrapper, as in:
2057 ** Send ticket (copy to *p, set n = length)
2058 ** n = krb5_ap_req.length;
2059 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2060 ** if (krb5_ap_req.data)
2061 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2063 ** Now using real RFC 2712 KerberosWrapper
2064 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2065 ** Note: 2712 "opaque" types are here replaced
2066 ** with a 2-byte length followed by the value.
2068 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2069 ** Where "xx xx" = length bytes. Shown here with
2070 ** optional authenticator omitted.
2073 /* KerberosWrapper.Ticket */
2074 s2n(enc_ticket->length,p);
2075 memcpy(p, enc_ticket->data, enc_ticket->length);
2076 p+= enc_ticket->length;
2077 n = enc_ticket->length + 2;
2079 /* KerberosWrapper.Authenticator */
2080 if (authp && authp->length)
2082 s2n(authp->length,p);
2083 memcpy(p, authp->data, authp->length);
2085 n+= authp->length + 2;
2093 s2n(0,p);/* null authenticator length */
2097 tmp_buf[0]=s->client_version>>8;
2098 tmp_buf[1]=s->client_version&0xff;
2099 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2102 /* 20010420 VRS. Tried it this way; failed.
2103 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2104 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2105 ** kssl_ctx->length);
2106 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2109 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2110 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
2112 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
2114 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
2116 if (outl > sizeof epms)
2118 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2121 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2123 /* KerberosWrapper.EncryptedPreMasterSecret */
2125 memcpy(p, epms, outl);
2129 s->session->master_key_length=
2130 s->method->ssl3_enc->generate_master_secret(s,
2131 s->session->master_key,
2132 tmp_buf, sizeof tmp_buf);
2134 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2135 OPENSSL_cleanse(epms, outl);
2138 #ifndef OPENSSL_NO_DH
2139 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2141 DH *dh_srvr,*dh_clnt;
2143 if (s->session->sess_cert->peer_dh_tmp != NULL)
2144 dh_srvr=s->session->sess_cert->peer_dh_tmp;
2147 /* we get them from the cert */
2148 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2149 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
2153 /* generate a new random key */
2154 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2156 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2159 if (!DH_generate_key(dh_clnt))
2161 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2165 /* use the 'p' output buffer for the DH key, but
2166 * make sure to clear it out afterwards */
2168 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2172 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2176 /* generate master key from the result */
2177 s->session->master_key_length=
2178 s->method->ssl3_enc->generate_master_secret(s,
2179 s->session->master_key,p,n);
2183 /* send off the data */
2184 n=BN_num_bytes(dh_clnt->pub_key);
2186 BN_bn2bin(dh_clnt->pub_key,p);
2191 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2195 #ifndef OPENSSL_NO_ECDH
2196 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2198 const EC_GROUP *srvr_group = NULL;
2200 int ecdh_clnt_cert = 0;
2203 /* Did we send out the client's
2204 * ECDH share for use in premaster
2205 * computation as part of client certificate?
2206 * If so, set ecdh_clnt_cert to 1.
2208 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2210 /* XXX: For now, we do not support client
2211 * authentication using ECDH certificates.
2212 * To add such support, one needs to add
2213 * code that checks for appropriate
2214 * conditions and sets ecdh_clnt_cert to 1.
2215 * For example, the cert have an ECC
2216 * key on the same curve as the server's
2217 * and the key should be authorized for
2220 * One also needs to add code in ssl3_connect
2221 * to skip sending the certificate verify
2224 * if ((s->cert->key->privatekey != NULL) &&
2225 * (s->cert->key->privatekey->type ==
2226 * EVP_PKEY_EC) && ...)
2227 * ecdh_clnt_cert = 1;
2231 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2233 tkey = s->session->sess_cert->peer_ecdh_tmp;
2237 /* Get the Server Public Key from Cert */
2238 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2239 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2240 if ((srvr_pub_pkey == NULL) ||
2241 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2242 (srvr_pub_pkey->pkey.ec == NULL))
2244 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2245 ERR_R_INTERNAL_ERROR);
2249 tkey = srvr_pub_pkey->pkey.ec;
2252 srvr_group = EC_KEY_get0_group(tkey);
2253 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2255 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2257 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2258 ERR_R_INTERNAL_ERROR);
2262 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2264 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2268 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2270 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
2275 /* Reuse key info from our certificate
2276 * We only need our private key to perform
2277 * the ECDH computation.
2279 const BIGNUM *priv_key;
2280 tkey = s->cert->key->privatekey->pkey.ec;
2281 priv_key = EC_KEY_get0_private_key(tkey);
2282 if (priv_key == NULL)
2284 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2287 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2289 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
2295 /* Generate a new ECDH key pair */
2296 if (!(EC_KEY_generate_key(clnt_ecdh)))
2298 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2303 /* use the 'p' output buffer for the ECDH key, but
2304 * make sure to clear it out afterwards
2307 field_size = EC_GROUP_get_degree(srvr_group);
2308 if (field_size <= 0)
2310 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2314 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2317 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2322 /* generate master key from the result */
2323 s->session->master_key_length = s->method->ssl3_enc \
2324 -> generate_master_secret(s,
2325 s->session->master_key,
2328 memset(p, 0, n); /* clean up */
2332 /* Send empty client key exch message */
2337 /* First check the size of encoding and
2338 * allocate memory accordingly.
2341 EC_POINT_point2oct(srvr_group,
2342 EC_KEY_get0_public_key(clnt_ecdh),
2343 POINT_CONVERSION_UNCOMPRESSED,
2346 encodedPoint = (unsigned char *)
2347 OPENSSL_malloc(encoded_pt_len *
2348 sizeof(unsigned char));
2349 bn_ctx = BN_CTX_new();
2350 if ((encodedPoint == NULL) ||
2353 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2357 /* Encode the public key */
2358 n = EC_POINT_point2oct(srvr_group,
2359 EC_KEY_get0_public_key(clnt_ecdh),
2360 POINT_CONVERSION_UNCOMPRESSED,
2361 encodedPoint, encoded_pt_len, bn_ctx);
2363 *p = n; /* length of encoded point */
2364 /* Encoded point will be copied here */
2366 /* copy the point */
2367 memcpy((unsigned char *)p, encodedPoint, n);
2368 /* increment n to account for length field */
2372 /* Free allocated memory */
2373 BN_CTX_free(bn_ctx);
2374 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2375 if (clnt_ecdh != NULL)
2376 EC_KEY_free(clnt_ecdh);
2377 EVP_PKEY_free(srvr_pub_pkey);
2379 #endif /* !OPENSSL_NO_ECDH */
2380 #ifndef OPENSSL_NO_PSK
2381 else if (alg_k & SSL_kPSK)
2383 char identity[PSK_MAX_IDENTITY_LEN];
2384 unsigned char *t = NULL;
2385 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
2386 unsigned int pre_ms_len = 0, psk_len = 0;
2390 if (s->psk_client_callback == NULL)
2392 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2393 SSL_R_PSK_NO_CLIENT_CB);
2397 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2398 identity, PSK_MAX_IDENTITY_LEN,
2399 psk_or_pre_ms, sizeof(psk_or_pre_ms));
2400 if (psk_len > PSK_MAX_PSK_LEN)
2402 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2403 ERR_R_INTERNAL_ERROR);
2406 else if (psk_len == 0)
2408 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2409 SSL_R_PSK_IDENTITY_NOT_FOUND);
2413 /* create PSK pre_master_secret */
2414 pre_ms_len = 2+psk_len+2+psk_len;
2416 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
2418 memset(t, 0, psk_len);
2422 if (s->session->psk_identity_hint != NULL)
2423 OPENSSL_free(s->session->psk_identity_hint);
2424 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2425 if (s->ctx->psk_identity_hint != NULL &&
2426 s->session->psk_identity_hint == NULL)
2428 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2429 ERR_R_MALLOC_FAILURE);
2433 if (s->session->psk_identity != NULL)
2434 OPENSSL_free(s->session->psk_identity);
2435 s->session->psk_identity = BUF_strdup(identity);
2436 if (s->session->psk_identity == NULL)
2438 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2439 ERR_R_MALLOC_FAILURE);
2443 s->session->master_key_length =
2444 s->method->ssl3_enc->generate_master_secret(s,
2445 s->session->master_key,
2446 psk_or_pre_ms, pre_ms_len);
2447 n = strlen(identity);
2449 memcpy(p, identity, n);
2453 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2454 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2457 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2464 ssl3_send_alert(s, SSL3_AL_FATAL,
2465 SSL_AD_HANDSHAKE_FAILURE);
2466 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2467 ERR_R_INTERNAL_ERROR);
2471 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
2474 s->state=SSL3_ST_CW_KEY_EXCH_B;
2475 /* number of bytes to write */
2480 /* SSL3_ST_CW_KEY_EXCH_B */
2481 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2483 #ifndef OPENSSL_NO_ECDH
2484 BN_CTX_free(bn_ctx);
2485 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2486 if (clnt_ecdh != NULL)
2487 EC_KEY_free(clnt_ecdh);
2488 EVP_PKEY_free(srvr_pub_pkey);
2493 int ssl3_send_client_verify(SSL *s)
2495 unsigned char *p,*d;
2496 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
2498 #ifndef OPENSSL_NO_RSA
2502 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
2506 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2508 d=(unsigned char *)s->init_buf->data;
2510 pkey=s->cert->key->privatekey;
2512 s->method->ssl3_enc->cert_verify_mac(s,
2514 &(data[MD5_DIGEST_LENGTH]));
2516 #ifndef OPENSSL_NO_RSA
2517 if (pkey->type == EVP_PKEY_RSA)
2519 s->method->ssl3_enc->cert_verify_mac(s,
2522 if (RSA_sign(NID_md5_sha1, data,
2523 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
2524 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
2526 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
2534 #ifndef OPENSSL_NO_DSA
2535 if (pkey->type == EVP_PKEY_DSA)
2537 if (!DSA_sign(pkey->save_type,
2538 &(data[MD5_DIGEST_LENGTH]),
2539 SHA_DIGEST_LENGTH,&(p[2]),
2540 (unsigned int *)&j,pkey->pkey.dsa))
2542 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
2550 #ifndef OPENSSL_NO_ECDSA
2551 if (pkey->type == EVP_PKEY_EC)
2553 if (!ECDSA_sign(pkey->save_type,
2554 &(data[MD5_DIGEST_LENGTH]),
2555 SHA_DIGEST_LENGTH,&(p[2]),
2556 (unsigned int *)&j,pkey->pkey.ec))
2558 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2568 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
2571 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
2574 s->state=SSL3_ST_CW_CERT_VRFY_B;
2575 s->init_num=(int)n+4;
2578 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2583 int ssl3_send_client_certificate(SSL *s)
2586 EVP_PKEY *pkey=NULL;
2590 if (s->state == SSL3_ST_CW_CERT_A)
2592 if ((s->cert == NULL) ||
2593 (s->cert->key->x509 == NULL) ||
2594 (s->cert->key->privatekey == NULL))
2595 s->state=SSL3_ST_CW_CERT_B;
2597 s->state=SSL3_ST_CW_CERT_C;
2600 /* We need to get a client cert */
2601 if (s->state == SSL3_ST_CW_CERT_B)
2603 /* If we get an error, we need to
2604 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2605 * We then get retied later */
2607 if (s->ctx->client_cert_cb != NULL)
2608 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
2611 s->rwstate=SSL_X509_LOOKUP;
2614 s->rwstate=SSL_NOTHING;
2615 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2617 s->state=SSL3_ST_CW_CERT_B;
2618 if ( !SSL_use_certificate(s,x509) ||
2619 !SSL_use_PrivateKey(s,pkey))
2625 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2628 if (x509 != NULL) X509_free(x509);
2629 if (pkey != NULL) EVP_PKEY_free(pkey);
2632 if (s->version == SSL3_VERSION)
2634 s->s3->tmp.cert_req=0;
2635 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2640 s->s3->tmp.cert_req=2;
2644 /* Ok, we have a cert */
2645 s->state=SSL3_ST_CW_CERT_C;
2648 if (s->state == SSL3_ST_CW_CERT_C)
2650 s->state=SSL3_ST_CW_CERT_D;
2651 l=ssl3_output_cert_chain(s,
2652 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
2656 /* SSL3_ST_CW_CERT_D */
2657 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2660 #define has_bits(i,m) (((i)&(m)) == (m))
2662 int ssl3_check_cert_and_algorithm(SSL *s)
2666 EVP_PKEY *pkey=NULL;
2668 #ifndef OPENSSL_NO_RSA
2671 #ifndef OPENSSL_NO_DH
2675 sc=s->session->sess_cert;
2678 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
2682 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2683 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2685 /* we don't have a certificate */
2686 if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK))
2689 #ifndef OPENSSL_NO_RSA
2690 rsa=s->session->sess_cert->peer_rsa_tmp;
2692 #ifndef OPENSSL_NO_DH
2693 dh=s->session->sess_cert->peer_dh_tmp;
2696 /* This is the passed certificate */
2698 idx=sc->peer_cert_type;
2699 #ifndef OPENSSL_NO_ECDH
2700 if (idx == SSL_PKEY_ECC)
2702 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2703 s->s3->tmp.new_cipher) == 0)
2704 { /* check failed */
2705 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
2714 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2715 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2716 EVP_PKEY_free(pkey);
2719 /* Check that we have a certificate if we require one */
2720 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2722 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
2725 #ifndef OPENSSL_NO_DSA
2726 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
2728 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
2732 #ifndef OPENSSL_NO_RSA
2733 if ((alg_k & SSL_kRSA) &&
2734 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2736 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2740 #ifndef OPENSSL_NO_DH
2741 if ((alg_k & SSL_kEDH) &&
2742 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2744 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
2747 else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
2749 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
2752 #ifndef OPENSSL_NO_DSA
2753 else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
2755 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
2761 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
2763 #ifndef OPENSSL_NO_RSA
2764 if (alg_k & SSL_kRSA)
2767 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2769 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
2775 #ifndef OPENSSL_NO_DH
2776 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2779 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2781 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
2788 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2794 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2799 /* Check to see if handshake is full or resumed. Usually this is just a
2800 * case of checking to see if a cache hit has occurred. In the case of
2801 * session tickets we have to check the next message to be sure.
2804 #ifndef OPENSSL_NO_TLSEXT
2805 static int ssl3_check_finished(SSL *s)
2809 if (!s->session->tlsext_tick)
2811 /* this function is called when we really expect a Certificate
2812 * message, so permit appropriate message length */
2813 n=s->method->ssl_get_message(s,
2819 if (!ok) return((int)n);
2820 s->s3->tmp.reuse_message = 1;
2821 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
2822 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))