⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s2_pkt.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 2 页
字号:
/* ssl/s2_pkt.c *//* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. *  * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to.  The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code.  The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). *  * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *    "This product includes cryptographic software written by *     Eric Young (eay@cryptsoft.com)" *    The word 'cryptographic' can be left out if the rouines from the library *    being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from  *    the apps directory (application code) you must include an acknowledgement: *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" *  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. *  * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed.  i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] *//* ==================================================================== * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. All advertising materials mentioning features or use of this *    software must display the following acknowledgment: *    "This product includes software developed by the OpenSSL Project *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to *    endorse or promote products derived from this software without *    prior written permission. For written permission, please contact *    openssl-core@openssl.org. * * 5. Products derived from this software may not be called "OpenSSL" *    nor may "OpenSSL" appear in their names without prior written *    permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following *    acknowledgment: *    "This product includes software developed by the OpenSSL Project *    for use in the OpenSSL Toolkit (http://www.openssl.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com).  This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */#include "ssl_locl.h"#ifndef OPENSSL_NO_SSL2#include <stdio.h>#include <errno.h>#define USE_SOCKETSstatic int read_n(SSL *s,unsigned int n,unsigned int max,unsigned int extend);static int do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len);static int write_pending(SSL *s, const unsigned char *buf, unsigned int len);static int ssl_mt_error(int n);/* SSL 2.0 imlementation for SSL_read/SSL_peek - * This routine will return 0 to len bytes, decrypted etc if required. */static int ssl2_read_internal(SSL *s, void *buf, int len, int peek)	{	int n;	unsigned char mac[MAX_MAC_SIZE];	unsigned char *p;	int i;	unsigned int mac_size; ssl2_read_again:	if (SSL_in_init(s) && !s->in_handshake)		{		n=s->handshake_func(s);		if (n < 0) return(n);		if (n == 0)			{			SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_SSL_HANDSHAKE_FAILURE);			return(-1);			}		}	clear_sys_error();	s->rwstate=SSL_NOTHING;	if (len <= 0) return(len);	if (s->s2->ract_data_length != 0) /* read from buffer */		{		if (len > s->s2->ract_data_length)			n=s->s2->ract_data_length;		else			n=len;		memcpy(buf,s->s2->ract_data,(unsigned int)n);		if (!peek)			{			s->s2->ract_data_length-=n;			s->s2->ract_data+=n;			if (s->s2->ract_data_length == 0)				s->rstate=SSL_ST_READ_HEADER;			}		return(n);		}	/* s->s2->ract_data_length == 0	 * 	 * Fill the buffer, then goto ssl2_read_again.	 */	if (s->rstate == SSL_ST_READ_HEADER)		{		if (s->first_packet)			{			n=read_n(s,5,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0);			if (n <= 0) return(n); /* error or non-blocking */			s->first_packet=0;			p=s->packet;			if (!((p[0] & 0x80) && (				(p[2] == SSL2_MT_CLIENT_HELLO) ||				(p[2] == SSL2_MT_SERVER_HELLO))))				{				SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_NON_SSLV2_INITIAL_PACKET);				return(-1);				}			}		else			{			n=read_n(s,2,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0);			if (n <= 0) return(n); /* error or non-blocking */			}		/* part read stuff */		s->rstate=SSL_ST_READ_BODY;		p=s->packet;		/* Do header */		/*s->s2->padding=0;*/		s->s2->escape=0;		s->s2->rlength=(((unsigned int)p[0])<<8)|((unsigned int)p[1]);		if ((p[0] & TWO_BYTE_BIT))		/* Two byte header? */			{			s->s2->three_byte_header=0;			s->s2->rlength&=TWO_BYTE_MASK;				}		else			{			s->s2->three_byte_header=1;			s->s2->rlength&=THREE_BYTE_MASK;			/* security >s2->escape */			s->s2->escape=((p[0] & SEC_ESC_BIT))?1:0;			}		}	if (s->rstate == SSL_ST_READ_BODY)		{		n=s->s2->rlength+2+s->s2->three_byte_header;		if (n > (int)s->packet_length)			{			n-=s->packet_length;			i=read_n(s,(unsigned int)n,(unsigned int)n,1);			if (i <= 0) return(i); /* ERROR */			}		p= &(s->packet[2]);		s->rstate=SSL_ST_READ_HEADER;		if (s->s2->three_byte_header)			s->s2->padding= *(p++);		else	s->s2->padding=0;		/* Data portion */		if (s->s2->clear_text)			{			mac_size = 0;			s->s2->mac_data=p;			s->s2->ract_data=p;			if (s->s2->padding)				{				SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_ILLEGAL_PADDING);				return(-1);				}			}		else			{			mac_size=EVP_MD_size(s->read_hash);			OPENSSL_assert(mac_size <= MAX_MAC_SIZE);			s->s2->mac_data=p;			s->s2->ract_data= &p[mac_size];			if (s->s2->padding + mac_size > s->s2->rlength)				{				SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_ILLEGAL_PADDING);				return(-1);				}			}		s->s2->ract_data_length=s->s2->rlength;		/* added a check for length > max_size in case		 * encryption was not turned on yet due to an error */		if ((!s->s2->clear_text) &&			(s->s2->rlength >= mac_size))			{			ssl2_enc(s,0);			s->s2->ract_data_length-=mac_size;			ssl2_mac(s,mac,0);			s->s2->ract_data_length-=s->s2->padding;			if (	(memcmp(mac,s->s2->mac_data,				(unsigned int)mac_size) != 0) ||				(s->s2->rlength%EVP_CIPHER_CTX_block_size(s->enc_read_ctx) != 0))				{				SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_MAC_DECODE);				return(-1);				}			}		INC32(s->s2->read_sequence); /* expect next number */		/* s->s2->ract_data is now available for processing */		/* Possibly the packet that we just read had 0 actual data bytes.		 * (SSLeay/OpenSSL itself never sends such packets; see ssl2_write.)		 * In this case, returning 0 would be interpreted by the caller		 * as indicating EOF, so it's not a good idea.  Instead, we just		 * continue reading; thus ssl2_read_internal may have to process		 * multiple packets before it can return.		 *		 * [Note that using select() for blocking sockets *never* guarantees		 * that the next SSL_read will not block -- the available		 * data may contain incomplete packets, and except for SSL 2,		 * renegotiation can confuse things even more.] */		goto ssl2_read_again; /* This should really be		                       * "return ssl2_read(s,buf,len)",		                       * but that would allow for		                       * denial-of-service attacks if a		                       * C compiler is used that does not		                       * recognize end-recursion. */		}	else		{		SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_STATE);			return(-1);		}	}int ssl2_read(SSL *s, void *buf, int len)	{	return ssl2_read_internal(s, buf, len, 0);	}int ssl2_peek(SSL *s, void *buf, int len)	{	return ssl2_read_internal(s, buf, len, 1);	}static int read_n(SSL *s, unsigned int n, unsigned int max,	     unsigned int extend)	{	int i,off,newb;	/* if there is stuff still in the buffer from a previous read,	 * and there is more than we want, take some. */	if (s->s2->rbuf_left >= (int)n)		{		if (extend)			s->packet_length+=n;		else			{			s->packet= &(s->s2->rbuf[s->s2->rbuf_offs]);			s->packet_length=n;			}		s->s2->rbuf_left-=n;		s->s2->rbuf_offs+=n;		return(n);		}	if (!s->read_ahead) max=n;	if (max > (unsigned int)(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2))		max=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2;		/* Else we want more than we have.	 * First, if there is some left or we want to extend */	off=0;	if ((s->s2->rbuf_left != 0) || ((s->packet_length != 0) && extend))		{		newb=s->s2->rbuf_left;		if (extend)			{			off=s->packet_length;			if (s->packet != s->s2->rbuf)				memcpy(s->s2->rbuf,s->packet,					(unsigned int)newb+off);			}		else if (s->s2->rbuf_offs != 0)			{			memcpy(s->s2->rbuf,&(s->s2->rbuf[s->s2->rbuf_offs]),				(unsigned int)newb);			s->s2->rbuf_offs=0;			}		s->s2->rbuf_left=0;		}	else		newb=0;	/* off is the offset to start writing too.	 * r->s2->rbuf_offs is the 'unread data', now 0. 	 * newb is the number of new bytes so far

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -