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>
163 #ifndef OPENSSL_NO_ENGINE
164 #include <openssl/engine.h>
167 static const SSL_METHOD *ssl3_get_client_method(int ver);
168 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
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);
187 void (*cb)(const SSL *ssl,int type,int val)=NULL;
189 int new_state,state,skip=0;
191 RAND_add(&Time,sizeof(Time),0);
195 if (s->info_callback != NULL)
197 else if (s->ctx->info_callback != NULL)
198 cb=s->ctx->info_callback;
201 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
209 case SSL_ST_RENEGOTIATE:
211 s->state=SSL_ST_CONNECT;
212 s->ctx->stats.sess_connect_renegotiate++;
216 case SSL_ST_BEFORE|SSL_ST_CONNECT:
217 case SSL_ST_OK|SSL_ST_CONNECT:
220 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
222 if ((s->version & 0xff00 ) != 0x0300)
224 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
229 /* s->version=SSL3_VERSION; */
230 s->type=SSL_ST_CONNECT;
232 if (s->init_buf == NULL)
234 if ((buf=BUF_MEM_new()) == NULL)
239 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
248 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
250 /* setup buffing BIO */
251 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
253 /* don't push the buffering BIO quite yet */
255 ssl3_init_finished_mac(s);
257 s->state=SSL3_ST_CW_CLNT_HELLO_A;
258 s->ctx->stats.sess_connect++;
262 case SSL3_ST_CW_CLNT_HELLO_A:
263 case SSL3_ST_CW_CLNT_HELLO_B:
266 ret=ssl3_client_hello(s);
267 if (ret <= 0) goto end;
268 s->state=SSL3_ST_CR_SRVR_HELLO_A;
271 /* turn on buffering for the next lot of output */
272 if (s->bbio != s->wbio)
273 s->wbio=BIO_push(s->bbio,s->wbio);
277 case SSL3_ST_CR_SRVR_HELLO_A:
278 case SSL3_ST_CR_SRVR_HELLO_B:
279 ret=ssl3_get_server_hello(s);
280 if (ret <= 0) goto end;
284 s->state=SSL3_ST_CR_FINISHED_A;
285 #ifndef OPENSSL_NO_TLSEXT
286 if (s->tlsext_ticket_expected)
288 /* receive renewed session ticket */
289 s->state=SSL3_ST_CR_SESSION_TICKET_A;
294 s->state=SSL3_ST_CR_CERT_A;
298 case SSL3_ST_CR_CERT_A:
299 case SSL3_ST_CR_CERT_B:
300 #ifndef OPENSSL_NO_TLSEXT
301 ret=ssl3_check_finished(s);
302 if (ret <= 0) goto end;
306 if (s->tlsext_ticket_expected)
307 s->state=SSL3_ST_CR_SESSION_TICKET_A;
309 s->state=SSL3_ST_CR_FINISHED_A;
314 /* Check if it is anon DH/ECDH */
316 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
317 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
319 ret=ssl3_get_server_certificate(s);
320 if (ret <= 0) goto end;
321 #ifndef OPENSSL_NO_TLSEXT
322 if (s->tlsext_status_expected)
323 s->state=SSL3_ST_CR_CERT_STATUS_A;
325 s->state=SSL3_ST_CR_KEY_EXCH_A;
330 s->state=SSL3_ST_CR_KEY_EXCH_A;
337 s->state=SSL3_ST_CR_KEY_EXCH_A;
342 case SSL3_ST_CR_KEY_EXCH_A:
343 case SSL3_ST_CR_KEY_EXCH_B:
344 ret=ssl3_get_key_exchange(s);
345 if (ret <= 0) goto end;
346 s->state=SSL3_ST_CR_CERT_REQ_A;
349 /* at this point we check that we have the
350 * required stuff from the server */
351 if (!ssl3_check_cert_and_algorithm(s))
358 case SSL3_ST_CR_CERT_REQ_A:
359 case SSL3_ST_CR_CERT_REQ_B:
360 ret=ssl3_get_certificate_request(s);
361 if (ret <= 0) goto end;
362 s->state=SSL3_ST_CR_SRVR_DONE_A;
366 case SSL3_ST_CR_SRVR_DONE_A:
367 case SSL3_ST_CR_SRVR_DONE_B:
368 ret=ssl3_get_server_done(s);
369 if (ret <= 0) goto end;
370 if (s->s3->tmp.cert_req)
371 s->state=SSL3_ST_CW_CERT_A;
373 s->state=SSL3_ST_CW_KEY_EXCH_A;
378 case SSL3_ST_CW_CERT_A:
379 case SSL3_ST_CW_CERT_B:
380 case SSL3_ST_CW_CERT_C:
381 case SSL3_ST_CW_CERT_D:
382 ret=ssl3_send_client_certificate(s);
383 if (ret <= 0) goto end;
384 s->state=SSL3_ST_CW_KEY_EXCH_A;
388 case SSL3_ST_CW_KEY_EXCH_A:
389 case SSL3_ST_CW_KEY_EXCH_B:
390 ret=ssl3_send_client_key_exchange(s);
391 if (ret <= 0) goto end;
392 /* EAY EAY EAY need to check for DH fix cert
394 /* For TLS, cert_req is set to 2, so a cert chain
395 * of nothing is sent, but no verify packet is sent */
396 /* XXX: For now, we do not support client
397 * authentication in ECDH cipher suites with
398 * ECDH (rather than ECDSA) certificates.
399 * We need to skip the certificate verify
400 * message when client's ECDH public key is sent
401 * inside the client certificate.
403 if (s->s3->tmp.cert_req == 1)
405 s->state=SSL3_ST_CW_CERT_VRFY_A;
409 s->state=SSL3_ST_CW_CHANGE_A;
410 s->s3->change_cipher_spec=0;
412 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
414 s->state=SSL3_ST_CW_CHANGE_A;
415 s->s3->change_cipher_spec=0;
421 case SSL3_ST_CW_CERT_VRFY_A:
422 case SSL3_ST_CW_CERT_VRFY_B:
423 ret=ssl3_send_client_verify(s);
424 if (ret <= 0) goto end;
425 s->state=SSL3_ST_CW_CHANGE_A;
427 s->s3->change_cipher_spec=0;
430 case SSL3_ST_CW_CHANGE_A:
431 case SSL3_ST_CW_CHANGE_B:
432 ret=ssl3_send_change_cipher_spec(s,
433 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
434 if (ret <= 0) goto end;
435 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 case SSL3_ST_CW_FINISHED_A:
464 case SSL3_ST_CW_FINISHED_B:
465 ret=ssl3_send_finished(s,
466 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
467 s->method->ssl3_enc->client_finished_label,
468 s->method->ssl3_enc->client_finished_label_len);
469 if (ret <= 0) goto end;
470 s->state=SSL3_ST_CW_FLUSH;
473 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
476 s->s3->tmp.next_state=SSL_ST_OK;
477 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
480 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
481 s->s3->delay_buf_pop_ret=0;
486 #ifndef OPENSSL_NO_TLSEXT
487 /* Allow NewSessionTicket if ticket expected */
488 if (s->tlsext_ticket_expected)
489 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
493 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
498 #ifndef OPENSSL_NO_TLSEXT
499 case SSL3_ST_CR_SESSION_TICKET_A:
500 case SSL3_ST_CR_SESSION_TICKET_B:
501 ret=ssl3_get_new_session_ticket(s);
502 if (ret <= 0) goto end;
503 s->state=SSL3_ST_CR_FINISHED_A;
507 case SSL3_ST_CR_CERT_STATUS_A:
508 case SSL3_ST_CR_CERT_STATUS_B:
509 ret=ssl3_get_cert_status(s);
510 if (ret <= 0) goto end;
511 s->state=SSL3_ST_CR_KEY_EXCH_A;
516 case SSL3_ST_CR_FINISHED_A:
517 case SSL3_ST_CR_FINISHED_B:
519 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
520 SSL3_ST_CR_FINISHED_B);
521 if (ret <= 0) goto end;
524 s->state=SSL3_ST_CW_CHANGE_A;
530 case SSL3_ST_CW_FLUSH:
531 s->rwstate=SSL_WRITING;
532 if (BIO_flush(s->wbio) <= 0)
537 s->rwstate=SSL_NOTHING;
538 s->state=s->s3->tmp.next_state;
542 /* clean a few things up */
543 ssl3_cleanup_key_block(s);
545 if (s->init_buf != NULL)
547 BUF_MEM_free(s->init_buf);
551 /* If we are not 'joining' the last two packets,
552 * remove the buffering now */
553 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
554 ssl_free_wbio_buffer(s);
555 /* else do it later in ssl3_write */
560 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
561 if (s->hit) s->ctx->stats.sess_hit++;
565 s->handshake_func=ssl3_connect;
566 s->ctx->stats.sess_connect_good++;
568 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
574 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
580 /* did we do anything */
581 if (!s->s3->tmp.reuse_message && !skip)
585 if ((ret=BIO_flush(s->wbio)) <= 0)
589 if ((cb != NULL) && (s->state != state))
593 cb(s,SSL_CB_CONNECT_LOOP,1);
604 cb(s,SSL_CB_CONNECT_EXIT,ret);
609 int ssl3_client_hello(SSL *s)
614 unsigned long Time,l;
615 #ifndef OPENSSL_NO_COMP
620 buf=(unsigned char *)s->init_buf->data;
621 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
623 SSL_SESSION *sess = s->session;
624 if ((sess == NULL) ||
625 (sess->ssl_version != s->version) ||
626 #ifdef OPENSSL_NO_TLSEXT
627 !sess->session_id_length ||
629 (!sess->session_id_length && !sess->tlsext_tick) ||
631 (sess->not_resumable))
633 if (!ssl_get_new_session(s,0))
636 /* else use the pre-loaded session */
638 p=s->s3->client_random;
639 Time=(unsigned long)time(NULL); /* Time */
641 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
644 /* Do the message type and length last */
647 *(p++)=s->version>>8;
648 *(p++)=s->version&0xff;
649 s->client_version=s->version;
652 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
659 i=s->session->session_id_length;
663 if (i > (int)sizeof(s->session->session_id))
665 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
668 memcpy(p,s->session->session_id,i);
672 /* Ciphers supported */
673 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
676 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
683 #ifdef OPENSSL_NO_COMP
687 if ((s->options & SSL_OP_NO_COMPRESSION)
688 || !s->ctx->comp_methods)
691 j=sk_SSL_COMP_num(s->ctx->comp_methods);
695 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
699 *(p++)=0; /* Add the NULL method */
701 #ifndef OPENSSL_NO_TLSEXT
703 if (ssl_prepare_clienthello_tlsext(s) <= 0)
705 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
708 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
710 SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
717 *(d++)=SSL3_MT_CLIENT_HELLO;
720 s->state=SSL3_ST_CW_CLNT_HELLO_B;
721 /* number of bytes to write */
726 /* SSL3_ST_CW_CLNT_HELLO_B */
727 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
732 int ssl3_get_server_hello(SSL *s)
734 STACK_OF(SSL_CIPHER) *sk;
740 #ifndef OPENSSL_NO_COMP
744 n=s->method->ssl_get_message(s,
745 SSL3_ST_CR_SRVR_HELLO_A,
746 SSL3_ST_CR_SRVR_HELLO_B,
751 if (!ok) return((int)n);
753 if ( SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
755 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
757 if ( s->d1->send_cookie == 0)
759 s->s3->tmp.reuse_message = 1;
762 else /* already sent a cookie */
764 al=SSL_AD_UNEXPECTED_MESSAGE;
765 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
771 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
773 al=SSL_AD_UNEXPECTED_MESSAGE;
774 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
778 d=p=(unsigned char *)s->init_msg;
780 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
782 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
783 s->version=(s->version&0xff00)|p[1];
784 al=SSL_AD_PROTOCOL_VERSION;
789 /* load the server hello data */
790 /* load the server random */
791 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
794 /* get the session-id */
797 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
799 al=SSL_AD_ILLEGAL_PARAMETER;
800 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
804 #ifndef OPENSSL_NO_TLSEXT
805 /* check if we want to resume the session based on external pre-shared secret */
806 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
808 SSL_CIPHER *pref_cipher=NULL;
809 s->session->master_key_length=sizeof(s->session->master_key);
810 if (s->tls_session_secret_cb(s, s->session->master_key,
811 &s->session->master_key_length,
813 s->tls_session_secret_cb_arg))
815 s->session->cipher = pref_cipher ?
816 pref_cipher : ssl_get_cipher_by_char(s, p+j);
819 #endif /* OPENSSL_NO_TLSEXT */
821 if (j != 0 && j == s->session->session_id_length
822 && memcmp(p,s->session->session_id,j) == 0)
824 if(s->sid_ctx_length != s->session->sid_ctx_length
825 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
827 /* actually a client application bug */
828 al=SSL_AD_ILLEGAL_PARAMETER;
829 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
834 else /* a miss or crap from the other end */
836 /* If we were trying for session-id reuse, make a new
837 * SSL_SESSION so we don't stuff up other people */
839 if (s->session->session_id_length > 0)
841 if (!ssl_get_new_session(s,0))
843 al=SSL_AD_INTERNAL_ERROR;
847 s->session->session_id_length=j;
848 memcpy(s->session->session_id,p,j); /* j could be 0 */
851 c=ssl_get_cipher_by_char(s,p);
855 al=SSL_AD_ILLEGAL_PARAMETER;
856 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
859 p+=ssl_put_cipher_by_char(s,NULL,NULL);
861 sk=ssl_get_ciphers_by_id(s);
862 i=sk_SSL_CIPHER_find(sk,c);
865 /* we did not say we would use this cipher */
866 al=SSL_AD_ILLEGAL_PARAMETER;
867 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
871 /* Depending on the session caching (internal/external), the cipher
872 and/or cipher_id values may not be set. Make sure that
873 cipher_id is set and use it for comparison. */
874 if (s->session->cipher)
875 s->session->cipher_id = s->session->cipher->id;
876 if (s->hit && (s->session->cipher_id != c->id))
878 /* Workaround is now obsolete */
881 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
884 al=SSL_AD_ILLEGAL_PARAMETER;
885 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
889 s->s3->tmp.new_cipher=c;
890 if (!ssl3_digest_cached_records(s))
893 /* lets get the compression algorithm */
895 #ifdef OPENSSL_NO_COMP
898 al=SSL_AD_ILLEGAL_PARAMETER;
899 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
902 /* If compression is disabled we'd better not try to resume a session
905 if (s->session->compress_meth != 0)
907 al=SSL_AD_INTERNAL_ERROR;
908 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
913 if (s->hit && j != s->session->compress_meth)
915 al=SSL_AD_ILLEGAL_PARAMETER;
916 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
921 else if (s->options & SSL_OP_NO_COMPRESSION)
923 al=SSL_AD_ILLEGAL_PARAMETER;
924 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED);
928 comp=ssl3_comp_find(s->ctx->comp_methods,j);
930 if ((j != 0) && (comp == NULL))
932 al=SSL_AD_ILLEGAL_PARAMETER;
933 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
938 s->s3->tmp.new_compression=comp;
942 #ifndef OPENSSL_NO_TLSEXT
944 if (s->version >= SSL3_VERSION)
946 if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
948 /* 'al' set by ssl_parse_serverhello_tlsext */
949 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
952 if (ssl_check_serverhello_tlsext(s) <= 0)
954 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
962 /* wrong packet length */
963 al=SSL_AD_DECODE_ERROR;
964 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
970 ssl3_send_alert(s,SSL3_AL_FATAL,al);
975 int ssl3_get_server_certificate(SSL *s)
978 unsigned long n,nc,llen,l;
980 const unsigned char *q,*p;
982 STACK_OF(X509) *sk=NULL;
985 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
987 n=s->method->ssl_get_message(s,
994 if (!ok) return((int)n);
996 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
997 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
998 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)))
1000 s->s3->tmp.reuse_message=1;
1004 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1006 al=SSL_AD_UNEXPECTED_MESSAGE;
1007 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
1010 p=d=(unsigned char *)s->init_msg;
1012 if ((sk=sk_X509_new_null()) == NULL)
1014 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1021 al=SSL_AD_DECODE_ERROR;
1022 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1025 for (nc=0; nc<llen; )
1028 if ((l+nc+3) > llen)
1030 al=SSL_AD_DECODE_ERROR;
1031 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1036 x=d2i_X509(NULL,&q,l);
1039 al=SSL_AD_BAD_CERTIFICATE;
1040 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
1045 al=SSL_AD_DECODE_ERROR;
1046 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1049 if (!sk_X509_push(sk,x))
1051 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1059 i=ssl_verify_cert_chain(s,sk);
1060 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1061 #ifndef OPENSSL_NO_KRB5
1062 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1063 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1064 #endif /* OPENSSL_NO_KRB5 */
1067 al=ssl_verify_alarm_type(s->verify_result);
1068 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
1071 ERR_clear_error(); /* but we keep s->verify_result */
1073 sc=ssl_sess_cert_new();
1074 if (sc == NULL) goto err;
1076 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1077 s->session->sess_cert=sc;
1080 /* Inconsistency alert: cert_chain does include the peer's
1081 * certificate, which we don't include in s3_srvr.c */
1082 x=sk_X509_value(sk,0);
1084 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1086 pkey=X509_get_pubkey(x);
1088 /* VRS: allow null cert if auth == KRB5 */
1089 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1090 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1094 printf("pkey,x = %p, %p\n", pkey,x);
1095 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
1096 printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name,
1097 s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1098 #endif /* KSSL_DEBUG */
1100 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
1104 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1105 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1109 i=ssl_cert_type(x,pkey);
1110 if (need_cert && i < 0)
1114 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1115 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1121 sc->peer_cert_type=i;
1122 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1123 /* Why would the following ever happen?
1124 * We just created sc a couple of lines ago. */
1125 if (sc->peer_pkeys[i].x509 != NULL)
1126 X509_free(sc->peer_pkeys[i].x509);
1127 sc->peer_pkeys[i].x509=x;
1128 sc->peer_key= &(sc->peer_pkeys[i]);
1130 if (s->session->peer != NULL)
1131 X509_free(s->session->peer);
1132 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1137 sc->peer_cert_type=i;
1140 if (s->session->peer != NULL)
1141 X509_free(s->session->peer);
1142 s->session->peer=NULL;
1144 s->session->verify_result = s->verify_result;
1152 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1155 EVP_PKEY_free(pkey);
1157 sk_X509_pop_free(sk,X509_free);
1161 int ssl3_get_key_exchange(SSL *s)
1163 #ifndef OPENSSL_NO_RSA
1164 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
1167 unsigned char *param,*p;
1168 int al,i,j,param_len,ok;
1170 EVP_PKEY *pkey=NULL;
1171 #ifndef OPENSSL_NO_RSA
1174 #ifndef OPENSSL_NO_DH
1177 #ifndef OPENSSL_NO_ECDH
1178 EC_KEY *ecdh = NULL;
1179 BN_CTX *bn_ctx = NULL;
1180 EC_POINT *srvr_ecpoint = NULL;
1182 int encoded_pt_len = 0;
1185 /* use same message size as in ssl3_get_certificate_request()
1186 * as ServerKeyExchange message may be skipped */
1187 n=s->method->ssl_get_message(s,
1188 SSL3_ST_CR_KEY_EXCH_A,
1189 SSL3_ST_CR_KEY_EXCH_B,
1193 if (!ok) return((int)n);
1195 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1197 #ifndef OPENSSL_NO_PSK
1198 /* In plain PSK ciphersuite, ServerKeyExchange can be
1199 omitted if no identity hint is sent. Set
1200 session->sess_cert anyway to avoid problems
1202 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
1204 s->session->sess_cert=ssl_sess_cert_new();
1205 if (s->ctx->psk_identity_hint)
1206 OPENSSL_free(s->ctx->psk_identity_hint);
1207 s->ctx->psk_identity_hint = NULL;
1210 s->s3->tmp.reuse_message=1;
1214 param=p=(unsigned char *)s->init_msg;
1215 if (s->session->sess_cert != NULL)
1217 #ifndef OPENSSL_NO_RSA
1218 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1220 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1221 s->session->sess_cert->peer_rsa_tmp=NULL;
1224 #ifndef OPENSSL_NO_DH
1225 if (s->session->sess_cert->peer_dh_tmp)
1227 DH_free(s->session->sess_cert->peer_dh_tmp);
1228 s->session->sess_cert->peer_dh_tmp=NULL;
1231 #ifndef OPENSSL_NO_ECDH
1232 if (s->session->sess_cert->peer_ecdh_tmp)
1234 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1235 s->session->sess_cert->peer_ecdh_tmp=NULL;
1241 s->session->sess_cert=ssl_sess_cert_new();
1245 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1246 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1247 EVP_MD_CTX_init(&md_ctx);
1249 #ifndef OPENSSL_NO_PSK
1250 if (alg_k & SSL_kPSK)
1252 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
1254 al=SSL_AD_HANDSHAKE_FAILURE;
1257 /* Store PSK identity hint for later use, hint is used
1258 * in ssl3_send_client_key_exchange. Assume that the
1259 * maximum length of a PSK identity hint can be as
1260 * long as the maximum length of a PSK identity. */
1261 if (i > PSK_MAX_IDENTITY_LEN)
1263 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1264 SSL_R_DATA_LENGTH_TOO_LONG);
1269 al=SSL_AD_DECODE_ERROR;
1270 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1271 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1274 /* If received PSK identity hint contains NULL
1275 * characters, the hint is truncated from the first
1276 * NULL. p may not be ending with NULL, so create a
1277 * NULL-terminated string. */
1278 memcpy(tmp_id_hint, p, i);
1279 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
1280 if (s->ctx->psk_identity_hint != NULL)
1281 OPENSSL_free(s->ctx->psk_identity_hint);
1282 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1283 if (s->ctx->psk_identity_hint == NULL)
1285 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1293 #endif /* !OPENSSL_NO_PSK */
1294 #ifndef OPENSSL_NO_RSA
1295 if (alg_k & SSL_kRSA)
1297 if ((rsa=RSA_new()) == NULL)
1299 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1306 al=SSL_AD_DECODE_ERROR;
1307 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
1310 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1312 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1321 al=SSL_AD_DECODE_ERROR;
1322 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
1325 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1327 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1333 /* this should be because we are using an export cipher */
1334 if (alg_a & SSL_aRSA)
1335 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1338 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1341 s->session->sess_cert->peer_rsa_tmp=rsa;
1344 #else /* OPENSSL_NO_RSA */
1348 #ifndef OPENSSL_NO_DH
1349 else if (alg_k & SSL_kEDH)
1351 if ((dh=DH_new()) == NULL)
1353 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
1360 al=SSL_AD_DECODE_ERROR;
1361 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
1364 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1366 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1375 al=SSL_AD_DECODE_ERROR;
1376 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
1379 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1381 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1390 al=SSL_AD_DECODE_ERROR;
1391 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
1394 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1396 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1402 #ifndef OPENSSL_NO_RSA
1403 if (alg_a & SSL_aRSA)
1404 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1409 #ifndef OPENSSL_NO_DSA
1410 else if (alg_a & SSL_aDSS)
1411 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1413 /* else anonymous DH, so no certificate or pkey. */
1415 s->session->sess_cert->peer_dh_tmp=dh;
1418 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1420 al=SSL_AD_ILLEGAL_PARAMETER;
1421 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1424 #endif /* !OPENSSL_NO_DH */
1426 #ifndef OPENSSL_NO_ECDH
1427 else if (alg_k & SSL_kEECDH)
1430 const EC_GROUP *group;
1432 if ((ecdh=EC_KEY_new()) == NULL)
1434 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1438 /* Extract elliptic curve parameters and the
1439 * server's ephemeral ECDH public key.
1440 * Keep accumulating lengths of various components in
1441 * param_len and make sure it never exceeds n.
1444 /* XXX: For now we only support named (not generic) curves
1445 * and the ECParameters in this case is just three bytes.
1448 if ((param_len > n) ||
1449 (*p != NAMED_CURVE_TYPE) ||
1450 ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0))
1452 al=SSL_AD_INTERNAL_ERROR;
1453 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1457 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1460 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1463 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1465 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1468 EC_GROUP_free(ngroup);
1470 group = EC_KEY_get0_group(ecdh);
1472 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1473 (EC_GROUP_get_degree(group) > 163))
1475 al=SSL_AD_EXPORT_RESTRICTION;
1476 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1482 /* Next, get the encoded ECPoint */
1483 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1484 ((bn_ctx = BN_CTX_new()) == NULL))
1486 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1490 encoded_pt_len = *p; /* length of encoded point */
1492 param_len += (1 + encoded_pt_len);
1493 if ((param_len > n) ||
1494 (EC_POINT_oct2point(group, srvr_ecpoint,
1495 p, encoded_pt_len, bn_ctx) == 0))
1497 al=SSL_AD_DECODE_ERROR;
1498 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
1505 /* The ECC/TLS specification does not mention
1506 * the use of DSA to sign ECParameters in the server
1507 * key exchange message. We do support RSA and ECDSA.
1510 #ifndef OPENSSL_NO_RSA
1511 else if (alg_a & SSL_aRSA)
1512 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1514 #ifndef OPENSSL_NO_ECDSA
1515 else if (alg_a & SSL_aECDSA)
1516 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1518 /* else anonymous ECDH, so no certificate or pkey. */
1519 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1520 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1522 BN_CTX_free(bn_ctx);
1524 EC_POINT_free(srvr_ecpoint);
1525 srvr_ecpoint = NULL;
1529 al=SSL_AD_UNEXPECTED_MESSAGE;
1530 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
1533 #endif /* !OPENSSL_NO_ECDH */
1536 /* p points to the next byte, there are 'n' bytes left */
1538 /* if it was signed, check the signature */
1543 j=EVP_PKEY_size(pkey);
1545 if ((i != n) || (n > j) || (n <= 0))
1547 /* wrong packet length */
1548 al=SSL_AD_DECODE_ERROR;
1549 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1553 #ifndef OPENSSL_NO_RSA
1554 if (pkey->type == EVP_PKEY_RSA)
1560 for (num=2; num > 0; num--)
1562 EVP_DigestInit_ex(&md_ctx,(num == 2)
1563 ?s->ctx->md5:s->ctx->sha1, NULL);
1564 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1565 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1566 EVP_DigestUpdate(&md_ctx,param,param_len);
1567 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1571 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1575 al=SSL_AD_DECRYPT_ERROR;
1576 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1582 al=SSL_AD_DECRYPT_ERROR;
1583 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1589 #ifndef OPENSSL_NO_DSA
1590 if (pkey->type == EVP_PKEY_DSA)
1593 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
1594 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1595 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1596 EVP_VerifyUpdate(&md_ctx,param,param_len);
1597 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1600 al=SSL_AD_DECRYPT_ERROR;
1601 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1607 #ifndef OPENSSL_NO_ECDSA
1608 if (pkey->type == EVP_PKEY_EC)
1610 /* let's do ECDSA */
1611 EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL);
1612 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1613 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1614 EVP_VerifyUpdate(&md_ctx,param,param_len);
1615 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1618 al=SSL_AD_DECRYPT_ERROR;
1619 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1626 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1632 if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK))
1633 /* aNULL or kPSK do not need public keys */
1635 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1638 /* still data left over */
1641 al=SSL_AD_DECODE_ERROR;
1642 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1646 EVP_PKEY_free(pkey);
1647 EVP_MD_CTX_cleanup(&md_ctx);
1650 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1652 EVP_PKEY_free(pkey);
1653 #ifndef OPENSSL_NO_RSA
1657 #ifndef OPENSSL_NO_DH
1661 #ifndef OPENSSL_NO_ECDH
1662 BN_CTX_free(bn_ctx);
1663 EC_POINT_free(srvr_ecpoint);
1667 EVP_MD_CTX_cleanup(&md_ctx);
1671 int ssl3_get_certificate_request(SSL *s)
1674 unsigned long n,nc,l;
1675 unsigned int llen,ctype_num,i;
1677 const unsigned char *p,*q;
1679 STACK_OF(X509_NAME) *ca_sk=NULL;
1681 n=s->method->ssl_get_message(s,
1682 SSL3_ST_CR_CERT_REQ_A,
1683 SSL3_ST_CR_CERT_REQ_B,
1688 if (!ok) return((int)n);
1690 s->s3->tmp.cert_req=0;
1692 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1694 s->s3->tmp.reuse_message=1;
1698 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1700 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1701 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1705 /* TLS does not like anon-DH with client cert */
1706 if (s->version > SSL3_VERSION)
1708 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
1710 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1711 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1716 p=d=(unsigned char *)s->init_msg;
1718 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1720 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1724 /* get the certificate types */
1726 if (ctype_num > SSL3_CT_NUMBER)
1727 ctype_num=SSL3_CT_NUMBER;
1728 for (i=0; i<ctype_num; i++)
1729 s->s3->tmp.ctype[i]= p[i];
1732 /* get the CA RDNs */
1737 out=fopen("/tmp/vsign.der","w");
1738 fwrite(p,1,llen,out);
1743 if ((llen+ctype_num+2+1) != n)
1745 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1746 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1750 for (nc=0; nc<llen; )
1753 if ((l+nc+2) > llen)
1755 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1756 goto cont; /* netscape bugs */
1757 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1758 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1764 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1766 /* If netscape tolerance is on, ignore errors */
1767 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1771 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1772 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1779 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1780 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1783 if (!sk_X509_NAME_push(ca_sk,xn))
1785 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1799 /* we should setup a certificate to return.... */
1800 s->s3->tmp.cert_req=1;
1801 s->s3->tmp.ctype_num=ctype_num;
1802 if (s->s3->tmp.ca_names != NULL)
1803 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1804 s->s3->tmp.ca_names=ca_sk;
1809 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1813 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1815 return(X509_NAME_cmp(*a,*b));
1817 #ifndef OPENSSL_NO_TLSEXT
1818 int ssl3_get_new_session_ticket(SSL *s)
1820 int ok,al,ret=0, ticklen;
1822 const unsigned char *p;
1825 n=s->method->ssl_get_message(s,
1826 SSL3_ST_CR_SESSION_TICKET_A,
1827 SSL3_ST_CR_SESSION_TICKET_B,
1835 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
1837 s->s3->tmp.reuse_message=1;
1840 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
1842 al=SSL_AD_UNEXPECTED_MESSAGE;
1843 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE);
1848 /* need at least ticket_lifetime_hint + ticket length */
1849 al = SSL_AD_DECODE_ERROR;
1850 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
1854 p=d=(unsigned char *)s->init_msg;
1855 n2l(p, s->session->tlsext_tick_lifetime_hint);
1857 /* ticket_lifetime_hint + ticket_length + ticket */
1858 if (ticklen + 6 != n)
1860 al = SSL_AD_DECODE_ERROR;
1861 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
1864 if (s->session->tlsext_tick)
1866 OPENSSL_free(s->session->tlsext_tick);
1867 s->session->tlsext_ticklen = 0;
1869 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1870 if (!s->session->tlsext_tick)
1872 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE);
1875 memcpy(s->session->tlsext_tick, p, ticklen);
1876 s->session->tlsext_ticklen = ticklen;
1877 /* There are two ways to detect a resumed ticket sesion.
1878 * One is to set an appropriate session ID and then the server
1879 * must return a match in ServerHello. This allows the normal
1880 * client session ID matching to work and we know much
1881 * earlier that the ticket has been accepted.
1883 * The other way is to set zero length session ID when the
1884 * ticket is presented and rely on the handshake to determine
1885 * session resumption.
1887 * We choose the former approach because this fits in with
1888 * assumptions elsewhere in OpenSSL. The session ID is set
1889 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
1892 EVP_Digest(p, ticklen,
1893 s->session->session_id, &s->session->session_id_length,
1894 #ifndef OPENSSL_NO_SHA256
1895 EVP_sha256(), NULL);
1902 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1907 int ssl3_get_cert_status(SSL *s)
1910 unsigned long resplen,n;
1911 const unsigned char *p;
1913 n=s->method->ssl_get_message(s,
1914 SSL3_ST_CR_CERT_STATUS_A,
1915 SSL3_ST_CR_CERT_STATUS_B,
1916 SSL3_MT_CERTIFICATE_STATUS,
1920 if (!ok) return((int)n);
1923 /* need at least status type + length */
1924 al = SSL_AD_DECODE_ERROR;
1925 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
1928 p = (unsigned char *)s->init_msg;
1929 if (*p++ != TLSEXT_STATUSTYPE_ocsp)
1931 al = SSL_AD_DECODE_ERROR;
1932 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE);
1936 if (resplen + 4 != n)
1938 al = SSL_AD_DECODE_ERROR;
1939 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
1942 if (s->tlsext_ocsp_resp)
1943 OPENSSL_free(s->tlsext_ocsp_resp);
1944 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
1945 if (!s->tlsext_ocsp_resp)
1947 al = SSL_AD_INTERNAL_ERROR;
1948 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
1951 s->tlsext_ocsp_resplen = resplen;
1952 if (s->ctx->tlsext_status_cb)
1955 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1958 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
1959 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_RESPONSE);
1964 al = SSL_AD_INTERNAL_ERROR;
1965 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
1971 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1976 int ssl3_get_server_done(SSL *s)
1981 n=s->method->ssl_get_message(s,
1982 SSL3_ST_CR_SRVR_DONE_A,
1983 SSL3_ST_CR_SRVR_DONE_B,
1984 SSL3_MT_SERVER_DONE,
1985 30, /* should be very small, like 0 :-) */
1988 if (!ok) return((int)n);
1991 /* should contain no data */
1992 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1993 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
2001 int ssl3_send_client_key_exchange(SSL *s)
2003 unsigned char *p,*d;
2005 unsigned long alg_k;
2006 #ifndef OPENSSL_NO_RSA
2008 EVP_PKEY *pkey=NULL;
2010 #ifndef OPENSSL_NO_KRB5
2012 #endif /* OPENSSL_NO_KRB5 */
2013 #ifndef OPENSSL_NO_ECDH
2014 EC_KEY *clnt_ecdh = NULL;
2015 const EC_POINT *srvr_ecpoint = NULL;
2016 EVP_PKEY *srvr_pub_pkey = NULL;
2017 unsigned char *encodedPoint = NULL;
2018 int encoded_pt_len = 0;
2019 BN_CTX * bn_ctx = NULL;
2022 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2024 d=(unsigned char *)s->init_buf->data;
2027 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2029 /* Fool emacs indentation */
2031 #ifndef OPENSSL_NO_RSA
2032 else if (alg_k & SSL_kRSA)
2035 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2037 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2038 rsa=s->session->sess_cert->peer_rsa_tmp;
2041 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2042 if ((pkey == NULL) ||
2043 (pkey->type != EVP_PKEY_RSA) ||
2044 (pkey->pkey.rsa == NULL))
2046 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
2050 EVP_PKEY_free(pkey);
2053 tmp_buf[0]=s->client_version>>8;
2054 tmp_buf[1]=s->client_version&0xff;
2055 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2058 s->session->master_key_length=sizeof tmp_buf;
2061 /* Fix buf for TLS and beyond */
2062 if (s->version > SSL3_VERSION)
2064 n=RSA_public_encrypt(sizeof tmp_buf,
2065 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
2067 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
2068 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
2072 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
2076 /* Fix buf for TLS and beyond */
2077 if (s->version > SSL3_VERSION)
2083 s->session->master_key_length=
2084 s->method->ssl3_enc->generate_master_secret(s,
2085 s->session->master_key,
2086 tmp_buf,sizeof tmp_buf);
2087 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2090 #ifndef OPENSSL_NO_KRB5
2091 else if (alg_k & SSL_kKRB5)
2093 krb5_error_code krb5rc;
2094 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2095 /* krb5_data krb5_ap_req; */
2096 krb5_data *enc_ticket;
2097 krb5_data authenticator, *authp = NULL;
2098 EVP_CIPHER_CTX ciph_ctx;
2099 const EVP_CIPHER *enc = NULL;
2100 unsigned char iv[EVP_MAX_IV_LENGTH];
2101 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2102 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
2103 + EVP_MAX_IV_LENGTH];
2104 int padl, outl = sizeof(epms);
2106 EVP_CIPHER_CTX_init(&ciph_ctx);
2109 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
2111 #endif /* KSSL_DEBUG */
2115 if (KRB5SENDAUTH) authp = &authenticator;
2116 #endif /* KRB5SENDAUTH */
2118 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
2120 enc = kssl_map_enc(kssl_ctx->enctype);
2125 printf("kssl_cget_tkt rtn %d\n", krb5rc);
2126 if (krb5rc && kssl_err.text)
2127 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
2129 #endif /* KSSL_DEBUG */
2133 ssl3_send_alert(s,SSL3_AL_FATAL,
2134 SSL_AD_HANDSHAKE_FAILURE);
2135 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2140 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
2141 ** in place of RFC 2712 KerberosWrapper, as in:
2143 ** Send ticket (copy to *p, set n = length)
2144 ** n = krb5_ap_req.length;
2145 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2146 ** if (krb5_ap_req.data)
2147 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2149 ** Now using real RFC 2712 KerberosWrapper
2150 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2151 ** Note: 2712 "opaque" types are here replaced
2152 ** with a 2-byte length followed by the value.
2154 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2155 ** Where "xx xx" = length bytes. Shown here with
2156 ** optional authenticator omitted.
2159 /* KerberosWrapper.Ticket */
2160 s2n(enc_ticket->length,p);
2161 memcpy(p, enc_ticket->data, enc_ticket->length);
2162 p+= enc_ticket->length;
2163 n = enc_ticket->length + 2;
2165 /* KerberosWrapper.Authenticator */
2166 if (authp && authp->length)
2168 s2n(authp->length,p);
2169 memcpy(p, authp->data, authp->length);
2171 n+= authp->length + 2;
2179 s2n(0,p);/* null authenticator length */
2183 tmp_buf[0]=s->client_version>>8;
2184 tmp_buf[1]=s->client_version&0xff;
2185 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2188 /* 20010420 VRS. Tried it this way; failed.
2189 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2190 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2191 ** kssl_ctx->length);
2192 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2195 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2196 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
2198 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
2200 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
2202 if (outl > (int)sizeof epms)
2204 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2207 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2209 /* KerberosWrapper.EncryptedPreMasterSecret */
2211 memcpy(p, epms, outl);
2215 s->session->master_key_length=
2216 s->method->ssl3_enc->generate_master_secret(s,
2217 s->session->master_key,
2218 tmp_buf, sizeof tmp_buf);
2220 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2221 OPENSSL_cleanse(epms, outl);
2224 #ifndef OPENSSL_NO_DH
2225 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2227 DH *dh_srvr,*dh_clnt;
2229 if (s->session->sess_cert == NULL)
2231 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2232 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
2236 if (s->session->sess_cert->peer_dh_tmp != NULL)
2237 dh_srvr=s->session->sess_cert->peer_dh_tmp;
2240 /* we get them from the cert */
2241 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
2242 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
2246 /* generate a new random key */
2247 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2249 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2252 if (!DH_generate_key(dh_clnt))
2254 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2259 /* use the 'p' output buffer for the DH key, but
2260 * make sure to clear it out afterwards */
2262 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2266 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2271 /* generate master key from the result */
2272 s->session->master_key_length=
2273 s->method->ssl3_enc->generate_master_secret(s,
2274 s->session->master_key,p,n);
2278 /* send off the data */
2279 n=BN_num_bytes(dh_clnt->pub_key);
2281 BN_bn2bin(dh_clnt->pub_key,p);
2286 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2290 #ifndef OPENSSL_NO_ECDH
2291 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
2293 const EC_GROUP *srvr_group = NULL;
2295 int ecdh_clnt_cert = 0;
2298 /* Did we send out the client's
2299 * ECDH share for use in premaster
2300 * computation as part of client certificate?
2301 * If so, set ecdh_clnt_cert to 1.
2303 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2305 /* XXX: For now, we do not support client
2306 * authentication using ECDH certificates.
2307 * To add such support, one needs to add
2308 * code that checks for appropriate
2309 * conditions and sets ecdh_clnt_cert to 1.
2310 * For example, the cert have an ECC
2311 * key on the same curve as the server's
2312 * and the key should be authorized for
2315 * One also needs to add code in ssl3_connect
2316 * to skip sending the certificate verify
2319 * if ((s->cert->key->privatekey != NULL) &&
2320 * (s->cert->key->privatekey->type ==
2321 * EVP_PKEY_EC) && ...)
2322 * ecdh_clnt_cert = 1;
2326 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2328 tkey = s->session->sess_cert->peer_ecdh_tmp;
2332 /* Get the Server Public Key from Cert */
2333 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2334 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2335 if ((srvr_pub_pkey == NULL) ||
2336 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2337 (srvr_pub_pkey->pkey.ec == NULL))
2339 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2340 ERR_R_INTERNAL_ERROR);
2344 tkey = srvr_pub_pkey->pkey.ec;
2347 srvr_group = EC_KEY_get0_group(tkey);
2348 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2350 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2352 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2353 ERR_R_INTERNAL_ERROR);
2357 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2359 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2363 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2365 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
2370 /* Reuse key info from our certificate
2371 * We only need our private key to perform
2372 * the ECDH computation.
2374 const BIGNUM *priv_key;
2375 tkey = s->cert->key->privatekey->pkey.ec;
2376 priv_key = EC_KEY_get0_private_key(tkey);
2377 if (priv_key == NULL)
2379 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2382 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2384 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
2390 /* Generate a new ECDH key pair */
2391 if (!(EC_KEY_generate_key(clnt_ecdh)))
2393 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2398 /* use the 'p' output buffer for the ECDH key, but
2399 * make sure to clear it out afterwards
2402 field_size = EC_GROUP_get_degree(srvr_group);
2403 if (field_size <= 0)
2405 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2409 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2412 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2417 /* generate master key from the result */
2418 s->session->master_key_length = s->method->ssl3_enc \
2419 -> generate_master_secret(s,
2420 s->session->master_key,
2423 memset(p, 0, n); /* clean up */
2427 /* Send empty client key exch message */
2432 /* First check the size of encoding and
2433 * allocate memory accordingly.
2436 EC_POINT_point2oct(srvr_group,
2437 EC_KEY_get0_public_key(clnt_ecdh),
2438 POINT_CONVERSION_UNCOMPRESSED,
2441 encodedPoint = (unsigned char *)
2442 OPENSSL_malloc(encoded_pt_len *
2443 sizeof(unsigned char));
2444 bn_ctx = BN_CTX_new();
2445 if ((encodedPoint == NULL) ||
2448 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2452 /* Encode the public key */
2453 n = EC_POINT_point2oct(srvr_group,
2454 EC_KEY_get0_public_key(clnt_ecdh),
2455 POINT_CONVERSION_UNCOMPRESSED,
2456 encodedPoint, encoded_pt_len, bn_ctx);
2458 *p = n; /* length of encoded point */
2459 /* Encoded point will be copied here */
2461 /* copy the point */
2462 memcpy((unsigned char *)p, encodedPoint, n);
2463 /* increment n to account for length field */
2467 /* Free allocated memory */
2468 BN_CTX_free(bn_ctx);
2469 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2470 if (clnt_ecdh != NULL)
2471 EC_KEY_free(clnt_ecdh);
2472 EVP_PKEY_free(srvr_pub_pkey);
2474 #endif /* !OPENSSL_NO_ECDH */
2475 else if (alg_k & SSL_kGOST)
2477 /* GOST key exchange message creation */
2478 EVP_PKEY_CTX *pkey_ctx;
2481 unsigned int md_len;
2483 unsigned char premaster_secret[32],shared_ukm[32], tmp[256];
2484 EVP_MD_CTX *ukm_hash;
2487 /* Get server sertificate PKEY and create ctx from it */
2488 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
2490 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
2492 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2496 pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);
2497 /* If we have send a certificate, and certificate key
2499 * parameters match those of server certificate, use
2500 * certificate key for key exchange
2503 /* Otherwise, generate ephemeral key pair */
2505 EVP_PKEY_encrypt_init(pkey_ctx);
2506 /* Generate session key */
2507 RAND_bytes(premaster_secret,32);
2508 /* If we have client certificate, use its secret as peer key */
2509 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2510 if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <=0) {
2511 /* If there was an error - just ignore it. Ephemeral key
2517 /* Compute shared IV and store it in algorithm-specific
2519 ukm_hash = EVP_MD_CTX_create();
2520 EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94));
2521 EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE);
2522 EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE);
2523 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2524 EVP_MD_CTX_destroy(ukm_hash);
2525 if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV,
2527 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2531 /* Make GOST keytransport blob message */
2532 /*Encapsulate it into sequence */
2533 *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2535 if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secret,32)<0) {
2536 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2543 *(p++)= msglen & 0xff;
2548 *(p++)= msglen & 0xff;
2551 memcpy(p, tmp, msglen);
2552 /* Check if pubkey from client certificate was used */
2553 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2555 /* Set flag "skip certificate verify" */
2556 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2558 EVP_PKEY_CTX_free(pkey_ctx);
2559 s->session->master_key_length=
2560 s->method->ssl3_enc->generate_master_secret(s,
2561 s->session->master_key,premaster_secret,32);
2562 EVP_PKEY_free(pub_key);
2565 #ifndef OPENSSL_NO_PSK
2566 else if (alg_k & SSL_kPSK)
2568 char identity[PSK_MAX_IDENTITY_LEN];
2569 unsigned char *t = NULL;
2570 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
2571 unsigned int pre_ms_len = 0, psk_len = 0;
2575 if (s->psk_client_callback == NULL)
2577 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2578 SSL_R_PSK_NO_CLIENT_CB);
2582 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
2583 identity, PSK_MAX_IDENTITY_LEN,
2584 psk_or_pre_ms, sizeof(psk_or_pre_ms));
2585 if (psk_len > PSK_MAX_PSK_LEN)
2587 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2588 ERR_R_INTERNAL_ERROR);
2591 else if (psk_len == 0)
2593 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2594 SSL_R_PSK_IDENTITY_NOT_FOUND);
2598 /* create PSK pre_master_secret */
2599 pre_ms_len = 2+psk_len+2+psk_len;
2601 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
2603 memset(t, 0, psk_len);
2607 if (s->session->psk_identity_hint != NULL)
2608 OPENSSL_free(s->session->psk_identity_hint);
2609 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2610 if (s->ctx->psk_identity_hint != NULL &&
2611 s->session->psk_identity_hint == NULL)
2613 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2614 ERR_R_MALLOC_FAILURE);
2618 if (s->session->psk_identity != NULL)
2619 OPENSSL_free(s->session->psk_identity);
2620 s->session->psk_identity = BUF_strdup(identity);
2621 if (s->session->psk_identity == NULL)
2623 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2624 ERR_R_MALLOC_FAILURE);
2628 s->session->master_key_length =
2629 s->method->ssl3_enc->generate_master_secret(s,
2630 s->session->master_key,
2631 psk_or_pre_ms, pre_ms_len);
2632 n = strlen(identity);
2634 memcpy(p, identity, n);
2638 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
2639 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2642 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2649 ssl3_send_alert(s, SSL3_AL_FATAL,
2650 SSL_AD_HANDSHAKE_FAILURE);
2651 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2652 ERR_R_INTERNAL_ERROR);
2656 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
2659 s->state=SSL3_ST_CW_KEY_EXCH_B;
2660 /* number of bytes to write */
2665 /* SSL3_ST_CW_KEY_EXCH_B */
2666 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2668 #ifndef OPENSSL_NO_ECDH
2669 BN_CTX_free(bn_ctx);
2670 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2671 if (clnt_ecdh != NULL)
2672 EC_KEY_free(clnt_ecdh);
2673 EVP_PKEY_free(srvr_pub_pkey);
2678 int ssl3_send_client_verify(SSL *s)
2680 unsigned char *p,*d;
2681 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
2683 EVP_PKEY_CTX *pctx=NULL;
2684 #ifndef OPENSSL_NO_RSA
2690 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
2692 d=(unsigned char *)s->init_buf->data;
2694 pkey=s->cert->key->privatekey;
2695 /* Create context from key and test if sha1 is allowed as digest */
2696 pctx = EVP_PKEY_CTX_new(pkey,NULL);
2697 EVP_PKEY_sign_init(pctx);
2698 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0)
2700 s->method->ssl3_enc->cert_verify_mac(s,
2702 &(data[MD5_DIGEST_LENGTH]));
2708 #ifndef OPENSSL_NO_RSA
2709 if (pkey->type == EVP_PKEY_RSA)
2711 s->method->ssl3_enc->cert_verify_mac(s,
2714 if (RSA_sign(NID_md5_sha1, data,
2715 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
2716 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
2718 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
2726 #ifndef OPENSSL_NO_DSA
2727 if (pkey->type == EVP_PKEY_DSA)
2729 if (!DSA_sign(pkey->save_type,
2730 &(data[MD5_DIGEST_LENGTH]),
2731 SHA_DIGEST_LENGTH,&(p[2]),
2732 (unsigned int *)&j,pkey->pkey.dsa))
2734 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
2742 #ifndef OPENSSL_NO_ECDSA
2743 if (pkey->type == EVP_PKEY_EC)
2745 if (!ECDSA_sign(pkey->save_type,
2746 &(data[MD5_DIGEST_LENGTH]),
2747 SHA_DIGEST_LENGTH,&(p[2]),
2748 (unsigned int *)&j,pkey->pkey.ec))
2750 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2759 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
2761 unsigned char signbuf[64];
2764 s->method->ssl3_enc->cert_verify_mac(s,
2765 NID_id_GostR3411_94,
2767 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
2768 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
2769 ERR_R_INTERNAL_ERROR);
2772 for (i=63,j=0; i>=0; j++, i--) {
2780 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
2783 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
2786 s->state=SSL3_ST_CW_CERT_VRFY_B;
2787 s->init_num=(int)n+4;
2790 EVP_PKEY_CTX_free(pctx);
2791 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2793 EVP_PKEY_CTX_free(pctx);
2797 int ssl3_send_client_certificate(SSL *s)
2800 EVP_PKEY *pkey=NULL;
2804 if (s->state == SSL3_ST_CW_CERT_A)
2806 if ((s->cert == NULL) ||
2807 (s->cert->key->x509 == NULL) ||
2808 (s->cert->key->privatekey == NULL))
2809 s->state=SSL3_ST_CW_CERT_B;
2811 s->state=SSL3_ST_CW_CERT_C;
2814 /* We need to get a client cert */
2815 if (s->state == SSL3_ST_CW_CERT_B)
2817 /* If we get an error, we need to
2818 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
2819 * We then get retied later */
2821 i = ssl_do_client_cert_cb(s, &x509, &pkey);
2824 s->rwstate=SSL_X509_LOOKUP;
2827 s->rwstate=SSL_NOTHING;
2828 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
2830 s->state=SSL3_ST_CW_CERT_B;
2831 if ( !SSL_use_certificate(s,x509) ||
2832 !SSL_use_PrivateKey(s,pkey))
2838 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
2841 if (x509 != NULL) X509_free(x509);
2842 if (pkey != NULL) EVP_PKEY_free(pkey);
2845 if (s->version == SSL3_VERSION)
2847 s->s3->tmp.cert_req=0;
2848 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
2853 s->s3->tmp.cert_req=2;
2857 /* Ok, we have a cert */
2858 s->state=SSL3_ST_CW_CERT_C;
2861 if (s->state == SSL3_ST_CW_CERT_C)
2863 s->state=SSL3_ST_CW_CERT_D;
2864 l=ssl3_output_cert_chain(s,
2865 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
2869 /* SSL3_ST_CW_CERT_D */
2870 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2873 #define has_bits(i,m) (((i)&(m)) == (m))
2875 int ssl3_check_cert_and_algorithm(SSL *s)
2879 EVP_PKEY *pkey=NULL;
2881 #ifndef OPENSSL_NO_RSA
2884 #ifndef OPENSSL_NO_DH
2888 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2889 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
2891 /* we don't have a certificate */
2892 if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK))
2895 sc=s->session->sess_cert;
2898 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
2902 #ifndef OPENSSL_NO_RSA
2903 rsa=s->session->sess_cert->peer_rsa_tmp;
2905 #ifndef OPENSSL_NO_DH
2906 dh=s->session->sess_cert->peer_dh_tmp;
2909 /* This is the passed certificate */
2911 idx=sc->peer_cert_type;
2912 #ifndef OPENSSL_NO_ECDH
2913 if (idx == SSL_PKEY_ECC)
2915 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
2916 s->s3->tmp.new_cipher) == 0)
2917 { /* check failed */
2918 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
2927 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
2928 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
2929 EVP_PKEY_free(pkey);
2932 /* Check that we have a certificate if we require one */
2933 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
2935 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
2938 #ifndef OPENSSL_NO_DSA
2939 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
2941 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
2945 #ifndef OPENSSL_NO_RSA
2946 if ((alg_k & SSL_kRSA) &&
2947 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
2949 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
2953 #ifndef OPENSSL_NO_DH
2954 if ((alg_k & SSL_kEDH) &&
2955 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
2957 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
2960 else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
2962 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
2965 #ifndef OPENSSL_NO_DSA
2966 else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
2968 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
2974 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
2976 #ifndef OPENSSL_NO_RSA
2977 if (alg_k & SSL_kRSA)
2980 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2982 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
2988 #ifndef OPENSSL_NO_DH
2989 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
2992 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
2994 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3001 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3007 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
3012 /* Check to see if handshake is full or resumed. Usually this is just a
3013 * case of checking to see if a cache hit has occurred. In the case of
3014 * session tickets we have to check the next message to be sure.
3017 #ifndef OPENSSL_NO_TLSEXT
3018 int ssl3_check_finished(SSL *s)
3022 /* If we have no ticket it cannot be a resumed session. */
3023 if (!s->session->tlsext_tick)
3025 /* this function is called when we really expect a Certificate
3026 * message, so permit appropriate message length */
3027 n=s->method->ssl_get_message(s,
3033 if (!ok) return((int)n);
3034 s->s3->tmp.reuse_message = 1;
3035 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
3036 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
3043 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3046 #ifndef OPENSSL_NO_ENGINE
3047 if (s->ctx->client_cert_engine)
3049 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3050 SSL_get_client_CA_list(s),
3051 px509, ppkey, NULL, NULL, NULL);
3056 if (s->ctx->client_cert_cb)
3057 i = s->ctx->client_cert_cb(s,px509,ppkey);