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 <openssl/crypto.h>
153 #include "ssl_locl.h"
154 #include "kssl_lcl.h"
155 #include <openssl/buffer.h>
156 #include <openssl/rand.h>
157 #include <openssl/objects.h>
158 #include <openssl/evp.h>
159 #include <openssl/md5.h>
160 #ifndef OPENSSL_NO_DH
161 #include <openssl/dh.h>
163 #include <openssl/bn.h>
164 #ifndef OPENSSL_NO_ENGINE
165 #include <openssl/engine.h>
168 static const SSL_METHOD *ssl3_get_client_method(int ver);
169 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
171 static const SSL_METHOD *ssl3_get_client_method(int ver)
173 if (ver == SSL3_VERSION)
174 return(SSLv3_client_method());
179 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
180 ssl_undefined_function,
182 ssl3_get_client_method)
184 int ssl3_connect(SSL *s)
187 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 if (s->tlsext_ticket_expected)
299 s->state=SSL3_ST_CR_SESSION_TICKET_A;
301 s->state=SSL3_ST_CR_FINISHED_A;
306 /* Check if it is anon DH/ECDH */
308 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
309 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
311 ret=ssl3_get_server_certificate(s);
312 if (ret <= 0) goto end;
313 #ifndef OPENSSL_NO_TLSEXT
314 if (s->tlsext_status_expected)
315 s->state=SSL3_ST_CR_CERT_STATUS_A;
317 s->state=SSL3_ST_CR_KEY_EXCH_A;
322 s->state=SSL3_ST_CR_KEY_EXCH_A;
329 s->state=SSL3_ST_CR_KEY_EXCH_A;
334 case SSL3_ST_CR_KEY_EXCH_A:
335 case SSL3_ST_CR_KEY_EXCH_B:
336 ret=ssl3_get_key_exchange(s);
337 if (ret <= 0) goto end;
338 s->state=SSL3_ST_CR_CERT_REQ_A;
341 /* at this point we check that we have the
342 * required stuff from the server */
343 if (!ssl3_check_cert_and_algorithm(s))
350 case SSL3_ST_CR_CERT_REQ_A:
351 case SSL3_ST_CR_CERT_REQ_B:
352 ret=ssl3_get_certificate_request(s);
353 if (ret <= 0) goto end;
354 s->state=SSL3_ST_CR_SRVR_DONE_A;
358 case SSL3_ST_CR_SRVR_DONE_A:
359 case SSL3_ST_CR_SRVR_DONE_B:
360 ret=ssl3_get_server_done(s);
361 if (ret <= 0) goto end;
362 if (s->s3->tmp.cert_req)
363 s->state=SSL3_ST_CW_CERT_A;
365 s->state=SSL3_ST_CW_KEY_EXCH_A;
370 case SSL3_ST_CW_CERT_A:
371 case SSL3_ST_CW_CERT_B:
372 case SSL3_ST_CW_CERT_C:
373 case SSL3_ST_CW_CERT_D:
374 ret=ssl3_send_client_certificate(s);
375 if (ret <= 0) goto end;
376 s->state=SSL3_ST_CW_KEY_EXCH_A;
380 case SSL3_ST_CW_KEY_EXCH_A:
381 case SSL3_ST_CW_KEY_EXCH_B:
382 ret=ssl3_send_client_key_exchange(s);
383 if (ret <= 0) goto end;
384 /* EAY EAY EAY need to check for DH fix cert
386 /* For TLS, cert_req is set to 2, so a cert chain
387 * of nothing is sent, but no verify packet is sent */
388 /* XXX: For now, we do not support client
389 * authentication in ECDH cipher suites with
390 * ECDH (rather than ECDSA) certificates.
391 * We need to skip the certificate verify
392 * message when client's ECDH public key is sent
393 * inside the client certificate.
395 if (s->s3->tmp.cert_req == 1)
397 s->state=SSL3_ST_CW_CERT_VRFY_A;
401 s->state=SSL3_ST_CW_CHANGE_A;
402 s->s3->change_cipher_spec=0;
404 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
406 s->state=SSL3_ST_CW_CHANGE_A;
407 s->s3->change_cipher_spec=0;
413 case SSL3_ST_CW_CERT_VRFY_A:
414 case SSL3_ST_CW_CERT_VRFY_B:
415 ret=ssl3_send_client_verify(s);
416 if (ret <= 0) goto end;
417 s->state=SSL3_ST_CW_CHANGE_A;
419 s->s3->change_cipher_spec=0;
422 case SSL3_ST_CW_CHANGE_A:
423 case SSL3_ST_CW_CHANGE_B:
424 ret=ssl3_send_change_cipher_spec(s,
425 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
426 if (ret <= 0) goto end;
428 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
429 s->state=SSL3_ST_CW_FINISHED_A;
431 if (s->next_proto_negotiated)
432 s->state=SSL3_ST_CW_NEXT_PROTO_A;
434 s->state=SSL3_ST_CW_FINISHED_A;
438 s->session->cipher=s->s3->tmp.new_cipher;
439 #ifdef OPENSSL_NO_COMP
440 s->session->compress_meth=0;
442 if (s->s3->tmp.new_compression == NULL)
443 s->session->compress_meth=0;
445 s->session->compress_meth=
446 s->s3->tmp.new_compression->id;
448 if (!s->method->ssl3_enc->setup_key_block(s))
454 if (!s->method->ssl3_enc->change_cipher_state(s,
455 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
463 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
464 case SSL3_ST_CW_NEXT_PROTO_A:
465 case SSL3_ST_CW_NEXT_PROTO_B:
466 ret=ssl3_send_next_proto(s);
467 if (ret <= 0) goto end;
468 s->state=SSL3_ST_CW_FINISHED_A;
472 case SSL3_ST_CW_FINISHED_A:
473 case SSL3_ST_CW_FINISHED_B:
474 ret=ssl3_send_finished(s,
475 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
476 s->method->ssl3_enc->client_finished_label,
477 s->method->ssl3_enc->client_finished_label_len);
478 if (ret <= 0) goto end;
479 s->state=SSL3_ST_CW_FLUSH;
482 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
485 s->s3->tmp.next_state=SSL_ST_OK;
486 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
489 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
490 s->s3->delay_buf_pop_ret=0;
495 #ifndef OPENSSL_NO_TLSEXT
496 /* Allow NewSessionTicket if ticket expected */
497 if (s->tlsext_ticket_expected)
498 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
502 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
507 #ifndef OPENSSL_NO_TLSEXT
508 case SSL3_ST_CR_SESSION_TICKET_A:
509 case SSL3_ST_CR_SESSION_TICKET_B:
510 ret=ssl3_get_new_session_ticket(s);
511 if (ret <= 0) goto end;
512 s->state=SSL3_ST_CR_FINISHED_A;
516 case SSL3_ST_CR_CERT_STATUS_A:
517 case SSL3_ST_CR_CERT_STATUS_B:
518 ret=ssl3_get_cert_status(s);
519 if (ret <= 0) goto end;
520 s->state=SSL3_ST_CR_KEY_EXCH_A;
525 case SSL3_ST_CR_FINISHED_A:
526 case SSL3_ST_CR_FINISHED_B:
528 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
529 SSL3_ST_CR_FINISHED_B);
530 if (ret <= 0) goto end;
533 s->state=SSL3_ST_CW_CHANGE_A;
539 case SSL3_ST_CW_FLUSH:
540 s->rwstate=SSL_WRITING;
541 if (BIO_flush(s->wbio) <= 0)
546 s->rwstate=SSL_NOTHING;
547 s->state=s->s3->tmp.next_state;
551 /* clean a few things up */
552 ssl3_cleanup_key_block(s);
554 if (s->init_buf != NULL)
556 BUF_MEM_free(s->init_buf);
560 /* If we are not 'joining' the last two packets,
561 * remove the buffering now */
562 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
563 ssl_free_wbio_buffer(s);
564 /* else do it later in ssl3_write */
570 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
571 if (s->hit) s->ctx->stats.sess_hit++;
575 s->handshake_func=ssl3_connect;
576 s->ctx->stats.sess_connect_good++;
578 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
584 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
590 /* did we do anything */
591 if (!s->s3->tmp.reuse_message && !skip)
595 if ((ret=BIO_flush(s->wbio)) <= 0)
599 if ((cb != NULL) && (s->state != state))
603 cb(s,SSL_CB_CONNECT_LOOP,1);
614 cb(s,SSL_CB_CONNECT_EXIT,ret);
619 int ssl3_client_hello(SSL *s)
624 unsigned long Time,l;
625 #ifndef OPENSSL_NO_COMP
630 buf=(unsigned char *)s->init_buf->data;
631 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
633 SSL_SESSION *sess = s->session;
634 if ((sess == NULL) ||
635 (sess->ssl_version != s->version) ||
636 #ifdef OPENSSL_NO_TLSEXT
637 !sess->session_id_length ||
639 (!sess->session_id_length && !sess->tlsext_tick) ||
641 (sess->not_resumable))
643 if (!ssl_get_new_session(s,0))
646 /* else use the pre-loaded session */
648 p=s->s3->client_random;
649 Time=(unsigned long)time(NULL); /* Time */
651 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
654 /* Do the message type and length last */
657 *(p++)=s->version>>8;
658 *(p++)=s->version&0xff;
659 s->client_version=s->version;
662 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
669 i=s->session->session_id_length;
673 if (i > (int)sizeof(s->session->session_id))
675 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
678 memcpy(p,s->session->session_id,i);
682 /* Ciphers supported */
683 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
686 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
693 #ifdef OPENSSL_NO_COMP
697 if ((s->options & SSL_OP_NO_COMPRESSION)
698 || !s->ctx->comp_methods)
701 j=sk_SSL_COMP_num(s->ctx->comp_methods);
705 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
709 *(p++)=0; /* Add the NULL method */
711 #ifndef OPENSSL_NO_TLSEXT
713 if (ssl_prepare_clienthello_tlsext(s) <= 0)
715 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
718 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
720 SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
727 *(d++)=SSL3_MT_CLIENT_HELLO;
730 s->state=SSL3_ST_CW_CLNT_HELLO_B;
731 /* number of bytes to write */
736 /* SSL3_ST_CW_CLNT_HELLO_B */
737 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
742 int ssl3_get_server_hello(SSL *s)
744 STACK_OF(SSL_CIPHER) *sk;
750 #ifndef OPENSSL_NO_COMP
754 n=s->method->ssl_get_message(s,
755 SSL3_ST_CR_SRVR_HELLO_A,
756 SSL3_ST_CR_SRVR_HELLO_B,
761 if (!ok) return((int)n);
763 if ( SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
765 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
767 if ( s->d1->send_cookie == 0)
769 s->s3->tmp.reuse_message = 1;
772 else /* already sent a cookie */
774 al=SSL_AD_UNEXPECTED_MESSAGE;
775 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
781 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
783 al=SSL_AD_UNEXPECTED_MESSAGE;
784 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
788 d=p=(unsigned char *)s->init_msg;
790 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
792 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
793 s->version=(s->version&0xff00)|p[1];
794 al=SSL_AD_PROTOCOL_VERSION;
799 /* load the server hello data */
800 /* load the server random */
801 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
804 /* get the session-id */
807 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
809 al=SSL_AD_ILLEGAL_PARAMETER;
810 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
814 #ifndef OPENSSL_NO_TLSEXT
815 /* check if we want to resume the session based on external pre-shared secret */
816 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
818 SSL_CIPHER *pref_cipher=NULL;
819 s->session->master_key_length=sizeof(s->session->master_key);
820 if (s->tls_session_secret_cb(s, s->session->master_key,
821 &s->session->master_key_length,
823 s->tls_session_secret_cb_arg))
825 s->session->cipher = pref_cipher ?
826 pref_cipher : ssl_get_cipher_by_char(s, p+j);
829 #endif /* OPENSSL_NO_TLSEXT */
831 if (j != 0 && j == s->session->session_id_length
832 && memcmp(p,s->session->session_id,j) == 0)
834 if(s->sid_ctx_length != s->session->sid_ctx_length
835 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
837 /* actually a client application bug */
838 al=SSL_AD_ILLEGAL_PARAMETER;
839 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
844 else /* a miss or crap from the other end */
846 /* If we were trying for session-id reuse, make a new
847 * SSL_SESSION so we don't stuff up other people */
849 if (s->session->session_id_length > 0)
851 if (!ssl_get_new_session(s,0))
853 al=SSL_AD_INTERNAL_ERROR;
857 s->session->session_id_length=j;
858 memcpy(s->session->session_id,p,j); /* j could be 0 */
861 c=ssl_get_cipher_by_char(s,p);
865 al=SSL_AD_ILLEGAL_PARAMETER;
866 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
869 p+=ssl_put_cipher_by_char(s,NULL,NULL);
871 sk=ssl_get_ciphers_by_id(s);
872 i=sk_SSL_CIPHER_find(sk,c);
875 /* we did not say we would use this cipher */
876 al=SSL_AD_ILLEGAL_PARAMETER;
877 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
881 /* Depending on the session caching (internal/external), the cipher
882 and/or cipher_id values may not be set. Make sure that
883 cipher_id is set and use it for comparison. */
884 if (s->session->cipher)
885 s->session->cipher_id = s->session->cipher->id;
886 if (s->hit && (s->session->cipher_id != c->id))
888 /* Workaround is now obsolete */
891 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
894 al=SSL_AD_ILLEGAL_PARAMETER;
895 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
899 s->s3->tmp.new_cipher=c;
900 if (!ssl3_digest_cached_records(s))
903 /* lets get the compression algorithm */
905 #ifdef OPENSSL_NO_COMP
908 al=SSL_AD_ILLEGAL_PARAMETER;
909 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
912 /* If compression is disabled we'd better not try to resume a session
915 if (s->session->compress_meth != 0)
917 al=SSL_AD_INTERNAL_ERROR;
918 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
923 if (s->hit && j != s->session->compress_meth)
925 al=SSL_AD_ILLEGAL_PARAMETER;
926 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
931 else if (s->options & SSL_OP_NO_COMPRESSION)
933 al=SSL_AD_ILLEGAL_PARAMETER;
934 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED);
938 comp=ssl3_comp_find(s->ctx->comp_methods,j);
940 if ((j != 0) && (comp == NULL))
942 al=SSL_AD_ILLEGAL_PARAMETER;
943 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
948 s->s3->tmp.new_compression=comp;
952 #ifndef OPENSSL_NO_TLSEXT
954 if (s->version >= SSL3_VERSION)
956 if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
958 /* 'al' set by ssl_parse_serverhello_tlsext */
959 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
962 if (ssl_check_serverhello_tlsext(s) <= 0)
964 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
972 /* wrong packet length */
973 al=SSL_AD_DECODE_ERROR;
974 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
980 ssl3_send_alert(s,SSL3_AL_FATAL,al);
985 int ssl3_get_server_certificate(SSL *s)
988 unsigned long n,nc,llen,l;
990 const unsigned char *q,*p;
992 STACK_OF(X509) *sk=NULL;
995 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
997 n=s->method->ssl_get_message(s,
1004 if (!ok) return((int)n);
1006 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1007 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1008 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)))
1010 s->s3->tmp.reuse_message=1;
1014 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1016 al=SSL_AD_UNEXPECTED_MESSAGE;
1017 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
1020 p=d=(unsigned char *)s->init_msg;
1022 if ((sk=sk_X509_new_null()) == NULL)
1024 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1031 al=SSL_AD_DECODE_ERROR;
1032 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1035 for (nc=0; nc<llen; )
1038 if ((l+nc+3) > llen)
1040 al=SSL_AD_DECODE_ERROR;
1041 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1046 x=d2i_X509(NULL,&q,l);
1049 al=SSL_AD_BAD_CERTIFICATE;
1050 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
1055 al=SSL_AD_DECODE_ERROR;
1056 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1059 if (!sk_X509_push(sk,x))
1061 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1069 i=ssl_verify_cert_chain(s,sk);
1070 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1071 #ifndef OPENSSL_NO_KRB5
1072 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1073 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1074 #endif /* OPENSSL_NO_KRB5 */
1077 al=ssl_verify_alarm_type(s->verify_result);
1078 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
1081 ERR_clear_error(); /* but we keep s->verify_result */
1083 sc=ssl_sess_cert_new();
1084 if (sc == NULL) goto err;
1086 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1087 s->session->sess_cert=sc;
1090 /* Inconsistency alert: cert_chain does include the peer's
1091 * certificate, which we don't include in s3_srvr.c */
1092 x=sk_X509_value(sk,0);
1094 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1096 pkey=X509_get_pubkey(x);
1098 /* VRS: allow null cert if auth == KRB5 */
1099 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1100 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1104 printf("pkey,x = %p, %p\n", pkey,x);
1105 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
1106 printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name,
1107 s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1108 #endif /* KSSL_DEBUG */
1110 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
1114 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1115 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1119 i=ssl_cert_type(x,pkey);
1120 if (need_cert && i < 0)
1124 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1125 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1131 sc->peer_cert_type=i;
1132 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1133 /* Why would the following ever happen?
1134 * We just created sc a couple of lines ago. */
1135 if (sc->peer_pkeys[i].x509 != NULL)
1136 X509_free(sc->peer_pkeys[i].x509);
1137 sc->peer_pkeys[i].x509=x;
1138 sc->peer_key= &(sc->peer_pkeys[i]);
1140 if (s->session->peer != NULL)
1141 X509_free(s->session->peer);
1142 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1147 sc->peer_cert_type=i;
1150 if (s->session->peer != NULL)
1151 X509_free(s->session->peer);
1152 s->session->peer=NULL;
1154 s->session->verify_result = s->verify_result;
1162 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1165 EVP_PKEY_free(pkey);
1167 sk_X509_pop_free(sk,X509_free);
1171 int ssl3_get_key_exchange(SSL *s)
1173 #ifndef OPENSSL_NO_RSA
1174 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
1177 unsigned char *param,*p;
1178 int al,i,j,param_len,ok;
1180 EVP_PKEY *pkey=NULL;
1181 #ifndef OPENSSL_NO_RSA
1184 #ifndef OPENSSL_NO_DH
1187 #ifndef OPENSSL_NO_ECDH
1188 EC_KEY *ecdh = NULL;
1189 BN_CTX *bn_ctx = NULL;
1190 EC_POINT *srvr_ecpoint = NULL;
1192 int encoded_pt_len = 0;
1195 /* use same message size as in ssl3_get_certificate_request()
1196 * as ServerKeyExchange message may be skipped */
1197 n=s->method->ssl_get_message(s,
1198 SSL3_ST_CR_KEY_EXCH_A,
1199 SSL3_ST_CR_KEY_EXCH_B,
1203 if (!ok) return((int)n);
1205 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1207 #ifndef OPENSSL_NO_PSK
1208 /* In plain PSK ciphersuite, ServerKeyExchange can be
1209 omitted if no identity hint is sent. Set
1210 session->sess_cert anyway to avoid problems
1212 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
1214 s->session->sess_cert=ssl_sess_cert_new();
1215 if (s->ctx->psk_identity_hint)
1216 OPENSSL_free(s->ctx->psk_identity_hint);
1217 s->ctx->psk_identity_hint = NULL;
1220 s->s3->tmp.reuse_message=1;
1224 param=p=(unsigned char *)s->init_msg;
1225 if (s->session->sess_cert != NULL)
1227 #ifndef OPENSSL_NO_RSA
1228 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1230 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1231 s->session->sess_cert->peer_rsa_tmp=NULL;
1234 #ifndef OPENSSL_NO_DH
1235 if (s->session->sess_cert->peer_dh_tmp)
1237 DH_free(s->session->sess_cert->peer_dh_tmp);
1238 s->session->sess_cert->peer_dh_tmp=NULL;
1241 #ifndef OPENSSL_NO_ECDH
1242 if (s->session->sess_cert->peer_ecdh_tmp)
1244 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1245 s->session->sess_cert->peer_ecdh_tmp=NULL;
1251 s->session->sess_cert=ssl_sess_cert_new();
1255 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1256 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1257 EVP_MD_CTX_init(&md_ctx);
1259 #ifndef OPENSSL_NO_PSK
1260 if (alg_k & SSL_kPSK)
1262 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
1264 al=SSL_AD_HANDSHAKE_FAILURE;
1267 /* Store PSK identity hint for later use, hint is used
1268 * in ssl3_send_client_key_exchange. Assume that the
1269 * maximum length of a PSK identity hint can be as
1270 * long as the maximum length of a PSK identity. */
1271 if (i > PSK_MAX_IDENTITY_LEN)
1273 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1274 SSL_R_DATA_LENGTH_TOO_LONG);
1279 al=SSL_AD_DECODE_ERROR;
1280 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1281 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1284 /* If received PSK identity hint contains NULL
1285 * characters, the hint is truncated from the first
1286 * NULL. p may not be ending with NULL, so create a
1287 * NULL-terminated string. */
1288 memcpy(tmp_id_hint, p, i);
1289 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
1290 if (s->ctx->psk_identity_hint != NULL)
1291 OPENSSL_free(s->ctx->psk_identity_hint);
1292 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1293 if (s->ctx->psk_identity_hint == NULL)
1295 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1303 #endif /* !OPENSSL_NO_PSK */
1304 #ifndef OPENSSL_NO_RSA
1305 if (alg_k & SSL_kRSA)
1307 if ((rsa=RSA_new()) == NULL)
1309 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1316 al=SSL_AD_DECODE_ERROR;
1317 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
1320 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1322 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1331 al=SSL_AD_DECODE_ERROR;
1332 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
1335 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1337 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1343 /* this should be because we are using an export cipher */
1344 if (alg_a & SSL_aRSA)
1345 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1348 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1351 s->session->sess_cert->peer_rsa_tmp=rsa;
1354 #else /* OPENSSL_NO_RSA */
1358 #ifndef OPENSSL_NO_DH
1359 else if (alg_k & SSL_kEDH)
1361 if ((dh=DH_new()) == NULL)
1363 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
1370 al=SSL_AD_DECODE_ERROR;
1371 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
1374 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1376 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1385 al=SSL_AD_DECODE_ERROR;
1386 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
1389 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1391 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1400 al=SSL_AD_DECODE_ERROR;
1401 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
1404 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1406 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1412 #ifndef OPENSSL_NO_RSA
1413 if (alg_a & SSL_aRSA)
1414 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1419 #ifndef OPENSSL_NO_DSA
1420 else if (alg_a & SSL_aDSS)
1421 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1423 /* else anonymous DH, so no certificate or pkey. */
1425 s->session->sess_cert->peer_dh_tmp=dh;
1428 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1430 al=SSL_AD_ILLEGAL_PARAMETER;
1431 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1434 #endif /* !OPENSSL_NO_DH */
1436 #ifndef OPENSSL_NO_ECDH
1437 else if (alg_k & SSL_kEECDH)
1440 const EC_GROUP *group;
1442 if ((ecdh=EC_KEY_new()) == NULL)
1444 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1448 /* Extract elliptic curve parameters and the
1449 * server's ephemeral ECDH public key.
1450 * Keep accumulating lengths of various components in
1451 * param_len and make sure it never exceeds n.
1454 /* XXX: For now we only support named (not generic) curves
1455 * and the ECParameters in this case is just three bytes.
1458 if ((param_len > n) ||
1459 (*p != NAMED_CURVE_TYPE) ||
1460 ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0))
1462 al=SSL_AD_INTERNAL_ERROR;
1463 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1467 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1470 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1473 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1475 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1478 EC_GROUP_free(ngroup);
1480 group = EC_KEY_get0_group(ecdh);
1482 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1483 (EC_GROUP_get_degree(group) > 163))
1485 al=SSL_AD_EXPORT_RESTRICTION;
1486 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1492 /* Next, get the encoded ECPoint */
1493 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1494 ((bn_ctx = BN_CTX_new()) == NULL))
1496 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1500 encoded_pt_len = *p; /* length of encoded point */
1502 param_len += (1 + encoded_pt_len);
1503 if ((param_len > n) ||
1504 (EC_POINT_oct2point(group, srvr_ecpoint,
1505 p, encoded_pt_len, bn_ctx) == 0))
1507 al=SSL_AD_DECODE_ERROR;
1508 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
1515 /* The ECC/TLS specification does not mention
1516 * the use of DSA to sign ECParameters in the server
1517 * key exchange message. We do support RSA and ECDSA.
1520 #ifndef OPENSSL_NO_RSA
1521 else if (alg_a & SSL_aRSA)
1522 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1524 #ifndef OPENSSL_NO_ECDSA
1525 else if (alg_a & SSL_aECDSA)
1526 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1528 /* else anonymous ECDH, so no certificate or pkey. */
1529 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1530 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1532 BN_CTX_free(bn_ctx);
1534 EC_POINT_free(srvr_ecpoint);
1535 srvr_ecpoint = NULL;
1539 al=SSL_AD_UNEXPECTED_MESSAGE;
1540 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
1543 #endif /* !OPENSSL_NO_ECDH */
1546 /* p points to the next byte, there are 'n' bytes left */
1548 /* if it was signed, check the signature */
1553 j=EVP_PKEY_size(pkey);
1555 if ((i != n) || (n > j) || (n <= 0))
1557 /* wrong packet length */
1558 al=SSL_AD_DECODE_ERROR;
1559 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1563 #ifndef OPENSSL_NO_RSA
1564 if (pkey->type == EVP_PKEY_RSA)
1570 for (num=2; num > 0; num--)
1572 EVP_DigestInit_ex(&md_ctx,(num == 2)
1573 ?s->ctx->md5:s->ctx->sha1, NULL);
1574 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1575 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1576 EVP_DigestUpdate(&md_ctx,param,param_len);
1577 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1581 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1585 al=SSL_AD_DECRYPT_ERROR;
1586 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1592 al=SSL_AD_DECRYPT_ERROR;
1593 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1599 #ifndef OPENSSL_NO_DSA
1600 if (pkey->type == EVP_PKEY_DSA)
1603 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
1604 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1605 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1606 EVP_VerifyUpdate(&md_ctx,param,param_len);
1607 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1610 al=SSL_AD_DECRYPT_ERROR;
1611 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1617 #ifndef OPENSSL_NO_ECDSA
1618 if (pkey->type == EVP_PKEY_EC)
1620 /* let's do ECDSA */
1621 EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL);
1622 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1623 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1624 EVP_VerifyUpdate(&md_ctx,param,param_len);
1625 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1628 al=SSL_AD_DECRYPT_ERROR;
1629 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1636 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1642 if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK))
1643 /* aNULL or kPSK do not need public keys */
1645 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1648 /* still data left over */
1651 al=SSL_AD_DECODE_ERROR;
1652 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1656 EVP_PKEY_free(pkey);
1657 EVP_MD_CTX_cleanup(&md_ctx);
1660 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1662 EVP_PKEY_free(pkey);
1663 #ifndef OPENSSL_NO_RSA
1667 #ifndef OPENSSL_NO_DH
1671 #ifndef OPENSSL_NO_ECDH
1672 BN_CTX_free(bn_ctx);
1673 EC_POINT_free(srvr_ecpoint);
1677 EVP_MD_CTX_cleanup(&md_ctx);
1681 int ssl3_get_certificate_request(SSL *s)
1684 unsigned long n,nc,l;
1685 unsigned int llen,ctype_num,i;
1687 const unsigned char *p,*q;
1689 STACK_OF(X509_NAME) *ca_sk=NULL;
1691 n=s->method->ssl_get_message(s,
1692 SSL3_ST_CR_CERT_REQ_A,
1693 SSL3_ST_CR_CERT_REQ_B,
1698 if (!ok) return((int)n);
1700 s->s3->tmp.cert_req=0;
1702 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1704 s->s3->tmp.reuse_message=1;
1708 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1710 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1711 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1715 /* TLS does not like anon-DH with client cert */
1716 if (s->version > SSL3_VERSION)
1718 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1720 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1721 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1726 p=d=(unsigned char *)s->init_msg;
1728 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1730 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1734 /* get the certificate types */
1736 if (ctype_num > SSL3_CT_NUMBER)
1737 ctype_num=SSL3_CT_NUMBER;
1738 for (i=0; i<ctype_num; i++)
1739 s->s3->tmp.ctype[i]= p[i];
1742 /* get the CA RDNs */
1747 out=fopen("/tmp/vsign.der","w");
1748 fwrite(p,1,llen,out);
1753 if ((llen+ctype_num+2+1) != n)
1755 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1756 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1760 for (nc=0; nc<llen; )
1763 if ((l+nc+2) > llen)
1765 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1766 goto cont; /* netscape bugs */
1767 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1768 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1774 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1776 /* If netscape tolerance is on, ignore errors */
1777 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1781 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1782 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1789 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1790 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1793 if (!sk_X509_NAME_push(ca_sk,xn))
1795 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1809 /* we should setup a certificate to return.... */
1810 s->s3->tmp.cert_req=1;
1811 s->s3->tmp.ctype_num=ctype_num;
1812 if (s->s3->tmp.ca_names != NULL)
1813 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1814 s->s3->tmp.ca_names=ca_sk;
1819 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1823 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1825 return(X509_NAME_cmp(*a,*b));
1827 #ifndef OPENSSL_NO_TLSEXT
1828 int ssl3_get_new_session_ticket(SSL *s)
1830 int ok,al,ret=0, ticklen;
1832 const unsigned char *p;
1835 n=s->method->ssl_get_message(s,
1836 SSL3_ST_CR_SESSION_TICKET_A,
1837 SSL3_ST_CR_SESSION_TICKET_B,
1845 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
1847 s->s3->tmp.reuse_message=1;
1850 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
1852 al=SSL_AD_UNEXPECTED_MESSAGE;
1853 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE);
1858 /* need at least ticket_lifetime_hint + ticket length */
1859 al = SSL3_AL_FATAL,SSL_AD_DECODE_ERROR;
1860 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
1864 p=d=(unsigned char *)s->init_msg;
1865 n2l(p, s->session->tlsext_tick_lifetime_hint);
1867 /* ticket_lifetime_hint + ticket_length + ticket */
1868 if (ticklen + 6 != n)
1870 al = SSL3_AL_FATAL,SSL_AD_DECODE_ERROR;
1871 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
1874 if (s->session->tlsext_tick)
1876 OPENSSL_free(s->session->tlsext_tick);
1877 s->session->tlsext_ticklen = 0;
1879 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1880 if (!s->session->tlsext_tick)
1882 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE);
1885 memcpy(s->session->tlsext_tick, p, ticklen);
1886 s->session->tlsext_ticklen = ticklen;
1887 /* There are two ways to detect a resumed ticket sesion.
1888 * One is to set an appropriate session ID and then the server
1889 * must return a match in ServerHello. This allows the normal
1890 * client session ID matching to work and we know much
1891 * earlier that the ticket has been accepted.
1893 * The other way is to set zero length session ID when the
1894 * ticket is presented and rely on the handshake to determine
1895 * session resumption.
1897 * We choose the former approach because this fits in with
1898 * assumptions elsewhere in OpenSSL. The session ID is set
1899 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1902 EVP_Digest(p, ticklen,
1903 s->session->session_id, &s->session->session_id_length,
1904 #ifndef OPENSSL_NO_SHA256
1905 EVP_sha256(), NULL);
1912 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1917 int ssl3_get_cert_status(SSL *s)
1920 unsigned long resplen,n;
1921 const unsigned char *p;
1923 n=s->method->ssl_get_message(s,
1924 SSL3_ST_CR_CERT_STATUS_A,
1925 SSL3_ST_CR_CERT_STATUS_B,
1926 SSL3_MT_CERTIFICATE_STATUS,
1930 if (!ok) return((int)n);
1933 /* need at least status type + length */
1934 al = SSL_AD_DECODE_ERROR;
1935 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
1938 p = (unsigned char *)s->init_msg;
1939 if (*p++ != TLSEXT_STATUSTYPE_ocsp)
1941 al = SSL_AD_DECODE_ERROR;
1942 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE);
1946 if (resplen + 4 != n)
1948 al = SSL_AD_DECODE_ERROR;
1949 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
1952 if (s->tlsext_ocsp_resp)
1953 OPENSSL_free(s->tlsext_ocsp_resp);
1954 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
1955 if (!s->tlsext_ocsp_resp)
1957 al = SSL_AD_INTERNAL_ERROR;
1958 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
1961 s->tlsext_ocsp_resplen = resplen;
1962 if (s->ctx->tlsext_status_cb)
1965 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1968 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1969 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_RESPONSE);
1974 al = SSL_AD_INTERNAL_ERROR;
1975 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
1981 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1986 int ssl3_get_server_done(SSL *s)
1991 n=s->method->ssl_get_message(s,
1992 SSL3_ST_CR_SRVR_DONE_A,
1993 SSL3_ST_CR_SRVR_DONE_B,
1994 SSL3_MT_SERVER_DONE,
1995 30, /* should be very small, like 0 :-) */
1998 if (!ok) return((int)n);
2001 /* should contain no data */
2002 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2003 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
2011 int ssl3_send_client_key_exchange(SSL *s)
2013 unsigned char *p,*d;
2015 unsigned long alg_k;
2016 #ifndef OPENSSL_NO_RSA
2018 EVP_PKEY *pkey=NULL;
2020 #ifndef OPENSSL_NO_KRB5
2022 #endif /* OPENSSL_NO_KRB5 */
2023 #ifndef OPENSSL_NO_ECDH
2024 EC_KEY *clnt_ecdh = NULL;
2025 const EC_POINT *srvr_ecpoint = NULL;
2026 EVP_PKEY *srvr_pub_pkey = NULL;
2027 unsigned char *encodedPoint = NULL;
2028 int encoded_pt_len = 0;
2029 BN_CTX * bn_ctx = NULL;
2032 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2034 d=(unsigned char *)s->init_buf->data;
2037 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2039 /* Fool emacs indentation */
2041 #ifndef OPENSSL_NO_RSA
2042 else if (alg_k & SSL_kRSA)
2045 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2047 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2048 rsa=s->session->sess_cert->peer_rsa_tmp;
2051 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2052 if ((pkey == NULL) ||
2053 (pkey->type != EVP_PKEY_RSA) ||
2054 (pkey->pkey.rsa == NULL))
2056 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
2060 EVP_PKEY_free(pkey);
2063 tmp_buf[0]=s->client_version>>8;
2064 tmp_buf[1]=s->client_version&0xff;
2065 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2068 s->session->master_key_length=sizeof tmp_buf;
2071 /* Fix buf for TLS and beyond */
2072 if (s->version > SSL3_VERSION)
2074 n=RSA_public_encrypt(sizeof tmp_buf,
2075 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
2077 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
2078 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
2082 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
2086 /* Fix buf for TLS and beyond */
2087 if (s->version > SSL3_VERSION)
2093 s->session->master_key_length=
2094 s->method->ssl3_enc->generate_master_secret(s,
2095 s->session->master_key,
2096 tmp_buf,sizeof tmp_buf);
2097 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2100 #ifndef OPENSSL_NO_KRB5
2101 else if (alg_k & SSL_kKRB5)
2103 krb5_error_code krb5rc;
2104 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2105 /* krb5_data krb5_ap_req; */
2106 krb5_data *enc_ticket;
2107 krb5_data authenticator, *authp = NULL;
2108 EVP_CIPHER_CTX ciph_ctx;
2109 const EVP_CIPHER *enc = NULL;
2110 unsigned char iv[EVP_MAX_IV_LENGTH];
2111 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2112 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
2113 + EVP_MAX_IV_LENGTH];
2114 int padl, outl = sizeof(epms);
2116 EVP_CIPHER_CTX_init(&ciph_ctx);
2119 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
2121 #endif /* KSSL_DEBUG */
2125 if (KRB5SENDAUTH) authp = &authenticator;
2126 #endif /* KRB5SENDAUTH */
2128 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
2130 enc = kssl_map_enc(kssl_ctx->enctype);
2135 printf("kssl_cget_tkt rtn %d\n", krb5rc);
2136 if (krb5rc && kssl_err.text)
2137 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
2139 #endif /* KSSL_DEBUG */
2143 ssl3_send_alert(s,SSL3_AL_FATAL,
2144 SSL_AD_HANDSHAKE_FAILURE);
2145 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2150 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
2151 ** in place of RFC 2712 KerberosWrapper, as in:
2153 ** Send ticket (copy to *p, set n = length)
2154 ** n = krb5_ap_req.length;
2155 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2156 ** if (krb5_ap_req.data)
2157 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2159 ** Now using real RFC 2712 KerberosWrapper
2160 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2161 ** Note: 2712 "opaque" types are here replaced
2162 ** with a 2-byte length followed by the value.
2164 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2165 ** Where "xx xx" = length bytes. Shown here with
2166 ** optional authenticator omitted.
2169 /* KerberosWrapper.Ticket */
2170 s2n(enc_ticket->length,p);
2171 memcpy(p, enc_ticket->data, enc_ticket->length);
2172 p+= enc_ticket->length;
2173 n = enc_ticket->length + 2;
2175 /* KerberosWrapper.Authenticator */
2176 if (authp && authp->length)
2178 s2n(authp->length,p);
2179 memcpy(p, authp->data, authp->length);
2181 n+= authp->length + 2;
2189 s2n(0,p);/* null authenticator length */
2193 tmp_buf[0]=s->client_version>>8;
2194 tmp_buf[1]=s->client_version&0xff;
2195 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2198 /* 20010420 VRS. Tried it this way; failed.
2199 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2200 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2201 ** kssl_ctx->length);
2202 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2205 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2206 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
2208 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
2210 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
2212 if (outl > (int)sizeof epms)
2214 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2217 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2219 /* KerberosWrapper.EncryptedPreMasterSecret */
2221 memcpy(p, epms, outl);
2225 s->session->master_key_length=
2226 s->method->ssl3_enc->generate_master_secret(s,
2227 s->session->master_key,
2228 tmp_buf, sizeof tmp_buf);
2230 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2231 OPENSSL_cleanse(epms, outl);
2234 #ifndef OPENSSL_NO_DH
2235 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2237 DH *dh_srvr,*dh_clnt;
2239 if (s->session->sess_cert == NULL)
2241 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2242 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
2246 if (s->session->sess_cert->peer_dh_tmp != NULL)
2247 dh_srvr=s->session->sess_cert->peer_dh_tmp;
2250 /* we get them from the cert */
2251 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2252 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
2256 /* generate a new random key */
2257 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2259 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2262 if (!DH_generate_key(dh_clnt))
2264 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2268 /* use the 'p' output buffer for the DH key, but
2269 * make sure to clear it out afterwards */
2271 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2275 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2279 /* generate master key from the result */
2280 s->session->master_key_length=
2281 s->method->ssl3_enc->generate_master_secret(s,
2282 s->session->master_key,p,n);
2286 /* send off the data */
2287 n=BN_num_bytes(dh_clnt->pub_key);
2289 BN_bn2bin(dh_clnt->pub_key,p);
2294 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2298 #ifndef OPENSSL_NO_ECDH
2299 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2301 const EC_GROUP *srvr_group = NULL;
2303 int ecdh_clnt_cert = 0;
2306 /* Did we send out the client's
2307 * ECDH share for use in premaster
2308 * computation as part of client certificate?
2309 * If so, set ecdh_clnt_cert to 1.
2311 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2313 /* XXX: For now, we do not support client
2314 * authentication using ECDH certificates.
2315 * To add such support, one needs to add
2316 * code that checks for appropriate
2317 * conditions and sets ecdh_clnt_cert to 1.
2318 * For example, the cert have an ECC
2319 * key on the same curve as the server's
2320 * and the key should be authorized for
2323 * One also needs to add code in ssl3_connect
2324 * to skip sending the certificate verify
2327 * if ((s->cert->key->privatekey != NULL) &&
2328 * (s->cert->key->privatekey->type ==
2329 * EVP_PKEY_EC) && ...)
2330 * ecdh_clnt_cert = 1;
2334 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2336 tkey = s->session->sess_cert->peer_ecdh_tmp;
2340 /* Get the Server Public Key from Cert */
2341 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2342 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2343 if ((srvr_pub_pkey == NULL) ||
2344 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2345 (srvr_pub_pkey->pkey.ec == NULL))
2347 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2348 ERR_R_INTERNAL_ERROR);
2352 tkey = srvr_pub_pkey->pkey.ec;
2355 srvr_group = EC_KEY_get0_group(tkey);
2356 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2358 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2360 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2361 ERR_R_INTERNAL_ERROR);
2365 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2367 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2371 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2373 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
2378 /* Reuse key info from our certificate
2379 * We only need our private key to perform
2380 * the ECDH computation.
2382 const BIGNUM *priv_key;
2383 tkey = s->cert->key->privatekey->pkey.ec;
2384 priv_key = EC_KEY_get0_private_key(tkey);
2385 if (priv_key == NULL)
2387 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2390 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2392 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
2398 /* Generate a new ECDH key pair */
2399 if (!(EC_KEY_generate_key(clnt_ecdh)))
2401 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2406 /* use the 'p' output buffer for the ECDH key, but
2407 * make sure to clear it out afterwards
2410 field_size = EC_GROUP_get_degree(srvr_group);
2411 if (field_size <= 0)
2413 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2417 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2420 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2425 /* generate master key from the result */
2426 s->session->master_key_length = s->method->ssl3_enc \
2427 -> generate_master_secret(s,
2428 s->session->master_key,
2431 memset(p, 0, n); /* clean up */
2435 /* Send empty client key exch message */
2440 /* First check the size of encoding and
2441 * allocate memory accordingly.
2444 EC_POINT_point2oct(srvr_group,
2445 EC_KEY_get0_public_key(clnt_ecdh),
2446 POINT_CONVERSION_UNCOMPRESSED,
2449 encodedPoint = (unsigned char *)
2450 OPENSSL_malloc(encoded_pt_len *
2451 sizeof(unsigned char));
2452 bn_ctx = BN_CTX_new();
2453 if ((encodedPoint == NULL) ||
2456 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2460 /* Encode the public key */
2461 n = EC_POINT_point2oct(srvr_group,
2462 EC_KEY_get0_public_key(clnt_ecdh),
2463 POINT_CONVERSION_UNCOMPRESSED,
2464 encodedPoint, encoded_pt_len, bn_ctx);
2466 *p = n; /* length of encoded point */
2467 /* Encoded point will be copied here */
2469 /* copy the point */
2470 memcpy((unsigned char *)p, encodedPoint, n);
2471 /* increment n to account for length field */
2475 /* Free allocated memory */
2476 BN_CTX_free(bn_ctx);
2477 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2478 if (clnt_ecdh != NULL)
2479 EC_KEY_free(clnt_ecdh);
2480 EVP_PKEY_free(srvr_pub_pkey);
2482 #endif /* !OPENSSL_NO_ECDH */
2483 else if (alg_k & SSL_kGOST)
2485 /* GOST key exchange message creation */
2486 EVP_PKEY_CTX *pkey_ctx;
2489 unsigned int md_len;
2491 unsigned char premaster_secret[32],shared_ukm[32], tmp[256];
2492 EVP_MD_CTX *ukm_hash;
2495 /* Get server sertificate PKEY and create ctx from it */
2496 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
2498 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
2500 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2504 pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);
2505 /* If we have send a certificate, and certificate key
2507 * parameters match those of server certificate, use
2508 * certificate key for key exchange
2511 /* Otherwise, generate ephemeral key pair */
2513 EVP_PKEY_encrypt_init(pkey_ctx);
2514 /* Generate session key */
2515 RAND_bytes(premaster_secret,32);
2516 /* If we have client certificate, use its secret as peer key */
2517 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2518 if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <=0) {
2519 /* If there was an error - just ignore it. Ephemeral key
2525 /* Compute shared IV and store it in algorithm-specific
2527 ukm_hash = EVP_MD_CTX_create();
2528 EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94));
2529 EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE);
2530 EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE);
2531 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2532 EVP_MD_CTX_destroy(ukm_hash);
2533 if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV,
2535 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2539 /* Make GOST keytransport blob message */
2540 /*Encapsulate it into sequence */
2541 *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2543 if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secret,32)<0) {
2544 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2551 *(p++)= msglen & 0xff;
2556 *(p++)= msglen & 0xff;
2559 memcpy(p, tmp, msglen);
2560 /* Check if pubkey from client certificate was used */
2561 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2563 /* Set flag "skip certificate verify" */
2564 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2566 EVP_PKEY_CTX_free(pkey_ctx);
2567 s->session->master_key_length=
2568 s->method->ssl3_enc->generate_master_secret(s,
2569 s->session->master_key,premaster_secret,32);
2570 EVP_PKEY_free(pub_key);
2573 #ifndef OPENSSL_NO_PSK
2574 else if (alg_k & SSL_kPSK)
2576 char identity[PSK_MAX_IDENTITY_LEN];
2577 unsigned char *t = NULL;
2578 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
2579 unsigned int pre_ms_len = 0, psk_len = 0;
2583 if (s->psk_client_callback == NULL)
2585 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2586 SSL_R_PSK_NO_CLIENT_CB);
2590 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2591 identity, PSK_MAX_IDENTITY_LEN,
2592 psk_or_pre_ms, sizeof(psk_or_pre_ms));
2593 if (psk_len > PSK_MAX_PSK_LEN)
2595 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2596 ERR_R_INTERNAL_ERROR);
2599 else if (psk_len == 0)
2601 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2602 SSL_R_PSK_IDENTITY_NOT_FOUND);
2606 /* create PSK pre_master_secret */
2607 pre_ms_len = 2+psk_len+2+psk_len;
2609 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
2611 memset(t, 0, psk_len);
2615 if (s->session->psk_identity_hint != NULL)
2616 OPENSSL_free(s->session->psk_identity_hint);
2617 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2618 if (s->ctx->psk_identity_hint != NULL &&
2619 s->session->psk_identity_hint == NULL)
2621 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2622 ERR_R_MALLOC_FAILURE);
2626 if (s->session->psk_identity != NULL)
2627 OPENSSL_free(s->session->psk_identity);
2628 s->session->psk_identity = BUF_strdup(identity);
2629 if (s->session->psk_identity == NULL)
2631 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2632 ERR_R_MALLOC_FAILURE);
2636 s->session->master_key_length =
2637 s->method->ssl3_enc->generate_master_secret(s,
2638 s->session->master_key,
2639 psk_or_pre_ms, pre_ms_len);
2640 n = strlen(identity);
2642 memcpy(p, identity, n);
2646 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2647 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2650 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2657 ssl3_send_alert(s, SSL3_AL_FATAL,
2658 SSL_AD_HANDSHAKE_FAILURE);
2659 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2660 ERR_R_INTERNAL_ERROR);
2664 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
2667 s->state=SSL3_ST_CW_KEY_EXCH_B;
2668 /* number of bytes to write */
2673 /* SSL3_ST_CW_KEY_EXCH_B */
2674 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2676 #ifndef OPENSSL_NO_ECDH
2677 BN_CTX_free(bn_ctx);
2678 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2679 if (clnt_ecdh != NULL)
2680 EC_KEY_free(clnt_ecdh);
2681 EVP_PKEY_free(srvr_pub_pkey);
2686 int ssl3_send_client_verify(SSL *s)
2688 unsigned char *p,*d;
2689 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
2691 EVP_PKEY_CTX *pctx=NULL;
2692 #ifndef OPENSSL_NO_RSA
2698 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2700 d=(unsigned char *)s->init_buf->data;
2702 pkey=s->cert->key->privatekey;
2703 /* Create context from key and test if sha1 is allowed as digest */
2704 pctx = EVP_PKEY_CTX_new(pkey,NULL);
2705 EVP_PKEY_sign_init(pctx);
2706 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0)
2708 s->method->ssl3_enc->cert_verify_mac(s,
2710 &(data[MD5_DIGEST_LENGTH]));
2716 #ifndef OPENSSL_NO_RSA
2717 if (pkey->type == EVP_PKEY_RSA)
2719 s->method->ssl3_enc->cert_verify_mac(s,
2722 if (RSA_sign(NID_md5_sha1, data,
2723 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
2724 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
2726 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
2734 #ifndef OPENSSL_NO_DSA
2735 if (pkey->type == EVP_PKEY_DSA)
2737 if (!DSA_sign(pkey->save_type,
2738 &(data[MD5_DIGEST_LENGTH]),
2739 SHA_DIGEST_LENGTH,&(p[2]),
2740 (unsigned int *)&j,pkey->pkey.dsa))
2742 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
2750 #ifndef OPENSSL_NO_ECDSA
2751 if (pkey->type == EVP_PKEY_EC)
2753 if (!ECDSA_sign(pkey->save_type,
2754 &(data[MD5_DIGEST_LENGTH]),
2755 SHA_DIGEST_LENGTH,&(p[2]),
2756 (unsigned int *)&j,pkey->pkey.ec))
2758 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2767 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
2769 unsigned char signbuf[64];
2772 s->method->ssl3_enc->cert_verify_mac(s,
2773 NID_id_GostR3411_94,
2775 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
2776 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2777 ERR_R_INTERNAL_ERROR);
2780 for (i=63,j=0; i>=0; j++, i--) {
2788 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
2791 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
2794 s->state=SSL3_ST_CW_CERT_VRFY_B;
2795 s->init_num=(int)n+4;
2798 EVP_PKEY_CTX_free(pctx);
2799 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2801 EVP_PKEY_CTX_free(pctx);
2805 int ssl3_send_client_certificate(SSL *s)
2808 EVP_PKEY *pkey=NULL;
2812 if (s->state == SSL3_ST_CW_CERT_A)
2814 if ((s->cert == NULL) ||
2815 (s->cert->key->x509 == NULL) ||
2816 (s->cert->key->privatekey == NULL))
2817 s->state=SSL3_ST_CW_CERT_B;
2819 s->state=SSL3_ST_CW_CERT_C;
2822 /* We need to get a client cert */
2823 if (s->state == SSL3_ST_CW_CERT_B)
2825 /* If we get an error, we need to
2826 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2827 * We then get retied later */
2829 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2832 s->rwstate=SSL_X509_LOOKUP;
2835 s->rwstate=SSL_NOTHING;
2836 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2838 s->state=SSL3_ST_CW_CERT_B;
2839 if ( !SSL_use_certificate(s,x509) ||
2840 !SSL_use_PrivateKey(s,pkey))
2846 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2849 if (x509 != NULL) X509_free(x509);
2850 if (pkey != NULL) EVP_PKEY_free(pkey);
2853 if (s->version == SSL3_VERSION)
2855 s->s3->tmp.cert_req=0;
2856 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2861 s->s3->tmp.cert_req=2;
2865 /* Ok, we have a cert */
2866 s->state=SSL3_ST_CW_CERT_C;
2869 if (s->state == SSL3_ST_CW_CERT_C)
2871 s->state=SSL3_ST_CW_CERT_D;
2872 l=ssl3_output_cert_chain(s,
2873 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
2877 /* SSL3_ST_CW_CERT_D */
2878 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2881 #define has_bits(i,m) (((i)&(m)) == (m))
2883 int ssl3_check_cert_and_algorithm(SSL *s)
2887 EVP_PKEY *pkey=NULL;
2889 #ifndef OPENSSL_NO_RSA
2892 #ifndef OPENSSL_NO_DH
2896 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2897 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2899 /* we don't have a certificate */
2900 if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK))
2903 sc=s->session->sess_cert;
2906 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
2910 #ifndef OPENSSL_NO_RSA
2911 rsa=s->session->sess_cert->peer_rsa_tmp;
2913 #ifndef OPENSSL_NO_DH
2914 dh=s->session->sess_cert->peer_dh_tmp;
2917 /* This is the passed certificate */
2919 idx=sc->peer_cert_type;
2920 #ifndef OPENSSL_NO_ECDH
2921 if (idx == SSL_PKEY_ECC)
2923 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2924 s->s3->tmp.new_cipher) == 0)
2925 { /* check failed */
2926 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
2935 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2936 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2937 EVP_PKEY_free(pkey);
2940 /* Check that we have a certificate if we require one */
2941 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2943 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
2946 #ifndef OPENSSL_NO_DSA
2947 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
2949 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
2953 #ifndef OPENSSL_NO_RSA
2954 if ((alg_k & SSL_kRSA) &&
2955 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2957 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2961 #ifndef OPENSSL_NO_DH
2962 if ((alg_k & SSL_kEDH) &&
2963 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2965 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
2968 else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
2970 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
2973 #ifndef OPENSSL_NO_DSA
2974 else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
2976 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
2982 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
2984 #ifndef OPENSSL_NO_RSA
2985 if (alg_k & SSL_kRSA)
2988 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2990 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
2996 #ifndef OPENSSL_NO_DH
2997 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
3000 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
3002 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3009 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3015 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
3020 /* Check to see if handshake is full or resumed. Usually this is just a
3021 * case of checking to see if a cache hit has occurred. In the case of
3022 * session tickets we have to check the next message to be sure.
3025 #ifndef OPENSSL_NO_TLSEXT
3026 # ifndef OPENSSL_NO_NEXTPROTONEG
3027 int ssl3_send_next_proto(SSL *s)
3029 unsigned int len, padding_len;
3032 if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
3034 len = s->next_proto_negotiated_len;
3035 padding_len = 32 - ((len + 2) % 32);
3036 d = (unsigned char *)s->init_buf->data;
3038 memcpy(d + 5, s->next_proto_negotiated, len);
3039 d[5 + len] = padding_len;
3040 memset(d + 6 + len, 0, padding_len);
3041 *(d++)=SSL3_MT_NEXT_PROTO;
3042 l2n3(2 + len + padding_len, d);
3043 s->state = SSL3_ST_CW_NEXT_PROTO_B;
3044 s->init_num = 4 + 2 + len + padding_len;
3048 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3052 int ssl3_check_finished(SSL *s)
3056 /* If we have no ticket it cannot be a resumed session. */
3057 if (!s->session->tlsext_tick)
3059 /* this function is called when we really expect a Certificate
3060 * message, so permit appropriate message length */
3061 n=s->method->ssl_get_message(s,
3067 if (!ok) return((int)n);
3068 s->s3->tmp.reuse_message = 1;
3069 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
3070 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
3077 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3080 #ifndef OPENSSL_NO_ENGINE
3081 if (s->ctx->client_cert_engine)
3083 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3084 SSL_get_client_CA_list(s),
3085 px509, ppkey, NULL, NULL, NULL);
3090 if (s->ctx->client_cert_cb)
3091 i = s->ctx->client_cert_cb(s,px509,ppkey);