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