sl@0: /* ssl/d1_lib.c */
sl@0: /* 
sl@0:  * DTLS implementation written by Nagendra Modadugu
sl@0:  * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.  
sl@0:  */
sl@0: /* ====================================================================
sl@0:  * Copyright (c) 1999-2005 The OpenSSL Project.  All rights reserved.
sl@0:  *
sl@0:  * Redistribution and use in source and binary forms, with or without
sl@0:  * modification, are permitted provided that the following conditions
sl@0:  * are met:
sl@0:  *
sl@0:  * 1. Redistributions of source code must retain the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer. 
sl@0:  *
sl@0:  * 2. Redistributions in binary form must reproduce the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer in
sl@0:  *    the documentation and/or other materials provided with the
sl@0:  *    distribution.
sl@0:  *
sl@0:  * 3. All advertising materials mentioning features or use of this
sl@0:  *    software must display the following acknowledgment:
sl@0:  *    "This product includes software developed by the OpenSSL Project
sl@0:  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
sl@0:  *
sl@0:  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0:  *    endorse or promote products derived from this software without
sl@0:  *    prior written permission. For written permission, please contact
sl@0:  *    openssl-core@OpenSSL.org.
sl@0:  *
sl@0:  * 5. Products derived from this software may not be called "OpenSSL"
sl@0:  *    nor may "OpenSSL" appear in their names without prior written
sl@0:  *    permission of the OpenSSL Project.
sl@0:  *
sl@0:  * 6. Redistributions of any form whatsoever must retain the following
sl@0:  *    acknowledgment:
sl@0:  *    "This product includes software developed by the OpenSSL Project
sl@0:  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
sl@0:  *
sl@0:  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0:  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0:  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0:  * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0:  * ====================================================================
sl@0:  *
sl@0:  * This product includes cryptographic software written by Eric Young
sl@0:  * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0:  * Hudson (tjh@cryptsoft.com).
sl@0:  *
sl@0:  */
sl@0: /*
sl@0:  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0:  */
sl@0:  
sl@0: #include <stdio.h>
sl@0: #include <openssl/objects.h>
sl@0: #include "ssl_locl.h"
sl@0: 
sl@0: const char dtls1_version_str[]="DTLSv1" OPENSSL_VERSION_PTEXT;
sl@0: 
sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0: #include "libssl_wsd.h"
sl@0: #endif 
sl@0: 
sl@0: #ifdef EMULATOR
sl@0: 	
sl@0: 	GET_STATIC_VAR_FROM_TLS(dtlsv1_base_method_data,d1_lib,SSL_METHOD)
sl@0: 	
sl@0: 	#define dtlsv1_base_method_data (*GET_WSD_VAR_NAME(dtlsv1_base_method_data,d1_lib,s)())
sl@0: 
sl@0: 
sl@0: 	GET_GLOBAL_VAR_FROM_TLS(DTLSv1_enc_data,d1_lib,SSL3_ENC_METHOD)
sl@0: 	
sl@0: 	#define DTLSv1_enc_data (GET_WSD_VAR_NAME(DTLSv1_enc_data,d1_lib,g)())
sl@0: 
sl@0: #endif
sl@0: 
sl@0: #ifndef EMULATOR
sl@0: SSL3_ENC_METHOD DTLSv1_enc_data={
sl@0: #else
sl@0: const SSL3_ENC_METHOD temp_DTLSv1_enc_data={
sl@0: #endif
sl@0:     dtls1_enc,
sl@0: 	tls1_mac,
sl@0: 	tls1_setup_key_block,
sl@0: 	tls1_generate_master_secret,
sl@0: 	tls1_change_cipher_state,
sl@0: 	tls1_final_finish_mac,
sl@0: 	TLS1_FINISH_MAC_LENGTH,
sl@0: 	tls1_cert_verify_mac,
sl@0: 	TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
sl@0: 	TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
sl@0: 	tls1_alert_code,
sl@0: 	};
sl@0: 
sl@0: long dtls1_default_timeout(void)
sl@0: 	{
sl@0: 	/* 2 hours, the 24 hours mentioned in the DTLSv1 spec
sl@0: 	 * is way too long for http, the cache would over fill */
sl@0: 	return(60*60*2);
sl@0: 	}
sl@0: 
sl@0: IMPLEMENT_dtls1_meth_func(dtlsv1_base_method,
sl@0: 			ssl_undefined_function,
sl@0: 			ssl_undefined_function,
sl@0: 			ssl_bad_method)
sl@0: 
sl@0: int dtls1_new(SSL *s)
sl@0: 	{
sl@0: 	DTLS1_STATE *d1;
sl@0: 
sl@0: 	if (!ssl3_new(s)) return(0);
sl@0: 	if ((d1=OPENSSL_malloc(sizeof *d1)) == NULL) return (0);
sl@0: 	memset(d1,0, sizeof *d1);
sl@0: 
sl@0: 	/* d1->handshake_epoch=0; */
sl@0: #if defined(OPENSSL_SYS_VMS) || defined(VMS_TEST)
sl@0: 	d1->bitmap.length=64;
sl@0: #else
sl@0: 	d1->bitmap.length=sizeof(d1->bitmap.map) * 8;
sl@0: #endif
sl@0: 	pq_64bit_init(&(d1->bitmap.map));
sl@0: 	pq_64bit_init(&(d1->bitmap.max_seq_num));
sl@0: 	
sl@0: 	pq_64bit_init(&(d1->next_bitmap.map));
sl@0: 	pq_64bit_init(&(d1->next_bitmap.max_seq_num));
sl@0: 
sl@0: 	d1->unprocessed_rcds.q=pqueue_new();
sl@0: 	d1->processed_rcds.q=pqueue_new();
sl@0: 	d1->buffered_messages = pqueue_new();
sl@0: 	d1->sent_messages=pqueue_new();
sl@0: 
sl@0: 	if ( s->server)
sl@0: 		{
sl@0: 		d1->cookie_len = sizeof(s->d1->cookie);
sl@0: 		}
sl@0: 
sl@0: 	if( ! d1->unprocessed_rcds.q || ! d1->processed_rcds.q 
sl@0:         || ! d1->buffered_messages || ! d1->sent_messages)
sl@0: 		{
sl@0:         if ( d1->unprocessed_rcds.q) pqueue_free(d1->unprocessed_rcds.q);
sl@0:         if ( d1->processed_rcds.q) pqueue_free(d1->processed_rcds.q);
sl@0:         if ( d1->buffered_messages) pqueue_free(d1->buffered_messages);
sl@0: 		if ( d1->sent_messages) pqueue_free(d1->sent_messages);
sl@0: 		OPENSSL_free(d1);
sl@0: 		return (0);
sl@0: 		}
sl@0: 
sl@0: 	s->d1=d1;
sl@0: 	s->method->ssl_clear(s);
sl@0: 	return(1);
sl@0: 	}
sl@0: 
sl@0: void dtls1_free(SSL *s)
sl@0: 	{
sl@0:     pitem *item = NULL;
sl@0:     hm_fragment *frag = NULL;
sl@0: 
sl@0: 	ssl3_free(s);
sl@0: 
sl@0:     while( (item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL)
sl@0:         {
sl@0:         OPENSSL_free(item->data);
sl@0:         pitem_free(item);
sl@0:         }
sl@0:     pqueue_free(s->d1->unprocessed_rcds.q);
sl@0: 
sl@0:     while( (item = pqueue_pop(s->d1->processed_rcds.q)) != NULL)
sl@0:         {
sl@0:         OPENSSL_free(item->data);
sl@0:         pitem_free(item);
sl@0:         }
sl@0:     pqueue_free(s->d1->processed_rcds.q);
sl@0: 
sl@0:     while( (item = pqueue_pop(s->d1->buffered_messages)) != NULL)
sl@0:         {
sl@0:         frag = (hm_fragment *)item->data;
sl@0:         OPENSSL_free(frag->fragment);
sl@0:         OPENSSL_free(frag);
sl@0:         pitem_free(item);
sl@0:         }
sl@0:     pqueue_free(s->d1->buffered_messages);
sl@0: 
sl@0:     while ( (item = pqueue_pop(s->d1->sent_messages)) != NULL)
sl@0:         {
sl@0:         frag = (hm_fragment *)item->data;
sl@0:         OPENSSL_free(frag->fragment);
sl@0:         OPENSSL_free(frag);
sl@0:         pitem_free(item);
sl@0:         }
sl@0: 	pqueue_free(s->d1->sent_messages);
sl@0: 
sl@0: 	pq_64bit_free(&(s->d1->bitmap.map));
sl@0: 	pq_64bit_free(&(s->d1->bitmap.max_seq_num));
sl@0: 
sl@0: 	pq_64bit_free(&(s->d1->next_bitmap.map));
sl@0: 	pq_64bit_free(&(s->d1->next_bitmap.max_seq_num));
sl@0: 
sl@0: 	OPENSSL_free(s->d1);
sl@0: 	}
sl@0: 
sl@0: void dtls1_clear(SSL *s)
sl@0: 	{
sl@0: 	ssl3_clear(s);
sl@0: 	s->version=DTLS1_VERSION;
sl@0: 	}
sl@0: 
sl@0: /*
sl@0:  * As it's impossible to use stream ciphers in "datagram" mode, this
sl@0:  * simple filter is designed to disengage them in DTLS. Unfortunately
sl@0:  * there is no universal way to identify stream SSL_CIPHER, so we have
sl@0:  * to explicitly list their SSL_* codes. Currently RC4 is the only one
sl@0:  * available, but if new ones emerge, they will have to be added...
sl@0:  */
sl@0: SSL_CIPHER *dtls1_get_cipher(unsigned int u)
sl@0: 	{
sl@0: 	SSL_CIPHER *ciph = ssl3_get_cipher(u);
sl@0: 
sl@0: 	if (ciph != NULL)
sl@0: 		{
sl@0: 		if ((ciph->algorithms&SSL_ENC_MASK) == SSL_RC4)
sl@0: 			return NULL;
sl@0: 		}
sl@0: 
sl@0: 	return ciph;
sl@0: 	}