os/ossrv/ssl/libssl/src/d1_lib.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* ssl/d1_lib.c */
sl@0
     2
/* 
sl@0
     3
 * DTLS implementation written by Nagendra Modadugu
sl@0
     4
 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.  
sl@0
     5
 */
sl@0
     6
/* ====================================================================
sl@0
     7
 * Copyright (c) 1999-2005 The OpenSSL Project.  All rights reserved.
sl@0
     8
 *
sl@0
     9
 * Redistribution and use in source and binary forms, with or without
sl@0
    10
 * modification, are permitted provided that the following conditions
sl@0
    11
 * are met:
sl@0
    12
 *
sl@0
    13
 * 1. Redistributions of source code must retain the above copyright
sl@0
    14
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    15
 *
sl@0
    16
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    17
 *    notice, this list of conditions and the following disclaimer in
sl@0
    18
 *    the documentation and/or other materials provided with the
sl@0
    19
 *    distribution.
sl@0
    20
 *
sl@0
    21
 * 3. All advertising materials mentioning features or use of this
sl@0
    22
 *    software must display the following acknowledgment:
sl@0
    23
 *    "This product includes software developed by the OpenSSL Project
sl@0
    24
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
sl@0
    25
 *
sl@0
    26
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    27
 *    endorse or promote products derived from this software without
sl@0
    28
 *    prior written permission. For written permission, please contact
sl@0
    29
 *    openssl-core@OpenSSL.org.
sl@0
    30
 *
sl@0
    31
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    32
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    33
 *    permission of the OpenSSL Project.
sl@0
    34
 *
sl@0
    35
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    36
 *    acknowledgment:
sl@0
    37
 *    "This product includes software developed by the OpenSSL Project
sl@0
    38
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
sl@0
    39
 *
sl@0
    40
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    41
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    43
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    44
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    45
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    46
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    47
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    49
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    50
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    51
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    52
 * ====================================================================
sl@0
    53
 *
sl@0
    54
 * This product includes cryptographic software written by Eric Young
sl@0
    55
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    56
 * Hudson (tjh@cryptsoft.com).
sl@0
    57
 *
sl@0
    58
 */
sl@0
    59
/*
sl@0
    60
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    61
 */
sl@0
    62
 
sl@0
    63
#include <stdio.h>
sl@0
    64
#include <openssl/objects.h>
sl@0
    65
#include "ssl_locl.h"
sl@0
    66
sl@0
    67
const char dtls1_version_str[]="DTLSv1" OPENSSL_VERSION_PTEXT;
sl@0
    68
sl@0
    69
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    70
#include "libssl_wsd.h"
sl@0
    71
#endif 
sl@0
    72
sl@0
    73
#ifdef EMULATOR
sl@0
    74
	
sl@0
    75
	GET_STATIC_VAR_FROM_TLS(dtlsv1_base_method_data,d1_lib,SSL_METHOD)
sl@0
    76
	
sl@0
    77
	#define dtlsv1_base_method_data (*GET_WSD_VAR_NAME(dtlsv1_base_method_data,d1_lib,s)())
sl@0
    78
sl@0
    79
sl@0
    80
	GET_GLOBAL_VAR_FROM_TLS(DTLSv1_enc_data,d1_lib,SSL3_ENC_METHOD)
sl@0
    81
	
sl@0
    82
	#define DTLSv1_enc_data (GET_WSD_VAR_NAME(DTLSv1_enc_data,d1_lib,g)())
sl@0
    83
sl@0
    84
#endif
sl@0
    85
sl@0
    86
#ifndef EMULATOR
sl@0
    87
