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