Simplify and provide the possibility to clean a compression method.
[openssl.git] / ssl / s2_pkt.c
1 /* ssl/s2_pkt.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 #include "ssl_locl.h"
60 #ifndef NO_SSL2
61 #include <stdio.h>
62 #include <errno.h>
63 #define USE_SOCKETS
64
65 static int read_n(SSL *s,unsigned int n,unsigned int max,unsigned int extend);
66 static int do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len);
67 static int write_pending(SSL *s, const unsigned char *buf, unsigned int len);
68 static int ssl_mt_error(int n);
69 int ssl2_peek(SSL *s, char *buf, int len)
70         {
71 #if 1
72         SSLerr(SSL_F_SSL2_PEEK, SSL_R_FIXME); /* function is totally broken */
73         return -1;
74 #else
75         int ret;
76
77         ret=ssl2_read(s,buf,len);
78         if (ret > 0)
79                 {
80                 s->s2->ract_data_length+=ret;
81                 s->s2->ract_data-=ret;
82                 }
83         return(ret);
84 #endif
85         }
86
87 /* SSL_read -
88  * This routine will return 0 to len bytes, decrypted etc if required.
89  */
90 int ssl2_read(SSL *s, void *buf, int len)
91         {
92         int n;
93         unsigned char mac[MAX_MAC_SIZE];
94         unsigned char *p;
95         int i;
96         unsigned int mac_size=0;
97
98 ssl2_read_again:
99         if (SSL_in_init(s) && !s->in_handshake)
100                 {
101                 n=s->handshake_func(s);
102                 if (n < 0) return(n);
103                 if (n == 0)
104                         {
105                         SSLerr(SSL_F_SSL2_READ,SSL_R_SSL_HANDSHAKE_FAILURE);
106                         return(-1);
107                         }
108                 }
109
110         clear_sys_error();
111         s->rwstate=SSL_NOTHING;
112         if (len <= 0) return(len);
113
114         if (s->s2->ract_data_length != 0) /* read from buffer */
115                 {
116                 if (len > s->s2->ract_data_length)
117                         n=s->s2->ract_data_length;
118                 else
119                         n=len;
120
121                 memcpy(buf,s->s2->ract_data,(unsigned int)n);
122                 s->s2->ract_data_length-=n;
123                 s->s2->ract_data+=n;
124                 if (s->s2->ract_data_length == 0)
125                         s->rstate=SSL_ST_READ_HEADER;
126                 return(n);
127                 }
128
129         if (s->rstate == SSL_ST_READ_HEADER)
130                 {
131                 if (s->first_packet)
132                         {
133                         n=read_n(s,5,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0);
134                         if (n <= 0) return(n); /* error or non-blocking */
135                         s->first_packet=0;
136                         p=s->packet;
137                         if (!((p[0] & 0x80) && (
138                                 (p[2] == SSL2_MT_CLIENT_HELLO) ||
139                                 (p[2] == SSL2_MT_SERVER_HELLO))))
140                                 {
141                                 SSLerr(SSL_F_SSL2_READ,SSL_R_NON_SSLV2_INITIAL_PACKET);
142                                 return(-1);
143                                 }
144                         }
145                 else
146                         {
147                         n=read_n(s,2,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0);
148                         if (n <= 0) return(n); /* error or non-blocking */
149                         }
150                 /* part read stuff */
151
152                 s->rstate=SSL_ST_READ_BODY;
153                 p=s->packet;
154                 /* Do header */
155                 /*s->s2->padding=0;*/
156                 s->s2->escape=0;
157                 s->s2->rlength=(((unsigned int)p[0])<<8)|((unsigned int)p[1]);
158                 if ((p[0] & TWO_BYTE_BIT))              /* Two byte header? */
159                         {
160                         s->s2->three_byte_header=0;
161                         s->s2->rlength&=TWO_BYTE_MASK;  
162                         }
163                 else
164                         {
165                         s->s2->three_byte_header=1;
166                         s->s2->rlength&=THREE_BYTE_MASK;
167
168                         /* security >s2->escape */
169                         s->s2->escape=((p[0] & SEC_ESC_BIT))?1:0;
170                         }
171                 }
172
173         if (s->rstate == SSL_ST_READ_BODY)
174                 {
175                 n=s->s2->rlength+2+s->s2->three_byte_header;
176                 if (n > (int)s->packet_length)
177                         {
178                         n-=s->packet_length;
179                         i=read_n(s,(unsigned int)n,(unsigned int)n,1);
180                         if (i <= 0) return(i); /* ERROR */
181                         }
182
183                 p= &(s->packet[2]);
184                 s->rstate=SSL_ST_READ_HEADER;
185                 if (s->s2->three_byte_header)
186                         s->s2->padding= *(p++);
187                 else    s->s2->padding=0;
188
189                 /* Data portion */
190                 if (s->s2->clear_text)
191                         {
192                         s->s2->mac_data=p;
193                         s->s2->ract_data=p;
194                         s->s2->pad_data=NULL;
195                         }
196                 else
197                         {
198                         mac_size=EVP_MD_size(s->read_hash);
199                         s->s2->mac_data=p;
200                         s->s2->ract_data= &p[mac_size];
201                         s->s2->pad_data= &p[mac_size+
202                                 s->s2->rlength-s->s2->padding];
203                         }
204
205                 s->s2->ract_data_length=s->s2->rlength;
206                 /* added a check for length > max_size in case
207                  * encryption was not turned on yet due to an error */
208                 if ((!s->s2->clear_text) &&
209                         (s->s2->rlength >= mac_size))
210                         {
211                         ssl2_enc(s,0);
212                         s->s2->ract_data_length-=mac_size;
213                         ssl2_mac(s,mac,0);
214                         s->s2->ract_data_length-=s->s2->padding;
215                         if (    (memcmp(mac,s->s2->mac_data,
216                                 (unsigned int)mac_size) != 0) ||
217                                 (s->s2->rlength%EVP_CIPHER_CTX_block_size(s->enc_read_ctx) != 0))
218                                 {
219                                 SSLerr(SSL_F_SSL2_READ,SSL_R_BAD_MAC_DECODE);
220                                 return(-1);
221                                 }
222                         }
223                 INC32(s->s2->read_sequence); /* expect next number */
224                 /* s->s2->ract_data is now available for processing */
225
226 #if 1
227                 /* How should we react when a packet containing 0
228                  * bytes is received?  (Note that SSLeay/OpenSSL itself
229                  * never sends such packets; see ssl2_write.)
230                  * Returning 0 would be interpreted by the caller as
231                  * indicating EOF, so it's not a good idea.
232                  * Instead, we just continue reading.  Note that using
233                  * select() for blocking sockets *never* guarantees
234                  * that the next SSL_read will not block -- the available
235                  * data may contain incomplete packets, and except for SSL 2
236                  * renegotiation can confuse things even more. */
237
238                 goto ssl2_read_again; /* This should really be
239                                        * "return ssl2_read(s,buf,len)",
240                                        * but that would allow for
241                                        * denial-of-service attacks if a
242                                        * C compiler is used that does not
243                                        * recognize end-recursion. */
244 #else
245                 /* If a 0 byte packet was sent, return 0, otherwise
246                  * we play havoc with people using select with
247                  * blocking sockets.  Let them handle a packet at a time,
248                  * they should really be using non-blocking sockets. */
249                 if (s->s2->ract_data_length == 0)
250                         return(0);
251                 return(ssl2_read(s,buf,len));
252 #endif
253                 }
254         else
255                 {
256                 SSLerr(SSL_F_SSL2_READ,SSL_R_BAD_STATE);
257                         return(-1);
258                 }
259         }
260
261 static int read_n(SSL *s, unsigned int n, unsigned int max,
262              unsigned int extend)
263         {
264         int i,off,newb;
265
266         /* if there is stuff still in the buffer from a previous read,
267          * and there is more than we want, take some. */
268         if (s->s2->rbuf_left >= (int)n)
269                 {
270                 if (extend)
271                         s->packet_length+=n;
272                 else
273                         {
274                         s->packet= &(s->s2->rbuf[s->s2->rbuf_offs]);
275                         s->packet_length=n;
276                         }
277                 s->s2->rbuf_left-=n;
278                 s->s2->rbuf_offs+=n;
279                 return(n);
280                 }
281
282         if (!s->read_ahead) max=n;
283         if (max > (unsigned int)(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2))
284                 max=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2;
285         
286
287         /* Else we want more than we have.
288          * First, if there is some left or we want to extend */
289         off=0;
290         if ((s->s2->rbuf_left != 0) || ((s->packet_length != 0) && extend))
291                 {
292                 newb=s->s2->rbuf_left;
293                 if (extend)
294                         {
295                         off=s->packet_length;
296                         if (s->packet != s->s2->rbuf)
297                                 memcpy(s->s2->rbuf,s->packet,
298                                         (unsigned int)newb+off);
299                         }
300                 else if (s->s2->rbuf_offs != 0)
301                         {
302                         memcpy(s->s2->rbuf,&(s->s2->rbuf[s->s2->rbuf_offs]),
303                                 (unsigned int)newb);
304                         s->s2->rbuf_offs=0;
305                         }
306                 s->s2->rbuf_left=0;
307                 }
308         else
309                 newb=0;
310
311         /* off is the offset to start writing too.
312          * r->s2->rbuf_offs is the 'unread data', now 0. 
313          * newb is the number of new bytes so far
314          */
315         s->packet=s->s2->rbuf;
316         while (newb < (int)n)
317                 {
318                 clear_sys_error();
319                 if (s->rbio != NULL)
320                         {
321                         s->rwstate=SSL_READING;
322                         i=BIO_read(s->rbio,(char *)&(s->s2->rbuf[off+newb]),
323                                 max-newb);
324                         }
325                 else
326                         {
327                         SSLerr(SSL_F_READ_N,SSL_R_READ_BIO_NOT_SET);
328                         i= -1;
329                         }
330 #ifdef PKT_DEBUG
331                 if (s->debug & 0x01) sleep(1);
332 #endif
333                 if (i <= 0)
334                         {
335                         s->s2->rbuf_left+=newb;
336                         return(i);
337                         }
338                 newb+=i;
339                 }
340
341         /* record unread data */
342         if (newb > (int)n)
343                 {
344                 s->s2->rbuf_offs=n+off;
345                 s->s2->rbuf_left=newb-n;
346                 }
347         else
348                 {
349                 s->s2->rbuf_offs=0;
350                 s->s2->rbuf_left=0;
351                 }
352         if (extend)
353                 s->packet_length+=n;
354         else
355                 s->packet_length=n;
356         s->rwstate=SSL_NOTHING;
357         return(n);
358         }
359
360 int ssl2_write(SSL *s, const void *_buf, int len)
361         {
362         const unsigned char *buf=_buf;
363         unsigned int n,tot;
364         int i;
365
366         if (SSL_in_init(s) && !s->in_handshake)
367                 {
368                 i=s->handshake_func(s);
369                 if (i < 0) return(i);
370                 if (i == 0)
371                         {
372                         SSLerr(SSL_F_SSL2_WRITE,SSL_R_SSL_HANDSHAKE_FAILURE);
373                         return(-1);
374                         }
375                 }
376
377         if (s->error)
378                 {
379                 ssl2_write_error(s);
380                 if (s->error)
381                         return(-1);
382                 }
383
384         clear_sys_error();
385         s->rwstate=SSL_NOTHING;
386         if (len <= 0) return(len);
387
388         tot=s->s2->wnum;
389         s->s2->wnum=0;
390
391         n=(len-tot);
392         for (;;)
393                 {
394                 i=do_ssl_write(s,&(buf[tot]),n);
395                 if (i <= 0)
396                         {
397                         s->s2->wnum=tot;
398                         return(i);
399                         }
400                 if ((i == (int)n) ||
401                         (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))
402                         {
403                         return(tot+i);
404                         }
405                 
406                 n-=i;
407                 tot+=i;
408                 }
409         }
410
411 static int write_pending(SSL *s, const unsigned char *buf, unsigned int len)
412         {
413         int i;
414
415         /* s->s2->wpend_len != 0 MUST be true. */
416
417         /* check that they have given us the same buffer to
418          * write */
419         if ((s->s2->wpend_tot > (int)len) ||
420                 ((s->s2->wpend_buf != buf) &&
421                  !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)))
422                 {
423                 SSLerr(SSL_F_WRITE_PENDING,SSL_R_BAD_WRITE_RETRY);
424                 return(-1);
425                 }
426
427         for (;;)
428                 {
429                 clear_sys_error();
430                 if (s->wbio != NULL)
431                         {
432                         s->rwstate=SSL_WRITING;
433                         i=BIO_write(s->wbio,
434                                 (char *)&(s->s2->write_ptr[s->s2->wpend_off]),
435                                 (unsigned int)s->s2->wpend_len);
436                         }
437                 else
438                         {
439                         SSLerr(SSL_F_WRITE_PENDING,SSL_R_WRITE_BIO_NOT_SET);
440                         i= -1;
441                         }
442 #ifdef PKT_DEBUG
443                 if (s->debug & 0x01) sleep(1);
444 #endif
445                 if (i == s->s2->wpend_len)
446                         {
447                         s->s2->wpend_len=0;
448                         s->rwstate=SSL_NOTHING;
449                         return(s->s2->wpend_ret);
450                         }
451                 else if (i <= 0)
452                         return(i);
453                 s->s2->wpend_off+=i;
454                 s->s2->wpend_len-=i;
455                 }
456         }
457
458 static int do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len)
459         {
460         unsigned int j,k,olen,p,mac_size,bs;
461         register unsigned char *pp;
462
463         olen=len;
464
465         /* first check if there is data from an encryption waiting to
466          * be sent - it must be sent because the other end is waiting.
467          * This will happen with non-blocking IO.  We print it and then
468          * return.
469          */
470         if (s->s2->wpend_len != 0) return(write_pending(s,buf,len));
471
472         /* set mac_size to mac size */
473         if (s->s2->clear_text)
474                 mac_size=0;
475         else
476                 mac_size=EVP_MD_size(s->write_hash);
477
478         /* lets set the pad p */
479         if (s->s2->clear_text)
480                 {
481                 if (len > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
482                         len=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER;
483                 p=0;
484                 s->s2->three_byte_header=0;
485                 /* len=len; */
486                 }
487         else
488                 {
489                 bs=EVP_CIPHER_CTX_block_size(s->enc_read_ctx);
490                 j=len+mac_size;
491                 if ((j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) &&
492                         (!s->s2->escape))
493                         {
494                         if (j > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
495                                 j=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER;
496                         /* set k to the max number of bytes with 2
497                          * byte header */
498                         k=j-(j%bs);
499                         /* how many data bytes? */
500                         len=k-mac_size; 
501                         s->s2->three_byte_header=0;
502                         p=0;
503                         }
504                 else if ((bs <= 1) && (!s->s2->escape))
505                         {
506                         /* len=len; */
507                         s->s2->three_byte_header=0;
508                         p=0;
509                         }
510                 else /* 3 byte header */
511                         {
512                         /*len=len; */
513                         p=(j%bs);
514                         p=(p == 0)?0:(bs-p);
515                         if (s->s2->escape)
516                                 s->s2->three_byte_header=1;
517                         else
518                                 s->s2->three_byte_header=(p == 0)?0:1;
519                         }
520                 }
521         /* mac_size is the number of MAC bytes
522          * len is the number of data bytes we are going to send
523          * p is the number of padding bytes
524          * if p == 0, it is a 2 byte header */
525
526         s->s2->wlength=len;
527         s->s2->padding=p;
528         s->s2->mac_data= &(s->s2->wbuf[3]);
529         s->s2->wact_data= &(s->s2->wbuf[3+mac_size]);
530         /* we copy the data into s->s2->wbuf */
531         memcpy(s->s2->wact_data,buf,len);
532 #ifdef PURIFY
533         if (p)
534                 memset(&(s->s2->wact_data[len]),0,p);
535 #endif
536
537         if (!s->s2->clear_text)
538                 {
539                 s->s2->wact_data_length=len+p;
540                 ssl2_mac(s,s->s2->mac_data,1);
541                 s->s2->wlength+=p+mac_size;
542                 ssl2_enc(s,1);
543                 }
544
545         /* package up the header */
546         s->s2->wpend_len=s->s2->wlength;
547         if (s->s2->three_byte_header) /* 3 byte header */
548                 {
549                 pp=s->s2->mac_data;
550                 pp-=3;
551                 pp[0]=(s->s2->wlength>>8)&(THREE_BYTE_MASK>>8);
552                 if (s->s2->escape) pp[0]|=SEC_ESC_BIT;
553                 pp[1]=s->s2->wlength&0xff;
554                 pp[2]=s->s2->padding;
555                 s->s2->wpend_len+=3;
556                 }
557         else
558                 {
559                 pp=s->s2->mac_data;
560                 pp-=2;
561                 pp[0]=((s->s2->wlength>>8)&(TWO_BYTE_MASK>>8))|TWO_BYTE_BIT;
562                 pp[1]=s->s2->wlength&0xff;
563                 s->s2->wpend_len+=2;
564                 }
565         s->s2->write_ptr=pp;
566         
567         INC32(s->s2->write_sequence); /* expect next number */
568
569         /* lets try to actually write the data */
570         s->s2->wpend_tot=olen;
571         s->s2->wpend_buf=buf;
572
573         s->s2->wpend_ret=len;
574
575         s->s2->wpend_off=0;
576         return(write_pending(s,buf,olen));
577         }
578
579 int ssl2_part_read(SSL *s, unsigned long f, int i)
580         {
581         unsigned char *p;
582         int j;
583
584         /* check for error */
585         if ((s->init_num == 0) && (i >= 3))
586                 {
587                 p=(unsigned char *)s->init_buf->data;
588                 if (p[0] == SSL2_MT_ERROR)
589                         {
590                         j=(p[1]<<8)|p[2];
591                         SSLerr((int)f,ssl_mt_error(j));
592                         }
593                 }
594
595         if (i < 0)
596                 {
597                 /* ssl2_return_error(s); */
598                 /* for non-blocking io,
599                  * this is not fatal */
600                 return(i);
601                 }
602         else
603                 {
604                 s->init_num+=i;
605                 return(0);
606                 }
607         }
608
609 int ssl2_do_write(SSL *s)
610         {
611         int ret;
612
613         ret=ssl2_write(s,&s->init_buf->data[s->init_off],s->init_num);
614         if (ret == s->init_num)
615                 return(1);
616         if (ret < 0)
617                 return(-1);
618         s->init_off+=ret;
619         s->init_num-=ret;
620         return(0);
621         }
622
623 static int ssl_mt_error(int n)
624         {
625         int ret;
626
627         switch (n)
628                 {
629         case SSL2_PE_NO_CIPHER:
630                 ret=SSL_R_PEER_ERROR_NO_CIPHER;
631                 break;
632         case SSL2_PE_NO_CERTIFICATE:
633                 ret=SSL_R_PEER_ERROR_NO_CERTIFICATE;
634                 break;
635         case SSL2_PE_BAD_CERTIFICATE:
636                 ret=SSL_R_PEER_ERROR_CERTIFICATE;
637                 break;
638         case SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE:
639                 ret=SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE;
640                 break;
641         default:
642                 ret=SSL_R_UNKNOWN_REMOTE_ERROR_TYPE;
643                 break;
644                 }
645         return(ret);
646         }
647 #else /* !NO_SSL2 */
648
649 # if PEDANTIC
650 static void *dummy=&dummy;
651 # endif
652
653 #endif