SSL3_ENC_METHOD DTLSv1_enc_data={
sl@0
    88
#else
sl@0
    89
const SSL3_ENC_METHOD temp_DTLSv1_enc_data={
sl@0
    90
#endif
sl@0
    91
    dtls1_enc,
sl@0
    92
	tls1_mac,
sl@0
    93
	tls1_setup_key_block,
sl@0
    94
	tls1_generate_master_secret,
sl@0
    95
	tls1_change_cipher_state,
sl@0
    96
	tls1_final_finish_mac,
sl@0
    97
	TLS1_FINISH_MAC_LENGTH,
sl@0
    98
	tls1_cert_verify_mac,
sl@0
    99
	TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
sl@0
   100
	TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
sl@0
   101
	tls1_alert_code,
sl@0
   102
	};
sl@0
   103
sl@0
   104
long dtls1_default_timeout(void)
sl@0
   105
	{
sl@0
   106
	/* 2 hours, the 24 hours mentioned in the DTLSv1 spec
sl@0
   107
	 * is way too long for http, the cache would over fill */
sl@0
   108
	return(60*60*2);
sl@0
   109
	}
sl@0
   110
sl@0
   111
IMPLEMENT_dtls1_meth_func(dtlsv1_base_method,
sl@0
   112
			ssl_undefined_function,
sl@0
   113
			ssl_undefined_function,
sl@0
   114
			ssl_bad_method)
sl@0
   115
sl@0
   116
int dtls1_new(SSL *s)
sl@0
   117
	{
sl@0
   118
	DTLS1_STATE *d1;
sl@0
   119
sl@0
   120
	if (!ssl3_new(s)) return(0);
sl@0
   121
	if ((d1=OPENSSL_malloc(sizeof *d1)) == NULL) return (0);
sl@0
   122
	memset(d1,0, sizeof *d1);
sl@0
   123
sl@0
   124
	/* d1->handshake_epoch=0; */
sl@0
   125
#if defined(OPENSSL_SYS_VMS) || defined(VMS_TEST)
sl@0
   126
	d1->bitmap.length=64;
sl@0
   127
#else
sl@0
   128
	d1->bitmap.length=sizeof(d1->bitmap.map) * 8;
sl@0
   129
#endif
sl@0
   130
	pq_64bit_init(&(d1->bitmap.map));
sl@0
   131
	pq_64bit_init(&(d1->bitmap.max_seq_num));
sl@0
   132
	
sl@0
   133
	pq_64bit_init(&(d1->next_bitmap.map));
sl@0
   134
	pq_64bit_init(&(d1->next_bitmap.max_seq_num));
sl@0
   135
sl@0
   136
	d1->unprocessed_rcds.q=pqueue_new();
sl@0
   137
	d1->processed_rcds.q=pqueue_new();
sl@0
   138
	d1->buffered_messages = pqueue_new();
sl@0
   139
	d1->sent_messages=pqueue_new();
sl@0
   140
sl@0
   141
	if ( s->server)
sl@0
   142
		{
sl@0
   143
		d1->cookie_len = sizeof(s->d1->cookie);
sl@0
   144
		}
sl@0
   145
sl@0
   146
	if( ! d1->unprocessed_rcds.q || ! d1->processed_rcds.q 
sl@0
   147
        || ! d1->buffered_messages || ! d1->sent_messages)
sl@0
   148
		{
sl@0
   149
        if ( d1->unprocessed_rcds.q) pqueue_free(d1->unprocessed_rcds.q);
sl@0
   150
        if ( d1->processed_rcds.q) pqueue_free(d1->processed_rcds.q);
sl@0
   151
        if ( d1->buffered_messages) pqueue_free(d1->buffered_messages);
sl@0
   152
		if ( d1->sent_messages) pqueue_free(d1->sent_messages);
sl@0
   153
		OPENSSL_free(d1);
sl@0
   154
		return (0);
sl@0
   155
		}
sl@0
   156
sl@0
   157
	s->d1=d1;
sl@0
   158
	s->method->ssl_clear(s);
sl@0
   159
	return(1);
sl@0
   160
	}
sl@0
   161
sl@0
   162
void dtls1_free(SSL *s)
sl@0
   163
	{
sl@0
   164
    pitem *item = NULL;
sl@0
   165
    hm_fragment *frag = NULL;
sl@0
   166
sl@0
   167
	ssl3_free(s);
sl@0
   168
sl@0
   169
    while( (item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL)
sl@0
   170
        {
sl@0
   171
        OPENSSL_free(item->data);
sl@0
   172
        pitem_free(item);
sl@0
   173
        }
sl@0
   174
    pqueue_free(s->d1->unprocessed_rcds.q);
sl@0
   175
sl@0
   176
    while( (item = pqueue_pop(s->d1->processed_rcds.q)) != NULL)
sl@0
   177
        {
sl@0
   178
        OPENSSL_free(item->data);
sl@0
   179
        pitem_free(item);
sl@0
   180
        }
sl@0
   181
    pqueue_free(s->d1->processed_rcds.q);
sl@0
   182
sl@0
   183
    while( (item = pqueue_pop(s->d1->buffered_messages)) != NULL)
sl@0
   184
        {
sl@0
   185
        frag = (hm_fragment *)item->data;
sl@0
   186
        OPENSSL_free(frag->fragment);
sl@0
   187
        OPENSSL_free(frag);
sl@0
   188
        pitem_free(item);
sl@0
   189
        }
sl@0
   190
    pqueue_free(s->d1->buffered_messages);
sl@0
   191
sl@0
   192
    while ( (item = pqueue_pop(s->d1->sent_messages)) != NULL)
sl@0
   193
        {
sl@0
   194
        frag = (hm_fragment *)item->data;
sl@0
   195
        OPENSSL_free(frag->fragment);
sl@0
   196
        OPENSSL_free(frag);
sl@0
   197
        pitem_free(item);
sl@0
   198
        }
sl@0
   199
	pqueue_free(s->d1->sent_messages);
sl@0
   200
sl@0
   201
	pq_64bit_free(&(s->d1->bitmap.map));
sl@0
   202
	pq_64bit_free(&(s->d1->bitmap.max_seq_num));
sl@0
   203
sl@0
   204
	pq_64bit_free(&(s->d1->next_bitmap.map));
sl@0
   205
	pq_64bit_free(&(s->d1->next_bitmap.max_seq_num));
sl@0
   206
sl@0
   207
	OPENSSL_free(s->d1);
sl@0
   208
	}
sl@0
   209
sl@0
   210
void dtls1_clear(SSL *s)
sl@0
   211
	{
sl@0
   212
	ssl3_clear(s);
sl@0
   213
	s->version=DTLS1_VERSION;
sl@0
   214
	}
sl@0
   215
sl@0
   216
/*
sl@0
   217
 * As it's impossible to use stream ciphers in "datagram" mode, this
sl@0
   218
 * simple filter is designed to disengage them in DTLS. Unfortunately
sl@0
   219
 * there is no universal way to identify stream SSL_CIPHER, so we have
sl@0
   220
 * to explicitly list their SSL_* codes. Currently RC4 is the only one
sl@0
   221
 * available, but if new ones emerge, they will have to be added...
sl@0
   222
 */
sl@0
   223
SSL_CIPHER *dtls1_get_cipher(unsigned int u)
sl@0
   224
	{
sl@0
   225
	SSL_CIPHER *ciph = ssl3_get_cipher(u);
sl@0
   226
sl@0
   227
	if (ciph != NULL)
sl@0
   228
		{
sl@0
   229
		if ((ciph->algorithms&SSL_ENC_MASK) == SSL_RC4)
sl@0
   230
			return NULL;
sl@0
   231
		}
sl@0
   232
sl@0
   233
	return ciph;
sl@0
   234
	}