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