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;
326 s->state=SSL3_ST_CR_KEY_EXCH_A;
331 case SSL3_ST_CR_KEY_EXCH_A:
332 case SSL3_ST_CR_KEY_EXCH_B:
333 ret=ssl3_get_key_exchange(s);
334 if (ret <= 0) goto end;
335 s->state=SSL3_ST_CR_CERT_REQ_A;
338 /* at this point we check that we have the
339 * required stuff from the server */
340 if (!ssl3_check_cert_and_algorithm(s))
347 case SSL3_ST_CR_CERT_REQ_A:
348 case SSL3_ST_CR_CERT_REQ_B:
349 ret=ssl3_get_certificate_request(s);
350 if (ret <= 0) goto end;
351 s->state=SSL3_ST_CR_SRVR_DONE_A;
355 case SSL3_ST_CR_SRVR_DONE_A:
356 case SSL3_ST_CR_SRVR_DONE_B:
357 ret=ssl3_get_server_done(s);
358 if (ret <= 0) goto end;
359 if (s->s3->tmp.cert_req)
360 s->state=SSL3_ST_CW_CERT_A;
362 s->state=SSL3_ST_CW_KEY_EXCH_A;
367 case SSL3_ST_CW_CERT_A:
368 case SSL3_ST_CW_CERT_B:
369 case SSL3_ST_CW_CERT_C:
370 case SSL3_ST_CW_CERT_D:
371 ret=ssl3_send_client_certificate(s);
372 if (ret <= 0) goto end;
373 s->state=SSL3_ST_CW_KEY_EXCH_A;
377 case SSL3_ST_CW_KEY_EXCH_A:
378 case SSL3_ST_CW_KEY_EXCH_B:
379 ret=ssl3_send_client_key_exchange(s);
380 if (ret <= 0) goto end;
381 /* EAY EAY EAY need to check for DH fix cert
383 /* For TLS, cert_req is set to 2, so a cert chain
384 * of nothing is sent, but no verify packet is sent */
385 /* XXX: For now, we do not support client
386 * authentication in ECDH cipher suites with
387 * ECDH (rather than ECDSA) certificates.
388 * We need to skip the certificate verify
389 * message when client's ECDH public key is sent
390 * inside the client certificate.
392 if (s->s3->tmp.cert_req == 1)
394 s->state=SSL3_ST_CW_CERT_VRFY_A;
398 s->state=SSL3_ST_CW_CHANGE_A;
399 s->s3->change_cipher_spec=0;
405 case SSL3_ST_CW_CERT_VRFY_A:
406 case SSL3_ST_CW_CERT_VRFY_B:
407 ret=ssl3_send_client_verify(s);
408 if (ret <= 0) goto end;
409 s->state=SSL3_ST_CW_CHANGE_A;
411 s->s3->change_cipher_spec=0;
414 case SSL3_ST_CW_CHANGE_A:
415 case SSL3_ST_CW_CHANGE_B:
416 ret=ssl3_send_change_cipher_spec(s,
417 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
418 if (ret <= 0) goto end;
419 s->state=SSL3_ST_CW_FINISHED_A;
422 s->session->cipher=s->s3->tmp.new_cipher;
423 #ifdef OPENSSL_NO_COMP
424 s->session->compress_meth=0;
426 if (s->s3->tmp.new_compression == NULL)
427 s->session->compress_meth=0;
429 s->session->compress_meth=
430 s->s3->tmp.new_compression->id;
432 if (!s->method->ssl3_enc->setup_key_block(s))
438 if (!s->method->ssl3_enc->change_cipher_state(s,
439 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
447 case SSL3_ST_CW_FINISHED_A:
448 case SSL3_ST_CW_FINISHED_B:
449 ret=ssl3_send_finished(s,
450 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
451 s->method->ssl3_enc->client_finished_label,
452 s->method->ssl3_enc->client_finished_label_len);
453 if (ret <= 0) goto end;
454 s->state=SSL3_ST_CW_FLUSH;
457 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
460 s->s3->tmp.next_state=SSL_ST_OK;
461 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
464 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
465 s->s3->delay_buf_pop_ret=0;
470 #ifndef OPENSSL_NO_TLSEXT
471 /* Allow NewSessionTicket if ticket expected */
472 if (s->tlsext_ticket_expected)
473 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
477 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
482 #ifndef OPENSSL_NO_TLSEXT
483 case SSL3_ST_CR_SESSION_TICKET_A:
484 case SSL3_ST_CR_SESSION_TICKET_B:
485 ret=ssl3_get_new_session_ticket(s);
486 if (ret <= 0) goto end;
487 s->state=SSL3_ST_CR_FINISHED_A;
491 case SSL3_ST_CR_CERT_STATUS_A:
492 case SSL3_ST_CR_CERT_STATUS_B:
493 ret=ssl3_get_cert_status(s);
494 if (ret <= 0) goto end;
495 s->state=SSL3_ST_CR_KEY_EXCH_A;
500 case SSL3_ST_CR_FINISHED_A:
501 case SSL3_ST_CR_FINISHED_B:
503 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
504 SSL3_ST_CR_FINISHED_B);
505 if (ret <= 0) goto end;
508 s->state=SSL3_ST_CW_CHANGE_A;
514 case SSL3_ST_CW_FLUSH:
515 /* number of bytes to be flushed */
516 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
519 s->rwstate=SSL_WRITING;
520 num1=BIO_flush(s->wbio);
521 if (num1 <= 0) { ret= -1; goto end; }
522 s->rwstate=SSL_NOTHING;
525 s->state=s->s3->tmp.next_state;
529 /* clean a few things up */
530 ssl3_cleanup_key_block(s);
532 if (s->init_buf != NULL)
534 BUF_MEM_free(s->init_buf);
538 /* If we are not 'joining' the last two packets,
539 * remove the buffering now */
540 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
541 ssl_free_wbio_buffer(s);
542 /* else do it later in ssl3_write */
547 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
548 if (s->hit) s->ctx->stats.sess_hit++;
552 s->handshake_func=ssl3_connect;
553 s->ctx->stats.sess_connect_good++;
555 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
561 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
567 /* did we do anything */
568 if (!s->s3->tmp.reuse_message && !skip)
572 if ((ret=BIO_flush(s->wbio)) <= 0)
576 if ((cb != NULL) && (s->state != state))
580 cb(s,SSL_CB_CONNECT_LOOP,1);
591 cb(s,SSL_CB_CONNECT_EXIT,ret);
596 int ssl3_client_hello(SSL *s)
601 unsigned long Time,l;
602 #ifndef OPENSSL_NO_COMP
607 buf=(unsigned char *)s->init_buf->data;
608 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
610 if ((s->session == NULL) ||
611 (s->session->ssl_version != s->version) ||
612 (s->session->not_resumable))
614 if (!ssl_get_new_session(s,0))
617 /* else use the pre-loaded session */
619 p=s->s3->client_random;
620 Time=(unsigned long)time(NULL); /* Time */
622 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
625 /* Do the message type and length last */
628 *(p++)=s->version>>8;
629 *(p++)=s->version&0xff;
630 s->client_version=s->version;
633 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
640 i=s->session->session_id_length;
644 if (i > (int)sizeof(s->session->session_id))
646 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
649 memcpy(p,s->session->session_id,i);
653 /* Ciphers supported */
654 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
657 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
664 #ifdef OPENSSL_NO_COMP
668 if ((s->options & SSL_OP_NO_COMPRESSION)
669 || !s->ctx->comp_methods)
672 j=sk_SSL_COMP_num(s->ctx->comp_methods);
676 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
680 *(p++)=0; /* Add the NULL method */
682 #ifndef OPENSSL_NO_TLSEXT
684 if (ssl_prepare_clienthello_tlsext(s) <= 0)
686 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
689 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
691 SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
698 *(d++)=SSL3_MT_CLIENT_HELLO;
701 s->state=SSL3_ST_CW_CLNT_HELLO_B;
702 /* number of bytes to write */
707 /* SSL3_ST_CW_CLNT_HELLO_B */
708 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
713 int ssl3_get_server_hello(SSL *s)
715 STACK_OF(SSL_CIPHER) *sk;
721 #ifndef OPENSSL_NO_COMP
725 n=s->method->ssl_get_message(s,
726 SSL3_ST_CR_SRVR_HELLO_A,
727 SSL3_ST_CR_SRVR_HELLO_B,
732 if (!ok) return((int)n);
734 if ( SSL_version(s) == DTLS1_VERSION)
736 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
738 if ( s->d1->send_cookie == 0)
740 s->s3->tmp.reuse_message = 1;
743 else /* already sent a cookie */
745 al=SSL_AD_UNEXPECTED_MESSAGE;
746 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
752 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
754 al=SSL_AD_UNEXPECTED_MESSAGE;
755 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
759 d=p=(unsigned char *)s->init_msg;
761 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
763 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
764 s->version=(s->version&0xff00)|p[1];
765 al=SSL_AD_PROTOCOL_VERSION;
770 /* load the server hello data */
771 /* load the server random */
772 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
775 /* get the session-id */
778 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
780 al=SSL_AD_ILLEGAL_PARAMETER;
781 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
785 if (j != 0 && j == s->session->session_id_length
786 && memcmp(p,s->session->session_id,j) == 0)
788 if(s->sid_ctx_length != s->session->sid_ctx_length
789 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
791 /* actually a client application bug */
792 al=SSL_AD_ILLEGAL_PARAMETER;
793 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
798 else /* a miss or crap from the other end */
800 /* If we were trying for session-id reuse, make a new
801 * SSL_SESSION so we don't stuff up other people */
803 if (s->session->session_id_length > 0)
805 if (!ssl_get_new_session(s,0))
807 al=SSL_AD_INTERNAL_ERROR;
811 s->session->session_id_length=j;
812 memcpy(s->session->session_id,p,j); /* j could be 0 */
815 c=ssl_get_cipher_by_char(s,p);
819 al=SSL_AD_ILLEGAL_PARAMETER;
820 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
823 p+=ssl_put_cipher_by_char(s,NULL,NULL);
825 sk=ssl_get_ciphers_by_id(s);
826 i=sk_SSL_CIPHER_find(sk,c);
829 /* we did not say we would use this cipher */
830 al=SSL_AD_ILLEGAL_PARAMETER;
831 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
835 /* Depending on the session caching (internal/external), the cipher
836 and/or cipher_id values may not be set. Make sure that
837 cipher_id is set and use it for comparison. */
838 if (s->session->cipher)
839 s->session->cipher_id = s->session->cipher->id;
840 if (s->hit && (s->session->cipher_id != c->id))
843 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
845 al=SSL_AD_ILLEGAL_PARAMETER;
846 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
850 s->s3->tmp.new_cipher=c;
851 ssl3_digest_cached_records(s);
853 /* lets get the compression algorithm */
855 #ifdef OPENSSL_NO_COMP
858 al=SSL_AD_ILLEGAL_PARAMETER;
859 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
864 if ((j == 0) || (s->options & SSL_OP_NO_COMPRESSION))
867 comp=ssl3_comp_find(s->ctx->comp_methods,j);
869 if ((j != 0) && (comp == NULL))
871 al=SSL_AD_ILLEGAL_PARAMETER;
872 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
877 s->s3->tmp.new_compression=comp;
881 #ifndef OPENSSL_NO_TLSEXT
883 if (s->version > SSL3_VERSION)
885 if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
887 /* 'al' set by ssl_parse_serverhello_tlsext */
888 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
891 if (ssl_check_serverhello_tlsext(s) <= 0)
893 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
901 /* wrong packet length */
902 al=SSL_AD_DECODE_ERROR;
903 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
909 ssl3_send_alert(s,SSL3_AL_FATAL,al);
914 int ssl3_get_server_certificate(SSL *s)
917 unsigned long n,nc,llen,l;
919 const unsigned char *q,*p;
921 STACK_OF(X509) *sk=NULL;
924 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
926 n=s->method->ssl_get_message(s,
933 if (!ok) return((int)n);
935 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
937 s->s3->tmp.reuse_message=1;
941 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
943 al=SSL_AD_UNEXPECTED_MESSAGE;
944 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
947 p=d=(unsigned char *)s->init_msg;
949 if ((sk=sk_X509_new_null()) == NULL)
951 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
958 al=SSL_AD_DECODE_ERROR;
959 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
962 for (nc=0; nc<llen; )
967 al=SSL_AD_DECODE_ERROR;
968 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
973 x=d2i_X509(NULL,&q,l);
976 al=SSL_AD_BAD_CERTIFICATE;
977 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
982 al=SSL_AD_DECODE_ERROR;
983 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
986 if (!sk_X509_push(sk,x))
988 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
996 i=ssl_verify_cert_chain(s,sk);
997 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
998 #ifndef OPENSSL_NO_KRB5
999 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1000 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1001 #endif /* OPENSSL_NO_KRB5 */
1004 al=ssl_verify_alarm_type(s->verify_result);
1005 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
1008 ERR_clear_error(); /* but we keep s->verify_result */
1010 sc=ssl_sess_cert_new();
1011 if (sc == NULL) goto err;
1013 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1014 s->session->sess_cert=sc;
1017 /* Inconsistency alert: cert_chain does include the peer's
1018 * certificate, which we don't include in s3_srvr.c */
1019 x=sk_X509_value(sk,0);
1021 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1023 pkey=X509_get_pubkey(x);
1025 /* VRS: allow null cert if auth == KRB5 */
1026 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1027 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1031 printf("pkey,x = %p, %p\n", pkey,x);
1032 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
1033 printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name,
1034 s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1035 #endif /* KSSL_DEBUG */
1037 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
1041 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1042 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1046 i=ssl_cert_type(x,pkey);
1047 if (need_cert && i < 0)
1051 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1052 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1058 sc->peer_cert_type=i;
1059 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1060 /* Why would the following ever happen?
1061 * We just created sc a couple of lines ago. */
1062 if (sc->peer_pkeys[i].x509 != NULL)
1063 X509_free(sc->peer_pkeys[i].x509);
1064 sc->peer_pkeys[i].x509=x;
1065 sc->peer_key= &(sc->peer_pkeys[i]);
1067 if (s->session->peer != NULL)
1068 X509_free(s->session->peer);
1069 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1074 sc->peer_cert_type=i;
1077 if (s->session->peer != NULL)
1078 X509_free(s->session->peer);
1079 s->session->peer=NULL;
1081 s->session->verify_result = s->verify_result;
1089 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1092 EVP_PKEY_free(pkey);
1094 sk_X509_pop_free(sk,X509_free);
1098 int ssl3_get_key_exchange(SSL *s)
1100 #ifndef OPENSSL_NO_RSA
1101 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
1104 unsigned char *param,*p;
1105 int al,i,j,param_len,ok;
1107 EVP_PKEY *pkey=NULL;
1108 #ifndef OPENSSL_NO_RSA
1111 #ifndef OPENSSL_NO_DH
1114 #ifndef OPENSSL_NO_ECDH
1115 EC_KEY *ecdh = NULL;
1116 BN_CTX *bn_ctx = NULL;
1117 EC_POINT *srvr_ecpoint = NULL;
1119 int encoded_pt_len = 0;
1122 /* use same message size as in ssl3_get_certificate_request()
1123 * as ServerKeyExchange message may be skipped */
1124 n=s->method->ssl_get_message(s,
1125 SSL3_ST_CR_KEY_EXCH_A,
1126 SSL3_ST_CR_KEY_EXCH_B,
1130 if (!ok) return((int)n);
1132 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1134 #ifndef OPENSSL_NO_PSK
1135 /* In plain PSK ciphersuite, ServerKeyExchange can be
1136 omitted if no identity hint is sent. Set
1137 session->sess_cert anyway to avoid problems
1139 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
1141 s->session->sess_cert=ssl_sess_cert_new();
1142 if (s->ctx->psk_identity_hint)
1143 OPENSSL_free(s->ctx->psk_identity_hint);
1144 s->ctx->psk_identity_hint = NULL;
1147 s->s3->tmp.reuse_message=1;
1151 param=p=(unsigned char *)s->init_msg;
1152 if (s->session->sess_cert != NULL)
1154 #ifndef OPENSSL_NO_RSA
1155 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1157 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1158 s->session->sess_cert->peer_rsa_tmp=NULL;
1161 #ifndef OPENSSL_NO_DH
1162 if (s->session->sess_cert->peer_dh_tmp)
1164 DH_free(s->session->sess_cert->peer_dh_tmp);
1165 s->session->sess_cert->peer_dh_tmp=NULL;
1168 #ifndef OPENSSL_NO_ECDH
1169 if (s->session->sess_cert->peer_ecdh_tmp)
1171 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1172 s->session->sess_cert->peer_ecdh_tmp=NULL;
1178 s->session->sess_cert=ssl_sess_cert_new();
1182 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1183 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1184 EVP_MD_CTX_init(&md_ctx);
1186 #ifndef OPENSSL_NO_PSK
1187 if (alg_k & SSL_kPSK)
1189 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
1191 al=SSL_AD_HANDSHAKE_FAILURE;
1194 /* Store PSK identity hint for later use, hint is used
1195 * in ssl3_send_client_key_exchange. Assume that the
1196 * maximum length of a PSK identity hint can be as
1197 * long as the maximum length of a PSK identity. */
1198 if (i > PSK_MAX_IDENTITY_LEN)
1200 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1201 SSL_R_DATA_LENGTH_TOO_LONG);
1206 al=SSL_AD_DECODE_ERROR;
1207 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1208 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1211 /* If received PSK identity hint contains NULL
1212 * characters, the hint is truncated from the first
1213 * NULL. p may not be ending with NULL, so create a
1214 * NULL-terminated string. */
1215 memcpy(tmp_id_hint, p, i);
1216 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
1217 if (s->ctx->psk_identity_hint != NULL)
1218 OPENSSL_free(s->ctx->psk_identity_hint);
1219 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1220 if (s->ctx->psk_identity_hint == NULL)
1222 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1230 #endif /* !OPENSSL_NO_PSK */
1231 #ifndef OPENSSL_NO_RSA
1232 if (alg_k & SSL_kRSA)
1234 if ((rsa=RSA_new()) == NULL)
1236 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1243 al=SSL_AD_DECODE_ERROR;
1244 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
1247 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1249 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1258 al=SSL_AD_DECODE_ERROR;
1259 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
1262 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1264 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1270 /* this should be because we are using an export cipher */
1271 if (alg_a & SSL_aRSA)
1272 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1275 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1278 s->session->sess_cert->peer_rsa_tmp=rsa;
1281 #else /* OPENSSL_NO_RSA */
1285 #ifndef OPENSSL_NO_DH
1286 else if (alg_k & SSL_kEDH)
1288 if ((dh=DH_new()) == NULL)
1290 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
1297 al=SSL_AD_DECODE_ERROR;
1298 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
1301 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1303 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1312 al=SSL_AD_DECODE_ERROR;
1313 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
1316 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1318 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1327 al=SSL_AD_DECODE_ERROR;
1328 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
1331 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1333 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1339 #ifndef OPENSSL_NO_RSA
1340 if (alg_a & SSL_aRSA)
1341 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1346 #ifndef OPENSSL_NO_DSA
1347 else if (alg_a & SSL_aDSS)
1348 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1350 /* else anonymous DH, so no certificate or pkey. */
1352 s->session->sess_cert->peer_dh_tmp=dh;
1355 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1357 al=SSL_AD_ILLEGAL_PARAMETER;
1358 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1361 #endif /* !OPENSSL_NO_DH */
1363 #ifndef OPENSSL_NO_ECDH
1364 else if (alg_k & SSL_kEECDH)
1367 const EC_GROUP *group;
1369 if ((ecdh=EC_KEY_new()) == NULL)
1371 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1375 /* Extract elliptic curve parameters and the
1376 * server's ephemeral ECDH public key.
1377 * Keep accumulating lengths of various components in
1378 * param_len and make sure it never exceeds n.
1381 /* XXX: For now we only support named (not generic) curves
1382 * and the ECParameters in this case is just three bytes.
1385 if ((param_len > n) ||
1386 (*p != NAMED_CURVE_TYPE) ||
1387 ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0))
1389 al=SSL_AD_INTERNAL_ERROR;
1390 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1394 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1397 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1400 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1402 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1405 EC_GROUP_free(ngroup);
1407 group = EC_KEY_get0_group(ecdh);
1409 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1410 (EC_GROUP_get_degree(group) > 163))
1412 al=SSL_AD_EXPORT_RESTRICTION;
1413 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1419 /* Next, get the encoded ECPoint */
1420 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1421 ((bn_ctx = BN_CTX_new()) == NULL))
1423 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1427 encoded_pt_len = *p; /* length of encoded point */
1429 param_len += (1 + encoded_pt_len);
1430 if ((param_len > n) ||
1431 (EC_POINT_oct2point(group, srvr_ecpoint,
1432 p, encoded_pt_len, bn_ctx) == 0))
1434 al=SSL_AD_DECODE_ERROR;
1435 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
1442 /* The ECC/TLS specification does not mention
1443 * the use of DSA to sign ECParameters in the server
1444 * key exchange message. We do support RSA and ECDSA.
1447 #ifndef OPENSSL_NO_RSA
1448 else if (alg_a & SSL_aRSA)
1449 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1451 #ifndef OPENSSL_NO_ECDSA
1452 else if (alg_a & SSL_aECDSA)
1453 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1455 /* else anonymous ECDH, so no certificate or pkey. */
1456 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1457 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1459 BN_CTX_free(bn_ctx);
1460 EC_POINT_free(srvr_ecpoint);
1461 srvr_ecpoint = NULL;
1465 al=SSL_AD_UNEXPECTED_MESSAGE;
1466 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
1469 #endif /* !OPENSSL_NO_ECDH */
1472 /* p points to the next byte, there are 'n' bytes left */
1474 /* if it was signed, check the signature */
1479 j=EVP_PKEY_size(pkey);
1481 if ((i != n) || (n > j) || (n <= 0))
1483 /* wrong packet length */
1484 al=SSL_AD_DECODE_ERROR;
1485 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1489 #ifndef OPENSSL_NO_RSA
1490 if (pkey->type == EVP_PKEY_RSA)
1496 for (num=2; num > 0; num--)
1498 EVP_DigestInit_ex(&md_ctx,(num == 2)
1499 ?s->ctx->md5:s->ctx->sha1, NULL);
1500 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1501 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1502 EVP_DigestUpdate(&md_ctx,param,param_len);
1503 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1507 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1511 al=SSL_AD_DECRYPT_ERROR;
1512 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1518 al=SSL_AD_DECRYPT_ERROR;
1519 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1525 #ifndef OPENSSL_NO_DSA
1526 if (pkey->type == EVP_PKEY_DSA)
1529 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
1530 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1531 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1532 EVP_VerifyUpdate(&md_ctx,param,param_len);
1533 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1536 al=SSL_AD_DECRYPT_ERROR;
1537 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1543 #ifndef OPENSSL_NO_ECDSA
1544 if (pkey->type == EVP_PKEY_EC)
1546 /* let's do ECDSA */
1547 EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL);
1548 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1549 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1550 EVP_VerifyUpdate(&md_ctx,param,param_len);
1551 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1554 al=SSL_AD_DECRYPT_ERROR;
1555 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1562 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1568 if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK))
1569 /* aNULL or kPSK do not need public keys */
1571 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1574 /* still data left over */
1577 al=SSL_AD_DECODE_ERROR;
1578 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1582 EVP_PKEY_free(pkey);
1583 EVP_MD_CTX_cleanup(&md_ctx);
1586 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1588 EVP_PKEY_free(pkey);
1589 #ifndef OPENSSL_NO_RSA
1593 #ifndef OPENSSL_NO_DH
1597 #ifndef OPENSSL_NO_ECDH
1598 BN_CTX_free(bn_ctx);
1599 EC_POINT_free(srvr_ecpoint);
1603 EVP_MD_CTX_cleanup(&md_ctx);
1607 int ssl3_get_certificate_request(SSL *s)
1610 unsigned long n,nc,l;
1611 unsigned int llen,ctype_num,i;
1613 const unsigned char *p,*q;
1615 STACK_OF(X509_NAME) *ca_sk=NULL;
1617 n=s->method->ssl_get_message(s,
1618 SSL3_ST_CR_CERT_REQ_A,
1619 SSL3_ST_CR_CERT_REQ_B,
1624 if (!ok) return((int)n);
1626 s->s3->tmp.cert_req=0;
1628 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1630 s->s3->tmp.reuse_message=1;
1634 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1636 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1637 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1641 /* TLS does not like anon-DH with client cert */
1642 if (s->version > SSL3_VERSION)
1644 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1646 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1647 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1652 p=d=(unsigned char *)s->init_msg;
1654 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1656 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1660 /* get the certificate types */
1662 if (ctype_num > SSL3_CT_NUMBER)
1663 ctype_num=SSL3_CT_NUMBER;
1664 for (i=0; i<ctype_num; i++)
1665 s->s3->tmp.ctype[i]= p[i];
1668 /* get the CA RDNs */
1673 out=fopen("/tmp/vsign.der","w");
1674 fwrite(p,1,llen,out);
1679 if ((llen+ctype_num+2+1) != n)
1681 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1682 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1686 for (nc=0; nc<llen; )
1689 if ((l+nc+2) > llen)
1691 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1692 goto cont; /* netscape bugs */
1693 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1694 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1700 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1702 /* If netscape tolerance is on, ignore errors */
1703 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1707 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1708 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1715 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1716 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1719 if (!sk_X509_NAME_push(ca_sk,xn))
1721 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1735 /* we should setup a certificate to return.... */
1736 s->s3->tmp.cert_req=1;
1737 s->s3->tmp.ctype_num=ctype_num;
1738 if (s->s3->tmp.ca_names != NULL)
1739 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1740 s->s3->tmp.ca_names=ca_sk;
1745 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1749 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1751 return(X509_NAME_cmp(*a,*b));
1753 #ifndef OPENSSL_NO_TLSEXT
1754 int ssl3_get_new_session_ticket(SSL *s)
1756 int ok,al,ret=0, ticklen;
1758 const unsigned char *p;
1761 n=s->method->ssl_get_message(s,
1762 SSL3_ST_CR_SESSION_TICKET_A,
1763 SSL3_ST_CR_SESSION_TICKET_B,
1771 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
1773 s->s3->tmp.reuse_message=1;
1776 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
1778 al=SSL_AD_UNEXPECTED_MESSAGE;
1779 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE);
1784 /* need at least ticket_lifetime_hint + ticket length */
1785 al = SSL3_AL_FATAL,SSL_AD_DECODE_ERROR;
1786 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
1789 p=d=(unsigned char *)s->init_msg;
1790 n2l(p, s->session->tlsext_tick_lifetime_hint);
1792 /* ticket_lifetime_hint + ticket_length + ticket */
1793 if (ticklen + 6 != n)
1795 al = SSL3_AL_FATAL,SSL_AD_DECODE_ERROR;
1796 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
1799 if (s->session->tlsext_tick)
1801 OPENSSL_free(s->session->tlsext_tick);
1802 s->session->tlsext_ticklen = 0;
1804 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1805 if (!s->session->tlsext_tick)
1807 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE);
1810 memcpy(s->session->tlsext_tick, p, ticklen);
1811 s->session->tlsext_ticklen = ticklen;
1816 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1821 int ssl3_get_cert_status(SSL *s)
1824 unsigned long resplen;
1826 const unsigned char *p;
1828 n=s->method->ssl_get_message(s,
1829 SSL3_ST_CR_CERT_STATUS_A,
1830 SSL3_ST_CR_CERT_STATUS_B,
1831 SSL3_MT_CERTIFICATE_STATUS,
1835 if (!ok) return((int)n);
1838 /* need at least status type + length */
1839 al = SSL_AD_DECODE_ERROR;
1840 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
1843 p = (unsigned char *)s->init_msg;
1844 if (*p++ != TLSEXT_STATUSTYPE_ocsp)
1846 al = SSL_AD_DECODE_ERROR;
1847 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE);
1851 if (resplen + 4 != n)
1853 al = SSL_AD_DECODE_ERROR;
1854 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
1857 if (s->tlsext_ocsp_resp)
1858 OPENSSL_free(s->tlsext_ocsp_resp);
1859 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
1860 if (!s->tlsext_ocsp_resp)
1862 al = SSL_AD_INTERNAL_ERROR;
1863 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
1866 s->tlsext_ocsp_resplen = resplen;
1867 if (s->ctx->tlsext_status_cb)
1870 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1873 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1874 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_RESPONSE);
1879 al = SSL_AD_INTERNAL_ERROR;
1880 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
1886 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1891 int ssl3_get_server_done(SSL *s)
1896 n=s->method->ssl_get_message(s,
1897 SSL3_ST_CR_SRVR_DONE_A,
1898 SSL3_ST_CR_SRVR_DONE_B,
1899 SSL3_MT_SERVER_DONE,
1900 30, /* should be very small, like 0 :-) */
1903 if (!ok) return((int)n);
1906 /* should contain no data */
1907 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1908 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1916 int ssl3_send_client_key_exchange(SSL *s)
1918 unsigned char *p,*d;
1920 unsigned long alg_k;
1921 #ifndef OPENSSL_NO_RSA
1923 EVP_PKEY *pkey=NULL;
1925 #ifndef OPENSSL_NO_KRB5
1927 #endif /* OPENSSL_NO_KRB5 */
1928 #ifndef OPENSSL_NO_ECDH
1929 EC_KEY *clnt_ecdh = NULL;
1930 const EC_POINT *srvr_ecpoint = NULL;
1931 EVP_PKEY *srvr_pub_pkey = NULL;
1932 unsigned char *encodedPoint = NULL;
1933 int encoded_pt_len = 0;
1934 BN_CTX * bn_ctx = NULL;
1937 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1939 d=(unsigned char *)s->init_buf->data;
1942 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1944 /* Fool emacs indentation */
1946 #ifndef OPENSSL_NO_RSA
1947 else if (alg_k & SSL_kRSA)
1950 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1952 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1953 rsa=s->session->sess_cert->peer_rsa_tmp;
1956 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1957 if ((pkey == NULL) ||
1958 (pkey->type != EVP_PKEY_RSA) ||
1959 (pkey->pkey.rsa == NULL))
1961 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1965 EVP_PKEY_free(pkey);
1968 tmp_buf[0]=s->client_version>>8;
1969 tmp_buf[1]=s->client_version&0xff;
1970 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
1973 s->session->master_key_length=sizeof tmp_buf;
1976 /* Fix buf for TLS and beyond */
1977 if (s->version > SSL3_VERSION)
1979 n=RSA_public_encrypt(sizeof tmp_buf,
1980 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1982 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1983 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1987 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1991 /* Fix buf for TLS and beyond */
1992 if (s->version > SSL3_VERSION)
1998 s->session->master_key_length=
1999 s->method->ssl3_enc->generate_master_secret(s,
2000 s->session->master_key,
2001 tmp_buf,sizeof tmp_buf);
2002 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2005 #ifndef OPENSSL_NO_KRB5
2006 else if (alg_k & SSL_kKRB5)
2008 krb5_error_code krb5rc;
2009 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2010 /* krb5_data krb5_ap_req; */
2011 krb5_data *enc_ticket;
2012 krb5_data authenticator, *authp = NULL;
2013 EVP_CIPHER_CTX ciph_ctx;
2014 EVP_CIPHER *enc = NULL;
2015 unsigned char iv[EVP_MAX_IV_LENGTH];
2016 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2017 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
2018 + EVP_MAX_IV_LENGTH];
2019 int padl, outl = sizeof(epms);
2021 EVP_CIPHER_CTX_init(&ciph_ctx);
2024 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
2026 #endif /* KSSL_DEBUG */
2030 if (KRB5SENDAUTH) authp = &authenticator;
2031 #endif /* KRB5SENDAUTH */
2033 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
2035 enc = kssl_map_enc(kssl_ctx->enctype);
2040 printf("kssl_cget_tkt rtn %d\n", krb5rc);
2041 if (krb5rc && kssl_err.text)
2042 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
2044 #endif /* KSSL_DEBUG */
2048 ssl3_send_alert(s,SSL3_AL_FATAL,
2049 SSL_AD_HANDSHAKE_FAILURE);
2050 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2055 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
2056 ** in place of RFC 2712 KerberosWrapper, as in:
2058 ** Send ticket (copy to *p, set n = length)
2059 ** n = krb5_ap_req.length;
2060 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2061 ** if (krb5_ap_req.data)
2062 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2064 ** Now using real RFC 2712 KerberosWrapper
2065 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2066 ** Note: 2712 "opaque" types are here replaced
2067 ** with a 2-byte length followed by the value.
2069 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2070 ** Where "xx xx" = length bytes. Shown here with
2071 ** optional authenticator omitted.
2074 /* KerberosWrapper.Ticket */
2075 s2n(enc_ticket->length,p);
2076 memcpy(p, enc_ticket->data, enc_ticket->length);
2077 p+= enc_ticket->length;
2078 n = enc_ticket->length + 2;
2080 /* KerberosWrapper.Authenticator */
2081 if (authp && authp->length)
2083 s2n(authp->length,p);
2084 memcpy(p, authp->data, authp->length);
2086 n+= authp->length + 2;
2094 s2n(0,p);/* null authenticator length */
2098 tmp_buf[0]=s->client_version>>8;
2099 tmp_buf[1]=s->client_version&0xff;
2100 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2103 /* 20010420 VRS. Tried it this way; failed.
2104 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2105 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2106 ** kssl_ctx->length);
2107 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2110 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2111 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
2113 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
2115 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
2117 if (outl > sizeof epms)
2119 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2122 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2124 /* KerberosWrapper.EncryptedPreMasterSecret */
2126 memcpy(p, epms, outl);
2130 s->session->master_key_length=
2131 s->method->ssl3_enc->generate_master_secret(s,
2132 s->session->master_key,
2133 tmp_buf, sizeof tmp_buf);
2135 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2136 OPENSSL_cleanse(epms, outl);
2139 #ifndef OPENSSL_NO_DH
2140 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2142 DH *dh_srvr,*dh_clnt;
2144 if (s->session->sess_cert->peer_dh_tmp != NULL)
2145 dh_srvr=s->session->sess_cert->peer_dh_tmp;
2148 /* we get them from the cert */
2149 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2150 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
2154 /* generate a new random key */
2155 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2157 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2160 if (!DH_generate_key(dh_clnt))
2162 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2166 /* use the 'p' output buffer for the DH key, but
2167 * make sure to clear it out afterwards */
2169 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2173 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2177 /* generate master key from the result */
2178 s->session->master_key_length=
2179 s->method->ssl3_enc->generate_master_secret(s,
2180 s->session->master_key,p,n);
2184 /* send off the data */
2185 n=BN_num_bytes(dh_clnt->pub_key);
2187 BN_bn2bin(dh_clnt->pub_key,p);
2192 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2196 #ifndef OPENSSL_NO_ECDH
2197 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2199 const EC_GROUP *srvr_group = NULL;
2201 int ecdh_clnt_cert = 0;
2204 /* Did we send out the client's
2205 * ECDH share for use in premaster
2206 * computation as part of client certificate?
2207 * If so, set ecdh_clnt_cert to 1.
2209 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2211 /* XXX: For now, we do not support client
2212 * authentication using ECDH certificates.
2213 * To add such support, one needs to add
2214 * code that checks for appropriate
2215 * conditions and sets ecdh_clnt_cert to 1.
2216 * For example, the cert have an ECC
2217 * key on the same curve as the server's
2218 * and the key should be authorized for
2221 * One also needs to add code in ssl3_connect
2222 * to skip sending the certificate verify
2225 * if ((s->cert->key->privatekey != NULL) &&
2226 * (s->cert->key->privatekey->type ==
2227 * EVP_PKEY_EC) && ...)
2228 * ecdh_clnt_cert = 1;
2232 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2234 tkey = s->session->sess_cert->peer_ecdh_tmp;
2238 /* Get the Server Public Key from Cert */
2239 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2240 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2241 if ((srvr_pub_pkey == NULL) ||
2242 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2243 (srvr_pub_pkey->pkey.ec == NULL))
2245 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2246 ERR_R_INTERNAL_ERROR);
2250 tkey = srvr_pub_pkey->pkey.ec;
2253 srvr_group = EC_KEY_get0_group(tkey);
2254 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2256 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2258 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2259 ERR_R_INTERNAL_ERROR);
2263 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2265 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2269 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2271 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
2276 /* Reuse key info from our certificate
2277 * We only need our private key to perform
2278 * the ECDH computation.
2280 const BIGNUM *priv_key;
2281 tkey = s->cert->key->privatekey->pkey.ec;
2282 priv_key = EC_KEY_get0_private_key(tkey);
2283 if (priv_key == NULL)
2285 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2288 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2290 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
2296 /* Generate a new ECDH key pair */
2297 if (!(EC_KEY_generate_key(clnt_ecdh)))
2299 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2304 /* use the 'p' output buffer for the ECDH key, but
2305 * make sure to clear it out afterwards
2308 field_size = EC_GROUP_get_degree(srvr_group);
2309 if (field_size <= 0)
2311 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2315 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2318 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2323 /* generate master key from the result */
2324 s->session->master_key_length = s->method->ssl3_enc \
2325 -> generate_master_secret(s,
2326 s->session->master_key,
2329 memset(p, 0, n); /* clean up */
2333 /* Send empty client key exch message */
2338 /* First check the size of encoding and
2339 * allocate memory accordingly.
2342 EC_POINT_point2oct(srvr_group,
2343 EC_KEY_get0_public_key(clnt_ecdh),
2344 POINT_CONVERSION_UNCOMPRESSED,
2347 encodedPoint = (unsigned char *)
2348 OPENSSL_malloc(encoded_pt_len *
2349 sizeof(unsigned char));
2350 bn_ctx = BN_CTX_new();
2351 if ((encodedPoint == NULL) ||
2354 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2358 /* Encode the public key */
2359 n = EC_POINT_point2oct(srvr_group,
2360 EC_KEY_get0_public_key(clnt_ecdh),
2361 POINT_CONVERSION_UNCOMPRESSED,
2362 encodedPoint, encoded_pt_len, bn_ctx);
2364 *p = n; /* length of encoded point */
2365 /* Encoded point will be copied here */
2367 /* copy the point */
2368 memcpy((unsigned char *)p, encodedPoint, n);
2369 /* increment n to account for length field */
2373 /* Free allocated memory */
2374 BN_CTX_free(bn_ctx);
2375 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2376 if (clnt_ecdh != NULL)
2377 EC_KEY_free(clnt_ecdh);
2378 EVP_PKEY_free(srvr_pub_pkey);
2380 #endif /* !OPENSSL_NO_ECDH */
2381 else if (alg_k & SSL_kGOST)
2383 /* GOST key exchange message creation */
2384 EVP_PKEY_CTX *pkey_ctx;
2387 unsigned int md_len;
2389 unsigned char premaster_secret[32],shared_ukm[32];
2390 EVP_MD_CTX *ukm_hash;
2393 /* Get server sertificate PKEY and create ctx from it */
2394 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
2396 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
2398 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2402 pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);
2403 /* If we have send a certificate, and certificate key
2405 * parameters match those of server certificate, use
2406 * certificate key for key exchange
2409 /* Otherwise, generate ephemeral key pair */
2411 EVP_PKEY_encrypt_init(pkey_ctx);
2412 /* Generate session key */
2413 RAND_bytes(premaster_secret,32);
2414 /* If we have client certificate, use its secret as peer key */
2415 if (s->cert->key->privatekey) {
2416 if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <0) {
2417 /* If there was an error - just ignore it. Ephemeral key
2422 /* Set flag "client cert key is used for key
2426 /* Compute shared IV and store it in algorithm-specific
2428 ukm_hash = EVP_MD_CTX_create();
2429 EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94));
2430 EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE);
2431 EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE);
2432 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2433 EVP_MD_CTX_destroy(ukm_hash);
2434 if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV,
2436 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2440 /* Make GOST keytransport blob message */
2441 /*Encapsulate it into sequence */
2442 *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2445 if (EVP_PKEY_encrypt(pkey_ctx,(unsigned char *)p+1,&msglen,premaster_secret,32)<0) {
2446 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2450 *(p++)= msglen & 0xff;
2452 EVP_PKEY_CTX_free(pkey_ctx);
2453 s->session->master_key_length=
2454 s->method->ssl3_enc->generate_master_secret(s,
2455 s->session->master_key,premaster_secret,32);
2456 EVP_PKEY_free(pub_key);
2459 #ifndef OPENSSL_NO_PSK
2460 else if (alg_k & SSL_kPSK)
2462 char identity[PSK_MAX_IDENTITY_LEN];
2463 unsigned char *t = NULL;
2464 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
2465 unsigned int pre_ms_len = 0, psk_len = 0;
2469 if (s->psk_client_callback == NULL)
2471 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2472 SSL_R_PSK_NO_CLIENT_CB);
2476 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2477 identity, PSK_MAX_IDENTITY_LEN,
2478 psk_or_pre_ms, sizeof(psk_or_pre_ms));
2479 if (psk_len > PSK_MAX_PSK_LEN)
2481 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2482 ERR_R_INTERNAL_ERROR);
2485 else if (psk_len == 0)
2487 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2488 SSL_R_PSK_IDENTITY_NOT_FOUND);
2492 /* create PSK pre_master_secret */
2493 pre_ms_len = 2+psk_len+2+psk_len;
2495 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
2497 memset(t, 0, psk_len);
2501 if (s->session->psk_identity_hint != NULL)
2502 OPENSSL_free(s->session->psk_identity_hint);
2503 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2504 if (s->ctx->psk_identity_hint != NULL &&
2505 s->session->psk_identity_hint == NULL)
2507 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2508 ERR_R_MALLOC_FAILURE);
2512 if (s->session->psk_identity != NULL)
2513 OPENSSL_free(s->session->psk_identity);
2514 s->session->psk_identity = BUF_strdup(identity);
2515 if (s->session->psk_identity == NULL)
2517 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2518 ERR_R_MALLOC_FAILURE);
2522 s->session->master_key_length =
2523 s->method->ssl3_enc->generate_master_secret(s,
2524 s->session->master_key,
2525 psk_or_pre_ms, pre_ms_len);
2526 n = strlen(identity);
2528 memcpy(p, identity, n);
2532 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2533 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2536 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2543 ssl3_send_alert(s, SSL3_AL_FATAL,
2544 SSL_AD_HANDSHAKE_FAILURE);
2545 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2546 ERR_R_INTERNAL_ERROR);
2550 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
2553 s->state=SSL3_ST_CW_KEY_EXCH_B;
2554 /* number of bytes to write */
2559 /* SSL3_ST_CW_KEY_EXCH_B */
2560 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2562 #ifndef OPENSSL_NO_ECDH
2563 BN_CTX_free(bn_ctx);
2564 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2565 if (clnt_ecdh != NULL)
2566 EC_KEY_free(clnt_ecdh);
2567 EVP_PKEY_free(srvr_pub_pkey);
2572 int ssl3_send_client_verify(SSL *s)
2574 unsigned char *p,*d;
2575 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
2577 EVP_PKEY_CTX *pctx=NULL;
2578 #ifndef OPENSSL_NO_RSA
2582 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
2586 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2588 d=(unsigned char *)s->init_buf->data;
2590 pkey=s->cert->key->privatekey;
2591 /* Create context from key and test if sha1 is allowed as digest */
2592 pctx = EVP_PKEY_CTX_new(pkey,NULL);
2593 EVP_PKEY_sign_init(pctx);
2594 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0)
2596 s->method->ssl3_enc->cert_verify_mac(s,
2598 &(data[MD5_DIGEST_LENGTH]));
2604 #ifndef OPENSSL_NO_RSA
2605 if (pkey->type == EVP_PKEY_RSA)
2607 s->method->ssl3_enc->cert_verify_mac(s,
2610 if (RSA_sign(NID_md5_sha1, data,
2611 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
2612 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
2614 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
2622 #ifndef OPENSSL_NO_DSA
2623 if (pkey->type == EVP_PKEY_DSA)
2625 if (!DSA_sign(pkey->save_type,
2626 &(data[MD5_DIGEST_LENGTH]),
2627 SHA_DIGEST_LENGTH,&(p[2]),
2628 (unsigned int *)&j,pkey->pkey.dsa))
2630 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
2638 #ifndef OPENSSL_NO_ECDSA
2639 if (pkey->type == EVP_PKEY_EC)
2641 if (!ECDSA_sign(pkey->save_type,
2642 &(data[MD5_DIGEST_LENGTH]),
2643 SHA_DIGEST_LENGTH,&(p[2]),
2644 (unsigned int *)&j,pkey->pkey.ec))
2646 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2655 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
2657 unsigned char signbuf[64];
2660 s->method->ssl3_enc->cert_verify_mac(s,
2661 NID_id_GostR3411_94,
2663 if (!EVP_PKEY_sign(pctx,signbuf,&sigsize,data,32)) {
2664 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2665 ERR_R_INTERNAL_ERROR);
2668 for (i=63,j=0; i>=0; j++, i--) {
2676 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
2679 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
2682 s->state=SSL3_ST_CW_CERT_VRFY_B;
2683 s->init_num=(int)n+4;
2686 EVP_PKEY_CTX_free(pctx);
2687 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2689 EVP_PKEY_CTX_free(pctx);
2693 int ssl3_send_client_certificate(SSL *s)
2696 EVP_PKEY *pkey=NULL;
2700 if (s->state == SSL3_ST_CW_CERT_A)
2702 if ((s->cert == NULL) ||
2703 (s->cert->key->x509 == NULL) ||
2704 (s->cert->key->privatekey == NULL))
2705 s->state=SSL3_ST_CW_CERT_B;
2707 s->state=SSL3_ST_CW_CERT_C;
2710 /* We need to get a client cert */
2711 if (s->state == SSL3_ST_CW_CERT_B)
2713 /* If we get an error, we need to
2714 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2715 * We then get retied later */
2717 if (s->ctx->client_cert_cb != NULL)
2718 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
2721 s->rwstate=SSL_X509_LOOKUP;
2724 s->rwstate=SSL_NOTHING;
2725 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2727 s->state=SSL3_ST_CW_CERT_B;
2728 if ( !SSL_use_certificate(s,x509) ||
2729 !SSL_use_PrivateKey(s,pkey))
2735 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2738 if (x509 != NULL) X509_free(x509);
2739 if (pkey != NULL) EVP_PKEY_free(pkey);
2742 if (s->version == SSL3_VERSION)
2744 s->s3->tmp.cert_req=0;
2745 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2750 s->s3->tmp.cert_req=2;
2754 /* Ok, we have a cert */
2755 s->state=SSL3_ST_CW_CERT_C;
2758 if (s->state == SSL3_ST_CW_CERT_C)
2760 s->state=SSL3_ST_CW_CERT_D;
2761 l=ssl3_output_cert_chain(s,
2762 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
2766 /* SSL3_ST_CW_CERT_D */
2767 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2770 #define has_bits(i,m) (((i)&(m)) == (m))
2772 int ssl3_check_cert_and_algorithm(SSL *s)
2776 EVP_PKEY *pkey=NULL;
2778 #ifndef OPENSSL_NO_RSA
2781 #ifndef OPENSSL_NO_DH
2785 sc=s->session->sess_cert;
2788 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
2792 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2793 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2795 /* we don't have a certificate */
2796 if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK))
2799 #ifndef OPENSSL_NO_RSA
2800 rsa=s->session->sess_cert->peer_rsa_tmp;
2802 #ifndef OPENSSL_NO_DH
2803 dh=s->session->sess_cert->peer_dh_tmp;
2806 /* This is the passed certificate */
2808 idx=sc->peer_cert_type;
2809 #ifndef OPENSSL_NO_ECDH
2810 if (idx == SSL_PKEY_ECC)
2812 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2813 s->s3->tmp.new_cipher) == 0)
2814 { /* check failed */
2815 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
2824 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2825 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2826 EVP_PKEY_free(pkey);
2829 /* Check that we have a certificate if we require one */
2830 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2832 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
2835 #ifndef OPENSSL_NO_DSA
2836 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
2838 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
2842 #ifndef OPENSSL_NO_RSA
2843 if ((alg_k & SSL_kRSA) &&
2844 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2846 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2850 #ifndef OPENSSL_NO_DH
2851 if ((alg_k & SSL_kEDH) &&
2852 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2854 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
2857 else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
2859 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
2862 #ifndef OPENSSL_NO_DSA
2863 else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
2865 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
2871 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
2873 #ifndef OPENSSL_NO_RSA
2874 if (alg_k & SSL_kRSA)
2877 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2879 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
2885 #ifndef OPENSSL_NO_DH
2886 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2889 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2891 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
2898 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2904 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2909 /* Check to see if handshake is full or resumed. Usually this is just a
2910 * case of checking to see if a cache hit has occurred. In the case of
2911 * session tickets we have to check the next message to be sure.
2914 #ifndef OPENSSL_NO_TLSEXT
2915 static int ssl3_check_finished(SSL *s)
2919 if (!s->session->tlsext_tick)
2921 /* this function is called when we really expect a Certificate
2922 * message, so permit appropriate message length */
2923 n=s->method->ssl_get_message(s,
2929 if (!ok) return((int)n);
2930 s->s3->tmp.reuse_message = 1;
2931 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
2932 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))