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