s2_pkt.c revision 89840
1286441Srpaulo/* ssl/s2_pkt.c */
2286441Srpaulo/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3286441Srpaulo * All rights reserved.
4286441Srpaulo *
5286441Srpaulo * This package is an SSL implementation written
6286441Srpaulo * by Eric Young (eay@cryptsoft.com).
7286441Srpaulo * The implementation was written so as to conform with Netscapes SSL.
8286441Srpaulo *
9286441Srpaulo * This library is free for commercial and non-commercial use as long as
10286441Srpaulo * the following conditions are aheared to.  The following conditions
11286441Srpaulo * apply to all code found in this distribution, be it the RC4, RSA,
12286441Srpaulo * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13286441Srpaulo * included with this distribution is covered by the same copyright terms
14286441Srpaulo * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15286441Srpaulo *
16286441Srpaulo * Copyright remains Eric Young's, and as such any Copyright notices in
17286441Srpaulo * the code are not to be removed.
18286441Srpaulo * If this package is used in a product, Eric Young should be given attribution
19286441Srpaulo * as the author of the parts of the library used.
20286441Srpaulo * This can be in the form of a textual message at program startup or
21286441Srpaulo * in documentation (online or textual) provided with the package.
22286441Srpaulo *
23286441Srpaulo * Redistribution and use in source and binary forms, with or without
24286441Srpaulo * modification, are permitted provided that the following conditions
25286441Srpaulo * are met:
26286441Srpaulo * 1. Redistributions of source code must retain the copyright
27286441Srpaulo *    notice, this list of conditions and the following disclaimer.
28286441Srpaulo * 2. Redistributions in binary form must reproduce the above copyright
29286441Srpaulo *    notice, this list of conditions and the following disclaimer in the
30286441Srpaulo *    documentation and/or other materials provided with the distribution.
31286441Srpaulo * 3. All advertising materials mentioning features or use of this software
32286441Srpaulo *    must display the following acknowledgement:
33286441Srpaulo *    "This product includes cryptographic software written by
34330204Seadler *     Eric Young (eay@cryptsoft.com)"
35286441Srpaulo *    The word 'cryptographic' can be left out if the rouines from the library
36286441Srpaulo *    being used are not cryptographic related :-).
37286441Srpaulo * 4. If you include any Windows specific code (or a derivative thereof) from
38286441Srpaulo *    the apps directory (application code) you must include an acknowledgement:
39286441Srpaulo *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40286441Srpaulo *
41286441Srpaulo * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42286441Srpaulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43286441Srpaulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44286441Srpaulo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45286441Srpaulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46286441Srpaulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47286441Srpaulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48286441Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49286441Srpaulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50286441Srpaulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51286441Srpaulo * SUCH DAMAGE.
52286441Srpaulo *
53286441Srpaulo * The licence and distribution terms for any publically available version or
54330204Seadler * derivative of this code cannot be changed.  i.e. this code cannot simply be
55286441Srpaulo * copied and put under another distribution licence
56286441Srpaulo * [including the GNU Public Licence.]
57286441Srpaulo */
58286441Srpaulo/* ====================================================================
59330204Seadler * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
60286441Srpaulo *
61286441Srpaulo * Redistribution and use in source and binary forms, with or without
62286441Srpaulo * modification, are permitted provided that the following conditions
63286441Srpaulo * are met:
64286441Srpaulo *
65286441Srpaulo * 1. Redistributions of source code must retain the above copyright
66286441Srpaulo *    notice, this list of conditions and the following disclaimer.
67286441Srpaulo *
68286441Srpaulo * 2. Redistributions in binary form must reproduce the above copyright
69286441Srpaulo *    notice, this list of conditions and the following disclaimer in
70286441Srpaulo *    the documentation and/or other materials provided with the
71286441Srpaulo *    distribution.
72286441Srpaulo *
73286441Srpaulo * 3. All advertising materials mentioning features or use of this
74286441Srpaulo *    software must display the following acknowledgment:
75286441Srpaulo *    "This product includes software developed by the OpenSSL Project
76286441Srpaulo *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77286441Srpaulo *
78286441Srpaulo * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79286441Srpaulo *    endorse or promote products derived from this software without
80286441Srpaulo *    prior written permission. For written permission, please contact
81286441Srpaulo *    openssl-core@openssl.org.
82286441Srpaulo *
83286441Srpaulo * 5. Products derived from this software may not be called "OpenSSL"
84286441Srpaulo *    nor may "OpenSSL" appear in their names without prior written
85286441Srpaulo *    permission of the OpenSSL Project.
86286441Srpaulo *
87286441Srpaulo * 6. Redistributions of any form whatsoever must retain the following
88286441Srpaulo *    acknowledgment:
89286441Srpaulo *    "This product includes software developed by the OpenSSL Project
90286441Srpaulo *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91286441Srpaulo *
92300248Savos * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93330455Seadler * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94300248Savos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95286441Srpaulo * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96286441Srpaulo * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97286441Srpaulo * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98286441Srpaulo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99286441Srpaulo * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100286441Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101286441Srpaulo * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102286441Srpaulo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103286441Srpaulo * OF THE POSSIBILITY OF SUCH DAMAGE.
104286441Srpaulo * ====================================================================
105286441Srpaulo *
106286441Srpaulo * This product includes cryptographic software written by Eric Young
107286441Srpaulo * (eay@cryptsoft.com).  This product includes software written by Tim
108286441Srpaulo * Hudson (tjh@cryptsoft.com).
109286441Srpaulo *
110286441Srpaulo * $FreeBSD: head/crypto/openssl/ssl/s2_pkt.c 89840 2002-01-27 03:17:13Z kris $
111286441Srpaulo */
112286441Srpaulo
113286441Srpaulo#include "ssl_locl.h"
114286441Srpaulo#ifndef NO_SSL2
115286441Srpaulo#include <stdio.h>
116286441Srpaulo#include <errno.h>
117286441Srpaulo#define USE_SOCKETS
118286441Srpaulo
119286441Srpaulostatic int read_n(SSL *s,unsigned int n,unsigned int max,unsigned int extend);
120286441Srpaulostatic int do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len);
121286441Srpaulostatic int write_pending(SSL *s, const unsigned char *buf, unsigned int len);
122286441Srpaulostatic int ssl_mt_error(int n);
123286441Srpaulo
124286441Srpaulo
125286441Srpaulo/* SSL 2.0 imlementation for SSL_read/SSL_peek -
126286441Srpaulo * This routine will return 0 to len bytes, decrypted etc if required.
127286441Srpaulo */
128286441Srpaulostatic int ssl2_read_internal(SSL *s, void *buf, int len, int peek)
129286441Srpaulo	{
130286441Srpaulo	int n;
131286441Srpaulo	unsigned char mac[MAX_MAC_SIZE];
132286441Srpaulo	unsigned char *p;
133286441Srpaulo	int i;
134286441Srpaulo	unsigned int mac_size;
135286441Srpaulo
136286441Srpaulo ssl2_read_again:
137286441Srpaulo	if (SSL_in_init(s) && !s->in_handshake)
138286475Srpaulo		{
139286475Srpaulo		n=s->handshake_func(s);
140286475Srpaulo		if (n < 0) return(n);
141286475Srpaulo		if (n == 0)
142286475Srpaulo			{
143286441Srpaulo			SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_SSL_HANDSHAKE_FAILURE);
144286441Srpaulo			return(-1);
145286441Srpaulo			}
146286441Srpaulo		}
147286441Srpaulo
148330204Seadler	clear_sys_error();
149330204Seadler	s->rwstate=SSL_NOTHING;
150330204Seadler	if (len <= 0) return(len);
151330204Seadler
152330204Seadler	if (s->s2->ract_data_length != 0) /* read from buffer */
153330204Seadler		{
154330204Seadler		if (len > s->s2->ract_data_length)
155330204Seadler			n=s->s2->ract_data_length;
156330204Seadler		else
157330204Seadler			n=len;
158330204Seadler
159330204Seadler		memcpy(buf,s->s2->ract_data,(unsigned int)n);
160286441Srpaulo		if (!peek)
161286441Srpaulo			{
162330204Seadler			s->s2->ract_data_length-=n;
163286441Srpaulo			s->s2->ract_data+=n;
164286441Srpaulo			if (s->s2->ract_data_length == 0)
165286441Srpaulo				s->rstate=SSL_ST_READ_HEADER;
166286441Srpaulo			}
167286441Srpaulo
168286441Srpaulo		return(n);
169286441Srpaulo		}
170286441Srpaulo
171286441Srpaulo	/* s->s2->ract_data_length == 0
172286441Srpaulo	 *
173330204Seadler	 * Fill the buffer, then goto ssl2_read_again.
174286441Srpaulo	 */
175330204Seadler
176330204Seadler	if (s->rstate == SSL_ST_READ_HEADER)
177330204Seadler		{
178286441Srpaulo		if (s->first_packet)
179286441Srpaulo			{
180286441Srpaulo			n=read_n(s,5,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0);
181286441Srpaulo			if (n <= 0) return(n); /* error or non-blocking */
182286441Srpaulo			s->first_packet=0;
183330204Seadler			p=s->packet;
184330204Seadler			if (!((p[0] & 0x80) && (
185286441Srpaulo				(p[2] == SSL2_MT_CLIENT_HELLO) ||
186286441Srpaulo				(p[2] == SSL2_MT_SERVER_HELLO))))
187286441Srpaulo				{
188286441Srpaulo				SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_NON_SSLV2_INITIAL_PACKET);
189330204Seadler				return(-1);
190330204Seadler				}
191286441Srpaulo			}
192286441Srpaulo		else
193286441Srpaulo			{
194286441Srpaulo			n=read_n(s,2,SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2,0);
195286441Srpaulo			if (n <= 0) return(n); /* error or non-blocking */
196286441Srpaulo			}
197330204Seadler		/* part read stuff */
198330204Seadler
199330204Seadler		s->rstate=SSL_ST_READ_BODY;
200286441Srpaulo		p=s->packet;
201330204Seadler		/* Do header */
202330204Seadler		/*s->s2->padding=0;*/
203330204Seadler		s->s2->escape=0;
204330204Seadler		s->s2->rlength=(((unsigned int)p[0])<<8)|((unsigned int)p[1]);
205330204Seadler		if ((p[0] & TWO_BYTE_BIT))		/* Two byte header? */
206330204Seadler			{
207330204Seadler			s->s2->three_byte_header=0;
208330204Seadler			s->s2->rlength&=TWO_BYTE_MASK;
209330204Seadler			}
210330204Seadler		else
211330204Seadler			{
212330204Seadler			s->s2->three_byte_header=1;
213330204Seadler			s->s2->rlength&=THREE_BYTE_MASK;
214330204Seadler
215330204Seadler			/* security >s2->escape */
216330204Seadler			s->s2->escape=((p[0] & SEC_ESC_BIT))?1:0;
217330204Seadler			}
218286441Srpaulo		}
219286441Srpaulo
220286441Srpaulo	if (s->rstate == SSL_ST_READ_BODY)
221330203Seadler		{
222286441Srpaulo		n=s->s2->rlength+2+s->s2->three_byte_header;
223330204Seadler		if (n > (int)s->packet_length)
224330204Seadler			{
225330204Seadler			n-=s->packet_length;
226330204Seadler			i=read_n(s,(unsigned int)n,(unsigned int)n,1);
227330204Seadler			if (i <= 0) return(i); /* ERROR */
228330204Seadler			}
229330204Seadler
230330204Seadler		p= &(s->packet[2]);
231330204Seadler		s->rstate=SSL_ST_READ_HEADER;
232330204Seadler		if (s->s2->three_byte_header)
233330204Seadler			s->s2->padding= *(p++);
234330204Seadler		else	s->s2->padding=0;
235330204Seadler
236286441Srpaulo		/* Data portion */
237330204Seadler		if (s->s2->clear_text)
238330204Seadler			{
239330204Seadler			mac_size = 0;
240330204Seadler			s->s2->mac_data=p;
241330204Seadler			s->s2->ract_data=p;
242330204Seadler			if (s->s2->padding)
243330204Seadler				{
244330204Seadler				SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_ILLEGAL_PADDING);
245330204Seadler				return(-1);
246330204Seadler				}
247330204Seadler			}
248330204Seadler		else
249330204Seadler			{
250330204Seadler			mac_size=EVP_MD_size(s->read_hash);
251330204Seadler			s->s2->mac_data=p;
252330204Seadler			s->s2->ract_data= &p[mac_size];
253330204Seadler			if (s->s2->padding + mac_size > s->s2->rlength)
254330204Seadler				{
255330204Seadler				SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_ILLEGAL_PADDING);
256330204Seadler				return(-1);
257330204Seadler				}
258			}
259
260		s->s2->ract_data_length=s->s2->rlength;
261		/* added a check for length > max_size in case
262		 * encryption was not turned on yet due to an error */
263		if ((!s->s2->clear_text) &&
264			(s->s2->rlength >= mac_size))
265			{
266			ssl2_enc(s,0);
267			s->s2->ract_data_length-=mac_size;
268			ssl2_mac(s,mac,0);
269			s->s2->ract_data_length-=s->s2->padding;
270			if (	(memcmp(mac,s->s2->mac_data,
271				(unsigned int)mac_size) != 0) ||
272				(s->s2->rlength%EVP_CIPHER_CTX_block_size(s->enc_read_ctx) != 0))
273				{
274				SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_MAC_DECODE);
275				return(-1);
276				}
277			}
278		INC32(s->s2->read_sequence); /* expect next number */
279		/* s->s2->ract_data is now available for processing */
280
281		/* Possibly the packet that we just read had 0 actual data bytes.
282		 * (SSLeay/OpenSSL itself never sends such packets; see ssl2_write.)
283		 * In this case, returning 0 would be interpreted by the caller
284		 * as indicating EOF, so it's not a good idea.  Instead, we just
285		 * continue reading; thus ssl2_read_internal may have to process
286		 * multiple packets before it can return.
287		 *
288		 * [Note that using select() for blocking sockets *never* guarantees
289		 * that the next SSL_read will not block -- the available
290		 * data may contain incomplete packets, and except for SSL 2,
291		 * renegotiation can confuse things even more.] */
292
293		goto ssl2_read_again; /* This should really be
294		                       * "return ssl2_read(s,buf,len)",
295		                       * but that would allow for
296		                       * denial-of-service attacks if a
297		                       * C compiler is used that does not
298		                       * recognize end-recursion. */
299		}
300	else
301		{
302		SSLerr(SSL_F_SSL2_READ_INTERNAL,SSL_R_BAD_STATE);
303			return(-1);
304		}
305	}
306
307int ssl2_read(SSL *s, void *buf, int len)
308	{
309	return ssl2_read_internal(s, buf, len, 0);
310	}
311
312int ssl2_peek(SSL *s, void *buf, int len)
313	{
314	return ssl2_read_internal(s, buf, len, 1);
315	}
316
317static int read_n(SSL *s, unsigned int n, unsigned int max,
318	     unsigned int extend)
319	{
320	int i,off,newb;
321
322	/* if there is stuff still in the buffer from a previous read,
323	 * and there is more than we want, take some. */
324	if (s->s2->rbuf_left >= (int)n)
325		{
326		if (extend)
327			s->packet_length+=n;
328		else
329			{
330			s->packet= &(s->s2->rbuf[s->s2->rbuf_offs]);
331			s->packet_length=n;
332			}
333		s->s2->rbuf_left-=n;
334		s->s2->rbuf_offs+=n;
335		return(n);
336		}
337
338	if (!s->read_ahead) max=n;
339	if (max > (unsigned int)(SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2))
340		max=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER+2;
341
342
343	/* Else we want more than we have.
344	 * First, if there is some left or we want to extend */
345	off=0;
346	if ((s->s2->rbuf_left != 0) || ((s->packet_length != 0) && extend))
347		{
348		newb=s->s2->rbuf_left;
349		if (extend)
350			{
351			off=s->packet_length;
352			if (s->packet != s->s2->rbuf)
353				memcpy(s->s2->rbuf,s->packet,
354					(unsigned int)newb+off);
355			}
356		else if (s->s2->rbuf_offs != 0)
357			{
358			memcpy(s->s2->rbuf,&(s->s2->rbuf[s->s2->rbuf_offs]),
359				(unsigned int)newb);
360			s->s2->rbuf_offs=0;
361			}
362		s->s2->rbuf_left=0;
363		}
364	else
365		newb=0;
366
367	/* off is the offset to start writing too.
368	 * r->s2->rbuf_offs is the 'unread data', now 0.
369	 * newb is the number of new bytes so far
370	 */
371	s->packet=s->s2->rbuf;
372	while (newb < (int)n)
373		{
374		clear_sys_error();
375		if (s->rbio != NULL)
376			{
377			s->rwstate=SSL_READING;
378			i=BIO_read(s->rbio,(char *)&(s->s2->rbuf[off+newb]),
379				max-newb);
380			}
381		else
382			{
383			SSLerr(SSL_F_READ_N,SSL_R_READ_BIO_NOT_SET);
384			i= -1;
385			}
386#ifdef PKT_DEBUG
387		if (s->debug & 0x01) sleep(1);
388#endif
389		if (i <= 0)
390			{
391			s->s2->rbuf_left+=newb;
392			return(i);
393			}
394		newb+=i;
395		}
396
397	/* record unread data */
398	if (newb > (int)n)
399		{
400		s->s2->rbuf_offs=n+off;
401		s->s2->rbuf_left=newb-n;
402		}
403	else
404		{
405		s->s2->rbuf_offs=0;
406		s->s2->rbuf_left=0;
407		}
408	if (extend)
409		s->packet_length+=n;
410	else
411		s->packet_length=n;
412	s->rwstate=SSL_NOTHING;
413	return(n);
414	}
415
416int ssl2_write(SSL *s, const void *_buf, int len)
417	{
418	const unsigned char *buf=_buf;
419	unsigned int n,tot;
420	int i;
421
422	if (SSL_in_init(s) && !s->in_handshake)
423		{
424		i=s->handshake_func(s);
425		if (i < 0) return(i);
426		if (i == 0)
427			{
428			SSLerr(SSL_F_SSL2_WRITE,SSL_R_SSL_HANDSHAKE_FAILURE);
429			return(-1);
430			}
431		}
432
433	if (s->error)
434		{
435		ssl2_write_error(s);
436		if (s->error)
437			return(-1);
438		}
439
440	clear_sys_error();
441	s->rwstate=SSL_NOTHING;
442	if (len <= 0) return(len);
443
444	tot=s->s2->wnum;
445	s->s2->wnum=0;
446
447	n=(len-tot);
448	for (;;)
449		{
450		i=do_ssl_write(s,&(buf[tot]),n);
451		if (i <= 0)
452			{
453			s->s2->wnum=tot;
454			return(i);
455			}
456		if ((i == (int)n) ||
457			(s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))
458			{
459			return(tot+i);
460			}
461
462		n-=i;
463		tot+=i;
464		}
465	}
466
467static int write_pending(SSL *s, const unsigned char *buf, unsigned int len)
468	{
469	int i;
470
471	/* s->s2->wpend_len != 0 MUST be true. */
472
473	/* check that they have given us the same buffer to
474	 * write */
475	if ((s->s2->wpend_tot > (int)len) ||
476		((s->s2->wpend_buf != buf) &&
477		 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)))
478		{
479		SSLerr(SSL_F_WRITE_PENDING,SSL_R_BAD_WRITE_RETRY);
480		return(-1);
481		}
482
483	for (;;)
484		{
485		clear_sys_error();
486		if (s->wbio != NULL)
487			{
488			s->rwstate=SSL_WRITING;
489			i=BIO_write(s->wbio,
490				(char *)&(s->s2->write_ptr[s->s2->wpend_off]),
491				(unsigned int)s->s2->wpend_len);
492			}
493		else
494			{
495			SSLerr(SSL_F_WRITE_PENDING,SSL_R_WRITE_BIO_NOT_SET);
496			i= -1;
497			}
498#ifdef PKT_DEBUG
499		if (s->debug & 0x01) sleep(1);
500#endif
501		if (i == s->s2->wpend_len)
502			{
503			s->s2->wpend_len=0;
504			s->rwstate=SSL_NOTHING;
505			return(s->s2->wpend_ret);
506			}
507		else if (i <= 0)
508			return(i);
509		s->s2->wpend_off+=i;
510		s->s2->wpend_len-=i;
511		}
512	}
513
514static int do_ssl_write(SSL *s, const unsigned char *buf, unsigned int len)
515	{
516	unsigned int j,k,olen,p,mac_size,bs;
517	register unsigned char *pp;
518
519	olen=len;
520
521	/* first check if there is data from an encryption waiting to
522	 * be sent - it must be sent because the other end is waiting.
523	 * This will happen with non-blocking IO.  We print it and then
524	 * return.
525	 */
526	if (s->s2->wpend_len != 0) return(write_pending(s,buf,len));
527
528	/* set mac_size to mac size */
529	if (s->s2->clear_text)
530		mac_size=0;
531	else
532		mac_size=EVP_MD_size(s->write_hash);
533
534	/* lets set the pad p */
535	if (s->s2->clear_text)
536		{
537		if (len > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
538			len=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER;
539		p=0;
540		s->s2->three_byte_header=0;
541		/* len=len; */
542		}
543	else
544		{
545		bs=EVP_CIPHER_CTX_block_size(s->enc_read_ctx);
546		j=len+mac_size;
547		/* Two-byte headers allow for a larger record length than
548		 * three-byte headers, but we can't use them if we need
549		 * padding or if we have to set the escape bit. */
550		if ((j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) &&
551			(!s->s2->escape))
552			{
553			if (j > SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER)
554				j=SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER;
555			/* set k to the max number of bytes with 2
556			 * byte header */
557			k=j-(j%bs);
558			/* how many data bytes? */
559			len=k-mac_size;
560			s->s2->three_byte_header=0;
561			p=0;
562			}
563		else if ((bs <= 1) && (!s->s2->escape))
564			{
565			/* j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, thus
566			 * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER */
567			s->s2->three_byte_header=0;
568			p=0;
569			}
570		else /* we may have to use a 3 byte header */
571			{
572			/* If s->s2->escape is not set, then
573			 * j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER, and thus
574			 * j < SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER. */
575			p=(j%bs);
576			p=(p == 0)?0:(bs-p);
577			if (s->s2->escape)
578				{
579				s->s2->three_byte_header=1;
580				if (j > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
581					j=SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER;
582				}
583			else
584				s->s2->three_byte_header=(p == 0)?0:1;
585			}
586		}
587
588	/* Now
589	 *      j <= SSL2_MAX_RECORD_LENGTH_2_BYTE_HEADER
590	 * holds, and if s->s2->three_byte_header is set, then even
591	 *      j <= SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER.
592	 */
593
594	/* mac_size is the number of MAC bytes
595	 * len is the number of data bytes we are going to send
596	 * p is the number of padding bytes
597	 * (if it is a two-byte header, then p == 0) */
598
599	s->s2->wlength=len;
600	s->s2->padding=p;
601	s->s2->mac_data= &(s->s2->wbuf[3]);
602	s->s2->wact_data= &(s->s2->wbuf[3+mac_size]);
603	/* we copy the data into s->s2->wbuf */
604	memcpy(s->s2->wact_data,buf,len);
605	if (p)
606		memset(&(s->s2->wact_data[len]),0,p); /* arbitrary padding */
607
608	if (!s->s2->clear_text)
609		{
610		s->s2->wact_data_length=len+p;
611		ssl2_mac(s,s->s2->mac_data,1);
612		s->s2->wlength+=p+mac_size;
613		ssl2_enc(s,1);
614		}
615
616	/* package up the header */
617	s->s2->wpend_len=s->s2->wlength;
618	if (s->s2->three_byte_header) /* 3 byte header */
619		{
620		pp=s->s2->mac_data;
621		pp-=3;
622		pp[0]=(s->s2->wlength>>8)&(THREE_BYTE_MASK>>8);
623		if (s->s2->escape) pp[0]|=SEC_ESC_BIT;
624		pp[1]=s->s2->wlength&0xff;
625		pp[2]=s->s2->padding;
626		s->s2->wpend_len+=3;
627		}
628	else
629		{
630		pp=s->s2->mac_data;
631		pp-=2;
632		pp[0]=((s->s2->wlength>>8)&(TWO_BYTE_MASK>>8))|TWO_BYTE_BIT;
633		pp[1]=s->s2->wlength&0xff;
634		s->s2->wpend_len+=2;
635		}
636	s->s2->write_ptr=pp;
637
638	INC32(s->s2->write_sequence); /* expect next number */
639
640	/* lets try to actually write the data */
641	s->s2->wpend_tot=olen;
642	s->s2->wpend_buf=buf;
643
644	s->s2->wpend_ret=len;
645
646	s->s2->wpend_off=0;
647	return(write_pending(s,buf,olen));
648	}
649
650int ssl2_part_read(SSL *s, unsigned long f, int i)
651	{
652	unsigned char *p;
653	int j;
654
655	if (i < 0)
656		{
657		/* ssl2_return_error(s); */
658		/* for non-blocking io,
659		 * this is not necessarily fatal */
660		return(i);
661		}
662	else
663		{
664		s->init_num+=i;
665
666		/* Check for error.  While there are recoverable errors,
667		 * this function is not called when those must be expected;
668		 * any error detected here is fatal. */
669		if (s->init_num >= 3)
670			{
671			p=(unsigned char *)s->init_buf->data;
672			if (p[0] == SSL2_MT_ERROR)
673				{
674				j=(p[1]<<8)|p[2];
675				SSLerr((int)f,ssl_mt_error(j));
676				s->init_num -= 3;
677				if (s->init_num > 0)
678					memmove(p, p+3, s->init_num);
679				}
680			}
681
682		/* If it's not an error message, we have some error anyway --
683		 * the message was shorter than expected.  This too is treated
684		 * as fatal (at least if SSL_get_error is asked for its opinion). */
685		return(0);
686		}
687	}
688
689int ssl2_do_write(SSL *s)
690	{
691	int ret;
692
693	ret=ssl2_write(s,&s->init_buf->data[s->init_off],s->init_num);
694	if (ret == s->init_num)
695		{
696		return(1);
697		}
698	if (ret < 0)
699		return(-1);
700	s->init_off+=ret;
701	s->init_num-=ret;
702	return(0);
703	}
704
705static int ssl_mt_error(int n)
706	{
707	int ret;
708
709	switch (n)
710		{
711	case SSL2_PE_NO_CIPHER:
712		ret=SSL_R_PEER_ERROR_NO_CIPHER;
713		break;
714	case SSL2_PE_NO_CERTIFICATE:
715		ret=SSL_R_PEER_ERROR_NO_CERTIFICATE;
716		break;
717	case SSL2_PE_BAD_CERTIFICATE:
718		ret=SSL_R_PEER_ERROR_CERTIFICATE;
719		break;
720	case SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE:
721		ret=SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE;
722		break;
723	default:
724		ret=SSL_R_UNKNOWN_REMOTE_ERROR_TYPE;
725		break;
726		}
727	return(ret);
728	}
729#else /* !NO_SSL2 */
730
731# if PEDANTIC
732static void *dummy=&dummy;
733# endif
734
735#endif
736