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>
160 #include <openssl/fips.h>
162 #ifndef OPENSSL_NO_DH
163 #include <openssl/dh.h>
165 #include <openssl/bn.h>
166 #ifndef OPENSSL_NO_ENGINE
167 #include <openssl/engine.h>
170 static const SSL_METHOD *ssl3_get_client_method(int ver);
171 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
173 static const SSL_METHOD *ssl3_get_client_method(int ver)
175 if (ver == SSL3_VERSION)
176 return(SSLv3_client_method());
181 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
182 ssl_undefined_function,
184 ssl3_get_client_method)
186 int ssl3_connect(SSL *s)
189 unsigned long Time=(unsigned long)time(NULL);
190 void (*cb)(const SSL *ssl,int type,int val)=NULL;
192 int new_state,state,skip=0;
194 RAND_add(&Time,sizeof(Time),0);
198 if (s->info_callback != NULL)
200 else if (s->ctx->info_callback != NULL)
201 cb=s->ctx->info_callback;
204 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
206 #ifndef OPENSSL_NO_HEARTBEATS
207 /* If we're awaiting a HeartbeatResponse, pretend we
208 * already got and don't await it anymore, because
209 * Heartbeats don't make sense during handshakes anyway.
211 if (s->tlsext_hb_pending)
213 s->tlsext_hb_pending = 0;
224 case SSL_ST_RENEGOTIATE:
226 s->state=SSL_ST_CONNECT;
227 s->ctx->stats.sess_connect_renegotiate++;
231 case SSL_ST_BEFORE|SSL_ST_CONNECT:
232 case SSL_ST_OK|SSL_ST_CONNECT:
235 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
237 if ((s->version & 0xff00 ) != 0x0300)
239 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
244 if (!ssl_security(s, SSL_SECOP_VERSION, 0,
247 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
251 /* s->version=SSL3_VERSION; */
252 s->type=SSL_ST_CONNECT;
254 if (s->init_buf == NULL)
256 if ((buf=BUF_MEM_new()) == NULL)
261 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
270 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
272 /* setup buffing BIO */
273 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
275 /* don't push the buffering BIO quite yet */
277 ssl3_init_finished_mac(s);
279 s->state=SSL3_ST_CW_CLNT_HELLO_A;
280 s->ctx->stats.sess_connect++;
284 case SSL3_ST_CW_CLNT_HELLO_A:
285 case SSL3_ST_CW_CLNT_HELLO_B:
288 ret=ssl3_client_hello(s);
289 if (ret <= 0) goto end;
290 s->state=SSL3_ST_CR_SRVR_HELLO_A;
293 /* turn on buffering for the next lot of output */
294 if (s->bbio != s->wbio)
295 s->wbio=BIO_push(s->bbio,s->wbio);
299 case SSL3_ST_CR_SRVR_HELLO_A:
300 case SSL3_ST_CR_SRVR_HELLO_B:
301 ret=ssl3_get_server_hello(s);
302 if (ret <= 0) goto end;
306 s->state=SSL3_ST_CR_FINISHED_A;
307 #ifndef OPENSSL_NO_TLSEXT
308 if (s->tlsext_ticket_expected)
310 /* receive renewed session ticket */
311 s->state=SSL3_ST_CR_SESSION_TICKET_A;
317 s->state=SSL3_ST_CR_CERT_A;
321 #ifndef OPENSSL_NO_TLSEXT
322 case SSL3_ST_CR_SUPPLEMENTAL_DATA_A:
323 case SSL3_ST_CR_SUPPLEMENTAL_DATA_B:
324 ret = tls1_get_server_supplemental_data(s);
325 if (ret <= 0) goto end;
326 s->state=SSL3_ST_CR_CERT_A;
330 case SSL3_ST_CR_CERT_A:
331 case SSL3_ST_CR_CERT_B:
332 #ifndef OPENSSL_NO_TLSEXT
333 ret=ssl3_check_finished(s);
334 if (ret <= 0) goto end;
337 s->state=SSL3_ST_CR_SUPPLEMENTAL_DATA_A;
344 if (s->tlsext_ticket_expected)
345 s->state=SSL3_ST_CR_SESSION_TICKET_A;
347 s->state=SSL3_ST_CR_FINISHED_A;
352 /* Check if it is anon DH/ECDH */
354 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
355 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
357 ret=ssl3_get_server_certificate(s);
358 if (ret <= 0) goto end;
359 #ifndef OPENSSL_NO_TLSEXT
360 if (s->tlsext_status_expected)
361 s->state=SSL3_ST_CR_CERT_STATUS_A;
363 s->state=SSL3_ST_CR_KEY_EXCH_A;
368 s->state=SSL3_ST_CR_KEY_EXCH_A;
375 s->state=SSL3_ST_CR_KEY_EXCH_A;
380 case SSL3_ST_CR_KEY_EXCH_A:
381 case SSL3_ST_CR_KEY_EXCH_B:
382 ret=ssl3_get_key_exchange(s);
383 if (ret <= 0) goto end;
384 s->state=SSL3_ST_CR_CERT_REQ_A;
387 /* at this point we check that we have the
388 * required stuff from the server */
389 if (!ssl3_check_cert_and_algorithm(s))
396 case SSL3_ST_CR_CERT_REQ_A:
397 case SSL3_ST_CR_CERT_REQ_B:
398 ret=ssl3_get_certificate_request(s);
399 if (ret <= 0) goto end;
400 s->state=SSL3_ST_CR_SRVR_DONE_A;
404 case SSL3_ST_CR_SRVR_DONE_A:
405 case SSL3_ST_CR_SRVR_DONE_B:
406 ret=ssl3_get_server_done(s);
407 if (ret <= 0) goto end;
408 #ifndef OPENSSL_NO_SRP
409 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP)
411 if ((ret = SRP_Calc_A_param(s))<=0)
413 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_SRP_A_CALC);
414 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
419 #ifndef OPENSSL_NO_TLSEXT
420 s->state=SSL3_ST_CW_SUPPLEMENTAL_DATA_A;
422 if (s->s3->tmp.cert_req)
423 s->state=SSL3_ST_CW_CERT_A;
425 s->state=SSL3_ST_CW_KEY_EXCH_A;
431 case SSL3_ST_CW_CERT_A:
432 case SSL3_ST_CW_CERT_B:
433 case SSL3_ST_CW_CERT_C:
434 case SSL3_ST_CW_CERT_D:
435 ret=ssl3_send_client_certificate(s);
436 if (ret <= 0) goto end;
437 s->state=SSL3_ST_CW_KEY_EXCH_A;
441 case SSL3_ST_CW_KEY_EXCH_A:
442 case SSL3_ST_CW_KEY_EXCH_B:
443 ret=ssl3_send_client_key_exchange(s);
444 if (ret <= 0) goto end;
445 /* EAY EAY EAY need to check for DH fix cert
447 /* For TLS, cert_req is set to 2, so a cert chain
448 * of nothing is sent, but no verify packet is sent */
449 /* XXX: For now, we do not support client
450 * authentication in ECDH cipher suites with
451 * ECDH (rather than ECDSA) certificates.
452 * We need to skip the certificate verify
453 * message when client's ECDH public key is sent
454 * inside the client certificate.
456 if (s->s3->tmp.cert_req == 1)
458 s->state=SSL3_ST_CW_CERT_VRFY_A;
462 s->state=SSL3_ST_CW_CHANGE_A;
463 s->s3->change_cipher_spec=0;
465 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
467 s->state=SSL3_ST_CW_CHANGE_A;
468 s->s3->change_cipher_spec=0;
474 case SSL3_ST_CW_CERT_VRFY_A:
475 case SSL3_ST_CW_CERT_VRFY_B:
476 ret=ssl3_send_client_verify(s);
477 if (ret <= 0) goto end;
478 s->state=SSL3_ST_CW_CHANGE_A;
480 s->s3->change_cipher_spec=0;
483 case SSL3_ST_CW_CHANGE_A:
484 case SSL3_ST_CW_CHANGE_B:
485 ret=ssl3_send_change_cipher_spec(s,
486 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
487 if (ret <= 0) goto end;
489 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
490 s->state=SSL3_ST_CW_FINISHED_A;
492 if (s->s3->next_proto_neg_seen)
493 s->state=SSL3_ST_CW_NEXT_PROTO_A;
495 s->state=SSL3_ST_CW_FINISHED_A;
499 s->session->cipher=s->s3->tmp.new_cipher;
500 #ifdef OPENSSL_NO_COMP
501 s->session->compress_meth=0;
503 if (s->s3->tmp.new_compression == NULL)
504 s->session->compress_meth=0;
506 s->session->compress_meth=
507 s->s3->tmp.new_compression->id;
509 if (!s->method->ssl3_enc->setup_key_block(s))
515 if (!s->method->ssl3_enc->change_cipher_state(s,
516 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
524 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
525 case SSL3_ST_CW_NEXT_PROTO_A:
526 case SSL3_ST_CW_NEXT_PROTO_B:
527 ret=ssl3_send_next_proto(s);
528 if (ret <= 0) goto end;
529 s->state=SSL3_ST_CW_FINISHED_A;
533 #ifndef OPENSSL_NO_TLSEXT
534 case SSL3_ST_CW_SUPPLEMENTAL_DATA_A:
535 case SSL3_ST_CW_SUPPLEMENTAL_DATA_B:
536 ret = tls1_send_client_supplemental_data(s, &skip);
537 if (ret <= 0) goto end;
538 if (s->s3->tmp.cert_req)
539 s->state=SSL3_ST_CW_CERT_A;
541 s->state=SSL3_ST_CW_KEY_EXCH_A;
546 case SSL3_ST_CW_FINISHED_A:
547 case SSL3_ST_CW_FINISHED_B:
548 ret=ssl3_send_finished(s,
549 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
550 s->method->ssl3_enc->client_finished_label,
551 s->method->ssl3_enc->client_finished_label_len);
552 if (ret <= 0) goto end;
553 s->state=SSL3_ST_CW_FLUSH;
556 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
559 s->s3->tmp.next_state=SSL_ST_OK;
560 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
563 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
564 s->s3->delay_buf_pop_ret=0;
569 #ifndef OPENSSL_NO_TLSEXT
570 /* Allow NewSessionTicket if ticket expected */
571 if (s->tlsext_ticket_expected)
572 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
576 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
581 #ifndef OPENSSL_NO_TLSEXT
582 case SSL3_ST_CR_SESSION_TICKET_A:
583 case SSL3_ST_CR_SESSION_TICKET_B:
584 ret=ssl3_get_new_session_ticket(s);
585 if (ret <= 0) goto end;
586 s->state=SSL3_ST_CR_FINISHED_A;
590 case SSL3_ST_CR_CERT_STATUS_A:
591 case SSL3_ST_CR_CERT_STATUS_B:
592 ret=ssl3_get_cert_status(s);
593 if (ret <= 0) goto end;
594 s->state=SSL3_ST_CR_KEY_EXCH_A;
599 case SSL3_ST_CR_FINISHED_A:
600 case SSL3_ST_CR_FINISHED_B:
602 s->s3->flags |= SSL3_FLAGS_CCS_OK;
603 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
604 SSL3_ST_CR_FINISHED_B);
605 if (ret <= 0) goto end;
608 s->state=SSL3_ST_CW_CHANGE_A;
614 case SSL3_ST_CW_FLUSH:
615 s->rwstate=SSL_WRITING;
616 if (BIO_flush(s->wbio) <= 0)
621 s->rwstate=SSL_NOTHING;
622 s->state=s->s3->tmp.next_state;
626 /* clean a few things up */
627 ssl3_cleanup_key_block(s);
629 if (s->init_buf != NULL)
631 BUF_MEM_free(s->init_buf);
635 /* If we are not 'joining' the last two packets,
636 * remove the buffering now */
637 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
638 ssl_free_wbio_buffer(s);
639 /* else do it later in ssl3_write */
645 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
646 if (s->hit) s->ctx->stats.sess_hit++;
650 s->handshake_func=ssl3_connect;
651 s->ctx->stats.sess_connect_good++;
653 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
659 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
665 /* did we do anything */
666 if (!s->s3->tmp.reuse_message && !skip)
670 if ((ret=BIO_flush(s->wbio)) <= 0)
674 if ((cb != NULL) && (s->state != state))
678 cb(s,SSL_CB_CONNECT_LOOP,1);
689 cb(s,SSL_CB_CONNECT_EXIT,ret);
694 int ssl3_client_hello(SSL *s)
701 #ifndef OPENSSL_NO_COMP
706 buf=(unsigned char *)s->init_buf->data;
707 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
709 SSL_SESSION *sess = s->session;
710 if ((sess == NULL) ||
711 (sess->ssl_version != s->version) ||
712 #ifdef OPENSSL_NO_TLSEXT
713 !sess->session_id_length ||
715 (!sess->session_id_length && !sess->tlsext_tick) ||
717 (sess->not_resumable))
719 if (!ssl_get_new_session(s,0))
722 if (s->method->version == DTLS_ANY_VERSION)
724 /* Determine which DTLS version to use */
725 int options = s->options;
726 /* If DTLS 1.2 disabled correct the version number */
727 if (options & SSL_OP_NO_DTLSv1_2)
731 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
734 /* Disabling all versions is silly: return an
737 if (options & SSL_OP_NO_DTLSv1)
739 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_WRONG_SSL_VERSION);
742 /* Update method so we don't use any DTLS 1.2
745 s->method = DTLSv1_client_method();
746 s->version = DTLS1_VERSION;
750 /* We only support one version: update method */
751 if (options & SSL_OP_NO_DTLSv1)
752 s->method = DTLSv1_2_client_method();
753 s->version = DTLS1_2_VERSION;
755 s->client_version = s->version;
757 /* else use the pre-loaded session */
759 p=s->s3->client_random;
761 /* for DTLS if client_random is initialized, reuse it, we are
762 * required to use same upon reply to HelloVerify */
767 for (idx=0; idx < sizeof(s->s3->client_random); idx++)
780 ssl_fill_hello_random(s, 0, p,
781 sizeof(s->s3->client_random));
783 /* Do the message type and length last */
784 d=p= ssl_handshake_start(s);
786 /* version indicates the negotiated version: for example from
787 * an SSLv2/v3 compatible client hello). The client_version
788 * field is the maximum version we permit and it is also
789 * used in RSA encrypted premaster secrets. Some servers can
790 * choke if we initially report a higher version then
791 * renegotiate to a lower one in the premaster secret. This
792 * didn't happen with TLS 1.0 as most servers supported it
793 * but it can with TLS 1.1 or later if the server only supports
796 * Possible scenario with previous logic:
797 * 1. Client hello indicates TLS 1.2
798 * 2. Server hello says TLS 1.0
799 * 3. RSA encrypted premaster secret uses 1.2.
800 * 4. Handhaked proceeds using TLS 1.0.
801 * 5. Server sends hello request to renegotiate.
802 * 6. Client hello indicates TLS v1.0 as we now
803 * know that is maximum server supports.
804 * 7. Server chokes on RSA encrypted premaster secret
805 * containing version 1.0.
807 * For interoperability it should be OK to always use the
808 * maximum version we support in client hello and then rely
809 * on the checking of version to ensure the servers isn't
810 * being inconsistent: for example initially negotiating with
811 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
812 * client_version in client hello and not resetting it to
813 * the negotiated version.
816 *(p++)=s->version>>8;
817 *(p++)=s->version&0xff;
818 s->client_version=s->version;
820 *(p++)=s->client_version>>8;
821 *(p++)=s->client_version&0xff;
825 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
832 i=s->session->session_id_length;
836 if (i > (int)sizeof(s->session->session_id))
838 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
841 memcpy(p,s->session->session_id,i);
845 /* cookie stuff for DTLS */
848 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
850 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
853 *(p++) = s->d1->cookie_len;
854 memcpy(p, s->d1->cookie, s->d1->cookie_len);
855 p += s->d1->cookie_len;
858 /* Ciphers supported */
859 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
862 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
865 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
866 /* Some servers hang if client hello > 256 bytes
867 * as hack workaround chop number of supported ciphers
868 * to keep it well below this if we use TLS v1.2
870 if (TLS1_get_version(s) >= TLS1_2_VERSION
871 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
872 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
878 #ifdef OPENSSL_NO_COMP
882 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
885 j=sk_SSL_COMP_num(s->ctx->comp_methods);
889 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
893 *(p++)=0; /* Add the NULL method */
895 #ifndef OPENSSL_NO_TLSEXT
897 if (ssl_prepare_clienthello_tlsext(s) <= 0)
899 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
902 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, &al)) == NULL)
904 ssl3_send_alert(s,SSL3_AL_FATAL,al);
905 SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
911 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
912 s->state=SSL3_ST_CW_CLNT_HELLO_B;
915 /* SSL3_ST_CW_CLNT_HELLO_B */
916 return ssl_do_write(s);
921 int ssl3_get_server_hello(SSL *s)
923 STACK_OF(SSL_CIPHER) *sk;
927 int i,al=SSL_AD_INTERNAL_ERROR,ok;
930 #ifndef OPENSSL_NO_COMP
933 /* Hello verify request and/or server hello version may not
934 * match so set first packet if we're negotiating version.
939 n=s->method->ssl_get_message(s,
940 SSL3_ST_CR_SRVR_HELLO_A,
941 SSL3_ST_CR_SRVR_HELLO_B,
946 if (!ok) return((int)n);
951 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
953 if ( s->d1->send_cookie == 0)
955 s->s3->tmp.reuse_message = 1;
958 else /* already sent a cookie */
960 al=SSL_AD_UNEXPECTED_MESSAGE;
961 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
967 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
969 al=SSL_AD_UNEXPECTED_MESSAGE;
970 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
974 d=p=(unsigned char *)s->init_msg;
975 if (s->method->version == DTLS_ANY_VERSION)
977 /* Work out correct protocol version to use */
978 int hversion = (p[0] << 8)|p[1];
979 int options = s->options;
980 if (hversion == DTLS1_2_VERSION
981 && !(options & SSL_OP_NO_DTLSv1_2))
982 s->method = DTLSv1_2_client_method();
983 else if (tls1_suiteb(s))
985 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
986 s->version = hversion;
987 al = SSL_AD_PROTOCOL_VERSION;
990 else if (hversion == DTLS1_VERSION
991 && !(options & SSL_OP_NO_DTLSv1))
992 s->method = DTLSv1_client_method();
995 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
996 s->version = hversion;
997 al = SSL_AD_PROTOCOL_VERSION;
1000 s->version = s->client_version = s->method->version;
1003 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
1005 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
1006 s->version=(s->version&0xff00)|p[1];
1007 al=SSL_AD_PROTOCOL_VERSION;
1012 /* load the server hello data */
1013 /* load the server random */
1014 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
1015 p+=SSL3_RANDOM_SIZE;
1017 /* get the session-id */
1020 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
1022 al=SSL_AD_ILLEGAL_PARAMETER;
1023 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
1027 #ifndef OPENSSL_NO_TLSEXT
1028 /* check if we want to resume the session based on external pre-shared secret */
1029 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
1031 SSL_CIPHER *pref_cipher=NULL;
1032 s->session->master_key_length=sizeof(s->session->master_key);
1033 if (s->tls_session_secret_cb(s, s->session->master_key,
1034 &s->session->master_key_length,
1036 s->tls_session_secret_cb_arg))
1038 s->session->cipher = pref_cipher ?
1039 pref_cipher : ssl_get_cipher_by_char(s, p+j);
1042 #endif /* OPENSSL_NO_TLSEXT */
1044 if (j != 0 && j == s->session->session_id_length
1045 && memcmp(p,s->session->session_id,j) == 0)
1047 if(s->sid_ctx_length != s->session->sid_ctx_length
1048 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
1050 /* actually a client application bug */
1051 al=SSL_AD_ILLEGAL_PARAMETER;
1052 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1055 s->s3->flags |= SSL3_FLAGS_CCS_OK;
1058 else /* a miss or crap from the other end */
1060 /* If we were trying for session-id reuse, make a new
1061 * SSL_SESSION so we don't stuff up other people */
1063 if (s->session->session_id_length > 0)
1065 if (!ssl_get_new_session(s,0))
1070 s->session->session_id_length=j;
1071 memcpy(s->session->session_id,p,j); /* j could be 0 */
1074 c=ssl_get_cipher_by_char(s,p);
1077 /* unknown cipher */
1078 al=SSL_AD_ILLEGAL_PARAMETER;
1079 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
1082 /* Set version disabled mask now we know version */
1083 if (!SSL_USE_TLS1_2_CIPHERS(s))
1084 ct->mask_ssl = SSL_TLSV1_2;
1087 /* If it is a disabled cipher we didn't send it in client hello,
1088 * so return an error.
1090 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK))
1092 al=SSL_AD_ILLEGAL_PARAMETER;
1093 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
1096 p+=ssl_put_cipher_by_char(s,NULL,NULL);
1098 sk=ssl_get_ciphers_by_id(s);
1099 i=sk_SSL_CIPHER_find(sk,c);
1102 /* we did not say we would use this cipher */
1103 al=SSL_AD_ILLEGAL_PARAMETER;
1104 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
1108 /* Depending on the session caching (internal/external), the cipher
1109 and/or cipher_id values may not be set. Make sure that
1110 cipher_id is set and use it for comparison. */
1111 if (s->session->cipher)
1112 s->session->cipher_id = s->session->cipher->id;
1113 if (s->hit && (s->session->cipher_id != c->id))
1115 /* Workaround is now obsolete */
1118 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1121 al=SSL_AD_ILLEGAL_PARAMETER;
1122 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1126 s->s3->tmp.new_cipher=c;
1127 /* Don't digest cached records if no sigalgs: we may need them for
1128 * client authentication.
1130 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1132 /* lets get the compression algorithm */
1134 #ifdef OPENSSL_NO_COMP
1137 al=SSL_AD_ILLEGAL_PARAMETER;
1138 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1141 /* If compression is disabled we'd better not try to resume a session
1142 * using compression.
1144 if (s->session->compress_meth != 0)
1146 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
1151 if (s->hit && j != s->session->compress_meth)
1153 al=SSL_AD_ILLEGAL_PARAMETER;
1154 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1159 else if (!ssl_allow_compression(s))
1161 al=SSL_AD_ILLEGAL_PARAMETER;
1162 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED);
1166 comp=ssl3_comp_find(s->ctx->comp_methods,j);
1168 if ((j != 0) && (comp == NULL))
1170 al=SSL_AD_ILLEGAL_PARAMETER;
1171 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1176 s->s3->tmp.new_compression=comp;
1180 #ifndef OPENSSL_NO_TLSEXT
1182 if (!ssl_parse_serverhello_tlsext(s,&p,d,n))
1184 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
1191 /* wrong packet length */
1192 al=SSL_AD_DECODE_ERROR;
1193 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
1199 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1204 int ssl3_get_server_certificate(SSL *s)
1206 int al,i,ok,ret= -1;
1207 unsigned long n,nc,llen,l;
1209 const unsigned char *q,*p;
1211 STACK_OF(X509) *sk=NULL;
1213 EVP_PKEY *pkey=NULL;
1214 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
1216 n=s->method->ssl_get_message(s,
1223 if (!ok) return((int)n);
1225 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1226 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1227 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)))
1229 s->s3->tmp.reuse_message=1;
1233 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1235 al=SSL_AD_UNEXPECTED_MESSAGE;
1236 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
1239 p=d=(unsigned char *)s->init_msg;
1241 if ((sk=sk_X509_new_null()) == NULL)
1243 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1250 al=SSL_AD_DECODE_ERROR;
1251 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1254 for (nc=0; nc<llen; )
1257 if ((l+nc+3) > llen)
1259 al=SSL_AD_DECODE_ERROR;
1260 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1265 x=d2i_X509(NULL,&q,l);
1268 al=SSL_AD_BAD_CERTIFICATE;
1269 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
1274 al=SSL_AD_DECODE_ERROR;
1275 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1278 if (!sk_X509_push(sk,x))
1280 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1288 i=ssl_verify_cert_chain(s,sk);
1289 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1290 #ifndef OPENSSL_NO_KRB5
1291 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1292 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1293 #endif /* OPENSSL_NO_KRB5 */
1296 al=ssl_verify_alarm_type(s->verify_result);
1297 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
1300 ERR_clear_error(); /* but we keep s->verify_result */
1303 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1304 al = SSL_AD_HANDSHAKE_FAILURE;
1308 sc=ssl_sess_cert_new();
1309 if (sc == NULL) goto err;
1311 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1312 s->session->sess_cert=sc;
1315 /* Inconsistency alert: cert_chain does include the peer's
1316 * certificate, which we don't include in s3_srvr.c */
1317 x=sk_X509_value(sk,0);
1319 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
1321 pkey=X509_get_pubkey(x);
1323 /* VRS: allow null cert if auth == KRB5 */
1324 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1325 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1329 printf("pkey,x = %p, %p\n", pkey,x);
1330 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
1331 printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name,
1332 s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1333 #endif /* KSSL_DEBUG */
1335 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
1339 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1340 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1344 i=ssl_cert_type(x,pkey);
1345 if (need_cert && i < 0)
1349 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1350 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1356 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1357 if (exp_idx >= 0 && i != exp_idx)
1360 al=SSL_AD_ILLEGAL_PARAMETER;
1361 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1362 SSL_R_WRONG_CERTIFICATE_TYPE);
1365 sc->peer_cert_type=i;
1366 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1367 /* Why would the following ever happen?
1368 * We just created sc a couple of lines ago. */
1369 if (sc->peer_pkeys[i].x509 != NULL)
1370 X509_free(sc->peer_pkeys[i].x509);
1371 sc->peer_pkeys[i].x509=x;
1372 sc->peer_key= &(sc->peer_pkeys[i]);
1374 if (s->session->peer != NULL)
1375 X509_free(s->session->peer);
1376 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
1381 sc->peer_cert_type=i;
1384 if (s->session->peer != NULL)
1385 X509_free(s->session->peer);
1386 s->session->peer=NULL;
1388 s->session->verify_result = s->verify_result;
1395 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1398 EVP_PKEY_free(pkey);
1400 sk_X509_pop_free(sk,X509_free);
1404 int ssl3_get_key_exchange(SSL *s)
1406 #ifndef OPENSSL_NO_RSA
1407 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
1410 unsigned char *param,*p;
1411 int al,i,j,param_len,ok;
1413 EVP_PKEY *pkey=NULL;
1414 const EVP_MD *md = NULL;
1415 #ifndef OPENSSL_NO_RSA
1418 #ifndef OPENSSL_NO_DH
1421 #ifndef OPENSSL_NO_ECDH
1422 EC_KEY *ecdh = NULL;
1423 BN_CTX *bn_ctx = NULL;
1424 EC_POINT *srvr_ecpoint = NULL;
1426 int encoded_pt_len = 0;
1429 /* use same message size as in ssl3_get_certificate_request()
1430 * as ServerKeyExchange message may be skipped */
1431 n=s->method->ssl_get_message(s,
1432 SSL3_ST_CR_KEY_EXCH_A,
1433 SSL3_ST_CR_KEY_EXCH_B,
1437 if (!ok) return((int)n);
1439 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
1441 #ifndef OPENSSL_NO_PSK
1442 /* In plain PSK ciphersuite, ServerKeyExchange can be
1443 omitted if no identity hint is sent. Set
1444 session->sess_cert anyway to avoid problems
1446 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
1448 s->session->sess_cert=ssl_sess_cert_new();
1449 if (s->ctx->psk_identity_hint)
1450 OPENSSL_free(s->ctx->psk_identity_hint);
1451 s->ctx->psk_identity_hint = NULL;
1454 s->s3->tmp.reuse_message=1;
1458 param=p=(unsigned char *)s->init_msg;
1459 if (s->session->sess_cert != NULL)
1461 #ifndef OPENSSL_NO_RSA
1462 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1464 RSA_free(s->session->sess_cert->peer_rsa_tmp);
1465 s->session->sess_cert->peer_rsa_tmp=NULL;
1468 #ifndef OPENSSL_NO_DH
1469 if (s->session->sess_cert->peer_dh_tmp)
1471 DH_free(s->session->sess_cert->peer_dh_tmp);
1472 s->session->sess_cert->peer_dh_tmp=NULL;
1475 #ifndef OPENSSL_NO_ECDH
1476 if (s->session->sess_cert->peer_ecdh_tmp)
1478 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1479 s->session->sess_cert->peer_ecdh_tmp=NULL;
1485 s->session->sess_cert=ssl_sess_cert_new();
1489 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
1490 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
1491 EVP_MD_CTX_init(&md_ctx);
1493 #ifndef OPENSSL_NO_PSK
1494 if (alg_k & SSL_kPSK)
1496 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
1498 al=SSL_AD_HANDSHAKE_FAILURE;
1501 /* Store PSK identity hint for later use, hint is used
1502 * in ssl3_send_client_key_exchange. Assume that the
1503 * maximum length of a PSK identity hint can be as
1504 * long as the maximum length of a PSK identity. */
1505 if (i > PSK_MAX_IDENTITY_LEN)
1507 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1508 SSL_R_DATA_LENGTH_TOO_LONG);
1513 al=SSL_AD_DECODE_ERROR;
1514 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1515 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1518 /* If received PSK identity hint contains NULL
1519 * characters, the hint is truncated from the first
1520 * NULL. p may not be ending with NULL, so create a
1521 * NULL-terminated string. */
1522 memcpy(tmp_id_hint, p, i);
1523 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
1524 if (s->ctx->psk_identity_hint != NULL)
1525 OPENSSL_free(s->ctx->psk_identity_hint);
1526 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
1527 if (s->ctx->psk_identity_hint == NULL)
1529 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1537 #endif /* !OPENSSL_NO_PSK */
1538 #ifndef OPENSSL_NO_SRP
1539 if (alg_k & SSL_kSRP)
1545 al=SSL_AD_DECODE_ERROR;
1546 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_N_LENGTH);
1549 if (!(s->srp_ctx.N=BN_bin2bn(p,i,NULL)))
1551 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1560 al=SSL_AD_DECODE_ERROR;
1561 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_G_LENGTH);
1564 if (!(s->srp_ctx.g=BN_bin2bn(p,i,NULL)))
1566 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1571 i = (unsigned int)(p[0]);
1576 al=SSL_AD_DECODE_ERROR;
1577 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_S_LENGTH);
1580 if (!(s->srp_ctx.s=BN_bin2bn(p,i,NULL)))
1582 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1591 al=SSL_AD_DECODE_ERROR;
1592 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SRP_B_LENGTH);
1595 if (!(s->srp_ctx.B=BN_bin2bn(p,i,NULL)))
1597 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1603 /* We must check if there is a certificate */
1604 #ifndef OPENSSL_NO_RSA
1605 if (alg_a & SSL_aRSA)
1606 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1611 #ifndef OPENSSL_NO_DSA
1612 else if (alg_a & SSL_aDSS)
1613 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1617 #endif /* !OPENSSL_NO_SRP */
1618 #ifndef OPENSSL_NO_RSA
1619 if (alg_k & SSL_kRSA)
1621 if ((rsa=RSA_new()) == NULL)
1623 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1630 al=SSL_AD_DECODE_ERROR;
1631 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
1634 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1636 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1645 al=SSL_AD_DECODE_ERROR;
1646 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
1649 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1651 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1657 /* this should be because we are using an export cipher */
1658 if (alg_a & SSL_aRSA)
1659 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1662 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1665 s->session->sess_cert->peer_rsa_tmp=rsa;
1668 #else /* OPENSSL_NO_RSA */
1672 #ifndef OPENSSL_NO_DH
1673 else if (alg_k & SSL_kDHE)
1675 if ((dh=DH_new()) == NULL)
1677 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
1684 al=SSL_AD_DECODE_ERROR;
1685 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
1688 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1690 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1699 al=SSL_AD_DECODE_ERROR;
1700 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
1703 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1705 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1714 al=SSL_AD_DECODE_ERROR;
1715 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
1718 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1720 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1726 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1727 DH_security_bits(dh), 0, dh))
1729 al=SSL_AD_HANDSHAKE_FAILURE;
1730 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_DH_KEY_TOO_SMALL);
1734 #ifndef OPENSSL_NO_RSA
1735 if (alg_a & SSL_aRSA)
1736 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1741 #ifndef OPENSSL_NO_DSA
1742 else if (alg_a & SSL_aDSS)
1743 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1745 /* else anonymous DH, so no certificate or pkey. */
1747 s->session->sess_cert->peer_dh_tmp=dh;
1750 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
1752 al=SSL_AD_ILLEGAL_PARAMETER;
1753 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1756 #endif /* !OPENSSL_NO_DH */
1758 #ifndef OPENSSL_NO_ECDH
1759 else if (alg_k & SSL_kECDHE)
1762 const EC_GROUP *group;
1764 if ((ecdh=EC_KEY_new()) == NULL)
1766 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1770 /* Extract elliptic curve parameters and the
1771 * server's ephemeral ECDH public key.
1772 * Keep accumulating lengths of various components in
1773 * param_len and make sure it never exceeds n.
1776 /* XXX: For now we only support named (not generic) curves
1777 * and the ECParameters in this case is just three bytes.
1780 /* Check curve is one of our preferences, if not server has
1781 * sent an invalid curve.
1783 if (!tls1_check_curve(s, p, param_len))
1785 al=SSL_AD_DECODE_ERROR;
1786 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_CURVE);
1790 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)
1792 al=SSL_AD_INTERNAL_ERROR;
1793 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1797 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1800 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1803 if (EC_KEY_set_group(ecdh, ngroup) == 0)
1805 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
1808 EC_GROUP_free(ngroup);
1810 group = EC_KEY_get0_group(ecdh);
1812 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1813 (EC_GROUP_get_degree(group) > 163))
1815 al=SSL_AD_EXPORT_RESTRICTION;
1816 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1822 /* Next, get the encoded ECPoint */
1823 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1824 ((bn_ctx = BN_CTX_new()) == NULL))
1826 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1830 encoded_pt_len = *p; /* length of encoded point */
1832 param_len += (1 + encoded_pt_len);
1833 if ((param_len > n) ||
1834 (EC_POINT_oct2point(group, srvr_ecpoint,
1835 p, encoded_pt_len, bn_ctx) == 0))
1837 al=SSL_AD_DECODE_ERROR;
1838 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
1845 /* The ECC/TLS specification does not mention
1846 * the use of DSA to sign ECParameters in the server
1847 * key exchange message. We do support RSA and ECDSA.
1850 #ifndef OPENSSL_NO_RSA
1851 else if (alg_a & SSL_aRSA)
1852 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1854 #ifndef OPENSSL_NO_ECDSA
1855 else if (alg_a & SSL_aECDSA)
1856 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1858 /* else anonymous ECDH, so no certificate or pkey. */
1859 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1860 s->session->sess_cert->peer_ecdh_tmp=ecdh;
1862 BN_CTX_free(bn_ctx);
1864 EC_POINT_free(srvr_ecpoint);
1865 srvr_ecpoint = NULL;
1869 al=SSL_AD_UNEXPECTED_MESSAGE;
1870 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
1873 #endif /* !OPENSSL_NO_ECDH */
1876 /* p points to the next byte, there are 'n' bytes left */
1878 /* if it was signed, check the signature */
1881 if (SSL_USE_SIGALGS(s))
1883 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1888 al = SSL_AD_DECODE_ERROR;
1892 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1902 j=EVP_PKEY_size(pkey);
1904 if ((i != n) || (n > j) || (n <= 0))
1906 /* wrong packet length */
1907 al=SSL_AD_DECODE_ERROR;
1908 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1912 #ifndef OPENSSL_NO_RSA
1913 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
1919 for (num=2; num > 0; num--)
1921 EVP_MD_CTX_set_flags(&md_ctx,
1922 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1923 EVP_DigestInit_ex(&md_ctx,(num == 2)
1924 ?s->ctx->md5:s->ctx->sha1, NULL);
1925 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1926 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1927 EVP_DigestUpdate(&md_ctx,param,param_len);
1928 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1932 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1936 al=SSL_AD_DECRYPT_ERROR;
1937 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1943 al=SSL_AD_DECRYPT_ERROR;
1944 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1951 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1952 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1953 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1954 EVP_VerifyUpdate(&md_ctx,param,param_len);
1955 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
1958 al=SSL_AD_DECRYPT_ERROR;
1959 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1966 /* aNULL or kPSK do not need public keys */
1967 if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK))
1969 /* Might be wrong key type, check it */
1970 if (ssl3_check_cert_and_algorithm(s))
1971 /* Otherwise this shouldn't happen */
1972 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1975 /* still data left over */
1978 al=SSL_AD_DECODE_ERROR;
1979 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1983 EVP_PKEY_free(pkey);
1984 EVP_MD_CTX_cleanup(&md_ctx);
1987 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1989 EVP_PKEY_free(pkey);
1990 #ifndef OPENSSL_NO_RSA
1994 #ifndef OPENSSL_NO_DH
1998 #ifndef OPENSSL_NO_ECDH
1999 BN_CTX_free(bn_ctx);
2000 EC_POINT_free(srvr_ecpoint);
2004 EVP_MD_CTX_cleanup(&md_ctx);
2008 int ssl3_get_certificate_request(SSL *s)
2011 unsigned long n,nc,l;
2012 unsigned int llen, ctype_num,i;
2014 const unsigned char *p,*q;
2016 STACK_OF(X509_NAME) *ca_sk=NULL;
2018 n=s->method->ssl_get_message(s,
2019 SSL3_ST_CR_CERT_REQ_A,
2020 SSL3_ST_CR_CERT_REQ_B,
2025 if (!ok) return((int)n);
2027 s->s3->tmp.cert_req=0;
2029 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
2031 s->s3->tmp.reuse_message=1;
2032 /* If we get here we don't need any cached handshake records
2033 * as we wont be doing client auth.
2035 if (s->s3->handshake_buffer)
2037 if (!ssl3_digest_cached_records(s))
2043 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
2045 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2046 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
2050 /* TLS does not like anon-DH with client cert */
2051 if (s->version > SSL3_VERSION)
2053 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
2055 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2056 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2061 p=d=(unsigned char *)s->init_msg;
2063 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
2065 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
2069 /* get the certificate types */
2071 if (s->cert->ctypes)
2073 OPENSSL_free(s->cert->ctypes);
2074 s->cert->ctypes = NULL;
2076 if (ctype_num > SSL3_CT_NUMBER)
2078 /* If we exceed static buffer copy all to cert structure */
2079 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2080 memcpy(s->cert->ctypes, p, ctype_num);
2081 s->cert->ctype_num = (size_t)ctype_num;
2082 ctype_num=SSL3_CT_NUMBER;
2084 for (i=0; i<ctype_num; i++)
2085 s->s3->tmp.ctype[i]= p[i];
2087 if (SSL_USE_SIGALGS(s))
2090 /* Check we have enough room for signature algorithms and
2091 * following length value.
2093 if ((unsigned long)(p - d + llen + 2) > n)
2095 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2096 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_DATA_LENGTH_TOO_LONG);
2099 /* Clear certificate digests and validity flags */
2100 for (i = 0; i < SSL_PKEY_NUM; i++)
2102 s->cert->pkeys[i].digest = NULL;
2103 s->cert->pkeys[i].valid_flags = 0;
2105 if ((llen & 1) || !tls1_process_sigalgs(s, p, llen))
2107 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2108 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2114 /* get the CA RDNs */
2119 out=fopen("/tmp/vsign.der","w");
2120 fwrite(p,1,llen,out);
2125 if ((unsigned long)(p - d + llen) != n)
2127 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2128 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
2132 for (nc=0; nc<llen; )
2135 if ((l+nc+2) > llen)
2137 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2138 goto cont; /* netscape bugs */
2139 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2140 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
2146 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
2148 /* If netscape tolerance is on, ignore errors */
2149 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
2153 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2154 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
2161 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2162 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
2165 if (!sk_X509_NAME_push(ca_sk,xn))
2167 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
2181 /* we should setup a certificate to return.... */
2182 s->s3->tmp.cert_req=1;
2183 s->s3->tmp.ctype_num=ctype_num;
2184 if (s->s3->tmp.ca_names != NULL)
2185 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
2186 s->s3->tmp.ca_names=ca_sk;
2191 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
2195 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
2197 return(X509_NAME_cmp(*a,*b));
2199 #ifndef OPENSSL_NO_TLSEXT
2200 int ssl3_get_new_session_ticket(SSL *s)
2202 int ok,al,ret=0, ticklen;
2204 const unsigned char *p;
2207 n=s->method->ssl_get_message(s,
2208 SSL3_ST_CR_SESSION_TICKET_A,
2209 SSL3_ST_CR_SESSION_TICKET_B,
2217 if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
2219 s->s3->tmp.reuse_message=1;
2222 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
2224 al=SSL_AD_UNEXPECTED_MESSAGE;
2225 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE);
2230 /* need at least ticket_lifetime_hint + ticket length */
2231 al = SSL_AD_DECODE_ERROR;
2232 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
2236 p=d=(unsigned char *)s->init_msg;
2237 n2l(p, s->session->tlsext_tick_lifetime_hint);
2239 /* ticket_lifetime_hint + ticket_length + ticket */
2240 if (ticklen + 6 != n)
2242 al = SSL_AD_DECODE_ERROR;
2243 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
2246 if (s->session->tlsext_tick)
2248 OPENSSL_free(s->session->tlsext_tick);
2249 s->session->tlsext_ticklen = 0;
2251 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2252 if (!s->session->tlsext_tick)
2254 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE);
2257 memcpy(s->session->tlsext_tick, p, ticklen);
2258 s->session->tlsext_ticklen = ticklen;
2259 /* There are two ways to detect a resumed ticket sesion.
2260 * One is to set an appropriate session ID and then the server
2261 * must return a match in ServerHello. This allows the normal
2262 * client session ID matching to work and we know much
2263 * earlier that the ticket has been accepted.
2265 * The other way is to set zero length session ID when the
2266 * ticket is presented and rely on the handshake to determine
2267 * session resumption.
2269 * We choose the former approach because this fits in with
2270 * assumptions elsewhere in OpenSSL. The session ID is set
2271 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
2274 EVP_Digest(p, ticklen,
2275 s->session->session_id, &s->session->session_id_length,
2276 #ifndef OPENSSL_NO_SHA256
2277 EVP_sha256(), NULL);
2284 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2289 int ssl3_get_cert_status(SSL *s)
2292 unsigned long resplen,n;
2293 const unsigned char *p;
2295 n=s->method->ssl_get_message(s,
2296 SSL3_ST_CR_CERT_STATUS_A,
2297 SSL3_ST_CR_CERT_STATUS_B,
2298 SSL3_MT_CERTIFICATE_STATUS,
2302 if (!ok) return((int)n);
2305 /* need at least status type + length */
2306 al = SSL_AD_DECODE_ERROR;
2307 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
2310 p = (unsigned char *)s->init_msg;
2311 if (*p++ != TLSEXT_STATUSTYPE_ocsp)
2313 al = SSL_AD_DECODE_ERROR;
2314 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE);
2318 if (resplen + 4 != n)
2320 al = SSL_AD_DECODE_ERROR;
2321 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
2324 if (s->tlsext_ocsp_resp)
2325 OPENSSL_free(s->tlsext_ocsp_resp);
2326 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2327 if (!s->tlsext_ocsp_resp)
2329 al = SSL_AD_INTERNAL_ERROR;
2330 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
2333 s->tlsext_ocsp_resplen = resplen;
2334 if (s->ctx->tlsext_status_cb)
2337 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2340 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2341 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_RESPONSE);
2346 al = SSL_AD_INTERNAL_ERROR;
2347 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
2353 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2358 int ssl3_get_server_done(SSL *s)
2363 n=s->method->ssl_get_message(s,
2364 SSL3_ST_CR_SRVR_DONE_A,
2365 SSL3_ST_CR_SRVR_DONE_B,
2366 SSL3_MT_SERVER_DONE,
2367 30, /* should be very small, like 0 :-) */
2370 if (!ok) return((int)n);
2373 /* should contain no data */
2374 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
2375 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
2383 int ssl3_send_client_key_exchange(SSL *s)
2387 unsigned long alg_k;
2388 #ifndef OPENSSL_NO_RSA
2390 EVP_PKEY *pkey=NULL;
2392 #ifndef OPENSSL_NO_KRB5
2394 #endif /* OPENSSL_NO_KRB5 */
2395 #ifndef OPENSSL_NO_ECDH
2396 EC_KEY *clnt_ecdh = NULL;
2397 const EC_POINT *srvr_ecpoint = NULL;
2398 EVP_PKEY *srvr_pub_pkey = NULL;
2399 unsigned char *encodedPoint = NULL;
2400 int encoded_pt_len = 0;
2401 BN_CTX * bn_ctx = NULL;
2404 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
2406 p = ssl_handshake_start(s);
2408 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
2410 /* Fool emacs indentation */
2412 #ifndef OPENSSL_NO_RSA
2413 else if (alg_k & SSL_kRSA)
2416 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2418 if (s->session->sess_cert->peer_rsa_tmp != NULL)
2419 rsa=s->session->sess_cert->peer_rsa_tmp;
2422 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
2423 if ((pkey == NULL) ||
2424 (pkey->type != EVP_PKEY_RSA) ||
2425 (pkey->pkey.rsa == NULL))
2427 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
2431 EVP_PKEY_free(pkey);
2434 tmp_buf[0]=s->client_version>>8;
2435 tmp_buf[1]=s->client_version&0xff;
2436 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2439 s->session->master_key_length=sizeof tmp_buf;
2442 /* Fix buf for TLS and beyond */
2443 if (s->version > SSL3_VERSION)
2445 n=RSA_public_encrypt(sizeof tmp_buf,
2446 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
2448 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
2449 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
2453 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
2457 /* Fix buf for TLS and beyond */
2458 if (s->version > SSL3_VERSION)
2464 s->session->master_key_length=
2465 s->method->ssl3_enc->generate_master_secret(s,
2466 s->session->master_key,
2467 tmp_buf,sizeof tmp_buf);
2468 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
2471 #ifndef OPENSSL_NO_KRB5
2472 else if (alg_k & SSL_kKRB5)
2474 krb5_error_code krb5rc;
2475 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2476 /* krb5_data krb5_ap_req; */
2477 krb5_data *enc_ticket;
2478 krb5_data authenticator, *authp = NULL;
2479 EVP_CIPHER_CTX ciph_ctx;
2480 const EVP_CIPHER *enc = NULL;
2481 unsigned char iv[EVP_MAX_IV_LENGTH];
2482 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2483 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
2484 + EVP_MAX_IV_LENGTH];
2485 int padl, outl = sizeof(epms);
2487 EVP_CIPHER_CTX_init(&ciph_ctx);
2490 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
2492 #endif /* KSSL_DEBUG */
2496 if (KRB5SENDAUTH) authp = &authenticator;
2497 #endif /* KRB5SENDAUTH */
2499 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
2501 enc = kssl_map_enc(kssl_ctx->enctype);
2506 printf("kssl_cget_tkt rtn %d\n", krb5rc);
2507 if (krb5rc && kssl_err.text)
2508 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
2510 #endif /* KSSL_DEBUG */
2514 ssl3_send_alert(s,SSL3_AL_FATAL,
2515 SSL_AD_HANDSHAKE_FAILURE);
2516 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2521 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
2522 ** in place of RFC 2712 KerberosWrapper, as in:
2524 ** Send ticket (copy to *p, set n = length)
2525 ** n = krb5_ap_req.length;
2526 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2527 ** if (krb5_ap_req.data)
2528 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2530 ** Now using real RFC 2712 KerberosWrapper
2531 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2532 ** Note: 2712 "opaque" types are here replaced
2533 ** with a 2-byte length followed by the value.
2535 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2536 ** Where "xx xx" = length bytes. Shown here with
2537 ** optional authenticator omitted.
2540 /* KerberosWrapper.Ticket */
2541 s2n(enc_ticket->length,p);
2542 memcpy(p, enc_ticket->data, enc_ticket->length);
2543 p+= enc_ticket->length;
2544 n = enc_ticket->length + 2;
2546 /* KerberosWrapper.Authenticator */
2547 if (authp && authp->length)
2549 s2n(authp->length,p);
2550 memcpy(p, authp->data, authp->length);
2552 n+= authp->length + 2;
2560 s2n(0,p);/* null authenticator length */
2564 tmp_buf[0]=s->client_version>>8;
2565 tmp_buf[1]=s->client_version&0xff;
2566 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
2569 /* 20010420 VRS. Tried it this way; failed.
2570 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2571 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2572 ** kssl_ctx->length);
2573 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2576 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2577 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
2579 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
2581 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
2583 if (outl > (int)sizeof epms)
2585 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2588 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2590 /* KerberosWrapper.EncryptedPreMasterSecret */
2592 memcpy(p, epms, outl);
2596 s->session->master_key_length=
2597 s->method->ssl3_enc->generate_master_secret(s,
2598 s->session->master_key,
2599 tmp_buf, sizeof tmp_buf);
2601 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2602 OPENSSL_cleanse(epms, outl);
2605 #ifndef OPENSSL_NO_DH
2606 else if (alg_k & (SSL_kDHE|SSL_kDHr|SSL_kDHd))
2608 DH *dh_srvr,*dh_clnt;
2609 SESS_CERT *scert = s->session->sess_cert;
2613 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2614 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
2618 if (scert->peer_dh_tmp != NULL)
2619 dh_srvr=scert->peer_dh_tmp;
2622 /* we get them from the cert */
2623 int idx = scert->peer_cert_type;
2624 EVP_PKEY *spkey = NULL;
2627 spkey = X509_get_pubkey(
2628 scert->peer_pkeys[idx].x509);
2631 dh_srvr = EVP_PKEY_get1_DH(spkey);
2632 EVP_PKEY_free(spkey);
2634 if (dh_srvr == NULL)
2636 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2637 ERR_R_INTERNAL_ERROR);
2641 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2643 /* Use client certificate key */
2644 EVP_PKEY *clkey = s->cert->key->privatekey;
2647 dh_clnt = EVP_PKEY_get1_DH(clkey);
2648 if (dh_clnt == NULL)
2650 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2651 ERR_R_INTERNAL_ERROR);
2657 /* generate a new random key */
2658 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
2660 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2663 if (!DH_generate_key(dh_clnt))
2665 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2671 /* use the 'p' output buffer for the DH key, but
2672 * make sure to clear it out afterwards */
2674 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
2675 if (scert->peer_dh_tmp == NULL)
2680 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
2685 /* generate master key from the result */
2686 s->session->master_key_length=
2687 s->method->ssl3_enc->generate_master_secret(s,
2688 s->session->master_key,p,n);
2692 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2696 /* send off the data */
2697 n=BN_num_bytes(dh_clnt->pub_key);
2699 BN_bn2bin(dh_clnt->pub_key,p);
2705 /* perhaps clean things up a bit EAY EAY EAY EAY*/
2709 #ifndef OPENSSL_NO_ECDH
2710 else if (alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe))
2712 const EC_GROUP *srvr_group = NULL;
2714 int ecdh_clnt_cert = 0;
2717 if (s->session->sess_cert == NULL)
2719 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
2720 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
2724 /* Did we send out the client's
2725 * ECDH share for use in premaster
2726 * computation as part of client certificate?
2727 * If so, set ecdh_clnt_cert to 1.
2729 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
2731 /* XXX: For now, we do not support client
2732 * authentication using ECDH certificates.
2733 * To add such support, one needs to add
2734 * code that checks for appropriate
2735 * conditions and sets ecdh_clnt_cert to 1.
2736 * For example, the cert have an ECC
2737 * key on the same curve as the server's
2738 * and the key should be authorized for
2741 * One also needs to add code in ssl3_connect
2742 * to skip sending the certificate verify
2745 * if ((s->cert->key->privatekey != NULL) &&
2746 * (s->cert->key->privatekey->type ==
2747 * EVP_PKEY_EC) && ...)
2748 * ecdh_clnt_cert = 1;
2752 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
2754 tkey = s->session->sess_cert->peer_ecdh_tmp;
2758 /* Get the Server Public Key from Cert */
2759 srvr_pub_pkey = X509_get_pubkey(s->session-> \
2760 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2761 if ((srvr_pub_pkey == NULL) ||
2762 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
2763 (srvr_pub_pkey->pkey.ec == NULL))
2765 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2766 ERR_R_INTERNAL_ERROR);
2770 tkey = srvr_pub_pkey->pkey.ec;
2773 srvr_group = EC_KEY_get0_group(tkey);
2774 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2776 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
2778 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2779 ERR_R_INTERNAL_ERROR);
2783 if ((clnt_ecdh=EC_KEY_new()) == NULL)
2785 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2789 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
2791 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
2796 /* Reuse key info from our certificate
2797 * We only need our private key to perform
2798 * the ECDH computation.
2800 const BIGNUM *priv_key;
2801 tkey = s->cert->key->privatekey->pkey.ec;
2802 priv_key = EC_KEY_get0_private_key(tkey);
2803 if (priv_key == NULL)
2805 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2808 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
2810 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
2816 /* Generate a new ECDH key pair */
2817 if (!(EC_KEY_generate_key(clnt_ecdh)))
2819 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2824 /* use the 'p' output buffer for the ECDH key, but
2825 * make sure to clear it out afterwards
2828 field_size = EC_GROUP_get_degree(srvr_group);
2829 if (field_size <= 0)
2831 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2835 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
2838 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2843 /* generate master key from the result */
2844 s->session->master_key_length = s->method->ssl3_enc \
2845 -> generate_master_secret(s,
2846 s->session->master_key,
2849 memset(p, 0, n); /* clean up */
2853 /* Send empty client key exch message */
2858 /* First check the size of encoding and
2859 * allocate memory accordingly.
2862 EC_POINT_point2oct(srvr_group,
2863 EC_KEY_get0_public_key(clnt_ecdh),
2864 POINT_CONVERSION_UNCOMPRESSED,
2867 encodedPoint = (unsigned char *)
2868 OPENSSL_malloc(encoded_pt_len *
2869 sizeof(unsigned char));
2870 bn_ctx = BN_CTX_new();
2871 if ((encodedPoint == NULL) ||
2874 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
2878 /* Encode the public key */
2879 n = EC_POINT_point2oct(srvr_group,
2880 EC_KEY_get0_public_key(clnt_ecdh),
2881 POINT_CONVERSION_UNCOMPRESSED,
2882 encodedPoint, encoded_pt_len, bn_ctx);
2884 *p = n; /* length of encoded point */
2885 /* Encoded point will be copied here */
2887 /* copy the point */
2888 memcpy((unsigned char *)p, encodedPoint, n);
2889 /* increment n to account for length field */
2893 /* Free allocated memory */
2894 BN_CTX_free(bn_ctx);
2895 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
2896 if (clnt_ecdh != NULL)
2897 EC_KEY_free(clnt_ecdh);
2898 EVP_PKEY_free(srvr_pub_pkey);
2900 #endif /* !OPENSSL_NO_ECDH */
2901 else if (alg_k & SSL_kGOST)
2903 /* GOST key exchange message creation */
2904 EVP_PKEY_CTX *pkey_ctx;
2907 unsigned int md_len;
2909 unsigned char premaster_secret[32],shared_ukm[32], tmp[256];
2910 EVP_MD_CTX *ukm_hash;
2913 /* Get server sertificate PKEY and create ctx from it */
2914 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
2916 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
2918 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2922 pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);
2923 /* If we have send a certificate, and certificate key
2925 * parameters match those of server certificate, use
2926 * certificate key for key exchange
2929 /* Otherwise, generate ephemeral key pair */
2931 EVP_PKEY_encrypt_init(pkey_ctx);
2932 /* Generate session key */
2933 RAND_bytes(premaster_secret,32);
2934 /* If we have client certificate, use its secret as peer key */
2935 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2936 if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <=0) {
2937 /* If there was an error - just ignore it. Ephemeral key
2943 /* Compute shared IV and store it in algorithm-specific
2945 ukm_hash = EVP_MD_CTX_create();
2946 EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94));
2947 EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE);
2948 EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE);
2949 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2950 EVP_MD_CTX_destroy(ukm_hash);
2951 if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV,
2953 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2957 /* Make GOST keytransport blob message */
2958 /*Encapsulate it into sequence */
2959 *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2961 if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secret,32)<0) {
2962 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2969 *(p++)= msglen & 0xff;
2974 *(p++)= msglen & 0xff;
2977 memcpy(p, tmp, msglen);
2978 /* Check if pubkey from client certificate was used */
2979 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2981 /* Set flag "skip certificate verify" */
2982 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2984 EVP_PKEY_CTX_free(pkey_ctx);
2985 s->session->master_key_length=
2986 s->method->ssl3_enc->generate_master_secret(s,
2987 s->session->master_key,premaster_secret,32);
2988 EVP_PKEY_free(pub_key);
2991 #ifndef OPENSSL_NO_SRP
2992 else if (alg_k & SSL_kSRP)
2994 if (s->srp_ctx.A != NULL)
2996 /* send off the data */
2997 n=BN_num_bytes(s->srp_ctx.A);
2999 BN_bn2bin(s->srp_ctx.A,p);
3004 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
3007 if (s->session->srp_username != NULL)
3008 OPENSSL_free(s->session->srp_username);
3009 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
3010 if (s->session->srp_username == NULL)
3012 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3013 ERR_R_MALLOC_FAILURE);
3017 if ((s->session->master_key_length = SRP_generate_client_master_secret(s,s->session->master_key))<0)
3019 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
3024 #ifndef OPENSSL_NO_PSK
3025 else if (alg_k & SSL_kPSK)
3027 char identity[PSK_MAX_IDENTITY_LEN];
3028 unsigned char *t = NULL;
3029 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
3030 unsigned int pre_ms_len = 0, psk_len = 0;
3034 if (s->psk_client_callback == NULL)
3036 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3037 SSL_R_PSK_NO_CLIENT_CB);
3041 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
3042 identity, PSK_MAX_IDENTITY_LEN,
3043 psk_or_pre_ms, sizeof(psk_or_pre_ms));
3044 if (psk_len > PSK_MAX_PSK_LEN)
3046 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3047 ERR_R_INTERNAL_ERROR);
3050 else if (psk_len == 0)
3052 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3053 SSL_R_PSK_IDENTITY_NOT_FOUND);
3057 /* create PSK pre_master_secret */
3058 pre_ms_len = 2+psk_len+2+psk_len;
3060 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
3062 memset(t, 0, psk_len);
3066 if (s->session->psk_identity_hint != NULL)
3067 OPENSSL_free(s->session->psk_identity_hint);
3068 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
3069 if (s->ctx->psk_identity_hint != NULL &&
3070 s->session->psk_identity_hint == NULL)
3072 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3073 ERR_R_MALLOC_FAILURE);
3077 if (s->session->psk_identity != NULL)
3078 OPENSSL_free(s->session->psk_identity);
3079 s->session->psk_identity = BUF_strdup(identity);
3080 if (s->session->psk_identity == NULL)
3082 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3083 ERR_R_MALLOC_FAILURE);
3087 s->session->master_key_length =
3088 s->method->ssl3_enc->generate_master_secret(s,
3089 s->session->master_key,
3090 psk_or_pre_ms, pre_ms_len);
3091 n = strlen(identity);
3093 memcpy(p, identity, n);
3097 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
3098 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
3101 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3108 ssl3_send_alert(s, SSL3_AL_FATAL,
3109 SSL_AD_HANDSHAKE_FAILURE);
3110 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3111 ERR_R_INTERNAL_ERROR);
3115 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
3116 s->state=SSL3_ST_CW_KEY_EXCH_B;
3119 /* SSL3_ST_CW_KEY_EXCH_B */
3120 return ssl_do_write(s);
3122 #ifndef OPENSSL_NO_ECDH
3123 BN_CTX_free(bn_ctx);
3124 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
3125 if (clnt_ecdh != NULL)
3126 EC_KEY_free(clnt_ecdh);
3127 EVP_PKEY_free(srvr_pub_pkey);
3132 int ssl3_send_client_verify(SSL *s)
3135 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
3137 EVP_PKEY_CTX *pctx=NULL;
3143 EVP_MD_CTX_init(&mctx);
3145 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
3147 p= ssl_handshake_start(s);
3148 pkey=s->cert->key->privatekey;
3149 /* Create context from key and test if sha1 is allowed as digest */
3150 pctx = EVP_PKEY_CTX_new(pkey,NULL);
3151 EVP_PKEY_sign_init(pctx);
3152 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0)
3154 if (!SSL_USE_SIGALGS(s))
3155 s->method->ssl3_enc->cert_verify_mac(s,
3157 &(data[MD5_DIGEST_LENGTH]));
3163 /* For TLS v1.2 send signature algorithm and signature
3164 * using agreed digest and cached handshake records.
3166 if (SSL_USE_SIGALGS(s))
3170 const EVP_MD *md = s->cert->key->digest;
3171 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer,
3173 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md))
3175 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
3176 ERR_R_INTERNAL_ERROR);
3181 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3184 if (!EVP_SignInit_ex(&mctx, md, NULL)
3185 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3186 || !EVP_SignFinal(&mctx, p + 2, &u, pkey))
3188 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
3194 if (!ssl3_digest_cached_records(s))
3198 #ifndef OPENSSL_NO_RSA
3199 if (pkey->type == EVP_PKEY_RSA)
3201 s->method->ssl3_enc->cert_verify_mac(s,
3204 if (RSA_sign(NID_md5_sha1, data,
3205 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
3206 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
3208 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
3216 #ifndef OPENSSL_NO_DSA
3217 if (pkey->type == EVP_PKEY_DSA)
3219 if (!DSA_sign(pkey->save_type,
3220 &(data[MD5_DIGEST_LENGTH]),
3221 SHA_DIGEST_LENGTH,&(p[2]),
3222 (unsigned int *)&j,pkey->pkey.dsa))
3224 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
3232 #ifndef OPENSSL_NO_ECDSA
3233 if (pkey->type == EVP_PKEY_EC)
3235 if (!ECDSA_sign(pkey->save_type,
3236 &(data[MD5_DIGEST_LENGTH]),
3237 SHA_DIGEST_LENGTH,&(p[2]),
3238 (unsigned int *)&j,pkey->pkey.ec))
3240 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
3249 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
3251 unsigned char signbuf[64];
3254 s->method->ssl3_enc->cert_verify_mac(s,
3255 NID_id_GostR3411_94,
3257 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3258 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
3259 ERR_R_INTERNAL_ERROR);
3262 for (i=63,j=0; i>=0; j++, i--) {
3270 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
3273 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
3274 s->state=SSL3_ST_CW_CERT_VRFY_B;
3276 EVP_MD_CTX_cleanup(&mctx);
3277 EVP_PKEY_CTX_free(pctx);
3278 return ssl_do_write(s);
3280 EVP_MD_CTX_cleanup(&mctx);
3281 EVP_PKEY_CTX_free(pctx);
3285 /* Check a certificate can be used for client authentication. Currently
3286 * check cert exists, if we have a suitable digest for TLS 1.2 if
3287 * static DH client certificates can be used and optionally checks
3288 * suitability for Suite B.
3290 static int ssl3_check_client_certificate(SSL *s)
3292 unsigned long alg_k;
3293 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3295 /* If no suitable signature algorithm can't use certificate */
3296 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
3298 /* If strict mode check suitability of chain before using it.
3299 * This also adjusts suite B digest if necessary.
3301 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3302 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3304 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
3305 /* See if we can use client certificate for fixed DH */
3306 if (alg_k & (SSL_kDHr|SSL_kDHd))
3308 SESS_CERT *scert = s->session->sess_cert;
3309 int i = scert->peer_cert_type;
3310 EVP_PKEY *clkey = NULL, *spkey = NULL;
3311 clkey = s->cert->key->privatekey;
3312 /* If client key not DH assume it can be used */
3313 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3316 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3319 /* Compare server and client parameters */
3320 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3321 EVP_PKEY_free(spkey);
3325 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3330 int ssl3_send_client_certificate(SSL *s)
3333 EVP_PKEY *pkey=NULL;
3336 if (s->state == SSL3_ST_CW_CERT_A)
3338 /* Let cert callback update client certificates if required */
3339 if (s->cert->cert_cb)
3341 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3344 s->rwstate=SSL_X509_LOOKUP;
3349 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
3352 s->rwstate=SSL_NOTHING;
3354 if (ssl3_check_client_certificate(s))
3355 s->state=SSL3_ST_CW_CERT_C;
3357 s->state=SSL3_ST_CW_CERT_B;
3360 /* We need to get a client cert */
3361 if (s->state == SSL3_ST_CW_CERT_B)
3363 /* If we get an error, we need to
3364 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
3365 * We then get retied later */
3367 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3370 s->rwstate=SSL_X509_LOOKUP;
3373 s->rwstate=SSL_NOTHING;
3374 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
3376 s->state=SSL3_ST_CW_CERT_B;
3377 if ( !SSL_use_certificate(s,x509) ||
3378 !SSL_use_PrivateKey(s,pkey))
3384 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3387 if (x509 != NULL) X509_free(x509);
3388 if (pkey != NULL) EVP_PKEY_free(pkey);
3389 if (i && !ssl3_check_client_certificate(s))
3393 if (s->version == SSL3_VERSION)
3395 s->s3->tmp.cert_req=0;
3396 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
3401 s->s3->tmp.cert_req=2;
3405 /* Ok, we have a cert */
3406 s->state=SSL3_ST_CW_CERT_C;
3409 if (s->state == SSL3_ST_CW_CERT_C)
3411 s->state=SSL3_ST_CW_CERT_D;
3412 if (!ssl3_output_cert_chain(s,
3413 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key))
3415 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3416 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
3420 /* SSL3_ST_CW_CERT_D */
3421 return ssl_do_write(s);
3424 #define has_bits(i,m) (((i)&(m)) == (m))
3426 int ssl3_check_cert_and_algorithm(SSL *s)
3430 EVP_PKEY *pkey=NULL;
3432 #ifndef OPENSSL_NO_RSA
3435 #ifndef OPENSSL_NO_DH
3439 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
3440 alg_a=s->s3->tmp.new_cipher->algorithm_auth;
3442 /* we don't have a certificate */
3443 if ((alg_a & (SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK))
3446 sc=s->session->sess_cert;
3449 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
3453 #ifndef OPENSSL_NO_RSA
3454 rsa=s->session->sess_cert->peer_rsa_tmp;
3456 #ifndef OPENSSL_NO_DH
3457 dh=s->session->sess_cert->peer_dh_tmp;