3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
6 /* ====================================================================
7 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60 * All rights reserved.
62 * This package is an SSL implementation written
63 * by Eric Young (eay@cryptsoft.com).
64 * The implementation was written so as to conform with Netscapes SSL.
66 * This library is free for commercial and non-commercial use as long as
67 * the following conditions are aheared to. The following conditions
68 * apply to all code found in this distribution, be it the RC4, RSA,
69 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
70 * included with this distribution is covered by the same copyright terms
71 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
73 * Copyright remains Eric Young's, and as such any Copyright notices in
74 * the code are not to be removed.
75 * If this package is used in a product, Eric Young should be given attribution
76 * as the author of the parts of the library used.
77 * This can be in the form of a textual message at program startup or
78 * in documentation (online or textual) provided with the package.
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
83 * 1. Redistributions of source code must retain the copyright
84 * notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
88 * 3. All advertising materials mentioning features or use of this software
89 * must display the following acknowledgement:
90 * "This product includes cryptographic software written by
91 * Eric Young (eay@cryptsoft.com)"
92 * The word 'cryptographic' can be left out if the rouines from the library
93 * being used are not cryptographic related :-).
94 * 4. If you include any Windows specific code (or a derivative thereof) from
95 * the apps directory (application code) you must include an acknowledgement:
96 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
98 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
110 * The licence and distribution terms for any publically available version or
111 * derivative of this code cannot be changed. i.e. this code cannot simply be
112 * copied and put under another distribution licence
113 * [including the GNU Public Licence.]
117 #include "ssl_locl.h"
118 #ifndef OPENSSL_NO_KRB5
119 #include "kssl_lcl.h"
121 #include <openssl/buffer.h>
122 #include <openssl/rand.h>
123 #include <openssl/objects.h>
124 #include <openssl/evp.h>
125 #include <openssl/md5.h>
126 #include <openssl/bn.h>
127 #ifndef OPENSSL_NO_DH
128 #include <openssl/dh.h>
131 static const SSL_METHOD *dtls1_get_client_method(int ver);
132 static int dtls1_get_hello_verify(SSL *s);
134 static const SSL_METHOD *dtls1_get_client_method(int ver)
136 if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER)
137 return(DTLSv1_client_method());
142 IMPLEMENT_dtls1_meth_func(DTLSv1_client_method,
143 ssl_undefined_function,
145 dtls1_get_client_method)
147 int dtls1_connect(SSL *s)
150 unsigned long Time=(unsigned long)time(NULL);
152 void (*cb)(const SSL *ssl,int type,int val)=NULL;
154 int new_state,state,skip=0;;
156 RAND_add(&Time,sizeof(Time),0);
160 if (s->info_callback != NULL)
162 else if (s->ctx->info_callback != NULL)
163 cb=s->ctx->info_callback;
166 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
174 case SSL_ST_RENEGOTIATE:
176 s->state=SSL_ST_CONNECT;
177 s->ctx->stats.sess_connect_renegotiate++;
181 case SSL_ST_BEFORE|SSL_ST_CONNECT:
182 case SSL_ST_OK|SSL_ST_CONNECT:
185 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
187 if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) &&
188 (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00))
190 SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);
195 /* s->version=SSL3_VERSION; */
196 s->type=SSL_ST_CONNECT;
198 if (s->init_buf == NULL)
200 if ((buf=BUF_MEM_new()) == NULL)
205 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
214 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
216 /* setup buffing BIO */
217 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
219 /* don't push the buffering BIO quite yet */
221 s->state=SSL3_ST_CW_CLNT_HELLO_A;
222 s->ctx->stats.sess_connect++;
224 /* mark client_random uninitialized */
225 memset(s->s3->client_random,0,sizeof(s->s3->client_random));
226 s->d1->send_cookie = 0;
230 case SSL3_ST_CW_CLNT_HELLO_A:
231 case SSL3_ST_CW_CLNT_HELLO_B:
235 /* every DTLS ClientHello resets Finished MAC */
236 ssl3_init_finished_mac(s);
238 dtls1_start_timer(s);
239 ret=dtls1_client_hello(s);
240 if (ret <= 0) goto end;
242 if ( s->d1->send_cookie)
244 s->state=SSL3_ST_CW_FLUSH;
245 s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A;
248 s->state=SSL3_ST_CR_SRVR_HELLO_A;
252 /* turn on buffering for the next lot of output */
253 if (s->bbio != s->wbio)
254 s->wbio=BIO_push(s->bbio,s->wbio);
258 case SSL3_ST_CR_SRVR_HELLO_A:
259 case SSL3_ST_CR_SRVR_HELLO_B:
260 ret=ssl3_get_server_hello(s);
261 if (ret <= 0) goto end;
266 s->state=SSL3_ST_CR_FINISHED_A;
268 s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
273 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
274 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
276 ret = dtls1_get_hello_verify(s);
280 if ( s->d1->send_cookie) /* start again, with a cookie */
281 s->state=SSL3_ST_CW_CLNT_HELLO_A;
283 s->state = SSL3_ST_CR_CERT_A;
287 case SSL3_ST_CR_CERT_A:
288 case SSL3_ST_CR_CERT_B:
289 /* Check if it is anon DH or PSK */
290 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
291 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
293 ret=ssl3_get_server_certificate(s);
294 if (ret <= 0) goto end;
298 s->state=SSL3_ST_CR_KEY_EXCH_A;
302 case SSL3_ST_CR_KEY_EXCH_A:
303 case SSL3_ST_CR_KEY_EXCH_B:
304 ret=ssl3_get_key_exchange(s);
305 if (ret <= 0) goto end;
306 s->state=SSL3_ST_CR_CERT_REQ_A;
309 /* at this point we check that we have the
310 * required stuff from the server */
311 if (!ssl3_check_cert_and_algorithm(s))
318 case SSL3_ST_CR_CERT_REQ_A:
319 case SSL3_ST_CR_CERT_REQ_B:
320 ret=ssl3_get_certificate_request(s);
321 if (ret <= 0) goto end;
322 s->state=SSL3_ST_CR_SRVR_DONE_A;
326 case SSL3_ST_CR_SRVR_DONE_A:
327 case SSL3_ST_CR_SRVR_DONE_B:
328 ret=ssl3_get_server_done(s);
329 if (ret <= 0) goto end;
330 if (s->s3->tmp.cert_req)
331 s->state=SSL3_ST_CW_CERT_A;
333 s->state=SSL3_ST_CW_KEY_EXCH_A;
338 case SSL3_ST_CW_CERT_A:
339 case SSL3_ST_CW_CERT_B:
340 case SSL3_ST_CW_CERT_C:
341 case SSL3_ST_CW_CERT_D:
342 dtls1_start_timer(s);
343 ret=dtls1_send_client_certificate(s);
344 if (ret <= 0) goto end;
345 s->state=SSL3_ST_CW_KEY_EXCH_A;
349 case SSL3_ST_CW_KEY_EXCH_A:
350 case SSL3_ST_CW_KEY_EXCH_B:
351 dtls1_start_timer(s);
352 ret=dtls1_send_client_key_exchange(s);
353 if (ret <= 0) goto end;
354 /* EAY EAY EAY need to check for DH fix cert
356 /* For TLS, cert_req is set to 2, so a cert chain
357 * of nothing is sent, but no verify packet is sent */
358 if (s->s3->tmp.cert_req == 1)
360 s->state=SSL3_ST_CW_CERT_VRFY_A;
364 s->state=SSL3_ST_CW_CHANGE_A;
365 s->s3->change_cipher_spec=0;
371 case SSL3_ST_CW_CERT_VRFY_A:
372 case SSL3_ST_CW_CERT_VRFY_B:
373 dtls1_start_timer(s);
374 ret=dtls1_send_client_verify(s);
375 if (ret <= 0) goto end;
376 s->state=SSL3_ST_CW_CHANGE_A;
378 s->s3->change_cipher_spec=0;
381 case SSL3_ST_CW_CHANGE_A:
382 case SSL3_ST_CW_CHANGE_B:
383 dtls1_start_timer(s);
384 ret=dtls1_send_change_cipher_spec(s,
385 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
386 if (ret <= 0) goto end;
387 s->state=SSL3_ST_CW_FINISHED_A;
390 s->session->cipher=s->s3->tmp.new_cipher;
391 #ifdef OPENSSL_NO_COMP
392 s->session->compress_meth=0;
394 if (s->s3->tmp.new_compression == NULL)
395 s->session->compress_meth=0;
397 s->session->compress_meth=
398 s->s3->tmp.new_compression->id;
400 if (!s->method->ssl3_enc->setup_key_block(s))
406 if (!s->method->ssl3_enc->change_cipher_state(s,
407 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
413 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
416 case SSL3_ST_CW_FINISHED_A:
417 case SSL3_ST_CW_FINISHED_B:
418 dtls1_start_timer(s);
419 ret=dtls1_send_finished(s,
420 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
421 s->method->ssl3_enc->client_finished_label,
422 s->method->ssl3_enc->client_finished_label_len);
423 if (ret <= 0) goto end;
424 s->state=SSL3_ST_CW_FLUSH;
427 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
430 s->s3->tmp.next_state=SSL_ST_OK;
431 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
434 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
435 s->s3->delay_buf_pop_ret=0;
440 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
445 case SSL3_ST_CR_FINISHED_A:
446 case SSL3_ST_CR_FINISHED_B:
447 s->d1->change_cipher_spec_ok = 1;
448 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
449 SSL3_ST_CR_FINISHED_B);
450 if (ret <= 0) goto end;
454 s->state=SSL3_ST_CW_CHANGE_A;
460 case SSL3_ST_CW_FLUSH:
461 /* number of bytes to be flushed */
462 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
465 s->rwstate=SSL_WRITING;
466 num1=BIO_flush(s->wbio);
467 if (num1 <= 0) { ret= -1; goto end; }
468 s->rwstate=SSL_NOTHING;
471 s->state=s->s3->tmp.next_state;
475 /* clean a few things up */
476 ssl3_cleanup_key_block(s);
479 if (s->init_buf != NULL)
481 BUF_MEM_free(s->init_buf);
486 /* If we are not 'joining' the last two packets,
487 * remove the buffering now */
488 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
489 ssl_free_wbio_buffer(s);
490 /* else do it later in ssl3_write */
495 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
496 if (s->hit) s->ctx->stats.sess_hit++;
500 s->handshake_func=dtls1_connect;
501 s->ctx->stats.sess_connect_good++;
503 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
505 /* done with handshaking */
506 s->d1->handshake_read_seq = 0;
507 s->d1->next_handshake_write_seq = 0;
512 SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE);
518 /* did we do anything */
519 if (!s->s3->tmp.reuse_message && !skip)
523 if ((ret=BIO_flush(s->wbio)) <= 0)
527 if ((cb != NULL) && (s->state != state))
531 cb(s,SSL_CB_CONNECT_LOOP,1);
542 cb(s,SSL_CB_CONNECT_EXIT,ret);
546 int dtls1_client_hello(SSL *s)
551 unsigned long Time,l;
554 buf=(unsigned char *)s->init_buf->data;
555 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
557 if ((s->session == NULL) ||
558 (s->session->ssl_version != s->version) ||
559 (s->session->not_resumable))
561 if (!ssl_get_new_session(s,0))
564 /* else use the pre-loaded session */
566 p=s->s3->client_random;
568 /* if client_random is initialized, reuse it, we are
569 * required to use same upon reply to HelloVerify */
570 for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ;
571 if (i==sizeof(s->s3->client_random))
573 Time=(unsigned long)time(NULL); /* Time */
575 RAND_pseudo_bytes(p,sizeof(s->s3->client_random)-4);
578 /* Do the message type and length last */
579 d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
581 *(p++)=s->version>>8;
582 *(p++)=s->version&0xff;
583 s->client_version=s->version;
586 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
593 i=s->session->session_id_length;
597 if (i > sizeof s->session->session_id)
599 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
602 memcpy(p,s->session->session_id,i);
607 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
609 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
612 *(p++) = s->d1->cookie_len;
613 memcpy(p, s->d1->cookie, s->d1->cookie_len);
614 p += s->d1->cookie_len;
616 /* Ciphers supported */
617 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
620 SSLerr(SSL_F_DTLS1_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
627 if (s->ctx->comp_methods == NULL)
630 j=sk_SSL_COMP_num(s->ctx->comp_methods);
634 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
637 *(p++)=0; /* Add the NULL method */
642 d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l);
644 s->state=SSL3_ST_CW_CLNT_HELLO_B;
645 /* number of bytes to write */
649 /* buffer the message to handle re-xmits */
650 dtls1_buffer_message(s, 0);
653 /* SSL3_ST_CW_CLNT_HELLO_B */
654 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
659 static int dtls1_get_hello_verify(SSL *s)
663 unsigned int cookie_len;
665 n=s->method->ssl_get_message(s,
666 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
667 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
672 if (!ok) return((int)n);
674 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
676 s->d1->send_cookie = 0;
677 s->s3->tmp.reuse_message=1;
681 data = (unsigned char *)s->init_msg;
683 if ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff)))
685 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION);
686 s->version=(s->version&0xff00)|data[1];
687 al = SSL_AD_PROTOCOL_VERSION;
692 cookie_len = *(data++);
693 if ( cookie_len > sizeof(s->d1->cookie))
695 al=SSL_AD_ILLEGAL_PARAMETER;
699 memcpy(s->d1->cookie, data, cookie_len);
700 s->d1->cookie_len = cookie_len;
702 s->d1->send_cookie = 1;
706 ssl3_send_alert(s, SSL3_AL_FATAL, al);
710 int dtls1_send_client_key_exchange(SSL *s)
715 #ifndef OPENSSL_NO_RSA
719 #ifndef OPENSSL_NO_KRB5
721 #endif /* OPENSSL_NO_KRB5 */
722 #ifndef OPENSSL_NO_ECDH
723 EC_KEY *clnt_ecdh = NULL;
724 const EC_POINT *srvr_ecpoint = NULL;
725 EVP_PKEY *srvr_pub_pkey = NULL;
726 unsigned char *encodedPoint = NULL;
727 int encoded_pt_len = 0;
728 BN_CTX * bn_ctx = NULL;
731 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
733 d=(unsigned char *)s->init_buf->data;
734 p= &(d[DTLS1_HM_HEADER_LENGTH]);
736 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
738 /* Fool emacs indentation */
740 #ifndef OPENSSL_NO_RSA
741 else if (alg_k & SSL_kRSA)
744 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
746 if (s->session->sess_cert->peer_rsa_tmp != NULL)
747 rsa=s->session->sess_cert->peer_rsa_tmp;
750 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
751 if ((pkey == NULL) ||
752 (pkey->type != EVP_PKEY_RSA) ||
753 (pkey->pkey.rsa == NULL))
755 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
762 tmp_buf[0]=s->client_version>>8;
763 tmp_buf[1]=s->client_version&0xff;
764 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
767 s->session->master_key_length=sizeof tmp_buf;
770 /* Fix buf for TLS and [incidentally] DTLS */
771 if (s->version > SSL3_VERSION)
773 n=RSA_public_encrypt(sizeof tmp_buf,
774 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
776 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
777 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
781 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
785 /* Fix buf for TLS and [incidentally] DTLS */
786 if (s->version > SSL3_VERSION)
792 s->session->master_key_length=
793 s->method->ssl3_enc->generate_master_secret(s,
794 s->session->master_key,
795 tmp_buf,sizeof tmp_buf);
796 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
799 #ifndef OPENSSL_NO_KRB5
800 else if (alg_k & SSL_kKRB5)
802 krb5_error_code krb5rc;
803 KSSL_CTX *kssl_ctx = s->kssl_ctx;
804 /* krb5_data krb5_ap_req; */
805 krb5_data *enc_ticket;
806 krb5_data authenticator, *authp = NULL;
807 EVP_CIPHER_CTX ciph_ctx;
808 const EVP_CIPHER *enc = NULL;
809 unsigned char iv[EVP_MAX_IV_LENGTH];
810 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
811 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
812 + EVP_MAX_IV_LENGTH];
813 int padl, outl = sizeof(epms);
815 EVP_CIPHER_CTX_init(&ciph_ctx);
818 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
820 #endif /* KSSL_DEBUG */
824 if (KRB5SENDAUTH) authp = &authenticator;
825 #endif /* KRB5SENDAUTH */
827 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
829 enc = kssl_map_enc(kssl_ctx->enctype);
834 printf("kssl_cget_tkt rtn %d\n", krb5rc);
835 if (krb5rc && kssl_err.text)
836 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
838 #endif /* KSSL_DEBUG */
842 ssl3_send_alert(s,SSL3_AL_FATAL,
843 SSL_AD_HANDSHAKE_FAILURE);
844 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
849 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
850 ** in place of RFC 2712 KerberosWrapper, as in:
852 ** Send ticket (copy to *p, set n = length)
853 ** n = krb5_ap_req.length;
854 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
855 ** if (krb5_ap_req.data)
856 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
858 ** Now using real RFC 2712 KerberosWrapper
859 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
860 ** Note: 2712 "opaque" types are here replaced
861 ** with a 2-byte length followed by the value.
863 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
864 ** Where "xx xx" = length bytes. Shown here with
865 ** optional authenticator omitted.
868 /* KerberosWrapper.Ticket */
869 s2n(enc_ticket->length,p);
870 memcpy(p, enc_ticket->data, enc_ticket->length);
871 p+= enc_ticket->length;
872 n = enc_ticket->length + 2;
874 /* KerberosWrapper.Authenticator */
875 if (authp && authp->length)
877 s2n(authp->length,p);
878 memcpy(p, authp->data, authp->length);
880 n+= authp->length + 2;
888 s2n(0,p);/* null authenticator length */
892 if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0)
895 /* 20010420 VRS. Tried it this way; failed.
896 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
897 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
898 ** kssl_ctx->length);
899 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
902 memset(iv, 0, sizeof iv); /* per RFC 1510 */
903 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
905 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
907 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
909 if (outl > (int)sizeof epms)
911 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
914 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
916 /* KerberosWrapper.EncryptedPreMasterSecret */
918 memcpy(p, epms, outl);
922 s->session->master_key_length=
923 s->method->ssl3_enc->generate_master_secret(s,
924 s->session->master_key,
925 tmp_buf, sizeof tmp_buf);
927 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
928 OPENSSL_cleanse(epms, outl);
931 #ifndef OPENSSL_NO_DH
932 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
934 DH *dh_srvr,*dh_clnt;
936 if (s->session->sess_cert->peer_dh_tmp != NULL)
937 dh_srvr=s->session->sess_cert->peer_dh_tmp;
940 /* we get them from the cert */
941 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
942 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
946 /* generate a new random key */
947 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
949 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
952 if (!DH_generate_key(dh_clnt))
954 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
958 /* use the 'p' output buffer for the DH key, but
959 * make sure to clear it out afterwards */
961 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
965 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
969 /* generate master key from the result */
970 s->session->master_key_length=
971 s->method->ssl3_enc->generate_master_secret(s,
972 s->session->master_key,p,n);
976 /* send off the data */
977 n=BN_num_bytes(dh_clnt->pub_key);
979 BN_bn2bin(dh_clnt->pub_key,p);
984 /* perhaps clean things up a bit EAY EAY EAY EAY*/
987 #ifndef OPENSSL_NO_ECDH
988 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
990 const EC_GROUP *srvr_group = NULL;
992 int ecdh_clnt_cert = 0;
995 /* Did we send out the client's
996 * ECDH share for use in premaster
997 * computation as part of client certificate?
998 * If so, set ecdh_clnt_cert to 1.
1000 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
1002 /* XXX: For now, we do not support client
1003 * authentication using ECDH certificates.
1004 * To add such support, one needs to add
1005 * code that checks for appropriate
1006 * conditions and sets ecdh_clnt_cert to 1.
1007 * For example, the cert have an ECC
1008 * key on the same curve as the server's
1009 * and the key should be authorized for
1012 * One also needs to add code in ssl3_connect
1013 * to skip sending the certificate verify
1016 * if ((s->cert->key->privatekey != NULL) &&
1017 * (s->cert->key->privatekey->type ==
1018 * EVP_PKEY_EC) && ...)
1019 * ecdh_clnt_cert = 1;
1023 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
1025 tkey = s->session->sess_cert->peer_ecdh_tmp;
1029 /* Get the Server Public Key from Cert */
1030 srvr_pub_pkey = X509_get_pubkey(s->session-> \
1031 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1032 if ((srvr_pub_pkey == NULL) ||
1033 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
1034 (srvr_pub_pkey->pkey.ec == NULL))
1036 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1037 ERR_R_INTERNAL_ERROR);
1041 tkey = srvr_pub_pkey->pkey.ec;
1044 srvr_group = EC_KEY_get0_group(tkey);
1045 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1047 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
1049 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1050 ERR_R_INTERNAL_ERROR);
1054 if ((clnt_ecdh=EC_KEY_new()) == NULL)
1056 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1060 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
1062 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
1067 /* Reuse key info from our certificate
1068 * We only need our private key to perform
1069 * the ECDH computation.
1071 const BIGNUM *priv_key;
1072 tkey = s->cert->key->privatekey->pkey.ec;
1073 priv_key = EC_KEY_get0_private_key(tkey);
1074 if (priv_key == NULL)
1076 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1079 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
1081 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
1087 /* Generate a new ECDH key pair */
1088 if (!(EC_KEY_generate_key(clnt_ecdh)))
1090 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1095 /* use the 'p' output buffer for the ECDH key, but
1096 * make sure to clear it out afterwards
1099 field_size = EC_GROUP_get_degree(srvr_group);
1100 if (field_size <= 0)
1102 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1106 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
1109 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1114 /* generate master key from the result */
1115 s->session->master_key_length = s->method->ssl3_enc \
1116 -> generate_master_secret(s,
1117 s->session->master_key,
1120 memset(p, 0, n); /* clean up */
1124 /* Send empty client key exch message */
1129 /* First check the size of encoding and
1130 * allocate memory accordingly.
1133 EC_POINT_point2oct(srvr_group,
1134 EC_KEY_get0_public_key(clnt_ecdh),
1135 POINT_CONVERSION_UNCOMPRESSED,
1138 encodedPoint = (unsigned char *)
1139 OPENSSL_malloc(encoded_pt_len *
1140 sizeof(unsigned char));
1141 bn_ctx = BN_CTX_new();
1142 if ((encodedPoint == NULL) ||
1145 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1149 /* Encode the public key */
1150 n = EC_POINT_point2oct(srvr_group,
1151 EC_KEY_get0_public_key(clnt_ecdh),
1152 POINT_CONVERSION_UNCOMPRESSED,
1153 encodedPoint, encoded_pt_len, bn_ctx);
1155 *p = n; /* length of encoded point */
1156 /* Encoded point will be copied here */
1158 /* copy the point */
1159 memcpy((unsigned char *)p, encodedPoint, n);
1160 /* increment n to account for length field */
1164 /* Free allocated memory */
1165 BN_CTX_free(bn_ctx);
1166 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
1167 if (clnt_ecdh != NULL)
1168 EC_KEY_free(clnt_ecdh);
1169 EVP_PKEY_free(srvr_pub_pkey);
1171 #endif /* !OPENSSL_NO_ECDH */
1173 #ifndef OPENSSL_NO_PSK
1174 else if (alg_k & SSL_kPSK)
1176 char identity[PSK_MAX_IDENTITY_LEN];
1177 unsigned char *t = NULL;
1178 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
1179 unsigned int pre_ms_len = 0, psk_len = 0;
1183 if (s->psk_client_callback == NULL)
1185 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1186 SSL_R_PSK_NO_CLIENT_CB);
1190 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
1191 identity, PSK_MAX_IDENTITY_LEN,
1192 psk_or_pre_ms, sizeof(psk_or_pre_ms));
1193 if (psk_len > PSK_MAX_PSK_LEN)
1195 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1196 ERR_R_INTERNAL_ERROR);
1199 else if (psk_len == 0)
1201 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1202 SSL_R_PSK_IDENTITY_NOT_FOUND);
1206 /* create PSK pre_master_secret */
1207 pre_ms_len = 2+psk_len+2+psk_len;
1209 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
1211 memset(t, 0, psk_len);
1215 if (s->session->psk_identity_hint != NULL)
1216 OPENSSL_free(s->session->psk_identity_hint);
1217 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
1218 if (s->ctx->psk_identity_hint != NULL &&
1219 s->session->psk_identity_hint == NULL)
1221 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1222 ERR_R_MALLOC_FAILURE);
1226 if (s->session->psk_identity != NULL)
1227 OPENSSL_free(s->session->psk_identity);
1228 s->session->psk_identity = BUF_strdup(identity);
1229 if (s->session->psk_identity == NULL)
1231 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1232 ERR_R_MALLOC_FAILURE);
1236 s->session->master_key_length =
1237 s->method->ssl3_enc->generate_master_secret(s,
1238 s->session->master_key,
1239 psk_or_pre_ms, pre_ms_len);
1240 n = strlen(identity);
1242 memcpy(p, identity, n);
1246 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
1247 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
1250 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1257 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1258 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1262 d = dtls1_set_message_header(s, d,
1263 SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n);
1265 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1267 l2n(s->d1->handshake_write_seq,d);
1268 s->d1->handshake_write_seq++;
1271 s->state=SSL3_ST_CW_KEY_EXCH_B;
1272 /* number of bytes to write */
1273 s->init_num=n+DTLS1_HM_HEADER_LENGTH;
1276 /* buffer the message to handle re-xmits */
1277 dtls1_buffer_message(s, 0);
1280 /* SSL3_ST_CW_KEY_EXCH_B */
1281 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1283 #ifndef OPENSSL_NO_ECDH
1284 BN_CTX_free(bn_ctx);
1285 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
1286 if (clnt_ecdh != NULL)
1287 EC_KEY_free(clnt_ecdh);
1288 EVP_PKEY_free(srvr_pub_pkey);
1293 int dtls1_send_client_verify(SSL *s)
1295 unsigned char *p,*d;
1296 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1298 #ifndef OPENSSL_NO_RSA
1302 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
1306 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1308 d=(unsigned char *)s->init_buf->data;
1309 p= &(d[DTLS1_HM_HEADER_LENGTH]);
1310 pkey=s->cert->key->privatekey;
1312 s->method->ssl3_enc->cert_verify_mac(s,
1314 &(data[MD5_DIGEST_LENGTH]));
1316 #ifndef OPENSSL_NO_RSA
1317 if (pkey->type == EVP_PKEY_RSA)
1319 s->method->ssl3_enc->cert_verify_mac(s,
1322 if (RSA_sign(NID_md5_sha1, data,
1323 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1324 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
1326 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1334 #ifndef OPENSSL_NO_DSA
1335 if (pkey->type == EVP_PKEY_DSA)
1337 if (!DSA_sign(pkey->save_type,
1338 &(data[MD5_DIGEST_LENGTH]),
1339 SHA_DIGEST_LENGTH,&(p[2]),
1340 (unsigned int *)&j,pkey->pkey.dsa))
1342 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1350 #ifndef OPENSSL_NO_ECDSA
1351 if (pkey->type == EVP_PKEY_EC)
1353 if (!ECDSA_sign(pkey->save_type,
1354 &(data[MD5_DIGEST_LENGTH]),
1355 SHA_DIGEST_LENGTH,&(p[2]),
1356 (unsigned int *)&j,pkey->pkey.ec))
1358 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,
1368 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
1372 d = dtls1_set_message_header(s, d,
1373 SSL3_MT_CERTIFICATE_VERIFY, n, 0, n) ;
1375 s->init_num=(int)n+DTLS1_HM_HEADER_LENGTH;
1378 /* buffer the message to handle re-xmits */
1379 dtls1_buffer_message(s, 0);
1381 s->state = SSL3_ST_CW_CERT_VRFY_B;
1384 /* s->state = SSL3_ST_CW_CERT_VRFY_B */
1385 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1390 int dtls1_send_client_certificate(SSL *s)
1393 EVP_PKEY *pkey=NULL;
1397 if (s->state == SSL3_ST_CW_CERT_A)
1399 if ((s->cert == NULL) ||
1400 (s->cert->key->x509 == NULL) ||
1401 (s->cert->key->privatekey == NULL))
1402 s->state=SSL3_ST_CW_CERT_B;
1404 s->state=SSL3_ST_CW_CERT_C;
1407 /* We need to get a client cert */
1408 if (s->state == SSL3_ST_CW_CERT_B)
1410 /* If we get an error, we need to
1411 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1412 * We then get retied later */
1414 i = ssl_do_client_cert_cb(s, &x509, &pkey);
1417 s->rwstate=SSL_X509_LOOKUP;
1420 s->rwstate=SSL_NOTHING;
1421 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1423 s->state=SSL3_ST_CW_CERT_B;
1424 if ( !SSL_use_certificate(s,x509) ||
1425 !SSL_use_PrivateKey(s,pkey))
1431 SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1434 if (x509 != NULL) X509_free(x509);
1435 if (pkey != NULL) EVP_PKEY_free(pkey);
1438 if (s->version == SSL3_VERSION)
1440 s->s3->tmp.cert_req=0;
1441 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1446 s->s3->tmp.cert_req=2;
1450 /* Ok, we have a cert */
1451 s->state=SSL3_ST_CW_CERT_C;
1454 if (s->state == SSL3_ST_CW_CERT_C)
1456 s->state=SSL3_ST_CW_CERT_D;
1457 l=dtls1_output_cert_chain(s,
1458 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1462 /* set header called by dtls1_output_cert_chain() */
1464 /* buffer the message to handle re-xmits */
1465 dtls1_buffer_message(s, 0);
1467 /* SSL3_ST_CW_CERT_D */
1468 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));