os/ossrv/ssl/libssl/src/s2_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.
     1 /* ssl/s2_lib.c */
     2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     3  * All rights reserved.
     4  *
     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.
     8  * 
     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).
    15  * 
    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.
    22  * 
    23  * Redistribution and use in source and binary forms, with or without
    24  * modification, are permitted provided that the following conditions
    25  * are met:
    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)"
    40  * 
    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
    51  * SUCH DAMAGE.
    52  * 
    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.]
    57  */
    58 /*
    59  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    60  */
    61  
    62 #include "ssl_locl.h"
    63 #ifndef OPENSSL_NO_SSL2
    64 #include <stdio.h>
    65 #include <openssl/objects.h>
    66 #include <openssl/evp.h>
    67 #include <openssl/md5.h>
    68 
    69 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    70 #include "libssl_wsd.h"
    71 #endif
    72 
    73 #ifdef EMULATOR
    74 
    75 	GET_STATIC_VAR_FROM_TLS(sslv2_base_method_data,s2_lib,SSL_METHOD)
    76 	
    77 	#define sslv2_base_method_data (*GET_WSD_VAR_NAME(sslv2_base_method_data,s2_lib,s)())
    78 	
    79 #endif
    80 
    81 const char ssl2_version_str[]="SSLv2" OPENSSL_VERSION_PTEXT;
    82 
    83 #ifndef EMULATOR
    84 #define SSL2_NUM_CIPHERS (sizeof(ssl2_ciphers)/sizeof(SSL_CIPHER))
    85 /* #else */
    86 /* #define SSL2_NUM_CIPHERS  10		Defined in libssl_wsd.h */
    87 #endif
    88 
    89 /* list of available SSLv2 ciphers (sorted by id) */
    90 
    91 
    92 #ifdef EMULATOR
    93 
    94 	GET_GLOBAL_ARRAY_FROM_TLS(ssl2_ciphers,s2_lib,SSL_CIPHER)
    95 	
    96 	#define ssl2_ciphers (GET_WSD_VAR_NAME(ssl2_ciphers,s2_lib,g)())
    97 
    98 #endif
    99 
   100 #ifndef EMULATOR
   101 OPENSSL_GLOBAL SSL_CIPHER ssl2_ciphers[]={
   102 #else
   103 OPENSSL_GLOBAL const SSL_CIPHER temp_ssl2_ciphers[]={
   104 #endif
   105 /* NULL_WITH_MD5 v3 */
   106 #if 0
   107 	{
   108 	1,
   109 	SSL2_TXT_NULL_WITH_MD5,
   110 	SSL2_CK_NULL_WITH_MD5,
   111 	SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_MD5|SSL_SSLV2,
   112 	SSL_EXPORT|SSL_EXP40|SSL_STRONG_NONE,
   113 	0,
   114 	0,
   115 	0,
   116 	SSL_ALL_CIPHERS,
   117 	SSL_ALL_STRENGTHS,
   118 	},
   119 #endif
   120 /* RC4_128_WITH_MD5 */
   121 	{
   122 	1,
   123 	SSL2_TXT_RC4_128_WITH_MD5,
   124 	SSL2_CK_RC4_128_WITH_MD5,
   125 	SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_SSLV2,
   126 	SSL_NOT_EXP|SSL_MEDIUM,
   127 	0,
   128 	128,
   129 	128,
   130 	SSL_ALL_CIPHERS,
   131 	SSL_ALL_STRENGTHS,
   132 	},
   133 /* RC4_128_EXPORT40_WITH_MD5 */
   134 	{
   135 	1,
   136 	SSL2_TXT_RC4_128_EXPORT40_WITH_MD5,
   137 	SSL2_CK_RC4_128_EXPORT40_WITH_MD5,
   138 	SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_SSLV2,
   139 	SSL_EXPORT|SSL_EXP40,
   140 	SSL2_CF_5_BYTE_ENC,
   141 	40,
   142 	128,
   143 	SSL_ALL_CIPHERS,
   144 	SSL_ALL_STRENGTHS,
   145 	},
   146 /* RC2_128_CBC_WITH_MD5 */
   147 	{
   148 	1,
   149 	SSL2_TXT_RC2_128_CBC_WITH_MD5,
   150 	SSL2_CK_RC2_128_CBC_WITH_MD5,
   151 	SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_SSLV2,
   152 	SSL_NOT_EXP|SSL_MEDIUM,
   153 	0,
   154 	128,
   155 	128,
   156 	SSL_ALL_CIPHERS,
   157 	SSL_ALL_STRENGTHS,
   158 	},
   159 /* RC2_128_CBC_EXPORT40_WITH_MD5 */
   160 	{
   161 	1,
   162 	SSL2_TXT_RC2_128_CBC_EXPORT40_WITH_MD5,
   163 	SSL2_CK_RC2_128_CBC_EXPORT40_WITH_MD5,
   164 	SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_SSLV2,
   165 	SSL_EXPORT|SSL_EXP40,
   166 	SSL2_CF_5_BYTE_ENC,
   167 	40,
   168 	128,
   169 	SSL_ALL_CIPHERS,
   170 	SSL_ALL_STRENGTHS,
   171 	},
   172 /* IDEA_128_CBC_WITH_MD5 */
   173 #ifndef OPENSSL_NO_IDEA
   174 	{
   175 	1,
   176 	SSL2_TXT_IDEA_128_CBC_WITH_MD5,
   177 	SSL2_CK_IDEA_128_CBC_WITH_MD5,
   178 	SSL_kRSA|SSL_aRSA|SSL_IDEA|SSL_MD5|SSL_SSLV2,
   179 	SSL_NOT_EXP|SSL_MEDIUM,
   180 	0,
   181 	128,
   182 	128,
   183 	SSL_ALL_CIPHERS,
   184 	SSL_ALL_STRENGTHS,
   185 	},
   186 #endif
   187 /* DES_64_CBC_WITH_MD5 */
   188 	{
   189 	1,
   190 	SSL2_TXT_DES_64_CBC_WITH_MD5,
   191 	SSL2_CK_DES_64_CBC_WITH_MD5,
   192 	SSL_kRSA|SSL_aRSA|SSL_DES|SSL_MD5|SSL_SSLV2,
   193 	SSL_NOT_EXP|SSL_LOW,
   194 	0,
   195 	56,
   196 	56,
   197 	SSL_ALL_CIPHERS,
   198 	SSL_ALL_STRENGTHS,
   199 	},
   200 /* DES_192_EDE3_CBC_WITH_MD5 */
   201 	{
   202 	1,
   203 	SSL2_TXT_DES_192_EDE3_CBC_WITH_MD5,
   204 	SSL2_CK_DES_192_EDE3_CBC_WITH_MD5,
   205 	SSL_kRSA|SSL_aRSA|SSL_3DES|SSL_MD5|SSL_SSLV2,
   206 	SSL_NOT_EXP|SSL_HIGH,
   207 	0,
   208 	168,
   209 	168,
   210 	SSL_ALL_CIPHERS,
   211 	SSL_ALL_STRENGTHS,
   212 	},
   213 /* RC4_64_WITH_MD5 */
   214 #if 0
   215 	{
   216 	1,
   217 	SSL2_TXT_RC4_64_WITH_MD5,
   218 	SSL2_CK_RC4_64_WITH_MD5,
   219 	SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_SSLV2,
   220 	SSL_NOT_EXP|SSL_LOW,
   221 	SSL2_CF_8_BYTE_ENC,
   222 	64,
   223 	64,
   224 	SSL_ALL_CIPHERS,
   225 	SSL_ALL_STRENGTHS,
   226 	},
   227 #endif
   228 /* NULL SSLeay (testing) */
   229 #if 0
   230 	{	
   231 	0,
   232 	SSL2_TXT_NULL,
   233 	SSL2_CK_NULL,
   234 	0,
   235 	SSL_STRONG_NONE,
   236 	0,
   237 	0,
   238 	0,
   239 	SSL_ALL_CIPHERS,
   240 	SSL_ALL_STRENGTHS,
   241 	},
   242 #endif
   243 
   244 /* end of list :-) */
   245 	};
   246 
   247 long ssl2_default_timeout(void)
   248 	{
   249 	return(300);
   250 	}
   251 
   252 IMPLEMENT_ssl2_meth_func(sslv2_base_method,
   253 			ssl_undefined_function,
   254 			ssl_undefined_function,
   255 			ssl_bad_method)
   256 
   257 int ssl2_num_ciphers(void)
   258 	{
   259 	return(SSL2_NUM_CIPHERS);
   260 	}
   261 
   262 SSL_CIPHER *ssl2_get_cipher(unsigned int u)
   263 	{
   264 	if (u < SSL2_NUM_CIPHERS)
   265 		return(&(ssl2_ciphers[SSL2_NUM_CIPHERS-1-u]));
   266 	else
   267 		return(NULL);
   268 	}
   269 
   270 int ssl2_pending(const SSL *s)
   271 	{
   272 	return SSL_in_init(s) ? 0 : s->s2->ract_data_length;
   273 	}
   274 
   275 int ssl2_new(SSL *s)
   276 	{
   277 	SSL2_STATE *s2;
   278 
   279 	if ((s2=OPENSSL_malloc(sizeof *s2)) == NULL) goto err;
   280 	memset(s2,0,sizeof *s2);
   281 
   282 #if SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER + 3 > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER + 2
   283 #  error "assertion failed"
   284 #endif
   285 
   286 	if ((s2->rbuf=OPENSSL_malloc(
   287 		SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2)) == NULL) goto err;
   288 	/* wbuf needs one byte more because when using two-byte headers,
   289 	 * we leave the first byte unused in do_ssl_write (s2_pkt.c) */
   290 	if ((s2->wbuf=OPENSSL_malloc(
   291 		SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+3)) == NULL) goto err;
   292 	s->s2=s2;
   293 
   294 	ssl2_clear(s);
   295 	return(1);
   296 err:
   297 	if (s2 != NULL)
   298 		{
   299 		if (s2->wbuf != NULL) OPENSSL_free(s2->wbuf);
   300 		if (s2->rbuf != NULL) OPENSSL_free(s2->rbuf);
   301 		OPENSSL_free(s2);
   302 		}
   303 	return(0);
   304 	}
   305 
   306 void ssl2_free(SSL *s)
   307 	{
   308 	SSL2_STATE *s2;
   309 
   310 	if(s == NULL)
   311 	    return;
   312 
   313 	s2=s->s2;
   314 	if (s2->rbuf != NULL) OPENSSL_free(s2->rbuf);
   315 	if (s2->wbuf != NULL) OPENSSL_free(s2->wbuf);
   316 	OPENSSL_cleanse(s2,sizeof *s2);
   317 	OPENSSL_free(s2);
   318 	s->s2=NULL;
   319 	}
   320 
   321 void ssl2_clear(SSL *s)
   322 	{
   323 	SSL2_STATE *s2;
   324 	unsigned char *rbuf,*wbuf;
   325 
   326 	s2=s->s2;
   327 
   328 	rbuf=s2->rbuf;
   329 	wbuf=s2->wbuf;
   330 
   331 	memset(s2,0,sizeof *s2);
   332 
   333 	s2->rbuf=rbuf;
   334 	s2->wbuf=wbuf;
   335 	s2->clear_text=1;
   336 	s->packet=s2->rbuf;
   337 	s->version=SSL2_VERSION;
   338 	s->packet_length=0;
   339 	}
   340 
   341 long ssl2_ctrl(SSL *s, int cmd, long larg, void *parg)
   342 	{
   343 	int ret=0;
   344 
   345 	switch(cmd)
   346 		{
   347 	case SSL_CTRL_GET_SESSION_REUSED:
   348 		ret=s->hit;
   349 		break;
   350 	default:
   351 		break;
   352 		}
   353 	return(ret);
   354 	}
   355 
   356 long ssl2_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
   357 	{
   358 	return(0);
   359 	}
   360 
   361 long ssl2_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
   362 	{
   363 	return(0);
   364 	}
   365 
   366 long ssl2_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
   367 	{
   368 	return(0);
   369 	}
   370 
   371 /* This function needs to check if the ciphers required are actually
   372  * available */
   373 SSL_CIPHER *ssl2_get_cipher_by_char(const unsigned char *p)
   374 	{
   375 	SSL_CIPHER c,*cp;
   376 	unsigned long id;
   377 
   378 	id=0x02000000L|((unsigned long)p[0]<<16L)|
   379 		((unsigned long)p[1]<<8L)|(unsigned long)p[2];
   380 	c.id=id;
   381 	cp = (SSL_CIPHER *)OBJ_bsearch((char *)&c,
   382 		(char *)ssl2_ciphers,
   383 		SSL2_NUM_CIPHERS,sizeof(SSL_CIPHER),
   384 		FP_ICC ssl_cipher_id_cmp);
   385 	if ((cp == NULL) || (cp->valid == 0))
   386 		return NULL;
   387 	else
   388 		return cp;
   389 	}
   390 
   391 int ssl2_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
   392 	{
   393 	long l;
   394 
   395 	if (p != NULL)
   396 		{
   397 		l=c->id;
   398 		if ((l & 0xff000000) != 0x02000000) return(0);
   399 		p[0]=((unsigned char)(l>>16L))&0xFF;
   400 		p[1]=((unsigned char)(l>> 8L))&0xFF;
   401 		p[2]=((unsigned char)(l     ))&0xFF;
   402 		}
   403 	return(3);
   404 	}
   405 
   406 int ssl2_generate_key_material(SSL *s)
   407 	{
   408 	unsigned int i;
   409 	EVP_MD_CTX ctx;
   410 	unsigned char *km;
   411 	unsigned char c='0';
   412 	const EVP_MD *md5;
   413 
   414 	md5 = EVP_md5();
   415 
   416 #ifdef CHARSET_EBCDIC
   417 	c = os_toascii['0']; /* Must be an ASCII '0', not EBCDIC '0',
   418 				see SSLv2 docu */
   419 #endif
   420 	EVP_MD_CTX_init(&ctx);
   421 	km=s->s2->key_material;
   422 
   423  	if (s->session->master_key_length < 0 ||
   424 			s->session->master_key_length > (int)sizeof(s->session->master_key))
   425  		{
   426  		SSLerr(SSL_F_SSL2_GENERATE_KEY_MATERIAL, ERR_R_INTERNAL_ERROR);
   427  		return 0;
   428  		}
   429 
   430 	for (i=0; i<s->s2->key_material_length; i += EVP_MD_size(md5))
   431 		{
   432 		if (((km - s->s2->key_material) + EVP_MD_size(md5)) >
   433 				(int)sizeof(s->s2->key_material))
   434 			{
   435 			/* EVP_DigestFinal_ex() below would write beyond buffer */
   436 			SSLerr(SSL_F_SSL2_GENERATE_KEY_MATERIAL, ERR_R_INTERNAL_ERROR);
   437 			return 0;
   438 			}
   439 
   440 		EVP_DigestInit_ex(&ctx, md5, NULL);
   441 
   442 		OPENSSL_assert(s->session->master_key_length >= 0
   443 		    && s->session->master_key_length
   444 		    < (int)sizeof(s->session->master_key));
   445 		EVP_DigestUpdate(&ctx,s->session->master_key,s->session->master_key_length);
   446 		EVP_DigestUpdate(&ctx,&c,1);
   447 		c++;
   448 		EVP_DigestUpdate(&ctx,s->s2->challenge,s->s2->challenge_length);
   449 		EVP_DigestUpdate(&ctx,s->s2->conn_id,s->s2->conn_id_length);
   450 		EVP_DigestFinal_ex(&ctx,km,NULL);
   451 		km += EVP_MD_size(md5);
   452 		}
   453 
   454 	EVP_MD_CTX_cleanup(&ctx);
   455 	return 1;
   456 	}
   457 
   458 void ssl2_return_error(SSL *s, int err)
   459 	{
   460 	if (!s->error)
   461 		{
   462 		s->error=3;
   463 		s->error_code=err;
   464 
   465 		ssl2_write_error(s);
   466 		}
   467 	}
   468 
   469 
   470 void ssl2_write_error(SSL *s)
   471 	{
   472 	unsigned char buf[3];
   473 	int i,error;
   474 
   475 	buf[0]=SSL2_MT_ERROR;
   476 	buf[1]=(s->error_code>>8)&0xff;
   477 	buf[2]=(s->error_code)&0xff;
   478 
   479 /*	state=s->rwstate;*/
   480 
   481 	error=s->error; /* number of bytes left to write */
   482 	s->error=0;
   483 	OPENSSL_assert(error >= 0 && error <= (int)sizeof(buf));
   484 	i=ssl2_write(s,&(buf[3-error]),error);
   485 
   486 /*	if (i == error) s->rwstate=state; */
   487 
   488 	if (i < 0)
   489 		s->error=error;
   490 	else
   491 		{
   492 		s->error=error-i;
   493 
   494 		if (s->error == 0)
   495 			if (s->msg_callback)
   496 				s->msg_callback(1, s->version, 0, buf, 3, s, s->msg_callback_arg); /* ERROR */
   497 		}
   498 	}
   499 
   500 int ssl2_shutdown(SSL *s)
   501 	{
   502 	s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
   503 	return(1);
   504 	}
   505 #else /* !OPENSSL_NO_SSL2 */
   506 
   507 # if PEDANTIC
   508 static void *dummy=&dummy;
   509 # endif
   510 
   511 #endif