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);
151 void (*cb)(const SSL *ssl,int type,int val)=NULL;
153 int new_state,state,skip=0;;
155 RAND_add(&Time,sizeof(Time),0);
159 if (s->info_callback != NULL)
161 else if (s->ctx->info_callback != NULL)
162 cb=s->ctx->info_callback;
165 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
173 case SSL_ST_RENEGOTIATE:
175 s->state=SSL_ST_CONNECT;
176 s->ctx->stats.sess_connect_renegotiate++;
180 case SSL_ST_BEFORE|SSL_ST_CONNECT:
181 case SSL_ST_OK|SSL_ST_CONNECT:
184 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
186 if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) &&
187 (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00))
189 SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);
194 /* s->version=SSL3_VERSION; */
195 s->type=SSL_ST_CONNECT;
197 if (s->init_buf == NULL)
199 if ((buf=BUF_MEM_new()) == NULL)
204 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
213 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
215 /* setup buffing BIO */
216 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
218 /* don't push the buffering BIO quite yet */
220 s->state=SSL3_ST_CW_CLNT_HELLO_A;
221 s->ctx->stats.sess_connect++;
223 /* mark client_random uninitialized */
224 memset(s->s3->client_random,0,sizeof(s->s3->client_random));
225 s->d1->send_cookie = 0;
229 case SSL3_ST_CW_CLNT_HELLO_A:
230 case SSL3_ST_CW_CLNT_HELLO_B:
234 /* every DTLS ClientHello resets Finished MAC */
235 ssl3_init_finished_mac(s);
237 dtls1_start_timer(s);
238 ret=dtls1_client_hello(s);
239 if (ret <= 0) goto end;
241 if ( s->d1->send_cookie)
243 s->state=SSL3_ST_CW_FLUSH;
244 s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A;
247 s->state=SSL3_ST_CR_SRVR_HELLO_A;
251 /* turn on buffering for the next lot of output */
252 if (s->bbio != s->wbio)
253 s->wbio=BIO_push(s->bbio,s->wbio);
257 case SSL3_ST_CR_SRVR_HELLO_A:
258 case SSL3_ST_CR_SRVR_HELLO_B:
259 ret=ssl3_get_server_hello(s);
260 if (ret <= 0) goto end;
265 s->state=SSL3_ST_CR_FINISHED_A;
267 s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
272 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
273 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
275 ret = dtls1_get_hello_verify(s);
279 if ( s->d1->send_cookie) /* start again, with a cookie */
280 s->state=SSL3_ST_CW_CLNT_HELLO_A;
282 s->state = SSL3_ST_CR_CERT_A;
286 case SSL3_ST_CR_CERT_A:
287 case SSL3_ST_CR_CERT_B:
288 #ifndef OPENSSL_NO_TLSEXT
289 ret=ssl3_check_finished(s);
290 if (ret <= 0) goto end;
294 if (s->tlsext_ticket_expected)
295 s->state=SSL3_ST_CR_SESSION_TICKET_A;
297 s->state=SSL3_ST_CR_FINISHED_A;
302 /* Check if it is anon DH or PSK */
303 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
304 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
306 ret=ssl3_get_server_certificate(s);
307 if (ret <= 0) goto end;
308 #ifndef OPENSSL_NO_TLSEXT
309 if (s->tlsext_status_expected)
310 s->state=SSL3_ST_CR_CERT_STATUS_A;
312 s->state=SSL3_ST_CR_KEY_EXCH_A;
317 s->state=SSL3_ST_CR_KEY_EXCH_A;
324 s->state=SSL3_ST_CR_KEY_EXCH_A;
329 case SSL3_ST_CR_KEY_EXCH_A:
330 case SSL3_ST_CR_KEY_EXCH_B:
331 ret=ssl3_get_key_exchange(s);
332 if (ret <= 0) goto end;
333 s->state=SSL3_ST_CR_CERT_REQ_A;
336 /* at this point we check that we have the
337 * required stuff from the server */
338 if (!ssl3_check_cert_and_algorithm(s))
345 case SSL3_ST_CR_CERT_REQ_A:
346 case SSL3_ST_CR_CERT_REQ_B:
347 ret=ssl3_get_certificate_request(s);
348 if (ret <= 0) goto end;
349 s->state=SSL3_ST_CR_SRVR_DONE_A;
353 case SSL3_ST_CR_SRVR_DONE_A:
354 case SSL3_ST_CR_SRVR_DONE_B:
355 ret=ssl3_get_server_done(s);
356 if (ret <= 0) goto end;
357 if (s->s3->tmp.cert_req)
358 s->state=SSL3_ST_CW_CERT_A;
360 s->state=SSL3_ST_CW_KEY_EXCH_A;
365 case SSL3_ST_CW_CERT_A:
366 case SSL3_ST_CW_CERT_B:
367 case SSL3_ST_CW_CERT_C:
368 case SSL3_ST_CW_CERT_D:
369 dtls1_start_timer(s);
370 ret=dtls1_send_client_certificate(s);
371 if (ret <= 0) goto end;
372 s->state=SSL3_ST_CW_KEY_EXCH_A;
376 case SSL3_ST_CW_KEY_EXCH_A:
377 case SSL3_ST_CW_KEY_EXCH_B:
378 dtls1_start_timer(s);
379 ret=dtls1_send_client_key_exchange(s);
380 if (ret <= 0) goto end;
381 /* EAY EAY EAY need to check for DH fix cert
383 /* For TLS, cert_req is set to 2, so a cert chain
384 * of nothing is sent, but no verify packet is sent */
385 if (s->s3->tmp.cert_req == 1)
387 s->state=SSL3_ST_CW_CERT_VRFY_A;
391 s->state=SSL3_ST_CW_CHANGE_A;
392 s->s3->change_cipher_spec=0;
398 case SSL3_ST_CW_CERT_VRFY_A:
399 case SSL3_ST_CW_CERT_VRFY_B:
400 dtls1_start_timer(s);
401 ret=dtls1_send_client_verify(s);
402 if (ret <= 0) goto end;
403 s->state=SSL3_ST_CW_CHANGE_A;
405 s->s3->change_cipher_spec=0;
408 case SSL3_ST_CW_CHANGE_A:
409 case SSL3_ST_CW_CHANGE_B:
410 dtls1_start_timer(s);
411 ret=dtls1_send_change_cipher_spec(s,
412 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
413 if (ret <= 0) goto end;
414 s->state=SSL3_ST_CW_FINISHED_A;
417 s->session->cipher=s->s3->tmp.new_cipher;
418 #ifdef OPENSSL_NO_COMP
419 s->session->compress_meth=0;
421 if (s->s3->tmp.new_compression == NULL)
422 s->session->compress_meth=0;
424 s->session->compress_meth=
425 s->s3->tmp.new_compression->id;
427 if (!s->method->ssl3_enc->setup_key_block(s))
433 if (!s->method->ssl3_enc->change_cipher_state(s,
434 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
440 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
443 case SSL3_ST_CW_FINISHED_A:
444 case SSL3_ST_CW_FINISHED_B:
445 dtls1_start_timer(s);
446 ret=dtls1_send_finished(s,
447 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
448 s->method->ssl3_enc->client_finished_label,
449 s->method->ssl3_enc->client_finished_label_len);
450 if (ret <= 0) goto end;
451 s->state=SSL3_ST_CW_FLUSH;
454 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
457 s->s3->tmp.next_state=SSL_ST_OK;
458 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
461 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
462 s->s3->delay_buf_pop_ret=0;
467 #ifndef OPENSSL_NO_TLSEXT
468 /* Allow NewSessionTicket if ticket expected */
469 if (s->tlsext_ticket_expected)
470 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
474 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
479 #ifndef OPENSSL_NO_TLSEXT
480 case SSL3_ST_CR_SESSION_TICKET_A:
481 case SSL3_ST_CR_SESSION_TICKET_B:
482 ret=ssl3_get_new_session_ticket(s);
483 if (ret <= 0) goto end;
484 s->state=SSL3_ST_CR_FINISHED_A;
488 case SSL3_ST_CR_CERT_STATUS_A:
489 case SSL3_ST_CR_CERT_STATUS_B:
490 ret=ssl3_get_cert_status(s);
491 if (ret <= 0) goto end;
492 s->state=SSL3_ST_CR_KEY_EXCH_A;
497 case SSL3_ST_CR_FINISHED_A:
498 case SSL3_ST_CR_FINISHED_B:
499 s->d1->change_cipher_spec_ok = 1;
500 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
501 SSL3_ST_CR_FINISHED_B);
502 if (ret <= 0) goto end;
506 s->state=SSL3_ST_CW_CHANGE_A;
512 case SSL3_ST_CW_FLUSH:
513 s->rwstate=SSL_WRITING;
514 if (BIO_flush(s->wbio) <= 0)
519 s->rwstate=SSL_NOTHING;
520 s->state=s->s3->tmp.next_state;
524 /* clean a few things up */
525 ssl3_cleanup_key_block(s);
528 if (s->init_buf != NULL)
530 BUF_MEM_free(s->init_buf);
535 /* If we are not 'joining' the last two packets,
536 * remove the buffering now */
537 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
538 ssl_free_wbio_buffer(s);
539 /* else do it later in ssl3_write */
544 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
545 if (s->hit) s->ctx->stats.sess_hit++;
549 s->handshake_func=dtls1_connect;
550 s->ctx->stats.sess_connect_good++;
552 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
554 /* done with handshaking */
555 s->d1->handshake_read_seq = 0;
556 s->d1->next_handshake_write_seq = 0;
561 SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE);
567 /* did we do anything */
568 if (!s->s3->tmp.reuse_message && !skip)
572 if ((ret=BIO_flush(s->wbio)) <= 0)
576 if ((cb != NULL) && (s->state != state))
580 cb(s,SSL_CB_CONNECT_LOOP,1);
591 cb(s,SSL_CB_CONNECT_EXIT,ret);
595 int dtls1_client_hello(SSL *s)
600 unsigned long Time,l;
603 buf=(unsigned char *)s->init_buf->data;
604 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
606 SSL_SESSION *sess = s->session;
607 if ((s->session == NULL) ||
608 (s->session->ssl_version != s->version) ||
609 #ifdef OPENSSL_NO_TLSEXT
610 !sess->session_id_length ||
612 (!sess->session_id_length && !sess->tlsext_tick) ||
614 (s->session->not_resumable))
616 if (!ssl_get_new_session(s,0))
619 /* else use the pre-loaded session */
621 p=s->s3->client_random;
623 /* if client_random is initialized, reuse it, we are
624 * required to use same upon reply to HelloVerify */
625 for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ;
626 if (i==sizeof(s->s3->client_random))
628 Time=(unsigned long)time(NULL); /* Time */
630 RAND_pseudo_bytes(p,sizeof(s->s3->client_random)-4);
633 /* Do the message type and length last */
634 d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
636 *(p++)=s->version>>8;
637 *(p++)=s->version&0xff;
638 s->client_version=s->version;
641 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
648 i=s->session->session_id_length;
652 if (i > sizeof s->session->session_id)
654 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
657 memcpy(p,s->session->session_id,i);
662 if ( s->d1->cookie_len > sizeof(s->d1->cookie))
664 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
667 *(p++) = s->d1->cookie_len;
668 memcpy(p, s->d1->cookie, s->d1->cookie_len);
669 p += s->d1->cookie_len;
671 /* Ciphers supported */
672 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
675 SSLerr(SSL_F_DTLS1_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
682 if (s->ctx->comp_methods == NULL)
685 j=sk_SSL_COMP_num(s->ctx->comp_methods);
689 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
692 *(p++)=0; /* Add the NULL method */
694 #ifndef OPENSSL_NO_TLSEXT
695 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
697 SSLerr(SSL_F_DTLS1_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
705 d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l);
707 s->state=SSL3_ST_CW_CLNT_HELLO_B;
708 /* number of bytes to write */
712 /* buffer the message to handle re-xmits */
713 dtls1_buffer_message(s, 0);
716 /* SSL3_ST_CW_CLNT_HELLO_B */
717 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
722 static int dtls1_get_hello_verify(SSL *s)
726 unsigned int cookie_len;
728 n=s->method->ssl_get_message(s,
729 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
730 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
735 if (!ok) return((int)n);
737 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
739 s->d1->send_cookie = 0;
740 s->s3->tmp.reuse_message=1;
744 data = (unsigned char *)s->init_msg;
746 if ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff)))
748 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION);
749 s->version=(s->version&0xff00)|data[1];
750 al = SSL_AD_PROTOCOL_VERSION;
755 cookie_len = *(data++);
756 if ( cookie_len > sizeof(s->d1->cookie))
758 al=SSL_AD_ILLEGAL_PARAMETER;
762 memcpy(s->d1->cookie, data, cookie_len);
763 s->d1->cookie_len = cookie_len;
765 s->d1->send_cookie = 1;
769 ssl3_send_alert(s, SSL3_AL_FATAL, al);
773 int dtls1_send_client_key_exchange(SSL *s)
778 #ifndef OPENSSL_NO_RSA
782 #ifndef OPENSSL_NO_KRB5
784 #endif /* OPENSSL_NO_KRB5 */
785 #ifndef OPENSSL_NO_ECDH
786 EC_KEY *clnt_ecdh = NULL;
787 const EC_POINT *srvr_ecpoint = NULL;
788 EVP_PKEY *srvr_pub_pkey = NULL;
789 unsigned char *encodedPoint = NULL;
790 int encoded_pt_len = 0;
791 BN_CTX * bn_ctx = NULL;
794 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
796 d=(unsigned char *)s->init_buf->data;
797 p= &(d[DTLS1_HM_HEADER_LENGTH]);
799 alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
801 /* Fool emacs indentation */
803 #ifndef OPENSSL_NO_RSA
804 else if (alg_k & SSL_kRSA)
807 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
809 if (s->session->sess_cert->peer_rsa_tmp != NULL)
810 rsa=s->session->sess_cert->peer_rsa_tmp;
813 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
814 if ((pkey == NULL) ||
815 (pkey->type != EVP_PKEY_RSA) ||
816 (pkey->pkey.rsa == NULL))
818 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
825 tmp_buf[0]=s->client_version>>8;
826 tmp_buf[1]=s->client_version&0xff;
827 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
830 s->session->master_key_length=sizeof tmp_buf;
833 /* Fix buf for TLS and [incidentally] DTLS */
834 if (s->version > SSL3_VERSION)
836 n=RSA_public_encrypt(sizeof tmp_buf,
837 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
839 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
840 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
844 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
848 /* Fix buf for TLS and [incidentally] DTLS */
849 if (s->version > SSL3_VERSION)
855 s->session->master_key_length=
856 s->method->ssl3_enc->generate_master_secret(s,
857 s->session->master_key,
858 tmp_buf,sizeof tmp_buf);
859 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
862 #ifndef OPENSSL_NO_KRB5
863 else if (alg_k & SSL_kKRB5)
865 krb5_error_code krb5rc;
866 KSSL_CTX *kssl_ctx = s->kssl_ctx;
867 /* krb5_data krb5_ap_req; */
868 krb5_data *enc_ticket;
869 krb5_data authenticator, *authp = NULL;
870 EVP_CIPHER_CTX ciph_ctx;
871 const EVP_CIPHER *enc = NULL;
872 unsigned char iv[EVP_MAX_IV_LENGTH];
873 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
874 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
875 + EVP_MAX_IV_LENGTH];
876 int padl, outl = sizeof(epms);
878 EVP_CIPHER_CTX_init(&ciph_ctx);
881 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
883 #endif /* KSSL_DEBUG */
887 if (KRB5SENDAUTH) authp = &authenticator;
888 #endif /* KRB5SENDAUTH */
890 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
892 enc = kssl_map_enc(kssl_ctx->enctype);
897 printf("kssl_cget_tkt rtn %d\n", krb5rc);
898 if (krb5rc && kssl_err.text)
899 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
901 #endif /* KSSL_DEBUG */
905 ssl3_send_alert(s,SSL3_AL_FATAL,
906 SSL_AD_HANDSHAKE_FAILURE);
907 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
912 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
913 ** in place of RFC 2712 KerberosWrapper, as in:
915 ** Send ticket (copy to *p, set n = length)
916 ** n = krb5_ap_req.length;
917 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
918 ** if (krb5_ap_req.data)
919 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
921 ** Now using real RFC 2712 KerberosWrapper
922 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
923 ** Note: 2712 "opaque" types are here replaced
924 ** with a 2-byte length followed by the value.
926 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
927 ** Where "xx xx" = length bytes. Shown here with
928 ** optional authenticator omitted.
931 /* KerberosWrapper.Ticket */
932 s2n(enc_ticket->length,p);
933 memcpy(p, enc_ticket->data, enc_ticket->length);
934 p+= enc_ticket->length;
935 n = enc_ticket->length + 2;
937 /* KerberosWrapper.Authenticator */
938 if (authp && authp->length)
940 s2n(authp->length,p);
941 memcpy(p, authp->data, authp->length);
943 n+= authp->length + 2;
951 s2n(0,p);/* null authenticator length */
955 if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0)
958 /* 20010420 VRS. Tried it this way; failed.
959 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
960 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
961 ** kssl_ctx->length);
962 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
965 memset(iv, 0, sizeof iv); /* per RFC 1510 */
966 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
968 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
970 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
972 if (outl > (int)sizeof epms)
974 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
977 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
979 /* KerberosWrapper.EncryptedPreMasterSecret */
981 memcpy(p, epms, outl);
985 s->session->master_key_length=
986 s->method->ssl3_enc->generate_master_secret(s,
987 s->session->master_key,
988 tmp_buf, sizeof tmp_buf);
990 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
991 OPENSSL_cleanse(epms, outl);
994 #ifndef OPENSSL_NO_DH
995 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
997 DH *dh_srvr,*dh_clnt;
999 if (s->session->sess_cert->peer_dh_tmp != NULL)
1000 dh_srvr=s->session->sess_cert->peer_dh_tmp;
1003 /* we get them from the cert */
1004 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1005 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1009 /* generate a new random key */
1010 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1012 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1015 if (!DH_generate_key(dh_clnt))
1017 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1021 /* use the 'p' output buffer for the DH key, but
1022 * make sure to clear it out afterwards */
1024 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1028 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1032 /* generate master key from the result */
1033 s->session->master_key_length=
1034 s->method->ssl3_enc->generate_master_secret(s,
1035 s->session->master_key,p,n);
1039 /* send off the data */
1040 n=BN_num_bytes(dh_clnt->pub_key);
1042 BN_bn2bin(dh_clnt->pub_key,p);
1047 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1050 #ifndef OPENSSL_NO_ECDH
1051 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
1053 const EC_GROUP *srvr_group = NULL;
1055 int ecdh_clnt_cert = 0;
1058 /* Did we send out the client's
1059 * ECDH share for use in premaster
1060 * computation as part of client certificate?
1061 * If so, set ecdh_clnt_cert to 1.
1063 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
1065 /* XXX: For now, we do not support client
1066 * authentication using ECDH certificates.
1067 * To add such support, one needs to add
1068 * code that checks for appropriate
1069 * conditions and sets ecdh_clnt_cert to 1.
1070 * For example, the cert have an ECC
1071 * key on the same curve as the server's
1072 * and the key should be authorized for
1075 * One also needs to add code in ssl3_connect
1076 * to skip sending the certificate verify
1079 * if ((s->cert->key->privatekey != NULL) &&
1080 * (s->cert->key->privatekey->type ==
1081 * EVP_PKEY_EC) && ...)
1082 * ecdh_clnt_cert = 1;
1086 if (s->session->sess_cert->peer_ecdh_tmp != NULL)
1088 tkey = s->session->sess_cert->peer_ecdh_tmp;
1092 /* Get the Server Public Key from Cert */
1093 srvr_pub_pkey = X509_get_pubkey(s->session-> \
1094 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1095 if ((srvr_pub_pkey == NULL) ||
1096 (srvr_pub_pkey->type != EVP_PKEY_EC) ||
1097 (srvr_pub_pkey->pkey.ec == NULL))
1099 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1100 ERR_R_INTERNAL_ERROR);
1104 tkey = srvr_pub_pkey->pkey.ec;
1107 srvr_group = EC_KEY_get0_group(tkey);
1108 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1110 if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
1112 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1113 ERR_R_INTERNAL_ERROR);
1117 if ((clnt_ecdh=EC_KEY_new()) == NULL)
1119 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1123 if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
1125 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
1130 /* Reuse key info from our certificate
1131 * We only need our private key to perform
1132 * the ECDH computation.
1134 const BIGNUM *priv_key;
1135 tkey = s->cert->key->privatekey->pkey.ec;
1136 priv_key = EC_KEY_get0_private_key(tkey);
1137 if (priv_key == NULL)
1139 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1142 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
1144 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
1150 /* Generate a new ECDH key pair */
1151 if (!(EC_KEY_generate_key(clnt_ecdh)))
1153 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1158 /* use the 'p' output buffer for the ECDH key, but
1159 * make sure to clear it out afterwards
1162 field_size = EC_GROUP_get_degree(srvr_group);
1163 if (field_size <= 0)
1165 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1169 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
1172 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1177 /* generate master key from the result */
1178 s->session->master_key_length = s->method->ssl3_enc \
1179 -> generate_master_secret(s,
1180 s->session->master_key,
1183 memset(p, 0, n); /* clean up */
1187 /* Send empty client key exch message */
1192 /* First check the size of encoding and
1193 * allocate memory accordingly.
1196 EC_POINT_point2oct(srvr_group,
1197 EC_KEY_get0_public_key(clnt_ecdh),
1198 POINT_CONVERSION_UNCOMPRESSED,
1201 encodedPoint = (unsigned char *)
1202 OPENSSL_malloc(encoded_pt_len *
1203 sizeof(unsigned char));
1204 bn_ctx = BN_CTX_new();
1205 if ((encodedPoint == NULL) ||
1208 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1212 /* Encode the public key */
1213 n = EC_POINT_point2oct(srvr_group,
1214 EC_KEY_get0_public_key(clnt_ecdh),
1215 POINT_CONVERSION_UNCOMPRESSED,
1216 encodedPoint, encoded_pt_len, bn_ctx);
1218 *p = n; /* length of encoded point */
1219 /* Encoded point will be copied here */
1221 /* copy the point */
1222 memcpy((unsigned char *)p, encodedPoint, n);
1223 /* increment n to account for length field */
1227 /* Free allocated memory */
1228 BN_CTX_free(bn_ctx);
1229 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
1230 if (clnt_ecdh != NULL)
1231 EC_KEY_free(clnt_ecdh);
1232 EVP_PKEY_free(srvr_pub_pkey);
1234 #endif /* !OPENSSL_NO_ECDH */
1236 #ifndef OPENSSL_NO_PSK
1237 else if (alg_k & SSL_kPSK)
1239 char identity[PSK_MAX_IDENTITY_LEN];
1240 unsigned char *t = NULL;
1241 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
1242 unsigned int pre_ms_len = 0, psk_len = 0;
1246 if (s->psk_client_callback == NULL)
1248 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1249 SSL_R_PSK_NO_CLIENT_CB);
1253 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
1254 identity, PSK_MAX_IDENTITY_LEN,
1255 psk_or_pre_ms, sizeof(psk_or_pre_ms));
1256 if (psk_len > PSK_MAX_PSK_LEN)
1258 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1259 ERR_R_INTERNAL_ERROR);
1262 else if (psk_len == 0)
1264 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1265 SSL_R_PSK_IDENTITY_NOT_FOUND);
1269 /* create PSK pre_master_secret */
1270 pre_ms_len = 2+psk_len+2+psk_len;
1272 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
1274 memset(t, 0, psk_len);
1278 if (s->session->psk_identity_hint != NULL)
1279 OPENSSL_free(s->session->psk_identity_hint);
1280 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
1281 if (s->ctx->psk_identity_hint != NULL &&
1282 s->session->psk_identity_hint == NULL)
1284 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1285 ERR_R_MALLOC_FAILURE);
1289 if (s->session->psk_identity != NULL)
1290 OPENSSL_free(s->session->psk_identity);
1291 s->session->psk_identity = BUF_strdup(identity);
1292 if (s->session->psk_identity == NULL)
1294 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1295 ERR_R_MALLOC_FAILURE);
1299 s->session->master_key_length =
1300 s->method->ssl3_enc->generate_master_secret(s,
1301 s->session->master_key,
1302 psk_or_pre_ms, pre_ms_len);
1303 n = strlen(identity);
1305 memcpy(p, identity, n);
1309 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
1310 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
1313 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1320 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1321 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1325 d = dtls1_set_message_header(s, d,
1326 SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n);
1328 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1330 l2n(s->d1->handshake_write_seq,d);
1331 s->d1->handshake_write_seq++;
1334 s->state=SSL3_ST_CW_KEY_EXCH_B;
1335 /* number of bytes to write */
1336 s->init_num=n+DTLS1_HM_HEADER_LENGTH;
1339 /* buffer the message to handle re-xmits */
1340 dtls1_buffer_message(s, 0);
1343 /* SSL3_ST_CW_KEY_EXCH_B */
1344 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1346 #ifndef OPENSSL_NO_ECDH
1347 BN_CTX_free(bn_ctx);
1348 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
1349 if (clnt_ecdh != NULL)
1350 EC_KEY_free(clnt_ecdh);
1351 EVP_PKEY_free(srvr_pub_pkey);
1356 int dtls1_send_client_verify(SSL *s)
1358 unsigned char *p,*d;
1359 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1361 #ifndef OPENSSL_NO_RSA
1365 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
1369 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1371 d=(unsigned char *)s->init_buf->data;
1372 p= &(d[DTLS1_HM_HEADER_LENGTH]);
1373 pkey=s->cert->key->privatekey;
1375 s->method->ssl3_enc->cert_verify_mac(s,
1377 &(data[MD5_DIGEST_LENGTH]));
1379 #ifndef OPENSSL_NO_RSA
1380 if (pkey->type == EVP_PKEY_RSA)
1382 s->method->ssl3_enc->cert_verify_mac(s,
1385 if (RSA_sign(NID_md5_sha1, data,
1386 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1387 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
1389 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1397 #ifndef OPENSSL_NO_DSA
1398 if (pkey->type == EVP_PKEY_DSA)
1400 if (!DSA_sign(pkey->save_type,
1401 &(data[MD5_DIGEST_LENGTH]),
1402 SHA_DIGEST_LENGTH,&(p[2]),
1403 (unsigned int *)&j,pkey->pkey.dsa))
1405 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1413 #ifndef OPENSSL_NO_ECDSA
1414 if (pkey->type == EVP_PKEY_EC)
1416 if (!ECDSA_sign(pkey->save_type,
1417 &(data[MD5_DIGEST_LENGTH]),
1418 SHA_DIGEST_LENGTH,&(p[2]),
1419 (unsigned int *)&j,pkey->pkey.ec))
1421 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,
1431 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
1435 d = dtls1_set_message_header(s, d,
1436 SSL3_MT_CERTIFICATE_VERIFY, n, 0, n) ;
1438 s->init_num=(int)n+DTLS1_HM_HEADER_LENGTH;
1441 /* buffer the message to handle re-xmits */
1442 dtls1_buffer_message(s, 0);
1444 s->state = SSL3_ST_CW_CERT_VRFY_B;
1447 /* s->state = SSL3_ST_CW_CERT_VRFY_B */
1448 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1453 int dtls1_send_client_certificate(SSL *s)
1456 EVP_PKEY *pkey=NULL;
1460 if (s->state == SSL3_ST_CW_CERT_A)
1462 if ((s->cert == NULL) ||
1463 (s->cert->key->x509 == NULL) ||
1464 (s->cert->key->privatekey == NULL))
1465 s->state=SSL3_ST_CW_CERT_B;
1467 s->state=SSL3_ST_CW_CERT_C;
1470 /* We need to get a client cert */
1471 if (s->state == SSL3_ST_CW_CERT_B)
1473 /* If we get an error, we need to
1474 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1475 * We then get retied later */
1477 i = ssl_do_client_cert_cb(s, &x509, &pkey);
1480 s->rwstate=SSL_X509_LOOKUP;
1483 s->rwstate=SSL_NOTHING;
1484 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1486 s->state=SSL3_ST_CW_CERT_B;
1487 if ( !SSL_use_certificate(s,x509) ||
1488 !SSL_use_PrivateKey(s,pkey))
1494 SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1497 if (x509 != NULL) X509_free(x509);
1498 if (pkey != NULL) EVP_PKEY_free(pkey);
1501 if (s->version == SSL3_VERSION)
1503 s->s3->tmp.cert_req=0;
1504 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1509 s->s3->tmp.cert_req=2;
1513 /* Ok, we have a cert */
1514 s->state=SSL3_ST_CW_CERT_C;
1517 if (s->state == SSL3_ST_CW_CERT_C)
1519 s->state=SSL3_ST_CW_CERT_D;
1520 l=dtls1_output_cert_chain(s,
1521 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1525 /* set header called by dtls1_output_cert_chain() */
1527 /* buffer the message to handle re-xmits */
1528 dtls1_buffer_message(s, 0);
1530 /* SSL3_ST_CW_CERT_D */
1531 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));