s2_pkt.c
来自「一个用于点对点传输加密的工具包源码」· C语言 代码 · 共 649 行 · 第 1/2 页
C
649 行
/* 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.] */#include "ssl_locl.h"#ifndef 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);int ssl2_peek(SSL *s, char *buf, int len) { int ret; ret=ssl2_read(s,buf,len); if (ret > 0) { s->s2->ract_data_length+=ret; s->s2->ract_data-=ret; } return(ret); }/* SSL_read - * This routine will return 0 to len bytes, decrypted etc if required. */int ssl2_read(SSL *s, void *buf, int len) { int n; unsigned char mac[MAX_MAC_SIZE]; unsigned char *p; int i; unsigned int mac_size=0;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,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); 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); } 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,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) { s->s2->mac_data=p; s->s2->ract_data=p; s->s2->pad_data=NULL; } else { mac_size=EVP_MD_size(s->read_hash); s->s2->mac_data=p; s->s2->ract_data= &p[mac_size]; s->s2->pad_data= &p[mac_size+ s->s2->rlength-s->s2->padding]; } 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,SSL_R_BAD_MAC_DECODE); return(-1); } } INC32(s->s2->read_sequence); /* expect next number */ /* s->s2->ract_data is now available for processing */#if 1 /* How should we react when a packet containing 0 * bytes is received? (Note that SSLeay/OpenSSL itself * never sends such packets; see ssl2_write.) * Returning 0 would be interpreted by the caller as * indicating EOF, so it's not a good idea. * Instead, we just continue reading. 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 /* If a 0 byte packet was sent, return 0, otherwise * we play havoc with people using select with * blocking sockets. Let them handle a packet at a time, * they should really be using non-blocking sockets. */ if (s->s2->ract_data_length == 0) return(0); return(ssl2_read(s,buf,len));#endif } else { SSLerr(SSL_F_SSL2_READ,SSL_R_BAD_STATE); return(-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 */ s->packet=s->s2->rbuf; while (newb < (int)n) { clear_sys_error(); if (s->rbio != NULL) { s->rwstate=SSL_READING; i=BIO_read(s->rbio,(char *)&(s->s2->rbuf[off+newb]), max-newb); } else { SSLerr(SSL_F_READ_N,SSL_R_READ_BIO_NOT_SET); i= -1; }#ifdef PKT_DEBUG
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?