It seems like OpenUnix's ld uses LD_LIBRARY_PATH to search for
[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  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer. 
67  *
68  * 2. Redistributions in binary form must reproduce the above copyright
69  *    notice, this list of conditions and the following disclaimer in
70  *    the documentation and/or other materials provided with the
71  *    distribution.
72  *
73  * 3. All advertising materials mentioning features or use of this
74  *    software must display the following acknowledgment:
75  *    "This product includes software developed by the OpenSSL Project
76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77  *
78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79  *    endorse or promote products derived from this software without
80  *    prior written permission. For written permission, please contact
81  *    openssl-core@openssl.org.
82  *
83  * 5. Products derived from this software may not be called "OpenSSL"
84  *    nor may "OpenSSL" appear in their names without prior written
85  *    permission of the OpenSSL Project.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103  * OF THE POSSIBILITY OF SUCH DAMAGE.
104  * ====================================================================
105  *
106  * This product includes cryptographic software written by Eric Young
107  * (eay@cryptsoft.com).  This product includes software written by Tim
108  * Hudson (tjh@cryptsoft.com).
109  *
110  */
111
112 #include "ssl_locl.h"
113 #ifndef OPENSSL_NO_SSL2
114 #include <stdio.h>
115 #include <errno.h>
116 #include "cryptlib.h"
117 #define USE_SOCKETS
118
119 static int read_n(SSL *s,unsigned int n,unsigned int max,unsigned int extend);
120 static int do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len);
121 static int write_pending(SSL *s, const unsigned char *buf, unsigned int len);
122 static int ssl_mt_error(int n);
123
124
125 /* SSL 2.0 imlementation for SSL_read/SSL_peek -
126  * This routine will return 0 to len bytes, decrypted etc if required.
127  */
128 static int ssl2_read_internal(SSL *s, void *buf, int len, int peek)
129         {
130         int n;
131         unsigned char mac[MAX_MAC_SIZE];
132         unsigned char *p;
133         int i;
134         unsigned int mac_size;
135
136  ssl2_read_again:
137         if (SSL_in_init(s) && !s->in_handshake)
138                 {
139                 n=s->handshake_func(s);
140                 if (n < 0) return(n);
141                 if (n == 0)
142                         {
143                         SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_SSL_HANDSHAKE_FAILURE);
144                         return(-1);
145                         }
146                 }
147
148         clear_sys_error();
149         s->rwstate=SSL_NOTHING;
150         if (len <= 0) return(len);
151
152         if (s->s2->ract_data_length != 0) /* read from buffer */
153                 {
154                 if (len > s->s2->ract_data_length)
155                         n=s->s2->ract_data_length;
156                 else
157                         n=len;
158
159                 memcpy(buf,s->s2->ract_data,(unsigned int)n);
160                 if (!peek)
161                         {
162                         s->s2->ract_data_length-=n;
163                         s->s2->ract_data+=n;
164                         if (s->s2->ract_data_length == 0)
165                                 s->rstate=SSL_ST_READ_HEADER;
166                         }
167
168                 return(n);
169                 }
170
171         /* s->s2->ract_data_length == 0
172          * 
173          * Fill the buffer, then goto ssl2_read_again.
174          */
175
176         if (s->rstate == SSL_ST_READ_HEADER)
177                 {
178                 if (s->first_packet)
179                         {
180                         n=read_n(s,5,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0);
181                         if (n <= 0) return(n); /* error or non-blocking */
182                         s->first_packet=0;
183                         p=s->packet;
184                         if (!((p[0] & 0x80) && (
185                                 (p[2] == SSL2_MT_CLIENT_HELLO) ||
186                                 (p[2] == SSL2_MT_SERVER_HELLO))))
187                                 {
188                                 SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_NON_SSLV2_INITIAL_PACKET);
189                                 return(-1);
190                                 }
191                         }
192                 else
193                         {
194                         n=read_n(s,2,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0);
195                         if (n <= 0) return(n); /* error or non-blocking */
196                         }
197                 /* part read stuff */
198
199                 s->rstate=SSL_ST_READ_BODY;
200                 p=s->packet;
201                 /* Do header */
202                 /*s->s2->padding=0;*/
203                 s->s2->escape=0;
204                 s->s2->rlength=(((unsigned int)p[0])<<8)|((unsigned int)p[1]);
205                 if ((p[0] & TWO_BYTE_BIT))              /* Two byte header? */
206                         {
207                         s->s2->three_byte_header=0;
208                         s->s2->rlength&=TWO_BYTE_MASK;  
209                         }
210                 else
211                         {
212                         s->s2->three_byte_header=1;
213                         s->s2->rlength&=THREE_BYTE_MASK;
214
215                         /* security >s2->escape */
216                         s->s2->escape=((p[0] & SEC_ESC_BIT))?1:0;
217                         }
218                 }
219
220         if (s->rstate == SSL_ST_READ_BODY)
221                 {
222                 n=s->s2->rlength+2+s->s2->three_byte_header;
223                 if (n > (int)s->packet_length)
224                         {
225                         n-=s->packet_length;
226                         i=read_n(s,(unsigned int)n,(unsigned int)n,1);
227                         if (i <= 0) return(i); /* ERROR */
228                         }
229
230                 p= &(s->packet[2]);
231                 s->rstate=SSL_ST_READ_HEADER;
232                 if (s->s2->three_byte_header)
233                         s->s2->padding= *(p++);
234                 else    s->s2->padding=0;
235
236                 /* Data portion */
237                 if (s->s2->clear_text)
238                         {
239                         mac_size = 0;
240                         s->s2->mac_data=p;
241                         s->s2->ract_data=p;
242                         if (s->s2->padding)
243                                 {
244                                 SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_ILLEGAL_PADDING);
245                                 return(-1);
246                                 }
247                         }
248                 else
249                         {
250                         mac_size=EVP_MD_size(s->read_hash);
251                         OPENSSL_assert(mac_size <= MAX_MAC_SIZE);
252                         s->s2->mac_data=p;
253                         s->s2->ract_data= &p[mac_size];
254                         if (s->s2->padding + mac_size > s->s2->rlength)
255                                 {
256                                 SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_ILLEGAL_PADDING);
257                                 return(-1);
258                                 }
259                         }
260
261                 s->s2->ract_data_length=s->s2->rlength;
262                 /* added a check for length > max_size in case
263                  * encryption was not turned on yet due to an error */
264                 if ((!s->s2->clear_text) &&
265                         (s->s2->rlength >= mac_size))
266                         {
267                         ssl2_enc(s,0);
268                         s->s2->ract_data_length-=mac_size;
269                         ssl2_mac(s,mac,0);
270                         s->s2->ract_data_length-=s->s2->padding;
271                         if (    (memcmp(mac,s->s2->mac_data,
272                                 (unsigned int)mac_size) != 0) ||
273                                 (s->s2->rlength%EVP_CIPHER_CTX_block_size(s->enc_read_ctx) != 0))
274                                 {
275                                 SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_MAC_DECODE);
276                                 return(-1);
277                                 }
278                         }
279                 INC32(s->s2->read_sequence); /* expect next number */
280                 /* s->s2->ract_data is now available for processing */
281
282                 /* Possibly the packet that we just read had 0 actual data bytes.
283                  * (SSLeay/OpenSSL itself never sends such packets; see ssl2_write.)
284                  * In this case, returning 0 would be interpreted by the caller
285                  * as indicating EOF, so it's not a good idea.  Instead, we just
286                  * continue reading; thus ssl2_read_internal may have to process
287                  * multiple packets before it can return.
288                  *
289                  * [Note that using select() for blocking sockets *never* guarantees
290                  * that the next SSL_read will not block -- the available
291                  * data may contain incomplete packets, and except for SSL 2,
292                  * renegotiation can confuse things even more.] */
293
294                 goto ssl2_read_again; /* This should really be
295                                        * "return ssl2_read(s,buf,len)",
296                                        * but that would allow for
297                                        * denial-of-service attacks if a
298                                        * C compiler is used that does not
299                                        * recognize end-recursion. */
300                 }
301         else
302                 {
303                 SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_STATE);
304                         return(-1);
305                 }
306         }
307
308 int ssl2_read(SSL *s, void *buf, int len)
309         {
310         return ssl2_read_internal(s, buf, len, 0);
311         }
312
313 int ssl2_peek(SSL *s, void *buf, int len)
314         {
315         return ssl2_read_internal(s, buf, len, 1);
316         }
317
318 static int read_n(SSL *s, unsigned int n, unsigned int max,
319              unsigned int extend)
320         {
321         int i,off,newb;
322
323         /* if there is stuff still in the buffer from a previous read,
324          * and there is more than we want, take some. */
325         if (s->s2->rbuf_left >= (int)n)
326                 {
327                 if (extend)
328                         s->packet_length+=n;
329                 else
330                         {
331                         s->packet= &(s->s2->rbuf[s->s2->rbuf_offs]);
332                         s->packet_length=n;
333                         }
334                 s->s2->rbuf_left-=n;
335                 s->s2->rbuf_offs+=n;
336                 return(n);
337                 }
338
339         if (!s->read_ahead) max=n;
340         if (max > (unsigned int)(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2))
341                 max=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2;
342         
343
344         /* Else we want more than we have.
345          * First, if there is some left or we want to extend */
346         off=0;
347         if ((s->s2->rbuf_left != 0) || ((s->packet_length != 0) && extend))
348                 {
349                 newb=s->s2->rbuf_left;
350                 if (extend)
351                         {
352                         off=s->packet_length;
353                         if (s->packet != s->s2->rbuf)
354                                 memcpy(s->s2->rbuf,s->packet,
355                                         (unsigned int)newb+off);
356                         }
357                 else if (s->s2->rbuf_offs != 0)
358                         {
359                         memcpy(s->s2->rbuf,&(s->s2->rbuf[s->s2->rbuf_offs]),
360                                 (unsigned int)newb);
361                         s->s2->rbuf_offs=0;
362                         }
363                 s->s2->rbuf_left=0;
364                 }
365         else
366                 newb=0;
367
368         /* off is the offset to start writing too.
369          * r->s2->rbuf_offs is the 'unread data', now 0. 
370          * newb is the number of new bytes so far
371          */
372         s->packet=s->s2->rbuf;
373         while (newb < (int)n)
374                 {
375                 clear_sys_error();
376                 if (s->rbio != NULL)
377                         {
378                         s->rwstate=SSL_READING;
379                         i=BIO_read(s->rbio,(char *)&(s->s2->rbuf[off+newb]),
380                                 max-newb);
381                         }
382                 else
383                         {
384                         SSLerr(SSL_F_READ_N,SSL_R_READ_BIO_NOT_SET);
385                         i= -1;
386                         }
387 #ifdef PKT_DEBUG
388                 if (s->debug & 0x01) sleep(1);
389 #endif
390                 if (i <= 0)
391                         {
392                         s->s2->rbuf_left+=newb;
393                         return(i);
394                         }
395                 newb+=i;
396                 }
397
398         /* record unread data */
399         if (newb > (int)n)
400                 {
401                 s->s2->rbuf_offs=n+off;
402                 s->s2->rbuf_left=newb-n;
403                 }
404         else
405                 {
406                 s->s2->rbuf_offs=0;
407                 s->s2->rbuf_left=0;
408                 }
409         if (extend)
410                 s->packet_length+=n;
411         else
412                 s->packet_length=n;
413         s->rwstate=SSL_NOTHING;
414         return(n);
415         }
416
417 int ssl2_write(SSL *s, const void *_buf, int len)
418         {
419         const unsigned char *buf=_buf;
420         unsigned int n,tot;
421         int i;
422
423         if (SSL_in_init(s) && !s->in_handshake)
424                 {
425                 i=s->handshake_func(s);
426                 if (i < 0) return(i);
427                 if (i == 0)
428                         {
429                         SSLerr(SSL_F_SSL2_WRITE,SSL_R_SSL_HANDSHAKE_FAILURE);
430                         return(-1);
431                         }
432                 }
433
434         if (s->error)
435                 {
436                 ssl2_write_error(s);
437                 if (s->error)
438                         return(-1);
439                 }
440
441         clear_sys_error();
442         s->rwstate=SSL_NOTHING;
443         if (len <= 0) return(len);
444
445         tot=s->s2->wnum;
446         s->s2->wnum=0;
447
448         n=(len-tot);
449         for (;;)
450                 {
451                 i=do_ssl_write(s,&(buf[tot]),n);
452                 if (i <= 0)
453                         {
454                         s->s2->wnum=tot;
455                         return(i);
456                         }
457                 if ((i == (int)n) ||
458                         (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))
459                         {
460                         return(tot+i);
461                         }
462                 
463                 n-=i;
464                 tot+=i;
465                 }
466         }
467
468 static int write_pending(SSL *s, const unsigned char *buf, unsigned int len)
469         {
470         int i;
471
472         /* s->s2->wpend_len != 0 MUST be true. */
473
474         /* check that they have given us the same buffer to
475          * write */
476         if ((s->s2->wpend_tot > (int)len) ||
477                 ((s->s2->wpend_buf != buf) &&
478                  !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)))
479                 {
480                 SSLerr(SSL_F_WRITE_PENDING,SSL_R_BAD_WRITE_RETRY);
481                 return(-1);
482                 }
483
484         for (;;)
485                 {
486                 clear_sys_error();
487                 if (s->wbio != NULL)
488                         {
489                         s->rwstate=SSL_WRITING;
490                         i=BIO_write(s->wbio,
491                                 (char *)&(s->s2->write_ptr[s->s2->wpend_off]),
492                                 (unsigned int)s->s2->wpend_len);
493                         }
494                 else
495                         {
496                         SSLerr(SSL_F_WRITE_PENDING,SSL_R_WRITE_BIO_NOT_SET);
497                         i= -1;
498                         }
499 #ifdef PKT_DEBUG
500                 if (s->debug & 0x01) sleep(1);
501 #endif
502                 if (i == s->s2->wpend_len)
503                         {
504                         s->s2->wpend_len=0;
505                         s->rwstate=SSL_NOTHING;
506                         return(s->s2->wpend_ret);
507                         }
508                 else if (i <= 0)
509                         return(i);
510                 s->s2->wpend_off+=i;
511                 s->s2->wpend_len-=i;
512                 }
513         }
514
515 static int do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len)
516         {
517         unsigned int j,k,olen,p,mac_size,bs;
518         register unsigned char *pp;
519
520         olen=len;
521
522         /* first check if there is data from an encryption waiting to
523          * be sent - it must be sent because the other end is waiting.
524          * This will happen with non-blocking IO.  We print it and then
525          * return.
526          */
527         if (s->s2->wpend_len != 0) return(write_pending(s,buf,len));
528
529         /* set mac_size to mac size */
530         if (s->s2->clear_text)
531                 mac_size=0;
532         else
533                 mac_size=EVP_MD_size(s->write_hash);
534
535         /* lets set the pad p */
536         if (s->s2->clear_text)
537                 {
538                 if (len > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
539                         len=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER;
540                 p=0;
541                 s->s2->three_byte_header=0;
542                 /* len=len; */
543                 }
544         else
545                 {
546                 bs=EVP_CIPHER_CTX_block_size(s->enc_read_ctx);
547                 j=len+mac_size;
548                 /* Two-byte headers allow for a larger record length than
549                  * three-byte headers, but we can't use them if we need
550                  * padding or if we have to set the escape bit. */
551                 if ((j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) &&
552                         (!s->s2->escape))
553                         {
554                         if (j > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
555                                 j=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER;
556                         /* set k to the max number of bytes with 2
557                          * byte header */
558                         k=j-(j%bs);
559                         /* how many data bytes? */
560                         len=k-mac_size; 
561                         s->s2->three_byte_header=0;
562                         p=0;
563                         }
564                 else if ((bs <= 1) && (!s->s2->escape))
565                         {
566                         /* j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, thus
567                          * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER */
568                         s->s2->three_byte_header=0;
569                         p=0;
570                         }
571                 else /* we may have to use a 3 byte header */
572                         {
573                         /* If s->s2->escape is not set, then
574                          * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, and thus
575                          * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER. */
576                         p=(j%bs);
577                         p=(p == 0)?0:(bs-p);
578                         if (s->s2->escape)
579                                 {
580                                 s->s2->three_byte_header=1;
581                                 if (j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
582                                         j=SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER;
583                                 }
584                         else
585                                 s->s2->three_byte_header=(p == 0)?0:1;
586                         }
587                 }
588
589         /* Now
590          *      j <= SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER
591          * holds, and if s->s2->three_byte_header is set, then even
592          *      j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER.
593          */
594
595         /* mac_size is the number of MAC bytes
596          * len is the number of data bytes we are going to send
597          * p is the number of padding bytes
598          * (if it is a two-byte header, then p == 0) */
599
600         s->s2->wlength=len;
601         s->s2->padding=p;
602         s->s2->mac_data= &(s->s2->wbuf[3]);
603         s->s2->wact_data= &(s->s2->wbuf[3+mac_size]);
604         /* we copy the data into s->s2->wbuf */
605         memcpy(s->s2->wact_data,buf,len);
606         if (p)
607                 memset(&(s->s2->wact_data[len]),0,p); /* arbitrary padding */
608
609         if (!s->s2->clear_text)
610                 {
611                 s->s2->wact_data_length=len+p;
612                 ssl2_mac(s,s->s2->mac_data,1);
613                 s->s2->wlength+=p+mac_size;
614                 ssl2_enc(s,1);
615                 }
616
617         /* package up the header */
618         s->s2->wpend_len=s->s2->wlength;
619         if (s->s2->three_byte_header) /* 3 byte header */
620                 {
621                 pp=s->s2->mac_data;
622                 pp-=3;
623                 pp[0]=(s->s2->wlength>>8)&(THREE_BYTE_MASK>>8);
624                 if (s->s2->escape) pp[0]|=SEC_ESC_BIT;
625                 pp[1]=s->s2->wlength&0xff;
626                 pp[2]=s->s2->padding;
627                 s->s2->wpend_len+=3;
628                 }
629         else
630                 {
631                 pp=s->s2->mac_data;
632                 pp-=2;
633                 pp[0]=((s->s2->wlength>>8)&(TWO_BYTE_MASK>>8))|TWO_BYTE_BIT;
634                 pp[1]=s->s2->wlength&0xff;
635                 s->s2->wpend_len+=2;
636                 }
637         s->s2->write_ptr=pp;
638         
639         INC32(s->s2->write_sequence); /* expect next number */
640
641         /* lets try to actually write the data */
642         s->s2->wpend_tot=olen;
643         s->s2->wpend_buf=buf;
644
645         s->s2->wpend_ret=len;
646
647         s->s2->wpend_off=0;
648         return(write_pending(s,buf,olen));
649         }
650
651 int ssl2_part_read(SSL *s, unsigned long f, int i)
652         {
653         unsigned char *p;
654         int j;
655
656         if (i < 0)
657                 {
658                 /* ssl2_return_error(s); */
659                 /* for non-blocking io,
660                  * this is not necessarily fatal */
661                 return(i);
662                 }
663         else
664                 {
665                 s->init_num+=i;
666
667                 /* Check for error.  While there are recoverable errors,
668                  * this function is not called when those must be expected;
669                  * any error detected here is fatal. */
670                 if (s->init_num >= 3)
671                         {
672                         p=(unsigned char *)s->init_buf->data;
673                         if (p[0] == SSL2_MT_ERROR)
674                                 {
675                                 j=(p[1]<<8)|p[2];
676                                 SSLerr((int)f,ssl_mt_error(j));
677                                 s->init_num -= 3;
678                                 if (s->init_num > 0)
679                                         memmove(p, p+3, s->init_num);
680                                 }
681                         }
682
683                 /* If it's not an error message, we have some error anyway --
684                  * the message was shorter than expected.  This too is treated
685                  * as fatal (at least if SSL_get_error is asked for its opinion). */
686                 return(0);
687                 }
688         }
689
690 int ssl2_do_write(SSL *s)
691         {
692         int ret;
693
694         ret=ssl2_write(s,&s->init_buf->data[s->init_off],s->init_num);
695         if (ret == s->init_num)
696                 {
697                 if (s->msg_callback)
698                         s->msg_callback(1, s->version, 0, s->init_buf->data, (size_t)(s->init_off + s->init_num), s, s->msg_callback_arg);
699                 return(1);
700                 }
701         if (ret < 0)
702                 return(-1);
703         s->init_off+=ret;
704         s->init_num-=ret;
705         return(0);
706         }
707
708 static int ssl_mt_error(int n)
709         {
710         int ret;
711
712         switch (n)
713                 {
714         case SSL2_PE_NO_CIPHER:
715                 ret=SSL_R_PEER_ERROR_NO_CIPHER;
716                 break;
717         case SSL2_PE_NO_CERTIFICATE:
718                 ret=SSL_R_PEER_ERROR_NO_CERTIFICATE;
719                 break;
720         case SSL2_PE_BAD_CERTIFICATE:
721                 ret=SSL_R_PEER_ERROR_CERTIFICATE;
722                 break;
723         case SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE:
724                 ret=SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE;
725                 break;
726         default:
727                 ret=SSL_R_UNKNOWN_REMOTE_ERROR_TYPE;
728                 break;
729                 }
730         return(ret);
731         }
732 #else /* !OPENSSL_NO_SSL2 */
733
734 # if PEDANTIC
735 static void *dummy=&dummy;
736 # endif
737
738 #endif